Skip to content

Error when a jump instruction jumps to a very distant location within a function #1045

@shioyadan

Description

@shioyadan

Hello,

Compiling a very large function with gcc causes an error. It appears that overflow occurs when a jump target in a function is beyond the range that can be represented by the displacement of the jump instruction.

The following is the code to reproduce this error:

// test.c
int main() {
    volatile int i = 0;
    for (int j = 0; j < 10; j++) {
        #define X10(x) x; x; x; x; x; x; x; x; x; x; 
        X10(X10(X10(X10(X10(i++)))));
    }
    return i;
}

The following error occurs.

$ ~/opt/gcc/riscv64-linux/11.1/bin/riscv64-unknown-linux-gnu-gcc test.c
/tmp/ccrQPqo1.o: in function `main':
test.c:(.text+0x14): relocation truncated to fit: R_RISCV_JAL against `.L2'
collect2: error: ld returned 1 exit status

When the compiled object file is disassembled, it appears that the jump target is broken.

0000000000000010 <.LBB2>:
    for (int j = 0; j < 10; j++) {
      10:	fe042623          	sw	zero,-20(s0)
      14:	c90e806f          	j	fffffffffffe84a4 <.LFE0+0xffffffffffdfffd4>

I have confirmed that this error occurs with various versions of gcc including riscv64/riscv32 version 11.1 (refs/tags/2021.09.21). It also occurs with the cross-compiler in apt on Ubuntu 20.04. The same problem occurred not only in C, but also in C++ and Fortran.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions