Issue
I was going through the assembly code generated by the compiler. I am using the C programming language and GCC
compiler.
I wrote a function in C which adds two numbers by calling another function and stores the result in the variable pointed to by the pointer passed as an argument to the function.
void add_two_num(int x, int y, int * dest)
{
int val;
val = dummy(x, y);
*dest = val;
}
I compiled the source code to object code (linking not done) and then disassembled the code using objdump -d
What is the meaning of the number +0x9
in the line call d <add_two_num+0x9>
?
Is that useful at the stage of linking when that line will be replaced by the actual function call?
file format elf64-x86-64
0000000000000004 <add_two_num>:
4: 53 push %rbx
5: 48 89 d3 mov %rdx,%rbx
8: e8 00 00 00 00 call d <add_two_num+0x9>
d: 89 03 mov %eax,(%rbx)
f: 5b pop %rbx
10: c3 ret
Solution
You are looking at an object file. This file has not been linked yet and the addresses of external functions have not been filled in yet. You can see this in the instruction encoding: the 00 00 00 00
is a dummy for the actual call target to be patched in later.
Unfortunately objdump is not smart enough to know about this on x86, so it disassembles as if the offset was actually 00 00 00 00
, i.e. the call goes to the next instruction. This instruction is 0x9
bytes after the last label, so you see it interprets this address as add_two_num+0x9
.
You can pass the -r
option to objdump
to have it show you relocation information. This way you know what function is actually being called. It'll look something like this:
0000000000000000 <add_two_num>:
0: 53 push %rbx
1: 48 89 d3 mov %rdx,%rbx
4: e8 00 00 00 00 call 9 <add_two_num+0x9>
5: R_X86_64_PLT32 dummy-0x4
9: 89 03 mov %eax,(%rbx)
b: 5b pop %rbx
c: c3 ret
Answered By - fuz Answer Checked By - Dawn Plyler (WPSolving Volunteer)