LLVM 23.0.0git
ELFTypes.h
Go to the documentation of this file.
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"
14#include "llvm/ADT/StringRef.h"
17#include "llvm/Object/Error.h"
18#include "llvm/Support/Endian.h"
19#include "llvm/Support/Error.h"
21#include <cassert>
22#include <cstdint>
23#include <cstring>
24#include <type_traits>
25
26namespace llvm {
27
28namespace callgraph {
29// ELF call graph section entry Flag field supported values.
38} // namespace callgraph
39
40namespace object {
41
42template <class ELFT> struct Elf_Ehdr_Impl;
43template <class ELFT> struct Elf_Shdr_Impl;
44template <class ELFT> struct Elf_Sym_Impl;
45template <class ELFT> struct Elf_Dyn_Impl;
46template <class ELFT> struct Elf_Phdr_Impl;
47template <class ELFT, bool isRela> struct Elf_Rel_Impl;
48template <bool Is64> struct Elf_Crel_Impl;
49template <class ELFT> struct Elf_Verdef_Impl;
50template <class ELFT> struct Elf_Verdaux_Impl;
51template <class ELFT> struct Elf_Verneed_Impl;
52template <class ELFT> struct Elf_Vernaux_Impl;
53template <class ELFT> struct Elf_Versym_Impl;
54template <class ELFT> struct Elf_Hash_Impl;
55template <class ELFT> struct Elf_GnuHash_Impl;
56template <class ELFT> struct Elf_Chdr_Impl;
57template <class ELFT> struct Elf_Nhdr_Impl;
58template <class ELFT> class Elf_Note_Impl;
59template <class ELFT> class Elf_Note_Iterator_Impl;
60template <class ELFT> struct Elf_CGProfile_Impl;
61
62template <endianness E, bool Is64> struct ELFType {
63private:
64 template <typename Ty>
66
67public:
68 static const endianness Endianness = E;
69 static const bool Is64Bits = Is64;
70
71 using uint = std::conditional_t<Is64, uint64_t, uint32_t>;
80 using Relr = packed<uint>;
100
101 using Half = packed<uint16_t>;
102 using Word = packed<uint32_t>;
103 using Sword = packed<int32_t>;
104 using Xword = packed<uint64_t>;
105 using Sxword = packed<int64_t>;
106 using Addr = packed<uint>;
107 using Off = packed<uint>;
108};
109
114
115// Use an alignment of 2 for the typedefs since that is the worst case for
116// ELF files in archives.
117
118// I really don't like doing this, but the alternative is copypasta.
119#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) \
120 using Elf_Addr = typename ELFT::Addr; \
121 using Elf_Off = typename ELFT::Off; \
122 using Elf_Half = typename ELFT::Half; \
123 using Elf_Word = typename ELFT::Word; \
124 using Elf_Sword = typename ELFT::Sword; \
125 using Elf_Xword = typename ELFT::Xword; \
126 using Elf_Sxword = typename ELFT::Sxword; \
127 using uintX_t = typename ELFT::uint; \
128 using Elf_Ehdr = typename ELFT::Ehdr; \
129 using Elf_Shdr = typename ELFT::Shdr; \
130 using Elf_Sym = typename ELFT::Sym; \
131 using Elf_Dyn = typename ELFT::Dyn; \
132 using Elf_Phdr = typename ELFT::Phdr; \
133 using Elf_Rel = typename ELFT::Rel; \
134 using Elf_Rela = typename ELFT::Rela; \
135 using Elf_Crel = typename ELFT::Crel; \
136 using Elf_Relr = typename ELFT::Relr; \
137 using Elf_Verdef = typename ELFT::Verdef; \
138 using Elf_Verdaux = typename ELFT::Verdaux; \
139 using Elf_Verneed = typename ELFT::Verneed; \
140 using Elf_Vernaux = typename ELFT::Vernaux; \
141 using Elf_Versym = typename ELFT::Versym; \
142 using Elf_Hash = typename ELFT::Hash; \
143 using Elf_GnuHash = typename ELFT::GnuHash; \
144 using Elf_Chdr = typename ELFT::Chdr; \
145 using Elf_Nhdr = typename ELFT::Nhdr; \
146 using Elf_Note = typename ELFT::Note; \
147 using Elf_Note_Iterator = typename ELFT::NoteIterator; \
148 using Elf_CGProfile = typename ELFT::CGProfile; \
149 using Elf_Dyn_Range = typename ELFT::DynRange; \
150 using Elf_Shdr_Range = typename ELFT::ShdrRange; \
151 using Elf_Sym_Range = typename ELFT::SymRange; \
152 using Elf_Rel_Range = typename ELFT::RelRange; \
153 using Elf_Rela_Range = typename ELFT::RelaRange; \
154 using Elf_Relr_Range = typename ELFT::RelrRange; \
155 using Elf_Phdr_Range = typename ELFT::PhdrRange;
156
157#define LLVM_ELF_COMMA ,
158#define LLVM_ELF_IMPORT_TYPES(E, W) \
159 LLVM_ELF_IMPORT_TYPES_ELFT(ELFType<E LLVM_ELF_COMMA W>)
160
161// Section header.
162template <class ELFT> struct Elf_Shdr_Base;
163
164template <endianness Endianness>
165struct Elf_Shdr_Base<ELFType<Endianness, false>> {
166 LLVM_ELF_IMPORT_TYPES(Endianness, false)
167 Elf_Word sh_name; // Section name (index into string table)
168 Elf_Word sh_type; // Section type (SHT_*)
169 Elf_Word sh_flags; // Section flags (SHF_*)
170 Elf_Addr sh_addr; // Address where section is to be loaded
171 Elf_Off sh_offset; // File offset of section data, in bytes
172 Elf_Word sh_size; // Size of section, in bytes
173 Elf_Word sh_link; // Section type-specific header table index link
174 Elf_Word sh_info; // Section type-specific extra information
175 Elf_Word sh_addralign; // Section address alignment
176 Elf_Word sh_entsize; // Size of records contained within the section
177};
178
179template <endianness Endianness>
180struct Elf_Shdr_Base<ELFType<Endianness, true>> {
181 LLVM_ELF_IMPORT_TYPES(Endianness, true)
182 Elf_Word sh_name; // Section name (index into string table)
183 Elf_Word sh_type; // Section type (SHT_*)
184 Elf_Xword sh_flags; // Section flags (SHF_*)
185 Elf_Addr sh_addr; // Address where section is to be loaded
186 Elf_Off sh_offset; // File offset of section data, in bytes
187 Elf_Xword sh_size; // Size of section, in bytes
188 Elf_Word sh_link; // Section type-specific header table index link
189 Elf_Word sh_info; // Section type-specific extra information
190 Elf_Xword sh_addralign; // Section address alignment
191 Elf_Xword sh_entsize; // Size of records contained within the section
192};
193
194template <class ELFT>
196 using Elf_Shdr_Base<ELFT>::sh_entsize;
197 using Elf_Shdr_Base<ELFT>::sh_size;
198
199 /// Get the number of entities this section contains if it has any.
200 unsigned getEntityCount() const {
201 if (sh_entsize == 0)
202 return 0;
203 return sh_size / sh_entsize;
204 }
205};
206
207template <class ELFT> struct Elf_Sym_Base;
208
209template <endianness Endianness>
210struct Elf_Sym_Base<ELFType<Endianness, false>> {
211 LLVM_ELF_IMPORT_TYPES(Endianness, false)
212 Elf_Word st_name; // Symbol name (index into string table)
213 Elf_Addr st_value; // Value or address associated with the symbol
214 Elf_Word st_size; // Size of the symbol
215 unsigned char st_info; // Symbol's type and binding attributes
216 unsigned char st_other; // Must be zero; reserved
217 Elf_Half st_shndx; // Which section (header table index) it's defined in
218};
219
220template <endianness Endianness>
221struct Elf_Sym_Base<ELFType<Endianness, true>> {
222 LLVM_ELF_IMPORT_TYPES(Endianness, true)
223 Elf_Word st_name; // Symbol name (index into string table)
224 unsigned char st_info; // Symbol's type and binding attributes
225 unsigned char st_other; // Must be zero; reserved
226 Elf_Half st_shndx; // Which section (header table index) it's defined in
227 Elf_Addr st_value; // Value or address associated with the symbol
228 Elf_Xword st_size; // Size of the symbol
229};
230
231template <class ELFT>
233 using Elf_Sym_Base<ELFT>::st_info;
234 using Elf_Sym_Base<ELFT>::st_shndx;
235 using Elf_Sym_Base<ELFT>::st_other;
236 using Elf_Sym_Base<ELFT>::st_value;
237
238 // These accessors and mutators correspond to the ELF32_ST_BIND,
239 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
240 unsigned char getBinding() const { return st_info >> 4; }
241 unsigned char getType() const { return st_info & 0x0f; }
242 uint64_t getValue() const { return st_value; }
243 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
244 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
245
246 void setBindingAndType(unsigned char b, unsigned char t) {
247 st_info = (b << 4) + (t & 0x0f);
248 }
249
250 /// Access to the STV_xxx flag stored in the first two bits of st_other.
251 /// STV_DEFAULT: 0
252 /// STV_INTERNAL: 1
253 /// STV_HIDDEN: 2
254 /// STV_PROTECTED: 3
255 unsigned char getVisibility() const { return st_other & 0x3; }
256 void setVisibility(unsigned char v) {
257 assert(v < 4 && "Invalid value for visibility");
258 st_other = (st_other & ~0x3) | v;
259 }
260
261 bool isAbsolute() const { return st_shndx == ELF::SHN_ABS; }
262
263 bool isCommon() const {
264 return getType() == ELF::STT_COMMON || st_shndx == ELF::SHN_COMMON;
265 }
266
267 bool isDefined() const { return !isUndefined(); }
268
269 bool isProcessorSpecific() const {
270 return st_shndx >= ELF::SHN_LOPROC && st_shndx <= ELF::SHN_HIPROC;
271 }
272
273 bool isOSSpecific() const {
274 return st_shndx >= ELF::SHN_LOOS && st_shndx <= ELF::SHN_HIOS;
275 }
276
277 bool isReserved() const {
278 // ELF::SHN_HIRESERVE is 0xffff so st_shndx <= ELF::SHN_HIRESERVE is always
279 // true and some compilers warn about it.
280 return st_shndx >= ELF::SHN_LORESERVE;
281 }
282
283 bool isUndefined() const { return st_shndx == ELF::SHN_UNDEF; }
284
285 bool isExternal() const {
286 return getBinding() != ELF::STB_LOCAL;
287 }
288
290};
291
292template <class ELFT>
294 uint32_t Offset = this->st_name;
295 if (Offset >= StrTab.size())
297 "st_name (0x%" PRIx32
298 ") is past the end of the string table"
299 " of size 0x%zx",
300 Offset, StrTab.size());
301 return StringRef(StrTab.data() + Offset);
302}
303
304/// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
305/// (.gnu.version). This structure is identical for ELF32 and ELF64.
306template <class ELFT>
309 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
310};
311
312/// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
313/// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
314template <class ELFT>
317 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
318 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
319 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
320 Elf_Half vd_cnt; // Number of Verdaux entries
321 Elf_Word vd_hash; // Hash of name
322 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
323 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
324
325 /// Get the first Verdaux entry for this Verdef.
326 const Elf_Verdaux *getAux() const {
327 return reinterpret_cast<const Elf_Verdaux *>((const char *)this + vd_aux);
328 }
329};
330
331/// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
332/// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
333template <class ELFT>
336 Elf_Word vda_name; // Version name (offset in string table)
337 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
338};
339
340/// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
341/// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
342template <class ELFT>
345 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
346 Elf_Half vn_cnt; // Number of associated Vernaux entries
347 Elf_Word vn_file; // Library name (string table offset)
348 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
349 Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
350};
351
352/// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
353/// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
354template <class ELFT>
357 Elf_Word vna_hash; // Hash of dependency name
358 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
359 Elf_Half vna_other; // Version index, used in .gnu.version entries
360 Elf_Word vna_name; // Dependency name
361 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
362};
363
364/// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
365/// table section (.dynamic) look like.
366template <class ELFT> struct Elf_Dyn_Base;
367
368template <endianness Endianness>
369struct Elf_Dyn_Base<ELFType<Endianness, false>> {
370 LLVM_ELF_IMPORT_TYPES(Endianness, false)
371 Elf_Sword d_tag;
372 union {
373 Elf_Word d_val;
374 Elf_Addr d_ptr;
375 } d_un;
376};
377
378template <endianness Endianness>
379struct Elf_Dyn_Base<ELFType<Endianness, true>> {
380 LLVM_ELF_IMPORT_TYPES(Endianness, true)
381 Elf_Sxword d_tag;
382 union {
383 Elf_Xword d_val;
384 Elf_Addr d_ptr;
385 } d_un;
386};
387
388/// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters.
389template <class ELFT>
391 using Elf_Dyn_Base<ELFT>::d_tag;
392 using Elf_Dyn_Base<ELFT>::d_un;
393 using intX_t = std::conditional_t<ELFT::Is64Bits, int64_t, int32_t>;
394 using uintX_t = std::conditional_t<ELFT::Is64Bits, uint64_t, uint32_t>;
395 intX_t getTag() const { return d_tag; }
396 uintX_t getVal() const { return d_un.d_val; }
397 uintX_t getPtr() const { return d_un.d_ptr; }
398};
399
400template <endianness Endianness>
401struct Elf_Rel_Impl<ELFType<Endianness, false>, false> {
402 LLVM_ELF_IMPORT_TYPES(Endianness, false)
403 static const bool HasAddend = false;
404 static const bool IsCrel = false;
405 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
406 Elf_Word r_info; // Symbol table index and type of relocation to apply
407
410 return r_info;
411 }
412 void setRInfo(uint32_t R, bool IsMips64EL) {
413 assert(!IsMips64EL);
414 r_info = R;
415 }
416
417 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
418 // and ELF32_R_INFO macros defined in the ELF specification:
420 return this->getRInfo(isMips64EL) >> 8;
421 }
422 unsigned char getType(bool isMips64EL) const {
423 return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff);
424 }
425 void setSymbol(uint32_t s, bool IsMips64EL) {
426 setSymbolAndType(s, getType(IsMips64EL), IsMips64EL);
427 }
428 void setType(unsigned char t, bool IsMips64EL) {
429 setSymbolAndType(getSymbol(IsMips64EL), t, IsMips64EL);
430 }
431 void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL) {
432 this->setRInfo((s << 8) + t, IsMips64EL);
433 }
434};
435
436template <endianness Endianness>
437struct Elf_Rel_Impl<ELFType<Endianness, false>, true>
438 : public Elf_Rel_Impl<ELFType<Endianness, false>, false> {
439 LLVM_ELF_IMPORT_TYPES(Endianness, false)
440 static const bool HasAddend = true;
441 static const bool IsCrel = false;
442 Elf_Sword r_addend; // Compute value for relocatable field by adding this
443};
444
445template <endianness Endianness>
446struct Elf_Rel_Impl<ELFType<Endianness, true>, false> {
447 LLVM_ELF_IMPORT_TYPES(Endianness, true)
448 static const bool HasAddend = false;
449 static const bool IsCrel = false;
450 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
451 Elf_Xword r_info; // Symbol table index and type of relocation to apply
452
454 uint64_t t = r_info;
455 if (!isMips64EL)
456 return t;
457 // Mips64 little endian has a "special" encoding of r_info. Instead of one
458 // 64 bit little endian number, it is a little endian 32 bit number followed
459 // by a 32 bit big endian number.
460 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
461 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
462 }
463
464 void setRInfo(uint64_t R, bool IsMips64EL) {
465 if (IsMips64EL)
466 r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) |
467 ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56);
468 else
469 r_info = R;
470 }
471
472 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
473 // and ELF64_R_INFO macros defined in the ELF specification:
475 return (uint32_t)(this->getRInfo(isMips64EL) >> 32);
476 }
478 return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL);
479 }
480 void setSymbol(uint32_t s, bool IsMips64EL) {
481 setSymbolAndType(s, getType(IsMips64EL), IsMips64EL);
482 }
483 void setType(uint32_t t, bool IsMips64EL) {
484 setSymbolAndType(getSymbol(IsMips64EL), t, IsMips64EL);
485 }
486 void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL) {
487 this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL), IsMips64EL);
488 }
489};
490
491template <endianness Endianness>
492struct Elf_Rel_Impl<ELFType<Endianness, true>, true>
493 : public Elf_Rel_Impl<ELFType<Endianness, true>, false> {
494 LLVM_ELF_IMPORT_TYPES(Endianness, true)
495 static const bool HasAddend = true;
496 static const bool IsCrel = false;
497 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
498};
499
500// In-memory representation. The serialized representation uses LEB128.
501template <bool Is64> struct Elf_Crel_Impl {
502 using uint = std::conditional_t<Is64, uint64_t, uint32_t>;
503 static const bool HasAddend = true;
504 static const bool IsCrel = true;
508 std::conditional_t<Is64, int64_t, int32_t> r_addend;
509
510 // Dummy bool parameter is for compatibility with Elf_Rel_Impl.
511 uint32_t getType(bool) const { return r_type; }
512 uint32_t getSymbol(bool) const { return r_symidx; }
513 void setSymbolAndType(uint32_t s, unsigned char t, bool) {
514 r_symidx = s;
515 r_type = t;
516 }
517};
518
519template <class ELFT>
522 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
523 Elf_Half e_type; // Type of file (see ET_*)
524 Elf_Half e_machine; // Required architecture for this file (see EM_*)
525 Elf_Word e_version; // Must be equal to 1
526 Elf_Addr e_entry; // Address to jump to in order to start program
527 Elf_Off e_phoff; // Program header table's file offset, in bytes
528 Elf_Off e_shoff; // Section header table's file offset, in bytes
529 Elf_Word e_flags; // Processor-specific flags
530 Elf_Half e_ehsize; // Size of ELF header, in bytes
531 Elf_Half e_phentsize; // Size of an entry in the program header table
532 Elf_Half e_phnum; // Number of entries in the program header table
533 Elf_Half e_shentsize; // Size of an entry in the section header table
534 Elf_Half e_shnum; // Number of entries in the section header table
535 Elf_Half e_shstrndx; // Section header table index of section name
536 // string table
537
538 bool checkMagic() const {
539 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
540 }
541
542 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
543 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
544};
545
546template <endianness Endianness>
547struct Elf_Phdr_Impl<ELFType<Endianness, false>> {
548 LLVM_ELF_IMPORT_TYPES(Endianness, false)
549 Elf_Word p_type; // Type of segment
550 Elf_Off p_offset; // FileOffset where segment is located, in bytes
551 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
552 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
553 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
554 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
555 Elf_Word p_flags; // Segment flags
556 Elf_Word p_align; // Segment alignment constraint
557};
558
559template <endianness Endianness>
560struct Elf_Phdr_Impl<ELFType<Endianness, true>> {
561 LLVM_ELF_IMPORT_TYPES(Endianness, true)
562 Elf_Word p_type; // Type of segment
563 Elf_Word p_flags; // Segment flags
564 Elf_Off p_offset; // FileOffset where segment is located, in bytes
565 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
566 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
567 Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
568 Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero)
569 Elf_Xword p_align; // Segment alignment constraint
570};
571
572// ELFT needed for endianness.
573template <class ELFT>
576 Elf_Word nbucket;
577 Elf_Word nchain;
578
579 ArrayRef<Elf_Word> buckets() const {
580 return ArrayRef<Elf_Word>(&nbucket + 2, &nbucket + 2 + nbucket);
581 }
582
584 return ArrayRef<Elf_Word>(&nbucket + 2 + nbucket,
585 &nbucket + 2 + nbucket + nchain);
586 }
587};
588
589// .gnu.hash section
590template <class ELFT>
593 Elf_Word nbuckets;
594 Elf_Word symndx;
595 Elf_Word maskwords;
596 Elf_Word shift2;
597
598 ArrayRef<Elf_Off> filter() const {
599 return ArrayRef<Elf_Off>(reinterpret_cast<const Elf_Off *>(&shift2 + 1),
600 maskwords);
601 }
602
604 return ArrayRef<Elf_Word>(
605 reinterpret_cast<const Elf_Word *>(filter().end()), nbuckets);
606 }
607
608 ArrayRef<Elf_Word> values(unsigned DynamicSymCount) const {
609 assert(DynamicSymCount >= symndx);
610 return ArrayRef<Elf_Word>(buckets().end(), DynamicSymCount - symndx);
611 }
612};
613
614// Compressed section headers.
615// http://www.sco.com/developers/gabi/latest/ch4.sheader.html#compression_header
616template <endianness Endianness>
617struct Elf_Chdr_Impl<ELFType<Endianness, false>> {
618 LLVM_ELF_IMPORT_TYPES(Endianness, false)
619 Elf_Word ch_type;
620 Elf_Word ch_size;
621 Elf_Word ch_addralign;
622};
623
624template <endianness Endianness>
625struct Elf_Chdr_Impl<ELFType<Endianness, true>> {
626 LLVM_ELF_IMPORT_TYPES(Endianness, true)
627 Elf_Word ch_type;
628 Elf_Word ch_reserved;
629 Elf_Xword ch_size;
630 Elf_Xword ch_addralign;
631};
632
633/// Note header
634template <class ELFT>
637 Elf_Word n_namesz;
638 Elf_Word n_descsz;
639 Elf_Word n_type;
640
641 /// Get the size of the note, including name, descriptor, and padding. Both
642 /// the start and the end of the descriptor are aligned by the section
643 /// alignment. In practice many 64-bit systems deviate from the generic ABI by
644 /// using sh_addralign=4.
645 size_t getSize(size_t Align) const {
646 return alignToPowerOf2(sizeof(*this) + n_namesz, Align) +
648 }
649};
650
651/// An ELF note.
652///
653/// Wraps a note header, providing methods for accessing the name and
654/// descriptor safely.
655template <class ELFT>
658
659 const Elf_Nhdr_Impl<ELFT> &Nhdr;
660
661 template <class NoteIteratorELFT> friend class Elf_Note_Iterator_Impl;
662
663public:
664 Elf_Note_Impl(const Elf_Nhdr_Impl<ELFT> &Nhdr) : Nhdr(Nhdr) {}
665
666 /// Get the note's name, excluding the terminating null byte.
668 if (!Nhdr.n_namesz)
669 return StringRef();
670 return StringRef(reinterpret_cast<const char *>(&Nhdr) + sizeof(Nhdr),
671 Nhdr.n_namesz - 1);
672 }
673
674 /// Get the note's descriptor.
676 if (!Nhdr.n_descsz)
677 return ArrayRef<uint8_t>();
678 return ArrayRef<uint8_t>(
679 reinterpret_cast<const uint8_t *>(&Nhdr) +
680 alignToPowerOf2(sizeof(Nhdr) + Nhdr.n_namesz, Align),
681 Nhdr.n_descsz);
682 }
683
684 /// Get the note's descriptor as StringRef
687 return StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
688 }
689
690 /// Get the note's type.
691 Elf_Word getType() const { return Nhdr.n_type; }
692};
693
694template <class ELFT> class Elf_Note_Iterator_Impl {
695public:
696 using iterator_category = std::forward_iterator_tag;
698 using difference_type = std::ptrdiff_t;
701
702private:
703 // Nhdr being a nullptr marks the end of iteration.
704 const Elf_Nhdr_Impl<ELFT> *Nhdr = nullptr;
705 size_t RemainingSize = 0u;
706 size_t Align = 0;
707 Error *Err = nullptr;
708
709 template <class ELFFileELFT> friend class ELFFile;
710
711 // Stop iteration and indicate an overflow.
712 void stopWithOverflowError() {
713 Nhdr = nullptr;
714 *Err = make_error<StringError>("ELF note overflows container",
716 }
717
718 // Advance Nhdr by NoteSize bytes, starting from NhdrPos.
719 //
720 // Assumes NoteSize <= RemainingSize. Ensures Nhdr->getSize() <= RemainingSize
721 // upon returning. Handles stopping iteration when reaching the end of the
722 // container, either cleanly or with an overflow error.
723 void advanceNhdr(const uint8_t *NhdrPos, size_t NoteSize) {
724 RemainingSize -= NoteSize;
725 if (RemainingSize == 0u) {
726 // Ensure that if the iterator walks to the end, the error is checked
727 // afterwards.
728 *Err = Error::success();
729 Nhdr = nullptr;
730 } else if (sizeof(*Nhdr) > RemainingSize)
731 stopWithOverflowError();
732 else {
733 Nhdr = reinterpret_cast<const Elf_Nhdr_Impl<ELFT> *>(NhdrPos + NoteSize);
734 if (Nhdr->getSize(Align) > RemainingSize)
735 stopWithOverflowError();
736 else
737 *Err = Error::success();
738 }
739 }
740
741 Elf_Note_Iterator_Impl() = default;
742 explicit Elf_Note_Iterator_Impl(Error &Err) : Err(&Err) {}
743 Elf_Note_Iterator_Impl(const uint8_t *Start, size_t Size, size_t Align,
744 Error &Err)
745 : RemainingSize(Size), Align(Align), Err(&Err) {
746 consumeError(std::move(Err));
747 assert(Start && "ELF note iterator starting at NULL");
748 advanceNhdr(Start, 0u);
749 }
750
751public:
752 Elf_Note_Iterator_Impl &operator++() {
753 assert(Nhdr && "incremented ELF note end iterator");
754 const uint8_t *NhdrPos = reinterpret_cast<const uint8_t *>(Nhdr);
755 size_t NoteSize = Nhdr->getSize(Align);
756 advanceNhdr(NhdrPos, NoteSize);
757 return *this;
758 }
759 bool operator==(Elf_Note_Iterator_Impl Other) const {
760 if (!Nhdr && Other.Err)
761 (void)(bool)(*Other.Err);
762 if (!Other.Nhdr && Err)
763 (void)(bool)(*Err);
764 return Nhdr == Other.Nhdr;
765 }
766 bool operator!=(Elf_Note_Iterator_Impl Other) const {
767 return !(*this == Other);
768 }
770 assert(Nhdr && "dereferenced ELF note end iterator");
771 return Elf_Note_Impl<ELFT>(*Nhdr);
772 }
773};
774
775template <class ELFT> struct Elf_CGProfile_Impl {
777 Elf_Xword cgp_weight;
778};
779
780// MIPS .reginfo section
781template <class ELFT>
783
784template <llvm::endianness Endianness>
785struct Elf_Mips_RegInfo<ELFType<Endianness, false>> {
786 LLVM_ELF_IMPORT_TYPES(Endianness, false)
787 Elf_Word ri_gprmask; // bit-mask of used general registers
788 Elf_Word ri_cprmask[4]; // bit-mask of used co-processor registers
789 Elf_Addr ri_gp_value; // gp register value
790};
791
792template <llvm::endianness Endianness>
793struct Elf_Mips_RegInfo<ELFType<Endianness, true>> {
794 LLVM_ELF_IMPORT_TYPES(Endianness, true)
795 Elf_Word ri_gprmask; // bit-mask of used general registers
796 Elf_Word ri_pad; // unused padding field
797 Elf_Word ri_cprmask[4]; // bit-mask of used co-processor registers
798 Elf_Addr ri_gp_value; // gp register value
799};
800
801// .MIPS.options section
802template <class ELFT> struct Elf_Mips_Options {
804 uint8_t kind; // Determines interpretation of variable part of descriptor
805 uint8_t size; // Byte size of descriptor, including this header
806 Elf_Half section; // Section header index of section affected,
807 // or 0 for global options
808 Elf_Word info; // Kind-specific information
809
812 return *reinterpret_cast<Elf_Mips_RegInfo<ELFT> *>(
813 (uint8_t *)this + sizeof(Elf_Mips_Options));
814 }
816 return const_cast<Elf_Mips_Options *>(this)->getRegInfo();
817 }
818};
819
820// .MIPS.abiflags section content
821template <class ELFT> struct Elf_Mips_ABIFlags {
823 Elf_Half version; // Version of the structure
824 uint8_t isa_level; // ISA level: 1-5, 32, and 64
825 uint8_t isa_rev; // ISA revision (0 for MIPS I - MIPS V)
826 uint8_t gpr_size; // General purpose registers size
827 uint8_t cpr1_size; // Co-processor 1 registers size
828 uint8_t cpr2_size; // Co-processor 2 registers size
829 uint8_t fp_abi; // Floating-point ABI flag
830 Elf_Word isa_ext; // Processor-specific extension
831 Elf_Word ases; // ASEs flags
832 Elf_Word flags1; // General flags
833 Elf_Word flags2; // General flags
834};
835
836} // end namespace object.
837} // end namespace llvm.
838
839#endif // LLVM_OBJECT_ELFTYPES_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
This file declares common types and utilities for basic-block address maps.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static bool isMips64EL(const ELFYAML::Object &Obj)
#define LLVM_ELF_IMPORT_TYPES(E, W)
Definition ELFTypes.h:158
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition ELFTypes.h:119
Merge contiguous icmps into a memcmp
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:143
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:137
StringRef getDescAsStringRef(size_t Align) const
Get the note's descriptor as StringRef.
Definition ELFTypes.h:685
Elf_Word getType() const
Get the note's type.
Definition ELFTypes.h:691
Elf_Note_Impl(const Elf_Nhdr_Impl< ELFT > &Nhdr)
Definition ELFTypes.h:664
StringRef getName() const
Get the note's name, excluding the terminating null byte.
Definition ELFTypes.h:667
ArrayRef< uint8_t > getDesc(size_t Align) const
Get the note's descriptor.
Definition ELFTypes.h:675
bool operator!=(Elf_Note_Iterator_Impl Other) const
Definition ELFTypes.h:766
Elf_Note_Impl< ELFType< E, Is64 > > value_type
Definition ELFTypes.h:697
Elf_Note_Impl< ELFT > operator*() const
Definition ELFTypes.h:769
Elf_Note_Iterator_Impl & operator++()
Definition ELFTypes.h:752
bool operator==(Elf_Note_Iterator_Impl Other) const
Definition ELFTypes.h:759
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ SHN_HIOS
Definition ELF.h:1136
@ SHN_ABS
Definition ELF.h:1137
@ SHN_COMMON
Definition ELF.h:1138
@ SHN_LOOS
Definition ELF.h:1135
@ SHN_LOPROC
Definition ELF.h:1133
@ SHN_UNDEF
Definition ELF.h:1131
@ SHN_HIPROC
Definition ELF.h:1134
@ SHN_LORESERVE
Definition ELF.h:1132
@ EI_DATA
Definition ELF.h:56
@ EI_CLASS
Definition ELF.h:55
static const char ElfMagic[]
Definition ELF.h:47
@ STB_LOCAL
Definition ELF.h:1403
@ STT_COMMON
Definition ELF.h:1420
@ ODK_REGINFO
Definition ELF.h:609
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE()
ELFType< llvm::endianness::big, true > ELF64BE
Definition ELFTypes.h:113
ELFType< llvm::endianness::little, false > ELF32LE
Definition ELFTypes.h:110
ELFType< llvm::endianness::little, true > ELF64LE
Definition ELFTypes.h:112
ELFType< llvm::endianness::big, false > ELF32BE
Definition ELFTypes.h:111
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
Op::Description Desc
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
Definition MathExtras.h:493
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
@ Other
Any other memory.
Definition ModRef.h:68
endianness
Definition bit.h:71
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Elf_Verdaux_Impl< ELFType< E, Is64 > > Verdaux
Definition ELFTypes.h:82
std::conditional_t< Is64, uint64_t, uint32_t > uint
Definition ELFTypes.h:71
Elf_Note_Impl< ELFType< E, Is64 > > Note
Definition ELFTypes.h:90
packed< int64_t > Sxword
Definition ELFTypes.h:105
Elf_Ehdr_Impl< ELFType< E, Is64 > > Ehdr
Definition ELFTypes.h:72
ArrayRef< Phdr > PhdrRange
Definition ELFTypes.h:99
packed< uint64_t > Xword
Definition ELFTypes.h:104
Elf_Dyn_Impl< ELFType< E, Is64 > > Dyn
Definition ELFTypes.h:75
Elf_Rel_Impl< ELFType< E, Is64 >, false > Rel
Definition ELFTypes.h:77
Elf_Versym_Impl< ELFType< E, Is64 > > Versym
Definition ELFTypes.h:85
Elf_Vernaux_Impl< ELFType< E, Is64 > > Vernaux
Definition ELFTypes.h:84
packed< uint16_t > Half
Definition ELFTypes.h:101
Elf_GnuHash_Impl< ELFType< E, Is64 > > GnuHash
Definition ELFTypes.h:87
packed< uint > Addr
Definition ELFTypes.h:106
Elf_Rel_Impl< ELFType< E, Is64 >, true > Rela
Definition ELFTypes.h:78
Elf_Note_Iterator_Impl< ELFType< E, Is64 > > NoteIterator
Definition ELFTypes.h:91
packed< int32_t > Sword
Definition ELFTypes.h:103
ArrayRef< Shdr > ShdrRange
Definition ELFTypes.h:94
packed< uint > Relr
Definition ELFTypes.h:80
packed< uint > Off
Definition ELFTypes.h:107
Elf_CGProfile_Impl< ELFType< E, Is64 > > CGProfile
Definition ELFTypes.h:92
Elf_Nhdr_Impl< ELFType< E, Is64 > > Nhdr
Definition ELFTypes.h:89
Elf_Shdr_Impl< ELFType< E, Is64 > > Shdr
Definition ELFTypes.h:73
ArrayRef< Rel > RelRange
Definition ELFTypes.h:96
Elf_Crel_Impl< Is64 > Crel
Definition ELFTypes.h:79
ArrayRef< Sym > SymRange
Definition ELFTypes.h:95
ArrayRef< Dyn > DynRange
Definition ELFTypes.h:93
Elf_Verneed_Impl< ELFType< E, Is64 > > Verneed
Definition ELFTypes.h:83
Elf_Chdr_Impl< ELFType< E, Is64 > > Chdr
Definition ELFTypes.h:88
ArrayRef< Rela > RelaRange
Definition ELFTypes.h:97
Elf_Hash_Impl< ELFType< E, Is64 > > Hash
Definition ELFTypes.h:86
Elf_Sym_Impl< ELFType< E, Is64 > > Sym
Definition ELFTypes.h:74
ArrayRef< Relr > RelrRange
Definition ELFTypes.h:98
Elf_Verdef_Impl< ELFType< E, Is64 > > Verdef
Definition ELFTypes.h:81
Elf_Phdr_Impl< ELFType< E, Is64 > > Phdr
Definition ELFTypes.h:76
packed< uint32_t > Word
Definition ELFTypes.h:102
uint32_t getType(bool) const
Definition ELFTypes.h:511
std::conditional_t< Is64, uint64_t, uint32_t > uint
Definition ELFTypes.h:502
uint32_t getSymbol(bool) const
Definition ELFTypes.h:512
static const bool IsCrel
Definition ELFTypes.h:504
static const bool HasAddend
Definition ELFTypes.h:503
void setSymbolAndType(uint32_t s, unsigned char t, bool)
Definition ELFTypes.h:513
std::conditional_t< Is64, int64_t, int32_t > r_addend
Definition ELFTypes.h:508
Elf_Dyn_Base: This structure matches the form of entries in the dynamic table section (....
Definition ELFTypes.h:366
Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters.
Definition ELFTypes.h:390
uintX_t getVal() const
Definition ELFTypes.h:396
std::conditional_t< ELFType< E, Is64 >::Is64Bits, int64_t, int32_t > intX_t
Definition ELFTypes.h:393
std::conditional_t< ELFType< E, Is64 >::Is64Bits, uint64_t, uint32_t > uintX_t
Definition ELFTypes.h:394
uintX_t getPtr() const
Definition ELFTypes.h:397
intX_t getTag() const
Definition ELFTypes.h:395
unsigned char getFileClass() const
Definition ELFTypes.h:542
unsigned char e_ident[ELF::EI_NIDENT]
Definition ELFTypes.h:522
unsigned char getDataEncoding() const
Definition ELFTypes.h:543
ArrayRef< Elf_Word > values(unsigned DynamicSymCount) const
Definition ELFTypes.h:608
ArrayRef< Elf_Word > buckets() const
Definition ELFTypes.h:603
ArrayRef< Elf_Word > chains() const
Definition ELFTypes.h:583
const Elf_Mips_RegInfo< ELFT > & getRegInfo() const
Definition ELFTypes.h:815
Elf_Mips_RegInfo< ELFT > & getRegInfo()
Definition ELFTypes.h:810
void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL)
Definition ELFTypes.h:431
void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL)
Definition ELFTypes.h:486
unsigned getEntityCount() const
Get the number of entities this section contains if it has any.
Definition ELFTypes.h:200
void setBindingAndType(unsigned char b, unsigned char t)
Definition ELFTypes.h:246
bool isProcessorSpecific() const
Definition ELFTypes.h:269
void setBinding(unsigned char b)
Definition ELFTypes.h:243
unsigned char getBinding() const
Definition ELFTypes.h:240
Expected< StringRef > getName(StringRef StrTab) const
Definition ELFTypes.h:293
bool isOSSpecific() const
Definition ELFTypes.h:273
unsigned char getType() const
Definition ELFTypes.h:241
uint64_t getValue() const
Definition ELFTypes.h:242
void setVisibility(unsigned char v)
Definition ELFTypes.h:256
void setType(unsigned char t)
Definition ELFTypes.h:244
unsigned char getVisibility() const
Access to the STV_xxx flag stored in the first two bits of st_other.
Definition ELFTypes.h:255
Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef section (....
Definition ELFTypes.h:334
Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section (.gnu.version_d).
Definition ELFTypes.h:315
Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed section (....
Definition ELFTypes.h:355
Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed section (.gnu....
Definition ELFTypes.h:343
Elf_Versym: This is the structure of entries in the SHT_GNU_versym section (.gnu.version).
Definition ELFTypes.h:307