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/TargetRegistry.h"
40 #include "llvm/Support/TargetSelect.h"
41 #include "llvm/Support/raw_ostream.h"
44 #include <system_error>
46 using namespace object;
49 UseDbg("g", cl::desc("Print line information from debug info if available"));
51 static cl::opt<std::string>
52 DSYMFile("dsym", cl::desc("Use .dSYM file for debug info"));
54 static std::string ThumbTripleName;
56 static const Target *GetTarget(const MachOObjectFile *MachOObj,
57 const char **McpuDefault,
58 const Target **ThumbTarget) {
59 // Figure out the target triple.
60 if (TripleName.empty()) {
61 llvm::Triple TT("unknown-unknown-unknown");
62 llvm::Triple ThumbTriple = Triple();
63 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
64 TripleName = TT.str();
65 ThumbTripleName = ThumbTriple.str();
68 // Get the target specific parser.
70 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
71 if (TheTarget && ThumbTripleName.empty())
74 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
78 errs() << "llvm-objdump: error: unable to get target for '";
82 errs() << ThumbTripleName;
83 errs() << "', see --version and --triple.\n";
88 bool operator()(const SymbolRef &A, const SymbolRef &B) {
89 SymbolRef::Type AType, BType;
93 uint64_t AAddr, BAddr;
94 if (AType != SymbolRef::ST_Function)
98 if (BType != SymbolRef::ST_Function)
102 return AAddr < BAddr;
106 // Types for the storted data in code table that is built before disassembly
107 // and the predicate function to sort them.
108 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
109 typedef std::vector<DiceTableEntry> DiceTable;
110 typedef DiceTable::iterator dice_table_iterator;
113 compareDiceTableEntries(const DiceTableEntry i,
114 const DiceTableEntry j) {
115 return i.first == j.first;
118 static void DumpDataInCode(const char *bytes, uint64_t Size,
119 unsigned short Kind) {
123 case MachO::DICE_KIND_DATA:
126 Value = bytes[3] << 24 |
130 outs() << "\t.long " << Value;
133 Value = bytes[1] << 8 |
135 outs() << "\t.short " << Value;
139 outs() << "\t.byte " << Value;
142 outs() << "\t@ KIND_DATA\n";
144 case MachO::DICE_KIND_JUMP_TABLE8:
146 outs() << "\t.byte " << Value << "\t@ KIND_JUMP_TABLE8";
148 case MachO::DICE_KIND_JUMP_TABLE16:
149 Value = bytes[1] << 8 |
151 outs() << "\t.short " << Value << "\t@ KIND_JUMP_TABLE16";
153 case MachO::DICE_KIND_JUMP_TABLE32:
154 Value = bytes[3] << 24 |
158 outs() << "\t.long " << Value << "\t@ KIND_JUMP_TABLE32";
161 outs() << "\t@ data in code kind = " << Kind << "\n";
166 static void getSectionsAndSymbols(const MachO::mach_header Header,
167 MachOObjectFile *MachOObj,
168 std::vector<SectionRef> &Sections,
169 std::vector<SymbolRef> &Symbols,
170 SmallVectorImpl<uint64_t> &FoundFns,
171 uint64_t &BaseSegmentAddress) {
172 for (const SymbolRef &Symbol : MachOObj->symbols())
173 Symbols.push_back(Symbol);
175 for (const SectionRef &Section : MachOObj->sections()) {
177 Section.getName(SectName);
178 Sections.push_back(Section);
181 MachOObjectFile::LoadCommandInfo Command =
182 MachOObj->getFirstLoadCommandInfo();
183 bool BaseSegmentAddressSet = false;
184 for (unsigned i = 0; ; ++i) {
185 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
186 // We found a function starts segment, parse the addresses for later
188 MachO::linkedit_data_command LLC =
189 MachOObj->getLinkeditDataLoadCommand(Command);
191 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
193 else if (Command.C.cmd == MachO::LC_SEGMENT) {
194 MachO::segment_command SLC =
195 MachOObj->getSegmentLoadCommand(Command);
196 StringRef SegName = SLC.segname;
197 if(!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
198 BaseSegmentAddressSet = true;
199 BaseSegmentAddress = SLC.vmaddr;
203 if (i == Header.ncmds - 1)
206 Command = MachOObj->getNextLoadCommandInfo(Command);
210 static void DisassembleInputMachO2(StringRef Filename,
211 MachOObjectFile *MachOOF);
213 void llvm::DisassembleInputMachO(StringRef Filename) {
214 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
215 MemoryBuffer::getFileOrSTDIN(Filename);
216 if (std::error_code EC = BuffOrErr.getError()) {
217 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << "\n";
220 std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
222 std::unique_ptr<MachOObjectFile> MachOOF = std::move(
223 ObjectFile::createMachOObjectFile(Buff.get()->getMemBufferRef()).get());
225 DisassembleInputMachO2(Filename, MachOOF.get());
228 // The block of info used by the Symbolizer call backs.
229 struct DisassembleInfo {
235 // SymbolizerGetOpInfo() is the operand information call back function.
236 // This is called to get the symbolic information for operand(s) of an
237 // instruction when it is being done. This routine does this from
238 // the relocation information, symbol table, etc. That block of information
239 // is a pointer to the struct DisassembleInfo that was passed when the
240 // disassembler context was created and passed to back to here when
241 // called back by the disassembler for instruction operands that could have
242 // relocation information. The address of the instruction containing operand is
243 // at the Pc parameter. The immediate value the operand has is passed in
244 // op_info->Value and is at Offset past the start of the instruction and has a
245 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
246 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
247 // names and addends of the symbolic expression to add for the operand. The
248 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
249 // information is returned then this function returns 1 else it returns 0.
250 int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
251 uint64_t Size, int TagType, void *TagBuf) {
252 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
253 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
254 unsigned int value = op_info->Value;
256 // Make sure all fields returned are zero if we don't set them.
257 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
258 op_info->Value = value;
260 // If the TagType is not the value 1 which it code knows about or if no
261 // verbose symbolic information is wanted then just return 0, indicating no
262 // information is being returned.
263 if (TagType != 1 || info->verbose == false)
266 unsigned int Arch = info->O->getArch();
267 if (Arch == Triple::x86) {
269 } else if (Arch == Triple::x86_64) {
270 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
272 // First search the section's relocation entries (if any) for an entry
273 // for this section offset.
275 info->S.getAddress(sect_addr);
276 uint64_t sect_offset = (Pc + Offset) - sect_addr;
277 bool reloc_found = false;
279 MachO::any_relocation_info RE;
280 bool isExtern = false;
282 for (const RelocationRef &Reloc : info->S.relocations()) {
283 uint64_t RelocOffset;
284 Reloc.getOffset(RelocOffset);
285 if (RelocOffset == sect_offset) {
286 Rel = Reloc.getRawDataRefImpl();
287 RE = info->O->getRelocation(Rel);
288 // NOTE: Scattered relocations don't exist on x86_64.
289 isExtern = info->O->getPlainRelocationExternal(RE);
291 symbol_iterator RelocSym = Reloc.getSymbol();
298 if (reloc_found && isExtern) {
299 // The Value passed in will be adjusted by the Pc if the instruction
300 // adds the Pc. But for x86_64 external relocation entries the Value
301 // is the offset from the external symbol.
302 if (info->O->getAnyRelocationPCRel(RE))
303 op_info->Value -= Pc + Offset + Size;
304 // SymbolRef Symbol = (*info->Relocs)[Idx].second;
306 Symbol.getName(SymName);
307 const char *name = SymName.data();
308 unsigned Type = info->O->getAnyRelocationType(RE);
309 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
310 DataRefImpl RelNext = Rel;
311 info->O->moveRelocationNext(RelNext);
312 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
313 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
314 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
315 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
316 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
317 op_info->SubtractSymbol.Present = 1;
318 op_info->SubtractSymbol.Name = name;
319 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
320 Symbol = *RelocSymNext;
321 StringRef SymNameNext;
322 Symbol.getName(SymNameNext);
323 name = SymNameNext.data();
326 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
327 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
328 op_info->AddSymbol.Present = 1;
329 op_info->AddSymbol.Name = name;
333 // Second search the external relocation entries of a fully linked image
334 // (if any) for an entry that matches this segment offset.
335 //uint64_t seg_offset = (Pc + Offset);
337 } else if (Arch == Triple::arm) {
339 } else if (Arch == Triple::aarch64) {
346 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
347 // the Symbolizer. It looks up the SymbolValue using the info passed via the
348 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
349 // is created and returns the symbol name that matches the ReferenceValue or
350 // nullptr if none. The ReferenceType is passed in for the IN type of
351 // reference the instruction is making from the values in defined in the header
352 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
353 // Out type and the ReferenceName will also be set which is added as a comment
354 // to the disassembled instruction.
356 // If the symbol name is a C++ mangled name then the demangled name is
357 // returned through ReferenceName and ReferenceType is set to
358 // LLVMDisassembler_ReferenceType_DeMangled_Name .
360 // When this is called to get a symbol name for a branch target then the
361 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
362 // SymbolValue will be looked for in the indirect symbol table to determine if
363 // it is an address for a symbol stub. If so then the symbol name for that
364 // stub is returned indirectly through ReferenceName and then ReferenceType is
365 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
367 // When this is called with an value loaded via a PC relative load then
368 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
369 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
370 // or an Objective-C meta data reference. If so the output ReferenceType is
371 // set to correspond to that as well as ReferenceName.
372 const char *SymbolizerSymbolLookUp(void *DisInfo, uint64_t ReferenceValue,
373 uint64_t *ReferenceType,
374 uint64_t ReferencePC,
375 const char **ReferenceName) {
376 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
377 *ReferenceName = nullptr;
378 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
379 unsigned int Arch = info->O->getArch();
380 if (Arch == Triple::x86) {
382 } else if (Arch == Triple::x86_64) {
384 } else if (Arch == Triple::arm) {
386 } else if (Arch == Triple::aarch64) {
393 static void DisassembleInputMachO2(StringRef Filename,
394 MachOObjectFile *MachOOF) {
395 const char *McpuDefault = nullptr;
396 const Target *ThumbTarget = nullptr;
397 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
399 // GetTarget prints out stuff.
402 if (MCPU.empty() && McpuDefault)
405 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
406 std::unique_ptr<MCInstrAnalysis> InstrAnalysis(
407 TheTarget->createMCInstrAnalysis(InstrInfo.get()));
408 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
409 std::unique_ptr<MCInstrAnalysis> ThumbInstrAnalysis;
411 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
412 ThumbInstrAnalysis.reset(
413 ThumbTarget->createMCInstrAnalysis(ThumbInstrInfo.get()));
416 // Package up features to be passed to target/subtarget
417 std::string FeaturesStr;
419 SubtargetFeatures Features;
420 for (unsigned i = 0; i != MAttrs.size(); ++i)
421 Features.AddFeature(MAttrs[i]);
422 FeaturesStr = Features.getString();
425 // Set up disassembler.
426 std::unique_ptr<const MCRegisterInfo> MRI(
427 TheTarget->createMCRegInfo(TripleName));
428 std::unique_ptr<const MCAsmInfo> AsmInfo(
429 TheTarget->createMCAsmInfo(*MRI, TripleName));
430 std::unique_ptr<const MCSubtargetInfo> STI(
431 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
432 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
433 std::unique_ptr<MCDisassembler> DisAsm(
434 TheTarget->createMCDisassembler(*STI, Ctx));
435 std::unique_ptr<MCSymbolizer> Symbolizer;
436 struct DisassembleInfo SymbolizerInfo;
437 std::unique_ptr<MCRelocationInfo> RelInfo(
438 TheTarget->createMCRelocationInfo(TripleName, Ctx));
440 Symbolizer.reset(TheTarget->createMCSymbolizer(
441 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
442 &SymbolizerInfo, &Ctx, RelInfo.release()));
443 DisAsm->setSymbolizer(std::move(Symbolizer));
445 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
446 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
447 AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI, *STI));
449 if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) {
450 errs() << "error: couldn't initialize disassembler for target "
451 << TripleName << '\n';
455 // Set up thumb disassembler.
456 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
457 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
458 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
459 std::unique_ptr<const MCDisassembler> ThumbDisAsm;
460 std::unique_ptr<MCInstPrinter> ThumbIP;
461 std::unique_ptr<MCContext> ThumbCtx;
463 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
465 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
467 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
468 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
469 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
470 // TODO: add MCSymbolizer here for the ThumbTarget like above for TheTarget.
471 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
472 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
473 ThumbAsmPrinterVariant, *ThumbAsmInfo, *ThumbInstrInfo, *ThumbMRI,
477 if (ThumbTarget && (!ThumbInstrAnalysis || !ThumbAsmInfo || !ThumbSTI ||
478 !ThumbDisAsm || !ThumbIP)) {
479 errs() << "error: couldn't initialize disassembler for target "
480 << ThumbTripleName << '\n';
484 outs() << '\n' << Filename << ":\n\n";
486 MachO::mach_header Header = MachOOF->getHeader();
488 // FIXME: Using the -cfg command line option, this code used to be able to
489 // annotate relocations with the referenced symbol's name, and if this was
490 // inside a __[cf]string section, the data it points to. This is now replaced
491 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
492 std::vector<SectionRef> Sections;
493 std::vector<SymbolRef> Symbols;
494 SmallVector<uint64_t, 8> FoundFns;
495 uint64_t BaseSegmentAddress;
497 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
500 // Sort the symbols by address, just in case they didn't come in that way.
501 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
503 // Build a data in code table that is sorted on by the address of each entry.
504 uint64_t BaseAddress = 0;
505 if (Header.filetype == MachO::MH_OBJECT)
506 Sections[0].getAddress(BaseAddress);
508 BaseAddress = BaseSegmentAddress;
510 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
513 DI->getOffset(Offset);
514 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
516 array_pod_sort(Dices.begin(), Dices.end());
519 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
521 raw_ostream &DebugOut = nulls();
524 std::unique_ptr<DIContext> diContext;
525 ObjectFile *DbgObj = MachOOF;
526 // Try to find debug info and set up the DIContext for it.
528 // A separate DSym file path was specified, parse it as a macho file,
529 // get the sections and supply it to the section name parsing machinery.
530 if (!DSYMFile.empty()) {
531 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
532 MemoryBuffer::getFileOrSTDIN(DSYMFile);
533 if (std::error_code EC = BufOrErr.getError()) {
534 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
538 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
543 // Setup the DIContext
544 diContext.reset(DIContext::getDWARFContext(*DbgObj));
547 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
549 bool SectIsText = false;
550 Sections[SectIdx].isText(SectIsText);
551 if (SectIsText == false)
555 if (Sections[SectIdx].getName(SectName) ||
556 SectName != "__text")
557 continue; // Skip non-text sections
559 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
561 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
562 if (SegmentName != "__TEXT")
566 Sections[SectIdx].getContents(Bytes);
567 StringRefMemoryObject memoryObject(Bytes);
568 bool symbolTableWorked = false;
570 // Parse relocations.
571 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
572 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
573 uint64_t RelocOffset, SectionAddress;
574 Reloc.getOffset(RelocOffset);
575 Sections[SectIdx].getAddress(SectionAddress);
576 RelocOffset -= SectionAddress;
578 symbol_iterator RelocSym = Reloc.getSymbol();
580 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
582 array_pod_sort(Relocs.begin(), Relocs.end());
584 // Set up the block of info used by the Symbolizer call backs.
585 SymbolizerInfo.verbose = true;
586 SymbolizerInfo.O = MachOOF;
587 SymbolizerInfo.S = Sections[SectIdx];
589 // Disassemble symbol by symbol.
590 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
592 Symbols[SymIdx].getName(SymName);
595 Symbols[SymIdx].getType(ST);
596 if (ST != SymbolRef::ST_Function)
599 // Make sure the symbol is defined in this section.
600 bool containsSym = false;
601 Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym);
605 // Start at the address of the symbol relative to the section's address.
606 uint64_t SectionAddress = 0;
608 Sections[SectIdx].getAddress(SectionAddress);
609 Symbols[SymIdx].getAddress(Start);
610 Start -= SectionAddress;
612 // Stop disassembling either at the beginning of the next symbol or at
613 // the end of the section.
614 bool containsNextSym = false;
615 uint64_t NextSym = 0;
616 uint64_t NextSymIdx = SymIdx+1;
617 while (Symbols.size() > NextSymIdx) {
618 SymbolRef::Type NextSymType;
619 Symbols[NextSymIdx].getType(NextSymType);
620 if (NextSymType == SymbolRef::ST_Function) {
621 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx],
623 Symbols[NextSymIdx].getAddress(NextSym);
624 NextSym -= SectionAddress;
631 Sections[SectIdx].getSize(SectSize);
632 uint64_t End = containsNextSym ? NextSym : SectSize;
635 symbolTableWorked = true;
637 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
639 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
641 outs() << SymName << ":\n";
643 for (uint64_t Index = Start; Index < End; Index += Size) {
646 uint64_t SectAddress = 0;
647 Sections[SectIdx].getAddress(SectAddress);
648 outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
650 // Check the data in code table here to see if this is data not an
651 // instruction to be disassembled.
653 Dice.push_back(std::make_pair(SectAddress + Index, DiceRef()));
654 dice_table_iterator DTI = std::search(Dices.begin(), Dices.end(),
655 Dice.begin(), Dice.end(),
656 compareDiceTableEntries);
657 if (DTI != Dices.end()){
659 DTI->second.getLength(Length);
660 DumpBytes(StringRef(Bytes.data() + Index, Length));
662 DTI->second.getKind(Kind);
663 DumpDataInCode(Bytes.data() + Index, Length, Kind);
669 gotInst = ThumbDisAsm->getInstruction(Inst, Size, memoryObject, Index,
672 gotInst = DisAsm->getInstruction(Inst, Size, memoryObject, Index,
675 DumpBytes(StringRef(Bytes.data() + Index, Size));
677 ThumbIP->printInst(&Inst, outs(), "");
679 IP->printInst(&Inst, outs(), "");
684 diContext->getLineInfoForAddress(SectAddress + Index);
685 // Print valid line info if it changed.
686 if (dli != lastLine && dli.Line != 0)
687 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
693 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
695 Size = 1; // skip illegible bytes
699 if (!symbolTableWorked) {
700 // Reading the symbol table didn't work, disassemble the whole section.
701 uint64_t SectAddress;
702 Sections[SectIdx].getAddress(SectAddress);
704 Sections[SectIdx].getSize(SectSize);
706 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
709 if (DisAsm->getInstruction(Inst, InstSize, memoryObject, Index,
710 DebugOut, nulls())) {
711 outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
712 DumpBytes(StringRef(Bytes.data() + Index, InstSize));
713 IP->printInst(&Inst, outs(), "");
716 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
718 InstSize = 1; // skip illegible bytes
726 //===----------------------------------------------------------------------===//
727 // __compact_unwind section dumping
728 //===----------------------------------------------------------------------===//
732 template <typename T> static uint64_t readNext(const char *&Buf) {
733 using llvm::support::little;
734 using llvm::support::unaligned;
736 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
741 struct CompactUnwindEntry {
742 uint32_t OffsetInSection;
744 uint64_t FunctionAddr;
746 uint32_t CompactEncoding;
747 uint64_t PersonalityAddr;
750 RelocationRef FunctionReloc;
751 RelocationRef PersonalityReloc;
752 RelocationRef LSDAReloc;
754 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
755 : OffsetInSection(Offset) {
757 read<uint64_t>(Contents.data() + Offset);
759 read<uint32_t>(Contents.data() + Offset);
763 template<typename UIntPtr>
764 void read(const char *Buf) {
765 FunctionAddr = readNext<UIntPtr>(Buf);
766 Length = readNext<uint32_t>(Buf);
767 CompactEncoding = readNext<uint32_t>(Buf);
768 PersonalityAddr = readNext<UIntPtr>(Buf);
769 LSDAAddr = readNext<UIntPtr>(Buf);
774 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
775 /// and data being relocated, determine the best base Name and Addend to use for
776 /// display purposes.
778 /// 1. An Extern relocation will directly reference a symbol (and the data is
779 /// then already an addend), so use that.
780 /// 2. Otherwise the data is an offset in the object file's layout; try to find
781 // a symbol before it in the same section, and use the offset from there.
782 /// 3. Finally, if all that fails, fall back to an offset from the start of the
783 /// referenced section.
784 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
785 std::map<uint64_t, SymbolRef> &Symbols,
786 const RelocationRef &Reloc,
788 StringRef &Name, uint64_t &Addend) {
789 if (Reloc.getSymbol() != Obj->symbol_end()) {
790 Reloc.getSymbol()->getName(Name);
795 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
796 SectionRef RelocSection = Obj->getRelocationSection(RE);
798 uint64_t SectionAddr;
799 RelocSection.getAddress(SectionAddr);
801 auto Sym = Symbols.upper_bound(Addr);
802 if (Sym == Symbols.begin()) {
803 // The first symbol in the object is after this reference, the best we can
804 // do is section-relative notation.
805 RelocSection.getName(Name);
806 Addend = Addr - SectionAddr;
810 // Go back one so that SymbolAddress <= Addr.
813 section_iterator SymSection = Obj->section_end();
814 Sym->second.getSection(SymSection);
815 if (RelocSection == *SymSection) {
816 // There's a valid symbol in the same section before this reference.
817 Sym->second.getName(Name);
818 Addend = Addr - Sym->first;
822 // There is a symbol before this reference, but it's in a different
823 // section. Probably not helpful to mention it, so use the section name.
824 RelocSection.getName(Name);
825 Addend = Addr - SectionAddr;
828 static void printUnwindRelocDest(const MachOObjectFile *Obj,
829 std::map<uint64_t, SymbolRef> &Symbols,
830 const RelocationRef &Reloc,
835 if (!Reloc.getObjectFile())
838 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
842 outs() << " + " << format("0x%" PRIx64, Addend);
846 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
847 std::map<uint64_t, SymbolRef> &Symbols,
848 const SectionRef &CompactUnwind) {
850 assert(Obj->isLittleEndian() &&
851 "There should not be a big-endian .o with __compact_unwind");
853 bool Is64 = Obj->is64Bit();
854 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
855 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
858 CompactUnwind.getContents(Contents);
860 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
862 // First populate the initial raw offsets, encodings and so on from the entry.
863 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
864 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
865 CompactUnwinds.push_back(Entry);
868 // Next we need to look at the relocations to find out what objects are
869 // actually being referred to.
870 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
871 uint64_t RelocAddress;
872 Reloc.getOffset(RelocAddress);
874 uint32_t EntryIdx = RelocAddress / EntrySize;
875 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
876 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
878 if (OffsetInEntry == 0)
879 Entry.FunctionReloc = Reloc;
880 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
881 Entry.PersonalityReloc = Reloc;
882 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
883 Entry.LSDAReloc = Reloc;
885 llvm_unreachable("Unexpected relocation in __compact_unwind section");
888 // Finally, we're ready to print the data we've gathered.
889 outs() << "Contents of __compact_unwind section:\n";
890 for (auto &Entry : CompactUnwinds) {
891 outs() << " Entry at offset "
892 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
894 // 1. Start of the region this entry applies to.
896 << format("0x%" PRIx64, Entry.FunctionAddr) << ' ';
897 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc,
901 // 2. Length of the region this entry applies to.
902 outs() << " length: "
903 << format("0x%" PRIx32, Entry.Length) << '\n';
904 // 3. The 32-bit compact encoding.
905 outs() << " compact encoding: "
906 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
908 // 4. The personality function, if present.
909 if (Entry.PersonalityReloc.getObjectFile()) {
910 outs() << " personality function: "
911 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
912 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
913 Entry.PersonalityAddr);
917 // 5. This entry's language-specific data area.
918 if (Entry.LSDAReloc.getObjectFile()) {
920 << format("0x%" PRIx64, Entry.LSDAAddr) << ' ';
921 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
927 //===----------------------------------------------------------------------===//
928 // __unwind_info section dumping
929 //===----------------------------------------------------------------------===//
931 static void printRegularSecondLevelUnwindPage(const char *PageStart) {
932 const char *Pos = PageStart;
933 uint32_t Kind = readNext<uint32_t>(Pos);
935 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
937 uint16_t EntriesStart = readNext<uint16_t>(Pos);
938 uint16_t NumEntries = readNext<uint16_t>(Pos);
940 Pos = PageStart + EntriesStart;
941 for (unsigned i = 0; i < NumEntries; ++i) {
942 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
943 uint32_t Encoding = readNext<uint32_t>(Pos);
945 outs() << " [" << i << "]: "
946 << "function offset="
947 << format("0x%08" PRIx32, FunctionOffset) << ", "
949 << format("0x%08" PRIx32, Encoding)
954 static void printCompressedSecondLevelUnwindPage(
955 const char *PageStart, uint32_t FunctionBase,
956 const SmallVectorImpl<uint32_t> &CommonEncodings) {
957 const char *Pos = PageStart;
958 uint32_t Kind = readNext<uint32_t>(Pos);
960 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
962 uint16_t EntriesStart = readNext<uint16_t>(Pos);
963 uint16_t NumEntries = readNext<uint16_t>(Pos);
965 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
966 readNext<uint16_t>(Pos);
967 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
968 PageStart + EncodingsStart);
970 Pos = PageStart + EntriesStart;
971 for (unsigned i = 0; i < NumEntries; ++i) {
972 uint32_t Entry = readNext<uint32_t>(Pos);
973 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
974 uint32_t EncodingIdx = Entry >> 24;
977 if (EncodingIdx < CommonEncodings.size())
978 Encoding = CommonEncodings[EncodingIdx];
980 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
982 outs() << " [" << i << "]: "
983 << "function offset="
984 << format("0x%08" PRIx32, FunctionOffset) << ", "
985 << "encoding[" << EncodingIdx << "]="
986 << format("0x%08" PRIx32, Encoding)
992 printMachOUnwindInfoSection(const MachOObjectFile *Obj,
993 std::map<uint64_t, SymbolRef> &Symbols,
994 const SectionRef &UnwindInfo) {
996 assert(Obj->isLittleEndian() &&
997 "There should not be a big-endian .o with __unwind_info");
999 outs() << "Contents of __unwind_info section:\n";
1002 UnwindInfo.getContents(Contents);
1003 const char *Pos = Contents.data();
1005 //===----------------------------------
1007 //===----------------------------------
1009 uint32_t Version = readNext<uint32_t>(Pos);
1010 outs() << " Version: "
1011 << format("0x%" PRIx32, Version) << '\n';
1012 assert(Version == 1 && "only understand version 1");
1014 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
1015 outs() << " Common encodings array section offset: "
1016 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
1017 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
1018 outs() << " Number of common encodings in array: "
1019 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
1021 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
1022 outs() << " Personality function array section offset: "
1023 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
1024 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
1025 outs() << " Number of personality functions in array: "
1026 << format("0x%" PRIx32, NumPersonalities) << '\n';
1028 uint32_t IndicesStart = readNext<uint32_t>(Pos);
1029 outs() << " Index array section offset: "
1030 << format("0x%" PRIx32, IndicesStart) << '\n';
1031 uint32_t NumIndices = readNext<uint32_t>(Pos);
1032 outs() << " Number of indices in array: "
1033 << format("0x%" PRIx32, NumIndices) << '\n';
1035 //===----------------------------------
1036 // A shared list of common encodings
1037 //===----------------------------------
1039 // These occupy indices in the range [0, N] whenever an encoding is referenced
1040 // from a compressed 2nd level index table. In practice the linker only
1041 // creates ~128 of these, so that indices are available to embed encodings in
1042 // the 2nd level index.
1044 SmallVector<uint32_t, 64> CommonEncodings;
1045 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
1046 Pos = Contents.data() + CommonEncodingsStart;
1047 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
1048 uint32_t Encoding = readNext<uint32_t>(Pos);
1049 CommonEncodings.push_back(Encoding);
1051 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
1056 //===----------------------------------
1057 // Personality functions used in this executable
1058 //===----------------------------------
1060 // There should be only a handful of these (one per source language,
1061 // roughly). Particularly since they only get 2 bits in the compact encoding.
1063 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
1064 Pos = Contents.data() + PersonalitiesStart;
1065 for (unsigned i = 0; i < NumPersonalities; ++i) {
1066 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
1067 outs() << " personality[" << i + 1
1068 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
1071 //===----------------------------------
1072 // The level 1 index entries
1073 //===----------------------------------
1075 // These specify an approximate place to start searching for the more detailed
1076 // information, sorted by PC.
1079 uint32_t FunctionOffset;
1080 uint32_t SecondLevelPageStart;
1084 SmallVector<IndexEntry, 4> IndexEntries;
1086 outs() << " Top level indices: (count = " << NumIndices << ")\n";
1087 Pos = Contents.data() + IndicesStart;
1088 for (unsigned i = 0; i < NumIndices; ++i) {
1091 Entry.FunctionOffset = readNext<uint32_t>(Pos);
1092 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
1093 Entry.LSDAStart = readNext<uint32_t>(Pos);
1094 IndexEntries.push_back(Entry);
1096 outs() << " [" << i << "]: "
1097 << "function offset="
1098 << format("0x%08" PRIx32, Entry.FunctionOffset) << ", "
1099 << "2nd level page offset="
1100 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
1102 << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
1106 //===----------------------------------
1107 // Next come the LSDA tables
1108 //===----------------------------------
1110 // The LSDA layout is rather implicit: it's a contiguous array of entries from
1111 // the first top-level index's LSDAOffset to the last (sentinel).
1113 outs() << " LSDA descriptors:\n";
1114 Pos = Contents.data() + IndexEntries[0].LSDAStart;
1115 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
1116 (2 * sizeof(uint32_t));
1117 for (int i = 0; i < NumLSDAs; ++i) {
1118 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
1119 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
1120 outs() << " [" << i << "]: "
1121 << "function offset="
1122 << format("0x%08" PRIx32, FunctionOffset) << ", "
1124 << format("0x%08" PRIx32, LSDAOffset) << '\n';
1127 //===----------------------------------
1128 // Finally, the 2nd level indices
1129 //===----------------------------------
1131 // Generally these are 4K in size, and have 2 possible forms:
1132 // + Regular stores up to 511 entries with disparate encodings
1133 // + Compressed stores up to 1021 entries if few enough compact encoding
1135 outs() << " Second level indices:\n";
1136 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
1137 // The final sentinel top-level index has no associated 2nd level page
1138 if (IndexEntries[i].SecondLevelPageStart == 0)
1141 outs() << " Second level index[" << i << "]: "
1142 << "offset in section="
1143 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
1145 << "base function offset="
1146 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
1148 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
1149 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
1151 printRegularSecondLevelUnwindPage(Pos);
1153 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
1156 llvm_unreachable("Do not know how to print this kind of 2nd level page");
1161 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
1162 std::map<uint64_t, SymbolRef> Symbols;
1163 for (const SymbolRef &SymRef : Obj->symbols()) {
1164 // Discard any undefined or absolute symbols. They're not going to take part
1165 // in the convenience lookup for unwind info and just take up resources.
1166 section_iterator Section = Obj->section_end();
1167 SymRef.getSection(Section);
1168 if (Section == Obj->section_end())
1172 SymRef.getAddress(Addr);
1173 Symbols.insert(std::make_pair(Addr, SymRef));
1176 for (const SectionRef &Section : Obj->sections()) {
1178 Section.getName(SectName);
1179 if (SectName == "__compact_unwind")
1180 printMachOCompactUnwindSection(Obj, Symbols, Section);
1181 else if (SectName == "__unwind_info")
1182 printMachOUnwindInfoSection(Obj, Symbols, Section);
1183 else if (SectName == "__eh_frame")
1184 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
1189 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
1190 uint32_t cpusubtype, uint32_t filetype,
1191 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
1193 outs() << "Mach header\n";
1194 outs() << " magic cputype cpusubtype caps filetype ncmds "
1195 "sizeofcmds flags\n";
1197 if (magic == MachO::MH_MAGIC)
1198 outs() << " MH_MAGIC";
1199 else if (magic == MachO::MH_MAGIC_64)
1200 outs() << "MH_MAGIC_64";
1202 outs() << format(" 0x%08" PRIx32, magic);
1204 case MachO::CPU_TYPE_I386:
1206 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1207 case MachO::CPU_SUBTYPE_I386_ALL:
1211 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1215 case MachO::CPU_TYPE_X86_64:
1216 outs() << " X86_64";
1217 case MachO::CPU_SUBTYPE_X86_64_ALL:
1220 case MachO::CPU_SUBTYPE_X86_64_H:
1221 outs() << " Haswell";
1222 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1224 case MachO::CPU_TYPE_ARM:
1226 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1227 case MachO::CPU_SUBTYPE_ARM_ALL:
1230 case MachO::CPU_SUBTYPE_ARM_V4T:
1233 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1236 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1237 outs() << " XSCALE";
1239 case MachO::CPU_SUBTYPE_ARM_V6:
1242 case MachO::CPU_SUBTYPE_ARM_V6M:
1245 case MachO::CPU_SUBTYPE_ARM_V7:
1248 case MachO::CPU_SUBTYPE_ARM_V7EM:
1251 case MachO::CPU_SUBTYPE_ARM_V7K:
1254 case MachO::CPU_SUBTYPE_ARM_V7M:
1257 case MachO::CPU_SUBTYPE_ARM_V7S:
1261 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1265 case MachO::CPU_TYPE_ARM64:
1267 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1268 case MachO::CPU_SUBTYPE_ARM64_ALL:
1272 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1276 case MachO::CPU_TYPE_POWERPC:
1278 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1279 case MachO::CPU_SUBTYPE_POWERPC_ALL:
1283 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1287 case MachO::CPU_TYPE_POWERPC64:
1289 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
1290 case MachO::CPU_SUBTYPE_POWERPC_ALL:
1294 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1299 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
1302 outs() << format(" 0x%02" PRIx32,
1303 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
1306 case MachO::MH_OBJECT:
1307 outs() << " OBJECT";
1309 case MachO::MH_EXECUTE:
1310 outs() << " EXECUTE";
1312 case MachO::MH_FVMLIB:
1313 outs() << " FVMLIB";
1315 case MachO::MH_CORE:
1318 case MachO::MH_PRELOAD:
1319 outs() << " PRELOAD";
1321 case MachO::MH_DYLIB:
1324 case MachO::MH_DYLIB_STUB:
1325 outs() << " DYLIB_STUB";
1327 case MachO::MH_DYLINKER:
1328 outs() << " DYLINKER";
1330 case MachO::MH_BUNDLE:
1331 outs() << " BUNDLE";
1333 case MachO::MH_DSYM:
1336 case MachO::MH_KEXT_BUNDLE:
1337 outs() << " KEXTBUNDLE";
1340 outs() << format(" %10u", filetype);
1343 outs() << format(" %5u", ncmds);
1344 outs() << format(" %10u", sizeofcmds);
1346 if (f & MachO::MH_NOUNDEFS) {
1347 outs() << " NOUNDEFS";
1348 f &= ~MachO::MH_NOUNDEFS;
1350 if (f & MachO::MH_INCRLINK) {
1351 outs() << " INCRLINK";
1352 f &= ~MachO::MH_INCRLINK;
1354 if (f & MachO::MH_DYLDLINK) {
1355 outs() << " DYLDLINK";
1356 f &= ~MachO::MH_DYLDLINK;
1358 if (f & MachO::MH_BINDATLOAD) {
1359 outs() << " BINDATLOAD";
1360 f &= ~MachO::MH_BINDATLOAD;
1362 if (f & MachO::MH_PREBOUND) {
1363 outs() << " PREBOUND";
1364 f &= ~MachO::MH_PREBOUND;
1366 if (f & MachO::MH_SPLIT_SEGS) {
1367 outs() << " SPLIT_SEGS";
1368 f &= ~MachO::MH_SPLIT_SEGS;
1370 if (f & MachO::MH_LAZY_INIT) {
1371 outs() << " LAZY_INIT";
1372 f &= ~MachO::MH_LAZY_INIT;
1374 if (f & MachO::MH_TWOLEVEL) {
1375 outs() << " TWOLEVEL";
1376 f &= ~MachO::MH_TWOLEVEL;
1378 if (f & MachO::MH_FORCE_FLAT) {
1379 outs() << " FORCE_FLAT";
1380 f &= ~MachO::MH_FORCE_FLAT;
1382 if (f & MachO::MH_NOMULTIDEFS) {
1383 outs() << " NOMULTIDEFS";
1384 f &= ~MachO::MH_NOMULTIDEFS;
1386 if (f & MachO::MH_NOFIXPREBINDING) {
1387 outs() << " NOFIXPREBINDING";
1388 f &= ~MachO::MH_NOFIXPREBINDING;
1390 if (f & MachO::MH_PREBINDABLE) {
1391 outs() << " PREBINDABLE";
1392 f &= ~MachO::MH_PREBINDABLE;
1394 if (f & MachO::MH_ALLMODSBOUND) {
1395 outs() << " ALLMODSBOUND";
1396 f &= ~MachO::MH_ALLMODSBOUND;
1398 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
1399 outs() << " SUBSECTIONS_VIA_SYMBOLS";
1400 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
1402 if (f & MachO::MH_CANONICAL) {
1403 outs() << " CANONICAL";
1404 f &= ~MachO::MH_CANONICAL;
1406 if (f & MachO::MH_WEAK_DEFINES) {
1407 outs() << " WEAK_DEFINES";
1408 f &= ~MachO::MH_WEAK_DEFINES;
1410 if (f & MachO::MH_BINDS_TO_WEAK) {
1411 outs() << " BINDS_TO_WEAK";
1412 f &= ~MachO::MH_BINDS_TO_WEAK;
1414 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
1415 outs() << " ALLOW_STACK_EXECUTION";
1416 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
1418 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
1419 outs() << " DEAD_STRIPPABLE_DYLIB";
1420 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
1422 if (f & MachO::MH_PIE) {
1424 f &= ~MachO::MH_PIE;
1426 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
1427 outs() << " NO_REEXPORTED_DYLIBS";
1428 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
1430 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
1431 outs() << " MH_HAS_TLV_DESCRIPTORS";
1432 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
1434 if (f & MachO::MH_NO_HEAP_EXECUTION) {
1435 outs() << " MH_NO_HEAP_EXECUTION";
1436 f &= ~MachO::MH_NO_HEAP_EXECUTION;
1438 if (f & MachO::MH_APP_EXTENSION_SAFE) {
1439 outs() << " APP_EXTENSION_SAFE";
1440 f &= ~MachO::MH_APP_EXTENSION_SAFE;
1442 if (f != 0 || flags == 0)
1443 outs() << format(" 0x%08" PRIx32, f);
1445 outs() << format(" 0x%08" PRIx32, magic);
1446 outs() << format(" %7d", cputype);
1447 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
1448 outs() << format(" 0x%02" PRIx32,
1449 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
1450 outs() << format(" %10u", filetype);
1451 outs() << format(" %5u", ncmds);
1452 outs() << format(" %10u", sizeofcmds);
1453 outs() << format(" 0x%08" PRIx32, flags);
1458 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
1459 StringRef SegName, uint64_t vmaddr,
1460 uint64_t vmsize, uint64_t fileoff,
1461 uint64_t filesize, uint32_t maxprot,
1462 uint32_t initprot, uint32_t nsects,
1463 uint32_t flags, uint32_t object_size,
1465 uint64_t expected_cmdsize;
1466 if (cmd == MachO::LC_SEGMENT) {
1467 outs() << " cmd LC_SEGMENT\n";
1468 expected_cmdsize = nsects;
1469 expected_cmdsize *= sizeof(struct MachO::section);
1470 expected_cmdsize += sizeof(struct MachO::segment_command);
1472 outs() << " cmd LC_SEGMENT_64\n";
1473 expected_cmdsize = nsects;
1474 expected_cmdsize *= sizeof(struct MachO::section_64);
1475 expected_cmdsize += sizeof(struct MachO::segment_command_64);
1477 outs() << " cmdsize " << cmdsize;
1478 if (cmdsize != expected_cmdsize)
1479 outs() << " Inconsistent size\n";
1482 outs() << " segname " << SegName << "\n";
1483 if (cmd == MachO::LC_SEGMENT_64) {
1484 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
1485 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
1487 outs() << " vmaddr " << format("0x%08" PRIx32, vmaddr) << "\n";
1488 outs() << " vmsize " << format("0x%08" PRIx32, vmsize) << "\n";
1490 outs() << " fileoff " << fileoff;
1491 if (fileoff > object_size)
1492 outs() << " (past end of file)\n";
1495 outs() << " filesize " << filesize;
1496 if (fileoff + filesize > object_size)
1497 outs() << " (past end of file)\n";
1502 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
1503 MachO::VM_PROT_EXECUTE)) != 0)
1504 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
1506 if (maxprot & MachO::VM_PROT_READ)
1507 outs() << " maxprot r";
1509 outs() << " maxprot -";
1510 if (maxprot & MachO::VM_PROT_WRITE)
1514 if (maxprot & MachO::VM_PROT_EXECUTE)
1520 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
1521 MachO::VM_PROT_EXECUTE)) != 0)
1522 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
1524 if (initprot & MachO::VM_PROT_READ)
1525 outs() << " initprot r";
1527 outs() << " initprot -";
1528 if (initprot & MachO::VM_PROT_WRITE)
1532 if (initprot & MachO::VM_PROT_EXECUTE)
1538 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
1539 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
1541 outs() << " nsects " << nsects << "\n";
1545 outs() << " (none)\n";
1547 if (flags & MachO::SG_HIGHVM) {
1548 outs() << " HIGHVM";
1549 flags &= ~MachO::SG_HIGHVM;
1551 if (flags & MachO::SG_FVMLIB) {
1552 outs() << " FVMLIB";
1553 flags &= ~MachO::SG_FVMLIB;
1555 if (flags & MachO::SG_NORELOC) {
1556 outs() << " NORELOC";
1557 flags &= ~MachO::SG_NORELOC;
1559 if (flags & MachO::SG_PROTECTED_VERSION_1) {
1560 outs() << " PROTECTED_VERSION_1";
1561 flags &= ~MachO::SG_PROTECTED_VERSION_1;
1564 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
1569 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
1573 static void PrintSection(const char *sectname, const char *segname,
1574 uint64_t addr, uint64_t size, uint32_t offset,
1575 uint32_t align, uint32_t reloff, uint32_t nreloc,
1576 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
1577 uint32_t cmd, const char *sg_segname,
1578 uint32_t filetype, uint32_t object_size,
1580 outs() << "Section\n";
1581 outs() << " sectname " << format("%.16s\n", sectname);
1582 outs() << " segname " << format("%.16s", segname);
1583 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
1584 outs() << " (does not match segment)\n";
1587 if (cmd == MachO::LC_SEGMENT_64) {
1588 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
1589 outs() << " size " << format("0x%016" PRIx64, size);
1591 outs() << " addr " << format("0x%08" PRIx32, addr) << "\n";
1592 outs() << " size " << format("0x%08" PRIx32, size);
1594 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
1595 outs() << " (past end of file)\n";
1598 outs() << " offset " << offset;
1599 if (offset > object_size)
1600 outs() << " (past end of file)\n";
1603 uint32_t align_shifted = 1 << align;
1604 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
1605 outs() << " reloff " << reloff;
1606 if (reloff > object_size)
1607 outs() << " (past end of file)\n";
1610 outs() << " nreloc " << nreloc;
1611 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
1612 outs() << " (past end of file)\n";
1615 uint32_t section_type = flags & MachO::SECTION_TYPE;
1618 if (section_type == MachO::S_REGULAR)
1619 outs() << " S_REGULAR\n";
1620 else if (section_type == MachO::S_ZEROFILL)
1621 outs() << " S_ZEROFILL\n";
1622 else if (section_type == MachO::S_CSTRING_LITERALS)
1623 outs() << " S_CSTRING_LITERALS\n";
1624 else if (section_type == MachO::S_4BYTE_LITERALS)
1625 outs() << " S_4BYTE_LITERALS\n";
1626 else if (section_type == MachO::S_8BYTE_LITERALS)
1627 outs() << " S_8BYTE_LITERALS\n";
1628 else if (section_type == MachO::S_16BYTE_LITERALS)
1629 outs() << " S_16BYTE_LITERALS\n";
1630 else if (section_type == MachO::S_LITERAL_POINTERS)
1631 outs() << " S_LITERAL_POINTERS\n";
1632 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
1633 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
1634 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
1635 outs() << " S_LAZY_SYMBOL_POINTERS\n";
1636 else if (section_type == MachO::S_SYMBOL_STUBS)
1637 outs() << " S_SYMBOL_STUBS\n";
1638 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
1639 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
1640 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
1641 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
1642 else if (section_type == MachO::S_COALESCED)
1643 outs() << " S_COALESCED\n";
1644 else if (section_type == MachO::S_INTERPOSING)
1645 outs() << " S_INTERPOSING\n";
1646 else if (section_type == MachO::S_DTRACE_DOF)
1647 outs() << " S_DTRACE_DOF\n";
1648 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
1649 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
1650 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
1651 outs() << " S_THREAD_LOCAL_REGULAR\n";
1652 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
1653 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
1654 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
1655 outs() << " S_THREAD_LOCAL_VARIABLES\n";
1656 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
1657 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
1658 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
1659 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
1661 outs() << format("0x%08" PRIx32, section_type) << "\n";
1662 outs() << "attributes";
1663 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
1664 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
1665 outs() << " PURE_INSTRUCTIONS";
1666 if (section_attributes & MachO::S_ATTR_NO_TOC)
1667 outs() << " NO_TOC";
1668 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
1669 outs() << " STRIP_STATIC_SYMS";
1670 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
1671 outs() << " NO_DEAD_STRIP";
1672 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
1673 outs() << " LIVE_SUPPORT";
1674 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
1675 outs() << " SELF_MODIFYING_CODE";
1676 if (section_attributes & MachO::S_ATTR_DEBUG)
1678 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
1679 outs() << " SOME_INSTRUCTIONS";
1680 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
1681 outs() << " EXT_RELOC";
1682 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
1683 outs() << " LOC_RELOC";
1684 if (section_attributes == 0)
1685 outs() << " (none)";
1688 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
1689 outs() << " reserved1 " << reserved1;
1690 if (section_type == MachO::S_SYMBOL_STUBS ||
1691 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
1692 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
1693 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
1694 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
1695 outs() << " (index into indirect symbol table)\n";
1698 outs() << " reserved2 " << reserved2;
1699 if (section_type == MachO::S_SYMBOL_STUBS)
1700 outs() << " (size of stubs)\n";
1705 static void PrintSymtabLoadCommand(MachO::symtab_command st, uint32_t cputype,
1706 uint32_t object_size) {
1707 outs() << " cmd LC_SYMTAB\n";
1708 outs() << " cmdsize " << st.cmdsize;
1709 if (st.cmdsize != sizeof(struct MachO::symtab_command))
1710 outs() << " Incorrect size\n";
1713 outs() << " symoff " << st.symoff;
1714 if (st.symoff > object_size)
1715 outs() << " (past end of file)\n";
1718 outs() << " nsyms " << st.nsyms;
1720 if (cputype & MachO::CPU_ARCH_ABI64) {
1721 big_size = st.nsyms;
1722 big_size *= sizeof(struct MachO::nlist_64);
1723 big_size += st.symoff;
1724 if (big_size > object_size)
1725 outs() << " (past end of file)\n";
1729 big_size = st.nsyms;
1730 big_size *= sizeof(struct MachO::nlist);
1731 big_size += st.symoff;
1732 if (big_size > object_size)
1733 outs() << " (past end of file)\n";
1737 outs() << " stroff " << st.stroff;
1738 if (st.stroff > object_size)
1739 outs() << " (past end of file)\n";
1742 outs() << " strsize " << st.strsize;
1743 big_size = st.stroff;
1744 big_size += st.strsize;
1745 if (big_size > object_size)
1746 outs() << " (past end of file)\n";
1751 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
1752 uint32_t nsyms, uint32_t object_size,
1754 outs() << " cmd LC_DYSYMTAB\n";
1755 outs() << " cmdsize " << dyst.cmdsize;
1756 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
1757 outs() << " Incorrect size\n";
1760 outs() << " ilocalsym " << dyst.ilocalsym;
1761 if (dyst.ilocalsym > nsyms)
1762 outs() << " (greater than the number of symbols)\n";
1765 outs() << " nlocalsym " << dyst.nlocalsym;
1767 big_size = dyst.ilocalsym;
1768 big_size += dyst.nlocalsym;
1769 if (big_size > nsyms)
1770 outs() << " (past the end of the symbol table)\n";
1773 outs() << " iextdefsym " << dyst.iextdefsym;
1774 if (dyst.iextdefsym > nsyms)
1775 outs() << " (greater than the number of symbols)\n";
1778 outs() << " nextdefsym " << dyst.nextdefsym;
1779 big_size = dyst.iextdefsym;
1780 big_size += dyst.nextdefsym;
1781 if (big_size > nsyms)
1782 outs() << " (past the end of the symbol table)\n";
1785 outs() << " iundefsym " << dyst.iundefsym;
1786 if (dyst.iundefsym > nsyms)
1787 outs() << " (greater than the number of symbols)\n";
1790 outs() << " nundefsym " << dyst.nundefsym;
1791 big_size = dyst.iundefsym;
1792 big_size += dyst.nundefsym;
1793 if (big_size > nsyms)
1794 outs() << " (past the end of the symbol table)\n";
1797 outs() << " tocoff " << dyst.tocoff;
1798 if (dyst.tocoff > object_size)
1799 outs() << " (past end of file)\n";
1802 outs() << " ntoc " << dyst.ntoc;
1803 big_size = dyst.ntoc;
1804 big_size *= sizeof(struct MachO::dylib_table_of_contents);
1805 big_size += dyst.tocoff;
1806 if (big_size > object_size)
1807 outs() << " (past end of file)\n";
1810 outs() << " modtaboff " << dyst.modtaboff;
1811 if (dyst.modtaboff > object_size)
1812 outs() << " (past end of file)\n";
1815 outs() << " nmodtab " << dyst.nmodtab;
1817 if (cputype & MachO::CPU_ARCH_ABI64) {
1818 modtabend = dyst.nmodtab;
1819 modtabend *= sizeof(struct MachO::dylib_module_64);
1820 modtabend += dyst.modtaboff;
1822 modtabend = dyst.nmodtab;
1823 modtabend *= sizeof(struct MachO::dylib_module);
1824 modtabend += dyst.modtaboff;
1826 if (modtabend > object_size)
1827 outs() << " (past end of file)\n";
1830 outs() << " extrefsymoff " << dyst.extrefsymoff;
1831 if (dyst.extrefsymoff > object_size)
1832 outs() << " (past end of file)\n";
1835 outs() << " nextrefsyms " << dyst.nextrefsyms;
1836 big_size = dyst.nextrefsyms;
1837 big_size *= sizeof(struct MachO::dylib_reference);
1838 big_size += dyst.extrefsymoff;
1839 if (big_size > object_size)
1840 outs() << " (past end of file)\n";
1843 outs() << " indirectsymoff " << dyst.indirectsymoff;
1844 if (dyst.indirectsymoff > object_size)
1845 outs() << " (past end of file)\n";
1848 outs() << " nindirectsyms " << dyst.nindirectsyms;
1849 big_size = dyst.nindirectsyms;
1850 big_size *= sizeof(uint32_t);
1851 big_size += dyst.indirectsymoff;
1852 if (big_size > object_size)
1853 outs() << " (past end of file)\n";
1856 outs() << " extreloff " << dyst.extreloff;
1857 if (dyst.extreloff > object_size)
1858 outs() << " (past end of file)\n";
1861 outs() << " nextrel " << dyst.nextrel;
1862 big_size = dyst.nextrel;
1863 big_size *= sizeof(struct MachO::relocation_info);
1864 big_size += dyst.extreloff;
1865 if (big_size > object_size)
1866 outs() << " (past end of file)\n";
1869 outs() << " locreloff " << dyst.locreloff;
1870 if (dyst.locreloff > object_size)
1871 outs() << " (past end of file)\n";
1874 outs() << " nlocrel " << dyst.nlocrel;
1875 big_size = dyst.nlocrel;
1876 big_size *= sizeof(struct MachO::relocation_info);
1877 big_size += dyst.locreloff;
1878 if (big_size > object_size)
1879 outs() << " (past end of file)\n";
1884 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
1885 uint32_t object_size) {
1886 if (dc.cmd == MachO::LC_DYLD_INFO)
1887 outs() << " cmd LC_DYLD_INFO\n";
1889 outs() << " cmd LC_DYLD_INFO_ONLY\n";
1890 outs() << " cmdsize " << dc.cmdsize;
1891 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
1892 outs() << " Incorrect size\n";
1895 outs() << " rebase_off " << dc.rebase_off;
1896 if (dc.rebase_off > object_size)
1897 outs() << " (past end of file)\n";
1900 outs() << " rebase_size " << dc.rebase_size;
1902 big_size = dc.rebase_off;
1903 big_size += dc.rebase_size;
1904 if (big_size > object_size)
1905 outs() << " (past end of file)\n";
1908 outs() << " bind_off " << dc.bind_off;
1909 if (dc.bind_off > object_size)
1910 outs() << " (past end of file)\n";
1913 outs() << " bind_size " << dc.bind_size;
1914 big_size = dc.bind_off;
1915 big_size += dc.bind_size;
1916 if (big_size > object_size)
1917 outs() << " (past end of file)\n";
1920 outs() << " weak_bind_off " << dc.weak_bind_off;
1921 if (dc.weak_bind_off > object_size)
1922 outs() << " (past end of file)\n";
1925 outs() << " weak_bind_size " << dc.weak_bind_size;
1926 big_size = dc.weak_bind_off;
1927 big_size += dc.weak_bind_size;
1928 if (big_size > object_size)
1929 outs() << " (past end of file)\n";
1932 outs() << " lazy_bind_off " << dc.lazy_bind_off;
1933 if (dc.lazy_bind_off > object_size)
1934 outs() << " (past end of file)\n";
1937 outs() << " lazy_bind_size " << dc.lazy_bind_size;
1938 big_size = dc.lazy_bind_off;
1939 big_size += dc.lazy_bind_size;
1940 if (big_size > object_size)
1941 outs() << " (past end of file)\n";
1944 outs() << " export_off " << dc.export_off;
1945 if (dc.export_off > object_size)
1946 outs() << " (past end of file)\n";
1949 outs() << " export_size " << dc.export_size;
1950 big_size = dc.export_off;
1951 big_size += dc.export_size;
1952 if (big_size > object_size)
1953 outs() << " (past end of file)\n";
1958 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
1960 if (dyld.cmd == MachO::LC_ID_DYLINKER)
1961 outs() << " cmd LC_ID_DYLINKER\n";
1962 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
1963 outs() << " cmd LC_LOAD_DYLINKER\n";
1964 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
1965 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
1967 outs() << " cmd ?(" << dyld.cmd << ")\n";
1968 outs() << " cmdsize " << dyld.cmdsize;
1969 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
1970 outs() << " Incorrect size\n";
1973 if (dyld.name >= dyld.cmdsize)
1974 outs() << " name ?(bad offset " << dyld.name << ")\n";
1976 const char *P = (const char *)(Ptr)+dyld.name;
1977 outs() << " name " << P << " (offset " << dyld.name << ")\n";
1981 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
1982 outs() << " cmd LC_UUID\n";
1983 outs() << " cmdsize " << uuid.cmdsize;
1984 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
1985 outs() << " Incorrect size\n";
1989 outs() << format("%02" PRIX32, uuid.uuid[0]);
1990 outs() << format("%02" PRIX32, uuid.uuid[1]);
1991 outs() << format("%02" PRIX32, uuid.uuid[2]);
1992 outs() << format("%02" PRIX32, uuid.uuid[3]);
1994 outs() << format("%02" PRIX32, uuid.uuid[4]);
1995 outs() << format("%02" PRIX32, uuid.uuid[5]);
1997 outs() << format("%02" PRIX32, uuid.uuid[6]);
1998 outs() << format("%02" PRIX32, uuid.uuid[7]);
2000 outs() << format("%02" PRIX32, uuid.uuid[8]);
2001 outs() << format("%02" PRIX32, uuid.uuid[9]);
2003 outs() << format("%02" PRIX32, uuid.uuid[10]);
2004 outs() << format("%02" PRIX32, uuid.uuid[11]);
2005 outs() << format("%02" PRIX32, uuid.uuid[12]);
2006 outs() << format("%02" PRIX32, uuid.uuid[13]);
2007 outs() << format("%02" PRIX32, uuid.uuid[14]);
2008 outs() << format("%02" PRIX32, uuid.uuid[15]);
2012 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
2013 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
2014 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
2015 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
2016 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
2018 outs() << " cmd " << vd.cmd << " (?)\n";
2019 outs() << " cmdsize " << vd.cmdsize;
2020 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
2021 outs() << " Incorrect size\n";
2024 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
2025 << ((vd.version >> 8) & 0xff);
2026 if ((vd.version & 0xff) != 0)
2027 outs() << "." << (vd.version & 0xff);
2030 outs() << " sdk n/a\n";
2032 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
2033 << ((vd.sdk >> 8) & 0xff);
2035 if ((vd.sdk & 0xff) != 0)
2036 outs() << "." << (vd.sdk & 0xff);
2040 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
2041 outs() << " cmd LC_SOURCE_VERSION\n";
2042 outs() << " cmdsize " << sd.cmdsize;
2043 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
2044 outs() << " Incorrect size\n";
2047 uint64_t a = (sd.version >> 40) & 0xffffff;
2048 uint64_t b = (sd.version >> 30) & 0x3ff;
2049 uint64_t c = (sd.version >> 20) & 0x3ff;
2050 uint64_t d = (sd.version >> 10) & 0x3ff;
2051 uint64_t e = sd.version & 0x3ff;
2052 outs() << " version " << a << "." << b;
2054 outs() << "." << c << "." << d << "." << e;
2056 outs() << "." << c << "." << d;
2062 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
2063 outs() << " cmd LC_MAIN\n";
2064 outs() << " cmdsize " << ep.cmdsize;
2065 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
2066 outs() << " Incorrect size\n";
2069 outs() << " entryoff " << ep.entryoff << "\n";
2070 outs() << " stacksize " << ep.stacksize << "\n";
2073 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
2074 if (dl.cmd == MachO::LC_ID_DYLIB)
2075 outs() << " cmd LC_ID_DYLIB\n";
2076 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
2077 outs() << " cmd LC_LOAD_DYLIB\n";
2078 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
2079 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
2080 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
2081 outs() << " cmd LC_REEXPORT_DYLIB\n";
2082 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
2083 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
2084 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
2085 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
2087 outs() << " cmd " << dl.cmd << " (unknown)\n";
2088 outs() << " cmdsize " << dl.cmdsize;
2089 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
2090 outs() << " Incorrect size\n";
2093 if (dl.dylib.name < dl.cmdsize) {
2094 const char *P = (const char *)(Ptr)+dl.dylib.name;
2095 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
2097 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
2099 outs() << " time stamp " << dl.dylib.timestamp << " ";
2100 time_t t = dl.dylib.timestamp;
2101 outs() << ctime(&t);
2102 outs() << " current version ";
2103 if (dl.dylib.current_version == 0xffffffff)
2106 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
2107 << ((dl.dylib.current_version >> 8) & 0xff) << "."
2108 << (dl.dylib.current_version & 0xff) << "\n";
2109 outs() << "compatibility version ";
2110 if (dl.dylib.compatibility_version == 0xffffffff)
2113 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
2114 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
2115 << (dl.dylib.compatibility_version & 0xff) << "\n";
2118 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
2119 uint32_t object_size) {
2120 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
2121 outs() << " cmd LC_FUNCTION_STARTS\n";
2122 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
2123 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
2124 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
2125 outs() << " cmd LC_FUNCTION_STARTS\n";
2126 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
2127 outs() << " cmd LC_DATA_IN_CODE\n";
2128 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
2129 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
2130 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
2131 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
2133 outs() << " cmd " << ld.cmd << " (?)\n";
2134 outs() << " cmdsize " << ld.cmdsize;
2135 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
2136 outs() << " Incorrect size\n";
2139 outs() << " dataoff " << ld.dataoff;
2140 if (ld.dataoff > object_size)
2141 outs() << " (past end of file)\n";
2144 outs() << " datasize " << ld.datasize;
2145 uint64_t big_size = ld.dataoff;
2146 big_size += ld.datasize;
2147 if (big_size > object_size)
2148 outs() << " (past end of file)\n";
2153 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds,
2154 uint32_t filetype, uint32_t cputype,
2156 StringRef Buf = Obj->getData();
2157 MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo();
2158 for (unsigned i = 0;; ++i) {
2159 outs() << "Load command " << i << "\n";
2160 if (Command.C.cmd == MachO::LC_SEGMENT) {
2161 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
2162 const char *sg_segname = SLC.segname;
2163 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
2164 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
2165 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
2167 for (unsigned j = 0; j < SLC.nsects; j++) {
2168 MachO::section_64 S = Obj->getSection64(Command, j);
2169 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
2170 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
2171 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
2173 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
2174 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
2175 const char *sg_segname = SLC_64.segname;
2176 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
2177 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
2178 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
2179 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
2180 for (unsigned j = 0; j < SLC_64.nsects; j++) {
2181 MachO::section_64 S_64 = Obj->getSection64(Command, j);
2182 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
2183 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
2184 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
2185 sg_segname, filetype, Buf.size(), verbose);
2187 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
2188 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
2189 PrintSymtabLoadCommand(Symtab, cputype, Buf.size());
2190 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
2191 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
2192 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
2193 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(), cputype);
2194 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
2195 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
2196 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
2197 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
2198 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
2199 Command.C.cmd == MachO::LC_ID_DYLINKER ||
2200 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
2201 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
2202 PrintDyldLoadCommand(Dyld, Command.Ptr);
2203 } else if (Command.C.cmd == MachO::LC_UUID) {
2204 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
2205 PrintUuidLoadCommand(Uuid);
2206 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX) {
2207 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
2208 PrintVersionMinLoadCommand(Vd);
2209 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
2210 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
2211 PrintSourceVersionCommand(Sd);
2212 } else if (Command.C.cmd == MachO::LC_MAIN) {
2213 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
2214 PrintEntryPointCommand(Ep);
2215 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB) {
2216 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
2217 PrintDylibCommand(Dl, Command.Ptr);
2218 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
2219 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
2220 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
2221 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
2222 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
2223 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
2224 MachO::linkedit_data_command Ld =
2225 Obj->getLinkeditDataLoadCommand(Command);
2226 PrintLinkEditDataCommand(Ld, Buf.size());
2228 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
2230 outs() << " cmdsize " << Command.C.cmdsize << "\n";
2231 // TODO: get and print the raw bytes of the load command.
2233 // TODO: print all the other kinds of load commands.
2237 Command = Obj->getNextLoadCommandInfo(Command);
2241 static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds,
2242 uint32_t &filetype, uint32_t &cputype,
2244 if (Obj->is64Bit()) {
2245 MachO::mach_header_64 H_64;
2246 H_64 = Obj->getHeader64();
2247 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
2248 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
2250 filetype = H_64.filetype;
2251 cputype = H_64.cputype;
2253 MachO::mach_header H;
2254 H = Obj->getHeader();
2255 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
2256 H.sizeofcmds, H.flags, verbose);
2258 filetype = H.filetype;
2259 cputype = H.cputype;
2263 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
2264 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
2266 uint32_t filetype = 0;
2267 uint32_t cputype = 0;
2268 getAndPrintMachHeader(file, ncmds, filetype, cputype, true);
2269 PrintLoadCommands(file, ncmds, filetype, cputype, true);
2272 //===----------------------------------------------------------------------===//
2273 // export trie dumping
2274 //===----------------------------------------------------------------------===//
2276 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
2277 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
2278 uint64_t Flags = Entry.flags();
2279 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
2280 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
2281 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
2282 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
2283 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
2284 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
2285 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
2287 outs() << "[re-export] ";
2290 << format("0x%08llX ", Entry.address()); // FIXME:add in base address
2291 outs() << Entry.name();
2292 if (WeakDef || ThreadLocal || Resolver || Abs) {
2293 bool NeedsComma = false;
2296 outs() << "weak_def";
2302 outs() << "per-thread";
2308 outs() << "absolute";
2314 outs() << format("resolver=0x%08llX", Entry.other());
2320 StringRef DylibName = "unknown";
2321 int Ordinal = Entry.other() - 1;
2322 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
2323 if (Entry.otherName().empty())
2324 outs() << " (from " << DylibName << ")";
2326 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
2333 //===----------------------------------------------------------------------===//
2334 // rebase table dumping
2335 //===----------------------------------------------------------------------===//
2340 SegInfo(const object::MachOObjectFile *Obj);
2342 StringRef segmentName(uint32_t SegIndex);
2343 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
2344 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
2347 struct SectionInfo {
2350 StringRef SectionName;
2351 StringRef SegmentName;
2352 uint64_t OffsetInSegment;
2353 uint64_t SegmentStartAddress;
2354 uint32_t SegmentIndex;
2356 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
2357 SmallVector<SectionInfo, 32> Sections;
2361 SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
2362 // Build table of sections so segIndex/offset pairs can be translated.
2363 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
2364 StringRef CurSegName;
2365 uint64_t CurSegAddress;
2366 for (const SectionRef &Section : Obj->sections()) {
2368 if (error(Section.getName(Info.SectionName)))
2370 if (error(Section.getAddress(Info.Address)))
2372 if (error(Section.getSize(Info.Size)))
2375 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
2376 if (!Info.SegmentName.equals(CurSegName)) {
2378 CurSegName = Info.SegmentName;
2379 CurSegAddress = Info.Address;
2381 Info.SegmentIndex = CurSegIndex - 1;
2382 Info.OffsetInSegment = Info.Address - CurSegAddress;
2383 Info.SegmentStartAddress = CurSegAddress;
2384 Sections.push_back(Info);
2388 StringRef SegInfo::segmentName(uint32_t SegIndex) {
2389 for (const SectionInfo &SI : Sections) {
2390 if (SI.SegmentIndex == SegIndex)
2391 return SI.SegmentName;
2393 llvm_unreachable("invalid segIndex");
2396 const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
2397 uint64_t OffsetInSeg) {
2398 for (const SectionInfo &SI : Sections) {
2399 if (SI.SegmentIndex != SegIndex)
2401 if (SI.OffsetInSegment > OffsetInSeg)
2403 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
2407 llvm_unreachable("segIndex and offset not in any section");
2410 StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
2411 return findSection(SegIndex, OffsetInSeg).SectionName;
2414 uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
2415 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
2416 return SI.SegmentStartAddress + OffsetInSeg;
2419 void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
2420 // Build table of sections so names can used in final output.
2421 SegInfo sectionTable(Obj);
2423 outs() << "segment section address type\n";
2424 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
2425 uint32_t SegIndex = Entry.segmentIndex();
2426 uint64_t OffsetInSeg = Entry.segmentOffset();
2427 StringRef SegmentName = sectionTable.segmentName(SegIndex);
2428 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
2429 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
2431 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
2432 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
2433 SegmentName.str().c_str(),
2434 SectionName.str().c_str(), Address,
2435 Entry.typeName().str().c_str());
2439 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
2440 StringRef DylibName;
2442 case MachO::BIND_SPECIAL_DYLIB_SELF:
2443 return "this-image";
2444 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
2445 return "main-executable";
2446 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
2447 return "flat-namespace";
2450 std::error_code EC = Obj->getLibraryShortNameByIndex(Ordinal-1,
2453 return "<<ordinal too big>>";
2457 return "<<unknown special ordinal>>";
2460 //===----------------------------------------------------------------------===//
2461 // bind table dumping
2462 //===----------------------------------------------------------------------===//
2464 void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
2465 // Build table of sections so names can used in final output.
2466 SegInfo sectionTable(Obj);
2468 outs() << "segment section address type "
2469 "addend dylib symbol\n";
2470 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
2471 uint32_t SegIndex = Entry.segmentIndex();
2472 uint64_t OffsetInSeg = Entry.segmentOffset();
2473 StringRef SegmentName = sectionTable.segmentName(SegIndex);
2474 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
2475 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
2477 // Table lines look like:
2478 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
2479 outs() << format("%-8s %-18s 0x%08" PRIX64 " %-8s %-8" PRId64 " %-20s",
2480 SegmentName.str().c_str(),
2481 SectionName.str().c_str(),
2483 Entry.typeName().str().c_str(),
2485 ordinalName(Obj, Entry.ordinal()))
2486 << Entry.symbolName();
2487 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
2488 outs() << " (weak_import)\n";
2494 //===----------------------------------------------------------------------===//
2495 // lazy bind table dumping
2496 //===----------------------------------------------------------------------===//
2498 void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
2499 // Build table of sections so names can used in final output.
2500 SegInfo sectionTable(Obj);
2502 outs() << "segment section address "
2504 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
2505 uint32_t SegIndex = Entry.segmentIndex();
2506 uint64_t OffsetInSeg = Entry.segmentOffset();
2507 StringRef SegmentName = sectionTable.segmentName(SegIndex);
2508 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
2509 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
2511 // Table lines look like:
2512 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
2513 outs() << format("%-8s %-18s 0x%08" PRIX64 " %-20s",
2514 SegmentName.str().c_str(),
2515 SectionName.str().c_str(),
2517 ordinalName(Obj, Entry.ordinal()))
2518 << Entry.symbolName() << "\n";
2523 //===----------------------------------------------------------------------===//
2524 // weak bind table dumping
2525 //===----------------------------------------------------------------------===//
2527 void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
2528 // Build table of sections so names can used in final output.
2529 SegInfo sectionTable(Obj);
2531 outs() << "segment section address "
2532 "type addend symbol\n";
2533 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
2534 // Strong symbols don't have a location to update.
2535 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
2536 outs() << " strong "
2537 << Entry.symbolName() << "\n";
2540 uint32_t SegIndex = Entry.segmentIndex();
2541 uint64_t OffsetInSeg = Entry.segmentOffset();
2542 StringRef SegmentName = sectionTable.segmentName(SegIndex);
2543 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
2544 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
2546 // Table lines look like:
2547 // __DATA __data 0x00001000 pointer 0 _foo
2548 outs() << format("%-8s %-18s 0x%08" PRIX64 " %-8s %-8" PRId64 " ",
2549 SegmentName.str().c_str(),
2550 SectionName.str().c_str(),
2552 Entry.typeName().str().c_str(),
2554 << Entry.symbolName() << "\n";