Consider the following piece of code:
$ cat foo.c
static int foo = 100;
int function(void)
{
return foo;
}
I understand the dissassembly of libfoo.so
$ gcc -m32 -fPIC -shared -o libfoo.so foo.c
$ objdump -D libfoo.so
000004cc <function>:
4cc: 55 push %ebp
4cd: 89 e5 mov %esp,%ebp
4cf: e8 0e 00 00 00 call 4e2 <__x86.get_pc_thunk.cx>
4d4: 81 c1 c0 11 00 00 add $0x11c0,%ecx
4da: 8b 81 18 00 00 00 mov 0x18(%ecx),%eax
4e0: 5d pop %ebp
4e1: c3 ret
000004e2 <__x86.get_pc_thunk.cx>:
4e2: 8b 0c 24 mov (%esp),%ecx
4e5: c3 ret
4e6: 66 90 xchg %ax,%ax
...
000016ac <foo>:
16ac: 64 00 00 add %al,%fs:(%eax)
In the function
the address of foo
is computed as 0x4d4 (the value of ecx
after the call to __x86.get_pc_thunk.cx
) + $0x11c0 + 0x18 = 0x16ac. And 0x16ac is the address of foo
.
However I do not understand the disassembly of
$ gcc -m32 -fPIC -shared -c foo.c
$ objdump -D foo.o
00000000 <function>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: e8 fc ff ff ff call 4 <function+0x4>
8: 81 c1 02 00 00 00 add $0x2,%ecx
e: 8b 81 00 00 00 00 mov 0x0(%ecx),%eax
14: 5d pop %ebp
15: c3 ret
00000000 <foo>:
0: 64 00 00 add %al,%fs:(%eax)
00000000 <__x86.get_pc_thunk.cx>:
0: 8b 0c 24 mov (%esp),%ecx
3: c3 ret
Why call 4 <function+0x4>
and why add $0x2,%ecx
?
Update: (added -r flag to objdump, -R flag produces the error not a dynamic object, Invalid operation
.
$ objdump -D -r foo.o
00000000 <function>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: e8 fc ff ff ff call 4 <function+0x4>
4: R_386_PC32 __x86.get_pc_thunk.cx
8: 81 c1 02 00 00 00 add $0x2,%ecx
a: R_386_GOTPC _GLOBAL_OFFSET_TABLE_
e: 8b 81 00 00 00 00 mov 0x0(%ecx),%eax
10: R_386_GOTOFF .data
14: 5d pop %ebp
15: c3 ret
Now 4
makes sense in call 4 <function+0x4>
, because the offset of this instruction in the text section is 4. I still do not have any clue why 0x2
in add $0x2,%ecx
.