endian_tag 个 dex 文件

endian_tag of dex files

dexes 文件中的 endian_tag 是如何工作的? Documentation 表示

The constant ENDIAN_CONSTANT is used to indicate the endianness of the file in which it is found. Although the standard .dex format is little-endian, implementations may choose to perform byte-swapping. Should an implementation come across a header whose endian_tag is REVERSE_ENDIAN_CONSTANT instead of ENDIAN_CONSTANT, it would know that the file has been byte-swapped from the expected form.

建议如果有 REVERSE_ENDIAN_CONSTANT 文件应该使用 big-endian。这对吗?此外——文件的哪一部分应该使用这个大端——整个文件包括 header_items?

big-endian 标志将应用于文件中任何位置的任何多字节值。您可以通过查看 DexSwapVerify source 来了解它适用的确切事物集,它负责将所有多字节值从文件的字节顺序转换为主机的字节顺序。这样做是为了让 VM 的其他部分或工具不必在每次访问值时都进行交换。

大多数 Android 设备都是小端 ARM 或 x86,因此交换操作是空操作。一些 Android OS 开发人员在早期使用 PPC Mac,并且有一些 big-endian 设备(例如 MIPS),因此交换代码很重要。

特别感兴趣的一行是:

if (pHeader->endianTag != kDexEndianConstant) {
    ALOGE("Unexpected endian_tag: %#x", pHeader->endianTag);
    return false;
}

请注意,它不是在检查反向字节序常量——代码不希望找到大字节序 DEX 文件。 "Raw" DEX 文件应该是小端格式——该格式为它们提供了一种方式,使其成为大端格式,但我希望大多数工具都能阻止它们。

优化的 DEX (.odex) 文件以主机字节顺序存储,因此从 .odex 读取时跳过字节交换步骤。优化的 DEX 文件只能在生成它们的系统上读取。