On 7/22/25 3:58 AM, Zdenek Sojka via Gcc-help wrote:
Hello,
I am facing an issue during linking when building code for riscv64 with gcc-15:
$ cat al.c
int a[1];
__UINTPTR_TYPE__ foo(void)
{
return (__UINTPTR_TYPE__)a + 0x7fffffff;
}
int main() {}
$ riscv64-unknown-linux-gnu-gcc-15 al.c -O -mcmodel=medany -save-temps
a-al.o: in function `foo':
al.c:(.text+0x0): relocation truncated to fit: R_RISCV_PCREL_HI20 against symbol `a' defined in .sbss section in a-al.o
collect2: error: ld returned 1 exit status
this seems to be caused by relocation:
$ objdump -r a-al.o
a-al.o: file format elf64-littleriscv
RELOCATION RECORDS FOR [.text]:
OFFSET TYPE VALUE
0000000000000000 R_RISCV_PCREL_HI20 a+0x000000007fffffff
0000000000000000 R_RISCV_RELAX *ABS*+0x000000007fffffff
0000000000000004 R_RISCV_PCREL_LO12_I .L0
0000000000000004 R_RISCV_RELAX *ABS*
...
Is it an issue in gcc, binutils - or in the code?
It's not 100% clear. I could make a case that GCC shouldn't have
generated that code. But I could also make a case that given the use of
the lla pseudo-op that gas should have managed to handle it better.
You can also make the case that the code is invalid. You've created an
invalid pointer.
Jeff