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