How to compile .so for Android O without Writable

2019-02-27 09:17发布

问题:

https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#Writable-and-Executable-Segments-Enforced-for-API-level-26

Currently Android O Preview 4 is blocking the loading of certain native libraries due to having Writable and Executable Segments. They determine this by doing a scan of the ELF.

I am trying to compile a native shared object library for Android without such Writable and Executable Segments.

Not being very familiar with this type of low-level compilation-speak, can someone explain how to set the segments to be properly mutually exclusive with respect to writable and executable permissions?

I figure I may need to add some specific flags to my Android.mk file to prevent this issue, with other initial thoughts that this may be something to do with the NDK toolchain being used for compilation.

Any help or advice on where to look is appreciated.

回答1:

The GNU toolchains and the Android toolchains have been doing the right thing for a long, long time now. You can use the readelf/grep command in the article:

$ readelf --program-headers -W libBadFlags.so | grep WE

If there is no output, you should be fine. If there is output, you need to track down the source of the writable/executable mapping. For a GNU_STACK header, it is likely an assembler file with a missing annotation (a mere oversight). But much more obscure things are possible, so there is no generic solution.

But in most cases, no changes to your project should be needed.