1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
9 * Android system headers have two different elf.h file. The one under linux/
10 * is the most complete on older Android API versions without unified headers.
12 #if defined(ANDROID) && __ANDROID_API__ < 21 && !defined(__ANDROID_API_L__)
13 #include <linux/elf.h>
19 #if defined(__ARM_EABI__) && !defined(PT_ARM_EXIDX)
20 #define PT_ARM_EXIDX 0x70000001
24 * Generic ELF macros for the target system
27 #define Elf_(type) Elf64_ ## type
28 #define ELFCLASS ELFCLASS64
29 #define ELF_R_TYPE ELF64_R_TYPE
30 #define ELF_R_SYM ELF64_R_SYM
32 #define ELF_ST_BIND ELF64_ST_BIND
35 #define Elf_(type) Elf32_ ## type
36 #define ELFCLASS ELFCLASS32
37 #define ELF_R_TYPE ELF32_R_TYPE
38 #define ELF_R_SYM ELF32_R_SYM
40 #define ELF_ST_BIND ELF32_ST_BIND
45 #error Cannot find endianness
48 #if __BYTE_ORDER == __LITTLE_ENDIAN
49 #define ELFDATA ELFDATA2LSB
50 #elif __BYTE_ORDER == __BIG_ENDIAN
51 #define ELFDATA ELFDATA2MSB
55 #define ELFOSABI ELFOSABI_LINUX
57 #define ELFABIVERSION 0
60 #error Unknown ELF OSABI
64 #define ELFMACHINE EM_386
66 // Doing this way probably doesn't scale to other architectures
67 #define R_ABS R_386_32
68 #define R_GLOB_DAT R_386_GLOB_DAT
69 #define R_JMP_SLOT R_386_JMP_SLOT
70 #define R_RELATIVE R_386_RELATIVE
71 #define RELOC(n) DT_REL ## n
72 #define UNSUPPORTED_RELOC(n) DT_RELA ## n
73 #define STR_RELOC(n) "DT_REL" # n
76 #elif defined(__x86_64__)
77 #define ELFMACHINE EM_X86_64
79 #define R_ABS R_X86_64_64
80 #define R_GLOB_DAT R_X86_64_GLOB_DAT
81 #define R_JMP_SLOT R_X86_64_JUMP_SLOT
82 #define R_RELATIVE R_X86_64_RELATIVE
83 #define RELOC(n) DT_RELA ## n
84 #define UNSUPPORTED_RELOC(n) DT_REL ## n
85 #define STR_RELOC(n) "DT_RELA" # n
88 #elif defined(__arm__)
89 #define ELFMACHINE EM_ARM
94 #ifndef R_ARM_GLOB_DAT
95 #define R_ARM_GLOB_DAT 21
97 #ifndef R_ARM_JUMP_SLOT
98 #define R_ARM_JUMP_SLOT 22
100 #ifndef R_ARM_RELATIVE
101 #define R_ARM_RELATIVE 23
104 #define R_ABS R_ARM_ABS32
105 #define R_GLOB_DAT R_ARM_GLOB_DAT
106 #define R_JMP_SLOT R_ARM_JUMP_SLOT
107 #define R_RELATIVE R_ARM_RELATIVE
108 #define RELOC(n) DT_REL ## n
109 #define UNSUPPORTED_RELOC(n) DT_RELA ## n
110 #define STR_RELOC(n) "DT_REL" # n
113 #elif defined(__aarch64__)
114 #define ELFMACHINE EM_AARCH64
116 #define R_ABS R_AARCH64_ABS64
117 #define R_GLOB_DAT R_AARCH64_GLOB_DAT
118 #define R_JMP_SLOT R_AARCH64_JUMP_SLOT
119 #define R_RELATIVE R_AARCH64_RELATIVE
120 #define RELOC(n) DT_RELA ## n
121 #define UNSUPPORTED_RELOC(n) DT_REL ## n
122 #define STR_RELOC(n) "DT_RELA" # n
126 #error Unknown ELF machine type
130 * Android system headers don't have all definitions
135 #ifndef DT_INIT_ARRAY
136 #define DT_INIT_ARRAY 25
138 #ifndef DT_FINI_ARRAY
139 #define DT_FINI_ARRAY 26
141 #ifndef DT_INIT_ARRAYSZ
142 #define DT_INIT_ARRAYSZ 27
144 #ifndef DT_FINI_ARRAYSZ
145 #define DT_FINI_ARRAYSZ 28
148 #define DT_RELACOUNT 0x6ffffff9
151 #define DT_RELCOUNT 0x6ffffffa
154 #define DT_VERSYM 0x6ffffff0
157 #define DT_VERDEF 0x6ffffffc
160 #define DT_VERDEFNUM 0x6ffffffd
163 #define DT_VERNEED 0x6ffffffe
165 #ifndef DT_VERNEEDNUM
166 #define DT_VERNEEDNUM 0x6fffffff
169 #define DT_FLAGS_1 0x6ffffffb
175 #define DF_SYMBOLIC 0x00000002
178 #define DF_TEXTREL 0x00000004
184 * Define a few basic Elf Types
186 typedef Elf_(Phdr
) Phdr
;
187 typedef Elf_(Dyn
) Dyn
;
188 typedef Elf_(Sym
) Sym
;
189 typedef Elf_(Addr
) Addr
;
190 typedef Elf_(Word
) Word
;
191 typedef Elf_(Half
) Half
;
194 * Helper class around the standard Elf header struct
196 struct Ehdr
: public Elf_(Ehdr
)
199 * Equivalent to reinterpret_cast<const Ehdr *>(buf), but additionally
200 * checking that this is indeed an Elf header and that the Elf type
201 * corresponds to that of the system
203 static const Ehdr
*validate(const void *buf
);
209 class Strtab
: public UnsizedArray
<const char>
213 * Returns the string at the given index in the table
215 const char *GetStringAt(off_t index
) const
217 return &UnsizedArray
<const char>::operator[](index
);
222 * Helper class around Elf relocation.
224 struct Rel
: public Elf_(Rel
)
227 * Returns the addend for the relocation, which is the value stored
230 Addr
GetAddend(void *base
) const
232 return *(reinterpret_cast<const Addr
*>(
233 reinterpret_cast<const char *>(base
) + r_offset
));
238 * Helper class around Elf relocation with addend.
240 struct Rela
: public Elf_(Rela
)
243 * Returns the addend for the relocation.
245 Addr
GetAddend(void *base
) const
251 } /* namespace Elf */