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 300 /// @brief Map sections to an array of relocation sections that reference 301 /// them sorted by section index. 302 RelocMap_t SectionRelocMap; 303 304 /// @brief Get the relocation section that contains \a Rel. 305 const Elf_Shdr *getRelSection(DataRefImpl Rel) const { 306 return getSection(Rel.w.b); 307 } 308 309 void validateSymbol(DataRefImpl Symb) const; 310 bool isRelocationHasAddend(DataRefImpl Rel) const; 311 template<typename T> 312 const T *getEntry(uint16_t Section, uint32_t Entry) const; 313 template<typename T> 314 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const; 315 const Elf_Sym *getSymbol(DataRefImpl Symb) const; 316 const Elf_Shdr *getSection(DataRefImpl index) const; 317 const Elf_Shdr *getSection(uint32_t index) const; 318 const Elf_Rel *getRel(DataRefImpl Rel) const; 319 const Elf_Rela *getRela(DataRefImpl Rela) const; 320 const char *getString(uint32_t section, uint32_t offset) const; 321 const char *getString(const Elf_Shdr *section, uint32_t offset) const; 322 error_code getSymbolName(const Elf_Sym *Symb, StringRef &Res) const; 323 324 protected: 325 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const; 326 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const; 327 virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const; 328 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const; 329 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const; 330 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const; 331 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const; 332 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const; 333 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::SymbolType &Res) const; 334 335 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const; 336 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const; 337 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const; 338 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const; 339 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const; 340 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const; 341 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const; 342 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const; 343 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const; 344 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, 345 bool &Result) const; 346 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const; 347 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const; 348 349 virtual error_code getRelocationNext(DataRefImpl Rel, 350 RelocationRef &Res) const; 351 virtual error_code getRelocationAddress(DataRefImpl Rel, 352 uint64_t &Res) const; 353 virtual error_code getRelocationSymbol(DataRefImpl Rel, 354 SymbolRef &Res) const; 355 virtual error_code getRelocationType(DataRefImpl Rel, 356 uint32_t &Res) const; 357 virtual error_code getRelocationTypeName(DataRefImpl Rel, 358 SmallVectorImpl<char> &Result) const; 359 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel, 360 int64_t &Res) const; 361 virtual error_code getRelocationValueString(DataRefImpl Rel, 362 SmallVectorImpl<char> &Result) const; 363 364 public: 365 ELFObjectFile(MemoryBuffer *Object, error_code &ec); 366 virtual symbol_iterator begin_symbols() const; 367 virtual symbol_iterator end_symbols() const; 368 virtual section_iterator begin_sections() const; 369 virtual section_iterator end_sections() const; 370 371 virtual uint8_t getBytesInAddress() const; 372 virtual StringRef getFileFormatName() const; 373 virtual unsigned getArch() const; 374 375 uint64_t getNumSections() const; 376 uint64_t getStringTableIndex() const; 377 }; 378 } // end namespace 379 380 template<support::endianness target_endianness, bool is64Bits> 381 void ELFObjectFile<target_endianness, is64Bits> 382 ::validateSymbol(DataRefImpl Symb) const { 383 const Elf_Sym *symb = getSymbol(Symb); 384 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; 385 // FIXME: We really need to do proper error handling in the case of an invalid 386 // input file. Because we don't use exceptions, I think we'll just pass 387 // an error object around. 388 if (!( symb 389 && SymbolTableSection 390 && symb >= (const Elf_Sym*)(base() 391 + SymbolTableSection->sh_offset) 392 && symb < (const Elf_Sym*)(base() 393 + SymbolTableSection->sh_offset 394 + SymbolTableSection->sh_size))) 395 // FIXME: Proper error handling. 396 report_fatal_error("Symb must point to a valid symbol!"); 397 } 398 399 template<support::endianness target_endianness, bool is64Bits> 400 error_code ELFObjectFile<target_endianness, is64Bits> 401 ::getSymbolNext(DataRefImpl Symb, 402 SymbolRef &Result) const { 403 validateSymbol(Symb); 404 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b]; 405 406 ++Symb.d.a; 407 // Check to see if we are at the end of this symbol table. 408 if (Symb.d.a >= SymbolTableSection->getEntityCount()) { 409 // We are at the end. If there are other symbol tables, jump to them. 410 ++Symb.d.b; 411 Symb.d.a = 1; // The 0th symbol in ELF is fake. 412 // Otherwise return the terminator. 413 if (Symb.d.b >= SymbolTableSections.size()) { 414 Symb.d.a = std::numeric_limits<uint32_t>::max(); 415 Symb.d.b = std::numeric_limits<uint32_t>::max(); 416 } 417 } 418 419 Result = SymbolRef(Symb, this); 420 return object_error::success; 421 } 422 423 template<support::endianness target_endianness, bool is64Bits> 424 error_code ELFObjectFile<target_endianness, is64Bits> 425 ::getSymbolName(DataRefImpl Symb, 426 StringRef &Result) const { 427 validateSymbol(Symb); 428 const Elf_Sym *symb = getSymbol(Symb); 429 return getSymbolName(symb, Result); 430 } 431 432 template<support::endianness target_endianness, bool is64Bits> 433 error_code ELFObjectFile<target_endianness, is64Bits> 434 ::getSymbolOffset(DataRefImpl Symb, 435 uint64_t &Result) const { 436 validateSymbol(Symb); 437 const Elf_Sym *symb = getSymbol(Symb); 438 const Elf_Shdr *Section; 439 switch (symb->st_shndx) { 440 case ELF::SHN_COMMON: 441 // Undefined symbols have no address yet. 442 case ELF::SHN_UNDEF: 443 Result = UnknownAddressOrSize; 444 return object_error::success; 445 case ELF::SHN_ABS: 446 Result = symb->st_value; 447 return object_error::success; 448 default: Section = getSection(symb->st_shndx); 449 } 450 451 switch (symb->getType()) { 452 case ELF::STT_SECTION: 453 Result = Section ? Section->sh_addr : UnknownAddressOrSize; 454 return object_error::success; 455 case ELF::STT_FUNC: 456 case ELF::STT_OBJECT: 457 case ELF::STT_NOTYPE: 458 Result = symb->st_value; 459 return object_error::success; 460 default: 461 Result = UnknownAddressOrSize; 462 return object_error::success; 463 } 464 } 465 466 template<support::endianness target_endianness, bool is64Bits> 467 error_code ELFObjectFile<target_endianness, is64Bits> 468 ::getSymbolAddress(DataRefImpl Symb, 469 uint64_t &Result) const { 470 validateSymbol(Symb); 471 const Elf_Sym *symb = getSymbol(Symb); 472 const Elf_Shdr *Section; 473 switch (symb->st_shndx) { 474 case ELF::SHN_COMMON: // Fall through. 475 // Undefined symbols have no address yet. 476 case ELF::SHN_UNDEF: 477 Result = UnknownAddressOrSize; 478 return object_error::success; 479 case ELF::SHN_ABS: 480 Result = reinterpret_cast<uintptr_t>(base()+symb->st_value); 481 return object_error::success; 482 default: Section = getSection(symb->st_shndx); 483 } 484 const uint8_t* addr = base(); 485 if (Section) 486 addr += Section->sh_offset; 487 switch (symb->getType()) { 488 case ELF::STT_SECTION: 489 Result = reinterpret_cast<uintptr_t>(addr); 490 return object_error::success; 491 case ELF::STT_FUNC: // Fall through. 492 case ELF::STT_OBJECT: // Fall through. 493 case ELF::STT_NOTYPE: 494 addr += symb->st_value; 495 Result = reinterpret_cast<uintptr_t>(addr); 496 return object_error::success; 497 default: 498 Result = UnknownAddressOrSize; 499 return object_error::success; 500 } 501 } 502 503 template<support::endianness target_endianness, bool is64Bits> 504 error_code ELFObjectFile<target_endianness, is64Bits> 505 ::getSymbolSize(DataRefImpl Symb, 506 uint64_t &Result) const { 507 validateSymbol(Symb); 508 const Elf_Sym *symb = getSymbol(Symb); 509 if (symb->st_size == 0) 510 Result = UnknownAddressOrSize; 511 Result = symb->st_size; 512 return object_error::success; 513 } 514 515 template<support::endianness target_endianness, bool is64Bits> 516 error_code ELFObjectFile<target_endianness, is64Bits> 517 ::getSymbolNMTypeChar(DataRefImpl Symb, 518 char &Result) const { 519 validateSymbol(Symb); 520 const Elf_Sym *symb = getSymbol(Symb); 521 const Elf_Shdr *Section = getSection(symb->st_shndx); 522 523 char ret = '?'; 524 525 if (Section) { 526 switch (Section->sh_type) { 527 case ELF::SHT_PROGBITS: 528 case ELF::SHT_DYNAMIC: 529 switch (Section->sh_flags) { 530 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR): 531 ret = 't'; break; 532 case (ELF::SHF_ALLOC | ELF::SHF_WRITE): 533 ret = 'd'; break; 534 case ELF::SHF_ALLOC: 535 case (ELF::SHF_ALLOC | ELF::SHF_MERGE): 536 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS): 537 ret = 'r'; break; 538 } 539 break; 540 case ELF::SHT_NOBITS: ret = 'b'; 541 } 542 } 543 544 switch (symb->st_shndx) { 545 case ELF::SHN_UNDEF: 546 if (ret == '?') 547 ret = 'U'; 548 break; 549 case ELF::SHN_ABS: ret = 'a'; break; 550 case ELF::SHN_COMMON: ret = 'c'; break; 551 } 552 553 switch (symb->getBinding()) { 554 case ELF::STB_GLOBAL: ret = ::toupper(ret); break; 555 case ELF::STB_WEAK: 556 if (symb->st_shndx == ELF::SHN_UNDEF) 557 ret = 'w'; 558 else 559 if (symb->getType() == ELF::STT_OBJECT) 560 ret = 'V'; 561 else 562 ret = 'W'; 563 } 564 565 if (ret == '?' && symb->getType() == ELF::STT_SECTION) { 566 StringRef name; 567 if (error_code ec = getSymbolName(Symb, name)) 568 return ec; 569 Result = StringSwitch<char>(name) 570 .StartsWith(".debug", 'N') 571 .StartsWith(".note", 'n') 572 .Default('?'); 573 return object_error::success; 574 } 575 576 Result = ret; 577 return object_error::success; 578 } 579 580 template<support::endianness target_endianness, bool is64Bits> 581 error_code ELFObjectFile<target_endianness, is64Bits> 582 ::getSymbolType(DataRefImpl Symb, 583 SymbolRef::SymbolType &Result) const { 584 validateSymbol(Symb); 585 const Elf_Sym *symb = getSymbol(Symb); 586 587 if (symb->st_shndx == ELF::SHN_UNDEF) { 588 Result = SymbolRef::ST_External; 589 return object_error::success; 590 } 591 592 switch (symb->getType()) { 593 case ELF::STT_FUNC: 594 Result = SymbolRef::ST_Function; 595 break; 596 case ELF::STT_OBJECT: 597 Result = SymbolRef::ST_Data; 598 break; 599 default: 600 Result = SymbolRef::ST_Other; 601 break; 602 } 603 return object_error::success; 604 } 605 606 template<support::endianness target_endianness, bool is64Bits> 607 error_code ELFObjectFile<target_endianness, is64Bits> 608 ::isSymbolGlobal(DataRefImpl Symb, 609 bool &Result) const { 610 validateSymbol(Symb); 611 const Elf_Sym *symb = getSymbol(Symb); 612 613 Result = symb->getBinding() == ELF::STB_GLOBAL; 614 return object_error::success; 615 } 616 617 template<support::endianness target_endianness, bool is64Bits> 618 error_code ELFObjectFile<target_endianness, is64Bits> 619 ::isSymbolInternal(DataRefImpl Symb, 620 bool &Result) const { 621 validateSymbol(Symb); 622 const Elf_Sym *symb = getSymbol(Symb); 623 624 if ( symb->getType() == ELF::STT_FILE 625 || symb->getType() == ELF::STT_SECTION) 626 Result = true; 627 Result = false; 628 return object_error::success; 629 } 630 631 template<support::endianness target_endianness, bool is64Bits> 632 error_code ELFObjectFile<target_endianness, is64Bits> 633 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const { 634 const uint8_t *sec = reinterpret_cast<const uint8_t *>(Sec.p); 635 sec += Header->e_shentsize; 636 Sec.p = reinterpret_cast<intptr_t>(sec); 637 Result = SectionRef(Sec, this); 638 return object_error::success; 639 } 640 641 template<support::endianness target_endianness, bool is64Bits> 642 error_code ELFObjectFile<target_endianness, is64Bits> 643 ::getSectionName(DataRefImpl Sec, 644 StringRef &Result) const { 645 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 646 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name)); 647 return object_error::success; 648 } 649 650 template<support::endianness target_endianness, bool is64Bits> 651 error_code ELFObjectFile<target_endianness, is64Bits> 652 ::getSectionAddress(DataRefImpl Sec, 653 uint64_t &Result) const { 654 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 655 Result = sec->sh_addr; 656 return object_error::success; 657 } 658 659 template<support::endianness target_endianness, bool is64Bits> 660 error_code ELFObjectFile<target_endianness, is64Bits> 661 ::getSectionSize(DataRefImpl Sec, 662 uint64_t &Result) const { 663 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 664 Result = sec->sh_size; 665 return object_error::success; 666 } 667 668 template<support::endianness target_endianness, bool is64Bits> 669 error_code ELFObjectFile<target_endianness, is64Bits> 670 ::getSectionContents(DataRefImpl Sec, 671 StringRef &Result) const { 672 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 673 const char *start = (const char*)base() + sec->sh_offset; 674 Result = StringRef(start, sec->sh_size); 675 return object_error::success; 676 } 677 678 template<support::endianness target_endianness, bool is64Bits> 679 error_code ELFObjectFile<target_endianness, is64Bits> 680 ::getSectionAlignment(DataRefImpl Sec, 681 uint64_t &Result) const { 682 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 683 Result = sec->sh_addralign; 684 return object_error::success; 685 } 686 687 template<support::endianness target_endianness, bool is64Bits> 688 error_code ELFObjectFile<target_endianness, is64Bits> 689 ::isSectionText(DataRefImpl Sec, 690 bool &Result) const { 691 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 692 if (sec->sh_flags & ELF::SHF_EXECINSTR) 693 Result = true; 694 else 695 Result = false; 696 return object_error::success; 697 } 698 699 template<support::endianness target_endianness, bool is64Bits> 700 error_code ELFObjectFile<target_endianness, is64Bits> 701 ::isSectionData(DataRefImpl Sec, 702 bool &Result) const { 703 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 704 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) 705 && sec->sh_type == ELF::SHT_PROGBITS) 706 Result = true; 707 else 708 Result = false; 709 return object_error::success; 710 } 711 712 template<support::endianness target_endianness, bool is64Bits> 713 error_code ELFObjectFile<target_endianness, is64Bits> 714 ::isSectionBSS(DataRefImpl Sec, 715 bool &Result) const { 716 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 717 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) 718 && sec->sh_type == ELF::SHT_NOBITS) 719 Result = true; 720 else 721 Result = false; 722 return object_error::success; 723 } 724 725 template<support::endianness target_endianness, bool is64Bits> 726 error_code ELFObjectFile<target_endianness, is64Bits> 727 ::sectionContainsSymbol(DataRefImpl Sec, 728 DataRefImpl Symb, 729 bool &Result) const { 730 // FIXME: Unimplemented. 731 Result = false; 732 return object_error::success; 733 } 734 735 template<support::endianness target_endianness, bool is64Bits> 736 relocation_iterator ELFObjectFile<target_endianness, is64Bits> 737 ::getSectionRelBegin(DataRefImpl Sec) const { 738 DataRefImpl RelData; 739 memset(&RelData, 0, sizeof(RelData)); 740 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 741 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec); 742 if (sec != 0 && ittr != SectionRelocMap.end()) { 743 RelData.w.a = getSection(ittr->second[0])->sh_link; 744 RelData.w.b = ittr->second[0]; 745 RelData.w.c = 0; 746 } 747 return relocation_iterator(RelocationRef(RelData, this)); 748 } 749 750 template<support::endianness target_endianness, bool is64Bits> 751 relocation_iterator ELFObjectFile<target_endianness, is64Bits> 752 ::getSectionRelEnd(DataRefImpl Sec) const { 753 DataRefImpl RelData; 754 memset(&RelData, 0, sizeof(RelData)); 755 const Elf_Shdr *sec = reinterpret_cast<const Elf_Shdr *>(Sec.p); 756 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec); 757 if (sec != 0 && ittr != SectionRelocMap.end()) { 758 // Get the index of the last relocation section for this section. 759 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1]; 760 const Elf_Shdr *relocsec = getSection(relocsecindex); 761 RelData.w.a = relocsec->sh_link; 762 RelData.w.b = relocsecindex; 763 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize; 764 } 765 return relocation_iterator(RelocationRef(RelData, this)); 766 } 767 768 // Relocations 769 template<support::endianness target_endianness, bool is64Bits> 770 error_code ELFObjectFile<target_endianness, is64Bits> 771 ::getRelocationNext(DataRefImpl Rel, 772 RelocationRef &Result) const { 773 ++Rel.w.c; 774 const Elf_Shdr *relocsec = getSection(Rel.w.b); 775 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) { 776 // We have reached the end of the relocations for this section. See if there 777 // is another relocation section. 778 typename RelocMap_t::mapped_type relocseclist = 779 SectionRelocMap.lookup(getSection(Rel.w.a)); 780 781 // Do a binary search for the current reloc section index (which must be 782 // present). Then get the next one. 783 typename RelocMap_t::mapped_type::const_iterator loc = 784 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b); 785 ++loc; 786 787 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel 788 // to the end iterator. 789 if (loc != relocseclist.end()) { 790 Rel.w.b = *loc; 791 Rel.w.a = 0; 792 } 793 } 794 Result = RelocationRef(Rel, this); 795 return object_error::success; 796 } 797 798 template<support::endianness target_endianness, bool is64Bits> 799 error_code ELFObjectFile<target_endianness, is64Bits> 800 ::getRelocationSymbol(DataRefImpl Rel, 801 SymbolRef &Result) const { 802 uint32_t symbolIdx; 803 const Elf_Shdr *sec = getSection(Rel.w.b); 804 switch (sec->sh_type) { 805 default : 806 report_fatal_error("Invalid section type in Rel!"); 807 case ELF::SHT_REL : { 808 symbolIdx = getRel(Rel)->getSymbol(); 809 break; 810 } 811 case ELF::SHT_RELA : { 812 symbolIdx = getRela(Rel)->getSymbol(); 813 break; 814 } 815 } 816 DataRefImpl SymbolData; 817 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link); 818 if (it == SymbolTableSectionsIndexMap.end()) 819 report_fatal_error("Relocation symbol table not found!"); 820 SymbolData.d.a = symbolIdx; 821 SymbolData.d.b = it->second; 822 Result = SymbolRef(SymbolData, this); 823 return object_error::success; 824 } 825 826 template<support::endianness target_endianness, bool is64Bits> 827 error_code ELFObjectFile<target_endianness, is64Bits> 828 ::getRelocationAddress(DataRefImpl Rel, 829 uint64_t &Result) const { 830 uint64_t offset; 831 const Elf_Shdr *sec = getSection(Rel.w.b); 832 switch (sec->sh_type) { 833 default : 834 report_fatal_error("Invalid section type in Rel!"); 835 case ELF::SHT_REL : { 836 offset = getRel(Rel)->r_offset; 837 break; 838 } 839 case ELF::SHT_RELA : { 840 offset = getRela(Rel)->r_offset; 841 break; 842 } 843 } 844 845 Result = offset; 846 return object_error::success; 847 } 848 849 template<support::endianness target_endianness, bool is64Bits> 850 error_code ELFObjectFile<target_endianness, is64Bits> 851 ::getRelocationType(DataRefImpl Rel, 852 uint32_t &Result) const { 853 const Elf_Shdr *sec = getSection(Rel.w.b); 854 switch (sec->sh_type) { 855 default : 856 report_fatal_error("Invalid section type in Rel!"); 857 case ELF::SHT_REL : { 858 Result = getRel(Rel)->getType(); 859 break; 860 } 861 case ELF::SHT_RELA : { 862 Result = getRela(Rel)->getType(); 863 break; 864 } 865 } 866 return object_error::success; 867 } 868 869 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \ 870 case ELF::enum: res = #enum; break; 871 872 template<support::endianness target_endianness, bool is64Bits> 873 error_code ELFObjectFile<target_endianness, is64Bits> 874 ::getRelocationTypeName(DataRefImpl Rel, 875 SmallVectorImpl<char> &Result) const { 876 const Elf_Shdr *sec = getSection(Rel.w.b); 877 uint8_t type; 878 StringRef res; 879 switch (sec->sh_type) { 880 default : 881 return object_error::parse_failed; 882 case ELF::SHT_REL : { 883 type = getRel(Rel)->getType(); 884 break; 885 } 886 case ELF::SHT_RELA : { 887 type = getRela(Rel)->getType(); 888 break; 889 } 890 } 891 switch (Header->e_machine) { 892 case ELF::EM_X86_64: 893 switch (type) { 894 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE); 895 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64); 896 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32); 897 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32); 898 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32); 899 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY); 900 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT); 901 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT); 902 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE); 903 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL); 904 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32); 905 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S); 906 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16); 907 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16); 908 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8); 909 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8); 910 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64); 911 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64); 912 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64); 913 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD); 914 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD); 915 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32); 916 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF); 917 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32); 918 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64); 919 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64); 920 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32); 921 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32); 922 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64); 923 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC); 924 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL); 925 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC); 926 default: 927 res = "Unknown"; 928 } 929 break; 930 case ELF::EM_386: 931 switch (type) { 932 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE); 933 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32); 934 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32); 935 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32); 936 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32); 937 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY); 938 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT); 939 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT); 940 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE); 941 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF); 942 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC); 943 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT); 944 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF); 945 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE); 946 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE); 947 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE); 948 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD); 949 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM); 950 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16); 951 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16); 952 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8); 953 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8); 954 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32); 955 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH); 956 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL); 957 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP); 958 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32); 959 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH); 960 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL); 961 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP); 962 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32); 963 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32); 964 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32); 965 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32); 966 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32); 967 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32); 968 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC); 969 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL); 970 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC); 971 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE); 972 default: 973 res = "Unknown"; 974 } 975 break; 976 default: 977 res = "Unknown"; 978 } 979 Result.append(res.begin(), res.end()); 980 return object_error::success; 981 } 982 983 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME 984 985 template<support::endianness target_endianness, bool is64Bits> 986 error_code ELFObjectFile<target_endianness, is64Bits> 987 ::getRelocationAdditionalInfo(DataRefImpl Rel, 988 int64_t &Result) const { 989 const Elf_Shdr *sec = getSection(Rel.w.b); 990 switch (sec->sh_type) { 991 default : 992 report_fatal_error("Invalid section type in Rel!"); 993 case ELF::SHT_REL : { 994 Result = 0; 995 return object_error::success; 996 } 997 case ELF::SHT_RELA : { 998 Result = getRela(Rel)->r_addend; 999 return object_error::success; 1000 } 1001 } 1002 } 1003 1004 template<support::endianness target_endianness, bool is64Bits> 1005 error_code ELFObjectFile<target_endianness, is64Bits> 1006 ::getRelocationValueString(DataRefImpl Rel, 1007 SmallVectorImpl<char> &Result) const { 1008 const Elf_Shdr *sec = getSection(Rel.w.b); 1009 uint8_t type; 1010 StringRef res; 1011 int64_t addend = 0; 1012 uint16_t symbol_index = 0; 1013 switch (sec->sh_type) { 1014 default : 1015 return object_error::parse_failed; 1016 case ELF::SHT_REL : { 1017 type = getRel(Rel)->getType(); 1018 symbol_index = getRel(Rel)->getSymbol(); 1019 // TODO: Read implicit addend from section data. 1020 break; 1021 } 1022 case ELF::SHT_RELA : { 1023 type = getRela(Rel)->getType(); 1024 symbol_index = getRela(Rel)->getSymbol(); 1025 addend = getRela(Rel)->r_addend; 1026 break; 1027 } 1028 } 1029 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index); 1030 StringRef symname; 1031 if (error_code ec = getSymbolName(symb, symname)) 1032 return ec; 1033 switch (Header->e_machine) { 1034 case ELF::EM_X86_64: 1035 switch (type) { 1036 case ELF::R_X86_64_32S: 1037 res = symname; 1038 break; 1039 case ELF::R_X86_64_PC32: { 1040 std::string fmtbuf; 1041 raw_string_ostream fmt(fmtbuf); 1042 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P"; 1043 fmt.flush(); 1044 Result.append(fmtbuf.begin(), fmtbuf.end()); 1045 } 1046 break; 1047 default: 1048 res = "Unknown"; 1049 } 1050 break; 1051 default: 1052 res = "Unknown"; 1053 } 1054 if (Result.empty()) 1055 Result.append(res.begin(), res.end()); 1056 return object_error::success; 1057 } 1058 1059 template<support::endianness target_endianness, bool is64Bits> 1060 ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object 1061 , error_code &ec) 1062 : ObjectFile(Binary::isELF, Object, ec) 1063 , SectionHeaderTable(0) 1064 , dot_shstrtab_sec(0) 1065 , dot_strtab_sec(0) { 1066 Header = reinterpret_cast<const Elf_Ehdr *>(base()); 1067 1068 if (Header->e_shoff == 0) 1069 return; 1070 1071 SectionHeaderTable = 1072 reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff); 1073 uint64_t SectionTableSize = getNumSections() * Header->e_shentsize; 1074 if (!( (const uint8_t *)SectionHeaderTable + SectionTableSize 1075 <= base() + Data->getBufferSize())) 1076 // FIXME: Proper error handling. 1077 report_fatal_error("Section table goes past end of file!"); 1078 1079 // To find the symbol tables we walk the section table to find SHT_SYMTAB. 1080 const Elf_Shdr* sh = reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable); 1081 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) { 1082 if (sh->sh_type == ELF::SHT_SYMTAB) { 1083 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size(); 1084 SymbolTableSections.push_back(sh); 1085 } 1086 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) { 1087 SectionRelocMap[getSection(sh->sh_link)].push_back(i); 1088 } 1089 ++sh; 1090 } 1091 1092 // Sort section relocation lists by index. 1093 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(), 1094 e = SectionRelocMap.end(); i != e; ++i) { 1095 std::sort(i->second.begin(), i->second.end()); 1096 } 1097 1098 // Get string table sections. 1099 dot_shstrtab_sec = getSection(getStringTableIndex()); 1100 if (dot_shstrtab_sec) { 1101 // Verify that the last byte in the string table in a null. 1102 if (((const char*)base() + dot_shstrtab_sec->sh_offset) 1103 [dot_shstrtab_sec->sh_size - 1] != 0) 1104 // FIXME: Proper error handling. 1105 report_fatal_error("String table must end with a null terminator!"); 1106 } 1107 1108 // Merge this into the above loop. 1109 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable), 1110 *e = i + getNumSections() * Header->e_shentsize; 1111 i != e; i += Header->e_shentsize) { 1112 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i); 1113 if (sh->sh_type == ELF::SHT_STRTAB) { 1114 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name)); 1115 if (SectionName == ".strtab") { 1116 if (dot_strtab_sec != 0) 1117 // FIXME: Proper error handling. 1118 report_fatal_error("Already found section named .strtab!"); 1119 dot_strtab_sec = sh; 1120 const char *dot_strtab = (const char*)base() + sh->sh_offset; 1121 if (dot_strtab[sh->sh_size - 1] != 0) 1122 // FIXME: Proper error handling. 1123 report_fatal_error("String table must end with a null terminator!"); 1124 } 1125 } 1126 } 1127 } 1128 1129 template<support::endianness target_endianness, bool is64Bits> 1130 symbol_iterator ELFObjectFile<target_endianness, is64Bits> 1131 ::begin_symbols() const { 1132 DataRefImpl SymbolData; 1133 memset(&SymbolData, 0, sizeof(SymbolData)); 1134 if (SymbolTableSections.size() == 0) { 1135 SymbolData.d.a = std::numeric_limits<uint32_t>::max(); 1136 SymbolData.d.b = std::numeric_limits<uint32_t>::max(); 1137 } else { 1138 SymbolData.d.a = 1; // The 0th symbol in ELF is fake. 1139 SymbolData.d.b = 0; 1140 } 1141 return symbol_iterator(SymbolRef(SymbolData, this)); 1142 } 1143 1144 template<support::endianness target_endianness, bool is64Bits> 1145 symbol_iterator ELFObjectFile<target_endianness, is64Bits> 1146 ::end_symbols() const { 1147 DataRefImpl SymbolData; 1148 memset(&SymbolData, 0, sizeof(SymbolData)); 1149 SymbolData.d.a = std::numeric_limits<uint32_t>::max(); 1150 SymbolData.d.b = std::numeric_limits<uint32_t>::max(); 1151 return symbol_iterator(SymbolRef(SymbolData, this)); 1152 } 1153 1154 template<support::endianness target_endianness, bool is64Bits> 1155 section_iterator ELFObjectFile<target_endianness, is64Bits> 1156 ::begin_sections() const { 1157 DataRefImpl ret; 1158 memset(&ret, 0, sizeof(DataRefImpl)); 1159 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff); 1160 return section_iterator(SectionRef(ret, this)); 1161 } 1162 1163 template<support::endianness target_endianness, bool is64Bits> 1164 section_iterator ELFObjectFile<target_endianness, is64Bits> 1165 ::end_sections() const { 1166 DataRefImpl ret; 1167 memset(&ret, 0, sizeof(DataRefImpl)); 1168 ret.p = reinterpret_cast<intptr_t>(base() 1169 + Header->e_shoff 1170 + (Header->e_shentsize*getNumSections())); 1171 return section_iterator(SectionRef(ret, this)); 1172 } 1173 1174 template<support::endianness target_endianness, bool is64Bits> 1175 uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const { 1176 return is64Bits ? 8 : 4; 1177 } 1178 1179 template<support::endianness target_endianness, bool is64Bits> 1180 StringRef ELFObjectFile<target_endianness, is64Bits> 1181 ::getFileFormatName() const { 1182 switch(Header->e_ident[ELF::EI_CLASS]) { 1183 case ELF::ELFCLASS32: 1184 switch(Header->e_machine) { 1185 case ELF::EM_386: 1186 return "ELF32-i386"; 1187 case ELF::EM_X86_64: 1188 return "ELF32-x86-64"; 1189 case ELF::EM_ARM: 1190 return "ELF32-arm"; 1191 default: 1192 return "ELF32-unknown"; 1193 } 1194 case ELF::ELFCLASS64: 1195 switch(Header->e_machine) { 1196 case ELF::EM_386: 1197 return "ELF64-i386"; 1198 case ELF::EM_X86_64: 1199 return "ELF64-x86-64"; 1200 default: 1201 return "ELF64-unknown"; 1202 } 1203 default: 1204 // FIXME: Proper error handling. 1205 report_fatal_error("Invalid ELFCLASS!"); 1206 } 1207 } 1208 1209 template<support::endianness target_endianness, bool is64Bits> 1210 unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const { 1211 switch(Header->e_machine) { 1212 case ELF::EM_386: 1213 return Triple::x86; 1214 case ELF::EM_X86_64: 1215 return Triple::x86_64; 1216 case ELF::EM_ARM: 1217 return Triple::arm; 1218 default: 1219 return Triple::UnknownArch; 1220 } 1221 } 1222 1223 template<support::endianness target_endianness, bool is64Bits> 1224 uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const { 1225 if (Header->e_shnum == ELF::SHN_UNDEF) 1226 return SectionHeaderTable->sh_size; 1227 return Header->e_shnum; 1228 } 1229 1230 template<support::endianness target_endianness, bool is64Bits> 1231 uint64_t 1232 ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const { 1233 if (Header->e_shnum == ELF::SHN_UNDEF) { 1234 if (Header->e_shstrndx == ELF::SHN_HIRESERVE) 1235 return SectionHeaderTable->sh_link; 1236 if (Header->e_shstrndx >= getNumSections()) 1237 return 0; 1238 } 1239 return Header->e_shstrndx; 1240 } 1241 1242 1243 template<support::endianness target_endianness, bool is64Bits> 1244 template<typename T> 1245 inline const T * 1246 ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section, 1247 uint32_t Entry) const { 1248 return getEntry<T>(getSection(Section), Entry); 1249 } 1250 1251 template<support::endianness target_endianness, bool is64Bits> 1252 template<typename T> 1253 inline const T * 1254 ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section, 1255 uint32_t Entry) const { 1256 return reinterpret_cast<const T *>( 1257 base() 1258 + Section->sh_offset 1259 + (Entry * Section->sh_entsize)); 1260 } 1261 1262 template<support::endianness target_endianness, bool is64Bits> 1263 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym * 1264 ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const { 1265 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a); 1266 } 1267 1268 template<support::endianness target_endianness, bool is64Bits> 1269 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel * 1270 ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const { 1271 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c); 1272 } 1273 1274 template<support::endianness target_endianness, bool is64Bits> 1275 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela * 1276 ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const { 1277 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c); 1278 } 1279 1280 template<support::endianness target_endianness, bool is64Bits> 1281 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * 1282 ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const { 1283 const Elf_Shdr *sec = getSection(Symb.d.b); 1284 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM) 1285 // FIXME: Proper error handling. 1286 report_fatal_error("Invalid symbol table section!"); 1287 return sec; 1288 } 1289 1290 template<support::endianness target_endianness, bool is64Bits> 1291 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr * 1292 ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const { 1293 if (index == 0) 1294 return 0; 1295 if (!SectionHeaderTable || index >= getNumSections()) 1296 // FIXME: Proper error handling. 1297 report_fatal_error("Invalid section index!"); 1298 1299 return reinterpret_cast<const Elf_Shdr *>( 1300 reinterpret_cast<const char *>(SectionHeaderTable) 1301 + (index * Header->e_shentsize)); 1302 } 1303 1304 template<support::endianness target_endianness, bool is64Bits> 1305 const char *ELFObjectFile<target_endianness, is64Bits> 1306 ::getString(uint32_t section, 1307 ELF::Elf32_Word offset) const { 1308 return getString(getSection(section), offset); 1309 } 1310 1311 template<support::endianness target_endianness, bool is64Bits> 1312 const char *ELFObjectFile<target_endianness, is64Bits> 1313 ::getString(const Elf_Shdr *section, 1314 ELF::Elf32_Word offset) const { 1315 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!"); 1316 if (offset >= section->sh_size) 1317 // FIXME: Proper error handling. 1318 report_fatal_error("Symbol name offset outside of string table!"); 1319 return (const char *)base() + section->sh_offset + offset; 1320 } 1321 1322 template<support::endianness target_endianness, bool is64Bits> 1323 error_code ELFObjectFile<target_endianness, is64Bits> 1324 ::getSymbolName(const Elf_Sym *symb, 1325 StringRef &Result) const { 1326 if (symb->st_name == 0) { 1327 const Elf_Shdr *section = getSection(symb->st_shndx); 1328 if (!section) 1329 Result = ""; 1330 else 1331 Result = getString(dot_shstrtab_sec, section->sh_name); 1332 return object_error::success; 1333 } 1334 1335 // Use the default symbol table name section. 1336 Result = getString(dot_strtab_sec, symb->st_name); 1337 return object_error::success; 1338 } 1339 1340 // EI_CLASS, EI_DATA. 1341 static std::pair<unsigned char, unsigned char> 1342 getElfArchType(MemoryBuffer *Object) { 1343 if (Object->getBufferSize() < ELF::EI_NIDENT) 1344 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE); 1345 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS] 1346 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]); 1347 } 1348 1349 namespace llvm { 1350 1351 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) { 1352 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object); 1353 error_code ec; 1354 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) 1355 return new ELFObjectFile<support::little, false>(Object, ec); 1356 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) 1357 return new ELFObjectFile<support::big, false>(Object, ec); 1358 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) 1359 return new ELFObjectFile<support::little, true>(Object, ec); 1360 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) 1361 return new ELFObjectFile<support::big, true>(Object, ec); 1362 // FIXME: Proper error handling. 1363 report_fatal_error("Not an ELF object file!"); 1364 } 1365 1366 } // end namespace llvm 1367