【发布时间】:2021-10-20 06:00:17
【问题描述】:
我使用 C 编写了以下 2 个程序:
第一:
int foo(int x)
{
return 1;
}
int main()
{
return foo(4);
}
第二:
static int foo(int x)
{
return 1;
}
int main()
{
return foo(4);
}
然后我跑了:
gcc -c my_file.c
对于我看到的第一个文件(不是完整的输出):
000000000000000e <main>:
e: 55 push %rbp
f: 48 89 e5 mov %rsp,%rbp
12: bf 04 00 00 00 mov $0x4,%edi
17: e8 00 00 00 00 callq 1c <main+0xe>
1c: 5d pop %rbp
1d: c3 retq
第二个:
000000000000000e <main>:
e: 55 push %rbp
f: 48 89 e5 mov %rsp,%rbp
12: bf 04 00 00 00 mov $0x4,%edi
17: e8 e4 ff ff ff callq 0 <foo>
1c: 5d pop %rbp
1d: c3 retq
我的问题是,为什么在第一个文件中,当函数在当前文件中定义(而不仅仅是声明)时我们需要重定位?这对我来说听起来太奇怪了。
【问题讨论】:
-
“重新分配”为特定用途保留或分配新内存。您正在寻找的术语是“搬迁”;在各种链接和加载步骤中,代码和数据会重新定位到内存中。
-
@n.1.8e9-where's-my-sharem。我在ubuntu上试过了
-
告诉我们GCC的版本很重要。
-
我在一个重复的问题中得到了很好的答复:stackoverflow.com/a/69043406/7084
标签: c static linker elf static-linking