1. 程式人生 > >基於Android arm64 可執行程式的編譯執行

基於Android arm64 可執行程式的編譯執行

Android預設編譯的應用程式動態連結的一般都是PIE,前文“基於Android arm64 Linux got 除錯”,每次除錯都要檢視載入地址,於是就想能否編譯生成非PIE的應用程式。

/opt/android-6.0.1_r9/external/hello$ mmm ./ showcommands 2>&1 | tee build.log

檢視build.log

prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-g++ -pie -nostdlib -Bdynamic -Wl
,-dynamic-linker,/system/bin/linker64 -Wl,--gc-sections -Wl,-z,nocopyreloc -Lout/target/product/generic_arm64/obj/lib -Wl,-rpath-link=out/target/product/generic_arm64/obj/lib out/target/product/generic_arm64/obj/lib/crtbegin_dynamic.o out/target/product/generic_arm64/obj/EXECUTABLES/hello_intermediates/hello.o -Wl
,--whole-archive -Wl,--no-whole-archive out/target/product/generic_arm64/obj/STATIC_LIBRARIES/libcompiler_rt-extras_intermediates/libcompiler_rt-extras.a prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/../lib/gcc/aarch64-linux-android/4.9.x-google/../../../../aarch64-linux-android/lib/../lib64/libatomic.
a prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/../lib/gcc/aarch64-linux-android/4.9.x-google/libgcc.a -lutils -lcutils -lc++ -ldl -lc -lm -o out/target/product/generic_arm64/obj/EXECUTABLES/hello_intermediates/LINKED/hello -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--build-id=md5 -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,-maarch64linux -Wl,--hash-style=gnu -Wl,--fix-cortex-a53-843419 -Wl,--allow-shlib-undefined -Wl,--no-undefined out/target/product/generic_arm64/obj/lib/crtend_android.o

果然有-pie引數。在android編譯根路徑輸入去除-pie引數的命令:

/opt/android-6.0.1_r9$ prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-g++  -nostdlib -Bdynamic -Wl,-dynamic-linker,/system/bin/linker64 -Wl,--gc-sections -Wl,-z,nocopyreloc  -Lout/target/product/generic_arm64/obj/lib -Wl,-rpath-link=out/target/product/generic_arm64/obj/lib out/target/product/generic_arm64/obj/lib/crtbegin_dynamic.o             out/target/product/generic_arm64/obj/EXECUTABLES/hello_intermediates/hello.o          -Wl,--whole-archive   -Wl,--no-whole-archive   out/target/product/generic_arm64/obj/STATIC_LIBRARIES/libcompiler_rt-extras_intermediates/libcompiler_rt-extras.a    prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/../lib/gcc/aarch64-linux-android/4.9.x-google/../../../../aarch64-linux-android/lib/../lib64/libatomic.a prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/../lib/gcc/aarch64-linux-android/4.9.x-google/libgcc.a -lutils -lcutils -lc++ -ldl -lc -lm  -o out/target/product/generic_arm64/obj/EXECUTABLES/hello_intermediates/LINKED/hello  -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--build-id=md5 -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,-maarch64linux -Wl,--hash-style=gnu -Wl,--fix-cortex-a53-843419  -Wl,--allow-shlib-undefined    -Wl,--no-undefined out/target/product/generic_arm64/obj/lib/crtend_android.o 

檢視編譯結果
這裡寫圖片描述

模擬器執行試試:
這裡寫圖片描述
夠狠,不支援。反正只是除錯,不做產品。看看改改連結源程式行不行。

修改bionic/linker/linker.cpp

#if 0
  if (elf_hdr->e_type != ET_DYN) {
    __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
    exit(EXIT_FAILURE);
  }
#else
 if (elf_hdr->e_type != ET_DYN) {
    __libc_format_fd(2, "warnning: only position independent executables (PIE) are supported. Just ignore temporarily.\n");
  }
#endif

編譯重新執行:
這裡寫圖片描述