[ARM] sext of a load is free
[llvm-core.git] / tools / llvm-readobj / MachODumper.cpp
blob17883df49e5856914bcb3ca4c0cab9efd3ea5a26
1 //===- MachODumper.cpp - Object file dumping utility for llvm -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the MachO-specific dumper for llvm-readobj.
11 //===----------------------------------------------------------------------===//
13 #include "Error.h"
14 #include "ObjDumper.h"
15 #include "StackMapPrinter.h"
16 #include "llvm-readobj.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Object/MachO.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/ScopedPrinter.h"
23 using namespace llvm;
24 using namespace object;
26 namespace {
28 class MachODumper : public ObjDumper {
29 public:
30 MachODumper(const MachOObjectFile *Obj, ScopedPrinter &Writer)
31 : ObjDumper(Writer), Obj(Obj) {}
33 void printFileHeaders() override;
34 void printSectionHeaders() override;
35 void printRelocations() override;
36 void printUnwindInfo() override;
37 void printStackMap() const override;
39 void printNeededLibraries() override;
41 // MachO-specific.
42 void printMachODataInCode() override;
43 void printMachOVersionMin() override;
44 void printMachODysymtab() override;
45 void printMachOSegment() override;
46 void printMachOIndirectSymbols() override;
47 void printMachOLinkerOptions () override;
49 private:
50 template<class MachHeader>
51 void printFileHeaders(const MachHeader &Header);
53 void printSymbols() override;
54 void printDynamicSymbols() override;
55 void printSymbol(const SymbolRef &Symbol);
57 void printRelocation(const RelocationRef &Reloc);
59 void printRelocation(const MachOObjectFile *Obj, const RelocationRef &Reloc);
61 void printSectionHeaders(const MachOObjectFile *Obj);
63 const MachOObjectFile *Obj;
66 } // namespace
69 namespace llvm {
71 std::error_code createMachODumper(const object::ObjectFile *Obj,
72 ScopedPrinter &Writer,
73 std::unique_ptr<ObjDumper> &Result) {
74 const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(Obj);
75 if (!MachOObj)
76 return readobj_error::unsupported_obj_file_format;
78 Result.reset(new MachODumper(MachOObj, Writer));
79 return readobj_error::success;
82 } // namespace llvm
84 static const EnumEntry<uint32_t> MachOMagics[] = {
85 { "Magic", MachO::MH_MAGIC },
86 { "Cigam", MachO::MH_CIGAM },
87 { "Magic64", MachO::MH_MAGIC_64 },
88 { "Cigam64", MachO::MH_CIGAM_64 },
89 { "FatMagic", MachO::FAT_MAGIC },
90 { "FatCigam", MachO::FAT_CIGAM },
93 static const EnumEntry<uint32_t> MachOHeaderFileTypes[] = {
94 { "Relocatable", MachO::MH_OBJECT },
95 { "Executable", MachO::MH_EXECUTE },
96 { "FixedVMLibrary", MachO::MH_FVMLIB },
97 { "Core", MachO::MH_CORE },
98 { "PreloadedExecutable", MachO::MH_PRELOAD },
99 { "DynamicLibrary", MachO::MH_DYLIB },
100 { "DynamicLinker", MachO::MH_DYLINKER },
101 { "Bundle", MachO::MH_BUNDLE },
102 { "DynamicLibraryStub", MachO::MH_DYLIB_STUB },
103 { "DWARFSymbol", MachO::MH_DSYM },
104 { "KextBundle", MachO::MH_KEXT_BUNDLE },
107 static const EnumEntry<uint32_t> MachOHeaderCpuTypes[] = {
108 { "Any" , static_cast<uint32_t>(MachO::CPU_TYPE_ANY) },
109 { "X86" , MachO::CPU_TYPE_X86 },
110 { "X86-64" , MachO::CPU_TYPE_X86_64 },
111 { "Mc98000" , MachO::CPU_TYPE_MC98000 },
112 { "Arm" , MachO::CPU_TYPE_ARM },
113 { "Arm64" , MachO::CPU_TYPE_ARM64 },
114 { "Sparc" , MachO::CPU_TYPE_SPARC },
115 { "PowerPC" , MachO::CPU_TYPE_POWERPC },
116 { "PowerPC64" , MachO::CPU_TYPE_POWERPC64 },
119 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX86[] = {
120 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_I386_ALL),
121 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_386),
122 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486),
123 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486SX),
124 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_586),
125 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTPRO),
126 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M3),
127 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M5),
128 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON),
129 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON_MOBILE),
130 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3),
131 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_M),
132 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_XEON),
133 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_M),
134 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4),
135 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4_M),
136 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM),
137 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM_2),
138 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON),
139 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON_MP),
142 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX64[] = {
143 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_ALL),
144 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_ARCH1),
145 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_H),
148 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM[] = {
149 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_ALL),
150 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V4T),
151 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6),
152 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5),
153 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5TEJ),
154 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_XSCALE),
155 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7),
156 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7S),
157 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7K),
158 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6M),
159 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7M),
160 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7EM),
163 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM64[] = {
164 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64_ALL),
165 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64E),
168 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesSPARC[] = {
169 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_SPARC_ALL),
172 static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesPPC[] = {
173 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_ALL),
174 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_601),
175 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_602),
176 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603),
177 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603e),
178 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603ev),
179 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604),
180 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604e),
181 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_620),
182 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_750),
183 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7400),
184 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7450),
185 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_970),
188 static const EnumEntry<uint32_t> MachOHeaderFlags[] = {
189 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOUNDEFS),
190 LLVM_READOBJ_ENUM_ENT(MachO, MH_INCRLINK),
191 LLVM_READOBJ_ENUM_ENT(MachO, MH_DYLDLINK),
192 LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDATLOAD),
193 LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBOUND),
194 LLVM_READOBJ_ENUM_ENT(MachO, MH_SPLIT_SEGS),
195 LLVM_READOBJ_ENUM_ENT(MachO, MH_LAZY_INIT),
196 LLVM_READOBJ_ENUM_ENT(MachO, MH_TWOLEVEL),
197 LLVM_READOBJ_ENUM_ENT(MachO, MH_FORCE_FLAT),
198 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOMULTIDEFS),
199 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOFIXPREBINDING),
200 LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBINDABLE),
201 LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLMODSBOUND),
202 LLVM_READOBJ_ENUM_ENT(MachO, MH_SUBSECTIONS_VIA_SYMBOLS),
203 LLVM_READOBJ_ENUM_ENT(MachO, MH_CANONICAL),
204 LLVM_READOBJ_ENUM_ENT(MachO, MH_WEAK_DEFINES),
205 LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDS_TO_WEAK),
206 LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLOW_STACK_EXECUTION),
207 LLVM_READOBJ_ENUM_ENT(MachO, MH_ROOT_SAFE),
208 LLVM_READOBJ_ENUM_ENT(MachO, MH_SETUID_SAFE),
209 LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_REEXPORTED_DYLIBS),
210 LLVM_READOBJ_ENUM_ENT(MachO, MH_PIE),
211 LLVM_READOBJ_ENUM_ENT(MachO, MH_DEAD_STRIPPABLE_DYLIB),
212 LLVM_READOBJ_ENUM_ENT(MachO, MH_HAS_TLV_DESCRIPTORS),
213 LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_HEAP_EXECUTION),
214 LLVM_READOBJ_ENUM_ENT(MachO, MH_APP_EXTENSION_SAFE),
217 static const EnumEntry<unsigned> MachOSectionAttributes[] = {
218 { "LocReloc" , 1 << 0 /*S_ATTR_LOC_RELOC */ },
219 { "ExtReloc" , 1 << 1 /*S_ATTR_EXT_RELOC */ },
220 { "SomeInstructions" , 1 << 2 /*S_ATTR_SOME_INSTRUCTIONS */ },
221 { "Debug" , 1 << 17 /*S_ATTR_DEBUG */ },
222 { "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ },
223 { "LiveSupport" , 1 << 19 /*S_ATTR_LIVE_SUPPORT */ },
224 { "NoDeadStrip" , 1 << 20 /*S_ATTR_NO_DEAD_STRIP */ },
225 { "StripStaticSyms" , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS */ },
226 { "NoTOC" , 1 << 22 /*S_ATTR_NO_TOC */ },
227 { "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS */ },
230 static const EnumEntry<unsigned> MachOSymbolRefTypes[] = {
231 { "UndefinedNonLazy", 0 },
232 { "ReferenceFlagUndefinedLazy", 1 },
233 { "ReferenceFlagDefined", 2 },
234 { "ReferenceFlagPrivateDefined", 3 },
235 { "ReferenceFlagPrivateUndefinedNonLazy", 4 },
236 { "ReferenceFlagPrivateUndefinedLazy", 5 }
239 static const EnumEntry<unsigned> MachOSymbolFlags[] = {
240 { "ReferencedDynamically", 0x10 },
241 { "NoDeadStrip", 0x20 },
242 { "WeakRef", 0x40 },
243 { "WeakDef", 0x80 },
244 { "AltEntry", 0x200 },
247 static const EnumEntry<unsigned> MachOSymbolTypes[] = {
248 { "Undef", 0x0 },
249 { "Abs", 0x2 },
250 { "Indirect", 0xA },
251 { "PreboundUndef", 0xC },
252 { "Section", 0xE }
255 namespace {
256 struct MachOSection {
257 ArrayRef<char> Name;
258 ArrayRef<char> SegmentName;
259 uint64_t Address;
260 uint64_t Size;
261 uint32_t Offset;
262 uint32_t Alignment;
263 uint32_t RelocationTableOffset;
264 uint32_t NumRelocationTableEntries;
265 uint32_t Flags;
266 uint32_t Reserved1;
267 uint32_t Reserved2;
268 uint32_t Reserved3;
271 struct MachOSegment {
272 std::string CmdName;
273 std::string SegName;
274 uint64_t cmdsize;
275 uint64_t vmaddr;
276 uint64_t vmsize;
277 uint64_t fileoff;
278 uint64_t filesize;
279 uint32_t maxprot;
280 uint32_t initprot;
281 uint32_t nsects;
282 uint32_t flags;
285 struct MachOSymbol {
286 uint32_t StringIndex;
287 uint8_t Type;
288 uint8_t SectionIndex;
289 uint16_t Flags;
290 uint64_t Value;
294 static std::string getMask(uint32_t prot)
296 // TODO (davide): This always assumes prot is valid.
297 // Catch mistakes and report if needed.
298 std::string Prot;
299 Prot = "";
300 Prot += (prot & MachO::VM_PROT_READ) ? "r" : "-";
301 Prot += (prot & MachO::VM_PROT_WRITE) ? "w" : "-";
302 Prot += (prot & MachO::VM_PROT_EXECUTE) ? "x" : "-";
303 return Prot;
306 static void getSection(const MachOObjectFile *Obj,
307 DataRefImpl Sec,
308 MachOSection &Section) {
309 if (!Obj->is64Bit()) {
310 MachO::section Sect = Obj->getSection(Sec);
311 Section.Address = Sect.addr;
312 Section.Size = Sect.size;
313 Section.Offset = Sect.offset;
314 Section.Alignment = Sect.align;
315 Section.RelocationTableOffset = Sect.reloff;
316 Section.NumRelocationTableEntries = Sect.nreloc;
317 Section.Flags = Sect.flags;
318 Section.Reserved1 = Sect.reserved1;
319 Section.Reserved2 = Sect.reserved2;
320 return;
322 MachO::section_64 Sect = Obj->getSection64(Sec);
323 Section.Address = Sect.addr;
324 Section.Size = Sect.size;
325 Section.Offset = Sect.offset;
326 Section.Alignment = Sect.align;
327 Section.RelocationTableOffset = Sect.reloff;
328 Section.NumRelocationTableEntries = Sect.nreloc;
329 Section.Flags = Sect.flags;
330 Section.Reserved1 = Sect.reserved1;
331 Section.Reserved2 = Sect.reserved2;
332 Section.Reserved3 = Sect.reserved3;
335 static void getSegment(const MachOObjectFile *Obj,
336 const MachOObjectFile::LoadCommandInfo &L,
337 MachOSegment &Segment) {
338 if (!Obj->is64Bit()) {
339 MachO::segment_command SC = Obj->getSegmentLoadCommand(L);
340 Segment.CmdName = "LC_SEGMENT";
341 Segment.SegName = SC.segname;
342 Segment.cmdsize = SC.cmdsize;
343 Segment.vmaddr = SC.vmaddr;
344 Segment.vmsize = SC.vmsize;
345 Segment.fileoff = SC.fileoff;
346 Segment.filesize = SC.filesize;
347 Segment.maxprot = SC.maxprot;
348 Segment.initprot = SC.initprot;
349 Segment.nsects = SC.nsects;
350 Segment.flags = SC.flags;
351 return;
353 MachO::segment_command_64 SC = Obj->getSegment64LoadCommand(L);
354 Segment.CmdName = "LC_SEGMENT_64";
355 Segment.SegName = SC.segname;
356 Segment.cmdsize = SC.cmdsize;
357 Segment.vmaddr = SC.vmaddr;
358 Segment.vmsize = SC.vmsize;
359 Segment.fileoff = SC.fileoff;
360 Segment.filesize = SC.filesize;
361 Segment.maxprot = SC.maxprot;
362 Segment.initprot = SC.initprot;
363 Segment.nsects = SC.nsects;
364 Segment.flags = SC.flags;
367 static void getSymbol(const MachOObjectFile *Obj,
368 DataRefImpl DRI,
369 MachOSymbol &Symbol) {
370 if (!Obj->is64Bit()) {
371 MachO::nlist Entry = Obj->getSymbolTableEntry(DRI);
372 Symbol.StringIndex = Entry.n_strx;
373 Symbol.Type = Entry.n_type;
374 Symbol.SectionIndex = Entry.n_sect;
375 Symbol.Flags = Entry.n_desc;
376 Symbol.Value = Entry.n_value;
377 return;
379 MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI);
380 Symbol.StringIndex = Entry.n_strx;
381 Symbol.Type = Entry.n_type;
382 Symbol.SectionIndex = Entry.n_sect;
383 Symbol.Flags = Entry.n_desc;
384 Symbol.Value = Entry.n_value;
387 void MachODumper::printFileHeaders() {
388 DictScope H(W, "MachHeader");
389 if (!Obj->is64Bit()) {
390 printFileHeaders(Obj->getHeader());
391 } else {
392 printFileHeaders(Obj->getHeader64());
393 W.printHex("Reserved", Obj->getHeader64().reserved);
397 template<class MachHeader>
398 void MachODumper::printFileHeaders(const MachHeader &Header) {
399 W.printEnum("Magic", Header.magic, makeArrayRef(MachOMagics));
400 W.printEnum("CpuType", Header.cputype, makeArrayRef(MachOHeaderCpuTypes));
401 uint32_t subtype = Header.cpusubtype & ~MachO::CPU_SUBTYPE_MASK;
402 switch (Header.cputype) {
403 case MachO::CPU_TYPE_X86:
404 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX86));
405 break;
406 case MachO::CPU_TYPE_X86_64:
407 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX64));
408 break;
409 case MachO::CPU_TYPE_ARM:
410 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM));
411 break;
412 case MachO::CPU_TYPE_POWERPC:
413 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesPPC));
414 break;
415 case MachO::CPU_TYPE_SPARC:
416 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesSPARC));
417 break;
418 case MachO::CPU_TYPE_ARM64:
419 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM64));
420 break;
421 case MachO::CPU_TYPE_POWERPC64:
422 default:
423 W.printHex("CpuSubtype", subtype);
425 W.printEnum("FileType", Header.filetype, makeArrayRef(MachOHeaderFileTypes));
426 W.printNumber("NumOfLoadCommands", Header.ncmds);
427 W.printNumber("SizeOfLoadCommands", Header.sizeofcmds);
428 W.printFlags("Flags", Header.flags, makeArrayRef(MachOHeaderFlags));
431 void MachODumper::printSectionHeaders() { return printSectionHeaders(Obj); }
433 void MachODumper::printSectionHeaders(const MachOObjectFile *Obj) {
434 ListScope Group(W, "Sections");
436 int SectionIndex = -1;
437 for (const SectionRef &Section : Obj->sections()) {
438 ++SectionIndex;
440 MachOSection MOSection;
441 getSection(Obj, Section.getRawDataRefImpl(), MOSection);
442 DataRefImpl DR = Section.getRawDataRefImpl();
444 StringRef Name;
445 error(Section.getName(Name));
447 ArrayRef<char> RawName = Obj->getSectionRawName(DR);
448 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
449 ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
451 DictScope SectionD(W, "Section");
452 W.printNumber("Index", SectionIndex);
453 W.printBinary("Name", Name, RawName);
454 W.printBinary("Segment", SegmentName, RawSegmentName);
455 W.printHex("Address", MOSection.Address);
456 W.printHex("Size", MOSection.Size);
457 W.printNumber("Offset", MOSection.Offset);
458 W.printNumber("Alignment", MOSection.Alignment);
459 W.printHex("RelocationOffset", MOSection.RelocationTableOffset);
460 W.printNumber("RelocationCount", MOSection.NumRelocationTableEntries);
461 W.printEnum("Type", MOSection.Flags & 0xFF,
462 makeArrayRef(MachOSectionAttributes));
463 W.printFlags("Attributes", MOSection.Flags >> 8,
464 makeArrayRef(MachOSectionAttributes));
465 W.printHex("Reserved1", MOSection.Reserved1);
466 W.printHex("Reserved2", MOSection.Reserved2);
467 if (Obj->is64Bit())
468 W.printHex("Reserved3", MOSection.Reserved3);
470 if (opts::SectionRelocations) {
471 ListScope D(W, "Relocations");
472 for (const RelocationRef &Reloc : Section.relocations())
473 printRelocation(Reloc);
476 if (opts::SectionSymbols) {
477 ListScope D(W, "Symbols");
478 for (const SymbolRef &Symbol : Obj->symbols()) {
479 if (!Section.containsSymbol(Symbol))
480 continue;
482 printSymbol(Symbol);
486 if (opts::SectionData && !Section.isBSS())
487 W.printBinaryBlock("SectionData", unwrapOrError(Obj->getFileName(),
488 Section.getContents()));
492 void MachODumper::printRelocations() {
493 ListScope D(W, "Relocations");
495 std::error_code EC;
496 for (const SectionRef &Section : Obj->sections()) {
497 StringRef Name;
498 error(Section.getName(Name));
500 bool PrintedGroup = false;
501 for (const RelocationRef &Reloc : Section.relocations()) {
502 if (!PrintedGroup) {
503 W.startLine() << "Section " << Name << " {\n";
504 W.indent();
505 PrintedGroup = true;
508 printRelocation(Reloc);
511 if (PrintedGroup) {
512 W.unindent();
513 W.startLine() << "}\n";
518 void MachODumper::printRelocation(const RelocationRef &Reloc) {
519 return printRelocation(Obj, Reloc);
522 void MachODumper::printRelocation(const MachOObjectFile *Obj,
523 const RelocationRef &Reloc) {
524 uint64_t Offset = Reloc.getOffset();
525 SmallString<32> RelocName;
526 Reloc.getTypeName(RelocName);
528 DataRefImpl DR = Reloc.getRawDataRefImpl();
529 MachO::any_relocation_info RE = Obj->getRelocation(DR);
530 bool IsScattered = Obj->isRelocationScattered(RE);
531 bool IsExtern = !IsScattered && Obj->getPlainRelocationExternal(RE);
533 StringRef TargetName;
534 if (IsExtern) {
535 symbol_iterator Symbol = Reloc.getSymbol();
536 if (Symbol != Obj->symbol_end()) {
537 Expected<StringRef> TargetNameOrErr = Symbol->getName();
538 if (!TargetNameOrErr)
539 error(errorToErrorCode(TargetNameOrErr.takeError()));
540 TargetName = *TargetNameOrErr;
542 } else if (!IsScattered) {
543 section_iterator SecI = Obj->getRelocationSection(DR);
544 if (SecI != Obj->section_end()) {
545 error(SecI->getName(TargetName));
548 if (TargetName.empty())
549 TargetName = "-";
551 if (opts::ExpandRelocs) {
552 DictScope Group(W, "Relocation");
553 W.printHex("Offset", Offset);
554 W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE));
555 W.printNumber("Length", Obj->getAnyRelocationLength(RE));
556 W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
557 if (IsScattered) {
558 W.printHex("Value", Obj->getScatteredRelocationValue(RE));
559 } else {
560 const char *Kind = IsExtern ? "Symbol" : "Section";
561 W.printNumber(Kind, TargetName, Obj->getPlainRelocationSymbolNum(RE));
563 } else {
564 SmallString<32> SymbolNameOrOffset("0x");
565 if (IsScattered) {
566 // Scattered relocations don't really have an associated symbol for some
567 // reason, even if one exists in the symtab at the correct address.
568 SymbolNameOrOffset += utohexstr(Obj->getScatteredRelocationValue(RE));
569 } else {
570 SymbolNameOrOffset = TargetName;
573 raw_ostream& OS = W.startLine();
574 OS << W.hex(Offset)
575 << " " << Obj->getAnyRelocationPCRel(RE)
576 << " " << Obj->getAnyRelocationLength(RE);
577 if (IsScattered)
578 OS << " n/a";
579 else
580 OS << " " << Obj->getPlainRelocationExternal(RE);
581 OS << " " << RelocName
582 << " " << IsScattered
583 << " " << SymbolNameOrOffset
584 << "\n";
588 void MachODumper::printSymbols() {
589 ListScope Group(W, "Symbols");
591 for (const SymbolRef &Symbol : Obj->symbols()) {
592 printSymbol(Symbol);
596 void MachODumper::printDynamicSymbols() {
597 ListScope Group(W, "DynamicSymbols");
600 void MachODumper::printSymbol(const SymbolRef &Symbol) {
601 StringRef SymbolName;
602 Expected<StringRef> SymbolNameOrErr = Symbol.getName();
603 if (!SymbolNameOrErr) {
604 // TODO: Actually report errors helpfully.
605 consumeError(SymbolNameOrErr.takeError());
606 } else
607 SymbolName = *SymbolNameOrErr;
609 MachOSymbol MOSymbol;
610 getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
612 StringRef SectionName = "";
613 Expected<section_iterator> SecIOrErr = Symbol.getSection();
614 error(errorToErrorCode(SecIOrErr.takeError()));
615 section_iterator SecI = *SecIOrErr;
616 if (SecI != Obj->section_end())
617 error(SecI->getName(SectionName));
619 DictScope D(W, "Symbol");
620 W.printNumber("Name", SymbolName, MOSymbol.StringIndex);
621 if (MOSymbol.Type & MachO::N_STAB) {
622 W.printHex("Type", "SymDebugTable", MOSymbol.Type);
623 } else {
624 if (MOSymbol.Type & MachO::N_PEXT)
625 W.startLine() << "PrivateExtern\n";
626 if (MOSymbol.Type & MachO::N_EXT)
627 W.startLine() << "Extern\n";
628 W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE),
629 makeArrayRef(MachOSymbolTypes));
631 W.printHex("Section", SectionName, MOSymbol.SectionIndex);
632 W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0xF),
633 makeArrayRef(MachOSymbolRefTypes));
634 W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0xF),
635 makeArrayRef(MachOSymbolFlags));
636 W.printHex("Value", MOSymbol.Value);
639 void MachODumper::printUnwindInfo() {
640 W.startLine() << "UnwindInfo not implemented.\n";
643 void MachODumper::printStackMap() const {
644 object::SectionRef StackMapSection;
645 for (auto Sec : Obj->sections()) {
646 StringRef Name;
647 Sec.getName(Name);
648 if (Name == "__llvm_stackmaps") {
649 StackMapSection = Sec;
650 break;
654 if (StackMapSection == object::SectionRef())
655 return;
657 StringRef StackMapContents =
658 unwrapOrError(Obj->getFileName(), StackMapSection.getContents());
659 ArrayRef<uint8_t> StackMapContentsArray =
660 arrayRefFromStringRef(StackMapContents);
662 if (Obj->isLittleEndian())
663 prettyPrintStackMap(
664 W, StackMapParser<support::little>(StackMapContentsArray));
665 else
666 prettyPrintStackMap(
667 W, StackMapParser<support::big>(StackMapContentsArray));
670 void MachODumper::printNeededLibraries() {
671 ListScope D(W, "NeededLibraries");
673 using LibsTy = std::vector<StringRef>;
674 LibsTy Libs;
676 for (const auto &Command : Obj->load_commands()) {
677 if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
678 Command.C.cmd == MachO::LC_ID_DYLIB ||
679 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
680 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
681 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
682 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
683 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
684 if (Dl.dylib.name < Dl.cmdsize) {
685 auto *P = static_cast<const char*>(Command.Ptr) + Dl.dylib.name;
686 Libs.push_back(P);
691 llvm::stable_sort(Libs);
693 for (const auto &L : Libs) {
694 W.startLine() << L << "\n";
698 void MachODumper::printMachODataInCode() {
699 for (const auto &Load : Obj->load_commands()) {
700 if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
701 MachO::linkedit_data_command LLC = Obj->getLinkeditDataLoadCommand(Load);
702 DictScope Group(W, "DataInCode");
703 W.printNumber("Data offset", LLC.dataoff);
704 W.printNumber("Data size", LLC.datasize);
705 ListScope D(W, "Data entries");
706 unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry);
707 for (unsigned i = 0; i < NumRegions; ++i) {
708 MachO::data_in_code_entry DICE = Obj->getDataInCodeTableEntry(
709 LLC.dataoff, i);
710 DictScope Group(W, "Entry");
711 W.printNumber("Index", i);
712 W.printNumber("Offset", DICE.offset);
713 W.printNumber("Length", DICE.length);
714 W.printNumber("Kind", DICE.kind);
720 void MachODumper::printMachOVersionMin() {
721 for (const auto &Load : Obj->load_commands()) {
722 StringRef Cmd;
723 switch (Load.C.cmd) {
724 case MachO::LC_VERSION_MIN_MACOSX:
725 Cmd = "LC_VERSION_MIN_MACOSX";
726 break;
727 case MachO::LC_VERSION_MIN_IPHONEOS:
728 Cmd = "LC_VERSION_MIN_IPHONEOS";
729 break;
730 case MachO::LC_VERSION_MIN_TVOS:
731 Cmd = "LC_VERSION_MIN_TVOS";
732 break;
733 case MachO::LC_VERSION_MIN_WATCHOS:
734 Cmd = "LC_VERSION_MIN_WATCHOS";
735 break;
736 case MachO::LC_BUILD_VERSION:
737 Cmd = "LC_BUILD_VERSION";
738 break;
739 default:
740 continue;
743 DictScope Group(W, "MinVersion");
744 // Handle LC_BUILD_VERSION.
745 if (Load.C.cmd == MachO::LC_BUILD_VERSION) {
746 MachO::build_version_command BVC = Obj->getBuildVersionLoadCommand(Load);
747 W.printString("Cmd", Cmd);
748 W.printNumber("Size", BVC.cmdsize);
749 W.printString("Platform",
750 MachOObjectFile::getBuildPlatform(BVC.platform));
751 W.printString("Version", MachOObjectFile::getVersionString(BVC.minos));
752 if (BVC.sdk)
753 W.printString("SDK", MachOObjectFile::getVersionString(BVC.sdk));
754 else
755 W.printString("SDK", StringRef("n/a"));
756 continue;
759 MachO::version_min_command VMC = Obj->getVersionMinLoadCommand(Load);
760 W.printString("Cmd", Cmd);
761 W.printNumber("Size", VMC.cmdsize);
762 SmallString<32> Version;
763 Version = utostr(MachOObjectFile::getVersionMinMajor(VMC, false)) + "." +
764 utostr(MachOObjectFile::getVersionMinMinor(VMC, false));
765 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, false);
766 if (Update != 0)
767 Version += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, false));
768 W.printString("Version", Version);
769 SmallString<32> SDK;
770 if (VMC.sdk == 0)
771 SDK = "n/a";
772 else {
773 SDK = utostr(MachOObjectFile::getVersionMinMajor(VMC, true)) + "." +
774 utostr(MachOObjectFile::getVersionMinMinor(VMC, true));
775 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, true);
776 if (Update != 0)
777 SDK += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, true));
779 W.printString("SDK", SDK);
783 void MachODumper::printMachODysymtab() {
784 for (const auto &Load : Obj->load_commands()) {
785 if (Load.C.cmd == MachO::LC_DYSYMTAB) {
786 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand();
787 DictScope Group(W, "Dysymtab");
788 W.printNumber("ilocalsym", DLC.ilocalsym);
789 W.printNumber("nlocalsym", DLC.nlocalsym);
790 W.printNumber("iextdefsym", DLC.iextdefsym);
791 W.printNumber("nextdefsym", DLC.nextdefsym);
792 W.printNumber("iundefsym", DLC.iundefsym);
793 W.printNumber("nundefsym", DLC.nundefsym);
794 W.printNumber("tocoff", DLC.tocoff);
795 W.printNumber("ntoc", DLC.ntoc);
796 W.printNumber("modtaboff", DLC.modtaboff);
797 W.printNumber("nmodtab", DLC.nmodtab);
798 W.printNumber("extrefsymoff", DLC.extrefsymoff);
799 W.printNumber("nextrefsyms", DLC.nextrefsyms);
800 W.printNumber("indirectsymoff", DLC.indirectsymoff);
801 W.printNumber("nindirectsyms", DLC.nindirectsyms);
802 W.printNumber("extreloff", DLC.extreloff);
803 W.printNumber("nextrel", DLC.nextrel);
804 W.printNumber("locreloff", DLC.locreloff);
805 W.printNumber("nlocrel", DLC.nlocrel);
810 void MachODumper::printMachOSegment() {
811 for (const auto &Load : Obj->load_commands()) {
812 if (Load.C.cmd == MachO::LC_SEGMENT || Load.C.cmd == MachO::LC_SEGMENT_64) {
813 MachOSegment MOSegment;
814 getSegment(Obj, Load, MOSegment);
815 DictScope Group(W, "Segment");
816 W.printString("Cmd", MOSegment.CmdName);
817 W.printString("Name", MOSegment.SegName);
818 W.printNumber("Size", MOSegment.cmdsize);
819 W.printHex("vmaddr", MOSegment.vmaddr);
820 W.printHex("vmsize", MOSegment.vmsize);
821 W.printNumber("fileoff", MOSegment.fileoff);
822 W.printNumber("filesize", MOSegment.filesize);
823 W.printString("maxprot", getMask(MOSegment.maxprot));
824 W.printString("initprot", getMask(MOSegment.initprot));
825 W.printNumber("nsects", MOSegment.nsects);
826 W.printHex("flags", MOSegment.flags);
831 void MachODumper::printMachOIndirectSymbols() {
832 for (const auto &Load : Obj->load_commands()) {
833 if (Load.C.cmd == MachO::LC_DYSYMTAB) {
834 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand();
835 DictScope Group(W, "Indirect Symbols");
836 W.printNumber("Number", DLC.nindirectsyms);
837 ListScope D(W, "Symbols");
838 for (unsigned i = 0; i < DLC.nindirectsyms; ++i) {
839 DictScope Group(W, "Entry");
840 W.printNumber("Entry Index", i);
841 W.printHex("Symbol Index", Obj->getIndirectSymbolTableEntry(DLC, i));
847 void MachODumper::printMachOLinkerOptions() {
848 for (const auto &Load : Obj->load_commands()) {
849 if (Load.C.cmd == MachO::LC_LINKER_OPTION) {
850 MachO::linker_option_command LOLC = Obj->getLinkerOptionLoadCommand(Load);
851 DictScope Group(W, "Linker Options");
852 W.printNumber("Size", LOLC.cmdsize);
853 ListScope D(W, "Strings");
854 uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_option_command);
855 const char *P = Load.Ptr + sizeof(MachO::linker_option_command);
856 StringRef Data(P, DataSize);
857 for (unsigned i = 0; i < LOLC.count; ++i) {
858 std::pair<StringRef,StringRef> Split = Data.split('\0');
859 W.printString("Value", Split.first);
860 Data = Split.second;