[PATCH] RISC-V: Fix local GOT and reloc size calculation for TLS.

Tatsuyuki Ishi ishitatsuyuki@gmail.com
Thu Aug 17 18:05:04 GMT 2023


The previous code did not account correctly for two cases:
* A TLS symbol can be referenced with multiple TLS types, in which case it
  only allocated the maximum slot size among the types, instead of the sum.
* TLS relocations are only needed for DLLs, unlike normal symbols which
  requires relocations for all PIE code.

Similar to the size calculation for global symbols, break up the
calculation into TLS and non-TLS symbols, and handle the two cases above
correctly.
---
No regression in binutils and gcc tests for rv64gc.

This contribution is made on behalf of Blue Whale Systems, which has
copyright assignment on file with the FSF.

 bfd/elfnn-riscv.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 09aa7be225e..02b46835cc7 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -1562,12 +1562,27 @@ riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
 	  if (*local_got > 0)
 	    {
 	      *local_got = s->size;
-	      s->size += RISCV_ELF_WORD_BYTES;
-	      if (*local_tls_type & GOT_TLS_GD)
-		s->size += RISCV_ELF_WORD_BYTES;
-	      if (bfd_link_pic (info)
-		  || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
-		srel->size += sizeof (ElfNN_External_Rela);
+	      if (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE))
+		{
+		  if (*local_tls_type & GOT_TLS_GD)
+		    {
+		      s->size += 2 * RISCV_ELF_WORD_BYTES;
+		      if (bfd_link_dll (info))
+			srel->size += sizeof (ElfNN_External_Rela);
+		    }
+		  if (*local_tls_type & GOT_TLS_IE)
+		    {
+		      s->size += RISCV_ELF_WORD_BYTES;
+		      if (bfd_link_dll (info))
+			srel->size += sizeof (ElfNN_External_Rela);
+		    }
+		}
+	      else
+		{
+		  s->size += RISCV_ELF_WORD_BYTES;
+		  if (bfd_link_pic (info))
+		    srel->size += sizeof (ElfNN_External_Rela);
+		}
 	    }
 	  else
 	    *local_got = (bfd_vma) -1;
-- 
2.41.0



More information about the Binutils mailing list