I'm using clang for OS X development.
The data model used for x86_64 makes a distinction between near and far data...
static int x;
is assumed to be reachable within 2GB rip-relative addressing and generates code accordingly.
extern int x;
is not assumed to be so reachable. It generates slightly longer code that may need to use the GOT. The linker (either static or dynamic) has the option of optimizing and replacing a mov instruction from GOT with a lea instruction if it finds that the data element is accessible with a 2GB rip-relative address.
However, even with a mov->lea replacement, a longer instruction sequence is usually needed (such as 2 instructions where one would suffice if the data is known to be near.) + the compiler is forced to allocate a register for holding the object's address - possibly impacting other optimizations.
The programmer may know that although the data is not static - it is still reachable with a near address. For instance, if it's in another source file that is statically linked to a resulting object file with size < 2GB.
Is there a way to tell clang to assume that a piece of data is near, such as
extern __attribute((assume_near))__ int x;
and generate code accordingly (i.e. not via GOT access).
Thanks in Advance.