Why does modern GCC compile files as PIE by default?

715 Views Asked by At

As far as I know PIC is used primarily for shared libs, so there is no intent for GCC to compile an executable as PIE unless it was hinted to do it. Also PIE executables are not so useful because in every process' virtual address space there will be only one executable image that started it, and PIEs were made to be loaded several times at different virtual bases.

1

There are 1 best solutions below

4
yugr On

PIE allows dynamic loader to randomize process location in virtual address space (via so called ASLR technology) which helps against certain types of attacks.

Note that program segments are mapped as read-only which allows kernel to reuse same set of physical pages and not waste memory (except for the .got relocation segment).