xiaoxiang781216 commented on code in PR #19940:
URL: https://github.com/apache/nuttx/pull/19940#discussion_r3906341706


##########
libs/libc/elf/gnu-elf.ld.in:
##########
@@ -98,7 +128,7 @@ SECTIONS
       . = ALIGN(SECTIONS_ALIGN);
       _efini = .;
       _edtors = .;
-    }
+    } PHDR_DATA
 
   .bss :

Review Comment:
   should we move .bss to the end



##########
arch/arm/src/common/Toolchain.defs:
##########
@@ -633,6 +633,14 @@ CELFFLAGS = $(filter-out --fixed-r9,$(CFLAGS)) 
-fvisibility=hidden \
 CXXELFFLAGS = $(filter-out --fixed-r9,$(CXXFLAGS)) -fvisibility=hidden \
               -mlong-calls
 
+# -fno-use-cxa-atexit, because the default registers each static object's
+# destructor with __cxa_atexit(dtor, obj, &__dso_handle), and __dso_handle
+# comes from crtbegin, which a module does not link.  Turning it off also
+# puts the destructors in .fini_array, which is where the loader looks for
+# them when the module is unloaded.
+
+CXXELFFLAGS += -fno-use-cxa-atexit

Review Comment:
   need modify  cmakefile and other arch



##########
libs/libc/elf/gnu-elf.ld.in:
##########
@@ -111,12 +141,19 @@ SECTIONS
       *(COMMON)
       . = ALIGN(SECTIONS_ALIGN);
       _ebss = . ;
-    }
+    } PHDR_DATA
+
+#ifdef CONFIG_FDPIC
+  .dynamic :
+    {
+      *(.dynamic)
+    } :data :dynamic

Review Comment:
   why put into teo sections 



##########
arch/arm/src/common/Toolchain.defs:
##########
@@ -646,11 +646,29 @@ ifeq ($(CONFIG_PIC),y)
   # after including this file, which would discard the flag.
 
   ARCHCFLAGS += --fixed-r9
+
+ifeq ($(CONFIG_FDPIC),y)
+  # An FDPIC module is a shared object whose two segments the loader places
+  # independently.  The stock compiler emits correct FDPIC objects for both C
+  # and C++, so only the link needs the arm-uclinuxfdpiceabi linker: the
+  # stock one carries the armelf emulation alone and would turn every import
+  # into a jump slot where the ABI wants a function descriptor.
+
+  FDPIC_CROSSDEV ?= arm-uclinuxfdpiceabi-
+  MODULELD = $(FDPIC_CROSSDEV)ld
+
+  CELFFLAGS += -mfdpic -fPIC -Wa,--noexecstack

Review Comment:
   why not reference PICFLAGS



##########
arch/arm/src/cmake/elf.cmake:
##########
@@ -27,17 +27,54 @@ nuttx_mod_compile_options(-fvisibility=hidden -mlong-calls)
 nuttx_elf_compile_options_ifdef(CONFIG_UNWINDER_ARM -fno-unwind-tables
                                 -fno-asynchronous-unwind-tables)
 
-# An ELF module needs r9 as its PIC base, so it must not also have the register
-# fixed: GCC rejects that pair with "unable to use 'r9' for PIC register".  
This
-# mirrors CELFFLAGS in common/Toolchain.defs, which filters --fixed-r9 back out
-# of the inherited CFLAGS for the same reason.
+# -fno-use-cxa-atexit, because the default registers each static object's
+# destructor with __cxa_atexit(dtor, obj, &__dso_handle), and __dso_handle 
comes
+# from crtbegin, which a module does not link.  Turning it off also puts the
+# destructors in .fini_array, which is where the loader looks for them when the
+# module is unloaded.  This mirrors CXXELFFLAGS in common/Toolchain.defs.
 
-nuttx_elf_compile_options_ifdef(CONFIG_PIC -mpic-register=r9)
+nuttx_elf_compile_options(-fno-use-cxa-atexit)
 
-nuttx_elf_link_options_ifdef(
-  CONFIG_PIC --unresolved-symbols=ignore-in-object-files --emit-relocs)
+if(CONFIG_FDPIC)
 
