llvm.org GIT mirror llvm / 4344b1e
Change relocation API to be per section. This time without breaking GCC. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141385 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 8 years ago
8 changed file(s) with 569 addition(s) and 211 deletion(s). Raw diff Collapse all Expand all
8383 error_code getSection(int32_t index,
8484 const coff_section *&Res) const;
8585 error_code getString(uint32_t offset, StringRef &Res) const;
86 error_code getSymbol(uint32_t index,
87 const coff_symbol *&Res) const;
8688
8789 const coff_symbol *toSymb(DataRefImpl Symb) const;
8890 const coff_section *toSec(DataRefImpl Sec) const;
109111 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
110112 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
111113 bool &Result) const;
114 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
115 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
112116
113117 virtual error_code getRelocationNext(DataRefImpl Rel,
114118 RelocationRef &Res) const;
118122 SymbolRef &Res) const;
119123 virtual error_code getRelocationType(DataRefImpl Rel,
120124 uint32_t &Res) const;
125 virtual error_code getRelocationTypeName(DataRefImpl Rel,
126 SmallVectorImpl &Result) const;
121127 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
122128 int64_t &Res) const;
129 virtual error_code getRelocationValueString(DataRefImpl Rel,
130 SmallVectorImpl &Result) const;
131
123132 public:
124133 COFFObjectFile(MemoryBuffer *Object, error_code &ec);
125134 virtual symbol_iterator begin_symbols() const;
126135 virtual symbol_iterator end_symbols() const;
127136 virtual section_iterator begin_sections() const;
128137 virtual section_iterator end_sections() const;
129 virtual relocation_iterator begin_relocations() const;
130 virtual relocation_iterator end_relocations() const;
131138
132139 virtual uint8_t getBytesInAddress() const;
133140 virtual StringRef getFileFormatName() const;
2727
2828 union DataRefImpl {
2929 struct {
30 // ELF needs this for relocations. This entire union should probably be a
31 // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
32 uint16_t a, b;
33 uint32_t c;
34 } w;
35 struct {
3036 uint32_t a, b;
3137 } d;
3238 uintptr_t p;
39 };
40
41 template
42 class content_iterator {
43 content_type Current;
44 public:
45 content_iterator(content_type symb)
46 : Current(symb) {}
47
48 const content_type* operator->() const {
49 return &Current;
50 }
51
52 const content_type &operator*() const {
53 return Current;
54 }
55
56 bool operator==(const content_iterator &other) const {
57 return Current == other.Current;
58 }
59
60 bool operator!=(const content_iterator &other) const {
61 return !(*this == other);
62 }
63
64 content_iterator& increment(error_code &err) {
65 content_type next;
66 if (error_code ec = Current.getNext(next))
67 err = ec;
68 else
69 Current = next;
70 return *this;
71 }
3372 };
3473
3574 static bool operator ==(const DataRefImpl &a, const DataRefImpl &b) {
81120 /// such as library functions
82121 error_code isGlobal(bool &Result) const;
83122 };
123 typedef content_iterator symbol_iterator;
84124
85125 /// RelocationRef - This is a value type class that represents a single
86126 /// relocation in the list of relocations in the object file.
102142 error_code getAddress(uint64_t &Result) const;
103143 error_code getSymbol(SymbolRef &Result) const;
104144 error_code getType(uint32_t &Result) const;
145
146 /// @brief Get a string that represents the type of this relocation.
147 ///
148 /// This is for display purposes only.
149 error_code getTypeName(SmallVectorImpl &Result) const;
105150 error_code getAdditionalInfo(int64_t &Result) const;
106 };
151
152 /// @brief Get a string that represents the calculation of the value of this
153 /// relocation.
154 ///
155 /// This is for display purposes only.
156 error_code getValueString(SmallVectorImpl &Result) const;
157 };
158 typedef content_iterator relocation_iterator;
107159
108160 /// SectionRef - This is a value type class that represents a single section in
109161 /// the list of sections in the object file.
134186 error_code isBSS(bool &Result) const;
135187
136188 error_code containsSymbol(SymbolRef S, bool &Result) const;
137 };
189
190 relocation_iterator begin_relocations() const;
191 relocation_iterator end_relocations() const;
192 };
193 typedef content_iterator section_iterator;
138194
139195 const uint64_t UnknownAddressOrSize = ~0ULL;
140196
184240 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0;
185241 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
186242 bool &Result) const = 0;
243 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const = 0;
244 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const = 0;
187245
188246
189247 // Same as above for RelocationRef.
196254 SymbolRef &Res) const = 0;
197255 virtual error_code getRelocationType(DataRefImpl Rel,
198256 uint32_t &Res) const = 0;
257 virtual error_code getRelocationTypeName(DataRefImpl Rel,
258 SmallVectorImpl &Result) const = 0;
199259 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
200260 int64_t &Res) const = 0;
201
202 public:
203 template
204 class content_iterator {
205 content_type Current;
206 public:
207 content_iterator(content_type symb)
208 : Current(symb) {}
209
210 const content_type* operator->() const {
211 return &Current;
212 }
213
214 const content_type &operator*() const {
215 return Current;
216 }
217
218 bool operator==(const content_iterator &other) const {
219 return Current == other.Current;
220 }
221
222 bool operator!=(const content_iterator &other) const {
223 return !(*this == other);
224 }
225
226 content_iterator& increment(error_code &err) {
227 content_type next;
228 if (error_code ec = Current.getNext(next))
229 err = ec;
230 else
231 Current = next;
232 return *this;
233 }
234 };
235
236 typedef content_iterator symbol_iterator;
237 typedef content_iterator section_iterator;
238 typedef content_iterator relocation_iterator;
261 virtual error_code getRelocationValueString(DataRefImpl Rel,
262 SmallVectorImpl &Result) const = 0;
263
264 public:
239265
240266 virtual symbol_iterator begin_symbols() const = 0;
241267 virtual symbol_iterator end_symbols() const = 0;
242268
243269 virtual section_iterator begin_sections() const = 0;
244270 virtual section_iterator end_sections() const = 0;
245
246 virtual relocation_iterator begin_relocations() const = 0;
247 virtual relocation_iterator end_relocations() const = 0;
248271
249272 /// @brief The number of bytes used to represent an address in this object
250273 /// file format.
365388 Result);
366389 }
367390
391 inline relocation_iterator SectionRef::begin_relocations() const {
392 return OwningObject->getSectionRelBegin(SectionPimpl);
393 }
394
395 inline relocation_iterator SectionRef::end_relocations() const {
396 return OwningObject->getSectionRelEnd(SectionPimpl);
397 }
398
368399
369400 /// RelocationRef
370401 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
392423 return OwningObject->getRelocationType(RelocationPimpl, Result);
393424 }
394425
426 inline error_code RelocationRef::getTypeName(SmallVectorImpl &Result)
427 const {
428 return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
429 }
430
395431 inline error_code RelocationRef::getAdditionalInfo(int64_t &Result) const {
396432 return OwningObject->getRelocationAdditionalInfo(RelocationPimpl, Result);
397433 }
398434
435 inline error_code RelocationRef::getValueString(SmallVectorImpl &Result)
436 const {
437 return OwningObject->getRelocationValueString(RelocationPimpl, Result);
438 }
439
399440 } // end namespace object
400441 } // end namespace llvm
401442
5858 return reinterpret_cast(const_cast(OF));
5959 }
6060
61 inline ObjectFile::section_iterator *unwrap(LLVMSectionIteratorRef SI) {
62 return reinterpret_cast(SI);
61 inline section_iterator *unwrap(LLVMSectionIteratorRef SI) {
62 return reinterpret_cast(SI);
6363 }
6464
6565 inline LLVMSectionIteratorRef
66 wrap(const ObjectFile::section_iterator *SI) {
66 wrap(const section_iterator *SI) {
6767 return reinterpret_cast
68 (const_cast<ObjectFile::section_iterator*>(SI));
68 (const_cast<section_iterator*>(SI));
6969 }
7070 }
7171 }
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/Object/COFF.h"
14 #include "llvm/ADT/SmallString.h"
1415 #include "llvm/ADT/StringSwitch.h"
1516 #include "llvm/ADT/Triple.h"
1617
363364 return object_error::success;
364365 }
365366
367 relocation_iterator COFFObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
368 const coff_section *sec = toSec(Sec);
369 DataRefImpl ret;
370 std::memset(&ret, 0, sizeof(ret));
371 if (sec->NumberOfRelocations == 0)
372 ret.p = 0;
373 else
374 ret.p = reinterpret_cast(base() + sec->PointerToRelocations);
375
376 return relocation_iterator(RelocationRef(ret, this));
377 }
378
379 relocation_iterator COFFObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
380 const coff_section *sec = toSec(Sec);
381 DataRefImpl ret;
382 std::memset(&ret, 0, sizeof(ret));
383 if (sec->NumberOfRelocations == 0)
384 ret.p = 0;
385 else
386 ret.p = reinterpret_cast(
387 reinterpret_cast(
388 base() + sec->PointerToRelocations)
389 + sec->NumberOfRelocations);
390
391 return relocation_iterator(RelocationRef(ret, this));
392 }
393
366394 COFFObjectFile::COFFObjectFile(MemoryBuffer *Object, error_code &ec)
367395 : ObjectFile(Binary::isCOFF, Object, ec) {
368396 // Check that we at least have enough room for a header.
426454 ec = object_error::success;
427455 }
428456
429 ObjectFile::symbol_iterator COFFObjectFile::begin_symbols() const {
457 symbol_iterator COFFObjectFile::begin_symbols() const {
430458 DataRefImpl ret;
431459 std::memset(&ret, 0, sizeof(DataRefImpl));
432460 ret.p = reinterpret_cast(SymbolTable);
433461 return symbol_iterator(SymbolRef(ret, this));
434462 }
435463
436 ObjectFile::symbol_iterator COFFObjectFile::end_symbols() const {
464 symbol_iterator COFFObjectFile::end_symbols() const {
437465 // The symbol table ends where the string table begins.
438466 DataRefImpl ret;
439467 std::memset(&ret, 0, sizeof(DataRefImpl));
441469 return symbol_iterator(SymbolRef(ret, this));
442470 }
443471
444 ObjectFile::section_iterator COFFObjectFile::begin_sections() const {
472 section_iterator COFFObjectFile::begin_sections() const {
445473 DataRefImpl ret;
446474 std::memset(&ret, 0, sizeof(DataRefImpl));
447475 ret.p = reinterpret_cast(SectionTable);
448476 return section_iterator(SectionRef(ret, this));
449477 }
450478
451 ObjectFile::section_iterator COFFObjectFile::end_sections() const {
479 section_iterator COFFObjectFile::end_sections() const {
452480 DataRefImpl ret;
453481 std::memset(&ret, 0, sizeof(DataRefImpl));
454482 ret.p = reinterpret_cast(SectionTable + Header->NumberOfSections);
507535 return object_error::success;
508536 }
509537
538 error_code COFFObjectFile::getSymbol(uint32_t index,
539 const coff_symbol *&Result) const {
540 if (index > 0 && index < Header->NumberOfSymbols)
541 Result = SymbolTable + index;
542 else
543 return object_error::parse_failed;
544 return object_error::success;
545 }
546
510547 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
511 assert(Rel.d.b < Header->NumberOfSections && "Section index out of range!");
512 const coff_section *Sect = NULL;
513 getSection(Rel.d.b, Sect);
514 assert(Rel.d.a < Sect->NumberOfRelocations && "Relocation index out of range!");
515 return
516 reinterpret_cast(base() +
517 Sect->PointerToRelocations) +
518 Rel.d.a;
548 return reinterpret_cast(Rel.p);
519549 }
520550 error_code COFFObjectFile::getRelocationNext(DataRefImpl Rel,
521551 RelocationRef &Res) const {
522 const coff_section *Sect = NULL;
523 if (error_code ec = getSection(Rel.d.b, Sect))
524 return ec;
525 if (++Rel.d.a >= Sect->NumberOfRelocations) {
526 Rel.d.a = 0;
527 while (++Rel.d.b < Header->NumberOfSections) {
528 const coff_section *Sect = NULL;
529 getSection(Rel.d.b, Sect);
530 if (Sect->NumberOfRelocations > 0)
531 break;
532 }
533 }
552 Rel.p = reinterpret_cast(
553 reinterpret_cast(Rel.p) + 1);
534554 Res = RelocationRef(Rel, this);
535555 return object_error::success;
536556 }
537557 error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
538558 uint64_t &Res) const {
539 const coff_section *Sect = NULL;
540 if (error_code ec = getSection(Rel.d.b, Sect))
541 return ec;
542 const coff_relocation* R = toRel(Rel);
543 Res = reinterpret_cast(base() +
544 Sect->PointerToRawData +
545 R->VirtualAddress);
559 Res = toRel(Rel)->VirtualAddress;
546560 return object_error::success;
547561 }
548562 error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel,
559573 Res = R->Type;
560574 return object_error::success;
561575 }
576
577 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(enum) \
578 case COFF::enum: res = #enum; break;
579
580 error_code COFFObjectFile::getRelocationTypeName(DataRefImpl Rel,
581 SmallVectorImpl &Result) const {
582 const coff_relocation *reloc = toRel(Rel);
583 StringRef res;
584 switch (Header->Machine) {
585 case COFF::IMAGE_FILE_MACHINE_AMD64:
586 switch (reloc->Type) {
587 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ABSOLUTE);
588 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR64);
589 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32);
590 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_ADDR32NB);
591 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32);
592 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_1);
593 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_2);
594 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_3);
595 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_4);
596 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_REL32_5);
597 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECTION);
598 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL);
599 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SECREL7);
600 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_TOKEN);
601 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SREL32);
602 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_PAIR);
603 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_AMD64_SSPAN32);
604 default:
605 res = "Unknown";
606 }
607 break;
608 case COFF::IMAGE_FILE_MACHINE_I386:
609 switch (reloc->Type) {
610 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_ABSOLUTE);
611 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR16);
612 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL16);
613 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32);
614 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_DIR32NB);
615 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SEG12);
616 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECTION);
617 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL);
618 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_TOKEN);
619 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_SECREL7);
620 LLVM_COFF_SWITCH_RELOC_TYPE_NAME(IMAGE_REL_I386_REL32);
621 default:
622 res = "Unknown";
623 }
624 break;
625 default:
626 res = "Unknown";
627 }
628 Result.append(res.begin(), res.end());
629 return object_error::success;
630 }
631
632 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
633
562634 error_code COFFObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
563635 int64_t &Res) const {
564636 Res = 0;
565637 return object_error::success;
566638 }
567 ObjectFile::relocation_iterator COFFObjectFile::begin_relocations() const {
568 DataRefImpl ret;
569 ret.d.a = 0;
570 ret.d.b = 1;
571 return relocation_iterator(RelocationRef(ret, this));
572 }
573 ObjectFile::relocation_iterator COFFObjectFile::end_relocations() const {
574 DataRefImpl ret;
575 ret.d.a = 0;
576 ret.d.b = Header->NumberOfSections;
577 return relocation_iterator(RelocationRef(ret, this));
578 }
579
639 error_code COFFObjectFile::getRelocationValueString(DataRefImpl Rel,
640 SmallVectorImpl &Result) const {
641 const coff_relocation *reloc = toRel(Rel);
642 const coff_symbol *symb;
643 if (error_code ec = getSymbol(reloc->SymbolTableIndex, symb)) return ec;
644 DataRefImpl sym;
645 ::memset(&sym, 0, sizeof(sym));
646 sym.p = reinterpret_cast(symb);
647 StringRef symname;
648 if (error_code ec = getSymbolName(sym, symname)) return ec;
649 Result.append(symname.begin(), symname.end());
650 return object_error::success;
651 }
580652
581653 namespace llvm {
582654
1919 #include "llvm/Support/Endian.h"
2020 #include "llvm/Support/ErrorHandling.h"
2121 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include
2224 #include
2325 #include
2426
285287
286288 typedef SmallVector Sections_t;
287289 typedef DenseMap IndexMap_t;
290 typedef DenseMap > RelocMap_t;
288291
289292 const Elf_Ehdr *Header;
290293 const Elf_Shdr *SectionHeaderTable;
292295 const Elf_Shdr *dot_strtab_sec; // Symbol header string table.
293296 Sections_t SymbolTableSections;
294297 IndexMap_t SymbolTableSectionsIndexMap;
295 Sections_t RelocationTableSections;
298
299 /// @brief Map sections to an array of relocation sections that reference
300 /// them sorted by section index.
301 RelocMap_t SectionRelocMap;
302
303 /// @brief Get the relocation section that contains \a Rel.
304 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
305 return getSection(Rel.w.b);
306 }
296307
297308 void validateSymbol(DataRefImpl Symb) const;
298309 bool isRelocationHasAddend(DataRefImpl Rel) const;
299310 template
300 const T *getEntry(DataRefImpl Entry, Sections_t Sections) const;
311 const T *getEntry(uint16_t Section, uint32_t Entry) const;
312 template
313 const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const;
301314 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
302315 const Elf_Shdr *getSection(DataRefImpl index) const;
303316 const Elf_Shdr *getSection(uint16_t index) const;
305318 const Elf_Rela *getRela(DataRefImpl Rela) const;
306319 const char *getString(uint16_t section, uint32_t offset) const;
307320 const char *getString(const Elf_Shdr *section, uint32_t offset) const;
321 error_code getSymbolName(const Elf_Sym *Symb, StringRef &Res) const;
308322
309323 protected:
310324 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
327341 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
328342 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
329343 bool &Result) const;
344 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
345 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
330346
331347 virtual error_code getRelocationNext(DataRefImpl Rel,
332348 RelocationRef &Res) const;
336352 SymbolRef &Res) const;
337353 virtual error_code getRelocationType(DataRefImpl Rel,
338354 uint32_t &Res) const;
355 virtual error_code getRelocationTypeName(DataRefImpl Rel,
356 SmallVectorImpl &Result) const;
339357 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
340358 int64_t &Res) const;
359 virtual error_code getRelocationValueString(DataRefImpl Rel,
360 SmallVectorImpl &Result) const;
341361
342362 public:
343363 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
345365 virtual symbol_iterator end_symbols() const;
346366 virtual section_iterator begin_sections() const;
347367 virtual section_iterator end_sections() const;
348 virtual relocation_iterator begin_relocations() const;
349 virtual relocation_iterator end_relocations() const;
350368
351369 virtual uint8_t getBytesInAddress() const;
352370 virtual StringRef getFileFormatName() const;
403421 StringRef &Result) const {
404422 validateSymbol(Symb);
405423 const Elf_Sym *symb = getSymbol(Symb);
406 if (symb->st_name == 0) {
407 const Elf_Shdr *section = getSection(symb->st_shndx);
408 if (!section)
409 Result = "";
410 else
411 Result = getString(dot_shstrtab_sec, section->sh_name);
412 return object_error::success;
413 }
414
415 // Use the default symbol table name section.
416 Result = getString(dot_strtab_sec, symb->st_name);
417 return object_error::success;
424 return getSymbolName(symb, Result);
418425 }
419426
420427 template
711718 return object_error::success;
712719 }
713720
721 template
722 relocation_iterator ELFObjectFile
723 ::getSectionRelBegin(DataRefImpl Sec) const {
724 DataRefImpl RelData;
725 memset(&RelData, 0, sizeof(RelData));
726 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
727 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
728 if (sec != 0 && ittr != SectionRelocMap.end()) {
729 RelData.w.a = getSection(ittr->second[0])->sh_link;
730 RelData.w.b = ittr->second[0];
731 RelData.w.c = 0;
732 }
733 return relocation_iterator(RelocationRef(RelData, this));
734 }
735
736 template
737 relocation_iterator ELFObjectFile
738 ::getSectionRelEnd(DataRefImpl Sec) const {
739 DataRefImpl RelData;
740 memset(&RelData, 0, sizeof(RelData));
741 const Elf_Shdr *sec = reinterpret_cast(Sec.p);
742 typename RelocMap_t::const_iterator ittr = SectionRelocMap.find(sec);
743 if (sec != 0 && ittr != SectionRelocMap.end()) {
744 // Get the index of the last relocation section for this section.
745 std::size_t relocsecindex = ittr->second[ittr->second.size() - 1];
746 const Elf_Shdr *relocsec = getSection(relocsecindex);
747 RelData.w.a = relocsec->sh_link;
748 RelData.w.b = relocsecindex;
749 RelData.w.c = relocsec->sh_size / relocsec->sh_entsize;
750 }
751 return relocation_iterator(RelocationRef(RelData, this));
752 }
753
714754 // Relocations
715755 template
716756 error_code ELFObjectFile
717757 ::getRelocationNext(DataRefImpl Rel,
718758 RelocationRef &Result) const {
719 const Elf_Shdr *RelocationTableSection = RelocationTableSections[Rel.d.b];
720
721 // Check to see if we are at the end of this relocation table.
722 if (++Rel.d.a >= RelocationTableSection->getEntityCount()) {
723 // We are at the end. If there are other relocation tables, jump to them.
724 Rel.d.a = 0;
725 // Otherwise return the terminator.
726 if (++Rel.d.b >= SymbolTableSections.size()) {
727 Rel.d.a = std::numeric_limits::max();
728 Rel.d.b = std::numeric_limits::max();
729 }
730 }
731
759 ++Rel.w.c;
760 const Elf_Shdr *relocsec = getSection(Rel.w.b);
761 if (Rel.w.c >= (relocsec->sh_size / relocsec->sh_entsize)) {
762 // We have reached the end of the relocations for this section. See if there
763 // is another relocation section.
764 typename RelocMap_t::mapped_type &relocseclist =
765 SectionRelocMap.lookup(getSection(Rel.w.a));
766
767 // Do a binary search for the current reloc section index (which must be
768 // present). Then get the next one.
769 typename RelocMap_t::mapped_type::const_iterator loc =
770 std::lower_bound(relocseclist.begin(), relocseclist.end(), Rel.w.b);
771 ++loc;
772
773 // If there is no next one, don't do anything. The ++Rel.w.c above sets Rel
774 // to the end iterator.
775 if (loc != relocseclist.end()) {
776 Rel.w.b = *loc;
777 Rel.w.a = 0;
778 }
779 }
732780 Result = RelocationRef(Rel, this);
733781 return object_error::success;
734782 }
738786 ::getRelocationSymbol(DataRefImpl Rel,
739787 SymbolRef &Result) const {
740788 uint32_t symbolIdx;
741 const Elf_Shdr *sec = RelocationTableSections[Rel.d.b];
789 const Elf_Shdr *sec = getSection(Rel.w.b);
742790 switch (sec->sh_type) {
743791 default :
744792 report_fatal_error("Invalid section type in Rel!");
766814 ::getRelocationAddress(DataRefImpl Rel,
767815 uint64_t &Result) const {
768816 uint64_t offset;
769 const Elf_Shdr *sec = RelocationTableSections[Rel.d.b];
817 const Elf_Shdr *sec = getSection(Rel.w.b);
770818 switch (sec->sh_type) {
771819 default :
772820 report_fatal_error("Invalid section type in Rel!");
780828 }
781829 }
782830
783 const Elf_Shdr *secAddr = getSection(sec->sh_info);
784 Result = offset + reinterpret_cast(base() + secAddr->sh_offset);
831 Result = offset;
785832 return object_error::success;
786833 }
787834
789836 error_code ELFObjectFile
790837 ::getRelocationType(DataRefImpl Rel,
791838 uint32_t &Result) const {
792 const Elf_Shdr *sec = RelocationTableSections[Rel.d.b];
839 const Elf_Shdr *sec = getSection(Rel.w.b);
793840 switch (sec->sh_type) {
794841 default :
795842 report_fatal_error("Invalid section type in Rel!");
805852 return object_error::success;
806853 }
807854
855 #define LLVM_ELF_SWITCH_RELOC_TYPE_NAME(enum) \
856 case ELF::enum: res = #enum; break;
857
858 template
859 error_code ELFObjectFile
860 ::getRelocationTypeName(DataRefImpl Rel,
861 SmallVectorImpl &Result) const {
862 const Elf_Shdr *sec = getSection(Rel.w.b);
863 uint8_t type;
864 StringRef res;
865 switch (sec->sh_type) {
866 default :
867 return object_error::parse_failed;
868 case ELF::SHT_REL : {
869 type = getRel(Rel)->getType();
870 break;
871 }
872 case ELF::SHT_RELA : {
873 type = getRela(Rel)->getType();
874 break;
875 }
876 }
877 switch (Header->e_machine) {
878 case ELF::EM_X86_64:
879 switch (type) {
880 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_NONE);
881 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_64);
882 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC32);
883 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOT32);
884 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PLT32);
885 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_COPY);
886 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GLOB_DAT);
887 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_JUMP_SLOT);
888 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_RELATIVE);
889 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPCREL);
890 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32);
891 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_32S);
892 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_16);
893 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC16);
894 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_8);
895 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC8);
896 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPMOD64);
897 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF64);
898 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF64);
899 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSGD);
900 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSLD);
901 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_DTPOFF32);
902 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTTPOFF);
903 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TPOFF32);
904 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_PC64);
905 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTOFF64);
906 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32);
907 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE32);
908 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_SIZE64);
909 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_GOTPC32_TLSDESC);
910 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC_CALL);
911 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_X86_64_TLSDESC);
912 default:
913 res = "Unknown";
914 }
915 break;
916 case ELF::EM_386:
917 switch (type) {
918 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_NONE);
919 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32);
920 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC32);
921 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOT32);
922 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PLT32);
923 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_COPY);
924 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GLOB_DAT);
925 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_JUMP_SLOT);
926 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_RELATIVE);
927 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTOFF);
928 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_GOTPC);
929 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_32PLT);
930 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF);
931 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE);
932 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTIE);
933 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE);
934 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD);
935 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM);
936 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_16);
937 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC16);
938 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_8);
939 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_PC8);
940 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_32);
941 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_PUSH);
942 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_CALL);
943 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GD_POP);
944 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_32);
945 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_PUSH);
946 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_CALL);
947 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDM_POP);
948 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LDO_32);
949 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_IE_32);
950 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_LE_32);
951 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPMOD32);
952 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DTPOFF32);
953 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_TPOFF32);
954 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_GOTDESC);
955 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC_CALL);
956 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_TLS_DESC);
957 LLVM_ELF_SWITCH_RELOC_TYPE_NAME(R_386_IRELATIVE);
958 default:
959 res = "Unknown";
960 }
961 break;
962 default:
963 res = "Unknown";
964 }
965 Result.append(res.begin(), res.end());
966 return object_error::success;
967 }
968
969 #undef LLVM_ELF_SWITCH_RELOC_TYPE_NAME
970
808971 template
809972 error_code ELFObjectFile
810973 ::getRelocationAdditionalInfo(DataRefImpl Rel,
811974 int64_t &Result) const {
812 const Elf_Shdr *sec = RelocationTableSections[Rel.d.b];
975 const Elf_Shdr *sec = getSection(Rel.w.b);
813976 switch (sec->sh_type) {
814977 default :
815978 report_fatal_error("Invalid section type in Rel!");
824987 }
825988 }
826989
827
990 template
991 error_code ELFObjectFile
992 ::getRelocationValueString(DataRefImpl Rel,
993 SmallVectorImpl &Result) const {
994 const Elf_Shdr *sec = getSection(Rel.w.b);
995 uint8_t type;
996 StringRef res;
997 int64_t addend = 0;
998 uint16_t symbol_index = 0;
999 switch (sec->sh_type) {
1000 default :
1001 return object_error::parse_failed;
1002 case ELF::SHT_REL : {
1003 type = getRel(Rel)->getType();
1004 symbol_index = getRel(Rel)->getSymbol();
1005 // TODO: Read implicit addend from section data.
1006 break;
1007 }
1008 case ELF::SHT_RELA : {
1009 type = getRela(Rel)->getType();
1010 symbol_index = getRela(Rel)->getSymbol();
1011 addend = getRela(Rel)->r_addend;
1012 break;
1013 }
1014 }
1015 const Elf_Sym *symb = getEntry(sec->sh_link, symbol_index);
1016 StringRef symname;
1017 if (error_code ec = getSymbolName(symb, symname))
1018 return ec;
1019 switch (Header->e_machine) {
1020 case ELF::EM_X86_64:
1021 switch (type) {
1022 case ELF::R_X86_64_32S:
1023 res = symname;
1024 break;
1025 case ELF::R_X86_64_PC32: {
1026 std::string fmtbuf;
1027 raw_string_ostream fmt(fmtbuf);
1028 fmt << symname << (addend < 0 ? "" : "+") << addend << "-P";
1029 fmt.flush();
1030 Result.append(fmtbuf.begin(), fmtbuf.end());
1031 }
1032 break;
1033 default:
1034 res = "Unknown";
1035 }
1036 break;
1037 default:
1038 res = "Unknown";
1039 }
1040 if (Result.empty())
1041 Result.append(res.begin(), res.end());
1042 return object_error::success;
1043 }
8281044
8291045 template
8301046 ELFObjectFile::ELFObjectFile(MemoryBuffer *Object
8481064
8491065
8501066 // To find the symbol tables we walk the section table to find SHT_STMTAB.
851 const Elf_Shdr* sh =
852 reinterpret_cast(SectionHeaderTable);
1067 const Elf_Shdr* sh = reinterpret_cast(SectionHeaderTable);
8531068 for (unsigned i = 0; i < Header->e_shnum; ++i) {
8541069 if (sh->sh_type == ELF::SHT_SYMTAB) {
8551070 SymbolTableSectionsIndexMap[i] = SymbolTableSections.size();
8561071 SymbolTableSections.push_back(sh);
8571072 }
8581073 if (sh->sh_type == ELF::SHT_REL || sh->sh_type == ELF::SHT_RELA) {
859 RelocationTableSections.push_back(sh);
1074 SectionRelocMap[getSection(sh->sh_link)].push_back(i);
8601075 }
8611076 ++sh;
1077 }
1078
1079 // Sort section relocation lists by index.
1080 for (typename RelocMap_t::iterator i = SectionRelocMap.begin(),
1081 e = SectionRelocMap.end(); i != e; ++i) {
1082 std::sort(i->second.begin(), i->second.end());
8621083 }
8631084
8641085 // Get string table sections.
8931114 }
8941115
8951116 template
896 ObjectFile::symbol_iterator ELFObjectFile
897 ::begin_symbols() const {
1117 symbol_iterator ELFObjectFile
1118 ::begin_symbols() const {
8981119 DataRefImpl SymbolData;
8991120 memset(&SymbolData, 0, sizeof(SymbolData));
9001121 if (SymbolTableSections.size() == 0) {
9081129 }
9091130
9101131 template
911 ObjectFile::symbol_iterator ELFObjectFile
912 ::end_symbols() const {
1132 symbol_iterator ELFObjectFile
1133 ::end_symbols() const {
9131134 DataRefImpl SymbolData;
9141135 memset(&SymbolData, 0, sizeof(SymbolData));
9151136 SymbolData.d.a = std::numeric_limits::max();
9181139 }
9191140
9201141 template
921 ObjectFile::section_iterator ELFObjectFile
922 ::begin_sections() const {
1142 section_iterator ELFObjectFile
1143 ::begin_sections() const {
9231144 DataRefImpl ret;
9241145 memset(&ret, 0, sizeof(DataRefImpl));
9251146 ret.p = reinterpret_cast(base() + Header->e_shoff);
9271148 }
9281149
9291150 template
930 ObjectFile::section_iterator ELFObjectFile
931 ::end_sections() const {
1151 section_iterator ELFObjectFile
1152 ::end_sections() const {
9321153 DataRefImpl ret;
9331154 memset(&ret, 0, sizeof(DataRefImpl));
9341155 ret.p = reinterpret_cast(base()
9351156 + Header->e_shoff
9361157 + (Header->e_shentsize * Header->e_shnum));
9371158 return section_iterator(SectionRef(ret, this));
938 }
939
940 template
941 ObjectFile::relocation_iterator ELFObjectFile
942 ::begin_relocations() const {
943 DataRefImpl RelData;
944 memset(&RelData, 0, sizeof(RelData));
945 if (RelocationTableSections.size() == 0) {
946 RelData.d.a = std::numeric_limits::max();
947 RelData.d.b = std::numeric_limits::max();
948 } else {
949 RelData.d.a = 0;
950 RelData.d.b = 0;
951 }
952 return relocation_iterator(RelocationRef(RelData, this));
953 }
954
955 template
956 ObjectFile::relocation_iterator ELFObjectFile
957 ::end_relocations() const {
958 DataRefImpl RelData;
959 memset(&RelData, 0, sizeof(RelData));
960 RelData.d.a = std::numeric_limits::max();
961 RelData.d.b = std::numeric_limits::max();
962 return relocation_iterator(RelocationRef(RelData, this));
9631159 }
9641160
9651161 template
10111207 }
10121208 }
10131209
1210
10141211 template
10151212 template
10161213 inline const T *
1017 ELFObjectFile::getEntry(DataRefImpl Entry,
1018 Sections_t Sections) const {
1019 const Elf_Shdr *sec = Sections[Entry.d.b];
1214 ELFObjectFile::getEntry(uint16_t Section,
1215 uint32_t Entry) const {
1216 return getEntry(getSection(Section), Entry);
1217 }
1218
1219 template
1220 template
1221 inline const T *
1222 ELFObjectFile::getEntry(const Elf_Shdr * Section,
1223 uint32_t Entry) const {
10201224 return reinterpret_cast(
10211225 base()
1022 + sec->sh_offset
1023 + (Entry.d.a * sec->sh_entsize));
1226 + Section->sh_offset
1227 + (Entry * Section->sh_entsize));
10241228 }
10251229
10261230 template
10271231 const typename ELFObjectFile::Elf_Sym *
10281232 ELFObjectFile::getSymbol(DataRefImpl Symb) const {
1029 return getEntry(Symb, SymbolTableSections);
1233 return getEntry(SymbolTableSections[Symb.d.b], Symb.d.a);
10301234 }
10311235
10321236 template
10331237 const typename ELFObjectFile::Elf_Rel *
10341238 ELFObjectFile::getRel(DataRefImpl Rel) const {
1035 return getEntry(Rel, RelocationTableSections);
1239 return getEntry(Rel.w.b, Rel.w.c);
10361240 }
10371241
10381242 template
10391243 const typename ELFObjectFile::Elf_Rela *
10401244 ELFObjectFile::getRela(DataRefImpl Rela) const {
1041 return getEntry(Rela, RelocationTableSections);
1245 return getEntry(Rela.w.b, Rela.w.c);
10421246 }
10431247
10441248 template
10451249 const typename ELFObjectFile::Elf_Shdr *
10461250 ELFObjectFile::getSection(DataRefImpl Symb) const {
10471251 const Elf_Shdr *sec = getSection(Symb.d.b);
1048 if (sec->sh_type != ELF::SHT_SYMTAB)
1252 if (sec->sh_type != ELF::SHT_SYMTAB || sec->sh_type != ELF::SHT_DYNSYM)
10491253 // FIXME: Proper error handling.
10501254 report_fatal_error("Invalid symbol table section!");
10511255 return sec;
10811285 // FIXME: Proper error handling.
10821286 report_fatal_error("Symbol name offset outside of string table!");
10831287 return (const char *)base() + section->sh_offset + offset;
1288 }
1289
1290 template
1291 error_code ELFObjectFile
1292 ::getSymbolName(const Elf_Sym *symb,
1293 StringRef &Result) const {
1294 if (symb->st_name == 0) {
1295 const Elf_Shdr *section = getSection(symb->st_shndx);
1296 if (!section)
1297 Result = "";
1298 else
1299 Result = getString(dot_shstrtab_sec, section->sh_name);
1300 return object_error::success;
1301 }
1302
1303 // Use the default symbol table name section.
1304 Result = getString(dot_strtab_sec, symb->st_name);
1305 return object_error::success;
10841306 }
10851307
10861308 // EI_CLASS, EI_DATA.
3838 virtual symbol_iterator end_symbols() const;
3939 virtual section_iterator begin_sections() const;
4040 virtual section_iterator end_sections() const;
41 virtual relocation_iterator begin_relocations() const;
42 virtual relocation_iterator end_relocations() const;
4341
4442 virtual uint8_t getBytesInAddress() const;
4543 virtual StringRef getFileFormatName() const;
6664 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
6765 virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
6866 bool &Result) const;
67 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
68 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
6969
7070 virtual error_code getRelocationNext(DataRefImpl Rel,
7171 RelocationRef &Res) const;
7575 SymbolRef &Res) const;
7676 virtual error_code getRelocationType(DataRefImpl Rel,
7777 uint32_t &Res) const;
78 virtual error_code getRelocationTypeName(DataRefImpl Rel,
79 SmallVectorImpl &Result) const;
7880 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
7981 int64_t &Res) const;
82 virtual error_code getRelocationValueString(DataRefImpl Rel,
83 SmallVectorImpl &Result) const;
84
8085 private:
8186 MachOObject *MachOObj;
8287 mutable uint32_t RegisteredStringTable;
95100 InMemoryStruct &Res) const;
96101 void getRelocation(DataRefImpl Rel,
97102 InMemoryStruct &Res) const;
103 std::size_t getSectionIndex(DataRefImpl Sec) const;
98104 };
99105
100106 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
323329 }
324330
325331
326 ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
332 symbol_iterator MachOObjectFile::begin_symbols() const {
327333 // DRI.d.a = segment number; DRI.d.b = symbol index.
328334 DataRefImpl DRI;
329335 DRI.d.a = DRI.d.b = 0;
331337 return symbol_iterator(SymbolRef(DRI, this));
332338 }
333339
334 ObjectFile::symbol_iterator MachOObjectFile::end_symbols() const {
340 symbol_iterator MachOObjectFile::end_symbols() const {
335341 DataRefImpl DRI;
336342 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
337343 DRI.d.b = 0;
379385 MachOObj->ReadSection(LCI, DRI.d.b, Res);
380386 }
381387
388 std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
389 SectionList::const_iterator loc =
390 std::find(Sections.begin(), Sections.end(), Sec);
391 assert(loc != Sections.end() && "Sec is not a valid section!");
392 return std::distance(Sections.begin(), loc);
393 }
394
382395 void
383396 MachOObjectFile::getSection64(DataRefImpl DRI,
384397 InMemoryStruct &Res) const {
510523 return object_error::success;
511524 }
512525
513 ObjectFile::section_iterator MachOObjectFile::begin_sections() const {
526 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
527 DataRefImpl ret;
528 ret.d.a = 0;
529 ret.d.b = getSectionIndex(Sec);
530 return relocation_iterator(RelocationRef(ret, this));
531 }
532 relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
533 uint32_t last_reloc;
534 if (is64BitLoadCommand(MachOObj, Sec)) {
535 InMemoryStruct Sect;
536 getSection64(Sec, Sect);
537 last_reloc = Sect->NumRelocationTableEntries;
538 } else {
539 InMemoryStruct Sect;
540 getSection(Sec, Sect);
541 last_reloc = Sect->NumRelocationTableEntries;
542 }
543 DataRefImpl ret;
544 ret.d.a = last_reloc;
545 ret.d.b = getSectionIndex(Sec);
546 return relocation_iterator(RelocationRef(ret, this));
547 }
548
549 section_iterator MachOObjectFile::begin_sections() const {
514550 DataRefImpl DRI;
515551 DRI.d.a = DRI.d.b = 0;
516552 moveToNextSection(DRI);
517553 return section_iterator(SectionRef(DRI, this));
518554 }
519555
520 ObjectFile::section_iterator MachOObjectFile::end_sections() const {
556 section_iterator MachOObjectFile::end_sections() const {
521557 DataRefImpl DRI;
522558 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
523559 DRI.d.b = 0;
544580 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
545581 RelocationRef &Res) const {
546582 ++Rel.d.a;
547 while (Rel.d.b < Sections.size()) {
548 unsigned relocationCount;
549 if (MachOObj->is64Bit()) {
550 InMemoryStruct Sect;
551 getSection64(Sections[Rel.d.b], Sect);
552 relocationCount = Sect->NumRelocationTableEntries;
553 } else {
554 InMemoryStruct Sect;
555 getSection(Sections[Rel.d.b], Sect);
556 relocationCount = Sect->NumRelocationTableEntries;
557 }
558 if (Rel.d.a < relocationCount)
559 break;
560
561 Rel.d.a = 0;
562 ++Rel.d.b;
563 }
564583 Res = RelocationRef(Rel, this);
565584 return object_error::success;
566585 }
607626 InMemoryStruct RE;
608627 getRelocation(Rel, RE);
609628 Res = RE->Word1;
629 return object_error::success;
630 }
631 error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
632 SmallVectorImpl &Result) const {
610633 return object_error::success;
611634 }
612635 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
630653 }
631654 return object_error::success;
632655 }
633 ObjectFile::relocation_iterator MachOObjectFile::begin_relocations() const {
634 DataRefImpl ret;
635 ret.d.a = ret.d.b = 0;
636 return relocation_iterator(RelocationRef(ret, this));
637 }
638 ObjectFile::relocation_iterator MachOObjectFile::end_relocations() const {
639 DataRefImpl ret;
640 ret.d.a = 0;
641 ret.d.b = Sections.size();
642 return relocation_iterator(RelocationRef(ret, this));
656 error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
657 SmallVectorImpl &Result) const {
658 return object_error::success;
643659 }
644660
645661 /*===-- Miscellaneous -----------------------------------------------------===*/
2626 }
2727
2828 LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
29 ObjectFile::section_iterator SI = unwrap(ObjectFile)->begin_sections();
30 return wrap(new ObjectFile::section_iterator(SI));
29 section_iterator SI = unwrap(ObjectFile)->begin_sections();
30 return wrap(new section_iterator(SI));
3131 }
3232
3333 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
154154 << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
155155
156156 error_code ec;
157 for (ObjectFile::section_iterator i = Obj->begin_sections(),
157 for (section_iterator i = Obj->begin_sections(),
158158 e = Obj->end_sections();
159159 i != e; i.increment(ec)) {
160160 if (error(ec)) break;
164164
165165 // Make a list of all the symbols in this section.
166166 std::vector > Symbols;
167 for (ObjectFile::symbol_iterator si = Obj->begin_symbols(),
167 for (symbol_iterator si = Obj->begin_symbols(),
168168 se = Obj->end_symbols();
169169 si != se; si.increment(ec)) {
170170 bool contains;