public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Tatsuyuki Ishi <ishitatsuyuki@gmail.com>
To: binutils@sourceware.org
Cc: rui314@gmail.com, ruiu@bluewhale.systems,
	Tatsuyuki Ishi <ishitatsuyuki@gmail.com>
Subject: [PATCH 4/4] RISC-V: Initial ld.bfd support for TLSDESC.
Date: Fri, 18 Aug 2023 03:08:37 +0900	[thread overview]
Message-ID: <20230817180852.121628-6-ishitatsuyuki@gmail.com> (raw)
In-Reply-To: <20230817180852.121628-2-ishitatsuyuki@gmail.com>

Only relocation handling for now; relaxation is not implemented yet.
---
 bfd/elfnn-riscv.c                          | 91 +++++++++++++++++++---
 ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp |  2 +
 2 files changed, 84 insertions(+), 9 deletions(-)

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 38883134828..a55bee7af52 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -166,6 +166,7 @@ struct riscv_elf_link_hash_entry
 #define GOT_TLS_GD	2
 #define GOT_TLS_IE	4
 #define GOT_TLS_LE	8
+#define GOT_TLSDESC	16
   char tls_type;
 };
 
@@ -303,6 +304,7 @@ riscv_is_insn_reloc (const reloc_howto_type *howto)
 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
 #define TLS_GD_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2)
 #define TLS_IE_GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
+#define TLSDESC_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2)
 /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
    the other is used for link map.  Other targets also reserve one more
    entry used for runtime profile?  */
@@ -858,6 +860,12 @@ riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 	    return false;
 	  break;
 
+	  case R_RISCV_TLSDESC_HI20:
+	    if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
+		|| !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLSDESC))
+	      return false;
+	  break;
+
 	case R_RISCV_CALL:
 	case R_RISCV_CALL_PLT:
 	  /* These symbol requires a procedure linkage table entry.
@@ -1316,7 +1324,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       s = htab->elf.sgot;
       h->got.offset = s->size;
       dyn = htab->elf.dynamic_sections_created;
-      if (tls_type & (GOT_TLS_GD | GOT_TLS_IE))
+      if (tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLSDESC))
 	{
 	  int indx = 0;
 	  bool need_reloc = false;
@@ -1337,6 +1345,14 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 	      if (need_reloc)
 		htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
 	    }
+
+	  /* TLSDESC needs one dynamic reloc and four GOT slots.  */
+	  if (tls_type & GOT_TLSDESC)
+	    {
+	      s->size += TLSDESC_GOT_ENTRY_SIZE;
+	      /* TLSDESC always use dynamic relocs.  */
+	      htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
+	    }
 	}
       else
 	{
@@ -1564,7 +1580,7 @@ riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
 	  if (*local_got > 0)
 	    {
 	      *local_got = s->size;
-	      if (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE))
+	      if (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLSDESC))
 		{
 		  if (*local_tls_type & GOT_TLS_GD)
 		    {
@@ -1578,6 +1594,11 @@ riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
 		      if (bfd_link_dll (info))
 			srel->size += sizeof (ElfNN_External_Rela);
 		    }
+		  if (*local_tls_type & GOT_TLSDESC)
+		    {
+		      s->size += TLSDESC_GOT_ENTRY_SIZE;
+		      srel->size += sizeof (ElfNN_External_Rela);
+		    }
 		}
 	      else
 		{
@@ -1728,6 +1749,17 @@ tpoff (struct bfd_link_info *info, bfd_vma address)
   return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET;
 }
 
+/* Return the relocation value for a static TLSDESC relocation.  */
+
+static bfd_vma
+tlsdescoff (struct bfd_link_info *info, bfd_vma address)
+{
+  /* If tls_sec is NULL, we should have signalled an error already.  */
+  if (elf_hash_table (info)->tls_sec == NULL)
+    return 0;
+  return address - elf_hash_table (info)->tls_sec->vma;
+}
+
 /* Return the global pointer's value, or 0 if it is not in use.  */
 
 static bfd_vma
