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