llvm.org GIT mirror llvm / f4eff4b
Expose the ELFObjectFile class directly in the Object/ELF.h header, similarly to what's done for MachO and COFF. This allows advanced uses of the class to be implemented outside the Object library. In particular, the DyldELFObject subclass is now moved into its logical home - ExecutionEngine/RuntimeDyld. This patch was reviewed by Michael Spencer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@150327 91177308-0d34-0410-b5e6-96231b3b80d8 Eli Bendersky 8 years ago
4 changed file(s) with 3385 addition(s) and 1698 deletion(s). Raw diff Collapse all Expand all
0 //===- ELF.h - ELF object file implementation -------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the ELFObjectFile template class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_OBJECT_ELF_H
14 #define LLVM_OBJECT_ELF_H
15
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/Casting.h"
22 #include "llvm/Support/ELF.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include
28 #include
29 #include
30
31 namespace llvm {
32 namespace object {
33
34 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
35 template
36 struct ELFDataTypeTypedefHelperCommon {
37 typedef support::detail::packed_endian_specific_integral
38 Elf_Half;
39 typedef support::detail::packed_endian_specific_integral
40 Elf_Word;
41 typedef support::detail::packed_endian_specific_integral
42 Elf_Sword;
43 typedef support::detail::packed_endian_specific_integral
44 Elf_Xword;
45 typedef support::detail::packed_endian_specific_integral
46 Elf_Sxword;
47 };
48
49 template
50 struct ELFDataTypeTypedefHelper;
51
52 /// ELF 32bit types.
53 template
54 struct ELFDataTypeTypedefHelper
55 : ELFDataTypeTypedefHelperCommon {
56 typedef uint32_t value_type;
57 typedef support::detail::packed_endian_specific_integral
58 Elf_Addr;
59 typedef support::detail::packed_endian_specific_integral
60 Elf_Off;
61 };
62
63 /// ELF 64bit types.
64 template
65 struct ELFDataTypeTypedefHelper
66 : ELFDataTypeTypedefHelperCommon{
67 typedef uint64_t value_type;
68 typedef support::detail::packed_endian_specific_integral
69 Elf_Addr;
70 typedef support::detail::packed_endian_specific_integral
71 Elf_Off;
72 };
73
74 // I really don't like doing this, but the alternative is copypasta.
75 #define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \
76 typedef typename \
77 ELFDataTypeTypedefHelper::Elf_Addr Elf_Addr; \
78 typedef typename \
79 ELFDataTypeTypedefHelper::Elf_Off Elf_Off; \
80 typedef typename \
81 ELFDataTypeTypedefHelper::Elf_Half Elf_Half; \
82 typedef typename \
83 ELFDataTypeTypedefHelper::Elf_Word Elf_Word; \
84 typedef typename \
85 ELFDataTypeTypedefHelper::Elf_Sword Elf_Sword; \
86 typedef typename \
87 ELFDataTypeTypedefHelper::Elf_Xword Elf_Xword; \
88 typedef typename \
89 ELFDataTypeTypedefHelper::Elf_Sxword Elf_Sxword;
90
91 // Section header.
92 template
93 struct Elf_Shdr_Base;
94
95 template
96 struct Elf_Shdr_Base {
97 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
98 Elf_Word sh_name; // Section name (index into string table)
99 Elf_Word sh_type; // Section type (SHT_*)
100 Elf_Word sh_flags; // Section flags (SHF_*)
101 Elf_Addr sh_addr; // Address where section is to be loaded
102 Elf_Off sh_offset; // File offset of section data, in bytes
103 Elf_Word sh_size; // Size of section, in bytes
104 Elf_Word sh_link; // Section type-specific header table index link
105 Elf_Word sh_info; // Section type-specific extra information
106 Elf_Word sh_addralign;// Section address alignment
107 Elf_Word sh_entsize; // Size of records contained within the section
108 };
109
110 template
111 struct Elf_Shdr_Base {
112 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
113 Elf_Word sh_name; // Section name (index into string table)
114 Elf_Word sh_type; // Section type (SHT_*)
115 Elf_Xword sh_flags; // Section flags (SHF_*)
116 Elf_Addr sh_addr; // Address where section is to be loaded
117 Elf_Off sh_offset; // File offset of section data, in bytes
118 Elf_Xword sh_size; // Size of section, in bytes
119 Elf_Word sh_link; // Section type-specific header table index link
120 Elf_Word sh_info; // Section type-specific extra information
121 Elf_Xword sh_addralign;// Section address alignment
122 Elf_Xword sh_entsize; // Size of records contained within the section
123 };
124
125 template
126 struct Elf_Shdr_Impl : Elf_Shdr_Base {
127 using Elf_Shdr_Base::sh_entsize;
128 using Elf_Shdr_Base::sh_size;
129
130 /// @brief Get the number of entities this section contains if it has any.
131 unsigned getEntityCount() const {
132 if (sh_entsize == 0)
133 return 0;
134 return sh_size / sh_entsize;
135 }
136 };
137
138 template
139 struct Elf_Sym_Base;
140
141 template
142 struct Elf_Sym_Base {
143 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
144 Elf_Word st_name; // Symbol name (index into string table)
145 Elf_Addr st_value; // Value or address associated with the symbol
146 Elf_Word st_size; // Size of the symbol
147 unsigned char st_info; // Symbol's type and binding attributes
148 unsigned char st_other; // Must be zero; reserved
149 Elf_Half st_shndx; // Which section (header table index) it's defined in
150 };
151
152 template
153 struct Elf_Sym_Base {
154 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
155 Elf_Word st_name; // Symbol name (index into string table)
156 unsigned char st_info; // Symbol's type and binding attributes
157 unsigned char st_other; // Must be zero; reserved
158 Elf_Half st_shndx; // Which section (header table index) it's defined in
159 Elf_Addr st_value; // Value or address associated with the symbol
160 Elf_Xword st_size; // Size of the symbol
161 };
162
163 template
164 struct Elf_Sym_Impl : Elf_Sym_Base {
165 using Elf_Sym_Base::st_info;
166
167 // These accessors and mutators correspond to the ELF32_ST_BIND,
168 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
169 unsigned char getBinding() const { return st_info >> 4; }
170 unsigned char getType() const { return st_info & 0x0f; }
171 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
172 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
173 void setBindingAndType(unsigned char b, unsigned char t) {
174 st_info = (b << 4) + (t & 0x0f);
175 }
176 };
177
178 template
179 struct Elf_Rel_Base;
180
181 template
182 struct Elf_Rel_Base {
183 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
184 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
185 Elf_Word r_info; // Symbol table index and type of relocation to apply
186 };
187
188 template
189 struct Elf_Rel_Base {
190 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
191 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
192 Elf_Xword r_info; // Symbol table index and type of relocation to apply
193 };
194
195 template
196 struct Elf_Rel_Base {
197 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
198 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
199 Elf_Word r_info; // Symbol table index and type of relocation to apply
200 Elf_Sword r_addend; // Compute value for relocatable field by adding this
201 };
202
203 template
204 struct Elf_Rel_Base {
205 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
206 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
207 Elf_Xword r_info; // Symbol table index and type of relocation to apply
208 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
209 };
210
211 template
212 struct Elf_Rel_Impl;
213
214 template
215 struct Elf_Rel_Impl
216 : Elf_Rel_Base {
217 using Elf_Rel_Base::r_info;
218 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
219
220 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
221 // and ELF64_R_INFO macros defined in the ELF specification:
222 uint64_t getSymbol() const { return (r_info >> 32); }
223 unsigned char getType() const {
224 return (unsigned char) (r_info & 0xffffffffL);
225 }
226 void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); }
227 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
228 void setSymbolAndType(uint64_t s, unsigned char t) {
229 r_info = (s << 32) + (t&0xffffffffL);
230 }
231 };
232
233 template
234 struct Elf_Rel_Impl
235 : Elf_Rel_Base {
236 using Elf_Rel_Base::r_info;
237 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
238
239 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
240 // and ELF32_R_INFO macros defined in the ELF specification:
241 uint32_t getSymbol() const { return (r_info >> 8); }
242 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
243 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
244 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
245 void setSymbolAndType(uint32_t s, unsigned char t) {
246 r_info = (s << 8) + t;
247 }
248 };
249
250
251 template
252 class ELFObjectFile : public ObjectFile {
253 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
254
255 typedef Elf_Shdr_Impl Elf_Shdr;
256 typedef Elf_Sym_Impl Elf_Sym;
257 typedef Elf_Rel_Impl Elf_Rel;
258 typedef Elf_Rel_Impl Elf_Rela;
259
260 protected:
261 struct Elf_Ehdr {
262 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
263 Elf_Half e_type; // Type of file (see ET_*)
264 Elf_Half e_machine; // Required architecture for this file (see EM_*)
265 Elf_Word e_version; // Must be equal to 1
266 Elf_Addr e_entry; // Address to jump to in order to start program
267 Elf_Off e_phoff; // Program header table's file offset, in bytes
268 Elf_Off e_shoff; // Section header table's file offset, in bytes
269 Elf_Word e_flags; // Processor-specific flags
270 Elf_Half e_ehsize; // Size of ELF header, in bytes
271 Elf_Half e_phentsize;// Size of an entry in the program header table
272 Elf_Half e_phnum; // Number of entries in the program header table
273 Elf_Half e_shentsize;// Size of an entry in the section header table
274 Elf_Half e_shnum; // Number of entries in the section header table
275 Elf_Half e_shstrndx; // Section header table index of section name
276 // string table
277 bool checkMagic() const {
278 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
279 }
280 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
281 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
282 };
283 // This flag is used for classof, to distinguish ELFObjectFile from
284 // its subclass. If more subclasses will be created, this flag will
285 // have to become an enum.
286 bool isDyldELFObject;
287
288 private:
289 typedef SmallVector Sections_t;
290 typedef DenseMap IndexMap_t;
291 typedef DenseMap > RelocMap_t;
292
293 const Elf_Ehdr *Header;
294 const Elf_Shdr *SectionHeaderTable;
295 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
296 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
297 Sections_t SymbolTableSections;
298 IndexMap_t SymbolTableSectionsIndexMap;
299 DenseMap ExtendedSymbolTable;
300
301 /// @brief Map sections to an array of relocation sections that reference
302 /// them sorted by section index.
303 RelocMap_t SectionRelocMap;
304
305 /// @brief Get the relocation section that contains \a Rel.
306 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
307 return getSection(Rel.w.b);
308 }
309
310 bool isRelocationHasAddend(DataRefImpl Rel) const;
311 template
312 const T *getEntry(uint16_t Section, uint32_t Entry) const;
313 template
314 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
315 const Elf_Shdr *getSection(DataRefImpl index) const;
316 const Elf_Shdr *getSection(uint32_t index) const;
317 const Elf_Rel *getRel(DataRefImpl Rel) const;
318 const Elf_Rela *getRela(DataRefImpl Rela) const;
319 const char *getString(uint32_t section, uint32_t offset) const;
320 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
321 error_code getSymbolName(const Elf_Sym *Symb, StringRef &Res) const;
322
323 protected:
324 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
325 void validateSymbol(DataRefImpl Symb) const;
326
327 protected:
328 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
329 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
330 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
331 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
332 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
333 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
334 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
335 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const;
336 virtual error_code isSymbolWeak(DataRefImpl Symb, bool &Res) const;
337 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
338 virtual error_code isSymbolAbsolute(DataRefImpl Symb, bool &Res) const;
339 virtual error_code getSymbolSection(DataRefImpl Symb,
340 section_iterator &Res) const;
341
342 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
343 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
344 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
345 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
346 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
347 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
348 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
349 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
350 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
351 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
352 bool &Result) const;
353 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
354 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
355
356 virtual error_code getRelocationNext(DataRefImpl Rel,
357 RelocationRef &Res) const;
358 virtual error_code getRelocationAddress(DataRefImpl Rel,
359 uint64_t &Res) const;
360 virtual error_code getRelocationOffset(DataRefImpl Rel,
361 uint64_t &Res) const;
362 virtual error_code getRelocationSymbol(DataRefImpl Rel,
363 SymbolRef &Res) const;
364 virtual error_code getRelocationType(DataRefImpl Rel,
365 uint64_t &Res) const;
366 virtual error_code getRelocationTypeName(DataRefImpl Rel,
367 SmallVectorImpl &Result) const;
368 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
369 int64_t &Res) const;
370 virtual error_code getRelocationValueString(DataRefImpl Rel,
371 SmallVectorImpl &Result) const;
372
373 public:
374 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
375 virtual symbol_iterator begin_symbols() const;
376 virtual symbol_iterator end_symbols() const;
377 virtual section_iterator begin_sections() const;
378 virtual section_iterator end_sections() const;
379
380 virtual uint8_t getBytesInAddress() const;
381 virtual StringRef getFileFormatName() const;
382 virtual unsigned getArch() const;
383
384 uint64_t getNumSections() const;
385 uint64_t getStringTableIndex() const;
386 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
387 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
388
389 // Methods for type inquiry through isa, cast, and dyn_cast
390 bool isDyldType() const { return isDyldELFObject; }
391 static inline bool classof(const Binary *v) {
392 return v->getType() == Binary::isELF;
393 }
394 static inline bool classof(const ELFObjectFile *v) { return true; }
395 };
396
397 template
398 void ELFObjectFile
399 ::validateSymbol(DataRefImpl Symb) const {
400 const Elf_Sym *symb = getSymbol(Symb);
401 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
402 // FIXME: We really need to do proper error handling in the case of an invalid
403 // input file. Because we don't use exceptions, I think we'll just pass
404 // an error object around.
405 if (!( symb
406 && SymbolTableSection
407 && symb >= (const Elf_Sym*)(base()
408 + SymbolTableSection->sh_offset)
409 && symb < (const Elf_Sym*)(base()
410 + SymbolTableSection->sh_offset
411 + SymbolTableSection->sh_size)))
412 // FIXME: Proper error handling.
413 report_fatal_error("Symb must point to a valid symbol!");
414 }
415
416 template
417 error_code ELFObjectFile
418 ::getSymbolNext(DataRefImpl Symb,
419 SymbolRef &Result) const {
420 validateSymbol(Symb);
421 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
422
423 ++Symb.d.a;
424 // Check to see if we are at the end of this symbol table.
425 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
426 // We are at the end. If there are other symbol tables, jump to them.
427 ++Symb.d.b;
428 Symb.d.a = 1; // The 0th symbol in ELF is fake.
429 // Otherwise return the terminator.
430 if (Symb.d.b >= SymbolTableSections.size()) {
431 Symb.d.a = std::numeric_limits::max();
432 Symb.d.b = std::numeric_limits::max();
433 }
434 }
435
436 Result = SymbolRef(Symb, this);
437 return object_error::success;
438 }
439
440 template
441 error_code ELFObjectFile
442 ::getSymbolName(DataRefImpl Symb,
443 StringRef &Result) const {
444 validateSymbol(Symb);
445 const Elf_Sym *symb = getSymbol(Symb);
446 return getSymbolName(symb, Result);
447 }
448
449 template
450 ELF::Elf64_Word ELFObjectFile
451 ::getSymbolTableIndex(const Elf_Sym *symb) const {
452 if (symb->st_shndx == ELF::SHN_XINDEX)
453 return ExtendedSymbolTable.lookup(symb);
454 return symb->st_shndx;
455 }
456
457 template
458 const typename ELFObjectFile::Elf_Shdr *
459 ELFObjectFile
460 ::getSection(const Elf_Sym *symb) const {
461 if (symb->st_shndx == ELF::SHN_XINDEX)
462 return getSection(ExtendedSymbolTable.lookup(symb));
463 if (symb->st_shndx >= ELF::SHN_LORESERVE)
464 return 0;
465 return getSection(symb->st_shndx);
466 }
467
468 template
469 error_code ELFObjectFile
470 ::getSymbolFileOffset(DataRefImpl Symb,
471 uint64_t &Result) const {
472 validateSymbol(Symb);
473 const Elf_Sym *symb = getSymbol(Symb);
474 const Elf_Shdr *Section;
475 switch (getSymbolTableIndex(symb)) {
476 case ELF::SHN_COMMON:
477 // Unintialized symbols have no offset in the object file
478 case ELF::SHN_UNDEF:
479 Result = UnknownAddressOrSize;
480 return object_error::success;
481 case ELF::SHN_ABS:
482 Result = symb->st_value;
483 return object_error::success;
484 default: Section = getSection(symb);
485 }
486
487 switch (symb->getType()) {
488 case ELF::STT_SECTION:
489 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
490 return object_error::success;
491 case ELF::STT_FUNC:
492 case ELF::STT_OBJECT:
493 case ELF::STT_NOTYPE:
494 Result = symb->st_value +
495 (Section ? Section->sh_offset : 0);
496 return object_error::success;
497 default:
498 Result = UnknownAddressOrSize;
499 return object_error::success;
500 }
501 }
502
503 template
504 error_code ELFObjectFile
505 ::getSymbolAddress(DataRefImpl Symb,
506 uint64_t &Result) const {
507 validateSymbol(Symb);
508 const Elf_Sym *symb = getSymbol(Symb);
509 const Elf_Shdr *Section;
510 switch (getSymbolTableIndex(symb)) {
511 case ELF::SHN_COMMON:
512 case ELF::SHN_UNDEF:
513 Result = UnknownAddressOrSize;
514 return object_error::success;
515 case ELF::SHN_ABS:
516 Result = symb->st_value;
517 return object_error::success;
518 default: Section = getSection(symb);
519 }
520
521 switch (symb->getType()) {
522 case ELF::STT_SECTION:
523 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
524 return object_error::success;
525 case ELF::STT_FUNC:
526 case ELF::STT_OBJECT:
527 case ELF::STT_NOTYPE:
528 Result = symb->st_value + (Section ? Section->sh_addr : 0);
529 return object_error::success;
530 default:
531 Result = UnknownAddressOrSize;
532 return object_error::success;
533 }
534 }
535
536 template
537 error_code ELFObjectFile
538 ::getSymbolSize(DataRefImpl Symb,
539 uint64_t &Result) const {
540 validateSymbol(Symb);
541 const Elf_Sym *symb = getSymbol(Symb);
542 if (symb->st_size == 0)
543 Result = UnknownAddressOrSize;
544 Result = symb->st_size;
545 return object_error::success;
546 }
547
548 template
549 error_code ELFObjectFile
550 ::getSymbolNMTypeChar(DataRefImpl Symb,
551 char &Result) const {
552 validateSymbol(Symb);
553 const Elf_Sym *symb = getSymbol(Symb);
554 const Elf_Shdr *Section = getSection(symb);
555
556 char ret = '?';
557
558 if (Section) {
559 switch (Section->sh_type) {
560 case ELF::SHT_PROGBITS:
561 case ELF::SHT_DYNAMIC:
562 switch (Section->sh_flags) {
563 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
564 ret = 't'; break;
565 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
566 ret = 'd'; break;
567 case ELF::SHF_ALLOC:
568 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
569 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
570 ret = 'r'; break;
571 }
572 break;
573 case ELF::SHT_NOBITS: ret = 'b';
574 }
575 }
576
577 switch (getSymbolTableIndex(symb)) {
578 case ELF::SHN_UNDEF:
579 if (ret == '?')
580 ret = 'U';
581 break;
582 case ELF::SHN_ABS: ret = 'a'; break;
583 case ELF::SHN_COMMON: ret = 'c'; break;
584 }
585
586 switch (symb->getBinding()) {
587 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
588 case ELF::STB_WEAK:
589 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
590 ret = 'w';
591 else
592 if (symb->getType() == ELF::STT_OBJECT)
593 ret = 'V';
594 else
595 ret = 'W';
596 }
597
598 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
599 StringRef name;
600 if (error_code ec = getSymbolName(Symb, name))
601 return ec;
602 Result = StringSwitch(name)
603 .StartsWith(".debug", 'N')
604 .StartsWith(".note", 'n')
605 .Default('?');
606 return object_error::success;
607 }
608
609 Result = ret;
610 return object_error::success;
611 }
612
613 template
614 error_code ELFObjectFile
615 ::getSymbolType(DataRefImpl Symb,
616 SymbolRef::Type &Result) const {
617 validateSymbol(Symb);
618 const Elf_Sym *symb = getSymbol(Symb);
619
620 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) {
621 Result = SymbolRef::ST_External;
622 return object_error::success;
623 }
624
625 switch (symb->getType()) {
626 case ELF::STT_SECTION:
627 Result = SymbolRef::ST_Debug;
628 break;
629 case ELF::STT_FILE:
630 Result = SymbolRef::ST_File;
631 break;
632 case ELF::STT_FUNC:
633 Result = SymbolRef::ST_Function;
634 break;
635 case ELF::STT_OBJECT:
636 Result = SymbolRef::ST_Data;
637 break;
638 default:
639 Result = SymbolRef::ST_Other;
640 break;
641 }
642 return object_error::success;
643 }
644
645 template
646 error_code ELFObjectFile
647 ::isSymbolGlobal(DataRefImpl Symb,
648 bool &Result) const {
649 validateSymbol(Symb);
650 const Elf_Sym *symb = getSymbol(Symb);
651
652 Result = symb->getBinding() == ELF::STB_GLOBAL;
653 return object_error::success;
654 }
655
656 template
657 error_code ELFObjectFile
658 ::isSymbolWeak(DataRefImpl Symb,
659 bool &Result) const {
660 validateSymbol(Symb);
661 const Elf_Sym *symb = getSymbol(Symb);
662
663 Result = symb->getBinding() == ELF::STB_WEAK;
664 return object_error::success;
665 }
666
667 template
668 error_code ELFObjectFile
669 ::isSymbolAbsolute(DataRefImpl Symb, bool &Res) const {
670 validateSymbol(Symb);
671 const Elf_Sym *symb = getSymbol(Symb);
672 Res = symb->st_shndx == ELF::SHN_ABS;
673 return object_error::success;
674 }
675
676 template
677 error_code ELFObjectFile
678 ::getSymbolSection(DataRefImpl Symb,
679 section_iterator &Res) const {
680 validateSymbol(Symb);
681 const Elf_Sym *symb = getSymbol(Symb);
682 const Elf_Shdr *sec = getSection(symb);
683 if (!sec)
684 Res = end_sections();
685 else {
686 DataRefImpl Sec;
687 Sec.p = reinterpret_cast(sec);
688 Res = section_iterator(SectionRef(Sec, this));
689 }
690 return object_error::success;
691 }
692
693 template
694 error_code ELFObjectFile
695 ::isSymbolInternal(DataRefImpl Symb,
696 bool &Result) const {
697 validateSymbol(Symb);
698 const Elf_Sym *symb = getSymbol(Symb);
699
700 if ( symb->getType() == ELF::STT_FILE
701 || symb->getType() == ELF::STT_SECTION)
702 Result = true;
703 Result = false;
704 return object_error::success;
705 }
706
707 template
708 error_code ELFObjectFile
709 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
710 const uint8_t *sec = reinterpret_cast(Sec.p);
711 sec += Header->e_shentsize;
712 Sec.p = reinterpret_cast(sec);
713 Result = SectionRef(Sec, this);
714 return object_error::success;
715 }
716
717 template
718 error_code ELFObjectFile
719 ::getSectionName(DataRefImpl Sec,
720 StringRef &Result) const {
721 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
722 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
723 return object_error::success;
724 }
725
726 template
727 error_code ELFObjectFile
728 ::getSectionAddress(DataRefImpl Sec,
729 uint64_t &Result) const {
730 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
731 Result = sec->sh_addr;
732 return object_error::success;
733 }
734
735 template
736 error_code ELFObjectFile
737 ::getSectionSize(DataRefImpl Sec,
738 uint64_t &Result) const {
739 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
740 Result = sec->sh_size;
741 return object_error::success;
742 }
743
744 template
745 error_code ELFObjectFile
746 ::getSectionContents(DataRefImpl Sec,
747 StringRef &Result) const {
748 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
749 const char *start = (const char*)base() + sec->sh_offset;
750 Result = StringRef(start, sec->sh_size);
751 return object_error::success;
752 }
753
754 template
755 error_code ELFObjectFile
756 ::getSectionAlignment(DataRefImpl Sec,
757 uint64_t &Result) const {
758 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
759 Result = sec->sh_addralign;
760 return object_error::success;
761 }
762
763 template
764 error_code ELFObjectFile
765 ::isSectionText(DataRefImpl Sec,
766 bool &Result) const {
767 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
768 if (sec->sh_flags & ELF::SHF_EXECINSTR)
769 Result = true;
770 else
771 Result = false;
772 return object_error::success;
773 }
774
775 template
776 error_code ELFObjectFile
777 ::isSectionData(DataRefImpl Sec,
778 bool &Result) const {
779 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
780 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
781 && sec->sh_type == ELF::SHT_PROGBITS)
782 Result = true;
783 else
784 Result = false;
785 return object_error::success;
786 }
787
788 template
789 error_code ELFObjectFile
790 ::isSectionBSS(DataRefImpl Sec,
791 bool &Result) const {
792 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
793 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
794 && sec->sh_type == ELF::SHT_NOBITS)
795 Result = true;
796 else
797 Result = false;
798 return object_error::success;
799 }
800
801 template
802 error_code ELFObjectFile
803 ::sectionContainsSymbol(DataRefImpl Sec,
804 DataRefImpl Symb,
805 bool &Result) const {
806 // FIXME: Unimplemented.
807 Result = false;
808 return object_error::success;
809 }
810
811 template
812 relocation_iterator ELFObjectFile
813 ::getSectionRelBegin(DataRefImpl Sec) const {
814 DataRefImpl RelData;
815 memset(&RelData, 0, sizeof(RelData));
816 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
817 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
818 if (sec != 0 && ittr != SectionRelocMap.end()) {
819 RelData.w.a = getSection(ittr->second[0])->sh_info;
820 RelData.w.b = ittr->second[0];
821 RelData.w.c = 0;
822 }
823 return relocation_iterator(RelocationRef(RelData, this));
824 }
825
826 template
827 relocation_iterator ELFObjectFile
828 ::getSectionRelEnd(DataRefImpl Sec) const {
829 DataRefImpl RelData;
830 memset(&RelData, 0, sizeof(RelData));
831 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
832 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
833 if (sec != 0 && ittr != SectionRelocMap.end()) {
834 // Get the index of the last relocation section for this section.
835 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
836 const Elf_Shdr *relocsec = getSection(relocsecindex);
837 RelData.w.a = relocsec->sh_info;
838 RelData.w.b = relocsecindex;
839 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
840 }
841 return relocation_iterator(RelocationRef(RelData, this));
842 }
843
844 // Relocations
845 template
846 error_code ELFObjectFile
847 ::getRelocationNext(DataRefImpl Rel,
848 RelocationRef &Result) const {
849 ++Rel.w.c;
850 const Elf_Shdr *relocsec = getSection(Rel.w.b);
851 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
852 // We have reached the end of the relocations for this section. See if there
853 // is another relocation section.
854 typename RelocMap_t::mapped_type relocseclist =
855 SectionRelocMap.lookup(getSection(Rel.w.a));
856
857 // Do a binary search for the current reloc section index (which must be
858 // present). Then get the next one.
859 typename RelocMap_t::mapped_type::const_iterator loc =
860 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
861 ++loc;
862
863 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
864 // to the end iterator.
865 if (loc != relocseclist.end()) {
866 Rel.w.b = *loc;
867 Rel.w.a = 0;
868 }
869 }
870 Result = RelocationRef(Rel, this);
871 return object_error::success;
872 }
873
874 template
875 error_code ELFObjectFile
876 ::getRelocationSymbol(DataRefImpl Rel,
877 SymbolRef &Result) const {
878 uint32_t symbolIdx;
879 const Elf_Shdr *sec = getSection(Rel.w.b);
880 switch (sec->sh_type) {
881 default :
882 report_fatal_error("Invalid section type in Rel!");
883 case ELF::SHT_REL : {
884 symbolIdx = getRel(Rel)->getSymbol();
885 break;
886 }
887 case ELF::SHT_RELA : {
888 symbolIdx = getRela(Rel)->getSymbol();
889 break;
890 }
891 }
892 DataRefImpl SymbolData;
893 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
894 if (it == SymbolTableSectionsIndexMap.end())
895 report_fatal_error("Relocation symbol table not found!");
896 SymbolData.d.a = symbolIdx;
897 SymbolData.d.b = it->second;
898 Result = SymbolRef(SymbolData, this);
899 return object_error::success;
900 }
901
902 template
903 error_code ELFObjectFile
904 ::getRelocationAddress(DataRefImpl Rel,
905 uint64_t &Result) const {
906 uint64_t offset;
907 const Elf_Shdr *sec = getSection(Rel.w.b);
908 switch (sec->sh_type) {
909 default :
910 report_fatal_error("Invalid section type in Rel!");
911 case ELF::SHT_REL : {
912 offset = getRel(Rel)->r_offset;
913 break;
914 }
915 case ELF::SHT_RELA : {
916 offset = getRela(Rel)->r_offset;
917 break;
918 }
919 }
920
921 Result = offset;
922 return object_error::success;
923 }
924
925 template
926 error_code ELFObjectFile
927 ::getRelocationOffset(DataRefImpl Rel,
928 uint64_t &Result) const {
929 uint64_t offset;
930 const Elf_Shdr *sec = getSection(Rel.w.b);
931 switch (sec->sh_type) {
932 default :
933 report_fatal_error("Invalid section type in Rel!");
934 case ELF::SHT_REL : {
935 offset = getRel(Rel)->r_offset;
936 break;
937 }
938 case ELF::SHT_RELA : {
939 offset = getRela(Rel)->r_offset;
940 break;
941 }
942 }
943
944 Result = offset - sec->sh_addr;
945 return object_error::success;
946 }
947
948 template
949 error_code ELFObjectFile
950 ::getRelocationType(DataRefImpl Rel,
951 uint64_t &Result) const {
952 const Elf_Shdr *sec = getSection(Rel.w.b);
953 switch (sec->sh_type) {
954 default :
955 report_fatal_error("Invalid section type in Rel!");
956 case ELF::SHT_REL : {
957 Result = getRel(Rel)->getType();
958 break;
959 }
960 case ELF::SHT_RELA : {
961 Result = getRela(Rel)->getType();
962 break;
963 }
964 }
965 return object_error::success;
966 }
967
968 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
969 case ELF::enum: res = #enum; break;
970
971 template
972 error_code ELFObjectFile
973 ::getRelocationTypeName(DataRefImpl Rel,
974 SmallVectorImpl &Result) const {
975 const Elf_Shdr *sec = getSection(Rel.w.b);
976 uint8_t type;
977 StringRef res;
978 switch (sec->sh_type) {
979 default :
980 return object_error::parse_failed;
981 case ELF::SHT_REL : {
982 type = getRel(Rel)->getType();
983 break;
984 }
985 case ELF::SHT_RELA : {
986 type = getRela(Rel)->getType();
987 break;
988 }
989 }
990 switch (Header->e_machine) {
991 case ELF::EM_X86_64:
992 switch (type) {
993 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
994 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
995 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
996 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
997 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
998 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
999 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
1000 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
1001 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
1002 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
1003 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
1004 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
1005 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
1006 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
1007 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
1008 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
1009 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
1010 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
1011 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
1012 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
1013 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
1014 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
1015 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
1016 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
1017 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
1018 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
1019 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
1020 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
1021 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
1022 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
1023 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
1024 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
1025 default:
1026 res = "Unknown";
1027 }
1028 break;
1029 case ELF::EM_386:
1030 switch (type) {
1031 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
1032 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
1033 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
1034 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
1035 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
1036 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
1037 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
1038 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
1039 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
1040 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
1041 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
1042 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
1043 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
1044 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
1045 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
1046 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
1047 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
1048 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
1049 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
1050 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
1051 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
1052 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
1053 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
1054 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
1055 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
1056 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
1057 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
1058 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
1059 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
1060 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
1061 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
1062 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
1063 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
1064 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
1065 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
1066 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
1067 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
1068 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
1069 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
1070 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
1071 default:
1072 res = "Unknown";
1073 }
1074 break;
1075 default:
1076 res = "Unknown";
1077 }
1078 Result.append(res.begin(), res.end());
1079 return object_error::success;
1080 }
1081
1082 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
1083
1084 template
1085 error_code ELFObjectFile
1086 ::getRelocationAdditionalInfo(DataRefImpl Rel,
1087 int64_t &Result) const {
1088 const Elf_Shdr *sec = getSection(Rel.w.b);
1089 switch (sec->sh_type) {
1090 default :
1091 report_fatal_error("Invalid section type in Rel!");
1092 case ELF::SHT_REL : {
1093 Result = 0;
1094 return object_error::success;
1095 }
1096 case ELF::SHT_RELA : {
1097 Result = getRela(Rel)->r_addend;
1098 return object_error::success;
1099 }
1100 }
1101 }
1102
1103 template
1104 error_code ELFObjectFile
1105 ::getRelocationValueString(DataRefImpl Rel,
1106 SmallVectorImpl &Result) const {
1107 const Elf_Shdr *sec = getSection(Rel.w.b);
1108 uint8_t type;
1109 StringRef res;
1110 int64_t addend = 0;
1111 uint16_t symbol_index = 0;
1112 switch (sec->sh_type) {
1113 default :
1114 return object_error::parse_failed;
1115 case ELF::SHT_REL : {
1116 type = getRel(Rel)->getType();
1117 symbol_index = getRel(Rel)->getSymbol();
1118 // TODO: Read implicit addend from section data.
1119 break;
1120 }
1121 case ELF::SHT_RELA : {
1122 type = getRela(Rel)->getType();
1123 symbol_index = getRela(Rel)->getSymbol();
1124 addend = getRela(Rel)->r_addend;
1125 break;
1126 }
1127 }
1128 const Elf_Sym *symb = getEntry(sec->sh_link, symbol_index);
1129 StringRef symname;
1130 if (error_code ec = getSymbolName(symb, symname))
1131 return ec;
1132 switch (Header->e_machine) {
1133 case ELF::EM_X86_64:
1134 switch (type) {
1135 case ELF::R_X86_64_32S:
1136 res = symname;
1137 break;
1138 case ELF::R_X86_64_PC32: {
1139 std::string fmtbuf;
1140 raw_string_ostream fmt(fmtbuf);
1141 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1142 fmt.flush();
1143 Result.append(fmtbuf.begin(), fmtbuf.end());
1144 }
1145 break;
1146 default:
1147 res = "Unknown";
1148 }
1149 break;
1150 default:
1151 res = "Unknown";
1152 }
1153 if (Result.empty())
1154 Result.append(res.begin(), res.end());
1155 return object_error::success;
1156 }
1157
1158 template
1159 ELFObjectFile::ELFObjectFile(MemoryBuffer *Object
1160 , error_code &ec)
1161 : ObjectFile(Binary::isELF, Object, ec)
1162 , isDyldELFObject(false)
1163 , SectionHeaderTable(0)
1164 , dot_shstrtab_sec(0)
1165 , dot_strtab_sec(0) {
1166
1167 const uint64_t FileSize = Data->getBufferSize();
1168
1169 if (sizeof(Elf_Ehdr) > FileSize)
1170 // FIXME: Proper error handling.
1171 report_fatal_error("File too short!");
1172
1173 Header = reinterpret_cast(base());
1174
1175 if (Header->e_shoff == 0)
1176 return;
1177
1178 const uint64_t SectionTableOffset = Header->e_shoff;
1179
1180 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
1181 // FIXME: Proper error handling.
1182 report_fatal_error("Section header table goes past end of file!");
1183
1184 // The getNumSections() call below depends on SectionHeaderTable being set.
1185 SectionHeaderTable =
1186 reinterpret_cast(base() + SectionTableOffset);
1187 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
1188
1189 if (SectionTableOffset + SectionTableSize > FileSize)
1190 // FIXME: Proper error handling.
1191 report_fatal_error("Section table goes past end of file!");
1192
1193 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
1194 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
1195 const Elf_Shdr* sh = SectionHeaderTable;
1196 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
1197 if (sh->sh_type == ELF::SHT_SYMTAB_SHNDX) {
1198 if (SymbolTableSectionHeaderIndex)
1199 // FIXME: Proper error handling.
1200 report_fatal_error("More than one .symtab_shndx!");
1201 SymbolTableSectionHeaderIndex = sh;
1202 }
1203 if (sh->sh_type == ELF::SHT_SYMTAB) {
1204 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
1205 SymbolTableSections.push_back(sh);
1206 }
1207 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
1208 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
1209 }
1210 ++sh;
1211 }
1212
1213 // Sort section relocation lists by index.
1214 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1215 e = SectionRelocMap.end(); i != e; ++i) {
1216 std::sort(i->second.begin(), i->second.end());
1217 }
1218
1219 // Get string table sections.
1220 dot_shstrtab_sec = getSection(getStringTableIndex());
1221 if (dot_shstrtab_sec) {
1222 // Verify that the last byte in the string table in a null.
1223 if (((const char*)base() + dot_shstrtab_sec->sh_offset)
1224 [dot_shstrtab_sec->sh_size - 1] != 0)
1225 // FIXME: Proper error handling.
1226 report_fatal_error("String table must end with a null terminator!");
1227 }
1228
1229 // Merge this into the above loop.
1230 for (const char *i = reinterpret_cast(SectionHeaderTable),
1231 *e = i + getNumSections() * Header->e_shentsize;
1232 i != e; i += Header->e_shentsize) {
1233 const Elf_Shdr *sh = reinterpret_cast(i);
1234 if (sh->sh_type == ELF::SHT_STRTAB) {
1235 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
1236 if (SectionName == ".strtab") {
1237 if (dot_strtab_sec != 0)
1238 // FIXME: Proper error handling.
1239 report_fatal_error("Already found section named .strtab!");
1240 dot_strtab_sec = sh;
1241 const char *dot_strtab = (const char*)base() + sh->sh_offset;
1242 if (dot_strtab[sh->sh_size - 1] != 0)
1243 // FIXME: Proper error handling.
1244 report_fatal_error("String table must end with a null terminator!");
1245 }
1246 }
1247 }
1248
1249 // Build symbol name side-mapping if there is one.
1250 if (SymbolTableSectionHeaderIndex) {
1251 const Elf_Word *ShndxTable = reinterpret_cast(base() +
1252 SymbolTableSectionHeaderIndex->sh_offset);
1253 error_code ec;
1254 for (symbol_iterator si = begin_symbols(),
1255 se = end_symbols(); si != se; si.increment(ec)) {
1256 if (ec)
1257 report_fatal_error("Fewer extended symbol table entries than symbols!");
1258 if (*ShndxTable != ELF::SHN_UNDEF)
1259 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
1260 ++ShndxTable;
1261 }
1262 }
1263 }
1264
1265 template
1266 symbol_iterator ELFObjectFile
1267 ::begin_symbols() const {
1268 DataRefImpl SymbolData;
1269 memset(&SymbolData, 0, sizeof(SymbolData));
1270 if (SymbolTableSections.size() == 0) {
1271 SymbolData.d.a = std::numeric_limits::max();
1272 SymbolData.d.b = std::numeric_limits::max();
1273 } else {
1274 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
1275 SymbolData.d.b = 0;
1276 }
1277 return symbol_iterator(SymbolRef(SymbolData, this));
1278 }
1279
1280 template
1281 symbol_iterator ELFObjectFile
1282 ::end_symbols() const {
1283 DataRefImpl SymbolData;
1284 memset(&SymbolData, 0, sizeof(SymbolData));
1285 SymbolData.d.a = std::numeric_limits::max();
1286 SymbolData.d.b = std::numeric_limits::max();
1287 return symbol_iterator(SymbolRef(SymbolData, this));
1288 }
1289
1290 template
1291 section_iterator ELFObjectFile
1292 ::begin_sections() const {
1293 DataRefImpl ret;
1294 memset(&ret, 0, sizeof(DataRefImpl));
1295 ret.p = reinterpret_cast(base() + Header->e_shoff);
1296 return section_iterator(SectionRef(ret, this));
1297 }
1298
1299 template
1300 section_iterator ELFObjectFile
1301 ::end_sections() const {
1302 DataRefImpl ret;
1303 memset(&ret, 0, sizeof(DataRefImpl));
1304 ret.p = reinterpret_cast(base()
1305 + Header->e_shoff
1306 + (Header->e_shentsize*getNumSections()));
1307 return section_iterator(SectionRef(ret, this));
1308 }
1309
1310 template
1311 uint8_t ELFObjectFile::getBytesInAddress() const {
1312 return is64Bits ? 8 : 4;
1313 }
1314
1315 template
1316 StringRef ELFObjectFile
1317 ::getFileFormatName() const {
1318 switch(Header->e_ident[ELF::EI_CLASS]) {
1319 case ELF::ELFCLASS32:
1320 switch(Header->e_machine) {
1321 case ELF::EM_386:
1322 return "ELF32-i386";
1323 case ELF::EM_X86_64:
1324 return "ELF32-x86-64";
1325 case ELF::EM_ARM:
1326 return "ELF32-arm";
1327 default:
1328 return "ELF32-unknown";
1329 }
1330 case ELF::ELFCLASS64:
1331 switch(Header->e_machine) {
1332 case ELF::EM_386:
1333 return "ELF64-i386";
1334 case ELF::EM_X86_64:
1335 return "ELF64-x86-64";
1336 default:
1337 return "ELF64-unknown";
1338 }
1339 default:
1340 // FIXME: Proper error handling.
1341 report_fatal_error("Invalid ELFCLASS!");
1342 }
1343 }
1344
1345 template
1346 unsigned ELFObjectFile::getArch() const {
1347 switch(Header->e_machine) {
1348 case ELF::EM_386:
1349 return Triple::x86;
1350 case ELF::EM_X86_64:
1351 return Triple::x86_64;
1352 case ELF::EM_ARM:
1353 return Triple::arm;
1354 default:
1355 return Triple::UnknownArch;
1356 }
1357 }
1358
1359 template
1360 uint64_t ELFObjectFile::getNumSections() const {
1361 assert(Header && "Header not initialized!");
1362 if (Header->e_shnum == ELF::SHN_UNDEF) {
1363 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
1364 return SectionHeaderTable->sh_size;
1365 }
1366 return Header->e_shnum;
1367 }
1368
1369 template
1370 uint64_t
1371 ELFObjectFile::getStringTableIndex() const {
1372 if (Header->e_shnum == ELF::SHN_UNDEF) {
1373 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
1374 return SectionHeaderTable->sh_link;
1375 if (Header->e_shstrndx >= getNumSections())
1376 return 0;
1377 }
1378 return Header->e_shstrndx;
1379 }
1380
1381
1382 template
1383 template
1384 inline const T *
1385 ELFObjectFile::getEntry(uint16_t Section,
1386 uint32_t Entry) const {
1387 return getEntry(getSection(Section), Entry);
1388 }
1389
1390 template
1391 template
1392 inline const T *
1393 ELFObjectFile::getEntry(const Elf_Shdr * Section,
1394 uint32_t Entry) const {
1395 return reinterpret_cast(
1396 base()
1397 + Section->sh_offset
1398 + (Entry * Section->sh_entsize));
1399 }
1400
1401 template
1402 const typename ELFObjectFile::Elf_Sym *
1403 ELFObjectFile::getSymbol(DataRefImpl Symb) const {
1404 return getEntry(SymbolTableSections[Symb.d.b], Symb.d.a);
1405 }
1406
1407 template
1408 const typename ELFObjectFile::Elf_Rel *
1409 ELFObjectFile::getRel(DataRefImpl Rel) const {
1410 return getEntry(Rel.w.b, Rel.w.c);
1411 }
1412
1413 template
1414 const typename ELFObjectFile::Elf_Rela *
1415 ELFObjectFile::getRela(DataRefImpl Rela) const {
1416 return getEntry(Rela.w.b, Rela.w.c);
1417 }
1418
1419 template
1420 const typename ELFObjectFile::Elf_Shdr *
1421 ELFObjectFile::getSection(DataRefImpl Symb) const {
1422 const Elf_Shdr *sec = getSection(Symb.d.b);
1423 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
1424 // FIXME: Proper error handling.
1425 report_fatal_error("Invalid symbol table section!");
1426 return sec;
1427 }
1428
1429 template
1430 const typename ELFObjectFile::Elf_Shdr *
1431 ELFObjectFile::getSection(uint32_t index) const {
1432 if (index == 0)
1433 return 0;
1434 if (!SectionHeaderTable || index >= getNumSections())
1435 // FIXME: Proper error handling.
1436 report_fatal_error("Invalid section index!");
1437
1438 return reinterpret_cast(
1439 reinterpret_cast(SectionHeaderTable)
1440 + (index * Header->e_shentsize));
1441 }
1442
1443 template
1444 const char *ELFObjectFile
1445 ::getString(uint32_t section,
1446 ELF::Elf32_Word offset) const {
1447 return getString(getSection(section), offset);
1448 }
1449
1450 template
1451 const char *ELFObjectFile
1452 ::getString(const Elf_Shdr *section,
1453 ELF::Elf32_Word offset) const {
1454 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
1455 if (offset >= section->sh_size)
1456 // FIXME: Proper error handling.
1457 report_fatal_error("Symbol name offset outside of string table!");
1458 return (const char *)base() + section->sh_offset + offset;
1459 }
1460
1461 template
1462 error_code ELFObjectFile
1463 ::getSymbolName(const Elf_Sym *symb,
1464 StringRef &Result) const {
1465 if (symb->st_name == 0) {
1466 const Elf_Shdr *section = getSection(symb);
1467 if (!section)
1468 Result = "";
1469 else
1470 Result = getString(dot_shstrtab_sec, section->sh_name);
1471 return object_error::success;
1472 }
1473
1474 // Use the default symbol table name section.
1475 Result = getString(dot_strtab_sec, symb->st_name);
1476 return object_error::success;
1477 }
1478
1479 }
1480 }
1481
1482 #endif
1483
1484
1485 //===- ELF.h - ELF object file implementation -------------------*- C++ -*-===//
1486 //
1487 // The LLVM Compiler Infrastructure
1488 //
1489 // This file is distributed under the University of Illinois Open Source
1490 // License. See LICENSE.TXT for details.
1491 //
1492 //===----------------------------------------------------------------------===//
1493 //
1494 // This file declares the ELFObjectFile template class.
1495 //
1496 //===----------------------------------------------------------------------===//
1497
1498 #ifndef LLVM_OBJECT_ELF_H
1499 #define LLVM_OBJECT_ELF_H
1500
1501 #include "llvm/ADT/SmallVector.h"
1502 #include "llvm/ADT/StringSwitch.h"
1503 #include "llvm/ADT/Triple.h"
1504 #include "llvm/ADT/DenseMap.h"
1505 #include "llvm/Object/ObjectFile.h"
1506 #include "llvm/Support/Casting.h"
1507 #include "llvm/Support/ELF.h"
1508 #include "llvm/Support/Endian.h"
1509 #include "llvm/Support/ErrorHandling.h"
1510 #include "llvm/Support/MemoryBuffer.h"
1511 #include "llvm/Support/raw_ostream.h"
1512 #include
1513 #include
1514 #include
1515
1516 namespace llvm {
1517 namespace object {
1518
1519 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
1520 template
1521 struct ELFDataTypeTypedefHelperCommon {
1522 typedef support::detail::packed_endian_specific_integral
1523 Elf_Half;
1524 typedef support::detail::packed_endian_specific_integral
1525 Elf_Word;
1526 typedef support::detail::packed_endian_specific_integral
1527 Elf_Sword;
1528 typedef support::detail::packed_endian_specific_integral
1529 Elf_Xword;
1530 typedef support::detail::packed_endian_specific_integral
1531 Elf_Sxword;
1532 };
1533
1534 template
1535 struct ELFDataTypeTypedefHelper;
1536
1537 /// ELF 32bit types.
1538 template
1539 struct ELFDataTypeTypedefHelper
1540 : ELFDataTypeTypedefHelperCommon {
1541 typedef uint32_t value_type;
1542 typedef support::detail::packed_endian_specific_integral
1543 Elf_Addr;
1544 typedef support::detail::packed_endian_specific_integral
1545 Elf_Off;
1546 };
1547
1548 /// ELF 64bit types.
1549 template
1550 struct ELFDataTypeTypedefHelper
1551 : ELFDataTypeTypedefHelperCommon{
1552 typedef uint64_t value_type;
1553 typedef support::detail::packed_endian_specific_integral
1554 Elf_Addr;
1555 typedef support::detail::packed_endian_specific_integral
1556 Elf_Off;
1557 };
1558
1559 // I really don't like doing this, but the alternative is copypasta.
1560 #define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \
1561 typedef typename \
1562 ELFDataTypeTypedefHelper::Elf_Addr Elf_Addr; \
1563 typedef typename \
1564 ELFDataTypeTypedefHelper::Elf_Off Elf_Off; \
1565 typedef typename \
1566 ELFDataTypeTypedefHelper::Elf_Half Elf_Half; \
1567 typedef typename \
1568 ELFDataTypeTypedefHelper::Elf_Word Elf_Word; \
1569 typedef typename \
1570 ELFDataTypeTypedefHelper::Elf_Sword Elf_Sword; \
1571 typedef typename \
1572 ELFDataTypeTypedefHelper::Elf_Xword Elf_Xword; \
1573 typedef typename \
1574 ELFDataTypeTypedefHelper::Elf_Sxword Elf_Sxword;
1575
1576 // Section header.
1577 template
1578 struct Elf_Shdr_Base;
1579
1580 template
1581 struct Elf_Shdr_Base {
1582 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
1583 Elf_Word sh_name; // Section name (index into string table)
1584 Elf_Word sh_type; // Section type (SHT_*)
1585 Elf_Word sh_flags; // Section flags (SHF_*)
1586 Elf_Addr sh_addr; // Address where section is to be loaded
1587 Elf_Off sh_offset; // File offset of section data, in bytes
1588 Elf_Word sh_size; // Size of section, in bytes
1589 Elf_Word sh_link; // Section type-specific header table index link
1590 Elf_Word sh_info; // Section type-specific extra information
1591 Elf_Word sh_addralign;// Section address alignment
1592 Elf_Word sh_entsize; // Size of records contained within the section
1593 };
1594
1595 template
1596 struct Elf_Shdr_Base {
1597 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
1598 Elf_Word sh_name; // Section name (index into string table)
1599 Elf_Word sh_type; // Section type (SHT_*)
1600 Elf_Xword sh_flags; // Section flags (SHF_*)
1601 Elf_Addr sh_addr; // Address where section is to be loaded
1602 Elf_Off sh_offset; // File offset of section data, in bytes
1603 Elf_Xword sh_size; // Size of section, in bytes
1604 Elf_Word sh_link; // Section type-specific header table index link
1605 Elf_Word sh_info; // Section type-specific extra information
1606 Elf_Xword sh_addralign;// Section address alignment
1607 Elf_Xword sh_entsize; // Size of records contained within the section
1608 };
1609
1610 template
1611 struct Elf_Shdr_Impl : Elf_Shdr_Base {
1612 using Elf_Shdr_Base::sh_entsize;
1613 using Elf_Shdr_Base::sh_size;
1614
1615 /// @brief Get the number of entities this section contains if it has any.
1616 unsigned getEntityCount() const {
1617 if (sh_entsize == 0)
1618 return 0;
1619 return sh_size / sh_entsize;
1620 }
1621 };
1622
1623 template
1624 struct Elf_Sym_Base;
1625
1626 template
1627 struct Elf_Sym_Base {
1628 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
1629 Elf_Word st_name; // Symbol name (index into string table)
1630 Elf_Addr st_value; // Value or address associated with the symbol
1631 Elf_Word st_size; // Size of the symbol
1632 unsigned char st_info; // Symbol's type and binding attributes
1633 unsigned char st_other; // Must be zero; reserved
1634 Elf_Half st_shndx; // Which section (header table index) it's defined in
1635 };
1636
1637 template
1638 struct Elf_Sym_Base {
1639 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
1640 Elf_Word st_name; // Symbol name (index into string table)
1641 unsigned char st_info; // Symbol's type and binding attributes
1642 unsigned char st_other; // Must be zero; reserved
1643 Elf_Half st_shndx; // Which section (header table index) it's defined in
1644 Elf_Addr st_value; // Value or address associated with the symbol
1645 Elf_Xword st_size; // Size of the symbol
1646 };
1647
1648 template
1649 struct Elf_Sym_Impl : Elf_Sym_Base {
1650 using Elf_Sym_Base::st_info;
1651
1652 // These accessors and mutators correspond to the ELF32_ST_BIND,
1653 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
1654 unsigned char getBinding() const { return st_info >> 4; }
1655 unsigned char getType() const { return st_info & 0x0f; }
1656 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
1657 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
1658 void setBindingAndType(unsigned char b, unsigned char t) {
1659 st_info = (b << 4) + (t & 0x0f);
1660 }
1661 };
1662
1663 template
1664 struct Elf_Rel_Base;
1665
1666 template
1667 struct Elf_Rel_Base {
1668 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
1669 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
1670 Elf_Word r_info; // Symbol table index and type of relocation to apply
1671 };
1672
1673 template
1674 struct Elf_Rel_Base {
1675 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
1676 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
1677 Elf_Xword r_info; // Symbol table index and type of relocation to apply
1678 };
1679
1680 template
1681 struct Elf_Rel_Base {
1682 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
1683 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
1684 Elf_Word r_info; // Symbol table index and type of relocation to apply
1685 Elf_Sword r_addend; // Compute value for relocatable field by adding this
1686 };
1687
1688 template
1689 struct Elf_Rel_Base {
1690 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
1691 Elf_Addr r_offset; // Location (file byte offset, or program virtual addr)
1692 Elf_Xword r_info; // Symbol table index and type of relocation to apply
1693 Elf_Sxword r_addend; // Compute value for relocatable field by adding this.
1694 };
1695
1696 template
1697 struct Elf_Rel_Impl;
1698
1699 template
1700 struct Elf_Rel_Impl
1701 : Elf_Rel_Base {
1702 using Elf_Rel_Base::r_info;
1703 LLVM_ELF_IMPORT_TYPES(target_endianness, true)
1704
1705 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
1706 // and ELF64_R_INFO macros defined in the ELF specification:
1707 uint64_t getSymbol() const { return (r_info >> 32); }
1708 unsigned char getType() const {
1709 return (unsigned char) (r_info & 0xffffffffL);
1710 }
1711 void setSymbol(uint64_t s) { setSymbolAndType(s, getType()); }
1712 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
1713 void setSymbolAndType(uint64_t s, unsigned char t) {
1714 r_info = (s << 32) + (t&0xffffffffL);
1715 }
1716 };
1717
1718 template
1719 struct Elf_Rel_Impl
1720 : Elf_Rel_Base {
1721 using Elf_Rel_Base::r_info;
1722 LLVM_ELF_IMPORT_TYPES(target_endianness, false)
1723
1724 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
1725 // and ELF32_R_INFO macros defined in the ELF specification:
1726 uint32_t getSymbol() const { return (r_info >> 8); }
1727 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
1728 void setSymbol(uint32_t s) { setSymbolAndType(s, getType()); }
1729 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
1730 void setSymbolAndType(uint32_t s, unsigned char t) {
1731 r_info = (s << 8) + t;
1732 }
1733 };
1734
1735
1736 template
1737 class ELFObjectFile : public ObjectFile {
1738 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
1739
1740 typedef Elf_Shdr_Impl Elf_Shdr;
1741 typedef Elf_Sym_Impl Elf_Sym;
1742 typedef Elf_Rel_Impl Elf_Rel;
1743 typedef Elf_Rel_Impl Elf_Rela;
1744
1745 protected:
1746 struct Elf_Ehdr {
1747 unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes
1748 Elf_Half e_type; // Type of file (see ET_*)
1749 Elf_Half e_machine; // Required architecture for this file (see EM_*)
1750 Elf_Word e_version; // Must be equal to 1
1751 Elf_Addr e_entry; // Address to jump to in order to start program
1752 Elf_Off e_phoff; // Program header table's file offset, in bytes
1753 Elf_Off e_shoff; // Section header table's file offset, in bytes
1754 Elf_Word e_flags; // Processor-specific flags
1755 Elf_Half e_ehsize; // Size of ELF header, in bytes
1756 Elf_Half e_phentsize;// Size of an entry in the program header table
1757 Elf_Half e_phnum; // Number of entries in the program header table
1758 Elf_Half e_shentsize;// Size of an entry in the section header table
1759 Elf_Half e_shnum; // Number of entries in the section header table
1760 Elf_Half e_shstrndx; // Section header table index of section name
1761 // string table
1762 bool checkMagic() const {
1763 return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
1764 }
1765 unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; }
1766 unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; }
1767 };
1768 // This flag is used for classof, to distinguish ELFObjectFile from
1769 // its subclass. If more subclasses will be created, this flag will
1770 // have to become an enum.
1771 bool isDyldELFObject;
1772
1773 private:
1774 typedef SmallVector Sections_t;
1775 typedef DenseMap IndexMap_t;
1776 typedef DenseMap > RelocMap_t;
1777
1778 const Elf_Ehdr *Header;
1779 const Elf_Shdr *SectionHeaderTable;
1780 const Elf_Shdr *dot_shstrtab_sec; // Section header string table.
1781 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
1782 Sections_t SymbolTableSections;
1783 IndexMap_t SymbolTableSectionsIndexMap;
1784 DenseMap ExtendedSymbolTable;
1785
1786 /// @brief Map sections to an array of relocation sections that reference
1787 /// them sorted by section index.
1788 RelocMap_t SectionRelocMap;
1789
1790 /// @brief Get the relocation section that contains \a Rel.
1791 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
1792 return getSection(Rel.w.b);
1793 }
1794
1795 bool isRelocationHasAddend(DataRefImpl Rel) const;
1796 template
1797 const T *getEntry(uint16_t Section, uint32_t Entry) const;
1798 template
1799 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
1800 const Elf_Shdr *getSection(DataRefImpl index) const;
1801 const Elf_Shdr *getSection(uint32_t index) const;
1802 const Elf_Rel *getRel(DataRefImpl Rel) const;
1803 const Elf_Rela *getRela(DataRefImpl Rela) const;
1804 const char *getString(uint32_t section, uint32_t offset) const;
1805 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
1806 error_code getSymbolName(const Elf_Sym *Symb, StringRef &Res) const;
1807
1808 protected:
1809 const Elf_Sym *getSymbol(DataRefImpl Symb) const; // FIXME: Should be private?
1810 void validateSymbol(DataRefImpl Symb) const;
1811
1812 protected:
1813 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
1814 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
1815 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
1816 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
1817 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
1818 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
1819 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
1820 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const;
1821 virtual error_code isSymbolWeak(DataRefImpl Symb, bool &Res) const;
1822 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
1823 virtual error_code isSymbolAbsolute(DataRefImpl Symb, bool &Res) const;
1824 virtual error_code getSymbolSection(DataRefImpl Symb,
1825 section_iterator &Res) const;
1826
1827 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
1828 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
1829 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
1830 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
1831 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
1832 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
1833 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
1834 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
1835 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
1836 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
1837 bool &Result) const;
1838 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
1839 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
1840
1841 virtual error_code getRelocationNext(DataRefImpl Rel,
1842 RelocationRef &Res) const;
1843 virtual error_code getRelocationAddress(DataRefImpl Rel,
1844 uint64_t &Res) const;
1845 virtual error_code getRelocationOffset(DataRefImpl Rel,
1846 uint64_t &Res) const;
1847 virtual error_code getRelocationSymbol(DataRefImpl Rel,
1848 SymbolRef &Res) const;
1849 virtual error_code getRelocationType(DataRefImpl Rel,
1850 uint64_t &Res) const;
1851 virtual error_code getRelocationTypeName(DataRefImpl Rel,
1852 SmallVectorImpl &Result) const;
1853 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
1854 int64_t &Res) const;
1855 virtual error_code getRelocationValueString(DataRefImpl Rel,
1856 SmallVectorImpl &Result) const;
1857
1858 public:
1859 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
1860 virtual symbol_iterator begin_symbols() const;
1861 virtual symbol_iterator end_symbols() const;
1862 virtual section_iterator begin_sections() const;
1863 virtual section_iterator end_sections() const;
1864
1865 virtual uint8_t getBytesInAddress() const;
1866 virtual StringRef getFileFormatName() const;
1867 virtual unsigned getArch() const;
1868
1869 uint64_t getNumSections() const;
1870 uint64_t getStringTableIndex() const;
1871 ELF::Elf64_Word getSymbolTableIndex(const Elf_Sym *symb) const;
1872 const Elf_Shdr *getSection(const Elf_Sym *symb) const;
1873
1874 // Methods for type inquiry through isa, cast, and dyn_cast
1875 bool isDyldType() const { return isDyldELFObject; }
1876 static inline bool classof(const Binary *v) {
1877 return v->getType() == Binary::isELF;
1878 }
1879 static inline bool classof(const ELFObjectFile *v) { return true; }
1880 };
1881
1882 template
1883 void ELFObjectFile
1884 ::validateSymbol(DataRefImpl Symb) const {
1885 const Elf_Sym *symb = getSymbol(Symb);
1886 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
1887 // FIXME: We really need to do proper error handling in the case of an invalid
1888 // input file. Because we don't use exceptions, I think we'll just pass
1889 // an error object around.
1890 if (!( symb
1891 && SymbolTableSection
1892 && symb >= (const Elf_Sym*)(base()
1893 + SymbolTableSection->sh_offset)
1894 && symb < (const Elf_Sym*)(base()
1895 + SymbolTableSection->sh_offset
1896 + SymbolTableSection->sh_size)))
1897 // FIXME: Proper error handling.
1898 report_fatal_error("Symb must point to a valid symbol!");
1899 }
1900
1901 template
1902 error_code ELFObjectFile
1903 ::getSymbolNext(DataRefImpl Symb,
1904 SymbolRef &Result) const {
1905 validateSymbol(Symb);
1906 const Elf_Shdr *SymbolTableSection = SymbolTableSections[Symb.d.b];
1907
1908 ++Symb.d.a;
1909 // Check to see if we are at the end of this symbol table.
1910 if (Symb.d.a >= SymbolTableSection->getEntityCount()) {
1911 // We are at the end. If there are other symbol tables, jump to them.
1912 ++Symb.d.b;
1913 Symb.d.a = 1; // The 0th symbol in ELF is fake.
1914 // Otherwise return the terminator.
1915 if (Symb.d.b >= SymbolTableSections.size()) {
1916 Symb.d.a = std::numeric_limits::max();
1917 Symb.d.b = std::numeric_limits::max();
1918 }
1919 }
1920
1921 Result = SymbolRef(Symb, this);
1922 return object_error::success;
1923 }
1924
1925 template
1926 error_code ELFObjectFile
1927 ::getSymbolName(DataRefImpl Symb,
1928 StringRef &Result) const {
1929 validateSymbol(Symb);
1930 const Elf_Sym *symb = getSymbol(Symb);
1931 return getSymbolName(symb, Result);
1932 }
1933
1934 template
1935 ELF::Elf64_Word ELFObjectFile
1936 ::getSymbolTableIndex(const Elf_Sym *symb) const {
1937 if (symb->st_shndx == ELF::SHN_XINDEX)
1938 return ExtendedSymbolTable.lookup(symb);
1939 return symb->st_shndx;
1940 }
1941
1942 template
1943 const typename ELFObjectFile::Elf_Shdr *
1944 ELFObjectFile
1945 ::getSection(const Elf_Sym *symb) const {
1946 if (symb->st_shndx == ELF::SHN_XINDEX)
1947 return getSection(ExtendedSymbolTable.lookup(symb));
1948 if (symb->st_shndx >= ELF::SHN_LORESERVE)
1949 return 0;
1950 return getSection(symb->st_shndx);
1951 }
1952
1953 template
1954 error_code ELFObjectFile
1955 ::getSymbolFileOffset(DataRefImpl Symb,
1956 uint64_t &Result) const {
1957 validateSymbol(Symb);
1958 const Elf_Sym *symb = getSymbol(Symb);
1959 const Elf_Shdr *Section;
1960 switch (getSymbolTableIndex(symb)) {
1961 case ELF::SHN_COMMON:
1962 // Unintialized symbols have no offset in the object file
1963 case ELF::SHN_UNDEF:
1964 Result = UnknownAddressOrSize;
1965 return object_error::success;
1966 case ELF::SHN_ABS:
1967 Result = symb->st_value;
1968 return object_error::success;
1969 default: Section = getSection(symb);
1970 }
1971
1972 switch (symb->getType()) {
1973 case ELF::STT_SECTION:
1974 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
1975 return object_error::success;
1976 case ELF::STT_FUNC:
1977 case ELF::STT_OBJECT:
1978 case ELF::STT_NOTYPE:
1979 Result = symb->st_value +
1980 (Section ? Section->sh_offset : 0);
1981 return object_error::success;
1982 default:
1983 Result = UnknownAddressOrSize;
1984 return object_error::success;
1985 }
1986 }
1987
1988 template
1989 error_code ELFObjectFile
1990 ::getSymbolAddress(DataRefImpl Symb,
1991 uint64_t &Result) const {
1992 validateSymbol(Symb);
1993 const Elf_Sym *symb = getSymbol(Symb);
1994 const Elf_Shdr *Section;
1995 switch (getSymbolTableIndex(symb)) {
1996 case ELF::SHN_COMMON:
1997 case ELF::SHN_UNDEF:
1998 Result = UnknownAddressOrSize;
1999 return object_error::success;
2000 case ELF::SHN_ABS:
2001 Result = symb->st_value;
2002 return object_error::success;
2003 default: Section = getSection(symb);
2004 }
2005
2006 switch (symb->getType()) {
2007 case ELF::STT_SECTION:
2008 Result = Section ? Section->sh_addr : UnknownAddressOrSize;
2009 return object_error::success;
2010 case ELF::STT_FUNC:
2011 case ELF::STT_OBJECT:
2012 case ELF::STT_NOTYPE:
2013 Result = symb->st_value + (Section ? Section->sh_addr : 0);
2014 return object_error::success;
2015 default:
2016 Result = UnknownAddressOrSize;
2017 return object_error::success;
2018 }
2019 }
2020
2021 template
2022 error_code ELFObjectFile
2023 ::getSymbolSize(DataRefImpl Symb,
2024 uint64_t &Result) const {
2025 validateSymbol(Symb);
2026 const Elf_Sym *symb = getSymbol(Symb);
2027 if (symb->st_size == 0)
2028 Result = UnknownAddressOrSize;
2029 Result = symb->st_size;
2030 return object_error::success;
2031 }
2032
2033 template
2034 error_code ELFObjectFile
2035 ::getSymbolNMTypeChar(DataRefImpl Symb,
2036 char &Result) const {
2037 validateSymbol(Symb);
2038 const Elf_Sym *symb = getSymbol(Symb);
2039 const Elf_Shdr *Section = getSection(symb);
2040
2041 char ret = '?';
2042
2043 if (Section) {
2044 switch (Section->sh_type) {
2045 case ELF::SHT_PROGBITS:
2046 case ELF::SHT_DYNAMIC:
2047 switch (Section->sh_flags) {
2048 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
2049 ret = 't'; break;
2050 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
2051 ret = 'd'; break;
2052 case ELF::SHF_ALLOC:
2053 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
2054 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
2055 ret = 'r'; break;
2056 }
2057 break;
2058 case ELF::SHT_NOBITS: ret = 'b';
2059 }
2060 }
2061
2062 switch (getSymbolTableIndex(symb)) {
2063 case ELF::SHN_UNDEF:
2064 if (ret == '?')
2065 ret = 'U';
2066 break;
2067 case ELF::SHN_ABS: ret = 'a'; break;
2068 case ELF::SHN_COMMON: ret = 'c'; break;
2069 }
2070
2071 switch (symb->getBinding()) {
2072 case ELF::STB_GLOBAL: ret = ::toupper(ret); break;
2073 case ELF::STB_WEAK:
2074 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF)
2075 ret = 'w';
2076 else
2077 if (symb->getType() == ELF::STT_OBJECT)
2078 ret = 'V';
2079 else
2080 ret = 'W';
2081 }
2082
2083 if (ret == '?' && symb->getType() == ELF::STT_SECTION) {
2084 StringRef name;
2085 if (error_code ec = getSymbolName(Symb, name))
2086 return ec;
2087 Result = StringSwitch(name)
2088 .StartsWith(".debug", 'N')
2089 .StartsWith(".note", 'n')
2090 .Default('?');
2091 return object_error::success;
2092 }
2093
2094 Result = ret;
2095 return object_error::success;
2096 }
2097
2098 template
2099 error_code ELFObjectFile
2100 ::getSymbolType(DataRefImpl Symb,
2101 SymbolRef::Type &Result) const {
2102 validateSymbol(Symb);
2103 const Elf_Sym *symb = getSymbol(Symb);
2104
2105 if (getSymbolTableIndex(symb) == ELF::SHN_UNDEF) {
2106 Result = SymbolRef::ST_External;
2107 return object_error::success;
2108 }
2109
2110 switch (symb->getType()) {
2111 case ELF::STT_SECTION:
2112 Result = SymbolRef::ST_Debug;
2113 break;
2114 case ELF::STT_FILE:
2115 Result = SymbolRef::ST_File;
2116 break;
2117 case ELF::STT_FUNC:
2118 Result = SymbolRef::ST_Function;
2119 break;
2120 case ELF::STT_OBJECT:
2121 Result = SymbolRef::ST_Data;
2122 break;
2123 default:
2124 Result = SymbolRef::ST_Other;
2125 break;
2126 }
2127 return object_error::success;
2128 }
2129
2130 template
2131 error_code ELFObjectFile
2132 ::isSymbolGlobal(DataRefImpl Symb,
2133 bool &Result) const {
2134 validateSymbol(Symb);
2135 const Elf_Sym *symb = getSymbol(Symb);
2136
2137 Result = symb->getBinding() == ELF::STB_GLOBAL;
2138 return object_error::success;
2139 }
2140
2141 template
2142 error_code ELFObjectFile
2143 ::isSymbolWeak(DataRefImpl Symb,
2144 bool &Result) const {
2145 validateSymbol(Symb);
2146 const Elf_Sym *symb = getSymbol(Symb);
2147
2148 Result = symb->getBinding() == ELF::STB_WEAK;
2149 return object_error::success;
2150 }
2151
2152 template
2153 error_code ELFObjectFile
2154 ::isSymbolAbsolute(DataRefImpl Symb, bool &Res) const {
2155 validateSymbol(Symb);
2156 const Elf_Sym *symb = getSymbol(Symb);
2157 Res = symb->st_shndx == ELF::SHN_ABS;
2158 return object_error::success;
2159 }
2160
2161 template
2162 error_code ELFObjectFile
2163 ::getSymbolSection(DataRefImpl Symb,
2164 section_iterator &Res) const {
2165 validateSymbol(Symb);
2166 const Elf_Sym *symb = getSymbol(Symb);
2167 const Elf_Shdr *sec = getSection(symb);
2168 if (!sec)
2169 Res = end_sections();
2170 else {
2171 DataRefImpl Sec;
2172 Sec.p = reinterpret_cast(sec);
2173 Res = section_iterator(SectionRef(Sec, this));
2174 }
2175 return object_error::success;
2176 }
2177
2178 template
2179 error_code ELFObjectFile
2180 ::isSymbolInternal(DataRefImpl Symb,
2181 bool &Result) const {
2182 validateSymbol(Symb);
2183 const Elf_Sym *symb = getSymbol(Symb);
2184
2185 if ( symb->getType() == ELF::STT_FILE
2186 || symb->getType() == ELF::STT_SECTION)
2187 Result = true;
2188 Result = false;
2189 return object_error::success;
2190 }
2191
2192 template
2193 error_code ELFObjectFile
2194 ::getSectionNext(DataRefImpl Sec, SectionRef &Result) const {
2195 const uint8_t *sec = reinterpret_cast(Sec.p);
2196 sec += Header->e_shentsize;
2197 Sec.p = reinterpret_cast(sec);
2198 Result = SectionRef(Sec, this);
2199 return object_error::success;
2200 }
2201
2202 template
2203 error_code ELFObjectFile
2204 ::getSectionName(DataRefImpl Sec,
2205 StringRef &Result) const {
2206 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
2207 Result = StringRef(getString(dot_shstrtab_sec, sec->sh_name));
2208 return object_error::success;
2209 }
2210
2211 template
2212 error_code ELFObjectFile
2213 ::getSectionAddress(DataRefImpl Sec,
2214 uint64_t &Result) const {
2215 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
2216 Result = sec->sh_addr;
2217 return object_error::success;
2218 }
2219
2220 template
2221 error_code ELFObjectFile
2222 ::getSectionSize(DataRefImpl Sec,
2223 uint64_t &Result) const {
2224 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
2225 Result = sec->sh_size;
2226 return object_error::success;
2227 }
2228
2229 template
2230 error_code ELFObjectFile
2231 ::getSectionContents(DataRefImpl Sec,
2232 StringRef &Result) const {
2233 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
2234 const char *start = (const char*)base() + sec->sh_offset;
2235 Result = StringRef(start, sec->sh_size);
2236 return object_error::success;
2237 }
2238
2239 template
2240 error_code ELFObjectFile
2241 ::getSectionAlignment(DataRefImpl Sec,
2242 uint64_t &Result) const {
2243 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
2244 Result = sec->sh_addralign;
2245 return object_error::success;
2246 }
2247
2248 template
2249 error_code ELFObjectFile
2250 ::isSectionText(DataRefImpl Sec,
2251 bool &Result) const {
2252 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
2253 if (sec->sh_flags & ELF::SHF_EXECINSTR)
2254 Result = true;
2255 else
2256 Result = false;
2257 return object_error::success;
2258 }
2259
2260 template
2261 error_code ELFObjectFile
2262 ::isSectionData(DataRefImpl Sec,
2263 bool &Result) const {
2264 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
2265 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
2266 && sec->sh_type == ELF::SHT_PROGBITS)
2267 Result = true;
2268 else
2269 Result = false;
2270 return object_error::success;
2271 }
2272
2273 template
2274 error_code ELFObjectFile
2275 ::isSectionBSS(DataRefImpl Sec,
2276 bool &Result) const {
2277 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
2278 if (sec->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE)
2279 && sec->sh_type == ELF::SHT_NOBITS)
2280 Result = true;
2281 else
2282 Result = false;
2283 return object_error::success;
2284 }
2285
2286 template
2287 error_code ELFObjectFile
2288 ::sectionContainsSymbol(DataRefImpl Sec,
2289 DataRefImpl Symb,
2290 bool &Result) const {
2291 // FIXME: Unimplemented.
2292 Result = false;
2293 return object_error::success;
2294 }
2295
2296 template
2297 relocation_iterator ELFObjectFile
2298 ::getSectionRelBegin(DataRefImpl Sec) const {
2299 DataRefImpl RelData;
2300 memset(&RelData, 0, sizeof(RelData));
2301 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
2302 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
2303 if (sec != 0 && ittr != SectionRelocMap.end()) {
2304 RelData.w.a = getSection(ittr->second[0])->sh_info;
2305 RelData.w.b = ittr->second[0];
2306 RelData.w.c = 0;
2307 }
2308 return relocation_iterator(RelocationRef(RelData, this));
2309 }
2310
2311 template
2312 relocation_iterator ELFObjectFile
2313 ::getSectionRelEnd(DataRefImpl Sec) const {
2314 DataRefImpl RelData;
2315 memset(&RelData, 0, sizeof(RelData));
2316 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
2317 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
2318 if (sec != 0 && ittr != SectionRelocMap.end()) {
2319 // Get the index of the last relocation section for this section.
2320 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
2321 const Elf_Shdr *relocsec = getSection(relocsecindex);
2322 RelData.w.a = relocsec->sh_info;
2323 RelData.w.b = relocsecindex;
2324 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
2325 }
2326 return relocation_iterator(RelocationRef(RelData, this));
2327 }
2328
2329 // Relocations
2330 template
2331 error_code ELFObjectFile
2332 ::getRelocationNext(DataRefImpl Rel,
2333 RelocationRef &Result) const {
2334 ++Rel.w.c;
2335 const Elf_Shdr *relocsec = getSection(Rel.w.b);
2336 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
2337 // We have reached the end of the relocations for this section. See if there
2338 // is another relocation section.
2339 typename RelocMap_t::mapped_type relocseclist =
2340 SectionRelocMap.lookup(getSection(Rel.w.a));
2341
2342 // Do a binary search for the current reloc section index (which must be
2343 // present). Then get the next one.
2344 typename RelocMap_t::mapped_type::const_iterator loc =
2345 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
2346 ++loc;
2347
2348 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
2349 // to the end iterator.
2350 if (loc != relocseclist.end()) {
2351 Rel.w.b = *loc;
2352 Rel.w.a = 0;
2353 }
2354 }
2355 Result = RelocationRef(Rel, this);
2356 return object_error::success;
2357 }
2358
2359 template
2360 error_code ELFObjectFile
2361 ::getRelocationSymbol(DataRefImpl Rel,
2362 SymbolRef &Result) const {
2363 uint32_t symbolIdx;
2364 const Elf_Shdr *sec = getSection(Rel.w.b);
2365 switch (sec->sh_type) {
2366 default :
2367 report_fatal_error("Invalid section type in Rel!");
2368 case ELF::SHT_REL : {
2369 symbolIdx = getRel(Rel)->getSymbol();
2370 break;
2371 }
2372 case ELF::SHT_RELA : {
2373 symbolIdx = getRela(Rel)->getSymbol();
2374 break;
2375 }
2376 }
2377 DataRefImpl SymbolData;
2378 IndexMap_t::const_iterator it = SymbolTableSectionsIndexMap.find(sec->sh_link);
2379 if (it == SymbolTableSectionsIndexMap.end())
2380 report_fatal_error("Relocation symbol table not found!");
2381 SymbolData.d.a = symbolIdx;
2382 SymbolData.d.b = it->second;
2383 Result = SymbolRef(SymbolData, this);
2384 return object_error::success;
2385 }
2386
2387 template
2388 error_code ELFObjectFile
2389 ::getRelocationAddress(DataRefImpl Rel,
2390 uint64_t &Result) const {
2391 uint64_t offset;
2392 const Elf_Shdr *sec = getSection(Rel.w.b);
2393 switch (sec->sh_type) {
2394 default :
2395 report_fatal_error("Invalid section type in Rel!");
2396 case ELF::SHT_REL : {
2397 offset = getRel(Rel)->r_offset;
2398 break;
2399 }
2400 case ELF::SHT_RELA : {
2401 offset = getRela(Rel)->r_offset;
2402 break;
2403 }
2404 }
2405
2406 Result = offset;
2407 return object_error::success;
2408 }
2409
2410 template
2411 error_code ELFObjectFile
2412 ::getRelocationOffset(DataRefImpl Rel,
2413 uint64_t &Result) const {
2414 uint64_t offset;
2415 const Elf_Shdr *sec = getSection(Rel.w.b);
2416 switch (sec->sh_type) {
2417 default :
2418 report_fatal_error("Invalid section type in Rel!");
2419 case ELF::SHT_REL : {
2420 offset = getRel(Rel)->r_offset;
2421 break;
2422 }
2423 case ELF::SHT_RELA : {
2424 offset = getRela(Rel)->r_offset;
2425 break;
2426 }
2427 }
2428
2429 Result = offset - sec->sh_addr;
2430 return object_error::success;
2431 }
2432
2433 template
2434 error_code ELFObjectFile
2435 ::getRelocationType(DataRefImpl Rel,
2436 uint64_t &Result) const {
2437 const Elf_Shdr *sec = getSection(Rel.w.b);
2438 switch (sec->sh_type) {
2439 default :
2440 report_fatal_error("Invalid section type in Rel!");
2441 case ELF::SHT_REL : {
2442 Result = getRel(Rel)->getType();
2443 break;
2444 }
2445 case ELF::SHT_RELA : {
2446 Result = getRela(Rel)->getType();
2447 break;
2448 }
2449 }
2450 return object_error::success;
2451 }
2452
2453 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
2454 case ELF::enum: res = #enum; break;
2455
2456 template
2457 error_code ELFObjectFile
2458 ::getRelocationTypeName(DataRefImpl Rel,
2459 SmallVectorImpl &Result) const {
2460 const Elf_Shdr *sec = getSection(Rel.w.b);
2461 uint8_t type;
2462 StringRef res;
2463 switch (sec->sh_type) {
2464 default :
2465 return object_error::parse_failed;
2466 case ELF::SHT_REL : {
2467 type = getRel(Rel)->getType();
2468 break;
2469 }
2470 case ELF::SHT_RELA : {
2471 type = getRela(Rel)->getType();
2472 break;
2473 }
2474 }
2475 switch (Header->e_machine) {
2476 case ELF::EM_X86_64:
2477 switch (type) {
2478 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
2479 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
2480 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
2481 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
2482 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
2483 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
2484 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
2485 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
2486 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
2487 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
2488 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
2489 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
2490 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
2491 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
2492 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
2493 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
2494 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
2495 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
2496 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
2497 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
2498 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
2499 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
2500 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
2501 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
2502 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
2503 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
2504 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
2505 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
2506 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
2507 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
2508 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
2509 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
2510 default:
2511 res = "Unknown";
2512 }
2513 break;
2514 case ELF::EM_386:
2515 switch (type) {
2516 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
2517 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
2518 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
2519 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
2520 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
2521 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
2522 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
2523 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
2524 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
2525 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
2526 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
2527 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
2528 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
2529 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
2530 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
2531 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
2532 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
2533 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
2534 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
2535 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
2536 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
2537 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
2538 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
2539 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
2540 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
2541 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
2542 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
2543 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
2544 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
2545 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
2546 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
2547 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
2548 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
2549 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
2550 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
2551 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
2552 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
2553 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
2554 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
2555 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
2556 default:
2557 res = "Unknown";
2558 }
2559 break;
2560 default:
2561 res = "Unknown";
2562 }
2563 Result.append(res.begin(), res.end());
2564 return object_error::success;
2565 }
2566
2567 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
2568
2569 template
2570 error_code ELFObjectFile
2571 ::getRelocationAdditionalInfo(DataRefImpl Rel,
2572 int64_t &Result) const {
2573 const Elf_Shdr *sec = getSection(Rel.w.b);
2574 switch (sec->sh_type) {
2575 default :
2576 report_fatal_error("Invalid section type in Rel!");
2577 case ELF::SHT_REL : {
2578 Result = 0;
2579 return object_error::success;
2580 }
2581 case ELF::SHT_RELA : {
2582 Result = getRela(Rel)->r_addend;
2583 return object_error::success;
2584 }
2585 }
2586 }
2587
2588 template
2589 error_code ELFObjectFile
2590 ::getRelocationValueString(DataRefImpl Rel,
2591 SmallVectorImpl &Result) const {
2592 const Elf_Shdr *sec = getSection(Rel.w.b);
2593 uint8_t type;
2594 StringRef res;
2595 int64_t addend = 0;
2596 uint16_t symbol_index = 0;
2597 switch (sec->sh_type) {
2598 default :
2599 return object_error::parse_failed;
2600 case ELF::SHT_REL : {
2601 type = getRel(Rel)->getType();
2602 symbol_index = getRel(Rel)->getSymbol();
2603 // TODO: Read implicit addend from section data.
2604 break;
2605 }
2606 case ELF::SHT_RELA : {
2607 type = getRela(Rel)->getType();
2608 symbol_index = getRela(Rel)->getSymbol();
2609 addend = getRela(Rel)->r_addend;
2610 break;
2611 }
2612 }
2613 const Elf_Sym *symb = getEntry(sec->sh_link, symbol_index);
2614 StringRef symname;
2615 if (error_code ec = getSymbolName(symb, symname))
2616 return ec;
2617 switch (Header->e_machine) {
2618 case ELF::EM_X86_64:
2619 switch (type) {
2620 case ELF::R_X86_64_32S:
2621 res = symname;
2622 break;
2623 case ELF::R_X86_64_PC32: {
2624 std::string fmtbuf;
2625 raw_string_ostream fmt(fmtbuf);
2626 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
2627 fmt.flush();
2628 Result.append(fmtbuf.begin(), fmtbuf.end());
2629 }
2630 break;
2631 default:
2632 res = "Unknown";
2633 }
2634 break;
2635 default:
2636 res = "Unknown";
2637 }
2638 if (Result.empty())
2639 Result.append(res.begin(), res.end());
2640 return object_error::success;
2641 }
2642
2643 template
2644 ELFObjectFile::ELFObjectFile(MemoryBuffer *Object
2645 , error_code &ec)
2646 : ObjectFile(Binary::isELF, Object, ec)
2647 , isDyldELFObject(false)
2648 , SectionHeaderTable(0)
2649 , dot_shstrtab_sec(0)
2650 , dot_strtab_sec(0) {
2651
2652 const uint64_t FileSize = Data->getBufferSize();
2653
2654 if (sizeof(Elf_Ehdr) > FileSize)
2655 // FIXME: Proper error handling.
2656 report_fatal_error("File too short!");
2657
2658 Header = reinterpret_cast(base());
2659
2660 if (Header->e_shoff == 0)
2661 return;
2662
2663 const uint64_t SectionTableOffset = Header->e_shoff;
2664
2665 if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize)
2666 // FIXME: Proper error handling.
2667 report_fatal_error("Section header table goes past end of file!");
2668
2669 // The getNumSections() call below depends on SectionHeaderTable being set.
2670 SectionHeaderTable =
2671 reinterpret_cast(base() + SectionTableOffset);
2672 const uint64_t SectionTableSize = getNumSections() * Header->e_shentsize;
2673
2674 if (SectionTableOffset + SectionTableSize > FileSize)
2675 // FIXME: Proper error handling.
2676 report_fatal_error("Section table goes past end of file!");
2677
2678 // To find the symbol tables we walk the section table to find SHT_SYMTAB.
2679 const Elf_Shdr* SymbolTableSectionHeaderIndex = 0;
2680 const Elf_Shdr* sh = SectionHeaderTable;
2681 for (uint64_t i = 0, e = getNumSections(); i != e; ++i) {
2682 if (sh->sh_type == ELF::SHT_SYMTAB_SHNDX) {
2683 if (SymbolTableSectionHeaderIndex)
2684 // FIXME: Proper error handling.
2685 report_fatal_error("More than one .symtab_shndx!");
2686 SymbolTableSectionHeaderIndex = sh;
2687 }
2688 if (sh->sh_type == ELF::SHT_SYMTAB) {
2689 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
2690 SymbolTableSections.push_back(sh);
2691 }
2692 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
2693 SectionRelocMap[getSection(sh->sh_info)].push_back(i);
2694 }
2695 ++sh;
2696 }
2697
2698 // Sort section relocation lists by index.
2699 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
2700 e = SectionRelocMap.end(); i != e; ++i) {
2701 std::sort(i->second.begin(), i->second.end());
2702 }
2703
2704 // Get string table sections.
2705 dot_shstrtab_sec = getSection(getStringTableIndex());
2706 if (dot_shstrtab_sec) {
2707 // Verify that the last byte in the string table in a null.
2708 if (((const char*)base() + dot_shstrtab_sec->sh_offset)
2709 [dot_shstrtab_sec->sh_size - 1] != 0)
2710 // FIXME: Proper error handling.
2711 report_fatal_error("String table must end with a null terminator!");
2712 }
2713
2714 // Merge this into the above loop.
2715 for (const char *i = reinterpret_cast(SectionHeaderTable),
2716 *e = i + getNumSections() * Header->e_shentsize;
2717 i != e; i += Header->e_shentsize) {
2718 const Elf_Shdr *sh = reinterpret_cast(i);
2719 if (sh->sh_type == ELF::SHT_STRTAB) {
2720 StringRef SectionName(getString(dot_shstrtab_sec, sh->sh_name));
2721 if (SectionName == ".strtab") {
2722 if (dot_strtab_sec != 0)
2723 // FIXME: Proper error handling.
2724 report_fatal_error("Already found section named .strtab!");
2725 dot_strtab_sec = sh;
2726 const char *dot_strtab = (const char*)base() + sh->sh_offset;
2727 if (dot_strtab[sh->sh_size - 1] != 0)
2728 // FIXME: Proper error handling.
2729 report_fatal_error("String table must end with a null terminator!");
2730 }
2731 }
2732 }
2733
2734 // Build symbol name side-mapping if there is one.
2735 if (SymbolTableSectionHeaderIndex) {
2736 const Elf_Word *ShndxTable = reinterpret_cast(base() +
2737 SymbolTableSectionHeaderIndex->sh_offset);
2738 error_code ec;
2739 for (symbol_iterator si = begin_symbols(),
2740 se = end_symbols(); si != se; si.increment(ec)) {
2741 if (ec)
2742 report_fatal_error("Fewer extended symbol table entries than symbols!");
2743 if (*ShndxTable != ELF::SHN_UNDEF)
2744 ExtendedSymbolTable[getSymbol(si->getRawDataRefImpl())] = *ShndxTable;
2745 ++ShndxTable;
2746 }
2747 }
2748 }
2749
2750 template
2751 symbol_iterator ELFObjectFile
2752 ::begin_symbols() const {
2753 DataRefImpl SymbolData;
2754 memset(&SymbolData, 0, sizeof(SymbolData));
2755 if (SymbolTableSections.size() == 0) {
2756 SymbolData.d.a = std::numeric_limits::max();
2757 SymbolData.d.b = std::numeric_limits::max();
2758 } else {
2759 SymbolData.d.a = 1; // The 0th symbol in ELF is fake.
2760 SymbolData.d.b = 0;
2761 }
2762 return symbol_iterator(SymbolRef(SymbolData, this));
2763 }
2764
2765 template
2766 symbol_iterator ELFObjectFile
2767 ::end_symbols() const {
2768 DataRefImpl SymbolData;
2769 memset(&SymbolData, 0, sizeof(SymbolData));
2770 SymbolData.d.a = std::numeric_limits::max();
2771 SymbolData.d.b = std::numeric_limits::max();
2772 return symbol_iterator(SymbolRef(SymbolData, this));
2773 }
2774
2775 template
2776 section_iterator ELFObjectFile
2777 ::begin_sections() const {
2778 DataRefImpl ret;
2779 memset(&ret, 0, sizeof(DataRefImpl));
2780 ret.p = reinterpret_cast(base() + Header->e_shoff);
2781 return section_iterator(SectionRef(ret, this));
2782 }
2783
2784 template
2785 section_iterator ELFObjectFile
2786 ::end_sections() const {
2787 DataRefImpl ret;
2788 memset(&ret, 0, sizeof(DataRefImpl));
2789 ret.p = reinterpret_cast(base()
2790 + Header->e_shoff
2791 + (Header->e_shentsize*getNumSections()));
2792 return section_iterator(SectionRef(ret, this));
2793 }
2794
2795 template
2796 uint8_t ELFObjectFile::getBytesInAddress() const {
2797 return is64Bits ? 8 : 4;
2798 }
2799
2800 template
2801 StringRef ELFObjectFile
2802 ::getFileFormatName() const {
2803 switch(Header->e_ident[ELF::EI_CLASS]) {
2804 case ELF::ELFCLASS32:
2805 switch(Header->e_machine) {
2806 case ELF::EM_386:
2807 return "ELF32-i386";
2808 case ELF::EM_X86_64:
2809 return "ELF32-x86-64";
2810 case ELF::EM_ARM:
2811 return "ELF32-arm";
2812 default:
2813 return "ELF32-unknown";
2814 }
2815 case ELF::ELFCLASS64:
2816 switch(Header->e_machine) {
2817 case ELF::EM_386:
2818 return "ELF64-i386";
2819 case ELF::EM_X86_64:
2820 return "ELF64-x86-64";
2821 default:
2822 return "ELF64-unknown";
2823 }
2824 default:
2825 // FIXME: Proper error handling.
2826 report_fatal_error("Invalid ELFCLASS!");
2827 }
2828 }
2829
2830 template
2831 unsigned ELFObjectFile::getArch() const {
2832 switch(Header->e_machine) {
2833 case ELF::EM_386:
2834 return Triple::x86;
2835 case ELF::EM_X86_64:
2836 return Triple::x86_64;
2837 case ELF::EM_ARM:
2838 return Triple::arm;
2839 default:
2840 return Triple::UnknownArch;
2841 }
2842 }
2843
2844 template
2845 uint64_t ELFObjectFile::getNumSections() const {
2846 assert(Header && "Header not initialized!");
2847 if (Header->e_shnum == ELF::SHN_UNDEF) {
2848 assert(SectionHeaderTable && "SectionHeaderTable not initialized!");
2849 return SectionHeaderTable->sh_size;
2850 }
2851 return Header->e_shnum;
2852 }
2853
2854 template
2855 uint64_t
2856 ELFObjectFile::getStringTableIndex() const {
2857 if (Header->e_shnum == ELF::SHN_UNDEF) {
2858 if (Header->e_shstrndx == ELF::SHN_HIRESERVE)
2859 return SectionHeaderTable->sh_link;
2860 if (Header->e_shstrndx >= getNumSections())
2861 return 0;
2862 }
2863 return Header->e_shstrndx;
2864 }
2865
2866
2867 template
2868 template
2869 inline const T *
2870 ELFObjectFile::getEntry(uint16_t Section,
2871 uint32_t Entry) const {
2872 return getEntry(getSection(Section), Entry);
2873 }
2874
2875 template
2876 template
2877 inline const T *
2878 ELFObjectFile::getEntry(const Elf_Shdr * Section,
2879 uint32_t Entry) const {
2880 return reinterpret_cast(
2881 base()
2882 + Section->sh_offset
2883 + (Entry * Section->sh_entsize));
2884 }
2885
2886 template
2887 const typename ELFObjectFile::Elf_Sym *
2888 ELFObjectFile::getSymbol(DataRefImpl Symb) const {
2889 return getEntry(SymbolTableSections[Symb.d.b], Symb.d.a);
2890 }
2891
2892 template
2893 const typename ELFObjectFile::Elf_Rel *
2894 ELFObjectFile::getRel(DataRefImpl Rel) const {
2895 return getEntry(Rel.w.b, Rel.w.c);
2896 }
2897
2898 template
2899 const typename ELFObjectFile::Elf_Rela *
2900 ELFObjectFile::getRela(DataRefImpl Rela) const {
2901 return getEntry(Rela.w.b, Rela.w.c);
2902 }
2903
2904 template
2905 const typename ELFObjectFile::Elf_Shdr *
2906 ELFObjectFile::getSection(DataRefImpl Symb) const {
2907 const Elf_Shdr *sec = getSection(Symb.d.b);
2908 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
2909 // FIXME: Proper error handling.
2910 report_fatal_error("Invalid symbol table section!");
2911 return sec;
2912 }
2913
2914 template
2915 const typename ELFObjectFile::Elf_Shdr *
2916 ELFObjectFile::getSection(uint32_t index) const {
2917 if (index == 0)
2918 return 0;
2919 if (!SectionHeaderTable || index >= getNumSections())
2920 // FIXME: Proper error handling.
2921 report_fatal_error("Invalid section index!");
2922
2923 return reinterpret_cast(
2924 reinterpret_cast(SectionHeaderTable)
2925 + (index * Header->e_shentsize));
2926 }
2927
2928 template
2929 const char *ELFObjectFile
2930 ::getString(uint32_t section,
2931 ELF::Elf32_Word offset) const {
2932 return getString(getSection(section), offset);
2933 }
2934
2935 template
2936 const char *ELFObjectFile
2937 ::getString(const Elf_Shdr *section,
2938 ELF::Elf32_Word offset) const {
2939 assert(section && section->sh_type == ELF::SHT_STRTAB && "Invalid section!");
2940 if (offset >= section->sh_size)
2941 // FIXME: Proper error handling.
2942 report_fatal_error("Symbol name offset outside of string table!");
2943 return (const char *)base() + section->sh_offset + offset;
2944 }
2945
2946 template
2947 error_code ELFObjectFile
2948 ::getSymbolName(const Elf_Sym *symb,
2949 StringRef &Result) const {
2950 if (symb->st_name == 0) {
2951 const Elf_Shdr *section = getSection(symb);
2952 if (!section)
2953 Result = "";
2954 else
2955 Result = getString(dot_shstrtab_sec, section->sh_name);
2956 return object_error::success;
2957 }
2958
2959 // Use the default symbol table name section.
2960 Result = getString(dot_strtab_sec, symb->st_name);
2961 return object_error::success;
2962 }
2963
2964 }
2965 }
2966
2967 #endif
2968
2969
337337
338338 public:
339339 static ObjectFile *createCOFFObjectFile(MemoryBuffer *Object);
340 static ObjectFile *createELFObjectFile(MemoryBuffer *Object,
341 bool doDyld = false, std::vector *MemoryMap = 0);
340 static ObjectFile *createELFObjectFile(MemoryBuffer *Object);
342341 static ObjectFile *createMachOObjectFile(MemoryBuffer *Object);
343342 };
344343
0 //===-- DyldELFObject.h - Dynamically loaded ELF object ----0---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Dynamically loaded ELF object class, a subclass of ELFObjectFile. Used
10 // to represent a loadable ELF image.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H
15 #define LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H
16
17 #include "llvm/Object/ELF.h"
18
19
20 namespace llvm {
21
22 using support::endianness;
23 using namespace llvm::object;
24
25 template
26 class DyldELFObject : public ELFObjectFile {
27 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
28
29 typedef Elf_Shdr_Impl Elf_Shdr;
30 typedef Elf_Sym_Impl Elf_Sym;
31 typedef Elf_Rel_Impl Elf_Rel;
32 typedef Elf_Rel_Impl Elf_Rela;
33
34 typedef typename ELFObjectFile::
35 Elf_Ehdr Elf_Ehdr;
36 Elf_Ehdr *Header;
37
38 // Update section headers according to the current location in memory
39 virtual void rebaseObject(std::vector *MemoryMap);
40 // Record memory addresses for cleanup
41 virtual void saveAddress(std::vector *MemoryMap, uint8_t *addr);
42
43 protected:
44 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
45
46 public:
47 DyldELFObject(MemoryBuffer *Object, std::vector *MemoryMap,
48 error_code &ec);
49
50 // Methods for type inquiry through isa, cast, and dyn_cast
51 static inline bool classof(const Binary *v) {
52 return (isa >(v)
53 && classof(cast >(v)));
54 }
55 static inline bool classof(
56 const ELFObjectFile *v) {
57 return v->isDyldType();
58 }
59 static inline bool classof(const DyldELFObject *v) {
60 return true;
61 }
62 };
63
64 template
65 DyldELFObject::DyldELFObject(MemoryBuffer *Object,
66 std::vector *MemoryMap, error_code &ec)
67 : ELFObjectFile(Object, ec)
68 , Header(0) {
69 this->isDyldELFObject = true;
70 Header = const_cast(
71 reinterpret_cast(this->base()));
72 if (Header->e_shoff == 0)
73 return;
74
75 // Mark the image as a dynamic shared library
76 Header->e_type = ELF::ET_DYN;
77
78 rebaseObject(MemoryMap);
79 }
80
81 // Walk through the ELF headers, updating virtual addresses to reflect where
82 // the object is currently loaded in memory
83 template
84 void DyldELFObject::rebaseObject(
85 std::vector *MemoryMap) {
86 typedef typename ELFDataTypeTypedefHelper<
87 target_endianness, is64Bits>::value_type addr_type;
88
89 uint8_t *base_p = const_cast(this->base());
90 Elf_Shdr *sectionTable =
91 reinterpret_cast(base_p + Header->e_shoff);
92 uint64_t numSections = this->getNumSections();
93
94 // Allocate memory space for NOBITS sections (such as .bss), which only exist
95 // in memory, but don't occupy space in the object file.
96 // Update the address in the section headers to reflect this allocation.
97 for (uint64_t index = 0; index < numSections; index++) {
98 Elf_Shdr *sec = reinterpret_cast(
99 reinterpret_cast(sectionTable) + index * Header->e_shentsize);
100
101 // Only update sections that are meant to be present in program memory
102 if (sec->sh_flags & ELF::SHF_ALLOC) {
103 uint8_t *addr = base_p + sec->sh_offset;
104 if (sec->sh_type == ELF::SHT_NOBITS) {
105 addr = static_cast(calloc(sec->sh_size, 1));
106 saveAddress(MemoryMap, addr);
107 }
108 else {
109 // FIXME: Currently memory with RWX permissions is allocated. In the
110 // future, make sure that permissions are as necessary
111 if (sec->sh_flags & ELF::SHF_WRITE) {
112 // see FIXME above
113 }
114 if (sec->sh_flags & ELF::SHF_EXECINSTR) {
115 // see FIXME above
116 }
117 }
118 assert(sizeof(addr_type) == sizeof(intptr_t) &&
119 "Cross-architecture ELF dy-load is not supported!");
120 sec->sh_addr = static_cast(intptr_t(addr));
121 }
122 }
123
124 // Now allocate actual space for COMMON symbols, which also don't occupy
125 // space in the object file.
126 // We want to allocate space for all COMMON symbols at once, so the flow is:
127 // 1. Go over all symbols, find those that are in COMMON. For each such
128 // symbol, record its size and the value field in its symbol header in a
129 // special vector.
130 // 2. Allocate memory for all COMMON symbols in one fell swoop.
131 // 3. Using the recorded information from (1), update the address fields in
132 // the symbol headers of the COMMON symbols to reflect their allocated
133 // address.
134 uint64_t TotalSize = 0;
135 std::vector > SymbAddrInfo;
136 error_code ec = object_error::success;
137 for (symbol_iterator si = this->begin_symbols(),
138 se = this->end_symbols(); si != se; si.increment(ec)) {
139 uint64_t Size = 0;
140 ec = si->getSize(Size);
141 Elf_Sym* symb = const_cast(
142 this->getSymbol(si->getRawDataRefImpl()));
143 if (ec == object_error::success &&
144 this->getSymbolTableIndex(symb) == ELF::SHN_COMMON && Size > 0) {
145 SymbAddrInfo.push_back(std::make_pair(&(symb->st_value), Size));
146 TotalSize += Size;
147 }
148 }
149
150 uint8_t* SectionPtr = (uint8_t *)calloc(TotalSize, 1);
151 saveAddress(MemoryMap, SectionPtr);
152
153 typedef typename std::vector >::iterator
154 AddrInfoIterator;
155 AddrInfoIterator EndIter = SymbAddrInfo.end();
156 for (AddrInfoIterator AddrIter = SymbAddrInfo.begin();
157 AddrIter != EndIter; ++AddrIter) {
158 assert(sizeof(addr_type) == sizeof(intptr_t) &&
159 "Cross-architecture ELF dy-load is not supported!");
160 *(AddrIter->first) = static_cast(intptr_t(SectionPtr));
161 SectionPtr += AddrIter->second;
162 }
163 }
164
165 // Record memory addresses for callers
166 template
167 void DyldELFObject::saveAddress(
168 std::vector *MemoryMap, uint8_t* addr) {
169 if (MemoryMap)
170 MemoryMap->push_back(addr);
171 else
172 errs() << "WARNING: Memory leak - cannot record memory for ELF dyld.";
173 }
174
175 template
176 error_code DyldELFObject::getSymbolAddress(
177 DataRefImpl Symb, uint64_t &Result) const {
178 this->validateSymbol(Symb);
179 const Elf_Sym *symb = this->getSymbol(Symb);
180 if (this->getSymbolTableIndex(symb) == ELF::SHN_COMMON) {
181 Result = symb->st_value;
182 return object_error::success;
183 }
184 else {
185 return ELFObjectFile::getSymbolAddress(
186 Symb, Result);
187 }
188 }
189
190 }
191
192 #endif
193
194 //===-- DyldELFObject.h - Dynamically loaded ELF object ----0---*- C++ -*-===//
195 //
196 // The LLVM Compiler Infrastructure
197 //
198 // This file is distributed under the University of Illinois Open Source
199 // License. See LICENSE.TXT for details.
200 //
201 //===----------------------------------------------------------------------===//
202 //
203 // Dynamically loaded ELF object class, a subclass of ELFObjectFile. Used
204 // to represent a loadable ELF image.
205 //
206 //===----------------------------------------------------------------------===//
207
208 #ifndef LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H
209 #define LLVM_RUNTIMEDYLD_DYLDELFOBJECT_H
210
211 #include "llvm/Object/ELF.h"
212
213
214 namespace llvm {
215
216 using support::endianness;
217 using namespace llvm::object;
218
219 template
220 class DyldELFObject : public ELFObjectFile {
221 LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits)
222
223 typedef Elf_Shdr_Impl Elf_Shdr;
224 typedef Elf_Sym_Impl Elf_Sym;
225 typedef Elf_Rel_Impl Elf_Rel;
226 typedef Elf_Rel_Impl Elf_Rela;
227
228 typedef typename ELFObjectFile::
229 Elf_Ehdr Elf_Ehdr;
230 Elf_Ehdr *Header;
231
232 // Update section headers according to the current location in memory
233 virtual void rebaseObject(std::vector *MemoryMap);
234 // Record memory addresses for cleanup
235 virtual void saveAddress(std::vector *MemoryMap, uint8_t *addr);
236
237 protected:
238 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
239
240 public:
241 DyldELFObject(MemoryBuffer *Object, std::vector *MemoryMap,
242 error_code &ec);
243
244 // Methods for type inquiry through isa, cast, and dyn_cast
245 static inline bool classof(const Binary *v) {
246 return (isa >(v)
247 && classof(cast >(v)));
248 }
249 static inline bool classof(
250 const ELFObjectFile *v) {
251 return v->isDyldType();
252 }
253 static inline bool classof(const DyldELFObject *v) {
254 return true;
255 }
256 };
257
258 template
259 DyldELFObject::DyldELFObject(MemoryBuffer *Object,
260 std::vector *MemoryMap, error_code &ec)
261 : ELFObjectFile(Object, ec)
262 , Header(0) {
263 this->isDyldELFObject = true;
264 Header = const_cast(
265 reinterpret_cast(this->base()));
266 if (Header->e_shoff == 0)
267 return;
268
269 // Mark the image as a dynamic shared library
270 Header->e_type = ELF::ET_DYN;
271
272 rebaseObject(MemoryMap);
273 }
274
275 // Walk through the ELF headers, updating virtual addresses to reflect where
276 // the object is currently loaded in memory
277 template
278 void DyldELFObject::rebaseObject(
279 std::vector *MemoryMap) {
280 typedef typename ELFDataTypeTypedefHelper<
281 target_endianness, is64Bits>::value_type addr_type;
282
283 uint8_t *base_p = const_cast(this->base());
284 Elf_Shdr *sectionTable =
285 reinterpret_cast(base_p + Header->e_shoff);
286 uint64_t numSections = this->getNumSections();
287
288 // Allocate memory space for NOBITS sections (such as .bss), which only exist
289 // in memory, but don't occupy space in the object file.
290 // Update the address in the section headers to reflect this allocation.
291 for (uint64_t index = 0; index < numSections; index++) {
292 Elf_Shdr *sec = reinterpret_cast(
293 reinterpret_cast(sectionTable) + index * Header->e_shentsize);
294
295 // Only update sections that are meant to be present in program memory
296 if (sec->sh_flags & ELF::SHF_ALLOC) {
297 uint8_t *addr = base_p + sec->sh_offset;
298 if (sec->sh_type == ELF::SHT_NOBITS) {
299 addr = static_cast(calloc(sec->sh_size, 1));
300 saveAddress(MemoryMap, addr);
301 }
302 else {
303 // FIXME: Currently memory with RWX permissions is allocated. In the
304 // future, make sure that permissions are as necessary
305 if (sec->sh_flags & ELF::SHF_WRITE) {
306 // see FIXME above
307 }
308 if (sec->sh_flags & ELF::SHF_EXECINSTR) {
309 // see FIXME above
310 }
311 }
312 assert(sizeof(addr_type) == sizeof(intptr_t) &&
313 "Cross-architecture ELF dy-load is not supported!");
314 sec->sh_addr = static_cast(intptr_t(addr));
315 }
316 }
317
318 // Now allocate actual space for COMMON symbols, which also don't occupy
319 // space in the object file.
320 // We want to allocate space for all COMMON symbols at once, so the flow is:
321 // 1. Go over all symbols, find those that are in COMMON. For each such
322 // symbol, record its size and the value field in its symbol header in a
323 // special vector.
324 // 2. Allocate memory for all COMMON symbols in one fell swoop.
325 // 3. Using the recorded information from (1), update the address fields in
326 // the symbol headers of the COMMON symbols to reflect their allocated
327 // address.
328 uint64_t TotalSize = 0;
329 std::vector > SymbAddrInfo;
330 error_code ec = object_error::success;
331 for (symbol_iterator si = this->begin_symbols(),
332 se = this->end_symbols(); si != se; si.increment(ec)) {
333 uint64_t Size = 0;
334 ec = si->getSize(Size);
335 Elf_Sym* symb = const_cast(
336 this->getSymbol(si->getRawDataRefImpl()));
337 if (ec == object_error::success &&
338 this->getSymbolTableIndex(symb) == ELF::SHN_COMMON && Size > 0) {
339 SymbAddrInfo.push_back(std::make_pair(&(symb->st_value), Size));
340 TotalSize += Size;
341 }
342 }
343
344 uint8_t* SectionPtr = (uint8_t *)calloc(TotalSize, 1);
345 saveAddress(MemoryMap, SectionPtr);
346
347 typedef typename std::vector >::iterator
348 AddrInfoIterator;
349 AddrInfoIterator EndIter = SymbAddrInfo.end();
350 for (AddrInfoIterator AddrIter = SymbAddrInfo.begin();
351 AddrIter != EndIter; ++AddrIter) {
352 assert(sizeof(addr_type) == sizeof(intptr_t) &&
353 "Cross-architecture ELF dy-load is not supported!");
354 *(AddrIter->first) = static_cast(intptr_t(SectionPtr));
355 SectionPtr += AddrIter->second;
356 }
357 }
358
359 // Record memory addresses for callers
360 template
361 void DyldELFObject::saveAddress(
362 std::vector *MemoryMap, uint8_t* addr) {
363 if (MemoryMap)
364 MemoryMap->push_back(addr);
365 else
366 errs() << "WARNING: Memory leak - cannot record memory for ELF dyld.";
367 }
368
369 template
370 error_code DyldELFObject::getSymbolAddress(
371 DataRefImpl Symb, uint64_t &Result) const {
372 this->validateSymbol(Symb);
373 const Elf_Sym *symb = this->getSymbol(Symb);
374 if (this->getSymbolTableIndex(symb) == ELF::SHN_COMMON) {
375 Result = symb->st_value;
376 return object_error::success;
377 }
378 else {
379 return ELFObjectFile::getSymbolAddress(
380 Symb, Result);
381 }
382 }
383
384 }
385
386 #endif
387
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This file defines the ELFObjectFile and DyldELFObject classes.
9 // Part of the ELFObjectFile class implementation.
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/Object/ObjectFile.h"
18 #include "llvm/Support/Casting.h"
19 #include "llvm/Support/ELF.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include
25 #include
26 #include
13 #include "llvm/Object/ELF.h"
2714
28 using namespace llvm;
15 namespace llvm {
16
2917 using namespace object;
3018
31 // Templates to choose Elf_Addr and Elf_Off depending on is64Bits.
3219 namespace {
33 template
34 struct ELFDataTypeTypedefHelperCommon {
35 typedef support::detail::packed_endian_specific_integral
36 Elf_Half;
37 typedef support::detail::packed_endian_specific_integral
38 Elf_Word;
39 typedef support::detail::packed_endian_specific_integral
40 Elf_Sword;
41 typedef support::detail::packed_endian_specific_integral
42 Elf_Xword;
43 typedef support::detail::packed_endian_specific_integral
44 Elf_Sxword;
45 };
46 }
47
48 namespace {
49 template
50 struct ELFDataTypeTypedefHelper;
51
52 /// ELF 32bit types.
53 template
54 struct ELFDataTypeTypedefHelper
55 : ELFDataTypeTypedefHelperCommon {
56 typedef uint32_t value_type;
57 typedef support::detail::packed_endian_specific_integral
58 Elf_Addr;
59 typedef support::detail::packed_endian_specific_integral
60 Elf_Off;
61 };
62
63 /// ELF 64bit types.
64 template
65 struct ELFDataTypeTypedefHelper
66 : ELFDataTypeTypedefHelperCommon{
67 typedef uint64_t value_type;
68 typedef support::detail::packed_endian_specific_integral
69 Elf_Addr;
70 typedef support::detail::packed_endian_specific_integral
71 Elf_Off;
72 };
73 }
74
75 // I really don't like doing this, but the alternative is copypasta.
76 #define LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) \
77 typedef typename \
78 ELFDataTypeTypedefHelper::Elf_Addr Elf_Addr; \
79 typedef typename \
80 ELFDataTypeTypedefHelper::Elf_Off Elf_Off; \
81 typedef typename \
82 ELFDataTypeTypedefHelper::Elf_Half Elf_Half; \
83 typedef typename \
84 ELFDataTypeTypedefHelper::Elf_Word Elf_Word; \
85