This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: is -fPIC safe for executable programs, or must -fPIE be used?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]