Can we enable pie (i.e. Position Independent Executables) for shared libraries in android-ndk r10c?

后端 未结 2 1450
谎友^
谎友^ 2021-02-20 05:24

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

2条回答
  •  情话喂你
    2021-02-20 06:08

    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 ), but it was optional. See Security Enhancements in Android 1.5 through 4.1. So I think it depends less on the NDK version, and more on the Android version.

    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 ), and without PIE, then you will get a link error. Also see Position Independent Executables and Android Lollipop.


    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.

提交回复
热议问题