From patchwork Tue May 26 18:52:14 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 135759 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1BEDF4BA23CC for ; Tue, 26 May 2026 18:53:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1BEDF4BA23CC Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=MS2/QS67 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-vk1-xa29.google.com (mail-vk1-xa29.google.com [IPv6:2607:f8b0:4864:20::a29]) by sourceware.org (Postfix) with ESMTPS id 163524BA2E0D for ; Tue, 26 May 2026 18:52:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 163524BA2E0D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 163524BA2E0D Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::a29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779821561; cv=none; b=FMI/W2pgciynE++9oqROeChlsZRp3dee17sPV4zptYSwsQKfeoR4NpLrsTKc+CX7ZI4SqPx9tWYQ/3TatTnTe2cu2+WDUenDCZywNuamAGf5zG9ZFZW2ujJyrerRDggEcQpYTrs52HAAotgkCOM6Lus5gJoeZ8XC9sdwkKreUik= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779821561; c=relaxed/simple; bh=Moe0QivD0v280crEzW8rwf4HMHo+/hvBLKZ99Uc06BQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=cqFJJRaEZIzm3z64So2/VUwhqUFYtxlANsI2/HsDexpnPK90Ezy4xRXSaKYjiP8jfphWQzphgMV9M/JJXl+hVzIFdGsyqpESeN3dO2T7W67/97hBLR37WqVbI9cdj7p+R2u05LCU0DhV1JTpX5OooQ1utccXrxoMhQGsp0LEYrs= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=MS2/QS67 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 163524BA2E0D Received: by mail-vk1-xa29.google.com with SMTP id 71dfb90a1353d-57513ac61f0so3572559e0c.3 for ; Tue, 26 May 2026 11:52:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1779821560; x=1780426360; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=UEChwUIsHO6lcexOdaUGEk3hYDUDkQebguA47LK8Dtw=; b=MS2/QS6736PZ/hCGZoEsu1KTvBUiqIlKLm+JyUCOlgWAw0xVrIpL78st2KJAkqp6O+ FkhuJE9ojZxW3jH18M2NF9sb+ct59nUa5MQU1sWQs/fSCEZ2YJWqZDxYWSYUWDSB1yG/ zhA8e4Az6ZGiqSaDVAYsrGAyyAqCFLKMDqoA67iSFj+up7u/x8/ZyD22BY13oiv6KdAC YlvhkPG4GG96jThjzqYjaV0Qvp1iuaCO4Kf268ZqulaZP/7oXF7mbbcKQ2AWu8l9EM87 luHjw46S/oMmYQSieRgZVxucpfySIk8qHqCBIQ9VGhGO3qi4UAE8zSygvr6lANYm5fAe iYWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779821560; x=1780426360; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg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U8= X-Received: by 2002:a05:6122:f8b:b0:56d:3b69:87d2 with SMTP id 71dfb90a1353d-58662ea14aemr10772038e0c.11.1779821560398; Tue, 26 May 2026 11:52:40 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:efc6:3e7d:d0ac:c69d:64f3]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-961737bc13fsm14665680241.3.2026.05.26.11.52.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2026 11:52:39 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Andreas Schwab , "H . J . Lu" Subject: [PATCH] elf: Re-initialise static TLS after .tdata relocation (BZ 34164) Date: Tue, 26 May 2026 15:52:14 -0300 Message-ID: <20260526185235.4093779-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLOCKED shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org The af34b1376a37fa27e1de9d869ed9493fc569bfa6 changed the .tdata setup from: relocation loop (applies relocations to .tdata in DSO memory) _dl_allocate_tls_init copies relocated .tdata -> main thread TLS to a new order: _dl_allocate_tls_init copies unrelocated .tdata -> main thread TLS relocation loop (relocates .tdata in DSO memory, but the TLS block has stale copies) This broke file-scope thread-local initialised with the address of a function (for instance the cache structs in libmpfr). Fix it by splitting ELF_DYNAMIC_RELOCATE inside _dl_relocate_object_no_relro into the non-IRELATIVE and IRELATIVE sub-passes (similar as done on static-pie startup by b75ad99d45b) and call _dl_init_static_tls between them. By the time the IFUNC pass fires, .tdata is fully relocated. Checked on x86_64-linux-gnu and aarch64-linux-gnu. --- elf/Makefile | 3 +++ elf/dl-reloc.c | 24 ++++++++++++++++++-- elf/tst-tls-tdata-reloc-lib.c | 42 +++++++++++++++++++++++++++++++++++ elf/tst-tls-tdata-reloc.c | 32 ++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 elf/tst-tls-tdata-reloc-lib.c create mode 100644 elf/tst-tls-tdata-reloc.c diff --git a/elf/Makefile b/elf/Makefile index d7bab52cd97..a58a902451c 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -1297,6 +1297,7 @@ tests += \ tst-ifunc-resolver-protector \ tst-ifunc-tls-init \ tst-ifunc-tls-write \ + tst-tls-tdata-reloc \ # tests # Note: sysdeps/x86_64/ifuncmain8.c uses ifuncmain8. tests-internal += \ @@ -1365,6 +1366,7 @@ modules-names += \ tst-ifunc-tls-init-lib1 \ tst-ifunc-tls-init-lib2 \ tst-ifunc-tls-write-lib \ + tst-tls-tdata-reloc-lib \ # modules-names ifeq (no,$(with-lld)) modules-names += ifuncmod5 @@ -2518,6 +2520,7 @@ $(objpfx)tst-ifunc-tls-init: $(objpfx)tst-ifunc-tls-init-lib1.so $(objpfx)tst-ifunc-tls-init.out: \ $(objpfx)tst-ifunc-tls-init-lib2.so $(objpfx)tst-ifunc-tls-write: $(objpfx)tst-ifunc-tls-write-lib.so +$(objpfx)tst-tls-tdata-reloc: $(objpfx)tst-tls-tdata-reloc-lib.so $(objpfx)tst-unique1.out: $(objpfx)tst-unique1mod1.so \ $(objpfx)tst-unique1mod2.so diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c index 91497b38397..15a6a4cffef 100644 --- a/elf/dl-reloc.c +++ b/elf/dl-reloc.c @@ -269,9 +269,29 @@ _dl_relocate_object_no_relro (struct link_map *l, struct r_scope_elem *scope[], } { - /* Do the actual relocation of the object's GOT and other data. */ + /* Do the actual relocation of the object's GOT and other data. - ELF_DYNAMIC_RELOCATE (l, scope, lazy, consider_profiling, skip_ifunc); + Process the non-IRELATIVE pass first so .tdata is fully relocated + (including R_*_RELATIVE / R_*_64 fixups for TLS initialisers, e.g. a + file-scope thread-local initialised with the address of a function), + then refresh the static TLS slot before the IRELATIVE pass runs the + IFUNC resolvers. Without this, a resolver would see the unrelocated + initialiser bytes that were placed into the slot by the early + _dl_allocate_tls_init. */ + ELF_DYNAMIC_RELOCATE_NOIFUNC (l, scope, lazy, consider_profiling); + +#ifdef SHARED + /* Re-initialise the static TLS slot with the .tdata so the IRELATIVE + pass observes a fully-relocated initialiser image. Skipped for objects + without static TLS or before the main thread TCB has been set up. */ + if (l->l_tls_blocksize != 0 + && __rtld_tls_init_tp_called + && l->l_tls_offset != NO_TLS_OFFSET + && l->l_tls_offset != FORCED_DYNAMIC_TLS_OFFSET) + _dl_init_static_tls (l); +#endif + + ELF_DYNAMIC_RELOCATE_IFUNC (l, scope, lazy, skip_ifunc); if ((consider_profiling || consider_symbind) && l->l_info[DT_PLTRELSZ] != NULL) diff --git a/elf/tst-tls-tdata-reloc-lib.c b/elf/tst-tls-tdata-reloc-lib.c new file mode 100644 index 00000000000..73aa58b0722 --- /dev/null +++ b/elf/tst-tls-tdata-reloc-lib.c @@ -0,0 +1,42 @@ +/* Shared library for tst-tls-tdata-reloc. + Copyright (C) 2026 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#define EXPECTED 0x12345678 + +static int +callee (void) +{ + return EXPECTED; +} + +typedef struct +{ + int (*func) (void); +} cache_t; + +/* General-dynamic TLS: the initialiser of '.func' produces an + R_*_RELATIVE on .tdata. */ +__thread cache_t cache = { .func = callee }; + +int +call_through_tls (void) +{ + if (cache.func == 0) + return -1; + return cache.func (); +} diff --git a/elf/tst-tls-tdata-reloc.c b/elf/tst-tls-tdata-reloc.c new file mode 100644 index 00000000000..1d38726e364 --- /dev/null +++ b/elf/tst-tls-tdata-reloc.c @@ -0,0 +1,32 @@ +/* Check that .tdata relocations are applied to the static TLS slot. + Copyright (C) 2026 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#define EXPECTED 0x12345678 + +extern int call_through_tls (void); + +static int +do_test (void) +{ + TEST_COMPARE (call_through_tls (), EXPECTED); + return 0; +} + +#include