Android 檢視動態庫依賴的庫檔案
參考:http://blog.csdn.net/renwotao2009/article/details/51398739
最近工作過程中,遇到了一個庫檔案開啟失敗的問題,需要檢視so檔案依賴哪些動態庫。之前看到有同事用到ldd命令,但是本地使用ldd命令來解析arm的動態庫是不行的。
armwind# ldd libcameraservice.so
not a dynamic executable
答案大家是知道的,由於是arm架構,用x86的程式來解析肯定是有問題的。所以又百度了一下,發現是有對應的命令來解析,如下我是64位的arm,所以使用了aarch64-linux-android-readelf命令。
aarch64-linux-android-readelf -d libcameraservice.so
執行上面的命令就得到下面的結果
Dynamic section at offset 0xe4cf0 contains 44 entries:
Tag Type Name/Value
0x0000000000000003 (PLTGOT) 0x1069f0
0x0000000000000002 (PLTRELSZ) 41448 (bytes)
0x0000000000000017 (JMPREL) 0x5fcd0
0x0000000000000014 (PLTREL) RELA
0x0000000060000011 (Operating System specific: 60000011) 0x58c28
0x0000000060000012 (Operating System specific: 60000012) 0x70a8
0x0000000000000009 (RELAENT) 24 (bytes)
0x000000006ffffff9 (RELACOUNT) 652
0x0000000000000006 (SYMTAB) 0x20238
0x000000000000000b (SYMENT) 24 (bytes)
0x0000000000000005 (STRTAB) 0x2e5d8
0x000000000000000a (STRSZ) 155156 (bytes)
0x000000006ffffef5 (GNU_HASH) 0x543f0
0x0000000000000001 (NEEDED) Shared library: [libui.so]
0x0000000000000001 (NEEDED) Shared library: [liblog.so]
0x0000000000000001 (NEEDED) Shared library: [libutils.so]
0x0000000000000001 (NEEDED) Shared library: [libbinder.so]
0x0000000000000001 (NEEDED) Shared library: [libcutils.so]
0x0000000000000001 (NEEDED) Shared library: [libmedia.so]
0x0000000000000001 (NEEDED) Shared library: [libmediautils.so]
0x0000000000000001 (NEEDED) Shared library: [libcamera_client.so]
0x0000000000000001 (NEEDED) Shared library: [libgui.so]
0x0000000000000001 (NEEDED) Shared library: [libhardware.so]
0x0000000000000001 (NEEDED) Shared library: [libsync.so]
0x0000000000000001 (NEEDED) Shared library: [libcamera_metadata.so]
0x0000000000000001 (NEEDED) Shared library: [libjpeg.so]
0x0000000000000001 (NEEDED) Shared library: [libmemunreachable.so]
0x0000000000000001 (NEEDED) Shared library: [libc++.so]
0x0000000000000001 (NEEDED) Shared library: [libdl.so]
0x0000000000000001 (NEEDED) Shared library: [libc.so]
0x0000000000000001 (NEEDED) Shared library: [libm.so]
0x000000000000000e (SONAME) Library soname: [libcameraservice.so]
0x000000000000001a (FINI_ARRAY) 0xf2a60
0x000000000000001c (FINI_ARRAYSZ) 8 (bytes)
0x0000000000000019 (INIT_ARRAY) 0x105ce8
0x000000000000001b (INIT_ARRAYSZ) 8 (bytes)
0x000000000000001e (FLAGS) BIND_NOW
0x000000006ffffffb (FLAGS_1) Flags: NOW
0x000000006ffffff0 (VERSYM) 0x578c4
0x000000006ffffffc (VERDEF) 0x58bbc
0x000000006ffffffd (VERDEFNUM) 1
0x000000006ffffffe (VERNEED) 0x58bd8
0x000000006fffffff (VERNEEDNUM) 2
0x0000000000000000 (NULL) 0x0
二、程式演示
如果是x86的動態庫就可以使用ldd來分析。
- 1.編譯成動態庫
例程:
#include<iostream>
using namespace std;
int main() {
cout<<"hello world" << endl;
return 0;
}
g++ -fPIC -shared -o libarmwind.so helloWorld.cpp
使用上面的命令就可以將方法封裝成一個動態庫,上面把helloworld封裝成了libarmwind.so.
armwind#ls -l libarmwind.so
-rwxrwxrwx 1 root root 8693 Sep 5 09:52 libarmwind.so
- 2.使用ldd檢視依賴庫
使用g++編譯得到預設的a.out可執行程式。
armwind# ldd a.out
linux-vdso.so.1 => (0x00007fff061ff000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fdf4e5b0000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fdf4e2b4000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fdf4e09d000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fdf4dcde000)
/lib64/ld-linux-x86-64.so.2 (0x00007fdf4e8d8000)
- 3.使用readelf來檢視依賴庫
armwind# readelf -d libarmwind.so | grep NEEDED
0x0000000000000001 (NEEDED) Shared library: [libstdc++.so.6]
0x0000000000000001 (NEEDED) Shared library: [libm.so.6]
0x0000000000000001 (NEEDED) Shared library: [libgcc_s.so.1]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]