• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This program is a utility that works like binutils "objdump", that is, it
11 // dumps out a plethora of information about an object file depending on the
12 // flags.
13 //
14 // The flags and output of this program should be near identical to those of
15 // binutils objdump.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "llvm-objdump.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/Triple.h"
24 #include "llvm/CodeGen/FaultMaps.h"
25 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCContext.h"
28 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
29 #include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
30 #include "llvm/MC/MCInst.h"
31 #include "llvm/MC/MCInstPrinter.h"
32 #include "llvm/MC/MCInstrAnalysis.h"
33 #include "llvm/MC/MCInstrInfo.h"
34 #include "llvm/MC/MCObjectFileInfo.h"
35 #include "llvm/MC/MCRegisterInfo.h"
36 #include "llvm/MC/MCSubtargetInfo.h"
37 #include "llvm/Object/Archive.h"
38 #include "llvm/Object/COFF.h"
39 #include "llvm/Object/ELFObjectFile.h"
40 #include "llvm/Object/MachO.h"
41 #include "llvm/Object/ObjectFile.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/Errc.h"
46 #include "llvm/Support/FileSystem.h"
47 #include "llvm/Support/Format.h"
48 #include "llvm/Support/GraphWriter.h"
49 #include "llvm/Support/Host.h"
50 #include "llvm/Support/ManagedStatic.h"
51 #include "llvm/Support/MemoryBuffer.h"
52 #include "llvm/Support/PrettyStackTrace.h"
53 #include "llvm/Support/Signals.h"
54 #include "llvm/Support/SourceMgr.h"
55 #include "llvm/Support/TargetRegistry.h"
56 #include "llvm/Support/TargetSelect.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include <algorithm>
59 #include <cctype>
60 #include <cstring>
61 #include <system_error>
62 #include <utility>
63 
64 using namespace llvm;
65 using namespace object;
66 
67 static cl::list<std::string>
68 InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
69 
70 cl::opt<bool>
71 llvm::Disassemble("disassemble",
72   cl::desc("Display assembler mnemonics for the machine instructions"));
73 static cl::alias
74 Disassembled("d", cl::desc("Alias for --disassemble"),
75              cl::aliasopt(Disassemble));
76 
77 cl::opt<bool>
78 llvm::DisassembleAll("disassemble-all",
79   cl::desc("Display assembler mnemonics for the machine instructions"));
80 static cl::alias
81 DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
82              cl::aliasopt(DisassembleAll));
83 
84 cl::opt<bool>
85 llvm::Relocations("r", cl::desc("Display the relocation entries in the file"));
86 
87 cl::opt<bool>
88 llvm::SectionContents("s", cl::desc("Display the content of each section"));
89 
90 cl::opt<bool>
91 llvm::SymbolTable("t", cl::desc("Display the symbol table"));
92 
93 cl::opt<bool>
94 llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
95 
96 cl::opt<bool>
97 llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
98 
99 cl::opt<bool>
100 llvm::Bind("bind", cl::desc("Display mach-o binding info"));
101 
102 cl::opt<bool>
103 llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
104 
105 cl::opt<bool>
106 llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
107 
108 cl::opt<bool>
109 llvm::RawClangAST("raw-clang-ast",
110     cl::desc("Dump the raw binary contents of the clang AST section"));
111 
112 static cl::opt<bool>
113 MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
114 static cl::alias
115 MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
116 
117 cl::opt<std::string>
118 llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
119                                     "see -version for available targets"));
120 
121 cl::opt<std::string>
122 llvm::MCPU("mcpu",
123      cl::desc("Target a specific cpu type (-mcpu=help for details)"),
124      cl::value_desc("cpu-name"),
125      cl::init(""));
126 
127 cl::opt<std::string>
128 llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, "
129                                 "see -version for available targets"));
130 
131 cl::opt<bool>
132 llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the "
133                                                  "headers for each section."));
134 static cl::alias
135 SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
136                     cl::aliasopt(SectionHeaders));
137 static cl::alias
138 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
139                       cl::aliasopt(SectionHeaders));
140 
141 cl::list<std::string>
142 llvm::FilterSections("section", cl::desc("Operate on the specified sections only. "
143                                          "With -macho dump segment,section"));
144 cl::alias
145 static FilterSectionsj("j", cl::desc("Alias for --section"),
146                  cl::aliasopt(llvm::FilterSections));
147 
148 cl::list<std::string>
149 llvm::MAttrs("mattr",
150   cl::CommaSeparated,
151   cl::desc("Target specific attributes"),
152   cl::value_desc("a1,+a2,-a3,..."));
153 
154 cl::opt<bool>
155 llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
156                                                  "instructions, do not print "
157                                                  "the instruction bytes."));
158 
159 cl::opt<bool>
160 llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information"));
161 
162 static cl::alias
163 UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
164                 cl::aliasopt(UnwindInfo));
165 
166 cl::opt<bool>
167 llvm::PrivateHeaders("private-headers",
168                      cl::desc("Display format specific file headers"));
169 
170 cl::opt<bool>
171 llvm::FirstPrivateHeader("private-header",
172                          cl::desc("Display only the first format specific file "
173                                   "header"));
174 
175 static cl::alias
176 PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
177                     cl::aliasopt(PrivateHeaders));
178 
179 cl::opt<bool>
180     llvm::PrintImmHex("print-imm-hex",
181                       cl::desc("Use hex format for immediate values"));
182 
183 cl::opt<bool> PrintFaultMaps("fault-map-section",
184                              cl::desc("Display contents of faultmap section"));
185 
186 cl::opt<DIDumpType> llvm::DwarfDumpType(
187     "dwarf", cl::init(DIDT_Null), cl::desc("Dump of dwarf debug sections:"),
188     cl::values(clEnumValN(DIDT_Frames, "frames", ".debug_frame"),
189                clEnumValEnd));
190 
191 static StringRef ToolName;
192 
193 namespace {
194 typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
195 
196 class SectionFilterIterator {
197 public:
SectionFilterIterator(FilterPredicate P,llvm::object::section_iterator const & I,llvm::object::section_iterator const & E)198   SectionFilterIterator(FilterPredicate P,
199                         llvm::object::section_iterator const &I,
200                         llvm::object::section_iterator const &E)
201       : Predicate(std::move(P)), Iterator(I), End(E) {
202     ScanPredicate();
203   }
operator *() const204   const llvm::object::SectionRef &operator*() const { return *Iterator; }
operator ++()205   SectionFilterIterator &operator++() {
206     ++Iterator;
207     ScanPredicate();
208     return *this;
209   }
operator !=(SectionFilterIterator const & Other) const210   bool operator!=(SectionFilterIterator const &Other) const {
211     return Iterator != Other.Iterator;
212   }
213 
214 private:
ScanPredicate()215   void ScanPredicate() {
216     while (Iterator != End && !Predicate(*Iterator)) {
217       ++Iterator;
218     }
219   }
220   FilterPredicate Predicate;
221   llvm::object::section_iterator Iterator;
222   llvm::object::section_iterator End;
223 };
224 
225 class SectionFilter {
226 public:
SectionFilter(FilterPredicate P,llvm::object::ObjectFile const & O)227   SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
228       : Predicate(std::move(P)), Object(O) {}
begin()229   SectionFilterIterator begin() {
230     return SectionFilterIterator(Predicate, Object.section_begin(),
231                                  Object.section_end());
232   }
end()233   SectionFilterIterator end() {
234     return SectionFilterIterator(Predicate, Object.section_end(),
235                                  Object.section_end());
236   }
237 
238 private:
239   FilterPredicate Predicate;
240   llvm::object::ObjectFile const &Object;
241 };
ToolSectionFilter(llvm::object::ObjectFile const & O)242 SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) {
243   return SectionFilter([](llvm::object::SectionRef const &S) {
244                          if(FilterSections.empty())
245                            return true;
246                          llvm::StringRef String;
247                          std::error_code error = S.getName(String);
248                          if (error)
249                            return false;
250                          return std::find(FilterSections.begin(),
251                                           FilterSections.end(),
252                                           String) != FilterSections.end();
253                        },
254                        O);
255 }
256 }
257 
error(std::error_code EC)258 void llvm::error(std::error_code EC) {
259   if (!EC)
260     return;
261 
262   errs() << ToolName << ": error reading file: " << EC.message() << ".\n";
263   errs().flush();
264   exit(1);
265 }
266 
error(Twine Message)267 LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) {
268   errs() << ToolName << ": " << Message << ".\n";
269   errs().flush();
270   exit(1);
271 }
272 
report_error(StringRef File,std::error_code EC)273 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
274                                                 std::error_code EC) {
275   assert(EC);
276   errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
277   exit(1);
278 }
279 
report_error(StringRef File,llvm::Error E)280 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
281                                                 llvm::Error E) {
282   assert(E);
283   std::string Buf;
284   raw_string_ostream OS(Buf);
285   logAllUnhandledErrors(std::move(E), OS, "");
286   OS.flush();
287   errs() << ToolName << ": '" << File << "': " << Buf;
288   exit(1);
289 }
290 
report_error(StringRef ArchiveName,StringRef FileName,llvm::Error E,StringRef ArchitectureName)291 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName,
292                                                 StringRef FileName,
293                                                 llvm::Error E,
294                                                 StringRef ArchitectureName) {
295   assert(E);
296   errs() << ToolName << ": ";
297   if (ArchiveName != "")
298     errs() << ArchiveName << "(" << FileName << ")";
299   else
300     errs() << FileName;
301   if (!ArchitectureName.empty())
302     errs() << " (for architecture " << ArchitectureName << ")";
303   std::string Buf;
304   raw_string_ostream OS(Buf);
305   logAllUnhandledErrors(std::move(E), OS, "");
306   OS.flush();
307   errs() << " " << Buf;
308   exit(1);
309 }
310 
report_error(StringRef ArchiveName,const object::Archive::Child & C,llvm::Error E,StringRef ArchitectureName)311 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName,
312                                                 const object::Archive::Child &C,
313                                                 llvm::Error E,
314                                                 StringRef ArchitectureName) {
315   ErrorOr<StringRef> NameOrErr = C.getName();
316   // TODO: if we have a error getting the name then it would be nice to print
317   // the index of which archive member this is and or its offset in the
318   // archive instead of "???" as the name.
319   if (NameOrErr.getError())
320     llvm::report_error(ArchiveName, "???", std::move(E), ArchitectureName);
321   else
322     llvm::report_error(ArchiveName, NameOrErr.get(), std::move(E),
323                        ArchitectureName);
324 }
325 
getTarget(const ObjectFile * Obj=nullptr)326 static const Target *getTarget(const ObjectFile *Obj = nullptr) {
327   // Figure out the target triple.
328   llvm::Triple TheTriple("unknown-unknown-unknown");
329   if (TripleName.empty()) {
330     if (Obj) {
331       TheTriple.setArch(Triple::ArchType(Obj->getArch()));
332       // TheTriple defaults to ELF, and COFF doesn't have an environment:
333       // the best we can do here is indicate that it is mach-o.
334       if (Obj->isMachO())
335         TheTriple.setObjectFormat(Triple::MachO);
336 
337       if (Obj->isCOFF()) {
338         const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
339         if (COFFObj->getArch() == Triple::thumb)
340           TheTriple.setTriple("thumbv7-windows");
341       }
342     }
343   } else
344     TheTriple.setTriple(Triple::normalize(TripleName));
345 
346   // Get the target specific parser.
347   std::string Error;
348   const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
349                                                          Error);
350   if (!TheTarget)
351     report_fatal_error("can't find target: " + Error);
352 
353   // Update the triple name and return the found target.
354   TripleName = TheTriple.getTriple();
355   return TheTarget;
356 }
357 
RelocAddressLess(RelocationRef a,RelocationRef b)358 bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
359   return a.getOffset() < b.getOffset();
360 }
361 
362 namespace {
363 class PrettyPrinter {
364 public:
~PrettyPrinter()365   virtual ~PrettyPrinter(){}
printInst(MCInstPrinter & IP,const MCInst * MI,ArrayRef<uint8_t> Bytes,uint64_t Address,raw_ostream & OS,StringRef Annot,MCSubtargetInfo const & STI)366   virtual void printInst(MCInstPrinter &IP, const MCInst *MI,
367                          ArrayRef<uint8_t> Bytes, uint64_t Address,
368                          raw_ostream &OS, StringRef Annot,
369                          MCSubtargetInfo const &STI) {
370     OS << format("%8" PRIx64 ":", Address);
371     if (!NoShowRawInsn) {
372       OS << "\t";
373       dumpBytes(Bytes, OS);
374     }
375     if (MI)
376       IP.printInst(MI, OS, "", STI);
377     else
378       OS << " <unknown>";
379   }
380 };
381 PrettyPrinter PrettyPrinterInst;
382 class HexagonPrettyPrinter : public PrettyPrinter {
383 public:
printLead(ArrayRef<uint8_t> Bytes,uint64_t Address,raw_ostream & OS)384   void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
385                  raw_ostream &OS) {
386     uint32_t opcode =
387       (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
388     OS << format("%8" PRIx64 ":", Address);
389     if (!NoShowRawInsn) {
390       OS << "\t";
391       dumpBytes(Bytes.slice(0, 4), OS);
392       OS << format("%08" PRIx32, opcode);
393     }
394   }
printInst(MCInstPrinter & IP,const MCInst * MI,ArrayRef<uint8_t> Bytes,uint64_t Address,raw_ostream & OS,StringRef Annot,MCSubtargetInfo const & STI)395   void printInst(MCInstPrinter &IP, const MCInst *MI,
396                  ArrayRef<uint8_t> Bytes, uint64_t Address,
397                  raw_ostream &OS, StringRef Annot,
398                  MCSubtargetInfo const &STI) override {
399     if (!MI) {
400       printLead(Bytes, Address, OS);
401       OS << " <unknown>";
402       return;
403     }
404     std::string Buffer;
405     {
406       raw_string_ostream TempStream(Buffer);
407       IP.printInst(MI, TempStream, "", STI);
408     }
409     StringRef Contents(Buffer);
410     // Split off bundle attributes
411     auto PacketBundle = Contents.rsplit('\n');
412     // Split off first instruction from the rest
413     auto HeadTail = PacketBundle.first.split('\n');
414     auto Preamble = " { ";
415     auto Separator = "";
416     while(!HeadTail.first.empty()) {
417       OS << Separator;
418       Separator = "\n";
419       printLead(Bytes, Address, OS);
420       OS << Preamble;
421       Preamble = "   ";
422       StringRef Inst;
423       auto Duplex = HeadTail.first.split('\v');
424       if(!Duplex.second.empty()){
425         OS << Duplex.first;
426         OS << "; ";
427         Inst = Duplex.second;
428       }
429       else
430         Inst = HeadTail.first;
431       OS << Inst;
432       Bytes = Bytes.slice(4);
433       Address += 4;
434       HeadTail = HeadTail.second.split('\n');
435     }
436     OS << " } " << PacketBundle.second;
437   }
438 };
439 HexagonPrettyPrinter HexagonPrettyPrinterInst;
440 
441 class AMDGCNPrettyPrinter : public PrettyPrinter {
442 public:
printInst(MCInstPrinter & IP,const MCInst * MI,ArrayRef<uint8_t> Bytes,uint64_t Address,raw_ostream & OS,StringRef Annot,MCSubtargetInfo const & STI)443   void printInst(MCInstPrinter &IP,
444                  const MCInst *MI,
445                  ArrayRef<uint8_t> Bytes,
446                  uint64_t Address,
447                  raw_ostream &OS,
448                  StringRef Annot,
449                  MCSubtargetInfo const &STI) override {
450     if (!MI) {
451       OS << " <unknown>";
452       return;
453     }
454 
455     SmallString<40> InstStr;
456     raw_svector_ostream IS(InstStr);
457 
458     IP.printInst(MI, IS, "", STI);
459 
460     OS << left_justify(IS.str(), 60) << format("// %012" PRIX64 ": ", Address);
461     typedef support::ulittle32_t U32;
462     for (auto D : makeArrayRef(reinterpret_cast<const U32*>(Bytes.data()),
463                                Bytes.size() / sizeof(U32)))
464       // D should be explicitly casted to uint32_t here as it is passed
465       // by format to snprintf as vararg.
466       OS << format("%08" PRIX32 " ", static_cast<uint32_t>(D));
467 
468     if (!Annot.empty())
469       OS << "// " << Annot;
470   }
471 };
472 AMDGCNPrettyPrinter AMDGCNPrettyPrinterInst;
473 
selectPrettyPrinter(Triple const & Triple)474 PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
475   switch(Triple.getArch()) {
476   default:
477     return PrettyPrinterInst;
478   case Triple::hexagon:
479     return HexagonPrettyPrinterInst;
480   case Triple::amdgcn:
481     return AMDGCNPrettyPrinterInst;
482   }
483 }
484 }
485 
486 template <class ELFT>
getRelocationValueString(const ELFObjectFile<ELFT> * Obj,const RelocationRef & RelRef,SmallVectorImpl<char> & Result)487 static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
488                                                 const RelocationRef &RelRef,
489                                                 SmallVectorImpl<char> &Result) {
490   DataRefImpl Rel = RelRef.getRawDataRefImpl();
491 
492   typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
493   typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
494   typedef typename ELFObjectFile<ELFT>::Elf_Rela Elf_Rela;
495 
496   const ELFFile<ELFT> &EF = *Obj->getELFFile();
497 
498   ErrorOr<const Elf_Shdr *> SecOrErr = EF.getSection(Rel.d.a);
499   if (std::error_code EC = SecOrErr.getError())
500     return EC;
501   const Elf_Shdr *Sec = *SecOrErr;
502   ErrorOr<const Elf_Shdr *> SymTabOrErr = EF.getSection(Sec->sh_link);
503   if (std::error_code EC = SymTabOrErr.getError())
504     return EC;
505   const Elf_Shdr *SymTab = *SymTabOrErr;
506   assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
507          SymTab->sh_type == ELF::SHT_DYNSYM);
508   ErrorOr<const Elf_Shdr *> StrTabSec = EF.getSection(SymTab->sh_link);
509   if (std::error_code EC = StrTabSec.getError())
510     return EC;
511   ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(*StrTabSec);
512   if (std::error_code EC = StrTabOrErr.getError())
513     return EC;
514   StringRef StrTab = *StrTabOrErr;
515   uint8_t type = RelRef.getType();
516   StringRef res;
517   int64_t addend = 0;
518   switch (Sec->sh_type) {
519   default:
520     return object_error::parse_failed;
521   case ELF::SHT_REL: {
522     // TODO: Read implicit addend from section data.
523     break;
524   }
525   case ELF::SHT_RELA: {
526     const Elf_Rela *ERela = Obj->getRela(Rel);
527     addend = ERela->r_addend;
528     break;
529   }
530   }
531   symbol_iterator SI = RelRef.getSymbol();
532   const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl());
533   StringRef Target;
534   if (symb->getType() == ELF::STT_SECTION) {
535     Expected<section_iterator> SymSI = SI->getSection();
536     if (!SymSI)
537       return errorToErrorCode(SymSI.takeError());
538     const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl());
539     ErrorOr<StringRef> SecName = EF.getSectionName(SymSec);
540     if (std::error_code EC = SecName.getError())
541       return EC;
542     Target = *SecName;
543   } else {
544     Expected<StringRef> SymName = symb->getName(StrTab);
545     if (!SymName)
546       return errorToErrorCode(SymName.takeError());
547     Target = *SymName;
548   }
549   switch (EF.getHeader()->e_machine) {
550   case ELF::EM_X86_64:
551     switch (type) {
552     case ELF::R_X86_64_PC8:
553     case ELF::R_X86_64_PC16:
554     case ELF::R_X86_64_PC32: {
555       std::string fmtbuf;
556       raw_string_ostream fmt(fmtbuf);
557       fmt << Target << (addend < 0 ? "" : "+") << addend << "-P";
558       fmt.flush();
559       Result.append(fmtbuf.begin(), fmtbuf.end());
560     } break;
561     case ELF::R_X86_64_8:
562     case ELF::R_X86_64_16:
563     case ELF::R_X86_64_32:
564     case ELF::R_X86_64_32S:
565     case ELF::R_X86_64_64: {
566       std::string fmtbuf;
567       raw_string_ostream fmt(fmtbuf);
568       fmt << Target << (addend < 0 ? "" : "+") << addend;
569       fmt.flush();
570       Result.append(fmtbuf.begin(), fmtbuf.end());
571     } break;
572     default:
573       res = "Unknown";
574     }
575     break;
576   case ELF::EM_LANAI:
577   case ELF::EM_AARCH64: {
578     std::string fmtbuf;
579     raw_string_ostream fmt(fmtbuf);
580     fmt << Target;
581     if (addend != 0)
582       fmt << (addend < 0 ? "" : "+") << addend;
583     fmt.flush();
584     Result.append(fmtbuf.begin(), fmtbuf.end());
585     break;
586   }
587   case ELF::EM_386:
588   case ELF::EM_IAMCU:
589   case ELF::EM_ARM:
590   case ELF::EM_HEXAGON:
591   case ELF::EM_MIPS:
592     res = Target;
593     break;
594   case ELF::EM_WEBASSEMBLY:
595     switch (type) {
596     case ELF::R_WEBASSEMBLY_DATA: {
597       std::string fmtbuf;
598       raw_string_ostream fmt(fmtbuf);
599       fmt << Target << (addend < 0 ? "" : "+") << addend;
600       fmt.flush();
601       Result.append(fmtbuf.begin(), fmtbuf.end());
602       break;
603     }
604     case ELF::R_WEBASSEMBLY_FUNCTION:
605       res = Target;
606       break;
607     default:
608       res = "Unknown";
609     }
610     break;
611   default:
612     res = "Unknown";
613   }
614   if (Result.empty())
615     Result.append(res.begin(), res.end());
616   return std::error_code();
617 }
618 
getRelocationValueString(const ELFObjectFileBase * Obj,const RelocationRef & Rel,SmallVectorImpl<char> & Result)619 static std::error_code getRelocationValueString(const ELFObjectFileBase *Obj,
620                                                 const RelocationRef &Rel,
621                                                 SmallVectorImpl<char> &Result) {
622   if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
623     return getRelocationValueString(ELF32LE, Rel, Result);
624   if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
625     return getRelocationValueString(ELF64LE, Rel, Result);
626   if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
627     return getRelocationValueString(ELF32BE, Rel, Result);
628   auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
629   return getRelocationValueString(ELF64BE, Rel, Result);
630 }
631 
getRelocationValueString(const COFFObjectFile * Obj,const RelocationRef & Rel,SmallVectorImpl<char> & Result)632 static std::error_code getRelocationValueString(const COFFObjectFile *Obj,
633                                                 const RelocationRef &Rel,
634                                                 SmallVectorImpl<char> &Result) {
635   symbol_iterator SymI = Rel.getSymbol();
636   Expected<StringRef> SymNameOrErr = SymI->getName();
637   if (!SymNameOrErr)
638     return errorToErrorCode(SymNameOrErr.takeError());
639   StringRef SymName = *SymNameOrErr;
640   Result.append(SymName.begin(), SymName.end());
641   return std::error_code();
642 }
643 
printRelocationTargetName(const MachOObjectFile * O,const MachO::any_relocation_info & RE,raw_string_ostream & fmt)644 static void printRelocationTargetName(const MachOObjectFile *O,
645                                       const MachO::any_relocation_info &RE,
646                                       raw_string_ostream &fmt) {
647   bool IsScattered = O->isRelocationScattered(RE);
648 
649   // Target of a scattered relocation is an address.  In the interest of
650   // generating pretty output, scan through the symbol table looking for a
651   // symbol that aligns with that address.  If we find one, print it.
652   // Otherwise, we just print the hex address of the target.
653   if (IsScattered) {
654     uint32_t Val = O->getPlainRelocationSymbolNum(RE);
655 
656     for (const SymbolRef &Symbol : O->symbols()) {
657       std::error_code ec;
658       Expected<uint64_t> Addr = Symbol.getAddress();
659       if (!Addr) {
660         std::string Buf;
661         raw_string_ostream OS(Buf);
662         logAllUnhandledErrors(Addr.takeError(), OS, "");
663         OS.flush();
664         report_fatal_error(Buf);
665       }
666       if (*Addr != Val)
667         continue;
668       Expected<StringRef> Name = Symbol.getName();
669       if (!Name) {
670         std::string Buf;
671         raw_string_ostream OS(Buf);
672         logAllUnhandledErrors(Name.takeError(), OS, "");
673         OS.flush();
674         report_fatal_error(Buf);
675       }
676       fmt << *Name;
677       return;
678     }
679 
680     // If we couldn't find a symbol that this relocation refers to, try
681     // to find a section beginning instead.
682     for (const SectionRef &Section : ToolSectionFilter(*O)) {
683       std::error_code ec;
684 
685       StringRef Name;
686       uint64_t Addr = Section.getAddress();
687       if (Addr != Val)
688         continue;
689       if ((ec = Section.getName(Name)))
690         report_fatal_error(ec.message());
691       fmt << Name;
692       return;
693     }
694 
695     fmt << format("0x%x", Val);
696     return;
697   }
698 
699   StringRef S;
700   bool isExtern = O->getPlainRelocationExternal(RE);
701   uint64_t Val = O->getPlainRelocationSymbolNum(RE);
702 
703   if (isExtern) {
704     symbol_iterator SI = O->symbol_begin();
705     advance(SI, Val);
706     Expected<StringRef> SOrErr = SI->getName();
707     error(errorToErrorCode(SOrErr.takeError()));
708     S = *SOrErr;
709   } else {
710     section_iterator SI = O->section_begin();
711     // Adjust for the fact that sections are 1-indexed.
712     advance(SI, Val - 1);
713     SI->getName(S);
714   }
715 
716   fmt << S;
717 }
718 
getRelocationValueString(const MachOObjectFile * Obj,const RelocationRef & RelRef,SmallVectorImpl<char> & Result)719 static std::error_code getRelocationValueString(const MachOObjectFile *Obj,
720                                                 const RelocationRef &RelRef,
721                                                 SmallVectorImpl<char> &Result) {
722   DataRefImpl Rel = RelRef.getRawDataRefImpl();
723   MachO::any_relocation_info RE = Obj->getRelocation(Rel);
724 
725   unsigned Arch = Obj->getArch();
726 
727   std::string fmtbuf;
728   raw_string_ostream fmt(fmtbuf);
729   unsigned Type = Obj->getAnyRelocationType(RE);
730   bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
731 
732   // Determine any addends that should be displayed with the relocation.
733   // These require decoding the relocation type, which is triple-specific.
734 
735   // X86_64 has entirely custom relocation types.
736   if (Arch == Triple::x86_64) {
737     bool isPCRel = Obj->getAnyRelocationPCRel(RE);
738 
739     switch (Type) {
740     case MachO::X86_64_RELOC_GOT_LOAD:
741     case MachO::X86_64_RELOC_GOT: {
742       printRelocationTargetName(Obj, RE, fmt);
743       fmt << "@GOT";
744       if (isPCRel)
745         fmt << "PCREL";
746       break;
747     }
748     case MachO::X86_64_RELOC_SUBTRACTOR: {
749       DataRefImpl RelNext = Rel;
750       Obj->moveRelocationNext(RelNext);
751       MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
752 
753       // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
754       // X86_64_RELOC_UNSIGNED.
755       // NOTE: Scattered relocations don't exist on x86_64.
756       unsigned RType = Obj->getAnyRelocationType(RENext);
757       if (RType != MachO::X86_64_RELOC_UNSIGNED)
758         report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
759                            "X86_64_RELOC_SUBTRACTOR.");
760 
761       // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
762       // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
763       printRelocationTargetName(Obj, RENext, fmt);
764       fmt << "-";
765       printRelocationTargetName(Obj, RE, fmt);
766       break;
767     }
768     case MachO::X86_64_RELOC_TLV:
769       printRelocationTargetName(Obj, RE, fmt);
770       fmt << "@TLV";
771       if (isPCRel)
772         fmt << "P";
773       break;
774     case MachO::X86_64_RELOC_SIGNED_1:
775       printRelocationTargetName(Obj, RE, fmt);
776       fmt << "-1";
777       break;
778     case MachO::X86_64_RELOC_SIGNED_2:
779       printRelocationTargetName(Obj, RE, fmt);
780       fmt << "-2";
781       break;
782     case MachO::X86_64_RELOC_SIGNED_4:
783       printRelocationTargetName(Obj, RE, fmt);
784       fmt << "-4";
785       break;
786     default:
787       printRelocationTargetName(Obj, RE, fmt);
788       break;
789     }
790     // X86 and ARM share some relocation types in common.
791   } else if (Arch == Triple::x86 || Arch == Triple::arm ||
792              Arch == Triple::ppc) {
793     // Generic relocation types...
794     switch (Type) {
795     case MachO::GENERIC_RELOC_PAIR: // prints no info
796       return std::error_code();
797     case MachO::GENERIC_RELOC_SECTDIFF: {
798       DataRefImpl RelNext = Rel;
799       Obj->moveRelocationNext(RelNext);
800       MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
801 
802       // X86 sect diff's must be followed by a relocation of type
803       // GENERIC_RELOC_PAIR.
804       unsigned RType = Obj->getAnyRelocationType(RENext);
805 
806       if (RType != MachO::GENERIC_RELOC_PAIR)
807         report_fatal_error("Expected GENERIC_RELOC_PAIR after "
808                            "GENERIC_RELOC_SECTDIFF.");
809 
810       printRelocationTargetName(Obj, RE, fmt);
811       fmt << "-";
812       printRelocationTargetName(Obj, RENext, fmt);
813       break;
814     }
815     }
816 
817     if (Arch == Triple::x86 || Arch == Triple::ppc) {
818       switch (Type) {
819       case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
820         DataRefImpl RelNext = Rel;
821         Obj->moveRelocationNext(RelNext);
822         MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
823 
824         // X86 sect diff's must be followed by a relocation of type
825         // GENERIC_RELOC_PAIR.
826         unsigned RType = Obj->getAnyRelocationType(RENext);
827         if (RType != MachO::GENERIC_RELOC_PAIR)
828           report_fatal_error("Expected GENERIC_RELOC_PAIR after "
829                              "GENERIC_RELOC_LOCAL_SECTDIFF.");
830 
831         printRelocationTargetName(Obj, RE, fmt);
832         fmt << "-";
833         printRelocationTargetName(Obj, RENext, fmt);
834         break;
835       }
836       case MachO::GENERIC_RELOC_TLV: {
837         printRelocationTargetName(Obj, RE, fmt);
838         fmt << "@TLV";
839         if (IsPCRel)
840           fmt << "P";
841         break;
842       }
843       default:
844         printRelocationTargetName(Obj, RE, fmt);
845       }
846     } else { // ARM-specific relocations
847       switch (Type) {
848       case MachO::ARM_RELOC_HALF:
849       case MachO::ARM_RELOC_HALF_SECTDIFF: {
850         // Half relocations steal a bit from the length field to encode
851         // whether this is an upper16 or a lower16 relocation.
852         bool isUpper = Obj->getAnyRelocationLength(RE) >> 1;
853 
854         if (isUpper)
855           fmt << ":upper16:(";
856         else
857           fmt << ":lower16:(";
858         printRelocationTargetName(Obj, RE, fmt);
859 
860         DataRefImpl RelNext = Rel;
861         Obj->moveRelocationNext(RelNext);
862         MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
863 
864         // ARM half relocs must be followed by a relocation of type
865         // ARM_RELOC_PAIR.
866         unsigned RType = Obj->getAnyRelocationType(RENext);
867         if (RType != MachO::ARM_RELOC_PAIR)
868           report_fatal_error("Expected ARM_RELOC_PAIR after "
869                              "ARM_RELOC_HALF");
870 
871         // NOTE: The half of the target virtual address is stashed in the
872         // address field of the secondary relocation, but we can't reverse
873         // engineer the constant offset from it without decoding the movw/movt
874         // instruction to find the other half in its immediate field.
875 
876         // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
877         // symbol/section pointer of the follow-on relocation.
878         if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
879           fmt << "-";
880           printRelocationTargetName(Obj, RENext, fmt);
881         }
882 
883         fmt << ")";
884         break;
885       }
886       default: { printRelocationTargetName(Obj, RE, fmt); }
887       }
888     }
889   } else
890     printRelocationTargetName(Obj, RE, fmt);
891 
892   fmt.flush();
893   Result.append(fmtbuf.begin(), fmtbuf.end());
894   return std::error_code();
895 }
896 
getRelocationValueString(const RelocationRef & Rel,SmallVectorImpl<char> & Result)897 static std::error_code getRelocationValueString(const RelocationRef &Rel,
898                                                 SmallVectorImpl<char> &Result) {
899   const ObjectFile *Obj = Rel.getObject();
900   if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
901     return getRelocationValueString(ELF, Rel, Result);
902   if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
903     return getRelocationValueString(COFF, Rel, Result);
904   auto *MachO = cast<MachOObjectFile>(Obj);
905   return getRelocationValueString(MachO, Rel, Result);
906 }
907 
908 /// @brief Indicates whether this relocation should hidden when listing
909 /// relocations, usually because it is the trailing part of a multipart
910 /// relocation that will be printed as part of the leading relocation.
getHidden(RelocationRef RelRef)911 static bool getHidden(RelocationRef RelRef) {
912   const ObjectFile *Obj = RelRef.getObject();
913   auto *MachO = dyn_cast<MachOObjectFile>(Obj);
914   if (!MachO)
915     return false;
916 
917   unsigned Arch = MachO->getArch();
918   DataRefImpl Rel = RelRef.getRawDataRefImpl();
919   uint64_t Type = MachO->getRelocationType(Rel);
920 
921   // On arches that use the generic relocations, GENERIC_RELOC_PAIR
922   // is always hidden.
923   if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
924     if (Type == MachO::GENERIC_RELOC_PAIR)
925       return true;
926   } else if (Arch == Triple::x86_64) {
927     // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
928     // an X86_64_RELOC_SUBTRACTOR.
929     if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
930       DataRefImpl RelPrev = Rel;
931       RelPrev.d.a--;
932       uint64_t PrevType = MachO->getRelocationType(RelPrev);
933       if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
934         return true;
935     }
936   }
937 
938   return false;
939 }
940 
DisassembleObject(const ObjectFile * Obj,bool InlineRelocs)941 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
942   const Target *TheTarget = getTarget(Obj);
943 
944   // Package up features to be passed to target/subtarget
945   SubtargetFeatures Features = Obj->getFeatures();
946   if (MAttrs.size()) {
947     for (unsigned i = 0; i != MAttrs.size(); ++i)
948       Features.AddFeature(MAttrs[i]);
949   }
950 
951   std::unique_ptr<const MCRegisterInfo> MRI(
952       TheTarget->createMCRegInfo(TripleName));
953   if (!MRI)
954     report_fatal_error("error: no register info for target " + TripleName);
955 
956   // Set up disassembler.
957   std::unique_ptr<const MCAsmInfo> AsmInfo(
958       TheTarget->createMCAsmInfo(*MRI, TripleName));
959   if (!AsmInfo)
960     report_fatal_error("error: no assembly info for target " + TripleName);
961   std::unique_ptr<const MCSubtargetInfo> STI(
962       TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString()));
963   if (!STI)
964     report_fatal_error("error: no subtarget info for target " + TripleName);
965   std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
966   if (!MII)
967     report_fatal_error("error: no instruction info for target " + TripleName);
968   std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
969   MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
970 
971   std::unique_ptr<MCDisassembler> DisAsm(
972     TheTarget->createMCDisassembler(*STI, Ctx));
973   if (!DisAsm)
974     report_fatal_error("error: no disassembler for target " + TripleName);
975 
976   std::unique_ptr<const MCInstrAnalysis> MIA(
977       TheTarget->createMCInstrAnalysis(MII.get()));
978 
979   int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
980   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
981       Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
982   if (!IP)
983     report_fatal_error("error: no instruction printer for target " +
984                        TripleName);
985   IP->setPrintImmHex(PrintImmHex);
986   PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
987 
988   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ":  " :
989                                                  "\t\t\t%08" PRIx64 ":  ";
990 
991   // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
992   // in RelocSecs contain the relocations for section S.
993   std::error_code EC;
994   std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
995   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
996     section_iterator Sec2 = Section.getRelocatedSection();
997     if (Sec2 != Obj->section_end())
998       SectionRelocMap[*Sec2].push_back(Section);
999   }
1000 
1001   // Create a mapping from virtual address to symbol name.  This is used to
1002   // pretty print the symbols while disassembling.
1003   typedef std::vector<std::pair<uint64_t, StringRef>> SectionSymbolsTy;
1004   std::map<SectionRef, SectionSymbolsTy> AllSymbols;
1005   for (const SymbolRef &Symbol : Obj->symbols()) {
1006     Expected<uint64_t> AddressOrErr = Symbol.getAddress();
1007     error(errorToErrorCode(AddressOrErr.takeError()));
1008     uint64_t Address = *AddressOrErr;
1009 
1010     Expected<StringRef> Name = Symbol.getName();
1011     error(errorToErrorCode(Name.takeError()));
1012     if (Name->empty())
1013       continue;
1014 
1015     Expected<section_iterator> SectionOrErr = Symbol.getSection();
1016     error(errorToErrorCode(SectionOrErr.takeError()));
1017     section_iterator SecI = *SectionOrErr;
1018     if (SecI == Obj->section_end())
1019       continue;
1020 
1021     AllSymbols[*SecI].emplace_back(Address, *Name);
1022   }
1023 
1024   // Create a mapping from virtual address to section.
1025   std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
1026   for (SectionRef Sec : Obj->sections())
1027     SectionAddresses.emplace_back(Sec.getAddress(), Sec);
1028   array_pod_sort(SectionAddresses.begin(), SectionAddresses.end());
1029 
1030   // Linked executables (.exe and .dll files) typically don't include a real
1031   // symbol table but they might contain an export table.
1032   if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) {
1033     for (const auto &ExportEntry : COFFObj->export_directories()) {
1034       StringRef Name;
1035       error(ExportEntry.getSymbolName(Name));
1036       if (Name.empty())
1037         continue;
1038       uint32_t RVA;
1039       error(ExportEntry.getExportRVA(RVA));
1040 
1041       uint64_t VA = COFFObj->getImageBase() + RVA;
1042       auto Sec = std::upper_bound(
1043           SectionAddresses.begin(), SectionAddresses.end(), VA,
1044           [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) {
1045             return LHS < RHS.first;
1046           });
1047       if (Sec != SectionAddresses.begin())
1048         --Sec;
1049       else
1050         Sec = SectionAddresses.end();
1051 
1052       if (Sec != SectionAddresses.end())
1053         AllSymbols[Sec->second].emplace_back(VA, Name);
1054     }
1055   }
1056 
1057   // Sort all the symbols, this allows us to use a simple binary search to find
1058   // a symbol near an address.
1059   for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
1060     array_pod_sort(SecSyms.second.begin(), SecSyms.second.end());
1061 
1062   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1063     if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
1064       continue;
1065 
1066     uint64_t SectionAddr = Section.getAddress();
1067     uint64_t SectSize = Section.getSize();
1068     if (!SectSize)
1069       continue;
1070 
1071     // Get the list of all the symbols in this section.
1072     SectionSymbolsTy &Symbols = AllSymbols[Section];
1073     std::vector<uint64_t> DataMappingSymsAddr;
1074     std::vector<uint64_t> TextMappingSymsAddr;
1075     if (Obj->isELF() && Obj->getArch() == Triple::aarch64) {
1076       for (const auto &Symb : Symbols) {
1077         uint64_t Address = Symb.first;
1078         StringRef Name = Symb.second;
1079         if (Name.startswith("$d"))
1080           DataMappingSymsAddr.push_back(Address - SectionAddr);
1081         if (Name.startswith("$x"))
1082           TextMappingSymsAddr.push_back(Address - SectionAddr);
1083       }
1084     }
1085 
1086     std::sort(DataMappingSymsAddr.begin(), DataMappingSymsAddr.end());
1087     std::sort(TextMappingSymsAddr.begin(), TextMappingSymsAddr.end());
1088 
1089     // Make a list of all the relocations for this section.
1090     std::vector<RelocationRef> Rels;
1091     if (InlineRelocs) {
1092       for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
1093         for (const RelocationRef &Reloc : RelocSec.relocations()) {
1094           Rels.push_back(Reloc);
1095         }
1096       }
1097     }
1098 
1099     // Sort relocations by address.
1100     std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
1101 
1102     StringRef SegmentName = "";
1103     if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
1104       DataRefImpl DR = Section.getRawDataRefImpl();
1105       SegmentName = MachO->getSectionFinalSegmentName(DR);
1106     }
1107     StringRef name;
1108     error(Section.getName(name));
1109     outs() << "Disassembly of section ";
1110     if (!SegmentName.empty())
1111       outs() << SegmentName << ",";
1112     outs() << name << ':';
1113 
1114     // If the section has no symbol at the start, just insert a dummy one.
1115     if (Symbols.empty() || Symbols[0].first != 0)
1116       Symbols.insert(Symbols.begin(), std::make_pair(SectionAddr, name));
1117 
1118     SmallString<40> Comments;
1119     raw_svector_ostream CommentStream(Comments);
1120 
1121     StringRef BytesStr;
1122     error(Section.getContents(BytesStr));
1123     ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
1124                             BytesStr.size());
1125 
1126     uint64_t Size;
1127     uint64_t Index;
1128 
1129     std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
1130     std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
1131     // Disassemble symbol by symbol.
1132     for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
1133 
1134       uint64_t Start = Symbols[si].first - SectionAddr;
1135       // The end is either the section end or the beginning of the next
1136       // symbol.
1137       uint64_t End =
1138           (si == se - 1) ? SectSize : Symbols[si + 1].first - SectionAddr;
1139       // Don't try to disassemble beyond the end of section contents.
1140       if (End > SectSize)
1141         End = SectSize;
1142       // If this symbol has the same address as the next symbol, then skip it.
1143       if (Start >= End)
1144         continue;
1145 
1146       if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) {
1147         // make size 4 bytes folded
1148         End = Start + ((End - Start) & ~0x3ull);
1149         Start += 256; // add sizeof(amd_kernel_code_t)
1150         // cut trailing zeroes - up to 256 bytes (align)
1151         const uint64_t EndAlign = 256;
1152         const auto Limit = End - (std::min)(EndAlign, End - Start);
1153         while (End > Limit &&
1154           *reinterpret_cast<const support::ulittle32_t*>(&Bytes[End - 4]) == 0)
1155           End -= 4;
1156       }
1157 
1158       outs() << '\n' << Symbols[si].second << ":\n";
1159 
1160 #ifndef NDEBUG
1161       raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
1162 #else
1163       raw_ostream &DebugOut = nulls();
1164 #endif
1165 
1166       for (Index = Start; Index < End; Index += Size) {
1167         MCInst Inst;
1168 
1169         // AArch64 ELF binaries can interleave data and text in the
1170         // same section. We rely on the markers introduced to
1171         // understand what we need to dump.
1172         if (Obj->isELF() && Obj->getArch() == Triple::aarch64) {
1173           uint64_t Stride = 0;
1174 
1175           auto DAI = std::lower_bound(DataMappingSymsAddr.begin(),
1176                                       DataMappingSymsAddr.end(), Index);
1177           if (DAI != DataMappingSymsAddr.end() && *DAI == Index) {
1178             // Switch to data.
1179             while (Index < End) {
1180               outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1181               outs() << "\t";
1182               if (Index + 4 <= End) {
1183                 Stride = 4;
1184                 dumpBytes(Bytes.slice(Index, 4), outs());
1185                 outs() << "\t.word";
1186               } else if (Index + 2 <= End) {
1187                 Stride = 2;
1188                 dumpBytes(Bytes.slice(Index, 2), outs());
1189                 outs() << "\t.short";
1190               } else {
1191                 Stride = 1;
1192                 dumpBytes(Bytes.slice(Index, 1), outs());
1193                 outs() << "\t.byte";
1194               }
1195               Index += Stride;
1196               outs() << "\n";
1197               auto TAI = std::lower_bound(TextMappingSymsAddr.begin(),
1198                                           TextMappingSymsAddr.end(), Index);
1199               if (TAI != TextMappingSymsAddr.end() && *TAI == Index)
1200                 break;
1201             }
1202           }
1203         }
1204 
1205         if (Index >= End)
1206           break;
1207 
1208         bool Disassembled = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
1209                                                    SectionAddr + Index, DebugOut,
1210                                                    CommentStream);
1211         if (Size == 0)
1212           Size = 1;
1213         PIP.printInst(*IP, Disassembled ? &Inst : nullptr,
1214                       Bytes.slice(Index, Size),
1215                       SectionAddr + Index, outs(), "", *STI);
1216         outs() << CommentStream.str();
1217         Comments.clear();
1218 
1219         // Try to resolve the target of a call, tail call, etc. to a specific
1220         // symbol.
1221         if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
1222                     MIA->isConditionalBranch(Inst))) {
1223           uint64_t Target;
1224           if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
1225             // In a relocatable object, the target's section must reside in
1226             // the same section as the call instruction or it is accessed
1227             // through a relocation.
1228             //
1229             // In a non-relocatable object, the target may be in any section.
1230             //
1231             // N.B. We don't walk the relocations in the relocatable case yet.
1232             auto *TargetSectionSymbols = &Symbols;
1233             if (!Obj->isRelocatableObject()) {
1234               auto SectionAddress = std::upper_bound(
1235                   SectionAddresses.begin(), SectionAddresses.end(), Target,
1236                   [](uint64_t LHS,
1237                       const std::pair<uint64_t, SectionRef> &RHS) {
1238                     return LHS < RHS.first;
1239                   });
1240               if (SectionAddress != SectionAddresses.begin()) {
1241                 --SectionAddress;
1242                 TargetSectionSymbols = &AllSymbols[SectionAddress->second];
1243               } else {
1244                 TargetSectionSymbols = nullptr;
1245               }
1246             }
1247 
1248             // Find the first symbol in the section whose offset is less than
1249             // or equal to the target.
1250             if (TargetSectionSymbols) {
1251               auto TargetSym = std::upper_bound(
1252                   TargetSectionSymbols->begin(), TargetSectionSymbols->end(),
1253                   Target, [](uint64_t LHS,
1254                               const std::pair<uint64_t, StringRef> &RHS) {
1255                     return LHS < RHS.first;
1256                   });
1257               if (TargetSym != TargetSectionSymbols->begin()) {
1258                 --TargetSym;
1259                 uint64_t TargetAddress = std::get<0>(*TargetSym);
1260                 StringRef TargetName = std::get<1>(*TargetSym);
1261                 outs() << " <" << TargetName;
1262                 uint64_t Disp = Target - TargetAddress;
1263                 if (Disp)
1264                   outs() << "+0x" << utohexstr(Disp);
1265                 outs() << '>';
1266               }
1267             }
1268           }
1269         }
1270         outs() << "\n";
1271 
1272         // Print relocation for instruction.
1273         while (rel_cur != rel_end) {
1274           bool hidden = getHidden(*rel_cur);
1275           uint64_t addr = rel_cur->getOffset();
1276           SmallString<16> name;
1277           SmallString<32> val;
1278 
1279           // If this relocation is hidden, skip it.
1280           if (hidden) goto skip_print_rel;
1281 
1282           // Stop when rel_cur's address is past the current instruction.
1283           if (addr >= Index + Size) break;
1284           rel_cur->getTypeName(name);
1285           error(getRelocationValueString(*rel_cur, val));
1286           outs() << format(Fmt.data(), SectionAddr + addr) << name
1287                  << "\t" << val << "\n";
1288 
1289         skip_print_rel:
1290           ++rel_cur;
1291         }
1292       }
1293     }
1294   }
1295 }
1296 
PrintRelocations(const ObjectFile * Obj)1297 void llvm::PrintRelocations(const ObjectFile *Obj) {
1298   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
1299                                                  "%08" PRIx64;
1300   // Regular objdump doesn't print relocations in non-relocatable object
1301   // files.
1302   if (!Obj->isRelocatableObject())
1303     return;
1304 
1305   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1306     if (Section.relocation_begin() == Section.relocation_end())
1307       continue;
1308     StringRef secname;
1309     error(Section.getName(secname));
1310     outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
1311     for (const RelocationRef &Reloc : Section.relocations()) {
1312       bool hidden = getHidden(Reloc);
1313       uint64_t address = Reloc.getOffset();
1314       SmallString<32> relocname;
1315       SmallString<32> valuestr;
1316       if (hidden)
1317         continue;
1318       Reloc.getTypeName(relocname);
1319       error(getRelocationValueString(Reloc, valuestr));
1320       outs() << format(Fmt.data(), address) << " " << relocname << " "
1321              << valuestr << "\n";
1322     }
1323     outs() << "\n";
1324   }
1325 }
1326 
PrintSectionHeaders(const ObjectFile * Obj)1327 void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
1328   outs() << "Sections:\n"
1329             "Idx Name          Size      Address          Type\n";
1330   unsigned i = 0;
1331   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1332     StringRef Name;
1333     error(Section.getName(Name));
1334     uint64_t Address = Section.getAddress();
1335     uint64_t Size = Section.getSize();
1336     bool Text = Section.isText();
1337     bool Data = Section.isData();
1338     bool BSS = Section.isBSS();
1339     std::string Type = (std::string(Text ? "TEXT " : "") +
1340                         (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
1341     outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
1342                      Name.str().c_str(), Size, Address, Type.c_str());
1343     ++i;
1344   }
1345 }
1346 
PrintSectionContents(const ObjectFile * Obj)1347 void llvm::PrintSectionContents(const ObjectFile *Obj) {
1348   std::error_code EC;
1349   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1350     StringRef Name;
1351     StringRef Contents;
1352     error(Section.getName(Name));
1353     uint64_t BaseAddr = Section.getAddress();
1354     uint64_t Size = Section.getSize();
1355     if (!Size)
1356       continue;
1357 
1358     outs() << "Contents of section " << Name << ":\n";
1359     if (Section.isBSS()) {
1360       outs() << format("<skipping contents of bss section at [%04" PRIx64
1361                        ", %04" PRIx64 ")>\n",
1362                        BaseAddr, BaseAddr + Size);
1363       continue;
1364     }
1365 
1366     error(Section.getContents(Contents));
1367 
1368     // Dump out the content as hex and printable ascii characters.
1369     for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
1370       outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
1371       // Dump line of hex.
1372       for (std::size_t i = 0; i < 16; ++i) {
1373         if (i != 0 && i % 4 == 0)
1374           outs() << ' ';
1375         if (addr + i < end)
1376           outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
1377                  << hexdigit(Contents[addr + i] & 0xF, true);
1378         else
1379           outs() << "  ";
1380       }
1381       // Print ascii.
1382       outs() << "  ";
1383       for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
1384         if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
1385           outs() << Contents[addr + i];
1386         else
1387           outs() << ".";
1388       }
1389       outs() << "\n";
1390     }
1391   }
1392 }
1393 
PrintSymbolTable(const ObjectFile * o,StringRef ArchiveName,StringRef ArchitectureName)1394 void llvm::PrintSymbolTable(const ObjectFile *o, StringRef ArchiveName,
1395                             StringRef ArchitectureName) {
1396   outs() << "SYMBOL TABLE:\n";
1397 
1398   if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
1399     printCOFFSymbolTable(coff);
1400     return;
1401   }
1402   for (const SymbolRef &Symbol : o->symbols()) {
1403     Expected<uint64_t> AddressOrError = Symbol.getAddress();
1404     if (!AddressOrError)
1405       report_error(ArchiveName, o->getFileName(), AddressOrError.takeError());
1406     uint64_t Address = *AddressOrError;
1407     Expected<SymbolRef::Type> TypeOrError = Symbol.getType();
1408     if (!TypeOrError)
1409       report_error(ArchiveName, o->getFileName(), TypeOrError.takeError());
1410     SymbolRef::Type Type = *TypeOrError;
1411     uint32_t Flags = Symbol.getFlags();
1412     Expected<section_iterator> SectionOrErr = Symbol.getSection();
1413     error(errorToErrorCode(SectionOrErr.takeError()));
1414     section_iterator Section = *SectionOrErr;
1415     StringRef Name;
1416     if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
1417       Section->getName(Name);
1418     } else {
1419       Expected<StringRef> NameOrErr = Symbol.getName();
1420       if (!NameOrErr)
1421         report_error(ArchiveName, o->getFileName(), NameOrErr.takeError(),
1422                      ArchitectureName);
1423       Name = *NameOrErr;
1424     }
1425 
1426     bool Global = Flags & SymbolRef::SF_Global;
1427     bool Weak = Flags & SymbolRef::SF_Weak;
1428     bool Absolute = Flags & SymbolRef::SF_Absolute;
1429     bool Common = Flags & SymbolRef::SF_Common;
1430     bool Hidden = Flags & SymbolRef::SF_Hidden;
1431 
1432     char GlobLoc = ' ';
1433     if (Type != SymbolRef::ST_Unknown)
1434       GlobLoc = Global ? 'g' : 'l';
1435     char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
1436                  ? 'd' : ' ';
1437     char FileFunc = ' ';
1438     if (Type == SymbolRef::ST_File)
1439       FileFunc = 'f';
1440     else if (Type == SymbolRef::ST_Function)
1441       FileFunc = 'F';
1442 
1443     const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
1444                                                    "%08" PRIx64;
1445 
1446     outs() << format(Fmt, Address) << " "
1447            << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
1448            << (Weak ? 'w' : ' ') // Weak?
1449            << ' ' // Constructor. Not supported yet.
1450            << ' ' // Warning. Not supported yet.
1451            << ' ' // Indirect reference to another symbol.
1452            << Debug // Debugging (d) or dynamic (D) symbol.
1453            << FileFunc // Name of function (F), file (f) or object (O).
1454            << ' ';
1455     if (Absolute) {
1456       outs() << "*ABS*";
1457     } else if (Common) {
1458       outs() << "*COM*";
1459     } else if (Section == o->section_end()) {
1460       outs() << "*UND*";
1461     } else {
1462       if (const MachOObjectFile *MachO =
1463           dyn_cast<const MachOObjectFile>(o)) {
1464         DataRefImpl DR = Section->getRawDataRefImpl();
1465         StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
1466         outs() << SegmentName << ",";
1467       }
1468       StringRef SectionName;
1469       error(Section->getName(SectionName));
1470       outs() << SectionName;
1471     }
1472 
1473     outs() << '\t';
1474     if (Common || isa<ELFObjectFileBase>(o)) {
1475       uint64_t Val =
1476           Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
1477       outs() << format("\t %08" PRIx64 " ", Val);
1478     }
1479 
1480     if (Hidden) {
1481       outs() << ".hidden ";
1482     }
1483     outs() << Name
1484            << '\n';
1485   }
1486 }
1487 
PrintUnwindInfo(const ObjectFile * o)1488 static void PrintUnwindInfo(const ObjectFile *o) {
1489   outs() << "Unwind info:\n\n";
1490 
1491   if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
1492     printCOFFUnwindInfo(coff);
1493   } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1494     printMachOUnwindInfo(MachO);
1495   else {
1496     // TODO: Extract DWARF dump tool to objdump.
1497     errs() << "This operation is only currently supported "
1498               "for COFF and MachO object files.\n";
1499     return;
1500   }
1501 }
1502 
printExportsTrie(const ObjectFile * o)1503 void llvm::printExportsTrie(const ObjectFile *o) {
1504   outs() << "Exports trie:\n";
1505   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1506     printMachOExportsTrie(MachO);
1507   else {
1508     errs() << "This operation is only currently supported "
1509               "for Mach-O executable files.\n";
1510     return;
1511   }
1512 }
1513 
printRebaseTable(const ObjectFile * o)1514 void llvm::printRebaseTable(const ObjectFile *o) {
1515   outs() << "Rebase table:\n";
1516   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1517     printMachORebaseTable(MachO);
1518   else {
1519     errs() << "This operation is only currently supported "
1520               "for Mach-O executable files.\n";
1521     return;
1522   }
1523 }
1524 
printBindTable(const ObjectFile * o)1525 void llvm::printBindTable(const ObjectFile *o) {
1526   outs() << "Bind table:\n";
1527   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1528     printMachOBindTable(MachO);
1529   else {
1530     errs() << "This operation is only currently supported "
1531               "for Mach-O executable files.\n";
1532     return;
1533   }
1534 }
1535 
printLazyBindTable(const ObjectFile * o)1536 void llvm::printLazyBindTable(const ObjectFile *o) {
1537   outs() << "Lazy bind table:\n";
1538   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1539     printMachOLazyBindTable(MachO);
1540   else {
1541     errs() << "This operation is only currently supported "
1542               "for Mach-O executable files.\n";
1543     return;
1544   }
1545 }
1546 
printWeakBindTable(const ObjectFile * o)1547 void llvm::printWeakBindTable(const ObjectFile *o) {
1548   outs() << "Weak bind table:\n";
1549   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1550     printMachOWeakBindTable(MachO);
1551   else {
1552     errs() << "This operation is only currently supported "
1553               "for Mach-O executable files.\n";
1554     return;
1555   }
1556 }
1557 
1558 /// Dump the raw contents of the __clangast section so the output can be piped
1559 /// into llvm-bcanalyzer.
printRawClangAST(const ObjectFile * Obj)1560 void llvm::printRawClangAST(const ObjectFile *Obj) {
1561   if (outs().is_displayed()) {
1562     errs() << "The -raw-clang-ast option will dump the raw binary contents of "
1563               "the clang ast section.\n"
1564               "Please redirect the output to a file or another program such as "
1565               "llvm-bcanalyzer.\n";
1566     return;
1567   }
1568 
1569   StringRef ClangASTSectionName("__clangast");
1570   if (isa<COFFObjectFile>(Obj)) {
1571     ClangASTSectionName = "clangast";
1572   }
1573 
1574   Optional<object::SectionRef> ClangASTSection;
1575   for (auto Sec : ToolSectionFilter(*Obj)) {
1576     StringRef Name;
1577     Sec.getName(Name);
1578     if (Name == ClangASTSectionName) {
1579       ClangASTSection = Sec;
1580       break;
1581     }
1582   }
1583   if (!ClangASTSection)
1584     return;
1585 
1586   StringRef ClangASTContents;
1587   error(ClangASTSection.getValue().getContents(ClangASTContents));
1588   outs().write(ClangASTContents.data(), ClangASTContents.size());
1589 }
1590 
printFaultMaps(const ObjectFile * Obj)1591 static void printFaultMaps(const ObjectFile *Obj) {
1592   const char *FaultMapSectionName = nullptr;
1593 
1594   if (isa<ELFObjectFileBase>(Obj)) {
1595     FaultMapSectionName = ".llvm_faultmaps";
1596   } else if (isa<MachOObjectFile>(Obj)) {
1597     FaultMapSectionName = "__llvm_faultmaps";
1598   } else {
1599     errs() << "This operation is only currently supported "
1600               "for ELF and Mach-O executable files.\n";
1601     return;
1602   }
1603 
1604   Optional<object::SectionRef> FaultMapSection;
1605 
1606   for (auto Sec : ToolSectionFilter(*Obj)) {
1607     StringRef Name;
1608     Sec.getName(Name);
1609     if (Name == FaultMapSectionName) {
1610       FaultMapSection = Sec;
1611       break;
1612     }
1613   }
1614 
1615   outs() << "FaultMap table:\n";
1616 
1617   if (!FaultMapSection.hasValue()) {
1618     outs() << "<not found>\n";
1619     return;
1620   }
1621 
1622   StringRef FaultMapContents;
1623   error(FaultMapSection.getValue().getContents(FaultMapContents));
1624 
1625   FaultMapParser FMP(FaultMapContents.bytes_begin(),
1626                      FaultMapContents.bytes_end());
1627 
1628   outs() << FMP;
1629 }
1630 
printPrivateFileHeaders(const ObjectFile * o)1631 static void printPrivateFileHeaders(const ObjectFile *o) {
1632   if (o->isELF())
1633     printELFFileHeader(o);
1634   else if (o->isCOFF())
1635     printCOFFFileHeader(o);
1636   else if (o->isMachO()) {
1637     printMachOFileHeader(o);
1638     printMachOLoadCommands(o);
1639   } else
1640     report_fatal_error("Invalid/Unsupported object file format");
1641 }
1642 
printFirstPrivateFileHeader(const ObjectFile * o)1643 static void printFirstPrivateFileHeader(const ObjectFile *o) {
1644   if (o->isELF())
1645     printELFFileHeader(o);
1646   else if (o->isCOFF())
1647     printCOFFFileHeader(o);
1648   else if (o->isMachO())
1649     printMachOFileHeader(o);
1650   else
1651     report_fatal_error("Invalid/Unsupported object file format");
1652 }
1653 
DumpObject(const ObjectFile * o,const Archive * a=nullptr)1654 static void DumpObject(const ObjectFile *o, const Archive *a = nullptr) {
1655   StringRef ArchiveName = a != nullptr ? a->getFileName() : "";
1656   // Avoid other output when using a raw option.
1657   if (!RawClangAST) {
1658     outs() << '\n';
1659     if (a)
1660       outs() << a->getFileName() << "(" << o->getFileName() << ")";
1661     else
1662       outs() << o->getFileName();
1663     outs() << ":\tfile format " << o->getFileFormatName() << "\n\n";
1664   }
1665 
1666   if (Disassemble)
1667     DisassembleObject(o, Relocations);
1668   if (Relocations && !Disassemble)
1669     PrintRelocations(o);
1670   if (SectionHeaders)
1671     PrintSectionHeaders(o);
1672   if (SectionContents)
1673     PrintSectionContents(o);
1674   if (SymbolTable)
1675     PrintSymbolTable(o, ArchiveName);
1676   if (UnwindInfo)
1677     PrintUnwindInfo(o);
1678   if (PrivateHeaders)
1679     printPrivateFileHeaders(o);
1680   if (FirstPrivateHeader)
1681     printFirstPrivateFileHeader(o);
1682   if (ExportsTrie)
1683     printExportsTrie(o);
1684   if (Rebase)
1685     printRebaseTable(o);
1686   if (Bind)
1687     printBindTable(o);
1688   if (LazyBind)
1689     printLazyBindTable(o);
1690   if (WeakBind)
1691     printWeakBindTable(o);
1692   if (RawClangAST)
1693     printRawClangAST(o);
1694   if (PrintFaultMaps)
1695     printFaultMaps(o);
1696   if (DwarfDumpType != DIDT_Null) {
1697     std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(*o));
1698     // Dump the complete DWARF structure.
1699     DICtx->dump(outs(), DwarfDumpType, true /* DumpEH */);
1700   }
1701 }
1702 
1703 /// @brief Dump each object file in \a a;
DumpArchive(const Archive * a)1704 static void DumpArchive(const Archive *a) {
1705   Error Err;
1706   for (auto &C : a->children(Err)) {
1707     Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1708     if (!ChildOrErr) {
1709       if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1710         report_error(a->getFileName(), C, std::move(E));
1711       continue;
1712     }
1713     if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
1714       DumpObject(o, a);
1715     else
1716       report_error(a->getFileName(), object_error::invalid_file_type);
1717   }
1718   if (Err)
1719     report_error(a->getFileName(), std::move(Err));
1720 }
1721 
1722 /// @brief Open file and figure out how to dump it.
DumpInput(StringRef file)1723 static void DumpInput(StringRef file) {
1724 
1725   // If we are using the Mach-O specific object file parser, then let it parse
1726   // the file and process the command line options.  So the -arch flags can
1727   // be used to select specific slices, etc.
1728   if (MachOOpt) {
1729     ParseInputMachO(file);
1730     return;
1731   }
1732 
1733   // Attempt to open the binary.
1734   Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
1735   if (!BinaryOrErr)
1736     report_error(file, BinaryOrErr.takeError());
1737   Binary &Binary = *BinaryOrErr.get().getBinary();
1738 
1739   if (Archive *a = dyn_cast<Archive>(&Binary))
1740     DumpArchive(a);
1741   else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
1742     DumpObject(o);
1743   else
1744     report_error(file, object_error::invalid_file_type);
1745 }
1746 
main(int argc,char ** argv)1747 int main(int argc, char **argv) {
1748   // Print a stack trace if we signal out.
1749   sys::PrintStackTraceOnErrorSignal(argv[0]);
1750   PrettyStackTraceProgram X(argc, argv);
1751   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
1752 
1753   // Initialize targets and assembly printers/parsers.
1754   llvm::InitializeAllTargetInfos();
1755   llvm::InitializeAllTargetMCs();
1756   llvm::InitializeAllDisassemblers();
1757 
1758   // Register the target printer for --version.
1759   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
1760 
1761   cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
1762   TripleName = Triple::normalize(TripleName);
1763 
1764   ToolName = argv[0];
1765 
1766   // Defaults to a.out if no filenames specified.
1767   if (InputFilenames.size() == 0)
1768     InputFilenames.push_back("a.out");
1769 
1770   if (DisassembleAll)
1771     Disassemble = true;
1772   if (!Disassemble
1773       && !Relocations
1774       && !SectionHeaders
1775       && !SectionContents
1776       && !SymbolTable
1777       && !UnwindInfo
1778       && !PrivateHeaders
1779       && !FirstPrivateHeader
1780       && !ExportsTrie
1781       && !Rebase
1782       && !Bind
1783       && !LazyBind
1784       && !WeakBind
1785       && !RawClangAST
1786       && !(UniversalHeaders && MachOOpt)
1787       && !(ArchiveHeaders && MachOOpt)
1788       && !(IndirectSymbols && MachOOpt)
1789       && !(DataInCode && MachOOpt)
1790       && !(LinkOptHints && MachOOpt)
1791       && !(InfoPlist && MachOOpt)
1792       && !(DylibsUsed && MachOOpt)
1793       && !(DylibId && MachOOpt)
1794       && !(ObjcMetaData && MachOOpt)
1795       && !(FilterSections.size() != 0 && MachOOpt)
1796       && !PrintFaultMaps
1797       && DwarfDumpType == DIDT_Null) {
1798     cl::PrintHelpMessage();
1799     return 2;
1800   }
1801 
1802   std::for_each(InputFilenames.begin(), InputFilenames.end(),
1803                 DumpInput);
1804 
1805   return EXIT_SUCCESS;
1806 }
1807