xref: /llvm-project/llvm/tools/llvm-objdump/llvm-objdump.cpp (revision 030ad37ef46a298287da6a5c78d62d54e2d77599)
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/StringSet.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/CodeGen/FaultMaps.h"
26 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
27 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
28 #include "llvm/Demangle/Demangle.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCContext.h"
31 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
32 #include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
33 #include "llvm/MC/MCInst.h"
34 #include "llvm/MC/MCInstPrinter.h"
35 #include "llvm/MC/MCInstrAnalysis.h"
36 #include "llvm/MC/MCInstrInfo.h"
37 #include "llvm/MC/MCObjectFileInfo.h"
38 #include "llvm/MC/MCRegisterInfo.h"
39 #include "llvm/MC/MCSubtargetInfo.h"
40 #include "llvm/Object/Archive.h"
41 #include "llvm/Object/COFF.h"
42 #include "llvm/Object/COFFImportFile.h"
43 #include "llvm/Object/ELFObjectFile.h"
44 #include "llvm/Object/MachO.h"
45 #include "llvm/Object/MachOUniversal.h"
46 #include "llvm/Object/ObjectFile.h"
47 #include "llvm/Object/Wasm.h"
48 #include "llvm/Support/Casting.h"
49 #include "llvm/Support/CommandLine.h"
50 #include "llvm/Support/Debug.h"
51 #include "llvm/Support/Errc.h"
52 #include "llvm/Support/FileSystem.h"
53 #include "llvm/Support/Format.h"
54 #include "llvm/Support/GraphWriter.h"
55 #include "llvm/Support/Host.h"
56 #include "llvm/Support/InitLLVM.h"
57 #include "llvm/Support/MemoryBuffer.h"
58 #include "llvm/Support/SourceMgr.h"
59 #include "llvm/Support/TargetRegistry.h"
60 #include "llvm/Support/TargetSelect.h"
61 #include "llvm/Support/raw_ostream.h"
62 #include <algorithm>
63 #include <cctype>
64 #include <cstring>
65 #include <system_error>
66 #include <unordered_map>
67 #include <utility>
68 
69 using namespace llvm;
70 using namespace object;
71 
72 cl::opt<bool>
73     llvm::AllHeaders("all-headers",
74                      cl::desc("Display all available header information"));
75 static cl::alias AllHeadersShort("x", cl::desc("Alias for --all-headers"),
76                                  cl::aliasopt(AllHeaders));
77 
78 static cl::list<std::string>
79 InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
80 
81 cl::opt<bool>
82 llvm::Disassemble("disassemble",
83   cl::desc("Display assembler mnemonics for the machine instructions"));
84 static cl::alias
85 Disassembled("d", cl::desc("Alias for --disassemble"),
86              cl::aliasopt(Disassemble));
87 
88 cl::opt<bool>
89 llvm::DisassembleAll("disassemble-all",
90   cl::desc("Display assembler mnemonics for the machine instructions"));
91 static cl::alias
92 DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
93              cl::aliasopt(DisassembleAll));
94 
95 cl::opt<bool> llvm::Demangle("demangle", cl::desc("Demangle symbols names"),
96                              cl::init(false));
97 
98 static cl::alias DemangleShort("C", cl::desc("Alias for --demangle"),
99                                cl::aliasopt(llvm::Demangle));
100 
101 static cl::list<std::string>
102 DisassembleFunctions("df",
103                      cl::CommaSeparated,
104                      cl::desc("List of functions to disassemble"));
105 static StringSet<> DisasmFuncsSet;
106 
107 cl::opt<bool>
108 llvm::Relocations("r", cl::desc("Display the relocation entries in the file"));
109 
110 cl::opt<bool>
111 llvm::DynamicRelocations("dynamic-reloc",
112   cl::desc("Display the dynamic relocation entries in the file"));
113 static cl::alias
114 DynamicRelocationsd("R", cl::desc("Alias for --dynamic-reloc"),
115              cl::aliasopt(DynamicRelocations));
116 
117 cl::opt<bool>
118 llvm::SectionContents("s", cl::desc("Display the content of each section"));
119 
120 cl::opt<bool>
121 llvm::SymbolTable("t", cl::desc("Display the symbol table"));
122 
123 cl::opt<bool>
124 llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
125 
126 cl::opt<bool>
127 llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
128 
129 cl::opt<bool>
130 llvm::Bind("bind", cl::desc("Display mach-o binding info"));
131 
132 cl::opt<bool>
133 llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
134 
135 cl::opt<bool>
136 llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
137 
138 cl::opt<bool>
139 llvm::RawClangAST("raw-clang-ast",
140     cl::desc("Dump the raw binary contents of the clang AST section"));
141 
142 static cl::opt<bool>
143 MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
144 static cl::alias
145 MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
146 
147 cl::opt<std::string>
148 llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
149                                     "see -version for available targets"));
150 
151 cl::opt<std::string>
152 llvm::MCPU("mcpu",
153      cl::desc("Target a specific cpu type (-mcpu=help for details)"),
154      cl::value_desc("cpu-name"),
155      cl::init(""));
156 
157 cl::opt<std::string>
158 llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, "
159                                 "see -version for available targets"));
160 
161 cl::opt<bool>
162 llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the "
163                                                  "headers for each section."));
164 static cl::alias
165 SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
166                     cl::aliasopt(SectionHeaders));
167 static cl::alias
168 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
169                       cl::aliasopt(SectionHeaders));
170 
171 cl::list<std::string>
172 llvm::FilterSections("section", cl::desc("Operate on the specified sections only. "
173                                          "With -macho dump segment,section"));
174 cl::alias
175 static FilterSectionsj("j", cl::desc("Alias for --section"),
176                  cl::aliasopt(llvm::FilterSections));
177 
178 cl::list<std::string>
179 llvm::MAttrs("mattr",
180   cl::CommaSeparated,
181   cl::desc("Target specific attributes"),
182   cl::value_desc("a1,+a2,-a3,..."));
183 
184 cl::opt<bool>
185 llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
186                                                  "instructions, do not print "
187                                                  "the instruction bytes."));
188 cl::opt<bool>
189 llvm::NoLeadingAddr("no-leading-addr", cl::desc("Print no leading address"));
190 
191 cl::opt<bool>
192 llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information"));
193 
194 static cl::alias
195 UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
196                 cl::aliasopt(UnwindInfo));
197 
198 cl::opt<bool>
199 llvm::PrivateHeaders("private-headers",
200                      cl::desc("Display format specific file headers"));
201 
202 cl::opt<bool>
203 llvm::FirstPrivateHeader("private-header",
204                          cl::desc("Display only the first format specific file "
205                                   "header"));
206 
207 static cl::alias
208 PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
209                     cl::aliasopt(PrivateHeaders));
210 
211 cl::opt<bool> llvm::FileHeaders(
212     "file-headers",
213     cl::desc("Display the contents of the overall file header"));
214 
215 static cl::alias FileHeadersShort("f", cl::desc("Alias for --file-headers"),
216                                   cl::aliasopt(FileHeaders));
217 
218 cl::opt<bool>
219     llvm::ArchiveHeaders("archive-headers",
220                          cl::desc("Display archive header information"));
221 
222 cl::alias
223 ArchiveHeadersShort("a", cl::desc("Alias for --archive-headers"),
224                     cl::aliasopt(ArchiveHeaders));
225 
226 cl::opt<bool>
227     llvm::PrintImmHex("print-imm-hex",
228                       cl::desc("Use hex format for immediate values"));
229 
230 cl::opt<bool> PrintFaultMaps("fault-map-section",
231                              cl::desc("Display contents of faultmap section"));
232 
233 cl::opt<DIDumpType> llvm::DwarfDumpType(
234     "dwarf", cl::init(DIDT_Null), cl::desc("Dump of dwarf debug sections:"),
235     cl::values(clEnumValN(DIDT_DebugFrame, "frames", ".debug_frame")));
236 
237 cl::opt<bool> PrintSource(
238     "source",
239     cl::desc(
240         "Display source inlined with disassembly. Implies disassemble object"));
241 
242 cl::alias PrintSourceShort("S", cl::desc("Alias for -source"),
243                            cl::aliasopt(PrintSource));
244 
245 cl::opt<bool> PrintLines("line-numbers",
246                          cl::desc("Display source line numbers with "
247                                   "disassembly. Implies disassemble object"));
248 
249 cl::alias PrintLinesShort("l", cl::desc("Alias for -line-numbers"),
250                           cl::aliasopt(PrintLines));
251 
252 cl::opt<unsigned long long>
253     StartAddress("start-address", cl::desc("Disassemble beginning at address"),
254                  cl::value_desc("address"), cl::init(0));
255 cl::opt<unsigned long long>
256     StopAddress("stop-address", cl::desc("Stop disassembly at address"),
257                 cl::value_desc("address"), cl::init(UINT64_MAX));
258 static StringRef ToolName;
259 
260 typedef std::vector<std::tuple<uint64_t, StringRef, uint8_t>> SectionSymbolsTy;
261 
262 namespace {
263 typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
264 
265 class SectionFilterIterator {
266 public:
267   SectionFilterIterator(FilterPredicate P,
268                         llvm::object::section_iterator const &I,
269                         llvm::object::section_iterator const &E)
270       : Predicate(std::move(P)), Iterator(I), End(E) {
271     ScanPredicate();
272   }
273   const llvm::object::SectionRef &operator*() const { return *Iterator; }
274   SectionFilterIterator &operator++() {
275     ++Iterator;
276     ScanPredicate();
277     return *this;
278   }
279   bool operator!=(SectionFilterIterator const &Other) const {
280     return Iterator != Other.Iterator;
281   }
282 
283 private:
284   void ScanPredicate() {
285     while (Iterator != End && !Predicate(*Iterator)) {
286       ++Iterator;
287     }
288   }
289   FilterPredicate Predicate;
290   llvm::object::section_iterator Iterator;
291   llvm::object::section_iterator End;
292 };
293 
294 class SectionFilter {
295 public:
296   SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
297       : Predicate(std::move(P)), Object(O) {}
298   SectionFilterIterator begin() {
299     return SectionFilterIterator(Predicate, Object.section_begin(),
300                                  Object.section_end());
301   }
302   SectionFilterIterator end() {
303     return SectionFilterIterator(Predicate, Object.section_end(),
304                                  Object.section_end());
305   }
306 
307 private:
308   FilterPredicate Predicate;
309   llvm::object::ObjectFile const &Object;
310 };
311 SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) {
312   return SectionFilter(
313       [](llvm::object::SectionRef const &S) {
314         if (FilterSections.empty())
315           return true;
316         llvm::StringRef String;
317         std::error_code error = S.getName(String);
318         if (error)
319           return false;
320         return is_contained(FilterSections, String);
321       },
322       O);
323 }
324 }
325 
326 void llvm::error(std::error_code EC) {
327   if (!EC)
328     return;
329 
330   errs() << ToolName << ": error reading file: " << EC.message() << ".\n";
331   errs().flush();
332   exit(1);
333 }
334 
335 LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) {
336   errs() << ToolName << ": " << Message << ".\n";
337   errs().flush();
338   exit(1);
339 }
340 
341 void llvm::warn(StringRef Message) {
342   errs() << ToolName << ": warning: " << Message << ".\n";
343   errs().flush();
344 }
345 
346 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
347                                                 Twine Message) {
348   errs() << ToolName << ": '" << File << "': " << Message << ".\n";
349   exit(1);
350 }
351 
352 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
353                                                 std::error_code EC) {
354   assert(EC);
355   errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
356   exit(1);
357 }
358 
359 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
360                                                 llvm::Error E) {
361   assert(E);
362   std::string Buf;
363   raw_string_ostream OS(Buf);
364   logAllUnhandledErrors(std::move(E), OS, "");
365   OS.flush();
366   errs() << ToolName << ": '" << File << "': " << Buf;
367   exit(1);
368 }
369 
370 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName,
371                                                 StringRef FileName,
372                                                 llvm::Error E,
373                                                 StringRef ArchitectureName) {
374   assert(E);
375   errs() << ToolName << ": ";
376   if (ArchiveName != "")
377     errs() << ArchiveName << "(" << FileName << ")";
378   else
379     errs() << "'" << FileName << "'";
380   if (!ArchitectureName.empty())
381     errs() << " (for architecture " << ArchitectureName << ")";
382   std::string Buf;
383   raw_string_ostream OS(Buf);
384   logAllUnhandledErrors(std::move(E), OS, "");
385   OS.flush();
386   errs() << ": " << Buf;
387   exit(1);
388 }
389 
390 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName,
391                                                 const object::Archive::Child &C,
392                                                 llvm::Error E,
393                                                 StringRef ArchitectureName) {
394   Expected<StringRef> NameOrErr = C.getName();
395   // TODO: if we have a error getting the name then it would be nice to print
396   // the index of which archive member this is and or its offset in the
397   // archive instead of "???" as the name.
398   if (!NameOrErr) {
399     consumeError(NameOrErr.takeError());
400     llvm::report_error(ArchiveName, "???", std::move(E), ArchitectureName);
401   } else
402     llvm::report_error(ArchiveName, NameOrErr.get(), std::move(E),
403                        ArchitectureName);
404 }
405 
406 static const Target *getTarget(const ObjectFile *Obj = nullptr) {
407   // Figure out the target triple.
408   llvm::Triple TheTriple("unknown-unknown-unknown");
409   if (TripleName.empty()) {
410     if (Obj) {
411       TheTriple = Obj->makeTriple();
412     }
413   } else {
414     TheTriple.setTriple(Triple::normalize(TripleName));
415 
416     // Use the triple, but also try to combine with ARM build attributes.
417     if (Obj) {
418       auto Arch = Obj->getArch();
419       if (Arch == Triple::arm || Arch == Triple::armeb) {
420         Obj->setARMSubArch(TheTriple);
421       }
422     }
423   }
424 
425   // Get the target specific parser.
426   std::string Error;
427   const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
428                                                          Error);
429   if (!TheTarget) {
430     if (Obj)
431       report_error(Obj->getFileName(), "can't find target: " + Error);
432     else
433       error("can't find target: " + Error);
434   }
435 
436   // Update the triple name and return the found target.
437   TripleName = TheTriple.getTriple();
438   return TheTarget;
439 }
440 
441 bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
442   return a.getOffset() < b.getOffset();
443 }
444 
445 template <class ELFT>
446 static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
447                                                 const RelocationRef &RelRef,
448                                                 SmallVectorImpl<char> &Result) {
449   DataRefImpl Rel = RelRef.getRawDataRefImpl();
450 
451   typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
452   typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
453   typedef typename ELFObjectFile<ELFT>::Elf_Rela Elf_Rela;
454 
455   const ELFFile<ELFT> &EF = *Obj->getELFFile();
456 
457   auto SecOrErr = EF.getSection(Rel.d.a);
458   if (!SecOrErr)
459     return errorToErrorCode(SecOrErr.takeError());
460   const Elf_Shdr *Sec = *SecOrErr;
461   auto SymTabOrErr = EF.getSection(Sec->sh_link);
462   if (!SymTabOrErr)
463     return errorToErrorCode(SymTabOrErr.takeError());
464   const Elf_Shdr *SymTab = *SymTabOrErr;
465   assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
466          SymTab->sh_type == ELF::SHT_DYNSYM);
467   auto StrTabSec = EF.getSection(SymTab->sh_link);
468   if (!StrTabSec)
469     return errorToErrorCode(StrTabSec.takeError());
470   auto StrTabOrErr = EF.getStringTable(*StrTabSec);
471   if (!StrTabOrErr)
472     return errorToErrorCode(StrTabOrErr.takeError());
473   StringRef StrTab = *StrTabOrErr;
474   int64_t addend = 0;
475   // If there is no Symbol associated with the relocation, we set the undef
476   // boolean value to 'true'. This will prevent us from calling functions that
477   // requires the relocation to be associated with a symbol.
478   bool undef = false;
479   switch (Sec->sh_type) {
480   default:
481     return object_error::parse_failed;
482   case ELF::SHT_REL: {
483     // TODO: Read implicit addend from section data.
484     break;
485   }
486   case ELF::SHT_RELA: {
487     const Elf_Rela *ERela = Obj->getRela(Rel);
488     addend = ERela->r_addend;
489     undef = ERela->getSymbol(false) == 0;
490     break;
491   }
492   }
493   StringRef Target;
494   if (!undef) {
495     symbol_iterator SI = RelRef.getSymbol();
496     const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl());
497     if (symb->getType() == ELF::STT_SECTION) {
498       Expected<section_iterator> SymSI = SI->getSection();
499       if (!SymSI)
500         return errorToErrorCode(SymSI.takeError());
501       const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl());
502       auto SecName = EF.getSectionName(SymSec);
503       if (!SecName)
504         return errorToErrorCode(SecName.takeError());
505       Target = *SecName;
506     } else {
507       Expected<StringRef> SymName = symb->getName(StrTab);
508       if (!SymName)
509         return errorToErrorCode(SymName.takeError());
510       Target = *SymName;
511     }
512   } else
513     Target = "*ABS*";
514 
515   // Default scheme is to print Target, as well as "+ <addend>" for nonzero
516   // addend. Should be acceptable for all normal purposes.
517   std::string fmtbuf;
518   raw_string_ostream fmt(fmtbuf);
519   fmt << Target;
520   if (addend != 0)
521     fmt << (addend < 0 ? "" : "+") << addend;
522   fmt.flush();
523   Result.append(fmtbuf.begin(), fmtbuf.end());
524   return std::error_code();
525 }
526 
527 static std::error_code getRelocationValueString(const ELFObjectFileBase *Obj,
528                                                 const RelocationRef &Rel,
529                                                 SmallVectorImpl<char> &Result) {
530   if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
531     return getRelocationValueString(ELF32LE, Rel, Result);
532   if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
533     return getRelocationValueString(ELF64LE, Rel, Result);
534   if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
535     return getRelocationValueString(ELF32BE, Rel, Result);
536   auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
537   return getRelocationValueString(ELF64BE, Rel, Result);
538 }
539 
540 static std::error_code getRelocationValueString(const COFFObjectFile *Obj,
541                                                 const RelocationRef &Rel,
542                                                 SmallVectorImpl<char> &Result) {
543   symbol_iterator SymI = Rel.getSymbol();
544   Expected<StringRef> SymNameOrErr = SymI->getName();
545   if (!SymNameOrErr)
546     return errorToErrorCode(SymNameOrErr.takeError());
547   StringRef SymName = *SymNameOrErr;
548   Result.append(SymName.begin(), SymName.end());
549   return std::error_code();
550 }
551 
552 static void printRelocationTargetName(const MachOObjectFile *O,
553                                       const MachO::any_relocation_info &RE,
554                                       raw_string_ostream &fmt) {
555   bool IsScattered = O->isRelocationScattered(RE);
556 
557   // Target of a scattered relocation is an address.  In the interest of
558   // generating pretty output, scan through the symbol table looking for a
559   // symbol that aligns with that address.  If we find one, print it.
560   // Otherwise, we just print the hex address of the target.
561   if (IsScattered) {
562     uint32_t Val = O->getPlainRelocationSymbolNum(RE);
563 
564     for (const SymbolRef &Symbol : O->symbols()) {
565       std::error_code ec;
566       Expected<uint64_t> Addr = Symbol.getAddress();
567       if (!Addr)
568         report_error(O->getFileName(), Addr.takeError());
569       if (*Addr != Val)
570         continue;
571       Expected<StringRef> Name = Symbol.getName();
572       if (!Name)
573         report_error(O->getFileName(), Name.takeError());
574       fmt << *Name;
575       return;
576     }
577 
578     // If we couldn't find a symbol that this relocation refers to, try
579     // to find a section beginning instead.
580     for (const SectionRef &Section : ToolSectionFilter(*O)) {
581       std::error_code ec;
582 
583       StringRef Name;
584       uint64_t Addr = Section.getAddress();
585       if (Addr != Val)
586         continue;
587       if ((ec = Section.getName(Name)))
588         report_error(O->getFileName(), ec);
589       fmt << Name;
590       return;
591     }
592 
593     fmt << format("0x%x", Val);
594     return;
595   }
596 
597   StringRef S;
598   bool isExtern = O->getPlainRelocationExternal(RE);
599   uint64_t Val = O->getPlainRelocationSymbolNum(RE);
600 
601   if (O->getAnyRelocationType(RE) == MachO::ARM64_RELOC_ADDEND) {
602     fmt << format("0x%0" PRIx64, Val);
603     return;
604   } else if (isExtern) {
605     symbol_iterator SI = O->symbol_begin();
606     advance(SI, Val);
607     Expected<StringRef> SOrErr = SI->getName();
608     if (!SOrErr)
609       report_error(O->getFileName(), SOrErr.takeError());
610     S = *SOrErr;
611   } else {
612     section_iterator SI = O->section_begin();
613     // Adjust for the fact that sections are 1-indexed.
614     if (Val == 0) {
615       fmt << "0 (?,?)";
616       return;
617     }
618     uint32_t i = Val - 1;
619     while (i != 0 && SI != O->section_end()) {
620       i--;
621       advance(SI, 1);
622     }
623     if (SI == O->section_end())
624       fmt << Val << " (?,?)";
625     else
626       SI->getName(S);
627   }
628 
629   fmt << S;
630 }
631 
632 static std::error_code getRelocationValueString(const WasmObjectFile *Obj,
633                                                 const RelocationRef &RelRef,
634                                                 SmallVectorImpl<char> &Result) {
635   const wasm::WasmRelocation& Rel = Obj->getWasmRelocation(RelRef);
636   symbol_iterator SI = RelRef.getSymbol();
637   std::string fmtbuf;
638   raw_string_ostream fmt(fmtbuf);
639   if (SI == Obj->symbol_end()) {
640     // Not all wasm relocations have symbols associated with them.
641     // In particular R_WEBASSEMBLY_TYPE_INDEX_LEB.
642     fmt << Rel.Index;
643   } else {
644     Expected<StringRef> SymNameOrErr = SI->getName();
645     if (!SymNameOrErr)
646       return errorToErrorCode(SymNameOrErr.takeError());
647     StringRef SymName = *SymNameOrErr;
648     Result.append(SymName.begin(), SymName.end());
649   }
650   fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend;
651   fmt.flush();
652   Result.append(fmtbuf.begin(), fmtbuf.end());
653   return std::error_code();
654 }
655 
656 static std::error_code getRelocationValueString(const MachOObjectFile *Obj,
657                                                 const RelocationRef &RelRef,
658                                                 SmallVectorImpl<char> &Result) {
659   DataRefImpl Rel = RelRef.getRawDataRefImpl();
660   MachO::any_relocation_info RE = Obj->getRelocation(Rel);
661 
662   unsigned Arch = Obj->getArch();
663 
664   std::string fmtbuf;
665   raw_string_ostream fmt(fmtbuf);
666   unsigned Type = Obj->getAnyRelocationType(RE);
667   bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
668 
669   // Determine any addends that should be displayed with the relocation.
670   // These require decoding the relocation type, which is triple-specific.
671 
672   // X86_64 has entirely custom relocation types.
673   if (Arch == Triple::x86_64) {
674     bool isPCRel = Obj->getAnyRelocationPCRel(RE);
675 
676     switch (Type) {
677     case MachO::X86_64_RELOC_GOT_LOAD:
678     case MachO::X86_64_RELOC_GOT: {
679       printRelocationTargetName(Obj, RE, fmt);
680       fmt << "@GOT";
681       if (isPCRel)
682         fmt << "PCREL";
683       break;
684     }
685     case MachO::X86_64_RELOC_SUBTRACTOR: {
686       DataRefImpl RelNext = Rel;
687       Obj->moveRelocationNext(RelNext);
688       MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
689 
690       // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
691       // X86_64_RELOC_UNSIGNED.
692       // NOTE: Scattered relocations don't exist on x86_64.
693       unsigned RType = Obj->getAnyRelocationType(RENext);
694       if (RType != MachO::X86_64_RELOC_UNSIGNED)
695         report_error(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after "
696                      "X86_64_RELOC_SUBTRACTOR.");
697 
698       // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
699       // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
700       printRelocationTargetName(Obj, RENext, fmt);
701       fmt << "-";
702       printRelocationTargetName(Obj, RE, fmt);
703       break;
704     }
705     case MachO::X86_64_RELOC_TLV:
706       printRelocationTargetName(Obj, RE, fmt);
707       fmt << "@TLV";
708       if (isPCRel)
709         fmt << "P";
710       break;
711     case MachO::X86_64_RELOC_SIGNED_1:
712       printRelocationTargetName(Obj, RE, fmt);
713       fmt << "-1";
714       break;
715     case MachO::X86_64_RELOC_SIGNED_2:
716       printRelocationTargetName(Obj, RE, fmt);
717       fmt << "-2";
718       break;
719     case MachO::X86_64_RELOC_SIGNED_4:
720       printRelocationTargetName(Obj, RE, fmt);
721       fmt << "-4";
722       break;
723     default:
724       printRelocationTargetName(Obj, RE, fmt);
725       break;
726     }
727     // X86 and ARM share some relocation types in common.
728   } else if (Arch == Triple::x86 || Arch == Triple::arm ||
729              Arch == Triple::ppc) {
730     // Generic relocation types...
731     switch (Type) {
732     case MachO::GENERIC_RELOC_PAIR: // prints no info
733       return std::error_code();
734     case MachO::GENERIC_RELOC_SECTDIFF: {
735       DataRefImpl RelNext = Rel;
736       Obj->moveRelocationNext(RelNext);
737       MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
738 
739       // X86 sect diff's must be followed by a relocation of type
740       // GENERIC_RELOC_PAIR.
741       unsigned RType = Obj->getAnyRelocationType(RENext);
742 
743       if (RType != MachO::GENERIC_RELOC_PAIR)
744         report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
745                      "GENERIC_RELOC_SECTDIFF.");
746 
747       printRelocationTargetName(Obj, RE, fmt);
748       fmt << "-";
749       printRelocationTargetName(Obj, RENext, fmt);
750       break;
751     }
752     }
753 
754     if (Arch == Triple::x86 || Arch == Triple::ppc) {
755       switch (Type) {
756       case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
757         DataRefImpl RelNext = Rel;
758         Obj->moveRelocationNext(RelNext);
759         MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
760 
761         // X86 sect diff's must be followed by a relocation of type
762         // GENERIC_RELOC_PAIR.
763         unsigned RType = Obj->getAnyRelocationType(RENext);
764         if (RType != MachO::GENERIC_RELOC_PAIR)
765           report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
766                        "GENERIC_RELOC_LOCAL_SECTDIFF.");
767 
768         printRelocationTargetName(Obj, RE, fmt);
769         fmt << "-";
770         printRelocationTargetName(Obj, RENext, fmt);
771         break;
772       }
773       case MachO::GENERIC_RELOC_TLV: {
774         printRelocationTargetName(Obj, RE, fmt);
775         fmt << "@TLV";
776         if (IsPCRel)
777           fmt << "P";
778         break;
779       }
780       default:
781         printRelocationTargetName(Obj, RE, fmt);
782       }
783     } else { // ARM-specific relocations
784       switch (Type) {
785       case MachO::ARM_RELOC_HALF:
786       case MachO::ARM_RELOC_HALF_SECTDIFF: {
787         // Half relocations steal a bit from the length field to encode
788         // whether this is an upper16 or a lower16 relocation.
789         bool isUpper = (Obj->getAnyRelocationLength(RE) & 0x1) == 1;
790 
791         if (isUpper)
792           fmt << ":upper16:(";
793         else
794           fmt << ":lower16:(";
795         printRelocationTargetName(Obj, RE, fmt);
796 
797         DataRefImpl RelNext = Rel;
798         Obj->moveRelocationNext(RelNext);
799         MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
800 
801         // ARM half relocs must be followed by a relocation of type
802         // ARM_RELOC_PAIR.
803         unsigned RType = Obj->getAnyRelocationType(RENext);
804         if (RType != MachO::ARM_RELOC_PAIR)
805           report_error(Obj->getFileName(), "Expected ARM_RELOC_PAIR after "
806                        "ARM_RELOC_HALF");
807 
808         // NOTE: The half of the target virtual address is stashed in the
809         // address field of the secondary relocation, but we can't reverse
810         // engineer the constant offset from it without decoding the movw/movt
811         // instruction to find the other half in its immediate field.
812 
813         // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
814         // symbol/section pointer of the follow-on relocation.
815         if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
816           fmt << "-";
817           printRelocationTargetName(Obj, RENext, fmt);
818         }
819 
820         fmt << ")";
821         break;
822       }
823       default: { printRelocationTargetName(Obj, RE, fmt); }
824       }
825     }
826   } else
827     printRelocationTargetName(Obj, RE, fmt);
828 
829   fmt.flush();
830   Result.append(fmtbuf.begin(), fmtbuf.end());
831   return std::error_code();
832 }
833 
834 static std::error_code getRelocationValueString(const RelocationRef &Rel,
835                                                 SmallVectorImpl<char> &Result) {
836   const ObjectFile *Obj = Rel.getObject();
837   if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
838     return getRelocationValueString(ELF, Rel, Result);
839   if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
840     return getRelocationValueString(COFF, Rel, Result);
841   if (auto *Wasm = dyn_cast<WasmObjectFile>(Obj))
842     return getRelocationValueString(Wasm, Rel, Result);
843   if (auto *MachO = dyn_cast<MachOObjectFile>(Obj))
844     return getRelocationValueString(MachO, Rel, Result);
845   llvm_unreachable("unknown object file format");
846 }
847 
848 /// Indicates whether this relocation should hidden when listing
849 /// relocations, usually because it is the trailing part of a multipart
850 /// relocation that will be printed as part of the leading relocation.
851 static bool getHidden(RelocationRef RelRef) {
852   const ObjectFile *Obj = RelRef.getObject();
853   auto *MachO = dyn_cast<MachOObjectFile>(Obj);
854   if (!MachO)
855     return false;
856 
857   unsigned Arch = MachO->getArch();
858   DataRefImpl Rel = RelRef.getRawDataRefImpl();
859   uint64_t Type = MachO->getRelocationType(Rel);
860 
861   // On arches that use the generic relocations, GENERIC_RELOC_PAIR
862   // is always hidden.
863   if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
864     if (Type == MachO::GENERIC_RELOC_PAIR)
865       return true;
866   } else if (Arch == Triple::x86_64) {
867     // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
868     // an X86_64_RELOC_SUBTRACTOR.
869     if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
870       DataRefImpl RelPrev = Rel;
871       RelPrev.d.a--;
872       uint64_t PrevType = MachO->getRelocationType(RelPrev);
873       if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
874         return true;
875     }
876   }
877 
878   return false;
879 }
880 
881 namespace {
882 class SourcePrinter {
883 protected:
884   DILineInfo OldLineInfo;
885   const ObjectFile *Obj = nullptr;
886   std::unique_ptr<symbolize::LLVMSymbolizer> Symbolizer;
887   // File name to file contents of source
888   std::unordered_map<std::string, std::unique_ptr<MemoryBuffer>> SourceCache;
889   // Mark the line endings of the cached source
890   std::unordered_map<std::string, std::vector<StringRef>> LineCache;
891 
892 private:
893   bool cacheSource(const DILineInfo& LineInfoFile);
894 
895 public:
896   SourcePrinter() = default;
897   SourcePrinter(const ObjectFile *Obj, StringRef DefaultArch) : Obj(Obj) {
898     symbolize::LLVMSymbolizer::Options SymbolizerOpts(
899         DILineInfoSpecifier::FunctionNameKind::None, true, false, false,
900         DefaultArch);
901     Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts));
902   }
903   virtual ~SourcePrinter() = default;
904   virtual void printSourceLine(raw_ostream &OS, uint64_t Address,
905                                StringRef Delimiter = "; ");
906 };
907 
908 bool SourcePrinter::cacheSource(const DILineInfo &LineInfo) {
909   std::unique_ptr<MemoryBuffer> Buffer;
910   if (LineInfo.Source) {
911     Buffer = MemoryBuffer::getMemBuffer(*LineInfo.Source);
912   } else {
913     auto BufferOrError = MemoryBuffer::getFile(LineInfo.FileName);
914     if (!BufferOrError)
915       return false;
916     Buffer = std::move(*BufferOrError);
917   }
918   // Chomp the file to get lines
919   size_t BufferSize = Buffer->getBufferSize();
920   const char *BufferStart = Buffer->getBufferStart();
921   for (const char *Start = BufferStart, *End = BufferStart;
922        End < BufferStart + BufferSize; End++)
923     if (*End == '\n' || End == BufferStart + BufferSize - 1 ||
924         (*End == '\r' && *(End + 1) == '\n')) {
925       LineCache[LineInfo.FileName].push_back(StringRef(Start, End - Start));
926       if (*End == '\r')
927         End++;
928       Start = End + 1;
929     }
930   SourceCache[LineInfo.FileName] = std::move(Buffer);
931   return true;
932 }
933 
934 void SourcePrinter::printSourceLine(raw_ostream &OS, uint64_t Address,
935                                     StringRef Delimiter) {
936   if (!Symbolizer)
937     return;
938   DILineInfo LineInfo = DILineInfo();
939   auto ExpectecLineInfo =
940       Symbolizer->symbolizeCode(Obj->getFileName(), Address);
941   if (!ExpectecLineInfo)
942     consumeError(ExpectecLineInfo.takeError());
943   else
944     LineInfo = *ExpectecLineInfo;
945 
946   if ((LineInfo.FileName == "<invalid>") || OldLineInfo.Line == LineInfo.Line ||
947       LineInfo.Line == 0)
948     return;
949 
950   if (PrintLines)
951     OS << Delimiter << LineInfo.FileName << ":" << LineInfo.Line << "\n";
952   if (PrintSource) {
953     if (SourceCache.find(LineInfo.FileName) == SourceCache.end())
954       if (!cacheSource(LineInfo))
955         return;
956     auto FileBuffer = SourceCache.find(LineInfo.FileName);
957     if (FileBuffer != SourceCache.end()) {
958       auto LineBuffer = LineCache.find(LineInfo.FileName);
959       if (LineBuffer != LineCache.end()) {
960         if (LineInfo.Line > LineBuffer->second.size())
961           return;
962         // Vector begins at 0, line numbers are non-zero
963         OS << Delimiter << LineBuffer->second[LineInfo.Line - 1].ltrim()
964            << "\n";
965       }
966     }
967   }
968   OldLineInfo = LineInfo;
969 }
970 
971 static bool isArmElf(const ObjectFile *Obj) {
972   return (Obj->isELF() &&
973           (Obj->getArch() == Triple::aarch64 ||
974            Obj->getArch() == Triple::aarch64_be ||
975            Obj->getArch() == Triple::arm || Obj->getArch() == Triple::armeb ||
976            Obj->getArch() == Triple::thumb ||
977            Obj->getArch() == Triple::thumbeb));
978 }
979 
980 class PrettyPrinter {
981 public:
982   virtual ~PrettyPrinter() = default;
983   virtual void printInst(MCInstPrinter &IP, const MCInst *MI,
984                          ArrayRef<uint8_t> Bytes, uint64_t Address,
985                          raw_ostream &OS, StringRef Annot,
986                          MCSubtargetInfo const &STI, SourcePrinter *SP,
987                          std::vector<RelocationRef> *Rels = nullptr) {
988     if (SP && (PrintSource || PrintLines))
989       SP->printSourceLine(OS, Address);
990     if (!NoLeadingAddr)
991       OS << format("%8" PRIx64 ":", Address);
992     if (!NoShowRawInsn) {
993       OS << "\t";
994       dumpBytes(Bytes, OS);
995     }
996     if (MI)
997       IP.printInst(MI, OS, "", STI);
998     else
999       OS << " <unknown>";
1000   }
1001 };
1002 PrettyPrinter PrettyPrinterInst;
1003 class HexagonPrettyPrinter : public PrettyPrinter {
1004 public:
1005   void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
1006                  raw_ostream &OS) {
1007     uint32_t opcode =
1008       (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
1009     if (!NoLeadingAddr)
1010       OS << format("%8" PRIx64 ":", Address);
1011     if (!NoShowRawInsn) {
1012       OS << "\t";
1013       dumpBytes(Bytes.slice(0, 4), OS);
1014       OS << format("%08" PRIx32, opcode);
1015     }
1016   }
1017   void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
1018                  uint64_t Address, raw_ostream &OS, StringRef Annot,
1019                  MCSubtargetInfo const &STI, SourcePrinter *SP,
1020                  std::vector<RelocationRef> *Rels) override {
1021     if (SP && (PrintSource || PrintLines))
1022       SP->printSourceLine(OS, Address, "");
1023     if (!MI) {
1024       printLead(Bytes, Address, OS);
1025       OS << " <unknown>";
1026       return;
1027     }
1028     std::string Buffer;
1029     {
1030       raw_string_ostream TempStream(Buffer);
1031       IP.printInst(MI, TempStream, "", STI);
1032     }
1033     StringRef Contents(Buffer);
1034     // Split off bundle attributes
1035     auto PacketBundle = Contents.rsplit('\n');
1036     // Split off first instruction from the rest
1037     auto HeadTail = PacketBundle.first.split('\n');
1038     auto Preamble = " { ";
1039     auto Separator = "";
1040     StringRef Fmt = "\t\t\t%08" PRIx64 ":  ";
1041     std::vector<RelocationRef>::const_iterator rel_cur = Rels->begin();
1042     std::vector<RelocationRef>::const_iterator rel_end = Rels->end();
1043 
1044     // Hexagon's packets require relocations to be inline rather than
1045     // clustered at the end of the packet.
1046     auto PrintReloc = [&]() -> void {
1047       while ((rel_cur != rel_end) && (rel_cur->getOffset() <= Address)) {
1048         if (rel_cur->getOffset() == Address) {
1049           SmallString<16> name;
1050           SmallString<32> val;
1051           rel_cur->getTypeName(name);
1052           error(getRelocationValueString(*rel_cur, val));
1053           OS << Separator << format(Fmt.data(), Address) << name << "\t" << val
1054                 << "\n";
1055           return;
1056         }
1057         rel_cur++;
1058       }
1059     };
1060 
1061     while(!HeadTail.first.empty()) {
1062       OS << Separator;
1063       Separator = "\n";
1064       if (SP && (PrintSource || PrintLines))
1065         SP->printSourceLine(OS, Address, "");
1066       printLead(Bytes, Address, OS);
1067       OS << Preamble;
1068       Preamble = "   ";
1069       StringRef Inst;
1070       auto Duplex = HeadTail.first.split('\v');
1071       if(!Duplex.second.empty()){
1072         OS << Duplex.first;
1073         OS << "; ";
1074         Inst = Duplex.second;
1075       }
1076       else
1077         Inst = HeadTail.first;
1078       OS << Inst;
1079       HeadTail = HeadTail.second.split('\n');
1080       if (HeadTail.first.empty())
1081         OS << " } " << PacketBundle.second;
1082       PrintReloc();
1083       Bytes = Bytes.slice(4);
1084       Address += 4;
1085     }
1086   }
1087 };
1088 HexagonPrettyPrinter HexagonPrettyPrinterInst;
1089 
1090 class AMDGCNPrettyPrinter : public PrettyPrinter {
1091 public:
1092   void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
1093                  uint64_t Address, raw_ostream &OS, StringRef Annot,
1094                  MCSubtargetInfo const &STI, SourcePrinter *SP,
1095                  std::vector<RelocationRef> *Rels) override {
1096     if (SP && (PrintSource || PrintLines))
1097       SP->printSourceLine(OS, Address);
1098 
1099     typedef support::ulittle32_t U32;
1100 
1101     if (MI) {
1102       SmallString<40> InstStr;
1103       raw_svector_ostream IS(InstStr);
1104 
1105       IP.printInst(MI, IS, "", STI);
1106 
1107       OS << left_justify(IS.str(), 60);
1108     } else {
1109       // an unrecognized encoding - this is probably data so represent it
1110       // using the .long directive, or .byte directive if fewer than 4 bytes
1111       // remaining
1112       if (Bytes.size() >= 4) {
1113         OS << format("\t.long 0x%08" PRIx32 " ",
1114                      static_cast<uint32_t>(*reinterpret_cast<const U32*>(Bytes.data())));
1115         OS.indent(42);
1116       } else {
1117           OS << format("\t.byte 0x%02" PRIx8, Bytes[0]);
1118           for (unsigned int i = 1; i < Bytes.size(); i++)
1119             OS << format(", 0x%02" PRIx8, Bytes[i]);
1120           OS.indent(55 - (6 * Bytes.size()));
1121       }
1122     }
1123 
1124     OS << format("// %012" PRIX64 ": ", Address);
1125     if (Bytes.size() >=4) {
1126       for (auto D : makeArrayRef(reinterpret_cast<const U32*>(Bytes.data()),
1127                                  Bytes.size() / sizeof(U32)))
1128         // D should be explicitly casted to uint32_t here as it is passed
1129         // by format to snprintf as vararg.
1130         OS << format("%08" PRIX32 " ", static_cast<uint32_t>(D));
1131     } else {
1132       for (unsigned int i = 0; i < Bytes.size(); i++)
1133         OS << format("%02" PRIX8 " ", Bytes[i]);
1134     }
1135 
1136     if (!Annot.empty())
1137       OS << "// " << Annot;
1138   }
1139 };
1140 AMDGCNPrettyPrinter AMDGCNPrettyPrinterInst;
1141 
1142 class BPFPrettyPrinter : public PrettyPrinter {
1143 public:
1144   void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
1145                  uint64_t Address, raw_ostream &OS, StringRef Annot,
1146                  MCSubtargetInfo const &STI, SourcePrinter *SP,
1147                  std::vector<RelocationRef> *Rels) override {
1148     if (SP && (PrintSource || PrintLines))
1149       SP->printSourceLine(OS, Address);
1150     if (!NoLeadingAddr)
1151       OS << format("%8" PRId64 ":", Address / 8);
1152     if (!NoShowRawInsn) {
1153       OS << "\t";
1154       dumpBytes(Bytes, OS);
1155     }
1156     if (MI)
1157       IP.printInst(MI, OS, "", STI);
1158     else
1159       OS << " <unknown>";
1160   }
1161 };
1162 BPFPrettyPrinter BPFPrettyPrinterInst;
1163 
1164 PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
1165   switch(Triple.getArch()) {
1166   default:
1167     return PrettyPrinterInst;
1168   case Triple::hexagon:
1169     return HexagonPrettyPrinterInst;
1170   case Triple::amdgcn:
1171     return AMDGCNPrettyPrinterInst;
1172   case Triple::bpfel:
1173   case Triple::bpfeb:
1174     return BPFPrettyPrinterInst;
1175   }
1176 }
1177 }
1178 
1179 static uint8_t getElfSymbolType(const ObjectFile *Obj, const SymbolRef &Sym) {
1180   assert(Obj->isELF());
1181   if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
1182     return Elf32LEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1183   if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
1184     return Elf64LEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1185   if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
1186     return Elf32BEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1187   if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj))
1188     return Elf64BEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1189   llvm_unreachable("Unsupported binary format");
1190 }
1191 
1192 template <class ELFT> static void
1193 addDynamicElfSymbols(const ELFObjectFile<ELFT> *Obj,
1194                      std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
1195   for (auto Symbol : Obj->getDynamicSymbolIterators()) {
1196     uint8_t SymbolType = Symbol.getELFType();
1197     if (SymbolType != ELF::STT_FUNC || Symbol.getSize() == 0)
1198       continue;
1199 
1200     Expected<uint64_t> AddressOrErr = Symbol.getAddress();
1201     if (!AddressOrErr)
1202       report_error(Obj->getFileName(), AddressOrErr.takeError());
1203     uint64_t Address = *AddressOrErr;
1204 
1205     Expected<StringRef> Name = Symbol.getName();
1206     if (!Name)
1207       report_error(Obj->getFileName(), Name.takeError());
1208     if (Name->empty())
1209       continue;
1210 
1211     Expected<section_iterator> SectionOrErr = Symbol.getSection();
1212     if (!SectionOrErr)
1213       report_error(Obj->getFileName(), SectionOrErr.takeError());
1214     section_iterator SecI = *SectionOrErr;
1215     if (SecI == Obj->section_end())
1216       continue;
1217 
1218     AllSymbols[*SecI].emplace_back(Address, *Name, SymbolType);
1219   }
1220 }
1221 
1222 static void
1223 addDynamicElfSymbols(const ObjectFile *Obj,
1224                      std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
1225   assert(Obj->isELF());
1226   if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
1227     addDynamicElfSymbols(Elf32LEObj, AllSymbols);
1228   else if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
1229     addDynamicElfSymbols(Elf64LEObj, AllSymbols);
1230   else if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
1231     addDynamicElfSymbols(Elf32BEObj, AllSymbols);
1232   else if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj))
1233     addDynamicElfSymbols(Elf64BEObj, AllSymbols);
1234   else
1235     llvm_unreachable("Unsupported binary format");
1236 }
1237 
1238 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
1239   if (StartAddress > StopAddress)
1240     error("Start address should be less than stop address");
1241 
1242   const Target *TheTarget = getTarget(Obj);
1243 
1244   // Package up features to be passed to target/subtarget
1245   SubtargetFeatures Features = Obj->getFeatures();
1246   if (MAttrs.size()) {
1247     for (unsigned i = 0; i != MAttrs.size(); ++i)
1248       Features.AddFeature(MAttrs[i]);
1249   }
1250 
1251   std::unique_ptr<const MCRegisterInfo> MRI(
1252       TheTarget->createMCRegInfo(TripleName));
1253   if (!MRI)
1254     report_error(Obj->getFileName(), "no register info for target " +
1255                  TripleName);
1256 
1257   // Set up disassembler.
1258   std::unique_ptr<const MCAsmInfo> AsmInfo(
1259       TheTarget->createMCAsmInfo(*MRI, TripleName));
1260   if (!AsmInfo)
1261     report_error(Obj->getFileName(), "no assembly info for target " +
1262                  TripleName);
1263   std::unique_ptr<const MCSubtargetInfo> STI(
1264       TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString()));
1265   if (!STI)
1266     report_error(Obj->getFileName(), "no subtarget info for target " +
1267                  TripleName);
1268   std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
1269   if (!MII)
1270     report_error(Obj->getFileName(), "no instruction info for target " +
1271                  TripleName);
1272   MCObjectFileInfo MOFI;
1273   MCContext Ctx(AsmInfo.get(), MRI.get(), &MOFI);
1274   // FIXME: for now initialize MCObjectFileInfo with default values
1275   MOFI.InitMCObjectFileInfo(Triple(TripleName), false, Ctx);
1276 
1277   std::unique_ptr<MCDisassembler> DisAsm(
1278     TheTarget->createMCDisassembler(*STI, Ctx));
1279   if (!DisAsm)
1280     report_error(Obj->getFileName(), "no disassembler for target " +
1281                  TripleName);
1282 
1283   std::unique_ptr<const MCInstrAnalysis> MIA(
1284       TheTarget->createMCInstrAnalysis(MII.get()));
1285 
1286   int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
1287   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
1288       Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
1289   if (!IP)
1290     report_error(Obj->getFileName(), "no instruction printer for target " +
1291                  TripleName);
1292   IP->setPrintImmHex(PrintImmHex);
1293   PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
1294 
1295   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ":  " :
1296                                                  "\t\t\t%08" PRIx64 ":  ";
1297 
1298   SourcePrinter SP(Obj, TheTarget->getName());
1299 
1300   // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
1301   // in RelocSecs contain the relocations for section S.
1302   std::error_code EC;
1303   std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
1304   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1305     section_iterator Sec2 = Section.getRelocatedSection();
1306     if (Sec2 != Obj->section_end())
1307       SectionRelocMap[*Sec2].push_back(Section);
1308   }
1309 
1310   // Create a mapping from virtual address to symbol name.  This is used to
1311   // pretty print the symbols while disassembling.
1312   std::map<SectionRef, SectionSymbolsTy> AllSymbols;
1313   SectionSymbolsTy AbsoluteSymbols;
1314   for (const SymbolRef &Symbol : Obj->symbols()) {
1315     Expected<uint64_t> AddressOrErr = Symbol.getAddress();
1316     if (!AddressOrErr)
1317       report_error(Obj->getFileName(), AddressOrErr.takeError());
1318     uint64_t Address = *AddressOrErr;
1319 
1320     Expected<StringRef> Name = Symbol.getName();
1321     if (!Name)
1322       report_error(Obj->getFileName(), Name.takeError());
1323     if (Name->empty())
1324       continue;
1325 
1326     Expected<section_iterator> SectionOrErr = Symbol.getSection();
1327     if (!SectionOrErr)
1328       report_error(Obj->getFileName(), SectionOrErr.takeError());
1329 
1330     uint8_t SymbolType = ELF::STT_NOTYPE;
1331     if (Obj->isELF())
1332       SymbolType = getElfSymbolType(Obj, Symbol);
1333 
1334     section_iterator SecI = *SectionOrErr;
1335     if (SecI != Obj->section_end())
1336       AllSymbols[*SecI].emplace_back(Address, *Name, SymbolType);
1337     else
1338       AbsoluteSymbols.emplace_back(Address, *Name, SymbolType);
1339 
1340 
1341   }
1342   if (AllSymbols.empty() && Obj->isELF())
1343     addDynamicElfSymbols(Obj, AllSymbols);
1344 
1345   // Create a mapping from virtual address to section.
1346   std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
1347   for (SectionRef Sec : Obj->sections())
1348     SectionAddresses.emplace_back(Sec.getAddress(), Sec);
1349   array_pod_sort(SectionAddresses.begin(), SectionAddresses.end());
1350 
1351   // Linked executables (.exe and .dll files) typically don't include a real
1352   // symbol table but they might contain an export table.
1353   if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) {
1354     for (const auto &ExportEntry : COFFObj->export_directories()) {
1355       StringRef Name;
1356       error(ExportEntry.getSymbolName(Name));
1357       if (Name.empty())
1358         continue;
1359       uint32_t RVA;
1360       error(ExportEntry.getExportRVA(RVA));
1361 
1362       uint64_t VA = COFFObj->getImageBase() + RVA;
1363       auto Sec = std::upper_bound(
1364           SectionAddresses.begin(), SectionAddresses.end(), VA,
1365           [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) {
1366             return LHS < RHS.first;
1367           });
1368       if (Sec != SectionAddresses.begin())
1369         --Sec;
1370       else
1371         Sec = SectionAddresses.end();
1372 
1373       if (Sec != SectionAddresses.end())
1374         AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE);
1375       else
1376         AbsoluteSymbols.emplace_back(VA, Name, ELF::STT_NOTYPE);
1377     }
1378   }
1379 
1380   // Sort all the symbols, this allows us to use a simple binary search to find
1381   // a symbol near an address.
1382   for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
1383     array_pod_sort(SecSyms.second.begin(), SecSyms.second.end());
1384   array_pod_sort(AbsoluteSymbols.begin(), AbsoluteSymbols.end());
1385 
1386   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1387     if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
1388       continue;
1389 
1390     uint64_t SectionAddr = Section.getAddress();
1391     uint64_t SectSize = Section.getSize();
1392     if (!SectSize)
1393       continue;
1394 
1395     // Get the list of all the symbols in this section.
1396     SectionSymbolsTy &Symbols = AllSymbols[Section];
1397     std::vector<uint64_t> DataMappingSymsAddr;
1398     std::vector<uint64_t> TextMappingSymsAddr;
1399     if (isArmElf(Obj)) {
1400       for (const auto &Symb : Symbols) {
1401         uint64_t Address = std::get<0>(Symb);
1402         StringRef Name = std::get<1>(Symb);
1403         if (Name.startswith("$d"))
1404           DataMappingSymsAddr.push_back(Address - SectionAddr);
1405         if (Name.startswith("$x"))
1406           TextMappingSymsAddr.push_back(Address - SectionAddr);
1407         if (Name.startswith("$a"))
1408           TextMappingSymsAddr.push_back(Address - SectionAddr);
1409         if (Name.startswith("$t"))
1410           TextMappingSymsAddr.push_back(Address - SectionAddr);
1411       }
1412     }
1413 
1414     llvm::sort(DataMappingSymsAddr.begin(), DataMappingSymsAddr.end());
1415     llvm::sort(TextMappingSymsAddr.begin(), TextMappingSymsAddr.end());
1416 
1417     if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) {
1418       // AMDGPU disassembler uses symbolizer for printing labels
1419       std::unique_ptr<MCRelocationInfo> RelInfo(
1420         TheTarget->createMCRelocationInfo(TripleName, Ctx));
1421       if (RelInfo) {
1422         std::unique_ptr<MCSymbolizer> Symbolizer(
1423           TheTarget->createMCSymbolizer(
1424             TripleName, nullptr, nullptr, &Symbols, &Ctx, std::move(RelInfo)));
1425         DisAsm->setSymbolizer(std::move(Symbolizer));
1426       }
1427     }
1428 
1429     // Make a list of all the relocations for this section.
1430     std::vector<RelocationRef> Rels;
1431     if (InlineRelocs) {
1432       for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
1433         for (const RelocationRef &Reloc : RelocSec.relocations()) {
1434           Rels.push_back(Reloc);
1435         }
1436       }
1437     }
1438 
1439     // Sort relocations by address.
1440     llvm::sort(Rels.begin(), Rels.end(), RelocAddressLess);
1441 
1442     StringRef SegmentName = "";
1443     if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
1444       DataRefImpl DR = Section.getRawDataRefImpl();
1445       SegmentName = MachO->getSectionFinalSegmentName(DR);
1446     }
1447     StringRef SectionName;
1448     error(Section.getName(SectionName));
1449 
1450     // If the section has no symbol at the start, just insert a dummy one.
1451     if (Symbols.empty() || std::get<0>(Symbols[0]) != 0) {
1452       Symbols.insert(
1453           Symbols.begin(),
1454           std::make_tuple(SectionAddr, SectionName,
1455                           Section.isText() ? ELF::STT_FUNC : ELF::STT_OBJECT));
1456     }
1457 
1458     SmallString<40> Comments;
1459     raw_svector_ostream CommentStream(Comments);
1460 
1461     StringRef BytesStr;
1462     error(Section.getContents(BytesStr));
1463     ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
1464                             BytesStr.size());
1465 
1466     uint64_t Size;
1467     uint64_t Index;
1468     bool PrintedSection = false;
1469 
1470     std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
1471     std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
1472     // Disassemble symbol by symbol.
1473     for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
1474       uint64_t Start = std::get<0>(Symbols[si]) - SectionAddr;
1475       // The end is either the section end or the beginning of the next
1476       // symbol.
1477       uint64_t End =
1478           (si == se - 1) ? SectSize : std::get<0>(Symbols[si + 1]) - SectionAddr;
1479       // Don't try to disassemble beyond the end of section contents.
1480       if (End > SectSize)
1481         End = SectSize;
1482       // If this symbol has the same address as the next symbol, then skip it.
1483       if (Start >= End)
1484         continue;
1485 
1486       // Check if we need to skip symbol
1487       // Skip if the symbol's data is not between StartAddress and StopAddress
1488       if (End + SectionAddr < StartAddress ||
1489           Start + SectionAddr > StopAddress) {
1490         continue;
1491       }
1492 
1493       /// Skip if user requested specific symbols and this is not in the list
1494       if (!DisasmFuncsSet.empty() &&
1495           !DisasmFuncsSet.count(std::get<1>(Symbols[si])))
1496         continue;
1497 
1498       if (!PrintedSection) {
1499         PrintedSection = true;
1500         outs() << "Disassembly of section ";
1501         if (!SegmentName.empty())
1502           outs() << SegmentName << ",";
1503         outs() << SectionName << ':';
1504       }
1505 
1506       // Stop disassembly at the stop address specified
1507       if (End + SectionAddr > StopAddress)
1508         End = StopAddress - SectionAddr;
1509 
1510       if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) {
1511         if (std::get<2>(Symbols[si]) == ELF::STT_AMDGPU_HSA_KERNEL) {
1512           // skip amd_kernel_code_t at the begining of kernel symbol (256 bytes)
1513           Start += 256;
1514         }
1515         if (si == se - 1 ||
1516             std::get<2>(Symbols[si + 1]) == ELF::STT_AMDGPU_HSA_KERNEL) {
1517           // cut trailing zeroes at the end of kernel
1518           // cut up to 256 bytes
1519           const uint64_t EndAlign = 256;
1520           const auto Limit = End - (std::min)(EndAlign, End - Start);
1521           while (End > Limit &&
1522             *reinterpret_cast<const support::ulittle32_t*>(&Bytes[End - 4]) == 0)
1523             End -= 4;
1524         }
1525       }
1526 
1527       auto PrintSymbol = [](StringRef Name) {
1528         outs() << '\n' << Name << ":\n";
1529       };
1530       StringRef SymbolName = std::get<1>(Symbols[si]);
1531       if (Demangle) {
1532         char *DemangledSymbol = nullptr;
1533         size_t Size = 0;
1534         int Status = -1;
1535         if (SymbolName.startswith("_Z"))
1536           DemangledSymbol = itaniumDemangle(SymbolName.data(), DemangledSymbol,
1537                                             &Size, &Status);
1538         else if (SymbolName.startswith("?"))
1539           DemangledSymbol = microsoftDemangle(SymbolName.data(),
1540                                               DemangledSymbol, &Size, &Status);
1541 
1542         if (Status == 0 && DemangledSymbol)
1543           PrintSymbol(StringRef(DemangledSymbol));
1544         else
1545           PrintSymbol(SymbolName);
1546 
1547         if (DemangledSymbol)
1548           free(DemangledSymbol);
1549       } else
1550         PrintSymbol(SymbolName);
1551 
1552       // Don't print raw contents of a virtual section. A virtual section
1553       // doesn't have any contents in the file.
1554       if (Section.isVirtual()) {
1555         outs() << "...\n";
1556         continue;
1557       }
1558 
1559 #ifndef NDEBUG
1560       raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
1561 #else
1562       raw_ostream &DebugOut = nulls();
1563 #endif
1564 
1565       for (Index = Start; Index < End; Index += Size) {
1566         MCInst Inst;
1567 
1568         if (Index + SectionAddr < StartAddress ||
1569             Index + SectionAddr > StopAddress) {
1570           // skip byte by byte till StartAddress is reached
1571           Size = 1;
1572           continue;
1573         }
1574         // AArch64 ELF binaries can interleave data and text in the
1575         // same section. We rely on the markers introduced to
1576         // understand what we need to dump. If the data marker is within a
1577         // function, it is denoted as a word/short etc
1578         if (isArmElf(Obj) && std::get<2>(Symbols[si]) != ELF::STT_OBJECT &&
1579             !DisassembleAll) {
1580           uint64_t Stride = 0;
1581 
1582           auto DAI = std::lower_bound(DataMappingSymsAddr.begin(),
1583                                       DataMappingSymsAddr.end(), Index);
1584           if (DAI != DataMappingSymsAddr.end() && *DAI == Index) {
1585             // Switch to data.
1586             while (Index < End) {
1587               outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1588               outs() << "\t";
1589               if (Index + 4 <= End) {
1590                 Stride = 4;
1591                 dumpBytes(Bytes.slice(Index, 4), outs());
1592                 outs() << "\t.word\t";
1593                 uint32_t Data = 0;
1594                 if (Obj->isLittleEndian()) {
1595                   const auto Word =
1596                       reinterpret_cast<const support::ulittle32_t *>(
1597                           Bytes.data() + Index);
1598                   Data = *Word;
1599                 } else {
1600                   const auto Word = reinterpret_cast<const support::ubig32_t *>(
1601                       Bytes.data() + Index);
1602                   Data = *Word;
1603                 }
1604                 outs() << "0x" << format("%08" PRIx32, Data);
1605               } else if (Index + 2 <= End) {
1606                 Stride = 2;
1607                 dumpBytes(Bytes.slice(Index, 2), outs());
1608                 outs() << "\t\t.short\t";
1609                 uint16_t Data = 0;
1610                 if (Obj->isLittleEndian()) {
1611                   const auto Short =
1612                       reinterpret_cast<const support::ulittle16_t *>(
1613                           Bytes.data() + Index);
1614                   Data = *Short;
1615                 } else {
1616                   const auto Short =
1617                       reinterpret_cast<const support::ubig16_t *>(Bytes.data() +
1618                                                                   Index);
1619                   Data = *Short;
1620                 }
1621                 outs() << "0x" << format("%04" PRIx16, Data);
1622               } else {
1623                 Stride = 1;
1624                 dumpBytes(Bytes.slice(Index, 1), outs());
1625                 outs() << "\t\t.byte\t";
1626                 outs() << "0x" << format("%02" PRIx8, Bytes.slice(Index, 1)[0]);
1627               }
1628               Index += Stride;
1629               outs() << "\n";
1630               auto TAI = std::lower_bound(TextMappingSymsAddr.begin(),
1631                                           TextMappingSymsAddr.end(), Index);
1632               if (TAI != TextMappingSymsAddr.end() && *TAI == Index)
1633                 break;
1634             }
1635           }
1636         }
1637 
1638         // If there is a data symbol inside an ELF text section and we are only
1639         // disassembling text (applicable all architectures),
1640         // we are in a situation where we must print the data and not
1641         // disassemble it.
1642         if (Obj->isELF() && std::get<2>(Symbols[si]) == ELF::STT_OBJECT &&
1643             !DisassembleAll && Section.isText()) {
1644           // print out data up to 8 bytes at a time in hex and ascii
1645           uint8_t AsciiData[9] = {'\0'};
1646           uint8_t Byte;
1647           int NumBytes = 0;
1648 
1649           for (Index = Start; Index < End; Index += 1) {
1650             if (((SectionAddr + Index) < StartAddress) ||
1651                 ((SectionAddr + Index) > StopAddress))
1652               continue;
1653             if (NumBytes == 0) {
1654               outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1655               outs() << "\t";
1656             }
1657             Byte = Bytes.slice(Index)[0];
1658             outs() << format(" %02x", Byte);
1659             AsciiData[NumBytes] = isPrint(Byte) ? Byte : '.';
1660 
1661             uint8_t IndentOffset = 0;
1662             NumBytes++;
1663             if (Index == End - 1 || NumBytes > 8) {
1664               // Indent the space for less than 8 bytes data.
1665               // 2 spaces for byte and one for space between bytes
1666               IndentOffset = 3 * (8 - NumBytes);
1667               for (int Excess = 8 - NumBytes; Excess < 8; Excess++)
1668                 AsciiData[Excess] = '\0';
1669               NumBytes = 8;
1670             }
1671             if (NumBytes == 8) {
1672               AsciiData[8] = '\0';
1673               outs() << std::string(IndentOffset, ' ') << "         ";
1674               outs() << reinterpret_cast<char *>(AsciiData);
1675               outs() << '\n';
1676               NumBytes = 0;
1677             }
1678           }
1679         }
1680         if (Index >= End)
1681           break;
1682 
1683         // Disassemble a real instruction or a data when disassemble all is
1684         // provided
1685         bool Disassembled = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
1686                                                    SectionAddr + Index, DebugOut,
1687                                                    CommentStream);
1688         if (Size == 0)
1689           Size = 1;
1690 
1691         PIP.printInst(*IP, Disassembled ? &Inst : nullptr,
1692                       Bytes.slice(Index, Size), SectionAddr + Index, outs(), "",
1693                       *STI, &SP, &Rels);
1694         outs() << CommentStream.str();
1695         Comments.clear();
1696 
1697         // Try to resolve the target of a call, tail call, etc. to a specific
1698         // symbol.
1699         if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
1700                     MIA->isConditionalBranch(Inst))) {
1701           uint64_t Target;
1702           if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
1703             // In a relocatable object, the target's section must reside in
1704             // the same section as the call instruction or it is accessed
1705             // through a relocation.
1706             //
1707             // In a non-relocatable object, the target may be in any section.
1708             //
1709             // N.B. We don't walk the relocations in the relocatable case yet.
1710             auto *TargetSectionSymbols = &Symbols;
1711             if (!Obj->isRelocatableObject()) {
1712               auto SectionAddress = std::upper_bound(
1713                   SectionAddresses.begin(), SectionAddresses.end(), Target,
1714                   [](uint64_t LHS,
1715                       const std::pair<uint64_t, SectionRef> &RHS) {
1716                     return LHS < RHS.first;
1717                   });
1718               if (SectionAddress != SectionAddresses.begin()) {
1719                 --SectionAddress;
1720                 TargetSectionSymbols = &AllSymbols[SectionAddress->second];
1721               } else {
1722                 TargetSectionSymbols = &AbsoluteSymbols;
1723               }
1724             }
1725 
1726             // Find the first symbol in the section whose offset is less than
1727             // or equal to the target. If there isn't a section that contains
1728             // the target, find the nearest preceding absolute symbol.
1729             auto TargetSym = std::upper_bound(
1730                 TargetSectionSymbols->begin(), TargetSectionSymbols->end(),
1731                 Target, [](uint64_t LHS,
1732                            const std::tuple<uint64_t, StringRef, uint8_t> &RHS) {
1733                   return LHS < std::get<0>(RHS);
1734                 });
1735             if (TargetSym == TargetSectionSymbols->begin()) {
1736               TargetSectionSymbols = &AbsoluteSymbols;
1737               TargetSym = std::upper_bound(
1738                   AbsoluteSymbols.begin(), AbsoluteSymbols.end(),
1739                   Target, [](uint64_t LHS,
1740                              const std::tuple<uint64_t, StringRef, uint8_t> &RHS) {
1741                             return LHS < std::get<0>(RHS);
1742                           });
1743             }
1744             if (TargetSym != TargetSectionSymbols->begin()) {
1745               --TargetSym;
1746               uint64_t TargetAddress = std::get<0>(*TargetSym);
1747               StringRef TargetName = std::get<1>(*TargetSym);
1748               outs() << " <" << TargetName;
1749               uint64_t Disp = Target - TargetAddress;
1750               if (Disp)
1751                 outs() << "+0x" << Twine::utohexstr(Disp);
1752               outs() << '>';
1753             }
1754           }
1755         }
1756         outs() << "\n";
1757 
1758         // Hexagon does this in pretty printer
1759         if (Obj->getArch() != Triple::hexagon)
1760           // Print relocation for instruction.
1761           while (rel_cur != rel_end) {
1762             bool hidden = getHidden(*rel_cur);
1763             uint64_t addr = rel_cur->getOffset();
1764             SmallString<16> name;
1765             SmallString<32> val;
1766 
1767             // If this relocation is hidden, skip it.
1768             if (hidden || ((SectionAddr + addr) < StartAddress)) {
1769               ++rel_cur;
1770               continue;
1771             }
1772 
1773             // Stop when rel_cur's address is past the current instruction.
1774             if (addr >= Index + Size) break;
1775             rel_cur->getTypeName(name);
1776             error(getRelocationValueString(*rel_cur, val));
1777             outs() << format(Fmt.data(), SectionAddr + addr) << name
1778                    << "\t" << val << "\n";
1779             ++rel_cur;
1780           }
1781       }
1782     }
1783   }
1784 }
1785 
1786 void llvm::PrintRelocations(const ObjectFile *Obj) {
1787   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
1788                                                  "%08" PRIx64;
1789   // Regular objdump doesn't print relocations in non-relocatable object
1790   // files.
1791   if (!Obj->isRelocatableObject())
1792     return;
1793 
1794   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1795     if (Section.relocation_begin() == Section.relocation_end())
1796       continue;
1797     StringRef secname;
1798     error(Section.getName(secname));
1799     outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
1800     for (const RelocationRef &Reloc : Section.relocations()) {
1801       bool hidden = getHidden(Reloc);
1802       uint64_t address = Reloc.getOffset();
1803       SmallString<32> relocname;
1804       SmallString<32> valuestr;
1805       if (address < StartAddress || address > StopAddress || hidden)
1806         continue;
1807       Reloc.getTypeName(relocname);
1808       error(getRelocationValueString(Reloc, valuestr));
1809       outs() << format(Fmt.data(), address) << " " << relocname << " "
1810              << valuestr << "\n";
1811     }
1812     outs() << "\n";
1813   }
1814 }
1815 
1816 void llvm::PrintDynamicRelocations(const ObjectFile *Obj) {
1817 
1818   // For the moment, this option is for ELF only
1819   if (!Obj->isELF())
1820     return;
1821 
1822   const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj);
1823 
1824   if (!Elf || Elf->getEType() != ELF::ET_DYN) {
1825     error("not a dynamic object");
1826     return;
1827   }
1828 
1829   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
1830 
1831   std::vector<SectionRef> DynRelSec = Obj->dynamic_relocation_sections();
1832   if (DynRelSec.empty())
1833     return;
1834 
1835   outs() << "DYNAMIC RELOCATION RECORDS\n";
1836   for (const SectionRef &Section : DynRelSec) {
1837     if (Section.relocation_begin() == Section.relocation_end())
1838       continue;
1839     for (const RelocationRef &Reloc : Section.relocations()) {
1840       uint64_t address = Reloc.getOffset();
1841       SmallString<32> relocname;
1842       SmallString<32> valuestr;
1843       Reloc.getTypeName(relocname);
1844       error(getRelocationValueString(Reloc, valuestr));
1845       outs() << format(Fmt.data(), address) << " " << relocname << " "
1846              << valuestr << "\n";
1847     }
1848   }
1849 }
1850 
1851 void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
1852   outs() << "Sections:\n"
1853             "Idx Name          Size      Address          Type\n";
1854   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1855     StringRef Name;
1856     error(Section.getName(Name));
1857     uint64_t Address = Section.getAddress();
1858     uint64_t Size = Section.getSize();
1859     bool Text = Section.isText();
1860     bool Data = Section.isData();
1861     bool BSS = Section.isBSS();
1862     std::string Type = (std::string(Text ? "TEXT " : "") +
1863                         (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
1864     outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n",
1865                      (unsigned)Section.getIndex(), Name.str().c_str(), Size,
1866                      Address, Type.c_str());
1867   }
1868 }
1869 
1870 void llvm::PrintSectionContents(const ObjectFile *Obj) {
1871   std::error_code EC;
1872   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1873     StringRef Name;
1874     StringRef Contents;
1875     error(Section.getName(Name));
1876     uint64_t BaseAddr = Section.getAddress();
1877     uint64_t Size = Section.getSize();
1878     if (!Size)
1879       continue;
1880 
1881     outs() << "Contents of section " << Name << ":\n";
1882     if (Section.isBSS()) {
1883       outs() << format("<skipping contents of bss section at [%04" PRIx64
1884                        ", %04" PRIx64 ")>\n",
1885                        BaseAddr, BaseAddr + Size);
1886       continue;
1887     }
1888 
1889     error(Section.getContents(Contents));
1890 
1891     // Dump out the content as hex and printable ascii characters.
1892     for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
1893       outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
1894       // Dump line of hex.
1895       for (std::size_t i = 0; i < 16; ++i) {
1896         if (i != 0 && i % 4 == 0)
1897           outs() << ' ';
1898         if (addr + i < end)
1899           outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
1900                  << hexdigit(Contents[addr + i] & 0xF, true);
1901         else
1902           outs() << "  ";
1903       }
1904       // Print ascii.
1905       outs() << "  ";
1906       for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
1907         if (isPrint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
1908           outs() << Contents[addr + i];
1909         else
1910           outs() << ".";
1911       }
1912       outs() << "\n";
1913     }
1914   }
1915 }
1916 
1917 void llvm::PrintSymbolTable(const ObjectFile *o, StringRef ArchiveName,
1918                             StringRef ArchitectureName) {
1919   outs() << "SYMBOL TABLE:\n";
1920 
1921   if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
1922     printCOFFSymbolTable(coff);
1923     return;
1924   }
1925   for (const SymbolRef &Symbol : o->symbols()) {
1926     Expected<uint64_t> AddressOrError = Symbol.getAddress();
1927     if (!AddressOrError)
1928       report_error(ArchiveName, o->getFileName(), AddressOrError.takeError(),
1929                    ArchitectureName);
1930     uint64_t Address = *AddressOrError;
1931     if ((Address < StartAddress) || (Address > StopAddress))
1932       continue;
1933     Expected<SymbolRef::Type> TypeOrError = Symbol.getType();
1934     if (!TypeOrError)
1935       report_error(ArchiveName, o->getFileName(), TypeOrError.takeError(),
1936                    ArchitectureName);
1937     SymbolRef::Type Type = *TypeOrError;
1938     uint32_t Flags = Symbol.getFlags();
1939     Expected<section_iterator> SectionOrErr = Symbol.getSection();
1940     if (!SectionOrErr)
1941       report_error(ArchiveName, o->getFileName(), SectionOrErr.takeError(),
1942                    ArchitectureName);
1943     section_iterator Section = *SectionOrErr;
1944     StringRef Name;
1945     if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
1946       Section->getName(Name);
1947     } else {
1948       Expected<StringRef> NameOrErr = Symbol.getName();
1949       if (!NameOrErr)
1950         report_error(ArchiveName, o->getFileName(), NameOrErr.takeError(),
1951                      ArchitectureName);
1952       Name = *NameOrErr;
1953     }
1954 
1955     bool Global = Flags & SymbolRef::SF_Global;
1956     bool Weak = Flags & SymbolRef::SF_Weak;
1957     bool Absolute = Flags & SymbolRef::SF_Absolute;
1958     bool Common = Flags & SymbolRef::SF_Common;
1959     bool Hidden = Flags & SymbolRef::SF_Hidden;
1960 
1961     char GlobLoc = ' ';
1962     if (Type != SymbolRef::ST_Unknown)
1963       GlobLoc = Global ? 'g' : 'l';
1964     char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
1965                  ? 'd' : ' ';
1966     char FileFunc = ' ';
1967     if (Type == SymbolRef::ST_File)
1968       FileFunc = 'f';
1969     else if (Type == SymbolRef::ST_Function)
1970       FileFunc = 'F';
1971 
1972     const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
1973                                                    "%08" PRIx64;
1974 
1975     outs() << format(Fmt, Address) << " "
1976            << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
1977            << (Weak ? 'w' : ' ') // Weak?
1978            << ' ' // Constructor. Not supported yet.
1979            << ' ' // Warning. Not supported yet.
1980            << ' ' // Indirect reference to another symbol.
1981            << Debug // Debugging (d) or dynamic (D) symbol.
1982            << FileFunc // Name of function (F), file (f) or object (O).
1983            << ' ';
1984     if (Absolute) {
1985       outs() << "*ABS*";
1986     } else if (Common) {
1987       outs() << "*COM*";
1988     } else if (Section == o->section_end()) {
1989       outs() << "*UND*";
1990     } else {
1991       if (const MachOObjectFile *MachO =
1992           dyn_cast<const MachOObjectFile>(o)) {
1993         DataRefImpl DR = Section->getRawDataRefImpl();
1994         StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
1995         outs() << SegmentName << ",";
1996       }
1997       StringRef SectionName;
1998       error(Section->getName(SectionName));
1999       outs() << SectionName;
2000     }
2001 
2002     outs() << '\t';
2003     if (Common || isa<ELFObjectFileBase>(o)) {
2004       uint64_t Val =
2005           Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
2006       outs() << format("\t %08" PRIx64 " ", Val);
2007     }
2008 
2009     if (Hidden) {
2010       outs() << ".hidden ";
2011     }
2012     outs() << Name
2013            << '\n';
2014   }
2015 }
2016 
2017 static void PrintUnwindInfo(const ObjectFile *o) {
2018   outs() << "Unwind info:\n\n";
2019 
2020   if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
2021     printCOFFUnwindInfo(coff);
2022   } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
2023     printMachOUnwindInfo(MachO);
2024   else {
2025     // TODO: Extract DWARF dump tool to objdump.
2026     errs() << "This operation is only currently supported "
2027               "for COFF and MachO object files.\n";
2028     return;
2029   }
2030 }
2031 
2032 void llvm::printExportsTrie(const ObjectFile *o) {
2033   outs() << "Exports trie:\n";
2034   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
2035     printMachOExportsTrie(MachO);
2036   else {
2037     errs() << "This operation is only currently supported "
2038               "for Mach-O executable files.\n";
2039     return;
2040   }
2041 }
2042 
2043 void llvm::printRebaseTable(ObjectFile *o) {
2044   outs() << "Rebase table:\n";
2045   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
2046     printMachORebaseTable(MachO);
2047   else {
2048     errs() << "This operation is only currently supported "
2049               "for Mach-O executable files.\n";
2050     return;
2051   }
2052 }
2053 
2054 void llvm::printBindTable(ObjectFile *o) {
2055   outs() << "Bind table:\n";
2056   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
2057     printMachOBindTable(MachO);
2058   else {
2059     errs() << "This operation is only currently supported "
2060               "for Mach-O executable files.\n";
2061     return;
2062   }
2063 }
2064 
2065 void llvm::printLazyBindTable(ObjectFile *o) {
2066   outs() << "Lazy bind table:\n";
2067   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
2068     printMachOLazyBindTable(MachO);
2069   else {
2070     errs() << "This operation is only currently supported "
2071               "for Mach-O executable files.\n";
2072     return;
2073   }
2074 }
2075 
2076 void llvm::printWeakBindTable(ObjectFile *o) {
2077   outs() << "Weak bind table:\n";
2078   if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
2079     printMachOWeakBindTable(MachO);
2080   else {
2081     errs() << "This operation is only currently supported "
2082               "for Mach-O executable files.\n";
2083     return;
2084   }
2085 }
2086 
2087 /// Dump the raw contents of the __clangast section so the output can be piped
2088 /// into llvm-bcanalyzer.
2089 void llvm::printRawClangAST(const ObjectFile *Obj) {
2090   if (outs().is_displayed()) {
2091     errs() << "The -raw-clang-ast option will dump the raw binary contents of "
2092               "the clang ast section.\n"
2093               "Please redirect the output to a file or another program such as "
2094               "llvm-bcanalyzer.\n";
2095     return;
2096   }
2097 
2098   StringRef ClangASTSectionName("__clangast");
2099   if (isa<COFFObjectFile>(Obj)) {
2100     ClangASTSectionName = "clangast";
2101   }
2102 
2103   Optional<object::SectionRef> ClangASTSection;
2104   for (auto Sec : ToolSectionFilter(*Obj)) {
2105     StringRef Name;
2106     Sec.getName(Name);
2107     if (Name == ClangASTSectionName) {
2108       ClangASTSection = Sec;
2109       break;
2110     }
2111   }
2112   if (!ClangASTSection)
2113     return;
2114 
2115   StringRef ClangASTContents;
2116   error(ClangASTSection.getValue().getContents(ClangASTContents));
2117   outs().write(ClangASTContents.data(), ClangASTContents.size());
2118 }
2119 
2120 static void printFaultMaps(const ObjectFile *Obj) {
2121   const char *FaultMapSectionName = nullptr;
2122 
2123   if (isa<ELFObjectFileBase>(Obj)) {
2124     FaultMapSectionName = ".llvm_faultmaps";
2125   } else if (isa<MachOObjectFile>(Obj)) {
2126     FaultMapSectionName = "__llvm_faultmaps";
2127   } else {
2128     errs() << "This operation is only currently supported "
2129               "for ELF and Mach-O executable files.\n";
2130     return;
2131   }
2132 
2133   Optional<object::SectionRef> FaultMapSection;
2134 
2135   for (auto Sec : ToolSectionFilter(*Obj)) {
2136     StringRef Name;
2137     Sec.getName(Name);
2138     if (Name == FaultMapSectionName) {
2139       FaultMapSection = Sec;
2140       break;
2141     }
2142   }
2143 
2144   outs() << "FaultMap table:\n";
2145 
2146   if (!FaultMapSection.hasValue()) {
2147     outs() << "<not found>\n";
2148     return;
2149   }
2150 
2151   StringRef FaultMapContents;
2152   error(FaultMapSection.getValue().getContents(FaultMapContents));
2153 
2154   FaultMapParser FMP(FaultMapContents.bytes_begin(),
2155                      FaultMapContents.bytes_end());
2156 
2157   outs() << FMP;
2158 }
2159 
2160 static void printPrivateFileHeaders(const ObjectFile *o, bool onlyFirst) {
2161   if (o->isELF()) {
2162     printELFFileHeader(o);
2163     return printELFDynamicSection(o);
2164   }
2165   if (o->isCOFF())
2166     return printCOFFFileHeader(o);
2167   if (o->isWasm())
2168     return printWasmFileHeader(o);
2169   if (o->isMachO()) {
2170     printMachOFileHeader(o);
2171     if (!onlyFirst)
2172       printMachOLoadCommands(o);
2173     return;
2174   }
2175   report_error(o->getFileName(), "Invalid/Unsupported object file format");
2176 }
2177 
2178 static void printFileHeaders(const ObjectFile *o) {
2179   if (!o->isELF() && !o->isCOFF())
2180     report_error(o->getFileName(), "Invalid/Unsupported object file format");
2181 
2182   Triple::ArchType AT = o->getArch();
2183   outs() << "architecture: " << Triple::getArchTypeName(AT) << "\n";
2184   Expected<uint64_t> StartAddrOrErr = o->getStartAddress();
2185   if (!StartAddrOrErr)
2186     report_error(o->getFileName(), StartAddrOrErr.takeError());
2187   outs() << "start address: "
2188          << format("0x%0*x", o->getBytesInAddress(), StartAddrOrErr.get())
2189          << "\n";
2190 }
2191 
2192 static void printArchiveChild(StringRef Filename, const Archive::Child &C) {
2193   Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
2194   if (!ModeOrErr) {
2195     errs() << "ill-formed archive entry.\n";
2196     consumeError(ModeOrErr.takeError());
2197     return;
2198   }
2199   sys::fs::perms Mode = ModeOrErr.get();
2200   outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
2201   outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
2202   outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
2203   outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
2204   outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
2205   outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
2206   outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
2207   outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
2208   outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
2209 
2210   outs() << " ";
2211 
2212   Expected<unsigned> UIDOrErr = C.getUID();
2213   if (!UIDOrErr)
2214     report_error(Filename, UIDOrErr.takeError());
2215   unsigned UID = UIDOrErr.get();
2216   outs() << format("%d/", UID);
2217 
2218   Expected<unsigned> GIDOrErr = C.getGID();
2219   if (!GIDOrErr)
2220     report_error(Filename, GIDOrErr.takeError());
2221   unsigned GID = GIDOrErr.get();
2222   outs() << format("%-d ", GID);
2223 
2224   Expected<uint64_t> Size = C.getRawSize();
2225   if (!Size)
2226     report_error(Filename, Size.takeError());
2227   outs() << format("%6" PRId64, Size.get()) << " ";
2228 
2229   StringRef RawLastModified = C.getRawLastModified();
2230   unsigned Seconds;
2231   if (RawLastModified.getAsInteger(10, Seconds))
2232     outs() << "(date: \"" << RawLastModified
2233            << "\" contains non-decimal chars) ";
2234   else {
2235     // Since ctime(3) returns a 26 character string of the form:
2236     // "Sun Sep 16 01:03:52 1973\n\0"
2237     // just print 24 characters.
2238     time_t t = Seconds;
2239     outs() << format("%.24s ", ctime(&t));
2240   }
2241 
2242   StringRef Name = "";
2243   Expected<StringRef> NameOrErr = C.getName();
2244   if (!NameOrErr) {
2245     consumeError(NameOrErr.takeError());
2246     Expected<StringRef> RawNameOrErr = C.getRawName();
2247     if (!RawNameOrErr)
2248       report_error(Filename, NameOrErr.takeError());
2249     Name = RawNameOrErr.get();
2250   } else {
2251     Name = NameOrErr.get();
2252   }
2253   outs() << Name << "\n";
2254 }
2255 
2256 static void DumpObject(ObjectFile *o, const Archive *a = nullptr,
2257                        const Archive::Child *c = nullptr) {
2258   StringRef ArchiveName = a != nullptr ? a->getFileName() : "";
2259   // Avoid other output when using a raw option.
2260   if (!RawClangAST) {
2261     outs() << '\n';
2262     if (a)
2263       outs() << a->getFileName() << "(" << o->getFileName() << ")";
2264     else
2265       outs() << o->getFileName();
2266     outs() << ":\tfile format " << o->getFileFormatName() << "\n\n";
2267   }
2268 
2269   if (ArchiveHeaders && !MachOOpt)
2270     printArchiveChild(a->getFileName(), *c);
2271   if (Disassemble)
2272     DisassembleObject(o, Relocations);
2273   if (Relocations && !Disassemble)
2274     PrintRelocations(o);
2275   if (DynamicRelocations)
2276     PrintDynamicRelocations(o);
2277   if (SectionHeaders)
2278     PrintSectionHeaders(o);
2279   if (SectionContents)
2280     PrintSectionContents(o);
2281   if (SymbolTable)
2282     PrintSymbolTable(o, ArchiveName);
2283   if (UnwindInfo)
2284     PrintUnwindInfo(o);
2285   if (PrivateHeaders || FirstPrivateHeader)
2286     printPrivateFileHeaders(o, FirstPrivateHeader);
2287   if (FileHeaders)
2288     printFileHeaders(o);
2289   if (ExportsTrie)
2290     printExportsTrie(o);
2291   if (Rebase)
2292     printRebaseTable(o);
2293   if (Bind)
2294     printBindTable(o);
2295   if (LazyBind)
2296     printLazyBindTable(o);
2297   if (WeakBind)
2298     printWeakBindTable(o);
2299   if (RawClangAST)
2300     printRawClangAST(o);
2301   if (PrintFaultMaps)
2302     printFaultMaps(o);
2303   if (DwarfDumpType != DIDT_Null) {
2304     std::unique_ptr<DIContext> DICtx = DWARFContext::create(*o);
2305     // Dump the complete DWARF structure.
2306     DIDumpOptions DumpOpts;
2307     DumpOpts.DumpType = DwarfDumpType;
2308     DICtx->dump(outs(), DumpOpts);
2309   }
2310 }
2311 
2312 static void DumpObject(const COFFImportFile *I, const Archive *A,
2313                        const Archive::Child *C = nullptr) {
2314   StringRef ArchiveName = A ? A->getFileName() : "";
2315 
2316   // Avoid other output when using a raw option.
2317   if (!RawClangAST)
2318     outs() << '\n'
2319            << ArchiveName << "(" << I->getFileName() << ")"
2320            << ":\tfile format COFF-import-file"
2321            << "\n\n";
2322 
2323   if (ArchiveHeaders && !MachOOpt)
2324     printArchiveChild(A->getFileName(), *C);
2325   if (SymbolTable)
2326     printCOFFSymbolTable(I);
2327 }
2328 
2329 /// Dump each object file in \a a;
2330 static void DumpArchive(const Archive *a) {
2331   Error Err = Error::success();
2332   for (auto &C : a->children(Err)) {
2333     Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2334     if (!ChildOrErr) {
2335       if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2336         report_error(a->getFileName(), C, std::move(E));
2337       continue;
2338     }
2339     if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
2340       DumpObject(o, a, &C);
2341     else if (COFFImportFile *I = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
2342       DumpObject(I, a, &C);
2343     else
2344       report_error(a->getFileName(), object_error::invalid_file_type);
2345   }
2346   if (Err)
2347     report_error(a->getFileName(), std::move(Err));
2348 }
2349 
2350 /// Open file and figure out how to dump it.
2351 static void DumpInput(StringRef file) {
2352 
2353   // If we are using the Mach-O specific object file parser, then let it parse
2354   // the file and process the command line options.  So the -arch flags can
2355   // be used to select specific slices, etc.
2356   if (MachOOpt) {
2357     ParseInputMachO(file);
2358     return;
2359   }
2360 
2361   // Attempt to open the binary.
2362   Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
2363   if (!BinaryOrErr)
2364     report_error(file, BinaryOrErr.takeError());
2365   Binary &Binary = *BinaryOrErr.get().getBinary();
2366 
2367   if (Archive *a = dyn_cast<Archive>(&Binary))
2368     DumpArchive(a);
2369   else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
2370     DumpObject(o);
2371   else if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Binary))
2372     ParseInputMachO(UB);
2373   else
2374     report_error(file, object_error::invalid_file_type);
2375 }
2376 
2377 int main(int argc, char **argv) {
2378   InitLLVM X(argc, argv);
2379 
2380   // Initialize targets and assembly printers/parsers.
2381   llvm::InitializeAllTargetInfos();
2382   llvm::InitializeAllTargetMCs();
2383   llvm::InitializeAllDisassemblers();
2384 
2385   // Register the target printer for --version.
2386   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
2387 
2388   cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
2389 
2390   ToolName = argv[0];
2391 
2392   // Defaults to a.out if no filenames specified.
2393   if (InputFilenames.size() == 0)
2394     InputFilenames.push_back("a.out");
2395 
2396   if (AllHeaders)
2397     PrivateHeaders = Relocations = SectionHeaders = SymbolTable = true;
2398 
2399   if (DisassembleAll || PrintSource || PrintLines)
2400     Disassemble = true;
2401 
2402   if (!Disassemble
2403       && !Relocations
2404       && !DynamicRelocations
2405       && !SectionHeaders
2406       && !SectionContents
2407       && !SymbolTable
2408       && !UnwindInfo
2409       && !PrivateHeaders
2410       && !FileHeaders
2411       && !FirstPrivateHeader
2412       && !ExportsTrie
2413       && !Rebase
2414       && !Bind
2415       && !LazyBind
2416       && !WeakBind
2417       && !RawClangAST
2418       && !(UniversalHeaders && MachOOpt)
2419       && !ArchiveHeaders
2420       && !(IndirectSymbols && MachOOpt)
2421       && !(DataInCode && MachOOpt)
2422       && !(LinkOptHints && MachOOpt)
2423       && !(InfoPlist && MachOOpt)
2424       && !(DylibsUsed && MachOOpt)
2425       && !(DylibId && MachOOpt)
2426       && !(ObjcMetaData && MachOOpt)
2427       && !(FilterSections.size() != 0 && MachOOpt)
2428       && !PrintFaultMaps
2429       && DwarfDumpType == DIDT_Null) {
2430     cl::PrintHelpMessage();
2431     return 2;
2432   }
2433 
2434   DisasmFuncsSet.insert(DisassembleFunctions.begin(),
2435                         DisassembleFunctions.end());
2436 
2437   llvm::for_each(InputFilenames, DumpInput);
2438 
2439   return EXIT_SUCCESS;
2440 }
2441