为什么动态链接时extern变量不使用GOT?

发布于 2025-01-10 13:57:17 字数 891 浏览 4 评论 0原文

我有下面两段代码,我将文件 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?

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文