为什么_GLOBAL_OFFSET_TABLE_的地址取决于采用哪个函数?

时间:2019-01-09 21:10:18

标签: c++ linker position-independent-code

我想使用_GLOBAL_OFFSET_TABLE_的地址。我的期望是该符号的地址在程序执行过程中不会改变。

但是,其地址的值似乎取决于使用它的函数:

#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;
}

程序输出:

$ 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)

使用-fPIC进行编译会产生一致的结果:

$ clang global_offset_table_repro.cpp -lstdc++ -fPIC
jhoberock@jhoberock-dt:~/dev/git/oz$ ./a.out 
foo(): 0x601000
bar(): 0x601000
baz(): 0x601000

g++进行编译一样:

$ g++ global_offset_table_repro.cpp -lstdc++
$ ./a.out 
foo(): 0x555eab725f80
bar(): 0x555eab725f80
baz(): 0x555eab725f80

编译器详细信息:

$ 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.

clang的行为是否正常?

0 个答案:

没有答案