/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Object/ELFTypes.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===- ELFTypes.h - Endian specific types for ELF ---------------*- C++ -*-===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #ifndef LLVM_OBJECT_ELFTYPES_H |
10 | | #define LLVM_OBJECT_ELFTYPES_H |
11 | | |
12 | | #include "llvm/ADT/ArrayRef.h" |
13 | | #include "llvm/ADT/StringRef.h" |
14 | | #include "llvm/BinaryFormat/ELF.h" |
15 | | #include "llvm/Object/Error.h" |
16 | | #include "llvm/Support/Endian.h" |
17 | | #include "llvm/Support/Error.h" |
18 | | #include <cassert> |
19 | | #include <cstdint> |
20 | | #include <cstring> |
21 | | #include <type_traits> |
22 | | |
23 | | namespace llvm { |
24 | | namespace object { |
25 | | |
26 | | using support::endianness; |
27 | | |
28 | | template <class ELFT> struct Elf_Ehdr_Impl; |
29 | | template <class ELFT> struct Elf_Shdr_Impl; |
30 | | template <class ELFT> struct Elf_Sym_Impl; |
31 | | template <class ELFT> struct Elf_Dyn_Impl; |
32 | | template <class ELFT> struct Elf_Phdr_Impl; |
33 | | template <class ELFT, bool isRela> struct Elf_Rel_Impl; |
34 | | template <class ELFT> struct Elf_Verdef_Impl; |
35 | | template <class ELFT> struct Elf_Verdaux_Impl; |
36 | | template <class ELFT> struct Elf_Verneed_Impl; |
37 | | template <class ELFT> struct Elf_Vernaux_Impl; |
38 | | template <class ELFT> struct Elf_Versym_Impl; |
39 | | template <class ELFT> struct Elf_Hash_Impl; |
40 | | template <class ELFT> struct Elf_GnuHash_Impl; |
41 | | template <class ELFT> struct Elf_Chdr_Impl; |
42 | | template <class ELFT> struct Elf_Nhdr_Impl; |
43 | | template <class ELFT> class Elf_Note_Impl; |
44 | | template <class ELFT> class Elf_Note_Iterator_Impl; |
45 | | template <class ELFT> struct Elf_CGProfile_Impl; |
46 | | |
47 | | template <endianness E, bool Is64> struct ELFType { |
48 | | private: |
49 | | template <typename Ty> |
50 | | using packed = support::detail::packed_endian_specific_integral<Ty, E, 1>; |
51 | | |
52 | | public: |
53 | | static const endianness TargetEndianness = E; |
54 | | static const bool Is64Bits = Is64; |
55 | | |
56 | | using uint = typename std::conditional<Is64, uint64_t, uint32_t>::type; |
57 | | using Ehdr = Elf_Ehdr_Impl<ELFType<E, Is64>>; |
58 | | using Shdr = Elf_Shdr_Impl<ELFType<E, Is64>>; |
59 | | using Sym = Elf_Sym_Impl<ELFType<E, Is64>>; |
60 | | using Dyn = Elf_Dyn_Impl<ELFType<E, Is64>>; |
61 | | using Phdr = Elf_Phdr_Impl<ELFType<E, Is64>>; |
62 | | using Rel = Elf_Rel_Impl<ELFType<E, Is64>, false>; |
63 | | using Rela = Elf_Rel_Impl<ELFType<E, Is64>, true>; |
64 | | using Relr = packed<uint>; |
65 | | using Verdef = Elf_Verdef_Impl<ELFType<E, Is64>>; |
66 | | using Verdaux = Elf_Verdaux_Impl<ELFType<E, Is64>>; |
67 | | using Verneed = Elf_Verneed_Impl<ELFType<E, Is64>>; |
68 | | using Vernaux = Elf_Vernaux_Impl<ELFType<E, Is64>>; |
69 | | using Versym = Elf_Versym_Impl<ELFType<E, Is64>>; |
70 | | using Hash = Elf_Hash_Impl<ELFType<E, Is64>>; |
71 | | using GnuHash = Elf_GnuHash_Impl<ELFType<E, Is64>>; |
72 | | using Chdr = Elf_Chdr_Impl<ELFType<E, Is64>>; |
73 | | using Nhdr = Elf_Nhdr_Impl<ELFType<E, Is64>>; |
74 | | using Note = Elf_Note_Impl<ELFType<E, Is64>>; |
75 | | using NoteIterator = Elf_Note_Iterator_Impl<ELFType<E, Is64>>; |
76 | | using CGProfile = Elf_CGProfile_Impl<ELFType<E, Is64>>; |
77 | | using DynRange = ArrayRef<Dyn>; |
78 | | using ShdrRange = ArrayRef<Shdr>; |
79 | | using SymRange = ArrayRef<Sym>; |
80 | | using RelRange = ArrayRef<Rel>; |
81 | | using RelaRange = ArrayRef<Rela>; |
82 | | using RelrRange = ArrayRef<Relr>; |
83 | | using PhdrRange = ArrayRef<Phdr>; |
84 | | |
85 | | using Half = packed<uint16_t>; |
86 | | using Word = packed<uint32_t>; |
87 | | using Sword = packed<int32_t>; |
88 | | using Xword = packed<uint64_t>; |
89 | | using Sxword = packed<int64_t>; |
90 | | using Addr = packed<uint>; |
91 | | using Off = packed<uint>; |
92 | | }; |
93 | | |
94 | | using ELF32LE = ELFType<support::little, false>; |
95 | | using ELF32BE = ELFType<support::big, false>; |
96 | | using ELF64LE = ELFType<support::little, true>; |
97 | | using ELF64BE = ELFType<support::big, true>; |
98 | | |
99 | | // Use an alignment of 2 for the typedefs since that is the worst case for |
100 | | // ELF files in archives. |
101 | | |
102 | | // I really don't like doing this, but the alternative is copypasta. |
103 | | #define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) \ |
104 | | using Elf_Addr = typename ELFT::Addr; \ |
105 | | using Elf_Off = typename ELFT::Off; \ |
106 | | using Elf_Half = typename ELFT::Half; \ |
107 | | using Elf_Word = typename ELFT::Word; \ |
108 | | using Elf_Sword = typename ELFT::Sword; \ |
109 | | using Elf_Xword = typename ELFT::Xword; \ |
110 | | using Elf_Sxword = typename ELFT::Sxword; |
111 | | |
112 | | #define LLVM_ELF_COMMA , |
113 | | #define LLVM_ELF_IMPORT_TYPES(E, W) \ |
114 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFType<E LLVM_ELF_COMMA W>) |
115 | | |
116 | | // Section header. |
117 | | template <class ELFT> struct Elf_Shdr_Base; |
118 | | |
119 | | template <endianness TargetEndianness> |
120 | | struct Elf_Shdr_Base<ELFType<TargetEndianness, false>> { |
121 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) |
122 | | Elf_Word sh_name; // Section name (index into string table) |
123 | | Elf_Word sh_type; // Section type (SHT_*) |
124 | | Elf_Word sh_flags; // Section flags (SHF_*) |
125 | | Elf_Addr sh_addr; // Address where section is to be loaded |
126 | | Elf_Off sh_offset; // File offset of section data, in bytes |
127 | | Elf_Word sh_size; // Size of section, in bytes |
128 | | Elf_Word sh_link; // Section type-specific header table index link |
129 | | Elf_Word sh_info; // Section type-specific extra information |
130 | | Elf_Word sh_addralign; // Section address alignment |
131 | | Elf_Word sh_entsize; // Size of records contained within the section |
132 | | }; |
133 | | |
134 | | template <endianness TargetEndianness> |
135 | | struct Elf_Shdr_Base<ELFType<TargetEndianness, true>> { |
136 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) |
137 | | Elf_Word sh_name; // Section name (index into string table) |
138 | | Elf_Word sh_type; // Section type (SHT_*) |
139 | | Elf_Xword sh_flags; // Section flags (SHF_*) |
140 | | Elf_Addr sh_addr; // Address where section is to be loaded |
141 | | Elf_Off sh_offset; // File offset of section data, in bytes |
142 | | Elf_Xword sh_size; // Size of section, in bytes |
143 | | Elf_Word sh_link; // Section type-specific header table index link |
144 | | Elf_Word sh_info; // Section type-specific extra information |
145 | | Elf_Xword sh_addralign; // Section address alignment |
146 | | Elf_Xword sh_entsize; // Size of records contained within the section |
147 | | }; |
148 | | |
149 | | template <class ELFT> |
150 | | struct Elf_Shdr_Impl : Elf_Shdr_Base<ELFT> { |
151 | | using Elf_Shdr_Base<ELFT>::sh_entsize; |
152 | | using Elf_Shdr_Base<ELFT>::sh_size; |
153 | | |
154 | | /// Get the number of entities this section contains if it has any. |
155 | | unsigned getEntityCount() const { |
156 | | if (sh_entsize == 0) |
157 | | return 0; |
158 | | return sh_size / sh_entsize; |
159 | | } |
160 | | }; |
161 | | |
162 | | template <class ELFT> struct Elf_Sym_Base; |
163 | | |
164 | | template <endianness TargetEndianness> |
165 | | struct Elf_Sym_Base<ELFType<TargetEndianness, false>> { |
166 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) |
167 | | Elf_Word st_name; // Symbol name (index into string table) |
168 | | Elf_Addr st_value; // Value or address associated with the symbol |
169 | | Elf_Word st_size; // Size of the symbol |
170 | | unsigned char st_info; // Symbol's type and binding attributes |
171 | | unsigned char st_other; // Must be zero; reserved |
172 | | Elf_Half st_shndx; // Which section (header table index) it's defined in |
173 | | }; |
174 | | |
175 | | template <endianness TargetEndianness> |
176 | | struct Elf_Sym_Base<ELFType<TargetEndianness, true>> { |
177 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) |
178 | | Elf_Word st_name; // Symbol name (index into string table) |
179 | | unsigned char st_info; // Symbol's type and binding attributes |
180 | | unsigned char st_other; // Must be zero; reserved |
181 | | Elf_Half st_shndx; // Which section (header table index) it's defined in |
182 | | Elf_Addr st_value; // Value or address associated with the symbol |
183 | | Elf_Xword st_size; // Size of the symbol |
184 | | }; |
185 | | |
186 | | template <class ELFT> |
187 | | struct Elf_Sym_Impl : Elf_Sym_Base<ELFT> { |
188 | | using Elf_Sym_Base<ELFT>::st_info; |
189 | | using Elf_Sym_Base<ELFT>::st_shndx; |
190 | | using Elf_Sym_Base<ELFT>::st_other; |
191 | | using Elf_Sym_Base<ELFT>::st_value; |
192 | | |
193 | | // These accessors and mutators correspond to the ELF32_ST_BIND, |
194 | | // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification: |
195 | 1.24M | unsigned char getBinding() const { return st_info >> 4; } llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getBinding() const Line | Count | Source | 195 | 34.2k | unsigned char getBinding() const { return st_info >> 4; } |
llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getBinding() const Line | Count | Source | 195 | 5.65k | unsigned char getBinding() const { return st_info >> 4; } |
llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getBinding() const Line | Count | Source | 195 | 1.17M | unsigned char getBinding() const { return st_info >> 4; } |
llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getBinding() const Line | Count | Source | 195 | 23.9k | unsigned char getBinding() const { return st_info >> 4; } |
|
196 | 1.53M | unsigned char getType() const { return st_info & 0x0f; } llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getType() const Line | Count | Source | 196 | 56.3k | unsigned char getType() const { return st_info & 0x0f; } |
llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getType() const Line | Count | Source | 196 | 6.92k | unsigned char getType() const { return st_info & 0x0f; } |
llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getType() const Line | Count | Source | 196 | 1.45M | unsigned char getType() const { return st_info & 0x0f; } |
llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getType() const Line | Count | Source | 196 | 14.9k | unsigned char getType() const { return st_info & 0x0f; } |
|
197 | | uint64_t getValue() const { return st_value; } |
198 | | void setBinding(unsigned char b) { setBindingAndType(b, getType()); } |
199 | | void setType(unsigned char t) { setBindingAndType(getBinding(), t); } |
200 | | |
201 | | void setBindingAndType(unsigned char b, unsigned char t) { |
202 | | st_info = (b << 4) + (t & 0x0f); |
203 | | } |
204 | | |
205 | | /// Access to the STV_xxx flag stored in the first two bits of st_other. |
206 | | /// STV_DEFAULT: 0 |
207 | | /// STV_INTERNAL: 1 |
208 | | /// STV_HIDDEN: 2 |
209 | | /// STV_PROTECTED: 3 |
210 | 227k | unsigned char getVisibility() const { return st_other & 0x3; } llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getVisibility() const Line | Count | Source | 210 | 18.4k | unsigned char getVisibility() const { return st_other & 0x3; } |
llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getVisibility() const Line | Count | Source | 210 | 2.40k | unsigned char getVisibility() const { return st_other & 0x3; } |
llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getVisibility() const Line | Count | Source | 210 | 205k | unsigned char getVisibility() const { return st_other & 0x3; } |
llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getVisibility() const Line | Count | Source | 210 | 1.67k | unsigned char getVisibility() const { return st_other & 0x3; } |
|
211 | | void setVisibility(unsigned char v) { |
212 | | assert(v < 4 && "Invalid value for visibility"); |
213 | | st_other = (st_other & ~0x3) | v; |
214 | | } |
215 | | |
216 | | bool isAbsolute() const { return st_shndx == ELF::SHN_ABS; } |
217 | | |
218 | | bool isCommon() const { |
219 | | return getType() == ELF::STT_COMMON || st_shndx == ELF::SHN_COMMON; |
220 | | } |
221 | | |
222 | | bool isDefined() const { return !isUndefined(); } |
223 | | |
224 | | bool isProcessorSpecific() const { |
225 | | return st_shndx >= ELF::SHN_LOPROC && st_shndx <= ELF::SHN_HIPROC; |
226 | | } |
227 | | |
228 | | bool isOSSpecific() const { |
229 | | return st_shndx >= ELF::SHN_LOOS && st_shndx <= ELF::SHN_HIOS; |
230 | | } |
231 | | |
232 | | bool isReserved() const { |
233 | | // ELF::SHN_HIRESERVE is 0xffff so st_shndx <= ELF::SHN_HIRESERVE is always |
234 | | // true and some compilers warn about it. |
235 | | return st_shndx >= ELF::SHN_LORESERVE; |
236 | | } |
237 | | |
238 | | bool isUndefined() const { return st_shndx == ELF::SHN_UNDEF; } |
239 | | |
240 | | bool isExternal() const { |
241 | | return getBinding() != ELF::STB_LOCAL; |
242 | | } |
243 | | |
244 | | Expected<StringRef> getName(StringRef StrTab) const; |
245 | | }; |
246 | | |
247 | | template <class ELFT> |
248 | 655k | Expected<StringRef> Elf_Sym_Impl<ELFT>::getName(StringRef StrTab) const { |
249 | 655k | uint32_t Offset = this->st_name; |
250 | 655k | if (Offset >= StrTab.size()) |
251 | 1 | return errorCodeToError(object_error::parse_failed); |
252 | 655k | return StringRef(StrTab.data() + Offset); |
253 | 655k | } llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getName(llvm::StringRef) const Line | Count | Source | 248 | 18.1k | Expected<StringRef> Elf_Sym_Impl<ELFT>::getName(StringRef StrTab) const { | 249 | 18.1k | uint32_t Offset = this->st_name; | 250 | 18.1k | if (Offset >= StrTab.size()) | 251 | 0 | return errorCodeToError(object_error::parse_failed); | 252 | 18.1k | return StringRef(StrTab.data() + Offset); | 253 | 18.1k | } |
llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getName(llvm::StringRef) const Line | Count | Source | 248 | 1.75k | Expected<StringRef> Elf_Sym_Impl<ELFT>::getName(StringRef StrTab) const { | 249 | 1.75k | uint32_t Offset = this->st_name; | 250 | 1.75k | if (Offset >= StrTab.size()) | 251 | 0 | return errorCodeToError(object_error::parse_failed); | 252 | 1.75k | return StringRef(StrTab.data() + Offset); | 253 | 1.75k | } |
llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getName(llvm::StringRef) const Line | Count | Source | 248 | 624k | Expected<StringRef> Elf_Sym_Impl<ELFT>::getName(StringRef StrTab) const { | 249 | 624k | uint32_t Offset = this->st_name; | 250 | 624k | if (Offset >= StrTab.size()) | 251 | 1 | return errorCodeToError(object_error::parse_failed); | 252 | 624k | return StringRef(StrTab.data() + Offset); | 253 | 624k | } |
llvm::object::Elf_Sym_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getName(llvm::StringRef) const Line | Count | Source | 248 | 11.4k | Expected<StringRef> Elf_Sym_Impl<ELFT>::getName(StringRef StrTab) const { | 249 | 11.4k | uint32_t Offset = this->st_name; | 250 | 11.4k | if (Offset >= StrTab.size()) | 251 | 0 | return errorCodeToError(object_error::parse_failed); | 252 | 11.4k | return StringRef(StrTab.data() + Offset); | 253 | 11.4k | } |
|
254 | | |
255 | | /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section |
256 | | /// (.gnu.version). This structure is identical for ELF32 and ELF64. |
257 | | template <class ELFT> |
258 | | struct Elf_Versym_Impl { |
259 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
260 | | Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN) |
261 | | }; |
262 | | |
263 | | /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section |
264 | | /// (.gnu.version_d). This structure is identical for ELF32 and ELF64. |
265 | | template <class ELFT> |
266 | | struct Elf_Verdef_Impl { |
267 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
268 | | using Elf_Verdaux = Elf_Verdaux_Impl<ELFT>; |
269 | | Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT) |
270 | | Elf_Half vd_flags; // Bitwise flags (VER_DEF_*) |
271 | | Elf_Half vd_ndx; // Version index, used in .gnu.version entries |
272 | | Elf_Half vd_cnt; // Number of Verdaux entries |
273 | | Elf_Word vd_hash; // Hash of name |
274 | | Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes) |
275 | | Elf_Word vd_next; // Offset to the next Verdef entry (in bytes) |
276 | | |
277 | | /// Get the first Verdaux entry for this Verdef. |
278 | | const Elf_Verdaux *getAux() const { |
279 | | return reinterpret_cast<const Elf_Verdaux *>((const char *)this + vd_aux); |
280 | | } |
281 | | }; |
282 | | |
283 | | /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef |
284 | | /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64. |
285 | | template <class ELFT> |
286 | | struct Elf_Verdaux_Impl { |
287 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
288 | | Elf_Word vda_name; // Version name (offset in string table) |
289 | | Elf_Word vda_next; // Offset to next Verdaux entry (in bytes) |
290 | | }; |
291 | | |
292 | | /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed |
293 | | /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64. |
294 | | template <class ELFT> |
295 | | struct Elf_Verneed_Impl { |
296 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
297 | | Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT) |
298 | | Elf_Half vn_cnt; // Number of associated Vernaux entries |
299 | | Elf_Word vn_file; // Library name (string table offset) |
300 | | Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes) |
301 | | Elf_Word vn_next; // Offset to next Verneed entry (in bytes) |
302 | | }; |
303 | | |
304 | | /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed |
305 | | /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64. |
306 | | template <class ELFT> |
307 | | struct Elf_Vernaux_Impl { |
308 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
309 | | Elf_Word vna_hash; // Hash of dependency name |
310 | | Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*) |
311 | | Elf_Half vna_other; // Version index, used in .gnu.version entries |
312 | | Elf_Word vna_name; // Dependency name |
313 | | Elf_Word vna_next; // Offset to next Vernaux entry (in bytes) |
314 | | }; |
315 | | |
316 | | /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic |
317 | | /// table section (.dynamic) look like. |
318 | | template <class ELFT> struct Elf_Dyn_Base; |
319 | | |
320 | | template <endianness TargetEndianness> |
321 | | struct Elf_Dyn_Base<ELFType<TargetEndianness, false>> { |
322 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) |
323 | | Elf_Sword d_tag; |
324 | | union { |
325 | | Elf_Word d_val; |
326 | | Elf_Addr d_ptr; |
327 | | } d_un; |
328 | | }; |
329 | | |
330 | | template <endianness TargetEndianness> |
331 | | struct Elf_Dyn_Base<ELFType<TargetEndianness, true>> { |
332 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) |
333 | | Elf_Sxword d_tag; |
334 | | union { |
335 | | Elf_Xword d_val; |
336 | | Elf_Addr d_ptr; |
337 | | } d_un; |
338 | | }; |
339 | | |
340 | | /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters. |
341 | | template <class ELFT> |
342 | | struct Elf_Dyn_Impl : Elf_Dyn_Base<ELFT> { |
343 | | using Elf_Dyn_Base<ELFT>::d_tag; |
344 | | using Elf_Dyn_Base<ELFT>::d_un; |
345 | | using intX_t = typename std::conditional<ELFT::Is64Bits, |
346 | | int64_t, int32_t>::type; |
347 | | using uintX_t = typename std::conditional<ELFT::Is64Bits, |
348 | | uint64_t, uint32_t>::type; |
349 | | intX_t getTag() const { return d_tag; } |
350 | | uintX_t getVal() const { return d_un.d_val; } |
351 | | uintX_t getPtr() const { return d_un.d_ptr; } |
352 | | }; |
353 | | |
354 | | template <endianness TargetEndianness> |
355 | | struct Elf_Rel_Impl<ELFType<TargetEndianness, false>, false> { |
356 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) |
357 | | static const bool IsRela = false; |
358 | | Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) |
359 | | Elf_Word r_info; // Symbol table index and type of relocation to apply |
360 | | |
361 | 13.3k | uint32_t getRInfo(bool isMips64EL) const { |
362 | 13.3k | assert(!isMips64EL); |
363 | 13.3k | return r_info; |
364 | 13.3k | } llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false>::getRInfo(bool) const Line | Count | Source | 361 | 9.87k | uint32_t getRInfo(bool isMips64EL) const { | 362 | 9.87k | assert(!isMips64EL); | 363 | 9.87k | return r_info; | 364 | 9.87k | } |
llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false>::getRInfo(bool) const Line | Count | Source | 361 | 3.50k | uint32_t getRInfo(bool isMips64EL) const { | 362 | 3.50k | assert(!isMips64EL); | 363 | 3.50k | return r_info; | 364 | 3.50k | } |
|
365 | 689 | void setRInfo(uint32_t R, bool IsMips64EL) { |
366 | 689 | assert(!IsMips64EL); |
367 | 689 | r_info = R; |
368 | 689 | } llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false>::setRInfo(unsigned int, bool) Line | Count | Source | 365 | 530 | void setRInfo(uint32_t R, bool IsMips64EL) { | 366 | 530 | assert(!IsMips64EL); | 367 | 530 | r_info = R; | 368 | 530 | } |
llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false>::setRInfo(unsigned int, bool) Line | Count | Source | 365 | 159 | void setRInfo(uint32_t R, bool IsMips64EL) { | 366 | 159 | assert(!IsMips64EL); | 367 | 159 | r_info = R; | 368 | 159 | } |
|
369 | | |
370 | | // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, |
371 | | // and ELF32_R_INFO macros defined in the ELF specification: |
372 | 5.53k | uint32_t getSymbol(bool isMips64EL) const { |
373 | 5.53k | return this->getRInfo(isMips64EL) >> 8; |
374 | 5.53k | } llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false>::getSymbol(bool) const Line | Count | Source | 372 | 4.12k | uint32_t getSymbol(bool isMips64EL) const { | 373 | 4.12k | return this->getRInfo(isMips64EL) >> 8; | 374 | 4.12k | } |
llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false>::getSymbol(bool) const Line | Count | Source | 372 | 1.41k | uint32_t getSymbol(bool isMips64EL) const { | 373 | 1.41k | return this->getRInfo(isMips64EL) >> 8; | 374 | 1.41k | } |
|
375 | 7.85k | unsigned char getType(bool isMips64EL) const { |
376 | 7.85k | return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff); |
377 | 7.85k | } llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false>::getType(bool) const Line | Count | Source | 375 | 5.75k | unsigned char getType(bool isMips64EL) const { | 376 | 5.75k | return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff); | 377 | 5.75k | } |
llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false>::getType(bool) const Line | Count | Source | 375 | 2.09k | unsigned char getType(bool isMips64EL) const { | 376 | 2.09k | return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff); | 377 | 2.09k | } |
|
378 | | void setSymbol(uint32_t s, bool IsMips64EL) { |
379 | | setSymbolAndType(s, getType(IsMips64EL), IsMips64EL); |
380 | | } |
381 | 4 | void setType(unsigned char t, bool IsMips64EL) { |
382 | 4 | setSymbolAndType(getSymbol(IsMips64EL), t, IsMips64EL); |
383 | 4 | } llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false>::setType(unsigned char, bool) Line | Count | Source | 381 | 4 | void setType(unsigned char t, bool IsMips64EL) { | 382 | 4 | setSymbolAndType(getSymbol(IsMips64EL), t, IsMips64EL); | 383 | 4 | } |
Unexecuted instantiation: llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false>::setType(unsigned char, bool) |
384 | 689 | void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL) { |
385 | 689 | this->setRInfo((s << 8) + t, IsMips64EL); |
386 | 689 | } llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false>, false>::setSymbolAndType(unsigned int, unsigned char, bool) Line | Count | Source | 384 | 530 | void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL) { | 385 | 530 | this->setRInfo((s << 8) + t, IsMips64EL); | 386 | 530 | } |
llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false>, false>::setSymbolAndType(unsigned int, unsigned char, bool) Line | Count | Source | 384 | 159 | void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL) { | 385 | 159 | this->setRInfo((s << 8) + t, IsMips64EL); | 386 | 159 | } |
|
387 | | }; |
388 | | |
389 | | template <endianness TargetEndianness> |
390 | | struct Elf_Rel_Impl<ELFType<TargetEndianness, false>, true> |
391 | | : public Elf_Rel_Impl<ELFType<TargetEndianness, false>, false> { |
392 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) |
393 | | static const bool IsRela = true; |
394 | | Elf_Sword r_addend; // Compute value for relocatable field by adding this |
395 | | }; |
396 | | |
397 | | template <endianness TargetEndianness> |
398 | | struct Elf_Rel_Impl<ELFType<TargetEndianness, true>, false> { |
399 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) |
400 | | static const bool IsRela = false; |
401 | | Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) |
402 | | Elf_Xword r_info; // Symbol table index and type of relocation to apply |
403 | | |
404 | 69.5k | uint64_t getRInfo(bool isMips64EL) const { |
405 | 69.5k | uint64_t t = r_info; |
406 | 69.5k | if (!isMips64EL) |
407 | 69.1k | return t; |
408 | 347 | // Mips64 little endian has a "special" encoding of r_info. Instead of one |
409 | 347 | // 64 bit little endian number, it is a little endian 32 bit number followed |
410 | 347 | // by a 32 bit big endian number. |
411 | 347 | return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) | |
412 | 347 | ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff); |
413 | 347 | } llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false>::getRInfo(bool) const Line | Count | Source | 404 | 35.8k | uint64_t getRInfo(bool isMips64EL) const { | 405 | 35.8k | uint64_t t = r_info; | 406 | 35.8k | if (!isMips64EL) | 407 | 35.4k | return t; | 408 | 347 | // Mips64 little endian has a "special" encoding of r_info. Instead of one | 409 | 347 | // 64 bit little endian number, it is a little endian 32 bit number followed | 410 | 347 | // by a 32 bit big endian number. | 411 | 347 | return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) | | 412 | 347 | ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff); | 413 | 347 | } |
llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false>::getRInfo(bool) const Line | Count | Source | 404 | 33.7k | uint64_t getRInfo(bool isMips64EL) const { | 405 | 33.7k | uint64_t t = r_info; | 406 | 33.7k | if (!isMips64EL) | 407 | 33.7k | return t; | 408 | 0 | // Mips64 little endian has a "special" encoding of r_info. Instead of one | 409 | 0 | // 64 bit little endian number, it is a little endian 32 bit number followed | 410 | 0 | // by a 32 bit big endian number. | 411 | 0 | return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) | | 412 | 0 | ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff); | 413 | 0 | } |
|
414 | | |
415 | 1.44k | void setRInfo(uint64_t R, bool IsMips64EL) { |
416 | 1.44k | if (IsMips64EL) |
417 | 58 | r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) | |
418 | 58 | ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56); |
419 | 1.39k | else |
420 | 1.39k | r_info = R; |
421 | 1.44k | } llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false>::setRInfo(unsigned long long, bool) Line | Count | Source | 415 | 1.39k | void setRInfo(uint64_t R, bool IsMips64EL) { | 416 | 1.39k | if (IsMips64EL) | 417 | 58 | r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) | | 418 | 58 | ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56); | 419 | 1.33k | else | 420 | 1.33k | r_info = R; | 421 | 1.39k | } |
llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false>::setRInfo(unsigned long long, bool) Line | Count | Source | 415 | 57 | void setRInfo(uint64_t R, bool IsMips64EL) { | 416 | 57 | if (IsMips64EL) | 417 | 0 | r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) | | 418 | 0 | ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56); | 419 | 57 | else | 420 | 57 | r_info = R; | 421 | 57 | } |
|
422 | | |
423 | | // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE, |
424 | | // and ELF64_R_INFO macros defined in the ELF specification: |
425 | 24.9k | uint32_t getSymbol(bool isMips64EL) const { |
426 | 24.9k | return (uint32_t)(this->getRInfo(isMips64EL) >> 32); |
427 | 24.9k | } llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false>::getSymbol(bool) const Line | Count | Source | 425 | 13.2k | uint32_t getSymbol(bool isMips64EL) const { | 426 | 13.2k | return (uint32_t)(this->getRInfo(isMips64EL) >> 32); | 427 | 13.2k | } |
llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false>::getSymbol(bool) const Line | Count | Source | 425 | 11.6k | uint32_t getSymbol(bool isMips64EL) const { | 426 | 11.6k | return (uint32_t)(this->getRInfo(isMips64EL) >> 32); | 427 | 11.6k | } |
|
428 | 44.6k | uint32_t getType(bool isMips64EL) const { |
429 | 44.6k | return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL); |
430 | 44.6k | } llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false>::getType(bool) const Line | Count | Source | 428 | 22.5k | uint32_t getType(bool isMips64EL) const { | 429 | 22.5k | return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL); | 430 | 22.5k | } |
llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false>::getType(bool) const Line | Count | Source | 428 | 22.0k | uint32_t getType(bool isMips64EL) const { | 429 | 22.0k | return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL); | 430 | 22.0k | } |
|
431 | | void setSymbol(uint32_t s, bool IsMips64EL) { |
432 | | setSymbolAndType(s, getType(IsMips64EL), IsMips64EL); |
433 | | } |
434 | 5 | void setType(uint32_t t, bool IsMips64EL) { |
435 | 5 | setSymbolAndType(getSymbol(IsMips64EL), t, IsMips64EL); |
436 | 5 | } llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false>::setType(unsigned int, bool) Line | Count | Source | 434 | 5 | void setType(uint32_t t, bool IsMips64EL) { | 435 | 5 | setSymbolAndType(getSymbol(IsMips64EL), t, IsMips64EL); | 436 | 5 | } |
Unexecuted instantiation: llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false>::setType(unsigned int, bool) |
437 | 1.44k | void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL) { |
438 | 1.44k | this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL), IsMips64EL); |
439 | 1.44k | } llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true>, false>::setSymbolAndType(unsigned int, unsigned int, bool) Line | Count | Source | 437 | 1.39k | void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL) { | 438 | 1.39k | this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL), IsMips64EL); | 439 | 1.39k | } |
llvm::object::Elf_Rel_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true>, false>::setSymbolAndType(unsigned int, unsigned int, bool) Line | Count | Source | 437 | 57 | void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL) { | 438 | 57 | this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL), IsMips64EL); | 439 | 57 | } |
|
440 | | }; |
441 | | |
442 | | template <endianness TargetEndianness> |
443 | | struct Elf_Rel_Impl<ELFType<TargetEndianness, true>, true> |
444 | | : public Elf_Rel_Impl<ELFType<TargetEndianness, true>, false> { |
445 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) |
446 | | static const bool IsRela = true; |
447 | | Elf_Sxword r_addend; // Compute value for relocatable field by adding this. |
448 | | }; |
449 | | |
450 | | template <class ELFT> |
451 | | struct Elf_Ehdr_Impl { |
452 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
453 | | unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes |
454 | | Elf_Half e_type; // Type of file (see ET_*) |
455 | | Elf_Half e_machine; // Required architecture for this file (see EM_*) |
456 | | Elf_Word e_version; // Must be equal to 1 |
457 | | Elf_Addr e_entry; // Address to jump to in order to start program |
458 | | Elf_Off e_phoff; // Program header table's file offset, in bytes |
459 | | Elf_Off e_shoff; // Section header table's file offset, in bytes |
460 | | Elf_Word e_flags; // Processor-specific flags |
461 | | Elf_Half e_ehsize; // Size of ELF header, in bytes |
462 | | Elf_Half e_phentsize; // Size of an entry in the program header table |
463 | | Elf_Half e_phnum; // Number of entries in the program header table |
464 | | Elf_Half e_shentsize; // Size of an entry in the section header table |
465 | | Elf_Half e_shnum; // Number of entries in the section header table |
466 | | Elf_Half e_shstrndx; // Section header table index of section name |
467 | | // string table |
468 | | |
469 | | bool checkMagic() const { |
470 | | return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0; |
471 | | } |
472 | | |
473 | 3.51k | unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; } llvm::object::Elf_Ehdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getFileClass() const Line | Count | Source | 473 | 774 | unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; } |
llvm::object::Elf_Ehdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getFileClass() const Line | Count | Source | 473 | 1.69k | unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; } |
llvm::object::Elf_Ehdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getFileClass() const Line | Count | Source | 473 | 499 | unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; } |
llvm::object::Elf_Ehdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getFileClass() const Line | Count | Source | 473 | 545 | unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; } |
|
474 | 861 | unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; } llvm::object::Elf_Ehdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getDataEncoding() const Line | Count | Source | 474 | 27 | unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; } |
llvm::object::Elf_Ehdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getDataEncoding() const Line | Count | Source | 474 | 1 | unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; } |
llvm::object::Elf_Ehdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getDataEncoding() const Line | Count | Source | 474 | 407 | unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; } |
llvm::object::Elf_Ehdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getDataEncoding() const Line | Count | Source | 474 | 426 | unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; } |
|
475 | | }; |
476 | | |
477 | | template <endianness TargetEndianness> |
478 | | struct Elf_Phdr_Impl<ELFType<TargetEndianness, false>> { |
479 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) |
480 | | Elf_Word p_type; // Type of segment |
481 | | Elf_Off p_offset; // FileOffset where segment is located, in bytes |
482 | | Elf_Addr p_vaddr; // Virtual Address of beginning of segment |
483 | | Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific) |
484 | | Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero) |
485 | | Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero) |
486 | | Elf_Word p_flags; // Segment flags |
487 | | Elf_Word p_align; // Segment alignment constraint |
488 | | }; |
489 | | |
490 | | template <endianness TargetEndianness> |
491 | | struct Elf_Phdr_Impl<ELFType<TargetEndianness, true>> { |
492 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) |
493 | | Elf_Word p_type; // Type of segment |
494 | | Elf_Word p_flags; // Segment flags |
495 | | Elf_Off p_offset; // FileOffset where segment is located, in bytes |
496 | | Elf_Addr p_vaddr; // Virtual Address of beginning of segment |
497 | | Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific) |
498 | | Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero) |
499 | | Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero) |
500 | | Elf_Xword p_align; // Segment alignment constraint |
501 | | }; |
502 | | |
503 | | // ELFT needed for endianness. |
504 | | template <class ELFT> |
505 | | struct Elf_Hash_Impl { |
506 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
507 | | Elf_Word nbucket; |
508 | | Elf_Word nchain; |
509 | | |
510 | | ArrayRef<Elf_Word> buckets() const { |
511 | | return ArrayRef<Elf_Word>(&nbucket + 2, &nbucket + 2 + nbucket); |
512 | | } |
513 | | |
514 | | ArrayRef<Elf_Word> chains() const { |
515 | | return ArrayRef<Elf_Word>(&nbucket + 2 + nbucket, |
516 | | &nbucket + 2 + nbucket + nchain); |
517 | | } |
518 | | }; |
519 | | |
520 | | // .gnu.hash section |
521 | | template <class ELFT> |
522 | | struct Elf_GnuHash_Impl { |
523 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
524 | | Elf_Word nbuckets; |
525 | | Elf_Word symndx; |
526 | | Elf_Word maskwords; |
527 | | Elf_Word shift2; |
528 | | |
529 | | ArrayRef<Elf_Off> filter() const { |
530 | | return ArrayRef<Elf_Off>(reinterpret_cast<const Elf_Off *>(&shift2 + 1), |
531 | | maskwords); |
532 | | } |
533 | | |
534 | | ArrayRef<Elf_Word> buckets() const { |
535 | | return ArrayRef<Elf_Word>( |
536 | | reinterpret_cast<const Elf_Word *>(filter().end()), nbuckets); |
537 | | } |
538 | | |
539 | | ArrayRef<Elf_Word> values(unsigned DynamicSymCount) const { |
540 | | return ArrayRef<Elf_Word>(buckets().end(), DynamicSymCount - symndx); |
541 | | } |
542 | | }; |
543 | | |
544 | | // Compressed section headers. |
545 | | // http://www.sco.com/developers/gabi/latest/ch4.sheader.html#compression_header |
546 | | template <endianness TargetEndianness> |
547 | | struct Elf_Chdr_Impl<ELFType<TargetEndianness, false>> { |
548 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) |
549 | | Elf_Word ch_type; |
550 | | Elf_Word ch_size; |
551 | | Elf_Word ch_addralign; |
552 | | }; |
553 | | |
554 | | template <endianness TargetEndianness> |
555 | | struct Elf_Chdr_Impl<ELFType<TargetEndianness, true>> { |
556 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) |
557 | | Elf_Word ch_type; |
558 | | Elf_Word ch_reserved; |
559 | | Elf_Xword ch_size; |
560 | | Elf_Xword ch_addralign; |
561 | | }; |
562 | | |
563 | | /// Note header |
564 | | template <class ELFT> |
565 | | struct Elf_Nhdr_Impl { |
566 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
567 | | Elf_Word n_namesz; |
568 | | Elf_Word n_descsz; |
569 | | Elf_Word n_type; |
570 | | |
571 | | /// The alignment of the name and descriptor. |
572 | | /// |
573 | | /// Implementations differ from the specification here: in practice all |
574 | | /// variants align both the name and descriptor to 4-bytes. |
575 | | static const unsigned int Align = 4; |
576 | | |
577 | | /// Get the size of the note, including name, descriptor, and padding. |
578 | 378 | size_t getSize() const { |
579 | 378 | return sizeof(*this) + alignTo<Align>(n_namesz) + alignTo<Align>(n_descsz); |
580 | 378 | } llvm::object::Elf_Nhdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getSize() const Line | Count | Source | 578 | 22 | size_t getSize() const { | 579 | 22 | return sizeof(*this) + alignTo<Align>(n_namesz) + alignTo<Align>(n_descsz); | 580 | 22 | } |
Unexecuted instantiation: llvm::object::Elf_Nhdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getSize() const llvm::object::Elf_Nhdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getSize() const Line | Count | Source | 578 | 356 | size_t getSize() const { | 579 | 356 | return sizeof(*this) + alignTo<Align>(n_namesz) + alignTo<Align>(n_descsz); | 580 | 356 | } |
Unexecuted instantiation: llvm::object::Elf_Nhdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getSize() const |
581 | | }; |
582 | | |
583 | | /// An ELF note. |
584 | | /// |
585 | | /// Wraps a note header, providing methods for accessing the name and |
586 | | /// descriptor safely. |
587 | | template <class ELFT> |
588 | | class Elf_Note_Impl { |
589 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
590 | | |
591 | | const Elf_Nhdr_Impl<ELFT> &Nhdr; |
592 | | |
593 | | template <class NoteIteratorELFT> friend class Elf_Note_Iterator_Impl; |
594 | | |
595 | | public: |
596 | | Elf_Note_Impl(const Elf_Nhdr_Impl<ELFT> &Nhdr) : Nhdr(Nhdr) {} |
597 | | |
598 | | /// Get the note's name, excluding the terminating null byte. |
599 | | StringRef getName() const { |
600 | | if (!Nhdr.n_namesz) |
601 | | return StringRef(); |
602 | | return StringRef(reinterpret_cast<const char *>(&Nhdr) + sizeof(Nhdr), |
603 | | Nhdr.n_namesz - 1); |
604 | | } |
605 | | |
606 | | /// Get the note's descriptor. |
607 | | ArrayRef<uint8_t> getDesc() const { |
608 | | if (!Nhdr.n_descsz) |
609 | | return ArrayRef<uint8_t>(); |
610 | | return ArrayRef<uint8_t>( |
611 | | reinterpret_cast<const uint8_t *>(&Nhdr) + sizeof(Nhdr) + |
612 | | alignTo<Elf_Nhdr_Impl<ELFT>::Align>(Nhdr.n_namesz), |
613 | | Nhdr.n_descsz); |
614 | | } |
615 | | |
616 | | /// Get the note's type. |
617 | | Elf_Word getType() const { return Nhdr.n_type; } |
618 | | }; |
619 | | |
620 | | template <class ELFT> |
621 | | class Elf_Note_Iterator_Impl |
622 | | : std::iterator<std::forward_iterator_tag, Elf_Note_Impl<ELFT>> { |
623 | | // Nhdr being a nullptr marks the end of iteration. |
624 | | const Elf_Nhdr_Impl<ELFT> *Nhdr = nullptr; |
625 | | size_t RemainingSize = 0u; |
626 | | Error *Err = nullptr; |
627 | | |
628 | | template <class ELFFileELFT> friend class ELFFile; |
629 | | |
630 | | // Stop iteration and indicate an overflow. |
631 | 0 | void stopWithOverflowError() { |
632 | 0 | Nhdr = nullptr; |
633 | 0 | *Err = make_error<StringError>("ELF note overflows container", |
634 | 0 | object_error::parse_failed); |
635 | 0 | } Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> >::stopWithOverflowError() Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> >::stopWithOverflowError() Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> >::stopWithOverflowError() Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> >::stopWithOverflowError() |
636 | | |
637 | | // Advance Nhdr by NoteSize bytes, starting from NhdrPos. |
638 | | // |
639 | | // Assumes NoteSize <= RemainingSize. Ensures Nhdr->getSize() <= RemainingSize |
640 | | // upon returning. Handles stopping iteration when reaching the end of the |
641 | | // container, either cleanly or with an overflow error. |
642 | 243 | void advanceNhdr(const uint8_t *NhdrPos, size_t NoteSize) { |
643 | 243 | RemainingSize -= NoteSize; |
644 | 243 | if (RemainingSize == 0u) { |
645 | 88 | // Ensure that if the iterator walks to the end, the error is checked |
646 | 88 | // afterwards. |
647 | 88 | *Err = Error::success(); |
648 | 88 | Nhdr = nullptr; |
649 | 155 | } else if (sizeof(*Nhdr) > RemainingSize) |
650 | 0 | stopWithOverflowError(); |
651 | 155 | else { |
652 | 155 | Nhdr = reinterpret_cast<const Elf_Nhdr_Impl<ELFT> *>(NhdrPos + NoteSize); |
653 | 155 | if (Nhdr->getSize() > RemainingSize) |
654 | 0 | stopWithOverflowError(); |
655 | 155 | else |
656 | 155 | *Err = Error::success(); |
657 | 155 | } |
658 | 243 | } llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> >::advanceNhdr(unsigned char const*, unsigned long) Line | Count | Source | 642 | 6 | void advanceNhdr(const uint8_t *NhdrPos, size_t NoteSize) { | 643 | 6 | RemainingSize -= NoteSize; | 644 | 6 | if (RemainingSize == 0u) { | 645 | 3 | // Ensure that if the iterator walks to the end, the error is checked | 646 | 3 | // afterwards. | 647 | 3 | *Err = Error::success(); | 648 | 3 | Nhdr = nullptr; | 649 | 3 | } else if (sizeof(*Nhdr) > RemainingSize) | 650 | 0 | stopWithOverflowError(); | 651 | 3 | else { | 652 | 3 | Nhdr = reinterpret_cast<const Elf_Nhdr_Impl<ELFT> *>(NhdrPos + NoteSize); | 653 | 3 | if (Nhdr->getSize() > RemainingSize) | 654 | 0 | stopWithOverflowError(); | 655 | 3 | else | 656 | 3 | *Err = Error::success(); | 657 | 3 | } | 658 | 6 | } |
Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> >::advanceNhdr(unsigned char const*, unsigned long) llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> >::advanceNhdr(unsigned char const*, unsigned long) Line | Count | Source | 642 | 237 | void advanceNhdr(const uint8_t *NhdrPos, size_t NoteSize) { | 643 | 237 | RemainingSize -= NoteSize; | 644 | 237 | if (RemainingSize == 0u) { | 645 | 85 | // Ensure that if the iterator walks to the end, the error is checked | 646 | 85 | // afterwards. | 647 | 85 | *Err = Error::success(); | 648 | 85 | Nhdr = nullptr; | 649 | 152 | } else if (sizeof(*Nhdr) > RemainingSize) | 650 | 0 | stopWithOverflowError(); | 651 | 152 | else { | 652 | 152 | Nhdr = reinterpret_cast<const Elf_Nhdr_Impl<ELFT> *>(NhdrPos + NoteSize); | 653 | 152 | if (Nhdr->getSize() > RemainingSize) | 654 | 0 | stopWithOverflowError(); | 655 | 152 | else | 656 | 152 | *Err = Error::success(); | 657 | 152 | } | 658 | 237 | } |
Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> >::advanceNhdr(unsigned char const*, unsigned long) |
659 | | |
660 | 104 | Elf_Note_Iterator_Impl() {} llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> >::Elf_Note_Iterator_Impl() Line | Count | Source | 660 | 3 | Elf_Note_Iterator_Impl() {} |
Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> >::Elf_Note_Iterator_Impl() llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> >::Elf_Note_Iterator_Impl() Line | Count | Source | 660 | 101 | Elf_Note_Iterator_Impl() {} |
Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> >::Elf_Note_Iterator_Impl() |
661 | 8 | explicit Elf_Note_Iterator_Impl(Error &Err) : Err(&Err) {} Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> >::Elf_Note_Iterator_Impl(llvm::Error&) Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> >::Elf_Note_Iterator_Impl(llvm::Error&) llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> >::Elf_Note_Iterator_Impl(llvm::Error&) Line | Count | Source | 661 | 8 | explicit Elf_Note_Iterator_Impl(Error &Err) : Err(&Err) {} |
Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> >::Elf_Note_Iterator_Impl(llvm::Error&) |
662 | | Elf_Note_Iterator_Impl(const uint8_t *Start, size_t Size, Error &Err) |
663 | 96 | : RemainingSize(Size), Err(&Err) { |
664 | 96 | consumeError(std::move(Err)); |
665 | 96 | assert(Start && "ELF note iterator starting at NULL"); |
666 | 96 | advanceNhdr(Start, 0u); |
667 | 96 | } llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> >::Elf_Note_Iterator_Impl(unsigned char const*, unsigned long, llvm::Error&) Line | Count | Source | 663 | 3 | : RemainingSize(Size), Err(&Err) { | 664 | 3 | consumeError(std::move(Err)); | 665 | 3 | assert(Start && "ELF note iterator starting at NULL"); | 666 | 3 | advanceNhdr(Start, 0u); | 667 | 3 | } |
Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> >::Elf_Note_Iterator_Impl(unsigned char const*, unsigned long, llvm::Error&) llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> >::Elf_Note_Iterator_Impl(unsigned char const*, unsigned long, llvm::Error&) Line | Count | Source | 663 | 93 | : RemainingSize(Size), Err(&Err) { | 664 | 93 | consumeError(std::move(Err)); | 665 | 93 | assert(Start && "ELF note iterator starting at NULL"); | 666 | 93 | advanceNhdr(Start, 0u); | 667 | 93 | } |
Unexecuted instantiation: llvm::object::Elf_Note_Iterator_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> >::Elf_Note_Iterator_Impl(unsigned char const*, unsigned long, llvm::Error&) |
668 | | |
669 | | public: |
670 | | Elf_Note_Iterator_Impl &operator++() { |
671 | | assert(Nhdr && "incremented ELF note end iterator"); |
672 | | const uint8_t *NhdrPos = reinterpret_cast<const uint8_t *>(Nhdr); |
673 | | size_t NoteSize = Nhdr->getSize(); |
674 | | advanceNhdr(NhdrPos, NoteSize); |
675 | | return *this; |
676 | | } |
677 | | bool operator==(Elf_Note_Iterator_Impl Other) const { |
678 | | if (!Nhdr && Other.Err) |
679 | | (void)(bool)(*Other.Err); |
680 | | if (!Other.Nhdr && Err) |
681 | | (void)(bool)(*Err); |
682 | | return Nhdr == Other.Nhdr; |
683 | | } |
684 | | bool operator!=(Elf_Note_Iterator_Impl Other) const { |
685 | | return !(*this == Other); |
686 | | } |
687 | | Elf_Note_Impl<ELFT> operator*() const { |
688 | | assert(Nhdr && "dereferenced ELF note end iterator"); |
689 | | return Elf_Note_Impl<ELFT>(*Nhdr); |
690 | | } |
691 | | }; |
692 | | |
693 | | template <class ELFT> struct Elf_CGProfile_Impl { |
694 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
695 | | Elf_Word cgp_from; |
696 | | Elf_Word cgp_to; |
697 | | Elf_Xword cgp_weight; |
698 | | }; |
699 | | |
700 | | // MIPS .reginfo section |
701 | | template <class ELFT> |
702 | | struct Elf_Mips_RegInfo; |
703 | | |
704 | | template <support::endianness TargetEndianness> |
705 | | struct Elf_Mips_RegInfo<ELFType<TargetEndianness, false>> { |
706 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) |
707 | | Elf_Word ri_gprmask; // bit-mask of used general registers |
708 | | Elf_Word ri_cprmask[4]; // bit-mask of used co-processor registers |
709 | | Elf_Addr ri_gp_value; // gp register value |
710 | | }; |
711 | | |
712 | | template <support::endianness TargetEndianness> |
713 | | struct Elf_Mips_RegInfo<ELFType<TargetEndianness, true>> { |
714 | | LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) |
715 | | Elf_Word ri_gprmask; // bit-mask of used general registers |
716 | | Elf_Word ri_pad; // unused padding field |
717 | | Elf_Word ri_cprmask[4]; // bit-mask of used co-processor registers |
718 | | Elf_Addr ri_gp_value; // gp register value |
719 | | }; |
720 | | |
721 | | // .MIPS.options section |
722 | | template <class ELFT> struct Elf_Mips_Options { |
723 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
724 | | uint8_t kind; // Determines interpretation of variable part of descriptor |
725 | | uint8_t size; // Byte size of descriptor, including this header |
726 | | Elf_Half section; // Section header index of section affected, |
727 | | // or 0 for global options |
728 | | Elf_Word info; // Kind-specific information |
729 | | |
730 | | Elf_Mips_RegInfo<ELFT> &getRegInfo() { |
731 | | assert(kind == ELF::ODK_REGINFO); |
732 | | return *reinterpret_cast<Elf_Mips_RegInfo<ELFT> *>( |
733 | | (uint8_t *)this + sizeof(Elf_Mips_Options)); |
734 | | } |
735 | | const Elf_Mips_RegInfo<ELFT> &getRegInfo() const { |
736 | | return const_cast<Elf_Mips_Options *>(this)->getRegInfo(); |
737 | | } |
738 | | }; |
739 | | |
740 | | // .MIPS.abiflags section content |
741 | | template <class ELFT> struct Elf_Mips_ABIFlags { |
742 | | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
743 | | Elf_Half version; // Version of the structure |
744 | | uint8_t isa_level; // ISA level: 1-5, 32, and 64 |
745 | | uint8_t isa_rev; // ISA revision (0 for MIPS I - MIPS V) |
746 | | uint8_t gpr_size; // General purpose registers size |
747 | | uint8_t cpr1_size; // Co-processor 1 registers size |
748 | | uint8_t cpr2_size; // Co-processor 2 registers size |
749 | | uint8_t fp_abi; // Floating-point ABI flag |
750 | | Elf_Word isa_ext; // Processor-specific extension |
751 | | Elf_Word ases; // ASEs flags |
752 | | Elf_Word flags1; // General flags |
753 | | Elf_Word flags2; // General flags |
754 | | }; |
755 | | |
756 | | } // end namespace object. |
757 | | } // end namespace llvm. |
758 | | |
759 | | #endif // LLVM_OBJECT_ELFTYPES_H |