3

我想获取和使用的地址_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预期?

4

0 回答 0