[PATCH] Skip convert lui to GPREL_L for .LANCHOR

Lifang Xia lifang_xia@linux.alibaba.com
Thu Mar 11 13:04:38 GMT 2021


If the .LANCHOR defined in .rodata, the gp may not reach the symbol
after lang_size_relro_segment while linking with "-z relro".

bfd/
	* elfnn-riscv.c (_bfd_riscv_relax_lui): Skip that the symbol
	  is defined in rodata and linking with "-z relro".
---
 bfd/elfnn-riscv.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 364d67b..74dce3c 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -4251,10 +4251,14 @@ _bfd_riscv_relax_lui (bfd *abfd,
      Valid gp range conservatively because of alignment issue.  */
   if (undefined_weak
       || (VALID_ITYPE_IMM (symval)
-	  || (symval >= gp
-	      && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
-	  || (symval < gp
-	      && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size))))
+	  /* Skip that the symbol defined in rodata by .set, such as .LANCHOR,
+	     and link with -z relro. The symbol may not be accessed with gp.  */
+	  || (!(sym_sec->flags & SEC_READONLY
+		&& link_info->relro)
+	      && ((symval >= gp
+		   && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
+		  || (symval < gp
+		      && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size))))))
     {
       unsigned sym = ELFNN_R_SYM (rel->r_info);
       switch (ELFNN_R_TYPE (rel->r_info))
-- 
2.6.0-rc0



More information about the Binutils mailing list