x86: Check TLS relocation against non-thread local symbol

Message ID CAMe9rOqKCufOrwvkKNOR-tj+ZFxCvypAJkr7UKLdSCOtO-zoWw@mail.gmail.com
State New
Headers
Series x86: Check TLS relocation against non-thread local symbol |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

H.J. Lu July 30, 2026, 10:59 a.m. UTC
  TLS relocations must be against thread local symbols.  Update x86 scan
relocs functions to issue an error for TLS relocation against non-thread
local symbol.

PR ld/34448
* elf32-i386.c (elf_i386_scan_relocs): Issue an error for TLS
relocation against non-thread local symbol.
* elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise.
* elfxx-x86.c (_bfd_x86_elf_link_report_tls_invalid_section_error):
Check relocation against non-thread local symbol.
  

Patch

From 99d3c0ff37799450c1a2f5ff43011cd8f85df287 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Thu, 30 Jul 2026 13:43:56 +0800
Subject: [PATCH] x86: Check TLS relocation against non-thread local symbol

TLS relocations must be against thread local symbols.  Update x86 scan
relocs functions to issue an error for TLS relocation against non-thread
local symbol.

	PR ld/34448
	* elf32-i386.c (elf_i386_scan_relocs): Issue an error for TLS
	relocation against non-thread local symbol.
	* elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise.
	* elfxx-x86.c (_bfd_x86_elf_link_report_tls_invalid_section_error):
	Check relocation against non-thread local symbol.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 bfd/elf32-i386.c   |  5 ++++-
 bfd/elf64-x86-64.c |  5 ++++-
 bfd/elfxx-x86.c    | 26 ++++++++++++++++++++------
 3 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
index bebe81d88f6..eda660b1206 100644
--- a/bfd/elf32-i386.c
+++ b/bfd/elf32-i386.c
@@ -1749,7 +1749,10 @@  elf_i386_scan_relocs (bfd *abfd,
 
 	    if (tls_type >= GOT_TLS_GD
 		&& tls_type <= GOT_TLS_GDESC
-		&& (elf_section_type (sec) != SHT_PROGBITS
+		&& ((h != NULL
+		     ? h->type != STT_TLS
+		     : ELF_ST_TYPE (isym->st_info) != STT_TLS)
+		    || elf_section_type (sec) != SHT_PROGBITS
 		    || (sec->flags & SEC_CODE) == 0))
 	      {
 		_bfd_x86_elf_link_report_tls_invalid_section_error
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index bbbaca8cab8..30c33ca3082 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -2772,7 +2772,10 @@  need_got:
 
 	    if (tls_type >= GOT_TLS_GD
 		&& tls_type <= GOT_TLS_GDESC
-		&& (elf_section_type (sec) != SHT_PROGBITS
+		&& ((h != NULL
+		     ? h->type != STT_TLS
+		     : ELF_ST_TYPE (isym->st_info) != STT_TLS)
+		    || elf_section_type (sec) != SHT_PROGBITS
 		    || (sec->flags & SEC_CODE) == 0))
 	      {
 		_bfd_x86_elf_link_report_tls_invalid_section_error
diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c
index 0f79ef58f02..3a69fe6b3f3 100644
--- a/bfd/elfxx-x86.c
+++ b/bfd/elfxx-x86.c
@@ -3378,14 +3378,28 @@  _bfd_x86_elf_link_report_tls_invalid_section_error
    reloc_howto_type *howto)
 {
   const char *name;
+  bool non_tls;
   if (h)
-    name = h->root.root.string;
+    {
+      name = h->root.root.string;
+      non_tls = h->type != STT_TLS;
+    }
+  else
+    {
+      non_tls = ELF_ST_TYPE (sym->st_info) != STT_TLS;
+      name = bfd_elf_sym_name (abfd, symtab_hdr, sym, NULL);
+    }
+  if (non_tls)
+    _bfd_error_handler
+      /* xgettext:c-format */
+      (_("%pB: relocation %s against non-thread local symbol "
+	 "`%s' in section `%pA'"),
+       abfd, howto->name, name, sec);
   else
-    name = bfd_elf_sym_name (abfd, symtab_hdr, sym, NULL);
-  _bfd_error_handler
-    /* xgettext:c-format */
-    (_("%pB: relocation %s against thread local symbol `%s' in "
-       "invalid section `%pA'"), abfd, howto->name, name, sec);
+    _bfd_error_handler
+      /* xgettext:c-format */
+      (_("%pB: relocation %s against thread local symbol `%s' in "
+	 "invalid section `%pA'"), abfd, howto->name, name, sec);
   bfd_set_error (bfd_error_bad_value);
 }
 
-- 
2.55.0