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);
108 moveToNextSection(DRI);
113 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
116 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
119 return new MachOObjectFile(Buffer, MachOObj, ec);
122 /*===-- Symbols -----------------------------------------------------------===*/
124 void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
125 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
126 while (DRI.d.a < LoadCommandCount) {
127 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
128 if (LCI.Command.Type == macho::LCT_Symtab) {
129 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
130 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
131 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
140 void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
141 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
142 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
143 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
144 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
146 if (RegisteredStringTable != DRI.d.a) {
147 MachOObj->RegisterStringTable(*SymtabLoadCmd);
148 RegisteredStringTable = DRI.d.a;
151 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
155 void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
156 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
157 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
158 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
159 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
161 if (RegisteredStringTable != DRI.d.a) {
162 MachOObj->RegisterStringTable(*SymtabLoadCmd);
163 RegisteredStringTable = DRI.d.a;
166 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
171 error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
172 SymbolRef &Result) const {
174 moveToNextSymbol(DRI);
175 Result = SymbolRef(DRI, this);
176 return object_error::success;
179 error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
180 StringRef &Result) const {
181 if (MachOObj->is64Bit()) {
182 InMemoryStruct<macho::Symbol64TableEntry> Entry;
183 getSymbol64TableEntry(DRI, Entry);
184 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
186 InMemoryStruct<macho::SymbolTableEntry> Entry;
187 getSymbolTableEntry(DRI, Entry);
188 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
190 return object_error::success;
193 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
194 uint64_t &Result) const {
195 if (MachOObj->is64Bit()) {
196 InMemoryStruct<macho::Symbol64TableEntry> Entry;
197 getSymbol64TableEntry(DRI, Entry);
198 Result = Entry->Value;
200 InMemoryStruct<macho::SymbolTableEntry> Entry;
201 getSymbolTableEntry(DRI, Entry);
202 Result = Entry->Value;
204 return object_error::success;
207 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
208 uint64_t &Result) const {
209 Result = UnknownAddressOrSize;
210 return object_error::success;
213 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
214 char &Result) const {
216 if (MachOObj->is64Bit()) {
217 InMemoryStruct<macho::Symbol64TableEntry> Entry;
218 getSymbol64TableEntry(DRI, Entry);
220 Flags = Entry->Flags;
222 InMemoryStruct<macho::SymbolTableEntry> Entry;
223 getSymbolTableEntry(DRI, Entry);
225 Flags = Entry->Flags;
229 switch (Type & macho::STF_TypeMask) {
230 case macho::STT_Undefined:
233 case macho::STT_Absolute:
234 case macho::STT_Section:
242 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
243 Char = toupper(Char);
245 return object_error::success;
248 error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
249 bool &Result) const {
250 if (MachOObj->is64Bit()) {
251 InMemoryStruct<macho::Symbol64TableEntry> Entry;
252 getSymbol64TableEntry(DRI, Entry);
253 Result = Entry->Flags & macho::STF_StabsEntryMask;
255 InMemoryStruct<macho::SymbolTableEntry> Entry;
256 getSymbolTableEntry(DRI, Entry);
257 Result = Entry->Flags & macho::STF_StabsEntryMask;
259 return object_error::success;
262 ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
263 // DRI.d.a = segment number; DRI.d.b = symbol index.
265 DRI.d.a = DRI.d.b = 0;
266 moveToNextSymbol(DRI);
267 return symbol_iterator(SymbolRef(DRI, this));
270 ObjectFile::symbol_iterator MachOObjectFile::end_symbols() const {
272 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
274 return symbol_iterator(SymbolRef(DRI, this));
278 /*===-- Sections ----------------------------------------------------------===*/
280 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
281 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
282 while (DRI.d.a < LoadCommandCount) {
283 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
284 if (LCI.Command.Type == macho::LCT_Segment) {
285 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
286 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
287 if (DRI.d.b < SegmentLoadCmd->NumSections)
289 } else if (LCI.Command.Type == macho::LCT_Segment64) {
290 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
291 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
292 if (DRI.d.b < Segment64LoadCmd->NumSections)
301 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
302 SectionRef &Result) const {
304 moveToNextSection(DRI);
305 Result = SectionRef(DRI, this);
306 return object_error::success;
310 MachOObjectFile::getSection(DataRefImpl DRI,
311 InMemoryStruct<macho::Section> &Res) const {
312 InMemoryStruct<macho::SegmentLoadCommand> SLC;
313 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
314 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
315 MachOObj->ReadSection(LCI, DRI.d.b, Res);
319 MachOObjectFile::getSection64(DataRefImpl DRI,
320 InMemoryStruct<macho::Section64> &Res) const {
321 InMemoryStruct<macho::Segment64LoadCommand> SLC;
322 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
323 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
324 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
327 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
328 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
329 if (LCI.Command.Type == macho::LCT_Segment64)
331 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
335 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
336 StringRef &Result) const {
337 // FIXME: thread safety.
338 static char result[34];
339 if (is64BitLoadCommand(MachOObj, DRI)) {
340 InMemoryStruct<macho::Segment64LoadCommand> SLC;
341 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
342 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
343 InMemoryStruct<macho::Section64> Sect;
344 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
346 strcpy(result, Sect->SegmentName);
348 strcat(result, Sect->Name);
350 InMemoryStruct<macho::SegmentLoadCommand> SLC;
351 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
352 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
353 InMemoryStruct<macho::Section> Sect;
354 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
356 strcpy(result, Sect->SegmentName);
358 strcat(result, Sect->Name);
360 Result = StringRef(result);
361 return object_error::success;
364 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
365 uint64_t &Result) const {
366 if (is64BitLoadCommand(MachOObj, DRI)) {
367 InMemoryStruct<macho::Section64> Sect;
368 getSection64(DRI, Sect);
369 Result = Sect->Address;
371 InMemoryStruct<macho::Section> Sect;
372 getSection(DRI, Sect);
373 Result = Sect->Address;
375 return object_error::success;
378 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
379 uint64_t &Result) const {
380 if (is64BitLoadCommand(MachOObj, DRI)) {
381 InMemoryStruct<macho::Section64> Sect;
382 getSection64(DRI, Sect);
385 InMemoryStruct<macho::Section> Sect;
386 getSection(DRI, Sect);
389 return object_error::success;
392 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
393 StringRef &Result) const {
394 if (is64BitLoadCommand(MachOObj, DRI)) {
395 InMemoryStruct<macho::Section64> Sect;
396 getSection64(DRI, Sect);
397 Result = MachOObj->getData(Sect->Offset, Sect->Size);
399 InMemoryStruct<macho::Section> Sect;
400 getSection(DRI, Sect);
401 Result = MachOObj->getData(Sect->Offset, Sect->Size);
403 return object_error::success;
406 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
407 bool &Result) const {
408 if (is64BitLoadCommand(MachOObj, DRI)) {
409 InMemoryStruct<macho::Section64> Sect;
410 getSection64(DRI, Sect);
411 Result = !strcmp(Sect->Name, "__text");
413 InMemoryStruct<macho::Section> Sect;
414 getSection(DRI, Sect);
415 Result = !strcmp(Sect->Name, "__text");
417 return object_error::success;
420 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
422 bool &Result) const {
423 if (MachOObj->is64Bit()) {
424 InMemoryStruct<macho::Symbol64TableEntry> Entry;
425 getSymbol64TableEntry(Symb, Entry);
426 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
428 InMemoryStruct<macho::SymbolTableEntry> Entry;
429 getSymbolTableEntry(Symb, Entry);
430 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
432 return object_error::success;
435 ObjectFile::section_iterator MachOObjectFile::begin_sections() const {
437 DRI.d.a = DRI.d.b = 0;
438 moveToNextSection(DRI);
439 return section_iterator(SectionRef(DRI, this));
442 ObjectFile::section_iterator MachOObjectFile::end_sections() const {
444 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
446 return section_iterator(SectionRef(DRI, this));
449 /*===-- Relocations -------------------------------------------------------===*/
451 void MachOObjectFile::
452 getRelocation(DataRefImpl Rel,
453 InMemoryStruct<macho::RelocationEntry> &Res) const {
455 if (MachOObj->is64Bit()) {
456 InMemoryStruct<macho::Section64> Sect;
457 getSection64(Sections[Rel.d.b], Sect);
458 relOffset = Sect->RelocationTableOffset;
460 InMemoryStruct<macho::Section> Sect;
461 getSection(Sections[Rel.d.b], Sect);
462 relOffset = Sect->RelocationTableOffset;
464 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
466 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
467 RelocationRef &Res) const {
469 while (Rel.d.b < Sections.size()) {
470 unsigned relocationCount;
471 if (MachOObj->is64Bit()) {
472 InMemoryStruct<macho::Section64> Sect;
473 getSection64(Sections[Rel.d.b], Sect);
474 relocationCount = Sect->NumRelocationTableEntries;
476 InMemoryStruct<macho::Section> Sect;
477 getSection(Sections[Rel.d.b], Sect);
478 relocationCount = Sect->NumRelocationTableEntries;
480 if (Rel.d.a < relocationCount)
486 Res = RelocationRef(Rel, this);
487 return object_error::success;
489 error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
490 uint64_t &Res) const {
491 const uint8_t* sectAddress = base();
492 if (MachOObj->is64Bit()) {
493 InMemoryStruct<macho::Section64> Sect;
494 getSection64(Sections[Rel.d.b], Sect);
495 sectAddress += Sect->Offset;
497 InMemoryStruct<macho::Section> Sect;
498 getSection(Sections[Rel.d.b], Sect);
499 sectAddress += Sect->Offset;
501 InMemoryStruct<macho::RelocationEntry> RE;
502 getRelocation(Rel, RE);
503 Res = reinterpret_cast<uintptr_t>(sectAddress + RE->Word0);
504 return object_error::success;
506 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
507 SymbolRef &Res) const {
508 InMemoryStruct<macho::RelocationEntry> RE;
509 getRelocation(Rel, RE);
510 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
511 bool isExtern = (RE->Word1 >> 27) & 1;
514 Sym.d.a = Sym.d.b = 0;
515 moveToNextSymbol(Sym);
517 for (unsigned i = 0; i < SymbolIdx; i++) {
519 moveToNextSymbol(Sym);
520 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
521 "Relocation symbol index out of range!");
524 Res = SymbolRef(Sym, this);
525 return object_error::success;
527 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
528 uint32_t &Res) const {
529 InMemoryStruct<macho::RelocationEntry> RE;
530 getRelocation(Rel, RE);
532 return object_error::success;
534 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
535 int64_t &Res) const {
536 InMemoryStruct<macho::RelocationEntry> RE;
537 getRelocation(Rel, RE);
538 bool isExtern = (RE->Word1 >> 27) & 1;
541 const uint8_t* sectAddress = base();
542 if (MachOObj->is64Bit()) {
543 InMemoryStruct<macho::Section64> Sect;
544 getSection64(Sections[Rel.d.b], Sect);
545 sectAddress += Sect->Offset;
547 InMemoryStruct<macho::Section> Sect;
548 getSection(Sections[Rel.d.b], Sect);
549 sectAddress += Sect->Offset;
551 Res = reinterpret_cast<uintptr_t>(sectAddress);
553 return object_error::success;
555 ObjectFile::relocation_iterator MachOObjectFile::begin_relocations() const {
557 ret.d.a = ret.d.b = 0;
558 return relocation_iterator(RelocationRef(ret, this));
560 ObjectFile::relocation_iterator MachOObjectFile::end_relocations() const {
563 ret.d.b = Sections.size();
564 return relocation_iterator(RelocationRef(ret, this));
567 /*===-- Miscellaneous -----------------------------------------------------===*/
569 uint8_t MachOObjectFile::getBytesInAddress() const {
570 return MachOObj->is64Bit() ? 8 : 4;
573 StringRef MachOObjectFile::getFileFormatName() const {
574 if (!MachOObj->is64Bit()) {
575 switch (MachOObj->getHeader().CPUType) {
576 case llvm::MachO::CPUTypeI386:
577 return "Mach-O 32-bit i386";
578 case llvm::MachO::CPUTypeARM:
580 case llvm::MachO::CPUTypePowerPC:
581 return "Mach-O 32-bit ppc";
583 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
584 "64-bit object file when we're not 64-bit?");
585 return "Mach-O 32-bit unknown";
589 switch (MachOObj->getHeader().CPUType) {
590 case llvm::MachO::CPUTypeX86_64:
591 return "Mach-O 64-bit x86-64";
592 case llvm::MachO::CPUTypePowerPC64:
593 return "Mach-O 64-bit ppc64";
595 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
596 "32-bit object file when we're 64-bit?");
597 return "Mach-O 64-bit unknown";
601 unsigned MachOObjectFile::getArch() const {
602 switch (MachOObj->getHeader().CPUType) {
603 case llvm::MachO::CPUTypeI386:
605 case llvm::MachO::CPUTypeX86_64:
606 return Triple::x86_64;
607 case llvm::MachO::CPUTypeARM:
609 case llvm::MachO::CPUTypePowerPC:
611 case llvm::MachO::CPUTypePowerPC64:
612 return Triple::ppc64;
614 return Triple::UnknownArch;
618 } // end namespace llvm