ELF.h revision 4b6fbf25600a94dc41ba96b174045e50b85862cf
1//===- ELF.h - ELF object file implementation -------------------*- 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// This file declares the ELFObjectFile template class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_OBJECT_ELF_H 15#define LLVM_OBJECT_ELF_H 16 17#include "llvm/ADT/SmallVector.h" 18#include "llvm/ADT/StringSwitch.h" 19#include "llvm/ADT/Triple.h" 20#include "llvm/ADT/DenseMap.h" 21#include "llvm/ADT/PointerIntPair.h" 22#include "llvm/Object/ObjectFile.h" 23#include "llvm/Support/Casting.h" 24#include "llvm/Support/ELF.h" 25#include "llvm/Support/Endian.h" 26#include "llvm/Support/ErrorHandling.h" 27#include "llvm/Support/MemoryBuffer.h" 28#include "llvm/Support/raw_ostream.h" 29#include <algorithm> 30#include <limits> 31#include <utility> 32 33namespace llvm { 34namespace object { 35 36// Subclasses of ELFObjectFile may need this for template instantiation 37inline std::pair<unsigned char, unsigned char> 38getElfArchType(MemoryBuffer *Object) { 39 if (Object->getBufferSize() < ELF::EI_NIDENT) 40 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE); 41 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS] 42 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]); 43} 44 45// Templates to choose Elf_Addr and Elf_Off depending on is64Bits. 46template<support::endianness target_endianness> 47struct ELFDataTypeTypedefHelperCommon { 48 typedef support::detail::packed_endian_specific_integral 49 <uint16_t, target_endianness, support::aligned> Elf_Half; 50 typedef support::detail::packed_endian_specific_integral 51 <uint32_t, target_endianness, support::aligned> Elf_Word; 52 typedef support::detail::packed_endian_specific_integral 53 <int32_t, target_endianness, support::aligned> Elf_Sword; 54 typedef support::detail::packed_endian_specific_integral 55 <uint64_t, target_endianness, support::aligned> Elf_Xword; 56 typedef support::detail::packed_endian_specific_integral 57 <int64_t, target_endianness, support::aligned> Elf_Sxword; 58}; 59 60template<support::endianness target_endianness, bool is64Bits> 61struct ELFDataTypeTypedefHelper; 62 63/// ELF 32bit types. 64template<support::endianness target_endianness> 65struct ELFDataTypeTypedefHelper<target_endianness, false> 66 : ELFDataTypeTypedefHelperCommon<target_endianness> { 67 typedef uint32_t value_type; 68 typedef support::detail::packed_endian_specific_integral 69 <value_type, target_endianness, support::aligned> Elf_Addr; 70 typedef support::detail::packed_endian_specific_integral 71 <value_type, target_endianness, support::aligned> Elf_Off; 72}; 73 74/// ELF 64bit types. 75template<support::endianness target_endianness> 76struct ELFDataTypeTypedefHelper<target_endianness, true> 77 : ELFDataTypeTypedefHelperCommon<target_endianness>{ 78 typedef uint64_t value_type; 79 typedef support::detail::packed_endian_specific_integral 80 <value_type, target_endianness, support::aligned> Elf_Addr; 81 typedef support::detail::packed_endian_specific_integral 82 <value_type, target_endianness, support::aligned> Elf_Off; 83}; 84 85// I really don't like doing this, but the alternative is copypasta. 86#define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \ 87typedef typename \ 88 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Addr Elf_Addr; \ 89typedef typename \ 90 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Off Elf_Off; \ 91typedef typename \ 92 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Half Elf_Half; \ 93typedef typename \ 94 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Word Elf_Word; \ 95typedef typename \ 96 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sword Elf_Sword; \ 97typedef typename \ 98 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Xword Elf_Xword; \ 99typedef typename \ 100 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sxword Elf_Sxword; 101 102 // Section header. 103template<support::endianness target_endianness, bool is64Bits> 104struct Elf_Shdr_Base; 105 106template<support::endianness target_endianness> 107struct Elf_Shdr_Base<target_endianness, false> { 108 LLVM_ELF_IMPORT_TYPES(target_endianness, false) 109 Elf_Word sh_name; // Section name (index into string table) 110 Elf_Word sh_type; // Section type (SHT_*) 111 Elf_Word sh_flags; // Section flags (SHF_*) 112 Elf_Addr sh_addr; // Address where section is to be loaded 113 Elf_Off sh_offset; // File offset of section data, in bytes 114 Elf_Word sh_size; // Size of section, in bytes 115 Elf_Word sh_link; // Section type-specific header table index link 116 Elf_Word sh_info; // Section type-specific extra information 117 Elf_Word sh_addralign;// Section address alignment 118 Elf_Word sh_entsize; // Size of records contained within the section 119}; 120 121template<support::endianness target_endianness> 122struct Elf_Shdr_Base<target_endianness, true> { 123 LLVM_ELF_IMPORT_TYPES(target_endianness, true) 124 Elf_Word sh_name; // Section name (index into string table) 125 Elf_Word sh_type; // Section type (SHT_*) 126 Elf_Xword sh_flags; // Section flags (SHF_*) 127 Elf_Addr sh_addr; // Address where section is to be loaded 128 Elf_Off sh_offset; // File offset of section data, in bytes 129 Elf_Xword sh_size; // Size of section, in bytes 130 Elf_Word sh_link; // Section type-specific header table index link 131 Elf_Word sh_info; // Section type-specific extra information 132 Elf_Xword sh_addralign;// Section address alignment 133 Elf_Xword sh_entsize; // Size of records contained within the section 134}; 135 136template<support::endianness target_endianness, bool is64Bits> 137struct Elf_Shdr_Impl : Elf_Shdr_Base<target_endianness, is64Bits> { 138 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_entsize; 139 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_size; 140 141 /// @brief Get the number of entities this section contains if it has any. 142 unsigned getEntityCount() const { 143 if (sh_entsize == 0) 144 return 0; 145 return sh_size / sh_entsize; 146 } 147}; 148 149template<support::endianness target_endianness, bool is64Bits> 150struct Elf_Sym_Base; 151 152template<support::endianness target_endianness> 153struct Elf_Sym_Base<target_endianness, false> { 154 LLVM_ELF_IMPORT_TYPES(target_endianness, false) 155 Elf_Word st_name; // Symbol name (index into string table) 156 Elf_Addr st_value; // Value or address associated with the symbol 157 Elf_Word st_size; // Size of the symbol 158 unsigned char st_info; // Symbol's type and binding attributes 159 unsigned char st_other; // Must be zero; reserved 160 Elf_Half st_shndx; // Which section (header table index) it's defined in 161}; 162 163template<support::endianness target_endianness> 164struct Elf_Sym_Base<target_endianness, true> { 165 LLVM_ELF_IMPORT_TYPES(target_endianness, true) 166 Elf_Word st_name; // Symbol name (index into string table) 167 unsigned char st_info; // Symbol's type and binding attributes 168 unsigned char st_other; // Must be zero; reserved 169 Elf_Half st_shndx; // Which section (header table index) it's defined in 170 Elf_Addr st_value; // Value or address associated with the symbol 171 Elf_Xword st_size; // Size of the symbol 172}; 173 174template<support::endianness target_endianness, bool is64Bits> 175struct Elf_Sym_Impl : Elf_Sym_Base<target_endianness, is64Bits> { 176 using Elf_Sym_Base<target_endianness, is64Bits>::st_info; 177 178 // These accessors and mutators correspond to the ELF32_ST_BIND, 179 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification: 180 unsigned char getBinding() const { return st_info >> 4; } 181 unsigned char getType() const { return st_info & 0x0f; } 182 void setBinding(unsigned char b) { setBindingAndType(b, getType()); } 183 void setType(unsigned char t) { setBindingAndType(getBinding(), t); } 184 void setBindingAndType(unsigned char b, unsigned char t) { 185 st_info = (b << 4) + (t & 0x0f); 186 } 187}; 188 189/// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section 190/// (.gnu.version). This structure is identical for ELF32 and ELF64. 191template<support::endianness target_endianness, bool is64Bits> 192struct Elf_Versym_Impl { 193 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) 194 Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN) 195}; 196 197template<support::endianness target_endianness, bool is64Bits> 198struct Elf_Verdaux_Impl; 199 200/// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section 201/// (.gnu.version_d). This structure is identical for ELF32 and ELF64. 202template<support::endianness target_endianness, bool is64Bits> 203struct Elf_Verdef_Impl { 204 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) 205 typedef Elf_Verdaux_Impl<target_endianness, is64Bits> Elf_Verdaux; 206 Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT) 207 Elf_Half vd_flags; // Bitwise flags (VER_DEF_*) 208 Elf_Half vd_ndx; // Version index, used in .gnu.version entries 209 Elf_Half vd_cnt; // Number of Verdaux entries 210 Elf_Word vd_hash; // Hash of name 211 Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes) 212 Elf_Word vd_next; // Offset to the next Verdef entry (in bytes) 213 214 /// Get the first Verdaux entry for this Verdef. 215 const Elf_Verdaux *getAux() const { 216 return reinterpret_cast<const Elf_Verdaux*>((const char*)this + vd_aux); 217 } 218}; 219 220/// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef 221/// section (.gnu.version_d). This structure is identical for ELF32 and ELF64. 222template<support::endianness target_endianness, bool is64Bits> 223struct Elf_Verdaux_Impl { 224 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) 225 Elf_Word vda_name; // Version name (offset in string table) 226 Elf_Word vda_next; // Offset to next Verdaux entry (in bytes) 227}; 228 229/// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed 230/// section (.gnu.version_r). This structure is identical for ELF32 and ELF64. 231template<support::endianness target_endianness, bool is64Bits> 232struct Elf_Verneed_Impl { 233 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) 234 Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT) 235 Elf_Half vn_cnt; // Number of associated Vernaux entries 236 Elf_Word vn_file; // Library name (string table offset) 237 Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes) 238 Elf_Word vn_next; // Offset to next Verneed entry (in bytes) 239}; 240 241/// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed 242/// section (.gnu.version_r). This structure is identical for ELF32 and ELF64. 243template<support::endianness target_endianness, bool is64Bits> 244struct Elf_Vernaux_Impl { 245 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) 246 Elf_Word vna_hash; // Hash of dependency name 247 Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*) 248 Elf_Half vna_other; // Version index, used in .gnu.version entries 249 Elf_Word vna_name; // Dependency name 250 Elf_Word vna_next; // Offset to next Vernaux entry (in bytes) 251}; 252 253/// Elf_Dyn_Base: This structure matches the form of entries in the dynamic 254/// table section (.dynamic) look like. 255template<support::endianness target_endianness, bool is64Bits> 256struct Elf_Dyn_Base; 257 258template<support::endianness target_endianness> 259struct Elf_Dyn_Base<target_endianness, false> { 260 LLVM_ELF_IMPORT_TYPES(target_endianness, false) 261 Elf_Sword d_tag; 262 union { 263 Elf_Word d_val; 264 Elf_Addr d_ptr; 265 } d_un; 266}; 267 268template<support::endianness target_endianness> 269struct Elf_Dyn_Base<target_endianness, true> { 270 LLVM_ELF_IMPORT_TYPES(target_endianness, true) 271 Elf_Sxword d_tag; 272 union { 273 Elf_Xword d_val; 274 Elf_Addr d_ptr; 275 } d_un; 276}; 277 278/// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters. 279template<support::endianness target_endianness, bool is64Bits> 280struct Elf_Dyn_Impl : Elf_Dyn_Base<target_endianness, is64Bits> { 281 using Elf_Dyn_Base<target_endianness, is64Bits>::d_tag; 282 using Elf_Dyn_Base<target_endianness, is64Bits>::d_un; 283 int64_t getTag() const { return d_tag; } 284 uint64_t getVal() const { return d_un.d_val; } 285 uint64_t getPtr() const { return d_un.ptr; } 286}; 287 288template<support::endianness target_endianness, bool is64Bits> 289class ELFObjectFile; 290 291// DynRefImpl: Reference to an entry in the dynamic table 292// This is an ELF-specific interface. 293template<support::endianness target_endianness, bool is64Bits> 294class DynRefImpl { 295 typedef Elf_Dyn_Impl<target_endianness, is64Bits> Elf_Dyn; 296 typedef ELFObjectFile<target_endianness, is64Bits> OwningType; 297 298 DataRefImpl DynPimpl; 299 const OwningType *OwningObject; 300 301public: 302 DynRefImpl() : OwningObject(NULL) { } 303 304 DynRefImpl(DataRefImpl DynP, const OwningType *Owner); 305 306 bool operator==(const DynRefImpl &Other) const; 307 bool operator <(const DynRefImpl &Other) const; 308 309 error_code getNext(DynRefImpl &Result) const; 310 int64_t getTag() const; 311 uint64_t getVal() const; 312 uint64_t getPtr() const; 313 314 DataRefImpl getRawDataRefImpl() const; 315}; 316 317// Elf_Rel: Elf Relocation 318template<support::endianness target_endianness, bool is64Bits, bool isRela> 319struct Elf_Rel_Base; 320 321template<support::endianness target_endianness> 322struct Elf_Rel_Base<target_endianness, false, false> { 323 LLVM_ELF_IMPORT_TYPES(target_endianness, false) 324 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 325 Elf_Word r_info; // Symbol table index and type of relocation to apply 326}; 327 328template<support::endianness target_endianness> 329struct Elf_Rel_Base<target_endianness, true, false> { 330 LLVM_ELF_IMPORT_TYPES(target_endianness, true) 331 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 332 Elf_Xword r_info; // Symbol table index and type of relocation to apply 333}; 334 335template<support::endianness target_endianness> 336struct Elf_Rel_Base<target_endianness, false, true> { 337 LLVM_ELF_IMPORT_TYPES(target_endianness, false) 338 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 339 Elf_Word r_info; // Symbol table index and type of relocation to apply 340 Elf_Sword r_addend; // Compute value for relocatable field by adding this 341}; 342 343template<support::endianness target_endianness> 344struct Elf_Rel_Base<target_endianness, true, true> { 345 LLVM_ELF_IMPORT_TYPES(target_endianness, true) 346 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 347 Elf_Xword r_info; // Symbol table index and type of relocation to apply 348 Elf_Sxword r_addend; // Compute value for relocatable field by adding this. 349}; 350 351template<support::endianness target_endianness, bool is64Bits, bool isRela> 352struct Elf_Rel_Impl; 353 354template<support::endianness target_endianness, bool isRela> 355struct Elf_Rel_Impl<target_endianness, true, isRela> 356 : Elf_Rel_Base<target_endianness, true, isRela> { 357 using Elf_Rel_Base<target_endianness, true, isRela>::r_info; 358 LLVM_ELF_IMPORT_TYPES(target_endianness, true) 359 360 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE, 361 // and ELF64_R_INFO macros defined in the ELF specification: 362 uint64_t getSymbol() const { return (r_info >> 32); } 363 unsigned char getType() const { 364 return (unsigned char) (r_info & 0xffffffffL); 365 } 366 void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); } 367 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); } 368 void setSymbolAndType(uint64_t s, unsigned char t) { 369 r_info = (s << 32) + (t&0xffffffffL); 370 } 371}; 372 373template<support::endianness target_endianness, bool isRela> 374struct Elf_Rel_Impl<target_endianness, false, isRela> 375 : Elf_Rel_Base<target_endianness, false, isRela> { 376 using Elf_Rel_Base<target_endianness, false, isRela>::r_info; 377 LLVM_ELF_IMPORT_TYPES(target_endianness, false) 378 379 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, 380 // and ELF32_R_INFO macros defined in the ELF specification: 381 uint32_t getSymbol() const { return (r_info >> 8); } 382 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); } 383 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); } 384 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); } 385 void setSymbolAndType(uint32_t s, unsigned char t) { 386 r_info = (s << 8) + t; 387 } 388}; 389 390 391template<support::endianness target_endianness, bool is64Bits> 392class ELFObjectFile : public ObjectFile { 393 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) 394 395 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr; 396 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym; 397 typedef Elf_Dyn_Impl<target_endianness, is64Bits> Elf_Dyn; 398 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel; 399 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela; 400 typedef Elf_Verdef_Impl<target_endianness, is64Bits> Elf_Verdef; 401 typedef Elf_Verdaux_Impl<target_endianness, is64Bits> Elf_Verdaux; 402 typedef Elf_Verneed_Impl<target_endianness, is64Bits> Elf_Verneed; 403 typedef Elf_Vernaux_Impl<target_endianness, is64Bits> Elf_Vernaux; 404 typedef Elf_Versym_Impl<target_endianness, is64Bits> Elf_Versym; 405 typedef DynRefImpl<target_endianness, is64Bits> DynRef; 406 typedef content_iterator<DynRef> dyn_iterator; 407 408protected: 409 struct Elf_Ehdr { 410 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes 411 Elf_Half e_type; // Type of file (see ET_*) 412 Elf_Half e_machine; // Required architecture for this file (see EM_*) 413 Elf_Word e_version; // Must be equal to 1 414 Elf_Addr e_entry; // Address to jump to in order to start program 415 Elf_Off e_phoff; // Program header table's file offset, in bytes 416 Elf_Off e_shoff; // Section header table's file offset, in bytes 417 Elf_Word e_flags; // Processor-specific flags 418 Elf_Half e_ehsize; // Size of ELF header, in bytes 419 Elf_Half e_phentsize;// Size of an entry in the program header table 420 Elf_Half e_phnum; // Number of entries in the program header table 421 Elf_Half e_shentsize;// Size of an entry in the section header table 422 Elf_Half e_shnum; // Number of entries in the section header table 423 Elf_Half e_shstrndx; // Section header table index of section name 424 // string table 425 bool checkMagic() const { 426 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0; 427 } 428 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; } 429 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; } 430 }; 431 // This flag is used for classof, to distinguish ELFObjectFile from 432 // its subclass. If more subclasses will be created, this flag will 433 // have to become an enum. 434 bool isDyldELFObject; 435 436private: 437 typedef SmallVector<const Elf_Shdr*, 1> Sections_t; 438 typedef DenseMap<unsigned, unsigned> IndexMap_t; 439 typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t; 440 441 const Elf_Ehdr *Header; 442 const Elf_Shdr *SectionHeaderTable; 443 const Elf_Shdr *dot_shstrtab_sec; // Section header string table. 444 const Elf_Shdr *dot_strtab_sec; // Symbol header string table. 445 const Elf_Shdr *dot_dynstr_sec; // Dynamic symbol string table. 446 447 // SymbolTableSections[0] always points to the dynamic string table section 448 // header, or NULL if there is no dynamic string table. 449 Sections_t SymbolTableSections; 450 IndexMap_t SymbolTableSectionsIndexMap; 451 DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable; 452 453 const Elf_Shdr *dot_dynamic_sec; // .dynamic 454 const Elf_Shdr *dot_gnu_version_sec; // .gnu.version 455 const Elf_Shdr *dot_gnu_version_r_sec; // .gnu.version_r 456 const Elf_Shdr *dot_gnu_version_d_sec; // .gnu.version_d 457 458 // Pointer to SONAME entry in dynamic string table 459 // This is set the first time getLoadName is called. 460 mutable const char *dt_soname; 461 462 // Records for each version index the corresponding Verdef or Vernaux entry. 463 // This is filled the first time LoadVersionMap() is called. 464 class VersionMapEntry : public PointerIntPair<const void*, 1> { 465 public: 466 // If the integer is 0, this is an Elf_Verdef*. 467 // If the integer is 1, this is an Elf_Vernaux*. 468 VersionMapEntry() : PointerIntPair<const void*, 1>(NULL, 0) { } 469 VersionMapEntry(const Elf_Verdef *verdef) 470 : PointerIntPair<const void*, 1>(verdef, 0) { } 471 VersionMapEntry(const Elf_Vernaux *vernaux) 472 : PointerIntPair<const void*, 1>(vernaux, 1) { } 473 bool isNull() const { return getPointer() == NULL; } 474 bool isVerdef() const { return !isNull() && getInt() == 0; } 475 bool isVernaux() const { return !isNull() && getInt() == 1; } 476 const Elf_Verdef *getVerdef() const { 477 return isVerdef() ? (const Elf_Verdef*)getPointer() : NULL; 478 } 479 const Elf_Vernaux *getVernaux() const { 480 return isVernaux() ? (const Elf_Vernaux*)getPointer() : NULL; 481 } 482 }; 483 mutable SmallVector<VersionMapEntry, 16> VersionMap; 484 void LoadVersionDefs(const Elf_Shdr *sec) const; 485 void LoadVersionNeeds(const Elf_Shdr *ec) const; 486 void LoadVersionMap() const; 487 488 /// @brief Map sections to an array of relocation sections that reference 489 /// them sorted by section index. 490 RelocMap_t SectionRelocMap; 491 492 /// @brief Get the relocation section that contains \a Rel. 493 const Elf_Shdr *getRelSection(DataRefImpl Rel) const { 494 return getSection(Rel.w.b); 495 } 496 497 bool isRelocationHasAddend(DataRefImpl Rel) const; 498 template<typename T> 499 const T *getEntry(uint16_t Section, uint32_t Entry) const; 500 template<typename T> 501 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const; 502 const Elf_Shdr *getSection(DataRefImpl index) const; 503 const Elf_Shdr *getSection(uint32_t index) const; 504 const Elf_Rel *getRel(DataRefImpl Rel) const; 505 const Elf_Rela *getRela(DataRefImpl Rela) const; 506 const char *getString(uint32_t section, uint32_t offset) const; 507 const char *getString(const Elf_Shdr *section, uint32_t offset) const; 508 error_code getSymbolVersion(const Elf_Shdr *section, 509 const Elf_Sym *Symb, 510 StringRef &Version, 511 bool &IsDefault) const; 512 void VerifyStrTab(const Elf_Shdr *sh) const; 513 514protected: 515 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private? 516 void validateSymbol(DataRefImpl Symb) const; 517 518public: 519 error_code getSymbolName(const Elf_Shdr *section, 520 const Elf_Sym *Symb, 521 StringRef &Res) const; 522 error_code getSectionName(const Elf_Shdr *section, 523 StringRef &Res) const; 524 const Elf_Dyn *getDyn(DataRefImpl DynData) const; 525 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version, 526 bool &IsDefault) const; 527protected: 528 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const; 529 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const; 530 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const; 531 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const; 532 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const; 533 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const; 534 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const; 535 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const; 536 virtual error_code getSymbolSection(DataRefImpl Symb, 537 section_iterator &Res) const; 538 539 friend class DynRefImpl<target_endianness, is64Bits>; 540 virtual error_code getDynNext(DataRefImpl DynData, DynRef &Result) const; 541 542 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const; 543 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const; 544 545 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const; 546 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const; 547 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const; 548 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const; 549 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const; 550 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const; 551 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const; 552 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const; 553 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const; 554 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec, 555 bool &Res) const; 556 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const; 557 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const; 558 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, 559 bool &Result) const; 560 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const; 561 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const; 562 563 virtual error_code getRelocationNext(DataRefImpl Rel, 564 RelocationRef &Res) const; 565 virtual error_code getRelocationAddress(DataRefImpl Rel, 566 uint64_t &Res) const; 567 virtual error_code getRelocationOffset(DataRefImpl Rel, 568 uint64_t &Res) const; 569 virtual error_code getRelocationSymbol(DataRefImpl Rel, 570 SymbolRef &Res) const; 571 virtual error_code getRelocationType(DataRefImpl Rel, 572 uint64_t &Res) const; 573 virtual error_code getRelocationTypeName(DataRefImpl Rel, 574 SmallVectorImpl<char> &Result) const; 575 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel, 576 int64_t &Res) const; 577 virtual error_code getRelocationValueString(DataRefImpl Rel, 578 SmallVectorImpl<char> &Result) const; 579 580public: 581 ELFObjectFile(MemoryBuffer *Object, error_code &ec); 582 virtual symbol_iterator begin_symbols() const; 583 virtual symbol_iterator end_symbols() const; 584 585 virtual symbol_iterator begin_dynamic_symbols() const; 586 virtual symbol_iterator end_dynamic_symbols() const; 587 588 virtual section_iterator begin_sections() const; 589 virtual section_iterator end_sections() const; 590 591 virtual library_iterator begin_libraries_needed() const; 592 virtual library_iterator end_libraries_needed() const; 593 594 virtual dyn_iterator begin_dynamic_table() const; 595 virtual dyn_iterator end_dynamic_table() const; 596 597 virtual uint8_t getBytesInAddress() const; 598 virtual StringRef getFileFormatName() const; 599 virtual StringRef getObjectType() const { return "ELF"; } 600 virtual unsigned getArch() const; 601 virtual StringRef getLoadName() const; 602 virtual error_code getSectionContents(const Elf_Shdr *sec, 603 StringRef &Res) const; 604 605 uint64_t getNumSections() const; 606 uint64_t getStringTableIndex() const; 607 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const; 608 const Elf_Shdr *getSection(const Elf_Sym *symb) const; 609 const Elf_Shdr *getElfSection(section_iterator &It) const; 610 const Elf_Sym *getElfSymbol(symbol_iterator &It) const; 611 612 // Methods for type inquiry through isa, cast, and dyn_cast 613 bool isDyldType() const { return isDyldELFObject; } 614 static inline bool classof(const Binary *v) { 615 return v->getType() == getELFType(target_endianness == support::little, 616 is64Bits); 617 } 618 static inline bool classof(const ELFObjectFile *v) { return true; } 619}; 620 621// Iterate through the version definitions, and place each Elf_Verdef 622// in the VersionMap according to its index. 623template<support::endianness target_endianness, bool is64Bits> 624void ELFObjectFile<target_endianness, is64Bits>:: 625 LoadVersionDefs(const Elf_Shdr *sec) const { 626 unsigned vd_size = sec->sh_size; // Size of section in bytes 627 unsigned vd_count = sec->sh_info; // Number of Verdef entries 628 const char *sec_start = (const char*)base() + sec->sh_offset; 629 const char *sec_end = sec_start + vd_size; 630 // The first Verdef entry is at the start of the section. 631 const char *p = sec_start; 632 for (unsigned i = 0; i < vd_count; i++) { 633 if (p + sizeof(Elf_Verdef) > sec_end) 634 report_fatal_error("Section ended unexpectedly while scanning " 635 "version definitions."); 636 const Elf_Verdef *vd = reinterpret_cast<const Elf_Verdef *>(p); 637 if (vd->vd_version != ELF::VER_DEF_CURRENT) 638 report_fatal_error("Unexpected verdef version"); 639 size_t index = vd->vd_ndx & ELF::VERSYM_VERSION; 640 if (index >= VersionMap.size()) 641 VersionMap.resize(index+1); 642 VersionMap[index] = VersionMapEntry(vd); 643 p += vd->vd_next; 644 } 645} 646 647// Iterate through the versions needed section, and place each Elf_Vernaux 648// in the VersionMap according to its index. 649template<support::endianness target_endianness, bool is64Bits> 650void ELFObjectFile<target_endianness, is64Bits>:: 651 LoadVersionNeeds(const Elf_Shdr *sec) const { 652 unsigned vn_size = sec->sh_size; // Size of section in bytes 653 unsigned vn_count = sec->sh_info; // Number of Verneed entries 654 const char *sec_start = (const char*)base() + sec->sh_offset; 655 const char *sec_end = sec_start + vn_size; 656 // The first Verneed entry is at the start of the section. 657 const char *p = sec_start; 658 for (unsigned i = 0; i < vn_count; i++) { 659 if (p + sizeof(Elf_Verneed) > sec_end) 660 report_fatal_error("Section ended unexpectedly while scanning " 661 "version needed records."); 662 const Elf_Verneed *vn = reinterpret_cast<const Elf_Verneed *>(p); 663 if (vn->vn_version != ELF::VER_NEED_CURRENT) 664 report_fatal_error("Unexpected verneed version"); 665 // Iterate through the Vernaux entries 666 const char *paux = p + vn->vn_aux; 667 for (unsigned j = 0; j < vn->vn_cnt; j++) { 668 if (paux + sizeof(Elf_Vernaux) > sec_end) 669 report_fatal_error("Section ended unexpected while scanning auxiliary " 670 "version needed records."); 671 const Elf_Vernaux *vna = reinterpret_cast<const Elf_Vernaux *>(paux); 672 size_t index = vna->vna_other & ELF::VERSYM_VERSION; 673 if (index >= VersionMap.size()) 674 VersionMap.resize(index+1); 675 VersionMap[index] = VersionMapEntry(vna); 676 paux += vna->vna_next; 677 } 678 p += vn->vn_next; 679 } 680} 681 682template<support::endianness target_endianness, bool is64Bits> 683void ELFObjectFile<target_endianness, is64Bits>::LoadVersionMap() const { 684 // If there is no dynamic symtab or version table, there is nothing to do. 685 if (SymbolTableSections[0] == NULL || dot_gnu_version_sec == NULL) 686 return; 687 688 // Has the VersionMap already been loaded? 689 if (VersionMap.size() > 0) 690 return; 691 692 // The first two version indexes are reserved. 693 // Index 0 is LOCAL, index 1 is GLOBAL. 694 VersionMap.push_back(VersionMapEntry()); 695 VersionMap.push_back(VersionMapEntry()); 696 697 if (dot_gnu_version_d_sec) 698 LoadVersionDefs(dot_gnu_version_d_sec); 699 700 if (dot_gnu_version_r_sec) 701 LoadVersionNeeds(dot_gnu_version_r_sec); 702} 703 704template<support::endianness target_endianness, bool is64Bits> 705void ELFObjectFile<target_endianness, is64Bits> 706 ::validateSymbol(DataRefImpl Symb) const { 707 const Elf_Sym *symb = getSymbol(Symb); 708 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; 709 // FIXME: We really need to do proper error handling in the case of an invalid 710 // input file. Because we don't use exceptions, I think we'll just pass 711 // an error object around. 712 if (!( symb 713 && SymbolTableSection 714 && symb >= (const Elf_Sym*)(base() 715 + SymbolTableSection->sh_offset) 716 && symb < (const Elf_Sym*)(base() 717 + SymbolTableSection->sh_offset 718 + SymbolTableSection->sh_size))) 719 // FIXME: Proper error handling. 720 report_fatal_error("Symb must point to a valid symbol!"); 721} 722 723template<support::endianness target_endianness, bool is64Bits> 724error_code ELFObjectFile<target_endianness, is64Bits> 725 ::getSymbolNext(DataRefImpl Symb, 726 SymbolRef &Result) const { 727 validateSymbol(Symb); 728 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; 729 730 ++Symb.d.a; 731 // Check to see if we are at the end of this symbol table. 732 if (Symb.d.a >= SymbolTableSection->getEntityCount()) { 733 // We are at the end. If there are other symbol tables, jump to them. 734 // If the symbol table is .dynsym, we are iterating dynamic symbols, 735 // and there is only one table of these. 736 if (Symb.d.b != 0) { 737 ++Symb.d.b; 738 Symb.d.a = 1; // The 0th symbol in ELF is fake. 739 } 740 // Otherwise return the terminator. 741 if (Symb.d.b == 0 || Symb.d.b >= SymbolTableSections.size()) { 742 Symb.d.a = std::numeric_limits<uint32_t>::max(); 743 Symb.d.b = std::numeric_limits<uint32_t>::max(); 744 } 745 } 746 747 Result = SymbolRef(Symb, this); 748 return object_error::success; 749} 750 751template<support::endianness target_endianness, bool is64Bits> 752error_code ELFObjectFile<target_endianness, is64Bits> 753 ::getSymbolName(DataRefImpl Symb, 754 StringRef &Result) const { 755 validateSymbol(Symb); 756 const Elf_Sym *symb = getSymbol(Symb); 757 return getSymbolName(SymbolTableSections[Symb.d.b], symb, Result); 758} 759 760template<support::endianness target_endianness, bool is64Bits> 761error_code ELFObjectFile<target_endianness, is64Bits> 762 ::getSymbolVersion(SymbolRef SymRef, 763 StringRef &Version, 764 bool &IsDefault) const { 765 DataRefImpl Symb = SymRef.getRawDataRefImpl(); 766 validateSymbol(Symb); 767 const Elf_Sym *symb = getSymbol(Symb); 768 return getSymbolVersion(SymbolTableSections[Symb.d.b], symb, 769 Version, IsDefault); 770} 771 772template<support::endianness target_endianness, bool is64Bits> 773ELF::Elf64_Word ELFObjectFile<target_endianness, is64Bits> 774 ::getSymbolTableIndex(const Elf_Sym *symb) const { 775 if (symb->st_shndx == ELF::SHN_XINDEX) 776 return ExtendedSymbolTable.lookup(symb); 777 return symb->st_shndx; 778} 779 780template<support::endianness target_endianness, bool is64Bits> 781const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * 782ELFObjectFile<target_endianness, is64Bits> 783 ::getSection(const Elf_Sym *symb) const { 784 if (symb->st_shndx == ELF::SHN_XINDEX) 785 return getSection(ExtendedSymbolTable.lookup(symb)); 786 if (symb->st_shndx >= ELF::SHN_LORESERVE) 787 return 0; 788 return getSection(symb->st_shndx); 789} 790 791template<support::endianness target_endianness, bool is64Bits> 792const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * 793ELFObjectFile<target_endianness, is64Bits> 794 ::getElfSection(section_iterator &It) const { 795 llvm::object::DataRefImpl ShdrRef = It->getRawDataRefImpl(); 796 return reinterpret_cast<const Elf_Shdr *>(ShdrRef.p); 797} 798 799template<support::endianness target_endianness, bool is64Bits> 800const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym * 801ELFObjectFile<target_endianness, is64Bits> 802 ::getElfSymbol(symbol_iterator &It) const { 803 return getSymbol(It->getRawDataRefImpl()); 804} 805 806template<support::endianness target_endianness, bool is64Bits> 807error_code ELFObjectFile<target_endianness, is64Bits> 808 ::getSymbolFileOffset(DataRefImpl Symb, 809 uint64_t &Result) const { 810 validateSymbol(Symb); 811 const Elf_Sym *symb = getSymbol(Symb); 812 const Elf_Shdr *Section; 813 switch (getSymbolTableIndex(symb)) { 814 case ELF::SHN_COMMON: 815 // Unintialized symbols have no offset in the object file 816 case ELF::SHN_UNDEF: 817 Result = UnknownAddressOrSize; 818 return object_error::success; 819 case ELF::SHN_ABS: 820 Result = symb->st_value; 821 return object_error::success; 822 default: Section = getSection(symb); 823 } 824 825 switch (symb->getType()) { 826 case ELF::STT_SECTION: 827 Result = Section ? Section->sh_addr : UnknownAddressOrSize; 828 return object_error::success; 829 case ELF::STT_FUNC: 830 case ELF::STT_OBJECT: 831 case ELF::STT_NOTYPE: 832 Result = symb->st_value + 833 (Section ? Section->sh_offset : 0); 834 return object_error::success; 835 default: 836 Result = UnknownAddressOrSize; 837 return object_error::success; 838 } 839} 840 841template<support::endianness target_endianness, bool is64Bits> 842error_code ELFObjectFile<target_endianness, is64Bits> 843 ::getSymbolAddress(DataRefImpl Symb, 844 uint64_t &Result) const { 845 validateSymbol(Symb); 846 const Elf_Sym *symb = getSymbol(Symb); 847 const Elf_Shdr *Section; 848 switch (getSymbolTableIndex(symb)) { 849 case ELF::SHN_COMMON: 850 case ELF::SHN_UNDEF: 851 Result = UnknownAddressOrSize; 852 return object_error::success; 853 case ELF::SHN_ABS: 854 Result = symb->st_value; 855 return object_error::success; 856 default: Section = getSection(symb); 857 } 858 859 switch (symb->getType()) { 860 case ELF::STT_SECTION: 861 Result = Section ? Section->sh_addr : UnknownAddressOrSize; 862 return object_error::success; 863 case ELF::STT_FUNC: 864 case ELF::STT_OBJECT: 865 case ELF::STT_NOTYPE: 866 Result = symb->st_value + (Section ? Section->sh_addr : 0); 867 return object_error::success; 868 default: 869 Result = UnknownAddressOrSize; 870 return object_error::success; 871 } 872} 873 874template<support::endianness target_endianness, bool is64Bits> 875error_code ELFObjectFile<target_endianness, is64Bits> 876 ::getSymbolSize(DataRefImpl Symb, 877 uint64_t &Result) const { 878 validateSymbol(Symb); 879 const Elf_Sym *symb = getSymbol(Symb); 880 if (symb->st_size == 0) 881 Result = UnknownAddressOrSize; 882 Result = symb->st_size; 883 return object_error::success; 884} 885 886template<support::endianness target_endianness, bool is64Bits> 887error_code ELFObjectFile<target_endianness, is64Bits> 888 ::getSymbolNMTypeChar(DataRefImpl Symb, 889 char &Result) const { 890 validateSymbol(Symb); 891 const Elf_Sym *symb = getSymbol(Symb); 892 const Elf_Shdr *Section = getSection(symb); 893 894 char ret = '?'; 895 896 if (Section) { 897 switch (Section->sh_type) { 898 case ELF::SHT_PROGBITS: 899 case ELF::SHT_DYNAMIC: 900 switch (Section->sh_flags) { 901 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR): 902 ret = 't'; break; 903 case (ELF::SHF_ALLOC | ELF::SHF_WRITE): 904 ret = 'd'; break; 905 case ELF::SHF_ALLOC: 906 case (ELF::SHF_ALLOC | ELF::SHF_MERGE): 907 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS): 908 ret = 'r'; break; 909 } 910 break; 911 case ELF::SHT_NOBITS: ret = 'b'; 912 } 913 } 914 915 switch (getSymbolTableIndex(symb)) { 916 case ELF::SHN_UNDEF: 917 if (ret == '?') 918 ret = 'U'; 919 break; 920 case ELF::SHN_ABS: ret = 'a'; break; 921 case ELF::SHN_COMMON: ret = 'c'; break; 922 } 923 924 switch (symb->getBinding()) { 925 case ELF::STB_GLOBAL: ret = ::toupper(ret); break; 926 case ELF::STB_WEAK: 927 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) 928 ret = 'w'; 929 else 930 if (symb->getType() == ELF::STT_OBJECT) 931 ret = 'V'; 932 else 933 ret = 'W'; 934 } 935 936 if (ret == '?' && symb->getType() == ELF::STT_SECTION) { 937 StringRef name; 938 if (error_code ec = getSymbolName(Symb, name)) 939 return ec; 940 Result = StringSwitch<char>(name) 941 .StartsWith(".debug", 'N') 942 .StartsWith(".note", 'n') 943 .Default('?'); 944 return object_error::success; 945 } 946 947 Result = ret; 948 return object_error::success; 949} 950 951template<support::endianness target_endianness, bool is64Bits> 952error_code ELFObjectFile<target_endianness, is64Bits> 953 ::getSymbolType(DataRefImpl Symb, 954 SymbolRef::Type &Result) const { 955 validateSymbol(Symb); 956 const Elf_Sym *symb = getSymbol(Symb); 957 958 switch (symb->getType()) { 959 case ELF::STT_NOTYPE: 960 Result = SymbolRef::ST_Unknown; 961 break; 962 case ELF::STT_SECTION: 963 Result = SymbolRef::ST_Debug; 964 break; 965 case ELF::STT_FILE: 966 Result = SymbolRef::ST_File; 967 break; 968 case ELF::STT_FUNC: 969 Result = SymbolRef::ST_Function; 970 break; 971 case ELF::STT_OBJECT: 972 case ELF::STT_COMMON: 973 case ELF::STT_TLS: 974 Result = SymbolRef::ST_Data; 975 break; 976 default: 977 Result = SymbolRef::ST_Other; 978 break; 979 } 980 return object_error::success; 981} 982 983template<support::endianness target_endianness, bool is64Bits> 984error_code ELFObjectFile<target_endianness, is64Bits> 985 ::getSymbolFlags(DataRefImpl Symb, 986 uint32_t &Result) const { 987 validateSymbol(Symb); 988 const Elf_Sym *symb = getSymbol(Symb); 989 990 Result = SymbolRef::SF_None; 991 992 if (symb->getBinding() != ELF::STB_LOCAL) 993 Result |= SymbolRef::SF_Global; 994 995 if (symb->getBinding() == ELF::STB_WEAK) 996 Result |= SymbolRef::SF_Weak; 997 998 if (symb->st_shndx == ELF::SHN_ABS) 999 Result |= SymbolRef::SF_Absolute; 1000 1001 if (symb->getType() == ELF::STT_FILE || 1002 symb->getType() == ELF::STT_SECTION) 1003 Result |= SymbolRef::SF_FormatSpecific; 1004 1005 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) 1006 Result |= SymbolRef::SF_Undefined; 1007 1008 if (symb->getType() == ELF::STT_COMMON || 1009 getSymbolTableIndex(symb) == ELF::SHN_COMMON) 1010 Result |= SymbolRef::SF_Common; 1011 1012 if (symb->getType() == ELF::STT_TLS) 1013 Result |= SymbolRef::SF_ThreadLocal; 1014 1015 return object_error::success; 1016} 1017 1018template<support::endianness target_endianness, bool is64Bits> 1019error_code ELFObjectFile<target_endianness, is64Bits> 1020 ::getSymbolSection(DataRefImpl Symb, 1021 section_iterator &Res) const { 1022 validateSymbol(Symb); 1023 const Elf_Sym *symb = getSymbol(Symb); 1024 const Elf_Shdr *sec = getSection(symb); 1025 if (!sec) 1026 Res = end_sections(); 1027 else { 1028 DataRefImpl Sec; 1029 Sec.p = reinterpret_cast<intptr_t>(sec); 1030 Res = section_iterator(SectionRef(Sec, this)); 1031 } 1032 return object_error::success; 1033} 1034 1035template<support::endianness target_endianness, bool is64Bits> 1036error_code ELFObjectFile<target_endianness, is64Bits> 1037 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const { 1038 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p); 1039 sec += Header->e_shentsize; 1040 Sec.p = reinterpret_cast<intptr_t>(sec); 1041 Result = SectionRef(Sec, this); 1042 return object_error::success; 1043} 1044 1045template<support::endianness target_endianness, bool is64Bits> 1046error_code ELFObjectFile<target_endianness, is64Bits> 1047 ::getSectionName(DataRefImpl Sec, 1048 StringRef &Result) const { 1049 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1050 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name)); 1051 return object_error::success; 1052} 1053 1054template<support::endianness target_endianness, bool is64Bits> 1055error_code ELFObjectFile<target_endianness, is64Bits> 1056 ::getSectionAddress(DataRefImpl Sec, 1057 uint64_t &Result) const { 1058 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1059 Result = sec->sh_addr; 1060 return object_error::success; 1061} 1062 1063template<support::endianness target_endianness, bool is64Bits> 1064error_code ELFObjectFile<target_endianness, is64Bits> 1065 ::getSectionSize(DataRefImpl Sec, 1066 uint64_t &Result) const { 1067 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1068 Result = sec->sh_size; 1069 return object_error::success; 1070} 1071 1072template<support::endianness target_endianness, bool is64Bits> 1073error_code ELFObjectFile<target_endianness, is64Bits> 1074 ::getSectionContents(DataRefImpl Sec, 1075 StringRef &Result) const { 1076 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1077 const char *start = (const char*)base() + sec->sh_offset; 1078 Result = StringRef(start, sec->sh_size); 1079 return object_error::success; 1080} 1081 1082template<support::endianness target_endianness, bool is64Bits> 1083error_code ELFObjectFile<target_endianness, is64Bits> 1084 ::getSectionContents(const Elf_Shdr *Sec, 1085 StringRef &Result) const { 1086 const char *start = (const char*)base() + Sec->sh_offset; 1087 Result = StringRef(start, Sec->sh_size); 1088 return object_error::success; 1089} 1090 1091template<support::endianness target_endianness, bool is64Bits> 1092error_code ELFObjectFile<target_endianness, is64Bits> 1093 ::getSectionAlignment(DataRefImpl Sec, 1094 uint64_t &Result) const { 1095 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1096 Result = sec->sh_addralign; 1097 return object_error::success; 1098} 1099 1100template<support::endianness target_endianness, bool is64Bits> 1101error_code ELFObjectFile<target_endianness, is64Bits> 1102 ::isSectionText(DataRefImpl Sec, 1103 bool &Result) const { 1104 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1105 if (sec->sh_flags & ELF::SHF_EXECINSTR) 1106 Result = true; 1107 else 1108 Result = false; 1109 return object_error::success; 1110} 1111 1112template<support::endianness target_endianness, bool is64Bits> 1113error_code ELFObjectFile<target_endianness, is64Bits> 1114 ::isSectionData(DataRefImpl Sec, 1115 bool &Result) const { 1116 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1117 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) 1118 && sec->sh_type == ELF::SHT_PROGBITS) 1119 Result = true; 1120 else 1121 Result = false; 1122 return object_error::success; 1123} 1124 1125template<support::endianness target_endianness, bool is64Bits> 1126error_code ELFObjectFile<target_endianness, is64Bits> 1127 ::isSectionBSS(DataRefImpl Sec, 1128 bool &Result) const { 1129 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1130 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) 1131 && sec->sh_type == ELF::SHT_NOBITS) 1132 Result = true; 1133 else 1134 Result = false; 1135 return object_error::success; 1136} 1137 1138template<support::endianness target_endianness, bool is64Bits> 1139error_code ELFObjectFile<target_endianness, is64Bits> 1140 ::isSectionRequiredForExecution(DataRefImpl Sec, 1141 bool &Result) const { 1142 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1143 if (sec->sh_flags & ELF::SHF_ALLOC) 1144 Result = true; 1145 else 1146 Result = false; 1147 return object_error::success; 1148} 1149 1150template<support::endianness target_endianness, bool is64Bits> 1151error_code ELFObjectFile<target_endianness, is64Bits> 1152 ::isSectionVirtual(DataRefImpl Sec, 1153 bool &Result) const { 1154 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1155 if (sec->sh_type == ELF::SHT_NOBITS) 1156 Result = true; 1157 else 1158 Result = false; 1159 return object_error::success; 1160} 1161 1162template<support::endianness target_endianness, bool is64Bits> 1163error_code ELFObjectFile<target_endianness, is64Bits>::isSectionZeroInit(DataRefImpl Sec, 1164 bool &Result) const { 1165 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1166 // For ELF, all zero-init sections are virtual (that is, they occupy no space 1167 // in the object image) and vice versa. 1168 if (sec->sh_flags & ELF::SHT_NOBITS) 1169 Result = true; 1170 else 1171 Result = false; 1172 return object_error::success; 1173} 1174 1175template<support::endianness target_endianness, bool is64Bits> 1176error_code ELFObjectFile<target_endianness, is64Bits> 1177 ::sectionContainsSymbol(DataRefImpl Sec, 1178 DataRefImpl Symb, 1179 bool &Result) const { 1180 // FIXME: Unimplemented. 1181 Result = false; 1182 return object_error::success; 1183} 1184 1185template<support::endianness target_endianness, bool is64Bits> 1186relocation_iterator ELFObjectFile<target_endianness, is64Bits> 1187 ::getSectionRelBegin(DataRefImpl Sec) const { 1188 DataRefImpl RelData; 1189 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1190 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec); 1191 if (sec != 0 && ittr != SectionRelocMap.end()) { 1192 RelData.w.a = getSection(ittr->second[0])->sh_info; 1193 RelData.w.b = ittr->second[0]; 1194 RelData.w.c = 0; 1195 } 1196 return relocation_iterator(RelocationRef(RelData, this)); 1197} 1198 1199template<support::endianness target_endianness, bool is64Bits> 1200relocation_iterator ELFObjectFile<target_endianness, is64Bits> 1201 ::getSectionRelEnd(DataRefImpl Sec) const { 1202 DataRefImpl RelData; 1203 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 1204 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec); 1205 if (sec != 0 && ittr != SectionRelocMap.end()) { 1206 // Get the index of the last relocation section for this section. 1207 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1]; 1208 const Elf_Shdr *relocsec = getSection(relocsecindex); 1209 RelData.w.a = relocsec->sh_info; 1210 RelData.w.b = relocsecindex; 1211 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize; 1212 } 1213 return relocation_iterator(RelocationRef(RelData, this)); 1214} 1215 1216// Relocations 1217template<support::endianness target_endianness, bool is64Bits> 1218error_code ELFObjectFile<target_endianness, is64Bits> 1219 ::getRelocationNext(DataRefImpl Rel, 1220 RelocationRef &Result) const { 1221 ++Rel.w.c; 1222 const Elf_Shdr *relocsec = getSection(Rel.w.b); 1223 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) { 1224 // We have reached the end of the relocations for this section. See if there 1225 // is another relocation section. 1226 typename RelocMap_t::mapped_type relocseclist = 1227 SectionRelocMap.lookup(getSection(Rel.w.a)); 1228 1229 // Do a binary search for the current reloc section index (which must be 1230 // present). Then get the next one. 1231 typename RelocMap_t::mapped_type::const_iterator loc = 1232 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b); 1233 ++loc; 1234 1235 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel 1236 // to the end iterator. 1237 if (loc != relocseclist.end()) { 1238 Rel.w.b = *loc; 1239 Rel.w.a = 0; 1240 } 1241 } 1242 Result = RelocationRef(Rel, this); 1243 return object_error::success; 1244} 1245 1246template<support::endianness target_endianness, bool is64Bits> 1247error_code ELFObjectFile<target_endianness, is64Bits> 1248 ::getRelocationSymbol(DataRefImpl Rel, 1249 SymbolRef &Result) const { 1250 uint32_t symbolIdx; 1251 const Elf_Shdr *sec = getSection(Rel.w.b); 1252 switch (sec->sh_type) { 1253 default : 1254 report_fatal_error("Invalid section type in Rel!"); 1255 case ELF::SHT_REL : { 1256 symbolIdx = getRel(Rel)->getSymbol(); 1257 break; 1258 } 1259 case ELF::SHT_RELA : { 1260 symbolIdx = getRela(Rel)->getSymbol(); 1261 break; 1262 } 1263 } 1264 DataRefImpl SymbolData; 1265 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link); 1266 if (it == SymbolTableSectionsIndexMap.end()) 1267 report_fatal_error("Relocation symbol table not found!"); 1268 SymbolData.d.a = symbolIdx; 1269 SymbolData.d.b = it->second; 1270 Result = SymbolRef(SymbolData, this); 1271 return object_error::success; 1272} 1273 1274template<support::endianness target_endianness, bool is64Bits> 1275error_code ELFObjectFile<target_endianness, is64Bits> 1276 ::getRelocationAddress(DataRefImpl Rel, 1277 uint64_t &Result) const { 1278 uint64_t offset; 1279 const Elf_Shdr *sec = getSection(Rel.w.b); 1280 switch (sec->sh_type) { 1281 default : 1282 report_fatal_error("Invalid section type in Rel!"); 1283 case ELF::SHT_REL : { 1284 offset = getRel(Rel)->r_offset; 1285 break; 1286 } 1287 case ELF::SHT_RELA : { 1288 offset = getRela(Rel)->r_offset; 1289 break; 1290 } 1291 } 1292 1293 Result = offset; 1294 return object_error::success; 1295} 1296 1297template<support::endianness target_endianness, bool is64Bits> 1298error_code ELFObjectFile<target_endianness, is64Bits> 1299 ::getRelocationOffset(DataRefImpl Rel, 1300 uint64_t &Result) const { 1301 uint64_t offset; 1302 const Elf_Shdr *sec = getSection(Rel.w.b); 1303 switch (sec->sh_type) { 1304 default : 1305 report_fatal_error("Invalid section type in Rel!"); 1306 case ELF::SHT_REL : { 1307 offset = getRel(Rel)->r_offset; 1308 break; 1309 } 1310 case ELF::SHT_RELA : { 1311 offset = getRela(Rel)->r_offset; 1312 break; 1313 } 1314 } 1315 1316 Result = offset - sec->sh_addr; 1317 return object_error::success; 1318} 1319 1320template<support::endianness target_endianness, bool is64Bits> 1321error_code ELFObjectFile<target_endianness, is64Bits> 1322 ::getRelocationType(DataRefImpl Rel, 1323 uint64_t &Result) const { 1324 const Elf_Shdr *sec = getSection(Rel.w.b); 1325 switch (sec->sh_type) { 1326 default : 1327 report_fatal_error("Invalid section type in Rel!"); 1328 case ELF::SHT_REL : { 1329 Result = getRel(Rel)->getType(); 1330 break; 1331 } 1332 case ELF::SHT_RELA : { 1333 Result = getRela(Rel)->getType(); 1334 break; 1335 } 1336 } 1337 return object_error::success; 1338} 1339 1340#define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \ 1341 case ELF::enum: res = #enum; break; 1342 1343template<support::endianness target_endianness, bool is64Bits> 1344error_code ELFObjectFile<target_endianness, is64Bits> 1345 ::getRelocationTypeName(DataRefImpl Rel, 1346 SmallVectorImpl<char> &Result) const { 1347 const Elf_Shdr *sec = getSection(Rel.w.b); 1348 uint8_t type; 1349 StringRef res; 1350 switch (sec->sh_type) { 1351 default : 1352 return object_error::parse_failed; 1353 case ELF::SHT_REL : { 1354 type = getRel(Rel)->getType(); 1355 break; 1356 } 1357 case ELF::SHT_RELA : { 1358 type = getRela(Rel)->getType(); 1359 break; 1360 } 1361 } 1362 switch (Header->e_machine) { 1363 case ELF::EM_X86_64: 1364 switch (type) { 1365 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE); 1366 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64); 1367 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32); 1368 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32); 1369 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32); 1370 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY); 1371 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT); 1372 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT); 1373 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE); 1374 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL); 1375 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32); 1376 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S); 1377 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16); 1378 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16); 1379 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8); 1380 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8); 1381 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64); 1382 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64); 1383 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64); 1384 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD); 1385 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD); 1386 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32); 1387 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF); 1388 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32); 1389 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64); 1390 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64); 1391 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32); 1392 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32); 1393 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64); 1394 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC); 1395 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL); 1396 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC); 1397 default: 1398 res = "Unknown"; 1399 } 1400 break; 1401 case ELF::EM_386: 1402 switch (type) { 1403 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE); 1404 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32); 1405 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32); 1406 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32); 1407 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32); 1408 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY); 1409 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT); 1410 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT); 1411 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE); 1412 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF); 1413 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC); 1414 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT); 1415 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF); 1416 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE); 1417 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE); 1418 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE); 1419 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD); 1420 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM); 1421 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16); 1422 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16); 1423 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8); 1424 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8); 1425 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32); 1426 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH); 1427 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL); 1428 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP); 1429 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32); 1430 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH); 1431 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL); 1432 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP); 1433 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32); 1434 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32); 1435 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32); 1436 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32); 1437 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32); 1438 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32); 1439 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC); 1440 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL); 1441 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC); 1442 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE); 1443 default: 1444 res = "Unknown"; 1445 } 1446 break; 1447 case ELF::EM_ARM: 1448 switch (type) { 1449 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_NONE); 1450 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PC24); 1451 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32); 1452 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32); 1453 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G0); 1454 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS16); 1455 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS12); 1456 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ABS5); 1457 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS8); 1458 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL32); 1459 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_CALL); 1460 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC8); 1461 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BREL_ADJ); 1462 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESC); 1463 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_SWI8); 1464 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_XPC25); 1465 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_XPC22); 1466 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPMOD32); 1467 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DTPOFF32); 1468 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_TPOFF32); 1469 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_COPY); 1470 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GLOB_DAT); 1471 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP_SLOT); 1472 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_RELATIVE); 1473 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF32); 1474 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_PREL); 1475 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL); 1476 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32); 1477 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_CALL); 1478 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_JUMP24); 1479 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP24); 1480 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_BASE_ABS); 1481 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_7_0); 1482 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_15_8); 1483 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PCREL_23_15); 1484 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SBREL_11_0_NC); 1485 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_19_12_NC); 1486 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SBREL_27_20_CK); 1487 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET1); 1488 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_SBREL31); 1489 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_V4BX); 1490 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TARGET2); 1491 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PREL31); 1492 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_ABS_NC); 1493 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_ABS); 1494 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_PREL_NC); 1495 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_PREL); 1496 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_ABS_NC); 1497 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_ABS); 1498 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_PREL_NC); 1499 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_PREL); 1500 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP19); 1501 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP6); 1502 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_ALU_PREL_11_0); 1503 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_PC12); 1504 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ABS32_NOI); 1505 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_REL32_NOI); 1506 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0_NC); 1507 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G0); 1508 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1_NC); 1509 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G1); 1510 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_PC_G2); 1511 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G1); 1512 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_PC_G2); 1513 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G0); 1514 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G1); 1515 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_PC_G2); 1516 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G0); 1517 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G1); 1518 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_PC_G2); 1519 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0_NC); 1520 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G0); 1521 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1_NC); 1522 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G1); 1523 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ALU_SB_G2); 1524 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G0); 1525 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G1); 1526 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDR_SB_G2); 1527 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G0); 1528 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G1); 1529 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDRS_SB_G2); 1530 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G0); 1531 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G1); 1532 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_LDC_SB_G2); 1533 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL_NC); 1534 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVT_BREL); 1535 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_MOVW_BREL); 1536 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL_NC); 1537 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVT_BREL); 1538 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_MOVW_BREL); 1539 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GOTDESC); 1540 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_CALL); 1541 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_DESCSEQ); 1542 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_CALL); 1543 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PLT32_ABS); 1544 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_ABS); 1545 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_PREL); 1546 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOT_BREL12); 1547 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTOFF12); 1548 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GOTRELAX); 1549 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTENTRY); 1550 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_GNU_VTINHERIT); 1551 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP11); 1552 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_JUMP8); 1553 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_GD32); 1554 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDM32); 1555 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO32); 1556 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE32); 1557 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE32); 1558 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LDO12); 1559 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_LE12); 1560 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_TLS_IE12GP); 1561 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_0); 1562 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_1); 1563 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_2); 1564 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_3); 1565 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_4); 1566 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_5); 1567 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_6); 1568 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_7); 1569 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_8); 1570 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_9); 1571 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_10); 1572 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_11); 1573 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_12); 1574 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_13); 1575 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_14); 1576 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_PRIVATE_15); 1577 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_ME_TOO); 1578 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ16); 1579 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_ARM_THM_TLS_DESCSEQ32); 1580 default: 1581 res = "Unknown"; 1582 } 1583 break; 1584 case ELF::EM_HEXAGON: 1585 switch (type) { 1586 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_NONE); 1587 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL); 1588 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL); 1589 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL); 1590 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_LO16); 1591 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HI16); 1592 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32); 1593 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16); 1594 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8); 1595 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_0); 1596 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_1); 1597 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_2); 1598 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GPREL16_3); 1599 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_HL16); 1600 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL); 1601 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL); 1602 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B32_PCREL_X); 1603 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_6_X); 1604 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B22_PCREL_X); 1605 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B15_PCREL_X); 1606 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B13_PCREL_X); 1607 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B9_PCREL_X); 1608 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_B7_PCREL_X); 1609 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_16_X); 1610 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_12_X); 1611 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_11_X); 1612 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_10_X); 1613 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_9_X); 1614 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_8_X); 1615 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_7_X); 1616 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_X); 1617 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_32_PCREL); 1618 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_COPY); 1619 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GLOB_DAT); 1620 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_JMP_SLOT); 1621 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_RELATIVE); 1622 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_PLT_B22_PCREL); 1623 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_LO16); 1624 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_HI16); 1625 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32); 1626 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_LO16); 1627 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_HI16); 1628 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32); 1629 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16); 1630 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPMOD_32); 1631 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_LO16); 1632 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_HI16); 1633 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32); 1634 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16); 1635 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_PLT_B22_PCREL); 1636 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_LO16); 1637 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_HI16); 1638 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32); 1639 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16); 1640 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_LO16); 1641 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_HI16); 1642 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32); 1643 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_LO16); 1644 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_HI16); 1645 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32); 1646 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16); 1647 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_LO16); 1648 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_HI16); 1649 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32); 1650 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16); 1651 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_6_PCREL_X); 1652 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_32_6_X); 1653 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_16_X); 1654 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOTREL_11_X); 1655 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_32_6_X); 1656 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_16_X); 1657 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GOT_11_X); 1658 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_32_6_X); 1659 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_16_X); 1660 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_DTPREL_11_X); 1661 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_32_6_X); 1662 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_16_X); 1663 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_GD_GOT_11_X); 1664 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_32_6_X); 1665 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_16_X); 1666 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_32_6_X); 1667 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_16_X); 1668 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_IE_GOT_11_X); 1669 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_32_6_X); 1670 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_16_X); 1671 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_HEX_TPREL_11_X); 1672 default: 1673 res = "Unknown"; 1674 } 1675 break; 1676 default: 1677 res = "Unknown"; 1678 } 1679 Result.append(res.begin(), res.end()); 1680 return object_error::success; 1681} 1682 1683#undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME 1684 1685template<support::endianness target_endianness, bool is64Bits> 1686error_code ELFObjectFile<target_endianness, is64Bits> 1687 ::getRelocationAdditionalInfo(DataRefImpl Rel, 1688 int64_t &Result) const { 1689 const Elf_Shdr *sec = getSection(Rel.w.b); 1690 switch (sec->sh_type) { 1691 default : 1692 report_fatal_error("Invalid section type in Rel!"); 1693 case ELF::SHT_REL : { 1694 Result = 0; 1695 return object_error::success; 1696 } 1697 case ELF::SHT_RELA : { 1698 Result = getRela(Rel)->r_addend; 1699 return object_error::success; 1700 } 1701 } 1702} 1703 1704template<support::endianness target_endianness, bool is64Bits> 1705error_code ELFObjectFile<target_endianness, is64Bits> 1706 ::getRelocationValueString(DataRefImpl Rel, 1707 SmallVectorImpl<char> &Result) const { 1708 const Elf_Shdr *sec = getSection(Rel.w.b); 1709 uint8_t type; 1710 StringRef res; 1711 int64_t addend = 0; 1712 uint16_t symbol_index = 0; 1713 switch (sec->sh_type) { 1714 default : 1715 return object_error::parse_failed; 1716 case ELF::SHT_REL : { 1717 type = getRel(Rel)->getType(); 1718 symbol_index = getRel(Rel)->getSymbol(); 1719 // TODO: Read implicit addend from section data. 1720 break; 1721 } 1722 case ELF::SHT_RELA : { 1723 type = getRela(Rel)->getType(); 1724 symbol_index = getRela(Rel)->getSymbol(); 1725 addend = getRela(Rel)->r_addend; 1726 break; 1727 } 1728 } 1729 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index); 1730 StringRef symname; 1731 if (error_code ec = getSymbolName(getSection(sec->sh_link), symb, symname)) 1732 return ec; 1733 switch (Header->e_machine) { 1734 case ELF::EM_X86_64: 1735 switch (type) { 1736 case ELF::R_X86_64_32S: 1737 res = symname; 1738 break; 1739 case ELF::R_X86_64_PC32: { 1740 std::string fmtbuf; 1741 raw_string_ostream fmt(fmtbuf); 1742 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P"; 1743 fmt.flush(); 1744 Result.append(fmtbuf.begin(), fmtbuf.end()); 1745 } 1746 break; 1747 default: 1748 res = "Unknown"; 1749 } 1750 break; 1751 case ELF::EM_ARM: 1752 case ELF::EM_HEXAGON: 1753 res = symname; 1754 break; 1755 default: 1756 res = "Unknown"; 1757 } 1758 if (Result.empty()) 1759 Result.append(res.begin(), res.end()); 1760 return object_error::success; 1761} 1762 1763// Verify that the last byte in the string table in a null. 1764template<support::endianness target_endianness, bool is64Bits> 1765void ELFObjectFile<target_endianness, is64Bits> 1766 ::VerifyStrTab(const Elf_Shdr *sh) const { 1767 const char *strtab = (const char*)base() + sh->sh_offset; 1768 if (strtab[sh->sh_size - 1] != 0) 1769 // FIXME: Proper error handling. 1770 report_fatal_error("String table must end with a null terminator!"); 1771} 1772 1773template<support::endianness target_endianness, bool is64Bits> 1774ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object 1775 , error_code &ec) 1776 : ObjectFile(getELFType(target_endianness == support::little, is64Bits), 1777 Object, ec) 1778 , isDyldELFObject(false) 1779 , SectionHeaderTable(0) 1780 , dot_shstrtab_sec(0) 1781 , dot_strtab_sec(0) 1782 , dot_dynstr_sec(0) 1783 , dot_dynamic_sec(0) 1784 , dot_gnu_version_sec(0) 1785 , dot_gnu_version_r_sec(0) 1786 , dot_gnu_version_d_sec(0) 1787 , dt_soname(0) 1788 { 1789 1790 const uint64_t FileSize = Data->getBufferSize(); 1791 1792 if (sizeof(Elf_Ehdr) > FileSize) 1793 // FIXME: Proper error handling. 1794 report_fatal_error("File too short!"); 1795 1796 Header = reinterpret_cast<const Elf_Ehdr *>(base()); 1797 1798 if (Header->e_shoff == 0) 1799 return; 1800 1801 const uint64_t SectionTableOffset = Header->e_shoff; 1802 1803 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) 1804 // FIXME: Proper error handling. 1805 report_fatal_error("Section header table goes past end of file!"); 1806 1807 // The getNumSections() call below depends on SectionHeaderTable being set. 1808 SectionHeaderTable = 1809 reinterpret_cast<const Elf_Shdr *>(base() + SectionTableOffset); 1810 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize; 1811 1812 if (SectionTableOffset + SectionTableSize > FileSize) 1813 // FIXME: Proper error handling. 1814 report_fatal_error("Section table goes past end of file!"); 1815 1816 // To find the symbol tables we walk the section table to find SHT_SYMTAB. 1817 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0; 1818 const Elf_Shdr* sh = SectionHeaderTable; 1819 1820 // Reserve SymbolTableSections[0] for .dynsym 1821 SymbolTableSections.push_back(NULL); 1822 1823 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) { 1824 switch (sh->sh_type) { 1825 case ELF::SHT_SYMTAB_SHNDX: { 1826 if (SymbolTableSectionHeaderIndex) 1827 // FIXME: Proper error handling. 1828 report_fatal_error("More than one .symtab_shndx!"); 1829 SymbolTableSectionHeaderIndex = sh; 1830 break; 1831 } 1832 case ELF::SHT_SYMTAB: { 1833 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size(); 1834 SymbolTableSections.push_back(sh); 1835 break; 1836 } 1837 case ELF::SHT_DYNSYM: { 1838 if (SymbolTableSections[0] != NULL) 1839 // FIXME: Proper error handling. 1840 report_fatal_error("More than one .dynsym!"); 1841 SymbolTableSectionsIndexMap[i] = 0; 1842 SymbolTableSections[0] = sh; 1843 break; 1844 } 1845 case ELF::SHT_REL: 1846 case ELF::SHT_RELA: { 1847 SectionRelocMap[getSection(sh->sh_info)].push_back(i); 1848 break; 1849 } 1850 case ELF::SHT_DYNAMIC: { 1851 if (dot_dynamic_sec != NULL) 1852 // FIXME: Proper error handling. 1853 report_fatal_error("More than one .dynamic!"); 1854 dot_dynamic_sec = sh; 1855 break; 1856 } 1857 case ELF::SHT_GNU_versym: { 1858 if (dot_gnu_version_sec != NULL) 1859 // FIXME: Proper error handling. 1860 report_fatal_error("More than one .gnu.version section!"); 1861 dot_gnu_version_sec = sh; 1862 break; 1863 } 1864 case ELF::SHT_GNU_verdef: { 1865 if (dot_gnu_version_d_sec != NULL) 1866 // FIXME: Proper error handling. 1867 report_fatal_error("More than one .gnu.version_d section!"); 1868 dot_gnu_version_d_sec = sh; 1869 break; 1870 } 1871 case ELF::SHT_GNU_verneed: { 1872 if (dot_gnu_version_r_sec != NULL) 1873 // FIXME: Proper error handling. 1874 report_fatal_error("More than one .gnu.version_r section!"); 1875 dot_gnu_version_r_sec = sh; 1876 break; 1877 } 1878 } 1879 ++sh; 1880 } 1881 1882 // Sort section relocation lists by index. 1883 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(), 1884 e = SectionRelocMap.end(); i != e; ++i) { 1885 std::sort(i->second.begin(), i->second.end()); 1886 } 1887 1888 // Get string table sections. 1889 dot_shstrtab_sec = getSection(getStringTableIndex()); 1890 if (dot_shstrtab_sec) { 1891 // Verify that the last byte in the string table in a null. 1892 VerifyStrTab(dot_shstrtab_sec); 1893 } 1894 1895 // Merge this into the above loop. 1896 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable), 1897 *e = i + getNumSections() * Header->e_shentsize; 1898 i != e; i += Header->e_shentsize) { 1899 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i); 1900 if (sh->sh_type == ELF::SHT_STRTAB) { 1901 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name)); 1902 if (SectionName == ".strtab") { 1903 if (dot_strtab_sec != 0) 1904 // FIXME: Proper error handling. 1905 report_fatal_error("Already found section named .strtab!"); 1906 dot_strtab_sec = sh; 1907 VerifyStrTab(dot_strtab_sec); 1908 } else if (SectionName == ".dynstr") { 1909 if (dot_dynstr_sec != 0) 1910 // FIXME: Proper error handling. 1911 report_fatal_error("Already found section named .dynstr!"); 1912 dot_dynstr_sec = sh; 1913 VerifyStrTab(dot_dynstr_sec); 1914 } 1915 } 1916 } 1917 1918 // Build symbol name side-mapping if there is one. 1919 if (SymbolTableSectionHeaderIndex) { 1920 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() + 1921 SymbolTableSectionHeaderIndex->sh_offset); 1922 error_code ec; 1923 for (symbol_iterator si = begin_symbols(), 1924 se = end_symbols(); si != se; si.increment(ec)) { 1925 if (ec) 1926 report_fatal_error("Fewer extended symbol table entries than symbols!"); 1927 if (*ShndxTable != ELF::SHN_UNDEF) 1928 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable; 1929 ++ShndxTable; 1930 } 1931 } 1932} 1933 1934template<support::endianness target_endianness, bool is64Bits> 1935symbol_iterator ELFObjectFile<target_endianness, is64Bits> 1936 ::begin_symbols() const { 1937 DataRefImpl SymbolData; 1938 if (SymbolTableSections.size() <= 1) { 1939 SymbolData.d.a = std::numeric_limits<uint32_t>::max(); 1940 SymbolData.d.b = std::numeric_limits<uint32_t>::max(); 1941 } else { 1942 SymbolData.d.a = 1; // The 0th symbol in ELF is fake. 1943 SymbolData.d.b = 1; // The 0th table is .dynsym 1944 } 1945 return symbol_iterator(SymbolRef(SymbolData, this)); 1946} 1947 1948template<support::endianness target_endianness, bool is64Bits> 1949symbol_iterator ELFObjectFile<target_endianness, is64Bits> 1950 ::end_symbols() const { 1951 DataRefImpl SymbolData; 1952 SymbolData.d.a = std::numeric_limits<uint32_t>::max(); 1953 SymbolData.d.b = std::numeric_limits<uint32_t>::max(); 1954 return symbol_iterator(SymbolRef(SymbolData, this)); 1955} 1956 1957template<support::endianness target_endianness, bool is64Bits> 1958symbol_iterator ELFObjectFile<target_endianness, is64Bits> 1959 ::begin_dynamic_symbols() const { 1960 DataRefImpl SymbolData; 1961 if (SymbolTableSections[0] == NULL) { 1962 SymbolData.d.a = std::numeric_limits<uint32_t>::max(); 1963 SymbolData.d.b = std::numeric_limits<uint32_t>::max(); 1964 } else { 1965 SymbolData.d.a = 1; // The 0th symbol in ELF is fake. 1966 SymbolData.d.b = 0; // The 0th table is .dynsym 1967 } 1968 return symbol_iterator(SymbolRef(SymbolData, this)); 1969} 1970 1971template<support::endianness target_endianness, bool is64Bits> 1972symbol_iterator ELFObjectFile<target_endianness, is64Bits> 1973 ::end_dynamic_symbols() const { 1974 DataRefImpl SymbolData; 1975 SymbolData.d.a = std::numeric_limits<uint32_t>::max(); 1976 SymbolData.d.b = std::numeric_limits<uint32_t>::max(); 1977 return symbol_iterator(SymbolRef(SymbolData, this)); 1978} 1979 1980template<support::endianness target_endianness, bool is64Bits> 1981section_iterator ELFObjectFile<target_endianness, is64Bits> 1982 ::begin_sections() const { 1983 DataRefImpl ret; 1984 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff); 1985 return section_iterator(SectionRef(ret, this)); 1986} 1987 1988template<support::endianness target_endianness, bool is64Bits> 1989section_iterator ELFObjectFile<target_endianness, is64Bits> 1990 ::end_sections() const { 1991 DataRefImpl ret; 1992 ret.p = reinterpret_cast<intptr_t>(base() 1993 + Header->e_shoff 1994 + (Header->e_shentsize*getNumSections())); 1995 return section_iterator(SectionRef(ret, this)); 1996} 1997 1998template<support::endianness target_endianness, bool is64Bits> 1999typename ELFObjectFile<target_endianness, is64Bits>::dyn_iterator 2000ELFObjectFile<target_endianness, is64Bits>::begin_dynamic_table() const { 2001 DataRefImpl DynData; 2002 if (dot_dynamic_sec == NULL || dot_dynamic_sec->sh_size == 0) { 2003 DynData.d.a = std::numeric_limits<uint32_t>::max(); 2004 } else { 2005 DynData.d.a = 0; 2006 } 2007 return dyn_iterator(DynRef(DynData, this)); 2008} 2009 2010template<support::endianness target_endianness, bool is64Bits> 2011typename ELFObjectFile<target_endianness, is64Bits>::dyn_iterator 2012ELFObjectFile<target_endianness, is64Bits> 2013 ::end_dynamic_table() const { 2014 DataRefImpl DynData; 2015 DynData.d.a = std::numeric_limits<uint32_t>::max(); 2016 return dyn_iterator(DynRef(DynData, this)); 2017} 2018 2019template<support::endianness target_endianness, bool is64Bits> 2020error_code ELFObjectFile<target_endianness, is64Bits> 2021 ::getDynNext(DataRefImpl DynData, 2022 DynRef &Result) const { 2023 ++DynData.d.a; 2024 2025 // Check to see if we are at the end of .dynamic 2026 if (DynData.d.a >= dot_dynamic_sec->getEntityCount()) { 2027 // We are at the end. Return the terminator. 2028 DynData.d.a = std::numeric_limits<uint32_t>::max(); 2029 } 2030 2031 Result = DynRef(DynData, this); 2032 return object_error::success; 2033} 2034 2035template<support::endianness target_endianness, bool is64Bits> 2036StringRef 2037ELFObjectFile<target_endianness, is64Bits>::getLoadName() const { 2038 if (!dt_soname) { 2039 // Find the DT_SONAME entry 2040 dyn_iterator it = begin_dynamic_table(); 2041 dyn_iterator ie = end_dynamic_table(); 2042 error_code ec; 2043 while (it != ie) { 2044 if (it->getTag() == ELF::DT_SONAME) 2045 break; 2046 it.increment(ec); 2047 if (ec) 2048 report_fatal_error("dynamic table iteration failed"); 2049 } 2050 if (it != ie) { 2051 if (dot_dynstr_sec == NULL) 2052 report_fatal_error("Dynamic string table is missing"); 2053 dt_soname = getString(dot_dynstr_sec, it->getVal()); 2054 } else { 2055 dt_soname = ""; 2056 } 2057 } 2058 return dt_soname; 2059} 2060 2061template<support::endianness target_endianness, bool is64Bits> 2062library_iterator ELFObjectFile<target_endianness, is64Bits> 2063 ::begin_libraries_needed() const { 2064 // Find the first DT_NEEDED entry 2065 dyn_iterator i = begin_dynamic_table(); 2066 dyn_iterator e = end_dynamic_table(); 2067 error_code ec; 2068 while (i != e) { 2069 if (i->getTag() == ELF::DT_NEEDED) 2070 break; 2071 i.increment(ec); 2072 if (ec) 2073 report_fatal_error("dynamic table iteration failed"); 2074 } 2075 // Use the same DataRefImpl format as DynRef. 2076 return library_iterator(LibraryRef(i->getRawDataRefImpl(), this)); 2077} 2078 2079template<support::endianness target_endianness, bool is64Bits> 2080error_code ELFObjectFile<target_endianness, is64Bits> 2081 ::getLibraryNext(DataRefImpl Data, 2082 LibraryRef &Result) const { 2083 // Use the same DataRefImpl format as DynRef. 2084 dyn_iterator i = dyn_iterator(DynRef(Data, this)); 2085 dyn_iterator e = end_dynamic_table(); 2086 2087 // Skip the current dynamic table entry. 2088 error_code ec; 2089 if (i != e) { 2090 i.increment(ec); 2091 // TODO: proper error handling 2092 if (ec) 2093 report_fatal_error("dynamic table iteration failed"); 2094 } 2095 2096 // Find the next DT_NEEDED entry. 2097 while (i != e) { 2098 if (i->getTag() == ELF::DT_NEEDED) 2099 break; 2100 i.increment(ec); 2101 if (ec) 2102 report_fatal_error("dynamic table iteration failed"); 2103 } 2104 Result = LibraryRef(i->getRawDataRefImpl(), this); 2105 return object_error::success; 2106} 2107 2108template<support::endianness target_endianness, bool is64Bits> 2109error_code ELFObjectFile<target_endianness, is64Bits> 2110 ::getLibraryPath(DataRefImpl Data, StringRef &Res) const { 2111 dyn_iterator i = dyn_iterator(DynRef(Data, this)); 2112 if (i == end_dynamic_table()) 2113 report_fatal_error("getLibraryPath() called on iterator end"); 2114 2115 if (i->getTag() != ELF::DT_NEEDED) 2116 report_fatal_error("Invalid library_iterator"); 2117 2118 // This uses .dynstr to lookup the name of the DT_NEEDED entry. 2119 // THis works as long as DT_STRTAB == .dynstr. This is true most of 2120 // the time, but the specification allows exceptions. 2121 // TODO: This should really use DT_STRTAB instead. Doing this requires 2122 // reading the program headers. 2123 if (dot_dynstr_sec == NULL) 2124 report_fatal_error("Dynamic string table is missing"); 2125 Res = getString(dot_dynstr_sec, i->getVal()); 2126 return object_error::success; 2127} 2128 2129template<support::endianness target_endianness, bool is64Bits> 2130library_iterator ELFObjectFile<target_endianness, is64Bits> 2131 ::end_libraries_needed() const { 2132 dyn_iterator e = end_dynamic_table(); 2133 // Use the same DataRefImpl format as DynRef. 2134 return library_iterator(LibraryRef(e->getRawDataRefImpl(), this)); 2135} 2136 2137template<support::endianness target_endianness, bool is64Bits> 2138uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const { 2139 return is64Bits ? 8 : 4; 2140} 2141 2142template<support::endianness target_endianness, bool is64Bits> 2143StringRef ELFObjectFile<target_endianness, is64Bits> 2144 ::getFileFormatName() const { 2145 switch(Header->e_ident[ELF::EI_CLASS]) { 2146 case ELF::ELFCLASS32: 2147 switch(Header->e_machine) { 2148 case ELF::EM_386: 2149 return "ELF32-i386"; 2150 case ELF::EM_X86_64: 2151 return "ELF32-x86-64"; 2152 case ELF::EM_ARM: 2153 return "ELF32-arm"; 2154 case ELF::EM_HEXAGON: 2155 return "ELF32-hexagon"; 2156 default: 2157 return "ELF32-unknown"; 2158 } 2159 case ELF::ELFCLASS64: 2160 switch(Header->e_machine) { 2161 case ELF::EM_386: 2162 return "ELF64-i386"; 2163 case ELF::EM_X86_64: 2164 return "ELF64-x86-64"; 2165 default: 2166 return "ELF64-unknown"; 2167 } 2168 default: 2169 // FIXME: Proper error handling. 2170 report_fatal_error("Invalid ELFCLASS!"); 2171 } 2172} 2173 2174template<support::endianness target_endianness, bool is64Bits> 2175unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const { 2176 switch(Header->e_machine) { 2177 case ELF::EM_386: 2178 return Triple::x86; 2179 case ELF::EM_X86_64: 2180 return Triple::x86_64; 2181 case ELF::EM_ARM: 2182 return Triple::arm; 2183 case ELF::EM_HEXAGON: 2184 return Triple::hexagon; 2185 case ELF::EM_MIPS: 2186 return (target_endianness == support::little) ? 2187 Triple::mipsel : Triple::mips; 2188 default: 2189 return Triple::UnknownArch; 2190 } 2191} 2192 2193template<support::endianness target_endianness, bool is64Bits> 2194uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const { 2195 assert(Header && "Header not initialized!"); 2196 if (Header->e_shnum == ELF::SHN_UNDEF) { 2197 assert(SectionHeaderTable && "SectionHeaderTable not initialized!"); 2198 return SectionHeaderTable->sh_size; 2199 } 2200 return Header->e_shnum; 2201} 2202 2203template<support::endianness target_endianness, bool is64Bits> 2204uint64_t 2205ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const { 2206 if (Header->e_shnum == ELF::SHN_UNDEF) { 2207 if (Header->e_shstrndx == ELF::SHN_HIRESERVE) 2208 return SectionHeaderTable->sh_link; 2209 if (Header->e_shstrndx >= getNumSections()) 2210 return 0; 2211 } 2212 return Header->e_shstrndx; 2213} 2214 2215 2216template<support::endianness target_endianness, bool is64Bits> 2217template<typename T> 2218inline const T * 2219ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section, 2220 uint32_t Entry) const { 2221 return getEntry<T>(getSection(Section), Entry); 2222} 2223 2224template<support::endianness target_endianness, bool is64Bits> 2225template<typename T> 2226inline const T * 2227ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section, 2228 uint32_t Entry) const { 2229 return reinterpret_cast<const T *>( 2230 base() 2231 + Section->sh_offset 2232 + (Entry * Section->sh_entsize)); 2233} 2234 2235template<support::endianness target_endianness, bool is64Bits> 2236const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym * 2237ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const { 2238 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a); 2239} 2240 2241template<support::endianness target_endianness, bool is64Bits> 2242const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Dyn * 2243ELFObjectFile<target_endianness, is64Bits>::getDyn(DataRefImpl DynData) const { 2244 return getEntry<Elf_Dyn>(dot_dynamic_sec, DynData.d.a); 2245} 2246 2247template<support::endianness target_endianness, bool is64Bits> 2248const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel * 2249ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const { 2250 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c); 2251} 2252 2253template<support::endianness target_endianness, bool is64Bits> 2254const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela * 2255ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const { 2256 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c); 2257} 2258 2259template<support::endianness target_endianness, bool is64Bits> 2260const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * 2261ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const { 2262 const Elf_Shdr *sec = getSection(Symb.d.b); 2263 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM) 2264 // FIXME: Proper error handling. 2265 report_fatal_error("Invalid symbol table section!"); 2266 return sec; 2267} 2268 2269template<support::endianness target_endianness, bool is64Bits> 2270const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * 2271ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const { 2272 if (index == 0) 2273 return 0; 2274 if (!SectionHeaderTable || index >= getNumSections()) 2275 // FIXME: Proper error handling. 2276 report_fatal_error("Invalid section index!"); 2277 2278 return reinterpret_cast<const Elf_Shdr *>( 2279 reinterpret_cast<const char *>(SectionHeaderTable) 2280 + (index * Header->e_shentsize)); 2281} 2282 2283template<support::endianness target_endianness, bool is64Bits> 2284const char *ELFObjectFile<target_endianness, is64Bits> 2285 ::getString(uint32_t section, 2286 ELF::Elf32_Word offset) const { 2287 return getString(getSection(section), offset); 2288} 2289 2290template<support::endianness target_endianness, bool is64Bits> 2291const char *ELFObjectFile<target_endianness, is64Bits> 2292 ::getString(const Elf_Shdr *section, 2293 ELF::Elf32_Word offset) const { 2294 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!"); 2295 if (offset >= section->sh_size) 2296 // FIXME: Proper error handling. 2297 report_fatal_error("Symbol name offset outside of string table!"); 2298 return (const char *)base() + section->sh_offset + offset; 2299} 2300 2301template<support::endianness target_endianness, bool is64Bits> 2302error_code ELFObjectFile<target_endianness, is64Bits> 2303 ::getSymbolName(const Elf_Shdr *section, 2304 const Elf_Sym *symb, 2305 StringRef &Result) const { 2306 if (symb->st_name == 0) { 2307 const Elf_Shdr *section = getSection(symb); 2308 if (!section) 2309 Result = ""; 2310 else 2311 Result = getString(dot_shstrtab_sec, section->sh_name); 2312 return object_error::success; 2313 } 2314 2315 if (section == SymbolTableSections[0]) { 2316 // Symbol is in .dynsym, use .dynstr string table 2317 Result = getString(dot_dynstr_sec, symb->st_name); 2318 } else { 2319 // Use the default symbol table name section. 2320 Result = getString(dot_strtab_sec, symb->st_name); 2321 } 2322 return object_error::success; 2323} 2324 2325template<support::endianness target_endianness, bool is64Bits> 2326error_code ELFObjectFile<target_endianness, is64Bits> 2327 ::getSectionName(const Elf_Shdr *section, 2328 StringRef &Result) const { 2329 Result = StringRef(getString(dot_shstrtab_sec, section->sh_name)); 2330 return object_error::success; 2331} 2332 2333template<support::endianness target_endianness, bool is64Bits> 2334error_code ELFObjectFile<target_endianness, is64Bits> 2335 ::getSymbolVersion(const Elf_Shdr *section, 2336 const Elf_Sym *symb, 2337 StringRef &Version, 2338 bool &IsDefault) const { 2339 // Handle non-dynamic symbols. 2340 if (section != SymbolTableSections[0]) { 2341 // Non-dynamic symbols can have versions in their names 2342 // A name of the form 'foo@V1' indicates version 'V1', non-default. 2343 // A name of the form 'foo@@V2' indicates version 'V2', default version. 2344 StringRef Name; 2345 error_code ec = getSymbolName(section, symb, Name); 2346 if (ec != object_error::success) 2347 return ec; 2348 size_t atpos = Name.find('@'); 2349 if (atpos == StringRef::npos) { 2350 Version = ""; 2351 IsDefault = false; 2352 return object_error::success; 2353 } 2354 ++atpos; 2355 if (atpos < Name.size() && Name[atpos] == '@') { 2356 IsDefault = true; 2357 ++atpos; 2358 } else { 2359 IsDefault = false; 2360 } 2361 Version = Name.substr(atpos); 2362 return object_error::success; 2363 } 2364 2365 // This is a dynamic symbol. Look in the GNU symbol version table. 2366 if (dot_gnu_version_sec == NULL) { 2367 // No version table. 2368 Version = ""; 2369 IsDefault = false; 2370 return object_error::success; 2371 } 2372 2373 // Determine the position in the symbol table of this entry. 2374 const char *sec_start = (const char*)base() + section->sh_offset; 2375 size_t entry_index = ((const char*)symb - sec_start)/section->sh_entsize; 2376 2377 // Get the corresponding version index entry 2378 const Elf_Versym *vs = getEntry<Elf_Versym>(dot_gnu_version_sec, entry_index); 2379 size_t version_index = vs->vs_index & ELF::VERSYM_VERSION; 2380 2381 // Special markers for unversioned symbols. 2382 if (version_index == ELF::VER_NDX_LOCAL || 2383 version_index == ELF::VER_NDX_GLOBAL) { 2384 Version = ""; 2385 IsDefault = false; 2386 return object_error::success; 2387 } 2388 2389 // Lookup this symbol in the version table 2390 LoadVersionMap(); 2391 if (version_index >= VersionMap.size() || VersionMap[version_index].isNull()) 2392 report_fatal_error("Symbol has version index without corresponding " 2393 "define or reference entry"); 2394 const VersionMapEntry &entry = VersionMap[version_index]; 2395 2396 // Get the version name string 2397 size_t name_offset; 2398 if (entry.isVerdef()) { 2399 // The first Verdaux entry holds the name. 2400 name_offset = entry.getVerdef()->getAux()->vda_name; 2401 } else { 2402 name_offset = entry.getVernaux()->vna_name; 2403 } 2404 Version = getString(dot_dynstr_sec, name_offset); 2405 2406 // Set IsDefault 2407 if (entry.isVerdef()) { 2408 IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN); 2409 } else { 2410 IsDefault = false; 2411 } 2412 2413 return object_error::success; 2414} 2415 2416template<support::endianness target_endianness, bool is64Bits> 2417inline DynRefImpl<target_endianness, is64Bits> 2418 ::DynRefImpl(DataRefImpl DynP, const OwningType *Owner) 2419 : DynPimpl(DynP) 2420 , OwningObject(Owner) {} 2421 2422template<support::endianness target_endianness, bool is64Bits> 2423inline bool DynRefImpl<target_endianness, is64Bits> 2424 ::operator==(const DynRefImpl &Other) const { 2425 return DynPimpl == Other.DynPimpl; 2426} 2427 2428template<support::endianness target_endianness, bool is64Bits> 2429inline bool DynRefImpl<target_endianness, is64Bits> 2430 ::operator <(const DynRefImpl &Other) const { 2431 return DynPimpl < Other.DynPimpl; 2432} 2433 2434template<support::endianness target_endianness, bool is64Bits> 2435inline error_code DynRefImpl<target_endianness, is64Bits> 2436 ::getNext(DynRefImpl &Result) const { 2437 return OwningObject->getDynNext(DynPimpl, Result); 2438} 2439 2440template<support::endianness target_endianness, bool is64Bits> 2441inline int64_t DynRefImpl<target_endianness, is64Bits> 2442 ::getTag() const { 2443 return OwningObject->getDyn(DynPimpl)->d_tag; 2444} 2445 2446template<support::endianness target_endianness, bool is64Bits> 2447inline uint64_t DynRefImpl<target_endianness, is64Bits> 2448 ::getVal() const { 2449 return OwningObject->getDyn(DynPimpl)->d_un.d_val; 2450} 2451 2452template<support::endianness target_endianness, bool is64Bits> 2453inline uint64_t DynRefImpl<target_endianness, is64Bits> 2454 ::getPtr() const { 2455 return OwningObject->getDyn(DynPimpl)->d_un.d_ptr; 2456} 2457 2458template<support::endianness target_endianness, bool is64Bits> 2459inline DataRefImpl DynRefImpl<target_endianness, is64Bits> 2460 ::getRawDataRefImpl() const { 2461 return DynPimpl; 2462} 2463 2464/// This is a generic interface for retrieving GNU symbol version 2465/// information from an ELFObjectFile. 2466static inline error_code GetELFSymbolVersion(const ObjectFile *Obj, 2467 const SymbolRef &Sym, 2468 StringRef &Version, 2469 bool &IsDefault) { 2470 // Little-endian 32-bit 2471 if (const ELFObjectFile<support::little, false> *ELFObj = 2472 dyn_cast<ELFObjectFile<support::little, false> >(Obj)) 2473 return ELFObj->getSymbolVersion(Sym, Version, IsDefault); 2474 2475 // Big-endian 32-bit 2476 if (const ELFObjectFile<support::big, false> *ELFObj = 2477 dyn_cast<ELFObjectFile<support::big, false> >(Obj)) 2478 return ELFObj->getSymbolVersion(Sym, Version, IsDefault); 2479 2480 // Little-endian 64-bit 2481 if (const ELFObjectFile<support::little, true> *ELFObj = 2482 dyn_cast<ELFObjectFile<support::little, true> >(Obj)) 2483 return ELFObj->getSymbolVersion(Sym, Version, IsDefault); 2484 2485 // Big-endian 64-bit 2486 if (const ELFObjectFile<support::big, true> *ELFObj = 2487 dyn_cast<ELFObjectFile<support::big, true> >(Obj)) 2488 return ELFObj->getSymbolVersion(Sym, Version, IsDefault); 2489 2490 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF"); 2491} 2492 2493} 2494} 2495 2496#endif 2497