[PATCH] LoongArch: Fix linker generate PLT entry for data symbol

mengqinggang mengqinggang@loongson.cn
Fri Dec 22 08:31:12 GMT 2023


With old "medium" code model, we call a function with a pair of PCALAU12I
and JIRL instructions. Currently the assembler produces something like:

   8:	1a00000c 	pcalau12i   	$t0, 0
			8: R_LARCH_PCALA_HI20	g
   c:	4c000181 	jirl        	$ra, $t0, 0
			c: R_LARCH_PCALA_LO12	g

Before the linker generates a "PLT entry" for data without any diagnostic.
If "g" is a data symbol, it may load two instructions in the PLT.

For R_LARCH_PCALA_HI20 reloc, linker only generate PLT entry for STT_FUNC and
STT_GNU_IFUNC symbol.

bfd/ChangeLog:

	* testsuite/ld-loongarch-elf/libjirl.s: Add function type.
	* elfnn-loongarch.c (loongarch_elf_check_relocs): Add function
	  condition for R_LARCH_PCALA_HI20 reloc.
---
 bfd/elfnn-loongarch.c                   | 6 +++++-
 ld/testsuite/ld-loongarch-elf/libjirl.s | 1 +
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 87eb65ab9f7..7302930ae01 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -762,8 +762,12 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 	    h->non_got_ref = 1;
 	  break;
 
+	/* For normal cmodel, pcalau12i + addi.d/w used to data.
+	   For first version medium cmodel, pcalau12i + jirl are used to
+	   function call, it need to creat PLT entry for STT_FUNC and
+	   STT_GNU_IFUNC type symbol.  */
 	case R_LARCH_PCALA_HI20:
-	  if (h != NULL)
+	  if (h != NULL && (STT_FUNC == h->type || STT_GNU_IFUNC == h->type))
 	    {
 	      /* For pcalau12i + jirl.  */
 	      h->needs_plt = 1;
diff --git a/ld/testsuite/ld-loongarch-elf/libjirl.s b/ld/testsuite/ld-loongarch-elf/libjirl.s
index 4d963870df0..de028c5a44b 100644
--- a/ld/testsuite/ld-loongarch-elf/libjirl.s
+++ b/ld/testsuite/ld-loongarch-elf/libjirl.s
@@ -1,2 +1,3 @@
+.type func @function
 pcalau12i $r12, %pc_hi20(func)
 jirl $r1,$r12, %pc_lo12(func)
-- 
2.36.0



More information about the Binutils mailing list