Section 12.19

Newlib and Picolibc

Definition

Newlib and Picolibc are C standard libraries for embedded systems. A C standard library, or libc, is the compiled code behind the functions the C standard declares [78], such as memcpy, printf and malloc. Both run without a full operating system: the platform supplies a few low-level functions, such as one that outputs a character. Picolibc, by Keith Packard, is derived largely from the older Newlib and designed for devices with little RAM. Their main difference: Newlib’s input and output functions allocate buffers with malloc, while Picolibc’s allocate nothing.

The idea

A program calls printf("%d\n", 42). On a desktop computer the C library comes with the operating system, and printf ends in a system call, a request to the operating-system kernel, that writes the characters to a terminal or file. A microcontroller §6.1, a small computer on one chip, often has no kernel, and its memory is small: for example 256 KiB of flash memory §6.6 for code and 64 KiB of RAM. There the same call must end in a function that puts the characters into a serial port, and the library’s code and data must fit beside the application.

An embedded C library therefore does two things that a desktop library does not. It is small and configurable, so that a program pays only for the features it uses. And it leaves a short list of low-level functions to the platform: where output goes, where heap memory comes from, and how threads keep out of each other’s way. Newlib and Picolibc are two such libraries with different trade-offs. A call to printf therefore names an interface, not one piece of code: which library, which configuration of it and which platform functions were linked decide the size, the memory use and even the output, as the worked example shows.

How it works

Newlib is a collection of library parts under free-software licenses. It began at Cygnus, a company that sold support for the GNU tools, and Red Hat maintains it at sourceware.org. GNU, the free-software project behind GCC §12.17 (the GNU Compiler Collection), stands for GNU’s Not Unix. Newlib is the C library of many bare-metal GCC toolchains §12.2, those that build programs to run without an operating system, such as arm-none-eabi-gcc.

Picolibc keeps much of Newlib’s code, reorganized, and takes part of its input and output code from AVR Libc, the C library for the 8-bit AVR microcontrollers; AVR is the name of that processor family.

Selection. The library is chosen when the program is linked. In a GCC toolchain a specs file, a file of rules that the compiler driver follows, decides: nano.specs selects Newlib-nano, and picolibc.specs selects Picolibc where it is installed. The options for the processor then choose the precompiled variant, for example the directory thumb/v7e-m/nofp for a Cortex-M4 §6.3 used without floating-point hardware. The build system of a real-time operating system can make the choice itself; Zephyr §6.33 uses the configuration options CONFIG_NEWLIB_LIBC, CONFIG_PICOLIBC and CONFIG_MINIMAL_LIBC. From the library archive, the linker §12.4 copies into the program only the members, the object files inside the archive, that the program needs.

Worked example

The program int main(void) { printf("%d\n", 42); return 0; } was built with arm-none-eabi-gcc 13.2.1 and Newlib 4.4.0, as packaged by Ubuntu 24.04, with the options -mcpu=cortex-m4 -mthumb -O2 --specs=nosys.specs. The last option links the placeholder platform functions. A second program prints a double variable x=3.14159x = 3.14159 with printf("%.2f\n", x). The table gives the sizes in bytes reported by arm-none-eabi-size: text is code and constant data, data is initialized variables, and bss is variables that start as zero.

build text data bss
Newlib, integer 28,996 1,732 812
Newlib-nano, integer 7,576 108 504
Newlib, double 29,060 1,740 812
Newlib-nano, double 7,580 120 504
Newlib-nano, double, -u _printf_float 22,152 484 508

Newlib-nano saves 28,996−7,576=21,42028{,}996 - 7{,}576 = 21{,}420 bytes of code, which is 21,420/28,996=73.9%21{,}420 / 28{,}996 = 73.9\% of the full build. On a microcontroller with 256 KiB, that is 256×1024=262,144256 \times 1024 = 262{,}144 bytes, of flash memory, the full build’s code takes 28,996/262,144=11.1%28{,}996 / 262{,}144 = 11.1\% and the nano build’s 7,576/262,144=2.9%7{,}576 / 262{,}144 = 2.9\%. The full Newlib build grows by only 29,060−28,996=6429{,}060 - 28{,}996 = 64 bytes for the second program, because its printf always contains floating-point formatting. In Newlib-nano, floating-point formatting costs 22,152−7,580=14,57222{,}152 - 7{,}580 = 14{,}572 bytes.

