Skip to content

Provide per-core thread local variables when using picolibc#2846

Open
keith-packard wants to merge 7 commits intoraspberrypi:developfrom
keith-packard:picolibc-per-core-tls
Open

Provide per-core thread local variables when using picolibc#2846
keith-packard wants to merge 7 commits intoraspberrypi:developfrom
keith-packard:picolibc-per-core-tls

Conversation

@keith-packard
Copy link
Copy Markdown

This PR contains a single patch atop #2840 to allocate per-core thread local storage which is necessary for safe thread local variable usage (like errno).

Change the linker scripts to statically allocate thread local storage space for each core and to initialize that using picolibc's _init_tls instead wedging it between .data and .bss.

Implement pico-specific __aeabi_read_tp and_set_tls functions which use the core ID register at 0xd0000000 to index an array of TLS base pointers.

Perform per-core TLS region setup, calling _init_tls and _set_tls using the areas allocated by the linker script.

nosys.specs controls how newlib performs linking, so it's not relevant
to other C libraries.

Signed-off-by: Keith Packard <keithp@keithp.com>
Picolibc doesn't provide __printflike, instead it provides a more
general __picolibc_format for either scanf or printf. Use that to
define our own __printflike version.

Signed-off-by: Keith Packard <keithp@keithp.com>

Gbp-Pq: Name 0005-pico_platform_compiler-Picolibc-compatibility-fix.patch
The kitchen_sink test enables -Wredundant-decls which generates
a warning about the stdout and stderr alias definitions. Disable the
warning as it is incorrect in this case.

Signed-off-by: Keith Packard <keithp@keithp.com>
Using -Wl,--script= hides this option from the compiler driver causing
it to insert it's own linker script when it uses picolibc. Using
the -T option asks it to skip adding its own.

Signed-off-by: Keith Packard <keithp@keithp.com>
Picolibc may combine the preinit and init arrays into a single list,
in which case it will use __bothinit_array_start and
__bothinit_array_end symbols instead of the separate
__preinit_array_start, __preinit_array_end and __init_array_start,
__init_array_end pairs.

Define both so that either method works.

Signed-off-by: Keith Packard <keithp@keithp.com>
When using pico_printf_compiler with picolibc, map the
various RUNTIME_INCLUDE_PRINTF_* options into the matching
picolibc link flags.

Signed-off-by: Keith Packard <keithp@keithp.com>
Change the linker scripts to statically allocate thread local storage
space for each core and to initialize that using picolibc's _init_tls
instead wedging it between .data and .bss.

Implement pico-specific __aeabi_read_tp and_set_tls functions which
use the core ID register at 0xd0000000 to index an array of TLS base
pointers.

Perform per-core TLS region setup, calling _init_tls and _set_tls
using the areas allocated by the linker script.

Signed-off-by: Keith Packard <keithp@keithp.com>
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 2, 2026

Please do not submit against master, use develop instead

@keith-packard keith-packard changed the base branch from master to develop March 2, 2026 23:48
Comment on lines +222 to +235
# Thread local storage static allocations, one per core

.tls0 (NOLOAD) : {
. = ALIGN(__tls_align);
PROVIDE(__tls0_base = .);
. = . + __tls_size_align;
} > RAM

.tls1 (NOLOAD) : {
. = ALIGN(__tls_align);
PROVIDE(__tls1_base = .);
. = . + __tls_size_align;
} > RAM

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to put these sections between data and bss, so that bss and heap are contiguous? Otherwise, this may break compatibility with some other changes that are being worked on.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but that might break code which assumes that data and bss are contiguous?

@kilograham kilograham added this to the 2.2.1 milestone Mar 5, 2026
@kilograham kilograham self-assigned this Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants