llvm.org GIT mirror llvm / 60e9ca4
Slightly refactor things for llvm-objdump and the -macho option so it can be used with options other than just -disassemble so that universal files can be used with other options combined with -arch options. No functional change to existing options and use. One test case added for the additional functionality with a universal file an a -arch option. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225383 91177308-0d34-0410-b5e6-96231b3b80d8 Kevin Enderby 4 years ago
4 changed file(s) with 109 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
1616 // RUN: | FileCheck %s -check-prefix=ROUTINE
1717 // RUN: llvm-objdump -p %p/Inputs/exeThread.macho-x86_64 \
1818 // RUN: | FileCheck %s -check-prefix=THREAD
19 // RUN: llvm-objdump -macho -p -arch i386 %p/Inputs/macho-universal.x86_64.i386 \
20 // RUN: | FileCheck %s -check-prefix=FATi386
1921
2022 CHECK: Mach header
2123 CHECK: magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
436438 THREAD: r15 0x0000000000000000 rip 0x0000000100000d00
437439 THREAD: rflags 0x0000000000000000 cs 0x0000000000000000 fs 0x0000000000000000
438440 THREAD: gs 0x0000000000000000
441
442 FATi386: Mach header
443 FATi386: magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
444 FATi386: MH_MAGIC I386 ALL 0x00 EXECUTE 16 716 NOUNDEFS DYLDLINK TWOLEVEL PIE MH_NO_HEAP_EXECUTION
284284 return true;
285285 }
286286
287 static void DisassembleInputMachO2(StringRef Filename, MachOObjectFile *MachOOF,
288 StringRef ArchiveMemberName = StringRef(),
289 StringRef ArchitectureName = StringRef());
290
291 void llvm::DisassembleInputMachO(StringRef Filename) {
287 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF);
288
289 // ProcessMachO() is passed a single opened Mach-O file, which may be an
290 // archive member and or in a slice of a universal file. It prints the
291 // the file name and header info and then processes it according to the
292 // command line options.
293 static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
294 StringRef ArchiveMemberName = StringRef(),
295 StringRef ArchitectureName = StringRef()) {
296 outs() << Filename;
297 if (!ArchiveMemberName.empty())
298 outs() << '(' << ArchiveMemberName << ')';
299 if (!ArchitectureName.empty())
300 outs() << " (architecture " << ArchitectureName << ")";
301 outs() << ":\n";
302
303 if (Disassemble)
304 DisassembleMachO(Filename, MachOOF);
305 // TODO: These should/could be printed in Darwin's otool(1) or nm(1) style
306 // for -macho. Or just used a new option that maps to the otool(1)
307 // option like -r, -l, etc. Or just the normal llvm-objdump option
308 // but now for this slice so that the -arch options can be used.
309 // if (Relocations)
310 // PrintRelocations(MachOOF);
311 // if (SectionHeaders)
312 // PrintSectionHeaders(MachOOF);
313 // if (SectionContents)
314 // PrintSectionContents(MachOOF);
315 // if (SymbolTable)
316 // PrintSymbolTable(MachOOF);
317 // if (UnwindInfo)
318 // PrintUnwindInfo(MachOOF);
319 if (PrivateHeaders)
320 printMachOFileHeader(MachOOF);
321 if (ExportsTrie)
322 printExportsTrie(MachOOF);
323 if (Rebase)
324 printRebaseTable(MachOOF);
325 if (Bind)
326 printBindTable(MachOOF);
327 if (LazyBind)
328 printLazyBindTable(MachOOF);
329 if (WeakBind)
330 printWeakBindTable(MachOOF);
331 }
332
333 // ParseInputMachO() parses the named Mach-O file in Filename and handles the
334 // -arch flags selecting just those slices as specified by them and also parses
335 // archive files. Then for each individual Mach-O file ProcessMachO() is
336 // called to process the file based on the command line options.
337 void llvm::ParseInputMachO(StringRef Filename) {
292338 // Check for -arch all and verifiy the -arch flags are valid.
293339 for (unsigned i = 0; i < ArchFlags.size(); ++i) {
294340 if (ArchFlags[i] == "all") {
320366 if (MachOObjectFile *O = dyn_cast(&*ChildOrErr.get())) {
321367 if (!checkMachOAndArchFlags(O, Filename))
322368 return;
323 DisassembleInputMachO2(Filename, O, O->getFileName());
369 ProcessMachO(Filename, O, O->getFileName());
324370 }
325371 }
326372 return;
345391 if (ObjOrErr) {
346392 ObjectFile &O = *ObjOrErr.get();
347393 if (MachOObjectFile *MachOOF = dyn_cast(&O))
348 DisassembleInputMachO2(Filename, MachOOF, "", ArchitectureName);
394 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
349395 } else if (ErrorOr> AOrErr =
350396 I->getAsArchive()) {
351397 std::unique_ptr &A = *AOrErr;
361407 continue;
362408 if (MachOObjectFile *O =
363409 dyn_cast(&*ChildOrErr.get()))
364 DisassembleInputMachO2(Filename, O, O->getFileName(),
365 ArchitectureName);
410 ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
366411 }
367412 }
368413 }
389434 if (ObjOrErr) {
390435 ObjectFile &O = *ObjOrErr.get();
391436 if (MachOObjectFile *MachOOF = dyn_cast(&O))
392 DisassembleInputMachO2(Filename, MachOOF);
437 ProcessMachO(Filename, MachOOF);
393438 } else if (ErrorOr> AOrErr =
394439 I->getAsArchive()) {
395440 std::unique_ptr &A = *AOrErr;
402447 continue;
403448 if (MachOObjectFile *O =
404449 dyn_cast(&*ChildOrErr.get()))
405 DisassembleInputMachO2(Filename, O, O->getFileName());
450 ProcessMachO(Filename, O, O->getFileName());
406451 }
407452 }
408453 return;
422467 if (ObjOrErr) {
423468 ObjectFile &Obj = *ObjOrErr.get();
424469 if (MachOObjectFile *MachOOF = dyn_cast(&Obj))
425 DisassembleInputMachO2(Filename, MachOOF, "", ArchitectureName);
470 ProcessMachO(Filename, MachOOF, "", ArchitectureName);
426471 } else if (ErrorOr> AOrErr = I->getAsArchive()) {
427472 std::unique_ptr &A = *AOrErr;
428473 outs() << "Archive : " << Filename;
437482 if (MachOObjectFile *O =
438483 dyn_cast(&*ChildOrErr.get())) {
439484 if (MachOObjectFile *MachOOF = dyn_cast(O))
440 DisassembleInputMachO2(Filename, MachOOF, MachOOF->getFileName(),
441 ArchitectureName);
485 ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
486 ArchitectureName);
442487 }
443488 }
444489 }
449494 if (!checkMachOAndArchFlags(O, Filename))
450495 return;
451496 if (MachOObjectFile *MachOOF = dyn_cast(&*O)) {
452 DisassembleInputMachO2(Filename, MachOOF);
497 ProcessMachO(Filename, MachOOF);
453498 } else
454499 errs() << "llvm-objdump: '" << Filename << "': "
455500 << "Object is not a Mach-O file type.\n";
17841829 CommentStream.resync();
17851830 }
17861831
1787 static void DisassembleInputMachO2(StringRef Filename, MachOObjectFile *MachOOF,
1788 StringRef ArchiveMemberName,
1789 StringRef ArchitectureName) {
1832 static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF) {
17901833 const char *McpuDefault = nullptr;
17911834 const Target *ThumbTarget = nullptr;
17921835 const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
18921935 << ThumbTripleName << '\n';
18931936 return;
18941937 }
1895
1896 outs() << Filename;
1897 if (!ArchiveMemberName.empty())
1898 outs() << '(' << ArchiveMemberName << ')';
1899 if (!ArchitectureName.empty())
1900 outs() << " (architecture " << ArchitectureName << ")";
1901 outs() << ":\n";
19021938
19031939 MachO::mach_header Header = MachOOF->getHeader();
19041940
6060 static cl::list
6161 InputFilenames(cl::Positional, cl::desc(""),cl::ZeroOrMore);
6262
63 static cl::opt
64 Disassemble("disassemble",
63 cl::opt
64 llvm::Disassemble("disassemble",
6565 cl::desc("Display assembler mnemonics for the machine instructions"));
6666 static cl::alias
6767 Disassembled("d", cl::desc("Alias for --disassemble"),
7676 static cl::opt
7777 SymbolTable("t", cl::desc("Display the symbol table"));
7878
79 static cl::opt
80 ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
81
82 static cl::opt
83 Rebase("rebase", cl::desc("Display mach-o rebasing info"));
84
85 static cl::opt
86 Bind("bind", cl::desc("Display mach-o binding info"));
87
88 static cl::opt
89 LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
90
91 static cl::opt
92 WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
79 cl::opt
80 llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
81
82 cl::opt
83 llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
84
85 cl::opt
86 llvm::Bind("bind", cl::desc("Display mach-o binding info"));
87
88 cl::opt
89 llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
90
91 cl::opt
92 llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
9393
9494 static cl::opt
9595 MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
138138 UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
139139 cl::aliasopt(UnwindInfo));
140140
141 static cl::opt
142 PrivateHeaders("private-headers",
143 cl::desc("Display format specific file headers"));
141 cl::opt
142 llvm::PrivateHeaders("private-headers",
143 cl::desc("Display format specific file headers"));
144144
145145 static cl::alias
146146 PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
707707 }
708708 }
709709
710 static void printExportsTrie(const ObjectFile *o) {
710 void llvm::printExportsTrie(const ObjectFile *o) {
711711 outs() << "Exports trie:\n";
712712 if (const MachOObjectFile *MachO = dyn_cast(o))
713713 printMachOExportsTrie(MachO);
718718 }
719719 }
720720
721 static void printRebaseTable(const ObjectFile *o) {
721 void llvm::printRebaseTable(const ObjectFile *o) {
722722 outs() << "Rebase table:\n";
723723 if (const MachOObjectFile *MachO = dyn_cast(o))
724724 printMachORebaseTable(MachO);
729729 }
730730 }
731731
732 static void printBindTable(const ObjectFile *o) {
732 void llvm::printBindTable(const ObjectFile *o) {
733733 outs() << "Bind table:\n";
734734 if (const MachOObjectFile *MachO = dyn_cast(o))
735735 printMachOBindTable(MachO);
740740 }
741741 }
742742
743 static void printLazyBindTable(const ObjectFile *o) {
743 void llvm::printLazyBindTable(const ObjectFile *o) {
744744 outs() << "Lazy bind table:\n";
745745 if (const MachOObjectFile *MachO = dyn_cast(o))
746746 printMachOLazyBindTable(MachO);
751751 }
752752 }
753753
754 static void printWeakBindTable(const ObjectFile *o) {
754 void llvm::printWeakBindTable(const ObjectFile *o) {
755755 outs() << "Weak bind table:\n";
756756 if (const MachOObjectFile *MachO = dyn_cast(o))
757757 printMachOWeakBindTable(MachO);
831831 return;
832832 }
833833
834 if (MachOOpt && Disassemble) {
835 DisassembleInputMachO(file);
834 // If we are using the Mach-O specific object file parser, then let it parse
835 // the file and process the command line options. So the -arch flags can
836 // be used to select specific slices, etc.
837 if (MachOOpt) {
838 ParseInputMachO(file);
836839 return;
837840 }
838841
2525 extern cl::opt ArchName;
2626 extern cl::opt MCPU;
2727 extern cl::list MAttrs;
28 extern cl::opt Disassemble;
2829 extern cl::opt NoShowRawInsn;
30 extern cl::opt PrivateHeaders;
31 extern cl::opt ExportsTrie;
32 extern cl::opt Rebase;
33 extern cl::opt Bind;
34 extern cl::opt LazyBind;
35 extern cl::opt WeakBind;
2936
3037 // Various helper functions.
3138 bool error(std::error_code ec);
3239 bool RelocAddressLess(object::RelocationRef a, object::RelocationRef b);
3340 void DumpBytes(StringRef bytes);
34 void DisassembleInputMachO(StringRef Filename);
41 void ParseInputMachO(StringRef Filename);
3542 void printCOFFUnwindInfo(const object::COFFObjectFile* o);
3643 void printMachOUnwindInfo(const object::MachOObjectFile* o);
3744 void printMachOExportsTrie(const object::MachOObjectFile* o);
4249 void printELFFileHeader(const object::ObjectFile *o);
4350 void printCOFFFileHeader(const object::ObjectFile *o);
4451 void printMachOFileHeader(const object::ObjectFile *o);
52 void printExportsTrie(const object::ObjectFile *o);
53 void printRebaseTable(const object::ObjectFile *o);
54 void printBindTable(const object::ObjectFile *o);
55 void printLazyBindTable(const object::ObjectFile *o);
56 void printWeakBindTable(const object::ObjectFile *o);
4557
4658 } // end namespace llvm
4759