* [PATCH] LoongArch:ld: Fix relocation error of pcrel.
@ 2022-09-03 1:28 liuzhensong
2022-09-03 4:33 ` Fangrui Song
0 siblings, 1 reply; 3+ messages in thread
From: liuzhensong @ 2022-09-03 1:28 UTC (permalink / raw)
To: binutils; +Cc: i.swmail, xry111, maskray, chenhuacai, liuzhensong
Patch for branch 2.39.
Need to reduce the address of pc when using
reloction R_LARCH_SOP_PUSH_PLT_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 | 11 ++++++
ld/testsuite/ld-loongarch-elf/pr.exp | 39 +++++++++++++++++++
5 files changed, 80 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..72abb204122
--- /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>:
+#...
+[ ]+4:[ ]+02c03084[ ]+addi.d[ ]+\$a0,[ ]+\$a0,[ ]+12\(0xc\)
+#...
+0+10 <__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..064a6894b4d
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.lds
@@ -0,0 +1,14 @@
+ENTRY(foo);
+SECTIONS
+{
+ .text : ALIGN(4) {
+ *(.text*)
+ }
+
+ .data : ALIGN(4) {
+ __sec_start = .;
+ *(.gzdata)
+ __sec_end = .;
+ }
+}
+PROVIDE(__sec_size = __sec_end - 4);
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..bc4fb979a80
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.s
@@ -0,0 +1,11 @@
+ .text
+ .align 2
+ .globl foo
+ .type foo, @function
+foo:
+ 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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] LoongArch:ld: Fix relocation error of pcrel.
2022-09-03 1:28 [PATCH] LoongArch:ld: Fix relocation error of pcrel liuzhensong
@ 2022-09-03 4:33 ` Fangrui Song
2022-09-03 7:12 ` liuzhensong
0 siblings, 1 reply; 3+ messages in thread
From: Fangrui Song @ 2022-09-03 4:33 UTC (permalink / raw)
To: liuzhensong; +Cc: binutils, i.swmail, xry111, chenhuacai
On Fri, Sep 2, 2022 at 6:29 PM liuzhensong <liuzhensong@loongson.cn> wrote:
>
> Patch for branch 2.39.
> Need to reduce the address of pc when using
> reloction R_LARCH_SOP_PUSH_PLT_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 | 11 ++++++
> ld/testsuite/ld-loongarch-elf/pr.exp | 39 +++++++++++++++++++
> 5 files changed, 80 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..72abb204122
> --- /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>:
> +#...
> +[ ]+4:[ ]+02c03084[ ]+addi.d[ ]+\$a0,[ ]+\$a0,[ ]+12\(0xc\)
> +#...
> +0+10 <__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..064a6894b4d
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.lds
> @@ -0,0 +1,14 @@
> +ENTRY(foo);
> +SECTIONS
> +{
> + .text : ALIGN(4) {
> + *(.text*)
> + }
> +
> + .data : ALIGN(4) {
> + __sec_start = .;
> + *(.gzdata)
> + __sec_end = .;
> + }
> +}
> +PROVIDE(__sec_size = __sec_end - 4);
I do not know the legacy relocation scheme, but for tests I would try
removing noise like ALIGN(4). .data output section containing .gzdata
input sections looks strange too.
Can .data input sections be used instead?
`__sec_size = __sec_end - 4` is unusual. Is `- 4` necessary to
reproduce the bug? If necessary, add a comment.
> 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..bc4fb979a80
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.s
> @@ -0,0 +1,11 @@
> + .text
> + .align 2
> + .globl foo
> + .type foo, @function
> +foo:
> + 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
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] LoongArch:ld: Fix relocation error of pcrel.
2022-09-03 4:33 ` Fangrui Song
@ 2022-09-03 7:12 ` liuzhensong
0 siblings, 0 replies; 3+ messages in thread
From: liuzhensong @ 2022-09-03 7:12 UTC (permalink / raw)
To: Fangrui Song; +Cc: binutils, i.swmail, xry111, chenhuacai
[-- Attachment #1: Type: text/plain, Size: 5761 bytes --]
在 2022/9/3 下午12:33, Fangrui Song 写道:
> On Fri, Sep 2, 2022 at 6:29 PM liuzhensong <liuzhensong@loongson.cn> wrote:
>> Patch for branch 2.39.
>> Need to reduce the address of pc when using
>> reloction R_LARCH_SOP_PUSH_PLT_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 | 11 ++++++
>> ld/testsuite/ld-loongarch-elf/pr.exp | 39 +++++++++++++++++++
>> 5 files changed, 80 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..72abb204122
>> --- /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>:
>> +#...
>> +[ ]+4:[ ]+02c03084[ ]+addi.d[ ]+\$a0,[ ]+\$a0,[ ]+12\(0xc\)
>> +#...
>> +0+10 <__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..064a6894b4d
>> --- /dev/null
>> +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.lds
>> @@ -0,0 +1,14 @@
>> +ENTRY(foo);
>> +SECTIONS
>> +{
>> + .text : ALIGN(4) {
>> + *(.text*)
>> + }
>> +
>> + .data : ALIGN(4) {
>> + __sec_start = .;
>> + *(.gzdata)
>> + __sec_end = .;
>> + }
>> +}
>> +PROVIDE(__sec_size = __sec_end - 4);
> I do not know the legacy relocation scheme, but for tests I would try
> removing noise like ALIGN(4). .data output section containing .gzdata
> input sections looks strange too.
> Can .data input sections be used instead?
The __sec_end segment cannot be dumped using .data.
t: file format elf64-loongarch
Disassembly of section .text:
0000000000000000 <foo>:
0: 03400000 andi $zero, $zero, 0x0
4: 1c000004 pcaddu12i $a0, 0
8: 02c06084 addi.d $a0, $a0, 24(0x18)
c: 24000084 ldptr.w $a0, $a0, 0
10: 4c000020 jirl $zero, $ra, 0
Disassembly of section .data:
0000000000000014 <__sec_start>:
14: 00000001 0x00000001
>
> `__sec_size = __sec_end - 4` is unusual. Is `- 4` necessary to
> reproduce the bug? If necessary, add a comment.
Unnecessary, using "nop" before pcaddu12i instead.
>
>> 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..bc4fb979a80
>> --- /dev/null
>> +++ b/ld/testsuite/ld-loongarch-elf/pcrel-const.s
>> @@ -0,0 +1,11 @@
>> + .text
>> + .align 2
>> + .globl foo
>> + .type foo, @function
>> +foo:
>> + 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
>>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-09-03 7:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-03 1:28 [PATCH] LoongArch:ld: Fix relocation error of pcrel liuzhensong
2022-09-03 4:33 ` Fangrui Song
2022-09-03 7:12 ` liuzhensong
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).