1 //===- ELFObjectFile.cpp - ELF object file implementation -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ELFObjectFile and DyldELFObject classes.
12 //===----------------------------------------------------------------------===//
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"
30 using namespace object;
32 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
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;
50 template<support::endianness target_endianness, bool is64Bits>
51 struct ELFDataTypeTypedefHelper;
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;
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;
76 // I really don't like doing this, but the alternative is copypasta.
77 #define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \
79 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Addr Elf_Addr; \
81 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Off Elf_Off; \
83 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Half Elf_Half; \
85 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Word Elf_Word; \
87 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sword Elf_Sword; \
89 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Xword Elf_Xword; \
91 ELFDataTypeTypedefHelper<target_endianness, is64Bits>::Elf_Sxword Elf_Sxword;
95 template<support::endianness target_endianness, bool is64Bits>
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
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
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;
133 /// @brief Get the number of entities this section contains if it has any.
134 unsigned getEntityCount() const {
137 return sh_size / sh_entsize;
143 template<support::endianness target_endianness, bool is64Bits>
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
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
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;
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);
185 template<support::endianness target_endianness, bool is64Bits, bool isRela>
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
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
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
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.
218 template<support::endianness target_endianness, bool is64Bits, bool isRela>
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)
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);
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);
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)
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;
260 template<support::endianness target_endianness, bool is64Bits>
261 class ELFObjectFile : public ObjectFile {
262 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
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;
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
286 bool checkMagic() const {
287 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
289 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
290 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
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;
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;
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;
310 /// @brief Map sections to an array of relocation sections that reference
311 /// them sorted by section index.
312 RelocMap_t SectionRelocMap;
314 /// @brief Get the relocation section that contains \a Rel.
315 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
316 return getSection(Rel.w.b);
319 bool isRelocationHasAddend(DataRefImpl Rel) const;
321 const T *getEntry(uint16_t Section, uint32_t Entry) const;
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;
333 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
334 void validateSymbol(DataRefImpl Symb) const;
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;
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,
362 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
363 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
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,
379 virtual error_code getRelocationValueString(DataRefImpl Rel,
380 SmallVectorImpl<char> &Result) const;
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;
389 virtual uint8_t getBytesInAddress() const;
390 virtual StringRef getFileFormatName() const;
391 virtual unsigned getArch() const;
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;
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;
403 static inline bool classof(const ELFObjectFile *v) { return true; }
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.
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!");
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];
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.
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();
446 Result = SymbolRef(Symb, this);
447 return object_error::success;
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);
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;
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)
475 return getSection(symb->st_shndx);
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
489 Result = UnknownAddressOrSize;
490 return object_error::success;
492 Result = symb->st_value;
493 return object_error::success;
494 default: Section = getSection(symb);
497 switch (symb->getType()) {
498 case ELF::STT_SECTION:
499 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
500 return object_error::success;
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;
508 Result = UnknownAddressOrSize;
509 return object_error::success;
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:
523 Result = UnknownAddressOrSize;
524 return object_error::success;
526 Result = symb->st_value;
527 return object_error::success;
528 default: Section = getSection(symb);
531 switch (symb->getType()) {
532 case ELF::STT_SECTION:
533 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
534 return object_error::success;
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;
541 Result = UnknownAddressOrSize;
542 return object_error::success;
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;
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);
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):
575 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
578 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
579 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
583 case ELF::SHT_NOBITS: ret = 'b';
587 switch (getSymbolTableIndex(symb)) {
592 case ELF::SHN_ABS: ret = 'a'; break;
593 case ELF::SHN_COMMON: ret = 'c'; break;
596 switch (symb->getBinding()) {
597 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
599 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
602 if (symb->getType() == ELF::STT_OBJECT)
608 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
610 if (error_code ec = getSymbolName(Symb, name))
612 Result = StringSwitch<char>(name)
613 .StartsWith(".debug", 'N')
614 .StartsWith(".note", 'n')
616 return object_error::success;
620 return object_error::success;
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);
630 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) {
631 Result = SymbolRef::ST_External;
632 return object_error::success;
635 switch (symb->getType()) {
636 case ELF::STT_SECTION:
637 Result = SymbolRef::ST_Debug;
640 Result = SymbolRef::ST_File;
643 Result = SymbolRef::ST_Function;
645 case ELF::STT_OBJECT:
646 Result = SymbolRef::ST_Data;
649 Result = SymbolRef::ST_Other;
652 return object_error::success;
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);
662 Result = symb->getBinding() == ELF::STB_GLOBAL;
663 return object_error::success;
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);
673 Result = symb->getBinding() == ELF::STB_WEAK;
674 return object_error::success;
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;
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);
694 Res = end_sections();
697 Sec.p = reinterpret_cast<intptr_t>(sec);
698 Res = section_iterator(SectionRef(Sec, this));
700 return object_error::success;
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);
710 if ( symb->getType() == ELF::STT_FILE
711 || symb->getType() == ELF::STT_SECTION)
714 return object_error::success;
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;
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;
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;
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;
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;
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;
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)
782 return object_error::success;
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)
795 return object_error::success;
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)
808 return object_error::success;
811 template<support::endianness target_endianness, bool is64Bits>
812 error_code ELFObjectFile<target_endianness, is64Bits>
813 ::sectionContainsSymbol(DataRefImpl Sec,
815 bool &Result) const {
816 // FIXME: Unimplemented.
818 return object_error::success;
821 template<support::endianness target_endianness, bool is64Bits>
822 relocation_iterator ELFObjectFile<target_endianness, is64Bits>
823 ::getSectionRelBegin(DataRefImpl Sec) const {
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];
833 return relocation_iterator(RelocationRef(RelData, this));
836 template<support::endianness target_endianness, bool is64Bits>
837 relocation_iterator ELFObjectFile<target_endianness, is64Bits>
838 ::getSectionRelEnd(DataRefImpl Sec) const {
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;
851 return relocation_iterator(RelocationRef(RelData, this));
855 template<support::endianness target_endianness, bool is64Bits>
856 error_code ELFObjectFile<target_endianness, is64Bits>
857 ::getRelocationNext(DataRefImpl Rel,
858 RelocationRef &Result) const {
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));
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);
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()) {
880 Result = RelocationRef(Rel, this);
881 return object_error::success;
884 template<support::endianness target_endianness, bool is64Bits>
885 error_code ELFObjectFile<target_endianness, is64Bits>
886 ::getRelocationSymbol(DataRefImpl Rel,
887 SymbolRef &Result) const {
889 const Elf_Shdr *sec = getSection(Rel.w.b);
890 switch (sec->sh_type) {
892 report_fatal_error("Invalid section type in Rel!");
893 case ELF::SHT_REL : {
894 symbolIdx = getRel(Rel)->getSymbol();
897 case ELF::SHT_RELA : {
898 symbolIdx = getRela(Rel)->getSymbol();
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;
912 template<support::endianness target_endianness, bool is64Bits>
913 error_code ELFObjectFile<target_endianness, is64Bits>
914 ::getRelocationAddress(DataRefImpl Rel,
915 uint64_t &Result) const {
917 const Elf_Shdr *sec = getSection(Rel.w.b);
918 switch (sec->sh_type) {
920 report_fatal_error("Invalid section type in Rel!");
921 case ELF::SHT_REL : {
922 offset = getRel(Rel)->r_offset;
925 case ELF::SHT_RELA : {
926 offset = getRela(Rel)->r_offset;
932 return object_error::success;
935 template<support::endianness target_endianness, bool is64Bits>
936 error_code ELFObjectFile<target_endianness, is64Bits>
937 ::getRelocationOffset(DataRefImpl Rel,
938 uint64_t &Result) const {
940 const Elf_Shdr *sec = getSection(Rel.w.b);
941 switch (sec->sh_type) {
943 report_fatal_error("Invalid section type in Rel!");
944 case ELF::SHT_REL : {
945 offset = getRel(Rel)->r_offset;
948 case ELF::SHT_RELA : {
949 offset = getRela(Rel)->r_offset;
954 Result = offset - sec->sh_addr;
955 return object_error::success;
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) {
965 report_fatal_error("Invalid section type in Rel!");
966 case ELF::SHT_REL : {
967 Result = getRel(Rel)->getType();
970 case ELF::SHT_RELA : {
971 Result = getRela(Rel)->getType();
975 return object_error::success;
978 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
979 case ELF::enum: res = #enum; break;
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);
988 switch (sec->sh_type) {
990 return object_error::parse_failed;
991 case ELF::SHT_REL : {
992 type = getRel(Rel)->getType();
995 case ELF::SHT_RELA : {
996 type = getRela(Rel)->getType();
1000 switch (Header->e_machine) {
1001 case ELF::EM_X86_64:
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);
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);
1088 Result.append(res.begin(), res.end());
1089 return object_error::success;
1092 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
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) {
1101 report_fatal_error("Invalid section type in Rel!");
1102 case ELF::SHT_REL : {
1104 return object_error::success;
1106 case ELF::SHT_RELA : {
1107 Result = getRela(Rel)->r_addend;
1108 return object_error::success;
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);
1121 uint16_t symbol_index = 0;
1122 switch (sec->sh_type) {
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.
1131 case ELF::SHT_RELA : {
1132 type = getRela(Rel)->getType();
1133 symbol_index = getRela(Rel)->getSymbol();
1134 addend = getRela(Rel)->r_addend;
1138 const Elf_Sym *symb = getEntry<Elf_Sym>(sec->sh_link, symbol_index);
1140 if (error_code ec = getSymbolName(symb, symname))
1142 switch (Header->e_machine) {
1143 case ELF::EM_X86_64:
1145 case ELF::R_X86_64_32S:
1148 case ELF::R_X86_64_PC32: {
1150 raw_string_ostream fmt(fmtbuf);
1151 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1153 Result.append(fmtbuf.begin(), fmtbuf.end());
1164 Result.append(res.begin(), res.end());
1165 return object_error::success;
1168 template<support::endianness target_endianness, bool is64Bits>
1169 ELFObjectFile<target_endianness, is64Bits>::ELFObjectFile(MemoryBuffer *Object
1171 : ObjectFile(Binary::isELF, Object, ec)
1172 , isDyldELFObject(false)
1173 , SectionHeaderTable(0)
1174 , dot_shstrtab_sec(0)
1175 , dot_strtab_sec(0) {
1176 Header = reinterpret_cast<const Elf_Ehdr *>(base());
1178 if (Header->e_shoff == 0)
1181 SectionHeaderTable =
1182 reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
1183 uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
1185 if ((const uint8_t *)SectionHeaderTable + SectionTableSize
1186 > base() + Data->getBufferSize()) {
1187 // FIXME: Proper error handling.
1188 report_fatal_error("Section table goes past end of file!");
1192 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
1193 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
1194 const Elf_Shdr* sh = reinterpret_cast<const Elf_Shdr*>(SectionHeaderTable);
1195 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
1196 if (sh->sh_type == ELF::SHT_SYMTAB_SHNDX) {
1197 if (SymbolTableSectionHeaderIndex)
1198 // FIXME: Proper error handling.
1199 report_fatal_error("More than one .symtab_shndx!");
1200 SymbolTableSectionHeaderIndex = sh;
1202 if (sh->sh_type == ELF::SHT_SYMTAB) {
1203 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
1204 SymbolTableSections.push_back(sh);
1206 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
1207 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
1212 // Sort section relocation lists by index.
1213 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1214 e = SectionRelocMap.end(); i != e; ++i) {
1215 std::sort(i->second.begin(), i->second.end());
1218 // Get string table sections.
1219 dot_shstrtab_sec = getSection(getStringTableIndex());
1220 if (dot_shstrtab_sec) {
1221 // Verify that the last byte in the string table in a null.
1222 if (((const char*)base() + dot_shstrtab_sec->sh_offset)
1223 [dot_shstrtab_sec->sh_size - 1] != 0)
1224 // FIXME: Proper error handling.
1225 report_fatal_error("String table must end with a null terminator!");
1228 // Merge this into the above loop.
1229 for (const char *i = reinterpret_cast<const char *>(SectionHeaderTable),
1230 *e = i + getNumSections() * Header->e_shentsize;
1231 i != e; i += Header->e_shentsize) {
1232 const Elf_Shdr *sh = reinterpret_cast<const Elf_Shdr*>(i);
1233 if (sh->sh_type == ELF::SHT_STRTAB) {
1234 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
1235 if (SectionName == ".strtab") {
1236 if (dot_strtab_sec != 0)
1237 // FIXME: Proper error handling.
1238 report_fatal_error("Already found section named .strtab!");
1239 dot_strtab_sec = sh;
1240 const char *dot_strtab = (const char*)base() + sh->sh_offset;
1241 if (dot_strtab[sh->sh_size - 1] != 0)
1242 // FIXME: Proper error handling.
1243 report_fatal_error("String table must end with a null terminator!");
1248 // Build symbol name side-mapping if there is one.
1249 if (SymbolTableSectionHeaderIndex) {
1250 const Elf_Word *ShndxTable = reinterpret_cast<const Elf_Word*>(base() +
1251 SymbolTableSectionHeaderIndex->sh_offset);
1253 for (symbol_iterator si = begin_symbols(),
1254 se = end_symbols(); si != se; si.increment(ec)) {
1256 report_fatal_error("Fewer extended symbol table entries than symbols!");
1257 if (*ShndxTable != ELF::SHN_UNDEF)
1258 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
1264 template<support::endianness target_endianness, bool is64Bits>
1265 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1266 ::begin_symbols() const {
1267 DataRefImpl SymbolData;
1268 memset(&SymbolData, 0, sizeof(SymbolData));
1269 if (SymbolTableSections.size() == 0) {
1270 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1271 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1273 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1276 return symbol_iterator(SymbolRef(SymbolData, this));
1279 template<support::endianness target_endianness, bool is64Bits>
1280 symbol_iterator ELFObjectFile<target_endianness, is64Bits>
1281 ::end_symbols() const {
1282 DataRefImpl SymbolData;
1283 memset(&SymbolData, 0, sizeof(SymbolData));
1284 SymbolData.d.a = std::numeric_limits<uint32_t>::max();
1285 SymbolData.d.b = std::numeric_limits<uint32_t>::max();
1286 return symbol_iterator(SymbolRef(SymbolData, this));
1289 template<support::endianness target_endianness, bool is64Bits>
1290 section_iterator ELFObjectFile<target_endianness, is64Bits>
1291 ::begin_sections() const {
1293 memset(&ret, 0, sizeof(DataRefImpl));
1294 ret.p = reinterpret_cast<intptr_t>(base() + Header->e_shoff);
1295 return section_iterator(SectionRef(ret, this));
1298 template<support::endianness target_endianness, bool is64Bits>
1299 section_iterator ELFObjectFile<target_endianness, is64Bits>
1300 ::end_sections() const {
1302 memset(&ret, 0, sizeof(DataRefImpl));
1303 ret.p = reinterpret_cast<intptr_t>(base()
1305 + (Header->e_shentsize*getNumSections()));
1306 return section_iterator(SectionRef(ret, this));
1309 template<support::endianness target_endianness, bool is64Bits>
1310 uint8_t ELFObjectFile<target_endianness, is64Bits>::getBytesInAddress() const {
1311 return is64Bits ? 8 : 4;
1314 template<support::endianness target_endianness, bool is64Bits>
1315 StringRef ELFObjectFile<target_endianness, is64Bits>
1316 ::getFileFormatName() const {
1317 switch(Header->e_ident[ELF::EI_CLASS]) {
1318 case ELF::ELFCLASS32:
1319 switch(Header->e_machine) {
1321 return "ELF32-i386";
1322 case ELF::EM_X86_64:
1323 return "ELF32-x86-64";
1327 return "ELF32-unknown";
1329 case ELF::ELFCLASS64:
1330 switch(Header->e_machine) {
1332 return "ELF64-i386";
1333 case ELF::EM_X86_64:
1334 return "ELF64-x86-64";
1336 return "ELF64-unknown";
1339 // FIXME: Proper error handling.
1340 report_fatal_error("Invalid ELFCLASS!");
1344 template<support::endianness target_endianness, bool is64Bits>
1345 unsigned ELFObjectFile<target_endianness, is64Bits>::getArch() const {
1346 switch(Header->e_machine) {
1349 case ELF::EM_X86_64:
1350 return Triple::x86_64;
1354 return Triple::UnknownArch;
1358 template<support::endianness target_endianness, bool is64Bits>
1359 uint64_t ELFObjectFile<target_endianness, is64Bits>::getNumSections() const {
1360 if (Header->e_shnum == ELF::SHN_UNDEF)
1361 return SectionHeaderTable->sh_size;
1362 return Header->e_shnum;
1365 template<support::endianness target_endianness, bool is64Bits>
1367 ELFObjectFile<target_endianness, is64Bits>::getStringTableIndex() const {
1368 if (Header->e_shnum == ELF::SHN_UNDEF) {
1369 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
1370 return SectionHeaderTable->sh_link;
1371 if (Header->e_shstrndx >= getNumSections())
1374 return Header->e_shstrndx;
1378 template<support::endianness target_endianness, bool is64Bits>
1379 template<typename T>
1381 ELFObjectFile<target_endianness, is64Bits>::getEntry(uint16_t Section,
1382 uint32_t Entry) const {
1383 return getEntry<T>(getSection(Section), Entry);
1386 template<support::endianness target_endianness, bool is64Bits>
1387 template<typename T>
1389 ELFObjectFile<target_endianness, is64Bits>::getEntry(const Elf_Shdr * Section,
1390 uint32_t Entry) const {
1391 return reinterpret_cast<const T *>(
1393 + Section->sh_offset
1394 + (Entry * Section->sh_entsize));
1397 template<support::endianness target_endianness, bool is64Bits>
1398 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Sym *
1399 ELFObjectFile<target_endianness, is64Bits>::getSymbol(DataRefImpl Symb) const {
1400 return getEntry<Elf_Sym>(SymbolTableSections[Symb.d.b], Symb.d.a);
1403 template<support::endianness target_endianness, bool is64Bits>
1404 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rel *
1405 ELFObjectFile<target_endianness, is64Bits>::getRel(DataRefImpl Rel) const {
1406 return getEntry<Elf_Rel>(Rel.w.b, Rel.w.c);
1409 template<support::endianness target_endianness, bool is64Bits>
1410 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Rela *
1411 ELFObjectFile<target_endianness, is64Bits>::getRela(DataRefImpl Rela) const {
1412 return getEntry<Elf_Rela>(Rela.w.b, Rela.w.c);
1415 template<support::endianness target_endianness, bool is64Bits>
1416 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1417 ELFObjectFile<target_endianness, is64Bits>::getSection(DataRefImpl Symb) const {
1418 const Elf_Shdr *sec = getSection(Symb.d.b);
1419 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
1420 // FIXME: Proper error handling.
1421 report_fatal_error("Invalid symbol table section!");
1425 template<support::endianness target_endianness, bool is64Bits>
1426 const typename ELFObjectFile<target_endianness, is64Bits>::Elf_Shdr *
1427 ELFObjectFile<target_endianness, is64Bits>::getSection(uint32_t index) const {
1430 if (!SectionHeaderTable || index >= getNumSections())
1431 // FIXME: Proper error handling.
1432 report_fatal_error("Invalid section index!");
1434 return reinterpret_cast<const Elf_Shdr *>(
1435 reinterpret_cast<const char *>(SectionHeaderTable)
1436 + (index * Header->e_shentsize));
1439 template<support::endianness target_endianness, bool is64Bits>
1440 const char *ELFObjectFile<target_endianness, is64Bits>
1441 ::getString(uint32_t section,
1442 ELF::Elf32_Word offset) const {
1443 return getString(getSection(section), offset);
1446 template<support::endianness target_endianness, bool is64Bits>
1447 const char *ELFObjectFile<target_endianness, is64Bits>
1448 ::getString(const Elf_Shdr *section,
1449 ELF::Elf32_Word offset) const {
1450 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
1451 if (offset >= section->sh_size)
1452 // FIXME: Proper error handling.
1453 report_fatal_error("Symbol name offset outside of string table!");
1454 return (const char *)base() + section->sh_offset + offset;
1457 template<support::endianness target_endianness, bool is64Bits>
1458 error_code ELFObjectFile<target_endianness, is64Bits>
1459 ::getSymbolName(const Elf_Sym *symb,
1460 StringRef &Result) const {
1461 if (symb->st_name == 0) {
1462 const Elf_Shdr *section = getSection(symb);
1466 Result = getString(dot_shstrtab_sec, section->sh_name);
1467 return object_error::success;
1470 // Use the default symbol table name section.
1471 Result = getString(dot_strtab_sec, symb->st_name);
1472 return object_error::success;
1475 // EI_CLASS, EI_DATA.
1476 static std::pair<unsigned char, unsigned char>
1477 getElfArchType(MemoryBuffer *Object) {
1478 if (Object->getBufferSize() < ELF::EI_NIDENT)
1479 return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
1480 return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
1481 , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
1486 template<support::endianness target_endianness, bool is64Bits>
1487 class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> {
1488 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
1490 typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
1491 typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
1492 typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel;
1493 typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela;
1495 typedef typename ELFObjectFile<target_endianness, is64Bits>::
1499 // Update section headers according to the current location in memory
1500 virtual void rebaseObject(std::vector<uint8_t*> *MemoryMap);
1501 // Record memory addresses for cleanup
1502 virtual void saveAddress(std::vector<uint8_t*> *MemoryMap, uint8_t *addr);
1505 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
1508 DyldELFObject(MemoryBuffer *Object, std::vector<uint8_t*> *MemoryMap,
1511 // Methods for type inquiry through isa, cast, and dyn_cast
1512 static inline bool classof(const Binary *v) {
1513 return (isa<ELFObjectFile<target_endianness, is64Bits> >(v)
1514 && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v)));
1516 static inline bool classof(
1517 const ELFObjectFile<target_endianness, is64Bits> *v) {
1518 return v->isDyldType();
1520 static inline bool classof(const DyldELFObject *v) {
1524 } // end anonymous namespace
1526 template<support::endianness target_endianness, bool is64Bits>
1527 DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Object,
1528 std::vector<uint8_t*> *MemoryMap, error_code &ec)
1529 : ELFObjectFile<target_endianness, is64Bits>(Object, ec)
1531 this->isDyldELFObject = true;
1532 Header = const_cast<Elf_Ehdr *>(
1533 reinterpret_cast<const Elf_Ehdr *>(this->base()));
1534 if (Header->e_shoff == 0)
1537 // Mark the image as a dynamic shared library
1538 Header->e_type = ELF::ET_DYN;
1540 rebaseObject(MemoryMap);
1543 // Walk through the ELF headers, updating virtual addresses to reflect where
1544 // the object is currently loaded in memory
1545 template<support::endianness target_endianness, bool is64Bits>
1546 void DyldELFObject<target_endianness, is64Bits>::rebaseObject(
1547 std::vector<uint8_t*> *MemoryMap) {
1548 typedef typename ELFDataTypeTypedefHelper<
1549 target_endianness, is64Bits>::value_type addr_type;
1551 uint8_t *base_p = const_cast<uint8_t *>(this->base());
1552 Elf_Shdr *sectionTable =
1553 reinterpret_cast<Elf_Shdr *>(base_p + Header->e_shoff);
1554 uint64_t numSections = this->getNumSections();
1556 // Allocate memory space for NOBITS sections (such as .bss), which only exist
1557 // in memory, but don't occupy space in the object file.
1558 // Update the address in the section headers to reflect this allocation.
1559 for (uint64_t index = 0; index < numSections; index++) {
1560 Elf_Shdr *sec = reinterpret_cast<Elf_Shdr *>(
1561 reinterpret_cast<char *>(sectionTable) + index * Header->e_shentsize);
1563 // Only update sections that are meant to be present in program memory
1564 if (sec->sh_flags & ELF::SHF_ALLOC) {
1565 uint8_t *addr = base_p + sec->sh_offset;
1566 if (sec->sh_type == ELF::SHT_NOBITS) {
1567 addr = static_cast<uint8_t *>(calloc(sec->sh_size, 1));
1568 saveAddress(MemoryMap, addr);
1571 // FIXME: Currently memory with RWX permissions is allocated. In the
1572 // future, make sure that permissions are as necessary
1573 if (sec->sh_flags & ELF::SHF_WRITE) {
1576 if (sec->sh_flags & ELF::SHF_EXECINSTR) {
1580 assert(sizeof(addr_type) == sizeof(intptr_t) &&
1581 "Cross-architecture ELF dy-load is not supported!");
1582 sec->sh_addr = static_cast<addr_type>(intptr_t(addr));
1586 // Now allocate actual space for COMMON symbols, which also don't occupy
1587 // space in the object file.
1588 // We want to allocate space for all COMMON symbols at once, so the flow is:
1589 // 1. Go over all symbols, find those that are in COMMON. For each such
1590 // symbol, record its size and the value field in its symbol header in a
1592 // 2. Allocate memory for all COMMON symbols in one fell swoop.
1593 // 3. Using the recorded information from (1), update the address fields in
1594 // the symbol headers of the COMMON symbols to reflect their allocated
1596 uint64_t TotalSize = 0;
1597 std::vector<std::pair<Elf_Addr *, uint64_t> > SymbAddrInfo;
1598 error_code ec = object_error::success;
1599 for (symbol_iterator si = this->begin_symbols(),
1600 se = this->end_symbols(); si != se; si.increment(ec)) {
1602 ec = si->getSize(Size);
1603 Elf_Sym* symb = const_cast<Elf_Sym*>(
1604 this->getSymbol(si->getRawDataRefImpl()));
1605 if (ec == object_error::success &&
1606 this->getSymbolTableIndex(symb) == ELF::SHN_COMMON && Size > 0) {
1607 SymbAddrInfo.push_back(std::make_pair(&(symb->st_value), Size));
1612 uint8_t* SectionPtr = (uint8_t *)calloc(TotalSize, 1);
1613 saveAddress(MemoryMap, SectionPtr);
1615 typedef typename std::vector<std::pair<Elf_Addr *, uint64_t> >::iterator
1617 AddrInfoIterator EndIter = SymbAddrInfo.end();
1618 for (AddrInfoIterator AddrIter = SymbAddrInfo.begin();
1619 AddrIter != EndIter; ++AddrIter) {
1620 assert(sizeof(addr_type) == sizeof(intptr_t) &&
1621 "Cross-architecture ELF dy-load is not supported!");
1622 *(AddrIter->first) = static_cast<addr_type>(intptr_t(SectionPtr));
1623 SectionPtr += AddrIter->second;
1627 // Record memory addresses for callers
1628 template<support::endianness target_endianness, bool is64Bits>
1629 void DyldELFObject<target_endianness, is64Bits>::saveAddress(
1630 std::vector<uint8_t*> *MemoryMap, uint8_t* addr) {
1632 MemoryMap->push_back(addr);
1634 errs() << "WARNING: Memory leak - cannot record memory for ELF dyld.";
1637 template<support::endianness target_endianness, bool is64Bits>
1638 error_code DyldELFObject<target_endianness, is64Bits>::getSymbolAddress(
1639 DataRefImpl Symb, uint64_t &Result) const {
1640 this->validateSymbol(Symb);
1641 const Elf_Sym *symb = this->getSymbol(Symb);
1642 if (this->getSymbolTableIndex(symb) == ELF::SHN_COMMON) {
1643 Result = symb->st_value;
1644 return object_error::success;
1647 return ELFObjectFile<target_endianness, is64Bits>::getSymbolAddress(
1654 // Creates an in-memory object-file by default: createELFObjectFile(Buffer)
1655 // Set doDyld to true to create a live (executable/debug-worthy) image
1656 // If doDyld is true, any memory allocated for non-resident sections and
1657 // symbols is recorded in MemoryMap.
1658 ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object,
1659 bool doDyld, std::vector<uint8_t *> *MemoryMap) {
1660 std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
1664 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
1665 return new DyldELFObject<support::little, false>(Object, MemoryMap, ec);
1666 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
1667 return new DyldELFObject<support::big, false>(Object, MemoryMap, ec);
1668 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
1669 return new DyldELFObject<support::big, true>(Object, MemoryMap, ec);
1670 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
1671 DyldELFObject<support::little, true> *result =
1672 new DyldELFObject<support::little, true>(Object, MemoryMap, ec);
1674 // Unit testing for type inquiry
1675 bool isBinary = isa<Binary>(result);
1676 bool isDyld = isa<DyldELFObject<support::little, true> >(result);
1677 bool isFile = isa<ELFObjectFile<support::little, true> >(result);
1678 assert(isBinary && isDyld && isFile &&
1679 "Type inquiry failed for ELF object!");
1684 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
1685 return new ELFObjectFile<support::little, false>(Object, ec);
1686 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
1687 return new ELFObjectFile<support::big, false>(Object, ec);
1688 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
1689 return new ELFObjectFile<support::big, true>(Object, ec);
1690 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
1691 ELFObjectFile<support::little, true> *result =
1692 new ELFObjectFile<support::little, true>(Object, ec);
1694 // Unit testing for type inquiry
1695 bool isBinary = isa<Binary>(result);
1696 bool isDyld = isa<DyldELFObject<support::little, true> >(result);
1697 bool isFile = isa<ELFObjectFile<support::little, true> >(result);
1698 assert(isBinary && isFile && !isDyld &&
1699 "Type inquiry failed for ELF object!");
1703 report_fatal_error("Buffer is not an ELF object file!");
1706 } // end namespace llvm