@@ -1764,6 +1796,7 @@ perform_relocation (const reloc_howto_type *howto,
     case R_RISCV_GOT_HI20:
     case R_RISCV_TLS_GOT_HI20:
     case R_RISCV_TLS_GD_HI20:
+    case R_RISCV_TLSDESC_HI20:
       if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
 	return bfd_reloc_overflow;
       value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
@@ -1774,6 +1807,8 @@ perform_relocation (const reloc_howto_type *howto,
     case R_RISCV_TPREL_LO12_I:
     case R_RISCV_TPREL_I:
     case R_RISCV_PCREL_LO12_I:
+    case R_RISCV_TLSDESC_LOAD_LO12:
+    case R_RISCV_TLSDESC_ADD_LO12:
       value = ENCODE_ITYPE_IMM (value);
       break;
 
@@ -2195,8 +2230,8 @@ riscv_elf_relocate_section (bfd *output_bfd,
       bfd_vma relocation;
       bfd_reloc_status_type r = bfd_reloc_ok;
       const char *name = NULL;
-      bfd_vma off, ie_off;
-      bool unresolved_reloc, is_ie = false;
+      bfd_vma off, ie_off, desc_off;
+      bool unresolved_reloc, is_ie = false, is_desc = false;
       bfd_vma pc = sec_addr (input_section) + rel->r_offset;
       int r_type = ELFNN_R_TYPE (rel->r_info), tls_type;
       reloc_howto_type *howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
@@ -2499,6 +2534,7 @@ riscv_elf_relocate_section (bfd *output_bfd,
 	case R_RISCV_NONE:
 	case R_RISCV_RELAX:
 	case R_RISCV_TPREL_ADD:
+	case R_RISCV_TLSDESC_CALL:
 	case R_RISCV_COPY:
 	case R_RISCV_JUMP_SLOT:
 	case R_RISCV_RELATIVE:
@@ -2820,6 +2856,15 @@ riscv_elf_relocate_section (bfd *output_bfd,
 	  relocation = dtpoff (info, relocation);
 	  break;
 
+	case R_RISCV_TLSDESC_LOAD_LO12:
+	case R_RISCV_TLSDESC_ADD_LO12:
+	  if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, relocation, rel,
+					   input_section, info, howto,
+					   contents))
+	      continue;
+	  r = bfd_reloc_overflow;
+	  break;
+
 	case R_RISCV_32:
 	  /* Non ABS symbol should be blocked in check_relocs.  */
 	  if (ARCH_SIZE > 32)
@@ -2885,11 +2930,16 @@ riscv_elf_relocate_section (bfd *output_bfd,
 	    }
 	  break;
 
+	case R_RISCV_TLSDESC_HI20:
+	  is_desc = true;
+	  goto tls;
+
 	case R_RISCV_TLS_GOT_HI20:
 	  is_ie = true;
-	  /* Fall through.  */
+	  goto tls;
 
 	case R_RISCV_TLS_GD_HI20:
+	tls:
 	  if (h != NULL)
 	    {
 	      off = h->got.offset;
@@ -2902,12 +2952,16 @@ riscv_elf_relocate_section (bfd *output_bfd,
 	    }
 
 	  tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx);
-	  BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD));
+	  BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD | GOT_TLSDESC));
 	  /* If this symbol is referenced by both GD and IE TLS, the IE
 	     reference's GOT slot follows the GD reference's slots.  */
 	  ie_off = 0;
 	  if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE))
-	    ie_off = TLS_GD_GOT_ENTRY_SIZE;
+	    ie_off += TLS_GD_GOT_ENTRY_SIZE;
+
+	  desc_off = ie_off;
+	  if (tls_type & GOT_TLS_IE)
+	    desc_off += TLS_IE_GOT_ENTRY_SIZE;
 
 	  if ((off & 1) != 0)
 	    off &= ~1;
