https://sourceware.org/bugzilla/show_bug.cgi?id=35029

            Bug ID: 35029
           Summary: [RISC-V] ld performs illegal R_RISCV_RVC_JUMP
                    relaxation for cross-section calls
           Product: binutils
           Version: 2.47
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: ld
          Assignee: unassigned at sourceware dot org
          Reporter: leonid.ternopol at cloudbear dot ru
  Target Milestone: ---

ld for RISC-V target can perform illegal R_RISCV_RVC_JUMP relaxation for
cross-section calls when sections are bound to different memory regions.

ld version: GNU ld (GNU Binutils) 2.47.20260726 (commit
6ce87bbc521cf46eaee9a1f7ef61cee2cdfb3e32)
target: riscv64-unknown-elf
host: x86_64-pc-linux-gnu

Reproducer:

// combined.c
int fun_c(void);
int fun_b(void);

__attribute__((__section__(".sect0.a")))
int calls() {
    int a = fun_c();
    a += fun_c();
    return a;
}

__attribute__((__section__(".sect0.b")))
int _start() {
    return calls() + fun_b();
}

__attribute__((__section__(".sect0.c")))
int fun_c() {
    return 1;
}

__attribute__((__section__(".sect1.b")))
int fun_b() {
    return 1;
}

// end combined.c //

// link.ld
OUTPUT_ARCH( "riscv" )
ENTRY(_start)

MEMORY
{
  REGION_A (rx) : ORIGIN = 0x40000000, LENGTH = 2K
  REGION_B (rx) : ORIGIN = 0x40000840, LENGTH = 2K
}

SECTIONS
{
  .sect0 : ALIGN(8)
  {
    *(.sect0.a)
    *(.sect0.b)
    *(.sect0.c)
  } >REGION_A AT>REGION_A = 0x00

  .sect1 : ALIGN(8)
  {
    *(.sect1.*)
  } >REGION_B AT>REGION_B =  0x00
}
// end link.ld //

Invocation:
riscv64-unknown-elf-gcc -nostdlib -nostartfiles -T link.ld combined.c
-march=rv32gc -mabi=ilp32
Or effectively:
riscv64-unknown-elf-gcc combined.c -march=rv32gc -mabi=ilp32 -c -o combined.o
riscv64-unknown-elf-ld -melf32lriscv -T link.ld combined.o

Expected behavior: successful ld invocation

Actual output:
combined.o: in function `_start':
combined.c:(.sect0.b+0xe): relocation truncated to fit: R_RISCV_RVC_JUMP
against symbol `fun_b' defined in .sect1.b section in combined.o

I used cross-gcc 16.1.0 from
https://github.com/riscv-collab/riscv-gnu-toolchain .

Explanation of the reproducer:
It places calls, _start and fun_c in one section and fun_b into another.
Addresses are specially chosen to ensure the distance between fun_b() call in
_start and fun_b definition is a little less than 2KiB before relaxations.
After relaxation of fun_c() calls in calls function, the beginning of _start
shifts a bit and the distance between fun_b() call and definition becomes a
little more than 2KiB.

This way, before relaxations in calls() the R_RISCV_RVC_JUMP relaxation for
fun_b() is legal, but then it becomes illegal.

There was a very similar problem with section alignment
https://sourceware.org/bugzilla/show_bug.cgi?id=25181 (already fixed in commit
c6261a00c3e70dd8e508062ea43a1bcb6d547621).

I dug a bit into the problem and found out the following (using gdb):
R_RISCV_RVC_JUMP is handled by _bfd_riscv_relax_call (elfnn-riscv.c), which
doesn't actually delete bytes freed by relaxation, but only marks them with
R_RISCV_DELETE relocation. After all relaxations in input section are
performed, riscv_relax_resolve_delete_relocs deletes marked bytes and then
size_input_section (ldlang.c) adjusts size and offset of the section. Then,
linker moves to the next input section and does the same steps.

In the reproducer it results in the following order of events:
1. Code in calls section is relaxed
2. calls section is shrunk
3. Code in _start section is relaxed (it is still on the same position as if
there were no
  relaxations in calls())
4. _start section is shrunk and shifted to lower address (making already
relaxed c.jal invalid)

In this case, the problem can be fixed by performing section relocations both
before and after relaxations. But it probably won't help in general as one can
craft similar program with the calls and _start functions joined. In that case
relaxation of fun_b() call would probably be already invalid (modulo bytes
marked to deletion) in the moment of its processing.

To fix the problem, we can probably prohibit R_RISCV_RVC_JUMP relaxation for
cross-section calls when the sections are in different memory regions. Though
I'm not yet experienced enough with the binutils codebase to prepare such
patch.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Reply via email to