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"
20 #include "llvm/Support/MachO.h"
27 using namespace object;
31 typedef MachOObject::LoadCommandInfo LoadCommandInfo;
33 class MachOObjectFile : public ObjectFile {
35 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec)
36 : ObjectFile(Binary::isMachO, Object, ec),
38 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {}
40 virtual symbol_iterator begin_symbols() const;
41 virtual symbol_iterator end_symbols() const;
42 virtual section_iterator begin_sections() const;
43 virtual section_iterator end_sections() const;
45 virtual uint8_t getBytesInAddress() const;
46 virtual StringRef getFileFormatName() const;
47 virtual unsigned getArch() const;
50 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
51 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
52 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
53 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
54 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
55 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
57 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
58 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
59 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
60 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
61 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
62 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
65 MachOObject *MachOObj;
66 mutable uint32_t RegisteredStringTable;
68 void moveToNextSection(DataRefImpl &DRI) const;
69 void getSymbolTableEntry(DataRefImpl DRI,
70 InMemoryStruct<macho::SymbolTableEntry> &Res) const;
71 void getSymbol64TableEntry(DataRefImpl DRI,
72 InMemoryStruct<macho::Symbol64TableEntry> &Res) const;
73 void moveToNextSymbol(DataRefImpl &DRI) const;
74 void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const;
75 void getSection64(DataRefImpl DRI,
76 InMemoryStruct<macho::Section64> &Res) const;
79 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
82 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
85 return new MachOObjectFile(Buffer, MachOObj, ec);
88 /*===-- Symbols -----------------------------------------------------------===*/
90 void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
91 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
92 while (DRI.d.a < LoadCommandCount) {
93 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
94 if (LCI.Command.Type == macho::LCT_Symtab) {
95 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
96 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
97 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
106 void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
107 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
108 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
109 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
110 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
112 if (RegisteredStringTable != DRI.d.a) {
113 MachOObj->RegisterStringTable(*SymtabLoadCmd);
114 RegisteredStringTable = DRI.d.a;
117 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
121 void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
122 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
123 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
124 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
125 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
127 if (RegisteredStringTable != DRI.d.a) {
128 MachOObj->RegisterStringTable(*SymtabLoadCmd);
129 RegisteredStringTable = DRI.d.a;
132 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
137 error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
138 SymbolRef &Result) const {
140 moveToNextSymbol(DRI);
141 Result = SymbolRef(DRI, this);
142 return object_error::success;
145 error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
146 StringRef &Result) const {
147 if (MachOObj->is64Bit()) {
148 InMemoryStruct<macho::Symbol64TableEntry> Entry;
149 getSymbol64TableEntry(DRI, Entry);
150 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
152 InMemoryStruct<macho::SymbolTableEntry> Entry;
153 getSymbolTableEntry(DRI, Entry);
154 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
156 return object_error::success;
159 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
160 uint64_t &Result) const {
161 if (MachOObj->is64Bit()) {
162 InMemoryStruct<macho::Symbol64TableEntry> Entry;
163 getSymbol64TableEntry(DRI, Entry);
164 Result = Entry->Value;
166 InMemoryStruct<macho::SymbolTableEntry> Entry;
167 getSymbolTableEntry(DRI, Entry);
168 Result = Entry->Value;
170 return object_error::success;
173 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
174 uint64_t &Result) const {
175 Result = UnknownAddressOrSize;
176 return object_error::success;
179 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
180 char &Result) const {
182 if (MachOObj->is64Bit()) {
183 InMemoryStruct<macho::Symbol64TableEntry> Entry;
184 getSymbol64TableEntry(DRI, Entry);
186 Flags = Entry->Flags;
188 InMemoryStruct<macho::SymbolTableEntry> Entry;
189 getSymbolTableEntry(DRI, Entry);
191 Flags = Entry->Flags;
195 switch (Type & macho::STF_TypeMask) {
196 case macho::STT_Undefined:
199 case macho::STT_Absolute:
200 case macho::STT_Section:
208 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
209 Char = toupper(Char);
211 return object_error::success;
214 error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
215 bool &Result) const {
216 if (MachOObj->is64Bit()) {
217 InMemoryStruct<macho::Symbol64TableEntry> Entry;
218 getSymbol64TableEntry(DRI, Entry);
219 Result = Entry->Flags & macho::STF_StabsEntryMask;
221 InMemoryStruct<macho::SymbolTableEntry> Entry;
222 getSymbolTableEntry(DRI, Entry);
223 Result = Entry->Flags & macho::STF_StabsEntryMask;
225 return object_error::success;
228 ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
229 // DRI.d.a = segment number; DRI.d.b = symbol index.
231 DRI.d.a = DRI.d.b = 0;
232 moveToNextSymbol(DRI);
233 return symbol_iterator(SymbolRef(DRI, this));
236 ObjectFile::symbol_iterator MachOObjectFile::end_symbols() const {
238 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
240 return symbol_iterator(SymbolRef(DRI, this));
244 /*===-- Sections ----------------------------------------------------------===*/
246 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
247 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
248 while (DRI.d.a < LoadCommandCount) {
249 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
250 if (LCI.Command.Type == macho::LCT_Segment) {
251 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
252 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
253 if (DRI.d.b < SegmentLoadCmd->NumSections)
255 } else if (LCI.Command.Type == macho::LCT_Segment64) {
256 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
257 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
258 if (DRI.d.b < Segment64LoadCmd->NumSections)
267 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
268 SectionRef &Result) const {
270 moveToNextSection(DRI);
271 Result = SectionRef(DRI, this);
272 return object_error::success;
276 MachOObjectFile::getSection(DataRefImpl DRI,
277 InMemoryStruct<macho::Section> &Res) const {
278 InMemoryStruct<macho::SegmentLoadCommand> SLC;
279 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
280 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
281 MachOObj->ReadSection(LCI, DRI.d.b, Res);
285 MachOObjectFile::getSection64(DataRefImpl DRI,
286 InMemoryStruct<macho::Section64> &Res) const {
287 InMemoryStruct<macho::Segment64LoadCommand> SLC;
288 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
289 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
290 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
293 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
294 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
295 if (LCI.Command.Type == macho::LCT_Segment64)
297 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
301 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
302 StringRef &Result) const {
303 // FIXME: thread safety.
304 static char result[34];
305 if (is64BitLoadCommand(MachOObj, DRI)) {
306 InMemoryStruct<macho::Segment64LoadCommand> SLC;
307 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
308 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
309 InMemoryStruct<macho::Section64> Sect;
310 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
312 strcpy(result, Sect->SegmentName);
314 strcat(result, Sect->Name);
316 InMemoryStruct<macho::SegmentLoadCommand> SLC;
317 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
318 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
319 InMemoryStruct<macho::Section> Sect;
320 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
322 strcpy(result, Sect->SegmentName);
324 strcat(result, Sect->Name);
326 Result = StringRef(result);
327 return object_error::success;
330 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
331 uint64_t &Result) const {
332 if (is64BitLoadCommand(MachOObj, DRI)) {
333 InMemoryStruct<macho::Section64> Sect;
334 getSection64(DRI, Sect);
335 Result = Sect->Address;
337 InMemoryStruct<macho::Section> Sect;
338 getSection(DRI, Sect);
339 Result = Sect->Address;
341 return object_error::success;
344 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
345 uint64_t &Result) const {
346 if (is64BitLoadCommand(MachOObj, DRI)) {
347 InMemoryStruct<macho::Section64> Sect;
348 getSection64(DRI, Sect);
351 InMemoryStruct<macho::Section> Sect;
352 getSection(DRI, Sect);
355 return object_error::success;
358 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
359 StringRef &Result) const {
360 if (is64BitLoadCommand(MachOObj, DRI)) {
361 InMemoryStruct<macho::Section64> Sect;
362 getSection64(DRI, Sect);
363 Result = MachOObj->getData(Sect->Offset, Sect->Size);
365 InMemoryStruct<macho::Section> Sect;
366 getSection(DRI, Sect);
367 Result = MachOObj->getData(Sect->Offset, Sect->Size);
369 return object_error::success;
372 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
373 bool &Result) const {
374 if (is64BitLoadCommand(MachOObj, DRI)) {
375 InMemoryStruct<macho::Section64> Sect;
376 getSection64(DRI, Sect);
377 Result = !strcmp(Sect->Name, "__text");
379 InMemoryStruct<macho::Section> Sect;
380 getSection(DRI, Sect);
381 Result = !strcmp(Sect->Name, "__text");
383 return object_error::success;
386 ObjectFile::section_iterator MachOObjectFile::begin_sections() const {
388 DRI.d.a = DRI.d.b = 0;
389 moveToNextSection(DRI);
390 return section_iterator(SectionRef(DRI, this));
393 ObjectFile::section_iterator MachOObjectFile::end_sections() const {
395 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
397 return section_iterator(SectionRef(DRI, this));
400 /*===-- Miscellaneous -----------------------------------------------------===*/
402 uint8_t MachOObjectFile::getBytesInAddress() const {
403 return MachOObj->is64Bit() ? 8 : 4;
406 StringRef MachOObjectFile::getFileFormatName() const {
407 if (!MachOObj->is64Bit()) {
408 switch (MachOObj->getHeader().CPUType) {
409 case llvm::MachO::CPUTypeI386:
410 return "Mach-O 32-bit i386";
411 case llvm::MachO::CPUTypeARM:
413 case llvm::MachO::CPUTypePowerPC:
414 return "Mach-O 32-bit ppc";
416 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
417 "64-bit object file when we're not 64-bit?");
418 return "Mach-O 32-bit unknown";
422 switch (MachOObj->getHeader().CPUType) {
423 case llvm::MachO::CPUTypeX86_64:
424 return "Mach-O 64-bit x86-64";
425 case llvm::MachO::CPUTypePowerPC64:
426 return "Mach-O 64-bit ppc64";
428 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
429 "32-bit object file when we're 64-bit?");
430 return "Mach-O 64-bit unknown";
434 unsigned MachOObjectFile::getArch() const {
435 switch (MachOObj->getHeader().CPUType) {
436 case llvm::MachO::CPUTypeI386:
438 case llvm::MachO::CPUTypeX86_64:
439 return Triple::x86_64;
440 case llvm::MachO::CPUTypeARM:
442 case llvm::MachO::CPUTypePowerPC:
444 case llvm::MachO::CPUTypePowerPC64:
445 return Triple::ppc64;
447 return Triple::UnknownArch;
451 } // end namespace llvm