@@ -2989,10 +3043,29 @@ riscv_elf_relocate_section (bfd *output_bfd,
 				  htab->elf.sgot->contents + off + ie_off);
 		    }
 		}
+
+	      if (tls_type & GOT_TLSDESC)
+		{
+		  /* TLSDESC is always handled by the dynamic linker and always need
+		   * a relocation.  */
+		  bfd_put_NN (output_bfd, 0,
+			      htab->elf.sgot->contents + off + desc_off);
+		  outrel.r_offset = sec_addr (htab->elf.sgot)
+				    + off + desc_off;
+		  outrel.r_addend = 0;
+		  if (indx == 0)
+		    outrel.r_addend = tlsdescoff (info, relocation);
+		  outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLSDESC);
+		  riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
+		}
 	    }
 
 	  BFD_ASSERT (off < (bfd_vma) -2);
-	  relocation = sec_addr (htab->elf.sgot) + off + (is_ie ? ie_off : 0);
+	  relocation = sec_addr (htab->elf.sgot) + off;
+	  if (is_ie)
+	    relocation += ie_off;
+	  else if (is_desc)
+	    relocation += desc_off;
 	  if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
 					    relocation, r_type,
 					    false))
@@ -3212,7 +3285,7 @@ riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
     }
 
   if (h->got.offset != (bfd_vma) -1
-      && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE))
+      && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE | GOT_TLSDESC))
       && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
     {
       asection *sgot;
diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
index 947a266ba72..e09c25486a5 100644
--- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
+++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
@@ -317,4 +317,6 @@ if [istarget "riscv*-*-*"] {
     run_dump_test "pcrel-reloc-rel-pie"
     run_dump_test "pcrel-reloc-abs-nopie"
     run_dump_test "pcrel-reloc-abs-pie"
+
+    run_dump_test "tlsdesc"
 }
-- 
2.41.0


  parent reply	other threads:[~2023-08-17 18:09 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-17 18:08 [PATCH 0/4] RISC-V: Implement TLS Descriptors Tatsuyuki Ishi
2023-08-17 18:08 ` [PATCH 1/4] RISC-V: Add TLSDESC reloc definitions Tatsuyuki Ishi
2023-08-17 18:08 ` [PATCH 2/4] RISC-V: Add assembly support for TLSDESC Tatsuyuki Ishi
2023-08-17 18:08 ` [PATCH 3/4] RISC-V: Define and use GOT entry size constants for TLS Tatsuyuki Ishi
2023-08-17 18:08 ` Tatsuyuki Ishi [this message]
2023-08-18  0:22 ` [PATCH 0/4] RISC-V: Implement TLS Descriptors Nelson Chu
2023-08-18  7:13   ` Fangrui Song
2023-08-31 17:13 ` [PATCH v2 0/5] " Tatsuyuki Ishi
2023-08-31 17:13   ` [PATCH v2 1/5] RISC-V: Fix local GOT and reloc size calculation for TLS Tatsuyuki Ishi
2024-02-21  0:49     ` Nelson Chu
2024-02-21  7:04       ` Nelson Chu
2023-08-31 17:13   ` [PATCH v2 2/5] RISC-V: Add TLSDESC reloc definitions Tatsuyuki Ishi
2023-08-31 17:13   ` [PATCH v2 3/5] RISC-V: Add assembly support for TLSDESC Tatsuyuki Ishi
2023-08-31 17:13   ` [PATCH v2 4/5] RISC-V: Define and use GOT entry size constants for TLS Tatsuyuki Ishi
2023-08-31 17:13   ` [PATCH v2 5/5] RISC-V: Initial ld.bfd support for TLSDESC Tatsuyuki Ishi
2023-11-28  8:51 ` [PATCH v3 0/9] RISC-V: Implement TLS Descriptors Tatsuyuki Ishi
2023-11-28  8:51   ` [PATCH v3 1/9] RISC-V: Fix local GOT and reloc size calculation for TLS Tatsuyuki Ishi
2023-11-28  8:51   ` [PATCH v3 2/9] RISC-V: Add TLSDESC reloc definitions Tatsuyuki Ishi
2024-02-19  0:49     ` Nelson Chu
2024-02-20 17:28       ` Tatsuyuki Ishi
2023-11-28  8:51   ` [PATCH v3 3/9] RISC-V: Add assembly support for TLSDESC Tatsuyuki Ishi
2024-02-19  1:44     ` Nelson Chu
2024-02-20 17:29       ` Tatsuyuki Ishi
2023-11-28  8:51   ` [PATCH v3 4/9] RISC-V: Define and use GOT entry size constants for TLS Tatsuyuki Ishi
2024-02-19  1:57     ` Nelson Chu
2024-02-20 17:32       ` Tatsuyuki Ishi
2023-11-28  8:51   ` [PATCH v3 5/9] RISC-V: Initial ld.bfd support for TLSDESC Tatsuyuki Ishi
2024-02-19  4:33     ` Nelson Chu
2024-02-20 17:36       ` Tatsuyuki Ishi
2023-11-28  8:51   ` [PATCH v3 6/9] RISC-V: Move STATIC_TLS handling into record_tls_type Tatsuyuki Ishi
2023-11-28  8:51   ` [PATCH v3 7/9] RISC-V: Unify TLS handling in check_relocs Tatsuyuki Ishi
2023-11-28  8:51   ` [PATCH v3 8/9] RISC-V: Add elf_link_hash_entry to relax_func args Tatsuyuki Ishi
2023-11-28  8:51   ` [PATCH v3 9/9] RISC-V: Introduce TLSDESC relaxation Tatsuyuki Ishi
2023-12-05 16:44   ` [PATCH v3 0/9] RISC-V: Implement TLS Descriptors Tatsuyuki Ishi
2023-12-06  0:33     ` Nelson Chu
2023-12-07  3:35       ` Fangrui Song
2023-12-13  0:27         ` Palmer Dabbelt
2023-12-13  1:53           ` Tatsuyuki Ishi
2024-01-27  0:57             ` Fangrui Song
2024-02-20 17:55 ` [PATCH v4 " Tatsuyuki Ishi
2024-02-20 17:55   ` [PATCH v4 1/9] RISC-V: Fix local GOT and reloc size calculation for TLS Tatsuyuki Ishi
2024-02-20 17:55   ` [PATCH v4 2/9] RISC-V: Add TLSDESC reloc definitions Tatsuyuki Ishi
2024-02-20 17:55   ` [PATCH v4 3/9] RISC-V: Add assembly support for TLSDESC Tatsuyuki Ishi
2024-02-20 17:55   ` [PATCH v4 4/9] RISC-V: Define and use GOT entry size constants for TLS Tatsuyuki Ishi
2024-02-20 17:55   ` [PATCH v4 5/9] RISC-V: Initial ld.bfd support for TLSDESC Tatsuyuki Ishi
2024-02-20 17:55   ` [PATCH v4 6/9] RISC-V: Move STATIC_TLS handling into record_tls_type Tatsuyuki Ishi
2024-02-20 17:55   ` [PATCH v4 7/9] RISC-V: Unify TLS handling in check_relocs Tatsuyuki Ishi
2024-02-20 17:55   ` [PATCH v4 8/9] RISC-V: Add elf_link_hash_entry to relax_func args Tatsuyuki Ishi
2024-02-20 17:55   ` [PATCH v4 9/9] RISC-V: Introduce TLSDESC relaxation Tatsuyuki Ishi
2024-02-29  7:06   ` [PATCH v4 0/9] RISC-V: Implement TLS Descriptors Nelson Chu
2024-02-29  7:14     ` Tatsuyuki Ishi
2024-03-29  6:22       ` Tatsuyuki Ishi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230817180852.121628-6-ishitatsuyuki@gmail.com \
    --to=ishitatsuyuki@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=rui314@gmail.com \
    --cc=ruiu@bluewhale.systems \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).