请问如何才能用NDK编译Android程序顺利执行不出现not executable: magic 7F451错误

时间:2023-02-21 20:43:47
编译环境:32位android-ndk-r10e-windows-x86/android-ndk-r14b-windows-x86 + ctgwin + jdk1.8.0_131

Application.mk文件:
APP_ABI := armeabi-v7a  
#APP_ABI := armeabi
APP_PLATFORM = android-19

Android.mk文件:
LOCAL_PATH := $(call my-dir)  
include $(CLEAR_VARS)  
LOCAL_MODULE := poison  
LOCAL_SRC_FILES := poison.c 
LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llog  
include $(BUILD_EXECUTABLE)

使用ndk-build编译后,输出文件为poison,把程序push到虚拟机,chmod 0777执行,提示"not executable: magic 7F451"
IDA自带的android_server在虚拟机中可以执行,用readelf查看文件信息发现android_server和poison文件的Type类型一个是"EXEC (Executable file)"一个是"DYN (Shared object file)",不知道是否由于这个问题导致poison无法执行,如果是的话,请问该如何修改才能让程序可执行呢?

android_server:
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0xd8b0
  Start of program headers:          52 (bytes into file)
  Start of section headers:          569984 (bytes into file)
  Flags:                             0x4000002, has entry point, Version4 EABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         7
  Size of section headers:           40 (bytes)
  Number of section headers:         23
  Section header string table index: 22

poison:
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0xb38
  Start of program headers:          52 (bytes into file)
  Start of section headers:          62048 (bytes into file)
  Flags:                             0x5000000, Version5 EABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         8
  Size of section headers:           40 (bytes)
  Number of section headers:         34
  Section header string table index: 33


cygwin信息:
$ gcc --version
gcc (GCC) 6.3.0
Copyright  2016 Free Software Foundation, Inc.
本程序是*软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。


$ g++ --version
g++ (GCC) 6.3.0
Copyright  2016 Free Software Foundation, Inc.
本程序是*软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。


$ make -version
GNU Make 4.2.1
为 i686-pc-cygwin 编译
Copyright (C) 1988-2016 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第 3 版或更新版本<http://gnu.org/licenses/gpl.                                                                                                                                                                                                                                                                                           html>。
本软件是*软件:您可以*修改和重新发布它。
在法律允许的范围内没有其他保证。


$ gdb --version
GNU gdb (GDB) (Cygwin 7.11.1-2) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-pc-cygwin".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".

7 个解决方案

#1


