I would like to take and use the address of _GLOBAL_OFFSET_TABLE_. My expectation is that the address of this symbol does not change over the course of a program.
However, the value of its address seems to depend on which function takes it:
#include <iostream>
#include <cassert>
extern void* _GLOBAL_OFFSET_TABLE_;
void* foo()
{
return (void*)&_GLOBAL_OFFSET_TABLE_;
}
void* bar()
{
return (void*)&_GLOBAL_OFFSET_TABLE_;
}
void* baz()
{
return (void*)&_GLOBAL_OFFSET_TABLE_;
}
int main()
{
std::cout << "foo(): " << foo() << std::endl;
std::cout << "bar(): " << bar() << std::endl;
std::cout << "baz(): " << baz() << std::endl;
assert(foo() == bar());
assert(foo() == baz());
assert(bar() == baz());
return 0;
}
Program output:
$ clang global_offset_table_repro.cpp -lstdc++
$ ./a.out
foo(): 0x20076c
bar(): 0x20075c
baz(): 0x20074c
a.out: global_offset_table_repro.cpp:27: int main(): Assertion `foo() == bar()' failed.
Aborted (core dumped)
Compiling with -fPIC produces a consistent result:
$ clang global_offset_table_repro.cpp -lstdc++ -fPIC
jhoberock@jhoberock-dt:~/dev/git/oz$ ./a.out
foo(): 0x601000
bar(): 0x601000
baz(): 0x601000
As does compiling with g++:
$ g++ global_offset_table_repro.cpp -lstdc++
$ ./a.out
foo(): 0x555eab725f80
bar(): 0x555eab725f80
baz(): 0x555eab725f80
Compiler details:
$ clang --version ; echo ; g++ --version
clang version 4.0.1-6 (tags/RELEASE_401/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
g++ (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Is the clang behavior expected?