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/. */
11 * Android system headers have two different elf.h file. The one under linux/
12 * is the most complete on older Android API versions without unified headers.
14 #if defined(ANDROID) && __ANDROID_API__ < 21 && !defined(__ANDROID_API_L__)
15 # include <linux/elf.h>
21 #if defined(__ARM_EABI__) && !defined(PT_ARM_EXIDX)
22 # define PT_ARM_EXIDX 0x70000001
26 * Generic ELF macros for the target system
29 # define Elf_(type) Elf64_##type
30 # define ELFCLASS ELFCLASS64
31 # define ELF_R_TYPE ELF64_R_TYPE
32 # define ELF_R_SYM ELF64_R_SYM
34 # define ELF_ST_BIND ELF64_ST_BIND
37 # define Elf_(type) Elf32_##type
38 # define ELFCLASS ELFCLASS32
39 # define ELF_R_TYPE ELF32_R_TYPE
40 # define ELF_R_SYM ELF32_R_SYM
42 # define ELF_ST_BIND ELF32_ST_BIND
47 # error Cannot find endianness
50 #if __BYTE_ORDER == __LITTLE_ENDIAN
51 # define ELFDATA ELFDATA2LSB
52 #elif __BYTE_ORDER == __BIG_ENDIAN
53 # define ELFDATA ELFDATA2MSB
57 # define ELFOSABI ELFOSABI_LINUX
59 # define ELFABIVERSION 0
62 # error Unknown ELF OSABI
66 # define ELFMACHINE EM_386
68 // Doing this way probably doesn't scale to other architectures
69 # define R_ABS R_386_32
70 # define R_GLOB_DAT R_386_GLOB_DAT
71 # define R_JMP_SLOT R_386_JMP_SLOT
72 # define R_RELATIVE R_386_RELATIVE
73 # define RELOC(n) DT_REL##n
74 # define UNSUPPORTED_RELOC(n) DT_RELA##n
75 # define STR_RELOC(n) "DT_REL" #n
78 #elif defined(__x86_64__)
79 # define ELFMACHINE EM_X86_64
81 # define R_ABS R_X86_64_64
82 # define R_GLOB_DAT R_X86_64_GLOB_DAT
83 # define R_JMP_SLOT R_X86_64_JUMP_SLOT
84 # define R_RELATIVE R_X86_64_RELATIVE
85 # define RELOC(n) DT_RELA##n
86 # define UNSUPPORTED_RELOC(n) DT_REL##n
87 # define STR_RELOC(n) "DT_RELA" #n
90 #elif defined(__arm__)
91 # define ELFMACHINE EM_ARM
94 # define R_ARM_ABS32 2
96 # ifndef R_ARM_GLOB_DAT
97 # define R_ARM_GLOB_DAT 21
99 # ifndef R_ARM_JUMP_SLOT
100 # define R_ARM_JUMP_SLOT 22
102 # ifndef R_ARM_RELATIVE
103 # define R_ARM_RELATIVE 23
106 # define R_ABS R_ARM_ABS32
107 # define R_GLOB_DAT R_ARM_GLOB_DAT
108 # define R_JMP_SLOT R_ARM_JUMP_SLOT
109 # define R_RELATIVE R_ARM_RELATIVE
110 # define RELOC(n) DT_REL##n
111 # define UNSUPPORTED_RELOC(n) DT_RELA##n
112 # define STR_RELOC(n) "DT_REL" #n
115 #elif defined(__aarch64__)
116 # define ELFMACHINE EM_AARCH64
118 # define R_ABS R_AARCH64_ABS64
119 # define R_GLOB_DAT R_AARCH64_GLOB_DAT
120 # define R_JMP_SLOT R_AARCH64_JUMP_SLOT
121 # define R_RELATIVE R_AARCH64_RELATIVE
122 # define RELOC(n) DT_RELA##n
123 # define UNSUPPORTED_RELOC(n) DT_REL##n
124 # define STR_RELOC(n) "DT_RELA" #n
128 # error Unknown ELF machine type
132 * Android system headers don't have all definitions
137 #ifndef DT_INIT_ARRAY
138 # define DT_INIT_ARRAY 25
140 #ifndef DT_FINI_ARRAY
141 # define DT_FINI_ARRAY 26
143 #ifndef DT_INIT_ARRAYSZ
144 # define DT_INIT_ARRAYSZ 27
146 #ifndef DT_FINI_ARRAYSZ
147 # define DT_FINI_ARRAYSZ 28
150 # define DT_RELACOUNT 0x6ffffff9
153 # define DT_RELCOUNT 0x6ffffffa
156 # define DT_VERSYM 0x6ffffff0
159 # define DT_VERDEF 0x6ffffffc
162 # define DT_VERDEFNUM 0x6ffffffd
165 # define DT_VERNEED 0x6ffffffe
167 #ifndef DT_VERNEEDNUM
168 # define DT_VERNEEDNUM 0x6fffffff
171 # define DT_FLAGS_1 0x6ffffffb
177 # define DF_SYMBOLIC 0x00000002
180 # define DF_TEXTREL 0x00000004
186 * Define a few basic Elf Types
188 typedef Elf_(Phdr
) Phdr
;
189 typedef Elf_(Dyn
) Dyn
;
190 typedef Elf_(Sym
) Sym
;
191 typedef Elf_(Addr
) Addr
;
192 typedef Elf_(Word
) Word
;
193 typedef Elf_(Half
) Half
;
196 * Helper class around the standard Elf header struct
198 struct Ehdr
: public Elf_(Ehdr
) {
200 * Equivalent to reinterpret_cast<const Ehdr *>(buf), but additionally
201 * checking that this is indeed an Elf header and that the Elf type
202 * corresponds to that of the system
204 static const Ehdr
* validate(const void* buf
);
210 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 {
216 return &UnsizedArray
<const char>::operator[](index
);
221 * Helper class around Elf relocation.
223 struct Rel
: public Elf_(Rel
) {
225 * Returns the addend for the relocation, which is the value stored
228 Addr
GetAddend(void* base
) const {
229 return *(reinterpret_cast<const Addr
*>(reinterpret_cast<const char*>(base
) +
235 * Helper class around Elf relocation with addend.
237 struct Rela
: public Elf_(Rela
) {
239 * Returns the addend for the relocation.
241 Addr
GetAddend(void* base
) const { return r_addend
; }
244 } /* namespace Elf */