llvm / llvm/llvm-project

[Mips][IAS] Forward local PIC `jal` incorrectly uses `R_MIPS_CALL16`

Open
#217,373 5 comments 0 reactions 0 assignees View on GitHub
backend:MIPS llvm:mc miscompilation
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

MIPS IAS expands a PIC `jal` to `R_MIPS_CALL16` when its target is a forward reference that is later defined as a local symbol.

The resulting object contains `R_MIPS_CALL16` against an `STB_LOCAL` symbol, which is invalid under the MIPS ELF ABI and rejected by `ld.bfd`.

GNU `as` handles the same input by delaying the local/global decision and emits the local `R_MIPS_GOT16` + `R_MIPS_LO16` sequence.

## Reproducer

```asm
.text
.abicalls
.option pic2

.globl caller
.type caller,@function
caller:
.set noreorder
.cpload $25
jal callee
nop
.set reorder

.type callee,@function
callee:
jr $31
nop
.size callee, .-callee
```

Assemble and inspect:

```sh
llvm-mc \
-triple=mipsel-unknown-linux-gnu \
-mcpu=mips32 \
-filetype=obj \
forward-local.s \
-o forward-local.o

llvm-readelf --symbols --relocations forward-local.o
```

### Actual

IAS emits:

```text
R_MIPS_CALL16 callee
R_MIPS_JALR callee
```

while `callee` is recorded as:

```text
FUNC LOCAL DEFAULT callee
```

Linking the object with `ld.bfd` fails:

```text
CALL16 reloc at 0xc not against global symbol
```

### Expected
gas emits the local PIC sequence:

```text
R_MIPS_GOT16 .text
R_MIPS_LO16 .text
R_MIPS_JALR callee
```

corresponding to:

```asm
lw $25, %got(callee)($gp)
addiu $25, $25, %lo(callee)
jalr $25
```

IAS should produce an equivalent object.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the forward-local.s commands using llvm-mc and llvm-readelf, then compare the output with GNU as and ld.bfd. Trace the MIPS IAS handling of a forward PIC jal whose target becomes local. Done means the object uses the expected local GOT16/LO16 sequence and links successfully with ld.bfd.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.