From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7867) id 8EF893858D33; Sat, 20 Apr 2024 04:11:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8EF893858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1713586290; bh=6necjTkRAs6qR3DVPxHLj8awZvUzWX8V84UxV3tGb7k=; h=From:To:Subject:Date:From; b=okFGjAkOQP/0e75MZJh+atElxQBayllrjvCdSFTAJ9crvR+9933t+vIVwNOtMCQ1F HN86DJzlRrvm1coKYp/PUh5O1i/PJ+D16L+0IaOAMixafGjF+KYThpLS/tPDh2pBFM JZT7PfW5nc9AVCgZpUNNznAM7m6upwuQfBxMnICE= 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 -mignore-start-align option X-Act-Checkin: binutils-gdb X-Git-Author: mengqinggang X-Git-Refname: refs/heads/master X-Git-Oldrev: 02fa4bbec1974df262f44e13023071f5ae4fc9b1 X-Git-Newrev: 20eee7540b9f2615f7661393756fec0bb62a1495 Message-Id: <20240420041130.8EF893858D33@sourceware.org> Date: Sat, 20 Apr 2024 04:11:30 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D20eee7540b9f= 2615f7661393756fec0bb62a1495 commit 20eee7540b9f2615f7661393756fec0bb62a1495 Author: mengqinggang Date: Sun Apr 7 16:34:42 2024 +0800 LoongArch: Add -mignore-start-align option =20 Ignore .align at the start of a section may result in misalignment when partial linking. Manually add -mignore-start-align option without parti= al linking. =20 Gcc -falign-functions add .align 5 to the start of a section, it causes= some error message mismatch. Set these testcases to xfail on LoongArch targe= t. Diff: --- gas/config/tc-loongarch.c | 70 +++++++++++++++---= ---- ...ax-align-first.d =3D> relax-align-ignore-start.d} | 2 +- ...ax-align-first.s =3D> relax-align-ignore-start.s} | 0 include/opcode/loongarch.h | 1 + ld/testsuite/ld-elf/dwarf.exp | 5 ++ .../ld-loongarch-elf/partial-link-align-a.s | 2 + .../ld-loongarch-elf/partial-link-align-b.s | 3 + ...ax-align-first.d =3D> relax-align-ignore-start.d} | 1 + ...ax-align-first.s =3D> relax-align-ignore-start.s} | 0 ld/testsuite/ld-loongarch-elf/relax.exp | 32 +++++++++- ld/testsuite/ld-undefined/undefined.exp | 2 + 11 files changed, 95 insertions(+), 23 deletions(-) diff --git a/gas/config/tc-loongarch.c b/gas/config/tc-loongarch.c index d156477d1e6..00b8c80c3de 100644 --- a/gas/config/tc-loongarch.c +++ b/gas/config/tc-loongarch.c @@ -139,15 +139,17 @@ enum options =20 OPTION_ABI, OPTION_FLOAT_ABI, - OPTION_FLOAT_ISA, =20 OPTION_LA_LOCAL_WITH_ABS, OPTION_LA_GLOBAL_WITH_PCREL, OPTION_LA_GLOBAL_WITH_ABS, + OPTION_RELAX, OPTION_NO_RELAX, + OPTION_THIN_ADD_SUB, + OPTION_IGNORE_START_ALIGN, =20 OPTION_END_OF_ENUM, }; @@ -165,6 +167,7 @@ struct option md_longopts[] =3D { "mrelax", no_argument, NULL, OPTION_RELAX }, { "mno-relax", no_argument, NULL, OPTION_NO_RELAX }, { "mthin-add-sub", no_argument, NULL, OPTION_THIN_ADD_SUB}, + { "mignore-start-align", no_argument, NULL, OPTION_IGNORE_START_ALIGN}, =20 { NULL, no_argument, NULL, 0 } }; @@ -247,6 +250,10 @@ md_parse_option (int c, const char *arg) LARCH_opts.thin_add_sub =3D 1; break; =20 + case OPTION_IGNORE_START_ALIGN: + LARCH_opts.ignore_start_align =3D 1; + break; + case OPTION_IGNORE: break; =20 @@ -1772,7 +1779,9 @@ md_show_usage (FILE *stream) -mthin-add-sub Convert a pair of R_LARCH_ADD32/64 and R_LARCH_SUB32/64= to\n\ R_LARCH_32/64_PCREL as much as possible\n\ The option does not affect the generation of R_LARCH_32_PCREL\n\ - relocations in .eh_frame\n")); + relocations in .eh_frame\n\ + -mignore-start-align Ignore .align if it is at the start of a section.= This option\n\ + can't be used when partial linking (ld -r).\n")); } =20 static void @@ -1794,39 +1803,60 @@ bool loongarch_frag_align_code (int n, int max) { char *nops; + expressionS ex; symbolS *s =3D NULL; =20 - bfd_vma insn_alignment =3D 4; - bfd_vma bytes =3D (bfd_vma) 1 << n; - bfd_vma worst_case_bytes =3D bytes - insn_alignment; + /* When not relaxing, loongarch_handle_align handles code alignment. */ + if (!LARCH_opts.relax) + return false; + + bfd_vma align_bytes =3D (bfd_vma) 1 << n; + bfd_vma worst_case_bytes =3D align_bytes - 4; + bfd_vma addend =3D worst_case_bytes; + bool align_max =3D max > 0 && (bfd_vma) max < worst_case_bytes; =20 /* If we are moving to a smaller alignment than the instruction size, th= en no alignment is required. */ - if (bytes <=3D insn_alignment) + if (align_bytes <=3D 4) return true; =20 - /* When not relaxing, loongarch_handle_align handles code alignment. */ - if (!LARCH_opts.relax) - return false; - /* If max <=3D 0, ignore max. If max >=3D worst_case_bytes, max has no effect. Similar to gas/write.c relax_segment function rs_align_code case: if (fragP->fr_subtype !=3D 0 && offset > fragP->fr_subtype). */ - if (max > 0 && (bfd_vma) max < worst_case_bytes) + if (align_max) { s =3D symbol_find (now_seg->name); - worst_case_bytes =3D ALIGN_MAX_ADDEND (n, max); + addend =3D ALIGN_MAX_ADDEND (n, max); + } + + if (LARCH_opts.ignore_start_align) + { + frag_grow (worst_case_bytes); + /* Use relaxable frag for .align. + If .align at the start of section, do nothing. Section alignment can + ensure correct alignment. + If .align is not at the start of a section, reserve NOP instructions + and R_LARCH_ALIGN relocation. */ + nops =3D frag_var (rs_machine_dependent, worst_case_bytes, worst_cas= e_bytes, + rs_align_code, s, addend, NULL); } + else + { + nops =3D frag_more (worst_case_bytes); + if (align_max) + { + ex.X_add_symbol =3D s; + ex.X_op =3D O_symbol; + } + else + ex.X_op =3D O_constant; + + ex.X_add_number =3D addend; =20 - frag_grow (worst_case_bytes); - /* Use relaxable frag for .align. - If .align at the start of section, do nothing. Section alignment can - ensure correct alignment. - If .align is not at the start of a section, reserve NOP instructions - and R_LARCH_ALIGN relocation. */ - nops =3D frag_var (rs_machine_dependent, worst_case_bytes, worst_case_by= tes, - rs_align_code, s, worst_case_bytes, NULL); + fix_new_exp (frag_now, nops - frag_now->fr_literal, 0, + &ex, false, BFD_RELOC_LARCH_ALIGN); + } =20 /* Default write NOP for aligned bytes. */ loongarch_make_nops (nops, worst_case_bytes); diff --git a/gas/testsuite/gas/loongarch/relax-align-first.d b/gas/testsuit= e/gas/loongarch/relax-align-ignore-start.d similarity index 87% rename from gas/testsuite/gas/loongarch/relax-align-first.d rename to gas/testsuite/gas/loongarch/relax-align-ignore-start.d index ec0698b6995..0a67392d163 100644 --- a/gas/testsuite/gas/loongarch/relax-align-first.d +++ b/gas/testsuite/gas/loongarch/relax-align-ignore-start.d @@ -1,4 +1,4 @@ -#as: +#as: -mignore-start-align #objdump: -dr =20 .*:[ ]+file format .* diff --git a/gas/testsuite/gas/loongarch/relax-align-first.s b/gas/testsuit= e/gas/loongarch/relax-align-ignore-start.s similarity index 100% rename from gas/testsuite/gas/loongarch/relax-align-first.s rename to gas/testsuite/gas/loongarch/relax-align-ignore-start.s diff --git a/include/opcode/loongarch.h b/include/opcode/loongarch.h index 251daf13943..965a164307f 100644 --- a/include/opcode/loongarch.h +++ b/include/opcode/loongarch.h @@ -256,6 +256,7 @@ dec2 : [1-9][0-9]? =20 int relax; int thin_add_sub; + int ignore_start_align; } LARCH_opts; =20 extern size_t loongarch_insn_length (insn_t insn); diff --git a/ld/testsuite/ld-elf/dwarf.exp b/ld/testsuite/ld-elf/dwarf.exp index 6a63269562c..a4748a47c98 100644 --- a/ld/testsuite/ld-elf/dwarf.exp +++ b/ld/testsuite/ld-elf/dwarf.exp @@ -52,6 +52,9 @@ set build_tests { {"DWARF parse during linker error" "" "-fno-toplevel-reorder" {dwarf2a.c dwarf2b.c} {{error_output "dwarf2.err"}} "dwarf2.x"} +} + +set build_tests_dwarf3 { {"Handle no DWARF information" "" "-g0" {dwarf3.c} {{error_output "dwarf3.err"}} "dwarf3.x"} @@ -72,6 +75,8 @@ set run_tests { set old_CFLAGS "$CFLAGS_FOR_TARGET" append CFLAGS_FOR_TARGET " $NOSANITIZE_CFLAGS" run_cc_link_tests $build_tests +setup_xfail loongarch*-*-* +run_cc_link_tests $build_tests_dwarf3 run_ld_link_exec_tests $run_tests set CFLAGS_FOR_TARGET "$old_CFLAGS" =20 diff --git a/ld/testsuite/ld-loongarch-elf/partial-link-align-a.s b/ld/test= suite/ld-loongarch-elf/partial-link-align-a.s new file mode 100644 index 00000000000..a8b4f295ea7 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/partial-link-align-a.s @@ -0,0 +1,2 @@ +.text +la.local $t0, .text diff --git a/ld/testsuite/ld-loongarch-elf/partial-link-align-b.s b/ld/test= suite/ld-loongarch-elf/partial-link-align-b.s new file mode 100644 index 00000000000..46fa058b437 --- /dev/null +++ b/ld/testsuite/ld-loongarch-elf/partial-link-align-b.s @@ -0,0 +1,3 @@ +.text +.align 4 +ret diff --git a/ld/testsuite/ld-loongarch-elf/relax-align-first.d b/ld/testsui= te/ld-loongarch-elf/relax-align-ignore-start.d similarity index 92% rename from ld/testsuite/ld-loongarch-elf/relax-align-first.d rename to ld/testsuite/ld-loongarch-elf/relax-align-ignore-start.d index 9a4fad8ea46..939cf427b4b 100644 --- a/ld/testsuite/ld-loongarch-elf/relax-align-first.d +++ b/ld/testsuite/ld-loongarch-elf/relax-align-ignore-start.d @@ -1,3 +1,4 @@ +#as: -mignore-start-align #ld: -e0 #objdump: -d =20 diff --git a/ld/testsuite/ld-loongarch-elf/relax-align-first.s b/ld/testsui= te/ld-loongarch-elf/relax-align-ignore-start.s similarity index 100% rename from ld/testsuite/ld-loongarch-elf/relax-align-first.s rename to ld/testsuite/ld-loongarch-elf/relax-align-ignore-start.s diff --git a/ld/testsuite/ld-loongarch-elf/relax.exp b/ld/testsuite/ld-loon= garch-elf/relax.exp index 23452142cd2..890f139d428 100644 --- a/ld/testsuite/ld-loongarch-elf/relax.exp +++ b/ld/testsuite/ld-loongarch-elf/relax.exp @@ -19,10 +19,38 @@ # MA 02110-1301, USA. # =20 -if [istarget loongarch64-*-*] { - run_dump_test "relax-align-first" +proc run_partial_linking_align_test {} { + global as + global ld + global srcdir + global subdir + global runtests + + set testname "partial-link-align" + if ![runtest_file_p $runtests $testname] then { + return + } =20 + if { ![ld_assemble $as "$srcdir/$subdir/$testname-a.s" tmpdir/a.o] + || ![ld_assemble $as "$srcdir/$subdir/$testname-b.s" tmpdir/b.o] + || ![ld_link $ld tmpdir/$testname.os "tmpdir/a.o tmpdir/b.o -r"] + || ![ld_link $ld tmpdir/$testname "tmpdir/$testname.os -e0 -Ttext 0x= 1000"] } { + fail $testname + } else { + set objdump_output [run_host_cmd "objdump" "-d tmpdir/$testname"] + if { [ regexp ".*1010:\\s*4c000020\\s*jirl.*" $objdump_output ] } { + pass $testname + } else { + fail $testname + } + } +} + +if [istarget loongarch64-*-*] { if [isbuild loongarch64-*-*] { + run_dump_test "relax-align-ignore-start" + run_partial_linking_align_test + set testname "loongarch relax .exe build" set pre_builds [list \ [list \ diff --git a/ld/testsuite/ld-undefined/undefined.exp b/ld/testsuite/ld-unde= fined/undefined.exp index 6f7ea003c96..2cc67fef16e 100644 --- a/ld/testsuite/ld-undefined/undefined.exp +++ b/ld/testsuite/ld-undefined/undefined.exp @@ -74,6 +74,7 @@ if { ![check_compiler_available] } { # in a literal pool outside the function, so that both the # "undefined function" and "undefined line" tests fail. setup_xfail xtensa*-*-linux* + setup_xfail loongarch*-*-* =20 set mf "tmpdir/undefined.o* in function `function':" checkund $mf $testfn @@ -154,6 +155,7 @@ if { ![check_compiler_available] } { =20 # eBPF doesn't support dwarf yet. setup_xfail bpf-*-* + setup_xfail loongarch*-*-* =20 checkund $ml $testline }