From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id AADFB3858D3C for ; Wed, 22 Feb 2023 11:35:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AADFB3858D3C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=foss.arm.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=foss.arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3ED08139F; Wed, 22 Feb 2023 03:36:36 -0800 (PST) Received: from [10.57.52.9] (unknown [10.57.52.9]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id A3FD43F703; Wed, 22 Feb 2023 03:35:52 -0800 (PST) Message-ID: <7eead147-36bf-9f31-da9b-be006efd8aa3@foss.arm.com> Date: Wed, 22 Feb 2023 11:35:51 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 Subject: Re: How to omit libc (newlib) on bare metal (freestanding)? Content-Language: en-GB To: Jonathan Wakely , Josef Wolf , gcc-help References: <20230221233125.GA2142@raven.inka.de> From: Richard Earnshaw In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3489.5 required=5.0 tests=BAYES_00,KAM_DMARC_STATUS,KAM_LAZY_DOMAIN_SECURITY,NICE_REPLY_A,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: 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: >> >>> Hello all, >>> >>> I am trying to build for a bare metal target without any OS support and I >>> want >>> to omit libc (newlib). >>> >> >> >> Do you mean you're trying to build GCC itself, or you're trying to build >> your own program? >> >> >> >>> 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 >> >> >> Please show the full commands you're using >> >> If you add -v to the gcc command it will show exactly what it's doing. >> > > Oh actually I see the problem. You're using -lgcc and your gcc was > configured to use newlib, so libgcc depends on newlib. 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. Josef, what is the *complete* command line that you issue when you link the program? R. > > > >> >> >> >> >> >> >>> >>> >>> But gcc still tries to pull newlib: >>> >>> /m/a/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/ld: >>> /m/a/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-init.o): >>> in function `__libc_init_array': >>> /var/tmp/builds/crossgcc/src/newlib-3.1.0/newlib/libc/misc/init.c:40: >>> undefined reference to `_init' >>> /m/a/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/ld: >>> /m/a/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' >>> /m/a/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/ld: >>> /m/a/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' >>> /m/a/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/ld: >>> /m/a/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' >>> /m/a/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/ld: >>> /m/a/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' >>> /m/a/local/crossgcc/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/9.3.0/../../../../arm-none-eabi/bin/ld: >>> /m/a/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 >>> >>> So what is the magic option to tell gcc not to mess around with libc >>> (newlib)? >>> >>> Thanks! >>> >>> >>>