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