| Message ID | 20250909203836.3988300-1-hjl.tools@gmail.com |
|---|---|
| State | Committed |
| Headers |
Return-Path: <binutils-bounces~patchwork=sourceware.org@sourceware.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ADBD13858D35 for <patchwork@sourceware.org>; Tue, 9 Sep 2025 20:40:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ADBD13858D35 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=aN8v/kDm X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by sourceware.org (Postfix) with ESMTPS id 856233858D35 for <binutils@sourceware.org>; Tue, 9 Sep 2025 20:38:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 856233858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 856233858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::52e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757450324; cv=none; b=Y0ZGvVLk/ETdp2G1GX7LHTdrTeYCU9PJ1yBpJb+HgESremRQZEI9oDZLc2R9tzeoyTL4MMu+wzIqfO8vwJ7A8/ObpicDxMP3uIpzI72VoRYz6i4nee/TuWPclyKN9cZZaBUNwb3G0LZqgp71d0pnkPL/hSULUwdLsX5EdygeQ4k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757450324; c=relaxed/simple; bh=Dv1ttzZJomXNdP1Dug/rvFFAGhkmc+zdv2nL8+ssyYk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=om2gSkHZsPFKRN9X9yP5bH9YOdKiRXUOyoC/iPSb/lNXa7ZAtrk4rCoZbWPblqBy3K8BOM75jStfrpbeCOHeKRrDHFOBJOVyBV/mL7yMuntH2o9xmEPo4vMZmaM/EardKiGWKMyQDshANOZLnyykp7pI2X3G0DknFj1s7bYG2bs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 856233858D35 Received: by mail-pg1-x52e.google.com with SMTP id 41be03b00d2f7-b4fa6cd2377so4047167a12.2 for <binutils@sourceware.org>; Tue, 09 Sep 2025 13:38:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1757450323; x=1758055123; 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=+mwjjwX4p3NfjOrtjuWs4hcEzJ9HubenYZPhBds2bQg=; b=aN8v/kDm4dv1/kJ7JBTaHAp/LSMO0To3kZAFb2aikA0KRPgIEXq4KktmeSiBzpJDBd qoFurHbvhjLDcC1iEB0HYd9bnNLpYWnmZTKJ4kZoIx8XkBkGrWzacXl22o8yrCSi663K yh+U+sp8h3zs7NclIYlDVuSWeOimVBNbST7Yi6RvuUOQkLq1zpP8OD2LB0NH5l6Dx4on DKefpPheV4JG9FMUfx78bmMNBph7oR7YH4eh8ZTKdjRnk7ZysgKydcp2LTjo522X75F/ JRlR0n8OG0uzKI/ExSZFjiCDePf+gKr3fn1Vqh5kz8slBlD3QyjEd2hEpLGqyvncPRUM im1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757450323; x=1758055123; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+mwjjwX4p3NfjOrtjuWs4hcEzJ9HubenYZPhBds2bQg=; b=BN7EsOTrCfQv1g63li0If4BgT1kp1stUD6wzwO9AxvRVoGNbpDRgNmFU0T5x6fgGV4 f4c/+Tw/DQwmzfoKrU8JDdP7ErNFONjeJEuURZtP6yMB0pIXbtnRippZRbQWymzI6Wj2 P8gRPbXWLpxBzlLz3SPqKRzV7W+qNN8RmcuOE99IlsG3UyLdtWfpp0llpow5VaCDaYV0 /pyjGmJqAILlyL7Gr54rQq/oO5T5SHfO2VcFtUtfXQAu0E75o0n3fbHyKXRv97CnH/CE c17aDsUhnas0NxyJFnKJimAAtc9ZtBxercCK+fc8okrepiZ8HaFaupepSXmHgNwEjUw3 CuQA== X-Gm-Message-State: AOJu0YySdA8spEXihIB+hgrZfpVkOe55noS/oEKRaWPKkqIjafUFgKAG 7RQyPH6X/Egcis+vESsp+mBCotJd6PAB/GtVOZAoScpJ7rpWwvpjBqJ1N/enzA== X-Gm-Gg: ASbGncuNWMfMGSzs9CiqRonysyEYhsa6Iz7tYEfMs/wTOVAXTYrtNmdue3VPCaK7qhU oixfjku5DKNR7e/2/vgoznhghwMwBnPW2/9RdNG2J/pymA/uAOKQZPAINN+CS4lg5+zFM1uzJuj jp6Z7Tm76Gb9vxvk9RUZ+hESN+cCJeUoffqhl1nhsLtJxFkWrNsRZAQTP/RLH+EOzTSjRxzIV22 URFjddxcAZ60J2BDp9WWWXt1vcbxUypJkT3gcJB8ik3bGt1dn5/LipU1dhLD7WOqv8adyisDR6t aoqdmNxBiMi5rbq9OfAxjfHb732V0tEay11Enp7J5NF/LvXHoSxbTVlYY6Qa12ke8T6jHh5mlbd BY+8izRXdf4ImGgkjLLCaFZUF2+wM7OnBboZzE6aEVYU5VBoYLA== X-Google-Smtp-Source: AGHT+IFUp3v2dOZ52mvGgdwNdvz3JrP9UpXQ58LTgbayIsO8nLjOZU7VKZV038DZfy4MCAJHRT/4Jg== X-Received: by 2002:a17:902:ce12:b0:248:7bf8:8db0 with SMTP id d9443c01a7336-251759a9cf6mr152917175ad.52.1757450322978; Tue, 09 Sep 2025 13:38:42 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.59.160.12]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-32dbb49a661sm42781a91.29.2025.09.09.13.38.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Sep 2025 13:38:42 -0700 (PDT) Received: from gnu-cfl-3.localdomain (localhost [127.0.0.1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 8C07F740272; Tue, 09 Sep 2025 13:38:41 -0700 (PDT) From: "H.J. Lu" <hjl.tools@gmail.com> To: binutils@sourceware.org Cc: jbeulich@suse.com Subject: [PATCH] x86-64: Fix misleading R_X86_64_TPOFF32 error message Date: Tue, 9 Sep 2025 13:38:36 -0700 Message-ID: <20250909203836.3988300-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.51.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3015.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list <binutils.sourceware.org> List-Unsubscribe: <https://sourceware.org/mailman/options/binutils>, <mailto:binutils-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/binutils/> List-Post: <mailto:binutils@sourceware.org> List-Help: <mailto:binutils-request@sourceware.org?subject=help> List-Subscribe: <https://sourceware.org/mailman/listinfo/binutils>, <mailto:binutils-request@sourceware.org?subject=subscribe> Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org |
| Series |
x86-64: Fix misleading R_X86_64_TPOFF32 error message
|
|
Commit Message
H.J. Lu
Sept. 9, 2025, 8:38 p.m. UTC
R_X86_64_TPOFF32 relocation of local-exec TLS model can only be used in
executable, not in a shared library, even if the source code is compiled
with -fPIC. Change the linker error message from
relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; recompile with -fPIC
to
relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model
bfd/
PR ld/33408
* elf64-x86-64.c (elf_x86_64_need_pic): Suggest "replace
local-exec with initial-exec TLS model" for R_X86_64_TPOFF32.
(elf_x86_64_scan_relocs): Drop ABI_64_P check for
R_X86_64_TPOFF32.
ld/
PR ld/33408
* testsuite/ld-x86-64/tls-le-pic-1-x32.d: New file.
* testsuite/ld-x86-64/tls-le-pic-1.d: Likewise.
* testsuite/ld-x86-64/tls-le-pic-1.s: Likewise.
* testsuite/ld-x86-64/tls-le-pic-2-x32.d: Likewise.
* testsuite/ld-x86-64/tls-le-pic-2.d: Likewise.
* testsuite/ld-x86-64/tls-le-pic-2.s: Likewise.
* testsuite/ld-x86-64/tls-le-pic-3-x32.d: Likewise.
* testsuite/ld-x86-64/tls-le-pic-3.d: Likewise.
* testsuite/ld-x86-64/tls-le-pic-3.s: Likewise.
Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
bfd/elf64-x86-64.c | 6 ++++--
ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d | 4 ++++
ld/testsuite/ld-x86-64/tls-le-pic-1.d | 3 +++
ld/testsuite/ld-x86-64/tls-le-pic-1.s | 17 +++++++++++++++++
ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d | 4 ++++
ld/testsuite/ld-x86-64/tls-le-pic-2.d | 3 +++
ld/testsuite/ld-x86-64/tls-le-pic-2.s | 10 ++++++++++
ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d | 4 ++++
ld/testsuite/ld-x86-64/tls-le-pic-3.d | 3 +++
ld/testsuite/ld-x86-64/tls-le-pic-3.s | 16 ++++++++++++++++
ld/testsuite/ld-x86-64/x86-64.exp | 6 ++++++
11 files changed, 74 insertions(+), 2 deletions(-)
create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d
create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-1.d
create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-1.s
create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d
create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-2.d
create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-2.s
create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d
create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-3.d
create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-3.s
Comments
On Tue, Sep 9, 2025 at 1:38 PM H.J. Lu <hjl.tools@gmail.com> wrote: > > R_X86_64_TPOFF32 relocation of local-exec TLS model can only be used in > executable, not in a shared library, even if the source code is compiled > with -fPIC. Change the linker error message from > > relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; recompile with -fPIC > > to > > relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > bfd/ > > PR ld/33408 > * elf64-x86-64.c (elf_x86_64_need_pic): Suggest "replace > local-exec with initial-exec TLS model" for R_X86_64_TPOFF32. > (elf_x86_64_scan_relocs): Drop ABI_64_P check for > R_X86_64_TPOFF32. > > ld/ > > PR ld/33408 > * testsuite/ld-x86-64/tls-le-pic-1-x32.d: New file. > * testsuite/ld-x86-64/tls-le-pic-1.d: Likewise. > * testsuite/ld-x86-64/tls-le-pic-1.s: Likewise. > * testsuite/ld-x86-64/tls-le-pic-2-x32.d: Likewise. > * testsuite/ld-x86-64/tls-le-pic-2.d: Likewise. > * testsuite/ld-x86-64/tls-le-pic-2.s: Likewise. > * testsuite/ld-x86-64/tls-le-pic-3-x32.d: Likewise. > * testsuite/ld-x86-64/tls-le-pic-3.d: Likewise. > * testsuite/ld-x86-64/tls-le-pic-3.s: Likewise. > > Signed-off-by: H.J. Lu <hjl.tools@gmail.com> > --- > bfd/elf64-x86-64.c | 6 ++++-- > ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d | 4 ++++ > ld/testsuite/ld-x86-64/tls-le-pic-1.d | 3 +++ > ld/testsuite/ld-x86-64/tls-le-pic-1.s | 17 +++++++++++++++++ > ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d | 4 ++++ > ld/testsuite/ld-x86-64/tls-le-pic-2.d | 3 +++ > ld/testsuite/ld-x86-64/tls-le-pic-2.s | 10 ++++++++++ > ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d | 4 ++++ > ld/testsuite/ld-x86-64/tls-le-pic-3.d | 3 +++ > ld/testsuite/ld-x86-64/tls-le-pic-3.s | 16 ++++++++++++++++ > ld/testsuite/ld-x86-64/x86-64.exp | 6 ++++++ > 11 files changed, 74 insertions(+), 2 deletions(-) > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-1.d > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-1.s > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-2.d > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-2.s > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-3.d > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-3.s > > diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c > index 00586455203..ad99cf6754e 100644 > --- a/bfd/elf64-x86-64.c > +++ b/bfd/elf64-x86-64.c > @@ -1717,7 +1717,9 @@ elf_x86_64_need_pic (struct bfd_link_info *info, > { > object = _("a shared object"); > if (!pic) > - pic = _("; recompile with -fPIC"); > + pic = (howto->type == R_X86_64_TPOFF32 > + ? _("; replace local-exec with initial-exec TLS model") > + : _("; recompile with -fPIC")); > } > else > { > @@ -2684,7 +2686,7 @@ elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info, > goto create_got; > > case R_X86_64_TPOFF32: > - if (!bfd_link_executable (info) && ABI_64_P (abfd)) > + if (!bfd_link_executable (info)) > { > elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym, > &x86_64_elf_howto_table[r_type]); > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d b/ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d > new file mode 100644 > index 00000000000..b0824c4ae10 > --- /dev/null > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d > @@ -0,0 +1,4 @@ > +#source: tls-le-pic-1.s > +#as: --x32 > +#ld: -shared -melf32_x86_64 > +#error: .*: relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-1.d b/ld/testsuite/ld-x86-64/tls-le-pic-1.d > new file mode 100644 > index 00000000000..6db5f029e75 > --- /dev/null > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-1.d > @@ -0,0 +1,3 @@ > +#as: --64 > +#ld: -shared -melf_x86_64 > +#error: .*: relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-1.s b/ld/testsuite/ld-x86-64/tls-le-pic-1.s > new file mode 100644 > index 00000000000..ffc8e63b3b4 > --- /dev/null > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-1.s > @@ -0,0 +1,17 @@ > + .text > + .p2align 4 > + .globl func > + .type func, @function > +func: > + movq %fs:0, %rax > + addq $foo@tpoff, %rax > + ret > + .size func, .-func > + .section .tbss,"awT",@nobits > + .align 4 > + .globl foo > + .type foo, @object > + .size foo, 4 > +foo: > + .zero 4 > + .section .note.GNU-stack,"",@progbits > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d b/ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d > new file mode 100644 > index 00000000000..e16395a5568 > --- /dev/null > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d > @@ -0,0 +1,4 @@ > +#source: tls-le-pic-2.s > +#as: --x32 > +#ld: -shared -melf32_x86_64 > +#error: .*: relocation R_X86_64_TPOFF32 against undefined symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-2.d b/ld/testsuite/ld-x86-64/tls-le-pic-2.d > new file mode 100644 > index 00000000000..0e718e01ebc > --- /dev/null > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-2.d > @@ -0,0 +1,3 @@ > +#as: --64 > +#ld: -shared -melf_x86_64 > +#error: .*: relocation R_X86_64_TPOFF32 against undefined symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-2.s b/ld/testsuite/ld-x86-64/tls-le-pic-2.s > new file mode 100644 > index 00000000000..ad930db1546 > --- /dev/null > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-2.s > @@ -0,0 +1,10 @@ > + .text > + .p2align 4 > + .globl func > + .type func, @function > +func: > + movq %fs:0, %rax > + addq $foo@tpoff, %rax > + ret > + .size func, .-func > + .section .note.GNU-stack,"",@progbits > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d b/ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d > new file mode 100644 > index 00000000000..f6731daa991 > --- /dev/null > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d > @@ -0,0 +1,4 @@ > +#source: tls-le-pic-3.s > +#as: --x32 > +#ld: -shared -melf32_x86_64 > +#error: .*: relocation R_X86_64_TPOFF32 against `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-3.d b/ld/testsuite/ld-x86-64/tls-le-pic-3.d > new file mode 100644 > index 00000000000..987e370ad17 > --- /dev/null > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-3.d > @@ -0,0 +1,3 @@ > +#as: --64 > +#ld: -shared -melf_x86_64 > +#error: .*: relocation R_X86_64_TPOFF32 against `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-3.s b/ld/testsuite/ld-x86-64/tls-le-pic-3.s > new file mode 100644 > index 00000000000..6e213fd5b54 > --- /dev/null > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-3.s > @@ -0,0 +1,16 @@ > + .text > + .p2align 4 > + .globl func > + .type func, @function > +func: > + movq %fs:0, %rax > + addq $foo@tpoff, %rax > + ret > + .size func, .-func > + .section .tbss,"awT",@nobits > + .align 4 > + .type foo, @object > + .size foo, 4 > +foo: > + .zero 4 > + .section .note.GNU-stack,"",@progbits > diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp > index 23e0a98d1cd..f04ae923ebc 100644 > --- a/ld/testsuite/ld-x86-64/x86-64.exp > +++ b/ld/testsuite/ld-x86-64/x86-64.exp > @@ -578,6 +578,12 @@ run_dump_test "pr33292" > run_dump_test "pr33292-x32" > run_dump_test "pr28387" > run_dump_test "pr28387-x32" > +run_dump_test "tls-le-pic-1" > +run_dump_test "tls-le-pic-1-x32" > +run_dump_test "tls-le-pic-2" > +run_dump_test "tls-le-pic-2-x32" > +run_dump_test "tls-le-pic-3" > +run_dump_test "tls-le-pic-3-x32" > > if { ![skip_sframe_tests] } { > run_dump_test "sframe-simple-1" > -- > 2.51.0 > I am checking it in.
On Wed, Sep 10, 2025 at 6:45 AM H.J. Lu <hjl.tools@gmail.com> wrote: > > On Tue, Sep 9, 2025 at 1:38 PM H.J. Lu <hjl.tools@gmail.com> wrote: > > > > R_X86_64_TPOFF32 relocation of local-exec TLS model can only be used in > > executable, not in a shared library, even if the source code is compiled > > with -fPIC. Change the linker error message from > > > > relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; recompile with -fPIC > > > > to > > > > relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > > > bfd/ > > > > PR ld/33408 > > * elf64-x86-64.c (elf_x86_64_need_pic): Suggest "replace > > local-exec with initial-exec TLS model" for R_X86_64_TPOFF32. > > (elf_x86_64_scan_relocs): Drop ABI_64_P check for > > R_X86_64_TPOFF32. > > > > ld/ > > > > PR ld/33408 > > * testsuite/ld-x86-64/tls-le-pic-1-x32.d: New file. > > * testsuite/ld-x86-64/tls-le-pic-1.d: Likewise. > > * testsuite/ld-x86-64/tls-le-pic-1.s: Likewise. > > * testsuite/ld-x86-64/tls-le-pic-2-x32.d: Likewise. > > * testsuite/ld-x86-64/tls-le-pic-2.d: Likewise. > > * testsuite/ld-x86-64/tls-le-pic-2.s: Likewise. > > * testsuite/ld-x86-64/tls-le-pic-3-x32.d: Likewise. > > * testsuite/ld-x86-64/tls-le-pic-3.d: Likewise. > > * testsuite/ld-x86-64/tls-le-pic-3.s: Likewise. > > > > Signed-off-by: H.J. Lu <hjl.tools@gmail.com> > > --- > > bfd/elf64-x86-64.c | 6 ++++-- > > ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d | 4 ++++ > > ld/testsuite/ld-x86-64/tls-le-pic-1.d | 3 +++ > > ld/testsuite/ld-x86-64/tls-le-pic-1.s | 17 +++++++++++++++++ > > ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d | 4 ++++ > > ld/testsuite/ld-x86-64/tls-le-pic-2.d | 3 +++ > > ld/testsuite/ld-x86-64/tls-le-pic-2.s | 10 ++++++++++ > > ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d | 4 ++++ > > ld/testsuite/ld-x86-64/tls-le-pic-3.d | 3 +++ > > ld/testsuite/ld-x86-64/tls-le-pic-3.s | 16 ++++++++++++++++ > > ld/testsuite/ld-x86-64/x86-64.exp | 6 ++++++ > > 11 files changed, 74 insertions(+), 2 deletions(-) > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-1.d > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-1.s > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-2.d > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-2.s > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-3.d > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-3.s > > > > diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c > > index 00586455203..ad99cf6754e 100644 > > --- a/bfd/elf64-x86-64.c > > +++ b/bfd/elf64-x86-64.c > > @@ -1717,7 +1717,9 @@ elf_x86_64_need_pic (struct bfd_link_info *info, > > { > > object = _("a shared object"); > > if (!pic) > > - pic = _("; recompile with -fPIC"); > > + pic = (howto->type == R_X86_64_TPOFF32 > > + ? _("; replace local-exec with initial-exec TLS model") > > + : _("; recompile with -fPIC")); > > } > > else > > { > > @@ -2684,7 +2686,7 @@ elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info, > > goto create_got; > > > > case R_X86_64_TPOFF32: > > - if (!bfd_link_executable (info) && ABI_64_P (abfd)) > > + if (!bfd_link_executable (info)) > > { > > elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym, > > &x86_64_elf_howto_table[r_type]); > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d b/ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d > > new file mode 100644 > > index 00000000000..b0824c4ae10 > > --- /dev/null > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d > > @@ -0,0 +1,4 @@ > > +#source: tls-le-pic-1.s > > +#as: --x32 > > +#ld: -shared -melf32_x86_64 > > +#error: .*: relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-1.d b/ld/testsuite/ld-x86-64/tls-le-pic-1.d > > new file mode 100644 > > index 00000000000..6db5f029e75 > > --- /dev/null > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-1.d > > @@ -0,0 +1,3 @@ > > +#as: --64 > > +#ld: -shared -melf_x86_64 > > +#error: .*: relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-1.s b/ld/testsuite/ld-x86-64/tls-le-pic-1.s > > new file mode 100644 > > index 00000000000..ffc8e63b3b4 > > --- /dev/null > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-1.s > > @@ -0,0 +1,17 @@ > > + .text > > + .p2align 4 > > + .globl func > > + .type func, @function > > +func: > > + movq %fs:0, %rax > > + addq $foo@tpoff, %rax > > + ret > > + .size func, .-func > > + .section .tbss,"awT",@nobits > > + .align 4 > > + .globl foo > > + .type foo, @object > > + .size foo, 4 > > +foo: > > + .zero 4 > > + .section .note.GNU-stack,"",@progbits > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d b/ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d > > new file mode 100644 > > index 00000000000..e16395a5568 > > --- /dev/null > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d > > @@ -0,0 +1,4 @@ > > +#source: tls-le-pic-2.s > > +#as: --x32 > > +#ld: -shared -melf32_x86_64 > > +#error: .*: relocation R_X86_64_TPOFF32 against undefined symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-2.d b/ld/testsuite/ld-x86-64/tls-le-pic-2.d > > new file mode 100644 > > index 00000000000..0e718e01ebc > > --- /dev/null > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-2.d > > @@ -0,0 +1,3 @@ > > +#as: --64 > > +#ld: -shared -melf_x86_64 > > +#error: .*: relocation R_X86_64_TPOFF32 against undefined symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-2.s b/ld/testsuite/ld-x86-64/tls-le-pic-2.s > > new file mode 100644 > > index 00000000000..ad930db1546 > > --- /dev/null > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-2.s > > @@ -0,0 +1,10 @@ > > + .text > > + .p2align 4 > > + .globl func > > + .type func, @function > > +func: > > + movq %fs:0, %rax > > + addq $foo@tpoff, %rax > > + ret > > + .size func, .-func > > + .section .note.GNU-stack,"",@progbits > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d b/ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d > > new file mode 100644 > > index 00000000000..f6731daa991 > > --- /dev/null > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d > > @@ -0,0 +1,4 @@ > > +#source: tls-le-pic-3.s > > +#as: --x32 > > +#ld: -shared -melf32_x86_64 > > +#error: .*: relocation R_X86_64_TPOFF32 against `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-3.d b/ld/testsuite/ld-x86-64/tls-le-pic-3.d > > new file mode 100644 > > index 00000000000..987e370ad17 > > --- /dev/null > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-3.d > > @@ -0,0 +1,3 @@ > > +#as: --64 > > +#ld: -shared -melf_x86_64 > > +#error: .*: relocation R_X86_64_TPOFF32 against `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-3.s b/ld/testsuite/ld-x86-64/tls-le-pic-3.s > > new file mode 100644 > > index 00000000000..6e213fd5b54 > > --- /dev/null > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-3.s > > @@ -0,0 +1,16 @@ > > + .text > > + .p2align 4 > > + .globl func > > + .type func, @function > > +func: > > + movq %fs:0, %rax > > + addq $foo@tpoff, %rax > > + ret > > + .size func, .-func > > + .section .tbss,"awT",@nobits > > + .align 4 > > + .type foo, @object > > + .size foo, 4 > > +foo: > > + .zero 4 > > + .section .note.GNU-stack,"",@progbits > > diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp > > index 23e0a98d1cd..f04ae923ebc 100644 > > --- a/ld/testsuite/ld-x86-64/x86-64.exp > > +++ b/ld/testsuite/ld-x86-64/x86-64.exp > > @@ -578,6 +578,12 @@ run_dump_test "pr33292" > > run_dump_test "pr33292-x32" > > run_dump_test "pr28387" > > run_dump_test "pr28387-x32" > > +run_dump_test "tls-le-pic-1" > > +run_dump_test "tls-le-pic-1-x32" > > +run_dump_test "tls-le-pic-2" > > +run_dump_test "tls-le-pic-2-x32" > > +run_dump_test "tls-le-pic-3" > > +run_dump_test "tls-le-pic-3-x32" > > > > if { ![skip_sframe_tests] } { > > run_dump_test "sframe-simple-1" > > -- > > 2.51.0 > > > > I am checking it in. > > -- > H.J. Removing "recompile with -fPIC" looks right, as this is about executable links vs -shared. However, the suggestion "; replace local-exec with initial-exec TLS model" might not be the best choice. Initial-exec for shared objects might not be supported by dlopen'ed shared objects. > From https://maskray.me/blog/2021-02-14-all-about-thread-local-storage > > If you add the __attribute((tls_model("initial-exec"))) attribute, a thread-local variable can use this model in -fpic mode. If the object file is linked into an executable, everything is fine. If the object file is linked into a shared object, the shared object generally needs to be an immediately loaded shared object. The linker sets the DF_STATIC_TLS flag to annotate a shared object with initial-exec TLS relocations. > > glibc ld.so reserves some space in static TLS blocks and allows dlopen on such a shared object if its TLS size is small. There could be an obscure reason for using such an attribute: general dynamic and local dynamic TLS models are not async-signal-safe in glibc. However, other libc implementations may not reserve additional TLS space for dlopen'ed initial-exec shared objects, e.g. musl will error. Simply suggesting that local-exec is incompatible with -shared suffices.
On Wed, Sep 10, 2025 at 9:57 AM Fangrui Song <i@maskray.me> wrote: > > On Wed, Sep 10, 2025 at 6:45 AM H.J. Lu <hjl.tools@gmail.com> wrote: > > > > On Tue, Sep 9, 2025 at 1:38 PM H.J. Lu <hjl.tools@gmail.com> wrote: > > > > > > R_X86_64_TPOFF32 relocation of local-exec TLS model can only be used in > > > executable, not in a shared library, even if the source code is compiled > > > with -fPIC. Change the linker error message from > > > > > > relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; recompile with -fPIC > > > > > > to > > > > > > relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > > > > > bfd/ > > > > > > PR ld/33408 > > > * elf64-x86-64.c (elf_x86_64_need_pic): Suggest "replace > > > local-exec with initial-exec TLS model" for R_X86_64_TPOFF32. > > > (elf_x86_64_scan_relocs): Drop ABI_64_P check for > > > R_X86_64_TPOFF32. > > > > > > ld/ > > > > > > PR ld/33408 > > > * testsuite/ld-x86-64/tls-le-pic-1-x32.d: New file. > > > * testsuite/ld-x86-64/tls-le-pic-1.d: Likewise. > > > * testsuite/ld-x86-64/tls-le-pic-1.s: Likewise. > > > * testsuite/ld-x86-64/tls-le-pic-2-x32.d: Likewise. > > > * testsuite/ld-x86-64/tls-le-pic-2.d: Likewise. > > > * testsuite/ld-x86-64/tls-le-pic-2.s: Likewise. > > > * testsuite/ld-x86-64/tls-le-pic-3-x32.d: Likewise. > > > * testsuite/ld-x86-64/tls-le-pic-3.d: Likewise. > > > * testsuite/ld-x86-64/tls-le-pic-3.s: Likewise. > > > > > > Signed-off-by: H.J. Lu <hjl.tools@gmail.com> > > > --- > > > bfd/elf64-x86-64.c | 6 ++++-- > > > ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d | 4 ++++ > > > ld/testsuite/ld-x86-64/tls-le-pic-1.d | 3 +++ > > > ld/testsuite/ld-x86-64/tls-le-pic-1.s | 17 +++++++++++++++++ > > > ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d | 4 ++++ > > > ld/testsuite/ld-x86-64/tls-le-pic-2.d | 3 +++ > > > ld/testsuite/ld-x86-64/tls-le-pic-2.s | 10 ++++++++++ > > > ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d | 4 ++++ > > > ld/testsuite/ld-x86-64/tls-le-pic-3.d | 3 +++ > > > ld/testsuite/ld-x86-64/tls-le-pic-3.s | 16 ++++++++++++++++ > > > ld/testsuite/ld-x86-64/x86-64.exp | 6 ++++++ > > > 11 files changed, 74 insertions(+), 2 deletions(-) > > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d > > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-1.d > > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-1.s > > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d > > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-2.d > > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-2.s > > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d > > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-3.d > > > create mode 100644 ld/testsuite/ld-x86-64/tls-le-pic-3.s > > > > > > diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c > > > index 00586455203..ad99cf6754e 100644 > > > --- a/bfd/elf64-x86-64.c > > > +++ b/bfd/elf64-x86-64.c > > > @@ -1717,7 +1717,9 @@ elf_x86_64_need_pic (struct bfd_link_info *info, > > > { > > > object = _("a shared object"); > > > if (!pic) > > > - pic = _("; recompile with -fPIC"); > > > + pic = (howto->type == R_X86_64_TPOFF32 > > > + ? _("; replace local-exec with initial-exec TLS model") > > > + : _("; recompile with -fPIC")); > > > } > > > else > > > { > > > @@ -2684,7 +2686,7 @@ elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info, > > > goto create_got; > > > > > > case R_X86_64_TPOFF32: > > > - if (!bfd_link_executable (info) && ABI_64_P (abfd)) > > > + if (!bfd_link_executable (info)) > > > { > > > elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym, > > > &x86_64_elf_howto_table[r_type]); > > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d b/ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d > > > new file mode 100644 > > > index 00000000000..b0824c4ae10 > > > --- /dev/null > > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d > > > @@ -0,0 +1,4 @@ > > > +#source: tls-le-pic-1.s > > > +#as: --x32 > > > +#ld: -shared -melf32_x86_64 > > > +#error: .*: relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-1.d b/ld/testsuite/ld-x86-64/tls-le-pic-1.d > > > new file mode 100644 > > > index 00000000000..6db5f029e75 > > > --- /dev/null > > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-1.d > > > @@ -0,0 +1,3 @@ > > > +#as: --64 > > > +#ld: -shared -melf_x86_64 > > > +#error: .*: relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-1.s b/ld/testsuite/ld-x86-64/tls-le-pic-1.s > > > new file mode 100644 > > > index 00000000000..ffc8e63b3b4 > > > --- /dev/null > > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-1.s > > > @@ -0,0 +1,17 @@ > > > + .text > > > + .p2align 4 > > > + .globl func > > > + .type func, @function > > > +func: > > > + movq %fs:0, %rax > > > + addq $foo@tpoff, %rax > > > + ret > > > + .size func, .-func > > > + .section .tbss,"awT",@nobits > > > + .align 4 > > > + .globl foo > > > + .type foo, @object > > > + .size foo, 4 > > > +foo: > > > + .zero 4 > > > + .section .note.GNU-stack,"",@progbits > > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d b/ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d > > > new file mode 100644 > > > index 00000000000..e16395a5568 > > > --- /dev/null > > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d > > > @@ -0,0 +1,4 @@ > > > +#source: tls-le-pic-2.s > > > +#as: --x32 > > > +#ld: -shared -melf32_x86_64 > > > +#error: .*: relocation R_X86_64_TPOFF32 against undefined symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-2.d b/ld/testsuite/ld-x86-64/tls-le-pic-2.d > > > new file mode 100644 > > > index 00000000000..0e718e01ebc > > > --- /dev/null > > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-2.d > > > @@ -0,0 +1,3 @@ > > > +#as: --64 > > > +#ld: -shared -melf_x86_64 > > > +#error: .*: relocation R_X86_64_TPOFF32 against undefined symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-2.s b/ld/testsuite/ld-x86-64/tls-le-pic-2.s > > > new file mode 100644 > > > index 00000000000..ad930db1546 > > > --- /dev/null > > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-2.s > > > @@ -0,0 +1,10 @@ > > > + .text > > > + .p2align 4 > > > + .globl func > > > + .type func, @function > > > +func: > > > + movq %fs:0, %rax > > > + addq $foo@tpoff, %rax > > > + ret > > > + .size func, .-func > > > + .section .note.GNU-stack,"",@progbits > > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d b/ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d > > > new file mode 100644 > > > index 00000000000..f6731daa991 > > > --- /dev/null > > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d > > > @@ -0,0 +1,4 @@ > > > +#source: tls-le-pic-3.s > > > +#as: --x32 > > > +#ld: -shared -melf32_x86_64 > > > +#error: .*: relocation R_X86_64_TPOFF32 against `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-3.d b/ld/testsuite/ld-x86-64/tls-le-pic-3.d > > > new file mode 100644 > > > index 00000000000..987e370ad17 > > > --- /dev/null > > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-3.d > > > @@ -0,0 +1,3 @@ > > > +#as: --64 > > > +#ld: -shared -melf_x86_64 > > > +#error: .*: relocation R_X86_64_TPOFF32 against `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model > > > diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-3.s b/ld/testsuite/ld-x86-64/tls-le-pic-3.s > > > new file mode 100644 > > > index 00000000000..6e213fd5b54 > > > --- /dev/null > > > +++ b/ld/testsuite/ld-x86-64/tls-le-pic-3.s > > > @@ -0,0 +1,16 @@ > > > + .text > > > + .p2align 4 > > > + .globl func > > > + .type func, @function > > > +func: > > > + movq %fs:0, %rax > > > + addq $foo@tpoff, %rax > > > + ret > > > + .size func, .-func > > > + .section .tbss,"awT",@nobits > > > + .align 4 > > > + .type foo, @object > > > + .size foo, 4 > > > +foo: > > > + .zero 4 > > > + .section .note.GNU-stack,"",@progbits > > > diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp > > > index 23e0a98d1cd..f04ae923ebc 100644 > > > --- a/ld/testsuite/ld-x86-64/x86-64.exp > > > +++ b/ld/testsuite/ld-x86-64/x86-64.exp > > > @@ -578,6 +578,12 @@ run_dump_test "pr33292" > > > run_dump_test "pr33292-x32" > > > run_dump_test "pr28387" > > > run_dump_test "pr28387-x32" > > > +run_dump_test "tls-le-pic-1" > > > +run_dump_test "tls-le-pic-1-x32" > > > +run_dump_test "tls-le-pic-2" > > > +run_dump_test "tls-le-pic-2-x32" > > > +run_dump_test "tls-le-pic-3" > > > +run_dump_test "tls-le-pic-3-x32" > > > > > > if { ![skip_sframe_tests] } { > > > run_dump_test "sframe-simple-1" > > > -- > > > 2.51.0 > > > > > > > I am checking it in. > > > > -- > > H.J. > > Removing "recompile with -fPIC" looks right, as this is about > executable links vs -shared. > However, the suggestion "; replace local-exec with initial-exec TLS > model" might not be the best choice. > Initial-exec for shared objects might not be supported by dlopen'ed > shared objects. > > > From https://maskray.me/blog/2021-02-14-all-about-thread-local-storage > > > > If you add the __attribute((tls_model("initial-exec"))) attribute, a thread-local variable can use this model in -fpic mode. If the object file is linked into an executable, everything is fine. If the object file is linked into a shared object, the shared object generally needs to be an immediately loaded shared object. The linker sets the DF_STATIC_TLS flag to annotate a shared object with initial-exec TLS relocations. > > > > glibc ld.so reserves some space in static TLS blocks and allows dlopen on such a shared object if its TLS size is small. There could be an obscure reason for using such an attribute: general dynamic and local dynamic TLS models are not async-signal-safe in glibc. However, other libc implementations may not reserve additional TLS space for dlopen'ed initial-exec shared objects, e.g. musl will error. > > Simply suggesting that local-exec is incompatible with -shared suffices. Good point. Submitted a patch.
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c index 00586455203..ad99cf6754e 100644 --- a/bfd/elf64-x86-64.c +++ b/bfd/elf64-x86-64.c @@ -1717,7 +1717,9 @@ elf_x86_64_need_pic (struct bfd_link_info *info, { object = _("a shared object"); if (!pic) - pic = _("; recompile with -fPIC"); + pic = (howto->type == R_X86_64_TPOFF32 + ? _("; replace local-exec with initial-exec TLS model") + : _("; recompile with -fPIC")); } else { @@ -2684,7 +2686,7 @@ elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info, goto create_got; case R_X86_64_TPOFF32: - if (!bfd_link_executable (info) && ABI_64_P (abfd)) + if (!bfd_link_executable (info)) { elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym, &x86_64_elf_howto_table[r_type]); diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d b/ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d new file mode 100644 index 00000000000..b0824c4ae10 --- /dev/null +++ b/ld/testsuite/ld-x86-64/tls-le-pic-1-x32.d @@ -0,0 +1,4 @@ +#source: tls-le-pic-1.s +#as: --x32 +#ld: -shared -melf32_x86_64 +#error: .*: relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-1.d b/ld/testsuite/ld-x86-64/tls-le-pic-1.d new file mode 100644 index 00000000000..6db5f029e75 --- /dev/null +++ b/ld/testsuite/ld-x86-64/tls-le-pic-1.d @@ -0,0 +1,3 @@ +#as: --64 +#ld: -shared -melf_x86_64 +#error: .*: relocation R_X86_64_TPOFF32 against symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-1.s b/ld/testsuite/ld-x86-64/tls-le-pic-1.s new file mode 100644 index 00000000000..ffc8e63b3b4 --- /dev/null +++ b/ld/testsuite/ld-x86-64/tls-le-pic-1.s @@ -0,0 +1,17 @@ + .text + .p2align 4 + .globl func + .type func, @function +func: + movq %fs:0, %rax + addq $foo@tpoff, %rax + ret + .size func, .-func + .section .tbss,"awT",@nobits + .align 4 + .globl foo + .type foo, @object + .size foo, 4 +foo: + .zero 4 + .section .note.GNU-stack,"",@progbits diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d b/ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d new file mode 100644 index 00000000000..e16395a5568 --- /dev/null +++ b/ld/testsuite/ld-x86-64/tls-le-pic-2-x32.d @@ -0,0 +1,4 @@ +#source: tls-le-pic-2.s +#as: --x32 +#ld: -shared -melf32_x86_64 +#error: .*: relocation R_X86_64_TPOFF32 against undefined symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-2.d b/ld/testsuite/ld-x86-64/tls-le-pic-2.d new file mode 100644 index 00000000000..0e718e01ebc --- /dev/null +++ b/ld/testsuite/ld-x86-64/tls-le-pic-2.d @@ -0,0 +1,3 @@ +#as: --64 +#ld: -shared -melf_x86_64 +#error: .*: relocation R_X86_64_TPOFF32 against undefined symbol `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-2.s b/ld/testsuite/ld-x86-64/tls-le-pic-2.s new file mode 100644 index 00000000000..ad930db1546 --- /dev/null +++ b/ld/testsuite/ld-x86-64/tls-le-pic-2.s @@ -0,0 +1,10 @@ + .text + .p2align 4 + .globl func + .type func, @function +func: + movq %fs:0, %rax + addq $foo@tpoff, %rax + ret + .size func, .-func + .section .note.GNU-stack,"",@progbits diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d b/ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d new file mode 100644 index 00000000000..f6731daa991 --- /dev/null +++ b/ld/testsuite/ld-x86-64/tls-le-pic-3-x32.d @@ -0,0 +1,4 @@ +#source: tls-le-pic-3.s +#as: --x32 +#ld: -shared -melf32_x86_64 +#error: .*: relocation R_X86_64_TPOFF32 against `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-3.d b/ld/testsuite/ld-x86-64/tls-le-pic-3.d new file mode 100644 index 00000000000..987e370ad17 --- /dev/null +++ b/ld/testsuite/ld-x86-64/tls-le-pic-3.d @@ -0,0 +1,3 @@ +#as: --64 +#ld: -shared -melf_x86_64 +#error: .*: relocation R_X86_64_TPOFF32 against `foo' can not be used when making a shared object; replace local-exec with initial-exec TLS model diff --git a/ld/testsuite/ld-x86-64/tls-le-pic-3.s b/ld/testsuite/ld-x86-64/tls-le-pic-3.s new file mode 100644 index 00000000000..6e213fd5b54 --- /dev/null +++ b/ld/testsuite/ld-x86-64/tls-le-pic-3.s @@ -0,0 +1,16 @@ + .text + .p2align 4 + .globl func + .type func, @function +func: + movq %fs:0, %rax + addq $foo@tpoff, %rax + ret + .size func, .-func + .section .tbss,"awT",@nobits + .align 4 + .type foo, @object + .size foo, 4 +foo: + .zero 4 + .section .note.GNU-stack,"",@progbits diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp index 23e0a98d1cd..f04ae923ebc 100644 --- a/ld/testsuite/ld-x86-64/x86-64.exp +++ b/ld/testsuite/ld-x86-64/x86-64.exp @@ -578,6 +578,12 @@ run_dump_test "pr33292" run_dump_test "pr33292-x32" run_dump_test "pr28387" run_dump_test "pr28387-x32" +run_dump_test "tls-le-pic-1" +run_dump_test "tls-le-pic-1-x32" +run_dump_test "tls-le-pic-2" +run_dump_test "tls-le-pic-2-x32" +run_dump_test "tls-le-pic-3" +run_dump_test "tls-le-pic-3-x32" if { ![skip_sframe_tests] } { run_dump_test "sframe-simple-1"