public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] LoongArch: Fix linker generate PLT entry for data symbol
@ 2023-12-22  8:31 mengqinggang
  0 siblings, 0 replies; only message in thread
From: mengqinggang @ 2023-12-22  8:31 UTC (permalink / raw)
  To: binutils
  Cc: xuchenghua, chenglulu, liuzhensong, cailulu, xry111, i.swmail,
	maskray, luweining, wanglei, hejinyang, mengqinggang

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-12-22  8:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-22  8:31 [PATCH] LoongArch: Fix linker generate PLT entry for data symbol mengqinggang

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).