I'm investigating relocation of shared libraries, and ran into something strange. Consider this code:
int myglob;
int ml_util_func(int p)
{
return p + 2;
}
int ml_func2(int a, int b)
{
int c = ml_util_func(a);
return c + b + myglob;
}
I compile it to a non-PIC shared lib with gcc -shared
. I do this on a 32-bit Ubuntu running on x86.
The resulting .so
has a relocation entry for the call to ml_util_func
in ml_func2
. Here's the output of objdump -dR -Mintel
on ml_func2
:
0000050d <ml_func2>:
50d: 55 push ebp
50e: 89 e5 mov ebp,esp
510: 83 ec 14 sub esp,0x14
513: 8b 45 08 mov eax,DWORD PTR [ebp+0x8]
516: 89 04 24 mov DWORD PTR [esp],eax
519: e8 fc ff ff ff call 51a <ml_func2+0xd>
51a: R_386_PC32 ml_util_func
51e: 89 45 fc mov DWORD PTR [ebp-0x4],eax
521: 8b 45 0c mov eax,DWORD PTR [ebp+0xc]
524: 8b 55 fc mov edx,DWORD PTR [ebp-0x4]
527: 01 c2 add edx,eax
529: a1 00 00 00 00 mov eax,ds:0x0
52a: R_386_32 myglob
52e: 8d 04 02 lea eax,[edx+eax*1]
531: c9 leave
532: c3 ret
533: 90 nop
Note the R_386_PC32
relocation on the call
instruction.
Now, my question is why is this relocation needed? e8
is "call relative..." on a x86, and since ml_util_func
is defined in the same object, surely the linker can compute the relative offset between it and the call without leaving it to the dynamic loader?
Interestingly, if ml_util_func
is declared static
, the relocation disappears and the linker correctly computes and inserts the offset. What is it about ml_util_func
being also exported that makes the linker lazy about it?
P.S.: I'm playing with non-PIC code on purpose, to understand load-time relocations.