0

I used to apply -fPIC option when I was compiling shared library in Linux but I didn't know much about PIC at that time, so I just typed it as if it is a convention.

Recently I read about PIC in wiki and several websites, and I think I understand general concept now. However, I found that including -fPIC option not only when compiling & linking a shared library, but also a executable (a.out) makes almost no difference in behavior at first glance such as compile error or something. For example I tested a simple Hello world program in C++ with and without -fPIC option, and both of all work well without any error.

In this case, I am wondering which is the difference that -fPIC makes in behaviour. All I've got is slight difference in addresses and _GLOBAL__sub_I_(sth) by checking objdump -t. Additionally, I am curious about if there will be any harmful effect while compiling executable with -fPIC option.

Thanks in advance!

Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
Jisung Kim
  • 93
  • 1
  • 8
  • `-fPIC` makes slower code, that's why it's no on by default. e.g. it may require indirection through the GOT for global variables, and may not always inline small functions if they're not `static`. And may call through the PLT instead of directly. For some ISAs, even `-fPIE` has downsides (just being position-independent, without even considering the visibility semantics: e.g. [32-bit absolute addresses no longer allowed in x86-64 Linux?](https://stackoverflow.com/q/43367427)) – Peter Cordes Feb 11 '22 at 19:41

0 Answers0