Skip to content

Commit 08e0064

Browse files
committed
RISC-V: Group relaxation features
It does not only deduplicate multiple relaxation feature detection, but enable more complex features so that querying the feature availability will get too slow if we perform it per-relocation (not per-section). Even if that wouldn't happen any time soon, it will improve the maintainability around the linker relaxation code. bfd/ChangeLog: * elfnn-riscv.c (RISCV_RELAX_RVC, RISCV_RELAX_GP): New. (relax_func_t): Add new relax_features argument. (_bfd_riscv_relax_call): Likewise. Move feature detection to _bfd_riscv_relax_section. Use bool for simplicity. (_bfd_riscv_relax_lui): Likewise. Move feature detection to _bfd_riscv_relax_section. (_bfd_riscv_relax_tls_le): Likewise but features are not used. (_bfd_riscv_relax_align): Likewise but features are not used. (_bfd_riscv_relax_pc): Likewise. Move feature detection to _bfd_riscv_relax_section. (_bfd_riscv_relax_section): Detect relaxation-related features and pass the flags to each relaxation function.
1 parent 5e2c9ce commit 08e0064

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

bfd/elfnn-riscv.c

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4085,6 +4085,10 @@ _bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
40854085
return false;
40864086
}
40874087

4088+
/* Enabled relaxation features to use. */
4089+
#define RISCV_RELAX_RVC 0x01U
4090+
#define RISCV_RELAX_GP 0x02U
4091+
40884092
/* A second format for recording PC-relative hi relocations. This stores the
40894093
information required to relax them to GP-relative addresses. */
40904094

@@ -4471,7 +4475,8 @@ typedef bool (*relax_func_t) (bfd *, asection *, asection *,
44714475
Elf_Internal_Rela *,
44724476
bfd_vma, bfd_vma, bfd_vma, bool *,
44734477
riscv_pcgp_relocs *,
4474-
bool undefined_weak);
4478+
bool undefined_weak,
4479+
unsigned relax_features);
44754480

44764481
/* Relax AUIPC + JALR into JAL. */
44774482

@@ -4484,13 +4489,15 @@ _bfd_riscv_relax_call (bfd *abfd, asection *sec, asection *sym_sec,
44844489
bfd_vma reserve_size ATTRIBUTE_UNUSED,
44854490
bool *again,
44864491
riscv_pcgp_relocs *pcgp_relocs,
4487-
bool undefined_weak ATTRIBUTE_UNUSED)
4492+
bool undefined_weak ATTRIBUTE_UNUSED,
4493+
unsigned relax_features)
44884494
{
44894495
bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
44904496
bfd_vma foff = symval - (sec_addr (sec) + rel->r_offset);
44914497
bool near_zero = (symval + RISCV_IMM_REACH / 2) < RISCV_IMM_REACH;
4498+
bool rvc = (relax_features & RISCV_RELAX_RVC) != 0;
44924499
bfd_vma auipc, jalr;
4493-
int rd, r_type, len = 4, rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
4500+
int rd, r_type, len = 4;
44944501

44954502
/* If the call crosses section boundaries, an alignment directive could
44964503
cause the PC-relative offset to later increase, so we need to add in the
@@ -4590,15 +4597,16 @@ _bfd_riscv_relax_lui (bfd *abfd,
45904597
bfd_vma reserve_size,
45914598
bool *again,
45924599
riscv_pcgp_relocs *pcgp_relocs,
4593-
bool undefined_weak)
4600+
bool undefined_weak,
4601+
unsigned relax_features)
45944602
{
45954603
struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (link_info);
45964604
bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
45974605
/* Can relax to x0 even when gp relaxation is disabled. */
4598-
bfd_vma gp = htab->params->relax_gp
4606+
bfd_vma gp = (relax_features & RISCV_RELAX_GP) != 0
45994607
? riscv_global_pointer_value (link_info)
46004608
: 0;
4601-
int use_rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
4609+
bool use_rvc = (relax_features & RISCV_RELAX_RVC) != 0;
46024610

46034611
BFD_ASSERT (rel->r_offset + 4 <= sec->size);
46044612

@@ -4703,7 +4711,8 @@ _bfd_riscv_relax_tls_le (bfd *abfd,
47034711
bfd_vma reserve_size ATTRIBUTE_UNUSED,
47044712
bool *again,
47054713
riscv_pcgp_relocs *pcgp_relocs,
4706-
bool undefined_weak ATTRIBUTE_UNUSED)
4714+
bool undefined_weak ATTRIBUTE_UNUSED,
4715+
unsigned relax_features ATTRIBUTE_UNUSED)
47074716
{
47084717
/* See if this symbol is in range of tp. */
47094718
if (RISCV_CONST_HIGH_PART (tpoff (link_info, symval)) != 0)
@@ -4745,7 +4754,8 @@ _bfd_riscv_relax_align (bfd *abfd, asection *sec,
47454754
bfd_vma reserve_size ATTRIBUTE_UNUSED,
47464755
bool *again ATTRIBUTE_UNUSED,
47474756
riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED,
4748-
bool undefined_weak ATTRIBUTE_UNUSED)
4757+
bool undefined_weak ATTRIBUTE_UNUSED,
4758+
unsigned relax_features ATTRIBUTE_UNUSED)
47494759
{
47504760
bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
47514761
bfd_vma alignment = 1, pos;
@@ -4805,11 +4815,12 @@ _bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED,
48054815
bfd_vma reserve_size,
48064816
bool *again,
48074817
riscv_pcgp_relocs *pcgp_relocs,
4808-
bool undefined_weak)
4818+
bool undefined_weak,
4819+
unsigned relax_features)
48094820
{
48104821
struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (link_info);
48114822
/* Can relax to x0 even when gp relaxation is disabled. */
4812-
bfd_vma gp = htab->params->relax_gp
4823+
bfd_vma gp = (relax_features & RISCV_RELAX_GP) != 0
48134824
? riscv_global_pointer_value (link_info)
48144825
: 0;
48154826

@@ -4965,6 +4976,13 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec,
49654976
bfd_vma max_alignment, reserve_size = 0;
49664977
riscv_pcgp_relocs pcgp_relocs;
49674978
static asection *first_section = NULL;
4979+
unsigned relax_features = 0;
4980+
4981+
/* Detect available/enabled relaxation features. */
4982+
if (elf_elfheader (abfd)->e_flags & EF_RISCV_RVC)
4983+
relax_features |= RISCV_RELAX_RVC;
4984+
if (htab->params->relax_gp)
4985+
relax_features |= RISCV_RELAX_GP;
49684986

49694987
*again = false;
49704988

@@ -5208,7 +5226,7 @@ _bfd_riscv_relax_section (bfd *abfd, asection *sec,
52085226

52095227
if (!relax_func (abfd, sec, sym_sec, info, rel, symval,
52105228
max_alignment, reserve_size, again,
5211-
&pcgp_relocs, undefined_weak))
5229+
&pcgp_relocs, undefined_weak, relax_features))
52125230
goto fail;
52135231
}
52145232

0 commit comments

Comments
 (0)