问题
I am trying to compile dummy function in gcc with flag -fno-pie and without.
void dummy_test_entrypoint() { }
When i compile without the flag.
gcc -m32 -ffreestanding -c test.c -o test.o
I get the following disassembled code.
00000000 <dummy_test_entrypoint>:
0: 55 push ebp
1: 89 e5 mov ebp,esp
3: e8 fc ff ff ff call 4 <dummy_test_entrypoint+0x4>
8: 05 01 00 00 00 add eax,0x1
d: 90 nop
e: 5d pop ebp
f: c3 ret
When i compile with the flag.
00000000 <dummy_test_entrypoint>:
0: 55 push ebp
1: 89 e5 mov ebp,esp
3: 90 nop
4: 5d pop ebp
5: c3 ret
My question.
What is it???
3: e8 fc ff ff ff call 4 <dummy_test_entrypoint+0x4>
8: 05 01 00 00 00 add eax,0x1
回答1:
You disassembled the object file without the --reloc
flag, so the output is misleading. With the --reloc
flag, you'll see this:
3: e8 fc ff ff ff call 4 <dummy_test_entrypoint+0x4>
4: R_386_PC32 __x86.get_pc_thunk.ax
8: 05 01 00 00 00 add $0x1,%eax
9: R_386_GOTPC _GLOBAL_OFFSET_TABLE_
And the subroutine looks like this:
00000000 <__x86.get_pc_thunk.ax>:
0: 8b 04 24 mov (%esp),%eax
3: c3 ret
This construct loads the GOT pointer into %eax
, in case the function needs to reference global data. The function does not contain such a reference, but because you compiled the code without optimization, GCC did not remove the dead code.
来源:https://stackoverflow.com/questions/57831288/why-does-gcc-generates-strange-code-without-flag-fno-pie