[clang] x86: Cannot get the correct address to _GLOBAL_OFFSET_TABLE_ from C code when using integrated assembler.
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
On 32-bit x86 I'm using the following C code to return pointer `_GLOBAL_OFFSET_TABLE_`
```
void *get_got_ptr(void) {
extern __attribute__((visibility("hidden"))) char _GLOBAL_OFFSET_TABLE_[];
return _GLOBAL_OFFSET_TABLE_;
}
```
When compiling this, clang seems to want to generate the correct instructions ([godbolt link](https://godbolt.org/z/x74jKsTMv)):
```
get_got_ptr:
calll .L0$pb
.L0$pb:
popl %eax
.Ltmp0:
addl $_GLOBAL_OFFSET_TABLE_+(.Ltmp0-.L0$pb), %eax
leal _GLOBAL_OFFSET_TABLE_@GOTOFF(%eax), %eax
retl
```
The `_GLOBAL_OFFSET_TABLE_@GOTOFF` symbol should resolve to 0 by the linker.
However, the object file produced when using the integrated assembler has the wrong relocation type for `_GLOBAL_OFFSET_TABLE_@GOTOFF` ([godbolt link](https://godbolt.org/z/hq6M31o5z)):
```
get_got_ptr:
call 5
pop %eax
add $0x3,%eax
R_386_GOTPC _GLOBAL_OFFSET_TABLE_
lea 0x2(%eax),%eax
R_386_GOTPC _GLOBAL_OFFSET_TABLE_
ret
```
The `lea` instruction should have relocation type `R_386_GOTOFF`, not `R_386_GOTPC`. The output is correct when using `-fno-integrated-as` ([goldbolt link](https://godbolt.org/z/xc19PKrWY)).
Contributor guide
Assessment
This issue has not been assessed yet.