Issue 177903
Summary [MC][Statepoint] Incorrect jump table relocations in .rodata when using gc "statepoint-example" with PIC relocation model
Labels new issue
Assignees
Reporter Cruise20
    It is a serious issue that looks like a bug in how LLVM handles Jump Table lowering in the presence of GC statepoints when Position Independent Code (PIC) is enabled.

## Description
I have encountered a potential bug in LLVM where the generated object file contains invalid relocation offsets for jump tables. This occurs when the following three conditions are met:

1. The function uses gc "statepoint-example" and contains llvm.experimental.gc.statepoint calls.
2. The function contains a switch statement with enough cases to trigger the generation of a Jump Table.
3. The code is compiled with --relocation-model=pic.

In this scenario, the R_X86_64_PC32 relocations in the .rela.rodata section (representing the jump table entries) point to offsets within the .text section that far exceed the actual size of the .text section.

This issue was discovered during a JIT project where we manually load and relocate LLVM-generated object files. The out-of-bounds offsets cause the relocation process to fail.

## Steps to Reproduce
1. Save the attached IR as `TestTableSwitch_callMethodSwitch.ll`.
2. Compile using llc: `llc --relocation-model=pic TestTableSwitch_callMethodSwitch.ll --filetype=obj -o TestTableSwitch_callMethodSwitch.o`
3. Inspect the section headers: `readelf -S TestTableSwitch_callMethodSwitch.o`.
    Observation: Note the size of the .text section (e.g., 0x406 bytes).
4. Inspect the relocations: `readelf -r TestTableSwitch_callMethodSwitch.o`

## Actual Results
The relocations for the jump table in .rela.rodata show offsets like .text + 0x103b or .text + 0x1001, even though the .text section size is only 0x406.

Example readelf -S output:
```
[Nr] Name              Type             Address Offset
     Size              EntSize          Flags  Link  Info Align
[ 2] .text             PROGBITS         0000000000000000  00000040
 0000000000000406  0000000000000000  AX       0     0 16

```

Example readelf -r output:

```
elocation section '.rela.rodata' at offset ... contains 835 entries:
  Offset          Info Type           Sym. Value    Sym. Name + Addend
...
000000000ccc 000200000002 R_X86_64_PC32     0000000000000000 .text + 103b
000000000cd8 000200000002 R_X86_64_PC32     0000000000000000 .text + 1001

```

## Expected Results

The addends for .text in the relocation table should be within the bounds of the .text section (i.e., less than 0x406).

## Environment

LLVM Version: LLVM version 22.0.0git
Target: x86_64-unknown-linux-gnu

## Additional Context

It appears that the llvm.experimental.gc.statepoint transformation or the subsequent stack map generation is interfering with the basic block offset calculations used by the jump table lowering logic under PIC. Without the statepoint attribute or when using a non-PIC relocation model, the issue does not seem to manifest.


[TestTableSwitch_callMethodSwitch.ll.txt](https://github.com/user-attachments/files/24854269/TestTableSwitch_callMethodSwitch.ll.txt)

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to