https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93062

            Bug ID: 93062
           Summary: Failed to generate indirect branch for long branches
                    on risc-v
           Product: gcc
           Version: 9.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adrien.sf.wu at gmail dot com
  Target Milestone: ---

When compiling for RISC-V targets, if the program contains a branch across 2^19
bytes of code or more (for example, an `if` block containing 2^18
instructions), the compiler does not generate an indirect branch. This results
in an immediate overflow in JAL instruction and makes the file unlinkable.

For example,

$ riscv64-linux-gnu-gcc -O0 -c test.c -o test.o

generates the following code for the `if` branch:

$ objdump -d test.o
  ...
  2e: 00e7d463  bge  a5,a4,36 <a+0x36>
  32: 8551606f  j    fffffffffff16886 <.L2+0xffffffffffe00000>
  ...

If trying to link this file, the following error is produced:

$ riscv64-linux-gnu-gcc test.o main.o
test1.o: in function `a':
test1.c:(.text+0x32): relocation truncated to fit: R_RISCV_JAL against `.L2'
collect2: error: ld returned 1 exit status

Source code of test.c:

int a(int c) {
  if (c < 12345) {
#define A \
    { int d = c % 12345; \
      int e = c ^ d * 12345; \
      int f = c * d % e % 12345; \
      c = f; }
#define B A A A A A A
#define C B B B B B B
#define D C C C C C C
#define E D D D D D D
    E E E E E
  }
  return c;
}

Reply via email to