<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/217371>217371</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[Mips][IAS] PIC la incorrectly treats an defined global symbol as local
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
FlyGoat
</td>
</tr>
</table>
<pre>
The IAS classifies any symbol that is already in a section as local when expanding the PIC `la` macro. A defined symbol can still have global ELF binding, so this makes the expansion depend incorrectly on whether the symbol is defined before or after its use.
For an O32 default-visible global defined before `la`, IAS emits the local GOT sequence using `R_MIPS_GOT16` followed by `R_MIPS_LO16`. The global GOT entry already contains the complete symbol address, so adding the low part again produces an incorrect address when the symbol has a nonzero low part.
gas uses the symbol's ELF binding and emits the global GOT form.
## Reproducer
```asm
.option pic2
.data
.space 4
.globl global_symbol
global_symbol:
.word 0
.local local_symbol
local_symbol:
.word 0
.text
la $5, global_symbol
la $25, global_symbol
la $6, local_symbol
```
Assemble and inspect:
```sh
llvm-mc \
-triple=mips-unknown-linux \
-mcpu=mips32r2 \
-filetype=obj \
defined-global.s \
-o defined-global.o
llvm-readelf --symbols --relocations defined-global.o
```
### Actual
IAS treats both symbols as local because both have already been defined in `.data`:
```text
R_MIPS_GOT16 global_symbol
R_MIPS_LO16 global_symbol
R_MIPS_GOT16 global_symbol
R_MIPS_LO16 global_symbol
R_MIPS_GOT16 .data
R_MIPS_LO16 .data
```
The first two relocations correspond to the ordinary `la`, and the next two correspond to `la $25, global_symbol`.
For a global symbol, the GOT entry contains the complete address. The generated code then adds `%lo(global_symbol)` a second time, producing the wrong result when the low part is non-zero.
### Expected
For O32 without XGOT, gas emits:
```text
R_MIPS_GOT16 global_symbol
R_MIPS_CALL16 global_symbol
R_MIPS_GOT16 .data
R_MIPS_LO16 .data
```
IAS should classify symbols using their ELF binding rather than whether they have already been assigned to a section.
</p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