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