You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "casaroli (via GitHub)" <gi...@apache.org> on 2026/10/19 12:20:43 UTC

[PR] build: Use the emutls functions of libc in a loadable module. [nuttx-apps]

casaroli opened a new pull request, #3882:
URL: https://github.com/apache/nuttx-apps/pull/3882

   ## Summary
   
   A toolchain without native TLS (for example the Arm GNU toolchain for aarch64-none-elf, or arm-none-eabi 13 as in CI) compiles a `__thread` variable into calls to `__emutls_get_address()`. In a flat or protected build, `Application.mk` and `Library.mk` link libgcc into each loadable module. The module refers to `__emutls_get_address()`, so the linker puts `emutls.o` of libgcc into the module. That copy is built with the single thread model: all threads of the module share one instance of each `__thread` variable. The loader never binds the module to the per-thread version of libc.
   
   This PR links such a module with a copy of the compiler runtime in which `__emutls_get_address()` and `__emutls_register_common()` are local symbols (`objcopy -L`). The linker does not take them, the module keeps them undefined, and the loader binds them to libc through the system symbol table. The module still takes all the other helpers (`__aeabi_ldivmod`, `__floatditf`, ...) from the runtime. The build makes the copy once, in `staging/module/`, with a temporary file and a rename, so parallel module links can share it. `make clean` removes it.
   
   A library order or a linker option cannot do this. The same object needs both `emutls.o` and the other helpers. `--just-symbols` makes the symbol absolute, `/DISCARD/` gives "defined in discarded section", `--wrap` renames the reference, and ld.lld takes archive members in any order.
   
   depends-on: apache/nuttx/pull/20723 (it exports the per-thread `__emutls_get_address()` of libc in libc.csv). The CMake build links modules in nuttx `cmake/nuttx_add_application.cmake`; apache/nuttx#20812 does the same change there.
   
   ## Impact
   
   - Loadable modules and shared libraries of a flat or protected build that use `__thread`: each thread now has its own copy. Without the libc export, the module does not load (`Exported symbol "__emutls_get_address" not found`) instead of sharing one copy between the threads.
   - Modules without `__thread` variables: no change (`bin/hello` is byte-identical).
   - Kernel build programs: no change. They link libc, which comes before libgcc.
   - Toolchains with native TLS or a runtime without emutls: the copy is the same as the runtime.
   - One copy of the runtime in `apps/staging/module/` when the build has a module.
   
   ## Testing
   
   `qemu-armv8a:nsh` (flat, Arm GNU 14.2 aarch64-none-elf, QEMU HVF) with `CONFIG_ELF`, `CONFIG_EXECFUNCS_SYSTEM_SYMTAB`, `CONFIG_LIBC_DLFCN` and `CONFIG_TLS_NELEM=4`, nuttx with apache/nuttx#20723 and apache/nuttx#20807. The same results with `CONFIG_TLS_NELEM=0`. The test programs are built with `MODULE = m` by `Application.mk` and loaded from a 9p share. `tlsdl` loads `libtlsso.so`, made by `Library.mk`, with `dlopen()`.
   
   | Module | master | this PR | this PR, no emutls export |
   |---|---|---|---|
   | tlsgroup (2 threads, run twice) | FAIL (one address for all threads) | PASS | `Exported symbol "__emutls_get_address" not found` |
   | tls4 (4 threads) | FAIL | PASS | not found |
   | tlsmany (12 variables) | FAIL | PASS | not found |
   | uctls (ucontext) | PASS | PASS | not found |
   | tlscxx (C++ `thread_local`) | FAIL | PASS | not found |
   | tlsdl + libtlsso.so | FAIL | PASS | `dlopen()` fails, not found |
   | hello | PASS | PASS (same file) | PASS |
   
   `nm` of a module:
   
   ```
   master:   00000000000001c0 T __emutls_get_address
             0000000000000280 T __emutls_register_common
             U abort  U malloc  U memcpy  U memset ...
   this PR:  U __emutls_get_address
   ```
   
   `mps2-an500:nsh` (flat, arm-none-eabi 13.2 as in CI, QEMU TCG), modules loaded from a semihosting hostfs, same tests without uctls: on master no module with `__thread` loads, because `emutls.o` of libgcc is not built with `-mlong-calls` and calls `malloc()` in the firmware with a branch that cannot reach it (`JUMP24 relocation out of range`). With this PR tlsgroup, tls4, tlsmany, tlsdl + libtlsso.so and tlscxx PASS, and hello runs as before.
   
   `qemu-armv8a:knsh` (kernel build, export and import): the 11 programs built with and without this PR are byte-identical, except the build time string in `init` and `sh`; no runtime copy is made.
   
   The uctls test needs `getcontext()`, `makecontext()` and `swapcontext()` in libc.csv; the test tree added them locally.
   
   `tools/checkpatch.sh -c -u -m -g` passes.
   


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build: Link the emutls calls of a module or program to libc. [nuttx-apps]

