Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling executable with "-fPIC" flag (no shared library)

I am currently working on some legacy packages and I see that some Makefiles use the -fPIC flag to compile the final executable.

My understanding is that this is used when creating a shared library and that it would make no sense to use it when creating an executable.

I wonder if I am right or I am missing something, because when I search on the internet, I only get results talking about shared libraries and not executables.

Thanks,

like image 793
XNor Avatar asked Oct 18 '25 19:10

XNor


1 Answers

Position independent code can be useful outside of shared library context. One common case is Address space layout randomization, which makes the final executable more secure against code injection attacks.

In several distributions, such as Fedora, most of the packages are built as PIE.

like image 148
Dmitry Grigoryev Avatar answered Oct 20 '25 08:10

Dmitry Grigoryev