1 //===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MachOObjectFile class, which binds the MachOObject
11 // class to the generic ObjectFile wrapper.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/Object/MachOFormat.h"
17 #include "llvm/Object/MachOObject.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Support/MemoryBuffer.h"
26 using namespace object;
30 typedef MachOObject::LoadCommandInfo LoadCommandInfo;
32 class MachOObjectFile : public ObjectFile {
34 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO)
37 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {}
39 virtual symbol_iterator begin_symbols() const;
40 virtual symbol_iterator end_symbols() const;
41 virtual section_iterator begin_sections() const;
42 virtual section_iterator end_sections() const;
44 virtual uint8_t getBytesInAddress() const;
45 virtual StringRef getFileFormatName() const;
46 virtual unsigned getArch() const;
49 virtual SymbolRef getSymbolNext(DataRefImpl Symb) const;
50 virtual StringRef getSymbolName(DataRefImpl Symb) const;
51 virtual uint64_t getSymbolAddress(DataRefImpl Symb) const;
52 virtual uint64_t getSymbolSize(DataRefImpl Symb) const;
53 virtual char getSymbolNMTypeChar(DataRefImpl Symb) const;
54 virtual bool isSymbolInternal(DataRefImpl Symb) const;
56 virtual SectionRef getSectionNext(DataRefImpl Sec) const;
57 virtual StringRef getSectionName(DataRefImpl Sec) const;
58 virtual uint64_t getSectionAddress(DataRefImpl Sec) const;
59 virtual uint64_t getSectionSize(DataRefImpl Sec) const;
60 virtual StringRef getSectionContents(DataRefImpl Sec) const;
61 virtual bool isSectionText(DataRefImpl Sec) const;
64 MachOObject *MachOObj;
65 mutable uint32_t RegisteredStringTable;
67 void moveToNextSection(DataRefImpl &DRI) const;
68 void getSymbolTableEntry(DataRefImpl DRI,
69 InMemoryStruct<macho::SymbolTableEntry> &Res) const;
70 void moveToNextSymbol(DataRefImpl &DRI) const;
71 void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const;
74 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
76 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
79 return new MachOObjectFile(Buffer, MachOObj);
82 /*===-- Symbols -----------------------------------------------------------===*/
84 void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
85 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
86 while (DRI.d.a < LoadCommandCount) {
87 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
88 if (LCI.Command.Type == macho::LCT_Symtab) {
89 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
90 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
91 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
100 void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
101 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
102 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
103 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
104 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
106 if (RegisteredStringTable != DRI.d.a) {
107 MachOObj->RegisterStringTable(*SymtabLoadCmd);
108 RegisteredStringTable = DRI.d.a;
111 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
116 SymbolRef MachOObjectFile::getSymbolNext(DataRefImpl DRI) const {
118 moveToNextSymbol(DRI);
119 return SymbolRef(DRI, this);
122 StringRef MachOObjectFile::getSymbolName(DataRefImpl DRI) const {
123 InMemoryStruct<macho::SymbolTableEntry> Entry;
124 getSymbolTableEntry(DRI, Entry);
125 return MachOObj->getStringAtIndex(Entry->StringIndex);
128 uint64_t MachOObjectFile::getSymbolAddress(DataRefImpl DRI) const {
129 InMemoryStruct<macho::SymbolTableEntry> Entry;
130 getSymbolTableEntry(DRI, Entry);
134 uint64_t MachOObjectFile::getSymbolSize(DataRefImpl DRI) const {
135 return UnknownAddressOrSize;
138 char MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI) const {
139 InMemoryStruct<macho::SymbolTableEntry> Entry;
140 getSymbolTableEntry(DRI, Entry);
143 switch (Entry->Type & macho::STF_TypeMask) {
144 case macho::STT_Undefined:
147 case macho::STT_Absolute:
148 case macho::STT_Section:
156 if (Entry->Flags & (macho::STF_External | macho::STF_PrivateExtern))
157 Char = toupper(Char);
161 bool MachOObjectFile::isSymbolInternal(DataRefImpl DRI) const {
162 InMemoryStruct<macho::SymbolTableEntry> Entry;
163 getSymbolTableEntry(DRI, Entry);
164 return Entry->Flags & macho::STF_StabsEntryMask;
167 ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
168 // DRI.d.a = segment number; DRI.d.b = symbol index.
170 DRI.d.a = DRI.d.b = 0;
171 moveToNextSymbol(DRI);
172 return symbol_iterator(SymbolRef(DRI, this));
175 ObjectFile::symbol_iterator MachOObjectFile::end_symbols() const {
177 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
179 return symbol_iterator(SymbolRef(DRI, this));
183 /*===-- Sections ----------------------------------------------------------===*/
185 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
186 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
187 while (DRI.d.a < LoadCommandCount) {
188 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
189 if (LCI.Command.Type == macho::LCT_Segment) {
190 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
191 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
192 if (DRI.d.b < SegmentLoadCmd->NumSections)
194 } else if (LCI.Command.Type == macho::LCT_Segment64) {
195 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
196 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
197 if (DRI.d.b < Segment64LoadCmd->NumSections)
206 SectionRef MachOObjectFile::getSectionNext(DataRefImpl DRI) const {
208 moveToNextSection(DRI);
209 return SectionRef(DRI, this);
213 MachOObjectFile::getSection(DataRefImpl DRI,
214 InMemoryStruct<macho::Section> &Res) const {
215 InMemoryStruct<macho::SegmentLoadCommand> SLC;
216 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
217 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
218 MachOObj->ReadSection(LCI, DRI.d.b, Res);
221 StringRef MachOObjectFile::getSectionName(DataRefImpl DRI) const {
222 InMemoryStruct<macho::SegmentLoadCommand> SLC;
223 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
224 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
225 InMemoryStruct<macho::Section> Sect;
226 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
228 static char Result[34];
229 strcpy(Result, SLC->Name);
231 strcat(Result, Sect->Name);
232 return StringRef(Result);
235 uint64_t MachOObjectFile::getSectionAddress(DataRefImpl DRI) const {
236 InMemoryStruct<macho::Section> Sect;
237 getSection(DRI, Sect);
238 return Sect->Address;
241 uint64_t MachOObjectFile::getSectionSize(DataRefImpl DRI) const {
242 InMemoryStruct<macho::Section> Sect;
243 getSection(DRI, Sect);
247 StringRef MachOObjectFile::getSectionContents(DataRefImpl DRI) const {
248 InMemoryStruct<macho::Section> Sect;
249 getSection(DRI, Sect);
250 return MachOObj->getData(Sect->Offset, Sect->Size);
253 bool MachOObjectFile::isSectionText(DataRefImpl DRI) const {
254 InMemoryStruct<macho::SegmentLoadCommand> SLC;
255 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
256 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
257 return !strcmp(SLC->Name, "__TEXT");
260 ObjectFile::section_iterator MachOObjectFile::begin_sections() const {
262 DRI.d.a = DRI.d.b = 0;
263 moveToNextSection(DRI);
264 return section_iterator(SectionRef(DRI, this));
267 ObjectFile::section_iterator MachOObjectFile::end_sections() const {
269 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
271 return section_iterator(SectionRef(DRI, this));
274 /*===-- Miscellaneous -----------------------------------------------------===*/
276 uint8_t MachOObjectFile::getBytesInAddress() const {
277 return MachOObj->is64Bit() ? 8 : 4;
280 StringRef MachOObjectFile::getFileFormatName() const {
281 if (!MachOObj->is64Bit()) {
282 switch (MachOObj->getHeader().CPUType) {
284 return "Mach-O 32-bit i386";
288 return "Mach-O 32-bit ppc";
290 assert((MachOObj->getHeader().CPUType & 0x01000000) == 0 &&
291 "64-bit object file when we're not 64-bit?");
292 return "Mach-O 32-bit unknown";
296 switch (MachOObj->getHeader().CPUType) {
298 return "Mach-O 64-bit x86-64";
300 return "Mach-O 64-bit ppc64";
302 assert((MachOObj->getHeader().CPUType & 0x01000000) == 1 &&
303 "32-bit object file when we're 64-bit?");
304 return "Mach-O 64-bit unknown";
308 unsigned MachOObjectFile::getArch() const {
309 switch (MachOObj->getHeader().CPUType) {
313 return Triple::x86_64;
319 return Triple::ppc64;
321 return Triple::UnknownArch;
325 } // end namespace llvm