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