Today we’ll take a look at how we can leverage Newlib to create a compact C standard library for usage on a bare metal system. In a small example, we’ll implement a few UART primitives and pass them on to Newlib which uses them as the building blocks for a full-blown printf functionality. The target platform will be RISC-V, but the concepts should, as usual, be the same for other platforms as well. Table of contents Open Table of contents Software abstractions and C standard library When running printf on a typical, fully operational, end-user system (e.g., a Mac or a Linux laptop), we invoke a pretty complex machinery. The application process calls the printf function, which is more often than not dynamically linked, and after a few layers of different C functions, a system call to the operating system kernel is typically invoked. The kernel will route the output through different subsystems: different terminal and pseudo-terminal primitives will be invoked, and at some point, you will also want to visually see the printf output on your screen. That also likely invokes a pretty thick stack of abstractions in order to render the characters on your screen. We won’t even talk about how printf formats the output strings based on the provided templates. On a bare metal system, however, most of these abstractions are not available at all and the stack is much thinner. If we’re working on bare metal, we don’t have anything below our C functions supporting us. In the full-blown example above, the process would hand over the output to the kernel through system calls, which are implemented through software interrupts. However, now we don’t have anything to hand over to, yet we want to have something like printf working, ideally outputting to a simple I/O device like UART. This is where Newlib jumps in. You’re probably familiar with different flavors of C standard library like GNU (glibc), musl and so on, but Newlib should definitely be on your radar if you’d like to enable C...
First seen: 2025-04-26 22:12
Last seen: 2025-04-27 19:17