1 //===- ELFTypes.h - Endian specific types for ELF ---------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_OBJECT_ELFTYPES_H 11 #define LLVM_OBJECT_ELFTYPES_H 12 13 #include "llvm/Support/AlignOf.h" 14 #include "llvm/Support/DataTypes.h" 15 #include "llvm/Support/ELF.h" 16 #include "llvm/Support/Endian.h" 17 18 namespace llvm { 19 namespace object { 20 21 using support::endianness; 22 23 template <endianness target_endianness, std::size_t max_alignment, 24 bool is64Bits> 25 struct ELFType { 26 static const endianness TargetEndianness = target_endianness; 27 static const std::size_t MaxAlignment = max_alignment; 28 static const bool Is64Bits = is64Bits; 29 }; 30 31 template <typename T, int max_align> struct MaximumAlignment { 32 enum { value = AlignOf<T>::Alignment > max_align ? max_align 33 : AlignOf<T>::Alignment 34 }; 35 }; 36 37 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits. 38 template <endianness target_endianness, std::size_t max_alignment> 39 struct ELFDataTypeTypedefHelperCommon { 40 typedef support::detail::packed_endian_specific_integral< 41 uint16_t, target_endianness, 42 MaximumAlignment<uint16_t, max_alignment>::value> Elf_Half; 43 typedef support::detail::packed_endian_specific_integral< 44 uint32_t, target_endianness, 45 MaximumAlignment<uint32_t, max_alignment>::value> Elf_Word; 46 typedef support::detail::packed_endian_specific_integral< 47 int32_t, target_endianness, 48 MaximumAlignment<int32_t, max_alignment>::value> Elf_Sword; 49 typedef support::detail::packed_endian_specific_integral< 50 uint64_t, target_endianness, 51 MaximumAlignment<uint64_t, max_alignment>::value> Elf_Xword; 52 typedef support::detail::packed_endian_specific_integral< 53 int64_t, target_endianness, 54 MaximumAlignment<int64_t, max_alignment>::value> Elf_Sxword; 55 }; 56 57 template <class ELFT> struct ELFDataTypeTypedefHelper; 58 59 /// ELF 32bit types. 60 template <endianness TargetEndianness, std::size_t MaxAlign> 61 struct ELFDataTypeTypedefHelper<ELFType<TargetEndianness, MaxAlign, false> > 62 : ELFDataTypeTypedefHelperCommon<TargetEndianness, MaxAlign> { 63 typedef uint32_t value_type; 64 typedef support::detail::packed_endian_specific_integral< 65 value_type, TargetEndianness, 66 MaximumAlignment<value_type, MaxAlign>::value> Elf_Addr; 67 typedef support::detail::packed_endian_specific_integral< 68 value_type, TargetEndianness, 69 MaximumAlignment<value_type, MaxAlign>::value> Elf_Off; 70 }; 71 72 /// ELF 64bit types. 73 template <endianness TargetEndianness, std::size_t MaxAlign> 74 struct ELFDataTypeTypedefHelper<ELFType<TargetEndianness, MaxAlign, true> > 75 : ELFDataTypeTypedefHelperCommon<TargetEndianness, MaxAlign> { 76 typedef uint64_t value_type; 77 typedef support::detail::packed_endian_specific_integral< 78 value_type, TargetEndianness, 79 MaximumAlignment<value_type, MaxAlign>::value> Elf_Addr; 80 typedef support::detail::packed_endian_specific_integral< 81 value_type, TargetEndianness, 82 MaximumAlignment<value_type, MaxAlign>::value> Elf_Off; 83 }; 84 85 // I really don't like doing this, but the alternative is copypasta. 86 #define LLVM_ELF_IMPORT_TYPES(E, M, W) \ 87 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Addr \ 88 Elf_Addr; \ 89 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Off \ 90 Elf_Off; \ 91 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Half \ 92 Elf_Half; \ 93 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Word \ 94 Elf_Word; \ 95 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Sword \ 96 Elf_Sword; \ 97 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Xword \ 98 Elf_Xword; \ 99 typedef typename ELFDataTypeTypedefHelper<ELFType<E, M, W> >::Elf_Sxword \ 100 Elf_Sxword; 101 102 #define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) \ 103 LLVM_ELF_IMPORT_TYPES(ELFT::TargetEndianness, ELFT::MaxAlignment, \ 104 ELFT::Is64Bits) 105 106 // Section header. 107 template <class ELFT> struct Elf_Shdr_Base; 108 109 template <endianness TargetEndianness, std::size_t MaxAlign> 110 struct Elf_Shdr_Base<ELFType<TargetEndianness, MaxAlign, false> > { 111 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 112 Elf_Word sh_name; // Section name (index into string table) 113 Elf_Word sh_type; // Section type (SHT_*) 114 Elf_Word sh_flags; // Section flags (SHF_*) 115 Elf_Addr sh_addr; // Address where section is to be loaded 116 Elf_Off sh_offset; // File offset of section data, in bytes 117 Elf_Word sh_size; // Size of section, in bytes 118 Elf_Word sh_link; // Section type-specific header table index link 119 Elf_Word sh_info; // Section type-specific extra information 120 Elf_Word sh_addralign; // Section address alignment 121 Elf_Word sh_entsize; // Size of records contained within the section 122 }; 123 124 template <endianness TargetEndianness, std::size_t MaxAlign> 125 struct Elf_Shdr_Base<ELFType<TargetEndianness, MaxAlign, true> > { 126 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 127 Elf_Word sh_name; // Section name (index into string table) 128 Elf_Word sh_type; // Section type (SHT_*) 129 Elf_Xword sh_flags; // Section flags (SHF_*) 130 Elf_Addr sh_addr; // Address where section is to be loaded 131 Elf_Off sh_offset; // File offset of section data, in bytes 132 Elf_Xword sh_size; // Size of section, in bytes 133 Elf_Word sh_link; // Section type-specific header table index link 134 Elf_Word sh_info; // Section type-specific extra information 135 Elf_Xword sh_addralign; // Section address alignment 136 Elf_Xword sh_entsize; // Size of records contained within the section 137 }; 138 139 template <class ELFT> 140 struct Elf_Shdr_Impl : Elf_Shdr_Base<ELFT> { 141 using Elf_Shdr_Base<ELFT>::sh_entsize; 142 using Elf_Shdr_Base<ELFT>::sh_size; 143 144 /// @brief Get the number of entities this section contains if it has any. 145 unsigned getEntityCount() const { 146 if (sh_entsize == 0) 147 return 0; 148 return sh_size / sh_entsize; 149 } 150 }; 151 152 template <class ELFT> struct Elf_Sym_Base; 153 154 template <endianness TargetEndianness, std::size_t MaxAlign> 155 struct Elf_Sym_Base<ELFType<TargetEndianness, MaxAlign, false> > { 156 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 157 Elf_Word st_name; // Symbol name (index into string table) 158 Elf_Addr st_value; // Value or address associated with the symbol 159 Elf_Word st_size; // Size of the symbol 160 unsigned char st_info; // Symbol's type and binding attributes 161 unsigned char st_other; // Must be zero; reserved 162 Elf_Half st_shndx; // Which section (header table index) it's defined in 163 }; 164 165 template <endianness TargetEndianness, std::size_t MaxAlign> 166 struct Elf_Sym_Base<ELFType<TargetEndianness, MaxAlign, true> > { 167 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 168 Elf_Word st_name; // Symbol name (index into string table) 169 unsigned char st_info; // Symbol's type and binding attributes 170 unsigned char st_other; // Must be zero; reserved 171 Elf_Half st_shndx; // Which section (header table index) it's defined in 172 Elf_Addr st_value; // Value or address associated with the symbol 173 Elf_Xword st_size; // Size of the symbol 174 }; 175 176 template <class ELFT> 177 struct Elf_Sym_Impl : Elf_Sym_Base<ELFT> { 178 using Elf_Sym_Base<ELFT>::st_info; 179 using Elf_Sym_Base<ELFT>::st_other; 180 181 // These accessors and mutators correspond to the ELF32_ST_BIND, 182 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification: 183 unsigned char getBinding() const { return st_info >> 4; } 184 unsigned char getType() const { return st_info & 0x0f; } 185 void setBinding(unsigned char b) { setBindingAndType(b, getType()); } 186 void setType(unsigned char t) { setBindingAndType(getBinding(), t); } 187 void setBindingAndType(unsigned char b, unsigned char t) { 188 st_info = (b << 4) + (t & 0x0f); 189 } 190 191 /// Access to the STV_xxx flag stored in the first two bits of st_other. 192 unsigned char getVisibility() const { return st_other & 0x3; } 193 }; 194 195 /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section 196 /// (.gnu.version). This structure is identical for ELF32 and ELF64. 197 template <class ELFT> 198 struct Elf_Versym_Impl { 199 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 200 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN) 201 }; 202 203 template <class ELFT> struct Elf_Verdaux_Impl; 204 205 /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section 206 /// (.gnu.version_d). This structure is identical for ELF32 and ELF64. 207 template <class ELFT> 208 struct Elf_Verdef_Impl { 209 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 210 typedef Elf_Verdaux_Impl<ELFT> Elf_Verdaux; 211 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT) 212 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*) 213 Elf_Half vd_ndx; // Version index, used in .gnu.version entries 214 Elf_Half vd_cnt; // Number of Verdaux entries 215 Elf_Word vd_hash; // Hash of name 216 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes) 217 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes) 218 219 /// Get the first Verdaux entry for this Verdef. 220 const Elf_Verdaux *getAux() const { 221 return reinterpret_cast<const Elf_Verdaux *>((const char *)this + vd_aux); 222 } 223 }; 224 225 /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef 226 /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64. 227 template <class ELFT> 228 struct Elf_Verdaux_Impl { 229 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 230 Elf_Word vda_name; // Version name (offset in string table) 231 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes) 232 }; 233 234 /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed 235 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64. 236 template <class ELFT> 237 struct Elf_Verneed_Impl { 238 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 239 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT) 240 Elf_Half vn_cnt; // Number of associated Vernaux entries 241 Elf_Word vn_file; // Library name (string table offset) 242 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes) 243 Elf_Word vn_next; // Offset to next Verneed entry (in bytes) 244 }; 245 246 /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed 247 /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64. 248 template <class ELFT> 249 struct Elf_Vernaux_Impl { 250 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 251 Elf_Word vna_hash; // Hash of dependency name 252 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*) 253 Elf_Half vna_other; // Version index, used in .gnu.version entries 254 Elf_Word vna_name; // Dependency name 255 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes) 256 }; 257 258 /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic 259 /// table section (.dynamic) look like. 260 template <class ELFT> struct Elf_Dyn_Base; 261 262 template <endianness TargetEndianness, std::size_t MaxAlign> 263 struct Elf_Dyn_Base<ELFType<TargetEndianness, MaxAlign, false> > { 264 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 265 Elf_Sword d_tag; 266 union { 267 Elf_Word d_val; 268 Elf_Addr d_ptr; 269 } d_un; 270 }; 271 272 template <endianness TargetEndianness, std::size_t MaxAlign> 273 struct Elf_Dyn_Base<ELFType<TargetEndianness, MaxAlign, true> > { 274 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 275 Elf_Sxword d_tag; 276 union { 277 Elf_Xword d_val; 278 Elf_Addr d_ptr; 279 } d_un; 280 }; 281 282 /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters. 283 template <class ELFT> 284 struct Elf_Dyn_Impl : Elf_Dyn_Base<ELFT> { 285 using Elf_Dyn_Base<ELFT>::d_tag; 286 using Elf_Dyn_Base<ELFT>::d_un; 287 int64_t getTag() const { return d_tag; } 288 uint64_t getVal() const { return d_un.d_val; } 289 uint64_t getPtr() const { return d_un.ptr; } 290 }; 291 292 // Elf_Rel: Elf Relocation 293 template <class ELFT, bool isRela> struct Elf_Rel_Base; 294 295 template <endianness TargetEndianness, std::size_t MaxAlign> 296 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, false>, false> { 297 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 298 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 299 Elf_Word r_info; // Symbol table index and type of relocation to apply 300 301 uint32_t getRInfo(bool isMips64EL) const { 302 assert(!isMips64EL); 303 return r_info; 304 } 305 void setRInfo(uint32_t R) { r_info = R; } 306 }; 307 308 template <endianness TargetEndianness, std::size_t MaxAlign> 309 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, true>, false> { 310 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 311 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 312 Elf_Xword r_info; // Symbol table index and type of relocation to apply 313 314 uint64_t getRInfo(bool isMips64EL) const { 315 uint64_t t = r_info; 316 if (!isMips64EL) 317 return t; 318 // Mips64 little endian has a "special" encoding of r_info. Instead of one 319 // 64 bit little endian number, it is a little endian 32 bit number followed 320 // by a 32 bit big endian number. 321 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) | 322 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff); 323 } 324 void setRInfo(uint64_t R) { 325 // FIXME: Add mips64el support. 326 r_info = R; 327 } 328 }; 329 330 template <endianness TargetEndianness, std::size_t MaxAlign> 331 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, false>, true> { 332 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 333 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 334 Elf_Word r_info; // Symbol table index and type of relocation to apply 335 Elf_Sword r_addend; // Compute value for relocatable field by adding this 336 337 uint32_t getRInfo(bool isMips64EL) const { 338 assert(!isMips64EL); 339 return r_info; 340 } 341 void setRInfo(uint32_t R) { r_info = R; } 342 }; 343 344 template <endianness TargetEndianness, std::size_t MaxAlign> 345 struct Elf_Rel_Base<ELFType<TargetEndianness, MaxAlign, true>, true> { 346 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 347 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 348 Elf_Xword r_info; // Symbol table index and type of relocation to apply 349 Elf_Sxword r_addend; // Compute value for relocatable field by adding this. 350 351 uint64_t getRInfo(bool isMips64EL) const { 352 // Mips64 little endian has a "special" encoding of r_info. Instead of one 353 // 64 bit little endian number, it is a little endian 32 bit number followed 354 // by a 32 bit big endian number. 355 uint64_t t = r_info; 356 if (!isMips64EL) 357 return t; 358 return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) | 359 ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff); 360 } 361 void setRInfo(uint64_t R) { 362 // FIXME: Add mips64el support. 363 r_info = R; 364 } 365 }; 366 367 template <class ELFT, bool isRela> struct Elf_Rel_Impl; 368 369 template <endianness TargetEndianness, std::size_t MaxAlign, bool isRela> 370 struct Elf_Rel_Impl<ELFType<TargetEndianness, MaxAlign, true>, 371 isRela> : Elf_Rel_Base< 372 ELFType<TargetEndianness, MaxAlign, true>, isRela> { 373 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 374 375 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE, 376 // and ELF64_R_INFO macros defined in the ELF specification: 377 uint32_t getSymbol(bool isMips64EL) const { 378 return (uint32_t)(this->getRInfo(isMips64EL) >> 32); 379 } 380 uint32_t getType(bool isMips64EL) const { 381 return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL); 382 } 383 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); } 384 void setType(uint32_t t) { setSymbolAndType(getSymbol(), t); } 385 void setSymbolAndType(uint32_t s, uint32_t t) { 386 this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL)); 387 } 388 }; 389 390 template <endianness TargetEndianness, std::size_t MaxAlign, bool isRela> 391 struct Elf_Rel_Impl<ELFType<TargetEndianness, MaxAlign, false>, 392 isRela> : Elf_Rel_Base< 393 ELFType<TargetEndianness, MaxAlign, false>, isRela> { 394 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 395 396 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, 397 // and ELF32_R_INFO macros defined in the ELF specification: 398 uint32_t getSymbol(bool isMips64EL) const { 399 return this->getRInfo(isMips64EL) >> 8; 400 } 401 unsigned char getType(bool isMips64EL) const { 402 return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff); 403 } 404 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); } 405 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); } 406 void setSymbolAndType(uint32_t s, unsigned char t) { 407 this->setRInfo((s << 8) + t); 408 } 409 }; 410 411 template <class ELFT> 412 struct Elf_Ehdr_Impl { 413 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 414 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes 415 Elf_Half e_type; // Type of file (see ET_*) 416 Elf_Half e_machine; // Required architecture for this file (see EM_*) 417 Elf_Word e_version; // Must be equal to 1 418 Elf_Addr e_entry; // Address to jump to in order to start program 419 Elf_Off e_phoff; // Program header table's file offset, in bytes 420 Elf_Off e_shoff; // Section header table's file offset, in bytes 421 Elf_Word e_flags; // Processor-specific flags 422 Elf_Half e_ehsize; // Size of ELF header, in bytes 423 Elf_Half e_phentsize; // Size of an entry in the program header table 424 Elf_Half e_phnum; // Number of entries in the program header table 425 Elf_Half e_shentsize; // Size of an entry in the section header table 426 Elf_Half e_shnum; // Number of entries in the section header table 427 Elf_Half e_shstrndx; // Section header table index of section name 428 // string table 429 bool checkMagic() const { 430 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0; 431 } 432 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; } 433 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; } 434 }; 435 436 template <class ELFT> struct Elf_Phdr_Impl; 437 438 template <endianness TargetEndianness, std::size_t MaxAlign> 439 struct Elf_Phdr_Impl<ELFType<TargetEndianness, MaxAlign, false> > { 440 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, false) 441 Elf_Word p_type; // Type of segment 442 Elf_Off p_offset; // FileOffset where segment is located, in bytes 443 Elf_Addr p_vaddr; // Virtual Address of beginning of segment 444 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific) 445 Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero) 446 Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero) 447 Elf_Word p_flags; // Segment flags 448 Elf_Word p_align; // Segment alignment constraint 449 }; 450 451 template <endianness TargetEndianness, std::size_t MaxAlign> 452 struct Elf_Phdr_Impl<ELFType<TargetEndianness, MaxAlign, true> > { 453 LLVM_ELF_IMPORT_TYPES(TargetEndianness, MaxAlign, true) 454 Elf_Word p_type; // Type of segment 455 Elf_Word p_flags; // Segment flags 456 Elf_Off p_offset; // FileOffset where segment is located, in bytes 457 Elf_Addr p_vaddr; // Virtual Address of beginning of segment 458 Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific) 459 Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero) 460 Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero) 461 Elf_Xword p_align; // Segment alignment constraint 462 }; 463 464 } // end namespace object. 465 } // end namespace llvm. 466 467 #endif 468