How will it change the code, e.g. function calls?
相关问题
- Error building gcc 4.8.3 from source: libstdc++.so
- Direct2D Only Partially Linking in C++ Builder
- What are the recommended GNU linker options to spe
- What is the right order of linker flags in gcc?
- Why doesn't g++ -Wconversion warn about conver
相关文章
- gcc/g++ gives me error “CreateProcess: No such fil
- Calls that precede a function's definition can
- How can I use gcc's -I command to add recursiv
- How do I know if std::map insert succeeded or fail
- How to specify gcc flags (CXXFLAGS) particularly f
- MPI and D: Linker Options
- How to generate assembly code with gcc that can be
- Weakly link static library via -weak_library
PIE is to support address space layout randomization (ASLR) in executable files.
Before the PIE mode was created, the program's executable could not be placed at a random address in memory, only position independent code (PIC) dynamic libraries could be relocated to a random offset. It works very much like what PIC does for dynamic libraries, the difference is that a Procedure Linkage Table (PLT) is not created, instead PC-relative relocation is used.
After enabling PIE support in gcc/linkers, the body of program is compiled and linked as position-independent code. A dynamic linker does full relocation processing on the program module, just like dynamic libraries. Any usage of global data is converted to access via the Global Offsets Table (GOT) and GOT relocations are added.
PIE is well described in this OpenBSD PIE presentation.
Changes to functions are shown in this slide (PIE vs PIC).
and in this slide (PIE vs old-style linking)
Note, that PIE may be incompatible with
-static
Minimal runnable example: GDB the executable twice
For those that want to see some action:
For the one with
-pie
, we see that the address ofmain
changes between runs:so in this example, the address for the first run was
0x55db0066b79a
and for the second0x563910ccd79a
.But for the one with
-no-pie
, the address ofmain
remains the same0x400627
for both runs:echo 2 | sudo tee /proc/sys/kernel/randomize_va_space
ensures that ASLR is on (the default in Ubuntu 17.10): How can I temporarily disable ASLR (Address space layout randomization)? | Ask Ubuntu.set disable-randomization off
is needed otherwise GDB, as the name suggests, turns off ASLR for the process by default to give fixed addresses across runs to improve the debugging experience: Difference between gdb addresses and "real" addresses? | Stack Overflowreadelf
funFurthermore, we can also observe that:
gives the actual runtime load address:
while:
gives just an offset:
By turning ASLR off (with either
randomize_va_space
orset disable-randomization off
), GDB always givesmain
the address:0x5555555547a9
, so we deduce that the-pie
address is composed from:TODO where is 0x555555554000 hard coded in the Linux kernel / glibc loader / wherever? How is the address of the text section of a PIE executable determined in Linux?
Tested in Ubuntu 18.04.
Related question: How can I tell, with something like objdump, if an object file has been built with -fPIC?