Trying to link
attribute((weak, visibility("hidden"))) extern int foo;
int *main(void) {
return &foo;
}
on OS X fails with
final section layout:
__TEXT/__text addr=0x100000FAB, size=0x0000000D, fileOffset=0x00000FAB, type=1 __TEXT/__unwind_info addr=0x100000FB8, size=0x00000048, fileOffset=0x00000FB8, type=22
ld: 32-bit RIP relative reference out of range (-4294971318 max is +/-2GB): from _main (0x100000FAB) to _foo@0x00001000 (0x00000000) in '_main' from test.o for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The problem being that 0 cannot be computed as a fixed difference from %rip. Exactly the same issue exists on ELF and we can use the same solution.