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