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/Config/config.h"
20 #include "llvm/DebugInfo/DIContext.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCDisassembler.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCInstPrinter.h"
26 #include "llvm/MC/MCInstrAnalysis.h"
27 #include "llvm/MC/MCInstrDesc.h"
28 #include "llvm/MC/MCInstrInfo.h"
29 #include "llvm/MC/MCRegisterInfo.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/Object/MachO.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/Endian.h"
36 #include "llvm/Support/Format.h"
37 #include "llvm/Support/GraphWriter.h"
38 #include "llvm/Support/MachO.h"
39 #include "llvm/Support/MemoryBuffer.h"
40 #include "llvm/Support/FormattedStream.h"
41 #include "llvm/Support/TargetRegistry.h"
42 #include "llvm/Support/TargetSelect.h"
43 #include "llvm/Support/raw_ostream.h"
46 #include <system_error>
53 using namespace object;
57 cl::desc("Print line information from debug info if available"));
59 static cl::opt<std::string> DSYMFile("dsym",
60 cl::desc("Use .dSYM file for debug info"));
62 static cl::opt<bool> FullLeadingAddr("full-leading-addr",
63 cl::desc("Print full leading address"));
66 PrintImmHex("print-imm-hex",
67 cl::desc("Use hex format for immediate values"));
69 static std::string ThumbTripleName;
71 static const Target *GetTarget(const MachOObjectFile *MachOObj,
72 const char **McpuDefault,
73 const Target **ThumbTarget) {
74 // Figure out the target triple.
75 if (TripleName.empty()) {
76 llvm::Triple TT("unknown-unknown-unknown");
77 llvm::Triple ThumbTriple = Triple();
78 TT = MachOObj->getArch(McpuDefault, &ThumbTriple);
79 TripleName = TT.str();
80 ThumbTripleName = ThumbTriple.str();
83 // Get the target specific parser.
85 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
86 if (TheTarget && ThumbTripleName.empty())
89 *ThumbTarget = TargetRegistry::lookupTarget(ThumbTripleName, Error);
93 errs() << "llvm-objdump: error: unable to get target for '";
97 errs() << ThumbTripleName;
98 errs() << "', see --version and --triple.\n";
102 struct SymbolSorter {
103 bool operator()(const SymbolRef &A, const SymbolRef &B) {
104 SymbolRef::Type AType, BType;
108 uint64_t AAddr, BAddr;
109 if (AType != SymbolRef::ST_Function)
113 if (BType != SymbolRef::ST_Function)
117 return AAddr < BAddr;
121 // Types for the storted data in code table that is built before disassembly
122 // and the predicate function to sort them.
123 typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
124 typedef std::vector<DiceTableEntry> DiceTable;
125 typedef DiceTable::iterator dice_table_iterator;
127 // This is used to search for a data in code table entry for the PC being
128 // disassembled. The j parameter has the PC in j.first. A single data in code
129 // table entry can cover many bytes for each of its Kind's. So if the offset,
130 // aka the i.first value, of the data in code table entry plus its Length
131 // covers the PC being searched for this will return true. If not it will
133 static bool compareDiceTableEntries(const DiceTableEntry &i,
134 const DiceTableEntry &j) {
136 i.second.getLength(Length);
138 return j.first >= i.first && j.first < i.first + Length;
141 static uint64_t DumpDataInCode(const char *bytes, uint64_t Length,
142 unsigned short Kind) {
143 uint32_t Value, Size = 1;
147 case MachO::DICE_KIND_DATA:
150 DumpBytes(StringRef(bytes, 4));
151 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
152 outs() << "\t.long " << Value;
154 } else if (Length >= 2) {
156 DumpBytes(StringRef(bytes, 2));
157 Value = bytes[1] << 8 | bytes[0];
158 outs() << "\t.short " << Value;
162 DumpBytes(StringRef(bytes, 2));
164 outs() << "\t.byte " << Value;
167 if (Kind == MachO::DICE_KIND_DATA)
168 outs() << "\t@ KIND_DATA\n";
170 outs() << "\t@ data in code kind = " << Kind << "\n";
172 case MachO::DICE_KIND_JUMP_TABLE8:
174 DumpBytes(StringRef(bytes, 1));
176 outs() << "\t.byte " << format("%3u", Value) << "\t@ KIND_JUMP_TABLE8\n";
179 case MachO::DICE_KIND_JUMP_TABLE16:
181 DumpBytes(StringRef(bytes, 2));
182 Value = bytes[1] << 8 | bytes[0];
183 outs() << "\t.short " << format("%5u", Value & 0xffff)
184 << "\t@ KIND_JUMP_TABLE16\n";
187 case MachO::DICE_KIND_JUMP_TABLE32:
188 case MachO::DICE_KIND_ABS_JUMP_TABLE32:
190 DumpBytes(StringRef(bytes, 4));
191 Value = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
192 outs() << "\t.long " << Value;
193 if (Kind == MachO::DICE_KIND_JUMP_TABLE32)
194 outs() << "\t@ KIND_JUMP_TABLE32\n";
196 outs() << "\t@ KIND_ABS_JUMP_TABLE32\n";
203 static void getSectionsAndSymbols(const MachO::mach_header Header,
204 MachOObjectFile *MachOObj,
205 std::vector<SectionRef> &Sections,
206 std::vector<SymbolRef> &Symbols,
207 SmallVectorImpl<uint64_t> &FoundFns,
208 uint64_t &BaseSegmentAddress) {
209 for (const SymbolRef &Symbol : MachOObj->symbols())
210 Symbols.push_back(Symbol);
212 for (const SectionRef &Section : MachOObj->sections()) {
214 Section.getName(SectName);
215 Sections.push_back(Section);
218 MachOObjectFile::LoadCommandInfo Command =
219 MachOObj->getFirstLoadCommandInfo();
220 bool BaseSegmentAddressSet = false;
221 for (unsigned i = 0;; ++i) {
222 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
223 // We found a function starts segment, parse the addresses for later
225 MachO::linkedit_data_command LLC =
226 MachOObj->getLinkeditDataLoadCommand(Command);
228 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
229 } else if (Command.C.cmd == MachO::LC_SEGMENT) {
230 MachO::segment_command SLC = MachOObj->getSegmentLoadCommand(Command);
231 StringRef SegName = SLC.segname;
232 if (!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
233 BaseSegmentAddressSet = true;
234 BaseSegmentAddress = SLC.vmaddr;
238 if (i == Header.ncmds - 1)
241 Command = MachOObj->getNextLoadCommandInfo(Command);
245 static void DisassembleInputMachO2(StringRef Filename,
246 MachOObjectFile *MachOOF);
248 void llvm::DisassembleInputMachO(StringRef Filename) {
249 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
250 MemoryBuffer::getFileOrSTDIN(Filename);
251 if (std::error_code EC = BuffOrErr.getError()) {
252 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << "\n";
255 std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
257 std::unique_ptr<MachOObjectFile> MachOOF = std::move(
258 ObjectFile::createMachOObjectFile(Buff.get()->getMemBufferRef()).get());
260 DisassembleInputMachO2(Filename, MachOOF.get());
263 typedef DenseMap<uint64_t, StringRef> SymbolAddressMap;
264 typedef std::pair<uint64_t, const char *> BindInfoEntry;
265 typedef std::vector<BindInfoEntry> BindTable;
266 typedef BindTable::iterator bind_table_iterator;
268 // The block of info used by the Symbolizer call backs.
269 struct DisassembleInfo {
273 SymbolAddressMap *AddrMap;
274 std::vector<SectionRef> *Sections;
275 const char *class_name;
276 const char *selector_name;
278 char *demangled_name;
279 BindTable *bindtable;
282 // GuessSymbolName is passed the address of what might be a symbol and a
283 // pointer to the DisassembleInfo struct. It returns the name of a symbol
284 // with that address or nullptr if no symbol is found with that address.
285 static const char *GuessSymbolName(uint64_t value,
286 struct DisassembleInfo *info) {
287 const char *SymbolName = nullptr;
288 // A DenseMap can't lookup up some values.
289 if (value != 0xffffffffffffffffULL && value != 0xfffffffffffffffeULL) {
290 StringRef name = info->AddrMap->lookup(value);
292 SymbolName = name.data();
297 // SymbolizerGetOpInfo() is the operand information call back function.
298 // This is called to get the symbolic information for operand(s) of an
299 // instruction when it is being done. This routine does this from
300 // the relocation information, symbol table, etc. That block of information
301 // is a pointer to the struct DisassembleInfo that was passed when the
302 // disassembler context was created and passed to back to here when
303 // called back by the disassembler for instruction operands that could have
304 // relocation information. The address of the instruction containing operand is
305 // at the Pc parameter. The immediate value the operand has is passed in
306 // op_info->Value and is at Offset past the start of the instruction and has a
307 // byte Size of 1, 2 or 4. The symbolc information is returned in TagBuf is the
308 // LLVMOpInfo1 struct defined in the header "llvm-c/Disassembler.h" as symbol
309 // names and addends of the symbolic expression to add for the operand. The
310 // value of TagType is currently 1 (for the LLVMOpInfo1 struct). If symbolic
311 // information is returned then this function returns 1 else it returns 0.
312 int SymbolizerGetOpInfo(void *DisInfo, uint64_t Pc, uint64_t Offset,
313 uint64_t Size, int TagType, void *TagBuf) {
314 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
315 struct LLVMOpInfo1 *op_info = (struct LLVMOpInfo1 *)TagBuf;
316 unsigned int value = op_info->Value;
318 // Make sure all fields returned are zero if we don't set them.
319 memset((void *)op_info, '\0', sizeof(struct LLVMOpInfo1));
320 op_info->Value = value;
322 // If the TagType is not the value 1 which it code knows about or if no
323 // verbose symbolic information is wanted then just return 0, indicating no
324 // information is being returned.
325 if (TagType != 1 || info->verbose == false)
328 unsigned int Arch = info->O->getArch();
329 if (Arch == Triple::x86) {
330 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
332 // First search the section's relocation entries (if any) for an entry
333 // for this section offset.
334 uint32_t sect_addr = info->S.getAddress();
335 uint32_t sect_offset = (Pc + Offset) - sect_addr;
336 bool reloc_found = false;
338 MachO::any_relocation_info RE;
339 bool isExtern = false;
341 bool r_scattered = false;
342 uint32_t r_value, pair_r_value, r_type;
343 for (const RelocationRef &Reloc : info->S.relocations()) {
344 uint64_t RelocOffset;
345 Reloc.getOffset(RelocOffset);
346 if (RelocOffset == sect_offset) {
347 Rel = Reloc.getRawDataRefImpl();
348 RE = info->O->getRelocation(Rel);
349 r_type = info->O->getAnyRelocationType(RE);
350 r_scattered = info->O->isRelocationScattered(RE);
352 r_value = info->O->getScatteredRelocationValue(RE);
353 if (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
354 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF) {
355 DataRefImpl RelNext = Rel;
356 info->O->moveRelocationNext(RelNext);
357 MachO::any_relocation_info RENext;
358 RENext = info->O->getRelocation(RelNext);
359 if (info->O->isRelocationScattered(RENext))
360 pair_r_value = info->O->getScatteredRelocationValue(RENext);
365 isExtern = info->O->getPlainRelocationExternal(RE);
367 symbol_iterator RelocSym = Reloc.getSymbol();
375 if (reloc_found && isExtern) {
377 Symbol.getName(SymName);
378 const char *name = SymName.data();
379 op_info->AddSymbol.Present = 1;
380 op_info->AddSymbol.Name = name;
381 // For i386 extern relocation entries the value in the instruction is
382 // the offset from the symbol, and value is already set in op_info->Value.
385 if (reloc_found && (r_type == MachO::GENERIC_RELOC_SECTDIFF ||
386 r_type == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)) {
387 const char *add = GuessSymbolName(r_value, info);
388 const char *sub = GuessSymbolName(pair_r_value, info);
389 uint32_t offset = value - (r_value - pair_r_value);
390 op_info->AddSymbol.Present = 1;
392 op_info->AddSymbol.Name = add;
394 op_info->AddSymbol.Value = r_value;
395 op_info->SubtractSymbol.Present = 1;
397 op_info->SubtractSymbol.Name = sub;
399 op_info->SubtractSymbol.Value = pair_r_value;
400 op_info->Value = offset;
404 // Second search the external relocation entries of a fully linked image
405 // (if any) for an entry that matches this segment offset.
406 // uint32_t seg_offset = (Pc + Offset);
408 } else if (Arch == Triple::x86_64) {
409 if (Size != 1 && Size != 2 && Size != 4 && Size != 0)
411 // First search the section's relocation entries (if any) for an entry
412 // for this section offset.
413 uint64_t sect_addr = info->S.getAddress();
414 uint64_t sect_offset = (Pc + Offset) - sect_addr;
415 bool reloc_found = false;
417 MachO::any_relocation_info RE;
418 bool isExtern = false;
420 for (const RelocationRef &Reloc : info->S.relocations()) {
421 uint64_t RelocOffset;
422 Reloc.getOffset(RelocOffset);
423 if (RelocOffset == sect_offset) {
424 Rel = Reloc.getRawDataRefImpl();
425 RE = info->O->getRelocation(Rel);
426 // NOTE: Scattered relocations don't exist on x86_64.
427 isExtern = info->O->getPlainRelocationExternal(RE);
429 symbol_iterator RelocSym = Reloc.getSymbol();
436 if (reloc_found && isExtern) {
437 // The Value passed in will be adjusted by the Pc if the instruction
438 // adds the Pc. But for x86_64 external relocation entries the Value
439 // is the offset from the external symbol.
440 if (info->O->getAnyRelocationPCRel(RE))
441 op_info->Value -= Pc + Offset + Size;
443 Symbol.getName(SymName);
444 const char *name = SymName.data();
445 unsigned Type = info->O->getAnyRelocationType(RE);
446 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
447 DataRefImpl RelNext = Rel;
448 info->O->moveRelocationNext(RelNext);
449 MachO::any_relocation_info RENext = info->O->getRelocation(RelNext);
450 unsigned TypeNext = info->O->getAnyRelocationType(RENext);
451 bool isExternNext = info->O->getPlainRelocationExternal(RENext);
452 unsigned SymbolNum = info->O->getPlainRelocationSymbolNum(RENext);
453 if (TypeNext == MachO::X86_64_RELOC_UNSIGNED && isExternNext) {
454 op_info->SubtractSymbol.Present = 1;
455 op_info->SubtractSymbol.Name = name;
456 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
457 Symbol = *RelocSymNext;
458 StringRef SymNameNext;
459 Symbol.getName(SymNameNext);
460 name = SymNameNext.data();
463 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
464 // like: X86_64_RELOC_TLV, X86_64_RELOC_GOT_LOAD and X86_64_RELOC_GOT.
465 op_info->AddSymbol.Present = 1;
466 op_info->AddSymbol.Name = name;
470 // Second search the external relocation entries of a fully linked image
471 // (if any) for an entry that matches this segment offset.
472 // uint64_t seg_offset = (Pc + Offset);
474 } else if (Arch == Triple::arm) {
475 if (Offset != 0 || (Size != 4 && Size != 2))
477 // First search the section's relocation entries (if any) for an entry
478 // for this section offset.
479 uint32_t sect_addr = info->S.getAddress();
480 uint32_t sect_offset = (Pc + Offset) - sect_addr;
481 bool reloc_found = false;
483 MachO::any_relocation_info RE;
484 bool isExtern = false;
486 bool r_scattered = false;
487 uint32_t r_value, pair_r_value, r_type, r_length, other_half;
488 for (const RelocationRef &Reloc : info->S.relocations()) {
489 uint64_t RelocOffset;
490 Reloc.getOffset(RelocOffset);
491 if (RelocOffset == sect_offset) {
492 Rel = Reloc.getRawDataRefImpl();
493 RE = info->O->getRelocation(Rel);
494 r_length = info->O->getAnyRelocationLength(RE);
495 r_scattered = info->O->isRelocationScattered(RE);
497 r_value = info->O->getScatteredRelocationValue(RE);
498 r_type = info->O->getScatteredRelocationType(RE);
500 r_type = info->O->getAnyRelocationType(RE);
501 isExtern = info->O->getPlainRelocationExternal(RE);
503 symbol_iterator RelocSym = Reloc.getSymbol();
507 if (r_type == MachO::ARM_RELOC_HALF ||
508 r_type == MachO::ARM_RELOC_SECTDIFF ||
509 r_type == MachO::ARM_RELOC_LOCAL_SECTDIFF ||
510 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
511 DataRefImpl RelNext = Rel;
512 info->O->moveRelocationNext(RelNext);
513 MachO::any_relocation_info RENext;
514 RENext = info->O->getRelocation(RelNext);
515 other_half = info->O->getAnyRelocationAddress(RENext) & 0xffff;
516 if (info->O->isRelocationScattered(RENext))
517 pair_r_value = info->O->getScatteredRelocationValue(RENext);
523 if (reloc_found && isExtern) {
525 Symbol.getName(SymName);
526 const char *name = SymName.data();
527 op_info->AddSymbol.Present = 1;
528 op_info->AddSymbol.Name = name;
531 case MachO::ARM_RELOC_HALF:
532 if ((r_length & 0x1) == 1) {
533 op_info->Value = value << 16 | other_half;
534 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
536 op_info->Value = other_half << 16 | value;
537 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
545 case MachO::ARM_RELOC_HALF:
546 if ((r_length & 0x1) == 1) {
547 op_info->Value = value << 16 | other_half;
548 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
550 op_info->Value = other_half << 16 | value;
551 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
560 // If we have a branch that is not an external relocation entry then
561 // return 0 so the code in tryAddingSymbolicOperand() can use the
562 // SymbolLookUp call back with the branch target address to look up the
563 // symbol and possiblity add an annotation for a symbol stub.
564 if (reloc_found && isExtern == 0 && (r_type == MachO::ARM_RELOC_BR24 ||
565 r_type == MachO::ARM_THUMB_RELOC_BR22))
570 if (r_type == MachO::ARM_RELOC_HALF ||
571 r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
572 if ((r_length & 0x1) == 1)
573 value = value << 16 | other_half;
575 value = other_half << 16 | value;
577 if (r_scattered && (r_type != MachO::ARM_RELOC_HALF &&
578 r_type != MachO::ARM_RELOC_HALF_SECTDIFF)) {
579 offset = value - r_value;
584 if (reloc_found && r_type == MachO::ARM_RELOC_HALF_SECTDIFF) {
585 if ((r_length & 0x1) == 1)
586 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
588 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
589 const char *add = GuessSymbolName(r_value, info);
590 const char *sub = GuessSymbolName(pair_r_value, info);
591 int32_t offset = value - (r_value - pair_r_value);
592 op_info->AddSymbol.Present = 1;
594 op_info->AddSymbol.Name = add;
596 op_info->AddSymbol.Value = r_value;
597 op_info->SubtractSymbol.Present = 1;
599 op_info->SubtractSymbol.Name = sub;
601 op_info->SubtractSymbol.Value = pair_r_value;
602 op_info->Value = offset;
606 if (reloc_found == false)
609 op_info->AddSymbol.Present = 1;
610 op_info->Value = offset;
612 if (r_type == MachO::ARM_RELOC_HALF) {
613 if ((r_length & 0x1) == 1)
614 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_HI16;
616 op_info->VariantKind = LLVMDisassembler_VariantKind_ARM_LO16;
619 const char *add = GuessSymbolName(value, info);
620 if (add != nullptr) {
621 op_info->AddSymbol.Name = add;
624 op_info->AddSymbol.Value = value;
626 } else if (Arch == Triple::aarch64) {
633 // GuessCstringPointer is passed the address of what might be a pointer to a
634 // literal string in a cstring section. If that address is in a cstring section
635 // it returns a pointer to that string. Else it returns nullptr.
636 const char *GuessCstringPointer(uint64_t ReferenceValue,
637 struct DisassembleInfo *info) {
638 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
639 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
640 for (unsigned I = 0;; ++I) {
641 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
642 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
643 for (unsigned J = 0; J < Seg.nsects; ++J) {
644 MachO::section_64 Sec = info->O->getSection64(Load, J);
645 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
646 if (section_type == MachO::S_CSTRING_LITERALS &&
647 ReferenceValue >= Sec.addr &&
648 ReferenceValue < Sec.addr + Sec.size) {
649 uint64_t sect_offset = ReferenceValue - Sec.addr;
650 uint64_t object_offset = Sec.offset + sect_offset;
651 StringRef MachOContents = info->O->getData();
652 uint64_t object_size = MachOContents.size();
653 const char *object_addr = (const char *)MachOContents.data();
654 if (object_offset < object_size) {
655 const char *name = object_addr + object_offset;
662 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
663 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
664 for (unsigned J = 0; J < Seg.nsects; ++J) {
665 MachO::section Sec = info->O->getSection(Load, J);
666 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
667 if (section_type == MachO::S_CSTRING_LITERALS &&
668 ReferenceValue >= Sec.addr &&
669 ReferenceValue < Sec.addr + Sec.size) {
670 uint64_t sect_offset = ReferenceValue - Sec.addr;
671 uint64_t object_offset = Sec.offset + sect_offset;
672 StringRef MachOContents = info->O->getData();
673 uint64_t object_size = MachOContents.size();
674 const char *object_addr = (const char *)MachOContents.data();
675 if (object_offset < object_size) {
676 const char *name = object_addr + object_offset;
684 if (I == LoadCommandCount - 1)
687 Load = info->O->getNextLoadCommandInfo(Load);
692 // GuessIndirectSymbol returns the name of the indirect symbol for the
693 // ReferenceValue passed in or nullptr. This is used when ReferenceValue maybe
694 // an address of a symbol stub or a lazy or non-lazy pointer to associate the
695 // symbol name being referenced by the stub or pointer.
696 static const char *GuessIndirectSymbol(uint64_t ReferenceValue,
697 struct DisassembleInfo *info) {
698 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
699 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
700 MachO::dysymtab_command Dysymtab = info->O->getDysymtabLoadCommand();
701 MachO::symtab_command Symtab = info->O->getSymtabLoadCommand();
702 for (unsigned I = 0;; ++I) {
703 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
704 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
705 for (unsigned J = 0; J < Seg.nsects; ++J) {
706 MachO::section_64 Sec = info->O->getSection64(Load, J);
707 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
708 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
709 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
710 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
711 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
712 section_type == MachO::S_SYMBOL_STUBS) &&
713 ReferenceValue >= Sec.addr &&
714 ReferenceValue < Sec.addr + Sec.size) {
716 if (section_type == MachO::S_SYMBOL_STUBS)
717 stride = Sec.reserved2;
722 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
723 if (index < Dysymtab.nindirectsyms) {
724 uint32_t indirect_symbol =
725 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
726 if (indirect_symbol < Symtab.nsyms) {
727 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
728 SymbolRef Symbol = *Sym;
730 Symbol.getName(SymName);
731 const char *name = SymName.data();
737 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
738 MachO::segment_command Seg = info->O->getSegmentLoadCommand(Load);
739 for (unsigned J = 0; J < Seg.nsects; ++J) {
740 MachO::section Sec = info->O->getSection(Load, J);
741 uint32_t section_type = Sec.flags & MachO::SECTION_TYPE;
742 if ((section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
743 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
744 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
745 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS ||
746 section_type == MachO::S_SYMBOL_STUBS) &&
747 ReferenceValue >= Sec.addr &&
748 ReferenceValue < Sec.addr + Sec.size) {
750 if (section_type == MachO::S_SYMBOL_STUBS)
751 stride = Sec.reserved2;
756 uint32_t index = Sec.reserved1 + (ReferenceValue - Sec.addr) / stride;
757 if (index < Dysymtab.nindirectsyms) {
758 uint32_t indirect_symbol =
759 info->O->getIndirectSymbolTableEntry(Dysymtab, index);
760 if (indirect_symbol < Symtab.nsyms) {
761 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
762 SymbolRef Symbol = *Sym;
764 Symbol.getName(SymName);
765 const char *name = SymName.data();
772 if (I == LoadCommandCount - 1)
775 Load = info->O->getNextLoadCommandInfo(Load);
780 // method_reference() is called passing it the ReferenceName that might be
781 // a reference it to an Objective-C method call. If so then it allocates and
782 // assembles a method call string with the values last seen and saved in
783 // the DisassembleInfo's class_name and selector_name fields. This is saved
784 // into the method field of the info and any previous string is free'ed.
785 // Then the class_name field in the info is set to nullptr. The method call
786 // string is set into ReferenceName and ReferenceType is set to
787 // LLVMDisassembler_ReferenceType_Out_Objc_Message. If this not a method call
788 // then both ReferenceType and ReferenceName are left unchanged.
789 static void method_reference(struct DisassembleInfo *info,
790 uint64_t *ReferenceType,
791 const char **ReferenceName) {
792 if (*ReferenceName != nullptr) {
793 if (strcmp(*ReferenceName, "_objc_msgSend") == 0) {
794 if (info->selector_name != NULL) {
795 if (info->method != nullptr)
797 if (info->class_name != nullptr) {
798 info->method = (char *)malloc(5 + strlen(info->class_name) +
799 strlen(info->selector_name));
800 if (info->method != nullptr) {
801 strcpy(info->method, "+[");
802 strcat(info->method, info->class_name);
803 strcat(info->method, " ");
804 strcat(info->method, info->selector_name);
805 strcat(info->method, "]");
806 *ReferenceName = info->method;
807 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
810 info->method = (char *)malloc(9 + strlen(info->selector_name));
811 if (info->method != nullptr) {
812 strcpy(info->method, "-[%rdi ");
813 strcat(info->method, info->selector_name);
814 strcat(info->method, "]");
815 *ReferenceName = info->method;
816 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
819 info->class_name = nullptr;
821 } else if (strcmp(*ReferenceName, "_objc_msgSendSuper2") == 0) {
822 if (info->selector_name != NULL) {
823 if (info->method != nullptr)
825 info->method = (char *)malloc(17 + strlen(info->selector_name));
826 if (info->method != nullptr) {
827 strcpy(info->method, "-[[%rdi super] ");
828 strcat(info->method, info->selector_name);
829 strcat(info->method, "]");
830 *ReferenceName = info->method;
831 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message;
833 info->class_name = nullptr;
839 // GuessPointerPointer() is passed the address of what might be a pointer to
840 // a reference to an Objective-C class, selector, message ref or cfstring.
841 // If so the value of the pointer is returned and one of the booleans are set
842 // to true. If not zero is returned and all the booleans are set to false.
843 static uint64_t GuessPointerPointer(uint64_t ReferenceValue,
844 struct DisassembleInfo *info,
845 bool &classref, bool &selref, bool &msgref,
851 uint32_t LoadCommandCount = info->O->getHeader().ncmds;
852 MachOObjectFile::LoadCommandInfo Load = info->O->getFirstLoadCommandInfo();
853 for (unsigned I = 0;; ++I) {
854 if (Load.C.cmd == MachO::LC_SEGMENT_64) {
855 MachO::segment_command_64 Seg = info->O->getSegment64LoadCommand(Load);
856 for (unsigned J = 0; J < Seg.nsects; ++J) {
857 MachO::section_64 Sec = info->O->getSection64(Load, J);
858 if ((strncmp(Sec.sectname, "__objc_selrefs", 16) == 0 ||
859 strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
860 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0 ||
861 strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 ||
862 strncmp(Sec.sectname, "__cfstring", 16) == 0) &&
863 ReferenceValue >= Sec.addr &&
864 ReferenceValue < Sec.addr + Sec.size) {
865 uint64_t sect_offset = ReferenceValue - Sec.addr;
866 uint64_t object_offset = Sec.offset + sect_offset;
867 StringRef MachOContents = info->O->getData();
868 uint64_t object_size = MachOContents.size();
869 const char *object_addr = (const char *)MachOContents.data();
870 if (object_offset < object_size) {
871 uint64_t pointer_value;
872 memcpy(&pointer_value, object_addr + object_offset,
874 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
875 sys::swapByteOrder(pointer_value);
876 if (strncmp(Sec.sectname, "__objc_selrefs", 16) == 0)
878 else if (strncmp(Sec.sectname, "__objc_classrefs", 16) == 0 ||
879 strncmp(Sec.sectname, "__objc_superrefs", 16) == 0)
881 else if (strncmp(Sec.sectname, "__objc_msgrefs", 16) == 0 &&
882 ReferenceValue + 8 < Sec.addr + Sec.size) {
884 memcpy(&pointer_value, object_addr + object_offset + 8,
886 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
887 sys::swapByteOrder(pointer_value);
888 } else if (strncmp(Sec.sectname, "__cfstring", 16) == 0)
890 return pointer_value;
897 // TODO: Look for LC_SEGMENT for 32-bit Mach-O files.
898 if (I == LoadCommandCount - 1)
901 Load = info->O->getNextLoadCommandInfo(Load);
906 // get_pointer_64 returns a pointer to the bytes in the object file at the
907 // Address from a section in the Mach-O file. And indirectly returns the
908 // offset into the section, number of bytes left in the section past the offset
909 // and which section is was being referenced. If the Address is not in a
910 // section nullptr is returned.
911 const char *get_pointer_64(uint64_t Address, uint32_t &offset, uint32_t &left,
912 SectionRef &S, DisassembleInfo *info) {
916 for (unsigned SectIdx = 0; SectIdx != info->Sections->size(); SectIdx++) {
917 uint64_t SectAddress = ((*(info->Sections))[SectIdx]).getAddress();
918 uint64_t SectSize = ((*(info->Sections))[SectIdx]).getSize();
919 if (Address >= SectAddress && Address < SectAddress + SectSize) {
920 S = (*(info->Sections))[SectIdx];
921 offset = Address - SectAddress;
922 left = SectSize - offset;
923 StringRef SectContents;
924 ((*(info->Sections))[SectIdx]).getContents(SectContents);
925 return SectContents.data() + offset;
931 // get_symbol_64() returns the name of a symbol (or nullptr) and the address of
932 // the symbol indirectly through n_value. Based on the relocation information
933 // for the specified section offset in the specified section reference.
934 const char *get_symbol_64(uint32_t sect_offset, SectionRef S,
935 DisassembleInfo *info, uint64_t &n_value) {
937 if (info->verbose == false)
940 // See if there is an external relocation entry at the sect_offset.
941 bool reloc_found = false;
943 MachO::any_relocation_info RE;
944 bool isExtern = false;
946 for (const RelocationRef &Reloc : S.relocations()) {
947 uint64_t RelocOffset;
948 Reloc.getOffset(RelocOffset);
949 if (RelocOffset == sect_offset) {
950 Rel = Reloc.getRawDataRefImpl();
951 RE = info->O->getRelocation(Rel);
952 if (info->O->isRelocationScattered(RE))
954 isExtern = info->O->getPlainRelocationExternal(RE);
956 symbol_iterator RelocSym = Reloc.getSymbol();
963 // If there is an external relocation entry for a symbol in this section
964 // at this section_offset then use that symbol's value for the n_value
965 // and return its name.
966 const char *SymbolName = nullptr;
967 if (reloc_found && isExtern) {
968 Symbol.getAddress(n_value);
970 Symbol.getName(name);
972 SymbolName = name.data();
977 // TODO: For fully linked images, look through the external relocation
978 // entries off the dynamic symtab command. For these the r_offset is from the
979 // start of the first writeable segment in the Mach-O file. So the offset
980 // to this section from that segment is passed to this routine by the caller,
981 // as the database_offset. Which is the difference of the section's starting
982 // address and the first writable segment.
984 // NOTE: need add passing the database_offset to this routine.
986 // TODO: We did not find an external relocation entry so look up the
987 // ReferenceValue as an address of a symbol and if found return that symbol's
990 // NOTE: need add passing the ReferenceValue to this routine. Then that code
991 // would simply be this:
992 // SymbolName = GuessSymbolName(ReferenceValue, info);
997 // These are structs in the Objective-C meta data and read to produce the
998 // comments for disassembly. While these are part of the ABI they are no
999 // public defintions. So the are here not in include/llvm/Support/MachO.h .
1001 // The cfstring object in a 64-bit Mach-O file.
1002 struct cfstring64_t {
1003 uint64_t isa; // class64_t * (64-bit pointer)
1004 uint64_t flags; // flag bits
1005 uint64_t characters; // char * (64-bit pointer)
1006 uint64_t length; // number of non-NULL characters in above
1009 // The class object in a 64-bit Mach-O file.
1011 uint64_t isa; // class64_t * (64-bit pointer)
1012 uint64_t superclass; // class64_t * (64-bit pointer)
1013 uint64_t cache; // Cache (64-bit pointer)
1014 uint64_t vtable; // IMP * (64-bit pointer)
1015 uint64_t data; // class_ro64_t * (64-bit pointer)
1018 struct class_ro64_t {
1020 uint32_t instanceStart;
1021 uint32_t instanceSize;
1023 uint64_t ivarLayout; // const uint8_t * (64-bit pointer)
1024 uint64_t name; // const char * (64-bit pointer)
1025 uint64_t baseMethods; // const method_list_t * (64-bit pointer)
1026 uint64_t baseProtocols; // const protocol_list_t * (64-bit pointer)
1027 uint64_t ivars; // const ivar_list_t * (64-bit pointer)
1028 uint64_t weakIvarLayout; // const uint8_t * (64-bit pointer)
1029 uint64_t baseProperties; // const struct objc_property_list (64-bit pointer)
1032 inline void swapStruct(struct cfstring64_t &cfs) {
1033 sys::swapByteOrder(cfs.isa);
1034 sys::swapByteOrder(cfs.flags);
1035 sys::swapByteOrder(cfs.characters);
1036 sys::swapByteOrder(cfs.length);
1039 inline void swapStruct(struct class64_t &c) {
1040 sys::swapByteOrder(c.isa);
1041 sys::swapByteOrder(c.superclass);
1042 sys::swapByteOrder(c.cache);
1043 sys::swapByteOrder(c.vtable);
1044 sys::swapByteOrder(c.data);
1047 inline void swapStruct(struct class_ro64_t &cro) {
1048 sys::swapByteOrder(cro.flags);
1049 sys::swapByteOrder(cro.instanceStart);
1050 sys::swapByteOrder(cro.instanceSize);
1051 sys::swapByteOrder(cro.reserved);
1052 sys::swapByteOrder(cro.ivarLayout);
1053 sys::swapByteOrder(cro.name);
1054 sys::swapByteOrder(cro.baseMethods);
1055 sys::swapByteOrder(cro.baseProtocols);
1056 sys::swapByteOrder(cro.ivars);
1057 sys::swapByteOrder(cro.weakIvarLayout);
1058 sys::swapByteOrder(cro.baseProperties);
1061 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
1062 struct DisassembleInfo *info);
1064 // get_objc2_64bit_class_name() is used for disassembly and is passed a pointer
1065 // to an Objective-C class and returns the class name. It is also passed the
1066 // address of the pointer, so when the pointer is zero as it can be in an .o
1067 // file, that is used to look for an external relocation entry with a symbol
1069 const char *get_objc2_64bit_class_name(uint64_t pointer_value,
1070 uint64_t ReferenceValue,
1071 struct DisassembleInfo *info) {
1073 uint32_t offset, left;
1076 // The pointer_value can be 0 in an object file and have a relocation
1077 // entry for the class symbol at the ReferenceValue (the address of the
1079 if (pointer_value == 0) {
1080 r = get_pointer_64(ReferenceValue, offset, left, S, info);
1081 if (r == nullptr || left < sizeof(uint64_t))
1084 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
1085 if (symbol_name == nullptr)
1087 const char *class_name = strrchr(symbol_name, '$');
1088 if (class_name != nullptr && class_name[1] == '_' && class_name[2] != '\0')
1089 return class_name + 2;
1094 // The case were the pointer_value is non-zero and points to a class defined
1095 // in this Mach-O file.
1096 r = get_pointer_64(pointer_value, offset, left, S, info);
1097 if (r == nullptr || left < sizeof(struct class64_t))
1100 memcpy(&c, r, sizeof(struct class64_t));
1101 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
1105 r = get_pointer_64(c.data, offset, left, S, info);
1106 if (r == nullptr || left < sizeof(struct class_ro64_t))
1108 struct class_ro64_t cro;
1109 memcpy(&cro, r, sizeof(struct class_ro64_t));
1110 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
1114 const char *name = get_pointer_64(cro.name, offset, left, S, info);
1118 // get_objc2_64bit_cfstring_name is used for disassembly and is passed a
1119 // pointer to a cfstring and returns its name or nullptr.
1120 const char *get_objc2_64bit_cfstring_name(uint64_t ReferenceValue,
1121 struct DisassembleInfo *info) {
1122 const char *r, *name;
1123 uint32_t offset, left;
1125 struct cfstring64_t cfs;
1126 uint64_t cfs_characters;
1128 r = get_pointer_64(ReferenceValue, offset, left, S, info);
1129 if (r == nullptr || left < sizeof(struct cfstring64_t))
1131 memcpy(&cfs, r, sizeof(struct cfstring64_t));
1132 if (info->O->isLittleEndian() != sys::IsLittleEndianHost)
1134 if (cfs.characters == 0) {
1136 const char *symbol_name = get_symbol_64(
1137 offset + offsetof(struct cfstring64_t, characters), S, info, n_value);
1138 if (symbol_name == nullptr)
1140 cfs_characters = n_value;
1142 cfs_characters = cfs.characters;
1143 name = get_pointer_64(cfs_characters, offset, left, S, info);
1148 // get_objc2_64bit_selref() is used for disassembly and is passed a the address
1149 // of a pointer to an Objective-C selector reference when the pointer value is
1150 // zero as in a .o file and is likely to have a external relocation entry with
1151 // who's symbol's n_value is the real pointer to the selector name. If that is
1152 // the case the real pointer to the selector name is returned else 0 is
1154 uint64_t get_objc2_64bit_selref(uint64_t ReferenceValue,
1155 struct DisassembleInfo *info) {
1156 uint32_t offset, left;
1159 const char *r = get_pointer_64(ReferenceValue, offset, left, S, info);
1160 if (r == nullptr || left < sizeof(uint64_t))
1163 const char *symbol_name = get_symbol_64(offset, S, info, n_value);
1164 if (symbol_name == nullptr)
1169 // GuessLiteralPointer returns a string which for the item in the Mach-O file
1170 // for the address passed in as ReferenceValue for printing as a comment with
1171 // the instruction and also returns the corresponding type of that item
1172 // indirectly through ReferenceType.
1174 // If ReferenceValue is an address of literal cstring then a pointer to the
1175 // cstring is returned and ReferenceType is set to
1176 // LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr .
1178 // If ReferenceValue is an address of an Objective-C CFString, Selector ref or
1179 // Class ref that name is returned and the ReferenceType is set accordingly.
1181 // Lastly, literals which are Symbol address in a literal pool are looked for
1182 // and if found the symbol name is returned and ReferenceType is set to
1183 // LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr .
1185 // If there is no item in the Mach-O file for the address passed in as
1186 // ReferenceValue nullptr is returned and ReferenceType is unchanged.
1187 const char *GuessLiteralPointer(uint64_t ReferenceValue, uint64_t ReferencePC,
1188 uint64_t *ReferenceType,
1189 struct DisassembleInfo *info) {
1190 // TODO: This rouine's code and the routines it calls are only work with
1191 // x86_64 Mach-O files for now.
1192 unsigned int Arch = info->O->getArch();
1193 if (Arch != Triple::x86_64)
1196 // First see if there is an external relocation entry at the ReferencePC.
1197 uint64_t sect_addr = info->S.getAddress();
1198 uint64_t sect_offset = ReferencePC - sect_addr;
1199 bool reloc_found = false;
1201 MachO::any_relocation_info RE;
1202 bool isExtern = false;
1204 for (const RelocationRef &Reloc : info->S.relocations()) {
1205 uint64_t RelocOffset;
1206 Reloc.getOffset(RelocOffset);
1207 if (RelocOffset == sect_offset) {
1208 Rel = Reloc.getRawDataRefImpl();
1209 RE = info->O->getRelocation(Rel);
1210 if (info->O->isRelocationScattered(RE))
1212 isExtern = info->O->getPlainRelocationExternal(RE);
1214 symbol_iterator RelocSym = Reloc.getSymbol();
1221 // If there is an external relocation entry for a symbol in a section
1222 // then used that symbol's value for the value of the reference.
1223 if (reloc_found && isExtern) {
1224 if (info->O->getAnyRelocationPCRel(RE)) {
1225 unsigned Type = info->O->getAnyRelocationType(RE);
1226 if (Type == MachO::X86_64_RELOC_SIGNED) {
1227 Symbol.getAddress(ReferenceValue);
1232 // Look for literals such as Objective-C CFStrings refs, Selector refs,
1233 // Message refs and Class refs.
1234 bool classref, selref, msgref, cfstring;
1235 uint64_t pointer_value = GuessPointerPointer(ReferenceValue, info, classref,
1236 selref, msgref, cfstring);
1237 if (classref == true && pointer_value == 0) {
1238 // Note the ReferenceValue is a pointer into the __objc_classrefs section.
1239 // And the pointer_value in that section is typically zero as it will be
1240 // set by dyld as part of the "bind information".
1241 const char *name = get_dyld_bind_info_symbolname(ReferenceValue, info);
1242 if (name != nullptr) {
1243 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
1244 const char *class_name = strrchr(name, '$');
1245 if (class_name != nullptr && class_name[1] == '_' &&
1246 class_name[2] != '\0') {
1247 info->class_name = class_name + 2;
1253 if (classref == true) {
1254 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Class_Ref;
1256 get_objc2_64bit_class_name(pointer_value, ReferenceValue, info);
1257 if (name != nullptr)
1258 info->class_name = name;
1260 name = "bad class ref";
1264 if (cfstring == true) {
1265 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_CFString_Ref;
1266 const char *name = get_objc2_64bit_cfstring_name(ReferenceValue, info);
1270 if (selref == true && pointer_value == 0)
1271 pointer_value = get_objc2_64bit_selref(ReferenceValue, info);
1273 if (pointer_value != 0)
1274 ReferenceValue = pointer_value;
1276 const char *name = GuessCstringPointer(ReferenceValue, info);
1278 if (pointer_value != 0 && selref == true) {
1279 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Selector_Ref;
1280 info->selector_name = name;
1281 } else if (pointer_value != 0 && msgref == true) {
1282 info->class_name = nullptr;
1283 *ReferenceType = LLVMDisassembler_ReferenceType_Out_Objc_Message_Ref;
1284 info->selector_name = name;
1286 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_CstrAddr;
1290 // Lastly look for an indirect symbol with this ReferenceValue which is in
1291 // a literal pool. If found return that symbol name.
1292 name = GuessIndirectSymbol(ReferenceValue, info);
1294 *ReferenceType = LLVMDisassembler_ReferenceType_Out_LitPool_SymAddr;
1301 // SymbolizerSymbolLookUp is the symbol lookup function passed when creating
1302 // the Symbolizer. It looks up the ReferenceValue using the info passed via the
1303 // pointer to the struct DisassembleInfo that was passed when MCSymbolizer
1304 // is created and returns the symbol name that matches the ReferenceValue or
1305 // nullptr if none. The ReferenceType is passed in for the IN type of
1306 // reference the instruction is making from the values in defined in the header
1307 // "llvm-c/Disassembler.h". On return the ReferenceType can set to a specific
1308 // Out type and the ReferenceName will also be set which is added as a comment
1309 // to the disassembled instruction.
1312 // If the symbol name is a C++ mangled name then the demangled name is
1313 // returned through ReferenceName and ReferenceType is set to
1314 // LLVMDisassembler_ReferenceType_DeMangled_Name .
1317 // When this is called to get a symbol name for a branch target then the
1318 // ReferenceType will be LLVMDisassembler_ReferenceType_In_Branch and then
1319 // SymbolValue will be looked for in the indirect symbol table to determine if
1320 // it is an address for a symbol stub. If so then the symbol name for that
1321 // stub is returned indirectly through ReferenceName and then ReferenceType is
1322 // set to LLVMDisassembler_ReferenceType_Out_SymbolStub.
1324 // When this is called with an value loaded via a PC relative load then
1325 // ReferenceType will be LLVMDisassembler_ReferenceType_In_PCrel_Load then the
1326 // SymbolValue is checked to be an address of literal pointer, symbol pointer,
1327 // or an Objective-C meta data reference. If so the output ReferenceType is
1328 // set to correspond to that as well as setting the ReferenceName.
1329 const char *SymbolizerSymbolLookUp(void *DisInfo, uint64_t ReferenceValue,
1330 uint64_t *ReferenceType,
1331 uint64_t ReferencePC,
1332 const char **ReferenceName) {
1333 struct DisassembleInfo *info = (struct DisassembleInfo *)DisInfo;
1334 // If no verbose symbolic information is wanted then just return nullptr.
1335 if (info->verbose == false) {
1336 *ReferenceName = nullptr;
1337 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
1341 const char *SymbolName = GuessSymbolName(ReferenceValue, info);
1343 if (*ReferenceType == LLVMDisassembler_ReferenceType_In_Branch) {
1344 *ReferenceName = GuessIndirectSymbol(ReferenceValue, info);
1345 if (*ReferenceName != nullptr) {
1346 method_reference(info, ReferenceType, ReferenceName);
1347 if (*ReferenceType != LLVMDisassembler_ReferenceType_Out_Objc_Message)
1348 *ReferenceType = LLVMDisassembler_ReferenceType_Out_SymbolStub;
1351 if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
1352 if (info->demangled_name != nullptr)
1353 free(info->demangled_name);
1355 info->demangled_name =
1356 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
1357 if (info->demangled_name != nullptr) {
1358 *ReferenceName = info->demangled_name;
1359 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
1361 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
1364 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
1365 } else if (*ReferenceType == LLVMDisassembler_ReferenceType_In_PCrel_Load) {
1367 GuessLiteralPointer(ReferenceValue, ReferencePC, ReferenceType, info);
1369 method_reference(info, ReferenceType, ReferenceName);
1371 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
1374 else if (SymbolName != nullptr && strncmp(SymbolName, "__Z", 3) == 0) {
1375 if (info->demangled_name != nullptr)
1376 free(info->demangled_name);
1378 info->demangled_name =
1379 abi::__cxa_demangle(SymbolName + 1, nullptr, nullptr, &status);
1380 if (info->demangled_name != nullptr) {
1381 *ReferenceName = info->demangled_name;
1382 *ReferenceType = LLVMDisassembler_ReferenceType_DeMangled_Name;
1387 *ReferenceName = nullptr;
1388 *ReferenceType = LLVMDisassembler_ReferenceType_InOut_None;
1394 /// \brief Emits the comments that are stored in the CommentStream.
1395 /// Each comment in the CommentStream must end with a newline.
1396 static void emitComments(raw_svector_ostream &CommentStream,
1397 SmallString<128> &CommentsToEmit,
1398 formatted_raw_ostream &FormattedOS,
1399 const MCAsmInfo &MAI) {
1400 // Flush the stream before taking its content.
1401 CommentStream.flush();
1402 StringRef Comments = CommentsToEmit.str();
1403 // Get the default information for printing a comment.
1404 const char *CommentBegin = MAI.getCommentString();
1405 unsigned CommentColumn = MAI.getCommentColumn();
1406 bool IsFirst = true;
1407 while (!Comments.empty()) {
1409 FormattedOS << '\n';
1410 // Emit a line of comments.
1411 FormattedOS.PadToColumn(CommentColumn);
1412 size_t Position = Comments.find('\n');
1413 FormattedOS << CommentBegin << ' ' << Comments.substr(0, Position);
1414 // Move after the newline character.
1415 Comments = Comments.substr(Position + 1);
1418 FormattedOS.flush();
1420 // Tell the comment stream that the vector changed underneath it.
1421 CommentsToEmit.clear();
1422 CommentStream.resync();
1425 static void DisassembleInputMachO2(StringRef Filename,
1426 MachOObjectFile *MachOOF) {
1427 const char *McpuDefault = nullptr;
1428 const Target *ThumbTarget = nullptr;
1429 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
1431 // GetTarget prints out stuff.
1434 if (MCPU.empty() && McpuDefault)
1437 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
1438 std::unique_ptr<MCInstrAnalysis> InstrAnalysis(
1439 TheTarget->createMCInstrAnalysis(InstrInfo.get()));
1440 std::unique_ptr<const MCInstrInfo> ThumbInstrInfo;
1441 std::unique_ptr<MCInstrAnalysis> ThumbInstrAnalysis;
1443 ThumbInstrInfo.reset(ThumbTarget->createMCInstrInfo());
1444 ThumbInstrAnalysis.reset(
1445 ThumbTarget->createMCInstrAnalysis(ThumbInstrInfo.get()));
1448 // Package up features to be passed to target/subtarget
1449 std::string FeaturesStr;
1450 if (MAttrs.size()) {
1451 SubtargetFeatures Features;
1452 for (unsigned i = 0; i != MAttrs.size(); ++i)
1453 Features.AddFeature(MAttrs[i]);
1454 FeaturesStr = Features.getString();
1457 // Set up disassembler.
1458 std::unique_ptr<const MCRegisterInfo> MRI(
1459 TheTarget->createMCRegInfo(TripleName));
1460 std::unique_ptr<const MCAsmInfo> AsmInfo(
1461 TheTarget->createMCAsmInfo(*MRI, TripleName));
1462 std::unique_ptr<const MCSubtargetInfo> STI(
1463 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
1464 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
1465 std::unique_ptr<MCDisassembler> DisAsm(
1466 TheTarget->createMCDisassembler(*STI, Ctx));
1467 std::unique_ptr<MCSymbolizer> Symbolizer;
1468 struct DisassembleInfo SymbolizerInfo;
1469 std::unique_ptr<MCRelocationInfo> RelInfo(
1470 TheTarget->createMCRelocationInfo(TripleName, Ctx));
1472 Symbolizer.reset(TheTarget->createMCSymbolizer(
1473 TripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
1474 &SymbolizerInfo, &Ctx, RelInfo.release()));
1475 DisAsm->setSymbolizer(std::move(Symbolizer));
1477 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
1478 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
1479 AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI, *STI));
1480 // Set the display preference for hex vs. decimal immediates.
1481 IP->setPrintImmHex(PrintImmHex);
1482 // Comment stream and backing vector.
1483 SmallString<128> CommentsToEmit;
1484 raw_svector_ostream CommentStream(CommentsToEmit);
1485 IP->setCommentStream(CommentStream);
1487 if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) {
1488 errs() << "error: couldn't initialize disassembler for target "
1489 << TripleName << '\n';
1493 // Set up thumb disassembler.
1494 std::unique_ptr<const MCRegisterInfo> ThumbMRI;
1495 std::unique_ptr<const MCAsmInfo> ThumbAsmInfo;
1496 std::unique_ptr<const MCSubtargetInfo> ThumbSTI;
1497 std::unique_ptr<MCDisassembler> ThumbDisAsm;
1498 std::unique_ptr<MCInstPrinter> ThumbIP;
1499 std::unique_ptr<MCContext> ThumbCtx;
1500 std::unique_ptr<MCSymbolizer> ThumbSymbolizer;
1501 struct DisassembleInfo ThumbSymbolizerInfo;
1502 std::unique_ptr<MCRelocationInfo> ThumbRelInfo;
1504 ThumbMRI.reset(ThumbTarget->createMCRegInfo(ThumbTripleName));
1506 ThumbTarget->createMCAsmInfo(*ThumbMRI, ThumbTripleName));
1508 ThumbTarget->createMCSubtargetInfo(ThumbTripleName, MCPU, FeaturesStr));
1509 ThumbCtx.reset(new MCContext(ThumbAsmInfo.get(), ThumbMRI.get(), nullptr));
1510 ThumbDisAsm.reset(ThumbTarget->createMCDisassembler(*ThumbSTI, *ThumbCtx));
1511 MCContext *PtrThumbCtx = ThumbCtx.get();
1513 ThumbTarget->createMCRelocationInfo(ThumbTripleName, *PtrThumbCtx));
1515 ThumbSymbolizer.reset(ThumbTarget->createMCSymbolizer(
1516 ThumbTripleName, SymbolizerGetOpInfo, SymbolizerSymbolLookUp,
1517 &ThumbSymbolizerInfo, PtrThumbCtx, ThumbRelInfo.release()));
1518 ThumbDisAsm->setSymbolizer(std::move(ThumbSymbolizer));
1520 int ThumbAsmPrinterVariant = ThumbAsmInfo->getAssemblerDialect();
1521 ThumbIP.reset(ThumbTarget->createMCInstPrinter(
1522 ThumbAsmPrinterVariant, *ThumbAsmInfo, *ThumbInstrInfo, *ThumbMRI,
1524 // Set the display preference for hex vs. decimal immediates.
1525 ThumbIP->setPrintImmHex(PrintImmHex);
1528 if (ThumbTarget && (!ThumbInstrAnalysis || !ThumbAsmInfo || !ThumbSTI ||
1529 !ThumbDisAsm || !ThumbIP)) {
1530 errs() << "error: couldn't initialize disassembler for target "
1531 << ThumbTripleName << '\n';
1535 outs() << '\n' << Filename << ":\n\n";
1537 MachO::mach_header Header = MachOOF->getHeader();
1539 // FIXME: Using the -cfg command line option, this code used to be able to
1540 // annotate relocations with the referenced symbol's name, and if this was
1541 // inside a __[cf]string section, the data it points to. This is now replaced
1542 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
1543 std::vector<SectionRef> Sections;
1544 std::vector<SymbolRef> Symbols;
1545 SmallVector<uint64_t, 8> FoundFns;
1546 uint64_t BaseSegmentAddress;
1548 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
1549 BaseSegmentAddress);
1551 // Sort the symbols by address, just in case they didn't come in that way.
1552 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
1554 // Build a data in code table that is sorted on by the address of each entry.
1555 uint64_t BaseAddress = 0;
1556 if (Header.filetype == MachO::MH_OBJECT)
1557 BaseAddress = Sections[0].getAddress();
1559 BaseAddress = BaseSegmentAddress;
1561 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
1564 DI->getOffset(Offset);
1565 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
1567 array_pod_sort(Dices.begin(), Dices.end());
1570 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
1572 raw_ostream &DebugOut = nulls();
1575 std::unique_ptr<DIContext> diContext;
1576 ObjectFile *DbgObj = MachOOF;
1577 // Try to find debug info and set up the DIContext for it.
1579 // A separate DSym file path was specified, parse it as a macho file,
1580 // get the sections and supply it to the section name parsing machinery.
1581 if (!DSYMFile.empty()) {
1582 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
1583 MemoryBuffer::getFileOrSTDIN(DSYMFile);
1584 if (std::error_code EC = BufOrErr.getError()) {
1585 errs() << "llvm-objdump: " << Filename << ": " << EC.message() << '\n';
1589 ObjectFile::createMachOObjectFile(BufOrErr.get()->getMemBufferRef())
1594 // Setup the DIContext
1595 diContext.reset(DIContext::getDWARFContext(*DbgObj));
1598 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
1600 bool SectIsText = Sections[SectIdx].isText();
1601 if (SectIsText == false)
1605 if (Sections[SectIdx].getName(SectName) || SectName != "__text")
1606 continue; // Skip non-text sections
1608 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
1610 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
1611 if (SegmentName != "__TEXT")
1615 Sections[SectIdx].getContents(BytesStr);
1616 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
1618 uint64_t SectAddress = Sections[SectIdx].getAddress();
1620 bool symbolTableWorked = false;
1622 // Parse relocations.
1623 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
1624 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
1625 uint64_t RelocOffset;
1626 Reloc.getOffset(RelocOffset);
1627 uint64_t SectionAddress = Sections[SectIdx].getAddress();
1628 RelocOffset -= SectionAddress;
1630 symbol_iterator RelocSym = Reloc.getSymbol();
1632 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1634 array_pod_sort(Relocs.begin(), Relocs.end());
1636 // Create a map of symbol addresses to symbol names for use by
1637 // the SymbolizerSymbolLookUp() routine.
1638 SymbolAddressMap AddrMap;
1639 for (const SymbolRef &Symbol : MachOOF->symbols()) {
1642 if (ST == SymbolRef::ST_Function || ST == SymbolRef::ST_Data ||
1643 ST == SymbolRef::ST_Other) {
1645 Symbol.getAddress(Address);
1647 Symbol.getName(SymName);
1648 AddrMap[Address] = SymName;
1651 // Set up the block of info used by the Symbolizer call backs.
1652 SymbolizerInfo.verbose = true;
1653 SymbolizerInfo.O = MachOOF;
1654 SymbolizerInfo.S = Sections[SectIdx];
1655 SymbolizerInfo.AddrMap = &AddrMap;
1656 SymbolizerInfo.Sections = &Sections;
1657 SymbolizerInfo.class_name = nullptr;
1658 SymbolizerInfo.selector_name = nullptr;
1659 SymbolizerInfo.method = nullptr;
1660 SymbolizerInfo.demangled_name = nullptr;
1661 SymbolizerInfo.bindtable = nullptr;
1662 // Same for the ThumbSymbolizer
1663 ThumbSymbolizerInfo.verbose = true;
1664 ThumbSymbolizerInfo.O = MachOOF;
1665 ThumbSymbolizerInfo.S = Sections[SectIdx];
1666 ThumbSymbolizerInfo.AddrMap = &AddrMap;
1667 ThumbSymbolizerInfo.Sections = &Sections;
1668 ThumbSymbolizerInfo.class_name = nullptr;
1669 ThumbSymbolizerInfo.selector_name = nullptr;
1670 ThumbSymbolizerInfo.method = nullptr;
1671 ThumbSymbolizerInfo.demangled_name = nullptr;
1672 ThumbSymbolizerInfo.bindtable = nullptr;
1674 // Disassemble symbol by symbol.
1675 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
1677 Symbols[SymIdx].getName(SymName);
1680 Symbols[SymIdx].getType(ST);
1681 if (ST != SymbolRef::ST_Function)
1684 // Make sure the symbol is defined in this section.
1685 bool containsSym = Sections[SectIdx].containsSymbol(Symbols[SymIdx]);
1689 // Start at the address of the symbol relative to the section's address.
1691 uint64_t SectionAddress = Sections[SectIdx].getAddress();
1692 Symbols[SymIdx].getAddress(Start);
1693 Start -= SectionAddress;
1695 // Stop disassembling either at the beginning of the next symbol or at
1696 // the end of the section.
1697 bool containsNextSym = false;
1698 uint64_t NextSym = 0;
1699 uint64_t NextSymIdx = SymIdx + 1;
1700 while (Symbols.size() > NextSymIdx) {
1701 SymbolRef::Type NextSymType;
1702 Symbols[NextSymIdx].getType(NextSymType);
1703 if (NextSymType == SymbolRef::ST_Function) {
1705 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx]);
1706 Symbols[NextSymIdx].getAddress(NextSym);
1707 NextSym -= SectionAddress;
1713 uint64_t SectSize = Sections[SectIdx].getSize();
1714 uint64_t End = containsNextSym ? NextSym : SectSize;
1717 symbolTableWorked = true;
1719 DataRefImpl Symb = Symbols[SymIdx].getRawDataRefImpl();
1721 (MachOOF->getSymbolFlags(Symb) & SymbolRef::SF_Thumb) && ThumbTarget;
1723 outs() << SymName << ":\n";
1724 DILineInfo lastLine;
1725 for (uint64_t Index = Start; Index < End; Index += Size) {
1728 uint64_t PC = SectAddress + Index;
1729 if (FullLeadingAddr) {
1730 if (MachOOF->is64Bit())
1731 outs() << format("%016" PRIx64, PC);
1733 outs() << format("%08" PRIx64, PC);
1735 outs() << format("%8" PRIx64 ":", PC);
1740 // Check the data in code table here to see if this is data not an
1741 // instruction to be disassembled.
1743 Dice.push_back(std::make_pair(PC, DiceRef()));
1744 dice_table_iterator DTI =
1745 std::search(Dices.begin(), Dices.end(), Dice.begin(), Dice.end(),
1746 compareDiceTableEntries);
1747 if (DTI != Dices.end()) {
1749 DTI->second.getLength(Length);
1751 DTI->second.getKind(Kind);
1752 Size = DumpDataInCode(reinterpret_cast<const char *>(Bytes.data()) +
1755 if ((Kind == MachO::DICE_KIND_JUMP_TABLE8) &&
1756 (PC == (DTI->first + Length - 1)) && (Length & 1))
1761 SmallVector<char, 64> AnnotationsBytes;
1762 raw_svector_ostream Annotations(AnnotationsBytes);
1766 gotInst = ThumbDisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
1767 PC, DebugOut, Annotations);
1769 gotInst = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), PC,
1770 DebugOut, Annotations);
1772 if (!NoShowRawInsn) {
1773 DumpBytes(StringRef(
1774 reinterpret_cast<const char *>(Bytes.data()) + Index, Size));
1776 formatted_raw_ostream FormattedOS(outs());
1777 Annotations.flush();
1778 StringRef AnnotationsStr = Annotations.str();
1780 ThumbIP->printInst(&Inst, FormattedOS, AnnotationsStr);
1782 IP->printInst(&Inst, FormattedOS, AnnotationsStr);
1783 emitComments(CommentStream, CommentsToEmit, FormattedOS, *AsmInfo);
1785 // Print debug info.
1787 DILineInfo dli = diContext->getLineInfoForAddress(PC);
1788 // Print valid line info if it changed.
1789 if (dli != lastLine && dli.Line != 0)
1790 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
1796 unsigned int Arch = MachOOF->getArch();
1797 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
1798 outs() << format("\t.byte 0x%02x #bad opcode\n",
1799 *(Bytes.data() + Index) & 0xff);
1800 Size = 1; // skip exactly one illegible byte and move on.
1802 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
1804 Size = 1; // skip illegible bytes
1809 if (!symbolTableWorked) {
1810 // Reading the symbol table didn't work, disassemble the whole section.
1811 uint64_t SectAddress = Sections[SectIdx].getAddress();
1812 uint64_t SectSize = Sections[SectIdx].getSize();
1814 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
1817 uint64_t PC = SectAddress + Index;
1818 if (DisAsm->getInstruction(Inst, InstSize, Bytes.slice(Index), PC,
1819 DebugOut, nulls())) {
1820 if (FullLeadingAddr) {
1821 if (MachOOF->is64Bit())
1822 outs() << format("%016" PRIx64, PC);
1824 outs() << format("%08" PRIx64, PC);
1826 outs() << format("%8" PRIx64 ":", PC);
1828 if (!NoShowRawInsn) {
1831 StringRef(reinterpret_cast<const char *>(Bytes.data()) + Index,
1834 IP->printInst(&Inst, outs(), "");
1837 unsigned int Arch = MachOOF->getArch();
1838 if (Arch == Triple::x86_64 || Arch == Triple::x86) {
1839 outs() << format("\t.byte 0x%02x #bad opcode\n",
1840 *(Bytes.data() + Index) & 0xff);
1841 InstSize = 1; // skip exactly one illegible byte and move on.
1843 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
1845 InstSize = 1; // skip illegible bytes
1850 if (SymbolizerInfo.method != nullptr)
1851 free(SymbolizerInfo.method);
1852 if (SymbolizerInfo.demangled_name != nullptr)
1853 free(SymbolizerInfo.demangled_name);
1854 if (SymbolizerInfo.bindtable != nullptr)
1855 delete SymbolizerInfo.bindtable;
1856 if (ThumbSymbolizerInfo.method != nullptr)
1857 free(ThumbSymbolizerInfo.method);
1858 if (ThumbSymbolizerInfo.demangled_name != nullptr)
1859 free(ThumbSymbolizerInfo.demangled_name);
1860 if (ThumbSymbolizerInfo.bindtable != nullptr)
1861 delete ThumbSymbolizerInfo.bindtable;
1865 //===----------------------------------------------------------------------===//
1866 // __compact_unwind section dumping
1867 //===----------------------------------------------------------------------===//
1871 template <typename T> static uint64_t readNext(const char *&Buf) {
1872 using llvm::support::little;
1873 using llvm::support::unaligned;
1875 uint64_t Val = support::endian::read<T, little, unaligned>(Buf);
1880 struct CompactUnwindEntry {
1881 uint32_t OffsetInSection;
1883 uint64_t FunctionAddr;
1885 uint32_t CompactEncoding;
1886 uint64_t PersonalityAddr;
1889 RelocationRef FunctionReloc;
1890 RelocationRef PersonalityReloc;
1891 RelocationRef LSDAReloc;
1893 CompactUnwindEntry(StringRef Contents, unsigned Offset, bool Is64)
1894 : OffsetInSection(Offset) {
1896 read<uint64_t>(Contents.data() + Offset);
1898 read<uint32_t>(Contents.data() + Offset);
1902 template <typename UIntPtr> void read(const char *Buf) {
1903 FunctionAddr = readNext<UIntPtr>(Buf);
1904 Length = readNext<uint32_t>(Buf);
1905 CompactEncoding = readNext<uint32_t>(Buf);
1906 PersonalityAddr = readNext<UIntPtr>(Buf);
1907 LSDAAddr = readNext<UIntPtr>(Buf);
1912 /// Given a relocation from __compact_unwind, consisting of the RelocationRef
1913 /// and data being relocated, determine the best base Name and Addend to use for
1914 /// display purposes.
1916 /// 1. An Extern relocation will directly reference a symbol (and the data is
1917 /// then already an addend), so use that.
1918 /// 2. Otherwise the data is an offset in the object file's layout; try to find
1919 // a symbol before it in the same section, and use the offset from there.
1920 /// 3. Finally, if all that fails, fall back to an offset from the start of the
1921 /// referenced section.
1922 static void findUnwindRelocNameAddend(const MachOObjectFile *Obj,
1923 std::map<uint64_t, SymbolRef> &Symbols,
1924 const RelocationRef &Reloc, uint64_t Addr,
1925 StringRef &Name, uint64_t &Addend) {
1926 if (Reloc.getSymbol() != Obj->symbol_end()) {
1927 Reloc.getSymbol()->getName(Name);
1932 auto RE = Obj->getRelocation(Reloc.getRawDataRefImpl());
1933 SectionRef RelocSection = Obj->getRelocationSection(RE);
1935 uint64_t SectionAddr = RelocSection.getAddress();
1937 auto Sym = Symbols.upper_bound(Addr);
1938 if (Sym == Symbols.begin()) {
1939 // The first symbol in the object is after this reference, the best we can
1940 // do is section-relative notation.
1941 RelocSection.getName(Name);
1942 Addend = Addr - SectionAddr;
1946 // Go back one so that SymbolAddress <= Addr.
1949 section_iterator SymSection = Obj->section_end();
1950 Sym->second.getSection(SymSection);
1951 if (RelocSection == *SymSection) {
1952 // There's a valid symbol in the same section before this reference.
1953 Sym->second.getName(Name);
1954 Addend = Addr - Sym->first;
1958 // There is a symbol before this reference, but it's in a different
1959 // section. Probably not helpful to mention it, so use the section name.
1960 RelocSection.getName(Name);
1961 Addend = Addr - SectionAddr;
1964 static void printUnwindRelocDest(const MachOObjectFile *Obj,
1965 std::map<uint64_t, SymbolRef> &Symbols,
1966 const RelocationRef &Reloc, uint64_t Addr) {
1970 if (!Reloc.getObjectFile())
1973 findUnwindRelocNameAddend(Obj, Symbols, Reloc, Addr, Name, Addend);
1977 outs() << " + " << format("0x%" PRIx64, Addend);
1981 printMachOCompactUnwindSection(const MachOObjectFile *Obj,
1982 std::map<uint64_t, SymbolRef> &Symbols,
1983 const SectionRef &CompactUnwind) {
1985 assert(Obj->isLittleEndian() &&
1986 "There should not be a big-endian .o with __compact_unwind");
1988 bool Is64 = Obj->is64Bit();
1989 uint32_t PointerSize = Is64 ? sizeof(uint64_t) : sizeof(uint32_t);
1990 uint32_t EntrySize = 3 * PointerSize + 2 * sizeof(uint32_t);
1993 CompactUnwind.getContents(Contents);
1995 SmallVector<CompactUnwindEntry, 4> CompactUnwinds;
1997 // First populate the initial raw offsets, encodings and so on from the entry.
1998 for (unsigned Offset = 0; Offset < Contents.size(); Offset += EntrySize) {
1999 CompactUnwindEntry Entry(Contents.data(), Offset, Is64);
2000 CompactUnwinds.push_back(Entry);
2003 // Next we need to look at the relocations to find out what objects are
2004 // actually being referred to.
2005 for (const RelocationRef &Reloc : CompactUnwind.relocations()) {
2006 uint64_t RelocAddress;
2007 Reloc.getOffset(RelocAddress);
2009 uint32_t EntryIdx = RelocAddress / EntrySize;
2010 uint32_t OffsetInEntry = RelocAddress - EntryIdx * EntrySize;
2011 CompactUnwindEntry &Entry = CompactUnwinds[EntryIdx];
2013 if (OffsetInEntry == 0)
2014 Entry.FunctionReloc = Reloc;
2015 else if (OffsetInEntry == PointerSize + 2 * sizeof(uint32_t))
2016 Entry.PersonalityReloc = Reloc;
2017 else if (OffsetInEntry == 2 * PointerSize + 2 * sizeof(uint32_t))
2018 Entry.LSDAReloc = Reloc;
2020 llvm_unreachable("Unexpected relocation in __compact_unwind section");
2023 // Finally, we're ready to print the data we've gathered.
2024 outs() << "Contents of __compact_unwind section:\n";
2025 for (auto &Entry : CompactUnwinds) {
2026 outs() << " Entry at offset "
2027 << format("0x%" PRIx32, Entry.OffsetInSection) << ":\n";
2029 // 1. Start of the region this entry applies to.
2030 outs() << " start: " << format("0x%" PRIx64,
2031 Entry.FunctionAddr) << ' ';
2032 printUnwindRelocDest(Obj, Symbols, Entry.FunctionReloc, Entry.FunctionAddr);
2035 // 2. Length of the region this entry applies to.
2036 outs() << " length: " << format("0x%" PRIx32, Entry.Length)
2038 // 3. The 32-bit compact encoding.
2039 outs() << " compact encoding: "
2040 << format("0x%08" PRIx32, Entry.CompactEncoding) << '\n';
2042 // 4. The personality function, if present.
2043 if (Entry.PersonalityReloc.getObjectFile()) {
2044 outs() << " personality function: "
2045 << format("0x%" PRIx64, Entry.PersonalityAddr) << ' ';
2046 printUnwindRelocDest(Obj, Symbols, Entry.PersonalityReloc,
2047 Entry.PersonalityAddr);
2051 // 5. This entry's language-specific data area.
2052 if (Entry.LSDAReloc.getObjectFile()) {
2053 outs() << " LSDA: " << format("0x%" PRIx64,
2054 Entry.LSDAAddr) << ' ';
2055 printUnwindRelocDest(Obj, Symbols, Entry.LSDAReloc, Entry.LSDAAddr);
2061 //===----------------------------------------------------------------------===//
2062 // __unwind_info section dumping
2063 //===----------------------------------------------------------------------===//
2065 static void printRegularSecondLevelUnwindPage(const char *PageStart) {
2066 const char *Pos = PageStart;
2067 uint32_t Kind = readNext<uint32_t>(Pos);
2069 assert(Kind == 2 && "kind for a regular 2nd level index should be 2");
2071 uint16_t EntriesStart = readNext<uint16_t>(Pos);
2072 uint16_t NumEntries = readNext<uint16_t>(Pos);
2074 Pos = PageStart + EntriesStart;
2075 for (unsigned i = 0; i < NumEntries; ++i) {
2076 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
2077 uint32_t Encoding = readNext<uint32_t>(Pos);
2079 outs() << " [" << i << "]: "
2080 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
2082 << "encoding=" << format("0x%08" PRIx32, Encoding) << '\n';
2086 static void printCompressedSecondLevelUnwindPage(
2087 const char *PageStart, uint32_t FunctionBase,
2088 const SmallVectorImpl<uint32_t> &CommonEncodings) {
2089 const char *Pos = PageStart;
2090 uint32_t Kind = readNext<uint32_t>(Pos);
2092 assert(Kind == 3 && "kind for a compressed 2nd level index should be 3");
2094 uint16_t EntriesStart = readNext<uint16_t>(Pos);
2095 uint16_t NumEntries = readNext<uint16_t>(Pos);
2097 uint16_t EncodingsStart = readNext<uint16_t>(Pos);
2098 readNext<uint16_t>(Pos);
2099 const auto *PageEncodings = reinterpret_cast<const support::ulittle32_t *>(
2100 PageStart + EncodingsStart);
2102 Pos = PageStart + EntriesStart;
2103 for (unsigned i = 0; i < NumEntries; ++i) {
2104 uint32_t Entry = readNext<uint32_t>(Pos);
2105 uint32_t FunctionOffset = FunctionBase + (Entry & 0xffffff);
2106 uint32_t EncodingIdx = Entry >> 24;
2109 if (EncodingIdx < CommonEncodings.size())
2110 Encoding = CommonEncodings[EncodingIdx];
2112 Encoding = PageEncodings[EncodingIdx - CommonEncodings.size()];
2114 outs() << " [" << i << "]: "
2115 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
2117 << "encoding[" << EncodingIdx
2118 << "]=" << format("0x%08" PRIx32, Encoding) << '\n';
2122 static void printMachOUnwindInfoSection(const MachOObjectFile *Obj,
2123 std::map<uint64_t, SymbolRef> &Symbols,
2124 const SectionRef &UnwindInfo) {
2126 assert(Obj->isLittleEndian() &&
2127 "There should not be a big-endian .o with __unwind_info");
2129 outs() << "Contents of __unwind_info section:\n";
2132 UnwindInfo.getContents(Contents);
2133 const char *Pos = Contents.data();
2135 //===----------------------------------
2137 //===----------------------------------
2139 uint32_t Version = readNext<uint32_t>(Pos);
2140 outs() << " Version: "
2141 << format("0x%" PRIx32, Version) << '\n';
2142 assert(Version == 1 && "only understand version 1");
2144 uint32_t CommonEncodingsStart = readNext<uint32_t>(Pos);
2145 outs() << " Common encodings array section offset: "
2146 << format("0x%" PRIx32, CommonEncodingsStart) << '\n';
2147 uint32_t NumCommonEncodings = readNext<uint32_t>(Pos);
2148 outs() << " Number of common encodings in array: "
2149 << format("0x%" PRIx32, NumCommonEncodings) << '\n';
2151 uint32_t PersonalitiesStart = readNext<uint32_t>(Pos);
2152 outs() << " Personality function array section offset: "
2153 << format("0x%" PRIx32, PersonalitiesStart) << '\n';
2154 uint32_t NumPersonalities = readNext<uint32_t>(Pos);
2155 outs() << " Number of personality functions in array: "
2156 << format("0x%" PRIx32, NumPersonalities) << '\n';
2158 uint32_t IndicesStart = readNext<uint32_t>(Pos);
2159 outs() << " Index array section offset: "
2160 << format("0x%" PRIx32, IndicesStart) << '\n';
2161 uint32_t NumIndices = readNext<uint32_t>(Pos);
2162 outs() << " Number of indices in array: "
2163 << format("0x%" PRIx32, NumIndices) << '\n';
2165 //===----------------------------------
2166 // A shared list of common encodings
2167 //===----------------------------------
2169 // These occupy indices in the range [0, N] whenever an encoding is referenced
2170 // from a compressed 2nd level index table. In practice the linker only
2171 // creates ~128 of these, so that indices are available to embed encodings in
2172 // the 2nd level index.
2174 SmallVector<uint32_t, 64> CommonEncodings;
2175 outs() << " Common encodings: (count = " << NumCommonEncodings << ")\n";
2176 Pos = Contents.data() + CommonEncodingsStart;
2177 for (unsigned i = 0; i < NumCommonEncodings; ++i) {
2178 uint32_t Encoding = readNext<uint32_t>(Pos);
2179 CommonEncodings.push_back(Encoding);
2181 outs() << " encoding[" << i << "]: " << format("0x%08" PRIx32, Encoding)
2185 //===----------------------------------
2186 // Personality functions used in this executable
2187 //===----------------------------------
2189 // There should be only a handful of these (one per source language,
2190 // roughly). Particularly since they only get 2 bits in the compact encoding.
2192 outs() << " Personality functions: (count = " << NumPersonalities << ")\n";
2193 Pos = Contents.data() + PersonalitiesStart;
2194 for (unsigned i = 0; i < NumPersonalities; ++i) {
2195 uint32_t PersonalityFn = readNext<uint32_t>(Pos);
2196 outs() << " personality[" << i + 1
2197 << "]: " << format("0x%08" PRIx32, PersonalityFn) << '\n';
2200 //===----------------------------------
2201 // The level 1 index entries
2202 //===----------------------------------
2204 // These specify an approximate place to start searching for the more detailed
2205 // information, sorted by PC.
2208 uint32_t FunctionOffset;
2209 uint32_t SecondLevelPageStart;
2213 SmallVector<IndexEntry, 4> IndexEntries;
2215 outs() << " Top level indices: (count = " << NumIndices << ")\n";
2216 Pos = Contents.data() + IndicesStart;
2217 for (unsigned i = 0; i < NumIndices; ++i) {
2220 Entry.FunctionOffset = readNext<uint32_t>(Pos);
2221 Entry.SecondLevelPageStart = readNext<uint32_t>(Pos);
2222 Entry.LSDAStart = readNext<uint32_t>(Pos);
2223 IndexEntries.push_back(Entry);
2225 outs() << " [" << i << "]: "
2226 << "function offset=" << format("0x%08" PRIx32, Entry.FunctionOffset)
2228 << "2nd level page offset="
2229 << format("0x%08" PRIx32, Entry.SecondLevelPageStart) << ", "
2230 << "LSDA offset=" << format("0x%08" PRIx32, Entry.LSDAStart) << '\n';
2233 //===----------------------------------
2234 // Next come the LSDA tables
2235 //===----------------------------------
2237 // The LSDA layout is rather implicit: it's a contiguous array of entries from
2238 // the first top-level index's LSDAOffset to the last (sentinel).
2240 outs() << " LSDA descriptors:\n";
2241 Pos = Contents.data() + IndexEntries[0].LSDAStart;
2242 int NumLSDAs = (IndexEntries.back().LSDAStart - IndexEntries[0].LSDAStart) /
2243 (2 * sizeof(uint32_t));
2244 for (int i = 0; i < NumLSDAs; ++i) {
2245 uint32_t FunctionOffset = readNext<uint32_t>(Pos);
2246 uint32_t LSDAOffset = readNext<uint32_t>(Pos);
2247 outs() << " [" << i << "]: "
2248 << "function offset=" << format("0x%08" PRIx32, FunctionOffset)
2250 << "LSDA offset=" << format("0x%08" PRIx32, LSDAOffset) << '\n';
2253 //===----------------------------------
2254 // Finally, the 2nd level indices
2255 //===----------------------------------
2257 // Generally these are 4K in size, and have 2 possible forms:
2258 // + Regular stores up to 511 entries with disparate encodings
2259 // + Compressed stores up to 1021 entries if few enough compact encoding
2261 outs() << " Second level indices:\n";
2262 for (unsigned i = 0; i < IndexEntries.size() - 1; ++i) {
2263 // The final sentinel top-level index has no associated 2nd level page
2264 if (IndexEntries[i].SecondLevelPageStart == 0)
2267 outs() << " Second level index[" << i << "]: "
2268 << "offset in section="
2269 << format("0x%08" PRIx32, IndexEntries[i].SecondLevelPageStart)
2271 << "base function offset="
2272 << format("0x%08" PRIx32, IndexEntries[i].FunctionOffset) << '\n';
2274 Pos = Contents.data() + IndexEntries[i].SecondLevelPageStart;
2275 uint32_t Kind = *reinterpret_cast<const support::ulittle32_t *>(Pos);
2277 printRegularSecondLevelUnwindPage(Pos);
2279 printCompressedSecondLevelUnwindPage(Pos, IndexEntries[i].FunctionOffset,
2282 llvm_unreachable("Do not know how to print this kind of 2nd level page");
2286 void llvm::printMachOUnwindInfo(const MachOObjectFile *Obj) {
2287 std::map<uint64_t, SymbolRef> Symbols;
2288 for (const SymbolRef &SymRef : Obj->symbols()) {
2289 // Discard any undefined or absolute symbols. They're not going to take part
2290 // in the convenience lookup for unwind info and just take up resources.
2291 section_iterator Section = Obj->section_end();
2292 SymRef.getSection(Section);
2293 if (Section == Obj->section_end())
2297 SymRef.getAddress(Addr);
2298 Symbols.insert(std::make_pair(Addr, SymRef));
2301 for (const SectionRef &Section : Obj->sections()) {
2303 Section.getName(SectName);
2304 if (SectName == "__compact_unwind")
2305 printMachOCompactUnwindSection(Obj, Symbols, Section);
2306 else if (SectName == "__unwind_info")
2307 printMachOUnwindInfoSection(Obj, Symbols, Section);
2308 else if (SectName == "__eh_frame")
2309 outs() << "llvm-objdump: warning: unhandled __eh_frame section\n";
2313 static void PrintMachHeader(uint32_t magic, uint32_t cputype,
2314 uint32_t cpusubtype, uint32_t filetype,
2315 uint32_t ncmds, uint32_t sizeofcmds, uint32_t flags,
2317 outs() << "Mach header\n";
2318 outs() << " magic cputype cpusubtype caps filetype ncmds "
2319 "sizeofcmds flags\n";
2321 if (magic == MachO::MH_MAGIC)
2322 outs() << " MH_MAGIC";
2323 else if (magic == MachO::MH_MAGIC_64)
2324 outs() << "MH_MAGIC_64";
2326 outs() << format(" 0x%08" PRIx32, magic);
2328 case MachO::CPU_TYPE_I386:
2330 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2331 case MachO::CPU_SUBTYPE_I386_ALL:
2335 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2339 case MachO::CPU_TYPE_X86_64:
2340 outs() << " X86_64";
2341 case MachO::CPU_SUBTYPE_X86_64_ALL:
2344 case MachO::CPU_SUBTYPE_X86_64_H:
2345 outs() << " Haswell";
2346 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2348 case MachO::CPU_TYPE_ARM:
2350 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2351 case MachO::CPU_SUBTYPE_ARM_ALL:
2354 case MachO::CPU_SUBTYPE_ARM_V4T:
2357 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
2360 case MachO::CPU_SUBTYPE_ARM_XSCALE:
2361 outs() << " XSCALE";
2363 case MachO::CPU_SUBTYPE_ARM_V6:
2366 case MachO::CPU_SUBTYPE_ARM_V6M:
2369 case MachO::CPU_SUBTYPE_ARM_V7:
2372 case MachO::CPU_SUBTYPE_ARM_V7EM:
2375 case MachO::CPU_SUBTYPE_ARM_V7K:
2378 case MachO::CPU_SUBTYPE_ARM_V7M:
2381 case MachO::CPU_SUBTYPE_ARM_V7S:
2385 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2389 case MachO::CPU_TYPE_ARM64:
2391 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2392 case MachO::CPU_SUBTYPE_ARM64_ALL:
2396 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2400 case MachO::CPU_TYPE_POWERPC:
2402 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2403 case MachO::CPU_SUBTYPE_POWERPC_ALL:
2407 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2411 case MachO::CPU_TYPE_POWERPC64:
2413 switch (cpusubtype & ~MachO::CPU_SUBTYPE_MASK) {
2414 case MachO::CPU_SUBTYPE_POWERPC_ALL:
2418 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2423 if ((cpusubtype & MachO::CPU_SUBTYPE_MASK) == MachO::CPU_SUBTYPE_LIB64) {
2426 outs() << format(" 0x%02" PRIx32,
2427 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
2430 case MachO::MH_OBJECT:
2431 outs() << " OBJECT";
2433 case MachO::MH_EXECUTE:
2434 outs() << " EXECUTE";
2436 case MachO::MH_FVMLIB:
2437 outs() << " FVMLIB";
2439 case MachO::MH_CORE:
2442 case MachO::MH_PRELOAD:
2443 outs() << " PRELOAD";
2445 case MachO::MH_DYLIB:
2448 case MachO::MH_DYLIB_STUB:
2449 outs() << " DYLIB_STUB";
2451 case MachO::MH_DYLINKER:
2452 outs() << " DYLINKER";
2454 case MachO::MH_BUNDLE:
2455 outs() << " BUNDLE";
2457 case MachO::MH_DSYM:
2460 case MachO::MH_KEXT_BUNDLE:
2461 outs() << " KEXTBUNDLE";
2464 outs() << format(" %10u", filetype);
2467 outs() << format(" %5u", ncmds);
2468 outs() << format(" %10u", sizeofcmds);
2470 if (f & MachO::MH_NOUNDEFS) {
2471 outs() << " NOUNDEFS";
2472 f &= ~MachO::MH_NOUNDEFS;
2474 if (f & MachO::MH_INCRLINK) {
2475 outs() << " INCRLINK";
2476 f &= ~MachO::MH_INCRLINK;
2478 if (f & MachO::MH_DYLDLINK) {
2479 outs() << " DYLDLINK";
2480 f &= ~MachO::MH_DYLDLINK;
2482 if (f & MachO::MH_BINDATLOAD) {
2483 outs() << " BINDATLOAD";
2484 f &= ~MachO::MH_BINDATLOAD;
2486 if (f & MachO::MH_PREBOUND) {
2487 outs() << " PREBOUND";
2488 f &= ~MachO::MH_PREBOUND;
2490 if (f & MachO::MH_SPLIT_SEGS) {
2491 outs() << " SPLIT_SEGS";
2492 f &= ~MachO::MH_SPLIT_SEGS;
2494 if (f & MachO::MH_LAZY_INIT) {
2495 outs() << " LAZY_INIT";
2496 f &= ~MachO::MH_LAZY_INIT;
2498 if (f & MachO::MH_TWOLEVEL) {
2499 outs() << " TWOLEVEL";
2500 f &= ~MachO::MH_TWOLEVEL;
2502 if (f & MachO::MH_FORCE_FLAT) {
2503 outs() << " FORCE_FLAT";
2504 f &= ~MachO::MH_FORCE_FLAT;
2506 if (f & MachO::MH_NOMULTIDEFS) {
2507 outs() << " NOMULTIDEFS";
2508 f &= ~MachO::MH_NOMULTIDEFS;
2510 if (f & MachO::MH_NOFIXPREBINDING) {
2511 outs() << " NOFIXPREBINDING";
2512 f &= ~MachO::MH_NOFIXPREBINDING;
2514 if (f & MachO::MH_PREBINDABLE) {
2515 outs() << " PREBINDABLE";
2516 f &= ~MachO::MH_PREBINDABLE;
2518 if (f & MachO::MH_ALLMODSBOUND) {
2519 outs() << " ALLMODSBOUND";
2520 f &= ~MachO::MH_ALLMODSBOUND;
2522 if (f & MachO::MH_SUBSECTIONS_VIA_SYMBOLS) {
2523 outs() << " SUBSECTIONS_VIA_SYMBOLS";
2524 f &= ~MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
2526 if (f & MachO::MH_CANONICAL) {
2527 outs() << " CANONICAL";
2528 f &= ~MachO::MH_CANONICAL;
2530 if (f & MachO::MH_WEAK_DEFINES) {
2531 outs() << " WEAK_DEFINES";
2532 f &= ~MachO::MH_WEAK_DEFINES;
2534 if (f & MachO::MH_BINDS_TO_WEAK) {
2535 outs() << " BINDS_TO_WEAK";
2536 f &= ~MachO::MH_BINDS_TO_WEAK;
2538 if (f & MachO::MH_ALLOW_STACK_EXECUTION) {
2539 outs() << " ALLOW_STACK_EXECUTION";
2540 f &= ~MachO::MH_ALLOW_STACK_EXECUTION;
2542 if (f & MachO::MH_DEAD_STRIPPABLE_DYLIB) {
2543 outs() << " DEAD_STRIPPABLE_DYLIB";
2544 f &= ~MachO::MH_DEAD_STRIPPABLE_DYLIB;
2546 if (f & MachO::MH_PIE) {
2548 f &= ~MachO::MH_PIE;
2550 if (f & MachO::MH_NO_REEXPORTED_DYLIBS) {
2551 outs() << " NO_REEXPORTED_DYLIBS";
2552 f &= ~MachO::MH_NO_REEXPORTED_DYLIBS;
2554 if (f & MachO::MH_HAS_TLV_DESCRIPTORS) {
2555 outs() << " MH_HAS_TLV_DESCRIPTORS";
2556 f &= ~MachO::MH_HAS_TLV_DESCRIPTORS;
2558 if (f & MachO::MH_NO_HEAP_EXECUTION) {
2559 outs() << " MH_NO_HEAP_EXECUTION";
2560 f &= ~MachO::MH_NO_HEAP_EXECUTION;
2562 if (f & MachO::MH_APP_EXTENSION_SAFE) {
2563 outs() << " APP_EXTENSION_SAFE";
2564 f &= ~MachO::MH_APP_EXTENSION_SAFE;
2566 if (f != 0 || flags == 0)
2567 outs() << format(" 0x%08" PRIx32, f);
2569 outs() << format(" 0x%08" PRIx32, magic);
2570 outs() << format(" %7d", cputype);
2571 outs() << format(" %10d", cpusubtype & ~MachO::CPU_SUBTYPE_MASK);
2572 outs() << format(" 0x%02" PRIx32,
2573 (cpusubtype & MachO::CPU_SUBTYPE_MASK) >> 24);
2574 outs() << format(" %10u", filetype);
2575 outs() << format(" %5u", ncmds);
2576 outs() << format(" %10u", sizeofcmds);
2577 outs() << format(" 0x%08" PRIx32, flags);
2582 static void PrintSegmentCommand(uint32_t cmd, uint32_t cmdsize,
2583 StringRef SegName, uint64_t vmaddr,
2584 uint64_t vmsize, uint64_t fileoff,
2585 uint64_t filesize, uint32_t maxprot,
2586 uint32_t initprot, uint32_t nsects,
2587 uint32_t flags, uint32_t object_size,
2589 uint64_t expected_cmdsize;
2590 if (cmd == MachO::LC_SEGMENT) {
2591 outs() << " cmd LC_SEGMENT\n";
2592 expected_cmdsize = nsects;
2593 expected_cmdsize *= sizeof(struct MachO::section);
2594 expected_cmdsize += sizeof(struct MachO::segment_command);
2596 outs() << " cmd LC_SEGMENT_64\n";
2597 expected_cmdsize = nsects;
2598 expected_cmdsize *= sizeof(struct MachO::section_64);
2599 expected_cmdsize += sizeof(struct MachO::segment_command_64);
2601 outs() << " cmdsize " << cmdsize;
2602 if (cmdsize != expected_cmdsize)
2603 outs() << " Inconsistent size\n";
2606 outs() << " segname " << SegName << "\n";
2607 if (cmd == MachO::LC_SEGMENT_64) {
2608 outs() << " vmaddr " << format("0x%016" PRIx64, vmaddr) << "\n";
2609 outs() << " vmsize " << format("0x%016" PRIx64, vmsize) << "\n";
2611 outs() << " vmaddr " << format("0x%08" PRIx32, vmaddr) << "\n";
2612 outs() << " vmsize " << format("0x%08" PRIx32, vmsize) << "\n";
2614 outs() << " fileoff " << fileoff;
2615 if (fileoff > object_size)
2616 outs() << " (past end of file)\n";
2619 outs() << " filesize " << filesize;
2620 if (fileoff + filesize > object_size)
2621 outs() << " (past end of file)\n";
2626 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
2627 MachO::VM_PROT_EXECUTE)) != 0)
2628 outs() << " maxprot ?" << format("0x%08" PRIx32, maxprot) << "\n";
2630 if (maxprot & MachO::VM_PROT_READ)
2631 outs() << " maxprot r";
2633 outs() << " maxprot -";
2634 if (maxprot & MachO::VM_PROT_WRITE)
2638 if (maxprot & MachO::VM_PROT_EXECUTE)
2644 ~(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE |
2645 MachO::VM_PROT_EXECUTE)) != 0)
2646 outs() << " initprot ?" << format("0x%08" PRIx32, initprot) << "\n";
2648 if (initprot & MachO::VM_PROT_READ)
2649 outs() << " initprot r";
2651 outs() << " initprot -";
2652 if (initprot & MachO::VM_PROT_WRITE)
2656 if (initprot & MachO::VM_PROT_EXECUTE)
2662 outs() << " maxprot " << format("0x%08" PRIx32, maxprot) << "\n";
2663 outs() << " initprot " << format("0x%08" PRIx32, initprot) << "\n";
2665 outs() << " nsects " << nsects << "\n";
2669 outs() << " (none)\n";
2671 if (flags & MachO::SG_HIGHVM) {
2672 outs() << " HIGHVM";
2673 flags &= ~MachO::SG_HIGHVM;
2675 if (flags & MachO::SG_FVMLIB) {
2676 outs() << " FVMLIB";
2677 flags &= ~MachO::SG_FVMLIB;
2679 if (flags & MachO::SG_NORELOC) {
2680 outs() << " NORELOC";
2681 flags &= ~MachO::SG_NORELOC;
2683 if (flags & MachO::SG_PROTECTED_VERSION_1) {
2684 outs() << " PROTECTED_VERSION_1";
2685 flags &= ~MachO::SG_PROTECTED_VERSION_1;
2688 outs() << format(" 0x%08" PRIx32, flags) << " (unknown flags)\n";
2693 outs() << " flags " << format("0x%" PRIx32, flags) << "\n";
2697 static void PrintSection(const char *sectname, const char *segname,
2698 uint64_t addr, uint64_t size, uint32_t offset,
2699 uint32_t align, uint32_t reloff, uint32_t nreloc,
2700 uint32_t flags, uint32_t reserved1, uint32_t reserved2,
2701 uint32_t cmd, const char *sg_segname,
2702 uint32_t filetype, uint32_t object_size,
2704 outs() << "Section\n";
2705 outs() << " sectname " << format("%.16s\n", sectname);
2706 outs() << " segname " << format("%.16s", segname);
2707 if (filetype != MachO::MH_OBJECT && strncmp(sg_segname, segname, 16) != 0)
2708 outs() << " (does not match segment)\n";
2711 if (cmd == MachO::LC_SEGMENT_64) {
2712 outs() << " addr " << format("0x%016" PRIx64, addr) << "\n";
2713 outs() << " size " << format("0x%016" PRIx64, size);
2715 outs() << " addr " << format("0x%08" PRIx32, addr) << "\n";
2716 outs() << " size " << format("0x%08" PRIx32, size);
2718 if ((flags & MachO::S_ZEROFILL) != 0 && offset + size > object_size)
2719 outs() << " (past end of file)\n";
2722 outs() << " offset " << offset;
2723 if (offset > object_size)
2724 outs() << " (past end of file)\n";
2727 uint32_t align_shifted = 1 << align;
2728 outs() << " align 2^" << align << " (" << align_shifted << ")\n";
2729 outs() << " reloff " << reloff;
2730 if (reloff > object_size)
2731 outs() << " (past end of file)\n";
2734 outs() << " nreloc " << nreloc;
2735 if (reloff + nreloc * sizeof(struct MachO::relocation_info) > object_size)
2736 outs() << " (past end of file)\n";
2739 uint32_t section_type = flags & MachO::SECTION_TYPE;
2742 if (section_type == MachO::S_REGULAR)
2743 outs() << " S_REGULAR\n";
2744 else if (section_type == MachO::S_ZEROFILL)
2745 outs() << " S_ZEROFILL\n";
2746 else if (section_type == MachO::S_CSTRING_LITERALS)
2747 outs() << " S_CSTRING_LITERALS\n";
2748 else if (section_type == MachO::S_4BYTE_LITERALS)
2749 outs() << " S_4BYTE_LITERALS\n";
2750 else if (section_type == MachO::S_8BYTE_LITERALS)
2751 outs() << " S_8BYTE_LITERALS\n";
2752 else if (section_type == MachO::S_16BYTE_LITERALS)
2753 outs() << " S_16BYTE_LITERALS\n";
2754 else if (section_type == MachO::S_LITERAL_POINTERS)
2755 outs() << " S_LITERAL_POINTERS\n";
2756 else if (section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS)
2757 outs() << " S_NON_LAZY_SYMBOL_POINTERS\n";
2758 else if (section_type == MachO::S_LAZY_SYMBOL_POINTERS)
2759 outs() << " S_LAZY_SYMBOL_POINTERS\n";
2760 else if (section_type == MachO::S_SYMBOL_STUBS)
2761 outs() << " S_SYMBOL_STUBS\n";
2762 else if (section_type == MachO::S_MOD_INIT_FUNC_POINTERS)
2763 outs() << " S_MOD_INIT_FUNC_POINTERS\n";
2764 else if (section_type == MachO::S_MOD_TERM_FUNC_POINTERS)
2765 outs() << " S_MOD_TERM_FUNC_POINTERS\n";
2766 else if (section_type == MachO::S_COALESCED)
2767 outs() << " S_COALESCED\n";
2768 else if (section_type == MachO::S_INTERPOSING)
2769 outs() << " S_INTERPOSING\n";
2770 else if (section_type == MachO::S_DTRACE_DOF)
2771 outs() << " S_DTRACE_DOF\n";
2772 else if (section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS)
2773 outs() << " S_LAZY_DYLIB_SYMBOL_POINTERS\n";
2774 else if (section_type == MachO::S_THREAD_LOCAL_REGULAR)
2775 outs() << " S_THREAD_LOCAL_REGULAR\n";
2776 else if (section_type == MachO::S_THREAD_LOCAL_ZEROFILL)
2777 outs() << " S_THREAD_LOCAL_ZEROFILL\n";
2778 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLES)
2779 outs() << " S_THREAD_LOCAL_VARIABLES\n";
2780 else if (section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
2781 outs() << " S_THREAD_LOCAL_VARIABLE_POINTERS\n";
2782 else if (section_type == MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS)
2783 outs() << " S_THREAD_LOCAL_INIT_FUNCTION_POINTERS\n";
2785 outs() << format("0x%08" PRIx32, section_type) << "\n";
2786 outs() << "attributes";
2787 uint32_t section_attributes = flags & MachO::SECTION_ATTRIBUTES;
2788 if (section_attributes & MachO::S_ATTR_PURE_INSTRUCTIONS)
2789 outs() << " PURE_INSTRUCTIONS";
2790 if (section_attributes & MachO::S_ATTR_NO_TOC)
2791 outs() << " NO_TOC";
2792 if (section_attributes & MachO::S_ATTR_STRIP_STATIC_SYMS)
2793 outs() << " STRIP_STATIC_SYMS";
2794 if (section_attributes & MachO::S_ATTR_NO_DEAD_STRIP)
2795 outs() << " NO_DEAD_STRIP";
2796 if (section_attributes & MachO::S_ATTR_LIVE_SUPPORT)
2797 outs() << " LIVE_SUPPORT";
2798 if (section_attributes & MachO::S_ATTR_SELF_MODIFYING_CODE)
2799 outs() << " SELF_MODIFYING_CODE";
2800 if (section_attributes & MachO::S_ATTR_DEBUG)
2802 if (section_attributes & MachO::S_ATTR_SOME_INSTRUCTIONS)
2803 outs() << " SOME_INSTRUCTIONS";
2804 if (section_attributes & MachO::S_ATTR_EXT_RELOC)
2805 outs() << " EXT_RELOC";
2806 if (section_attributes & MachO::S_ATTR_LOC_RELOC)
2807 outs() << " LOC_RELOC";
2808 if (section_attributes == 0)
2809 outs() << " (none)";
2812 outs() << " flags " << format("0x%08" PRIx32, flags) << "\n";
2813 outs() << " reserved1 " << reserved1;
2814 if (section_type == MachO::S_SYMBOL_STUBS ||
2815 section_type == MachO::S_LAZY_SYMBOL_POINTERS ||
2816 section_type == MachO::S_LAZY_DYLIB_SYMBOL_POINTERS ||
2817 section_type == MachO::S_NON_LAZY_SYMBOL_POINTERS ||
2818 section_type == MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
2819 outs() << " (index into indirect symbol table)\n";
2822 outs() << " reserved2 " << reserved2;
2823 if (section_type == MachO::S_SYMBOL_STUBS)
2824 outs() << " (size of stubs)\n";
2829 static void PrintSymtabLoadCommand(MachO::symtab_command st, uint32_t cputype,
2830 uint32_t object_size) {
2831 outs() << " cmd LC_SYMTAB\n";
2832 outs() << " cmdsize " << st.cmdsize;
2833 if (st.cmdsize != sizeof(struct MachO::symtab_command))
2834 outs() << " Incorrect size\n";
2837 outs() << " symoff " << st.symoff;
2838 if (st.symoff > object_size)
2839 outs() << " (past end of file)\n";
2842 outs() << " nsyms " << st.nsyms;
2844 if (cputype & MachO::CPU_ARCH_ABI64) {
2845 big_size = st.nsyms;
2846 big_size *= sizeof(struct MachO::nlist_64);
2847 big_size += st.symoff;
2848 if (big_size > object_size)
2849 outs() << " (past end of file)\n";
2853 big_size = st.nsyms;
2854 big_size *= sizeof(struct MachO::nlist);
2855 big_size += st.symoff;
2856 if (big_size > object_size)
2857 outs() << " (past end of file)\n";
2861 outs() << " stroff " << st.stroff;
2862 if (st.stroff > object_size)
2863 outs() << " (past end of file)\n";
2866 outs() << " strsize " << st.strsize;
2867 big_size = st.stroff;
2868 big_size += st.strsize;
2869 if (big_size > object_size)
2870 outs() << " (past end of file)\n";
2875 static void PrintDysymtabLoadCommand(MachO::dysymtab_command dyst,
2876 uint32_t nsyms, uint32_t object_size,
2878 outs() << " cmd LC_DYSYMTAB\n";
2879 outs() << " cmdsize " << dyst.cmdsize;
2880 if (dyst.cmdsize != sizeof(struct MachO::dysymtab_command))
2881 outs() << " Incorrect size\n";
2884 outs() << " ilocalsym " << dyst.ilocalsym;
2885 if (dyst.ilocalsym > nsyms)
2886 outs() << " (greater than the number of symbols)\n";
2889 outs() << " nlocalsym " << dyst.nlocalsym;
2891 big_size = dyst.ilocalsym;
2892 big_size += dyst.nlocalsym;
2893 if (big_size > nsyms)
2894 outs() << " (past the end of the symbol table)\n";
2897 outs() << " iextdefsym " << dyst.iextdefsym;
2898 if (dyst.iextdefsym > nsyms)
2899 outs() << " (greater than the number of symbols)\n";
2902 outs() << " nextdefsym " << dyst.nextdefsym;
2903 big_size = dyst.iextdefsym;
2904 big_size += dyst.nextdefsym;
2905 if (big_size > nsyms)
2906 outs() << " (past the end of the symbol table)\n";
2909 outs() << " iundefsym " << dyst.iundefsym;
2910 if (dyst.iundefsym > nsyms)
2911 outs() << " (greater than the number of symbols)\n";
2914 outs() << " nundefsym " << dyst.nundefsym;
2915 big_size = dyst.iundefsym;
2916 big_size += dyst.nundefsym;
2917 if (big_size > nsyms)
2918 outs() << " (past the end of the symbol table)\n";
2921 outs() << " tocoff " << dyst.tocoff;
2922 if (dyst.tocoff > object_size)
2923 outs() << " (past end of file)\n";
2926 outs() << " ntoc " << dyst.ntoc;
2927 big_size = dyst.ntoc;
2928 big_size *= sizeof(struct MachO::dylib_table_of_contents);
2929 big_size += dyst.tocoff;
2930 if (big_size > object_size)
2931 outs() << " (past end of file)\n";
2934 outs() << " modtaboff " << dyst.modtaboff;
2935 if (dyst.modtaboff > object_size)
2936 outs() << " (past end of file)\n";
2939 outs() << " nmodtab " << dyst.nmodtab;
2941 if (cputype & MachO::CPU_ARCH_ABI64) {
2942 modtabend = dyst.nmodtab;
2943 modtabend *= sizeof(struct MachO::dylib_module_64);
2944 modtabend += dyst.modtaboff;
2946 modtabend = dyst.nmodtab;
2947 modtabend *= sizeof(struct MachO::dylib_module);
2948 modtabend += dyst.modtaboff;
2950 if (modtabend > object_size)
2951 outs() << " (past end of file)\n";
2954 outs() << " extrefsymoff " << dyst.extrefsymoff;
2955 if (dyst.extrefsymoff > object_size)
2956 outs() << " (past end of file)\n";
2959 outs() << " nextrefsyms " << dyst.nextrefsyms;
2960 big_size = dyst.nextrefsyms;
2961 big_size *= sizeof(struct MachO::dylib_reference);
2962 big_size += dyst.extrefsymoff;
2963 if (big_size > object_size)
2964 outs() << " (past end of file)\n";
2967 outs() << " indirectsymoff " << dyst.indirectsymoff;
2968 if (dyst.indirectsymoff > object_size)
2969 outs() << " (past end of file)\n";
2972 outs() << " nindirectsyms " << dyst.nindirectsyms;
2973 big_size = dyst.nindirectsyms;
2974 big_size *= sizeof(uint32_t);
2975 big_size += dyst.indirectsymoff;
2976 if (big_size > object_size)
2977 outs() << " (past end of file)\n";
2980 outs() << " extreloff " << dyst.extreloff;
2981 if (dyst.extreloff > object_size)
2982 outs() << " (past end of file)\n";
2985 outs() << " nextrel " << dyst.nextrel;
2986 big_size = dyst.nextrel;
2987 big_size *= sizeof(struct MachO::relocation_info);
2988 big_size += dyst.extreloff;
2989 if (big_size > object_size)
2990 outs() << " (past end of file)\n";
2993 outs() << " locreloff " << dyst.locreloff;
2994 if (dyst.locreloff > object_size)
2995 outs() << " (past end of file)\n";
2998 outs() << " nlocrel " << dyst.nlocrel;
2999 big_size = dyst.nlocrel;
3000 big_size *= sizeof(struct MachO::relocation_info);
3001 big_size += dyst.locreloff;
3002 if (big_size > object_size)
3003 outs() << " (past end of file)\n";
3008 static void PrintDyldInfoLoadCommand(MachO::dyld_info_command dc,
3009 uint32_t object_size) {
3010 if (dc.cmd == MachO::LC_DYLD_INFO)
3011 outs() << " cmd LC_DYLD_INFO\n";
3013 outs() << " cmd LC_DYLD_INFO_ONLY\n";
3014 outs() << " cmdsize " << dc.cmdsize;
3015 if (dc.cmdsize != sizeof(struct MachO::dyld_info_command))
3016 outs() << " Incorrect size\n";
3019 outs() << " rebase_off " << dc.rebase_off;
3020 if (dc.rebase_off > object_size)
3021 outs() << " (past end of file)\n";
3024 outs() << " rebase_size " << dc.rebase_size;
3026 big_size = dc.rebase_off;
3027 big_size += dc.rebase_size;
3028 if (big_size > object_size)
3029 outs() << " (past end of file)\n";
3032 outs() << " bind_off " << dc.bind_off;
3033 if (dc.bind_off > object_size)
3034 outs() << " (past end of file)\n";
3037 outs() << " bind_size " << dc.bind_size;
3038 big_size = dc.bind_off;
3039 big_size += dc.bind_size;
3040 if (big_size > object_size)
3041 outs() << " (past end of file)\n";
3044 outs() << " weak_bind_off " << dc.weak_bind_off;
3045 if (dc.weak_bind_off > object_size)
3046 outs() << " (past end of file)\n";
3049 outs() << " weak_bind_size " << dc.weak_bind_size;
3050 big_size = dc.weak_bind_off;
3051 big_size += dc.weak_bind_size;
3052 if (big_size > object_size)
3053 outs() << " (past end of file)\n";
3056 outs() << " lazy_bind_off " << dc.lazy_bind_off;
3057 if (dc.lazy_bind_off > object_size)
3058 outs() << " (past end of file)\n";
3061 outs() << " lazy_bind_size " << dc.lazy_bind_size;
3062 big_size = dc.lazy_bind_off;
3063 big_size += dc.lazy_bind_size;
3064 if (big_size > object_size)
3065 outs() << " (past end of file)\n";
3068 outs() << " export_off " << dc.export_off;
3069 if (dc.export_off > object_size)
3070 outs() << " (past end of file)\n";
3073 outs() << " export_size " << dc.export_size;
3074 big_size = dc.export_off;
3075 big_size += dc.export_size;
3076 if (big_size > object_size)
3077 outs() << " (past end of file)\n";
3082 static void PrintDyldLoadCommand(MachO::dylinker_command dyld,
3084 if (dyld.cmd == MachO::LC_ID_DYLINKER)
3085 outs() << " cmd LC_ID_DYLINKER\n";
3086 else if (dyld.cmd == MachO::LC_LOAD_DYLINKER)
3087 outs() << " cmd LC_LOAD_DYLINKER\n";
3088 else if (dyld.cmd == MachO::LC_DYLD_ENVIRONMENT)
3089 outs() << " cmd LC_DYLD_ENVIRONMENT\n";
3091 outs() << " cmd ?(" << dyld.cmd << ")\n";
3092 outs() << " cmdsize " << dyld.cmdsize;
3093 if (dyld.cmdsize < sizeof(struct MachO::dylinker_command))
3094 outs() << " Incorrect size\n";
3097 if (dyld.name >= dyld.cmdsize)
3098 outs() << " name ?(bad offset " << dyld.name << ")\n";
3100 const char *P = (const char *)(Ptr) + dyld.name;
3101 outs() << " name " << P << " (offset " << dyld.name << ")\n";
3105 static void PrintUuidLoadCommand(MachO::uuid_command uuid) {
3106 outs() << " cmd LC_UUID\n";
3107 outs() << " cmdsize " << uuid.cmdsize;
3108 if (uuid.cmdsize != sizeof(struct MachO::uuid_command))
3109 outs() << " Incorrect size\n";
3113 outs() << format("%02" PRIX32, uuid.uuid[0]);
3114 outs() << format("%02" PRIX32, uuid.uuid[1]);
3115 outs() << format("%02" PRIX32, uuid.uuid[2]);
3116 outs() << format("%02" PRIX32, uuid.uuid[3]);
3118 outs() << format("%02" PRIX32, uuid.uuid[4]);
3119 outs() << format("%02" PRIX32, uuid.uuid[5]);
3121 outs() << format("%02" PRIX32, uuid.uuid[6]);
3122 outs() << format("%02" PRIX32, uuid.uuid[7]);
3124 outs() << format("%02" PRIX32, uuid.uuid[8]);
3125 outs() << format("%02" PRIX32, uuid.uuid[9]);
3127 outs() << format("%02" PRIX32, uuid.uuid[10]);
3128 outs() << format("%02" PRIX32, uuid.uuid[11]);
3129 outs() << format("%02" PRIX32, uuid.uuid[12]);
3130 outs() << format("%02" PRIX32, uuid.uuid[13]);
3131 outs() << format("%02" PRIX32, uuid.uuid[14]);
3132 outs() << format("%02" PRIX32, uuid.uuid[15]);
3136 static void PrintVersionMinLoadCommand(MachO::version_min_command vd) {
3137 if (vd.cmd == MachO::LC_VERSION_MIN_MACOSX)
3138 outs() << " cmd LC_VERSION_MIN_MACOSX\n";
3139 else if (vd.cmd == MachO::LC_VERSION_MIN_IPHONEOS)
3140 outs() << " cmd LC_VERSION_MIN_IPHONEOS\n";
3142 outs() << " cmd " << vd.cmd << " (?)\n";
3143 outs() << " cmdsize " << vd.cmdsize;
3144 if (vd.cmdsize != sizeof(struct MachO::version_min_command))
3145 outs() << " Incorrect size\n";
3148 outs() << " version " << ((vd.version >> 16) & 0xffff) << "."
3149 << ((vd.version >> 8) & 0xff);
3150 if ((vd.version & 0xff) != 0)
3151 outs() << "." << (vd.version & 0xff);
3154 outs() << " sdk n/a\n";
3156 outs() << " sdk " << ((vd.sdk >> 16) & 0xffff) << "."
3157 << ((vd.sdk >> 8) & 0xff);
3159 if ((vd.sdk & 0xff) != 0)
3160 outs() << "." << (vd.sdk & 0xff);
3164 static void PrintSourceVersionCommand(MachO::source_version_command sd) {
3165 outs() << " cmd LC_SOURCE_VERSION\n";
3166 outs() << " cmdsize " << sd.cmdsize;
3167 if (sd.cmdsize != sizeof(struct MachO::source_version_command))
3168 outs() << " Incorrect size\n";
3171 uint64_t a = (sd.version >> 40) & 0xffffff;
3172 uint64_t b = (sd.version >> 30) & 0x3ff;
3173 uint64_t c = (sd.version >> 20) & 0x3ff;
3174 uint64_t d = (sd.version >> 10) & 0x3ff;
3175 uint64_t e = sd.version & 0x3ff;
3176 outs() << " version " << a << "." << b;
3178 outs() << "." << c << "." << d << "." << e;
3180 outs() << "." << c << "." << d;
3186 static void PrintEntryPointCommand(MachO::entry_point_command ep) {
3187 outs() << " cmd LC_MAIN\n";
3188 outs() << " cmdsize " << ep.cmdsize;
3189 if (ep.cmdsize != sizeof(struct MachO::entry_point_command))
3190 outs() << " Incorrect size\n";
3193 outs() << " entryoff " << ep.entryoff << "\n";
3194 outs() << " stacksize " << ep.stacksize << "\n";
3197 static void PrintDylibCommand(MachO::dylib_command dl, const char *Ptr) {
3198 if (dl.cmd == MachO::LC_ID_DYLIB)
3199 outs() << " cmd LC_ID_DYLIB\n";
3200 else if (dl.cmd == MachO::LC_LOAD_DYLIB)
3201 outs() << " cmd LC_LOAD_DYLIB\n";
3202 else if (dl.cmd == MachO::LC_LOAD_WEAK_DYLIB)
3203 outs() << " cmd LC_LOAD_WEAK_DYLIB\n";
3204 else if (dl.cmd == MachO::LC_REEXPORT_DYLIB)
3205 outs() << " cmd LC_REEXPORT_DYLIB\n";
3206 else if (dl.cmd == MachO::LC_LAZY_LOAD_DYLIB)
3207 outs() << " cmd LC_LAZY_LOAD_DYLIB\n";
3208 else if (dl.cmd == MachO::LC_LOAD_UPWARD_DYLIB)
3209 outs() << " cmd LC_LOAD_UPWARD_DYLIB\n";
3211 outs() << " cmd " << dl.cmd << " (unknown)\n";
3212 outs() << " cmdsize " << dl.cmdsize;
3213 if (dl.cmdsize < sizeof(struct MachO::dylib_command))
3214 outs() << " Incorrect size\n";
3217 if (dl.dylib.name < dl.cmdsize) {
3218 const char *P = (const char *)(Ptr) + dl.dylib.name;
3219 outs() << " name " << P << " (offset " << dl.dylib.name << ")\n";
3221 outs() << " name ?(bad offset " << dl.dylib.name << ")\n";
3223 outs() << " time stamp " << dl.dylib.timestamp << " ";
3224 time_t t = dl.dylib.timestamp;
3225 outs() << ctime(&t);
3226 outs() << " current version ";
3227 if (dl.dylib.current_version == 0xffffffff)
3230 outs() << ((dl.dylib.current_version >> 16) & 0xffff) << "."
3231 << ((dl.dylib.current_version >> 8) & 0xff) << "."
3232 << (dl.dylib.current_version & 0xff) << "\n";
3233 outs() << "compatibility version ";
3234 if (dl.dylib.compatibility_version == 0xffffffff)
3237 outs() << ((dl.dylib.compatibility_version >> 16) & 0xffff) << "."
3238 << ((dl.dylib.compatibility_version >> 8) & 0xff) << "."
3239 << (dl.dylib.compatibility_version & 0xff) << "\n";
3242 static void PrintLinkEditDataCommand(MachO::linkedit_data_command ld,
3243 uint32_t object_size) {
3244 if (ld.cmd == MachO::LC_CODE_SIGNATURE)
3245 outs() << " cmd LC_FUNCTION_STARTS\n";
3246 else if (ld.cmd == MachO::LC_SEGMENT_SPLIT_INFO)
3247 outs() << " cmd LC_SEGMENT_SPLIT_INFO\n";
3248 else if (ld.cmd == MachO::LC_FUNCTION_STARTS)
3249 outs() << " cmd LC_FUNCTION_STARTS\n";
3250 else if (ld.cmd == MachO::LC_DATA_IN_CODE)
3251 outs() << " cmd LC_DATA_IN_CODE\n";
3252 else if (ld.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS)
3253 outs() << " cmd LC_DYLIB_CODE_SIGN_DRS\n";
3254 else if (ld.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT)
3255 outs() << " cmd LC_LINKER_OPTIMIZATION_HINT\n";
3257 outs() << " cmd " << ld.cmd << " (?)\n";
3258 outs() << " cmdsize " << ld.cmdsize;
3259 if (ld.cmdsize != sizeof(struct MachO::linkedit_data_command))
3260 outs() << " Incorrect size\n";
3263 outs() << " dataoff " << ld.dataoff;
3264 if (ld.dataoff > object_size)
3265 outs() << " (past end of file)\n";
3268 outs() << " datasize " << ld.datasize;
3269 uint64_t big_size = ld.dataoff;
3270 big_size += ld.datasize;
3271 if (big_size > object_size)
3272 outs() << " (past end of file)\n";
3277 static void PrintLoadCommands(const MachOObjectFile *Obj, uint32_t ncmds,
3278 uint32_t filetype, uint32_t cputype,
3280 StringRef Buf = Obj->getData();
3281 MachOObjectFile::LoadCommandInfo Command = Obj->getFirstLoadCommandInfo();
3282 for (unsigned i = 0;; ++i) {
3283 outs() << "Load command " << i << "\n";
3284 if (Command.C.cmd == MachO::LC_SEGMENT) {
3285 MachO::segment_command SLC = Obj->getSegmentLoadCommand(Command);
3286 const char *sg_segname = SLC.segname;
3287 PrintSegmentCommand(SLC.cmd, SLC.cmdsize, SLC.segname, SLC.vmaddr,
3288 SLC.vmsize, SLC.fileoff, SLC.filesize, SLC.maxprot,
3289 SLC.initprot, SLC.nsects, SLC.flags, Buf.size(),
3291 for (unsigned j = 0; j < SLC.nsects; j++) {
3292 MachO::section_64 S = Obj->getSection64(Command, j);
3293 PrintSection(S.sectname, S.segname, S.addr, S.size, S.offset, S.align,
3294 S.reloff, S.nreloc, S.flags, S.reserved1, S.reserved2,
3295 SLC.cmd, sg_segname, filetype, Buf.size(), verbose);
3297 } else if (Command.C.cmd == MachO::LC_SEGMENT_64) {
3298 MachO::segment_command_64 SLC_64 = Obj->getSegment64LoadCommand(Command);
3299 const char *sg_segname = SLC_64.segname;
3300 PrintSegmentCommand(SLC_64.cmd, SLC_64.cmdsize, SLC_64.segname,
3301 SLC_64.vmaddr, SLC_64.vmsize, SLC_64.fileoff,
3302 SLC_64.filesize, SLC_64.maxprot, SLC_64.initprot,
3303 SLC_64.nsects, SLC_64.flags, Buf.size(), verbose);
3304 for (unsigned j = 0; j < SLC_64.nsects; j++) {
3305 MachO::section_64 S_64 = Obj->getSection64(Command, j);
3306 PrintSection(S_64.sectname, S_64.segname, S_64.addr, S_64.size,
3307 S_64.offset, S_64.align, S_64.reloff, S_64.nreloc,
3308 S_64.flags, S_64.reserved1, S_64.reserved2, SLC_64.cmd,
3309 sg_segname, filetype, Buf.size(), verbose);
3311 } else if (Command.C.cmd == MachO::LC_SYMTAB) {
3312 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
3313 PrintSymtabLoadCommand(Symtab, cputype, Buf.size());
3314 } else if (Command.C.cmd == MachO::LC_DYSYMTAB) {
3315 MachO::dysymtab_command Dysymtab = Obj->getDysymtabLoadCommand();
3316 MachO::symtab_command Symtab = Obj->getSymtabLoadCommand();
3317 PrintDysymtabLoadCommand(Dysymtab, Symtab.nsyms, Buf.size(), cputype);
3318 } else if (Command.C.cmd == MachO::LC_DYLD_INFO ||
3319 Command.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
3320 MachO::dyld_info_command DyldInfo = Obj->getDyldInfoLoadCommand(Command);
3321 PrintDyldInfoLoadCommand(DyldInfo, Buf.size());
3322 } else if (Command.C.cmd == MachO::LC_LOAD_DYLINKER ||
3323 Command.C.cmd == MachO::LC_ID_DYLINKER ||
3324 Command.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
3325 MachO::dylinker_command Dyld = Obj->getDylinkerCommand(Command);
3326 PrintDyldLoadCommand(Dyld, Command.Ptr);
3327 } else if (Command.C.cmd == MachO::LC_UUID) {
3328 MachO::uuid_command Uuid = Obj->getUuidCommand(Command);
3329 PrintUuidLoadCommand(Uuid);
3330 } else if (Command.C.cmd == MachO::LC_VERSION_MIN_MACOSX) {
3331 MachO::version_min_command Vd = Obj->getVersionMinLoadCommand(Command);
3332 PrintVersionMinLoadCommand(Vd);
3333 } else if (Command.C.cmd == MachO::LC_SOURCE_VERSION) {
3334 MachO::source_version_command Sd = Obj->getSourceVersionCommand(Command);
3335 PrintSourceVersionCommand(Sd);
3336 } else if (Command.C.cmd == MachO::LC_MAIN) {
3337 MachO::entry_point_command Ep = Obj->getEntryPointCommand(Command);
3338 PrintEntryPointCommand(Ep);
3339 } else if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
3340 Command.C.cmd == MachO::LC_ID_DYLIB ||
3341 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
3342 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
3343 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
3344 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
3345 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
3346 PrintDylibCommand(Dl, Command.Ptr);
3347 } else if (Command.C.cmd == MachO::LC_CODE_SIGNATURE ||
3348 Command.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO ||
3349 Command.C.cmd == MachO::LC_FUNCTION_STARTS ||
3350 Command.C.cmd == MachO::LC_DATA_IN_CODE ||
3351 Command.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS ||
3352 Command.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
3353 MachO::linkedit_data_command Ld =
3354 Obj->getLinkeditDataLoadCommand(Command);
3355 PrintLinkEditDataCommand(Ld, Buf.size());
3357 outs() << " cmd ?(" << format("0x%08" PRIx32, Command.C.cmd)
3359 outs() << " cmdsize " << Command.C.cmdsize << "\n";
3360 // TODO: get and print the raw bytes of the load command.
3362 // TODO: print all the other kinds of load commands.
3366 Command = Obj->getNextLoadCommandInfo(Command);
3370 static void getAndPrintMachHeader(const MachOObjectFile *Obj, uint32_t &ncmds,
3371 uint32_t &filetype, uint32_t &cputype,
3373 if (Obj->is64Bit()) {
3374 MachO::mach_header_64 H_64;
3375 H_64 = Obj->getHeader64();
3376 PrintMachHeader(H_64.magic, H_64.cputype, H_64.cpusubtype, H_64.filetype,
3377 H_64.ncmds, H_64.sizeofcmds, H_64.flags, verbose);
3379 filetype = H_64.filetype;
3380 cputype = H_64.cputype;
3382 MachO::mach_header H;
3383 H = Obj->getHeader();
3384 PrintMachHeader(H.magic, H.cputype, H.cpusubtype, H.filetype, H.ncmds,
3385 H.sizeofcmds, H.flags, verbose);
3387 filetype = H.filetype;
3388 cputype = H.cputype;
3392 void llvm::printMachOFileHeader(const object::ObjectFile *Obj) {
3393 const MachOObjectFile *file = dyn_cast<const MachOObjectFile>(Obj);
3395 uint32_t filetype = 0;
3396 uint32_t cputype = 0;
3397 getAndPrintMachHeader(file, ncmds, filetype, cputype, true);
3398 PrintLoadCommands(file, ncmds, filetype, cputype, true);
3401 //===----------------------------------------------------------------------===//
3402 // export trie dumping
3403 //===----------------------------------------------------------------------===//
3405 void llvm::printMachOExportsTrie(const object::MachOObjectFile *Obj) {
3406 for (const llvm::object::ExportEntry &Entry : Obj->exports()) {
3407 uint64_t Flags = Entry.flags();
3408 bool ReExport = (Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
3409 bool WeakDef = (Flags & MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
3410 bool ThreadLocal = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
3411 MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
3412 bool Abs = ((Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK) ==
3413 MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
3414 bool Resolver = (Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
3416 outs() << "[re-export] ";
3418 outs() << format("0x%08llX ",
3419 Entry.address()); // FIXME:add in base address
3420 outs() << Entry.name();
3421 if (WeakDef || ThreadLocal || Resolver || Abs) {
3422 bool NeedsComma = false;
3425 outs() << "weak_def";
3431 outs() << "per-thread";
3437 outs() << "absolute";
3443 outs() << format("resolver=0x%08llX", Entry.other());
3449 StringRef DylibName = "unknown";
3450 int Ordinal = Entry.other() - 1;
3451 Obj->getLibraryShortNameByIndex(Ordinal, DylibName);
3452 if (Entry.otherName().empty())
3453 outs() << " (from " << DylibName << ")";
3455 outs() << " (" << Entry.otherName() << " from " << DylibName << ")";
3461 //===----------------------------------------------------------------------===//
3462 // rebase table dumping
3463 //===----------------------------------------------------------------------===//
3468 SegInfo(const object::MachOObjectFile *Obj);
3470 StringRef segmentName(uint32_t SegIndex);
3471 StringRef sectionName(uint32_t SegIndex, uint64_t SegOffset);
3472 uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
3475 struct SectionInfo {
3478 StringRef SectionName;
3479 StringRef SegmentName;
3480 uint64_t OffsetInSegment;
3481 uint64_t SegmentStartAddress;
3482 uint32_t SegmentIndex;
3484 const SectionInfo &findSection(uint32_t SegIndex, uint64_t SegOffset);
3485 SmallVector<SectionInfo, 32> Sections;
3489 SegInfo::SegInfo(const object::MachOObjectFile *Obj) {
3490 // Build table of sections so segIndex/offset pairs can be translated.
3491 uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
3492 StringRef CurSegName;
3493 uint64_t CurSegAddress;
3494 for (const SectionRef &Section : Obj->sections()) {
3496 if (error(Section.getName(Info.SectionName)))
3498 Info.Address = Section.getAddress();
3499 Info.Size = Section.getSize();
3501 Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
3502 if (!Info.SegmentName.equals(CurSegName)) {
3504 CurSegName = Info.SegmentName;
3505 CurSegAddress = Info.Address;
3507 Info.SegmentIndex = CurSegIndex - 1;
3508 Info.OffsetInSegment = Info.Address - CurSegAddress;
3509 Info.SegmentStartAddress = CurSegAddress;
3510 Sections.push_back(Info);
3514 StringRef SegInfo::segmentName(uint32_t SegIndex) {
3515 for (const SectionInfo &SI : Sections) {
3516 if (SI.SegmentIndex == SegIndex)
3517 return SI.SegmentName;
3519 llvm_unreachable("invalid segIndex");
3522 const SegInfo::SectionInfo &SegInfo::findSection(uint32_t SegIndex,
3523 uint64_t OffsetInSeg) {
3524 for (const SectionInfo &SI : Sections) {
3525 if (SI.SegmentIndex != SegIndex)
3527 if (SI.OffsetInSegment > OffsetInSeg)
3529 if (OffsetInSeg >= (SI.OffsetInSegment + SI.Size))
3533 llvm_unreachable("segIndex and offset not in any section");
3536 StringRef SegInfo::sectionName(uint32_t SegIndex, uint64_t OffsetInSeg) {
3537 return findSection(SegIndex, OffsetInSeg).SectionName;
3540 uint64_t SegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
3541 const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
3542 return SI.SegmentStartAddress + OffsetInSeg;
3545 void llvm::printMachORebaseTable(const object::MachOObjectFile *Obj) {
3546 // Build table of sections so names can used in final output.
3547 SegInfo sectionTable(Obj);
3549 outs() << "segment section address type\n";
3550 for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
3551 uint32_t SegIndex = Entry.segmentIndex();
3552 uint64_t OffsetInSeg = Entry.segmentOffset();
3553 StringRef SegmentName = sectionTable.segmentName(SegIndex);
3554 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
3555 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
3557 // Table lines look like: __DATA __nl_symbol_ptr 0x0000F00C pointer
3558 outs() << format("%-8s %-18s 0x%08" PRIX64 " %s\n",
3559 SegmentName.str().c_str(), SectionName.str().c_str(),
3560 Address, Entry.typeName().str().c_str());
3564 static StringRef ordinalName(const object::MachOObjectFile *Obj, int Ordinal) {
3565 StringRef DylibName;
3567 case MachO::BIND_SPECIAL_DYLIB_SELF:
3568 return "this-image";
3569 case MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE:
3570 return "main-executable";
3571 case MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP:
3572 return "flat-namespace";
3575 std::error_code EC =
3576 Obj->getLibraryShortNameByIndex(Ordinal - 1, DylibName);
3578 return "<<bad library ordinal>>";
3582 return "<<unknown special ordinal>>";
3585 //===----------------------------------------------------------------------===//
3586 // bind table dumping
3587 //===----------------------------------------------------------------------===//
3589 void llvm::printMachOBindTable(const object::MachOObjectFile *Obj) {
3590 // Build table of sections so names can used in final output.
3591 SegInfo sectionTable(Obj);
3593 outs() << "segment section address type "
3594 "addend dylib symbol\n";
3595 for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
3596 uint32_t SegIndex = Entry.segmentIndex();
3597 uint64_t OffsetInSeg = Entry.segmentOffset();
3598 StringRef SegmentName = sectionTable.segmentName(SegIndex);
3599 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
3600 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
3602 // Table lines look like:
3603 // __DATA __got 0x00012010 pointer 0 libSystem ___stack_chk_guard
3605 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT)
3606 Attr = " (weak_import)";
3607 outs() << left_justify(SegmentName, 8) << " "
3608 << left_justify(SectionName, 18) << " "
3609 << format_hex(Address, 10, true) << " "
3610 << left_justify(Entry.typeName(), 8) << " "
3611 << format_decimal(Entry.addend(), 8) << " "
3612 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
3613 << Entry.symbolName() << Attr << "\n";
3617 //===----------------------------------------------------------------------===//
3618 // lazy bind table dumping
3619 //===----------------------------------------------------------------------===//
3621 void llvm::printMachOLazyBindTable(const object::MachOObjectFile *Obj) {
3622 // Build table of sections so names can used in final output.
3623 SegInfo sectionTable(Obj);
3625 outs() << "segment section address "
3627 for (const llvm::object::MachOBindEntry &Entry : Obj->lazyBindTable()) {
3628 uint32_t SegIndex = Entry.segmentIndex();
3629 uint64_t OffsetInSeg = Entry.segmentOffset();
3630 StringRef SegmentName = sectionTable.segmentName(SegIndex);
3631 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
3632 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
3634 // Table lines look like:
3635 // __DATA __got 0x00012010 libSystem ___stack_chk_guard
3636 outs() << left_justify(SegmentName, 8) << " "
3637 << left_justify(SectionName, 18) << " "
3638 << format_hex(Address, 10, true) << " "
3639 << left_justify(ordinalName(Obj, Entry.ordinal()), 16) << " "
3640 << Entry.symbolName() << "\n";
3644 //===----------------------------------------------------------------------===//
3645 // weak bind table dumping
3646 //===----------------------------------------------------------------------===//
3648 void llvm::printMachOWeakBindTable(const object::MachOObjectFile *Obj) {
3649 // Build table of sections so names can used in final output.
3650 SegInfo sectionTable(Obj);
3652 outs() << "segment section address "
3653 "type addend symbol\n";
3654 for (const llvm::object::MachOBindEntry &Entry : Obj->weakBindTable()) {
3655 // Strong symbols don't have a location to update.
3656 if (Entry.flags() & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) {
3657 outs() << " strong "
3658 << Entry.symbolName() << "\n";
3661 uint32_t SegIndex = Entry.segmentIndex();
3662 uint64_t OffsetInSeg = Entry.segmentOffset();
3663 StringRef SegmentName = sectionTable.segmentName(SegIndex);
3664 StringRef SectionName = sectionTable.sectionName(SegIndex, OffsetInSeg);
3665 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
3667 // Table lines look like:
3668 // __DATA __data 0x00001000 pointer 0 _foo
3669 outs() << left_justify(SegmentName, 8) << " "
3670 << left_justify(SectionName, 18) << " "
3671 << format_hex(Address, 10, true) << " "
3672 << left_justify(Entry.typeName(), 8) << " "
3673 << format_decimal(Entry.addend(), 8) << " " << Entry.symbolName()
3678 // get_dyld_bind_info_symbolname() is used for disassembly and passed an
3679 // address, ReferenceValue, in the Mach-O file and looks in the dyld bind
3680 // information for that address. If the address is found its binding symbol
3681 // name is returned. If not nullptr is returned.
3682 static const char *get_dyld_bind_info_symbolname(uint64_t ReferenceValue,
3683 struct DisassembleInfo *info) {
3684 if (info->bindtable == nullptr) {
3685 info->bindtable = new (BindTable);
3686 SegInfo sectionTable(info->O);
3687 for (const llvm::object::MachOBindEntry &Entry : info->O->bindTable()) {
3688 uint32_t SegIndex = Entry.segmentIndex();
3689 uint64_t OffsetInSeg = Entry.segmentOffset();
3690 uint64_t Address = sectionTable.address(SegIndex, OffsetInSeg);
3691 const char *SymbolName = nullptr;
3692 StringRef name = Entry.symbolName();
3694 SymbolName = name.data();
3695 info->bindtable->push_back(std::make_pair(Address, SymbolName));
3698 for (bind_table_iterator BI = info->bindtable->begin(),
3699 BE = info->bindtable->end();
3701 uint64_t Address = BI->first;
3702 if (ReferenceValue == Address) {
3703 const char *SymbolName = BI->second;