public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: liuzhensong <liuzhensong@loongson.cn>
To: binutils@sourceware.org
Cc: i.swmail@xen0n.name, xry111@xry111.site, maskray@google.com,
	liuzhensong <liuzhensong@loongson.cn>
Subject: [PATCH v2] LoongArch:ld: Fix relocation error of pcrel.
Date: Sat,  3 Sep 2022 16:41:49 +0800	[thread overview]
Message-ID: <20220903084149.1612310-1-liuzhensong@loongson.cn> (raw)

  Patch for branch 2.39.
  Need to reduce the address of pc when using
  reloction R_LARCH_SOP_PUSH_PCREL.

  bfd/
    * elfnn-loongarch.c
---
 bfd/elfnn-loongarch.c                         |  3 +-
 ld/testsuite/ld-loongarch-elf/pcrel-const.d   | 14 +++++++
 ld/testsuite/ld-loongarch-elf/pcrel-const.lds | 14 +++++++
 ld/testsuite/ld-loongarch-elf/pcrel-const.s   | 12 ++++++
 ld/testsuite/ld-loongarch-elf/pr.exp          | 39 +++++++++++++++++++
 5 files changed, 81 insertions(+), 1 deletion(-)
 create mode 100644 ld/testsuite/ld-loongarch-elf/pcrel-const.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/pcrel-const.lds
 create mode 100644 ld/testsuite/ld-loongarch-elf/pcrel-const.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/pr.exp

diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c
index 5b44901b9e0..fafdc7c7458 100644
--- a/bfd/elfnn-loongarch.c
+++ b/bfd/elfnn-loongarch.c
@@ -2341,9 +2341,10 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
 	case R_LARCH_SOP_PUSH_PLT_PCREL:
 	  unresolved_reloc = false;
 
-	  if (resolved_to_const)
+	  if (!is_undefweak && resolved_to_const)
 	    {
 	      relocation += rel->r_addend;
+	      relocation -= pc;
 	      break;
 	    }
 	  else if (is_undefweak)
diff --git a/ld/testsuite/ld-loongarch-elf/pcrel-const.d b/ld/testsuite/ld-loongarch-elf/pcrel-const.d
new file mode 100644
index 00000000000..ae4705aa531
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.d
@@ -0,0 +1,14 @@
+#as: -mla-global-with-pcrel
+#objdump: -Drsz
+
+.*:[    ]+file format .*
+
+
+Disassembly of section .text:
+
+.* <foo>:
+#...
+[ 	]+8:[ 	]+02c04084[ 	]+addi.d[ 	]+\$a0,[ 	]+\$a0,[ 	]+16\(0x10\)
+#...
+0+14 <__sec_end>:
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/pcrel-const.lds b/ld/testsuite/ld-loongarch-elf/pcrel-const.lds
new file mode 100644
index 00000000000..34b9861f4fa
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.lds
@@ -0,0 +1,14 @@
+ENTRY(foo);
+SECTIONS
+{
+	.text : {
+		*(.text*)
+	}
+
+	.data : {
+		__sec_start = .;
+		*(.gzdata)
+		__sec_end = .;
+	}
+}
+PROVIDE(__sec_size = __sec_end);
diff --git a/ld/testsuite/ld-loongarch-elf/pcrel-const.s b/ld/testsuite/ld-loongarch-elf/pcrel-const.s
new file mode 100644
index 00000000000..8b4a9d7aea0
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.s
@@ -0,0 +1,12 @@
+	.text
+	.align	2
+	.globl	foo
+	.type	foo, @function
+foo:
+	nop
+	la.global	$r4,__sec_size
+	ldptr.w	$r4,$r4,0
+	jr	$r1
+	.size	foo, .-foo
+	.data
+	.word 1
diff --git a/ld/testsuite/ld-loongarch-elf/pr.exp b/ld/testsuite/ld-loongarch-elf/pr.exp
new file mode 100644
index 00000000000..c0cc2fc30d2
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/pr.exp
@@ -0,0 +1,39 @@
+# Expect script for LoongArch ELF linker tests
+#   Copyright (C) 2022 Free Software Foundation, Inc.
+#
+# This file is part of the GNU Binutils.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+# MA 02110-1301, USA.
+#
+
+if ![istarget loongarch64-*-*] {
+  return
+}
+
+set link_tests [list \
+  [list \
+    "pcrel const" \
+    "-T pcrel-const.lds" "" \
+    "-mla-global-with-pcrel" \
+    { pcrel-const.s } \
+    [list \
+      [list objdump -D pcrel-const.d] \
+    ] \
+    "pcrel-const" \
+  ] \
+]
+
+run_ld_link_tests $link_tests
-- 
2.31.1


             reply	other threads:[~2022-09-03  8:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-03  8:41 liuzhensong [this message]
2022-09-03 16:55 ` Fangrui Song
2022-09-05  1:17   ` liuzhensong

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=20220903084149.1612310-1-liuzhensong@loongson.cn \
    --to=liuzhensong@loongson.cn \
    --cc=binutils@sourceware.org \
    --cc=i.swmail@xen0n.name \
    --cc=maskray@google.com \
    --cc=xry111@xry111.site \
    /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).