The output differs too. With full Newlib the second program prints 3.14. With Newlib-nano and no -u _printf_float, it prints only the line break: Newlib-nano’s printf takes the argument and produces no characters for the conversion. The build gives no warning about this.

The linker’s map file, its report of what it linked, shows where the code of the nano build came from. printf came from member libc_a-printf.o of libc_nano.a, and it needed _vfprintf_r from member libc_a-nano-vfprintf.o. The map also lists malloc (member libc_a-mallocr.o) and _sbrk (from libnosys.a): printing one integer brought in the heap allocator. The linker warned that _close, _fstat, _isatty, _lseek, _read and _write each “is not implemented and will always fail”. On a real device this program’s output goes nowhere until the platform supplies _write.

Stated precisely

The behavior of a C library function in a built program is fixed by four layers.

  1. The standard’s contract: what ISO C requires of the function. For example, printf returns the number of characters transmitted, or a negative value if an output or encoding error occurred.

  2. The implementation and its release: Newlib, Newlib-nano or Picolibc, and which version.

  3. The options with which the library itself was built, such as Newlib’s reduced formatted output or Picolibc’s use of thread-local storage. A precompiled library carries the choices of whoever built it.

  4. Link-time choices and platform functions: the specs file, the processor variant, options such as -u _printf_float, and the platform’s _write, _sbrk and lock functions or Picolibc’s character functions.

Where the standard leaves a choice to the implementation, layers 2 to 4 decide it. Where the standard is exact, a conforming library follows it. Newlib-nano without floating-point formatting is a deliberate departure from the standard: it prints nothing for a %f conversion.

Limits and common mistakes

The same function name does not mean the same code, size, stack use or timing. Measure the linked program with its map file and a size report.

Placeholder platform functions make a program link, not work. With libnosys, _write always fails, so output silently disappears.

In Newlib, the first output to a stream allocates a buffer from the heap. On a device with a small heap that allocation can fail or add to heap fragmentation §4.9, the splitting of free memory into pieces too small to use. In a program with several threads, __malloc_lock must really lock, or two threads can corrupt the heap.

Per-thread state needs platform support. Newlib needs a separate struct _reent for each thread and working lock functions; Picolibc needs each thread’s thread-local storage set up when the thread starts. Without that, threads share errno and stream state, and one thread’s library call can corrupt another’s.

A library can hide a bug in the program. A call that breaks the function’s contract, such as printf("%d", x) with a long long argument x, has undefined behavior §4.12. One library may print the expected value by chance, and another may not; the bug is in the program.

Questions answered

Which libc implementation and build options define a function’s behavior?

A function’s behavior is defined by the C library actually linked into the program, together with the options that library was built with, the choices made when the program was linked, and the platform functions the library calls; the C standard sets only the outer contract. Libc is the usual name for the C standard library, the compiled code behind functions such as printf and malloc; Newlib, its reduced configuration Newlib-nano, and Picolibc are common choices on embedded devices. To establish them for one program:

  1. read the linker’s map file, which names the library archive and member that supplied the function, for example libc_nano.a(libc_a-printf.o);

  2. identify that library’s release and how it was built, from the toolchain’s documentation or the build system’s configuration, such as Zephyr’s CONFIG_PICOLIBC;

  3. find the link options: the specs file, options such as -u _printf_float, and the processor variant directory;

  4. find the platform functions, such as _write, _sbrk and the lock functions, or Picolibc’s character functions;

  5. test on the device the behavior that matters, such as printing a floating-point number §4.49.

A toolchain or source tree that merely contains a library does not show which library was linked or how it was configured.

Related entries: Linker (§12.4), Zephyr SDK (§12.16), Ownership (§4.2), Asynchronous completion (§9.28), Toolchain (§12.2), Reentrancy (§9.27), Heap fragmentation (§4.9). Further reading: [78, 120, 121].