Skip to content

libs/libc/tls: disable TLS_EMUTLS by default on DEFAULT_SMALL - #22055

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/libc-tls-emutls-default-small
Nov 20, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/libc-tls-emutls-default-small

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Change CONFIG_TLS_EMUTLS to default to !DEFAULT_SMALL.

In PR #20723, CONFIG_TLS_EMUTLS was added with default y for toolchains using emulated TLS. To support LTO, tools/Config.mk passes -u __wrap___emutls_get_address and -u __wrap___emutls_register_common, unconditionally forcing tls_emutls.o and its dependencies (realloc, memmove, semaphores) into every linked image, even when no __thread variables are used.

On memory-constrained targets (CONFIG_DEFAULT_SMALL=y), this wastes up to ~1.5 KB of Flash.

Before & After Comparison Table

Target / Build Flash Before (master) Flash After (this PR) Flash Delta SRAM Before SRAM After SRAM Delta
nucleo-f302r8:cansock (DEFAULT_SMALL=y, LTO_FULL=y) 23,208 B 21,752 B -1,456 B (-6.7%) 1,160 B 1,120 B -40 B
Minimal Cortex-M (no console, no files, LTO) 10.1 KB 8.8 KB -1.3 KB (-12.8%) - - -
Configuration Attribute Before (master) After (this PR)
CONFIG_TLS_EMUTLS default on DEFAULT_SMALL=y y (enabled) n (disabled)
CONFIG_TLS_EMUTLS default on standard targets y (enabled) y (enabled)
Forced linker symbols on small targets -u __wrap___emutls_get_address
-u __wrap___emutls_register_common
None
tls_emutls.o pulled into small LTO images Yes (unconditional) No (completely excluded)

By making CONFIG_TLS_EMUTLS default to !DEFAULT_SMALL, small/minimal configurations avoid this bloat, while standard/large systems continue to have emulated TLS enabled by default. Configurations that explicitly require per-thread __thread support can still enable CONFIG_TLS_EMUTLS=y in their defconfig.

Also updates Documentation/guides/concurrency/thread_local_storage.rst to document the new default.

Related to #20414
Fixes flash bloat reported in #20723

Impact

  • Is new feature added? No
  • Is existing feature changed? Yes (CONFIG_TLS_EMUTLS default changed on DEFAULT_SMALL=y)
  • Impact on user: Saves ~1.5 KB Flash on small systems. Any small board needing __thread can still explicitly enable CONFIG_TLS_EMUTLS=y.
  • Impact on build: None.
  • Impact on hardware: Preserves valuable Flash space on memory-constrained microcontrollers.
  • Impact on documentation: Updated thread_local_storage.rst.
  • Impact on security: None.
  • Impact on compatibility: Compatible with existing defconfigs; standard systems remain unaffected.

Testing

I confirm that changes are verified on local setup and works as intended:

  • Build Host(s): Linux (Ubuntu 22.04 container via Docker), GCC 10.3.1 (arm-none-eabi-gcc)
  • Target(s): stm32f103-minimum:nsh (CONFIG_DEFAULT_SMALL=y), nucleo-f302r8:cansock (CONFIG_DEFAULT_SMALL=y, CONFIG_LTO_FULL=y)

Size Comparison:

Target Flash (Before) Flash (After) Flash Delta SRAM (Before) SRAM (After) SRAM Delta
nucleo-f302r8:cansock 23,208 B 21,752 B -1,456 B (-6.7%) 1,160 B 1,120 B -40 B

Testing logs before change:

  1. Kconfig configuration on stm32f103-minimum:nsh (master with default y):
$ ./tools/configure.sh -l stm32f103-minimum:nsh
$ grep -E "DEFAULT_SMALL|TLS_EMUTLS" .config
CONFIG_DEFAULT_SMALL=y
CONFIG_TLS_EMUTLS=y

CONFIG_TLS_EMUTLS=y was enabled by default on small targets, passing -u __wrap___emutls_get_address to the linker.

  1. Size measurement on nucleo-f302r8:cansock (CONFIG_DEFAULT_SMALL=y, CONFIG_LTO_FULL=y, master 8a282a5):
$ size nuttx
   text    data     bss     dec     hex filename
  23208       0    1160   24368    5f30 nuttx

Testing logs after change:

  1. Kconfig configuration on stm32f103-minimum:nsh (with default !DEFAULT_SMALL):
$ ./tools/configure.sh -l stm32f103-minimum:nsh
$ grep -E "DEFAULT_SMALL|TLS_EMUTLS" .config
CONFIG_DEFAULT_SMALL=y
# CONFIG_TLS_EMUTLS is not set

CONFIG_TLS_EMUTLS is now cleanly disabled by default on small configurations without manual intervention.

  1. Size measurement on nucleo-f302r8:cansock with CONFIG_TLS_EMUTLS=n:
$ size nuttx
   text    data     bss     dec     hex filename
  21752       0    1120   22872    5958 nuttx

Difference: -1,456 B (-6.7%) Flash saved, -40 B SRAM saved.

  1. Checkpatch and commit message verification:
$ ./tools/checkpatch.sh -r -g HEAD~1..HEAD
✔️ All checks pass.

$ git log -1 --format=%B | ./tools/checkpatch.sh -m -g --stdin
✔️ All checks pass.

@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: XS The size of the change in this PR is very small labels Nov 19, 2026
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text -28 B (-0.0%, 64,823 B / 262,144 B, total: 25% used)

mirtoo

  • kseg0_progmem: .text -20 B (-0.0%, 68,480 B / 131,072 B, total: 52% used)
  • kseg1_datamem: .sbss -4 B (-0.2%, 2,636 B / 32,256 B, total: 8% used)

stm32-nucleo-f103rb

  • flash: .text -16 B (-0.0%, 35,460 B / 131,072 B, total: 27% used)
  • sram: .bss -8 B (-0.3%, 2,504 B / 20,480 B, total: 12% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

PR apache#20723 added CONFIG_TLS_EMUTLS with 'default y'. To support LTO,
tools/Config.mk passes '-u __wrap___emutls_get_address' and
'-u __wrap___emutls_register_common', unconditionally forcing
tls_emutls.o and its dependencies (realloc, memmove, semaphores) into
every linked image, even when __thread variables are never used.

On memory-constrained targets (CONFIG_DEFAULT_SMALL=y), this wastes
up to ~1.5 KB of Flash. For example, on nucleo-f302r8 (DEFAULT_SMALL=y,
LTO_FULL=y):
  CONFIG_TLS_EMUTLS=y: 23208 B Flash, 1160 B SRAM
  CONFIG_TLS_EMUTLS=n: 21752 B Flash, 1120 B SRAM
Difference: -1456 B Flash (-6.7%).

This patch changes CONFIG_TLS_EMUTLS to default to !DEFAULT_SMALL,
saving Flash across small configurations while keeping emulated TLS
enabled by default on standard configurations. Targets requiring
per-thread __thread emulation can still explicitly enable it in their
defconfig.

Related to apache#20414
Fixes flash bloat reported in apache#20723

Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swatantra Yadav <[email protected]>
@xiaoxiang781216
xiaoxiang781216 merged commit fb2b61c into apache:master Nov 20, 2026
55 of 64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Board: arm Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants