1 //===-- MachODump.cpp - Object file dumping utility for llvm --------------===//
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 implements the MachO-specific dumper for llvm-objdump.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-objdump.h"
15 #include "llvm-c/Disassembler.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/DebugInfo/DIContext.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCDisassembler.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstPrinter.h"
25 #include "llvm/MC/MCInstrAnalysis.h"
26 #include "llvm/MC/MCInstrDesc.h"
27 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCSubtargetInfo.h"
30 #include "llvm/Object/MachO.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/Format.h"
36 #include "llvm/Support/GraphWriter.h"
37 #include "llvm/Support/MachO.h"
38 #include "llvm/Support/MemoryBuffer.h"
39 #include "llvm/Support/FormattedStream.h"
40 #include "llvm/Support/TargetRegistry.h"
41 #include "llvm/Support/TargetSelect.h"
42 #include "llvm/Support/raw_ostream.h"
45 #include <system_error>
47 using namespace object;
50 UseDbg("g", cl::desc("Print line information from debug info if available"));
52 static cl::opt<std::string>
53 DSYMFile("dsym", cl::desc("Use .dSYM file for debug info"));
56 FullLeadingAddr("full-leading-addr",
57 cl::desc("Print full leading address"));
60 PrintImmHex("print-imm-hex",
61 cl::desc("Use hex format for immediate values"));
63 static std::string ThumbTripleName;
65 static const Target *GetTarget(const MachOObjectFile *MachOObj,
66 const char **McpuDefault,
67 const Target **ThumbTarget) {
68 // Figure out the target triple.
69 if (TripleName.empty()) {
70 llvm::Triple TT("unknown-unknown-unknown");
71 llvm::Triple ThumbTriple = Triple();
72 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
73 TripleName = TT.str();
74 ThumbTripleName = ThumbTriple.str();
77 // Get the target specific parser.
79 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
80 if (TheTarget && ThumbTripleName.empty())
83 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
87 errs() << "llvm-objdump: error: unable to get target for '";
91 errs() << ThumbTripleName;
92 errs() << "', see --version and --triple.\n";
97 bool operator()(const SymbolRef &A, const SymbolRef &B) {
98 SymbolRef::Type AType, BType;
102 uint64_t AAddr, BAddr;
103 if (AType != SymbolRef::ST_Function)
107 if (BType != SymbolRef::ST_Function)
111 return AAddr < BAddr;
115 // Types for the storted data in code table that is built before disassembly
116 // and the predicate function to sort them.
117 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
118 typedef std::vector<DiceTableEntry> DiceTable;
119 typedef DiceTable::iterator dice_table_iterator;
122 compareDiceTableEntries(const DiceTableEntry i,
123 const DiceTableEntry j) {
124 return i.first == j.first;
127 static void DumpDataInCode(const char *bytes, uint64_t Size,
128 unsigned short Kind) {
132 case MachO::DICE_KIND_DATA:
135 Value = bytes[3] << 24 |
139 outs() << "\t.long " << Value;
142 Value = bytes[1] << 8 |
144 outs() << "\t.short " << Value;
148 outs() << "\t.byte " << Value;
151 outs() << "\t@ KIND_DATA\n";
153 case MachO::DICE_KIND_JUMP_TABLE8:
155 outs() << "\t.byte " << Value << "\t@ KIND_JUMP_TABLE8";
157 case MachO::DICE_KIND_JUMP_TABLE16:
158 Value = bytes[1] << 8 |
160 outs() << "\t.short " << Value << "\t@ KIND_JUMP_TABLE16";
162 case MachO::DICE_KIND_JUMP_TABLE32:
163 Value = bytes[3] << 24 |
167 outs() << "\t.long " << Value << "\t@ KIND_JUMP_TABLE32";
170 outs() << "\t@ data in code kind = " << Kind << "\n";
175 static void getSectionsAndSymbols(const MachO::mach_header Header,
176 MachOObjectFile *MachOObj,
177 std::vector<SectionRef> &Sections,
178 std::vector<SymbolRef> &Symbols,
179 SmallVectorImpl<uint64_t> &FoundFns,
180 uint64_t &BaseSegmentAddress) {
181 for (const SymbolRef &Symbol : MachOObj->symbols())
182 Symbols.push_back(Symbol);
184 for (const SectionRef &Section : MachOObj->sections()) {
186 Section.getName(SectName);
187 Sections.push_back(Section);
190 MachOObjectFile::LoadCommandInfo Command =
191 MachOObj->getFirstLoadCommandInfo();
192 bool BaseSegmentAddressSet = false;
193 for (unsigned i = 0; ; ++i) {
194 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
195 // We found a function starts segment, parse the addresses for later
197 MachO::linkedit_data_command LLC =
198 MachOObj->getLinkeditDataLoadCommand(Command);
200 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
202 else if (Command.C.cmd == MachO::LC_SEGMENT) {
203 MachO::segment_command SLC =
204 MachOObj->getSegmentLoadCommand(Command);
205 StringRef SegName = SLC.segname;
206 if(!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
207 BaseSegmentAddressSet = true;
208 BaseSegmentAddress = SLC.vmaddr;
212 if (i == Header.ncmds - 1)
215 Command = MachOObj->getNextLoadCommandInfo(Command);
219 static void DisassembleInputMachO2(StringRef Filename,
220 MachOObjectFile *MachOOF);
222 void llvm::DisassembleInputMachO(StringRef Filename) {
223 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
224 MemoryBuffer::getFileOrSTDIN(Filename);
225 if (std::error_code EC = BuffOrErr.getError()) {
226 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << "\n";
229 std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
231 std::unique_ptr<MachOObjectFile> MachOOF = std::move(
232 ObjectFile::createMachOObjectFile(Buff.get()->getMemBufferRef()).get());
234 DisassembleInputMachO2(Filename, MachOOF.get());
237 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
239 // The block of info used by the Symbolizer call backs.
240 struct DisassembleInfo {
244 SymbolAddressMap *AddrMap;
247 // SymbolizerGetOpInfo() is the operand information call back function.
248 // This is called to get the symbolic information for operand(s) of an
249 // instruction when it is being done. This routine does this from
250 // the relocation information, symbol table, etc. That block of information
251 // is a pointer to the struct DisassembleInfo that was passed when the
252 // disassembler context was created and passed to back to here when
253 // called back by the disassembler for instruction operands that could have
254 // relocation information. The address of the instruction containing operand is
255 // at the Pc parameter. The immediate value the operand has is passed in
256 // op_info->Value and is at Offset past the start of the instruction and has a
257 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
258 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
259 // names and addends of the symbolic expression to add for the operand. The
260 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
261 // information is returned then this function returns 1 else it returns 0.
262 int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
263 uint64_t Size, int TagType, void *TagBuf) {
264 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
265 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
266 unsigned int value = op_info->Value;
268 // Make sure all fields returned are zero if we don't set them.
269 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
270 op_info->Value = value;
272 // If the TagType is not the value 1 which it code knows about or if no
273 // verbose symbolic information is wanted then just return 0, indicating no
274 // information is being returned.
275 if (TagType != 1 || info->verbose == false)
278 unsigned int Arch = info->O->getArch();
279 if (Arch == Triple::x86) {
281 } else if (Arch == Triple::x86_64) {
282 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
284 // First search the section's relocation entries (if any) for an entry
285 // for this section offset.
287 info->S.getAddress(sect_addr);
288 uint64_t sect_offset = (Pc + Offset) - sect_addr;
289 bool reloc_found = false;
291 MachO::any_relocation_info RE;
292 bool isExtern = false;
294 for (const RelocationRef &Reloc : info->S.relocations()) {
295 uint64_t RelocOffset;
296 Reloc.getOffset(RelocOffset);
297 if (RelocOffset == sect_offset) {
298 Rel = Reloc.getRawDataRefImpl();
299 RE = info->O->getRelocation(Rel);
300 // NOTE: Scattered relocations don't exist on x86_64.
301 isExtern = info->O->getPlainRelocationExternal(RE);
303 symbol_iterator RelocSym = Reloc.getSymbol();
310 if (reloc_found && isExtern) {
311 // The Value passed in will be adjusted by the Pc if the instruction
312 // adds the Pc. But for x86_64 external relocation entries the Value
313 // is the offset from the external symbol.
314 if (info->O->getAnyRelocationPCRel(RE))
315 op_info->Value -= Pc + Offset + Size;
317 Symbol.getName(SymName);
318 const char *name = SymName.data();
319 unsigned Type = info->O->getAnyRelocationType(RE);
320 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
321 DataRefImpl RelNext = Rel;
322 info->O->moveRelocationNext(RelNext);
323 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
324 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
325 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
326 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
327 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
328 op_info->SubtractSymbol.Present = 1;
329 op_info->SubtractSymbol.Name = name;
330 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
331 Symbol = *RelocSymNext;
332 StringRef SymNameNext;
333 Symbol.getName(SymNameNext);
334 name = SymNameNext.data();
337 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
338 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
339 op_info->AddSymbol.Present = 1;
340 op_info->AddSymbol.Name = name;
344 // Second search the external relocation entries of a fully linked image
345 // (if any) for an entry that matches this segment offset.
346 //uint64_t seg_offset = (Pc + Offset);
348 } else if (Arch == Triple::arm) {
350 } else if (Arch == Triple::aarch64) {
357 // GuessCstringPointer is passed the address of what might be a pointer to a
358 // literal string in a cstring section. If that address is in a cstring section
359 // it returns a pointer to that string. Else it returns nullptr.
360 const char *GuessCstringPointer(uint64_t ReferenceValue,
361 struct DisassembleInfo *info) {
362 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
363 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
364 for (unsigned I = 0;; ++I) {
365 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
366 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
367 for (unsigned J = 0; J < Seg.nsects; ++J) {
368 MachO::section_64 Sec = info->O->getSection64(Load, J);
369 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
370 if (section_type == MachO::S_CSTRING_LITERALS &&
371 ReferenceValue >= Sec.addr &&
372 ReferenceValue < Sec.addr + Sec.size) {
373 uint64_t sect_offset = ReferenceValue - Sec.addr;
374 uint64_t object_offset = Sec.offset + sect_offset;
375 StringRef MachOContents = info->O->getData();
376 uint64_t object_size = MachOContents.size();
377 const char *object_addr = (const char *)MachOContents.data();
378 if (object_offset < object_size) {
379 const char *name = object_addr + object_offset;
386 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
387 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
388 for (unsigned J = 0; J < Seg.nsects; ++J) {
389 MachO::section Sec = info->O->getSection(Load, J);
390 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
391 if (section_type == MachO::S_CSTRING_LITERALS &&
392 ReferenceValue >= Sec.addr &&
393 ReferenceValue < Sec.addr + Sec.size) {
394 uint64_t sect_offset = ReferenceValue - Sec.addr;
395 uint64_t object_offset = Sec.offset + sect_offset;
396 StringRef MachOContents = info->O->getData();
397 uint64_t object_size = MachOContents.size();
398 const char *object_addr = (const char *)MachOContents.data();
399 if (object_offset < object_size) {
400 const char *name = object_addr + object_offset;
408 if (I == LoadCommandCount - 1)
411 Load = info->O->getNextLoadCommandInfo(Load);
416 // GuessLiteralPointer returns a string which for the item in the Mach-O file
417 // for the address passed in as ReferenceValue for printing as a comment with
418 // the instruction and also returns the corresponding type of that item
419 // indirectly through ReferenceType.
421 // If ReferenceValue is an address of literal cstring then a pointer to the
422 // cstring is returned and ReferenceType is set to
423 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
425 // TODO: other literals such as Objective-C CFStrings refs, Selector refs,
426 // Message refs, Class refs and a Symbol address in a literal pool are yet
428 const char *GuessLiteralPointer(uint64_t ReferenceValue, uint64_t ReferencePC,
429 uint64_t *ReferenceType,
430 struct DisassembleInfo *info) {
431 // TODO: This rouine's code is only for an x86_64 Mach-O file for now.
432 unsigned int Arch = info->O->getArch();
433 if (Arch != Triple::x86_64)
436 // First see if there is an external relocation entry at the ReferencePC.
438 info->S.getAddress(sect_addr);
439 uint64_t sect_offset = ReferencePC - sect_addr;
440 bool reloc_found = false;
442 MachO::any_relocation_info RE;
443 bool isExtern = false;
445 for (const RelocationRef &Reloc : info->S.relocations()) {
446 uint64_t RelocOffset;
447 Reloc.getOffset(RelocOffset);
448 if (RelocOffset == sect_offset) {
449 Rel = Reloc.getRawDataRefImpl();
450 RE = info->O->getRelocation(Rel);
451 if (info->O->isRelocationScattered(RE))
453 isExtern = info->O->getPlainRelocationExternal(RE);
455 symbol_iterator RelocSym = Reloc.getSymbol();
462 // If there is an external relocation entry for a symbol in a section
463 // then used that symbol's value for the value of the reference.
464 if (reloc_found && isExtern) {
465 if (info->O->getAnyRelocationPCRel(RE)) {
466 unsigned Type = info->O->getAnyRelocationType(RE);
467 if (Type == MachO::X86_64_RELOC_SIGNED) {
468 Symbol.getAddress(ReferenceValue);
473 // TODO: the code to look for other literals such as Objective-C CFStrings
474 // refs, Selector refs, Message refs, Class refs will be added here.
476 const char *name = GuessCstringPointer(ReferenceValue, info);
478 // TODO: note when the code is added above for Selector refs and Message
479 // refs we will need check for that here and set the ReferenceType
481 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
485 // TODO: look for an indirect symbol with this ReferenceValue which is in
491 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
492 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
493 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
494 // is created and returns the symbol name that matches the ReferenceValue or
495 // nullptr if none. The ReferenceType is passed in for the IN type of
496 // reference the instruction is making from the values in defined in the header
497 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
498 // Out type and the ReferenceName will also be set which is added as a comment
499 // to the disassembled instruction.
501 // If the symbol name is a C++ mangled name then the demangled name is
502 // returned through ReferenceName and ReferenceType is set to
503 // LLVMDisassembler_ReferenceType_DeMangled_Name .
505 // When this is called to get a symbol name for a branch target then the
506 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
507 // SymbolValue will be looked for in the indirect symbol table to determine if
508 // it is an address for a symbol stub. If so then the symbol name for that
509 // stub is returned indirectly through ReferenceName and then ReferenceType is
510 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
512 // When this is called with an value loaded via a PC relative load then
513 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
514 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
515 // or an Objective-C meta data reference. If so the output ReferenceType is
516 // set to correspond to that as well as ReferenceName.
517 const char *SymbolizerSymbolLookUp(void *DisInfo, uint64_t ReferenceValue,
518 uint64_t *ReferenceType,
519 uint64_t ReferencePC,
520 const char **ReferenceName) {
521 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
522 // If no verbose symbolic information is wanted then just return nullptr.
523 if (info->verbose == false) {
524 *ReferenceName = nullptr;
525 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
529 const char *SymbolName = nullptr;
530 StringRef name = info->AddrMap->lookup(ReferenceValue);
532 SymbolName = name.data();
534 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
535 *ReferenceName = GuessLiteralPointer(ReferenceValue, ReferencePC,
536 ReferenceType, info);
537 if (*ReferenceName == nullptr)
538 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
539 // TODO: other types of references to be added.
541 *ReferenceName = nullptr;
542 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
549 // This is the memory object used by DisAsm->getInstruction() which has its
550 // BasePC. This then allows the 'address' parameter to getInstruction() to
551 // be the actual PC of the instruction. Then when a branch dispacement is
552 // added to the PC of an instruction, the 'ReferenceValue' passed to the
553 // SymbolizerSymbolLookUp() routine is the correct target addresses. As in
554 // the case of a fully linked Mach-O file where a section being disassembled
555 // generally not linked at address zero.
557 class DisasmMemoryObject : public MemoryObject {
562 DisasmMemoryObject(uint8_t *bytes, uint64_t size, uint64_t basePC) :
563 Bytes(bytes), Size(size), BasePC(basePC) {}
565 uint64_t getBase() const override { return BasePC; }
566 uint64_t getExtent() const override { return Size; }
568 int readByte(uint64_t Addr, uint8_t *Byte) const override {
569 if (Addr - BasePC >= Size)
571 *Byte = Bytes[Addr - BasePC];
576 /// \brief Emits the comments that are stored in the CommentStream.
577 /// Each comment in the CommentStream must end with a newline.
578 static void emitComments(raw_svector_ostream &CommentStream,
579 SmallString<128> &CommentsToEmit,
580 formatted_raw_ostream &FormattedOS,
581 const MCAsmInfo &MAI) {
582 // Flush the stream before taking its content.
583 CommentStream.flush();
584 StringRef Comments = CommentsToEmit.str();
585 // Get the default information for printing a comment.
586 const char *CommentBegin = MAI.getCommentString();
587 unsigned CommentColumn = MAI.getCommentColumn();
589 while (!Comments.empty()) {
592 // Emit a line of comments.
593 FormattedOS.PadToColumn(CommentColumn);
594 size_t Position = Comments.find('\n');
595 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
596 // Move after the newline character.
597 Comments = Comments.substr(Position + 1);
602 // Tell the comment stream that the vector changed underneath it.
603 CommentsToEmit.clear();
604 CommentStream.resync();
607 static void DisassembleInputMachO2(StringRef Filename,
608 MachOObjectFile *MachOOF) {
609 const char *McpuDefault = nullptr;
610 const Target *ThumbTarget = nullptr;
611 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
613 // GetTarget prints out stuff.
616 if (MCPU.empty() && McpuDefault)
619 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
620 std::unique_ptr<MCInstrAnalysis> InstrAnalysis(
621 TheTarget->createMCInstrAnalysis(InstrInfo.get()));
622 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
623 std::unique_ptr<MCInstrAnalysis> ThumbInstrAnalysis;
625 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
626 ThumbInstrAnalysis.reset(
627 ThumbTarget->createMCInstrAnalysis(ThumbInstrInfo.get()));
630 // Package up features to be passed to target/subtarget
631 std::string FeaturesStr;
633 SubtargetFeatures Features;
634 for (unsigned i = 0; i != MAttrs.size(); ++i)
635 Features.AddFeature(MAttrs[i]);
636 FeaturesStr = Features.getString();
639 // Set up disassembler.
640 std::unique_ptr<const MCRegisterInfo> MRI(
641 TheTarget->createMCRegInfo(TripleName));
642 std::unique_ptr<const MCAsmInfo> AsmInfo(
643 TheTarget->createMCAsmInfo(*MRI, TripleName));
644 std::unique_ptr<const MCSubtargetInfo> STI(
645 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
646 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
647 std::unique_ptr<MCDisassembler> DisAsm(
648 TheTarget->createMCDisassembler(*STI, Ctx));
649 std::unique_ptr<MCSymbolizer> Symbolizer;
650 struct DisassembleInfo SymbolizerInfo;
651 std::unique_ptr<MCRelocationInfo> RelInfo(
652 TheTarget->createMCRelocationInfo(TripleName, Ctx));
654 Symbolizer.reset(TheTarget->createMCSymbolizer(
655 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
656 &SymbolizerInfo, &Ctx, RelInfo.release()));
657 DisAsm->setSymbolizer(std::move(Symbolizer));
659 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
660 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
661 AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI, *STI));
662 // Set the display preference for hex vs. decimal immediates.
663 IP->setPrintImmHex(PrintImmHex);
664 // Comment stream and backing vector.
665 SmallString<128> CommentsToEmit;
666 raw_svector_ostream CommentStream(CommentsToEmit);
667 IP->setCommentStream(CommentStream);
669 if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) {
670 errs() << "error: couldn't initialize disassembler for target "
671 << TripleName << '\n';
675 // Set up thumb disassembler.
676 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
677 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
678 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
679 std::unique_ptr<const MCDisassembler> ThumbDisAsm;
680 std::unique_ptr<MCInstPrinter> ThumbIP;
681 std::unique_ptr<MCContext> ThumbCtx;
683 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
685 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
687 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
688 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
689 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
690 // TODO: add MCSymbolizer here for the ThumbTarget like above for TheTarget.
691 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
692 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
693 ThumbAsmPrinterVariant, *ThumbAsmInfo, *ThumbInstrInfo, *ThumbMRI,
695 // Set the display preference for hex vs. decimal immediates.
696 ThumbIP->setPrintImmHex(PrintImmHex);
699 if (ThumbTarget && (!ThumbInstrAnalysis || !ThumbAsmInfo || !ThumbSTI ||
700 !ThumbDisAsm || !ThumbIP)) {
701 errs() << "error: couldn't initialize disassembler for target "
702 << ThumbTripleName << '\n';
706 outs() << '\n' << Filename << ":\n\n";
708 MachO::mach_header Header = MachOOF->getHeader();
710 // FIXME: Using the -cfg command line option, this code used to be able to
711 // annotate relocations with the referenced symbol's name, and if this was
712 // inside a __[cf]string section, the data it points to. This is now replaced
713 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
714 std::vector<SectionRef> Sections;
715 std::vector<SymbolRef> Symbols;
716 SmallVector<uint64_t, 8> FoundFns;
717 uint64_t BaseSegmentAddress;
719 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
722 // Sort the symbols by address, just in case they didn't come in that way.
723 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
725 // Build a data in code table that is sorted on by the address of each entry.
726 uint64_t BaseAddress = 0;
727 if (Header.filetype == MachO::MH_OBJECT)
728 Sections[0].getAddress(BaseAddress);
730 BaseAddress = BaseSegmentAddress;
732 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
735 DI->getOffset(Offset);
736 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
738 array_pod_sort(Dices.begin(), Dices.end());
741 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
743 raw_ostream &DebugOut = nulls();
746 std::unique_ptr<DIContext> diContext;
747 ObjectFile *DbgObj = MachOOF;
748 // Try to find debug info and set up the DIContext for it.
750 // A separate DSym file path was specified, parse it as a macho file,
751 // get the sections and supply it to the section name parsing machinery.
752 if (!DSYMFile.empty()) {
753 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
754 MemoryBuffer::getFileOrSTDIN(DSYMFile);
755 if (std::error_code EC = BufOrErr.getError()) {
756 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
760 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
765 // Setup the DIContext
766 diContext.reset(DIContext::getDWARFContext(*DbgObj));
769 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
771 bool SectIsText = false;
772 Sections[SectIdx].isText(SectIsText);
773 if (SectIsText == false)
777 if (Sections[SectIdx].getName(SectName) ||
778 SectName != "__text")
779 continue; // Skip non-text sections
781 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
783 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
784 if (SegmentName != "__TEXT")
788 Sections[SectIdx].getContents(Bytes);
789 uint64_t SectAddress = 0;
790 Sections[SectIdx].getAddress(SectAddress);
791 DisasmMemoryObject MemoryObject((uint8_t *)Bytes.data(), Bytes.size(),
793 bool symbolTableWorked = false;
795 // Parse relocations.
796 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
797 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
798 uint64_t RelocOffset, SectionAddress;
799 Reloc.getOffset(RelocOffset);
800 Sections[SectIdx].getAddress(SectionAddress);
801 RelocOffset -= SectionAddress;
803 symbol_iterator RelocSym = Reloc.getSymbol();
805 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
807 array_pod_sort(Relocs.begin(), Relocs.end());
809 // Create a map of symbol addresses to symbol names for use by
810 // the SymbolizerSymbolLookUp() routine.
811 SymbolAddressMap AddrMap;
812 for (const SymbolRef &Symbol : MachOOF->symbols()) {
815 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
816 ST == SymbolRef::ST_Other) {
818 Symbol.getAddress(Address);
820 Symbol.getName(SymName);
821 AddrMap[Address] = SymName;
824 // Set up the block of info used by the Symbolizer call backs.
825 SymbolizerInfo.verbose = true;
826 SymbolizerInfo.O = MachOOF;
827 SymbolizerInfo.S = Sections[SectIdx];
828 SymbolizerInfo.AddrMap = &AddrMap;
830 // Disassemble symbol by symbol.
831 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
833 Symbols[SymIdx].getName(SymName);
836 Symbols[SymIdx].getType(ST);
837 if (ST != SymbolRef::ST_Function)
840 // Make sure the symbol is defined in this section.
841 bool containsSym = false;
842 Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym);
846 // Start at the address of the symbol relative to the section's address.
847 uint64_t SectionAddress = 0;
849 Sections[SectIdx].getAddress(SectionAddress);
850 Symbols[SymIdx].getAddress(Start);
851 Start -= SectionAddress;
853 // Stop disassembling either at the beginning of the next symbol or at
854 // the end of the section.
855 bool containsNextSym = false;
856 uint64_t NextSym = 0;
857 uint64_t NextSymIdx = SymIdx+1;
858 while (Symbols.size() > NextSymIdx) {
859 SymbolRef::Type NextSymType;
860 Symbols[NextSymIdx].getType(NextSymType);
861 if (NextSymType == SymbolRef::ST_Function) {
862 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx],
864 Symbols[NextSymIdx].getAddress(NextSym);
865 NextSym -= SectionAddress;
872 Sections[SectIdx].getSize(SectSize);
873 uint64_t End = containsNextSym ? NextSym : SectSize;
876 symbolTableWorked = true;
878 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
880 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
882 outs() << SymName << ":\n";
884 for (uint64_t Index = Start; Index < End; Index += Size) {
887 uint64_t PC = SectAddress + Index;
888 if (FullLeadingAddr) {
889 if (MachOOF->is64Bit())
890 outs() << format("%016" PRIx64, PC);
892 outs() << format("%08" PRIx64, PC);
894 outs() << format("%8" PRIx64 ":", PC);
899 // Check the data in code table here to see if this is data not an
900 // instruction to be disassembled.
902 Dice.push_back(std::make_pair(PC, DiceRef()));
903 dice_table_iterator DTI = std::search(Dices.begin(), Dices.end(),
904 Dice.begin(), Dice.end(),
905 compareDiceTableEntries);
906 if (DTI != Dices.end()){
908 DTI->second.getLength(Length);
909 DumpBytes(StringRef(Bytes.data() + Index, Length));
911 DTI->second.getKind(Kind);
912 DumpDataInCode(Bytes.data() + Index, Length, Kind);
916 SmallVector<char, 64> AnnotationsBytes;
917 raw_svector_ostream Annotations(AnnotationsBytes);
921 gotInst = ThumbDisAsm->getInstruction(Inst, Size, MemoryObject, PC,
922 DebugOut, Annotations);
924 gotInst = DisAsm->getInstruction(Inst, Size, MemoryObject, PC,
925 DebugOut, Annotations);
927 if (!NoShowRawInsn) {
928 DumpBytes(StringRef(Bytes.data() + Index, Size));
930 formatted_raw_ostream FormattedOS(outs());
932 StringRef AnnotationsStr = Annotations.str();
934 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr);
936 IP->printInst(&Inst, FormattedOS, AnnotationsStr);
937 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
942 diContext->getLineInfoForAddress(PC);
943 // Print valid line info if it changed.
944 if (dli != lastLine && dli.Line != 0)
945 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
951 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
953 Size = 1; // skip illegible bytes
957 if (!symbolTableWorked) {
958 // Reading the symbol table didn't work, disassemble the whole section.
959 uint64_t SectAddress;
960 Sections[SectIdx].getAddress(SectAddress);
962 Sections[SectIdx].getSize(SectSize);
964 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
967 uint64_t PC = SectAddress + Index;
968 if (DisAsm->getInstruction(Inst, InstSize, MemoryObject, PC,
969 DebugOut, nulls())) {
970 if (FullLeadingAddr) {
971 if (MachOOF->is64Bit())
972 outs() << format("%016" PRIx64, PC);
974 outs() << format("%08" PRIx64, PC);
976 outs() << format("%8" PRIx64 ":", PC);
978 if (!NoShowRawInsn) {
980 DumpBytes(StringRef(Bytes.data() + Index, InstSize));
982 IP->printInst(&Inst, outs(), "");
985 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
987 InstSize = 1; // skip illegible bytes
995 //===----------------------------------------------------------------------===//
996 // __compact_unwind section dumping
997 //===----------------------------------------------------------------------===//
1001 template <typename T> static uint64_t readNext(const char *&Buf) {
1002 using llvm::support::little;
1003 using llvm::support::unaligned;
1005 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
1010 struct CompactUnwindEntry {
1011 uint32_t OffsetInSection;
1013 uint64_t FunctionAddr;
1015 uint32_t CompactEncoding;
1016 uint64_t PersonalityAddr;
1019 RelocationRef FunctionReloc;
1020 RelocationRef PersonalityReloc;
1021 RelocationRef LSDAReloc;
1023 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
1024 : OffsetInSection(Offset) {
1026 read<uint64_t>(Contents.data() + Offset);
1028 read<uint32_t>(Contents.data() + Offset);
1032 template<typename UIntPtr>
1033 void read(const char *Buf) {
1034 FunctionAddr = readNext<UIntPtr>(Buf);
1035 Length = readNext<uint32_t>(Buf);
1036 CompactEncoding = readNext<uint32_t>(Buf);
1037 PersonalityAddr = readNext<UIntPtr>(Buf);
1038 LSDAAddr = readNext<UIntPtr>(Buf);
1043 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
1044 /// and data being relocated, determine the best base Name and Addend to use for
1045 /// display purposes.
1047 /// 1. An Extern relocation will directly reference a symbol (and the data is
1048 /// then already an addend), so use that.
1049 /// 2. Otherwise the data is an offset in the object file's layout; try to find
1050 // a symbol before it in the same section, and use the offset from there.
1051 /// 3. Finally, if all that fails, fall back to an offset from the start of the
1052 /// referenced section.
1053 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
1054 std::map<uint64_t, SymbolRef> &Symbols,
1055 const RelocationRef &Reloc,
1057 StringRef &Name, uint64_t &Addend) {
1058 if (Reloc.getSymbol() != Obj->symbol_end()) {
1059 Reloc.getSymbol()->getName(Name);
1064 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
1065 SectionRef RelocSection = Obj->getRelocationSection(RE);
1067 uint64_t SectionAddr;
1068 RelocSection.getAddress(SectionAddr);
1070 auto Sym = Symbols.upper_bound(Addr);
1071 if (Sym == Symbols.begin()) {
1072 // The first symbol in the object is after this reference, the best we can
1073 // do is section-relative notation.
1074 RelocSection.getName(Name);
1075 Addend = Addr - SectionAddr;
1079 // Go back one so that SymbolAddress <= Addr.
1082 section_iterator SymSection = Obj->section_end();
1083 Sym->second.getSection(SymSection);
1084 if (RelocSection == *SymSection) {
1085 // There's a valid symbol in the same section before this reference.
1086 Sym->second.getName(Name);
1087 Addend = Addr - Sym->first;
1091 // There is a symbol before this reference, but it's in a different
1092 // section. Probably not helpful to mention it, so use the section name.
1093 RelocSection.getName(Name);
1094 Addend = Addr - SectionAddr;
1097 static void printUnwindRelocDest(const MachOObjectFile *Obj,
1098 std::map<uint64_t, SymbolRef> &Symbols,
1099 const RelocationRef &Reloc,
1104 if (!Reloc.getObjectFile())
1107 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
1111 outs() << " + " << format("0x%" PRIx64, Addend);
1115 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
1116 std::map<uint64_t, SymbolRef> &Symbols,
1117 const SectionRef &CompactUnwind) {
1119 assert(Obj->isLittleEndian() &&
1120 "There should not be a big-endian .o with __compact_unwind");
1122 bool Is64 = Obj->is64Bit();
1123 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
1124 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
1127 CompactUnwind.getContents(Contents);
1129 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
1131 // First populate the initial raw offsets, encodings and so on from the entry.
1132 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
1133 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
1134 CompactUnwinds.push_back(Entry);
1137 // Next we need to look at the relocations to find out what objects are
1138 // actually being referred to.
1139 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
1140 uint64_t RelocAddress;
1141 Reloc.getOffset(RelocAddress);
1143 uint32_t EntryIdx = RelocAddress / EntrySize;
1144 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
1145 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
1147 if (OffsetInEntry == 0)
1148 Entry.FunctionReloc = Reloc;
1149 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
1150 Entry.PersonalityReloc = Reloc;
1151 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
1152 Entry.LSDAReloc = Reloc;
1154 llvm_unreachable("Unexpected relocation in __compact_unwind section");
1157 // Finally, we're ready to print the data we've gathered.
1158 outs() << "Contents of __compact_unwind section:\n";
1159 for (auto &Entry : CompactUnwinds) {
1160 outs() << " Entry at offset "
1161 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
1163 // 1. Start of the region this entry applies to.
1164 outs() << " start: "
1165 << format("0x%" PRIx64, Entry.FunctionAddr) << ' ';
1166 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc,
1167 Entry.FunctionAddr);
1170 // 2. Length of the region this entry applies to.
1171 outs() << " length: "
1172 << format("0x%" PRIx32, Entry.Length) << '\n';
1173 // 3. The 32-bit compact encoding.
1174 outs() << " compact encoding: "
1175 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
1177 // 4. The personality function, if present.
1178 if (Entry.PersonalityReloc.getObjectFile()) {
1179 outs() << " personality function: "
1180 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
1181 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
1182 Entry.PersonalityAddr);
1186 // 5. This entry's language-specific data area.
1187 if (Entry.LSDAReloc.getObjectFile()) {
1189 << format("0x%" PRIx64, Entry.LSDAAddr) << ' ';
1190 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
1196 //===----------------------------------------------------------------------===//
1197 // __unwind_info section dumping
1198 //===----------------------------------------------------------------------===//
1200 static void printRegularSecondLevelUnwindPage(const char *PageStart) {
1201 const char *Pos = PageStart;
1202 uint32_t Kind = readNext<uint32_t>(Pos);
1204 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
1206 uint16_t EntriesStart = readNext<uint16_t>(Pos);
1207 uint16_t NumEntries = readNext<uint16_t>(Pos);
1209 Pos = PageStart + EntriesStart;
1210 for (unsigned i = 0; i < NumEntries; ++i) {
1211 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
1212 uint32_t Encoding = readNext<uint32_t>(Pos);
1214 outs() << " [" << i << "]: "
1215 << "function offset="
1216 << format("0x%08" PRIx32, FunctionOffset) << ", "
1218 << format("0x%08" PRIx32, Encoding)
1223 static void printCompressedSecondLevelUnwindPage(
1224 const char *PageStart, uint32_t FunctionBase,
1225 const SmallVectorImpl<uint32_t> &CommonEncodings) {
1226 const char *Pos = PageStart;
1227 uint32_t Kind = readNext<uint32_t>(Pos);
1229 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
1231 uint16_t EntriesStart = readNext<uint16_t>(Pos);
1232 uint16_t NumEntries = readNext<uint16_t>(Pos);
1234 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
1235 readNext<uint16_t>(Pos);
1236 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
1237 PageStart + EncodingsStart);
1239 Pos = PageStart + EntriesStart;
1240 for (unsigned i = 0; i < NumEntries; ++i) {
1241 uint32_t Entry = readNext<uint32_t>(Pos);
1242 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
1243 uint32_t EncodingIdx = Entry >> 24;
1246 if (EncodingIdx < CommonEncodings.size())
1247 Encoding = CommonEncodings[EncodingIdx];
1249 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
1251 outs() << " [" << i << "]: "
1252 << "function offset="
1253 << format("0x%08" PRIx32, FunctionOffset) << ", "
1254 << "encoding[" << EncodingIdx << "]="
1255 << format("0x%08" PRIx32, Encoding)
1261 printMachOUnwindInfoSection(const MachOObjectFile *Obj,
1262 std::map<uint64_t, SymbolRef> &Symbols,
1263 const SectionRef &UnwindInfo) {
1265 assert(Obj->isLittleEndian() &&
1266 "There should not be a big-endian .o with __unwind_info");
1268 outs() << "Contents of __unwind_info section:\n";
1271 UnwindInfo.getContents(Contents);
1272 const char *Pos = Contents.data();
1274 //===----------------------------------
1276 //===----------------------------------
1278 uint32_t Version = readNext<uint32_t>(Pos);
1279 outs() << " Version: "
1280 << format("0x%" PRIx32, Version) << '\n';
1281 assert(Version == 1 && "only understand version 1");
1283 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
1284 outs() << " Common encodings array section offset: "
1285 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
1286 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
1287 outs() << " Number of common encodings in array: "
1288 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
1290 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
1291 outs() << " Personality function array section offset: "
1292 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
1293 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
1294 outs() << " Number of personality functions in array: "
1295 << format("0x%" PRIx32, NumPersonalities) << '\n';
1297 uint32_t IndicesStart = readNext<uint32_t>(Pos);
1298 outs() << " Index array section offset: "
1299 << format("0x%" PRIx32, IndicesStart) << '\n';
1300 uint32_t NumIndices = readNext<uint32_t>(Pos);
1301 outs() << " Number of indices in array: "
1302 << format("0x%" PRIx32, NumIndices) << '\n';
1304 //===----------------------------------
1305 // A shared list of common encodings
1306 //===----------------------------------
1308 // These occupy indices in the range [0, N] whenever an encoding is referenced
1309 // from a compressed 2nd level index table. In practice the linker only
1310 // creates ~128 of these, so that indices are available to embed encodings in
1311 // the 2nd level index.
1313 SmallVector<uint32_t, 64> CommonEncodings;
1314 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
1315 Pos = Contents.data() + CommonEncodingsStart;
1316 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
1317 uint32_t Encoding = readNext<uint32_t>(Pos);
1318 CommonEncodings.push_back(Encoding);
1320 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
1325 //===----------------------------------
1326 // Personality functions used in this executable
1327 //===----------------------------------
1329 // There should be only a handful of these (one per source language,
1330 // roughly). Particularly since they only get 2 bits in the compact encoding.
1332 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
1333 Pos = Contents.data() + PersonalitiesStart;
1334 for (unsigned i = 0; i < NumPersonalities; ++i) {
1335 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
1336 outs() << " personality[" << i + 1
1337 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
1340 //===----------------------------------
1341 // The level 1 index entries
1342 //===----------------------------------
1344 // These specify an approximate place to start searching for the more detailed
1345 // information, sorted by PC.
1348 uint32_t FunctionOffset;
1349 uint32_t SecondLevelPageStart;
1353 SmallVector<IndexEntry, 4> IndexEntries;
1355 outs() << " Top level indices: (count = " << NumIndices << ")\n";
1356 Pos = Contents.data() + IndicesStart;
1357 for (unsigned i = 0; i < NumIndices; ++i) {
1360 Entry.FunctionOffset = readNext<uint32_t>(Pos);
1361 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
1362 Entry.LSDAStart = readNext<uint32_t>(Pos);
1363 IndexEntries.push_back(Entry);
1365 outs() << " [" << i << "]: "
1366 << "function offset="
1367 << format("0x%08" PRIx32, Entry.FunctionOffset) << ", "
1368 << "2nd level page offset="
1369 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
1371 << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
1375 //===----------------------------------
1376 // Next come the LSDA tables
1377 //===----------------------------------
1379 // The LSDA layout is rather implicit: it's a contiguous array of entries from
1380 // the first top-level index's LSDAOffset to the last (sentinel).
1382 outs() << " LSDA descriptors:\n";
1383 Pos = Contents.data() + IndexEntries[0].LSDAStart;
1384 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
1385 (2 * sizeof(uint32_t));
1386 for (int i = 0; i < NumLSDAs; ++i) {
1387 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
1388 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
1389 outs() << " [" << i << "]: "
1390 << "function offset="
1391 << format("0x%08" PRIx32, FunctionOffset) << ", "
1393 << format("0x%08" PRIx32, LSDAOffset) << '\n';
1396 //===----------------------------------
1397 // Finally, the 2nd level indices
1398 //===----------------------------------
1400 // Generally these are 4K in size, and have 2 possible forms:
1401 // + Regular stores up to 511 entries with disparate encodings
1402 // + Compressed stores up to 1021 entries if few enough compact encoding
1404 outs() << " Second level indices:\n";
1405 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
1406 // The final sentinel top-level index has no associated 2nd level page
1407 if (IndexEntries[i].SecondLevelPageStart == 0)
1410 outs() << " Second level index[" << i << "]: "
1411 << "offset in section="
1412 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
1414 << "base function offset="
1415 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
1417 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
1418 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
1420 printRegularSecondLevelUnwindPage(Pos);
1422 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
1425 llvm_unreachable("Do not know how to print this kind of 2nd level page");
1430 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
1431 std::map<uint64_t, SymbolRef> Symbols;
1432 for (const SymbolRef &SymRef : Obj->symbols()) {
1433 // Discard any undefined or absolute symbols. They're not going to take part
1434 // in the convenience lookup for unwind info and just take up resources.
1435 section_iterator Section = Obj->section_end();
1436 SymRef.getSection(Section);
1437 if (Section == Obj->section_end())
1441 SymRef.getAddress(Addr);
1442 Symbols.insert(std::make_pair(Addr, SymRef));
1445 for (const SectionRef &Section : Obj->sections()) {
1447 Section.getName(SectName);
1448 if (SectName == "__compact_unwind")
1449 printMachOCompactUnwindSection(Obj, Symbols, Section);
1450 else if (SectName == "__unwind_info")
1451 printMachOUnwindInfoSection(Obj, Symbols, Section);
1452 else if (SectName == "__eh_frame")
1453 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
1458 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
1459 uint32_t cpusubtype, uint32_t filetype,
1460 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
1462 outs() << "Mach header\n";
1463 outs() << " magic cputype cpusubtype caps filetype ncmds "
1464 "sizeofcmds flags\n";
1466 if (magic == MachO::MH_MAGIC)
1467 outs() << " MH_MAGIC";
1468 else if (magic == MachO::MH_MAGIC_64)
1469 outs() << "MH_MAGIC_64";
1471 outs() << format(" 0x%08" PRIx32, magic);
1473 case MachO::CPU_TYPE_I386:
1475 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1476 case MachO::CPU_SUBTYPE_I386_ALL:
1480 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1484 case MachO::CPU_TYPE_X86_64:
1485 outs() << " X86_64";
1486 case MachO::CPU_SUBTYPE_X86_64_ALL:
1489 case MachO::CPU_SUBTYPE_X86_64_H:
1490 outs() << " Haswell";
1491 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1493 case MachO::CPU_TYPE_ARM:
1495 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1496 case MachO::CPU_SUBTYPE_ARM_ALL:
1499 case MachO::CPU_SUBTYPE_ARM_V4T:
1502 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1505 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1506 outs() << " XSCALE";
1508 case MachO::CPU_SUBTYPE_ARM_V6:
1511 case MachO::CPU_SUBTYPE_ARM_V6M:
1514 case MachO::CPU_SUBTYPE_ARM_V7:
1517 case MachO::CPU_SUBTYPE_ARM_V7EM:
1520 case MachO::CPU_SUBTYPE_ARM_V7K:
1523 case MachO::CPU_SUBTYPE_ARM_V7M:
1526 case MachO::CPU_SUBTYPE_ARM_V7S:
1530 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1534 case MachO::CPU_TYPE_ARM64:
1536 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1537 case MachO::CPU_SUBTYPE_ARM64_ALL:
1541 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1545 case MachO::CPU_TYPE_POWERPC:
1547 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1548 case MachO::CPU_SUBTYPE_POWERPC_ALL:
1552 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1556 case MachO::CPU_TYPE_POWERPC64:
1558 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1559 case MachO::CPU_SUBTYPE_POWERPC_ALL:
1563 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1568 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
1571 outs() << format(" 0x%02" PRIx32,
1572 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
1575 case MachO::MH_OBJECT:
1576 outs() << " OBJECT";
1578 case MachO::MH_EXECUTE:
1579 outs() << " EXECUTE";
1581 case MachO::MH_FVMLIB:
1582 outs() << " FVMLIB";
1584 case MachO::MH_CORE:
1587 case MachO::MH_PRELOAD:
1588 outs() << " PRELOAD";
1590 case MachO::MH_DYLIB:
1593 case MachO::MH_DYLIB_STUB:
1594 outs() << " DYLIB_STUB";
1596 case MachO::MH_DYLINKER:
1597 outs() << " DYLINKER";
1599 case MachO::MH_BUNDLE:
1600 outs() << " BUNDLE";
1602 case MachO::MH_DSYM:
1605 case MachO::MH_KEXT_BUNDLE:
1606 outs() << " KEXTBUNDLE";
1609 outs() << format(" %10u", filetype);
1612 outs() << format(" %5u", ncmds);
1613 outs() << format(" %10u", sizeofcmds);
1615 if (f & MachO::MH_NOUNDEFS) {
1616 outs() << " NOUNDEFS";
1617 f &= ~MachO::MH_NOUNDEFS;
1619 if (f & MachO::MH_INCRLINK) {
1620 outs() << " INCRLINK";
1621 f &= ~MachO::MH_INCRLINK;
1623 if (f & MachO::MH_DYLDLINK) {
1624 outs() << " DYLDLINK";
1625 f &= ~MachO::MH_DYLDLINK;
1627 if (f & MachO::MH_BINDATLOAD) {
1628 outs() << " BINDATLOAD";
1629 f &= ~MachO::MH_BINDATLOAD;
1631 if (f & MachO::MH_PREBOUND) {
1632 outs() << " PREBOUND";
1633 f &= ~MachO::MH_PREBOUND;
1635 if (f & MachO::MH_SPLIT_SEGS) {
1636 outs() << " SPLIT_SEGS";
1637 f &= ~MachO::MH_SPLIT_SEGS;
1639 if (f & MachO::MH_LAZY_INIT) {
1640 outs() << " LAZY_INIT";
1641 f &= ~MachO::MH_LAZY_INIT;
1643 if (f & MachO::MH_TWOLEVEL) {
1644 outs() << " TWOLEVEL";
1645 f &= ~MachO::MH_TWOLEVEL;
1647 if (f & MachO::MH_FORCE_FLAT) {
1648 outs() << " FORCE_FLAT";
1649 f &= ~MachO::MH_FORCE_FLAT;
1651 if (f & MachO::MH_NOMULTIDEFS) {
1652 outs() << " NOMULTIDEFS";
1653 f &= ~MachO::MH_NOMULTIDEFS;
1655 if (f & MachO::MH_NOFIXPREBINDING) {
1656 outs() << " NOFIXPREBINDING";
1657 f &= ~MachO::MH_NOFIXPREBINDING;
1659 if (f & MachO::MH_PREBINDABLE) {
1660 outs() << " PREBINDABLE";
1661 f &= ~MachO::MH_PREBINDABLE;
1663 if (f & MachO::MH_ALLMODSBOUND) {
1664 outs() << " ALLMODSBOUND";
1665 f &= ~MachO::MH_ALLMODSBOUND;
1667 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
1668 outs() << " SUBSECTIONS_VIA_SYMBOLS";
1669 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
1671 if (f & MachO::MH_CANONICAL) {
1672 outs() << " CANONICAL";
1673 f &= ~MachO::MH_CANONICAL;
1675 if (f & MachO::MH_WEAK_DEFINES) {
1676 outs() << " WEAK_DEFINES";
1677 f &= ~MachO::MH_WEAK_DEFINES;
1679 if (f & MachO::MH_BINDS_TO_WEAK) {
1680 outs() << " BINDS_TO_WEAK";
1681 f &= ~MachO::MH_BINDS_TO_WEAK;
1683 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
1684 outs() << " ALLOW_STACK_EXECUTION";
1685 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
1687 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
1688 outs() << " DEAD_STRIPPABLE_DYLIB";
1689 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
1691 if (f & MachO::MH_PIE) {
1693 f &= ~MachO::MH_PIE;
1695 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
1696 outs() << " NO_REEXPORTED_DYLIBS";
1697 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
1699 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
1700 outs() << " MH_HAS_TLV_DESCRIPTORS";
1701 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
1703 if (f & MachO::MH_NO_HEAP_EXECUTION) {
1704 outs() << " MH_NO_HEAP_EXECUTION";
1705 f &= ~MachO::MH_NO_HEAP_EXECUTION;
1707 if (f & MachO::MH_APP_EXTENSION_SAFE) {
1708 outs() << " APP_EXTENSION_SAFE";
1709 f &= ~MachO::MH_APP_EXTENSION_SAFE;
1711 if (f != 0 || flags == 0)
1712 outs() << format(" 0x%08" PRIx32, f);
1714 outs() << format(" 0x%08" PRIx32, magic);
1715 outs() << format(" %7d", cputype);
1716 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1717 outs() << format(" 0x%02" PRIx32,
1718 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
1719 outs() << format(" %10u", filetype);
1720 outs() << format(" %5u", ncmds);
1721 outs() << format(" %10u", sizeofcmds);
1722 outs() << format(" 0x%08" PRIx32, flags);
1727 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
1728 StringRef SegName, uint64_t vmaddr,
1729 uint64_t vmsize, uint64_t fileoff,
1730 uint64_t filesize, uint32_t maxprot,
1731 uint32_t initprot, uint32_t nsects,
1732 uint32_t flags, uint32_t object_size,
1734 uint64_t expected_cmdsize;
1735 if (cmd == MachO::LC_SEGMENT) {
1736 outs() << " cmd LC_SEGMENT\n";
1737 expected_cmdsize = nsects;
1738 expected_cmdsize *= sizeof(struct MachO::section);
1739 expected_cmdsize += sizeof(struct MachO::segment_command);
1741 outs() << " cmd LC_SEGMENT_64\n";
1742 expected_cmdsize = nsects;
1743 expected_cmdsize *= sizeof(struct MachO::section_64);
1744 expected_cmdsize += sizeof(struct MachO::segment_command_64);
1746 outs() << " cmdsize " << cmdsize;
1747 if (cmdsize != expected_cmdsize)
1748 outs() << " Inconsistent size\n";
1751 outs() << " segname " << SegName << "\n";
1752 if (cmd == MachO::LC_SEGMENT_64) {
1753 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
1754 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
1756 outs() << " vmaddr " << format("0x%08" PRIx32, vmaddr) << "\n";
1757 outs() << " vmsize " << format("0x%08" PRIx32, vmsize) << "\n";
1759 outs() << " fileoff " << fileoff;
1760 if (fileoff > object_size)
1761 outs() << " (past end of file)\n";
1764 outs() << " filesize " << filesize;
1765 if (fileoff + filesize > object_size)
1766 outs() << " (past end of file)\n";
1771 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
1772 MachO::VM_PROT_EXECUTE)) != 0)
1773 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
1775 if (maxprot & MachO::VM_PROT_READ)
1776 outs() << " maxprot r";
1778 outs() << " maxprot -";
1779 if (maxprot & MachO::VM_PROT_WRITE)
1783 if (maxprot & MachO::VM_PROT_EXECUTE)
1789 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
1790 MachO::VM_PROT_EXECUTE)) != 0)
1791 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
1793 if (initprot & MachO::VM_PROT_READ)
1794 outs() << " initprot r";
1796 outs() << " initprot -";
1797 if (initprot & MachO::VM_PROT_WRITE)
1801 if (initprot & MachO::VM_PROT_EXECUTE)
1807 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
1808 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
1810 outs() << " nsects " << nsects << "\n";
1814 outs() << " (none)\n";
1816 if (flags & MachO::SG_HIGHVM) {
1817 outs() << " HIGHVM";
1818 flags &= ~MachO::SG_HIGHVM;
1820 if (flags & MachO::SG_FVMLIB) {
1821 outs() << " FVMLIB";
1822 flags &= ~MachO::SG_FVMLIB;
1824 if (flags & MachO::SG_NORELOC) {
1825 outs() << " NORELOC";
1826 flags &= ~MachO::SG_NORELOC;
1828 if (flags & MachO::SG_PROTECTED_VERSION_1) {
1829 outs() << " PROTECTED_VERSION_1";
1830 flags &= ~MachO::SG_PROTECTED_VERSION_1;
1833 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
1838 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
1842 static void PrintSection(const char *sectname, const char *segname,
1843 uint64_t addr, uint64_t size, uint32_t offset,
1844 uint32_t align, uint32_t reloff, uint32_t nreloc,
1845 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
1846 uint32_t cmd, const char *sg_segname,
1847 uint32_t filetype, uint32_t object_size,
1849 outs() << "Section\n";
1850 outs() << " sectname " << format("%.16s\n", sectname);
1851 outs() << " segname " << format("%.16s", segname);
1852 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
1853 outs() << " (does not match segment)\n";
1856 if (cmd == MachO::LC_SEGMENT_64) {
1857 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
1858 outs() << " size " << format("0x%016" PRIx64, size);
1860 outs() << " addr " << format("0x%08" PRIx32, addr) << "\n";
1861 outs() << " size " << format("0x%08" PRIx32, size);
1863 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
1864 outs() << " (past end of file)\n";
1867 outs() << " offset " << offset;
1868 if (offset > object_size)
1869 outs() << " (past end of file)\n";
1872 uint32_t align_shifted = 1 << align;
1873 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
1874 outs() << " reloff " << reloff;
1875 if (reloff > object_size)
1876 outs() << " (past end of file)\n";
1879 outs() << " nreloc " << nreloc;
1880 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
1881 outs() << " (past end of file)\n";
1884 uint32_t section_type = flags & MachO::SECTION_TYPE;
1887 if (section_type == MachO::S_REGULAR)
1888 outs() << " S_REGULAR\n";
1889 else if (section_type == MachO::S_ZEROFILL)
1890 outs() << " S_ZEROFILL\n";
1891 else if (section_type == MachO::S_CSTRING_LITERALS)
1892 outs() << " S_CSTRING_LITERALS\n";
1893 else if (section_type == MachO::S_4BYTE_LITERALS)
1894 outs() << " S_4BYTE_LITERALS\n";
1895 else if (section_type == MachO::S_8BYTE_LITERALS)
1896 outs() << " S_8BYTE_LITERALS\n";
1897 else if (section_type == MachO::S_16BYTE_LITERALS)
1898 outs() << " S_16BYTE_LITERALS\n";
1899 else if (section_type == MachO::S_LITERAL_POINTERS)
1900 outs() << " S_LITERAL_POINTERS\n";
1901 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
1902 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
1903 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
1904 outs() << " S_LAZY_SYMBOL_POINTERS\n";
1905 else if (section_type == MachO::S_SYMBOL_STUBS)
1906 outs() << " S_SYMBOL_STUBS\n";
1907 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
1908 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
1909 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
1910 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
1911 else if (section_type == MachO::S_COALESCED)
1912 outs() << " S_COALESCED\n";
1913 else if (section_type == MachO::S_INTERPOSING)
1914 outs() << " S_INTERPOSING\n";
1915 else if (section_type == MachO::S_DTRACE_DOF)
1916 outs() << " S_DTRACE_DOF\n";
1917 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
1918 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
1919 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
1920 outs() << " S_THREAD_LOCAL_REGULAR\n";
1921 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
1922 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
1923 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
1924 outs() << " S_THREAD_LOCAL_VARIABLES\n";
1925 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
1926 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
1927 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
1928 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
1930 outs() << format("0x%08" PRIx32, section_type) << "\n";
1931 outs() << "attributes";
1932 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
1933 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
1934 outs() << " PURE_INSTRUCTIONS";
1935 if (section_attributes & MachO::S_ATTR_NO_TOC)
1936 outs() << " NO_TOC";
1937 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
1938 outs() << " STRIP_STATIC_SYMS";
1939 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
1940 outs() << " NO_DEAD_STRIP";
1941 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
1942 outs() << " LIVE_SUPPORT";
1943 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
1944 outs() << " SELF_MODIFYING_CODE";
1945 if (section_attributes & MachO::S_ATTR_DEBUG)
1947 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
1948 outs() << " SOME_INSTRUCTIONS";
1949 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
1950 outs() << " EXT_RELOC";
1951 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
1952 outs() << " LOC_RELOC";
1953 if (section_attributes == 0)
1954 outs() << " (none)";
1957 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
1958 outs() << " reserved1 " << reserved1;
1959 if (section_type == MachO::S_SYMBOL_STUBS ||
1960 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
1961 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
1962 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
1963 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
1964 outs() << " (index into indirect symbol table)\n";
1967 outs() << " reserved2 " << reserved2;
1968 if (section_type == MachO::S_SYMBOL_STUBS)
1969 outs() << " (size of stubs)\n";
1974 static void PrintSymtabLoadCommand(MachO::symtab_command st, uint32_t cputype,
1975 uint32_t object_size) {
1976 outs() << " cmd LC_SYMTAB\n";
1977 outs() << " cmdsize " << st.cmdsize;
1978 if (st.cmdsize != sizeof(struct MachO::symtab_command))
1979 outs() << " Incorrect size\n";
1982 outs() << " symoff " << st.symoff;
1983 if (st.symoff > object_size)
1984 outs() << " (past end of file)\n";
1987 outs() << " nsyms " << st.nsyms;
1989 if (cputype & MachO::CPU_ARCH_ABI64) {
1990 big_size = st.nsyms;
1991 big_size *= sizeof(struct MachO::nlist_64);
1992 big_size += st.symoff;
1993 if (big_size > object_size)
1994 outs() << " (past end of file)\n";
1998 big_size = st.nsyms;
1999 big_size *= sizeof(struct MachO::nlist);
2000 big_size += st.symoff;
2001 if (big_size > object_size)
2002 outs() << " (past end of file)\n";
2006 outs() << " stroff " << st.stroff;
2007 if (st.stroff > object_size)
2008 outs() << " (past end of file)\n";
2011 outs() << " strsize " << st.strsize;
2012 big_size = st.stroff;
2013 big_size += st.strsize;
2014 if (big_size > object_size)
2015 outs() << " (past end of file)\n";
2020 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
2021 uint32_t nsyms, uint32_t object_size,
2023 outs() << " cmd LC_DYSYMTAB\n";
2024 outs() << " cmdsize " << dyst.cmdsize;
2025 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
2026 outs() << " Incorrect size\n";
2029 outs() << " ilocalsym " << dyst.ilocalsym;
2030 if (dyst.ilocalsym > nsyms)
2031 outs() << " (greater than the number of symbols)\n";
2034 outs() << " nlocalsym " << dyst.nlocalsym;
2036 big_size = dyst.ilocalsym;
2037 big_size += dyst.nlocalsym;
2038 if (big_size > nsyms)
2039 outs() << " (past the end of the symbol table)\n";
2042 outs() << " iextdefsym " << dyst.iextdefsym;
2043 if (dyst.iextdefsym > nsyms)
2044 outs() << " (greater than the number of symbols)\n";
2047 outs() << " nextdefsym " << dyst.nextdefsym;
2048 big_size = dyst.iextdefsym;
2049 big_size += dyst.nextdefsym;
2050 if (big_size > nsyms)
2051 outs() << " (past the end of the symbol table)\n";
2054 outs() << " iundefsym " << dyst.iundefsym;
2055 if (dyst.iundefsym > nsyms)
2056 outs() << " (greater than the number of symbols)\n";
2059 outs() << " nundefsym " << dyst.nundefsym;
2060 big_size = dyst.iundefsym;
2061 big_size += dyst.nundefsym;
2062 if (big_size > nsyms)
2063 outs() << " (past the end of the symbol table)\n";
2066 outs() << " tocoff " << dyst.tocoff;
2067 if (dyst.tocoff > object_size)
2068 outs() << " (past end of file)\n";
2071 outs() << " ntoc " << dyst.ntoc;
2072 big_size = dyst.ntoc;
2073 big_size *= sizeof(struct MachO::dylib_table_of_contents);
2074 big_size += dyst.tocoff;
2075 if (big_size > object_size)
2076 outs() << " (past end of file)\n";
2079 outs() << " modtaboff " << dyst.modtaboff;
2080 if (dyst.modtaboff > object_size)
2081 outs() << " (past end of file)\n";
2084 outs() << " nmodtab " << dyst.nmodtab;
2086 if (cputype & MachO::CPU_ARCH_ABI64) {
2087 modtabend = dyst.nmodtab;
2088 modtabend *= sizeof(struct MachO::dylib_module_64);
2089 modtabend += dyst.modtaboff;
2091 modtabend = dyst.nmodtab;
2092 modtabend *= sizeof(struct MachO::dylib_module);
2093 modtabend += dyst.modtaboff;
2095 if (modtabend > object_size)
2096 outs() << " (past end of file)\n";
2099 outs() << " extrefsymoff " << dyst.extrefsymoff;
2100 if (dyst.extrefsymoff > object_size)
2101 outs() << " (past end of file)\n";
2104 outs() << " nextrefsyms " << dyst.nextrefsyms;
2105 big_size = dyst.nextrefsyms;
2106 big_size *= sizeof(struct MachO::dylib_reference);
2107 big_size += dyst.extrefsymoff;
2108 if (big_size > object_size)
2109 outs() << " (past end of file)\n";
2112 outs() << " indirectsymoff " << dyst.indirectsymoff;
2113 if (dyst.indirectsymoff > object_size)
2114 outs() << " (past end of file)\n";
2117 outs() << " nindirectsyms " << dyst.nindirectsyms;
2118 big_size = dyst.nindirectsyms;
2119 big_size *= sizeof(uint32_t);
2120 big_size += dyst.indirectsymoff;
2121 if (big_size > object_size)
2122 outs() << " (past end of file)\n";
2125 outs() << " extreloff " << dyst.extreloff;
2126 if (dyst.extreloff > object_size)
2127 outs() << " (past end of file)\n";
2130 outs() << " nextrel " << dyst.nextrel;
2131 big_size = dyst.nextrel;
2132 big_size *= sizeof(struct MachO::relocation_info);
2133 big_size += dyst.extreloff;
2134 if (big_size > object_size)
2135 outs() << " (past end of file)\n";
2138 outs() << " locreloff " << dyst.locreloff;
2139 if (dyst.locreloff > object_size)
2140 outs() << " (past end of file)\n";
2143 outs() << " nlocrel " << dyst.nlocrel;
2144 big_size = dyst.nlocrel;
2145 big_size *= sizeof(struct MachO::relocation_info);
2146 big_size += dyst.locreloff;
2147 if (big_size > object_size)
2148 outs() << " (past end of file)\n";
2153 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
2154 uint32_t object_size) {
2155 if (dc.cmd == MachO::LC_DYLD_INFO)
2156 outs() << " cmd LC_DYLD_INFO\n";
2158 outs() << " cmd LC_DYLD_INFO_ONLY\n";
2159 outs() << " cmdsize " << dc.cmdsize;
2160 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
2161 outs() << " Incorrect size\n";
2164 outs() << " rebase_off " << dc.rebase_off;
2165 if (dc.rebase_off > object_size)
2166 outs() << " (past end of file)\n";
2169 outs() << " rebase_size " << dc.rebase_size;
2171 big_size = dc.rebase_off;
2172 big_size += dc.rebase_size;
2173 if (big_size > object_size)
2174 outs() << " (past end of file)\n";
2177 outs() << " bind_off " << dc.bind_off;
2178 if (dc.bind_off > object_size)
2179 outs() << " (past end of file)\n";
2182 outs() << " bind_size " << dc.bind_size;
2183 big_size = dc.bind_off;
2184 big_size += dc.bind_size;
2185 if (big_size > object_size)
2186 outs() << " (past end of file)\n";
2189 outs() << " weak_bind_off " << dc.weak_bind_off;
2190 if (dc.weak_bind_off > object_size)
2191 outs() << " (past end of file)\n";
2194 outs() << " weak_bind_size " << dc.weak_bind_size;
2195 big_size = dc.weak_bind_off;
2196 big_size += dc.weak_bind_size;
2197 if (big_size > object_size)
2198 outs() << " (past end of file)\n";
2201 outs() << " lazy_bind_off " << dc.lazy_bind_off;
2202 if (dc.lazy_bind_off > object_size)
2203 outs() << " (past end of file)\n";
2206 outs() << " lazy_bind_size " << dc.lazy_bind_size;
2207 big_size = dc.lazy_bind_off;
2208 big_size += dc.lazy_bind_size;
2209 if (big_size > object_size)
2210 outs() << " (past end of file)\n";
2213 outs() << " export_off " << dc.export_off;
2214 if (dc.export_off > object_size)
2215 outs() << " (past end of file)\n";
2218 outs() << " export_size " << dc.export_size;
2219 big_size = dc.export_off;
2220 big_size += dc.export_size;
2221 if (big_size > object_size)
2222 outs() << " (past end of file)\n";
2227 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
2229 if (dyld.cmd == MachO::LC_ID_DYLINKER)
2230 outs() << " cmd LC_ID_DYLINKER\n";
2231 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
2232 outs() << " cmd LC_LOAD_DYLINKER\n";
2233 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
2234 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
2236 outs() << " cmd ?(" << dyld.cmd << ")\n";
2237 outs() << " cmdsize " << dyld.cmdsize;
2238 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
2239 outs() << " Incorrect size\n";
2242 if (dyld.name >= dyld.cmdsize)
2243 outs() << " name ?(bad offset " << dyld.name << ")\n";
2245 const char *P = (const char *)(Ptr)+dyld.name;
2246 outs() << " name " << P << " (offset " << dyld.name << ")\n";
2250 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
2251 outs() << " cmd LC_UUID\n";
2252 outs() << " cmdsize " << uuid.cmdsize;
2253 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
2254 outs() << " Incorrect size\n";
2258 outs() << format("%02" PRIX32, uuid.uuid[0]);
2259 outs() << format("%02" PRIX32, uuid.uuid[1]);
2260 outs() << format("%02" PRIX32, uuid.uuid[2]);
2261 outs() << format("%02" PRIX32, uuid.uuid[3]);
2263 outs() << format("%02" PRIX32, uuid.uuid[4]);
2264 outs() << format("%02" PRIX32, uuid.uuid[5]);
2266 outs() << format("%02" PRIX32, uuid.uuid[6]);
2267 outs() << format("%02" PRIX32, uuid.uuid[7]);
2269 outs() << format("%02" PRIX32, uuid.uuid[8]);
2270 outs() << format("%02" PRIX32, uuid.uuid[9]);
2272 outs() << format("%02" PRIX32, uuid.uuid[10]);
2273 outs() << format("%02" PRIX32, uuid.uuid[11]);
2274 outs() << format("%02" PRIX32, uuid.uuid[12]);
2275 outs() << format("%02" PRIX32, uuid.uuid[13]);
2276 outs() << format("%02" PRIX32, uuid.uuid[14]);
2277 outs() << format("%02" PRIX32, uuid.uuid[15]);
2281 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
2282 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
2283 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
2284 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
2285 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
2287 outs() << " cmd " << vd.cmd << " (?)\n";
2288 outs() << " cmdsize " << vd.cmdsize;
2289 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
2290 outs() << " Incorrect size\n";
2293 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
2294 << ((vd.version >> 8) & 0xff);
2295 if ((vd.version & 0xff) != 0)
2296 outs() << "." << (vd.version & 0xff);
2299 outs() << " sdk n/a\n";
2301 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
2302 << ((vd.sdk >> 8) & 0xff);
2304 if ((vd.sdk & 0xff) != 0)
2305 outs() << "." << (vd.sdk & 0xff);
2309 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
2310 outs() << " cmd LC_SOURCE_VERSION\n";
2311 outs() << " cmdsize " << sd.cmdsize;
2312 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
2313 outs() << " Incorrect size\n";
2316 uint64_t a = (sd.version >> 40) & 0xffffff;
2317 uint64_t b = (sd.version >> 30) & 0x3ff;
2318 uint64_t c = (sd.version >> 20) & 0x3ff;
2319 uint64_t d = (sd.version >> 10) & 0x3ff;
2320 uint64_t e = sd.version & 0x3ff;
2321 outs() << " version " << a << "." << b;
2323 outs() << "." << c << "." << d << "." << e;
2325 outs() << "." << c << "." << d;
2331 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
2332 outs() << " cmd LC_MAIN\n";
2333 outs() << " cmdsize " << ep.cmdsize;
2334 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
2335 outs() << " Incorrect size\n";
2338 outs() << " entryoff " << ep.entryoff << "\n";
2339 outs() << " stacksize " << ep.stacksize << "\n";
2342 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
2343 if (dl.cmd == MachO::LC_ID_DYLIB)
2344 outs() << " cmd LC_ID_DYLIB\n";
2345 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
2346 outs() << " cmd LC_LOAD_DYLIB\n";
2347 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
2348 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
2349 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
2350 outs() << " cmd LC_REEXPORT_DYLIB\n";
2351 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
2352 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
2353 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
2354 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
2356 outs() << " cmd " << dl.cmd << " (unknown)\n";
2357 outs() << " cmdsize " << dl.cmdsize;
2358 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
2359 outs() << " Incorrect size\n";
2362 if (dl.dylib.name < dl.cmdsize) {
2363 const char *P = (const char *)(Ptr)+dl.dylib.name;
2364 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
2366 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
2368 outs() << " time stamp " << dl.dylib.timestamp << " ";
2369 time_t t = dl.dylib.timestamp;
2370 outs() << ctime(&t);
2371 outs() << " current version ";
2372 if (dl.dylib.current_version == 0xffffffff)
2375 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
2376 << ((dl.dylib.current_version >> 8) & 0xff) << "."
2377 << (dl.dylib.current_version & 0xff) << "\n";
2378 outs() << "compatibility version ";
2379 if (dl.dylib.compatibility_version == 0xffffffff)
2382 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
2383 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
2384 << (dl.dylib.compatibility_version & 0xff) << "\n";
2387 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
2388 uint32_t object_size) {
2389 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
2390 outs() << " cmd LC_FUNCTION_STARTS\n";
2391 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
2392 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
2393 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
2394 outs() << " cmd LC_FUNCTION_STARTS\n";
2395 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
2396 outs() << " cmd LC_DATA_IN_CODE\n";
2397 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
2398 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
2399 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
2400 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
2402 outs() << " cmd " << ld.cmd << " (?)\n";
2403 outs() << " cmdsize " << ld.cmdsize;
2404 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
2405 outs() << " Incorrect size\n";
2408 outs() << " dataoff " << ld.dataoff;
2409 if (ld.dataoff > object_size)
2410 outs() << " (past end of file)\n";
2413 outs() << " datasize " << ld.datasize;
2414 uint64_t big_size = ld.dataoff;
2415 big_size += ld.datasize;
2416 if (big_size > object_size)
2417 outs() << " (past end of file)\n";
2422 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds,
2423 uint32_t filetype, uint32_t cputype,
2425 StringRef Buf = Obj->getData();
2426 MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo();
2427 for (unsigned i = 0;; ++i) {
2428 outs() << "Load command " << i << "\n";
2429 if (Command.C.cmd == MachO::LC_SEGMENT) {
2430 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
2431 const char *sg_segname = SLC.segname;
2432 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
2433 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
2434 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
2436 for (unsigned j = 0; j < SLC.nsects; j++) {
2437 MachO::section_64 S = Obj->getSection64(Command, j);
2438 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
2439 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
2440 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
2442 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
2443 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
2444 const char *sg_segname = SLC_64.segname;
2445 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
2446 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
2447 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
2448 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
2449 for (unsigned j = 0; j < SLC_64.nsects; j++) {
2450 MachO::section_64 S_64 = Obj->getSection64(Command, j);
2451 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
2452 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
2453 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
2454 sg_segname, filetype, Buf.size(), verbose);
2456 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
2457 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
2458 PrintSymtabLoadCommand(Symtab, cputype, Buf.size());
2459 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
2460 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
2461 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
2462 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(), cputype);
2463 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
2464 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
2465 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
2466 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
2467 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
2468 Command.C.cmd == MachO::LC_ID_DYLINKER ||
2469 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
2470 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
2471 PrintDyldLoadCommand(Dyld, Command.Ptr);
2472 } else if (Command.C.cmd == MachO::LC_UUID) {
2473 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
2474 PrintUuidLoadCommand(Uuid);
2475 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX) {
2476 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
2477 PrintVersionMinLoadCommand(Vd);
2478 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
2479 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
2480 PrintSourceVersionCommand(Sd);
2481 } else if (Command.C.cmd == MachO::LC_MAIN) {
2482 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
2483 PrintEntryPointCommand(Ep);
2484 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB) {
2485 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
2486 PrintDylibCommand(Dl, Command.Ptr);
2487 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
2488 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
2489 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
2490 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
2491 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
2492 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
2493 MachO::linkedit_data_command Ld =
2494 Obj->getLinkeditDataLoadCommand(Command);
2495 PrintLinkEditDataCommand(Ld, Buf.size());
2497 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
2499 outs() << " cmdsize " << Command.C.cmdsize << "\n";
2500 // TODO: get and print the raw bytes of the load command.
2502 // TODO: print all the other kinds of load commands.
2506 Command = Obj->getNextLoadCommandInfo(Command);
2510 static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds,
2511 uint32_t &filetype, uint32_t &cputype,
2513 if (Obj->is64Bit()) {
2514 MachO::mach_header_64 H_64;
2515 H_64 = Obj->getHeader64();
2516 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
2517 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
2519 filetype = H_64.filetype;
2520 cputype = H_64.cputype;
2522 MachO::mach_header H;
2523 H = Obj->getHeader();
2524 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
2525 H.sizeofcmds, H.flags, verbose);
2527 filetype = H.filetype;
2528 cputype = H.cputype;
2532 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
2533 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
2535 uint32_t filetype = 0;
2536 uint32_t cputype = 0;
2537 getAndPrintMachHeader(file, ncmds, filetype, cputype, true);
2538 PrintLoadCommands(file, ncmds, filetype, cputype, true);
2541 //===----------------------------------------------------------------------===//
2542 // export trie dumping
2543 //===----------------------------------------------------------------------===//
2545 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
2546 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
2547 uint64_t Flags = Entry.flags();
2548 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
2549 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
2550 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
2551 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
2552 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
2553 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
2554 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
2556 outs() << "[re-export] ";
2559 << format("0x%08llX ", Entry.address()); // FIXME:add in base address
2560 outs() << Entry.name();
2561 if (WeakDef || ThreadLocal || Resolver || Abs) {
2562 bool NeedsComma = false;
2565 outs() << "weak_def";
2571 outs() << "per-thread";
2577 outs() << "absolute";
2583 outs() << format("resolver=0x%08llX", Entry.other());
2589 StringRef DylibName = "unknown";
2590 int Ordinal = Entry.other() - 1;
2591 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
2592 if (Entry.otherName().empty())
2593 outs() << " (from " << DylibName << ")";
2595 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
2602 //===----------------------------------------------------------------------===//
2603 // rebase table dumping
2604 //===----------------------------------------------------------------------===//
2609 SegInfo(const object::MachOObjectFile *Obj);
2611 StringRef segmentName(uint32_t SegIndex);
2612 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
2613 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
2616 struct SectionInfo {
2619 StringRef SectionName;
2620 StringRef SegmentName;
2621 uint64_t OffsetInSegment;
2622 uint64_t SegmentStartAddress;
2623 uint32_t SegmentIndex;
2625 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
2626 SmallVector<SectionInfo, 32> Sections;
2630 SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
2631 // Build table of sections so segIndex/offset pairs can be translated.
2632 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
2633 StringRef CurSegName;
2634 uint64_t CurSegAddress;
2635 for (const SectionRef &Section : Obj->sections()) {
2637 if (error(Section.getName(Info.SectionName)))
2639 if (error(Section.getAddress(Info.Address)))
2641 if (error(Section.getSize(Info.Size)))
2644 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
2645 if (!Info.SegmentName.equals(CurSegName)) {
2647 CurSegName = Info.SegmentName;
2648 CurSegAddress = Info.Address;
2650 Info.SegmentIndex = CurSegIndex - 1;
2651 Info.OffsetInSegment = Info.Address - CurSegAddress;
2652 Info.SegmentStartAddress = CurSegAddress;
2653 Sections.push_back(Info);
2657 StringRef SegInfo::segmentName(uint32_t SegIndex) {
2658 for (const SectionInfo &SI : Sections) {
2659 if (SI.SegmentIndex == SegIndex)
2660 return SI.SegmentName;
2662 llvm_unreachable("invalid segIndex");
2665 const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
2666 uint64_t OffsetInSeg) {
2667 for (const SectionInfo &SI : Sections) {
2668 if (SI.SegmentIndex != SegIndex)
2670 if (SI.OffsetInSegment > OffsetInSeg)
2672 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
2676 llvm_unreachable("segIndex and offset not in any section");
2679 StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
2680 return findSection(SegIndex, OffsetInSeg).SectionName;
2683 uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
2684 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
2685 return SI.SegmentStartAddress + OffsetInSeg;
2688 void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
2689 // Build table of sections so names can used in final output.
2690 SegInfo sectionTable(Obj);
2692 outs() << "segment section address type\n";
2693 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
2694 uint32_t SegIndex = Entry.segmentIndex();
2695 uint64_t OffsetInSeg = Entry.segmentOffset();
2696 StringRef SegmentName = sectionTable.segmentName(SegIndex);
2697 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
2698 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
2700 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
2701 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
2702 SegmentName.str().c_str(),
2703 SectionName.str().c_str(), Address,
2704 Entry.typeName().str().c_str());
2708 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
2709 StringRef DylibName;
2711 case MachO::BIND_SPECIAL_DYLIB_SELF:
2712 return "this-image";
2713 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
2714 return "main-executable";
2715 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
2716 return "flat-namespace";
2719 std::error_code EC = Obj->getLibraryShortNameByIndex(Ordinal-1,
2722 return "<<ordinal too big>>";
2726 return "<<unknown special ordinal>>";
2729 //===----------------------------------------------------------------------===//
2730 // bind table dumping
2731 //===----------------------------------------------------------------------===//
2733 void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
2734 // Build table of sections so names can used in final output.
2735 SegInfo sectionTable(Obj);
2737 outs() << "segment section address type "
2738 "addend dylib symbol\n";
2739 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
2740 uint32_t SegIndex = Entry.segmentIndex();
2741 uint64_t OffsetInSeg = Entry.segmentOffset();
2742 StringRef SegmentName = sectionTable.segmentName(SegIndex);
2743 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
2744 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
2746 // Table lines look like:
2747 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
2748 outs() << format("%-8s %-18s 0x%08" PRIX64 " %-8s %-8" PRId64 " %-20s",
2749 SegmentName.str().c_str(),
2750 SectionName.str().c_str(),
2752 Entry.typeName().str().c_str(),
2754 ordinalName(Obj, Entry.ordinal()).str().c_str())
2755 << Entry.symbolName();
2756 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
2757 outs() << " (weak_import)\n";
2763 //===----------------------------------------------------------------------===//
2764 // lazy bind table dumping
2765 //===----------------------------------------------------------------------===//
2767 void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
2768 // Build table of sections so names can used in final output.
2769 SegInfo sectionTable(Obj);
2771 outs() << "segment section address "
2773 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
2774 uint32_t SegIndex = Entry.segmentIndex();
2775 uint64_t OffsetInSeg = Entry.segmentOffset();
2776 StringRef SegmentName = sectionTable.segmentName(SegIndex);
2777 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
2778 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
2780 // Table lines look like:
2781 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
2782 outs() << format("%-8s %-18s 0x%08" PRIX64 " %-20s",
2783 SegmentName.str().c_str(),
2784 SectionName.str().c_str(),
2786 ordinalName(Obj, Entry.ordinal()).str().c_str())
2787 << Entry.symbolName() << "\n";
2792 //===----------------------------------------------------------------------===//
2793 // weak bind table dumping
2794 //===----------------------------------------------------------------------===//
2796 void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
2797 // Build table of sections so names can used in final output.
2798 SegInfo sectionTable(Obj);
2800 outs() << "segment section address "
2801 "type addend symbol\n";
2802 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
2803 // Strong symbols don't have a location to update.
2804 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
2805 outs() << " strong "
2806 << Entry.symbolName() << "\n";
2809 uint32_t SegIndex = Entry.segmentIndex();
2810 uint64_t OffsetInSeg = Entry.segmentOffset();
2811 StringRef SegmentName = sectionTable.segmentName(SegIndex);
2812 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
2813 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
2815 // Table lines look like:
2816 // __DATA __data 0x00001000 pointer 0 _foo
2817 outs() << format("%-8s %-18s 0x%08" PRIX64 " %-8s %-8" PRId64 " ",
2818 SegmentName.str().c_str(),
2819 SectionName.str().c_str(),
2821 Entry.typeName().str().c_str(),
2823 << Entry.symbolName() << "\n";