In this simple program, I get a relocation in main
for compute
, but not for compute2
:
static int compute2()
{
return 2;
}
int compute()
{
return 1;
}
int main()
{
return compute() + compute2();
}
I compile this with gcc -c main.cpp
using gcc 11.2.0 on Ubuntu 21.10.
Here's what objdump
says about main
:
000000000000001e <main>:
1e: f3 0f 1e fa endbr64
22: 55 push rbp
23: 48 89 e5 mov rbp,rsp
26: 53 push rbx
27: e8 00 00 00 00 call 2c <main+0xe> 28: R_X86_64_PLT32 compute()-0x4
2c: 89 c3 mov ebx,eax
2e: e8 cd ff ff ff call 0 <compute2()>
33: 01 d8 add eax,ebx
35: 48 8b 5d f8 mov rbx,QWORD PTR [rbp-0x8]
39: c9 leave
3a: c3 ret
As you can see, for the call to compute2
(internal linkage) there is a relative jump with no relocation. But for the call to compute
(external linkage) there is a relocation, even if all three functions are in the same section in the same object file.
Why is that relocation needed? I thought the linker would never split up a section, so no matter where this section gets loaded, relative addresses should still be the same? Why does linkage seemingly affect this?