Re: How to omit libc (newlib) on bare metal (freestanding)?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thanks for the replies, and sorry for the delay!

On Wed, Feb 22, 2023 at 11:35:51AM +0000, Richard Earnshaw wrote:
> On 22/02/2023 09:03, Jonathan Wakely via Gcc-help wrote:
> > On Wed, 22 Feb 2023, 09:02 Jonathan Wakely, <jwakely.gcc@xxxxxxxxx> wrote:
> > > On Tue, 21 Feb 2023, 23:32 Josef Wolf, <jw@xxxxxxxxxxxxx> wrote:
> > > >
> > > > I am trying to build for a bare metal target without any OS support and I
> > > > want to omit libc (newlib).
> > > > 
> > > > So I add
> > > > 
> > > >     -ffunction-sections -nostdlib -ffreestanding
> > > > 
> > > > flags to assembler/compiler and
> > > > 
> > > >     -lnosys -nolibc -nodefaultlibs -nostartfiles -nostdlib -ffreestanding
> > > >     -static-libgcc -lgcc  -lnosys -nolibc -nodefaultlibs -nostartfiles
> > > >     -nostdlib -ffreestanding -static-libgcc -lgcc
> > > > 
> > > > to linker
> > > 
> > > Do you mean you're trying to build GCC itself, or you're trying to build
> > > your own program?

I am trying to build my own program using GCC which I have built by myself :)

> > > Please show the full commands you're using
> > > 
> > > If you add -v to the gcc command it will show exactly what it's doing.

Appended to the end of this post.

> > Oh actually I see the problem. You're using -lgcc and your gcc was
> > configured to use newlib, so libgcc depends on newlib.

I am irritated about that. I see that libgcc needs malloc() and friends. But I
don't see why libgcc would need sbrk/write/close/lseek/read.

> Whilst there are functions in libgcc that depend on newlib (not many and
> probably none that you'd need in this instance), I wouldn't have expected
> -lgcc to cause -lc to be added to the link set.

Exactly. As a side-note: I provide my own implementations of memcpy() and
friends, So I don't see why gcc would need to pull them from
newlib. Especially when I explicitly provide all those fancy options I
mentioned above to prevent using any libraries.

> Josef, what is the *complete* command line that you issue when you link the
> program?

Here it comes:

$ /usr/local/crossgcc/arm-none-eabi/bin/arm-none-eabi-gcc \
    lots.o of.o object.o files.o \
    -v -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard \
    -Tcubemx/STM32F429BITx_FLASH.ld  -nostdlib -lm -lnosys -nolibc \
    -nodefaultlibs -nostartfiles -nostdlib -ffreestanding -static-libgcc \
    -Wl,-Map=build/cubemx.map,--cref \
    -Wl,--gc-sections,--exclude-libs,libc.a \
    -o build/cubemx.elf
Using built-in specs.
COLLECT_GCC=/usr/local/crossgcc/arm-none-eabi/bin/arm-none-eabi-gcc
COLLECT_LTO_WRAPPER=/usr/local/crossgcc/arm-none-eabi/bin/../libexec/gcc/arm-none-eabi/9.3.0/lto-wrapper
Target: arm-none-eabi
Configured with: /var/tmp/builds/crossgcc/src/gcc-9.3.0/configure --target=arm-none-eabi --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --enable-languages=c,c++ --with-gnu-ld --with-gnu-as --with-newlib --with-system-zlib --enable-commonbfdlib --enable-interwork --enable-install-libbfd --enable-plugins --enable-multilib --disable-nls --disable-shared --disable-libssp --nfp --gas -v --prefix=/usr/local/crossgcc/arm-none-eabi --enable-languages=c,c++ --with-pkgversion=crossgcc --disable-libstdcxx-verbose --enable-plugins --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libstdcxx-pch --disable-threads --disable-tls --with-headers=yes --with-multilib-list=rmprofile
Thread model: single
gcc version 9.3.0 (crossgcc)
COMPILER_PATH=/usr/local/crossgcc/arm-none-eabi/bin/../libexec/gcc/arm-none-eabi/9.3.0/:/usr/local/crossgcc/arm-none-eabi/bin/../libexec/gcc/:/usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/
LIBRARY_PATH=/usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/thumb/v7e-m+fp/hard/:/usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/:/usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/:/usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/:/usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/lib/
COLLECT_GCC_OPTIONS='-v' '-mcpu=cortex-m4' '-mthumb' '-mfpu=fpv4-sp-d16' '-mfloat-abi=hard' '-T' 'cubemx/STM32F429BITx_FLASH.ld' '-nostdlib' '-nolibc' '-nodefaultlibs' '-nostartfiles' '-nostdlib' '-ffreestanding' '-static-libgcc' '-o' 'build/cubemx.elf' '-march=armv7e-m+fp'
 /usr/local/crossgcc/arm-none-eabi/bin/../libexec/gcc/arm-none-eabi/9.3.0/collect2 -plugin /usr/local/crossgcc/arm-none-eabi/bin/../libexec/gcc/arm-none-eabi/9.3.0/liblto_plugin.so -plugin-opt=/usr/local/crossgcc/arm-none-eabi/bin/../libexec/gcc/arm-none-eabi/9.3.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccXjRFlS.res -X -o build/cubemx.elf -L/usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/thumb/v7e-m+fp/hard -L/usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard -L/usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0 -L/usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc -L/usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/lib lots.o of.o object.o files.o -lm -lnosys -Map=build/cubemx.map --cref --gc-sections --exclude-libs libc.a -T cubemx/STM32F429BITx_FLASH.ld
 /usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/ld: /usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libc.a(lib_a-sbrkr.o): in function `_sbrk_r':
 /var/tmp/builds/crossgcc/src/newlib-3.1.0/newlib/libc/reent/sbrkr.c:51: undefined reference to `_sbrk'
 /usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/ld: /usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libc.a(lib_a-writer.o): in function `_write_r':
 /var/tmp/builds/crossgcc/src/newlib-3.1.0/newlib/libc/reent/writer.c:49: undefined reference to `_write'
 /usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/ld: /usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libc.a(lib_a-closer.o): in function `_close_r':
 /var/tmp/builds/crossgcc/src/newlib-3.1.0/newlib/libc/reent/closer.c:47: undefined reference to `_close'
 /usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/ld: /usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libc.a(lib_a-lseekr.o): in function `_lseek_r':
 /var/tmp/builds/crossgcc/src/newlib-3.1.0/newlib/libc/reent/lseekr.c:49: undefined reference to `_lseek'
 /usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/ld: /usr/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libc.a(lib_a-readr.o): in function `_read_r':
 /var/tmp/builds/crossgcc/src/newlib-3.1.0/newlib/libc/reent/readr.c:49: undefined reference to `_read'
 collect2: error: ld returned 1 exit status
 make[1]: *** [cubemx/Makefile:209: build/cubemx.elf] Error 1
 make[1]: Leaving directory '/m/s/rep/git/siemens/it1010/src'
 make: *** [Makefile:16: all] Error 2


-- 
Josef Wolf
jw@xxxxxxxxxxxxx



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux