From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 18033393A424; Fri, 12 Mar 2021 10:06:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 18033393A424 From: "kabel at blackhole dot sk" To: gcc-bugs@gcc.gnu.org Subject: [Bug lto/99559] New: LTO with -Os -ffreestanding -nodefaultlibs may generate call to memcpy, but fail linking even though memcpy is defined Date: Fri, 12 Mar 2021 10:06:29 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: lto X-Bugzilla-Version: 10.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: kabel at blackhole dot sk X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter cc target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Mar 2021 10:06:30 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99559 Bug ID: 99559 Summary: LTO with -Os -ffreestanding -nodefaultlibs may generate call to memcpy, but fail linking even though memcpy is defined Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: lto Assignee: unassigned at gcc dot gnu.org Reporter: kabel at blackhole dot sk CC: marxin at gcc dot gnu.org Target Milestone: --- Created attachment 50372 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D50372&action=3Dedit test.c When compiling for target where the assignment operator *dst =3D *src; will generate a call to memcpy (for example aarch64), and when also using compile flags -ffreestanding -Os -flto and link flags -nodefaultlibs -nostartfiles the linking will fail with undefined reference to `memcpy' even if memcpy is defined in the source. The memcpy is optimized away, even though it is used by the =3D operator. It needs to be declared with __attribute__((used)) to not be optimized away. What is interesting is that if the memcpy function is used explicitly, the compilation may still fail with -Os, because the code will be inlined. If memcpy is used explicitly multiple times, it may be generated with symbol name memcpy.constprop.0.isra.0 or memcpy.isra.0 depending on whether the arguments passed are the same or different. But the linking fill still fail, because the assignment operator will gener= ate a call to memcpy, and this symbol is not defined, only memcpy.isra.0 or memcpy.constprop.0.isra.0 is. I discovered this recently while trying to make U-Boot compile with LTO. We have solved it by declaring "memcmp", "memset", "memcpy" and "memmove" with attribute used, since according to GCC man page, calls to this functions may still be done when -nodefaultlibs is used.=