你如何交叉编译的ELF可执行程序为Android? [英] How do you cross compile ELF executables for Android?
问题描述
我有prepared样品C code以下,使其在Android操作系统上运行通过以下步骤,
/ * test.c的文件* /
#包括< stdio.h中>
INT
主(INT ARGC,字符** argv的)
{
的printf(Android的测试程序\ n);
返回0;
}
$臂无-Linux的gnueabi-gcc的-o测试test.c的-Wall
我复制的二进制文件--test--到目标设备/系统/ bin目录。
当我试图进行交叉编译的二进制运行在目标系统上,我收到此错误
$ PWD
/系统/斌
$ ./test
庆典:./test:没有这样的文件或目录
$ LS -al | grep的测试
-rwxr-XR-X 1 0 0 8384 2011-12-22 15:26测试
虽然二进制文件--test--已经在/系统/ bin目录。
我的交叉编译器是
$臂无-Linux的gnueabi - 海湾合作委员会 - 版本
臂无-Linux的gnueabi - 海湾合作委员会(的Sourcery G ++精简版2009q1-203)4.3.3 版权所有(C)2008自由软件基金会这是自由 软件;看到源复制的条件。有否 保证;甚至没有适销性或适用于特定 目的。
为什么我收到此错误?
庆典:./test:没有这样的文件或目录
这个问题涉及到动态加载程序。当我检查了二进制文件readelf的输出,我通知了交叉编译器,我使用了不同类型的默认动态加载器 - /lib/ld-linux.so.3,但Android使用/系统/斌/连接器
$ readelf --program报头测试
ELF文件类型是EXEC(可执行文件)
入口点0x8380
有8个程序头,开始于偏移52
程序头:
型胶印VirtAddr PhysAddr FileSiz MemSiz FLG对齐
EXIDX 0x0004fc 0x000084fc 0x000084fc 0x00050 0x00050 - [R为0x4
PHDR 0x000034 0x00008034 0x00008034 0x00100 0x00100 RE为0x4
INTERP 0x000134 0x00008134 0x00008134 0x00013 0x00013 - [R为0x1
[请求程序间preTER:/lib/ld-linux.so.3]
LOAD 0x000000处0x00008000 0x00008000 0x00550 0x00550 RE为0x8000
LOAD 0x000550 0x00010550 0x00010550 0x00124 0x00128 RW为0x8000
动态0x00055c 0x0001055c 0x0001055c 0x000f0 0x000f0 RW为0x4
注0x000148 0x00008148 0x00008148 0x00020 0x00020 - [R为0x4
GNU_STACK 0x000000处00000000 00000000 0x00000 0x00000 RW为0x4
为了改变动态加载程序(动态链接),--dynamic-链接= /路径/要/装载机应该用于连接器。
$臂无-Linux的gnueabi-gcc的-o测试test.c的-Wall 轮候册, - 动态链接= /系统/斌/链接
I have prepared a sample C code below to make it run on Android Operating System by following these procedures,
/*test.c file*/
#include <stdio.h>
int
main(int argc, char **argv)
{
printf("Android Test Application.\n");
return 0;
}
$ arm-none-linux-gnueabi-gcc -o test test.c -Wall
I copied the binary file --test-- into target device "/system/bin" directory.
When I attempt to make cross-compiled binary run on the target system, I am getting this error
$ pwd
/system/bin
$ ./test
bash: ./test: No such file or directory
$ ls -al | grep test
-rwxr-xr-x 1 0 0 8384 2011-12-22 15:26 test
although the binary file --test-- is already in "/system/bin" directory.
My cross-compiler is
$ arm-none-linux-gnueabi-gcc --version
arm-none-linux-gnueabi-gcc (Sourcery G++ Lite 2009q1-203) 4.3.3 Copyright (C) 2008 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Why I am getting this error?
bash: ./test: No such file or directory
This problem was related to the dynamic loader. When I checked the output of the binary file with readelf, I notified that the cross-compiler that I use has a different type of default dynamic loader - "/lib/ld-linux.so.3", but Android uses "/system/bin/linker"
$ readelf --program-headers test
Elf file type is EXEC (Executable file)
Entry point 0x8380
There are 8 program headers, starting at offset 52
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
EXIDX 0x0004fc 0x000084fc 0x000084fc 0x00050 0x00050 R 0x4
PHDR 0x000034 0x00008034 0x00008034 0x00100 0x00100 R E 0x4
INTERP 0x000134 0x00008134 0x00008134 0x00013 0x00013 R 0x1
[Requesting program interpreter: /lib/ld-linux.so.3]
LOAD 0x000000 0x00008000 0x00008000 0x00550 0x00550 R E 0x8000
LOAD 0x000550 0x00010550 0x00010550 0x00124 0x00128 RW 0x8000
DYNAMIC 0x00055c 0x0001055c 0x0001055c 0x000f0 0x000f0 RW 0x4
NOTE 0x000148 0x00008148 0x00008148 0x00020 0x00020 R 0x4
GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4
In order to change dynamic loader(dynamic linker), "--dynamic-linker=/path/to/loader" should be used for the linker.
$ arm-none-linux-gnueabi-gcc -o test test.c -Wall -Wl,--dynamic-linker=/system/bin/linker
这篇关于你如何交叉编译的ELF可执行程序为Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!