xref: /llvm-project/llvm/tools/llvm-objdump/llvm-objdump.cpp (revision 1d14864bb35b011d1a1cd08a7c3c033ef87bde42)
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   SubtargetFeatures Features = Obj->getFeatures();
935   if (MAttrs.size()) {
936     for (unsigned i = 0; i != MAttrs.size(); ++i)
937       Features.AddFeature(MAttrs[i]);
938   }
939 
940   std::unique_ptr<const MCRegisterInfo> MRI(
941       TheTarget->createMCRegInfo(TripleName));
942   if (!MRI)
943     report_fatal_error("error: no register info for target " + TripleName);
944 
945   // Set up disassembler.
946   std::unique_ptr<const MCAsmInfo> AsmInfo(
947       TheTarget->createMCAsmInfo(*MRI, TripleName));
948   if (!AsmInfo)
949     report_fatal_error("error: no assembly info for target " + TripleName);
950   std::unique_ptr<const MCSubtargetInfo> STI(
951       TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString()));
952   if (!STI)
953     report_fatal_error("error: no subtarget info for target " + TripleName);
954   std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
955   if (!MII)
956     report_fatal_error("error: no instruction info for target " + TripleName);
957   std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
958   MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
959 
960   std::unique_ptr<MCDisassembler> DisAsm(
961     TheTarget->createMCDisassembler(*STI, Ctx));
962   if (!DisAsm)
963     report_fatal_error("error: no disassembler for target " + TripleName);
964 
965   std::unique_ptr<const MCInstrAnalysis> MIA(
966       TheTarget->createMCInstrAnalysis(MII.get()));
967 
968   int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
969   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
970       Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
971   if (!IP)
972     report_fatal_error("error: no instruction printer for target " +
973                        TripleName);
974   IP->setPrintImmHex(PrintImmHex);
975   PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
976 
977   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ":  " :
978                                                  "\t\t\t%08" PRIx64 ":  ";
979 
980   // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
981   // in RelocSecs contain the relocations for section S.
982   std::error_code EC;
983   std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
984   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
985     section_iterator Sec2 = Section.getRelocatedSection();
986     if (Sec2 != Obj->section_end())
987       SectionRelocMap[*Sec2].push_back(Section);
988   }
989 
990   // Create a mapping from virtual address to symbol name.  This is used to
991   // pretty print the symbols while disassembling.
992   typedef std::vector<std::pair<uint64_t, StringRef>> SectionSymbolsTy;
993   std::map<SectionRef, SectionSymbolsTy> AllSymbols;
994   for (const SymbolRef &Symbol : Obj->symbols()) {
995     ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
996     error(AddressOrErr.getError());
997     uint64_t Address = *AddressOrErr;
998 
999     Expected<StringRef> Name = Symbol.getName();
1000     error(errorToErrorCode(Name.takeError()));
1001     if (Name->empty())
1002       continue;
1003 
1004     Expected<section_iterator> SectionOrErr = Symbol.getSection();
1005     error(errorToErrorCode(SectionOrErr.takeError()));
1006     section_iterator SecI = *SectionOrErr;
1007     if (SecI == Obj->section_end())
1008       continue;
1009 
1010     AllSymbols[*SecI].emplace_back(Address, *Name);
1011   }
1012 
1013   // Create a mapping from virtual address to section.
1014   std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
1015   for (SectionRef Sec : Obj->sections())
1016     SectionAddresses.emplace_back(Sec.getAddress(), Sec);
1017   array_pod_sort(SectionAddresses.begin(), SectionAddresses.end());
1018 
1019   // Linked executables (.exe and .dll files) typically don't include a real
1020   // symbol table but they might contain an export table.
1021   if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) {
1022     for (const auto &ExportEntry : COFFObj->export_directories()) {
1023       StringRef Name;
1024       error(ExportEntry.getSymbolName(Name));
1025       if (Name.empty())
1026         continue;
1027       uint32_t RVA;
1028       error(ExportEntry.getExportRVA(RVA));
1029 
1030       uint64_t VA = COFFObj->getImageBase() + RVA;
1031       auto Sec = std::upper_bound(
1032           SectionAddresses.begin(), SectionAddresses.end(), VA,
1033           [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) {
1034             return LHS < RHS.first;
1035           });
1036       if (Sec != SectionAddresses.begin())
1037         --Sec;
1038       else
1039         Sec = SectionAddresses.end();
1040 
1041       if (Sec != SectionAddresses.end())
1042         AllSymbols[Sec->second].emplace_back(VA, Name);
1043     }
1044   }
1045 
1046   // Sort all the symbols, this allows us to use a simple binary search to find
1047   // a symbol near an address.
1048   for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
1049     array_pod_sort(SecSyms.second.begin(), SecSyms.second.end());
1050 
1051   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1052     if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
1053       continue;
1054 
1055     uint64_t SectionAddr = Section.getAddress();
1056     uint64_t SectSize = Section.getSize();
1057     if (!SectSize)
1058       continue;
1059 
1060     // Get the list of all the symbols in this section.
1061     SectionSymbolsTy &Symbols = AllSymbols[Section];
1062     std::vector<uint64_t> DataMappingSymsAddr;
1063     std::vector<uint64_t> TextMappingSymsAddr;
1064     if (Obj->isELF() && Obj->getArch() == Triple::aarch64) {
1065       for (const auto &Symb : Symbols) {
1066         uint64_t Address = Symb.first;
1067         StringRef Name = Symb.second;
1068         if (Name.startswith("$d"))
1069           DataMappingSymsAddr.push_back(Address - SectionAddr);
1070         if (Name.startswith("$x"))
1071           TextMappingSymsAddr.push_back(Address - SectionAddr);
1072       }
1073     }
1074 
1075     std::sort(DataMappingSymsAddr.begin(), DataMappingSymsAddr.end());
1076     std::sort(TextMappingSymsAddr.begin(), TextMappingSymsAddr.end());
1077 
1078     // Make a list of all the relocations for this section.
1079     std::vector<RelocationRef> Rels;
1080     if (InlineRelocs) {
1081       for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
1082         for (const RelocationRef &Reloc : RelocSec.relocations()) {
1083           Rels.push_back(Reloc);
1084         }
1085       }
1086     }
1087 
1088     // Sort relocations by address.
1089     std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
1090 
1091     StringRef SegmentName = "";
1092     if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
1093       DataRefImpl DR = Section.getRawDataRefImpl();
1094       SegmentName = MachO->getSectionFinalSegmentName(DR);
1095     }
1096     StringRef name;
1097     error(Section.getName(name));
1098     outs() << "Disassembly of section ";
1099     if (!SegmentName.empty())
1100       outs() << SegmentName << ",";
1101     outs() << name << ':';
1102 
1103     // If the section has no symbol at the start, just insert a dummy one.
1104     if (Symbols.empty() || Symbols[0].first != 0)
1105       Symbols.insert(Symbols.begin(), std::make_pair(SectionAddr, name));
1106 
1107     SmallString<40> Comments;
1108     raw_svector_ostream CommentStream(Comments);
1109 
1110     StringRef BytesStr;
1111     error(Section.getContents(BytesStr));
1112     ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
1113                             BytesStr.size());
1114 
1115     uint64_t Size;
1116     uint64_t Index;
1117 
1118     std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
1119     std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
1120     // Disassemble symbol by symbol.
1121     for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
1122 
1123       uint64_t Start = Symbols[si].first - SectionAddr;
1124       // The end is either the section end or the beginning of the next
1125       // symbol.
1126       uint64_t End =
1127           (si == se - 1) ? SectSize : Symbols[si + 1].first - SectionAddr;
1128       // Don't try to disassemble beyond the end of section contents.
1129       if (End > SectSize)
1130         End = SectSize;
1131       // If this symbol has the same address as the next symbol, then skip it.
1132       if (Start >= End)
1133         continue;
1134 
1135       if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) {
1136         // make size 4 bytes folded
1137         End = Start + ((End - Start) & ~0x3ull);
1138         Start += 256; // add sizeof(amd_kernel_code_t)
1139         // cut trailing zeroes - up to 256 bytes (align)
1140         const uint64_t EndAlign = 256;
1141         const auto Limit = End - (std::min)(EndAlign, End - Start);
1142         while (End > Limit &&
1143           *reinterpret_cast<const support::ulittle32_t*>(&Bytes[End - 4]) == 0)
1144           End -= 4;
1145       }
1146 
1147       outs() << '\n' << Symbols[si].second << ":\n";
1148 
1149 #ifndef NDEBUG
1150       raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
1151 #else
1152       raw_ostream &DebugOut = nulls();
1153 #endif
1154 
1155       for (Index = Start; Index < End; Index += Size) {
1156         MCInst Inst;
1157 
1158         // AArch64 ELF binaries can interleave data and text in the
1159         // same section. We rely on the markers introduced to
1160         // understand what we need to dump.
1161         if (Obj->isELF() && Obj->getArch() == Triple::aarch64) {
1162           uint64_t Stride = 0;
1163 
1164           auto DAI = std::lower_bound(DataMappingSymsAddr.begin(),
1165                                       DataMappingSymsAddr.end(), Index);
1166           if (DAI != DataMappingSymsAddr.end() && *DAI == Index) {
1167             // Switch to data.
1168             while (Index < End) {
1169               outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1170               outs() << "\t";
1171               if (Index + 4 <= End) {
1172                 Stride = 4;
1173                 dumpBytes(Bytes.slice(Index, 4), outs());
1174                 outs() << "\t.word";
1175               } else if (Index + 2 <= End) {
1176                 Stride = 2;
1177                 dumpBytes(Bytes.slice(Index, 2), outs());
1178                 outs() << "\t.short";
1179               } else {
1180                 Stride = 1;
1181                 dumpBytes(Bytes.slice(Index, 1), outs());
1182                 outs() << "\t.byte";
1183               }
1184               Index += Stride;
1185               outs() << "\n";
1186               auto TAI = std::lower_bound(TextMappingSymsAddr.begin(),
1187                                           TextMappingSymsAddr.end(), Index);
1188               if (TAI != TextMappingSymsAddr.end() && *TAI == Index)
1189                 break;
1190             }
1191           }
1192         }
1193 
1194         if (Index >= End)
1195           break;
1196 
1197         bool Disassembled = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
1198                                                    SectionAddr + Index, DebugOut,
1199                                                    CommentStream);
1200         if (Size == 0)
1201           Size = 1;
1202         PIP.printInst(*IP, Disassembled ? &Inst : nullptr,
1203                       Bytes.slice(Index, Size),
1204                       SectionAddr + Index, outs(), "", *STI);
1205         outs() << CommentStream.str();
1206         Comments.clear();
1207 
1208         // Try to resolve the target of a call, tail call, etc. to a specific
1209         // symbol.
1210         if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
1211                     MIA->isConditionalBranch(Inst))) {
1212           uint64_t Target;
1213           if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
1214             // In a relocatable object, the target's section must reside in
1215             // the same section as the call instruction or it is accessed
1216             // through a relocation.
1217             //
1218             // In a non-relocatable object, the target may be in any section.
1219             //
1220             // N.B. We don't walk the relocations in the relocatable case yet.
1221             auto *TargetSectionSymbols = &Symbols;
1222             if (!Obj->isRelocatableObject()) {
1223               auto SectionAddress = std::upper_bound(
1224                   SectionAddresses.begin(), SectionAddresses.end(), Target,
1225                   [](uint64_t LHS,
1226                       const std::pair<uint64_t, SectionRef> &RHS) {
1227                     return LHS < RHS.first;
1228                   });
1229               if (SectionAddress != SectionAddresses.begin()) {
1230                 --SectionAddress;
1231                 TargetSectionSymbols = &AllSymbols[SectionAddress->second];
1232               } else {
1233                 TargetSectionSymbols = nullptr;
1234               }
1235             }
1236 
1237             // Find the first symbol in the section whose offset is less than
1238             // or equal to the target.
1239             if (TargetSectionSymbols) {
1240               auto TargetSym = std::upper_bound(
1241                   TargetSectionSymbols->begin(), TargetSectionSymbols->end(),
1242                   Target, [](uint64_t LHS,
1243                               const std::pair<uint64_t, StringRef> &RHS) {
1244                     return LHS < RHS.first;
1245                   });
1246               if (TargetSym != TargetSectionSymbols->begin()) {
1247                 --TargetSym;
1248                 uint64_t TargetAddress = std::get<0>(*TargetSym);
1249                 StringRef TargetName = std::get<1>(*TargetSym);
1250                 outs() << " <" << TargetName;
1251                 uint64_t Disp = Target - TargetAddress;
1252                 if (Disp)
1253                   outs() << "+0x" << utohexstr(Disp);
1254                 outs() << '>';
1255               }
1256             }
1257           }
1258         }
1259         outs() << "\n";
1260 
1261         // Print relocation for instruction.
1262         while (rel_cur != rel_end) {
1263           bool hidden = getHidden(*rel_cur);
1264           uint64_t addr = rel_cur->getOffset();
1265           SmallString<16> name;
1266           SmallString<32> val;
1267 
1268           // If this relocation is hidden, skip it.
1269           if (hidden) goto skip_print_rel;
1270 
1271           // Stop when rel_cur's address is past the current instruction.
1272           if (addr >= Index + Size) break;
1273           rel_cur->getTypeName(name);
1274           error(getRelocationValueString(*rel_cur, val));
1275           outs() << format(Fmt.data(), SectionAddr + addr) << name
1276                  << "\t" << val << "\n";
1277 
1278         skip_print_rel:
1279           ++rel_cur;
1280         }
1281       }
1282     }
1283   }
1284 }
1285 
1286 void llvm::PrintRelocations(const ObjectFile *Obj) {
1287   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
1288                                                  "%08" PRIx64;
1289   // Regular objdump doesn't print relocations in non-relocatable object
1290   // files.
1291   if (!Obj->isRelocatableObject())
1292     return;
1293 
1294   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1295     if (Section.relocation_begin() == Section.relocation_end())
1296       continue;
1297     StringRef secname;
1298     error(Section.getName(secname));
1299     outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
1300     for (const RelocationRef &Reloc : Section.relocations()) {
1301       bool hidden = getHidden(Reloc);
1302       uint64_t address = Reloc.getOffset();
1303       SmallString<32> relocname;
1304       SmallString<32> valuestr;
1305       if (hidden)
1306         continue;
1307       Reloc.getTypeName(relocname);
1308       error(getRelocationValueString(Reloc, valuestr));
1309       outs() << format(Fmt.data(), address) << " " << relocname << " "
1310              << valuestr << "\n";
1311     }
1312     outs() << "\n";
1313   }
1314 }
1315 
1316 void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
1317   outs() << "Sections:\n"
1318             "Idx Name          Size      Address          Type\n";
1319   unsigned i = 0;
1320   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1321     StringRef Name;
1322     error(Section.getName(Name));
1323     uint64_t Address = Section.getAddress();
1324     uint64_t Size = Section.getSize();
1325     bool Text = Section.isText();
1326     bool Data = Section.isData();
1327     bool BSS = Section.isBSS();
1328     std::string Type = (std::string(Text ? "TEXT " : "") +
1329                         (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
1330     outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
1331                      Name.str().c_str(), Size, Address, Type.c_str());
1332     ++i;
1333   }
1334 }
1335 
1336 void llvm::PrintSectionContents(const ObjectFile *Obj) {
1337   std::error_code EC;
1338   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1339     StringRef Name;
1340     StringRef Contents;
1341     error(Section.getName(Name));
1342     uint64_t BaseAddr = Section.getAddress();
1343     uint64_t Size = Section.getSize();
1344     if (!Size)
1345       continue;
1346 
1347     outs() << "Contents of section " << Name << ":\n";
1348     if (Section.isBSS()) {
1349       outs() << format("<skipping contents of bss section at [%04" PRIx64
1350                        ", %04" PRIx64 ")>\n",
1351                        BaseAddr, BaseAddr + Size);
1352       continue;
1353     }
1354 
1355     error(Section.getContents(Contents));
1356 
1357     // Dump out the content as hex and printable ascii characters.
1358     for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
1359       outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
1360       // Dump line of hex.
1361       for (std::size_t i = 0; i < 16; ++i) {
1362         if (i != 0 && i % 4 == 0)
1363           outs() << ' ';
1364         if (addr + i < end)
1365           outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
1366                  << hexdigit(Contents[addr + i] & 0xF, true);
1367         else
1368           outs() << "  ";
1369       }
1370       // Print ascii.
1371       outs() << "  ";
1372       for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
1373         if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
1374           outs() << Contents[addr + i];
1375         else
1376           outs() << ".";
1377       }
1378       outs() << "\n";
1379     }
1380   }
1381 }
1382 
1383 void llvm::PrintSymbolTable(const ObjectFile *o, StringRef ArchiveName,
1384                             StringRef ArchitectureName) {
1385   outs() << "SYMBOL TABLE:\n";
1386 
1387   if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
1388     printCOFFSymbolTable(coff);
1389     return;
1390   }
1391   for (const SymbolRef &Symbol : o->symbols()) {
1392     ErrorOr<uint64_t> AddressOrError = Symbol.getAddress();
1393     error(AddressOrError.getError());
1394     uint64_t Address = *AddressOrError;
1395     Expected<SymbolRef::Type> TypeOrError = Symbol.getType();
1396     if (!TypeOrError)
1397       report_error(ArchiveName, o->getFileName(), TypeOrError.takeError());
1398     SymbolRef::Type Type = *TypeOrError;
1399     uint32_t Flags = Symbol.getFlags();
1400     Expected<section_iterator> SectionOrErr = Symbol.getSection();
1401     error(errorToErrorCode(SectionOrErr.takeError()));
1402     section_iterator Section = *SectionOrErr;
1403     StringRef Name;
1404     if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
1405       Section->getName(Name);
1406     } else {
1407       Expected<StringRef> NameOrErr = Symbol.getName();
1408       if (!NameOrErr)
1409         report_error(ArchiveName, o->getFileName(), NameOrErr.takeError(),
1410                      ArchitectureName);
1411       Name = *NameOrErr;
1412     }
1413 
1414     bool Global = Flags & SymbolRef::SF_Global;
1415     bool Weak = Flags & SymbolRef::SF_Weak;
1416     bool Absolute = Flags & SymbolRef::SF_Absolute;
1417     bool Common = Flags & SymbolRef::SF_Common;
1418     bool Hidden = Flags & SymbolRef::SF_Hidden;
1419 
1420     char GlobLoc = ' ';
1421     if (Type != SymbolRef::ST_Unknown)
1422       GlobLoc = Global ? 'g' : 'l';
1423     char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
1424                  ? 'd' : ' ';
1425     char FileFunc = ' ';
1426     if (Type == SymbolRef::ST_File)
1427       FileFunc = 'f';
1428     else if (Type == SymbolRef::ST_Function)
1429       FileFunc = 'F';
1430 
1431     const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
1432                                                    "%08" PRIx64;
1433 
1434     outs() << format(Fmt, Address) << " "
1435            << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
1436            << (Weak ? 'w' : ' ') // Weak?
1437            << ' ' // Constructor. Not supported yet.
1438            << ' ' // Warning. Not supported yet.
1439            << ' ' // Indirect reference to another symbol.
1440            << Debug // Debugging (d) or dynamic (D) symbol.
1441            << FileFunc // Name of function (F), file (f) or object (O).
1442            << ' ';
1443     if (Absolute) {
1444       outs() << "*ABS*";
1445     } else if (Common) {
1446       outs() << "*COM*";
1447     } else if (Section == o->section_end()) {
1448       outs() << "*UND*";
1449     } else {
1450       if (const MachOObjectFile *MachO =
1451           dyn_cast<const MachOObjectFile>(o)) {
1452         DataRefImpl DR = Section->getRawDataRefImpl();
1453         StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
1454         outs() << SegmentName << ",";
1455       }
1456       StringRef SectionName;
1457       error(Section->getName(SectionName));
1458       outs() << SectionName;
1459     }
1460 
1461     outs() << '\t';
1462     if (Common || isa<ELFObjectFileBase>(o)) {
1463       uint64_t Val =
1464           Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
1465       outs() << format("\t %08" PRIx64 " ", Val);
1466     }
1467 
1468     if (Hidden) {
1469       outs() << ".hidden ";
1470     }
1471     outs() << Name
1472            << '\n';
1473   }
1474 }
1475 
1476 static void PrintUnwindInfo(const ObjectFile *o) {
1477   outs() << "Unwind info:\n\n";
1478 
1479   if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
1480     printCOFFUnwindInfo(coff);
1481   } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1482     printMachOUnwindInfo(MachO);
1483   else {
1484     // TODO: Extract DWARF dump tool to objdump.
1485     errs() << "This operation is only currently supported "
1486               "for COFF and MachO object files.\n";
1487     return;
1488   }
1489 }
1490 
1491 void llvm::printExportsTrie(const ObjectFile *o) {
1492   outs() << "Exports trie:\n";
1493   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1494     printMachOExportsTrie(MachO);
1495   else {
1496     errs() << "This operation is only currently supported "
1497               "for Mach-O executable files.\n";
1498     return;
1499   }
1500 }
1501 
1502 void llvm::printRebaseTable(const ObjectFile *o) {
1503   outs() << "Rebase table:\n";
1504   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1505     printMachORebaseTable(MachO);
1506   else {
1507     errs() << "This operation is only currently supported "
1508               "for Mach-O executable files.\n";
1509     return;
1510   }
1511 }
1512 
1513 void llvm::printBindTable(const ObjectFile *o) {
1514   outs() << "Bind table:\n";
1515   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1516     printMachOBindTable(MachO);
1517   else {
1518     errs() << "This operation is only currently supported "
1519               "for Mach-O executable files.\n";
1520     return;
1521   }
1522 }
1523 
1524 void llvm::printLazyBindTable(const ObjectFile *o) {
1525   outs() << "Lazy bind table:\n";
1526   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1527     printMachOLazyBindTable(MachO);
1528   else {
1529     errs() << "This operation is only currently supported "
1530               "for Mach-O executable files.\n";
1531     return;
1532   }
1533 }
1534 
1535 void llvm::printWeakBindTable(const ObjectFile *o) {
1536   outs() << "Weak bind table:\n";
1537   if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1538     printMachOWeakBindTable(MachO);
1539   else {
1540     errs() << "This operation is only currently supported "
1541               "for Mach-O executable files.\n";
1542     return;
1543   }
1544 }
1545 
1546 /// Dump the raw contents of the __clangast section so the output can be piped
1547 /// into llvm-bcanalyzer.
1548 void llvm::printRawClangAST(const ObjectFile *Obj) {
1549   if (outs().is_displayed()) {
1550     errs() << "The -raw-clang-ast option will dump the raw binary contents of "
1551               "the clang ast section.\n"
1552               "Please redirect the output to a file or another program such as "
1553               "llvm-bcanalyzer.\n";
1554     return;
1555   }
1556 
1557   StringRef ClangASTSectionName("__clangast");
1558   if (isa<COFFObjectFile>(Obj)) {
1559     ClangASTSectionName = "clangast";
1560   }
1561 
1562   Optional<object::SectionRef> ClangASTSection;
1563   for (auto Sec : ToolSectionFilter(*Obj)) {
1564     StringRef Name;
1565     Sec.getName(Name);
1566     if (Name == ClangASTSectionName) {
1567       ClangASTSection = Sec;
1568       break;
1569     }
1570   }
1571   if (!ClangASTSection)
1572     return;
1573 
1574   StringRef ClangASTContents;
1575   error(ClangASTSection.getValue().getContents(ClangASTContents));
1576   outs().write(ClangASTContents.data(), ClangASTContents.size());
1577 }
1578 
1579 static void printFaultMaps(const ObjectFile *Obj) {
1580   const char *FaultMapSectionName = nullptr;
1581 
1582   if (isa<ELFObjectFileBase>(Obj)) {
1583     FaultMapSectionName = ".llvm_faultmaps";
1584   } else if (isa<MachOObjectFile>(Obj)) {
1585     FaultMapSectionName = "__llvm_faultmaps";
1586   } else {
1587     errs() << "This operation is only currently supported "
1588               "for ELF and Mach-O executable files.\n";
1589     return;
1590   }
1591 
1592   Optional<object::SectionRef> FaultMapSection;
1593 
1594   for (auto Sec : ToolSectionFilter(*Obj)) {
1595     StringRef Name;
1596     Sec.getName(Name);
1597     if (Name == FaultMapSectionName) {
1598       FaultMapSection = Sec;
1599       break;
1600     }
1601   }
1602 
1603   outs() << "FaultMap table:\n";
1604 
1605   if (!FaultMapSection.hasValue()) {
1606     outs() << "<not found>\n";
1607     return;
1608   }
1609 
1610   StringRef FaultMapContents;
1611   error(FaultMapSection.getValue().getContents(FaultMapContents));
1612 
1613   FaultMapParser FMP(FaultMapContents.bytes_begin(),
1614                      FaultMapContents.bytes_end());
1615 
1616   outs() << FMP;
1617 }
1618 
1619 static void printPrivateFileHeaders(const ObjectFile *o) {
1620   if (o->isELF())
1621     printELFFileHeader(o);
1622   else if (o->isCOFF())
1623     printCOFFFileHeader(o);
1624   else if (o->isMachO()) {
1625     printMachOFileHeader(o);
1626     printMachOLoadCommands(o);
1627   } else
1628     report_fatal_error("Invalid/Unsupported object file format");
1629 }
1630 
1631 static void printFirstPrivateFileHeader(const ObjectFile *o) {
1632   if (o->isELF())
1633     printELFFileHeader(o);
1634   else if (o->isCOFF())
1635     printCOFFFileHeader(o);
1636   else if (o->isMachO())
1637     printMachOFileHeader(o);
1638   else
1639     report_fatal_error("Invalid/Unsupported object file format");
1640 }
1641 
1642 static void DumpObject(const ObjectFile *o, const Archive *a = nullptr) {
1643   StringRef ArchiveName = a != nullptr ? a->getFileName() : "";
1644   // Avoid other output when using a raw option.
1645   if (!RawClangAST) {
1646     outs() << '\n';
1647     if (a)
1648       outs() << a->getFileName() << "(" << o->getFileName() << ")";
1649     else
1650       outs() << o->getFileName();
1651     outs() << ":\tfile format " << o->getFileFormatName() << "\n\n";
1652   }
1653 
1654   if (Disassemble)
1655     DisassembleObject(o, Relocations);
1656   if (Relocations && !Disassemble)
1657     PrintRelocations(o);
1658   if (SectionHeaders)
1659     PrintSectionHeaders(o);
1660   if (SectionContents)
1661     PrintSectionContents(o);
1662   if (SymbolTable)
1663     PrintSymbolTable(o, ArchiveName);
1664   if (UnwindInfo)
1665     PrintUnwindInfo(o);
1666   if (PrivateHeaders)
1667     printPrivateFileHeaders(o);
1668   if (FirstPrivateHeader)
1669     printFirstPrivateFileHeader(o);
1670   if (ExportsTrie)
1671     printExportsTrie(o);
1672   if (Rebase)
1673     printRebaseTable(o);
1674   if (Bind)
1675     printBindTable(o);
1676   if (LazyBind)
1677     printLazyBindTable(o);
1678   if (WeakBind)
1679     printWeakBindTable(o);
1680   if (RawClangAST)
1681     printRawClangAST(o);
1682   if (PrintFaultMaps)
1683     printFaultMaps(o);
1684   if (DwarfDumpType != DIDT_Null) {
1685     std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(*o));
1686     // Dump the complete DWARF structure.
1687     DICtx->dump(outs(), DwarfDumpType, true /* DumpEH */);
1688   }
1689 }
1690 
1691 /// @brief Dump each object file in \a a;
1692 static void DumpArchive(const Archive *a) {
1693   for (auto &ErrorOrChild : a->children()) {
1694     if (std::error_code EC = ErrorOrChild.getError())
1695       report_error(a->getFileName(), EC);
1696     const Archive::Child &C = *ErrorOrChild;
1697     Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1698     if (!ChildOrErr) {
1699       if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1700         report_error(a->getFileName(), C, std::move(E));
1701       continue;
1702     }
1703     if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
1704       DumpObject(o, a);
1705     else
1706       report_error(a->getFileName(), object_error::invalid_file_type);
1707   }
1708 }
1709 
1710 /// @brief Open file and figure out how to dump it.
1711 static void DumpInput(StringRef file) {
1712 
1713   // If we are using the Mach-O specific object file parser, then let it parse
1714   // the file and process the command line options.  So the -arch flags can
1715   // be used to select specific slices, etc.
1716   if (MachOOpt) {
1717     ParseInputMachO(file);
1718     return;
1719   }
1720 
1721   // Attempt to open the binary.
1722   Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
1723   if (!BinaryOrErr)
1724     report_error(file, BinaryOrErr.takeError());
1725   Binary &Binary = *BinaryOrErr.get().getBinary();
1726 
1727   if (Archive *a = dyn_cast<Archive>(&Binary))
1728     DumpArchive(a);
1729   else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
1730     DumpObject(o);
1731   else
1732     report_error(file, object_error::invalid_file_type);
1733 }
1734 
1735 int main(int argc, char **argv) {
1736   // Print a stack trace if we signal out.
1737   sys::PrintStackTraceOnErrorSignal(argv[0]);
1738   PrettyStackTraceProgram X(argc, argv);
1739   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
1740 
1741   // Initialize targets and assembly printers/parsers.
1742   llvm::InitializeAllTargetInfos();
1743   llvm::InitializeAllTargetMCs();
1744   llvm::InitializeAllDisassemblers();
1745 
1746   // Register the target printer for --version.
1747   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
1748 
1749   cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
1750   TripleName = Triple::normalize(TripleName);
1751 
1752   ToolName = argv[0];
1753 
1754   // Defaults to a.out if no filenames specified.
1755   if (InputFilenames.size() == 0)
1756     InputFilenames.push_back("a.out");
1757 
1758   if (DisassembleAll)
1759     Disassemble = true;
1760   if (!Disassemble
1761       && !Relocations
1762       && !SectionHeaders
1763       && !SectionContents
1764       && !SymbolTable
1765       && !UnwindInfo
1766       && !PrivateHeaders
1767       && !FirstPrivateHeader
1768       && !ExportsTrie
1769       && !Rebase
1770       && !Bind
1771       && !LazyBind
1772       && !WeakBind
1773       && !RawClangAST
1774       && !(UniversalHeaders && MachOOpt)
1775       && !(ArchiveHeaders && MachOOpt)
1776       && !(IndirectSymbols && MachOOpt)
1777       && !(DataInCode && MachOOpt)
1778       && !(LinkOptHints && MachOOpt)
1779       && !(InfoPlist && MachOOpt)
1780       && !(DylibsUsed && MachOOpt)
1781       && !(DylibId && MachOOpt)
1782       && !(ObjcMetaData && MachOOpt)
1783       && !(FilterSections.size() != 0 && MachOOpt)
1784       && !PrintFaultMaps
1785       && DwarfDumpType == DIDT_Null) {
1786     cl::PrintHelpMessage();
1787     return 2;
1788   }
1789 
1790   std::for_each(InputFilenames.begin(), InputFilenames.end(),
1791                 DumpInput);
1792 
1793   return EXIT_SUCCESS;
1794 }
1795