LLVM 19.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"
13#include "llvm/ADT/StringRef.h"
15#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 {
27namespace object {
28
29template <class ELFT> struct Elf_Ehdr_Impl;
30template <class ELFT> struct Elf_Shdr_Impl;
31template <class ELFT> struct Elf_Sym_Impl;
32template <class ELFT> struct Elf_Dyn_Impl;
33template <class ELFT> struct Elf_Phdr_Impl;
34template <class ELFT, bool isRela> struct Elf_Rel_Impl;
35template <class ELFT> struct Elf_Verdef_Impl;
36template <class ELFT> struct Elf_Verdaux_Impl;
37template <class ELFT> struct Elf_Verneed_Impl;
38template <class ELFT> struct Elf_Vernaux_Impl;
39template <class ELFT> struct Elf_Versym_Impl;
40template <class ELFT> struct Elf_Hash_Impl;
41template <class ELFT> struct Elf_GnuHash_Impl;
42template <class ELFT> struct Elf_Chdr_Impl;
43template <class ELFT> struct Elf_Nhdr_Impl;
44template <class ELFT> class Elf_Note_Impl;
45template <class ELFT> class Elf_Note_Iterator_Impl;
46template <class ELFT> struct Elf_CGProfile_Impl;
47
48template <endianness E, bool Is64> struct ELFType {
49private:
50 template <typename Ty>
52
53public:
55 static const bool Is64Bits = Is64;
56
57 using uint = std::conditional_t<Is64, uint64_t, uint32_t>;
85
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 using uintX_t = typename ELFT::uint; \
113 using Elf_Ehdr = typename ELFT::Ehdr; \
114 using Elf_Shdr = typename ELFT::Shdr; \
115 using Elf_Sym = typename ELFT::Sym; \
116 using Elf_Dyn = typename ELFT::Dyn; \
117 using Elf_Phdr = typename ELFT::Phdr; \
118 using Elf_Rel = typename ELFT::Rel; \
119 using Elf_Rela = typename ELFT::Rela; \
120 using Elf_Relr = typename ELFT::Relr; \
121 using Elf_Verdef = typename ELFT::Verdef; \
122 using Elf_Verdaux = typename ELFT::Verdaux; \
123 using Elf_Verneed = typename ELFT::Verneed; \
124 using Elf_Vernaux = typename ELFT::Vernaux; \
125 using Elf_Versym = typename ELFT::Versym; \
126 using Elf_Hash = typename ELFT::Hash; \
127 using Elf_GnuHash = typename ELFT::GnuHash; \
128 using Elf_Chdr = typename ELFT::Chdr; \
129 using Elf_Nhdr = typename ELFT::Nhdr; \
130 using Elf_Note = typename ELFT::Note; \
131 using Elf_Note_Iterator = typename ELFT::NoteIterator; \
132 using Elf_CGProfile = typename ELFT::CGProfile; \
133 using Elf_Dyn_Range = typename ELFT::DynRange; \
134 using Elf_Shdr_Range = typename ELFT::ShdrRange; \
135 using Elf_Sym_Range = typename ELFT::SymRange; \
136 using Elf_Rel_Range = typename ELFT::RelRange; \
137 using Elf_Rela_Range = typename ELFT::RelaRange; \
138 using Elf_Relr_Range = typename ELFT::RelrRange; \
139 using Elf_Phdr_Range = typename ELFT::PhdrRange;
140
141#define LLVM_ELF_COMMA ,
142#define LLVM_ELF_IMPORT_TYPES(E, W) \
143 LLVM_ELF_IMPORT_TYPES_ELFT(ELFType<E LLVM_ELF_COMMA W>)
144
145// Section header.
146template <class ELFT> struct Elf_Shdr_Base;
147
148template <endianness TargetEndianness>
149struct Elf_Shdr_Base<ELFType<TargetEndianness, false>> {
150 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
151 Elf_Word sh_name; // Section name (index into string table)
152 Elf_Word sh_type; // Section type (SHT_*)
153 Elf_Word sh_flags; // Section flags (SHF_*)
154 Elf_Addr sh_addr; // Address where section is to be loaded
155 Elf_Off sh_offset; // File offset of section data, in bytes
156 Elf_Word sh_size; // Size of section, in bytes
157 Elf_Word sh_link; // Section type-specific header table index link
158 Elf_Word sh_info; // Section type-specific extra information
159 Elf_Word sh_addralign; // Section address alignment
160 Elf_Word sh_entsize; // Size of records contained within the section
161};
162
163template <endianness TargetEndianness>
164struct Elf_Shdr_Base<ELFType<TargetEndianness, true>> {
165 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
166 Elf_Word sh_name; // Section name (index into string table)
167 Elf_Word sh_type; // Section type (SHT_*)
168 Elf_Xword sh_flags; // Section flags (SHF_*)
169 Elf_Addr sh_addr; // Address where section is to be loaded
170 Elf_Off sh_offset; // File offset of section data, in bytes
171 Elf_Xword sh_size; // Size of section, in bytes
172 Elf_Word sh_link; // Section type-specific header table index link
173 Elf_Word sh_info; // Section type-specific extra information
174 Elf_Xword sh_addralign; // Section address alignment
175 Elf_Xword sh_entsize; // Size of records contained within the section
176};
177
178template <class ELFT>
180 using Elf_Shdr_Base<ELFT>::sh_entsize;
181 using Elf_Shdr_Base<ELFT>::sh_size;
182
183 /// Get the number of entities this section contains if it has any.
184 unsigned getEntityCount() const {
185 if (sh_entsize == 0)
186 return 0;
187 return sh_size / sh_entsize;
188 }
189};
190
191template <class ELFT> struct Elf_Sym_Base;
192
193template <endianness TargetEndianness>
194struct Elf_Sym_Base<ELFType<TargetEndianness, false>> {
195 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
196 Elf_Word st_name; // Symbol name (index into string table)
197 Elf_Addr st_value; // Value or address associated with the symbol
198 Elf_Word st_size; // Size of the symbol
199 unsigned char st_info; // Symbol's type and binding attributes
200 unsigned char st_other; // Must be zero; reserved
201 Elf_Half st_shndx; // Which section (header table index) it's defined in
202};
203
204template <endianness TargetEndianness>
205struct Elf_Sym_Base<ELFType<TargetEndianness, true>> {
206 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
207 Elf_Word st_name; // Symbol name (index into string table)
208 unsigned char st_info; // Symbol's type and binding attributes
209 unsigned char st_other; // Must be zero; reserved
210 Elf_Half st_shndx; // Which section (header table index) it's defined in
211 Elf_Addr st_value; // Value or address associated with the symbol
212 Elf_Xword st_size; // Size of the symbol
213};
214
215template <class ELFT>
217 using Elf_Sym_Base<ELFT>::st_info;
218 using Elf_Sym_Base<ELFT>::st_shndx;
219 using Elf_Sym_Base<ELFT>::st_other;
220 using Elf_Sym_Base<ELFT>::st_value;
221
222 // These accessors and mutators correspond to the ELF32_ST_BIND,
223 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
224 unsigned char getBinding() const { return st_info >> 4; }
225 unsigned char getType() const { return st_info & 0x0f; }
226 uint64_t getValue() const { return st_value; }
227 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
228 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
229
230 void setBindingAndType(unsigned char b, unsigned char t) {
231 st_info = (b << 4) + (t & 0x0f);
232 }
233
234 /// Access to the STV_xxx flag stored in the first two bits of st_other.
235 /// STV_DEFAULT: 0
236 /// STV_INTERNAL: 1
237 /// STV_HIDDEN: 2
238 /// STV_PROTECTED: 3
239 unsigned char getVisibility() const { return st_other & 0x3; }
240 void setVisibility(unsigned char v) {
241 assert(v < 4 && "Invalid value for visibility");
242 st_other = (st_other & ~0x3) | v;
243 }
244
245 bool isAbsolute() const { return st_shndx == ELF::SHN_ABS; }
246
247 bool isCommon() const {
248 return getType() == ELF::STT_COMMON || st_shndx == ELF::SHN_COMMON;
249 }
250
251 bool isDefined() const { return !isUndefined(); }
252
253 bool isProcessorSpecific() const {
254 return st_shndx >= ELF::SHN_LOPROC && st_shndx <= ELF::SHN_HIPROC;
255 }
256
257 bool isOSSpecific() const {
258 return st_shndx >= ELF::SHN_LOOS && st_shndx <= ELF::SHN_HIOS;
259 }
260
261 bool isReserved() const {
262 // ELF::SHN_HIRESERVE is 0xffff so st_shndx <= ELF::SHN_HIRESERVE is always
263 // true and some compilers warn about it.
264 return st_shndx >= ELF::SHN_LORESERVE;
265 }
266
267 bool isUndefined() const { return st_shndx == ELF::SHN_UNDEF; }
268
269 bool isExternal() const {
270 return getBinding() != ELF::STB_LOCAL;
271 }
272
274};
275
276template <class ELFT>
278 uint32_t Offset = this->st_name;
279 if (Offset >= StrTab.size())
280 return createStringError(object_error::parse_failed,
281 "st_name (0x%" PRIx32
282 ") is past the end of the string table"
283 " of size 0x%zx",
284 Offset, StrTab.size());
285 return StringRef(StrTab.data() + Offset);
286}
287
288/// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
289/// (.gnu.version). This structure is identical for ELF32 and ELF64.
290template <class ELFT>
293 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
294};
295
296/// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
297/// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
298template <class ELFT>
301 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
302 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
303 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
304 Elf_Half vd_cnt; // Number of Verdaux entries
305 Elf_Word vd_hash; // Hash of name
306 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
307 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
308
309 /// Get the first Verdaux entry for this Verdef.
310 const Elf_Verdaux *getAux() const {
311 return reinterpret_cast<const Elf_Verdaux *>((const char *)this + vd_aux);
312 }
313};
314
315/// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
316/// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
317template <class ELFT>
320 Elf_Word vda_name; // Version name (offset in string table)
321 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
322};
323
324/// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
325/// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
326template <class ELFT>
329 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
330 Elf_Half vn_cnt; // Number of associated Vernaux entries
331 Elf_Word vn_file; // Library name (string table offset)
332 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
333 Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
334};
335
336/// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
337/// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
338template <class ELFT>
341 Elf_Word vna_hash; // Hash of dependency name
342 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
343 Elf_Half vna_other; // Version index, used in .gnu.version entries
344 Elf_Word vna_name; // Dependency name
345 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
346};
347
348/// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
349/// table section (.dynamic) look like.
350template <class ELFT> struct Elf_Dyn_Base;
351
352template <endianness TargetEndianness>
353struct Elf_Dyn_Base<ELFType<TargetEndianness, false>> {
354 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
355 Elf_Sword d_tag;
356 union {
357 Elf_Word d_val;
358 Elf_Addr d_ptr;
359 } d_un;
360};
361
362template <endianness TargetEndianness>
363struct Elf_Dyn_Base<ELFType<TargetEndianness, true>> {
364 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
365 Elf_Sxword d_tag;
366 union {
367 Elf_Xword d_val;
368 Elf_Addr d_ptr;
369 } d_un;
370};
371
372/// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters.
373template <class ELFT>
375 using Elf_Dyn_Base<ELFT>::d_tag;
376 using Elf_Dyn_Base<ELFT>::d_un;
377 using intX_t = std::conditional_t<ELFT::Is64Bits, int64_t, int32_t>;
378 using uintX_t = std::conditional_t<ELFT::Is64Bits, uint64_t, uint32_t>;
379 intX_t getTag() const { return d_tag; }
380 uintX_t getVal() const { return d_un.d_val; }
381 uintX_t getPtr() const { return d_un.d_ptr; }
382};
383
384template <endianness TargetEndianness>
385struct Elf_Rel_Impl<ELFType<TargetEndianness, false>, false> {
386 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
387 static const bool IsRela = false;
388 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
389 Elf_Word r_info; // Symbol table index and type of relocation to apply
390
391 uint32_t getRInfo(bool isMips64EL) const {
393 return r_info;
394 }
395 void setRInfo(uint32_t R, bool IsMips64EL) {
396 assert(!IsMips64EL);
397 r_info = R;
398 }
399
400 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
401 // and ELF32_R_INFO macros defined in the ELF specification:
403 return this->getRInfo(isMips64EL) >> 8;
404 }
405 unsigned char getType(bool isMips64EL) const {
406 return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff);
407 }
408 void setSymbol(uint32_t s, bool IsMips64EL) {
409 setSymbolAndType(s, getType(IsMips64EL), IsMips64EL);
410 }
411 void setType(unsigned char t, bool IsMips64EL) {
412 setSymbolAndType(getSymbol(IsMips64EL), t, IsMips64EL);
413 }
414 void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL) {
415 this->setRInfo((s << 8) + t, IsMips64EL);
416 }
417};
418
419template <endianness TargetEndianness>
420struct Elf_Rel_Impl<ELFType<TargetEndianness, false>, true>
421 : public Elf_Rel_Impl<ELFType<TargetEndianness, false>, false> {
422 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
423 static const bool IsRela = true;
424 Elf_Sword r_addend; // Compute value for relocatable field by adding this
425};
426
427template <endianness TargetEndianness>
428struct Elf_Rel_Impl<ELFType<TargetEndianness, true>, false> {
429 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
430 static const bool IsRela = false;
431 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
432 Elf_Xword r_info; // Symbol table index and type of relocation to apply
433
434 uint64_t getRInfo(bool isMips64EL) const {
435 uint64_t t = r_info;
436 if (!isMips64EL)
437 return t;
438 // Mips64 little endian has a "special" encoding of r_info. Instead of one
439 // 64 bit little endian number, it is a little endian 32 bit number followed
440 // by a 32 bit big endian number.
441 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
442 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
443 }
444
445 void setRInfo(uint64_t R, bool IsMips64EL) {
446 if (IsMips64EL)
447 r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) |
448 ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56);
449 else
450 r_info = R;
451 }
452
453 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
454 // and ELF64_R_INFO macros defined in the ELF specification:
456 return (uint32_t)(this->getRInfo(isMips64EL) >> 32);
457 }
459 return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL);
460 }
461 void setSymbol(uint32_t s, bool IsMips64EL) {
462 setSymbolAndType(s, getType(IsMips64EL), IsMips64EL);
463 }
464 void setType(uint32_t t, bool IsMips64EL) {
465 setSymbolAndType(getSymbol(IsMips64EL), t, IsMips64EL);
466 }
467 void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL) {
468 this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL), IsMips64EL);
469 }
470};
471
472template <endianness TargetEndianness>
473struct Elf_Rel_Impl<ELFType<TargetEndianness, true>, true>
474 : public Elf_Rel_Impl<ELFType<TargetEndianness, true>, false> {
475 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
476 static const bool IsRela = true;
477 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
478};
479
480template <class ELFT>
483 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
484 Elf_Half e_type; // Type of file (see ET_*)
485 Elf_Half e_machine; // Required architecture for this file (see EM_*)
486 Elf_Word e_version; // Must be equal to 1
487 Elf_Addr e_entry; // Address to jump to in order to start program
488 Elf_Off e_phoff; // Program header table's file offset, in bytes
489 Elf_Off e_shoff; // Section header table's file offset, in bytes
490 Elf_Word e_flags; // Processor-specific flags
491 Elf_Half e_ehsize; // Size of ELF header, in bytes
492 Elf_Half e_phentsize; // Size of an entry in the program header table
493 Elf_Half e_phnum; // Number of entries in the program header table
494 Elf_Half e_shentsize; // Size of an entry in the section header table
495 Elf_Half e_shnum; // Number of entries in the section header table
496 Elf_Half e_shstrndx; // Section header table index of section name
497 // string table
498
499 bool checkMagic() const {
500 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
501 }
502
503 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
504 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
505};
506
507template <endianness TargetEndianness>
508struct Elf_Phdr_Impl<ELFType<TargetEndianness, false>> {
509 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
510 Elf_Word p_type; // Type of segment
511 Elf_Off p_offset; // FileOffset where segment is located, in bytes
512 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
513 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
514 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
515 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
516 Elf_Word p_flags; // Segment flags
517 Elf_Word p_align; // Segment alignment constraint
518};
519
520template <endianness TargetEndianness>
521struct Elf_Phdr_Impl<ELFType<TargetEndianness, true>> {
522 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
523 Elf_Word p_type; // Type of segment
524 Elf_Word p_flags; // Segment flags
525 Elf_Off p_offset; // FileOffset where segment is located, in bytes
526 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
527 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
528 Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
529 Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero)
530 Elf_Xword p_align; // Segment alignment constraint
531};
532
533// ELFT needed for endianness.
534template <class ELFT>
537 Elf_Word nbucket;
538 Elf_Word nchain;
539
540 ArrayRef<Elf_Word> buckets() const {
541 return ArrayRef<Elf_Word>(&nbucket + 2, &nbucket + 2 + nbucket);
542 }
543
545 return ArrayRef<Elf_Word>(&nbucket + 2 + nbucket,
546 &nbucket + 2 + nbucket + nchain);
547 }
548};
549
550// .gnu.hash section
551template <class ELFT>
554 Elf_Word nbuckets;
555 Elf_Word symndx;
556 Elf_Word maskwords;
557 Elf_Word shift2;
558
559 ArrayRef<Elf_Off> filter() const {
560 return ArrayRef<Elf_Off>(reinterpret_cast<const Elf_Off *>(&shift2 + 1),
561 maskwords);
562 }
563
565 return ArrayRef<Elf_Word>(
566 reinterpret_cast<const Elf_Word *>(filter().end()), nbuckets);
567 }
568
569 ArrayRef<Elf_Word> values(unsigned DynamicSymCount) const {
570 assert(DynamicSymCount >= symndx);
571 return ArrayRef<Elf_Word>(buckets().end(), DynamicSymCount - symndx);
572 }
573};
574
575// Compressed section headers.
576// http://www.sco.com/developers/gabi/latest/ch4.sheader.html#compression_header
577template <endianness TargetEndianness>
578struct Elf_Chdr_Impl<ELFType<TargetEndianness, false>> {
579 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
580 Elf_Word ch_type;
581 Elf_Word ch_size;
582 Elf_Word ch_addralign;
583};
584
585template <endianness TargetEndianness>
586struct Elf_Chdr_Impl<ELFType<TargetEndianness, true>> {
587 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
588 Elf_Word ch_type;
589 Elf_Word ch_reserved;
590 Elf_Xword ch_size;
591 Elf_Xword ch_addralign;
592};
593
594/// Note header
595template <class ELFT>
598 Elf_Word n_namesz;
599 Elf_Word n_descsz;
600 Elf_Word n_type;
601
602 /// Get the size of the note, including name, descriptor, and padding. Both
603 /// the start and the end of the descriptor are aligned by the section
604 /// alignment. In practice many 64-bit systems deviate from the generic ABI by
605 /// using sh_addralign=4.
606 size_t getSize(size_t Align) const {
607 return alignToPowerOf2(sizeof(*this) + n_namesz, Align) +
608 alignToPowerOf2(n_descsz, Align);
609 }
610};
611
612/// An ELF note.
613///
614/// Wraps a note header, providing methods for accessing the name and
615/// descriptor safely.
616template <class ELFT>
619
620 const Elf_Nhdr_Impl<ELFT> &Nhdr;
621
622 template <class NoteIteratorELFT> friend class Elf_Note_Iterator_Impl;
623
624public:
625 Elf_Note_Impl(const Elf_Nhdr_Impl<ELFT> &Nhdr) : Nhdr(Nhdr) {}
626
627 /// Get the note's name, excluding the terminating null byte.
629 if (!Nhdr.n_namesz)
630 return StringRef();
631 return StringRef(reinterpret_cast<const char *>(&Nhdr) + sizeof(Nhdr),
632 Nhdr.n_namesz - 1);
633 }
634
635 /// Get the note's descriptor.
637 if (!Nhdr.n_descsz)
638 return ArrayRef<uint8_t>();
639 return ArrayRef<uint8_t>(
640 reinterpret_cast<const uint8_t *>(&Nhdr) +
641 alignToPowerOf2(sizeof(Nhdr) + Nhdr.n_namesz, Align),
642 Nhdr.n_descsz);
643 }
644
645 /// Get the note's descriptor as StringRef
647 ArrayRef<uint8_t> Desc = getDesc(Align);
648 return StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
649 }
650
651 /// Get the note's type.
652 Elf_Word getType() const { return Nhdr.n_type; }
653};
654
655template <class ELFT> class Elf_Note_Iterator_Impl {
656public:
657 using iterator_category = std::forward_iterator_tag;
659 using difference_type = std::ptrdiff_t;
662
663private:
664 // Nhdr being a nullptr marks the end of iteration.
665 const Elf_Nhdr_Impl<ELFT> *Nhdr = nullptr;
666 size_t RemainingSize = 0u;
667 size_t Align = 0;
668 Error *Err = nullptr;
669
670 template <class ELFFileELFT> friend class ELFFile;
671
672 // Stop iteration and indicate an overflow.
673 void stopWithOverflowError() {
674 Nhdr = nullptr;
675 *Err = make_error<StringError>("ELF note overflows container",
676 object_error::parse_failed);
677 }
678
679 // Advance Nhdr by NoteSize bytes, starting from NhdrPos.
680 //
681 // Assumes NoteSize <= RemainingSize. Ensures Nhdr->getSize() <= RemainingSize
682 // upon returning. Handles stopping iteration when reaching the end of the
683 // container, either cleanly or with an overflow error.
684 void advanceNhdr(const uint8_t *NhdrPos, size_t NoteSize) {
685 RemainingSize -= NoteSize;
686 if (RemainingSize == 0u) {
687 // Ensure that if the iterator walks to the end, the error is checked
688 // afterwards.
689 *Err = Error::success();
690 Nhdr = nullptr;
691 } else if (sizeof(*Nhdr) > RemainingSize)
692 stopWithOverflowError();
693 else {
694 Nhdr = reinterpret_cast<const Elf_Nhdr_Impl<ELFT> *>(NhdrPos + NoteSize);
695 if (Nhdr->getSize(Align) > RemainingSize)
696 stopWithOverflowError();
697 else
698 *Err = Error::success();
699 }
700 }
701
702 Elf_Note_Iterator_Impl() = default;
703 explicit Elf_Note_Iterator_Impl(Error &Err) : Err(&Err) {}
704 Elf_Note_Iterator_Impl(const uint8_t *Start, size_t Size, size_t Align,
705 Error &Err)
706 : RemainingSize(Size), Align(Align), Err(&Err) {
707 consumeError(std::move(Err));
708 assert(Start && "ELF note iterator starting at NULL");
709 advanceNhdr(Start, 0u);
710 }
711
712public:
714 assert(Nhdr && "incremented ELF note end iterator");
715 const uint8_t *NhdrPos = reinterpret_cast<const uint8_t *>(Nhdr);
716 size_t NoteSize = Nhdr->getSize(Align);
717 advanceNhdr(NhdrPos, NoteSize);
718 return *this;
719 }
721 if (!Nhdr && Other.Err)
722 (void)(bool)(*Other.Err);
723 if (!Other.Nhdr && Err)
724 (void)(bool)(*Err);
725 return Nhdr == Other.Nhdr;
726 }
728 return !(*this == Other);
729 }
731 assert(Nhdr && "dereferenced ELF note end iterator");
732 return Elf_Note_Impl<ELFT>(*Nhdr);
733 }
734};
735
736template <class ELFT> struct Elf_CGProfile_Impl {
738 Elf_Xword cgp_weight;
739};
740
741// MIPS .reginfo section
742template <class ELFT>
744
745template <llvm::endianness TargetEndianness>
746struct Elf_Mips_RegInfo<ELFType<TargetEndianness, false>> {
747 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
748 Elf_Word ri_gprmask; // bit-mask of used general registers
749 Elf_Word ri_cprmask[4]; // bit-mask of used co-processor registers
750 Elf_Addr ri_gp_value; // gp register value
751};
752
753template <llvm::endianness TargetEndianness>
754struct Elf_Mips_RegInfo<ELFType<TargetEndianness, true>> {
755 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
756 Elf_Word ri_gprmask; // bit-mask of used general registers
757 Elf_Word ri_pad; // unused padding field
758 Elf_Word ri_cprmask[4]; // bit-mask of used co-processor registers
759 Elf_Addr ri_gp_value; // gp register value
760};
761
762// .MIPS.options section
763template <class ELFT> struct Elf_Mips_Options {
765 uint8_t kind; // Determines interpretation of variable part of descriptor
766 uint8_t size; // Byte size of descriptor, including this header
767 Elf_Half section; // Section header index of section affected,
768 // or 0 for global options
769 Elf_Word info; // Kind-specific information
770
771 Elf_Mips_RegInfo<ELFT> &getRegInfo() {
772 assert(kind == ELF::ODK_REGINFO);
773 return *reinterpret_cast<Elf_Mips_RegInfo<ELFT> *>(
774 (uint8_t *)this + sizeof(Elf_Mips_Options));
775 }
777 return const_cast<Elf_Mips_Options *>(this)->getRegInfo();
778 }
779};
780
781// .MIPS.abiflags section content
782template <class ELFT> struct Elf_Mips_ABIFlags {
784 Elf_Half version; // Version of the structure
785 uint8_t isa_level; // ISA level: 1-5, 32, and 64
786 uint8_t isa_rev; // ISA revision (0 for MIPS I - MIPS V)
787 uint8_t gpr_size; // General purpose registers size
788 uint8_t cpr1_size; // Co-processor 1 registers size
789 uint8_t cpr2_size; // Co-processor 2 registers size
790 uint8_t fp_abi; // Floating-point ABI flag
791 Elf_Word isa_ext; // Processor-specific extension
792 Elf_Word ases; // ASEs flags
793 Elf_Word flags1; // General flags
794 Elf_Word flags2; // General flags
795};
796
797// Struct representing the BBAddrMap for one function.
798struct BBAddrMap {
799
800 // Bitfield of optional features to control the extra information
801 // emitted/encoded in the the section.
802 struct Features {
804 bool BBFreq : 1;
805 bool BrProb : 1;
806 bool MultiBBRange : 1;
807
808 bool hasPGOAnalysis() const { return FuncEntryCount || BBFreq || BrProb; }
809
810 bool hasPGOAnalysisBBData() const { return BBFreq || BrProb; }
811
812 // Encodes to minimum bit width representation.
813 uint8_t encode() const {
814 return (static_cast<uint8_t>(FuncEntryCount) << 0) |
815 (static_cast<uint8_t>(BBFreq) << 1) |
816 (static_cast<uint8_t>(BrProb) << 2) |
817 (static_cast<uint8_t>(MultiBBRange) << 3);
818 }
819
820 // Decodes from minimum bit width representation and validates no
821 // unnecessary bits are used.
822 static Expected<Features> decode(uint8_t Val) {
823 Features Feat{
824 static_cast<bool>(Val & (1 << 0)), static_cast<bool>(Val & (1 << 1)),
825 static_cast<bool>(Val & (1 << 2)), static_cast<bool>(Val & (1 << 3))};
826 if (Feat.encode() != Val)
827 return createStringError(
828 std::error_code(), "invalid encoding for BBAddrMap::Features: 0x%x",
829 Val);
830 return Feat;
831 }
832
833 bool operator==(const Features &Other) const {
834 return std::tie(FuncEntryCount, BBFreq, BrProb, MultiBBRange) ==
835 std::tie(Other.FuncEntryCount, Other.BBFreq, Other.BrProb,
836 Other.MultiBBRange);
837 }
838 };
839
840 // Struct representing the BBAddrMap information for one basic block.
841 struct BBEntry {
842 struct Metadata {
843 bool HasReturn : 1; // If this block ends with a return (or tail
844 // call).
845 bool HasTailCall : 1; // If this block ends with a tail call.
846 bool IsEHPad : 1; // If this is an exception handling block.
847 bool CanFallThrough : 1; // If this block can fall through to its next.
848 bool HasIndirectBranch : 1; // If this block ends with an indirect branch
849 // (branch via a register).
850
851 bool operator==(const Metadata &Other) const {
852 return HasReturn == Other.HasReturn &&
853 HasTailCall == Other.HasTailCall && IsEHPad == Other.IsEHPad &&
854 CanFallThrough == Other.CanFallThrough &&
855 HasIndirectBranch == Other.HasIndirectBranch;
856 }
857
858 // Encodes this struct as a uint32_t value.
859 uint32_t encode() const {
860 return static_cast<uint32_t>(HasReturn) |
861 (static_cast<uint32_t>(HasTailCall) << 1) |
862 (static_cast<uint32_t>(IsEHPad) << 2) |
863 (static_cast<uint32_t>(CanFallThrough) << 3) |
864 (static_cast<uint32_t>(HasIndirectBranch) << 4);
865 }
866
867 // Decodes and returns a Metadata struct from a uint32_t value.
869 Metadata MD{/*HasReturn=*/static_cast<bool>(V & 1),
870 /*HasTailCall=*/static_cast<bool>(V & (1 << 1)),
871 /*IsEHPad=*/static_cast<bool>(V & (1 << 2)),
872 /*CanFallThrough=*/static_cast<bool>(V & (1 << 3)),
873 /*HasIndirectBranch=*/static_cast<bool>(V & (1 << 4))};
874 if (MD.encode() != V)
875 return createStringError(
876 std::error_code(), "invalid encoding for BBEntry::Metadata: 0x%x",
877 V);
878 return MD;
879 }
880 };
881
882 uint32_t ID = 0; // Unique ID of this basic block.
883 uint32_t Offset = 0; // Offset of basic block relative to the base address.
884 uint32_t Size = 0; // Size of the basic block.
885 Metadata MD = {false, false, false, false,
886 false}; // Metdata for this basic block.
887
889 : ID(ID), Offset(Offset), Size(Size), MD(MD){};
890
891 bool operator==(const BBEntry &Other) const {
892 return ID == Other.ID && Offset == Other.Offset && Size == Other.Size &&
893 MD == Other.MD;
894 }
895
896 bool hasReturn() const { return MD.HasReturn; }
897 bool hasTailCall() const { return MD.HasTailCall; }
898 bool isEHPad() const { return MD.IsEHPad; }
899 bool canFallThrough() const { return MD.CanFallThrough; }
900 bool hasIndirectBranch() const { return MD.HasIndirectBranch; }
901 };
902
903 // Struct representing the BBAddrMap information for a contiguous range of
904 // basic blocks (a function or a basic block section).
906 uint64_t BaseAddress = 0; // Base address of the range.
907 std::vector<BBEntry> BBEntries; // Basic block entries for this range.
908
909 // Equality operator for unit testing.
910 bool operator==(const BBRangeEntry &Other) const {
911 return BaseAddress == Other.BaseAddress &&
912 std::equal(BBEntries.begin(), BBEntries.end(),
913 Other.BBEntries.begin());
914 }
915 };
916
917 // All ranges for this function. Cannot be empty. The first range always
918 // corresponds to the function entry.
919 std::vector<BBRangeEntry> BBRanges;
920
921 // Returns the function address associated with this BBAddrMap, which is
922 // stored as the `BaseAddress` of its first BBRangeEntry.
924 assert(!BBRanges.empty());
925 return BBRanges.front().BaseAddress;
926 }
927
928 // Returns the total number of bb entries in all bb ranges.
929 size_t getNumBBEntries() const {
930 size_t NumBBEntries = 0;
931 for (const auto &BBR : BBRanges)
932 NumBBEntries += BBR.BBEntries.size();
933 return NumBBEntries;
934 }
935
936 // Returns the index of the bb range with the given base address, or
937 // `std::nullopt` if no such range exists.
938 std::optional<size_t>
940 for (size_t I = 0; I < BBRanges.size(); ++I)
941 if (BBRanges[I].BaseAddress == BaseAddress)
942 return I;
943 return {};
944 }
945
946 // Returns bb entries in the first range.
947 const std::vector<BBEntry> &getBBEntries() const {
948 return BBRanges.front().BBEntries;
949 }
950
951 const std::vector<BBRangeEntry> &getBBRanges() const { return BBRanges; }
952
953 // Equality operator for unit testing.
954 bool operator==(const BBAddrMap &Other) const {
955 return std::equal(BBRanges.begin(), BBRanges.end(), Other.BBRanges.begin());
956 }
957};
958
959/// A feature extension of BBAddrMap that holds information relevant to PGO.
961 /// Extra basic block data with fields for block frequency and branch
962 /// probability.
963 struct PGOBBEntry {
964 /// Single successor of a given basic block that contains the tag and branch
965 /// probability associated with it.
967 /// Unique ID of this successor basic block.
969 /// Branch Probability of the edge to this successor taken from MBPI.
971
972 bool operator==(const SuccessorEntry &Other) const {
973 return std::tie(ID, Prob) == std::tie(Other.ID, Other.Prob);
974 }
975 };
976
977 /// Block frequency taken from MBFI
979 /// List of successors of the current block
981
982 bool operator==(const PGOBBEntry &Other) const {
983 return std::tie(BlockFreq, Successors) ==
984 std::tie(Other.BlockFreq, Other.Successors);
985 }
986 };
987
988 uint64_t FuncEntryCount; // Prof count from IR function
989 std::vector<PGOBBEntry> BBEntries; // Extended basic block entries
990
991 // Flags to indicate if each PGO related info was enabled in this function
993
994 bool operator==(const PGOAnalysisMap &Other) const {
995 return std::tie(FuncEntryCount, BBEntries, FeatEnable) ==
996 std::tie(Other.FuncEntryCount, Other.BBEntries, Other.FeatEnable);
997 }
998};
999
1000} // end namespace object.
1001} // end namespace llvm.
1002
1003#endif // LLVM_OBJECT_ELFTYPES_H
aarch64 promote const
basic Basic Alias true
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static bool isMips64EL(const ELFYAML::Object &Obj)
uint64_t Align
uint64_t Size
#define LLVM_ELF_IMPORT_TYPES(E, W)
Definition: ELFTypes.h:142
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition: ELFTypes.h:104
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1290
uint64_t Offset
Definition: ELF_riscv.cpp:478
lazy value info
#define I(x, y, z)
Definition: MD5.cpp:58
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:911
static StringRef getName(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:40
static unsigned getSize(unsigned Kind)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:474
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
StringRef getDescAsStringRef(size_t Align) const
Get the note's descriptor as StringRef.
Definition: ELFTypes.h:646
Elf_Word getType() const
Get the note's type.
Definition: ELFTypes.h:652
Elf_Note_Impl(const Elf_Nhdr_Impl< ELFT > &Nhdr)
Definition: ELFTypes.h:625
StringRef getName() const
Get the note's name, excluding the terminating null byte.
Definition: ELFTypes.h:628
ArrayRef< uint8_t > getDesc(size_t Align) const
Get the note's descriptor.
Definition: ELFTypes.h:636
bool operator!=(Elf_Note_Iterator_Impl Other) const
Definition: ELFTypes.h:727
std::forward_iterator_tag iterator_category
Definition: ELFTypes.h:657
Elf_Note_Impl< ELFT > operator*() const
Definition: ELFTypes.h:730
Elf_Note_Iterator_Impl & operator++()
Definition: ELFTypes.h:713
bool operator==(Elf_Note_Iterator_Impl Other) const
Definition: ELFTypes.h:720
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align)
Definition: MathExtras.h:382
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1258
endianness
Definition: bit.h:70
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Description of the encoding of one expression Op.
static Expected< Metadata > decode(uint32_t V)
Definition: ELFTypes.h:868
bool operator==(const Metadata &Other) const
Definition: ELFTypes.h:851
bool operator==(const BBEntry &Other) const
Definition: ELFTypes.h:891
BBEntry(uint32_t ID, uint32_t Offset, uint32_t Size, Metadata MD)
Definition: ELFTypes.h:888
bool operator==(const BBRangeEntry &Other) const
Definition: ELFTypes.h:910
std::vector< BBEntry > BBEntries
Definition: ELFTypes.h:907
bool operator==(const Features &Other) const
Definition: ELFTypes.h:833
static Expected< Features > decode(uint8_t Val)
Definition: ELFTypes.h:822
const std::vector< BBRangeEntry > & getBBRanges() const
Definition: ELFTypes.h:951
std::vector< BBRangeEntry > BBRanges
Definition: ELFTypes.h:919
size_t getNumBBEntries() const
Definition: ELFTypes.h:929
bool operator==(const BBAddrMap &Other) const
Definition: ELFTypes.h:954
const std::vector< BBEntry > & getBBEntries() const
Definition: ELFTypes.h:947
uint64_t getFunctionAddress() const
Definition: ELFTypes.h:923
std::optional< size_t > getBBRangeIndexForBaseAddress(uint64_t BaseAddress) const
Definition: ELFTypes.h:939
std::conditional_t< Is64, uint64_t, uint32_t > uint
Definition: ELFTypes.h:57
static const bool Is64Bits
Definition: ELFTypes.h:55
static const endianness TargetEndianness
Definition: ELFTypes.h:54
Elf_Dyn_Base: This structure matches the form of entries in the dynamic table section (....
Definition: ELFTypes.h:350
Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters.
Definition: ELFTypes.h:374
uintX_t getVal() const
Definition: ELFTypes.h:380
std::conditional_t< ELFT::Is64Bits, int64_t, int32_t > intX_t
Definition: ELFTypes.h:377
std::conditional_t< ELFT::Is64Bits, uint64_t, uint32_t > uintX_t
Definition: ELFTypes.h:378
uintX_t getPtr() const
Definition: ELFTypes.h:381
intX_t getTag() const
Definition: ELFTypes.h:379
unsigned char getFileClass() const
Definition: ELFTypes.h:503
unsigned char getDataEncoding() const
Definition: ELFTypes.h:504
ArrayRef< Elf_Word > values(unsigned DynamicSymCount) const
Definition: ELFTypes.h:569
ArrayRef< Elf_Word > buckets() const
Definition: ELFTypes.h:564
ArrayRef< Elf_Word > chains() const
Definition: ELFTypes.h:544
const Elf_Mips_RegInfo< ELFT > & getRegInfo() const
Definition: ELFTypes.h:776
size_t getSize(size_t Align) const
Get the size of the note, including name, descriptor, and padding.
Definition: ELFTypes.h:606
void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL)
Definition: ELFTypes.h:414
void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL)
Definition: ELFTypes.h:467
unsigned getEntityCount() const
Get the number of entities this section contains if it has any.
Definition: ELFTypes.h:184
void setBindingAndType(unsigned char b, unsigned char t)
Definition: ELFTypes.h:230
bool isProcessorSpecific() const
Definition: ELFTypes.h:253
void setBinding(unsigned char b)
Definition: ELFTypes.h:227
unsigned char getBinding() const
Definition: ELFTypes.h:224
bool isDefined() const
Definition: ELFTypes.h:251
bool isAbsolute() const
Definition: ELFTypes.h:245
bool isOSSpecific() const
Definition: ELFTypes.h:257
unsigned char getType() const
Definition: ELFTypes.h:225
uint64_t getValue() const
Definition: ELFTypes.h:226
void setVisibility(unsigned char v)
Definition: ELFTypes.h:240
bool isExternal() const
Definition: ELFTypes.h:269
void setType(unsigned char t)
Definition: ELFTypes.h:228
bool isUndefined() const
Definition: ELFTypes.h:267
bool isReserved() const
Definition: ELFTypes.h:261
unsigned char getVisibility() const
Access to the STV_xxx flag stored in the first two bits of st_other.
Definition: ELFTypes.h:239
Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef section (....
Definition: ELFTypes.h:318
Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section (.gnu.version_d).
Definition: ELFTypes.h:299
Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed section (....
Definition: ELFTypes.h:339
Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed section (.gnu....
Definition: ELFTypes.h:327
Elf_Versym: This is the structure of entries in the SHT_GNU_versym section (.gnu.version).
Definition: ELFTypes.h:291
Single successor of a given basic block that contains the tag and branch probability associated with ...
Definition: ELFTypes.h:966
uint32_t ID
Unique ID of this successor basic block.
Definition: ELFTypes.h:968
BranchProbability Prob
Branch Probability of the edge to this successor taken from MBPI.
Definition: ELFTypes.h:970
bool operator==(const SuccessorEntry &Other) const
Definition: ELFTypes.h:972
Extra basic block data with fields for block frequency and branch probability.
Definition: ELFTypes.h:963
bool operator==(const PGOBBEntry &Other) const
Definition: ELFTypes.h:982
llvm::SmallVector< SuccessorEntry, 2 > Successors
List of successors of the current block.
Definition: ELFTypes.h:980
BlockFrequency BlockFreq
Block frequency taken from MBFI.
Definition: ELFTypes.h:978
A feature extension of BBAddrMap that holds information relevant to PGO.
Definition: ELFTypes.h:960
bool operator==(const PGOAnalysisMap &Other) const
Definition: ELFTypes.h:994
std::vector< PGOBBEntry > BBEntries
Definition: ELFTypes.h:989
BBAddrMap::Features FeatEnable
Definition: ELFTypes.h:992