1 //===- MachO.h - MachO object file implementation ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the MachOObjectFile class, which binds the MachOObject
11 // class to the generic ObjectFile wrapper.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_OBJECT_MACHO_H
16 #define LLVM_OBJECT_MACHO_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Object/MachOObject.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/MachO.h"
23 #include "llvm/Support/raw_ostream.h"
28 typedef MachOObject::LoadCommandInfo LoadCommandInfo;
30 class MachOObjectFile : public ObjectFile {
32 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec);
34 virtual symbol_iterator begin_symbols() const;
35 virtual symbol_iterator end_symbols() const;
36 virtual symbol_iterator begin_dynamic_symbols() const;
37 virtual symbol_iterator end_dynamic_symbols() const;
38 virtual library_iterator begin_libraries_needed() const;
39 virtual library_iterator end_libraries_needed() const;
40 virtual section_iterator begin_sections() const;
41 virtual section_iterator end_sections() const;
43 virtual uint8_t getBytesInAddress() const;
44 virtual StringRef getFileFormatName() const;
45 virtual unsigned getArch() const;
46 virtual StringRef getLoadName() const;
48 // In a MachO file, sections have a segment name. This is used in the .o
49 // files. They have a single segment, but this field specifies which segment
50 // a section should be put in in the final object.
51 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
53 // Names are stored as 16 bytes. These returns the raw 16 bytes without
54 // interpreting them as a C string.
55 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
56 ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const;
58 MachOObject *getObject() { return MachOObj.get(); }
60 static inline bool classof(const Binary *v) {
65 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
66 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
67 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
68 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
69 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
70 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
71 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
72 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
73 virtual error_code getSymbolSection(DataRefImpl Symb,
74 section_iterator &Res) const;
75 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
77 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
78 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
79 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
80 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
81 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
82 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
83 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
84 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
85 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
86 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
88 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
89 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
90 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
91 virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
93 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
94 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
96 virtual error_code getRelocationNext(DataRefImpl Rel,
97 RelocationRef &Res) const;
98 virtual error_code getRelocationAddress(DataRefImpl Rel,
100 virtual error_code getRelocationOffset(DataRefImpl Rel,
101 uint64_t &Res) const;
102 virtual error_code getRelocationSymbol(DataRefImpl Rel,
103 SymbolRef &Res) const;
104 virtual error_code getRelocationType(DataRefImpl Rel,
105 uint64_t &Res) const;
106 virtual error_code getRelocationTypeName(DataRefImpl Rel,
107 SmallVectorImpl<char> &Result) const;
108 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
110 virtual error_code getRelocationValueString(DataRefImpl Rel,
111 SmallVectorImpl<char> &Result) const;
112 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
114 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
115 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
118 OwningPtr<MachOObject> MachOObj;
119 mutable uint32_t RegisteredStringTable;
120 typedef SmallVector<DataRefImpl, 1> SectionList;
121 SectionList Sections;
124 void moveToNextSection(DataRefImpl &DRI) const;
125 void getSymbolTableEntry(DataRefImpl DRI,
126 InMemoryStruct<macho::SymbolTableEntry> &Res) const;
127 void getSymbol64TableEntry(DataRefImpl DRI,
128 InMemoryStruct<macho::Symbol64TableEntry> &Res) const;
129 void moveToNextSymbol(DataRefImpl &DRI) const;
130 void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const;
131 void getSection64(DataRefImpl DRI,
132 InMemoryStruct<macho::Section64> &Res) const;
133 void getRelocation(DataRefImpl Rel,
134 InMemoryStruct<macho::RelocationEntry> &Res) const;
135 std::size_t getSectionIndex(DataRefImpl Sec) const;
137 void printRelocationTargetName(InMemoryStruct<macho::RelocationEntry>& RE,
138 raw_string_ostream &fmt) const;