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"
21 #include "llvm/ADT/SmallVector.h"
28 using namespace object;
32 typedef MachOObject::LoadCommandInfo LoadCommandInfo;
34 class MachOObjectFile : public ObjectFile {
36 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec);
38 virtual symbol_iterator begin_symbols() const;
39 virtual symbol_iterator end_symbols() const;
40 virtual section_iterator begin_sections() const;
41 virtual section_iterator end_sections() const;
42 virtual relocation_iterator begin_relocations() const;
43 virtual relocation_iterator end_relocations() 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;
63 virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
66 virtual error_code getRelocationNext(DataRefImpl Rel,
67 RelocationRef &Res) const;
68 virtual error_code getRelocationAddress(DataRefImpl Rel,
70 virtual error_code getRelocationSymbol(DataRefImpl Rel,
71 SymbolRef &Res) const;
72 virtual error_code getRelocationType(DataRefImpl Rel,
74 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
77 MachOObject *MachOObj;
78 mutable uint32_t RegisteredStringTable;
79 typedef SmallVector<DataRefImpl, 1> SectionList;
83 void moveToNextSection(DataRefImpl &DRI) const;
84 void getSymbolTableEntry(DataRefImpl DRI,
85 InMemoryStruct<macho::SymbolTableEntry> &Res) const;
86 void getSymbol64TableEntry(DataRefImpl DRI,
87 InMemoryStruct<macho::Symbol64TableEntry> &Res) const;
88 void moveToNextSymbol(DataRefImpl &DRI) const;
89 void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const;
90 void getSection64(DataRefImpl DRI,
91 InMemoryStruct<macho::Section64> &Res) const;
92 void getRelocation(DataRefImpl Rel,
93 InMemoryStruct<macho::RelocationEntry> &Res) const;
96 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
98 : ObjectFile(Binary::isMachO, Object, ec),
100 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
102 DRI.d.a = DRI.d.b = 0;
103 moveToNextSection(DRI);
104 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
105 while (DRI.d.a < LoadCommandCount) {
106 Sections.push_back(DRI);
110 getSectionAddress(DRI, Addr);
111 getSectionSize(DRI, Size);
112 getSectionName(DRI, Name);
113 InMemoryStruct<macho::Section> Sect;
114 getSection(DRI, Sect);
116 moveToNextSection(DRI);
121 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
124 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
127 return new MachOObjectFile(Buffer, MachOObj, ec);
130 /*===-- Symbols -----------------------------------------------------------===*/
132 void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
133 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
134 while (DRI.d.a < LoadCommandCount) {
135 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
136 if (LCI.Command.Type == macho::LCT_Symtab) {
137 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
138 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
139 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
148 void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
149 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
150 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
151 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
152 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
154 if (RegisteredStringTable != DRI.d.a) {
155 MachOObj->RegisterStringTable(*SymtabLoadCmd);
156 RegisteredStringTable = DRI.d.a;
159 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
163 void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
164 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
165 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
166 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
167 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
169 if (RegisteredStringTable != DRI.d.a) {
170 MachOObj->RegisterStringTable(*SymtabLoadCmd);
171 RegisteredStringTable = DRI.d.a;
174 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
179 error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
180 SymbolRef &Result) const {
182 moveToNextSymbol(DRI);
183 Result = SymbolRef(DRI, this);
184 return object_error::success;
187 error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
188 StringRef &Result) const {
189 if (MachOObj->is64Bit()) {
190 InMemoryStruct<macho::Symbol64TableEntry> Entry;
191 getSymbol64TableEntry(DRI, Entry);
192 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
194 InMemoryStruct<macho::SymbolTableEntry> Entry;
195 getSymbolTableEntry(DRI, Entry);
196 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
198 return object_error::success;
201 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
202 uint64_t &Result) const {
203 if (MachOObj->is64Bit()) {
204 InMemoryStruct<macho::Symbol64TableEntry> Entry;
205 getSymbol64TableEntry(DRI, Entry);
206 Result = Entry->Value;
208 InMemoryStruct<macho::SymbolTableEntry> Entry;
209 getSymbolTableEntry(DRI, Entry);
210 Result = Entry->Value;
212 return object_error::success;
215 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
216 uint64_t &Result) const {
217 Result = UnknownAddressOrSize;
218 return object_error::success;
221 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
222 char &Result) const {
224 if (MachOObj->is64Bit()) {
225 InMemoryStruct<macho::Symbol64TableEntry> Entry;
226 getSymbol64TableEntry(DRI, Entry);
228 Flags = Entry->Flags;
230 InMemoryStruct<macho::SymbolTableEntry> Entry;
231 getSymbolTableEntry(DRI, Entry);
233 Flags = Entry->Flags;
237 switch (Type & macho::STF_TypeMask) {
238 case macho::STT_Undefined:
241 case macho::STT_Absolute:
242 case macho::STT_Section:
250 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
251 Char = toupper(Char);
253 return object_error::success;
256 error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
257 bool &Result) const {
258 if (MachOObj->is64Bit()) {
259 InMemoryStruct<macho::Symbol64TableEntry> Entry;
260 getSymbol64TableEntry(DRI, Entry);
261 Result = Entry->Flags & macho::STF_StabsEntryMask;
263 InMemoryStruct<macho::SymbolTableEntry> Entry;
264 getSymbolTableEntry(DRI, Entry);
265 Result = Entry->Flags & macho::STF_StabsEntryMask;
267 return object_error::success;
270 ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
271 // DRI.d.a = segment number; DRI.d.b = symbol index.
273 DRI.d.a = DRI.d.b = 0;
274 moveToNextSymbol(DRI);
275 return symbol_iterator(SymbolRef(DRI, this));
278 ObjectFile::symbol_iterator MachOObjectFile::end_symbols() const {
280 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
282 return symbol_iterator(SymbolRef(DRI, this));
286 /*===-- Sections ----------------------------------------------------------===*/
288 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
289 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
290 while (DRI.d.a < LoadCommandCount) {
291 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
292 if (LCI.Command.Type == macho::LCT_Segment) {
293 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
294 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
295 if (DRI.d.b < SegmentLoadCmd->NumSections)
297 } else if (LCI.Command.Type == macho::LCT_Segment64) {
298 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
299 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
300 if (DRI.d.b < Segment64LoadCmd->NumSections)
309 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
310 SectionRef &Result) const {
312 moveToNextSection(DRI);
313 Result = SectionRef(DRI, this);
314 return object_error::success;
318 MachOObjectFile::getSection(DataRefImpl DRI,
319 InMemoryStruct<macho::Section> &Res) const {
320 InMemoryStruct<macho::SegmentLoadCommand> SLC;
321 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
322 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
323 MachOObj->ReadSection(LCI, DRI.d.b, Res);
327 MachOObjectFile::getSection64(DataRefImpl DRI,
328 InMemoryStruct<macho::Section64> &Res) const {
329 InMemoryStruct<macho::Segment64LoadCommand> SLC;
330 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
331 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
332 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
335 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
336 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
337 if (LCI.Command.Type == macho::LCT_Segment64)
339 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
343 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
344 StringRef &Result) const {
345 // FIXME: thread safety.
346 static char result[34];
347 if (is64BitLoadCommand(MachOObj, DRI)) {
348 InMemoryStruct<macho::Segment64LoadCommand> SLC;
349 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
350 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
351 InMemoryStruct<macho::Section64> Sect;
352 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
354 strcpy(result, Sect->SegmentName);
356 strcat(result, Sect->Name);
358 InMemoryStruct<macho::SegmentLoadCommand> SLC;
359 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
360 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
361 InMemoryStruct<macho::Section> Sect;
362 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
364 strcpy(result, Sect->SegmentName);
366 strcat(result, Sect->Name);
368 Result = StringRef(result);
369 return object_error::success;
372 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
373 uint64_t &Result) const {
374 if (is64BitLoadCommand(MachOObj, DRI)) {
375 InMemoryStruct<macho::Section64> Sect;
376 getSection64(DRI, Sect);
377 Result = Sect->Address;
379 InMemoryStruct<macho::Section> Sect;
380 getSection(DRI, Sect);
381 Result = Sect->Address;
383 return object_error::success;
386 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
387 uint64_t &Result) const {
388 if (is64BitLoadCommand(MachOObj, DRI)) {
389 InMemoryStruct<macho::Section64> Sect;
390 getSection64(DRI, Sect);
393 InMemoryStruct<macho::Section> Sect;
394 getSection(DRI, Sect);
397 return object_error::success;
400 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
401 StringRef &Result) const {
402 if (is64BitLoadCommand(MachOObj, DRI)) {
403 InMemoryStruct<macho::Section64> Sect;
404 getSection64(DRI, Sect);
405 Result = MachOObj->getData(Sect->Offset, Sect->Size);
407 InMemoryStruct<macho::Section> Sect;
408 getSection(DRI, Sect);
409 Result = MachOObj->getData(Sect->Offset, Sect->Size);
411 return object_error::success;
414 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
415 bool &Result) const {
416 if (is64BitLoadCommand(MachOObj, DRI)) {
417 InMemoryStruct<macho::Section64> Sect;
418 getSection64(DRI, Sect);
419 Result = !strcmp(Sect->Name, "__text");
421 InMemoryStruct<macho::Section> Sect;
422 getSection(DRI, Sect);
423 Result = !strcmp(Sect->Name, "__text");
425 return object_error::success;
428 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
430 bool &Result) const {
431 if (MachOObj->is64Bit()) {
432 InMemoryStruct<macho::Symbol64TableEntry> Entry;
433 getSymbol64TableEntry(Symb, Entry);
434 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
436 InMemoryStruct<macho::SymbolTableEntry> Entry;
437 getSymbolTableEntry(Symb, Entry);
438 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
440 return object_error::success;
443 ObjectFile::section_iterator MachOObjectFile::begin_sections() const {
445 DRI.d.a = DRI.d.b = 0;
446 moveToNextSection(DRI);
447 return section_iterator(SectionRef(DRI, this));
450 ObjectFile::section_iterator MachOObjectFile::end_sections() const {
452 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
454 return section_iterator(SectionRef(DRI, this));
457 /*===-- Relocations -------------------------------------------------------===*/
459 void MachOObjectFile::
460 getRelocation(DataRefImpl Rel,
461 InMemoryStruct<macho::RelocationEntry> &Res) const {
463 if (MachOObj->is64Bit()) {
464 InMemoryStruct<macho::Section64> Sect;
465 getSection64(Sections[Rel.d.b], Sect);
466 relOffset = Sect->RelocationTableOffset;
468 InMemoryStruct<macho::Section> Sect;
469 getSection(Sections[Rel.d.b], Sect);
470 relOffset = Sect->RelocationTableOffset;
472 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
474 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
475 RelocationRef &Res) const {
477 while (Rel.d.b < Sections.size()) {
478 unsigned relocationCount;
479 if (MachOObj->is64Bit()) {
480 InMemoryStruct<macho::Section64> Sect;
481 getSection64(Sections[Rel.d.b], Sect);
482 relocationCount = Sect->NumRelocationTableEntries;
484 InMemoryStruct<macho::Section> Sect;
485 getSection(Sections[Rel.d.b], Sect);
486 relocationCount = Sect->NumRelocationTableEntries;
488 if (Rel.d.a < relocationCount)
494 Res = RelocationRef(Rel, this);
495 return object_error::success;
497 error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
498 uint64_t &Res) const {
499 const uint8_t* sectAddress = base();
500 if (MachOObj->is64Bit()) {
501 InMemoryStruct<macho::Section64> Sect;
502 getSection64(Sections[Rel.d.b], Sect);
503 sectAddress += Sect->Offset;
505 InMemoryStruct<macho::Section> Sect;
506 getSection(Sections[Rel.d.b], Sect);
507 sectAddress += Sect->Offset;
509 InMemoryStruct<macho::RelocationEntry> RE;
510 getRelocation(Rel, RE);
511 Res = reinterpret_cast<uintptr_t>(sectAddress + RE->Word0);
512 return object_error::success;
514 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
515 SymbolRef &Res) const {
516 InMemoryStruct<macho::RelocationEntry> RE;
517 getRelocation(Rel, RE);
518 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
519 bool isExtern = (RE->Word1 >> 27) & 1;
522 Sym.d.a = Sym.d.b = 0;
523 moveToNextSymbol(Sym);
525 for (unsigned i = 0; i < SymbolIdx; i++) {
527 moveToNextSymbol(Sym);
528 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
529 "Relocation symbol index out of range!");
532 Res = SymbolRef(Sym, this);
533 return object_error::success;
535 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
536 uint32_t &Res) const {
537 InMemoryStruct<macho::RelocationEntry> RE;
538 getRelocation(Rel, RE);
540 return object_error::success;
542 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
543 int64_t &Res) const {
544 InMemoryStruct<macho::RelocationEntry> RE;
545 getRelocation(Rel, RE);
546 bool isExtern = (RE->Word1 >> 27) & 1;
549 const uint8_t* sectAddress = base();
550 if (MachOObj->is64Bit()) {
551 InMemoryStruct<macho::Section64> Sect;
552 getSection64(Sections[Rel.d.b], Sect);
553 sectAddress += Sect->Offset;
555 InMemoryStruct<macho::Section> Sect;
556 getSection(Sections[Rel.d.b], Sect);
557 sectAddress += Sect->Offset;
559 Res = reinterpret_cast<uintptr_t>(sectAddress);
561 return object_error::success;
563 ObjectFile::relocation_iterator MachOObjectFile::begin_relocations() const {
565 ret.d.a = ret.d.b = 0;
566 return relocation_iterator(RelocationRef(ret, this));
568 ObjectFile::relocation_iterator MachOObjectFile::end_relocations() const {
571 ret.d.b = Sections.size();
572 return relocation_iterator(RelocationRef(ret, this));
575 /*===-- Miscellaneous -----------------------------------------------------===*/
577 uint8_t MachOObjectFile::getBytesInAddress() const {
578 return MachOObj->is64Bit() ? 8 : 4;
581 StringRef MachOObjectFile::getFileFormatName() const {
582 if (!MachOObj->is64Bit()) {
583 switch (MachOObj->getHeader().CPUType) {
584 case llvm::MachO::CPUTypeI386:
585 return "Mach-O 32-bit i386";
586 case llvm::MachO::CPUTypeARM:
588 case llvm::MachO::CPUTypePowerPC:
589 return "Mach-O 32-bit ppc";
591 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
592 "64-bit object file when we're not 64-bit?");
593 return "Mach-O 32-bit unknown";
597 switch (MachOObj->getHeader().CPUType) {
598 case llvm::MachO::CPUTypeX86_64:
599 return "Mach-O 64-bit x86-64";
600 case llvm::MachO::CPUTypePowerPC64:
601 return "Mach-O 64-bit ppc64";
603 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
604 "32-bit object file when we're 64-bit?");
605 return "Mach-O 64-bit unknown";
609 unsigned MachOObjectFile::getArch() const {
610 switch (MachOObj->getHeader().CPUType) {
611 case llvm::MachO::CPUTypeI386:
613 case llvm::MachO::CPUTypeX86_64:
614 return Triple::x86_64;
615 case llvm::MachO::CPUTypeARM:
617 case llvm::MachO::CPUTypePowerPC:
619 case llvm::MachO::CPUTypePowerPC64:
620 return Triple::ppc64;
622 return Triple::UnknownArch;
626 } // end namespace llvm