This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: is -fPIC safe for executable programs, or must -fPIE be used?
- From: Jeffrey Walton <noloader at gmail dot com>
- To: Britton Kerin <britton dot kerin at gmail dot com>
- Cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Thu, 24 Mar 2016 15:21:36 -0400
- Subject: Re: is -fPIC safe for executable programs, or must -fPIE be used?
- Authentication-results: sourceware.org; auth=none
- References: <CAC4O8c-FrGrpKUQptyYzZpu8Zr9afmyWaRHTFLRLDMyA9QZtaw at mail dot gmail dot com>
- Reply-to: noloader at gmail dot com
On Wed, Mar 23, 2016 at 6:12 PM, Britton Kerin <britton.kerin@gmail.com> wrote:
> I can't tell from the manual if -fPIC is safe for executable programs,
> or only for libraries.
>
> I want to use -fPIC or -fPIE so dladdr() will work right. I would
> rather use -fPIC if possible, because it seems simpler to use one such
> option for everything if possible.
You can build all your object files with -fPIC.
When you build you shared library you can use the object files and:
g++ -shared myobj1.o myobj2.o ... -o mylib.so
When you build your program you use the same object files and:
g++ -pie myobj1.o myobj2.o ... -o myprog.exe
The reverse is not true. You cannot build your object files with -fPIE
and then create a shared object with -shared.
One of the GCC devs explained why its OK a few years ago, but I can't
find the bug report with the explanation. I just use -fPIC all the
time unless its i686 because the GOT pointer basically reserves EBX.
On i686 it becomes a tradeoff because you lose a register, so you may
not want -fPIC/-fPIE at all.
Jeff