I've read the topic GCC -fPIC option
So I've created my testlib.cpp.
int foo(int num)
{
int result;
if (num != 0)
{
result = 1;
}
else
{
result = 2;
}
return result;
}
When I compile as g++ -c -o testlib.o testlib.cpp and as g++ -fPIC -c -o testlib.o testlib.cpp the corresponding objdumps of testlib.o are identical:
objdump -d testlib.o -M intel
testlib.o: file format elf32-i386
Disassembly of section .text:
00000000 <_Z3fooi>:
0: 55 push ebp
1: 89 e5 mov ebp,esp
3: 83 ec 10 sub esp,0x10
6: 83 7d 08 00 cmp DWORD PTR [ebp+0x8],0x0
a: 74 09 je 15 <_Z3fooi+0x15>
c: c7 45 fc 01 00 00 00 mov DWORD PTR [ebp-0x4],0x1
13: eb 07 jmp 1c <_Z3fooi+0x1c>
15: c7 45 fc 02 00 00 00 mov DWORD PTR [ebp-0x4],0x2
1c: 8b 45 fc mov eax,DWORD PTR [ebp-0x4]
1f: c9 leave
20: c3 ret
And I expected the addresses of arguments of jump and je commands to be position independent when compiling with -fPIC. So the two objdumps should be different. What do I understand wrong?
-fPIC
is on by default in newergcc
versions. The code is position independent even without the option:this is position independent, looking at the 2-byte opcode, even if the disassembly prints the symbol and its offset for clarity.
Note that it's very likely that the compiler would produce position-independent code for that kind of short jumps whatever the options.
So this flag is not very useful now. BUT you can disable PIC with
-fno-PIC
switch.