我正在使用 android NDK -r10d构建在 adb shell 上运行的 Android x86 可执行文件(共享链接)。在运行时,我收到以下警告:
WARNING: linker: ./myapp: **unused DT entry:** type 0x1d arg 0x4a604
我正在使用有根的Nexus Player来测试可执行文件。
我的构建机器是 Ubuntu 14.04(也在 Fedora 14 机器上试过)。
我正在使用 android NDK -r10d构建在 adb shell 上运行的 Android x86 可执行文件(共享链接)。在运行时,我收到以下警告:
WARNING: linker: ./myapp: **unused DT entry:** type 0x1d arg 0x4a604
我正在使用有根的Nexus Player来测试可执行文件。
我的构建机器是 Ubuntu 14.04(也在 Fedora 14 机器上试过)。
什么是“未使用的 DT 条目”错误?
如果您到达此页面,可能是因为您在基于 ARM 的 Android 系统上编译或尝试运行一些二进制文件,结果导致您的二进制文件/应用程序崩溃或在logcat
. 通常是这样的:
WARNING: linker: /blahblah/libopenssl.so: unused DT entry: type 0x6ffffffe arg 0x1188
问:什么是“DT 条目”?
简而言之,它们是ELF文件的文件结构中的描述性数组条目。具体来说,它们被称为
Dynamic Array Tags
并且是可执行和共享对象的要求。但是,并非所有条目都是必需的或可用的,具体取决于处理器和内核架构。
在我们的案例中,我们面临一个“警告”,其中一个“未使用”。这意味着,您的可执行文件或库 ( *.so
) 文件已使用指示的DT条目进行编译,但由于各种原因,您的内核不支持该条目。在基于 ARM 的 Android 系统上可以找到最好的示例,其中系统库路径是固定的,并且用于您的固件(操作系统/内核)的交叉编译器设置为不使用这些条目。通常二进制文件仍然可以正常运行,但内核会在您每次使用它时标记此警告。
问:这种情况什么时候发生?
这可能发生在以下情况:
从 5.1 (API 22) 开始,Android 链接器会针对 VERNEED 和 VERNEEDNUM ELF 动态部分发出警告。
在 Android 设备上导致此错误的最常见标志是:
DT_RPATH 0x0f (15) The DT_STRTAB string table offset of a null-terminated library search path string.
This element's use has been superseded by DT_RUNPATH.
DT_RUNPATH 0x1d (29) The DT_STRTAB string table offset of a null-terminated library search path string.
DT_VERNEED 0x6ffffffe The address of the version dependency table. Elements within this table contain
indexes into the string table DT_STRTAB. This element requires that the
DT_VERNEEDNUM element also be present.
DT_VERNEEDNUM 0x6fffffff The number of entries in the DT_VERNEEDNUM table.
跟踪上面的错误,我们发现这条消息来自bionic
库linker.cpp:
case DT_VERNEED:
verneed_ptr_ = load_bias + d->d_un.d_ptr;
break;
case DT_VERNEEDNUM:
verneed_cnt_ = d->d_un.d_val;
break;
case DT_RUNPATH:
// this is parsed after we have strtab initialized (see below).
break;
default:
if (!relocating_linker) {
DL_WARN("\"%s\" unused DT entry: type %p arg %p", get_realpath(),
reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
}
break;
}
支持此符号版本控制的代码(上图)于 2015 年4 月 9 日提交。因此,如果您的 NDK 构建设置为支持比此更早的 API,或者使用链接到此早期库的构建工具,您将收到这些警告。
问:如何找到我的系统或二进制文件正在使用的 DT 条目?
有很多方法可以做到这一点:
<linux/elf.h>
。# To find all elf.h files:
find /<path_to>/ndk/platforms/android-*/arch-arm*/usr/include/linux/ -iname "elf.h"
readelf
你的二进制文件:$ readelf --dynamic libopenssl.so
Dynamic section at offset 0x23b960 contains 28 entries:
Tag Type Name/Value
0x00000003 (PLTGOT) 0x23ce18
0x00000002 (PLTRELSZ) 952 (bytes)
0x00000017 (JMPREL) 0x15e70
0x00000014 (PLTREL) REL
0x00000011 (REL) 0x11c8
0x00000012 (RELSZ) 85160 (bytes)
0x00000013 (RELENT) 8 (bytes)
0x6ffffffa (RELCOUNT) 10632
0x00000015 (DEBUG) 0x0
0x00000006 (SYMTAB) 0x148
0x0000000b (SYMENT) 16 (bytes)
0x00000005 (STRTAB) 0x918
0x0000000a (STRSZ) 1011 (bytes)
0x00000004 (HASH) 0xd0c
0x00000001 (NEEDED) Shared library: [libdl.so]
0x00000001 (NEEDED) Shared library: [libc.so]
0x0000001a (FINI_ARRAY) 0x238458
0x0000001c (FINI_ARRAYSZ) 8 (bytes)
0x00000019 (INIT_ARRAY) 0x238460
0x0000001b (INIT_ARRAYSZ) 16 (bytes)
0x00000020 (PREINIT_ARRAY) 0x238470
0x00000021 (PREINIT_ARRAYSZ) 0x8
0x0000001e (FLAGS) BIND_NOW
0x6ffffffb (FLAGS_1) Flags: NOW
0x6ffffff0 (VERSYM) 0x108c
0x6ffffffe (VERNEED) 0x1188
0x6fffffff (VERNEEDNUM) 2
0x00000000 (NULL) 0x0
从上面的错误中可以看出,type
对应于DT_VERNEED
.
从这份文件:
DT_RPATH
此元素保存以空字符结尾的搜索库搜索路径字符串的字符串表偏移量,在“共享对象依赖关系”中进行了讨论。偏移量是记录在 DT_STRTAB 条目中的表的索引。DT_RPATH 可以给出一个包含目录列表的字符串,用冒号 (:) 分隔。所有 LD_LIBRARY_PATH 目录都在 DT_RPATH 之后搜索。
问:那么您如何解决或处理这些问题?
基本上有3种方法可以解决这个问题:
快速(你没有任何来源或只是不能被打扰)
使用“ELF 清洁器”从所有二进制文件中删除有问题的 DT 条目。这是一种简单快捷的补救措施,尤其是当您没有为您的系统正确重新编译它们的源代码时。至少有两种清洁剂可供您使用。
坏的(你有消息来源)
是正确的方法,因为在让它工作的过程中,你将成为一个糟糕的 ARM 交叉编译器专家。您基本上需要在使用的 Makefile 中查找和调整编译器设置。
从这里:
Android 链接器 (/system/bin/linker) 不支持 RPATH 或 RUNPATH,因此我们设置 LD_LIBRARY_PATH=$USR/lib 并尽量避免使用 --disable-rpath 配置标志构建无用的 rpath 条目。避免依赖 LD_LIBRARY_PATH 的另一个选项是提供自定义链接器 - 由于维护自定义链接器的开销,这没有完成。
丑陋的(您只希望您的应用程序可以使用任何脏二进制文件。)
您告诉您的 Java 应用程序在错误处理程序中检查 null 时不要惊慌失措,而是会收到这些警告,这可能会导致致命异常。使用类似的东西:
class OpensslErrorThread extends Thread {
@Override
public void run() {
try {
while(true){
String line = opensslStderr.readLine();
if(line == null){
// OK
return;
}
if(line.contains("unused DT entry")){
Log.i(TAG, "Ignoring \"unused DT entry\" error from openssl: " + line);
} else {
// throw exception!
break;
}
}
} catch(Exception e) {
Log.e(TAG, "Exception!")
}
}
}
这是非常糟糕和丑陋的,因为它不能解决任何问题,同时会使您的代码膨胀。此外,出现警告是有原因的,那就是在未来的 AOS 版本中,这将成为一个成熟的错误!
问:还有什么?
在 Android 内核和库的编译方式中,API 在 18-25(J 到 N)之间发生了许多变化。我无法对所有这些提供一个远程的详细解释,但也许这将有助于引导您朝着正确的方向前进。最好的资源当然是查看 Android 资源和文档本身。
最后是完整列表:
Name Value d_un Executable Shared Object
---------------------------------------------------------------------------------------------
DT_NULL 0 Ignored Mandatory Mandatory
DT_NEEDED 1 d_val Optional Optional
DT_PLTRELSZ 2 d_val Optional Optional
DT_PLTGOT 3 d_ptr Optional Optional
DT_HASH 4 d_ptr Mandatory Mandatory
DT_STRTAB 5 d_ptr Mandatory Mandatory
DT_SYMTAB 6 d_ptr Mandatory Mandatory
DT_RELA 7 d_ptr Mandatory Optional
DT_RELASZ 8 d_val Mandatory Optional
DT_RELAENT 9 d_val Mandatory Optional
DT_STRSZ 0x0a (10) d_val Mandatory Mandatory
DT_SYMENT 0x0b (11) d_val Mandatory Mandatory
DT_INIT 0x0c (12) d_ptr Optional Optional
DT_FINI 0x0d (13) d_ptr Optional Optional
DT_SONAME 0x0e (14) d_val Ignored Optional
DT_RPATH 0x0f (15) d_val Optional Optional
DT_SYMBOLIC 0x10 (16) Ignored Ignored Optional
DT_REL 0x11 (17) d_ptr Mandatory Optional
DT_RELSZ 0x12 (18) d_val Mandatory Optional
DT_RELENT 0x13 (19) d_val Mandatory Optional
DT_PLTREL 0x14 (20) d_val Optional Optional
DT_DEBUG 0x15 (21) d_ptr Optional Ignored
DT_TEXTREL 0x16 (22) Ignored Optional Optional
DT_JMPREL 0x17 (23) d_ptr Optional Optional
DT_BIND_NOW 0x18 (24) Ignored Optional Optional
DT_INIT_ARRAY 0x19 (25) d_ptr Optional Optional
DT_FINI_ARRAY 0x1a (26) d_ptr Optional Optional
DT_INIT_ARRAYSZ 0x1b (27) d_val Optional Optional
DT_FINI_ARRAYSZ 0x1c (28) d_val Optional Optional
DT_RUNPATH 0x1d (29) d_val Optional Optional
DT_FLAGS 0x1e (30) d_val Optional Optional
DT_ENCODING 0x1f (32) Unspecified Unspecified Unspecified
DT_PREINIT_ARRAY 0x20 (32) d_ptr Optional Ignored
DT_PREINIT_ARRAYSZ 0x21 (33) d_val Optional Ignored
DT_MAXPOSTAGS 0x22 (34) Unspecified Unspecified Unspecified
DT_LOOS 0x6000000d Unspecified Unspecified Unspecified
DT_SUNW_AUXILIARY 0x6000000d d_ptr Unspecified Optional
DT_SUNW_RTLDINF 0x6000000e d_ptr Optional Optional
DT_SUNW_FILTER 0x6000000e d_ptr Unspecified Optional
DT_SUNW_CAP 0x60000010 d_ptr Optional Optional
DT_SUNW_SYMTAB 0x60000011 d_ptr Optional Optional
DT_SUNW_SYMSZ 0x60000012 d_val Optional Optional
DT_SUNW_ENCODING 0x60000013 Unspecified Unspecified Unspecified
DT_SUNW_SORTENT 0x60000013 d_val Optional Optional
DT_SUNW_SYMSORT 0x60000014 d_ptr Optional Optional
DT_SUNW_SYMSORTSZ 0x60000015 d_val Optional Optional
DT_SUNW_TLSSORT 0x60000016 d_ptr Optional Optional
DT_SUNW_TLSSORTSZ 0x60000017 d_val Optional Optional
DT_SUNW_CAPINFO 0x60000018 d_ptr Optional Optional
DT_SUNW_STRPAD 0x60000019 d_val Optional Optional
DT_SUNW_CAPCHAIN 0x6000001a d_ptr Optional Optional
DT_SUNW_LDMACH 0x6000001b d_val Optional Optional
DT_SUNW_CAPCHAINENT 0x6000001d d_val Optional Optional
DT_SUNW_CAPCHAINSZ 0x6000001f d_val Optional Optional
DT_HIOS 0x6ffff000 Unspecified Unspecified Unspecified
DT_VALRNGLO 0x6ffffd00 Unspecified Unspecified Unspecified
DT_CHECKSUM 0x6ffffdf8 d_val Optional Optional
DT_PLTPADSZ 0x6ffffdf9 d_val Optional Optional
DT_MOVEENT 0x6ffffdfa d_val Optional Optional
DT_MOVESZ 0x6ffffdfb d_val Optional Optional
DT_POSFLAG_1 0x6ffffdfd d_val Optional Optional
DT_SYMINSZ 0x6ffffdfe d_val Optional Optional
DT_SYMINENT 0x6ffffdff d_val Optional Optional
DT_VALRNGHI 0x6ffffdff Unspecified Unspecified Unspecified
DT_ADDRRNGLO 0x6ffffe00 Unspecified Unspecified Unspecified
DT_CONFIG 0x6ffffefa d_ptr Optional Optional
DT_DEPAUDIT 0x6ffffefb d_ptr Optional Optional
DT_AUDIT 0x6ffffefc d_ptr Optional Optional
DT_PLTPAD 0x6ffffefd d_ptr Optional Optional
DT_MOVETAB 0x6ffffefe d_ptr Optional Optional
DT_SYMINFO 0x6ffffeff d_ptr Optional Optional
DT_ADDRRNGHI 0x6ffffeff Unspecified Unspecified Unspecified
DT_RELACOUNT 0x6ffffff9 d_val Optional Optional
DT_RELCOUNT 0x6ffffffa d_val Optional Optional
DT_FLAGS_1 0x6ffffffb d_val Optional Optional
DT_VERDEF 0x6ffffffc d_ptr Optional Optional
DT_VERDEFNUM 0x6ffffffd d_val Optional Optional
DT_VERNEED 0x6ffffffe d_ptr Optional Optional
DT_VERNEEDNUM 0x6fffffff d_val Optional Optional
DT_LOPROC 0x70000000 Unspecified Unspecified Unspecified
DT_SPARC_REGISTER 0x70000001 d_val Optional Optional
DT_AUXILIARY 0x7ffffffd d_val Unspecified Optional
DT_USED 0x7ffffffe d_val Optional Optional
DT_FILTER 0x7fffffff d_val Unspecified Optional
DT_HIPROC 0x7fffffff Unspecified Unspecified Unspecified
使用 readelf -d 您可以在二进制文件中列出 DT 条目:
0x0000001d (RUNPATH) Library runpath: [lib]
如您所见, 0x1d 对应于 RUNPATH 该条目添加了链接器选项 -rpath (或 -R,如果后跟目录)