Tho*_*ire 5 linux linker elf ld prelink
我正在使用交叉预链接来预链接使用 Qt 用于嵌入式 ARM 设备的大型 C++ 可执行文件。请注意,我没有使用 Yocto,而是使用自定义发行版 - 因此我目前正在手动运行预链接。
查看 prelink 的输出,它似乎有效:
$ prelink --verbose --ld-library-path=/opt/<product>/lib:/usr/local/Qt-5.3.1/lib --root=$PRODUCT_TARGET_ROOT/<product>/rfs/ /path/to/binary
Laying out 56 libraries in virtual address space 41000000-50000000
Assigned virtual address space slots for libraries:
/lib/ld-linux.so.3 41000000-41027908
/opt/<product>/lib/lib<product>common.so.1 41030000-41cf0fd0
/lib/libc.so.6 442b0000-443e3980
/usr/local/Qt-5.3.1/lib/libQt5Qml.so.5 434f0000-4380ee84
[..]
Prelinking /lib/ld-2.17.so
Prelinking /lib/libc-2.17.so
Prelinking /path/to/binary
Prelinking /<product>/lib/lib<product>common.so.1.0.0
Prelinking /usr/local/Qt-5.3.1/lib/libQt5Qml.so.5.3.1
[..]
Run Code Online (Sandbox Code Playgroud)
当库被加载时,至少 libQt5Qml.so 和 libproductcommon.so 似乎被加载到预链接设置的首选加载地址:
$ cat /proc/`pidof binary`/maps
2ab49000-2ab4a000 r--p 0001e000 07:00 9357 /roroot/lib/ld-2.17.so
2ab4a000-2ab4b000 rw-p 0001f000 07:00 9357 /roroot/lib/ld-2.17.so
2b0fd000-2b223000 r-xp 00000000 07:00 9730 /roroot/lib/libc-2.17.so
2b223000-2b22a000 ---p 00126000 07:00 9730 /roroot/lib/libc-2.17.so
2b22a000-2b22c000 r--p 00125000 07:00 9730 /roroot/lib/libc-2.17.so
2b22c000-2b22d000 rw-p 00127000 07:00 9730 /roroot/lib/libc-2.17.so
41030000-41ce7000 r-xp 00000000 07:00 9305 /roroot/<product>/lib/lib<product>common.so.1.0.0
41ce7000-41cef000 ---p 00cb7000 07:00 9305 /roroot/<product>/lib/lib<product>common.so.1.0.0
41cef000-41cf1000 rw-p 00cb7000 07:00 9305 /roroot/<product>/lib/lib<product>common.so.1.0.0
434f0000-437f8000 r-xp 00000000 07:00 1355 /roroot/usr/local/Qt-5.3.1/lib/libQt5Qml.so.5.3.1
437f8000-437ff000 ---p 00308000 07:00 1355 /roroot/usr/local/Qt-5.3.1/lib/libQt5Qml.so.5.3.1
437ff000-4380e000 rw-p 00307000 07:00 1355 /roroot/usr/local/Qt-5.3.1/lib/libQt5Qml.so.5.3.1
[..]
Run Code Online (Sandbox Code Playgroud)
现在,我预计搬迁数量会有所减少:
$ LD_DEBUG=statistics /path/to/binary
20453: number of relocations: 66379
20453: number of relocations from cache: 38995
20453: number of relative relocations: 21690
$ LD_USE_LOAD_BIAS=0 LD_DEBUG=statistics /path/to/binary
20478: number of relocations: 66379
20478: number of relocations from cache: 38995
20478: number of relative relocations: 62981
Run Code Online (Sandbox Code Playgroud)
这表明只有相对 重定位由于预链接而减少,而不是正常重定位(可能需要符号查找)。我特别有兴趣减少其他搬迁,因为那些可能是更昂贵的。
现在我的问题是:
好的,事实证明,问题在于某些库未正确预链接,如我原来的问题所示,其中例如 libc.so 未加载到正确的加载地址。
似乎预链接是一种全有或全无的方法:如果可执行文件的依赖项之一未正确预链接或无法在首选地址加载,则可执行文件和库都将无法利用预链接的符号重定位,并且仅利用预链接的相对重定位。
除了上述内容之外,还应检查库是否已正确预链接:
# readelf --dynamic usr/lib/someLibrary.so
[..]
0x6ffffdf5 (GNU_PRELINKED) 2014-12-15T14:16:56
[..]
# readelf --program-headers usr/lib/someLibrary.so
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
[..]
LOAD 0x000000 0x44bf0000 0x44bf0000 0xb56d4 0xb56d4 R E 0x8000
[..]
Run Code Online (Sandbox Code Playgroud)
prelink --verbose、输出的地址readelf --program-headers需要cat /proc/PID/maps匹配。
我的错误是我没有检查readelf- 如果我这样做了,我会意识到目标设备上的某些库没有预先链接,因为构建系统中的错误导致预先链接的版本被非预先链接的版本覆盖...
修复我的构建系统问题后,正常的重定位确实下降到了 0:
# LD_DEBUG=statistics /path/to/binary
5089: number of relocations: 0
5089: number of relocations from cache: 19477
5089: number of relative relocations: 0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1173 次 |
| 最近记录: |