Bug 1473441 [wpt PR 11792] - [css-properties-values-api] Require PropertyDescriptor...
[gecko.git] / mozglue / linker / Elfxx.h
blob6b21a8b7deb2b178943713b1e35ca12f62712718
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/. */
5 #ifndef Elfxx_h
6 #define Elfxx_h
8 /**
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>
14 #else
15 #include <elf.h>
16 #endif
17 #include <endian.h>
19 #if defined(__ARM_EABI__) && !defined(PT_ARM_EXIDX)
20 #define PT_ARM_EXIDX 0x70000001
21 #endif
23 /**
24 * Generic ELF macros for the target system
26 #ifdef __LP64__
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
31 #ifndef ELF_ST_BIND
32 #define ELF_ST_BIND ELF64_ST_BIND
33 #endif
34 #else
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
39 #ifndef ELF_ST_BIND
40 #define ELF_ST_BIND ELF32_ST_BIND
41 #endif
42 #endif
44 #ifndef __BYTE_ORDER
45 #error Cannot find endianness
46 #endif
48 #if __BYTE_ORDER == __LITTLE_ENDIAN
49 #define ELFDATA ELFDATA2LSB
50 #elif __BYTE_ORDER == __BIG_ENDIAN
51 #define ELFDATA ELFDATA2MSB
52 #endif
54 #ifdef __linux__
55 #define ELFOSABI ELFOSABI_LINUX
56 #ifdef EI_ABIVERSION
57 #define ELFABIVERSION 0
58 #endif
59 #else
60 #error Unknown ELF OSABI
61 #endif
63 #if defined(__i386__)
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
74 #define Reloc Rel
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
86 #define Reloc Rela
88 #elif defined(__arm__)
89 #define ELFMACHINE EM_ARM
91 #ifndef R_ARM_ABS32
92 #define R_ARM_ABS32 2
93 #endif
94 #ifndef R_ARM_GLOB_DAT
95 #define R_ARM_GLOB_DAT 21
96 #endif
97 #ifndef R_ARM_JUMP_SLOT
98 #define R_ARM_JUMP_SLOT 22
99 #endif
100 #ifndef R_ARM_RELATIVE
101 #define R_ARM_RELATIVE 23
102 #endif
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
111 #define Reloc Rel
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
123 #define Reloc Rela
125 #else
126 #error Unknown ELF machine type
127 #endif
130 * Android system headers don't have all definitions
132 #ifndef STN_UNDEF
133 #define STN_UNDEF 0
134 #endif
135 #ifndef DT_INIT_ARRAY
136 #define DT_INIT_ARRAY 25
137 #endif
138 #ifndef DT_FINI_ARRAY
139 #define DT_FINI_ARRAY 26
140 #endif
141 #ifndef DT_INIT_ARRAYSZ
142 #define DT_INIT_ARRAYSZ 27
143 #endif
144 #ifndef DT_FINI_ARRAYSZ
145 #define DT_FINI_ARRAYSZ 28
146 #endif
147 #ifndef DT_RELACOUNT
148 #define DT_RELACOUNT 0x6ffffff9
149 #endif
150 #ifndef DT_RELCOUNT
151 #define DT_RELCOUNT 0x6ffffffa
152 #endif
153 #ifndef DT_VERSYM
154 #define DT_VERSYM 0x6ffffff0
155 #endif
156 #ifndef DT_VERDEF
157 #define DT_VERDEF 0x6ffffffc
158 #endif
159 #ifndef DT_VERDEFNUM
160 #define DT_VERDEFNUM 0x6ffffffd
161 #endif
162 #ifndef DT_VERNEED
163 #define DT_VERNEED 0x6ffffffe
164 #endif
165 #ifndef DT_VERNEEDNUM
166 #define DT_VERNEEDNUM 0x6fffffff
167 #endif
168 #ifndef DT_FLAGS_1
169 #define DT_FLAGS_1 0x6ffffffb
170 #endif
171 #ifndef DT_FLAGS
172 #define DT_FLAGS 30
173 #endif
174 #ifndef DF_SYMBOLIC
175 #define DF_SYMBOLIC 0x00000002
176 #endif
177 #ifndef DF_TEXTREL
178 #define DF_TEXTREL 0x00000004
179 #endif
181 namespace Elf {
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);
207 * Elf String table
209 class Strtab: public UnsizedArray<const char>
211 public:
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
228 * at r_offset.
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
247 return r_addend;
251 } /* namespace Elf */
253 #endif /* Elfxx_h */