Posted by "casaroli (via GitHub)" <gi...@apache.org>.
casaroli closed pull request #3882: build: Link the emutls calls of a module or program to libc.
URL: https://github.com/apache/nuttx-apps/pull/3882


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build: Link the emutls calls of a module or program to libc. [nuttx-apps]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #3882:
URL: https://github.com/apache/nuttx-apps/pull/3882#discussion_r4329176369


##########
Make.defs:
##########
@@ -23,6 +23,12 @@
 TOPDIR ?= $(APPDIR)/import
 -include $(TOPDIR)/Make.defs
 
+# Send the emulated TLS calls of loadable modules and of the programs of a
+# kernel build to libc (EMUTLS_LDFLAGS: see tools/Config.mk of NuttX)
+
+LDELFFLAGS += $(EMUTLS_LDFLAGS)

Review Comment:
   yes, it's better to initialize the flag with the correct value instead patch them later.



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build: Link the emutls calls of a module or program to libc. [nuttx-apps]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #3882:
URL: https://github.com/apache/nuttx-apps/pull/3882#discussion_r4327479816


##########
Make.defs:
##########
@@ -23,6 +23,12 @@
 TOPDIR ?= $(APPDIR)/import
 -include $(TOPDIR)/Make.defs
 
+# Send the emulated TLS calls of loadable modules and of the programs of a
+# kernel build to libc (EMUTLS_LDFLAGS: see tools/Config.mk of NuttX)
+
+LDELFFLAGS += $(EMUTLS_LDFLAGS)

Review Comment:
   why not modify the initialization of LDELFFLAG directly



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build: Use the emutls functions of libc in a loadable module. [nuttx-apps]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #3882:
URL: https://github.com/apache/nuttx-apps/pull/3882#discussion_r4316163861


##########
Application.mk:
##########
@@ -63,7 +63,12 @@ LDLIBS += $(call CONVERT_PATH,$(BIN))
 # uses __udivdi3.
 ifeq ($(BUILD_MODULE),y)
   COMPILER_RT_LIB := $(call FIND_COMPILER_RT_LIB)
-  LDLIBS += $(COMPILER_RT_LIB)
+  ifneq ($(CONFIG_BUILD_KERNEL),y)
+    ifneq ($(COMPILER_RT_LIB),)
+      MODULE_RT_LIB := $(call MODULE_COMPILER_RT_LIB,$(COMPILER_RT_LIB))

Review Comment:
   or rename __emutls_get_address like https://github.com/apache/nuttx/pull/20803/changes#diff-a5d9c121a2d2de17f13a157ea372f6d8545f5df795f4b28dab8a0e16a455b1bdR50



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build: Link the emutls calls of a module or program to libc. [nuttx-apps]

Posted by "casaroli (via GitHub)" <gi...@apache.org>.
casaroli commented on code in PR #3882:
URL: https://github.com/apache/nuttx-apps/pull/3882#discussion_r4329029500


##########
Make.defs:
##########
@@ -23,6 +23,12 @@
 TOPDIR ?= $(APPDIR)/import
 -include $(TOPDIR)/Make.defs
 
+# Send the emulated TLS calls of loadable modules and of the programs of a
+# kernel build to libc (EMUTLS_LDFLAGS: see tools/Config.mk of NuttX)
+
+LDELFFLAGS += $(EMUTLS_LDFLAGS)

Review Comment:
   `EMUTLS_LDFLAGS` comes from `tools/Config.mk`, but `Config.mk` is included before the arch `Toolchain.defs`, which sets `LDELFFLAGS` and `LDMODULEFLAGS` with `=`, so `Config.mk` cannot add to them. After `Toolchain.defs`, 28 board `Make.defs` files set them again with `=` (for example `boards/arm/stm32h7/openh743i/scripts/Make.defs:49`, `boards/arm/samv7/samv71-xult/configs/knsh/Make.defs:59`, `boards/arm64/imx9/imx95-a55-evk/scripts/Make.defs:45`, `boards/renesas/rx65n/rx65n/scripts/Make.defs:59`, `boards/sim/sim/sim/scripts/Make.defs:273,304`), and `tools/mkexport.sh:243` sets `LDMODULEFLAGS="-r"` for the export. To do it in NuttX, I must change the `Toolchain.defs` of every arch that can use emulated TLS, these board files and `mkexport.sh`. apps `Make.defs` is the first place after all of them, so the two lines here cover every board and the export.
   
   If you prefer the NuttX side, I can first remove the board lines that only repeat the arch values (some point to `binfmt/elf/gnu-elf.ld`, which is gone) in a separate PR, and then add `$(EMUTLS_LDFLAGS)` where `Toolchain.defs` sets the two variables. Is this what you want?
   



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build: Link the emutls calls of a module or program to libc. [nuttx-apps]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #3882:
URL: https://github.com/apache/nuttx-apps/pull/3882#issuecomment-6283732768

   <!-- nuttx-depends-on-bot run-39339254294 -->
   ### 🔗 Cross-repo PR dependencies
   
   The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):
   
   - https://github.com/apache/nuttx/pull/20723 @ 4e6c379bd3
   
   CI run: https://github.com/apache/nuttx-apps/actions/runs/39339254294


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build: Use the emutls functions of libc in a loadable module. [nuttx-apps]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #3882:
URL: https://github.com/apache/nuttx-apps/pull/3882#issuecomment-6274304684

   <!-- nuttx-depends-on-bot run-39251328999 -->
   ### 🔗 Cross-repo PR dependencies
   
   The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):
   
   - https://github.com/apache/nuttx/pull/20723 @ 4600c89614
   
   CI run: https://github.com/apache/nuttx-apps/actions/runs/39251328999


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build: Link the emutls calls of a module or program to libc. [nuttx-apps]

