From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7867) id 4F7F13858D28; Sun, 31 Mar 2024 06:21:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4F7F13858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1711866109; bh=7ofcNSUiW90ucn+R+wI7yDpENWdfZt1pQ+HdHXF2Cz0=; h=From:To:Subject:Date:From; b=WdRTGGAtQkL30VTdbqzQc2qjCBg69qyslN4UQovSOCTEQpHl8mPdkTynWMS2hxwgC PHyrFf7bkg7FW3sV/5/QaSvrXIs23XZnDl4JdzKsiK/e7Zjp+LPE8OY5hNIPnsHIfw vLgI8h0cmvbYAni0G6EYpIC2KH4XqdPAMfb1/sHw= 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] BFD: Fix the bug of R_LARCH_AGLIN caused by discard section X-Act-Checkin: binutils-gdb X-Git-Author: mengqinggang X-Git-Refname: refs/heads/master X-Git-Oldrev: c7a5bea4c62f286df830418de694821c7617cccd X-Git-Newrev: daeda14191c1710ce967259a47ef4e0a3fb6eebf Message-Id: <20240331062149.4F7F13858D28@sourceware.org> Date: Sun, 31 Mar 2024 06:21:49 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Ddaeda14191c1= 710ce967259a47ef4e0a3fb6eebf commit daeda14191c1710ce967259a47ef4e0a3fb6eebf Author: mengqinggang Date: Wed Jan 24 14:34:26 2024 +0800 BFD: Fix the bug of R_LARCH_AGLIN caused by discard section =20 To represent the first and third expression of .align, R_LARCH_ALIGN ne= ed to associate with a symbol. We define a local symbol for R_LARCH_AGLIN. But if the section of the local symbol is discarded, it may result in a undefined symbol error. =20 Instead, we use the section name symbols, and this does not need to add extra symbols. =20 During partial linking (ld -r), if the symbol associated with a relocat= ion is STT_SECTION type, the addend of relocation needs to add the section out= put offset. We prevent it for R_LARCH_ALIGN. =20 The elf_backend_data.rela_normal only can set all relocations of a targ= et to rela_normal. Add a new function is_rela_normal to elf_backend_data, it = can set part of relocations to rela_normal. Diff: --- bfd/elf-bfd.h | 4 ++++ bfd/elflink.c | 5 ++++- bfd/elfnn-loongarch.c | 16 ++++++++++++++++ bfd/elfxx-target.h | 5 +++++ gas/config/tc-loongarch.c | 5 +---- gas/testsuite/gas/loongarch/relax_align.d | 6 +++--- ld/testsuite/ld-loongarch-elf/relax-align-discard.lds | 4 ++++ ld/testsuite/ld-loongarch-elf/relax-align-discard.s | 17 +++++++++++++++= ++ ld/testsuite/ld-loongarch-elf/relax.exp | 12 ++++++++++++ 9 files changed, 66 insertions(+), 8 deletions(-) diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index c5d325435b6..af507b93df5 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -1721,6 +1721,10 @@ struct elf_backend_data backend relocate_section routine for relocatable linking. */ unsigned rela_normal : 1; =20 + /* Whether a relocation is rela_normal. Compared with rela_normal, + is_rela_normal can set part of relocations to rela_normal. */ + bool (*is_rela_normal) (Elf_Internal_Rela *); + /* Set if DT_REL/DT_RELA/DT_RELSZ/DT_RELASZ should not include PLT relocations. */ unsigned dtrel_excludes_plt : 1; diff --git a/bfd/elflink.c b/bfd/elflink.c index 24eb30d1946..5d9b4092e48 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -11692,7 +11692,10 @@ elf_link_input_bfd (struct elf_final_link_info *fl= info, bfd *input_bfd) { rel_hash =3D PTR_ADD (esdo->rela.hashes, esdo->rela.count); rela_hash_list =3D rel_hash; - rela_normal =3D bed->rela_normal; + if (bed->is_rela_normal !=3D NULL) + rela_normal =3D bed->is_rela_normal (irela); + else + rela_normal =3D bed->rela_normal; } =20 irela->r_offset =3D _bfd_elf_section_offset (output_bfd, diff --git a/bfd/elfnn-loongarch.c b/bfd/elfnn-loongarch.c index c42052f9321..1679aa5da7d 100644 --- a/bfd/elfnn-loongarch.c +++ b/bfd/elfnn-loongarch.c @@ -5454,6 +5454,21 @@ elf_loongarch64_hash_symbol (struct elf_link_hash_en= try *h) return _bfd_elf_hash_symbol (h); } =20 +/* If a relocation is rela_normal and the symbol associated with the + relocation is STT_SECTION type, the addend of the relocation would add + sec->output_offset when partial linking (ld -r). + See elf_backend_data.rela_normal and elf_link_input_bfd(). + The addend of R_LARCH_ALIGN is used to represent the first and third + expression of .align, it should be a constant when linking. */ + +static bool +loongarch_elf_is_rela_normal (Elf_Internal_Rela *rel) +{ + if (R_LARCH_ALIGN =3D=3D ELFNN_R_TYPE (rel->r_info)) + return false; + return true; +} + #define TARGET_LITTLE_SYM loongarch_elfNN_vec #define TARGET_LITTLE_NAME "elfNN-loongarch" #define ELF_ARCH bfd_arch_loongarch @@ -5489,6 +5504,7 @@ elf_loongarch64_hash_symbol (struct elf_link_hash_ent= ry *h) #define elf_backend_grok_psinfo loongarch_elf_grok_psinfo #define elf_backend_hash_symbol elf_loongarch64_hash_symbol #define bfd_elfNN_bfd_relax_section loongarch_elf_relax_section +#define elf_backend_is_rela_normal loongarch_elf_is_rela_normal =20 #define elf_backend_dtrel_excludes_plt 1 =20 diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h index 1e6992b5793..6e2d948b69b 100644 --- a/bfd/elfxx-target.h +++ b/bfd/elfxx-target.h @@ -709,6 +709,10 @@ #define elf_backend_rela_normal 0 #endif =20 +#ifndef elf_backend_is_rela_normal +#define elf_backend_is_rela_normal NULL +#endif + #ifndef elf_backend_dtrel_excludes_plt #define elf_backend_dtrel_excludes_plt 0 #endif @@ -955,6 +959,7 @@ static const struct elf_backend_data elfNN_bed =3D elf_backend_default_use_rela_p, elf_backend_rela_plts_and_copies_p, elf_backend_rela_normal, + elf_backend_is_rela_normal, elf_backend_dtrel_excludes_plt, elf_backend_sign_extend_vma, elf_backend_want_got_plt, diff --git a/gas/config/tc-loongarch.c b/gas/config/tc-loongarch.c index 30aefce36fd..6b1a89738ef 100644 --- a/gas/config/tc-loongarch.c +++ b/gas/config/tc-loongarch.c @@ -1791,10 +1791,7 @@ loongarch_frag_align_code (int n, int max) if (fragP->fr_subtype !=3D 0 && offset > fragP->fr_subtype). */ if (max > 0 && (bfd_vma) max < worst_case_bytes) { - s =3D symbol_find (".Lla-relax-align"); - if (s =3D=3D NULL) - s =3D (symbolS *)local_symbol_make (".Lla-relax-align", now_seg, - &zero_address_frag, 0); + s =3D symbol_find (now_seg->name); ex.X_add_symbol =3D s; ex.X_op =3D O_symbol; ex.X_add_number =3D (max << 8) | n; diff --git a/gas/testsuite/gas/loongarch/relax_align.d b/gas/testsuite/gas/= loongarch/relax_align.d index fc1fd032611..acd215a4abc 100644 --- a/gas/testsuite/gas/loongarch/relax_align.d +++ b/gas/testsuite/gas/loongarch/relax_align.d @@ -7,7 +7,7 @@ =20 Disassembly of section .text: =20 -[ ]*0000000000000000 <.Lla-relax-align>: +[ ]*0000000000000000 <.text>: [ ]+0:[ ]+4c000020[ ]+ret [ ]+4:[ ]+03400000[ ]+nop [ ]+4: R_LARCH_ALIGN[ ]+\*ABS\*\+0xc @@ -20,12 +20,12 @@ Disassembly of section .text: [ ]+1c:[ ]+03400000[ ]+nop [ ]+20:[ ]+4c000020[ ]+ret [ ]+24:[ ]+03400000[ ]+nop -[ ]+24: R_LARCH_ALIGN[ ]+.Lla-relax-align\+0x104 +[ ]+24: R_LARCH_ALIGN[ ]+.text\+0x104 [ ]+28:[ ]+03400000[ ]+nop [ ]+2c:[ ]+03400000[ ]+nop [ ]+30:[ ]+4c000020[ ]+ret [ ]+34:[ ]+03400000[ ]+nop -[ ]+34: R_LARCH_ALIGN[ ]+.Lla-relax-align\+0xb04 +[ ]+34: R_LARCH_ALIGN[ ]+.text\+0xb04 [ ]+38:[ ]+03400000[ ]+nop [ ]+3c:[ ]+03400000[ ]+nop [ ]+40:[ ]+4c000020[ ]+ret diff --git a/ld/testsuite/ld-loongarch-elf/relax-align-discard.lds b/ld/tes= tsuite/ld-loongarch-elf/relax-align-discard.lds new file mode 100644 index 00000000000..4a81323d926 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/relax-align-discard.lds @@ -0,0 +1,4 @@ +SECTIONS +{ + /DISCARD/ : { *(.another.*) } +} diff --git a/ld/testsuite/ld-loongarch-elf/relax-align-discard.s b/ld/tests= uite/ld-loongarch-elf/relax-align-discard.s new file mode 100644 index 00000000000..b65d63f370f --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/relax-align-discard.s @@ -0,0 +1,17 @@ +# Use the section name symbol for R_LARCH_ALIGN to avoid discard section p= roblem +.section ".another.text", "ax" +.cfi_startproc +break 0 +.cfi_def_cfa_offset 16 +.p2align 5 +break 1 +.cfi_endproc + +.text +.cfi_startproc +break 0 +.cfi_def_cfa_offset 16 +.p2align 5 +break 1 +.cfi_endproc + diff --git a/ld/testsuite/ld-loongarch-elf/relax.exp b/ld/testsuite/ld-loon= garch-elf/relax.exp index 7d95a9ca41d..ed71fb45b46 100644 --- a/ld/testsuite/ld-loongarch-elf/relax.exp +++ b/ld/testsuite/ld-loongarch-elf/relax.exp @@ -295,6 +295,18 @@ if [istarget loongarch64-*-*] { "relax-align" \ ] \ ] + + run_ld_link_tests \ + [list \ + [list \ + "loongarch relax align discard" \ + "-e 0x0 -T relax-align-discard.lds -r" "" \ + "" \ + {relax-align-discard.s} \ + {} \ + "relax-align-discard" \ + ] \ + ] } =20 set objdump_flags "-s -j .data"