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