This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 68e3c122ba1a6f64c991f6bb13c72dde108ca99d
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Oct 18 15:18:34 2026 +0200

    arch/arm: Link without the start files of the compiler with LTO.
    
    With CONFIG_LTO_FULL and the GNU toolchain, gcc links nuttx.  The
    Makefile gave -nostdlib to ld through -Xlinker, so gcc still added its
    start files (crti.o, crtbegin.o, crt0.o of the C library, crtend.o and
    crtn.o) and its default libraries.  crtbegin.o puts an entry into
    .init_array, a writable section, and the linker script places it in
    flash, so ld warned:
    
      ld: warning: nuttx has a LOAD segment with RWX permissions
    
    Give -nostdlib to gcc, as the CMake build does.  GNU ld then adds the
    libgcc functions that LTO calls only if the link has an object file
    outside the libraries, so link an empty object, as the CMake build does
    with empty.c.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 arch/arm/src/Makefile             | 16 +++++++++++++---
 arch/arm/src/common/arm_ldempty.c | 30 ++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/arch/arm/src/Makefile b/arch/arm/src/Makefile
index 77ef444647c..3145100bc8b 100644
--- a/arch/arm/src/Makefile
+++ b/arch/arm/src/Makefile
@@ -128,6 +128,13 @@ ifeq ($(CONFIG_ARM_TOOLCHAIN_ARMCLANG),)
     LDENDGROUP   ?= -Wl,--end-group
     LDFLAGS      := $(addprefix -Xlinker ,$(LDFLAGS))
     LDFLAGS      += $(filter-out $(STRIPCFLAGS),$(CFLAGS))
+
+    # The driver must not add its start files and libraries.  GNU ld
+    # then adds the libgcc functions that LTO calls only if the link has
+    # an object file outside the libraries, so link an empty one.
+
+    LDFLAGS      += -nostdlib
+    LDEMPTY_OBJ   = arm_ldempty$(OBJEXT)
   else ifneq ($(CONFIG_ARM_TOOLCHAIN_GHS),y)
     LDSTARTGROUP ?= --start-group
     LDENDGROUP   ?= --end-group
@@ -178,6 +185,9 @@ $(AOBJS) $(UAOBJS) $(HEAD_OBJ): %$(OBJEXT): %.S
 $(COBJS) $(UCOBJS): %$(OBJEXT): %.c
        $(call COMPILE, $<, $@)
 
+$(LDEMPTY_OBJ): common$(DELIM)arm_ldempty.c
+       $(call COMPILE, $<, $@)
+
 $(STARTUP_OBJS): %$(OBJEXT): %.c
        $(Q) $(CC) $(CELFFLAGS) -c common$(DELIM)crt0.c -o crt0$(OBJEXT)
 
@@ -218,18 +228,18 @@ define LINK_ALLSYMS_KASAN
        $(Q) $(call COMPILE, kasan_globals.tmp, kasan_globals$(OBJEXT) 
-fno-sanitize=kernel-address, -x c)
        $(Q) $(call DELFILE, kasan_globals.tmp))
        $(Q) $(LD) $(LDFLAGS) $(LIBPATHS) $(EXTRA_LIBPATHS) \
-               -o $(NUTTX) $(HEAD_OBJ) $(EXTRA_OBJS) \
+               -o $(NUTTX) $(HEAD_OBJ) $(LDEMPTY_OBJ) $(EXTRA_OBJS) \
                $(LDSTARTGROUP) $(LDLIBS) $(EXTRA_LIBS) $(LDENDGROUP)
 endef
 
 $(addsuffix .tmp,$(ARCHSCRIPT)): $(ARCHSCRIPT) $(TOPDIR)$(DELIM).config
        $(call PREPROCESS, $(patsubst %.tmp,%,$@), $@)
 
-nuttx$(EXEEXT): $(HEAD_OBJ) board$(DELIM)libboard$(LIBEXT) $(addsuffix 
.tmp,$(ARCHSCRIPT)) $(addprefix $(TOPDIR)$(DELIM)staging$(DELIM),$(LINKLIBS))
+nuttx$(EXEEXT): $(HEAD_OBJ) $(LDEMPTY_OBJ) board$(DELIM)libboard$(LIBEXT) 
$(addsuffix .tmp,$(ARCHSCRIPT)) $(addprefix 
$(TOPDIR)$(DELIM)staging$(DELIM),$(LINKLIBS))
        $(Q) echo "LD: nuttx"
 ifeq ($(CONFIG_ALLSYMS)$(CONFIG_MM_KASAN_GLOBAL),)
        $(Q) $(LD) $(LDFLAGS) $(LIBPATHS) $(EXTRA_LIBPATHS) \
-               -o $(NUTTX) $(HEAD_OBJ) $(EXTRA_OBJS) \
+               -o $(NUTTX) $(HEAD_OBJ) $(LDEMPTY_OBJ) $(EXTRA_OBJS) \
                $(LDSTARTGROUP) $(LDLIBS) $(EXTRA_LIBS) $(LDENDGROUP)
 else
        $(Q) $(call LINK_ALLSYMS_KASAN)
diff --git a/arch/arm/src/common/arm_ldempty.c 
b/arch/arm/src/common/arm_ldempty.c
new file mode 100644
index 00000000000..d0e32fc54a5
--- /dev/null
+++ b/arch/arm/src/common/arm_ldempty.c
@@ -0,0 +1,30 @@
+/****************************************************************************
+ * arch/arm/src/common/arm_ldempty.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* This file is empty on purpose.  When the compiler driver links nuttx
+ * (LTO), the link must have an object file outside the libraries: else
+ * GNU ld does not add the libgcc functions that LTO calls.
+ */
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/

Reply via email to