有人知道我为什么会遇到以下错误吗:-
/tmp/cceP5axg.o: in function `.L0 ':
(.text+0xc4): relocation truncated to fit: R_RISCV_JAL against `*UND*'
collect2: error: ld returned 1 exit status
典型错误如下:
relocation R_RISCV_JAL out of range: 4734022 is not in [-1048576, 1048575];
R_RISCV_JAL 重定位可以表示偶数符号 21 位偏移量(-1MiB 到 +1MiB-2)。如果您的符号超出此限制,则会出现此错误。
使用硬编码偏移量的分支指令的奇怪结果也可能会发生此错误。我在远小于 2Mib 的程序上遇到了同样的错误。事实证明,这是因为我有几条看起来像
bne rd, rs, offset
的指令,但偏移量是像0x8这样的数字文字。
解决方案是删除文字偏移量并将其替换为代码中的标签,使其看起来像
bne x7, x9, branch_to_here
[code to skip]
branch_to_here:
more code ...
而不是
bne x7, x9, 0x8
[code to skip]
more code ...
当我对每个分支指令执行此操作时,错误就消失了。很抱歉迟了 10 个月才回答这个问题,但我希望它对您有所帮助,匿名读者。
由于我搜索了很多资源来解决这个问题,我认为我的尝试可能会帮助其他人。 有 2 个原因可能会触发此问题:
bne ra, ra, <odd offset>
bne ra, ra, 0x80003000
我尝试解决:
label:
addi x0, x0, 0x0
addi x0, x0, 0x0
bne ra, ra, label + 6 // Jump to an address that relates to a label
// This can generate Instruction Address Misaligned exception
sub_label:
addi x0, x0, 0x0
beq ra, ra, sub_label // Jump to a label directly
addi x0, x0, 0x0
nop
一种方法是尝试使用优化标志进行构建(例如,https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111420#c3中建议的-O2或-O3)
我已经用 binutils 打开了一个错误来跟踪此问题:https://sourceware.org/bugzilla/show_bug.cgi?id=30855