Posted by "casaroli (via GitHub)" <gi...@apache.org>.
casaroli commented on code in PR #3882:
URL: https://github.com/apache/nuttx-apps/pull/3882#discussion_r4319970236


##########
Application.mk:
##########
@@ -63,7 +63,12 @@ LDLIBS += $(call CONVERT_PATH,$(BIN))
 # uses __udivdi3.
 ifeq ($(BUILD_MODULE),y)
   COMPILER_RT_LIB := $(call FIND_COMPILER_RT_LIB)
-  LDLIBS += $(COMPILER_RT_LIB)
+  ifneq ($(CONFIG_BUILD_KERNEL),y)
+    ifneq ($(COMPILER_RT_LIB),)
+      MODULE_RT_LIB := $(call MODULE_COMPILER_RT_LIB,$(COMPILER_RT_LIB))

Review Comment:
   Done: `Application.mk` and `Library.mk` now link with `EMUTLS_LDFLAGS` (`--wrap=...`) from NuttX, and the copy of the compiler runtime is gone.
   



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build: Link the emutls calls of a module or program to libc. [nuttx-apps]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #3882:
URL: https://github.com/apache/nuttx-apps/pull/3882#discussion_r4332140942


##########
Make.defs:
##########
@@ -23,6 +23,12 @@
 TOPDIR ?= $(APPDIR)/import
 -include $(TOPDIR)/Make.defs
 
+# Send the emulated TLS calls of loadable modules and of the programs of a
+# kernel build to libc (EMUTLS_LDFLAGS: see tools/Config.mk of NuttX)
+
+LDELFFLAGS += $(EMUTLS_LDFLAGS)

Review Comment:
   let's add this special flag  in nuttx when we define LDELFFLAGS



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build: Use the emutls functions of libc in a loadable module. [nuttx-apps]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #3882:
URL: https://github.com/apache/nuttx-apps/pull/3882#issuecomment-6279275070

   <!-- nuttx-depends-on-bot run-39305620122 -->
   ### 🔗 Cross-repo PR dependencies
   
   The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):
   
   - https://github.com/apache/nuttx/pull/20723 @ 3830f0d0c6
   
   CI run: https://github.com/apache/nuttx-apps/actions/runs/39305620122


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build: Use the emutls functions of libc in a loadable module. [nuttx-apps]

Posted by "casaroli (via GitHub)" <gi...@apache.org>.
casaroli closed pull request #3882: build: Use the emutls functions of libc in a loadable module.
URL: https://github.com/apache/nuttx-apps/pull/3882


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] build: Link the emutls calls of a module or program to libc. [nuttx-apps]

Posted by "casaroli (via GitHub)" <gi...@apache.org>.
casaroli commented on code in PR #3882:
URL: https://github.com/apache/nuttx-apps/pull/3882#discussion_r4331084273


##########
Make.defs:
##########
@@ -23,6 +23,12 @@
 TOPDIR ?= $(APPDIR)/import
 -include $(TOPDIR)/Make.defs
 
+# Send the emulated TLS calls of loadable modules and of the programs of a
+# kernel build to libc (EMUTLS_LDFLAGS: see tools/Config.mk of NuttX)
+
+LDELFFLAGS += $(EMUTLS_LDFLAGS)

Review Comment:
   Done in apache/nuttx#20723: the arch `Toolchain.defs` (and the sim `Make.defs`) now adds `EMUTLS_LDFLAGS` to `LDELFFLAGS` and `LDMODULEFLAGS`, and `mkexport.sh` keeps it in the export. With apps master, a module that uses `__thread` now links to `__wrap___emutls_get_address` of libc (`tlsgroup: PASS` on `qemu-armv8a:nsh`), and the export of `qemu-armv8a:knsh` has the options. apps needs no change, so I close this PR. The boards that set these variables again in their `Make.defs` are cleaned up in apache/nuttx#20844.
   



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org