Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 1 | //===- MachO.h - MachO object file implementation ---------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file declares the MachOObjectFile class, which binds the MachOObject |
| 11 | // class to the generic ObjectFile wrapper. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_OBJECT_MACHO_H |
| 16 | #define LLVM_OBJECT_MACHO_H |
| 17 | |
Chandler Carruth | 255f89f | 2012-12-03 17:02:12 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 19 | #include "llvm/Object/MachOObject.h" |
Chandler Carruth | 255f89f | 2012-12-03 17:02:12 +0000 | [diff] [blame] | 20 | #include "llvm/Object/ObjectFile.h" |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MachO.h" |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | namespace object { |
| 26 | |
| 27 | typedef MachOObject::LoadCommandInfo LoadCommandInfo; |
| 28 | |
| 29 | class MachOObjectFile : public ObjectFile { |
| 30 | public: |
| 31 | MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec); |
| 32 | |
| 33 | virtual symbol_iterator begin_symbols() const; |
| 34 | virtual symbol_iterator end_symbols() const; |
Michael J. Spencer | dfa1896 | 2012-02-28 00:40:37 +0000 | [diff] [blame] | 35 | virtual symbol_iterator begin_dynamic_symbols() const; |
| 36 | virtual symbol_iterator end_dynamic_symbols() const; |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 37 | virtual library_iterator begin_libraries_needed() const; |
| 38 | virtual library_iterator end_libraries_needed() const; |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 39 | virtual section_iterator begin_sections() const; |
| 40 | virtual section_iterator end_sections() const; |
| 41 | |
| 42 | virtual uint8_t getBytesInAddress() const; |
| 43 | virtual StringRef getFileFormatName() const; |
| 44 | virtual unsigned getArch() const; |
David Meyer | 97f7787 | 2012-03-01 22:19:54 +0000 | [diff] [blame] | 45 | virtual StringRef getLoadName() const; |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 46 | |
Rafael Espindola | e3ec87a | 2012-12-13 04:07:18 +0000 | [diff] [blame^] | 47 | // In a MachO file, sections have a segment name. This is used in the .o |
| 48 | // files. They have a single segment, but this field specifies which segment |
| 49 | // a section should be put in in the final object. |
| 50 | error_code getSectionFinalSegmentName(DataRefImpl Sec, StringRef &Res) const; |
| 51 | |
Jim Grosbach | 596e474 | 2012-11-29 19:14:11 +0000 | [diff] [blame] | 52 | MachOObject *getObject() { return MachOObj.get(); } |
Owen Anderson | df3f929 | 2011-10-17 21:37:03 +0000 | [diff] [blame] | 53 | |
Michael J. Spencer | ab6bcf3 | 2011-10-17 23:53:37 +0000 | [diff] [blame] | 54 | static inline bool classof(const Binary *v) { |
David Meyer | 6f9489a | 2012-03-09 20:41:57 +0000 | [diff] [blame] | 55 | return v->isMachO(); |
Michael J. Spencer | ab6bcf3 | 2011-10-17 23:53:37 +0000 | [diff] [blame] | 56 | } |
Michael J. Spencer | ab6bcf3 | 2011-10-17 23:53:37 +0000 | [diff] [blame] | 57 | |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 58 | protected: |
| 59 | virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const; |
| 60 | virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const; |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 61 | virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const; |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 62 | virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const; |
| 63 | virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const; |
| 64 | virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const; |
David Meyer | c46255a | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 65 | virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const; |
Michael J. Spencer | 1130a79 | 2011-10-17 20:19:29 +0000 | [diff] [blame] | 66 | virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const; |
Michael J. Spencer | 9b2b812 | 2011-10-17 23:54:46 +0000 | [diff] [blame] | 67 | virtual error_code getSymbolSection(DataRefImpl Symb, |
| 68 | section_iterator &Res) const; |
Tim Northover | a41dce3 | 2012-10-29 10:47:00 +0000 | [diff] [blame] | 69 | virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const; |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 70 | |
| 71 | virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const; |
| 72 | virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const; |
| 73 | virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const; |
| 74 | virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const; |
| 75 | virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const; |
| 76 | virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const; |
| 77 | virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const; |
| 78 | virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const; |
| 79 | virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const; |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 80 | virtual error_code isSectionRequiredForExecution(DataRefImpl Sec, |
| 81 | bool &Res) const; |
| 82 | virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const; |
| 83 | virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const; |
Andrew Kaylor | 3a129c8 | 2012-10-10 01:41:33 +0000 | [diff] [blame] | 84 | virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const; |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 85 | virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S, |
| 86 | bool &Result) const; |
| 87 | virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const; |
| 88 | virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const; |
| 89 | |
| 90 | virtual error_code getRelocationNext(DataRefImpl Rel, |
| 91 | RelocationRef &Res) const; |
| 92 | virtual error_code getRelocationAddress(DataRefImpl Rel, |
| 93 | uint64_t &Res) const; |
Danil Malyshev | b0436a7 | 2011-11-29 17:40:10 +0000 | [diff] [blame] | 94 | virtual error_code getRelocationOffset(DataRefImpl Rel, |
| 95 | uint64_t &Res) const; |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 96 | virtual error_code getRelocationSymbol(DataRefImpl Rel, |
| 97 | SymbolRef &Res) const; |
| 98 | virtual error_code getRelocationType(DataRefImpl Rel, |
Owen Anderson | 9472b8d | 2011-10-26 17:08:49 +0000 | [diff] [blame] | 99 | uint64_t &Res) const; |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 100 | virtual error_code getRelocationTypeName(DataRefImpl Rel, |
| 101 | SmallVectorImpl<char> &Result) const; |
| 102 | virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel, |
| 103 | int64_t &Res) const; |
| 104 | virtual error_code getRelocationValueString(DataRefImpl Rel, |
| 105 | SmallVectorImpl<char> &Result) const; |
Owen Anderson | 0685e94 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 106 | virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const; |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 107 | |
David Meyer | 5c2b4ea | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 108 | virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const; |
| 109 | virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const; |
| 110 | |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 111 | private: |
Jim Grosbach | 596e474 | 2012-11-29 19:14:11 +0000 | [diff] [blame] | 112 | OwningPtr<MachOObject> MachOObj; |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 113 | mutable uint32_t RegisteredStringTable; |
| 114 | typedef SmallVector<DataRefImpl, 1> SectionList; |
| 115 | SectionList Sections; |
| 116 | |
| 117 | |
| 118 | void moveToNextSection(DataRefImpl &DRI) const; |
| 119 | void getSymbolTableEntry(DataRefImpl DRI, |
| 120 | InMemoryStruct<macho::SymbolTableEntry> &Res) const; |
| 121 | void getSymbol64TableEntry(DataRefImpl DRI, |
| 122 | InMemoryStruct<macho::Symbol64TableEntry> &Res) const; |
| 123 | void moveToNextSymbol(DataRefImpl &DRI) const; |
| 124 | void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const; |
| 125 | void getSection64(DataRefImpl DRI, |
| 126 | InMemoryStruct<macho::Section64> &Res) const; |
| 127 | void getRelocation(DataRefImpl Rel, |
| 128 | InMemoryStruct<macho::RelocationEntry> &Res) const; |
| 129 | std::size_t getSectionIndex(DataRefImpl Sec) const; |
Owen Anderson | d8fa76d | 2011-10-24 23:20:07 +0000 | [diff] [blame] | 130 | |
Owen Anderson | 1832f4d | 2011-10-26 20:42:54 +0000 | [diff] [blame] | 131 | void printRelocationTargetName(InMemoryStruct<macho::RelocationEntry>& RE, |
| 132 | raw_string_ostream &fmt) const; |
Owen Anderson | f7c93a3 | 2011-10-11 17:32:27 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | #endif |
| 139 | |