LLVM 18.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"
16#include "llvm/Support/Endian.h"
17#include "llvm/Support/Error.h"
19#include <cassert>
20#include <cstdint>
21#include <cstring>
22#include <type_traits>
23
24namespace llvm {
25namespace object {
26
27template <class ELFT> struct Elf_Ehdr_Impl;
28template <class ELFT> struct Elf_Shdr_Impl;
29template <class ELFT> struct Elf_Sym_Impl;
30template <class ELFT> struct Elf_Dyn_Impl;
31template <class ELFT> struct Elf_Phdr_Impl;
32template <class ELFT, bool isRela> struct Elf_Rel_Impl;
33template <class ELFT> struct Elf_Verdef_Impl;
34template <class ELFT> struct Elf_Verdaux_Impl;
35template <class ELFT> struct Elf_Verneed_Impl;
36template <class ELFT> struct Elf_Vernaux_Impl;
37template <class ELFT> struct Elf_Versym_Impl;
38template <class ELFT> struct Elf_Hash_Impl;
39template <class ELFT> struct Elf_GnuHash_Impl;
40template <class ELFT> struct Elf_Chdr_Impl;
41template <class ELFT> struct Elf_Nhdr_Impl;
42template <class ELFT> class Elf_Note_Impl;
43template <class ELFT> class Elf_Note_Iterator_Impl;
44template <class ELFT> struct Elf_CGProfile_Impl;
45
46template <endianness E, bool Is64> struct ELFType {
47private:
48 template <typename Ty>
50
51public:
53 static const bool Is64Bits = Is64;
54
55 using uint = std::conditional_t<Is64, uint64_t, uint32_t>;
83
91};
92
97
98// Use an alignment of 2 for the typedefs since that is the worst case for
99// ELF files in archives.
100
101// I really don't like doing this, but the alternative is copypasta.
102#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) \
103 using Elf_Addr = typename ELFT::Addr; \
104 using Elf_Off = typename ELFT::Off; \
105 using Elf_Half = typename ELFT::Half; \
106 using Elf_Word = typename ELFT::Word; \
107 using Elf_Sword = typename ELFT::Sword; \
108 using Elf_Xword = typename ELFT::Xword; \
109 using Elf_Sxword = typename ELFT::Sxword; \
110 using uintX_t = typename ELFT::uint; \
111 using Elf_Ehdr = typename ELFT::Ehdr; \
112 using Elf_Shdr = typename ELFT::Shdr; \
113 using Elf_Sym = typename ELFT::Sym; \
114 using Elf_Dyn = typename ELFT::Dyn; \
115 using Elf_Phdr = typename ELFT::Phdr; \
116 using Elf_Rel = typename ELFT::Rel; \
117 using Elf_Rela = typename ELFT::Rela; \
118 using Elf_Relr = typename ELFT::Relr; \
119 using Elf_Verdef = typename ELFT::Verdef; \
120 using Elf_Verdaux = typename ELFT::Verdaux; \
121 using Elf_Verneed = typename ELFT::Verneed; \
122 using Elf_Vernaux = typename ELFT::Vernaux; \
123 using Elf_Versym = typename ELFT::Versym; \
124 using Elf_Hash = typename ELFT::Hash; \
125 using Elf_GnuHash = typename ELFT::GnuHash; \
126 using Elf_Chdr = typename ELFT::Chdr; \
127 using Elf_Nhdr = typename ELFT::Nhdr; \
128 using Elf_Note = typename ELFT::Note; \
129 using Elf_Note_Iterator = typename ELFT::NoteIterator; \
130 using Elf_CGProfile = typename ELFT::CGProfile; \
131 using Elf_Dyn_Range = typename ELFT::DynRange; \
132 using Elf_Shdr_Range = typename ELFT::ShdrRange; \
133 using Elf_Sym_Range = typename ELFT::SymRange; \
134 using Elf_Rel_Range = typename ELFT::RelRange; \
135 using Elf_Rela_Range = typename ELFT::RelaRange; \
136 using Elf_Relr_Range = typename ELFT::RelrRange; \
137 using Elf_Phdr_Range = typename ELFT::PhdrRange;
138
139#define LLVM_ELF_COMMA ,
140#define LLVM_ELF_IMPORT_TYPES(E, W) \
141 LLVM_ELF_IMPORT_TYPES_ELFT(ELFType<E LLVM_ELF_COMMA W>)
142
143// Section header.
144template <class ELFT> struct Elf_Shdr_Base;
145
146template <endianness TargetEndianness>
147struct Elf_Shdr_Base<ELFType<TargetEndianness, false>> {
148 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
149 Elf_Word sh_name; // Section name (index into string table)
150 Elf_Word sh_type; // Section type (SHT_*)
151 Elf_Word sh_flags; // Section flags (SHF_*)
152 Elf_Addr sh_addr; // Address where section is to be loaded
153 Elf_Off sh_offset; // File offset of section data, in bytes
154 Elf_Word sh_size; // Size of section, in bytes
155 Elf_Word sh_link; // Section type-specific header table index link
156 Elf_Word sh_info; // Section type-specific extra information
157 Elf_Word sh_addralign; // Section address alignment
158 Elf_Word sh_entsize; // Size of records contained within the section
159};
160
161template <endianness TargetEndianness>
162struct Elf_Shdr_Base<ELFType<TargetEndianness, true>> {
163 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
164 Elf_Word sh_name; // Section name (index into string table)
165 Elf_Word sh_type; // Section type (SHT_*)
166 Elf_Xword sh_flags; // Section flags (SHF_*)
167 Elf_Addr sh_addr; // Address where section is to be loaded
168 Elf_Off sh_offset; // File offset of section data, in bytes
169 Elf_Xword sh_size; // Size of section, in bytes
170 Elf_Word sh_link; // Section type-specific header table index link
171 Elf_Word sh_info; // Section type-specific extra information
172 Elf_Xword sh_addralign; // Section address alignment
173 Elf_Xword sh_entsize; // Size of records contained within the section
174};
175
176template <class ELFT>
178 using Elf_Shdr_Base<ELFT>::sh_entsize;
179 using Elf_Shdr_Base<ELFT>::sh_size;
180
181 /// Get the number of entities this section contains if it has any.
182 unsigned getEntityCount() const {
183 if (sh_entsize == 0)
184 return 0;
185 return sh_size / sh_entsize;
186 }
187};
188
189template <class ELFT> struct Elf_Sym_Base;
190
191template <endianness TargetEndianness>
192struct Elf_Sym_Base<ELFType<TargetEndianness, false>> {
193 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
194 Elf_Word st_name; // Symbol name (index into string table)
195 Elf_Addr st_value; // Value or address associated with the symbol
196 Elf_Word st_size; // Size of the symbol
197 unsigned char st_info; // Symbol's type and binding attributes
198 unsigned char st_other; // Must be zero; reserved
199 Elf_Half st_shndx; // Which section (header table index) it's defined in
200};
201
202template <endianness TargetEndianness>
203struct Elf_Sym_Base<ELFType<TargetEndianness, true>> {
204 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
205 Elf_Word st_name; // Symbol name (index into string table)
206 unsigned char st_info; // Symbol's type and binding attributes
207 unsigned char st_other; // Must be zero; reserved
208 Elf_Half st_shndx; // Which section (header table index) it's defined in
209 Elf_Addr st_value; // Value or address associated with the symbol
210 Elf_Xword st_size; // Size of the symbol
211};
212
213template <class ELFT>
215 using Elf_Sym_Base<ELFT>::st_info;
216 using Elf_Sym_Base<ELFT>::st_shndx;
217 using Elf_Sym_Base<ELFT>::st_other;
218 using Elf_Sym_Base<ELFT>::st_value;
219
220 // These accessors and mutators correspond to the ELF32_ST_BIND,
221 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
222 unsigned char getBinding() const { return st_info >> 4; }
223 unsigned char getType() const { return st_info & 0x0f; }
224 uint64_t getValue() const { return st_value; }
225 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
226 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
227
228 void setBindingAndType(unsigned char b, unsigned char t) {
229 st_info = (b << 4) + (t & 0x0f);
230 }
231
232 /// Access to the STV_xxx flag stored in the first two bits of st_other.
233 /// STV_DEFAULT: 0
234 /// STV_INTERNAL: 1
235 /// STV_HIDDEN: 2
236 /// STV_PROTECTED: 3
237 unsigned char getVisibility() const { return st_other & 0x3; }
238 void setVisibility(unsigned char v) {
239 assert(v < 4 && "Invalid value for visibility");
240 st_other = (st_other & ~0x3) | v;
241 }
242
243 bool isAbsolute() const { return st_shndx == ELF::SHN_ABS; }
244
245 bool isCommon() const {
246 return getType() == ELF::STT_COMMON || st_shndx == ELF::SHN_COMMON;
247 }
248
249 bool isDefined() const { return !isUndefined(); }
250
251 bool isProcessorSpecific() const {
252 return st_shndx >= ELF::SHN_LOPROC && st_shndx <= ELF::SHN_HIPROC;
253 }
254
255 bool isOSSpecific() const {
256 return st_shndx >= ELF::SHN_LOOS && st_shndx <= ELF::SHN_HIOS;
257 }
258
259 bool isReserved() const {
260 // ELF::SHN_HIRESERVE is 0xffff so st_shndx <= ELF::SHN_HIRESERVE is always
261 // true and some compilers warn about it.
262 return st_shndx >= ELF::SHN_LORESERVE;
263 }
264
265 bool isUndefined() const { return st_shndx == ELF::SHN_UNDEF; }
266
267 bool isExternal() const {
268 return getBinding() != ELF::STB_LOCAL;
269 }
270
272};
273
274template <class ELFT>
276 uint32_t Offset = this->st_name;
277 if (Offset >= StrTab.size())
278 return createStringError(object_error::parse_failed,
279 "st_name (0x%" PRIx32
280 ") is past the end of the string table"
281 " of size 0x%zx",
282 Offset, StrTab.size());
283 return StringRef(StrTab.data() + Offset);
284}
285
286/// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section
287/// (.gnu.version). This structure is identical for ELF32 and ELF64.
288template <class ELFT>
291 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN)
292};
293
294/// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section
295/// (.gnu.version_d). This structure is identical for ELF32 and ELF64.
296template <class ELFT>
299 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT)
300 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*)
301 Elf_Half vd_ndx; // Version index, used in .gnu.version entries
302 Elf_Half vd_cnt; // Number of Verdaux entries
303 Elf_Word vd_hash; // Hash of name
304 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes)
305 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes)
306
307 /// Get the first Verdaux entry for this Verdef.
308 const Elf_Verdaux *getAux() const {
309 return reinterpret_cast<const Elf_Verdaux *>((const char *)this + vd_aux);
310 }
311};
312
313/// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef
314/// section (.gnu.version_d). This structure is identical for ELF32 and ELF64.
315template <class ELFT>
318 Elf_Word vda_name; // Version name (offset in string table)
319 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes)
320};
321
322/// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed
323/// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
324template <class ELFT>
327 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT)
328 Elf_Half vn_cnt; // Number of associated Vernaux entries
329 Elf_Word vn_file; // Library name (string table offset)
330 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes)
331 Elf_Word vn_next; // Offset to next Verneed entry (in bytes)
332};
333
334/// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed
335/// section (.gnu.version_r). This structure is identical for ELF32 and ELF64.
336template <class ELFT>
339 Elf_Word vna_hash; // Hash of dependency name
340 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*)
341 Elf_Half vna_other; // Version index, used in .gnu.version entries
342 Elf_Word vna_name; // Dependency name
343 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes)
344};
345
346/// Elf_Dyn_Base: This structure matches the form of entries in the dynamic
347/// table section (.dynamic) look like.
348template <class ELFT> struct Elf_Dyn_Base;
349
350template <endianness TargetEndianness>
351struct Elf_Dyn_Base<ELFType<TargetEndianness, false>> {
352 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
353 Elf_Sword d_tag;
354 union {
355 Elf_Word d_val;
356 Elf_Addr d_ptr;
357 } d_un;
358};
359
360template <endianness TargetEndianness>
361struct Elf_Dyn_Base<ELFType<TargetEndianness, true>> {
362 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
363 Elf_Sxword d_tag;
364 union {
365 Elf_Xword d_val;
366 Elf_Addr d_ptr;
367 } d_un;
368};
369
370/// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters.
371template <class ELFT>
373 using Elf_Dyn_Base<ELFT>::d_tag;
374 using Elf_Dyn_Base<ELFT>::d_un;
375 using intX_t = std::conditional_t<ELFT::Is64Bits, int64_t, int32_t>;
376 using uintX_t = std::conditional_t<ELFT::Is64Bits, uint64_t, uint32_t>;
377 intX_t getTag() const { return d_tag; }
378 uintX_t getVal() const { return d_un.d_val; }
379 uintX_t getPtr() const { return d_un.d_ptr; }
380};
381
382template <endianness TargetEndianness>
383struct Elf_Rel_Impl<ELFType<TargetEndianness, false>, false> {
384 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
385 static const bool IsRela = false;
386 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
387 Elf_Word r_info; // Symbol table index and type of relocation to apply
388
389 uint32_t getRInfo(bool isMips64EL) const {
391 return r_info;
392 }
393 void setRInfo(uint32_t R, bool IsMips64EL) {
394 assert(!IsMips64EL);
395 r_info = R;
396 }
397
398 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
399 // and ELF32_R_INFO macros defined in the ELF specification:
401 return this->getRInfo(isMips64EL) >> 8;
402 }
403 unsigned char getType(bool isMips64EL) const {
404 return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff);
405 }
406 void setSymbol(uint32_t s, bool IsMips64EL) {
407 setSymbolAndType(s, getType(IsMips64EL), IsMips64EL);
408 }
409 void setType(unsigned char t, bool IsMips64EL) {
410 setSymbolAndType(getSymbol(IsMips64EL), t, IsMips64EL);
411 }
412 void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL) {
413 this->setRInfo((s << 8) + t, IsMips64EL);
414 }
415};
416
417template <endianness TargetEndianness>
418struct Elf_Rel_Impl<ELFType<TargetEndianness, false>, true>
419 : public Elf_Rel_Impl<ELFType<TargetEndianness, false>, false> {
420 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
421 static const bool IsRela = true;
422 Elf_Sword r_addend; // Compute value for relocatable field by adding this
423};
424
425template <endianness TargetEndianness>
426struct Elf_Rel_Impl<ELFType<TargetEndianness, true>, false> {
427 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
428 static const bool IsRela = false;
429 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
430 Elf_Xword r_info; // Symbol table index and type of relocation to apply
431
432 uint64_t getRInfo(bool isMips64EL) const {
433 uint64_t t = r_info;
434 if (!isMips64EL)
435 return t;
436 // Mips64 little endian has a "special" encoding of r_info. Instead of one
437 // 64 bit little endian number, it is a little endian 32 bit number followed
438 // by a 32 bit big endian number.
439 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) |
440 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff);
441 }
442
443 void setRInfo(uint64_t R, bool IsMips64EL) {
444 if (IsMips64EL)
445 r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) |
446 ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56);
447 else
448 r_info = R;
449 }
450
451 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
452 // and ELF64_R_INFO macros defined in the ELF specification:
454 return (uint32_t)(this->getRInfo(isMips64EL) >> 32);
455 }
457 return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL);
458 }
459 void setSymbol(uint32_t s, bool IsMips64EL) {
460 setSymbolAndType(s, getType(IsMips64EL), IsMips64EL);
461 }
462 void setType(uint32_t t, bool IsMips64EL) {
463 setSymbolAndType(getSymbol(IsMips64EL), t, IsMips64EL);
464 }
465 void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL) {
466 this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL), IsMips64EL);
467 }
468};
469
470template <endianness TargetEndianness>
471struct Elf_Rel_Impl<ELFType<TargetEndianness, true>, true>
472 : public Elf_Rel_Impl<ELFType<TargetEndianness, true>, false> {
473 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
474 static const bool IsRela = true;
475 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
476};
477
478template <class ELFT>
481 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
482 Elf_Half e_type; // Type of file (see ET_*)
483 Elf_Half e_machine; // Required architecture for this file (see EM_*)
484 Elf_Word e_version; // Must be equal to 1
485 Elf_Addr e_entry; // Address to jump to in order to start program
486 Elf_Off e_phoff; // Program header table's file offset, in bytes
487 Elf_Off e_shoff; // Section header table's file offset, in bytes
488 Elf_Word e_flags; // Processor-specific flags
489 Elf_Half e_ehsize; // Size of ELF header, in bytes
490 Elf_Half e_phentsize; // Size of an entry in the program header table
491 Elf_Half e_phnum; // Number of entries in the program header table
492 Elf_Half e_shentsize; // Size of an entry in the section header table
493 Elf_Half e_shnum; // Number of entries in the section header table
494 Elf_Half e_shstrndx; // Section header table index of section name
495 // string table
496
497 bool checkMagic() const {
498 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
499 }
500
501 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
502 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
503};
504
505template <endianness TargetEndianness>
506struct Elf_Phdr_Impl<ELFType<TargetEndianness, false>> {
507 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
508 Elf_Word p_type; // Type of segment
509 Elf_Off p_offset; // FileOffset where segment is located, in bytes
510 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
511 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
512 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
513 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
514 Elf_Word p_flags; // Segment flags
515 Elf_Word p_align; // Segment alignment constraint
516};
517
518template <endianness TargetEndianness>
519struct Elf_Phdr_Impl<ELFType<TargetEndianness, true>> {
520 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
521 Elf_Word p_type; // Type of segment
522 Elf_Word p_flags; // Segment flags
523 Elf_Off p_offset; // FileOffset where segment is located, in bytes
524 Elf_Addr p_vaddr; // Virtual Address of beginning of segment
525 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
526 Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
527 Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero)
528 Elf_Xword p_align; // Segment alignment constraint
529};
530
531// ELFT needed for endianness.
532template <class ELFT>
535 Elf_Word nbucket;
536 Elf_Word nchain;
537
538 ArrayRef<Elf_Word> buckets() const {
539 return ArrayRef<Elf_Word>(&nbucket + 2, &nbucket + 2 + nbucket);
540 }
541
543 return ArrayRef<Elf_Word>(&nbucket + 2 + nbucket,
544 &nbucket + 2 + nbucket + nchain);
545 }
546};
547
548// .gnu.hash section
549template <class ELFT>
552 Elf_Word nbuckets;
553 Elf_Word symndx;
554 Elf_Word maskwords;
555 Elf_Word shift2;
556
557 ArrayRef<Elf_Off> filter() const {
558 return ArrayRef<Elf_Off>(reinterpret_cast<const Elf_Off *>(&shift2 + 1),
559 maskwords);
560 }
561
563 return ArrayRef<Elf_Word>(
564 reinterpret_cast<const Elf_Word *>(filter().end()), nbuckets);
565 }
566
567 ArrayRef<Elf_Word> values(unsigned DynamicSymCount) const {
568 assert(DynamicSymCount >= symndx);
569 return ArrayRef<Elf_Word>(buckets().end(), DynamicSymCount - symndx);
570 }
571};
572
573// Compressed section headers.
574// http://www.sco.com/developers/gabi/latest/ch4.sheader.html#compression_header
575template <endianness TargetEndianness>
576struct Elf_Chdr_Impl<ELFType<TargetEndianness, false>> {
577 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
578 Elf_Word ch_type;
579 Elf_Word ch_size;
580 Elf_Word ch_addralign;
581};
582
583template <endianness TargetEndianness>
584struct Elf_Chdr_Impl<ELFType<TargetEndianness, true>> {
585 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
586 Elf_Word ch_type;
587 Elf_Word ch_reserved;
588 Elf_Xword ch_size;
589 Elf_Xword ch_addralign;
590};
591
592/// Note header
593template <class ELFT>
596 Elf_Word n_namesz;
597 Elf_Word n_descsz;
598 Elf_Word n_type;
599
600 /// Get the size of the note, including name, descriptor, and padding. Both
601 /// the start and the end of the descriptor are aligned by the section
602 /// alignment. In practice many 64-bit systems deviate from the generic ABI by
603 /// using sh_addralign=4.
604 size_t getSize(size_t Align) const {
605 return alignToPowerOf2(sizeof(*this) + n_namesz, Align) +
606 alignToPowerOf2(n_descsz, Align);
607 }
608};
609
610/// An ELF note.
611///
612/// Wraps a note header, providing methods for accessing the name and
613/// descriptor safely.
614template <class ELFT>
617
618 const Elf_Nhdr_Impl<ELFT> &Nhdr;
619
620 template <class NoteIteratorELFT> friend class Elf_Note_Iterator_Impl;
621
622public:
623 Elf_Note_Impl(const Elf_Nhdr_Impl<ELFT> &Nhdr) : Nhdr(Nhdr) {}
624
625 /// Get the note's name, excluding the terminating null byte.
627 if (!Nhdr.n_namesz)
628 return StringRef();
629 return StringRef(reinterpret_cast<const char *>(&Nhdr) + sizeof(Nhdr),
630 Nhdr.n_namesz - 1);
631 }
632
633 /// Get the note's descriptor.
635 if (!Nhdr.n_descsz)
636 return ArrayRef<uint8_t>();
637 return ArrayRef<uint8_t>(
638 reinterpret_cast<const uint8_t *>(&Nhdr) +
639 alignToPowerOf2(sizeof(Nhdr) + Nhdr.n_namesz, Align),
640 Nhdr.n_descsz);
641 }
642
643 /// Get the note's descriptor as StringRef
645 ArrayRef<uint8_t> Desc = getDesc(Align);
646 return StringRef(reinterpret_cast<const char *>(Desc.data()), Desc.size());
647 }
648
649 /// Get the note's type.
650 Elf_Word getType() const { return Nhdr.n_type; }
651};
652
653template <class ELFT> class Elf_Note_Iterator_Impl {
654public:
655 using iterator_category = std::forward_iterator_tag;
657 using difference_type = std::ptrdiff_t;
660
661private:
662 // Nhdr being a nullptr marks the end of iteration.
663 const Elf_Nhdr_Impl<ELFT> *Nhdr = nullptr;
664 size_t RemainingSize = 0u;
665 size_t Align = 0;
666 Error *Err = nullptr;
667
668 template <class ELFFileELFT> friend class ELFFile;
669
670 // Stop iteration and indicate an overflow.
671 void stopWithOverflowError() {
672 Nhdr = nullptr;
673 *Err = make_error<StringError>("ELF note overflows container",
674 object_error::parse_failed);
675 }
676
677 // Advance Nhdr by NoteSize bytes, starting from NhdrPos.
678 //
679 // Assumes NoteSize <= RemainingSize. Ensures Nhdr->getSize() <= RemainingSize
680 // upon returning. Handles stopping iteration when reaching the end of the
681 // container, either cleanly or with an overflow error.
682 void advanceNhdr(const uint8_t *NhdrPos, size_t NoteSize) {
683 RemainingSize -= NoteSize;
684 if (RemainingSize == 0u) {
685 // Ensure that if the iterator walks to the end, the error is checked
686 // afterwards.
687 *Err = Error::success();
688 Nhdr = nullptr;
689 } else if (sizeof(*Nhdr) > RemainingSize)
690 stopWithOverflowError();
691 else {
692 Nhdr = reinterpret_cast<const Elf_Nhdr_Impl<ELFT> *>(NhdrPos + NoteSize);
693 if (Nhdr->getSize(Align) > RemainingSize)
694 stopWithOverflowError();
695 else
696 *Err = Error::success();
697 }
698 }
699
700 Elf_Note_Iterator_Impl() = default;
701 explicit Elf_Note_Iterator_Impl(Error &Err) : Err(&Err) {}
702 Elf_Note_Iterator_Impl(const uint8_t *Start, size_t Size, size_t Align,
703 Error &Err)
704 : RemainingSize(Size), Align(Align), Err(&Err) {
705 consumeError(std::move(Err));
706 assert(Start && "ELF note iterator starting at NULL");
707 advanceNhdr(Start, 0u);
708 }
709
710public:
712 assert(Nhdr && "incremented ELF note end iterator");
713 const uint8_t *NhdrPos = reinterpret_cast<const uint8_t *>(Nhdr);
714 size_t NoteSize = Nhdr->getSize(Align);
715 advanceNhdr(NhdrPos, NoteSize);
716 return *this;
717 }
719 if (!Nhdr && Other.Err)
720 (void)(bool)(*Other.Err);
721 if (!Other.Nhdr && Err)
722 (void)(bool)(*Err);
723 return Nhdr == Other.Nhdr;
724 }
726 return !(*this == Other);
727 }
729 assert(Nhdr && "dereferenced ELF note end iterator");
730 return Elf_Note_Impl<ELFT>(*Nhdr);
731 }
732};
733
734template <class ELFT> struct Elf_CGProfile_Impl {
736 Elf_Xword cgp_weight;
737};
738
739// MIPS .reginfo section
740template <class ELFT>
742
743template <llvm::endianness TargetEndianness>
744struct Elf_Mips_RegInfo<ELFType<TargetEndianness, false>> {
745 LLVM_ELF_IMPORT_TYPES(TargetEndianness, false)
746 Elf_Word ri_gprmask; // bit-mask of used general registers
747 Elf_Word ri_cprmask[4]; // bit-mask of used co-processor registers
748 Elf_Addr ri_gp_value; // gp register value
749};
750
751template <llvm::endianness TargetEndianness>
752struct Elf_Mips_RegInfo<ELFType<TargetEndianness, true>> {
753 LLVM_ELF_IMPORT_TYPES(TargetEndianness, true)
754 Elf_Word ri_gprmask; // bit-mask of used general registers
755 Elf_Word ri_pad; // unused padding field
756 Elf_Word ri_cprmask[4]; // bit-mask of used co-processor registers
757 Elf_Addr ri_gp_value; // gp register value
758};
759
760// .MIPS.options section
761template <class ELFT> struct Elf_Mips_Options {
763 uint8_t kind; // Determines interpretation of variable part of descriptor
764 uint8_t size; // Byte size of descriptor, including this header
765 Elf_Half section; // Section header index of section affected,
766 // or 0 for global options
767 Elf_Word info; // Kind-specific information
768
769 Elf_Mips_RegInfo<ELFT> &getRegInfo() {
770 assert(kind == ELF::ODK_REGINFO);
771 return *reinterpret_cast<Elf_Mips_RegInfo<ELFT> *>(
772 (uint8_t *)this + sizeof(Elf_Mips_Options));
773 }
775 return const_cast<Elf_Mips_Options *>(this)->getRegInfo();
776 }
777};
778
779// .MIPS.abiflags section content
780template <class ELFT> struct Elf_Mips_ABIFlags {
782 Elf_Half version; // Version of the structure
783 uint8_t isa_level; // ISA level: 1-5, 32, and 64
784 uint8_t isa_rev; // ISA revision (0 for MIPS I - MIPS V)
785 uint8_t gpr_size; // General purpose registers size
786 uint8_t cpr1_size; // Co-processor 1 registers size
787 uint8_t cpr2_size; // Co-processor 2 registers size
788 uint8_t fp_abi; // Floating-point ABI flag
789 Elf_Word isa_ext; // Processor-specific extension
790 Elf_Word ases; // ASEs flags
791 Elf_Word flags1; // General flags
792 Elf_Word flags2; // General flags
793};
794
795// Struct representing the BBAddrMap for one function.
796struct BBAddrMap {
797 // Struct representing the BBAddrMap information for one basic block.
798 struct BBEntry {
799 struct Metadata {
800 bool HasReturn : 1; // If this block ends with a return (or tail
801 // call).
802 bool HasTailCall : 1; // If this block ends with a tail call.
803 bool IsEHPad : 1; // If this is an exception handling block.
804 bool CanFallThrough : 1; // If this block can fall through to its next.
805 bool HasIndirectBranch : 1; // If this block ends with an indirect branch
806 // (branch via a register).
807
808 bool operator==(const Metadata &Other) const {
809 return HasReturn == Other.HasReturn &&
810 HasTailCall == Other.HasTailCall && IsEHPad == Other.IsEHPad &&
811 CanFallThrough == Other.CanFallThrough &&
812 HasIndirectBranch == Other.HasIndirectBranch;
813 }
814
815 // Encodes this struct as a uint32_t value.
816 uint32_t encode() const {
817 return static_cast<uint32_t>(HasReturn) |
818 (static_cast<uint32_t>(HasTailCall) << 1) |
819 (static_cast<uint32_t>(IsEHPad) << 2) |
820 (static_cast<uint32_t>(CanFallThrough) << 3) |
821 (static_cast<uint32_t>(HasIndirectBranch) << 4);
822 }
823
824 // Decodes and returns a Metadata struct from a uint32_t value.
826 Metadata MD{/*HasReturn=*/static_cast<bool>(V & 1),
827 /*HasTailCall=*/static_cast<bool>(V & (1 << 1)),
828 /*IsEHPad=*/static_cast<bool>(V & (1 << 2)),
829 /*CanFallThrough=*/static_cast<bool>(V & (1 << 3)),
830 /*HasIndirectBranch=*/static_cast<bool>(V & (1 << 4))};
831 if (MD.encode() != V)
832 return createStringError(
833 std::error_code(), "invalid encoding for BBEntry::Metadata: 0x%x",
834 V);
835 return MD;
836 }
837 };
838
839 uint32_t ID; // Unique ID of this basic block.
840 uint32_t Offset; // Offset of basic block relative to function start.
841 uint32_t Size; // Size of the basic block.
842 Metadata MD; // Metdata for this basic block.
843
845 : ID(ID), Offset(Offset), Size(Size), MD(MD){};
846
847 bool operator==(const BBEntry &Other) const {
848 return ID == Other.ID && Offset == Other.Offset && Size == Other.Size &&
849 MD == Other.MD;
850 }
851
852 bool hasReturn() const { return MD.HasReturn; }
853 bool hasTailCall() const { return MD.HasTailCall; }
854 bool isEHPad() const { return MD.IsEHPad; }
855 bool canFallThrough() const { return MD.CanFallThrough; }
856 bool hasIndirectBranch() const { return MD.HasIndirectBranch; }
857 };
858
859 BBAddrMap(uint64_t Addr, std::vector<BBEntry> BBEntries)
860 : Addr(Addr), BBEntries(std::move(BBEntries)) {}
861
862 // Returns the address of the corresponding function.
863 uint64_t getFunctionAddress() const { return Addr; }
864
865 // Returns the basic block entries for this function.
866 const std::vector<BBEntry> &getBBEntries() const { return BBEntries; }
867
868 // Equality operator for unit testing.
869 bool operator==(const BBAddrMap &Other) const {
870 return Addr == Other.Addr && std::equal(BBEntries.begin(), BBEntries.end(),
871 Other.BBEntries.begin());
872 }
873
874 uint64_t Addr; // Function address
875 std::vector<BBEntry> BBEntries; // Basic block entries for this function.
876};
877
878} // end namespace object.
879} // end namespace llvm.
880
881#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 Addr
uint64_t Size
#define LLVM_ELF_IMPORT_TYPES(E, W)
Definition: ELFTypes.h:140
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition: ELFTypes.h:102
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1275
uint64_t Offset
Definition: ELF_riscv.cpp:476
lazy value info
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
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
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:644
Elf_Word getType() const
Get the note's type.
Definition: ELFTypes.h:650
Elf_Note_Impl(const Elf_Nhdr_Impl< ELFT > &Nhdr)
Definition: ELFTypes.h:623
StringRef getName() const
Get the note's name, excluding the terminating null byte.
Definition: ELFTypes.h:626
ArrayRef< uint8_t > getDesc(size_t Align) const
Get the note's descriptor.
Definition: ELFTypes.h:634
bool operator!=(Elf_Note_Iterator_Impl Other) const
Definition: ELFTypes.h:725
std::forward_iterator_tag iterator_category
Definition: ELFTypes.h:655
Elf_Note_Impl< ELFT > operator*() const
Definition: ELFTypes.h:728
Elf_Note_Iterator_Impl & operator++()
Definition: ELFTypes.h:711
bool operator==(Elf_Note_Iterator_Impl Other) const
Definition: ELFTypes.h:718
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:1244
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1853
endianness
Definition: bit.h:70
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
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:825
bool operator==(const Metadata &Other) const
Definition: ELFTypes.h:808
bool operator==(const BBEntry &Other) const
Definition: ELFTypes.h:847
BBEntry(uint32_t ID, uint32_t Offset, uint32_t Size, Metadata MD)
Definition: ELFTypes.h:844
std::vector< BBEntry > BBEntries
Definition: ELFTypes.h:875
BBAddrMap(uint64_t Addr, std::vector< BBEntry > BBEntries)
Definition: ELFTypes.h:859
bool operator==(const BBAddrMap &Other) const
Definition: ELFTypes.h:869
const std::vector< BBEntry > & getBBEntries() const
Definition: ELFTypes.h:866
uint64_t getFunctionAddress() const
Definition: ELFTypes.h:863
std::conditional_t< Is64, uint64_t, uint32_t > uint
Definition: ELFTypes.h:55
static const bool Is64Bits
Definition: ELFTypes.h:53
static const endianness TargetEndianness
Definition: ELFTypes.h:52
Elf_Dyn_Base: This structure matches the form of entries in the dynamic table section (....
Definition: ELFTypes.h:348
Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters.
Definition: ELFTypes.h:372
uintX_t getVal() const
Definition: ELFTypes.h:378
std::conditional_t< ELFT::Is64Bits, int64_t, int32_t > intX_t
Definition: ELFTypes.h:375
std::conditional_t< ELFT::Is64Bits, uint64_t, uint32_t > uintX_t
Definition: ELFTypes.h:376
uintX_t getPtr() const
Definition: ELFTypes.h:379
intX_t getTag() const
Definition: ELFTypes.h:377
unsigned char getFileClass() const
Definition: ELFTypes.h:501
unsigned char getDataEncoding() const
Definition: ELFTypes.h:502
ArrayRef< Elf_Word > values(unsigned DynamicSymCount) const
Definition: ELFTypes.h:567
ArrayRef< Elf_Word > buckets() const
Definition: ELFTypes.h:562
ArrayRef< Elf_Word > chains() const
Definition: ELFTypes.h:542
const Elf_Mips_RegInfo< ELFT > & getRegInfo() const
Definition: ELFTypes.h:774
size_t getSize(size_t Align) const
Get the size of the note, including name, descriptor, and padding.
Definition: ELFTypes.h:604
void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL)
Definition: ELFTypes.h:412
void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL)
Definition: ELFTypes.h:465
unsigned getEntityCount() const
Get the number of entities this section contains if it has any.
Definition: ELFTypes.h:182
void setBindingAndType(unsigned char b, unsigned char t)
Definition: ELFTypes.h:228
bool isProcessorSpecific() const
Definition: ELFTypes.h:251
void setBinding(unsigned char b)
Definition: ELFTypes.h:225
unsigned char getBinding() const
Definition: ELFTypes.h:222
bool isDefined() const
Definition: ELFTypes.h:249
bool isAbsolute() const
Definition: ELFTypes.h:243
bool isOSSpecific() const
Definition: ELFTypes.h:255
unsigned char getType() const
Definition: ELFTypes.h:223
uint64_t getValue() const
Definition: ELFTypes.h:224
void setVisibility(unsigned char v)
Definition: ELFTypes.h:238
bool isExternal() const
Definition: ELFTypes.h:267
void setType(unsigned char t)
Definition: ELFTypes.h:226
bool isUndefined() const
Definition: ELFTypes.h:265
bool isReserved() const
Definition: ELFTypes.h:259
unsigned char getVisibility() const
Access to the STV_xxx flag stored in the first two bits of st_other.
Definition: ELFTypes.h:237
Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef section (....
Definition: ELFTypes.h:316
Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section (.gnu.version_d).
Definition: ELFTypes.h:297
Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed section (....
Definition: ELFTypes.h:337
Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed section (.gnu....
Definition: ELFTypes.h:325
Elf_Versym: This is the structure of entries in the SHT_GNU_versym section (.gnu.version).
Definition: ELFTypes.h:289