编译过程:
$ $NDK/ndk-build -B V=1
Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 10 in /cygdrive/d/source/Poison-master/AndroidManifest.xml
rm -f /cygdrive/d/source/Poison-master/libs/arm64-v8a/lib*.so /cygdrive/d/source/Poison-master/libs/armeabi/lib*.so /cygdrive/d/source/Poison-master/libs/armeabi-v7a/lib*.so /cygdrive/d/source/Poison-master/libs/armeabi-v7a-hard/lib*.so /cygdrive/d/source/Poison-master/libs/mips/lib*.so /cygdrive/d/source/Poison-master/libs/mips64/lib*.so /cygdrive/d/source/Poison-master/libs/x86/lib*.so /cygdrive/d/source/Poison-master/libs/x86_64/lib*.so
rm -f /cygdrive/d/source/Poison-master/libs/arm64-v8a/gdbserver /cygdrive/d/source/Poison-master/libs/armeabi/gdbserver /cygdrive/d/source/Poison-master/libs/armeabi-v7a/gdbserver /cygdrive/d/source/Poison-master/libs/armeabi-v7a-hard/gdbserver /cygdrive/d/source/Poison-master/libs/mips/gdbserver /cygdrive/d/source/Poison-master/libs/mips64/gdbserver /cygdrive/d/source/Poison-master/libs/x86/gdbserver /cygdrive/d/source/Poison-master/libs/x86_64/gdbserver
rm -f /cygdrive/d/source/Poison-master/libs/arm64-v8a/gdb.setup /cygdrive/d/source/Poison-master/libs/armeabi/gdb.setup /cygdrive/d/source/Poison-master/libs/armeabi-v7a/gdb.setup /cygdrive/d/source/Poison-master/libs/armeabi-v7a-hard/gdb.setup /cygdrive/d/source/Poison-master/libs/mips/gdb.setup /cygdrive/d/source/Poison-master/libs/mips64/gdb.setup /cygdrive/d/source/Poison-master/libs/x86/gdb.setup /cygdrive/d/source/Poison-master/libs/x86_64/gdb.setup
[armeabi-v7a] Cygwin         : Generating dependency file converter script
mkdir -p /cygdrive/d/source/Poison-master/obj/
mount | tr '\\' '/' | awk -f /cygdrive/D/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/build/awk/gen-cygwin-deps-converter.awk > /cygdrive/d/source/Poison-master/obj/convert-dependencies.sh && chmod +x /cygdrive/d/source/Poison-master/obj/convert-dependencies.sh
[armeabi-v7a] Compile thumb  : poison <= poison.c
/cygdrive/D/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/toolchains/arm-linux-androideabi-4.8/prebuilt/windows/bin/arm-linux-androideabi-gcc -MMD -MP -MF D:/source/Poison-master/obj/local/armeabi-v7a/objs/poison/poison.o.d.org -fpic -ffunction-sections -funwind-tables -fstack-protector -no-canonical-prefixes -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp -mthumb -Os -g -DNDEBUG -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -ID:/source/Poison-master/jni -DANDROID  -Wa,--noexecstack -Wformat -Werror=format-security -fPIE    -ID:/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/platforms/android-19/arch-arm/usr/include -c  D:/source/Poison-master/jni/poison.c -o D:/source/Poison-master/obj/local/armeabi-v7a/objs/poison/poison.o && /cygdrive/d/source/Poison-master/obj/convert-dependencies.sh D:/source/Poison-master/obj/local/armeabi-v7a/objs/poison/poison.o.d
D:/source/Poison-master/jni/poison.c: In function 'zygote_special_process':
D:/source/Poison-master/jni/poison.c:757:2: warning: passing argument 2 of 'ptrace_call' makes integer from pointer without a cast [enabled by default]
  if (ptrace_call(target_pid, remote_getpid_addr, NULL, 0, &regs) == -1) {
  ^
D:/source/Poison-master/jni/poison.c:656:5: note: expected 'uint32_t' but argument is of type 'void *'
 int ptrace_call(pid_t pid, uint32_t addr, long *params, int num_params, struct pt_regs* regs) {
     ^
D:/source/Poison-master/jni/poison.c: At top level:
D:/source/Poison-master/jni/poison.c:882:1: warning: excess elements in struct initializer [enabled by default]
 } process_hook = {0, "", NULL, NULL, NULL};
 ^
D:/source/Poison-master/jni/poison.c:882:1: warning: (near initialization for 'process_hook') [enabled by default]
D:/source/Poison-master/jni/poison.c:882:1: warning: excess elements in struct initializer [enabled by default]
D:/source/Poison-master/jni/poison.c:882:1: warning: (near initialization for 'process_hook') [enabled by default]
D:/source/Poison-master/jni/poison.c:882:1: warning: excess elements in struct initializer [enabled by default]
D:/source/Poison-master/jni/poison.c:882:1: warning: (near initialization for 'process_hook') [enabled by default]
D:/source/Poison-master/jni/poison.c: In function 'main':
D:/source/Poison-master/jni/poison.c:915:2: warning: passing argument 2 of 'ptrace_attach' makes integer from pointer without a cast [enabled by default]
  ptrace_attach(process_hook.pid, strstr(process_name,"zygote"));
  ^
D:/source/Poison-master/jni/poison.c:505:5: note: expected 'int' but argument is of type 'char *'
 int ptrace_attach(pid_t pid, int zygote) {
     ^
[armeabi-v7a] Executable     : poison
/cygdrive/D/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/toolchains/arm-linux-androideabi-4.8/prebuilt/windows/bin/arm-linux-androideabi-g++ -Wl,--gc-sections -Wl,-z,nocopyreloc --sysroot=D:/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/platforms/android-19/arch-arm -Wl,-rpath-link=D:/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/platforms/android-19/arch-arm/usr/lib -Wl,-rpath-link=D:/source/Poison-master/obj/local/armeabi-v7a D:/source/Poison-master/obj/local/armeabi-v7a/objs/poison/poison.o -lgcc -no-canonical-prefixes -march=armv7-a -Wl,--fix-cortex-a8  -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -fPIE -pie -mthumb  -LD:/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/platforms/android-19/arch-arm/usr/lib -L/usr/lib -llog -lc -lm -o D:/source/Poison-master/obj/local/armeabi-v7a/poison
[armeabi-v7a] Install        : poison => libs/armeabi-v7a/poison
install -p /cygdrive/d/source/Poison-master/obj/local/armeabi-v7a/poison /cygdrive/d/source/Poison-master/libs/armeabi-v7a/poison
/cygdrive/D/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/toolchains/arm-linux-androideabi-4.8/prebuilt/windows/bin/arm-linux-androideabi-strip --strip-unneeded D:/source/Poison-master/libs/armeabi-v7a/poison


#2


是不是表述的不太清晰啊...

#3


怎么感觉你编出个SO文件,poison.c里有没有main函数?

#4


poison.c中有main函数
// 主函数
int main(int argc, char* argv[]) {

// 对传入的参数的个数进行判断(要求3个参数)
if(argc < 2)
exit(0);
..........
}


因为之前找到的资料中一种解释是由于gcc -c参数导致的编译出程序无法执行,所以我修改了definitions.mk去掉"[armeabi-v7a] Executable     : poison"处的-c参数,结果依旧

#5


引用 4 楼 lengx7 的回复:
poison.c中有main函数
// 主函数
int main(int argc, char* argv[]) {

// 对传入的参数的个数进行判断(要求3个参数)
if(argc < 2)
exit(0);
..........
}


因为之前找到的资料中一种解释是由于gcc -c参数导致的编译出程序无法执行,所以我修改了definitions.mk去掉"[armeabi-v7a] Executable     : poison"处的-c参数,结果依旧


看你编出来的是ARM版本的,模拟器是什么CPU,最好在真机上试试。 MK文件写的没什么问题。

#6


模拟器是逍遥模拟器,不知道如何查看模拟器支持的指令集
因为IDA自带的android_server,微信,QQ国际版和一些游戏都可以在虚拟机中运行,而android_server用readelf查看的结果就如在主楼中的那样,我就觉得模拟器是支持ARM指令集的了,不知道这个认知是否有误

#7


各位觉得哪里可能有问题的都请说下

#1


编译过程:
$ $NDK/ndk-build -B V=1
Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 10 in /cygdrive/d/source/Poison-master/AndroidManifest.xml
rm -f /cygdrive/d/source/Poison-master/libs/arm64-v8a/lib*.so /cygdrive/d/source/Poison-master/libs/armeabi/lib*.so /cygdrive/d/source/Poison-master/libs/armeabi-v7a/lib*.so /cygdrive/d/source/Poison-master/libs/armeabi-v7a-hard/lib*.so /cygdrive/d/source/Poison-master/libs/mips/lib*.so /cygdrive/d/source/Poison-master/libs/mips64/lib*.so /cygdrive/d/source/Poison-master/libs/x86/lib*.so /cygdrive/d/source/Poison-master/libs/x86_64/lib*.so
rm -f /cygdrive/d/source/Poison-master/libs/arm64-v8a/gdbserver /cygdrive/d/source/Poison-master/libs/armeabi/gdbserver /cygdrive/d/source/Poison-master/libs/armeabi-v7a/gdbserver /cygdrive/d/source/Poison-master/libs/armeabi-v7a-hard/gdbserver /cygdrive/d/source/Poison-master/libs/mips/gdbserver /cygdrive/d/source/Poison-master/libs/mips64/gdbserver /cygdrive/d/source/Poison-master/libs/x86/gdbserver /cygdrive/d/source/Poison-master/libs/x86_64/gdbserver
rm -f /cygdrive/d/source/Poison-master/libs/arm64-v8a/gdb.setup /cygdrive/d/source/Poison-master/libs/armeabi/gdb.setup /cygdrive/d/source/Poison-master/libs/armeabi-v7a/gdb.setup /cygdrive/d/source/Poison-master/libs/armeabi-v7a-hard/gdb.setup /cygdrive/d/source/Poison-master/libs/mips/gdb.setup /cygdrive/d/source/Poison-master/libs/mips64/gdb.setup /cygdrive/d/source/Poison-master/libs/x86/gdb.setup /cygdrive/d/source/Poison-master/libs/x86_64/gdb.setup
[armeabi-v7a] Cygwin         : Generating dependency file converter script
mkdir -p /cygdrive/d/source/Poison-master/obj/
mount | tr '\\' '/' | awk -f /cygdrive/D/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/build/awk/gen-cygwin-deps-converter.awk > /cygdrive/d/source/Poison-master/obj/convert-dependencies.sh && chmod +x /cygdrive/d/source/Poison-master/obj/convert-dependencies.sh
[armeabi-v7a] Compile thumb  : poison <= poison.c
/cygdrive/D/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/toolchains/arm-linux-androideabi-4.8/prebuilt/windows/bin/arm-linux-androideabi-gcc -MMD -MP -MF D:/source/Poison-master/obj/local/armeabi-v7a/objs/poison/poison.o.d.org -fpic -ffunction-sections -funwind-tables -fstack-protector -no-canonical-prefixes -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp -mthumb -Os -g -DNDEBUG -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -ID:/source/Poison-master/jni -DANDROID  -Wa,--noexecstack -Wformat -Werror=format-security -fPIE    -ID:/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/platforms/android-19/arch-arm/usr/include -c  D:/source/Poison-master/jni/poison.c -o D:/source/Poison-master/obj/local/armeabi-v7a/objs/poison/poison.o && /cygdrive/d/source/Poison-master/obj/convert-dependencies.sh D:/source/Poison-master/obj/local/armeabi-v7a/objs/poison/poison.o.d
D:/source/Poison-master/jni/poison.c: In function 'zygote_special_process':
D:/source/Poison-master/jni/poison.c:757:2: warning: passing argument 2 of 'ptrace_call' makes integer from pointer without a cast [enabled by default]
  if (ptrace_call(target_pid, remote_getpid_addr, NULL, 0, &regs) == -1) {
  ^
D:/source/Poison-master/jni/poison.c:656:5: note: expected 'uint32_t' but argument is of type 'void *'
 int ptrace_call(pid_t pid, uint32_t addr, long *params, int num_params, struct pt_regs* regs) {
     ^
D:/source/Poison-master/jni/poison.c: At top level:
D:/source/Poison-master/jni/poison.c:882:1: warning: excess elements in struct initializer [enabled by default]
 } process_hook = {0, "", NULL, NULL, NULL};
 ^
D:/source/Poison-master/jni/poison.c:882:1: warning: (near initialization for 'process_hook') [enabled by default]
D:/source/Poison-master/jni/poison.c:882:1: warning: excess elements in struct initializer [enabled by default]
D:/source/Poison-master/jni/poison.c:882:1: warning: (near initialization for 'process_hook') [enabled by default]
D:/source/Poison-master/jni/poison.c:882:1: warning: excess elements in struct initializer [enabled by default]
D:/source/Poison-master/jni/poison.c:882:1: warning: (near initialization for 'process_hook') [enabled by default]
D:/source/Poison-master/jni/poison.c: In function 'main':
D:/source/Poison-master/jni/poison.c:915:2: warning: passing argument 2 of 'ptrace_attach' makes integer from pointer without a cast [enabled by default]
  ptrace_attach(process_hook.pid, strstr(process_name,"zygote"));
  ^
D:/source/Poison-master/jni/poison.c:505:5: note: expected 'int' but argument is of type 'char *'
 int ptrace_attach(pid_t pid, int zygote) {
     ^
[armeabi-v7a] Executable     : poison
/cygdrive/D/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/toolchains/arm-linux-androideabi-4.8/prebuilt/windows/bin/arm-linux-androideabi-g++ -Wl,--gc-sections -Wl,-z,nocopyreloc --sysroot=D:/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/platforms/android-19/arch-arm -Wl,-rpath-link=D:/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/platforms/android-19/arch-arm/usr/lib -Wl,-rpath-link=D:/source/Poison-master/obj/local/armeabi-v7a D:/source/Poison-master/obj/local/armeabi-v7a/objs/poison/poison.o -lgcc -no-canonical-prefixes -march=armv7-a -Wl,--fix-cortex-a8  -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -fPIE -pie -mthumb  -LD:/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/platforms/android-19/arch-arm/usr/lib -L/usr/lib -llog -lc -lm -o D:/source/Poison-master/obj/local/armeabi-v7a/poison
[armeabi-v7a] Install        : poison => libs/armeabi-v7a/poison
install -p /cygdrive/d/source/Poison-master/obj/local/armeabi-v7a/poison /cygdrive/d/source/Poison-master/libs/armeabi-v7a/poison
/cygdrive/D/source/NDK/android-ndk-r14b-windows-x86/android-ndk-r14b/toolchains/arm-linux-androideabi-4.8/prebuilt/windows/bin/arm-linux-androideabi-strip --strip-unneeded D:/source/Poison-master/libs/armeabi-v7a/poison


#2


是不是表述的不太清晰啊...

#3


怎么感觉你编出个SO文件,poison.c里有没有main函数?

#4


poison.c中有main函数
// 主函数
int main(int argc, char* argv[]) {

// 对传入的参数的个数进行判断(要求3个参数)
if(argc < 2)
exit(0);
..........
}


因为之前找到的资料中一种解释是由于gcc -c参数导致的编译出程序无法执行,所以我修改了definitions.mk去掉"[armeabi-v7a] Executable     : poison"处的-c参数,结果依旧

#5


引用 4 楼 lengx7 的回复:
poison.c中有main函数
// 主函数
int main(int argc, char* argv[]) {

// 对传入的参数的个数进行判断(要求3个参数)
if(argc < 2)
exit(0);
..........
}


因为之前找到的资料中一种解释是由于gcc -c参数导致的编译出程序无法执行,所以我修改了definitions.mk去掉"[armeabi-v7a] Executable     : poison"处的-c参数,结果依旧


看你编出来的是ARM版本的,模拟器是什么CPU,最好在真机上试试。 MK文件写的没什么问题。

#6


模拟器是逍遥模拟器,不知道如何查看模拟器支持的指令集
因为IDA自带的android_server,微信,QQ国际版和一些游戏都可以在虚拟机中运行,而android_server用readelf查看的结果就如在主楼中的那样,我就觉得模拟器是支持ARM指令集的了,不知道这个认知是否有误

#7


各位觉得哪里可能有问题的都请说下