-nuttx_elf_link_options_ifdef(CONFIG_BINFMT_ELF_RELOCATABLE -r)
+  # An FDPIC module is a shared object whose two segments the loader places
+  # independently.  The stock compiler emits correct FDPIC objects for both C
+  # and C++, so only the link needs the arm-uclinuxfdpiceabi linker: the stock
+  # one carries the armelf emulation alone and would turn every import into a
+  # jump slot where the ABI wants a function descriptor.
+
+  if(NOT FDPIC_CROSSDEV)
+    set(FDPIC_CROSSDEV arm-uclinuxfdpiceabi-)
+  endif()
+
+  set(CMAKE_ELF_LD
+      "${FDPIC_CROSSDEV}ld"
+      CACHE INTERNAL "Linker for FDPIC modules")
+
+  nuttx_elf_compile_options(-mfdpic -fPIC -Wa,--noexecstack)
+
+  nuttx_elf_link_options(-m armelf_linux_fdpiceabi -shared -z now)
+
+else()

Review Comment:
   add if CONFIG_PIC and remove ifdef suffix at line 65 and 67



##########
arch/arm/src/cmake/elf.cmake:
##########
@@ -27,17 +27,54 @@ nuttx_mod_compile_options(-fvisibility=hidden -mlong-calls)
 nuttx_elf_compile_options_ifdef(CONFIG_UNWINDER_ARM -fno-unwind-tables
                                 -fno-asynchronous-unwind-tables)
 
-# An ELF module needs r9 as its PIC base, so it must not also have the register
-# fixed: GCC rejects that pair with "unable to use 'r9' for PIC register".  
This
-# mirrors CELFFLAGS in common/Toolchain.defs, which filters --fixed-r9 back out
-# of the inherited CFLAGS for the same reason.
+# -fno-use-cxa-atexit, because the default registers each static object's
+# destructor with __cxa_atexit(dtor, obj, &__dso_handle), and __dso_handle 
comes
+# from crtbegin, which a module does not link.  Turning it off also puts the
+# destructors in .fini_array, which is where the loader looks for them when the
+# module is unloaded.  This mirrors CXXELFFLAGS in common/Toolchain.defs.
 
-nuttx_elf_compile_options_ifdef(CONFIG_PIC -mpic-register=r9)
+nuttx_elf_compile_options(-fno-use-cxa-atexit)
 
-nuttx_elf_link_options_ifdef(
-  CONFIG_PIC --unresolved-symbols=ignore-in-object-files --emit-relocs)
+if(CONFIG_FDPIC)
 
-nuttx_elf_link_options_ifdef(CONFIG_BINFMT_ELF_RELOCATABLE -r)
+  # An FDPIC module is a shared object whose two segments the loader places
+  # independently.  The stock compiler emits correct FDPIC objects for both C
+  # and C++, so only the link needs the arm-uclinuxfdpiceabi linker: the stock
+  # one carries the armelf emulation alone and would turn every import into a
+  # jump slot where the ABI wants a function descriptor.
+
+  if(NOT FDPIC_CROSSDEV)
+    set(FDPIC_CROSSDEV arm-uclinuxfdpiceabi-)
+  endif()
+
+  set(CMAKE_ELF_LD
+      "${FDPIC_CROSSDEV}ld"
+      CACHE INTERNAL "Linker for FDPIC modules")
+
+  nuttx_elf_compile_options(-mfdpic -fPIC -Wa,--noexecstack)
+
+  nuttx_elf_link_options(-m armelf_linux_fdpiceabi -shared -z now)
+
+else()
+
+  # An ELF module needs r9 as its PIC base, so it must not also have the
+  # register fixed: GCC rejects that pair with "unable to use 'r9' for PIC
+  # register".  This mirrors CELFFLAGS in common/Toolchain.defs, which filters
+  # --fixed-r9 back out of the inherited CFLAGS for the same reason.
+
+  nuttx_elf_compile_options_ifdef(CONFIG_PIC -mpic-register=r9)
+
+  nuttx_elf_link_options_ifdef(
+    CONFIG_PIC --unresolved-symbols=ignore-in-object-files --emit-relocs)
+
+endif()
+
+# Not with CONFIG_PIC: there the module is linked as an executable, which is
+# what common/Toolchain.defs does too.
+
+if(CONFIG_BINFMT_ELF_RELOCATABLE AND NOT CONFIG_PIC)

Review Comment:
   why need check pic



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to