我们可以启用饼(即与位置无关的可执行文件)在Android的NDK R10C共享库?我们可以、可执行文件、位置、R10C

2023-09-06 13:55:24 作者:落雪无声

我们可以启用饼(即与位置无关的可执行文件)在Android的NDK R10C共享库?如果是的话那么该怎么办呢?

我看,我们应该使用PIC的动态库和PIE可执行但看起来像Android的NDK不支持PIC。

我试图让-pie标志LDFLAGS,但我发现了以下错误:

  /android-ndk-r10c/platforms/android-19/arch-arm/usr/lib/crtbegin_dynamic.o:
  在功能上_start:crtbrand.c(的.text + 0x8c):错误:未定义的引用主
 

Android虚拟机与ClassLoader类加载

请帮我解决这个问题,因为我已阅读,谷歌将在即将推出的Andr​​oid版本授权PIE,所以我想我的应用程序将与Android-L +兼容。

解决方案

在非常短的故事,如果你是构建共享库(而不是可执行文件),你不需要做任何事情。这在旧版本的Andr​​oid上运行将继续工作就好库 - 没有在安卓5.0已经改变相对于这个

在几乎同样短的故事,如果您正在构建使用Android.mk和定位的Andr​​oid 4.1+的可执行文件,所需的标志应自动已添加。

完整的故事:为什么你当您尝试添加 -pie 标志LDFLAGS图书馆失败的原因是,这个标志是只为可执行文件,而不是库。在构建共享库的编译器标志 -fPIC (而建立的目标文件,如果手动运行编译器 - Android.mk和NDK建造完成这个功能自动)可需要对某些架构,但是你会发现,这是必要的,因为连接器​​将拒绝产生一个共享库,如果它的需要,你还没有设置。因此,如果你有一个问题,你就已经知道了,因为它将无法​​建立 - 如果你已经建立了它成功,你没有任何问题。

同样构建可执行文件时,你需要添加 -fPIE 构建的目标文件时,与 -fPIE -pie 链接可执行文件时。 Android.mk和NDK建造会照顾这个自动,如果你的APP_PLATFORM的是Android-16(安卓4.1)或更高。这里的大疑难杂症 - 建有 -pie 将仅适用于Android的16或更高版本的可执行文件,同时内置无 -pie 将不会在Android-21的工作(安卓5.0)。因此,有一个宽限期这里,安卓4.1到4.4将运行任何可执行文件就好了,当你明确地需要一个版本,而 -pie 对于上了年纪的人,另一个版本 -pie $为较新的。

如果你需要瞄准的Andr​​oid版本4.1之前为好,见 http://stackoverflow.com/a/26422855/3115956 ,获取有关如何轻松地构建两个版本的可执行文件的说明。

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 dynamic libraries and PIE for executable but looks like android NDK doesn't support PIC.

I tried enabling -pie flag in LDFLAGS, but i'm getting following error:

/android-ndk-r10c/platforms/android-19/arch-arm/usr/lib/crtbegin_dynamic.o:
  in function _start:crtbrand.c(.text+0x8c): error: undefined reference to 'main'

Please help me to solve this as i have read that google will mandate PIE in upcoming android versions so i want my app to be compatible with ANDROID-L+.

解决方案

The really short story is, if you're building shared libraries (as opposed to executables), you don't need to do anything. Libraries that run on older versions of android will keep on working just fine - nothing has changed in Android 5.0 with respect to this.

The almost as short story is, if you're building executables using Android.mk and targeting Android 4.1+, the necessary flags should be added automatically already.

The full story: The reason why you fail when you try to add the -pie flag to LDFLAGS for libraries, is that this flag is only for executables, not for libraries. When building shared libraries, the compiler flag -fPIC (while building individual object files, if running the compiler manually - Android.mk and ndk-build takes care of this automatically) may be needed on some architectures, but you will notice that it is needed because the linker will refuse to produce a shared library if it's needed and you haven't set it. Thus, if you have an issue you will know already because it will fail to build - if you've built it successfully you don't have any issue.

Similarly when building executables, you need to add -fPIE when building the object files, and -fPIE -pie when linking the executables. Android.mk and ndk-build will take care of this automatically, if your APP_PLATFORM is android-16 (Android 4.1) or higher. Here's the big gotcha - executables built with -pie will only work on android-16 or higher, while executables built without -pie won't work on android-21 (Android 5.0). So there's a grace period here, Android 4.1 to 4.4 will run any executable just fine, while you explicitly need a version without -pie for the older ones and another version with -pie for the newer ones.

If you need to target Android versions prior to 4.1 as well, see http://stackoverflow.com/a/26422855/3115956 for an explanation on how to easily build two versions of your executable.