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