1 From 509a2ec6ad3ea7eb3f4cf59538cf636a2126e4c3 Mon Sep 17 00:00:00 2001
2 From: liuzhensong <liuzhensong@loongson.cn>
3 Date: Fri, 2 Sep 2022 16:29:14 +0800
4 Subject: [PATCH 039/160] LoongArch:ld: Fix relocation error of pcrel.
7 Need to reduce the address of pc when using
8 reloction R_LARCH_SOP_PUSH_PCREL.
13 bfd/elfnn-loongarch.c | 3 +-
14 ld/testsuite/ld-loongarch-elf/pcrel-const.d | 14 +++++++
15 ld/testsuite/ld-loongarch-elf/pcrel-const.lds | 14 +++++++
16 ld/testsuite/ld-loongarch-elf/pcrel-const.s | 12 ++++++
17 ld/testsuite/ld-loongarch-elf/pr.exp | 39 +++++++++++++++++++
18 5 files changed, 81 insertions(+), 1 deletion(-)
19 create mode 100644 ld/testsuite/ld-loongarch-elf/pcrel-const.d
20 create mode 100644 ld/testsuite/ld-loongarch-elf/pcrel-const.lds
21 create mode 100644 ld/testsuite/ld-loongarch-elf/pcrel-const.s
22 create mode 100644 ld/testsuite/ld-loongarch-elf/pr.exp
24 --- a/bfd/elfnn-loongarch.c
25 +++ b/bfd/elfnn-loongarch.c
26 @@ -2341,9 +2341,10 @@ loongarch_elf_relocate_section (bfd *out
27 case R_LARCH_SOP_PUSH_PLT_PCREL:
28 unresolved_reloc = false;
30 - if (resolved_to_const)
31 + if (!is_undefweak && resolved_to_const)
33 relocation += rel->r_addend;
37 else if (is_undefweak)
39 +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.d
41 +#as: -mla-global-with-pcrel
44 +.*:[ ]+file format .*
47 +Disassembly of section .text:
51 +[ ]+8:[ ]+02c04084[ ]+addi.d[ ]+\$a0,[ ]+\$a0,[ ]+16\(0x10\)
56 +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.lds
71 +PROVIDE(__sec_size = __sec_end);
73 +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.s
78 + .type foo, @function
81 + la.global $r4,__sec_size
88 +++ b/ld/testsuite/ld-loongarch-elf/pr.exp
90 +# Expect script for LoongArch ELF linker tests
91 +# Copyright (C) 2022 Free Software Foundation, Inc.
93 +# This file is part of the GNU Binutils.
95 +# This program is free software; you can redistribute it and/or modify
96 +# it under the terms of the GNU General Public License as published by
97 +# the Free Software Foundation; either version 3 of the License, or
98 +# (at your option) any later version.
100 +# This program is distributed in the hope that it will be useful,
101 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
102 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
103 +# GNU General Public License for more details.
105 +# You should have received a copy of the GNU General Public License
106 +# along with this program; if not, write to the Free Software
107 +# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
108 +# MA 02110-1301, USA.
111 +if ![istarget loongarch64-*-*] {
115 +set link_tests [list \
118 + "-T pcrel-const.lds" "" \
119 + "-mla-global-with-pcrel" \
120 + { pcrel-const.s } \
122 + [list objdump -D pcrel-const.d] \
128 +run_ld_link_tests $link_tests