为什么动态链接时extern变量不使用GOT?
我有下面两段代码,我将文件 sum.c 编译为动态库(.so),然后将其与 main.c 一起编译。我使用的命令行如下:
gcc sum.c -shared -fPIC -o libsum.so
gcc -o main main.c -lsum -L.
代码:
// main.c
#define LEN 2
extern int sharedArr[LEN];
extern int sum(int *arr, int n);
int* array = sharedArr;
int main(void) {
int val = sum(array, LEN);
return val;
}
// sum.c
#define LEN 2
int sharedArr[LEN] = { 1, 2 };
int sum(int *arr, int n) {
int i, s = 0;
for (i = 0; i < n; i++) {
s += arr[i];
}
return s;
}
我尝试查看变量“sharedArr”的重定位策略,但它显示为 R_X86_64_64,这不是利用 GOT 的方式,而是直接访问 .data部分,为什么?
I have below two pieces of code, and I compiled the file sum.c as a dynamic lib (.so), and then compiled it together with main.c. And the command lines I used are listed below:
gcc sum.c -shared -fPIC -o libsum.so
gcc -o main main.c -lsum -L.
The code:
// main.c
#define LEN 2
extern int sharedArr[LEN];
extern int sum(int *arr, int n);
int* array = sharedArr;
int main(void) {
int val = sum(array, LEN);
return val;
}
// sum.c
#define LEN 2
int sharedArr[LEN] = { 1, 2 };
int sum(int *arr, int n) {
int i, s = 0;
for (i = 0; i < n; i++) {
s += arr[i];
}
return s;
}
And I tried to see the relocation strategy of the variable "sharedArr", but it showed as R_X86_64_64 which was not the way of leveraging GOT but instead a direct access to the .data section, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论