This is an archive of the discontinued LLVM Phabricator instance.

Use a got to access a hidden weak undefined on MachO
ClosedPublic

Authored by espindola on Jan 12 2018, 1:24 PM.

Details

Summary

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.

Diff Detail

Event Timeline

espindola created this revision.Jan 12 2018, 1:24 PM
rnk accepted this revision.Jan 17 2018, 10:27 AM

lgtm, makes sense.

This revision is now accepted and ready to land.Jan 17 2018, 10:27 AM