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] RISC-V: Fix local GOT and reloc size calculation for TLS.
Date: Fri, 18 Aug 2023 03:05:04 +0900	[thread overview]
Message-ID: <20230817180508.120318-2-ishitatsuyuki@gmail.com> (raw)

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


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

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20230817180508.120318-2-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).