Running a native library on Android L. error: only position independent executables (PIE) are supported
I built two executable files: one with APP_PLATFORM := android-9
and the other with APP_PLATFORM := android-16
. To run the native code in Java I need this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
// Run the file which was created using APP_PLATFORM := android-16
} else {
// Run the file which was created using APP_PLATFORM := android-9
}
“only position independent executables (PIE) are supported”
You don't need an ffmpeg "update". PIE is a compile time setting. You can just compile it yourself with PIE options set.
CFLAGS="-fPIE -pie"
see more here:
https://github.com/danielkop/android-ffmpeg/commit/616a099151fb6be05b559adc4c9ed95afacd92c2
Position Independent Executables and Android
I don't know anything about PIE, Please tell me how to create a position independent executable.
Position Independent Executable or PIE allows a program to be relocated, just like a shared object. At each run of the program, the program can be loaded at different addresses to make it harder for an attacker to guess certain program state.
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 both a shared object and a program, then compile everything with -fPIC
. Link the shared object with -shared
, and link the program with -pie
.
You cannot do it the other way. That is, you cannot compile everything with -fPIE
and build both a shared object and a program. For the details, see Code Generation Options in the GCC manual.
One thing to watch out for on Android: building with PIE prior to 4.1 will cause a segmentation fault in /system/bin/linker
. PIE was added at Android 4.1, and it crashes lesser versions.
Someone told me to supply a custom link/loader to avoid the problem, but I can't find the reference at the moment.
Also see Security Enhancements in Android 1.5 through 4.1.
Error: only position independent executables (PIE) are supported
Yes, that's a Lollipop feature. See Security Enhancements in Android 5.0.
You can check if a program is built with PIE using readelf
:
$ readelf -l my-prog | grep -i "file type"
Elf filetype is DYN (shared object file)
The important part is readelf
is reporting DYN
, and not reporting EXE
. EXE
means it lacks PIE, and that should trigger a security related defect.
Related, see Is PIE (Position-independent executable) for main executables supported in Android 4.0 (ICS)?
ndk can't get executable file
This is not an error in itself - it's only the file
utility which interprets a position independent executable (PIE) as a shared object - your executable has been built just fine.
Only Android 4.1 and newer supports PIE executables, and on 5.0, non-PIE executables aren't allowed any longer - this may be why you can't run it in the emulator. See Running a native library on Android L. error: only position independent executables (PIE) are supported for more details on this issue. If your target platform in the NDK is Android 4.1 or newer (android-16), executables will be built with PIE enabled.
Related Topics
Displaying Images from a Specific Folder on the Sdcard Using a Gridview
Android Volley to Handle Redirect
Fast Video Compression on Android
Android L (API 21) - Java.Lang.Illegalargumentexception: Service Intent Must Be Explicit
Keyboard Not Shown When I Click on Edittextview in Android
How to Highlight Row in Listview in Android
Android: Detect Orientation Changed
Urlconnection or Httpclient: Which Offers Better Functionality and More Efficiency
Actionbarsherlock - How to Set the Padding of Each Actionbar's Icon
Place Imageview Over Button Android
How to Get Contacts from Native Phonebook in Android
Explain the Meaning of Span Flags Like Span_Exclusive_Exclusive
Parse JSON Array Response Using Retrofit & Gson
Restrict Edittext to Single Line