From patchwork Tue May 26 23:04:02 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 135769 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 592CB4BA2E2F for ; Tue, 26 May 2026 23:05:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 592CB4BA2E2F 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=FjtI/6C+ X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-vk1-xa32.google.com (mail-vk1-xa32.google.com [IPv6:2607:f8b0:4864:20::a32]) by sourceware.org (Postfix) with ESMTPS id CA4CB4BA2E2E for ; Tue, 26 May 2026 23:04:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CA4CB4BA2E2E 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 CA4CB4BA2E2E Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::a32 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779836689; cv=none; b=R+KjFDWHcpjCJUNh3JXK9xrC7v7rKYH1nvCvdUr8/GIsUi9blOZ8+icaT8Fun24+A7O/EZMr7eGyIpx3if9XrpkgijYEL0DPInRvw2lnGa2BRVUVznwZ1JXg+NQPCVRUaFcFBv6mH0hnbKrOPynh2sqAs/YaH0SGvpPGoTNLbzk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779836689; c=relaxed/simple; bh=9jGO0CegVXryRU13jonixMRcJ2d1bgLmkZt0PEOGfYY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Ybnez2DfZs2/HcqN8NnJ9q284O2SpdM2RIQeG8CnoGO2GG5DAWDV8/grwwn75k6Kt3LdB8cNnWdhSq+cgNxEzrl9mBWkj9fVyBCWWf7teHReze8BtvmpoDlZwy+xGmJMzqDCl2UAUTt+4gFO3FjMam6UdH12QQ9htBlMS1Y7k9Y= 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=FjtI/6C+ DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA4CB4BA2E2E Received: by mail-vk1-xa32.google.com with SMTP id 71dfb90a1353d-57524e52a3dso9295480e0c.2 for ; Tue, 26 May 2026 16:04:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1779836688; x=1780441488; 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=TBefGdK8E3hep5YFaS38Cy5J4p4Y3sz55QAOw2Kx8q8=; b=FjtI/6C++zrR0vkk3h7ABx0ThzjENkolYyQCyvvLGMM8fSnCd1I/0EHl8sa9UcoZ7k Tl5SFo8Kvn7432Qg11c4X9ZIdy/bQYBbJaaARsn6RmR6CEPQkEfAbK2q0wOqZqrwA3RF Zd6mzc2Bd40T60zFogMue9Ff15Eoytwx5XvhCzvEgsx2TQP5L6uRO2WRfSiA2CDq2ev+ F7GnofRpddyZoPLQ1QCtuqtTFSkXOGhYE4KvVRgVwmKyFXQUB6v1sjGjUUN4NJSD/N3H iNMVI3khK2AScFnM+X3fafGXYKrtXyD3laYlE/QS2N7sKP2SaOdRjjufhr23LMJ5h20k qM0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779836688; x=1780441488; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=TBefGdK8E3hep5YFaS38Cy5J4p4Y3sz55QAOw2Kx8q8=; b=EALIlh+KSoS8wL5lCZRMGGECJSwztbGpH6bugtJXU3iPmxP/NBCtkcIkZg7kqCu3rc K4P764iKiHl51Izp3Pw5jUlcLjtYCcPehu1Fj8L0M6gfnjsCg6jh6P5Vk+YHX/Y31npK CrrRi2LE/8BqKae3PlmMwhUX7if6ifup6rNGheBRoshCTC/ly25ynjpr7Hj69SvLhbXv EF/LMLpTXTeCHBmu9jFuTke2QG2LDAbus6IXsHnBiqT5zRdMMhuZgivEZNSzdlQyb4dJ T/SJHSA3+gmzqXsk3pSoeTpBpgEDzVHRbxPqNXUb+YvVQcXDeRQYNqMLKAmy6BJpvgd2 94WA== X-Gm-Message-State: AOJu0YwtIGQB4v+kuQQlgSeQ17lXqMgsrRzoqbRyerVSJMPHRMVZT+fT VAU+unDFaCDOFlis1MfFlOoHvYYwcyA8AZkbQmQUJrlZZy/D8cNh3yekBpk2TcyXS9uMEVA3Qyw r4ik9 X-Gm-Gg: Acq92OEW3fea7F//rR3b4tJXk4Zi2TXqwL9vmFiO/6tcwPZSNv5UQeO5BrstzNyaW9B /wK0WykojOY4idME/vqSy3il0dUE1dmMKnqraC4SDDhTfAc8I4yk8UEM6hPplc8rIhLjK2kobY/ O6VWCpDjWeZYk/yU4yBmnxTFDJtmn0G83RRpBda71HSoM0mkIE66H1AbQkOlawUaexv4JqV+Fnm CtgDMRWyfxpzeSvmYuDiDp3DIGX0AWUWOJqqodNhjD61upJDaBOGmvEcg2+vBotNJb9Gbzp7J45 B9qQvBkr62j1O+Bf4ZTmZVnqxUQ1aueex1qBLw14xxuodG/L3FiPLFvXPaY5WoXkPiVVP1DH5Wh CYctpsu10a2KUY22XRtUlXFT1V5zPp+PDqeXbZyXRpiow2RFFY66mUEqGwb2Jt9hCL6vH27oSXg h5/ZqlfMOBpKEf+o8IKRPUXnG0u4ptUjs2HPnLNJBjpslYtwQ0FCHVrthZ X-Received: by 2002:a05:6122:e149:b0:56e:8afc:de8c with SMTP id 71dfb90a1353d-5865f62586cmr10792893e0c.3.1779836688071; Tue, 26 May 2026 16:04:48 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:efc6:3e7d:d0ac:c69d:64f3]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-586f85bde2fsm18144426e0c.13.2026.05.26.16.04.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2026 16:04:47 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Andreas Schwab , "H . J . Lu" Subject: [PATCH v2] elf: Re-initialise static TLS after .tdata relocation (BZ 34164) Date: Tue, 26 May 2026 20:04:02 -0300 Message-ID: <20260526230442.17610-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_BLOCKED, 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 (BZ 34164) changed the TLS 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. Reviewed-by: H.J. Lu --- Changes from v1: * Mention BZ 34164 on commit body. --- 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