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

            Bug ID: 32718
           Summary: RISCV - relaxation seem to break static-PIE addressing
           Product: binutils
           Version: 2.44
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: ld
          Assignee: unassigned at sourceware dot org
          Reporter: tprism at gmail dot com
  Target Milestone: ---

Consider the code:
--------------------
    volatile unsigned temp;
    int main(void) {
        temp = 0x12345678;
        return 0;
    }
---------------------

When compiled with 
> riscv64-unknown-elf-gcc -nostdlib -nostartfiles -O3 -fpie -static-pie -c 
> main.c

it is generating the following (disassembled) object code:
-----------------------------
0000000000000000 <main>:
   0:   123457b7                lui     a5,0x12345
   4:   67878793                addi    a5,a5,1656 # 12345678 <main+0x12345678>
   8:   00000717                auipc   a4,0x0
                        8: R_RISCV_PCREL_HI20   .LANCHOR0
                        8: R_RISCV_RELAX        *ABS*
   c:   00f72023                sw      a5,0(a4) # 8 <main+0x8>
                        c: R_RISCV_PCREL_LO12_S .L0 
                        c: R_RISCV_RELAX        *ABS*
  10:   4501                    li      a0,0
  12:   8082                    ret
--------------------------------
Here the access to `temp` is correctly PC-relative and is using relaxation

However, when compiled and linked with the following script:
----------------------
ENTRY(main)

SECTIONS {
  .text : {
    *(.text .text.*)
  }
  .rodata : ALIGN(16) {
    *(.srodata .srodata.*)
    *(.rodata .rodata.*)
  }

  .data ALIGN(16): {
    _data_start = .;
    *(.got .got.*)
    *(.sdata .sdata.*)
    *(.data .data.*)
  }
  .bss ALIGN(16): {
    _bss_start = .;
    *(.sbss .sbss.*)
    *(.bss .bss.*)
    _bss_end = .;
  }
}
--------------------
> riscv64-unknown-elf-gcc -nostdlib -nostartfiles -O3 -fpie -static-pie -T 
> link.ld main.c -o main.elf

the disassembled elf looks like this:

0000000000000000 <main>:
   0:   123457b7                lui     a5,0x12345
   4:   67878793                addi    a5,a5,1656 # 12345678
<_bss_end+0x12345664>
   8:   00f02823                sw      a5,16(zero) # 10 <_data_start>
   c:   4501                    li      a0,0
   e:   8082                    ret

As we can see the access to `temp` is not PC-relative anymore, but for some
reason `zero`-relative.

It seems like this behavior is depending on the "absolute" address of `temp`.
Whenever it is less than 2048, the demonstrated behavior is observed. When it
is greater than that (by aligning `.bss` to higher boundary), correct
PC-relative addressing is generated. `--no-relax` is "fixing" this issue.

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

Reply via email to