diff --git a/binutils-LoongArch-fix-gas-BFD_RELOC_8-16-24-bug.patch b/binutils-LoongArch-fix-gas-BFD_RELOC_8-16-24-bug.patch new file mode 100644 index 0000000000000000000000000000000000000000..e807b9b7c7bfc3b8e6b722b829ead2cb528f52f0 --- /dev/null +++ b/binutils-LoongArch-fix-gas-BFD_RELOC_8-16-24-bug.patch @@ -0,0 +1,136 @@ +From 09e55fb75d08ba82aa8b90fc92111c4f8e0e61bb Mon Sep 17 00:00:00 2001 +From: Li Xing +Date: Sat, 17 Sep 2022 00:56:33 +0000 +Subject: [PATCH 2/2] LoongArch: fix gas BFD_RELOC_8/16/24 bug + +If fixP->fx_subsy is NULL, BFD_RELOC_8/16/24 can't convert to +BFD_RELOC_LARCH_xxx. + +gas/config/tc-loongarch.c + +From: mengqinggang + +Change-Id: I03dfa44174d35342399593e1f70d1e8b68dae7dc +Signed-off-by: Li Xing +--- + gas/config/tc-loongarch.c | 57 ++++++++++++++++------- + gas/testsuite/gas/loongarch/bfd_reloc_8.s | 16 +++++++ + gas/testsuite/gas/loongarch/loongarch.exp | 1 + + 3 files changed, 57 insertions(+), 17 deletions(-) + create mode 100644 gas/testsuite/gas/loongarch/bfd_reloc_8.s + +diff --git a/gas/config/tc-loongarch.c b/gas/config/tc-loongarch.c +index fbbaca55..a8a9f95f 100644 +--- a/gas/config/tc-loongarch.c ++++ b/gas/config/tc-loongarch.c +@@ -1157,9 +1157,6 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED) + + case BFD_RELOC_64: + case BFD_RELOC_32: +- case BFD_RELOC_24: +- case BFD_RELOC_16: +- case BFD_RELOC_8: + + if (fixP->fx_r_type == BFD_RELOC_32 + && fixP->fx_addsy && fixP->fx_subsy +@@ -1191,25 +1188,51 @@ md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED) + fixP->fx_r_type = BFD_RELOC_LARCH_ADD32; + fixP->fx_next->fx_r_type = BFD_RELOC_LARCH_SUB32; + break; +- case BFD_RELOC_24: +- fixP->fx_r_type = BFD_RELOC_LARCH_ADD24; +- fixP->fx_next->fx_r_type = BFD_RELOC_LARCH_SUB24; +- break; +- case BFD_RELOC_16: +- fixP->fx_r_type = BFD_RELOC_LARCH_ADD16; +- fixP->fx_next->fx_r_type = BFD_RELOC_LARCH_SUB16; +- break; +- case BFD_RELOC_8: +- fixP->fx_r_type = BFD_RELOC_LARCH_ADD8; +- fixP->fx_next->fx_r_type = BFD_RELOC_LARCH_SUB8; +- break; + default: + break; + } + md_number_to_chars (buf, 0, fixP->fx_size); +- if (fixP->fx_next->fx_addsy == NULL) +- fixP->fx_next->fx_done = 1; + } ++ ++ if (fixP->fx_addsy == NULL) ++ { ++ fixP->fx_done = 1; ++ md_number_to_chars (buf, *valP, fixP->fx_size); ++ } ++ break; ++ ++ case BFD_RELOC_24: ++ case BFD_RELOC_16: ++ case BFD_RELOC_8: ++ fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP)); ++ fixP->fx_next->fx_addsy = fixP->fx_subsy; ++ fixP->fx_next->fx_subsy = NULL; ++ fixP->fx_next->fx_offset = 0; ++ fixP->fx_subsy = NULL; ++ ++ switch (fixP->fx_r_type) ++ { ++ case BFD_RELOC_24: ++ fixP->fx_r_type = BFD_RELOC_LARCH_ADD24; ++ fixP->fx_next->fx_r_type = BFD_RELOC_LARCH_SUB24; ++ break; ++ case BFD_RELOC_16: ++ fixP->fx_r_type = BFD_RELOC_LARCH_ADD16; ++ fixP->fx_next->fx_r_type = BFD_RELOC_LARCH_SUB16; ++ break; ++ case BFD_RELOC_8: ++ fixP->fx_r_type = BFD_RELOC_LARCH_ADD8; ++ fixP->fx_next->fx_r_type = BFD_RELOC_LARCH_SUB8; ++ break; ++ default: ++ break; ++ } ++ ++ md_number_to_chars (buf, 0, fixP->fx_size); ++ ++ if (fixP->fx_next->fx_addsy == NULL) ++ fixP->fx_next->fx_done = 1; ++ + if (fixP->fx_addsy == NULL) + { + fixP->fx_done = 1; +diff --git a/gas/testsuite/gas/loongarch/bfd_reloc_8.s b/gas/testsuite/gas/loongarch/bfd_reloc_8.s +new file mode 100644 +index 00000000..27388a58 +--- /dev/null ++++ b/gas/testsuite/gas/loongarch/bfd_reloc_8.s +@@ -0,0 +1,16 @@ ++# from linux kernel entry.s ++# test line 10 ".byte \type", BFD_RELOC_8 -> BFD_RELOC_RLARCH_ADD8 -> R_LARCH_ADD8 ++ ++.macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 end=0 ++.Lunwind_hint_ip_\@: ++ .pushsection .discard.unwind_hints ++ .long .Lunwind_hint_ip_\@ - . ++ .short \sp_offset ++ .byte \sp_reg ++ .byte \type ++ .byte \end ++ .balign 4 ++ .popsection ++.endm ++ ++UNWIND_HINT type=ORC_TYPE_CALL sp_reg=2 +diff --git a/gas/testsuite/gas/loongarch/loongarch.exp b/gas/testsuite/gas/loongarch/loongarch.exp +index 34a2f78c..b8ee4b25 100644 +--- a/gas/testsuite/gas/loongarch/loongarch.exp ++++ b/gas/testsuite/gas/loongarch/loongarch.exp +@@ -20,4 +20,5 @@ + + if [istarget loongarch*-*-*] { + run_dump_tests [lsort [glob -nocomplain $srcdir/$subdir/*.d]] ++ gas_test_old bfd_reloc_8.s "" "bfd_reloc_8" + } +-- +2.37.1 + diff --git a/binutils-LoongArch-ld-Fix-bug-not-generate-plt-when-link-a-ds.patch b/binutils-LoongArch-ld-Fix-bug-not-generate-plt-when-link-a-ds.patch new file mode 100644 index 0000000000000000000000000000000000000000..4f2b66d469a1d727a09c25cab8dc5b40399ec39c --- /dev/null +++ b/binutils-LoongArch-ld-Fix-bug-not-generate-plt-when-link-a-ds.patch @@ -0,0 +1,120 @@ +From 279f697a1b7959bd1a221ae5a442420073ac97e3 Mon Sep 17 00:00:00 2001 +From: Li Xing +Date: Sat, 17 Sep 2022 00:52:36 +0000 +Subject: [PATCH 1/2] LoongArch: ld: Fix bug not generate plt when link a dso + + Fix the bug that can not generate func@plt + when linking a undefined function with cmodel=medium. + Add testcase. + + bfd/ + * elfnn-loongarch.c + ld/testsuite/ld-loongarch-elf/ + * cmodel-libjirl.dd + * cmodel.exp + * libjirl.s + +From: liuzhensong + +Change-Id: I88566d34bf0011f16d6aab718ba0c2c4a887669d +Signed-off-by: Li Xing +--- + bfd/elfnn-loongarch.c | 8 ++++ + .../ld-loongarch-elf/cmodel-libjirl.dd | 4 ++ + ld/testsuite/ld-loongarch-elf/cmodel.exp | 37 +++++++++++++++++++ + ld/testsuite/ld-loongarch-elf/libjirl.s | 2 + + 4 files changed, 51 insertions(+) + create mode 100644 ld/testsuite/ld-loongarch-elf/cmodel-libjirl.dd + create mode 100644 ld/testsuite/ld-loongarch-elf/cmodel.exp + create mode 100644 ld/testsuite/ld-loongarch-elf/libjirl.s + +diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c +index 43182ead..c64e29fb 100644 +--- a/bfd/elfnn-loongarch.c ++++ b/bfd/elfnn-loongarch.c +@@ -746,6 +746,12 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, + case R_LARCH_PCALA_HI20: + if (h != NULL) + { ++ /* For pcalau12i + jirl. */ ++ h->needs_plt = 1; ++ if (h->plt.refcount < 0) ++ h->plt.refcount = 0; ++ h->plt.refcount++; ++ + h->non_got_ref = 1; + h->pointer_equality_needed = 1; + } +@@ -3123,6 +3129,8 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info, + { + unresolved_reloc = false; + BFD_ASSERT (rel->r_addend == 0); ++// if (rel->r_addend == 0) ++// fprintf(stderr, "----symbol %s got 0 addend\n", name); + + bfd_vma got_off = 0; + if (h != NULL) +diff --git a/ld/testsuite/ld-loongarch-elf/cmodel-libjirl.dd b/ld/testsuite/ld-loongarch-elf/cmodel-libjirl.dd +new file mode 100644 +index 00000000..52d3dca8 +--- /dev/null ++++ b/ld/testsuite/ld-loongarch-elf/cmodel-libjirl.dd +@@ -0,0 +1,4 @@ ++.*file format.*loongarch ++#... ++[0-9a-f]+ : ++#pass +diff --git a/ld/testsuite/ld-loongarch-elf/cmodel.exp b/ld/testsuite/ld-loongarch-elf/cmodel.exp +new file mode 100644 +index 00000000..7ef972a4 +--- /dev/null ++++ b/ld/testsuite/ld-loongarch-elf/cmodel.exp +@@ -0,0 +1,37 @@ ++# 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 loongarch*-*-*] { ++ return ++} ++ ++run_ld_link_tests [list \ ++ [list \ ++ "medium jirl plt" \ ++ "-shared" "" \ ++ "" \ ++ {libjirl.s} \ ++ [list \ ++ [list objdump -d cmodel-libjirl.dd] \ ++ ] \ ++ "libjirl.so" \ ++ ] \ ++ ] +diff --git a/ld/testsuite/ld-loongarch-elf/libjirl.s b/ld/testsuite/ld-loongarch-elf/libjirl.s +new file mode 100644 +index 00000000..4d963870 +--- /dev/null ++++ b/ld/testsuite/ld-loongarch-elf/libjirl.s +@@ -0,0 +1,2 @@ ++pcalau12i $r12, %pc_hi20(func) ++jirl $r1,$r12, %pc_lo12(func) +-- +2.37.1 + diff --git a/binutils.spec b/binutils.spec index a10b2f5363047060d1cb7ebe445b27e5d7b10761..fa2a2bcfdcd248823bb3f441f4ef38702b28dd85 100644 --- a/binutils.spec +++ b/binutils.spec @@ -1,7 +1,7 @@ Summary: Binary utilities Name: binutils Version: 2.37 -Release: 11 +Release: 13 License: GPLv3+ URL: https://sourceware.org/binutils @@ -31,6 +31,8 @@ Patch8: backport-0002-CVE-2021-42574.patch Patch9: backport-0003-CVE-2021-42574.patch Patch10: bfd-Close-the-file-descriptor-if-there-is-no-archive.patch Patch11: binutils-LoongArch-support.patch +Patch12: binutils-LoongArch-ld-Fix-bug-not-generate-plt-when-link-a-ds.patch +Patch13: binutils-LoongArch-fix-gas-BFD_RELOC_8-16-24-bug.patch Provides: bundled(libiberty) @@ -371,6 +373,18 @@ fi %{_infodir}/bfd*info* %changelog +* Mon Sep 19 2022 lixing - 2.37-13 +- Type:requirements +- ID:NA +- SUG:NA +- DESC: LoongArch fix gas BFD_RELOC_8-16-24 bug + +* Mon Sep 19 2022 lixing - 2.37-12 +- Type:requirements +- ID:NA +- SUG:NA +- DESC: LoongArch ld fix not generate plt bug + * Fri Sep 02 2022 lixing - 2.37-11 - Type:requirements - ID:NA