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/MachO.h"
17 #include "llvm/Object/MachOFormat.h"
18 #include "llvm/Support/MemoryBuffer.h"
25 using namespace object;
30 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
32 : ObjectFile(Binary::isMachO, Object, ec),
34 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
36 DRI.d.a = DRI.d.b = 0;
37 moveToNextSection(DRI);
38 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
39 while (DRI.d.a < LoadCommandCount) {
40 Sections.push_back(DRI);
42 moveToNextSection(DRI);
47 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
50 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
53 return new MachOObjectFile(Buffer, MachOObj, ec);
56 /*===-- Symbols -----------------------------------------------------------===*/
58 void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
59 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
60 while (DRI.d.a < LoadCommandCount) {
61 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
62 if (LCI.Command.Type == macho::LCT_Symtab) {
63 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
64 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
65 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
74 void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
75 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
76 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
77 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
78 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
80 if (RegisteredStringTable != DRI.d.a) {
81 MachOObj->RegisterStringTable(*SymtabLoadCmd);
82 RegisteredStringTable = DRI.d.a;
85 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
89 void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
90 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
91 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
92 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
93 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
95 if (RegisteredStringTable != DRI.d.a) {
96 MachOObj->RegisterStringTable(*SymtabLoadCmd);
97 RegisteredStringTable = DRI.d.a;
100 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
105 error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
106 SymbolRef &Result) const {
108 moveToNextSymbol(DRI);
109 Result = SymbolRef(DRI, this);
110 return object_error::success;
113 error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
114 StringRef &Result) const {
115 if (MachOObj->is64Bit()) {
116 InMemoryStruct<macho::Symbol64TableEntry> Entry;
117 getSymbol64TableEntry(DRI, Entry);
118 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
120 InMemoryStruct<macho::SymbolTableEntry> Entry;
121 getSymbolTableEntry(DRI, Entry);
122 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
124 return object_error::success;
127 error_code MachOObjectFile::getSymbolOffset(DataRefImpl DRI,
128 uint64_t &Result) const {
129 if (MachOObj->is64Bit()) {
130 InMemoryStruct<macho::Symbol64TableEntry> Entry;
131 getSymbol64TableEntry(DRI, Entry);
132 Result = Entry->Value;
134 InMemoryStruct<macho::SymbolTableEntry> Entry;
135 getSymbolTableEntry(DRI, Entry);
136 Result = Entry->Value;
138 return object_error::success;
141 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
142 uint64_t &Result) const {
143 uint64_t SymbolOffset;
144 uint8_t SectionIndex;
145 if (MachOObj->is64Bit()) {
146 InMemoryStruct<macho::Symbol64TableEntry> Entry;
147 getSymbol64TableEntry(DRI, Entry);
148 SymbolOffset = Entry->Value;
149 SectionIndex = Entry->SectionIndex;
151 InMemoryStruct<macho::SymbolTableEntry> Entry;
152 getSymbolTableEntry(DRI, Entry);
153 SymbolOffset = Entry->Value;
154 SectionIndex = Entry->SectionIndex;
156 getSectionAddress(Sections[SectionIndex-1], Result);
157 Result += SymbolOffset;
159 return object_error::success;
162 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
163 uint64_t &Result) const {
164 Result = UnknownAddressOrSize;
165 return object_error::success;
168 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
169 char &Result) const {
171 if (MachOObj->is64Bit()) {
172 InMemoryStruct<macho::Symbol64TableEntry> Entry;
173 getSymbol64TableEntry(DRI, Entry);
175 Flags = Entry->Flags;
177 InMemoryStruct<macho::SymbolTableEntry> Entry;
178 getSymbolTableEntry(DRI, Entry);
180 Flags = Entry->Flags;
184 switch (Type & macho::STF_TypeMask) {
185 case macho::STT_Undefined:
188 case macho::STT_Absolute:
189 case macho::STT_Section:
197 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
198 Char = toupper(Char);
200 return object_error::success;
203 error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
204 bool &Result) const {
205 if (MachOObj->is64Bit()) {
206 InMemoryStruct<macho::Symbol64TableEntry> Entry;
207 getSymbol64TableEntry(DRI, Entry);
208 Result = Entry->Flags & macho::STF_StabsEntryMask;
210 InMemoryStruct<macho::SymbolTableEntry> Entry;
211 getSymbolTableEntry(DRI, Entry);
212 Result = Entry->Flags & macho::STF_StabsEntryMask;
214 return object_error::success;
217 error_code MachOObjectFile::isSymbolGlobal(DataRefImpl Symb, bool &Res) const {
219 if (MachOObj->is64Bit()) {
220 InMemoryStruct<macho::Symbol64TableEntry> Entry;
221 getSymbol64TableEntry(Symb, Entry);
222 Res = Entry->Type & MachO::NlistMaskExternal;
224 InMemoryStruct<macho::SymbolTableEntry> Entry;
225 getSymbolTableEntry(Symb, Entry);
226 Res = Entry->Type & MachO::NlistMaskExternal;
228 return object_error::success;
231 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
232 SymbolRef::SymbolType &Res) const {
234 if (MachOObj->is64Bit()) {
235 InMemoryStruct<macho::Symbol64TableEntry> Entry;
236 getSymbol64TableEntry(Symb, Entry);
237 n_type = Entry->Type;
239 InMemoryStruct<macho::SymbolTableEntry> Entry;
240 getSymbolTableEntry(Symb, Entry);
241 n_type = Entry->Type;
243 Res = SymbolRef::ST_Other;
244 switch (n_type & MachO::NlistMaskType) {
245 case MachO::NListTypeUndefined :
246 Res = SymbolRef::ST_External;
248 case MachO::NListTypeSection :
249 Res = SymbolRef::ST_Function;
252 return object_error::success;
256 symbol_iterator MachOObjectFile::begin_symbols() const {
257 // DRI.d.a = segment number; DRI.d.b = symbol index.
259 DRI.d.a = DRI.d.b = 0;
260 moveToNextSymbol(DRI);
261 return symbol_iterator(SymbolRef(DRI, this));
264 symbol_iterator MachOObjectFile::end_symbols() const {
266 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
268 return symbol_iterator(SymbolRef(DRI, this));
272 /*===-- Sections ----------------------------------------------------------===*/
274 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
275 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
276 while (DRI.d.a < LoadCommandCount) {
277 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
278 if (LCI.Command.Type == macho::LCT_Segment) {
279 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
280 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
281 if (DRI.d.b < SegmentLoadCmd->NumSections)
283 } else if (LCI.Command.Type == macho::LCT_Segment64) {
284 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
285 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
286 if (DRI.d.b < Segment64LoadCmd->NumSections)
295 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
296 SectionRef &Result) const {
298 moveToNextSection(DRI);
299 Result = SectionRef(DRI, this);
300 return object_error::success;
304 MachOObjectFile::getSection(DataRefImpl DRI,
305 InMemoryStruct<macho::Section> &Res) const {
306 InMemoryStruct<macho::SegmentLoadCommand> SLC;
307 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
308 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
309 MachOObj->ReadSection(LCI, DRI.d.b, Res);
312 std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
313 SectionList::const_iterator loc =
314 std::find(Sections.begin(), Sections.end(), Sec);
315 assert(loc != Sections.end() && "Sec is not a valid section!");
316 return std::distance(Sections.begin(), loc);
320 MachOObjectFile::getSection64(DataRefImpl DRI,
321 InMemoryStruct<macho::Section64> &Res) const {
322 InMemoryStruct<macho::Segment64LoadCommand> SLC;
323 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
324 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
325 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
328 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
329 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
330 if (LCI.Command.Type == macho::LCT_Segment64)
332 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
336 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
337 StringRef &Result) const {
338 // FIXME: thread safety.
339 static char result[34];
340 if (is64BitLoadCommand(MachOObj, DRI)) {
341 InMemoryStruct<macho::Segment64LoadCommand> SLC;
342 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
343 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
344 InMemoryStruct<macho::Section64> Sect;
345 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
347 strcpy(result, Sect->SegmentName);
349 strcat(result, Sect->Name);
351 InMemoryStruct<macho::SegmentLoadCommand> SLC;
352 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
353 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
354 InMemoryStruct<macho::Section> Sect;
355 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
357 strcpy(result, Sect->SegmentName);
359 strcat(result, Sect->Name);
361 Result = StringRef(result);
362 return object_error::success;
365 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
366 uint64_t &Result) const {
367 if (is64BitLoadCommand(MachOObj, DRI)) {
368 InMemoryStruct<macho::Section64> Sect;
369 getSection64(DRI, Sect);
370 Result = Sect->Address;
372 InMemoryStruct<macho::Section> Sect;
373 getSection(DRI, Sect);
374 Result = Sect->Address;
376 return object_error::success;
379 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
380 uint64_t &Result) const {
381 if (is64BitLoadCommand(MachOObj, DRI)) {
382 InMemoryStruct<macho::Section64> Sect;
383 getSection64(DRI, Sect);
386 InMemoryStruct<macho::Section> Sect;
387 getSection(DRI, Sect);
390 return object_error::success;
393 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
394 StringRef &Result) const {
395 if (is64BitLoadCommand(MachOObj, DRI)) {
396 InMemoryStruct<macho::Section64> Sect;
397 getSection64(DRI, Sect);
398 Result = MachOObj->getData(Sect->Offset, Sect->Size);
400 InMemoryStruct<macho::Section> Sect;
401 getSection(DRI, Sect);
402 Result = MachOObj->getData(Sect->Offset, Sect->Size);
404 return object_error::success;
407 error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
408 uint64_t &Result) const {
409 if (is64BitLoadCommand(MachOObj, DRI)) {
410 InMemoryStruct<macho::Section64> Sect;
411 getSection64(DRI, Sect);
412 Result = uint64_t(1) << Sect->Align;
414 InMemoryStruct<macho::Section> Sect;
415 getSection(DRI, Sect);
416 Result = uint64_t(1) << Sect->Align;
418 return object_error::success;
421 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
422 bool &Result) const {
423 if (is64BitLoadCommand(MachOObj, DRI)) {
424 InMemoryStruct<macho::Section64> Sect;
425 getSection64(DRI, Sect);
426 Result = !strcmp(Sect->Name, "__text");
428 InMemoryStruct<macho::Section> Sect;
429 getSection(DRI, Sect);
430 Result = !strcmp(Sect->Name, "__text");
432 return object_error::success;
435 error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
436 bool &Result) const {
437 // FIXME: Unimplemented.
439 return object_error::success;
442 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
443 bool &Result) const {
444 // FIXME: Unimplemented.
446 return object_error::success;
449 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
451 bool &Result) const {
452 if (MachOObj->is64Bit()) {
453 InMemoryStruct<macho::Symbol64TableEntry> Entry;
454 getSymbol64TableEntry(Symb, Entry);
455 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
457 InMemoryStruct<macho::SymbolTableEntry> Entry;
458 getSymbolTableEntry(Symb, Entry);
459 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
461 return object_error::success;
464 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
467 ret.d.b = getSectionIndex(Sec);
468 return relocation_iterator(RelocationRef(ret, this));
470 relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
472 if (is64BitLoadCommand(MachOObj, Sec)) {
473 InMemoryStruct<macho::Section64> Sect;
474 getSection64(Sec, Sect);
475 last_reloc = Sect->NumRelocationTableEntries;
477 InMemoryStruct<macho::Section> Sect;
478 getSection(Sec, Sect);
479 last_reloc = Sect->NumRelocationTableEntries;
482 ret.d.a = last_reloc;
483 ret.d.b = getSectionIndex(Sec);
484 return relocation_iterator(RelocationRef(ret, this));
487 section_iterator MachOObjectFile::begin_sections() const {
489 DRI.d.a = DRI.d.b = 0;
490 moveToNextSection(DRI);
491 return section_iterator(SectionRef(DRI, this));
494 section_iterator MachOObjectFile::end_sections() const {
496 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
498 return section_iterator(SectionRef(DRI, this));
501 /*===-- Relocations -------------------------------------------------------===*/
503 void MachOObjectFile::
504 getRelocation(DataRefImpl Rel,
505 InMemoryStruct<macho::RelocationEntry> &Res) const {
507 if (MachOObj->is64Bit()) {
508 InMemoryStruct<macho::Section64> Sect;
509 getSection64(Sections[Rel.d.b], Sect);
510 relOffset = Sect->RelocationTableOffset;
512 InMemoryStruct<macho::Section> Sect;
513 getSection(Sections[Rel.d.b], Sect);
514 relOffset = Sect->RelocationTableOffset;
516 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
518 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
519 RelocationRef &Res) const {
521 Res = RelocationRef(Rel, this);
522 return object_error::success;
524 error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
525 uint64_t &Res) const {
526 const uint8_t* sectAddress = base();
527 if (MachOObj->is64Bit()) {
528 InMemoryStruct<macho::Section64> Sect;
529 getSection64(Sections[Rel.d.b], Sect);
530 sectAddress += Sect->Offset;
532 InMemoryStruct<macho::Section> Sect;
533 getSection(Sections[Rel.d.b], Sect);
534 sectAddress += Sect->Offset;
536 InMemoryStruct<macho::RelocationEntry> RE;
537 getRelocation(Rel, RE);
538 Res = reinterpret_cast<uintptr_t>(sectAddress + RE->Word0);
539 return object_error::success;
541 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
542 SymbolRef &Res) const {
543 InMemoryStruct<macho::RelocationEntry> RE;
544 getRelocation(Rel, RE);
545 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
546 bool isExtern = (RE->Word1 >> 27) & 1;
549 Sym.d.a = Sym.d.b = 0;
550 moveToNextSymbol(Sym);
552 for (unsigned i = 0; i < SymbolIdx; i++) {
554 moveToNextSymbol(Sym);
555 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
556 "Relocation symbol index out of range!");
559 Res = SymbolRef(Sym, this);
560 return object_error::success;
562 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
563 uint32_t &Res) const {
564 InMemoryStruct<macho::RelocationEntry> RE;
565 getRelocation(Rel, RE);
567 return object_error::success;
569 error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
570 SmallVectorImpl<char> &Result) const {
571 return object_error::success;
573 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
574 int64_t &Res) const {
575 InMemoryStruct<macho::RelocationEntry> RE;
576 getRelocation(Rel, RE);
577 bool isExtern = (RE->Word1 >> 27) & 1;
580 const uint8_t* sectAddress = base();
581 if (MachOObj->is64Bit()) {
582 InMemoryStruct<macho::Section64> Sect;
583 getSection64(Sections[Rel.d.b], Sect);
584 sectAddress += Sect->Offset;
586 InMemoryStruct<macho::Section> Sect;
587 getSection(Sections[Rel.d.b], Sect);
588 sectAddress += Sect->Offset;
590 Res = reinterpret_cast<uintptr_t>(sectAddress);
592 return object_error::success;
594 error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
595 SmallVectorImpl<char> &Result) const {
596 return object_error::success;
599 /*===-- Miscellaneous -----------------------------------------------------===*/
601 uint8_t MachOObjectFile::getBytesInAddress() const {
602 return MachOObj->is64Bit() ? 8 : 4;
605 StringRef MachOObjectFile::getFileFormatName() const {
606 if (!MachOObj->is64Bit()) {
607 switch (MachOObj->getHeader().CPUType) {
608 case llvm::MachO::CPUTypeI386:
609 return "Mach-O 32-bit i386";
610 case llvm::MachO::CPUTypeARM:
612 case llvm::MachO::CPUTypePowerPC:
613 return "Mach-O 32-bit ppc";
615 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
616 "64-bit object file when we're not 64-bit?");
617 return "Mach-O 32-bit unknown";
621 switch (MachOObj->getHeader().CPUType) {
622 case llvm::MachO::CPUTypeX86_64:
623 return "Mach-O 64-bit x86-64";
624 case llvm::MachO::CPUTypePowerPC64:
625 return "Mach-O 64-bit ppc64";
627 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
628 "32-bit object file when we're 64-bit?");
629 return "Mach-O 64-bit unknown";
633 unsigned MachOObjectFile::getArch() const {
634 switch (MachOObj->getHeader().CPUType) {
635 case llvm::MachO::CPUTypeI386:
637 case llvm::MachO::CPUTypeX86_64:
638 return Triple::x86_64;
639 case llvm::MachO::CPUTypeARM:
641 case llvm::MachO::CPUTypePowerPC:
643 case llvm::MachO::CPUTypePowerPC64:
644 return Triple::ppc64;
646 return Triple::UnknownArch;
650 } // end namespace object
651 } // end namespace llvm