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