Android Oat 文件格式分析笔记
ManyFace
2015.12.2
目录
1 Elf Header ............................................................................................................................... 4
1.1 e_type 取值 ..................................................................................................................... 4
1.2 e_machine 取值 .............................................................................................................. 4
1.3 文件中 Elf header 分析.................................................................................................. 5
2 program header table ............................................................................................................. 6
2.1 p_type 取值 ..................................................................................................................... 6
2.2 p_flags 取值 .................................................................................................................... 7
2.3 文件中 program header table 分析............................................................................... 8
3 section header table ............................................................................................................... 9
3.1 sh_type 取值 ................................................................................................................. 10
3.2 sh_flag 取值 .................................................................................................................. 13
3.3 文件中 section header table 分析 ............................................................................... 15
3.3.1 第零个 section header .......................................................................................... 15
3.3.2 第一个 section header .......................................................................................... 16
3.3.3 第二个 section header .......................................................................................... 19
3.3.4 第三个 section header .......................................................................................... 20
3.3.5 第四个 section header .......................................................................................... 20
3.3.6 第五个 section header .......................................................................................... 21
3.3.7 第六个 section header .......................................................................................... 21
3.3.8 第七个 section header .......................................................................................... 22
4 Oatdata 段 ............................................................................................................................ 23
4.1 OatHeader ..................................................................................................................... 24
4.2 文件中 oatHeader 分析 ............................................................................................... 27
4.3 Dex Meta Data ............................................................................................................... 28
4.4 文件中 Dex Meta Data 分析 ...................................................................................... 29
4.5 OatClass ......................................................................................................................... 30
4.5.1 文件中分析 OatClass ............................................................................................ 33
5 总结 ..................................................................................................................................... 35
参考:
http://blog.csdn.net/luoshengyang/article/details/39307813
http://blog.csdn.net/tenfyguo/article/details/5631561
https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
http://androidxref.com/5.1.1_r6/
最近分析了 Oat 文件格式,基于 Android5.1.1、32 位,现把分析笔记与大家
共享。由于能力有限,笔记中有什么不对的地方,还请各位大神指正。
本次分析基于一个从 Nexus6 中拷贝出来的真实 oat 文件。以该文件为样本,
逐步分析,达到深入理解的效果。大家在看本文时,可以结合附件中的图和 oat
文件(demo.oat)。下面进入正题。
Oat 文件其实就是类型为 shared object 的 Elf 文件,因而具有 elf 文件的外
壳,图 1 是来自维基的 Elf 文件结构图。在 Oat 文件中,有两个特殊的段:oatdata
段和 oatexec 段,其实就是图 1 中的.rodata 和.text。oatdata 段包含与原始 dex 文
件相关的信息,oatexec 段存的是方法的 native code。Program header table 中的
信息用于告诉操作系统如何加载该文件,section header table 中的信息主要用于
链接阶段。因而,本次分析从 Elf 文件格式开始分析,再深入分析 Oatdata 段。
图 1 Elf 文件格式
1 Elf Header
/art/runtime/elf.h Elf32_Ehdr 34h bytes
69struct
Elf32_Ehdr
{
70 unsigned char e_ident[EI_NIDENT]; // ELF Identification bytes
71 Elf32_Half e_type; // Type of file (see ET_* below)
72 Elf32_Half e_machine; // Required architecture for this file (see EM_*)
73 Elf32_Word e_version; // Must be equal to 1
74 Elf32_Addr e_entry; // Address to jump to in order to start program
75 Elf32_Off e_phoff; // Program header table's file offset, in bytes
76 Elf32_Off e_shoff; // Section header table's file offset, in bytes
77 Elf32_Word e_flags; // Processor-specific flags
78 Elf32_Half e_ehsize; // Size of ELF header, in bytes
79 Elf32_Half e_phentsize; // Size of an entry in the program header table
80 Elf32_Half e_phnum; // Number of entries in the program header table
81 Elf32_Half e_shentsize; // Size of an entry in the section header table
82 Elf32_Half e_shnum; // Number of entries in the section header table
83 Elf32_Half e_shstrndx; // Sect hdr table index of sect name string table
84 bool checkMagic() const {
85 return (memcmp(e_ident, ElfMagic, strlen(ElfMagic))) == 0;
86 }
87 unsigned char getFileClass() const { return e_ident[EI_CLASS]; }
88 unsigned char getDataEncoding() const { return e_ident[EI_DATA]; }
89}
1.1 e_type 取值
115// File types
116enum {
117
ET_NONE
= 0, // No file type
118
ET_REL
= 1, // Relocatable file
119
ET_EXEC
= 2, // Executable file
120
ET_DYN
= 3, // Shared object file
121
ET_CORE
= 4, // Core file
122
ET_LOPROC
= 0xff00, // Beginning of processor-specific codes
123
ET_HIPROC
= 0xffff // Processor-specific
124};
1.2 e_machine 取值
(0028h 表示是 arm)
132// Machine architectures
133enum {
134 … …
157
EM_ARM
= 40, // ARM
158 … …
297};
1.3 文件中 Elf header 分析
字段
值
地址
unsigned char e_ident[16]
该字段不用转字节序
7F454C46 (magicNumber)
01 (32 or 64)
01 (little or big endianness)
01 (Version)
03 (OSabi)
00 (abiVersion)
00000000000000 (pad)
0
e_type (2b)
0003 (共享目标文件)
10
e_machine (2b)
0028
12
e_version (4b)
00000001
14
e_entry (4b)
00000000
18
e_phoff (4b)
00000034
1C
e_shoff (4b)
0012B070
20
e_flags (4b)
05000000
24
e_ehsize(2b)
0034
28
e_phentsize(2b)
0020
2A
e_phnum(2b)
0005
2C
e_shentsize(2b)
0028
2E
e_shnum(2b)
0008
30
e_shstrndx(2b)
0007
32
由上可知,该 elf 类型是共享目标文件。
Elf header 的字节数为 e_ehsize 34h bytes。
Program header table 开始地址是 e_phoff 34h,table 中每一条记录的字节数
是 e_phentsize 20h bytes,记录条数为 e_phnum 5h。因而 Program header table
的字节数是 20h*5h=A0h bytes,在文件中的范围[34 h, D3 h]。
Section header table 开始地址是 e_shoff 12B070h,table 中每一条记录的字节
数是 e_shentsize 28h bytes,记录条数为 e_shnum 8h。因而 Section header
table 的字节数是 28h*8h=140h bytes,在文件中的范围[12B070 h, 12B1AF h]。
Section names string table 在 section header table 中的索引是 e_shstrndx 7,
也就是说 section header table 中的第 8 条记录是关于 Section names string
table 的相关信息。Section names string table 中存着每个 sectioin 的名字。