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