如何 运行 应用与 android 上的 glibc.so 关联

How to run app linked with glibc.so on android

编译器是 gcc-linaro-5.5.0-2017.10-i686_arm-linux-gnueabihf.tar.xz,应用程序 运行 on armv7 android4.4 device

我的C代码是test.c,

#include"stdio.h"
int main(void)
{
        printf("starting...\n");
        return 0;
}

如果我编译它使用命令:

arm-linux-gnueabihf-gcc -o test_s test.c -g -Wl,-rpath=.:./lib -Wl,-rpath=.:./lib -Wl,-q -Wl,-dynamic-linker=ld-2.21.so -static

它可以很好地工作:

root@test:/data/mine # chmod 555 test_s
root@test:/data/mine # ./test_s
starting

但是,如果我编译它使用命令:

 arm-linux-gnueabihf-gcc -o test_d test.c -g -Wl,-rpath=.:./lib -Wl,-rpath=.:./lib -Wl,-q -Wl,-dynamic-linker=ld-2.21.so

它给我错误:

root@test:/data/mine # chmod 555 test_d
root@test:/data/mine # ls -l test_d
-rwxrwxrwx root     root        12316 2018-04-19 13:37 test_d
root@test:/data/mine # ./test_d
/system/bin/sh: ./test_d: Permission denied

我已经将所需的动态库复制到路径/data/mine/lib

我该如何处理这个问题?

更新

在 运行 命令之前 mount -o remount,exec /data 命令 mount return:

/dev/block/platform/comip-mmc.1/by-name/userdata /data ext4 rw,seclabel,nosuid,nodev,noatime,noauto_da_alloc,data=ordered 0 0

在运行之后 mount -o remount,exec /data 坐骑 return:

/dev/block/platform/comip-mmc.1/by-name/userdata /data ext4 rw,seclabel,relatime,noauto_da_alloc,data=ordered 0 0

我再次 运行 strace ./test_d,它给了我相同的结果 EACCES。 我认为 "EACCES The filesystem is mounted noexec" 不是根本原因,因为 test_s 在同一目录中,而 运行 正确

我尝试用命令编译:arm-linux-gnueabihf-gcc -o test_d_ndl test.c -g -Wl,-rpath=。 :./lib

arm-linux-gnueabihf-readelf -d test_d_ndl
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
 0x0000000f (RPATH)                      Library rpath: [.:./lib]

我将 libc.so.6 复制到路径 /data/mine 和 运行 commond

root@test:/data/mine # ls -l
-rw-rw-rw- root     root       774660 2018-04-20 13:32 ld-2.21.so
drwxrwxr-x root     root              2018-04-17 14:43 lib
-rw-rw-rw- root     root      9180280 2018-04-20 16:50 libc.so.6
-rwxrwxrwx root     root        13808 2018-04-18 16:56 test
-rwxrwxrwx root     root        12316 2018-04-20 13:31 test_d
-r-xr-xr-x root     root        10520 2018-04-20 16:46 test_d_ndl
-rwxrwxrwx root     root         3348 2018-04-20 09:47 test_ld
-rwxrwxrwx root     root      3672356 2018-04-19 09:58 test_s

root@test:/data/mine # strace  ./test_d_ndl
execve("./test_d_ndl", ["./test_d_ndl"], [/* 25 vars */]) = -1 ENOENT (No such file or directory)
write(2, "strace: exec", 12strace: exec)            = 12
write(2, ": ", 2: )                       = 2
write(2, "No such file or directory", 25No such file or directory) = 25
write(2, "\n", 1
)                       = 1
exit_group(1)                           = ?

test_d_ndl给出错误原因ENOENT,接下来我该怎么做?

更新

使用以下命令效果很好

arm-linux-gnueabihf-gcc test.c -o test_r_dl -Wl,-rpath=/data/mine/libc/lib -Wl,-dynamic-linker= /data/mine/libc/lib/ld-2.21.so

if i compile it use command: ... -Wl,-dynamic-linker=ld-2.21.so ...

使用 --dynamic-linker 的相对路径是 错误 的建议:您的应用程序将 运行 IFF 在您当前的工作中有一个 ./ld-2.21.so目录。

I have copied the required dynamic libs to the path /data/mine/lib

因为当您启动二进制文件时您不在 /data/mine/lib 中,并且假设您没有 ld-2.21.so 复制到 /data/mine, 将找不到您的动态链接器。

但是,通常 这是用 ENOENT 而不是 EPERM 报告的,所以我不确定这是你真正的问题。 运行 strace ./test_d 可能会提供更多线索。

更新:

root@test:/data/mine # strace ./test_d execve("./test_d", ["./test_d"], [/* 25 vars */]) = -1 EACCES (Permission denied) ...

execve man page 说:

EACCES The filesystem is mounted noexec.

这听起来可能是您出错的直接原因。

(一旦你 mount -o remount,exec /data/mine,它可能会开始失败,而不是 ENOENT。)