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/. */
14 * Generic ELF macros for the target system
17 # define Elf_(type) Elf64_##type
18 # define ELFCLASS ELFCLASS64
19 # define ELF_R_TYPE ELF64_R_TYPE
20 # define ELF_R_SYM ELF64_R_SYM
22 # define ELF_ST_BIND ELF64_ST_BIND
25 # define Elf_(type) Elf32_##type
26 # define ELFCLASS ELFCLASS32
27 # define ELF_R_TYPE ELF32_R_TYPE
28 # define ELF_R_SYM ELF32_R_SYM
30 # define ELF_ST_BIND ELF32_ST_BIND
35 # error Cannot find endianness
38 #if __BYTE_ORDER == __LITTLE_ENDIAN
39 # define ELFDATA ELFDATA2LSB
40 #elif __BYTE_ORDER == __BIG_ENDIAN
41 # define ELFDATA ELFDATA2MSB
45 # define ELFOSABI ELFOSABI_LINUX
47 # define ELFABIVERSION 0
50 # error Unknown ELF OSABI
54 # define ELFMACHINE EM_386
56 // Doing this way probably doesn't scale to other architectures
57 # define R_ABS R_386_32
58 # define R_GLOB_DAT R_386_GLOB_DAT
59 # define R_JMP_SLOT R_386_JMP_SLOT
60 # define R_RELATIVE R_386_RELATIVE
61 # define RELOC(n) DT_REL##n
62 # define UNSUPPORTED_RELOC(n) DT_RELA##n
63 # define STR_RELOC(n) "DT_REL" #n
66 #elif defined(__x86_64__)
67 # define ELFMACHINE EM_X86_64
69 # define R_ABS R_X86_64_64
70 # define R_GLOB_DAT R_X86_64_GLOB_DAT
71 # define R_JMP_SLOT R_X86_64_JUMP_SLOT
72 # define R_RELATIVE R_X86_64_RELATIVE
73 # define RELOC(n) DT_RELA##n
74 # define UNSUPPORTED_RELOC(n) DT_REL##n
75 # define STR_RELOC(n) "DT_RELA" #n
78 #elif defined(__arm__)
79 # define ELFMACHINE EM_ARM
82 # define R_ARM_ABS32 2
84 # ifndef R_ARM_GLOB_DAT
85 # define R_ARM_GLOB_DAT 21
87 # ifndef R_ARM_JUMP_SLOT
88 # define R_ARM_JUMP_SLOT 22
90 # ifndef R_ARM_RELATIVE
91 # define R_ARM_RELATIVE 23
94 # define R_ABS R_ARM_ABS32
95 # define R_GLOB_DAT R_ARM_GLOB_DAT
96 # define R_JMP_SLOT R_ARM_JUMP_SLOT
97 # define R_RELATIVE R_ARM_RELATIVE
98 # define RELOC(n) DT_REL##n
99 # define UNSUPPORTED_RELOC(n) DT_RELA##n
100 # define STR_RELOC(n) "DT_REL" #n
103 #elif defined(__aarch64__)
104 # define ELFMACHINE EM_AARCH64
106 # define R_ABS R_AARCH64_ABS64
107 # define R_GLOB_DAT R_AARCH64_GLOB_DAT
108 # define R_JMP_SLOT R_AARCH64_JUMP_SLOT
109 # define R_RELATIVE R_AARCH64_RELATIVE
110 # define RELOC(n) DT_RELA##n
111 # define UNSUPPORTED_RELOC(n) DT_REL##n
112 # define STR_RELOC(n) "DT_RELA" #n
116 # error Unknown ELF machine type
122 * Define a few basic Elf Types
124 typedef Elf_(Phdr
) Phdr
;
125 typedef Elf_(Dyn
) Dyn
;
126 typedef Elf_(Sym
) Sym
;
127 typedef Elf_(Addr
) Addr
;
128 typedef Elf_(Word
) Word
;
129 typedef Elf_(Half
) Half
;
132 * Helper class around the standard Elf header struct
134 struct Ehdr
: public Elf_(Ehdr
) {
136 * Equivalent to reinterpret_cast<const Ehdr *>(buf), but additionally
137 * checking that this is indeed an Elf header and that the Elf type
138 * corresponds to that of the system
140 static const Ehdr
* validate(const void* buf
);
146 class Strtab
: public UnsizedArray
<const char> {
149 * Returns the string at the given index in the table
151 const char* GetStringAt(off_t index
) const {
152 return &UnsizedArray
<const char>::operator[](index
);
157 * Helper class around Elf relocation.
160 : public Elf_(Rel
){/**
161 * Returns the addend for the relocation, which is the
162 * value stored at r_offset.
164 Addr
GetAddend(void* base
)
165 const {return *(reinterpret_cast<const Addr
*>(
166 reinterpret_cast<const char*>(base
) + r_offset
));
172 * Helper class around Elf relocation with addend.
175 : public Elf_(Rela
){/**
176 * Returns the addend for the relocation.
178 Addr
GetAddend(void* base
) const {return r_addend
;
183 } /* namespace Elf */