From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7814) id 0F059385840F; Fri, 24 Feb 2023 06:11:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0F059385840F Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Fangrui Song To: bfd-cvs@sourceware.org Subject: [binutils-gdb] RISC-V: Add --[no-]relax-gp to ld X-Act-Checkin: binutils-gdb X-Git-Author: Fangrui Song X-Git-Refname: refs/heads/master X-Git-Oldrev: 6777dece58127236db900215857f9070ad63e0bf X-Git-Newrev: 50980ba351856dff75bb0743bfca62f4c3ab19ff Message-Id: <20230224061117.0F059385840F@sourceware.org> Date: Fri, 24 Feb 2023 06:11:17 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 24 Feb 2023 06:11:17 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D50980ba35185= 6dff75bb0743bfca62f4c3ab19ff commit 50980ba351856dff75bb0743bfca62f4c3ab19ff Author: Fangrui Song Date: Thu Feb 23 22:11:14 2023 -0800 RISC-V: Add --[no-]relax-gp to ld =20 --relax enables all relaxations. --no-relax-gp disables GP relaxation = to allow measuring its effect. =20 The option can test effectiveness of GP relaxation and support some ABI variants that use GP for other purposes. =20 Link: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/298 =20 bfd/ * elfnn-riscv.c (struct riscv_elf_link_hash_table): Add params. (riscv_elfNN_set_options): New. (riscv_info_to_howto_rela): Check relax_gp. (_bfd_riscv_relax_section): Likewise. * elfxx-riscv.h (struct riscv_elf_params): New. (riscv_elf32_set_options): New. (riscv_elf64_set_options): New. ld/ * emultempl/riscvelf.em: Add option parsing. * testsuite/ld-riscv-elf/code-model-relax-medlow-01-norelaxgp.d: Ne= w. * testsuite/ld-riscv-elf/pcgp-relax-01-norelaxgp.d: New. * testsuite/ld-riscv-elf/pcgp-relax-02.d: Test --relax --relax-gp c= an be used together. Diff: --- bfd/ChangeLog | 10 ++++++ bfd/elfnn-riscv.c | 16 ++++++++-- bfd/elfxx-riscv.h | 11 +++++++ ld/ChangeLog | 8 +++++ ld/emultempl/riscvelf.em | 36 ++++++++++++++++++= ++++ .../code-model-relax-medlow-01-norelaxgp.d | 4 +++ ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 2 ++ .../ld-riscv-elf/pcgp-relax-01-norelaxgp.d | 18 +++++++++++ ld/testsuite/ld-riscv-elf/pcgp-relax-02.d | 2 +- 9 files changed, 104 insertions(+), 3 deletions(-) diff --git a/bfd/ChangeLog b/bfd/ChangeLog index b7c03c11678..50e1e17ef4d 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,13 @@ +2023-02-23 Fangrui Song + + * elfnn-riscv.c (struct riscv_elf_link_hash_table): Add params. + (riscv_elfNN_set_options): New. + (riscv_info_to_howto_rela): Check relax_gp. + (_bfd_riscv_relax_section): Likewise. + * elfxx-riscv.h (struct riscv_elf_params): New. + (riscv_elf32_set_options): New. + (riscv_elf64_set_options): New. + 2023-02-23 Nick Clifton Alan Modra =20 diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 355ddb22566..1200e6b11b5 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -118,6 +118,9 @@ struct riscv_elf_link_hash_table { struct elf_link_hash_table elf; =20 + /* Various options and other info passed from the linker. */ + struct riscv_elf_params *params; + /* Short-cuts to get to dynamic linker sections. */ asection *sdyntdata; =20 @@ -157,6 +160,13 @@ struct riscv_elf_link_hash_table && elf_hash_table_id (elf_hash_table (p)) =3D=3D RISCV_ELF_DATA) \ ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL) =20 +void +riscv_elfNN_set_options (struct bfd_link_info *link_info, + struct riscv_elf_params *params) +{ + riscv_elf_hash_table (link_info)->params =3D params; +} + static bool riscv_info_to_howto_rela (bfd *abfd, arelent *cache_ptr, @@ -4389,7 +4399,9 @@ _bfd_riscv_relax_lui (bfd *abfd, bool undefined_weak) { bfd_byte *contents =3D elf_section_data (sec)->this_hdr.contents; - bfd_vma gp =3D riscv_global_pointer_value (link_info); + bfd_vma gp =3D riscv_elf_hash_table (link_info)->params->relax_gp + ? riscv_global_pointer_value (link_info) + : 0; int use_rvc =3D elf_elfheader (abfd)->e_flags & EF_RISCV_RVC; =20 BFD_ASSERT (rel->r_offset + 4 <=3D sec->size); @@ -4834,7 +4846,7 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec, || type =3D=3D R_RISCV_TPREL_LO12_I || type =3D=3D R_RISCV_TPREL_LO12_S) relax_func =3D _bfd_riscv_relax_tls_le; - else if (!bfd_link_pic (info) + else if (!bfd_link_pic (info) && htab->params->relax_gp && (type =3D=3D R_RISCV_PCREL_HI20 || type =3D=3D R_RISCV_PCREL_LO12_I || type =3D=3D R_RISCV_PCREL_LO12_S)) diff --git a/bfd/elfxx-riscv.h b/bfd/elfxx-riscv.h index 5e1bdae9b2a..abcb409bd78 100644 --- a/bfd/elfxx-riscv.h +++ b/bfd/elfxx-riscv.h @@ -27,6 +27,17 @@ =20 #define RISCV_UNKNOWN_VERSION -1 =20 +struct riscv_elf_params +{ + /* Whether to relax code sequences to GP-relative addressing. */ + bool relax_gp; +}; + +extern void riscv_elf32_set_options (struct bfd_link_info *, + struct riscv_elf_params *); +extern void riscv_elf64_set_options (struct bfd_link_info *, + struct riscv_elf_params *); + extern reloc_howto_type * riscv_reloc_name_lookup (bfd *, const char *); =20 diff --git a/ld/ChangeLog b/ld/ChangeLog index de9ed2753bc..9f4ad7cbf25 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,11 @@ +2023-02-23 Fangrui Song + + * emultempl/riscvelf.em: Add option parsing. + * testsuite/ld-riscv-elf/code-model-relax-medlow-01-norelaxgp.d: New. + * testsuite/ld-riscv-elf/pcgp-relax-01-norelaxgp.d: New. + * testsuite/ld-riscv-elf/pcgp-relax-02.d: Test --relax --relax-gp can be + used together. + 2023-02-20 Nick Clifton =20 PR 30004 diff --git a/ld/emultempl/riscvelf.em b/ld/emultempl/riscvelf.em index b12d15065c4..bb6298d3e8d 100644 --- a/ld/emultempl/riscvelf.em +++ b/ld/emultempl/riscvelf.em @@ -25,6 +25,40 @@ fragment <: +.*:[ ]+[0-9a-f]+[ ]+addi[ ]+a0,a0,[0-9]+ +.*:[ ]+[0-9a-f]+[ ]+jal[ ]+ra,[0-9a-f]+ <_start> +.*:[ ]+[0-9a-f]+[ ]+auipc[ ]+a1,0x[0-9a-f]+ +.*:[ ]+[0-9a-f]+[ ]+addi[ ]+a1,a1,[0-9]+ # [0-9a-f]+ +.*:[ ]+[0-9a-f]+[ ]+lui[ ]+a2,0x[0-9a-f]+ +.*:[ ]+[0-9a-f]+[ ]+addi[ ]+a2,a2,[0-9]+ # [0-9a-f]+ +.*:[ ]+[0-9a-f]+[ ]+addi[ ]+a3,tp,0 # 0 +.*:[ ]+[0-9a-f]+[ ]+auipc[ ]+a0,0x[0-9a-f]+ diff --git a/ld/testsuite/ld-riscv-elf/pcgp-relax-02.d b/ld/testsuite/ld-ri= scv-elf/pcgp-relax-02.d index 1248132ab42..055f03e57f2 100644 --- a/ld/testsuite/ld-riscv-elf/pcgp-relax-02.d +++ b/ld/testsuite/ld-riscv-elf/pcgp-relax-02.d @@ -1,6 +1,6 @@ #source: pcgp-relax-02.s #as: -#ld: --relax +#ld: --relax --relax-gp #objdump: -d =20 .*:[ ]+file format .*