From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7867) id CD3383846426; Thu, 25 Apr 2024 01:25:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CD3383846426 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1714008345; bh=ahHykryNLpKlzRQ4NpdQKbG7ExE1GxEdRBfCZnXqcU4=; h=From:To:Subject:Date:From; b=GPZrZhvq10tdvsLcYOGFqUEDAsvGNSrnKrJ663eHMmUxQvWHMkzMC3C6g3JqjVxcZ MXPYW+Nq0SbWq/6N0DbPr1HwCx8buQlNv2AcgtIZTynv1ti+38Bq8FZI8f1LxZMkta zun4WYwXan4UHFWhqzOBwobMod4cGjIJCicaYIbw= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: liu & zhensong To: binutils-cvs@sourceware.org Subject: [binutils-gdb] LoongArch: Add bad static relocation check and output more information to user X-Act-Checkin: binutils-gdb X-Git-Author: Lulu Cai X-Git-Refname: refs/heads/master X-Git-Oldrev: b221bff7c7ca20d961a7e6b663a545384054a5be X-Git-Newrev: 66156bae7a0581879763514382d112716464db8a Message-Id: <20240425012545.CD3383846426@sourceware.org> Date: Thu, 25 Apr 2024 01:25:45 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D66156bae7a05= 81879763514382d112716464db8a commit 66156bae7a0581879763514382d112716464db8a Author: Lulu Cai Date: Tue Mar 19 17:51:19 2024 +0800 LoongArch: Add bad static relocation check and output more information = to user =20 Absolute address symbols cannot be used with -shared. We output more information to the user than just BFD_ASSETR. Diff: --- bfd/elfnn-loongarch.c | 33 ++++++++++++++++++= ++-- ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp | 3 ++ .../ld-loongarch-elf/reloc_abs_with_shared.d | 6 ++++ .../ld-loongarch-elf/reloc_abs_with_shared.s | 9 ++++++ .../ld-loongarch-elf/reloc_le_with_shared.d | 6 ++++ .../ld-loongarch-elf/reloc_le_with_shared.s | 8 ++++++ .../ld-loongarch-elf/reloc_ler_with_shared.d | 4 +++ .../ld-loongarch-elf/reloc_ler_with_shared.s | 9 ++++++ 8 files changed, 76 insertions(+), 2 deletions(-) diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index fe780e39610..4761ee1851c 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -760,6 +760,33 @@ loongarch_tls_transition (bfd *input_bfd, allocate space in the global offset table or procedure linkage table. */ =20 +static bool +bad_static_reloc (bfd *abfd, const Elf_Internal_Rela *rel, asection *sec, + unsigned r_type, struct elf_link_hash_entry *h, + Elf_Internal_Sym *isym) +{ + /* We propably can improve the information to tell users that they should + be recompile the code with -fPIC or -fPIE, just like what x86 does. = */ + reloc_howto_type * r =3D loongarch_elf_rtype_to_howto (abfd, r_type); + const char *name =3D NULL; + + if (h) + name =3D h->root.root.string; + else if (isym) + name =3D bfd_elf_string_from_elf_section (abfd, + elf_symtab_hdr (abfd).sh_link, + isym->st_name); + if (name =3D=3D NULL || *name =3D=3D '\0') + name =3D""; + + (*_bfd_error_handler) + (_("%pB:(%pA+%#lx): relocation %s against `%s` can not be used when mak= ing " + "a shared object; recompile with -fPIC"), + abfd, sec, rel->r_offset, r ? r->name : _(""), name); + bfd_set_error (bfd_error_bad_value); + return false; +} + static bool loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec, const Elf_Internal_Rela *relocs) @@ -904,7 +931,7 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_= info *info, case R_LARCH_TLS_LE_HI20_R: case R_LARCH_SOP_PUSH_TLS_TPREL: if (!bfd_link_executable (info)) - return false; + return bad_static_reloc (abfd, rel, sec, r_type, h, isym); =20 if (!loongarch_elf_record_tls_and_got_reference (abfd, info, h, r_symndx, @@ -922,6 +949,9 @@ loongarch_elf_check_relocs (bfd *abfd, struct bfd_link_= info *info, =20 case R_LARCH_ABS_HI20: case R_LARCH_SOP_PUSH_ABSOLUTE: + if (bfd_link_pic (info)) + return bad_static_reloc (abfd, rel, sec, r_type, h, isym); + if (h !=3D NULL) /* If this reloc is in a read-only section, we might need a copy reloc. We can't check reliably at this @@ -3398,7 +3428,6 @@ loongarch_elf_relocate_section (bfd *output_bfd, stru= ct bfd_link_info *info, case R_LARCH_ABS_LO12: case R_LARCH_ABS64_LO20: case R_LARCH_ABS64_HI12: - BFD_ASSERT (!is_pic); =20 if (is_undefweak) { diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsu= ite/ld-loongarch-elf/ld-loongarch-elf.exp index c2d616b8d0a..40f56576cbc 100644 --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp @@ -134,6 +134,9 @@ if [istarget "loongarch64-*-*"] { run_dump_test "desc-norelax" run_dump_test "desc-relax" run_dump_test "data-got" + run_dump_test "reloc_le_with_shared" + run_dump_test "reloc_ler_with_shared" + run_dump_test "reloc_abs_with_shared" } =20 if [check_pie_support] { diff --git a/ld/testsuite/ld-loongarch-elf/reloc_abs_with_shared.d b/ld/tes= tsuite/ld-loongarch-elf/reloc_abs_with_shared.d new file mode 100644 index 00000000000..532e84fb600 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/reloc_abs_with_shared.d @@ -0,0 +1,6 @@ +#source: reloc_abs_with_shared.s +#as: +#ld: -shared +#error: .*relocation R_LARCH_ABS_HI20 against `s` can not be used when mak= ing a shared object; recompile with -fPIC +#... +#pass diff --git a/ld/testsuite/ld-loongarch-elf/reloc_abs_with_shared.s b/ld/tes= tsuite/ld-loongarch-elf/reloc_abs_with_shared.s new file mode 100644 index 00000000000..13341af2bd9 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/reloc_abs_with_shared.s @@ -0,0 +1,9 @@ + .text + .globl s + .data + .type s, @object + s: + .word 123 + .text + lu12i.w $r4,%abs_hi20(s) + addi.d $r4,$r4,%abs_lo12(s) diff --git a/ld/testsuite/ld-loongarch-elf/reloc_le_with_shared.d b/ld/test= suite/ld-loongarch-elf/reloc_le_with_shared.d new file mode 100644 index 00000000000..562b079af44 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/reloc_le_with_shared.d @@ -0,0 +1,6 @@ +#source: reloc_le_with_shared.s +#as: +#ld: -shared +#error: .*relocation R_LARCH_TLS_LE_HI20 against `s` can not be used when = making a shared object; recompile with -fPIC +#... +#pass diff --git a/ld/testsuite/ld-loongarch-elf/reloc_le_with_shared.s b/ld/test= suite/ld-loongarch-elf/reloc_le_with_shared.s new file mode 100644 index 00000000000..c720665002b --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/reloc_le_with_shared.s @@ -0,0 +1,8 @@ + .text + .globl s + .section .tdata,"awT",@progbits + .type s, @object + s: + .word 123 + .text + la.tls.le $r4, s diff --git a/ld/testsuite/ld-loongarch-elf/reloc_ler_with_shared.d b/ld/tes= tsuite/ld-loongarch-elf/reloc_ler_with_shared.d new file mode 100644 index 00000000000..7382d5b8e1a --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/reloc_ler_with_shared.d @@ -0,0 +1,4 @@ +#source: reloc_ler_with_shared.s +#as: +#ld: -shared +#error: .*relocation R_LARCH_TLS_LE_HI20_R against `s` can not be used whe= n making a shared object; recompile with -fPIC diff --git a/ld/testsuite/ld-loongarch-elf/reloc_ler_with_shared.s b/ld/tes= tsuite/ld-loongarch-elf/reloc_ler_with_shared.s new file mode 100644 index 00000000000..a9e5696715c --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/reloc_ler_with_shared.s @@ -0,0 +1,9 @@ + .text + .globl s + .section .tdata,"awT",@progbits + .type s, @object + s: + .word 123 + .text + lu12i.w $r4,%le_hi20_r(s) + add.d $r4,$r4,$r2,%le_add_r(s)