User Tools

Site Tools



newlib is a library meant for embedded systems. It contains implementations for all of libc and libm, the standard C library and the standard Math library. newlib can be compiled for a variety of embedded systems, I have compiled it for target=mipsel-elf. newlib isn't entirely complete though, it requires a few operating-system like calls, such as read, write and _exit. Stubs for these functions are given in libgloss/libnosys with some machine-independent implementations in libgloss. The most important of these to us are:

  • write (similar to putc)
  • _exit (similar to exit)
  • sbrk (required for dynamic memory allocation)
  • read (similar to getc)

libgloss has implementations of sbrk, read, and write, but for I/O, it requires you to write implementations for inbyte and outbyte (similar to getchar and putchar). This is easy to do in the Verilog backend (use $write) as well as the MIPS backends (already implemented as print_c_uart). If inbyte and outbyte are implemented, supposedly you get all of stdio working for free. With the simple sbrk implementation, dynamic memory allocation should also work for free (for Verilog backend, detect sbrk call as impossible to put to hardware for now).

There is a problem with printf, putchar, and other stdio functions. The current newlib implementation is very general and implements these using file open/close with stdout as the file. With either backend, there is no notion of files, so this is a problem. Also, this file implementation uses sbrk, implying dynamic memory allocation. I don't think we'll really need that for just printing to stdout. I'll try to look into modifying newlib to make all output go to stdout, skipping the need for file operations and sbrk for I/O.

Other libraries

Another important library is libgcc. libgcc includes arithmetic operations that the host processor cannot perform directly, including soft float libraries. If we want to support floating point, one possible way would be to include libgcc and compile with a -msoft-float switch and let LegUp synthesize the soft float libraries into hardware. libgcc is automatically compiled when compiling gcc from source.

Building newlib and gcc, cross-compiled for mipsel

This site is very good: I used TARGET=mipsel-elf. To compile gcc by source, you will also need gmp and mpfr libraries. Download the source and extract the gmp source into the gcc source directory into folder gmp. Similarly, extract the mpfr source into folder mpfr. While compiling, there was a header problem when configuring. Just copy the required headers from the gmp source directory to build-gcc/gmp and continue `make all-gcc`. I compiled binutils, gcc and newlib for a mipsel target. binutils is about 2 minutes, gcc and newlib take more than an hour together.


July 5, 2010

  • Compiled gcc, newlib for mipsel-elf
  • Compiled libnosys and two temporary libraries that define _exit, inbyte and outbyte for gxemul/tiger mips
  • Simplified commands work such as outbyte and write. strlen works, so puts(str) can be replaced by write(0, str, strlen(str))
  • Anything that uses malloc doesn't work:
    • /home/victor/Downloads/utils/bin/mipsel-elf-ld: small-data section exceeds 64KB; lower small-data size limit (see option -G)
    • relocation truncated to fit: R_MIPS_GPREL16 against `_impure_ptr'
    • Unfortunately this includes printf, putchar and of course malloc and the other dynamic memory allocation functions
    • According to:, GPREL16 should not be used since it limits static data to 64KB, should be using GPREL32

July 6, 2010

  • Compiled newlib as an LLVM bytecode library
  • Configured as:
  • ../newlib-1.18.0/configure –prefix=$PREFIX –target=i386 CC_FOR_TARGET=llvm-gcc CFLAGS=-emit-llvm AR_FOR_TARGET=llvm-ar AS_FOR_TARGET=llvm-as LD_FOR_TARGET=llvm-ld NM_FOR_TARGET=llvm-nm RANLIB_FOR_TARGET=llvm-ranlib –enable-newlib-io-long-long –disable-newlib-io-float –disable-newlib-supplied-syscalls
  • Produces a lot of debug symbols. Trying to put printf through the mips backend, but it's having problems, can look into that
supporting_newlib_and_other_useful_libraries.txt · Last modified: 2010/12/15 15:53 (external edit)