toolchain/binutils: backport stable patches
[openwrt/staging/dangole.git] / toolchain / binutils / patches / 2.39 / 039-LoongArch-ld-Fix-relocation-error-of-pcrel.patch
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.
5
6 Patch for branch 2.39.
7 Need to reduce the address of pc when using
8 reloction R_LARCH_SOP_PUSH_PCREL.
9
10 bfd/
11 * elfnn-loongarch.c
12 ---
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
23
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;
29
30 - if (resolved_to_const)
31 + if (!is_undefweak && resolved_to_const)
32 {
33 relocation += rel->r_addend;
34 + relocation -= pc;
35 break;
36 }
37 else if (is_undefweak)
38 --- /dev/null
39 +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.d
40 @@ -0,0 +1,14 @@
41 +#as: -mla-global-with-pcrel
42 +#objdump: -Drsz
43 +
44 +.*:[ ]+file format .*
45 +
46 +
47 +Disassembly of section .text:
48 +
49 +.* <foo>:
50 +#...
51 +[ ]+8:[ ]+02c04084[ ]+addi.d[ ]+\$a0,[ ]+\$a0,[ ]+16\(0x10\)
52 +#...
53 +0+14 <__sec_end>:
54 +#pass
55 --- /dev/null
56 +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.lds
57 @@ -0,0 +1,14 @@
58 +ENTRY(foo);
59 +SECTIONS
60 +{
61 + .text : {
62 + *(.text*)
63 + }
64 +
65 + .data : {
66 + __sec_start = .;
67 + *(.gzdata)
68 + __sec_end = .;
69 + }
70 +}
71 +PROVIDE(__sec_size = __sec_end);
72 --- /dev/null
73 +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.s
74 @@ -0,0 +1,12 @@
75 + .text
76 + .align 2
77 + .globl foo
78 + .type foo, @function
79 +foo:
80 + nop
81 + la.global $r4,__sec_size
82 + ldptr.w $r4,$r4,0
83 + jr $r1
84 + .size foo, .-foo
85 + .data
86 + .word 1
87 --- /dev/null
88 +++ b/ld/testsuite/ld-loongarch-elf/pr.exp
89 @@ -0,0 +1,39 @@
90 +# Expect script for LoongArch ELF linker tests
91 +# Copyright (C) 2022 Free Software Foundation, Inc.
92 +#
93 +# This file is part of the GNU Binutils.
94 +#
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.
99 +#
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.
104 +#
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.
109 +#
110 +
111 +if ![istarget loongarch64-*-*] {
112 + return
113 +}
114 +
115 +set link_tests [list \
116 + [list \
117 + "pcrel const" \
118 + "-T pcrel-const.lds" "" \
119 + "-mla-global-with-pcrel" \
120 + { pcrel-const.s } \
121 + [list \
122 + [list objdump -D pcrel-const.d] \
123 + ] \
124 + "pcrel-const" \
125 + ] \
126 +]
127 +
128 +run_ld_link_tests $link_tests