1 //===- ELFObjectFile.cpp - 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 defines the ELFObjectFile class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/SmallVector.h" 15 #include "llvm/ADT/StringSwitch.h" 16 #include "llvm/ADT/Triple.h" 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/Object/ObjectFile.h" 19 #include "llvm/Support/ELF.h" 20 #include "llvm/Support/Endian.h" 21 #include "llvm/Support/ErrorHandling.h" 22 #include "llvm/Support/MemoryBuffer.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include <algorithm> 25 #include <limits> 26 #include <utility> 27 28 using namespace llvm; 29 using namespace object; 30 31 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits. 32 namespace { 33 template<support::endianness target_endianness> 34 struct ELFDataTypeTypedefHelperCommon { 35 typedef support::detail::packed_endian_specific_integral 36 <uint16_t, target_endianness, support::aligned> Elf_Half; 37 typedef support::detail::packed_endian_specific_integral 38 <uint32_t, target_endianness, support::aligned> Elf_Word; 39 typedef support::detail::packed_endian_specific_integral 40 <int32_t, target_endianness, support::aligned> Elf_Sword; 41 typedef support::detail::packed_endian_specific_integral 42 <uint64_t, target_endianness, support::aligned> Elf_Xword; 43 typedef support::detail::packed_endian_specific_integral 44 <int64_t, target_endianness, support::aligned> Elf_Sxword; 45 }; 46 } 47 48 namespace { 49 template<support::endianness target_endianness, bool is64Bits> 50 struct ELFDataTypeTypedefHelper; 51 52 /// ELF 32bit types. 53 template<support::endianness target_endianness> 54 struct ELFDataTypeTypedefHelper<target_endianness, false> 55 : ELFDataTypeTypedefHelperCommon<target_endianness> { 56 typedef support::detail::packed_endian_specific_integral 57 <uint32_t, target_endianness, support::aligned> Elf_Addr; 58 typedef support::detail::packed_endian_specific_integral 59 <uint32_t, target_endianness, support::aligned> Elf_Off; 60 }; 61 62 /// ELF 64bit types. 63 template<support::endianness target_endianness> 64 struct ELFDataTypeTypedefHelper<target_endianness, true> 65 : ELFDataTypeTypedefHelperCommon<target_endianness>{ 66 typedef support::detail::packed_endian_specific_integral 67 <uint64_t, target_endianness, support::aligned> Elf_Addr; 68 typedef support::detail::packed_endian_specific_integral 69 <uint64_t, target_endianness, support::aligned> Elf_Off; 70 }; 71 } 72 73 // I really don't like doing this, but the alternative is copypasta. 74 #define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \ 75 typedef typename \ 76 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Addr Elf_Addr; \ 77 typedef typename \ 78 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Off Elf_Off; \ 79 typedef typename \ 80 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Half Elf_Half; \ 81 typedef typename \ 82 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Word Elf_Word; \ 83 typedef typename \ 84 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sword Elf_Sword; \ 85 typedef typename \ 86 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Xword Elf_Xword; \ 87 typedef typename \ 88 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sxword Elf_Sxword; 89 90 // Section header. 91 namespace { 92 template<support::endianness target_endianness, bool is64Bits> 93 struct Elf_Shdr_Base; 94 95 template<support::endianness target_endianness> 96 struct Elf_Shdr_Base<target_endianness, false> { 97 LLVM_ELF_IMPORT_TYPES(target_endianness, false) 98 Elf_Word sh_name; // Section name (index into string table) 99 Elf_Word sh_type; // Section type (SHT_*) 100 Elf_Word sh_flags; // Section flags (SHF_*) 101 Elf_Addr sh_addr; // Address where section is to be loaded 102 Elf_Off sh_offset; // File offset of section data, in bytes 103 Elf_Word sh_size; // Size of section, in bytes 104 Elf_Word sh_link; // Section type-specific header table index link 105 Elf_Word sh_info; // Section type-specific extra information 106 Elf_Word sh_addralign;// Section address alignment 107 Elf_Word sh_entsize; // Size of records contained within the section 108 }; 109 110 template<support::endianness target_endianness> 111 struct Elf_Shdr_Base<target_endianness, true> { 112 LLVM_ELF_IMPORT_TYPES(target_endianness, true) 113 Elf_Word sh_name; // Section name (index into string table) 114 Elf_Word sh_type; // Section type (SHT_*) 115 Elf_Xword sh_flags; // Section flags (SHF_*) 116 Elf_Addr sh_addr; // Address where section is to be loaded 117 Elf_Off sh_offset; // File offset of section data, in bytes 118 Elf_Xword sh_size; // Size of section, in bytes 119 Elf_Word sh_link; // Section type-specific header table index link 120 Elf_Word sh_info; // Section type-specific extra information 121 Elf_Xword sh_addralign;// Section address alignment 122 Elf_Xword sh_entsize; // Size of records contained within the section 123 }; 124 125 template<support::endianness target_endianness, bool is64Bits> 126 struct Elf_Shdr_Impl : Elf_Shdr_Base<target_endianness, is64Bits> { 127 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_entsize; 128 using Elf_Shdr_Base<target_endianness, is64Bits>::sh_size; 129 130 /// @brief Get the number of entities this section contains if it has any. 131 unsigned getEntityCount() const { 132 if (sh_entsize == 0) 133 return 0; 134 return sh_size / sh_entsize; 135 } 136 }; 137 } 138 139 namespace { 140 template<support::endianness target_endianness, bool is64Bits> 141 struct Elf_Sym_Base; 142 143 template<support::endianness target_endianness> 144 struct Elf_Sym_Base<target_endianness, false> { 145 LLVM_ELF_IMPORT_TYPES(target_endianness, false) 146 Elf_Word st_name; // Symbol name (index into string table) 147 Elf_Addr st_value; // Value or address associated with the symbol 148 Elf_Word st_size; // Size of the symbol 149 unsigned char st_info; // Symbol's type and binding attributes 150 unsigned char st_other; // Must be zero; reserved 151 Elf_Half st_shndx; // Which section (header table index) it's defined in 152 }; 153 154 template<support::endianness target_endianness> 155 struct Elf_Sym_Base<target_endianness, true> { 156 LLVM_ELF_IMPORT_TYPES(target_endianness, true) 157 Elf_Word st_name; // Symbol name (index into string table) 158 unsigned char st_info; // Symbol's type and binding attributes 159 unsigned char st_other; // Must be zero; reserved 160 Elf_Half st_shndx; // Which section (header table index) it's defined in 161 Elf_Addr st_value; // Value or address associated with the symbol 162 Elf_Xword st_size; // Size of the symbol 163 }; 164 165 template<support::endianness target_endianness, bool is64Bits> 166 struct Elf_Sym_Impl : Elf_Sym_Base<target_endianness, is64Bits> { 167 using Elf_Sym_Base<target_endianness, is64Bits>::st_info; 168 169 // These accessors and mutators correspond to the ELF32_ST_BIND, 170 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification: 171 unsigned char getBinding() const { return st_info >> 4; } 172 unsigned char getType() const { return st_info & 0x0f; } 173 void setBinding(unsigned char b) { setBindingAndType(b, getType()); } 174 void setType(unsigned char t) { setBindingAndType(getBinding(), t); } 175 void setBindingAndType(unsigned char b, unsigned char t) { 176 st_info = (b << 4) + (t & 0x0f); 177 } 178 }; 179 } 180 181 namespace { 182 template<support::endianness target_endianness, bool is64Bits, bool isRela> 183 struct Elf_Rel_Base; 184 185 template<support::endianness target_endianness> 186 struct Elf_Rel_Base<target_endianness, false, false> { 187 LLVM_ELF_IMPORT_TYPES(target_endianness, false) 188 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 189 Elf_Word r_info; // Symbol table index and type of relocation to apply 190 }; 191 192 template<support::endianness target_endianness> 193 struct Elf_Rel_Base<target_endianness, true, false> { 194 LLVM_ELF_IMPORT_TYPES(target_endianness, true) 195 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 196 Elf_Xword r_info; // Symbol table index and type of relocation to apply 197 }; 198 199 template<support::endianness target_endianness> 200 struct Elf_Rel_Base<target_endianness, false, true> { 201 LLVM_ELF_IMPORT_TYPES(target_endianness, false) 202 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 203 Elf_Word r_info; // Symbol table index and type of relocation to apply 204 Elf_Sword r_addend; // Compute value for relocatable field by adding this 205 }; 206 207 template<support::endianness target_endianness> 208 struct Elf_Rel_Base<target_endianness, true, true> { 209 LLVM_ELF_IMPORT_TYPES(target_endianness, true) 210 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) 211 Elf_Xword r_info; // Symbol table index and type of relocation to apply 212 Elf_Sxword r_addend; // Compute value for relocatable field by adding this. 213 }; 214 215 template<support::endianness target_endianness, bool is64Bits, bool isRela> 216 struct Elf_Rel_Impl; 217 218 template<support::endianness target_endianness, bool isRela> 219 struct Elf_Rel_Impl<target_endianness, true, isRela> 220 : Elf_Rel_Base<target_endianness, true, isRela> { 221 using Elf_Rel_Base<target_endianness, true, isRela>::r_info; 222 LLVM_ELF_IMPORT_TYPES(target_endianness, true) 223 224 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE, 225 // and ELF64_R_INFO macros defined in the ELF specification: 226 uint64_t getSymbol() const { return (r_info >> 32); } 227 unsigned char getType() const { 228 return (unsigned char) (r_info & 0xffffffffL); 229 } 230 void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); } 231 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); } 232 void setSymbolAndType(uint64_t s, unsigned char t) { 233 r_info = (s << 32) + (t&0xffffffffL); 234 } 235 }; 236 237 template<support::endianness target_endianness, bool isRela> 238 struct Elf_Rel_Impl<target_endianness, false, isRela> 239 : Elf_Rel_Base<target_endianness, false, isRela> { 240 using Elf_Rel_Base<target_endianness, false, isRela>::r_info; 241 LLVM_ELF_IMPORT_TYPES(target_endianness, false) 242 243 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, 244 // and ELF32_R_INFO macros defined in the ELF specification: 245 uint32_t getSymbol() const { return (r_info >> 8); } 246 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); } 247 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); } 248 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); } 249 void setSymbolAndType(uint32_t s, unsigned char t) { 250 r_info = (s << 8) + t; 251 } 252 }; 253 254 } 255 256 namespace { 257 template<support::endianness target_endianness, bool is64Bits> 258 class ELFObjectFile : public ObjectFile { 259 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) 260 261 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr; 262 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym; 263 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel; 264 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela; 265 266 struct Elf_Ehdr { 267 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes 268 Elf_Half e_type; // Type of file (see ET_*) 269 Elf_Half e_machine; // Required architecture for this file (see EM_*) 270 Elf_Word e_version; // Must be equal to 1 271 Elf_Addr e_entry; // Address to jump to in order to start program 272 Elf_Off e_phoff; // Program header table's file offset, in bytes 273 Elf_Off e_shoff; // Section header table's file offset, in bytes 274 Elf_Word e_flags; // Processor-specific flags 275 Elf_Half e_ehsize; // Size of ELF header, in bytes 276 Elf_Half e_phentsize;// Size of an entry in the program header table 277 Elf_Half e_phnum; // Number of entries in the program header table 278 Elf_Half e_shentsize;// Size of an entry in the section header table 279 Elf_Half e_shnum; // Number of entries in the section header table 280 Elf_Half e_shstrndx; // Section header table index of section name 281 // string table 282 bool checkMagic() const { 283 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0; 284 } 285 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; } 286 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; } 287 }; 288 289 typedef SmallVector<const Elf_Shdr*, 1> Sections_t; 290 typedef DenseMap<unsigned, unsigned> IndexMap_t; 291 typedef DenseMap<const Elf_Shdr*, SmallVector<uint32_t, 1> > RelocMap_t; 292 293 const Elf_Ehdr *Header; 294 const Elf_Shdr *SectionHeaderTable; 295 const Elf_Shdr *dot_shstrtab_sec; // Section header string table. 296 const Elf_Shdr *dot_strtab_sec; // Symbol header string table. 297 Sections_t SymbolTableSections; 298 IndexMap_t SymbolTableSectionsIndexMap; 299 DenseMap<const Elf_Sym*, ELF::Elf64_Word> ExtendedSymbolTable; 300 301 /// @brief Map sections to an array of relocation sections that reference 302 /// them sorted by section index. 303 RelocMap_t SectionRelocMap; 304 305 /// @brief Get the relocation section that contains \a Rel. 306 const Elf_Shdr *getRelSection(DataRefImpl Rel) const { 307 return getSection(Rel.w.b); 308 } 309 310 void validateSymbol(DataRefImpl Symb) const; 311 bool isRelocationHasAddend(DataRefImpl Rel) const; 312 template<typename T> 313 const T *getEntry(uint16_t Section, uint32_t Entry) const; 314 template<typename T> 315 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const; 316 const Elf_Sym *getSymbol(DataRefImpl Symb) const; 317 const Elf_Shdr *getSection(DataRefImpl index) const; 318 const Elf_Shdr *getSection(uint32_t index) const; 319 const Elf_Rel *getRel(DataRefImpl Rel) const; 320 const Elf_Rela *getRela(DataRefImpl Rela) const; 321 const char *getString(uint32_t section, uint32_t offset) const; 322 const char *getString(const Elf_Shdr *section, uint32_t offset) const; 323 error_code getSymbolName(const Elf_Sym *Symb, StringRef &Res) const; 324 325 protected: 326 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const; 327 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const; 328 virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const; 329 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const; 330 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const; 331 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const; 332 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const; 333 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const; 334 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const; 335 336 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const; 337 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const; 338 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const; 339 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const; 340 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const; 341 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const; 342 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const; 343 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const; 344 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const; 345 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, 346 bool &Result) const; 347 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const; 348 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const; 349 350 virtual error_code getRelocationNext(DataRefImpl Rel, 351 RelocationRef &Res) const; 352 virtual error_code getRelocationAddress(DataRefImpl Rel, 353 uint64_t &Res) const; 354 virtual error_code getRelocationSymbol(DataRefImpl Rel, 355 SymbolRef &Res) const; 356 virtual error_code getRelocationType(DataRefImpl Rel, 357 uint32_t &Res) const; 358 virtual error_code getRelocationTypeName(DataRefImpl Rel, 359 SmallVectorImpl<char> &Result) const; 360 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel, 361 int64_t &Res) const; 362 virtual error_code getRelocationValueString(DataRefImpl Rel, 363 SmallVectorImpl<char> &Result) const; 364 365 public: 366 ELFObjectFile(MemoryBuffer *Object, error_code &ec); 367 virtual symbol_iterator begin_symbols() const; 368 virtual symbol_iterator end_symbols() const; 369 virtual section_iterator begin_sections() const; 370 virtual section_iterator end_sections() const; 371 372 virtual uint8_t getBytesInAddress() const; 373 virtual StringRef getFileFormatName() const; 374 virtual unsigned getArch() const; 375 376 uint64_t getNumSections() const; 377 uint64_t getStringTableIndex() const; 378 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const; 379 const Elf_Shdr *getSection(const Elf_Sym *symb) const; 380 381 static inline bool classof(const Binary *v) { 382 return v->getType() == isELF; 383 } 384 static inline bool classof(const ELFObjectFile *v) { return true; } 385 }; 386 } // end namespace 387 388 template<support::endianness target_endianness, bool is64Bits> 389 void ELFObjectFile<target_endianness, is64Bits> 390 ::validateSymbol(DataRefImpl Symb) const { 391 const Elf_Sym *symb = getSymbol(Symb); 392 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; 393 // FIXME: We really need to do proper error handling in the case of an invalid 394 // input file. Because we don't use exceptions, I think we'll just pass 395 // an error object around. 396 if (!( symb 397 && SymbolTableSection 398 && symb >= (const Elf_Sym*)(base() 399 + SymbolTableSection->sh_offset) 400 && symb < (const Elf_Sym*)(base() 401 + SymbolTableSection->sh_offset 402 + SymbolTableSection->sh_size))) 403 // FIXME: Proper error handling. 404 report_fatal_error("Symb must point to a valid symbol!"); 405 } 406 407 template<support::endianness target_endianness, bool is64Bits> 408 error_code ELFObjectFile<target_endianness, is64Bits> 409 ::getSymbolNext(DataRefImpl Symb, 410 SymbolRef &Result) const { 411 validateSymbol(Symb); 412 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; 413 414 ++Symb.d.a; 415 // Check to see if we are at the end of this symbol table. 416 if (Symb.d.a >= SymbolTableSection->getEntityCount()) { 417 // We are at the end. If there are other symbol tables, jump to them. 418 ++Symb.d.b; 419 Symb.d.a = 1; // The 0th symbol in ELF is fake. 420 // Otherwise return the terminator. 421 if (Symb.d.b >= SymbolTableSections.size()) { 422 Symb.d.a = std::numeric_limits<uint32_t>::max(); 423 Symb.d.b = std::numeric_limits<uint32_t>::max(); 424 } 425 } 426 427 Result = SymbolRef(Symb, this); 428 return object_error::success; 429 } 430 431 template<support::endianness target_endianness, bool is64Bits> 432 error_code ELFObjectFile<target_endianness, is64Bits> 433 ::getSymbolName(DataRefImpl Symb, 434 StringRef &Result) const { 435 validateSymbol(Symb); 436 const Elf_Sym *symb = getSymbol(Symb); 437 return getSymbolName(symb, Result); 438 } 439 440 template<support::endianness target_endianness, bool is64Bits> 441 ELF::Elf64_Word ELFObjectFile<target_endianness, is64Bits> 442 ::getSymbolTableIndex(const Elf_Sym *symb) const { 443 if (symb->st_shndx == ELF::SHN_XINDEX) 444 return ExtendedSymbolTable.lookup(symb); 445 return symb->st_shndx; 446 } 447 448 template<support::endianness target_endianness, bool is64Bits> 449 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * 450 ELFObjectFile<target_endianness, is64Bits> 451 ::getSection(const Elf_Sym *symb) const { 452 if (symb->st_shndx == ELF::SHN_XINDEX) 453 return getSection(ExtendedSymbolTable.lookup(symb)); 454 if (symb->st_shndx >= ELF::SHN_LORESERVE) 455 return 0; 456 return getSection(symb->st_shndx); 457 } 458 459 template<support::endianness target_endianness, bool is64Bits> 460 error_code ELFObjectFile<target_endianness, is64Bits> 461 ::getSymbolOffset(DataRefImpl Symb, 462 uint64_t &Result) const { 463 validateSymbol(Symb); 464 const Elf_Sym *symb = getSymbol(Symb); 465 const Elf_Shdr *Section; 466 switch (getSymbolTableIndex(symb)) { 467 case ELF::SHN_COMMON: 468 // Undefined symbols have no address yet. 469 case ELF::SHN_UNDEF: 470 Result = UnknownAddressOrSize; 471 return object_error::success; 472 case ELF::SHN_ABS: 473 Result = symb->st_value; 474 return object_error::success; 475 default: Section = getSection(symb); 476 } 477 478 switch (symb->getType()) { 479 case ELF::STT_SECTION: 480 Result = Section ? Section->sh_addr : UnknownAddressOrSize; 481 return object_error::success; 482 case ELF::STT_FUNC: 483 case ELF::STT_OBJECT: 484 case ELF::STT_NOTYPE: 485 Result = symb->st_value; 486 return object_error::success; 487 default: 488 Result = UnknownAddressOrSize; 489 return object_error::success; 490 } 491 } 492 493 template<support::endianness target_endianness, bool is64Bits> 494 error_code ELFObjectFile<target_endianness, is64Bits> 495 ::getSymbolAddress(DataRefImpl Symb, 496 uint64_t &Result) const { 497 validateSymbol(Symb); 498 const Elf_Sym *symb = getSymbol(Symb); 499 const Elf_Shdr *Section; 500 switch (getSymbolTableIndex(symb)) { 501 case ELF::SHN_COMMON: // Fall through. 502 // Undefined symbols have no address yet. 503 case ELF::SHN_UNDEF: 504 Result = UnknownAddressOrSize; 505 return object_error::success; 506 case ELF::SHN_ABS: 507 Result = reinterpret_cast<uintptr_t>(base()+symb->st_value); 508 return object_error::success; 509 default: Section = getSection(symb); 510 } 511 const uint8_t* addr = base(); 512 if (Section) 513 addr += Section->sh_offset; 514 switch (symb->getType()) { 515 case ELF::STT_SECTION: 516 Result = reinterpret_cast<uintptr_t>(addr); 517 return object_error::success; 518 case ELF::STT_FUNC: // Fall through. 519 case ELF::STT_OBJECT: // Fall through. 520 case ELF::STT_NOTYPE: 521 addr += symb->st_value; 522 Result = reinterpret_cast<uintptr_t>(addr); 523 return object_error::success; 524 default: 525 Result = UnknownAddressOrSize; 526 return object_error::success; 527 } 528 } 529 530 template<support::endianness target_endianness, bool is64Bits> 531 error_code ELFObjectFile<target_endianness, is64Bits> 532 ::getSymbolSize(DataRefImpl Symb, 533 uint64_t &Result) const { 534 validateSymbol(Symb); 535 const Elf_Sym *symb = getSymbol(Symb); 536 if (symb->st_size == 0) 537 Result = UnknownAddressOrSize; 538 Result = symb->st_size; 539 return object_error::success; 540 } 541 542 template<support::endianness target_endianness, bool is64Bits> 543 error_code ELFObjectFile<target_endianness, is64Bits> 544 ::getSymbolNMTypeChar(DataRefImpl Symb, 545 char &Result) const { 546 validateSymbol(Symb); 547 const Elf_Sym *symb = getSymbol(Symb); 548 const Elf_Shdr *Section = getSection(symb); 549 550 char ret = '?'; 551 552 if (Section) { 553 switch (Section->sh_type) { 554 case ELF::SHT_PROGBITS: 555 case ELF::SHT_DYNAMIC: 556 switch (Section->sh_flags) { 557 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR): 558 ret = 't'; break; 559 case (ELF::SHF_ALLOC | ELF::SHF_WRITE): 560 ret = 'd'; break; 561 case ELF::SHF_ALLOC: 562 case (ELF::SHF_ALLOC | ELF::SHF_MERGE): 563 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS): 564 ret = 'r'; break; 565 } 566 break; 567 case ELF::SHT_NOBITS: ret = 'b'; 568 } 569 } 570 571 switch (getSymbolTableIndex(symb)) { 572 case ELF::SHN_UNDEF: 573 if (ret == '?') 574 ret = 'U'; 575 break; 576 case ELF::SHN_ABS: ret = 'a'; break; 577 case ELF::SHN_COMMON: ret = 'c'; break; 578 } 579 580 switch (symb->getBinding()) { 581 case ELF::STB_GLOBAL: ret = ::toupper(ret); break; 582 case ELF::STB_WEAK: 583 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) 584 ret = 'w'; 585 else 586 if (symb->getType() == ELF::STT_OBJECT) 587 ret = 'V'; 588 else 589 ret = 'W'; 590 } 591 592 if (ret == '?' && symb->getType() == ELF::STT_SECTION) { 593 StringRef name; 594 if (error_code ec = getSymbolName(Symb, name)) 595 return ec; 596 Result = StringSwitch<char>(name) 597 .StartsWith(".debug", 'N') 598 .StartsWith(".note", 'n') 599 .Default('?'); 600 return object_error::success; 601 } 602 603 Result = ret; 604 return object_error::success; 605 } 606 607 template<support::endianness target_endianness, bool is64Bits> 608 error_code ELFObjectFile<target_endianness, is64Bits> 609 ::getSymbolType(DataRefImpl Symb, 610 SymbolRef::Type &Result) const { 611 validateSymbol(Symb); 612 const Elf_Sym *symb = getSymbol(Symb); 613 614 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) { 615 Result = SymbolRef::ST_External; 616 return object_error::success; 617 } 618 619 switch (symb->getType()) { 620 case ELF::STT_FUNC: 621 Result = SymbolRef::ST_Function; 622 break; 623 case ELF::STT_OBJECT: 624 Result = SymbolRef::ST_Data; 625 break; 626 default: 627 Result = SymbolRef::ST_Other; 628 break; 629 } 630 return object_error::success; 631 } 632 633 template<support::endianness target_endianness, bool is64Bits> 634 error_code ELFObjectFile<target_endianness, is64Bits> 635 ::isSymbolGlobal(DataRefImpl Symb, 636 bool &Result) const { 637 validateSymbol(Symb); 638 const Elf_Sym *symb = getSymbol(Symb); 639 640 Result = symb->getBinding() == ELF::STB_GLOBAL; 641 return object_error::success; 642 } 643 644 template<support::endianness target_endianness, bool is64Bits> 645 error_code ELFObjectFile<target_endianness, is64Bits> 646 ::isSymbolInternal(DataRefImpl Symb, 647 bool &Result) const { 648 validateSymbol(Symb); 649 const Elf_Sym *symb = getSymbol(Symb); 650 651 if ( symb->getType() == ELF::STT_FILE 652 || symb->getType() == ELF::STT_SECTION) 653 Result = true; 654 Result = false; 655 return object_error::success; 656 } 657 658 template<support::endianness target_endianness, bool is64Bits> 659 error_code ELFObjectFile<target_endianness, is64Bits> 660 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const { 661 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p); 662 sec += Header->e_shentsize; 663 Sec.p = reinterpret_cast<intptr_t>(sec); 664 Result = SectionRef(Sec, this); 665 return object_error::success; 666 } 667 668 template<support::endianness target_endianness, bool is64Bits> 669 error_code ELFObjectFile<target_endianness, is64Bits> 670 ::getSectionName(DataRefImpl Sec, 671 StringRef &Result) const { 672 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 673 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name)); 674 return object_error::success; 675 } 676 677 template<support::endianness target_endianness, bool is64Bits> 678 error_code ELFObjectFile<target_endianness, is64Bits> 679 ::getSectionAddress(DataRefImpl Sec, 680 uint64_t &Result) const { 681 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 682 Result = sec->sh_addr; 683 return object_error::success; 684 } 685 686 template<support::endianness target_endianness, bool is64Bits> 687 error_code ELFObjectFile<target_endianness, is64Bits> 688 ::getSectionSize(DataRefImpl Sec, 689 uint64_t &Result) const { 690 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 691 Result = sec->sh_size; 692 return object_error::success; 693 } 694 695 template<support::endianness target_endianness, bool is64Bits> 696 error_code ELFObjectFile<target_endianness, is64Bits> 697 ::getSectionContents(DataRefImpl Sec, 698 StringRef &Result) const { 699 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 700 const char *start = (const char*)base() + sec->sh_offset; 701 Result = StringRef(start, sec->sh_size); 702 return object_error::success; 703 } 704 705 template<support::endianness target_endianness, bool is64Bits> 706 error_code ELFObjectFile<target_endianness, is64Bits> 707 ::getSectionAlignment(DataRefImpl Sec, 708 uint64_t &Result) const { 709 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 710 Result = sec->sh_addralign; 711 return object_error::success; 712 } 713 714 template<support::endianness target_endianness, bool is64Bits> 715 error_code ELFObjectFile<target_endianness, is64Bits> 716 ::isSectionText(DataRefImpl Sec, 717 bool &Result) const { 718 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 719 if (sec->sh_flags & ELF::SHF_EXECINSTR) 720 Result = true; 721 else 722 Result = false; 723 return object_error::success; 724 } 725 726 template<support::endianness target_endianness, bool is64Bits> 727 error_code ELFObjectFile<target_endianness, is64Bits> 728 ::isSectionData(DataRefImpl Sec, 729 bool &Result) const { 730 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 731 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) 732 && sec->sh_type == ELF::SHT_PROGBITS) 733 Result = true; 734 else 735 Result = false; 736 return object_error::success; 737 } 738 739 template<support::endianness target_endianness, bool is64Bits> 740 error_code ELFObjectFile<target_endianness, is64Bits> 741 ::isSectionBSS(DataRefImpl Sec, 742 bool &Result) const { 743 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 744 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) 745 && sec->sh_type == ELF::SHT_NOBITS) 746 Result = true; 747 else 748 Result = false; 749 return object_error::success; 750 } 751 752 template<support::endianness target_endianness, bool is64Bits> 753 error_code ELFObjectFile<target_endianness, is64Bits> 754 ::sectionContainsSymbol(DataRefImpl Sec, 755 DataRefImpl Symb, 756 bool &Result) const { 757 // FIXME: Unimplemented. 758 Result = false; 759 return object_error::success; 760 } 761 762 template<support::endianness target_endianness, bool is64Bits> 763 relocation_iterator ELFObjectFile<target_endianness, is64Bits> 764 ::getSectionRelBegin(DataRefImpl Sec) const { 765 DataRefImpl RelData; 766 memset(&RelData, 0, sizeof(RelData)); 767 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 768 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec); 769 if (sec != 0 && ittr != SectionRelocMap.end()) { 770 RelData.w.a = getSection(ittr->second[0])->sh_info; 771 RelData.w.b = ittr->second[0]; 772 RelData.w.c = 0; 773 } 774 return relocation_iterator(RelocationRef(RelData, this)); 775 } 776 777 template<support::endianness target_endianness, bool is64Bits> 778 relocation_iterator ELFObjectFile<target_endianness, is64Bits> 779 ::getSectionRelEnd(DataRefImpl Sec) const { 780 DataRefImpl RelData; 781 memset(&RelData, 0, sizeof(RelData)); 782 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 783 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec); 784 if (sec != 0 && ittr != SectionRelocMap.end()) { 785 // Get the index of the last relocation section for this section. 786 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1]; 787 const Elf_Shdr *relocsec = getSection(relocsecindex); 788 RelData.w.a = relocsec->sh_info; 789 RelData.w.b = relocsecindex; 790 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize; 791 } 792 return relocation_iterator(RelocationRef(RelData, this)); 793 } 794 795 // Relocations 796 template<support::endianness target_endianness, bool is64Bits> 797 error_code ELFObjectFile<target_endianness, is64Bits> 798 ::getRelocationNext(DataRefImpl Rel, 799 RelocationRef &Result) const { 800 ++Rel.w.c; 801 const Elf_Shdr *relocsec = getSection(Rel.w.b); 802 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) { 803 // We have reached the end of the relocations for this section. See if there 804 // is another relocation section. 805 typename RelocMap_t::mapped_type relocseclist = 806 SectionRelocMap.lookup(getSection(Rel.w.a)); 807 808 // Do a binary search for the current reloc section index (which must be 809 // present). Then get the next one. 810 typename RelocMap_t::mapped_type::const_iterator loc = 811 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b); 812 ++loc; 813 814 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel 815 // to the end iterator. 816 if (loc != relocseclist.end()) { 817 Rel.w.b = *loc; 818 Rel.w.a = 0; 819 } 820 } 821 Result = RelocationRef(Rel, this); 822 return object_error::success; 823 } 824 825 template<support::endianness target_endianness, bool is64Bits> 826 error_code ELFObjectFile<target_endianness, is64Bits> 827 ::getRelocationSymbol(DataRefImpl Rel, 828 SymbolRef &Result) const { 829 uint32_t symbolIdx; 830 const Elf_Shdr *sec = getSection(Rel.w.b); 831 switch (sec->sh_type) { 832 default : 833 report_fatal_error("Invalid section type in Rel!"); 834 case ELF::SHT_REL : { 835 symbolIdx = getRel(Rel)->getSymbol(); 836 break; 837 } 838 case ELF::SHT_RELA : { 839 symbolIdx = getRela(Rel)->getSymbol(); 840 break; 841 } 842 } 843 DataRefImpl SymbolData; 844 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link); 845 if (it == SymbolTableSectionsIndexMap.end()) 846 report_fatal_error("Relocation symbol table not found!"); 847 SymbolData.d.a = symbolIdx; 848 SymbolData.d.b = it->second; 849 Result = SymbolRef(SymbolData, this); 850 return object_error::success; 851 } 852 853 template<support::endianness target_endianness, bool is64Bits> 854 error_code ELFObjectFile<target_endianness, is64Bits> 855 ::getRelocationAddress(DataRefImpl Rel, 856 uint64_t &Result) const { 857 uint64_t offset; 858 const Elf_Shdr *sec = getSection(Rel.w.b); 859 switch (sec->sh_type) { 860 default : 861 report_fatal_error("Invalid section type in Rel!"); 862 case ELF::SHT_REL : { 863 offset = getRel(Rel)->r_offset; 864 break; 865 } 866 case ELF::SHT_RELA : { 867 offset = getRela(Rel)->r_offset; 868 break; 869 } 870 } 871 872 Result = offset; 873 return object_error::success; 874 } 875 876 template<support::endianness target_endianness, bool is64Bits> 877 error_code ELFObjectFile<target_endianness, is64Bits> 878 ::getRelocationType(DataRefImpl Rel, 879 uint32_t &Result) const { 880 const Elf_Shdr *sec = getSection(Rel.w.b); 881 switch (sec->sh_type) { 882 default : 883 report_fatal_error("Invalid section type in Rel!"); 884 case ELF::SHT_REL : { 885 Result = getRel(Rel)->getType(); 886 break; 887 } 888 case ELF::SHT_RELA : { 889 Result = getRela(Rel)->getType(); 890 break; 891 } 892 } 893 return object_error::success; 894 } 895 896 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \ 897 case ELF::enum: res = #enum; break; 898 899 template<support::endianness target_endianness, bool is64Bits> 900 error_code ELFObjectFile<target_endianness, is64Bits> 901 ::getRelocationTypeName(DataRefImpl Rel, 902 SmallVectorImpl<char> &Result) const { 903 const Elf_Shdr *sec = getSection(Rel.w.b); 904 uint8_t type; 905 StringRef res; 906 switch (sec->sh_type) { 907 default : 908 return object_error::parse_failed; 909 case ELF::SHT_REL : { 910 type = getRel(Rel)->getType(); 911 break; 912 } 913 case ELF::SHT_RELA : { 914 type = getRela(Rel)->getType(); 915 break; 916 } 917 } 918 switch (Header->e_machine) { 919 case ELF::EM_X86_64: 920 switch (type) { 921 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE); 922 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64); 923 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32); 924 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32); 925 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32); 926 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY); 927 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT); 928 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT); 929 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE); 930 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL); 931 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32); 932 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S); 933 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16); 934 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16); 935 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8); 936 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8); 937 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64); 938 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64); 939 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64); 940 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD); 941 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD); 942 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32); 943 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF); 944 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32); 945 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64); 946 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64); 947 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32); 948 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32); 949 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64); 950 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC); 951 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL); 952 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC); 953 default: 954 res = "Unknown"; 955 } 956 break; 957 case ELF::EM_386: 958 switch (type) { 959 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE); 960 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32); 961 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32); 962 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32); 963 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32); 964 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY); 965 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT); 966 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT); 967 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE); 968 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF); 969 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC); 970 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT); 971 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF); 972 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE); 973 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE); 974 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE); 975 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD); 976 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM); 977 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16); 978 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16); 979 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8); 980 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8); 981 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32); 982 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH); 983 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL); 984 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP); 985 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32); 986 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH); 987 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL); 988 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP); 989 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32); 990 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32); 991 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32); 992 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32); 993 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32); 994 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32); 995 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC); 996 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL); 997 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC); 998 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE); 999 default: 1000 res = "Unknown"; 1001 } 1002 break; 1003 default: 1004 res = "Unknown"; 1005 } 1006 Result.append(res.begin(), res.end()); 1007 return object_error::success; 1008 } 1009 1010 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME 1011 1012 template<support::endianness target_endianness, bool is64Bits> 1013 error_code ELFObjectFile<target_endianness, is64Bits> 1014 ::getRelocationAdditionalInfo(DataRefImpl Rel, 1015 int64_t &Result) const { 1016 const Elf_Shdr *sec = getSection(Rel.w.b); 1017 switch (sec->sh_type) { 1018 default : 1019 report_fatal_error("Invalid section type in Rel!"); 1020 case ELF::SHT_REL : { 1021 Result = 0; 1022 return object_error::success; 1023 } 1024 case ELF::SHT_RELA : { 1025 Result = getRela(Rel)->r_addend; 1026 return object_error::success; 1027 } 1028 } 1029 } 1030 1031 template<support::endianness target_endianness, bool is64Bits> 1032 error_code ELFObjectFile<target_endianness, is64Bits> 1033 ::getRelocationValueString(DataRefImpl Rel, 1034 SmallVectorImpl<char> &Result) const { 1035 const Elf_Shdr *sec = getSection(Rel.w.b); 1036 uint8_t type; 1037 StringRef res; 1038 int64_t addend = 0; 1039 uint16_t symbol_index = 0; 1040 switch (sec->sh_type) { 1041 default : 1042 return object_error::parse_failed; 1043 case ELF::SHT_REL : { 1044 type = getRel(Rel)->getType(); 1045 symbol_index = getRel(Rel)->getSymbol(); 1046 // TODO: Read implicit addend from section data. 1047 break; 1048 } 1049 case ELF::SHT_RELA : { 1050 type = getRela(Rel)->getType(); 1051 symbol_index = getRela(Rel)->getSymbol(); 1052 addend = getRela(Rel)->r_addend; 1053 break; 1054 } 1055 } 1056 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index); 1057 StringRef symname; 1058 if (error_code ec = getSymbolName(symb, symname)) 1059 return ec; 1060 switch (Header->e_machine) { 1061 case ELF::EM_X86_64: 1062 switch (type) { 1063 case ELF::R_X86_64_32S: 1064 res = symname; 1065 break; 1066 case ELF::R_X86_64_PC32: { 1067 std::string fmtbuf; 1068 raw_string_ostream fmt(fmtbuf); 1069 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P"; 1070 fmt.flush(); 1071 Result.append(fmtbuf.begin(), fmtbuf.end()); 1072 } 1073 break; 1074 default: 1075 res = "Unknown"; 1076 } 1077 break; 1078 default: 1079 res = "Unknown"; 1080 } 1081 if (Result.empty()) 1082 Result.append(res.begin(), res.end()); 1083 return object_error::success; 1084 } 1085 1086 template<support::endianness target_endianness, bool is64Bits> 1087 ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object 1088 , error_code &ec) 1089 : ObjectFile(Binary::isELF, Object, ec) 1090 , SectionHeaderTable(0) 1091 , dot_shstrtab_sec(0) 1092 , dot_strtab_sec(0) { 1093 Header = reinterpret_cast<const Elf_Ehdr *>(base()); 1094 1095 if (Header->e_shoff == 0) 1096 return; 1097 1098 SectionHeaderTable = 1099 reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff); 1100 uint64_t SectionTableSize = getNumSections() * Header->e_shentsize; 1101 if (!( (const uint8_t *)SectionHeaderTable + SectionTableSize 1102 <= base() + Data->getBufferSize())) 1103 // FIXME: Proper error handling. 1104 report_fatal_error("Section table goes past end of file!"); 1105 1106 1107 // To find the symbol tables we walk the section table to find SHT_SYMTAB. 1108 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0; 1109 const Elf_Shdr* sh = reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable); 1110 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) { 1111 if (sh->sh_type == ELF::SHT_SYMTAB_SHNDX) { 1112 if (SymbolTableSectionHeaderIndex) 1113 // FIXME: Proper error handling. 1114 report_fatal_error("More than one .symtab_shndx!"); 1115 SymbolTableSectionHeaderIndex = sh; 1116 } 1117 if (sh->sh_type == ELF::SHT_SYMTAB) { 1118 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size(); 1119 SymbolTableSections.push_back(sh); 1120 } 1121 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) { 1122 SectionRelocMap[getSection(sh->sh_info)].push_back(i); 1123 } 1124 ++sh; 1125 } 1126 1127 // Sort section relocation lists by index. 1128 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(), 1129 e = SectionRelocMap.end(); i != e; ++i) { 1130 std::sort(i->second.begin(), i->second.end()); 1131 } 1132 1133 // Get string table sections. 1134 dot_shstrtab_sec = getSection(getStringTableIndex()); 1135 if (dot_shstrtab_sec) { 1136 // Verify that the last byte in the string table in a null. 1137 if (((const char*)base() + dot_shstrtab_sec->sh_offset) 1138 [dot_shstrtab_sec->sh_size - 1] != 0) 1139 // FIXME: Proper error handling. 1140 report_fatal_error("String table must end with a null terminator!"); 1141 } 1142 1143 // Merge this into the above loop. 1144 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable), 1145 *e = i + getNumSections() * Header->e_shentsize; 1146 i != e; i += Header->e_shentsize) { 1147 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i); 1148 if (sh->sh_type == ELF::SHT_STRTAB) { 1149 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name)); 1150 if (SectionName == ".strtab") { 1151 if (dot_strtab_sec != 0) 1152 // FIXME: Proper error handling. 1153 report_fatal_error("Already found section named .strtab!"); 1154 dot_strtab_sec = sh; 1155 const char *dot_strtab = (const char*)base() + sh->sh_offset; 1156 if (dot_strtab[sh->sh_size - 1] != 0) 1157 // FIXME: Proper error handling. 1158 report_fatal_error("String table must end with a null terminator!"); 1159 } 1160 } 1161 } 1162 1163 // Build symbol name side-mapping if there is one. 1164 if (SymbolTableSectionHeaderIndex) { 1165 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() + 1166 SymbolTableSectionHeaderIndex->sh_offset); 1167 error_code ec; 1168 for (symbol_iterator si = begin_symbols(), 1169 se = end_symbols(); si != se; si.increment(ec)) { 1170 if (ec) 1171 report_fatal_error("Fewer extended symbol table entries than symbols!"); 1172 if (*ShndxTable != ELF::SHN_UNDEF) 1173 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable; 1174 ++ShndxTable; 1175 } 1176 } 1177 } 1178 1179 template<support::endianness target_endianness, bool is64Bits> 1180 symbol_iterator ELFObjectFile<target_endianness, is64Bits> 1181 ::begin_symbols() const { 1182 DataRefImpl SymbolData; 1183 memset(&SymbolData, 0, sizeof(SymbolData)); 1184 if (SymbolTableSections.size() == 0) { 1185 SymbolData.d.a = std::numeric_limits<uint32_t>::max(); 1186 SymbolData.d.b = std::numeric_limits<uint32_t>::max(); 1187 } else { 1188 SymbolData.d.a = 1; // The 0th symbol in ELF is fake. 1189 SymbolData.d.b = 0; 1190 } 1191 return symbol_iterator(SymbolRef(SymbolData, this)); 1192 } 1193 1194 template<support::endianness target_endianness, bool is64Bits> 1195 symbol_iterator ELFObjectFile<target_endianness, is64Bits> 1196 ::end_symbols() const { 1197 DataRefImpl SymbolData; 1198 memset(&SymbolData, 0, sizeof(SymbolData)); 1199 SymbolData.d.a = std::numeric_limits<uint32_t>::max(); 1200 SymbolData.d.b = std::numeric_limits<uint32_t>::max(); 1201 return symbol_iterator(SymbolRef(SymbolData, this)); 1202 } 1203 1204 template<support::endianness target_endianness, bool is64Bits> 1205 section_iterator ELFObjectFile<target_endianness, is64Bits> 1206 ::begin_sections() const { 1207 DataRefImpl ret; 1208 memset(&ret, 0, sizeof(DataRefImpl)); 1209 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff); 1210 return section_iterator(SectionRef(ret, this)); 1211 } 1212 1213 template<support::endianness target_endianness, bool is64Bits> 1214 section_iterator ELFObjectFile<target_endianness, is64Bits> 1215 ::end_sections() const { 1216 DataRefImpl ret; 1217 memset(&ret, 0, sizeof(DataRefImpl)); 1218 ret.p = reinterpret_cast<intptr_t>(base() 1219 + Header->e_shoff 1220 + (Header->e_shentsize*getNumSections())); 1221 return section_iterator(SectionRef(ret, this)); 1222 } 1223 1224 template<support::endianness target_endianness, bool is64Bits> 1225 uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const { 1226 return is64Bits ? 8 : 4; 1227 } 1228 1229 template<support::endianness target_endianness, bool is64Bits> 1230 StringRef ELFObjectFile<target_endianness, is64Bits> 1231 ::getFileFormatName() const { 1232 switch(Header->e_ident[ELF::EI_CLASS]) { 1233 case ELF::ELFCLASS32: 1234 switch(Header->e_machine) { 1235 case ELF::EM_386: 1236 return "ELF32-i386"; 1237 case ELF::EM_X86_64: 1238 return "ELF32-x86-64"; 1239 case ELF::EM_ARM: 1240 return "ELF32-arm"; 1241 default: 1242 return "ELF32-unknown"; 1243 } 1244 case ELF::ELFCLASS64: 1245 switch(Header->e_machine) { 1246 case ELF::EM_386: 1247 return "ELF64-i386"; 1248 case ELF::EM_X86_64: 1249 return "ELF64-x86-64"; 1250 default: 1251 return "ELF64-unknown"; 1252 } 1253 default: 1254 // FIXME: Proper error handling. 1255 report_fatal_error("Invalid ELFCLASS!"); 1256 } 1257 } 1258 1259 template<support::endianness target_endianness, bool is64Bits> 1260 unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const { 1261 switch(Header->e_machine) { 1262 case ELF::EM_386: 1263 return Triple::x86; 1264 case ELF::EM_X86_64: 1265 return Triple::x86_64; 1266 case ELF::EM_ARM: 1267 return Triple::arm; 1268 default: 1269 return Triple::UnknownArch; 1270 } 1271 } 1272 1273 template<support::endianness target_endianness, bool is64Bits> 1274 uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const { 1275 if (Header->e_shnum == ELF::SHN_UNDEF) 1276 return SectionHeaderTable->sh_size; 1277 return Header->e_shnum; 1278 } 1279 1280 template<support::endianness target_endianness, bool is64Bits> 1281 uint64_t 1282 ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const { 1283 if (Header->e_shnum == ELF::SHN_UNDEF) { 1284 if (Header->e_shstrndx == ELF::SHN_HIRESERVE) 1285 return SectionHeaderTable->sh_link; 1286 if (Header->e_shstrndx >= getNumSections()) 1287 return 0; 1288 } 1289 return Header->e_shstrndx; 1290 } 1291 1292 1293 template<support::endianness target_endianness, bool is64Bits> 1294 template<typename T> 1295 inline const T * 1296 ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section, 1297 uint32_t Entry) const { 1298 return getEntry<T>(getSection(Section), Entry); 1299 } 1300 1301 template<support::endianness target_endianness, bool is64Bits> 1302 template<typename T> 1303 inline const T * 1304 ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section, 1305 uint32_t Entry) const { 1306 return reinterpret_cast<const T *>( 1307 base() 1308 + Section->sh_offset 1309 + (Entry * Section->sh_entsize)); 1310 } 1311 1312 template<support::endianness target_endianness, bool is64Bits> 1313 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym * 1314 ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const { 1315 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a); 1316 } 1317 1318 template<support::endianness target_endianness, bool is64Bits> 1319 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel * 1320 ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const { 1321 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c); 1322 } 1323 1324 template<support::endianness target_endianness, bool is64Bits> 1325 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela * 1326 ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const { 1327 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c); 1328 } 1329 1330 template<support::endianness target_endianness, bool is64Bits> 1331 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * 1332 ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const { 1333 const Elf_Shdr *sec = getSection(Symb.d.b); 1334 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM) 1335 // FIXME: Proper error handling. 1336 report_fatal_error("Invalid symbol table section!"); 1337 return sec; 1338 } 1339 1340 template<support::endianness target_endianness, bool is64Bits> 1341 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * 1342 ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const { 1343 if (index == 0) 1344 return 0; 1345 if (!SectionHeaderTable || index >= getNumSections()) 1346 // FIXME: Proper error handling. 1347 report_fatal_error("Invalid section index!"); 1348 1349 return reinterpret_cast<const Elf_Shdr *>( 1350 reinterpret_cast<const char *>(SectionHeaderTable) 1351 + (index * Header->e_shentsize)); 1352 } 1353 1354 template<support::endianness target_endianness, bool is64Bits> 1355 const char *ELFObjectFile<target_endianness, is64Bits> 1356 ::getString(uint32_t section, 1357 ELF::Elf32_Word offset) const { 1358 return getString(getSection(section), offset); 1359 } 1360 1361 template<support::endianness target_endianness, bool is64Bits> 1362 const char *ELFObjectFile<target_endianness, is64Bits> 1363 ::getString(const Elf_Shdr *section, 1364 ELF::Elf32_Word offset) const { 1365 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!"); 1366 if (offset >= section->sh_size) 1367 // FIXME: Proper error handling. 1368 report_fatal_error("Symbol name offset outside of string table!"); 1369 return (const char *)base() + section->sh_offset + offset; 1370 } 1371 1372 template<support::endianness target_endianness, bool is64Bits> 1373 error_code ELFObjectFile<target_endianness, is64Bits> 1374 ::getSymbolName(const Elf_Sym *symb, 1375 StringRef &Result) const { 1376 if (symb->st_name == 0) { 1377 const Elf_Shdr *section = getSection(symb); 1378 if (!section) 1379 Result = ""; 1380 else 1381 Result = getString(dot_shstrtab_sec, section->sh_name); 1382 return object_error::success; 1383 } 1384 1385 // Use the default symbol table name section. 1386 Result = getString(dot_strtab_sec, symb->st_name); 1387 return object_error::success; 1388 } 1389 1390 // EI_CLASS, EI_DATA. 1391 static std::pair<unsigned char, unsigned char> 1392 getElfArchType(MemoryBuffer *Object) { 1393 if (Object->getBufferSize() < ELF::EI_NIDENT) 1394 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE); 1395 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS] 1396 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]); 1397 } 1398 1399 namespace llvm { 1400 1401 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) { 1402 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object); 1403 error_code ec; 1404 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) 1405 return new ELFObjectFile<support::little, false>(Object, ec); 1406 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) 1407 return new ELFObjectFile<support::big, false>(Object, ec); 1408 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) 1409 return new ELFObjectFile<support::little, true>(Object, ec); 1410 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) 1411 return new ELFObjectFile<support::big, true>(Object, ec); 1412 // FIXME: Proper error handling. 1413 report_fatal_error("Not an ELF object file!"); 1414 } 1415 1416 } // end namespace llvm 1417