From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.inka.de (mail.inka.de [IPv6:2a04:c9c7:0:1073:217:a4ff:fe3b:e77c]) by sourceware.org (Postfix) with ESMTPS id 970E53858C66 for ; Mon, 6 Mar 2023 14:42:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 970E53858C66 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=raven.inka.de Authentication-Results: sourceware.org; spf=none smtp.mailfrom=raven.inka.de Received: from raven.inka.de (uucp@[127.0.0.1]) by mail.inka.de with uucp (rmailwrap 0.5) id 1pZC2Q-003mON-Kz; Mon, 06 Mar 2023 15:42:06 +0100 Received: by raven.inka.de (Postfix, from userid 1000) id 55927600A7; Mon, 6 Mar 2023 15:40:47 +0100 (CET) Date: Mon, 6 Mar 2023 15:40:47 +0100 From: Josef Wolf To: Richard Earnshaw Cc: Jonathan Wakely , gcc-help Subject: Re: How to omit libc (newlib) on bare metal (freestanding)? Message-ID: <20230306144047.GB2142@raven.inka.de> Mail-Followup-To: Josef Wolf , Richard Earnshaw , Jonathan Wakely , gcc-help References: <20230221233125.GA2142@raven.inka.de> <7eead147-36bf-9f31-da9b-be006efd8aa3@foss.arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7eead147-36bf-9f31-da9b-be006efd8aa3@foss.arm.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-3489.9 required=5.0 tests=BAYES_00,KAM_DMARC_STATUS,KAM_LAZY_DOMAIN_SECURITY,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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, wrote: > > > On Tue, 21 Feb 2023, 23:32 Josef Wolf, 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@raven.inka.de