Can we enable pie (i.e. Position Independent Executables) for shared libraries in android-ndk r10c? And if yes then how to do it?
I read that we should use PIC for dynam
Can we enable pie (i.e. Position Independent Executables) for shared libraries in android-ndk r10c?
PIE was introduced in Android 4.1/android-16
(see Android
When I try to run PIE code on Android 4.0 or below, I get a segfault in /system/bin/linker
. That's with an HTC Evo 4G. Your mileage may vary, depending on how robust the link/loader is provided by the OEM. Also see Is PIE (Position-independent executable) for main executables supported in Android 4.0 (ICS)?
Now, PIE is required for Android 5.0 and above. Also see Security Enhancements in Android 5.0.
If you try to compile/link for Android 5.0/android-21
(see Android
Can we enable pie ... for shared libraries in android-ndk
And a quick word on the obvious (once you know about it). PIC
is slightly different than PIE
. You use PIE on executable programs, and PIC on shared objects.
If you are building an executable and shared object from the same set of sources and object files, then you would use PIC because PIC works for both (the same is not true of PIE). Also see Position Independent Executables and Android Lollipop.
And if yes then how to do it?
You can compile and link a PIE executable in one of two ways. First, compile everything with -fPIE
and link with -pie
. The second is to compile everything with -fPIC
and link with -pie
.
If you are building an executable and shared object from the same set of sources and object files, then you would use PIC because PIC works for both (the same is not true of PIE). Also see Position Independent Executables and Android Lollipop.