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