xref: /llvm-project/llvm/tools/llvm-objdump/llvm-objdump.cpp (revision ad92a3db2e43f1fcac65a1a7949dd24c855d7002)
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 "COFFDump.h"
20 #include "ELFDump.h"
21 #include "MachODump.h"
22 #include "ObjdumpOptID.h"
23 #include "OffloadDump.h"
24 #include "SourcePrinter.h"
25 #include "WasmDump.h"
26 #include "XCOFFDump.h"
27 #include "llvm/ADT/IndexedMap.h"
28 #include "llvm/ADT/Optional.h"
29 #include "llvm/ADT/STLExtras.h"
30 #include "llvm/ADT/SetOperations.h"
31 #include "llvm/ADT/SmallSet.h"
32 #include "llvm/ADT/StringExtras.h"
33 #include "llvm/ADT/StringSet.h"
34 #include "llvm/ADT/Triple.h"
35 #include "llvm/ADT/Twine.h"
36 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
37 #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
38 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
39 #include "llvm/Debuginfod/BuildIDFetcher.h"
40 #include "llvm/Debuginfod/Debuginfod.h"
41 #include "llvm/Debuginfod/HTTPClient.h"
42 #include "llvm/Demangle/Demangle.h"
43 #include "llvm/MC/MCAsmInfo.h"
44 #include "llvm/MC/MCContext.h"
45 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
46 #include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
47 #include "llvm/MC/MCInst.h"
48 #include "llvm/MC/MCInstPrinter.h"
49 #include "llvm/MC/MCInstrAnalysis.h"
50 #include "llvm/MC/MCInstrInfo.h"
51 #include "llvm/MC/MCObjectFileInfo.h"
52 #include "llvm/MC/MCRegisterInfo.h"
53 #include "llvm/MC/MCSubtargetInfo.h"
54 #include "llvm/MC/MCTargetOptions.h"
55 #include "llvm/MC/TargetRegistry.h"
56 #include "llvm/Object/Archive.h"
57 #include "llvm/Object/BuildID.h"
58 #include "llvm/Object/COFF.h"
59 #include "llvm/Object/COFFImportFile.h"
60 #include "llvm/Object/ELFObjectFile.h"
61 #include "llvm/Object/ELFTypes.h"
62 #include "llvm/Object/FaultMapParser.h"
63 #include "llvm/Object/MachO.h"
64 #include "llvm/Object/MachOUniversal.h"
65 #include "llvm/Object/ObjectFile.h"
66 #include "llvm/Object/OffloadBinary.h"
67 #include "llvm/Object/Wasm.h"
68 #include "llvm/Option/Arg.h"
69 #include "llvm/Option/ArgList.h"
70 #include "llvm/Option/Option.h"
71 #include "llvm/Support/Casting.h"
72 #include "llvm/Support/Debug.h"
73 #include "llvm/Support/Errc.h"
74 #include "llvm/Support/FileSystem.h"
75 #include "llvm/Support/Format.h"
76 #include "llvm/Support/FormatVariadic.h"
77 #include "llvm/Support/GraphWriter.h"
78 #include "llvm/Support/Host.h"
79 #include "llvm/Support/InitLLVM.h"
80 #include "llvm/Support/MemoryBuffer.h"
81 #include "llvm/Support/SourceMgr.h"
82 #include "llvm/Support/StringSaver.h"
83 #include "llvm/Support/TargetSelect.h"
84 #include "llvm/Support/WithColor.h"
85 #include "llvm/Support/raw_ostream.h"
86 #include <algorithm>
87 #include <cctype>
88 #include <cstring>
89 #include <system_error>
90 #include <unordered_map>
91 #include <utility>
92 
93 using namespace llvm;
94 using namespace llvm::object;
95 using namespace llvm::objdump;
96 using namespace llvm::opt;
97 
98 namespace {
99 
100 class CommonOptTable : public opt::OptTable {
101 public:
102   CommonOptTable(ArrayRef<Info> OptionInfos, const char *Usage,
103                  const char *Description)
104       : OptTable(OptionInfos), Usage(Usage), Description(Description) {
105     setGroupedShortOptions(true);
106   }
107 
108   void printHelp(StringRef Argv0, bool ShowHidden = false) const {
109     Argv0 = sys::path::filename(Argv0);
110     opt::OptTable::printHelp(outs(), (Argv0 + Usage).str().c_str(), Description,
111                              ShowHidden, ShowHidden);
112     // TODO Replace this with OptTable API once it adds extrahelp support.
113     outs() << "\nPass @FILE as argument to read options from FILE.\n";
114   }
115 
116 private:
117   const char *Usage;
118   const char *Description;
119 };
120 
121 // ObjdumpOptID is in ObjdumpOptID.h
122 
123 #define PREFIX(NAME, VALUE) const char *const OBJDUMP_##NAME[] = VALUE;
124 #include "ObjdumpOpts.inc"
125 #undef PREFIX
126 
127 static constexpr opt::OptTable::Info ObjdumpInfoTable[] = {
128 #define OBJDUMP_nullptr nullptr
129 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
130                HELPTEXT, METAVAR, VALUES)                                      \
131   {OBJDUMP_##PREFIX, NAME,         HELPTEXT,                                   \
132    METAVAR,          OBJDUMP_##ID, opt::Option::KIND##Class,                   \
133    PARAM,            FLAGS,        OBJDUMP_##GROUP,                            \
134    OBJDUMP_##ALIAS,  ALIASARGS,    VALUES},
135 #include "ObjdumpOpts.inc"
136 #undef OPTION
137 #undef OBJDUMP_nullptr
138 };
139 
140 class ObjdumpOptTable : public CommonOptTable {
141 public:
142   ObjdumpOptTable()
143       : CommonOptTable(ObjdumpInfoTable, " [options] <input object files>",
144                        "llvm object file dumper") {}
145 };
146 
147 enum OtoolOptID {
148   OTOOL_INVALID = 0, // This is not an option ID.
149 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
150                HELPTEXT, METAVAR, VALUES)                                      \
151   OTOOL_##ID,
152 #include "OtoolOpts.inc"
153 #undef OPTION
154 };
155 
156 #define PREFIX(NAME, VALUE) const char *const OTOOL_##NAME[] = VALUE;
157 #include "OtoolOpts.inc"
158 #undef PREFIX
159 
160 static constexpr opt::OptTable::Info OtoolInfoTable[] = {
161 #define OTOOL_nullptr nullptr
162 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
163                HELPTEXT, METAVAR, VALUES)                                      \
164   {OTOOL_##PREFIX, NAME,       HELPTEXT,                                       \
165    METAVAR,        OTOOL_##ID, opt::Option::KIND##Class,                       \
166    PARAM,          FLAGS,      OTOOL_##GROUP,                                  \
167    OTOOL_##ALIAS,  ALIASARGS,  VALUES},
168 #include "OtoolOpts.inc"
169 #undef OPTION
170 #undef OTOOL_nullptr
171 };
172 
173 class OtoolOptTable : public CommonOptTable {
174 public:
175   OtoolOptTable()
176       : CommonOptTable(OtoolInfoTable, " [option...] [file...]",
177                        "Mach-O object file displaying tool") {}
178 };
179 
180 } // namespace
181 
182 #define DEBUG_TYPE "objdump"
183 
184 static uint64_t AdjustVMA;
185 static bool AllHeaders;
186 static std::string ArchName;
187 bool objdump::ArchiveHeaders;
188 bool objdump::Demangle;
189 bool objdump::Disassemble;
190 bool objdump::DisassembleAll;
191 bool objdump::SymbolDescription;
192 static std::vector<std::string> DisassembleSymbols;
193 static bool DisassembleZeroes;
194 static std::vector<std::string> DisassemblerOptions;
195 DIDumpType objdump::DwarfDumpType;
196 static bool DynamicRelocations;
197 static bool FaultMapSection;
198 static bool FileHeaders;
199 bool objdump::SectionContents;
200 static std::vector<std::string> InputFilenames;
201 bool objdump::PrintLines;
202 static bool MachOOpt;
203 std::string objdump::MCPU;
204 std::vector<std::string> objdump::MAttrs;
205 bool objdump::ShowRawInsn;
206 bool objdump::LeadingAddr;
207 static bool Offloading;
208 static bool RawClangAST;
209 bool objdump::Relocations;
210 bool objdump::PrintImmHex;
211 bool objdump::PrivateHeaders;
212 std::vector<std::string> objdump::FilterSections;
213 bool objdump::SectionHeaders;
214 static bool ShowAllSymbols;
215 static bool ShowLMA;
216 bool objdump::PrintSource;
217 
218 static uint64_t StartAddress;
219 static bool HasStartAddressFlag;
220 static uint64_t StopAddress = UINT64_MAX;
221 static bool HasStopAddressFlag;
222 
223 bool objdump::SymbolTable;
224 static bool SymbolizeOperands;
225 static bool DynamicSymbolTable;
226 std::string objdump::TripleName;
227 bool objdump::UnwindInfo;
228 static bool Wide;
229 std::string objdump::Prefix;
230 uint32_t objdump::PrefixStrip;
231 
232 DebugVarsFormat objdump::DbgVariables = DVDisabled;
233 
234 int objdump::DbgIndent = 52;
235 
236 static StringSet<> DisasmSymbolSet;
237 StringSet<> objdump::FoundSectionSet;
238 static StringRef ToolName;
239 
240 std::unique_ptr<BuildIDFetcher> BIDFetcher;
241 ExitOnError ExitOnErr;
242 
243 namespace {
244 struct FilterResult {
245   // True if the section should not be skipped.
246   bool Keep;
247 
248   // True if the index counter should be incremented, even if the section should
249   // be skipped. For example, sections may be skipped if they are not included
250   // in the --section flag, but we still want those to count toward the section
251   // count.
252   bool IncrementIndex;
253 };
254 } // namespace
255 
256 static FilterResult checkSectionFilter(object::SectionRef S) {
257   if (FilterSections.empty())
258     return {/*Keep=*/true, /*IncrementIndex=*/true};
259 
260   Expected<StringRef> SecNameOrErr = S.getName();
261   if (!SecNameOrErr) {
262     consumeError(SecNameOrErr.takeError());
263     return {/*Keep=*/false, /*IncrementIndex=*/false};
264   }
265   StringRef SecName = *SecNameOrErr;
266 
267   // StringSet does not allow empty key so avoid adding sections with
268   // no name (such as the section with index 0) here.
269   if (!SecName.empty())
270     FoundSectionSet.insert(SecName);
271 
272   // Only show the section if it's in the FilterSections list, but always
273   // increment so the indexing is stable.
274   return {/*Keep=*/is_contained(FilterSections, SecName),
275           /*IncrementIndex=*/true};
276 }
277 
278 SectionFilter objdump::ToolSectionFilter(object::ObjectFile const &O,
279                                          uint64_t *Idx) {
280   // Start at UINT64_MAX so that the first index returned after an increment is
281   // zero (after the unsigned wrap).
282   if (Idx)
283     *Idx = UINT64_MAX;
284   return SectionFilter(
285       [Idx](object::SectionRef S) {
286         FilterResult Result = checkSectionFilter(S);
287         if (Idx != nullptr && Result.IncrementIndex)
288           *Idx += 1;
289         return Result.Keep;
290       },
291       O);
292 }
293 
294 std::string objdump::getFileNameForError(const object::Archive::Child &C,
295                                          unsigned Index) {
296   Expected<StringRef> NameOrErr = C.getName();
297   if (NameOrErr)
298     return std::string(NameOrErr.get());
299   // If we have an error getting the name then we print the index of the archive
300   // member. Since we are already in an error state, we just ignore this error.
301   consumeError(NameOrErr.takeError());
302   return "<file index: " + std::to_string(Index) + ">";
303 }
304 
305 void objdump::reportWarning(const Twine &Message, StringRef File) {
306   // Output order between errs() and outs() matters especially for archive
307   // files where the output is per member object.
308   outs().flush();
309   WithColor::warning(errs(), ToolName)
310       << "'" << File << "': " << Message << "\n";
311 }
312 
313 [[noreturn]] void objdump::reportError(StringRef File, const Twine &Message) {
314   outs().flush();
315   WithColor::error(errs(), ToolName) << "'" << File << "': " << Message << "\n";
316   exit(1);
317 }
318 
319 [[noreturn]] void objdump::reportError(Error E, StringRef FileName,
320                                        StringRef ArchiveName,
321                                        StringRef ArchitectureName) {
322   assert(E);
323   outs().flush();
324   WithColor::error(errs(), ToolName);
325   if (ArchiveName != "")
326     errs() << ArchiveName << "(" << FileName << ")";
327   else
328     errs() << "'" << FileName << "'";
329   if (!ArchitectureName.empty())
330     errs() << " (for architecture " << ArchitectureName << ")";
331   errs() << ": ";
332   logAllUnhandledErrors(std::move(E), errs());
333   exit(1);
334 }
335 
336 static void reportCmdLineWarning(const Twine &Message) {
337   WithColor::warning(errs(), ToolName) << Message << "\n";
338 }
339 
340 [[noreturn]] static void reportCmdLineError(const Twine &Message) {
341   WithColor::error(errs(), ToolName) << Message << "\n";
342   exit(1);
343 }
344 
345 static void warnOnNoMatchForSections() {
346   SetVector<StringRef> MissingSections;
347   for (StringRef S : FilterSections) {
348     if (FoundSectionSet.count(S))
349       return;
350     // User may specify a unnamed section. Don't warn for it.
351     if (!S.empty())
352       MissingSections.insert(S);
353   }
354 
355   // Warn only if no section in FilterSections is matched.
356   for (StringRef S : MissingSections)
357     reportCmdLineWarning("section '" + S +
358                          "' mentioned in a -j/--section option, but not "
359                          "found in any input file");
360 }
361 
362 static const Target *getTarget(const ObjectFile *Obj) {
363   // Figure out the target triple.
364   Triple TheTriple("unknown-unknown-unknown");
365   if (TripleName.empty()) {
366     TheTriple = Obj->makeTriple();
367   } else {
368     TheTriple.setTriple(Triple::normalize(TripleName));
369     auto Arch = Obj->getArch();
370     if (Arch == Triple::arm || Arch == Triple::armeb)
371       Obj->setARMSubArch(TheTriple);
372   }
373 
374   // Get the target specific parser.
375   std::string Error;
376   const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
377                                                          Error);
378   if (!TheTarget)
379     reportError(Obj->getFileName(), "can't find target: " + Error);
380 
381   // Update the triple name and return the found target.
382   TripleName = TheTriple.getTriple();
383   return TheTarget;
384 }
385 
386 bool objdump::isRelocAddressLess(RelocationRef A, RelocationRef B) {
387   return A.getOffset() < B.getOffset();
388 }
389 
390 static Error getRelocationValueString(const RelocationRef &Rel,
391                                       SmallVectorImpl<char> &Result) {
392   const ObjectFile *Obj = Rel.getObject();
393   if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
394     return getELFRelocationValueString(ELF, Rel, Result);
395   if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
396     return getCOFFRelocationValueString(COFF, Rel, Result);
397   if (auto *Wasm = dyn_cast<WasmObjectFile>(Obj))
398     return getWasmRelocationValueString(Wasm, Rel, Result);
399   if (auto *MachO = dyn_cast<MachOObjectFile>(Obj))
400     return getMachORelocationValueString(MachO, Rel, Result);
401   if (auto *XCOFF = dyn_cast<XCOFFObjectFile>(Obj))
402     return getXCOFFRelocationValueString(*XCOFF, Rel, Result);
403   llvm_unreachable("unknown object file format");
404 }
405 
406 /// Indicates whether this relocation should hidden when listing
407 /// relocations, usually because it is the trailing part of a multipart
408 /// relocation that will be printed as part of the leading relocation.
409 static bool getHidden(RelocationRef RelRef) {
410   auto *MachO = dyn_cast<MachOObjectFile>(RelRef.getObject());
411   if (!MachO)
412     return false;
413 
414   unsigned Arch = MachO->getArch();
415   DataRefImpl Rel = RelRef.getRawDataRefImpl();
416   uint64_t Type = MachO->getRelocationType(Rel);
417 
418   // On arches that use the generic relocations, GENERIC_RELOC_PAIR
419   // is always hidden.
420   if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc)
421     return Type == MachO::GENERIC_RELOC_PAIR;
422 
423   if (Arch == Triple::x86_64) {
424     // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
425     // an X86_64_RELOC_SUBTRACTOR.
426     if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
427       DataRefImpl RelPrev = Rel;
428       RelPrev.d.a--;
429       uint64_t PrevType = MachO->getRelocationType(RelPrev);
430       if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
431         return true;
432     }
433   }
434 
435   return false;
436 }
437 
438 namespace {
439 
440 /// Get the column at which we want to start printing the instruction
441 /// disassembly, taking into account anything which appears to the left of it.
442 unsigned getInstStartColumn(const MCSubtargetInfo &STI) {
443   return !ShowRawInsn ? 16 : STI.getTargetTriple().isX86() ? 40 : 24;
444 }
445 
446 static bool isAArch64Elf(const ObjectFile &Obj) {
447   const auto *Elf = dyn_cast<ELFObjectFileBase>(&Obj);
448   return Elf && Elf->getEMachine() == ELF::EM_AARCH64;
449 }
450 
451 static bool isArmElf(const ObjectFile &Obj) {
452   const auto *Elf = dyn_cast<ELFObjectFileBase>(&Obj);
453   return Elf && Elf->getEMachine() == ELF::EM_ARM;
454 }
455 
456 static bool isCSKYElf(const ObjectFile &Obj) {
457   const auto *Elf = dyn_cast<ELFObjectFileBase>(&Obj);
458   return Elf && Elf->getEMachine() == ELF::EM_CSKY;
459 }
460 
461 static bool hasMappingSymbols(const ObjectFile &Obj) {
462   return isArmElf(Obj) || isAArch64Elf(Obj) || isCSKYElf(Obj) ;
463 }
464 
465 static void printRelocation(formatted_raw_ostream &OS, StringRef FileName,
466                             const RelocationRef &Rel, uint64_t Address,
467                             bool Is64Bits) {
468   StringRef Fmt = Is64Bits ? "%016" PRIx64 ":  " : "%08" PRIx64 ":  ";
469   SmallString<16> Name;
470   SmallString<32> Val;
471   Rel.getTypeName(Name);
472   if (Error E = getRelocationValueString(Rel, Val))
473     reportError(std::move(E), FileName);
474   OS << (Is64Bits || !LeadingAddr ? "\t\t" : "\t\t\t");
475   if (LeadingAddr)
476     OS << format(Fmt.data(), Address);
477   OS << Name << "\t" << Val;
478 }
479 
480 static void AlignToInstStartColumn(size_t Start, const MCSubtargetInfo &STI,
481                                    raw_ostream &OS) {
482   // The output of printInst starts with a tab. Print some spaces so that
483   // the tab has 1 column and advances to the target tab stop.
484   unsigned TabStop = getInstStartColumn(STI);
485   unsigned Column = OS.tell() - Start;
486   OS.indent(Column < TabStop - 1 ? TabStop - 1 - Column : 7 - Column % 8);
487 }
488 
489 class PrettyPrinter {
490 public:
491   virtual ~PrettyPrinter() = default;
492   virtual void
493   printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
494             object::SectionedAddress Address, formatted_raw_ostream &OS,
495             StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
496             StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
497             LiveVariablePrinter &LVP) {
498     if (SP && (PrintSource || PrintLines))
499       SP->printSourceLine(OS, Address, ObjectFilename, LVP);
500     LVP.printBetweenInsts(OS, false);
501 
502     size_t Start = OS.tell();
503     if (LeadingAddr)
504       OS << format("%8" PRIx64 ":", Address.Address);
505     if (ShowRawInsn) {
506       OS << ' ';
507       dumpBytes(Bytes, OS);
508     }
509 
510     AlignToInstStartColumn(Start, STI, OS);
511 
512     if (MI) {
513       // See MCInstPrinter::printInst. On targets where a PC relative immediate
514       // is relative to the next instruction and the length of a MCInst is
515       // difficult to measure (x86), this is the address of the next
516       // instruction.
517       uint64_t Addr =
518           Address.Address + (STI.getTargetTriple().isX86() ? Bytes.size() : 0);
519       IP.printInst(MI, Addr, "", STI, OS);
520     } else
521       OS << "\t<unknown>";
522   }
523 };
524 PrettyPrinter PrettyPrinterInst;
525 
526 class HexagonPrettyPrinter : public PrettyPrinter {
527 public:
528   void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
529                  formatted_raw_ostream &OS) {
530     uint32_t opcode =
531       (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
532     if (LeadingAddr)
533       OS << format("%8" PRIx64 ":", Address);
534     if (ShowRawInsn) {
535       OS << "\t";
536       dumpBytes(Bytes.slice(0, 4), OS);
537       OS << format("\t%08" PRIx32, opcode);
538     }
539   }
540   void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
541                  object::SectionedAddress Address, formatted_raw_ostream &OS,
542                  StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
543                  StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
544                  LiveVariablePrinter &LVP) override {
545     if (SP && (PrintSource || PrintLines))
546       SP->printSourceLine(OS, Address, ObjectFilename, LVP, "");
547     if (!MI) {
548       printLead(Bytes, Address.Address, OS);
549       OS << " <unknown>";
550       return;
551     }
552     std::string Buffer;
553     {
554       raw_string_ostream TempStream(Buffer);
555       IP.printInst(MI, Address.Address, "", STI, TempStream);
556     }
557     StringRef Contents(Buffer);
558     // Split off bundle attributes
559     auto PacketBundle = Contents.rsplit('\n');
560     // Split off first instruction from the rest
561     auto HeadTail = PacketBundle.first.split('\n');
562     auto Preamble = " { ";
563     auto Separator = "";
564 
565     // Hexagon's packets require relocations to be inline rather than
566     // clustered at the end of the packet.
567     std::vector<RelocationRef>::const_iterator RelCur = Rels->begin();
568     std::vector<RelocationRef>::const_iterator RelEnd = Rels->end();
569     auto PrintReloc = [&]() -> void {
570       while ((RelCur != RelEnd) && (RelCur->getOffset() <= Address.Address)) {
571         if (RelCur->getOffset() == Address.Address) {
572           printRelocation(OS, ObjectFilename, *RelCur, Address.Address, false);
573           return;
574         }
575         ++RelCur;
576       }
577     };
578 
579     while (!HeadTail.first.empty()) {
580       OS << Separator;
581       Separator = "\n";
582       if (SP && (PrintSource || PrintLines))
583         SP->printSourceLine(OS, Address, ObjectFilename, LVP, "");
584       printLead(Bytes, Address.Address, OS);
585       OS << Preamble;
586       Preamble = "   ";
587       StringRef Inst;
588       auto Duplex = HeadTail.first.split('\v');
589       if (!Duplex.second.empty()) {
590         OS << Duplex.first;
591         OS << "; ";
592         Inst = Duplex.second;
593       }
594       else
595         Inst = HeadTail.first;
596       OS << Inst;
597       HeadTail = HeadTail.second.split('\n');
598       if (HeadTail.first.empty())
599         OS << " } " << PacketBundle.second;
600       PrintReloc();
601       Bytes = Bytes.slice(4);
602       Address.Address += 4;
603     }
604   }
605 };
606 HexagonPrettyPrinter HexagonPrettyPrinterInst;
607 
608 class AMDGCNPrettyPrinter : public PrettyPrinter {
609 public:
610   void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
611                  object::SectionedAddress Address, formatted_raw_ostream &OS,
612                  StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
613                  StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
614                  LiveVariablePrinter &LVP) override {
615     if (SP && (PrintSource || PrintLines))
616       SP->printSourceLine(OS, Address, ObjectFilename, LVP);
617 
618     if (MI) {
619       SmallString<40> InstStr;
620       raw_svector_ostream IS(InstStr);
621 
622       IP.printInst(MI, Address.Address, "", STI, IS);
623 
624       OS << left_justify(IS.str(), 60);
625     } else {
626       // an unrecognized encoding - this is probably data so represent it
627       // using the .long directive, or .byte directive if fewer than 4 bytes
628       // remaining
629       if (Bytes.size() >= 4) {
630         OS << format("\t.long 0x%08" PRIx32 " ",
631                      support::endian::read32<support::little>(Bytes.data()));
632         OS.indent(42);
633       } else {
634           OS << format("\t.byte 0x%02" PRIx8, Bytes[0]);
635           for (unsigned int i = 1; i < Bytes.size(); i++)
636             OS << format(", 0x%02" PRIx8, Bytes[i]);
637           OS.indent(55 - (6 * Bytes.size()));
638       }
639     }
640 
641     OS << format("// %012" PRIX64 ":", Address.Address);
642     if (Bytes.size() >= 4) {
643       // D should be casted to uint32_t here as it is passed by format to
644       // snprintf as vararg.
645       for (uint32_t D : makeArrayRef(
646                reinterpret_cast<const support::little32_t *>(Bytes.data()),
647                Bytes.size() / 4))
648         OS << format(" %08" PRIX32, D);
649     } else {
650       for (unsigned char B : Bytes)
651         OS << format(" %02" PRIX8, B);
652     }
653 
654     if (!Annot.empty())
655       OS << " // " << Annot;
656   }
657 };
658 AMDGCNPrettyPrinter AMDGCNPrettyPrinterInst;
659 
660 class BPFPrettyPrinter : public PrettyPrinter {
661 public:
662   void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
663                  object::SectionedAddress Address, formatted_raw_ostream &OS,
664                  StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
665                  StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
666                  LiveVariablePrinter &LVP) override {
667     if (SP && (PrintSource || PrintLines))
668       SP->printSourceLine(OS, Address, ObjectFilename, LVP);
669     if (LeadingAddr)
670       OS << format("%8" PRId64 ":", Address.Address / 8);
671     if (ShowRawInsn) {
672       OS << "\t";
673       dumpBytes(Bytes, OS);
674     }
675     if (MI)
676       IP.printInst(MI, Address.Address, "", STI, OS);
677     else
678       OS << "\t<unknown>";
679   }
680 };
681 BPFPrettyPrinter BPFPrettyPrinterInst;
682 
683 class ARMPrettyPrinter : public PrettyPrinter {
684 public:
685   void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
686                  object::SectionedAddress Address, formatted_raw_ostream &OS,
687                  StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
688                  StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
689                  LiveVariablePrinter &LVP) override {
690     if (SP && (PrintSource || PrintLines))
691       SP->printSourceLine(OS, Address, ObjectFilename, LVP);
692     LVP.printBetweenInsts(OS, false);
693 
694     size_t Start = OS.tell();
695     if (LeadingAddr)
696       OS << format("%8" PRIx64 ":", Address.Address);
697     if (ShowRawInsn) {
698       size_t Pos = 0, End = Bytes.size();
699       if (STI.checkFeatures("+thumb-mode")) {
700         for (; Pos + 2 <= End; Pos += 2)
701           OS << ' '
702              << format_hex_no_prefix(
703                     llvm::support::endian::read<uint16_t>(
704                         Bytes.data() + Pos, InstructionEndianness),
705                     4);
706       } else {
707         for (; Pos + 4 <= End; Pos += 4)
708           OS << ' '
709              << format_hex_no_prefix(
710                     llvm::support::endian::read<uint32_t>(
711                         Bytes.data() + Pos, InstructionEndianness),
712                     8);
713       }
714       if (Pos < End) {
715         OS << ' ';
716         dumpBytes(Bytes.slice(Pos), OS);
717       }
718     }
719 
720     AlignToInstStartColumn(Start, STI, OS);
721 
722     if (MI) {
723       IP.printInst(MI, Address.Address, "", STI, OS);
724     } else
725       OS << "\t<unknown>";
726   }
727 
728   void setInstructionEndianness(llvm::support::endianness Endianness) {
729     InstructionEndianness = Endianness;
730   }
731 
732 private:
733   llvm::support::endianness InstructionEndianness = llvm::support::little;
734 };
735 ARMPrettyPrinter ARMPrettyPrinterInst;
736 
737 class AArch64PrettyPrinter : public PrettyPrinter {
738 public:
739   void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
740                  object::SectionedAddress Address, formatted_raw_ostream &OS,
741                  StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
742                  StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
743                  LiveVariablePrinter &LVP) override {
744     if (SP && (PrintSource || PrintLines))
745       SP->printSourceLine(OS, Address, ObjectFilename, LVP);
746     LVP.printBetweenInsts(OS, false);
747 
748     size_t Start = OS.tell();
749     if (LeadingAddr)
750       OS << format("%8" PRIx64 ":", Address.Address);
751     if (ShowRawInsn) {
752       size_t Pos = 0, End = Bytes.size();
753       for (; Pos + 4 <= End; Pos += 4)
754         OS << ' '
755            << format_hex_no_prefix(
756                   llvm::support::endian::read<uint32_t>(Bytes.data() + Pos,
757                                                         llvm::support::little),
758                   8);
759       if (Pos < End) {
760         OS << ' ';
761         dumpBytes(Bytes.slice(Pos), OS);
762       }
763     }
764 
765     AlignToInstStartColumn(Start, STI, OS);
766 
767     if (MI) {
768       IP.printInst(MI, Address.Address, "", STI, OS);
769     } else
770       OS << "\t<unknown>";
771   }
772 };
773 AArch64PrettyPrinter AArch64PrettyPrinterInst;
774 
775 PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
776   switch(Triple.getArch()) {
777   default:
778     return PrettyPrinterInst;
779   case Triple::hexagon:
780     return HexagonPrettyPrinterInst;
781   case Triple::amdgcn:
782     return AMDGCNPrettyPrinterInst;
783   case Triple::bpfel:
784   case Triple::bpfeb:
785     return BPFPrettyPrinterInst;
786   case Triple::arm:
787   case Triple::armeb:
788   case Triple::thumb:
789   case Triple::thumbeb:
790     return ARMPrettyPrinterInst;
791   case Triple::aarch64:
792   case Triple::aarch64_be:
793   case Triple::aarch64_32:
794     return AArch64PrettyPrinterInst;
795   }
796 }
797 }
798 
799 static uint8_t getElfSymbolType(const ObjectFile &Obj, const SymbolRef &Sym) {
800   assert(Obj.isELF());
801   if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(&Obj))
802     return unwrapOrError(Elf32LEObj->getSymbol(Sym.getRawDataRefImpl()),
803                          Obj.getFileName())
804         ->getType();
805   if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(&Obj))
806     return unwrapOrError(Elf64LEObj->getSymbol(Sym.getRawDataRefImpl()),
807                          Obj.getFileName())
808         ->getType();
809   if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(&Obj))
810     return unwrapOrError(Elf32BEObj->getSymbol(Sym.getRawDataRefImpl()),
811                          Obj.getFileName())
812         ->getType();
813   if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(&Obj))
814     return unwrapOrError(Elf64BEObj->getSymbol(Sym.getRawDataRefImpl()),
815                          Obj.getFileName())
816         ->getType();
817   llvm_unreachable("Unsupported binary format");
818 }
819 
820 template <class ELFT>
821 static void
822 addDynamicElfSymbols(const ELFObjectFile<ELFT> &Obj,
823                      std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
824   for (auto Symbol : Obj.getDynamicSymbolIterators()) {
825     uint8_t SymbolType = Symbol.getELFType();
826     if (SymbolType == ELF::STT_SECTION)
827       continue;
828 
829     uint64_t Address = unwrapOrError(Symbol.getAddress(), Obj.getFileName());
830     // ELFSymbolRef::getAddress() returns size instead of value for common
831     // symbols which is not desirable for disassembly output. Overriding.
832     if (SymbolType == ELF::STT_COMMON)
833       Address = unwrapOrError(Obj.getSymbol(Symbol.getRawDataRefImpl()),
834                               Obj.getFileName())
835                     ->st_value;
836 
837     StringRef Name = unwrapOrError(Symbol.getName(), Obj.getFileName());
838     if (Name.empty())
839       continue;
840 
841     section_iterator SecI =
842         unwrapOrError(Symbol.getSection(), Obj.getFileName());
843     if (SecI == Obj.section_end())
844       continue;
845 
846     AllSymbols[*SecI].emplace_back(Address, Name, SymbolType);
847   }
848 }
849 
850 static void
851 addDynamicElfSymbols(const ELFObjectFileBase &Obj,
852                      std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
853   if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(&Obj))
854     addDynamicElfSymbols(*Elf32LEObj, AllSymbols);
855   else if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(&Obj))
856     addDynamicElfSymbols(*Elf64LEObj, AllSymbols);
857   else if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(&Obj))
858     addDynamicElfSymbols(*Elf32BEObj, AllSymbols);
859   else if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(&Obj))
860     addDynamicElfSymbols(*Elf64BEObj, AllSymbols);
861   else
862     llvm_unreachable("Unsupported binary format");
863 }
864 
865 static Optional<SectionRef> getWasmCodeSection(const WasmObjectFile &Obj) {
866   for (auto SecI : Obj.sections()) {
867     const WasmSection &Section = Obj.getWasmSection(SecI);
868     if (Section.Type == wasm::WASM_SEC_CODE)
869       return SecI;
870   }
871   return None;
872 }
873 
874 static void
875 addMissingWasmCodeSymbols(const WasmObjectFile &Obj,
876                           std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
877   Optional<SectionRef> Section = getWasmCodeSection(Obj);
878   if (!Section)
879     return;
880   SectionSymbolsTy &Symbols = AllSymbols[*Section];
881 
882   std::set<uint64_t> SymbolAddresses;
883   for (const auto &Sym : Symbols)
884     SymbolAddresses.insert(Sym.Addr);
885 
886   for (const wasm::WasmFunction &Function : Obj.functions()) {
887     uint64_t Address = Function.CodeSectionOffset;
888     // Only add fallback symbols for functions not already present in the symbol
889     // table.
890     if (SymbolAddresses.count(Address))
891       continue;
892     // This function has no symbol, so it should have no SymbolName.
893     assert(Function.SymbolName.empty());
894     // We use DebugName for the name, though it may be empty if there is no
895     // "name" custom section, or that section is missing a name for this
896     // function.
897     StringRef Name = Function.DebugName;
898     Symbols.emplace_back(Address, Name, ELF::STT_NOTYPE);
899   }
900 }
901 
902 static void addPltEntries(const ObjectFile &Obj,
903                           std::map<SectionRef, SectionSymbolsTy> &AllSymbols,
904                           StringSaver &Saver) {
905   Optional<SectionRef> Plt;
906   for (const SectionRef &Section : Obj.sections()) {
907     Expected<StringRef> SecNameOrErr = Section.getName();
908     if (!SecNameOrErr) {
909       consumeError(SecNameOrErr.takeError());
910       continue;
911     }
912     if (*SecNameOrErr == ".plt")
913       Plt = Section;
914   }
915   if (!Plt)
916     return;
917   if (auto *ElfObj = dyn_cast<ELFObjectFileBase>(&Obj)) {
918     for (auto PltEntry : ElfObj->getPltAddresses()) {
919       if (PltEntry.first) {
920         SymbolRef Symbol(*PltEntry.first, ElfObj);
921         uint8_t SymbolType = getElfSymbolType(Obj, Symbol);
922         if (Expected<StringRef> NameOrErr = Symbol.getName()) {
923           if (!NameOrErr->empty())
924             AllSymbols[*Plt].emplace_back(
925                 PltEntry.second, Saver.save((*NameOrErr + "@plt").str()),
926                 SymbolType);
927           continue;
928         } else {
929           // The warning has been reported in disassembleObject().
930           consumeError(NameOrErr.takeError());
931         }
932       }
933       reportWarning("PLT entry at 0x" + Twine::utohexstr(PltEntry.second) +
934                         " references an invalid symbol",
935                     Obj.getFileName());
936     }
937   }
938 }
939 
940 // Normally the disassembly output will skip blocks of zeroes. This function
941 // returns the number of zero bytes that can be skipped when dumping the
942 // disassembly of the instructions in Buf.
943 static size_t countSkippableZeroBytes(ArrayRef<uint8_t> Buf) {
944   // Find the number of leading zeroes.
945   size_t N = 0;
946   while (N < Buf.size() && !Buf[N])
947     ++N;
948 
949   // We may want to skip blocks of zero bytes, but unless we see
950   // at least 8 of them in a row.
951   if (N < 8)
952     return 0;
953 
954   // We skip zeroes in multiples of 4 because do not want to truncate an
955   // instruction if it starts with a zero byte.
956   return N & ~0x3;
957 }
958 
959 // Returns a map from sections to their relocations.
960 static std::map<SectionRef, std::vector<RelocationRef>>
961 getRelocsMap(object::ObjectFile const &Obj) {
962   std::map<SectionRef, std::vector<RelocationRef>> Ret;
963   uint64_t I = (uint64_t)-1;
964   for (SectionRef Sec : Obj.sections()) {
965     ++I;
966     Expected<section_iterator> RelocatedOrErr = Sec.getRelocatedSection();
967     if (!RelocatedOrErr)
968       reportError(Obj.getFileName(),
969                   "section (" + Twine(I) +
970                       "): failed to get a relocated section: " +
971                       toString(RelocatedOrErr.takeError()));
972 
973     section_iterator Relocated = *RelocatedOrErr;
974     if (Relocated == Obj.section_end() || !checkSectionFilter(*Relocated).Keep)
975       continue;
976     std::vector<RelocationRef> &V = Ret[*Relocated];
977     append_range(V, Sec.relocations());
978     // Sort relocations by address.
979     llvm::stable_sort(V, isRelocAddressLess);
980   }
981   return Ret;
982 }
983 
984 // Used for --adjust-vma to check if address should be adjusted by the
985 // specified value for a given section.
986 // For ELF we do not adjust non-allocatable sections like debug ones,
987 // because they are not loadable.
988 // TODO: implement for other file formats.
989 static bool shouldAdjustVA(const SectionRef &Section) {
990   const ObjectFile *Obj = Section.getObject();
991   if (Obj->isELF())
992     return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC;
993   return false;
994 }
995 
996 
997 typedef std::pair<uint64_t, char> MappingSymbolPair;
998 static char getMappingSymbolKind(ArrayRef<MappingSymbolPair> MappingSymbols,
999                                  uint64_t Address) {
1000   auto It =
1001       partition_point(MappingSymbols, [Address](const MappingSymbolPair &Val) {
1002         return Val.first <= Address;
1003       });
1004   // Return zero for any address before the first mapping symbol; this means
1005   // we should use the default disassembly mode, depending on the target.
1006   if (It == MappingSymbols.begin())
1007     return '\x00';
1008   return (It - 1)->second;
1009 }
1010 
1011 static uint64_t dumpARMELFData(uint64_t SectionAddr, uint64_t Index,
1012                                uint64_t End, const ObjectFile &Obj,
1013                                ArrayRef<uint8_t> Bytes,
1014                                ArrayRef<MappingSymbolPair> MappingSymbols,
1015                                const MCSubtargetInfo &STI, raw_ostream &OS) {
1016   support::endianness Endian =
1017       Obj.isLittleEndian() ? support::little : support::big;
1018   size_t Start = OS.tell();
1019   OS << format("%8" PRIx64 ": ", SectionAddr + Index);
1020   if (Index + 4 <= End) {
1021     dumpBytes(Bytes.slice(Index, 4), OS);
1022     AlignToInstStartColumn(Start, STI, OS);
1023     OS << "\t.word\t"
1024            << format_hex(support::endian::read32(Bytes.data() + Index, Endian),
1025                          10);
1026     return 4;
1027   }
1028   if (Index + 2 <= End) {
1029     dumpBytes(Bytes.slice(Index, 2), OS);
1030     AlignToInstStartColumn(Start, STI, OS);
1031     OS << "\t.short\t"
1032        << format_hex(support::endian::read16(Bytes.data() + Index, Endian), 6);
1033     return 2;
1034   }
1035   dumpBytes(Bytes.slice(Index, 1), OS);
1036   AlignToInstStartColumn(Start, STI, OS);
1037   OS << "\t.byte\t" << format_hex(Bytes[Index], 4);
1038   return 1;
1039 }
1040 
1041 static void dumpELFData(uint64_t SectionAddr, uint64_t Index, uint64_t End,
1042                         ArrayRef<uint8_t> Bytes) {
1043   // print out data up to 8 bytes at a time in hex and ascii
1044   uint8_t AsciiData[9] = {'\0'};
1045   uint8_t Byte;
1046   int NumBytes = 0;
1047 
1048   for (; Index < End; ++Index) {
1049     if (NumBytes == 0)
1050       outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1051     Byte = Bytes.slice(Index)[0];
1052     outs() << format(" %02x", Byte);
1053     AsciiData[NumBytes] = isPrint(Byte) ? Byte : '.';
1054 
1055     uint8_t IndentOffset = 0;
1056     NumBytes++;
1057     if (Index == End - 1 || NumBytes > 8) {
1058       // Indent the space for less than 8 bytes data.
1059       // 2 spaces for byte and one for space between bytes
1060       IndentOffset = 3 * (8 - NumBytes);
1061       for (int Excess = NumBytes; Excess < 8; Excess++)
1062         AsciiData[Excess] = '\0';
1063       NumBytes = 8;
1064     }
1065     if (NumBytes == 8) {
1066       AsciiData[8] = '\0';
1067       outs() << std::string(IndentOffset, ' ') << "         ";
1068       outs() << reinterpret_cast<char *>(AsciiData);
1069       outs() << '\n';
1070       NumBytes = 0;
1071     }
1072   }
1073 }
1074 
1075 SymbolInfoTy objdump::createSymbolInfo(const ObjectFile &Obj,
1076                                        const SymbolRef &Symbol) {
1077   const StringRef FileName = Obj.getFileName();
1078   const uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
1079   const StringRef Name = unwrapOrError(Symbol.getName(), FileName);
1080 
1081   if (Obj.isXCOFF() && SymbolDescription) {
1082     const auto &XCOFFObj = cast<XCOFFObjectFile>(Obj);
1083     DataRefImpl SymbolDRI = Symbol.getRawDataRefImpl();
1084 
1085     const uint32_t SymbolIndex = XCOFFObj.getSymbolIndex(SymbolDRI.p);
1086     Optional<XCOFF::StorageMappingClass> Smc =
1087         getXCOFFSymbolCsectSMC(XCOFFObj, Symbol);
1088     return SymbolInfoTy(Addr, Name, Smc, SymbolIndex,
1089                         isLabel(XCOFFObj, Symbol));
1090   } else if (Obj.isXCOFF()) {
1091     const SymbolRef::Type SymType = unwrapOrError(Symbol.getType(), FileName);
1092     return SymbolInfoTy(Addr, Name, SymType, true);
1093   } else
1094     return SymbolInfoTy(Addr, Name,
1095                         Obj.isELF() ? getElfSymbolType(Obj, Symbol)
1096                                     : (uint8_t)ELF::STT_NOTYPE);
1097 }
1098 
1099 static SymbolInfoTy createDummySymbolInfo(const ObjectFile &Obj,
1100                                           const uint64_t Addr, StringRef &Name,
1101                                           uint8_t Type) {
1102   if (Obj.isXCOFF() && SymbolDescription)
1103     return SymbolInfoTy(Addr, Name, None, None, false);
1104   else
1105     return SymbolInfoTy(Addr, Name, Type);
1106 }
1107 
1108 static void
1109 collectBBAddrMapLabels(const std::unordered_map<uint64_t, BBAddrMap> &AddrToBBAddrMap,
1110                        uint64_t SectionAddr, uint64_t Start, uint64_t End,
1111                        std::unordered_map<uint64_t, std::vector<std::string>> &Labels) {
1112   if (AddrToBBAddrMap.empty())
1113     return;
1114   Labels.clear();
1115   uint64_t StartAddress = SectionAddr + Start;
1116   uint64_t EndAddress = SectionAddr + End;
1117   auto Iter = AddrToBBAddrMap.find(StartAddress);
1118   if (Iter == AddrToBBAddrMap.end())
1119     return;
1120   for (unsigned I = 0, Size = Iter->second.BBEntries.size(); I < Size; ++I) {
1121     uint64_t BBAddress = Iter->second.BBEntries[I].Offset + Iter->second.Addr;
1122     if (BBAddress >= EndAddress)
1123       continue;
1124     Labels[BBAddress].push_back(("BB" + Twine(I)).str());
1125   }
1126 }
1127 
1128 static void collectLocalBranchTargets(
1129     ArrayRef<uint8_t> Bytes, const MCInstrAnalysis *MIA, MCDisassembler *DisAsm,
1130     MCInstPrinter *IP, const MCSubtargetInfo *STI, uint64_t SectionAddr,
1131     uint64_t Start, uint64_t End, std::unordered_map<uint64_t, std::string> &Labels) {
1132   // So far only supports PowerPC and X86.
1133   if (!STI->getTargetTriple().isPPC() && !STI->getTargetTriple().isX86())
1134     return;
1135 
1136   Labels.clear();
1137   unsigned LabelCount = 0;
1138   Start += SectionAddr;
1139   End += SectionAddr;
1140   uint64_t Index = Start;
1141   while (Index < End) {
1142     // Disassemble a real instruction and record function-local branch labels.
1143     MCInst Inst;
1144     uint64_t Size;
1145     ArrayRef<uint8_t> ThisBytes = Bytes.slice(Index - SectionAddr);
1146     bool Disassembled =
1147         DisAsm->getInstruction(Inst, Size, ThisBytes, Index, nulls());
1148     if (Size == 0)
1149       Size = std::min<uint64_t>(ThisBytes.size(),
1150                                 DisAsm->suggestBytesToSkip(ThisBytes, Index));
1151 
1152     if (Disassembled && MIA) {
1153       uint64_t Target;
1154       bool TargetKnown = MIA->evaluateBranch(Inst, Index, Size, Target);
1155       // On PowerPC, if the address of a branch is the same as the target, it
1156       // means that it's a function call. Do not mark the label for this case.
1157       if (TargetKnown && (Target >= Start && Target < End) &&
1158           !Labels.count(Target) &&
1159           !(STI->getTargetTriple().isPPC() && Target == Index))
1160         Labels[Target] = ("L" + Twine(LabelCount++)).str();
1161     }
1162     Index += Size;
1163   }
1164 }
1165 
1166 // Create an MCSymbolizer for the target and add it to the MCDisassembler.
1167 // This is currently only used on AMDGPU, and assumes the format of the
1168 // void * argument passed to AMDGPU's createMCSymbolizer.
1169 static void addSymbolizer(
1170     MCContext &Ctx, const Target *Target, StringRef TripleName,
1171     MCDisassembler *DisAsm, uint64_t SectionAddr, ArrayRef<uint8_t> Bytes,
1172     SectionSymbolsTy &Symbols,
1173     std::vector<std::unique_ptr<std::string>> &SynthesizedLabelNames) {
1174 
1175   std::unique_ptr<MCRelocationInfo> RelInfo(
1176       Target->createMCRelocationInfo(TripleName, Ctx));
1177   if (!RelInfo)
1178     return;
1179   std::unique_ptr<MCSymbolizer> Symbolizer(Target->createMCSymbolizer(
1180       TripleName, nullptr, nullptr, &Symbols, &Ctx, std::move(RelInfo)));
1181   MCSymbolizer *SymbolizerPtr = &*Symbolizer;
1182   DisAsm->setSymbolizer(std::move(Symbolizer));
1183 
1184   if (!SymbolizeOperands)
1185     return;
1186 
1187   // Synthesize labels referenced by branch instructions by
1188   // disassembling, discarding the output, and collecting the referenced
1189   // addresses from the symbolizer.
1190   for (size_t Index = 0; Index != Bytes.size();) {
1191     MCInst Inst;
1192     uint64_t Size;
1193     ArrayRef<uint8_t> ThisBytes = Bytes.slice(Index - SectionAddr);
1194     DisAsm->getInstruction(Inst, Size, ThisBytes, Index, nulls());
1195     if (Size == 0)
1196       Size = std::min<uint64_t>(ThisBytes.size(),
1197                                 DisAsm->suggestBytesToSkip(ThisBytes, Index));
1198     Index += Size;
1199   }
1200   ArrayRef<uint64_t> LabelAddrsRef = SymbolizerPtr->getReferencedAddresses();
1201   // Copy and sort to remove duplicates.
1202   std::vector<uint64_t> LabelAddrs;
1203   LabelAddrs.insert(LabelAddrs.end(), LabelAddrsRef.begin(),
1204                     LabelAddrsRef.end());
1205   llvm::sort(LabelAddrs);
1206   LabelAddrs.resize(std::unique(LabelAddrs.begin(), LabelAddrs.end()) -
1207                     LabelAddrs.begin());
1208   // Add the labels.
1209   for (unsigned LabelNum = 0; LabelNum != LabelAddrs.size(); ++LabelNum) {
1210     auto Name = std::make_unique<std::string>();
1211     *Name = (Twine("L") + Twine(LabelNum)).str();
1212     SynthesizedLabelNames.push_back(std::move(Name));
1213     Symbols.push_back(SymbolInfoTy(
1214         LabelAddrs[LabelNum], *SynthesizedLabelNames.back(), ELF::STT_NOTYPE));
1215   }
1216   llvm::stable_sort(Symbols);
1217   // Recreate the symbolizer with the new symbols list.
1218   RelInfo.reset(Target->createMCRelocationInfo(TripleName, Ctx));
1219   Symbolizer.reset(Target->createMCSymbolizer(
1220       TripleName, nullptr, nullptr, &Symbols, &Ctx, std::move(RelInfo)));
1221   DisAsm->setSymbolizer(std::move(Symbolizer));
1222 }
1223 
1224 static StringRef getSegmentName(const MachOObjectFile *MachO,
1225                                 const SectionRef &Section) {
1226   if (MachO) {
1227     DataRefImpl DR = Section.getRawDataRefImpl();
1228     StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
1229     return SegmentName;
1230   }
1231   return "";
1232 }
1233 
1234 static void emitPostInstructionInfo(formatted_raw_ostream &FOS,
1235                                     const MCAsmInfo &MAI,
1236                                     const MCSubtargetInfo &STI,
1237                                     StringRef Comments,
1238                                     LiveVariablePrinter &LVP) {
1239   do {
1240     if (!Comments.empty()) {
1241       // Emit a line of comments.
1242       StringRef Comment;
1243       std::tie(Comment, Comments) = Comments.split('\n');
1244       // MAI.getCommentColumn() assumes that instructions are printed at the
1245       // position of 8, while getInstStartColumn() returns the actual position.
1246       unsigned CommentColumn =
1247           MAI.getCommentColumn() - 8 + getInstStartColumn(STI);
1248       FOS.PadToColumn(CommentColumn);
1249       FOS << MAI.getCommentString() << ' ' << Comment;
1250     }
1251     LVP.printAfterInst(FOS);
1252     FOS << '\n';
1253   } while (!Comments.empty());
1254   FOS.flush();
1255 }
1256 
1257 static void createFakeELFSections(ObjectFile &Obj) {
1258   assert(Obj.isELF());
1259   if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(&Obj))
1260     Elf32LEObj->createFakeSections();
1261   else if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(&Obj))
1262     Elf64LEObj->createFakeSections();
1263   else if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(&Obj))
1264     Elf32BEObj->createFakeSections();
1265   else if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(&Obj))
1266     Elf64BEObj->createFakeSections();
1267   else
1268     llvm_unreachable("Unsupported binary format");
1269 }
1270 
1271 // Tries to fetch a more complete version of the given object file using its
1272 // Build ID. Returns None if nothing was found.
1273 static Optional<OwningBinary<Binary>>
1274 fetchBinaryByBuildID(const ObjectFile &Obj) {
1275   Optional<object::BuildIDRef> BuildID = getBuildID(&Obj);
1276   if (!BuildID)
1277     return None;
1278   Optional<std::string> Path = BIDFetcher->fetch(*BuildID);
1279   if (!Path)
1280     return None;
1281   Expected<OwningBinary<Binary>> DebugBinary = createBinary(*Path);
1282   if (!DebugBinary) {
1283     reportWarning(toString(DebugBinary.takeError()), *Path);
1284     return None;
1285   }
1286   return std::move(*DebugBinary);
1287 }
1288 
1289 static void disassembleObject(const Target *TheTarget, ObjectFile &Obj,
1290                               MCContext &Ctx, MCDisassembler *PrimaryDisAsm,
1291                               MCDisassembler *SecondaryDisAsm,
1292                               const MCInstrAnalysis *MIA, MCInstPrinter *IP,
1293                               const MCSubtargetInfo *PrimarySTI,
1294                               const MCSubtargetInfo *SecondarySTI,
1295                               PrettyPrinter &PIP, SourcePrinter &SP,
1296                               bool InlineRelocs) {
1297   const MCSubtargetInfo *STI = PrimarySTI;
1298   MCDisassembler *DisAsm = PrimaryDisAsm;
1299   bool PrimaryIsThumb = false;
1300   if (isArmElf(Obj))
1301     PrimaryIsThumb = STI->checkFeatures("+thumb-mode");
1302 
1303   std::map<SectionRef, std::vector<RelocationRef>> RelocMap;
1304   if (InlineRelocs)
1305     RelocMap = getRelocsMap(Obj);
1306   bool Is64Bits = Obj.getBytesInAddress() > 4;
1307 
1308   // Create a mapping from virtual address to symbol name.  This is used to
1309   // pretty print the symbols while disassembling.
1310   std::map<SectionRef, SectionSymbolsTy> AllSymbols;
1311   SectionSymbolsTy AbsoluteSymbols;
1312   const StringRef FileName = Obj.getFileName();
1313   const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(&Obj);
1314   for (const SymbolRef &Symbol : Obj.symbols()) {
1315     Expected<StringRef> NameOrErr = Symbol.getName();
1316     if (!NameOrErr) {
1317       reportWarning(toString(NameOrErr.takeError()), FileName);
1318       continue;
1319     }
1320     if (NameOrErr->empty() && !(Obj.isXCOFF() && SymbolDescription))
1321       continue;
1322 
1323     if (Obj.isELF() && getElfSymbolType(Obj, Symbol) == ELF::STT_SECTION)
1324       continue;
1325 
1326     if (MachO) {
1327       // __mh_(execute|dylib|dylinker|bundle|preload|object)_header are special
1328       // symbols that support MachO header introspection. They do not bind to
1329       // code locations and are irrelevant for disassembly.
1330       if (NameOrErr->startswith("__mh_") && NameOrErr->endswith("_header"))
1331         continue;
1332       // Don't ask a Mach-O STAB symbol for its section unless you know that
1333       // STAB symbol's section field refers to a valid section index. Otherwise
1334       // the symbol may error trying to load a section that does not exist.
1335       DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
1336       uint8_t NType = (MachO->is64Bit() ?
1337                        MachO->getSymbol64TableEntry(SymDRI).n_type:
1338                        MachO->getSymbolTableEntry(SymDRI).n_type);
1339       if (NType & MachO::N_STAB)
1340         continue;
1341     }
1342 
1343     section_iterator SecI = unwrapOrError(Symbol.getSection(), FileName);
1344     if (SecI != Obj.section_end())
1345       AllSymbols[*SecI].push_back(createSymbolInfo(Obj, Symbol));
1346     else
1347       AbsoluteSymbols.push_back(createSymbolInfo(Obj, Symbol));
1348   }
1349 
1350   if (AllSymbols.empty() && Obj.isELF())
1351     addDynamicElfSymbols(cast<ELFObjectFileBase>(Obj), AllSymbols);
1352 
1353   if (Obj.isWasm())
1354     addMissingWasmCodeSymbols(cast<WasmObjectFile>(Obj), AllSymbols);
1355 
1356   if (Obj.isELF() && Obj.sections().empty())
1357     createFakeELFSections(Obj);
1358 
1359   BumpPtrAllocator A;
1360   StringSaver Saver(A);
1361   addPltEntries(Obj, AllSymbols, Saver);
1362 
1363   // Create a mapping from virtual address to section. An empty section can
1364   // cause more than one section at the same address. Sort such sections to be
1365   // before same-addressed non-empty sections so that symbol lookups prefer the
1366   // non-empty section.
1367   std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
1368   for (SectionRef Sec : Obj.sections())
1369     SectionAddresses.emplace_back(Sec.getAddress(), Sec);
1370   llvm::stable_sort(SectionAddresses, [](const auto &LHS, const auto &RHS) {
1371     if (LHS.first != RHS.first)
1372       return LHS.first < RHS.first;
1373     return LHS.second.getSize() < RHS.second.getSize();
1374   });
1375 
1376   // Linked executables (.exe and .dll files) typically don't include a real
1377   // symbol table but they might contain an export table.
1378   if (const auto *COFFObj = dyn_cast<COFFObjectFile>(&Obj)) {
1379     for (const auto &ExportEntry : COFFObj->export_directories()) {
1380       StringRef Name;
1381       if (Error E = ExportEntry.getSymbolName(Name))
1382         reportError(std::move(E), Obj.getFileName());
1383       if (Name.empty())
1384         continue;
1385 
1386       uint32_t RVA;
1387       if (Error E = ExportEntry.getExportRVA(RVA))
1388         reportError(std::move(E), Obj.getFileName());
1389 
1390       uint64_t VA = COFFObj->getImageBase() + RVA;
1391       auto Sec = partition_point(
1392           SectionAddresses, [VA](const std::pair<uint64_t, SectionRef> &O) {
1393             return O.first <= VA;
1394           });
1395       if (Sec != SectionAddresses.begin()) {
1396         --Sec;
1397         AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE);
1398       } else
1399         AbsoluteSymbols.emplace_back(VA, Name, ELF::STT_NOTYPE);
1400     }
1401   }
1402 
1403   // Sort all the symbols, this allows us to use a simple binary search to find
1404   // Multiple symbols can have the same address. Use a stable sort to stabilize
1405   // the output.
1406   StringSet<> FoundDisasmSymbolSet;
1407   for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
1408     llvm::stable_sort(SecSyms.second);
1409   llvm::stable_sort(AbsoluteSymbols);
1410 
1411   std::unique_ptr<DWARFContext> DICtx;
1412   LiveVariablePrinter LVP(*Ctx.getRegisterInfo(), *STI);
1413 
1414   if (DbgVariables != DVDisabled) {
1415     DICtx = DWARFContext::create(Obj);
1416     for (const std::unique_ptr<DWARFUnit> &CU : DICtx->compile_units())
1417       LVP.addCompileUnit(CU->getUnitDIE(false));
1418   }
1419 
1420   LLVM_DEBUG(LVP.dump());
1421 
1422   std::unordered_map<uint64_t, BBAddrMap> AddrToBBAddrMap;
1423   auto ReadBBAddrMap = [&](Optional<unsigned> SectionIndex = None) {
1424     AddrToBBAddrMap.clear();
1425     if (const auto *Elf = dyn_cast<ELFObjectFileBase>(&Obj)) {
1426       auto BBAddrMapsOrErr = Elf->readBBAddrMap(SectionIndex);
1427       if (!BBAddrMapsOrErr)
1428           reportWarning(toString(BBAddrMapsOrErr.takeError()),
1429                         Obj.getFileName());
1430       for (auto &FunctionBBAddrMap : *BBAddrMapsOrErr)
1431         AddrToBBAddrMap.emplace(FunctionBBAddrMap.Addr,
1432                                 std::move(FunctionBBAddrMap));
1433     }
1434   };
1435 
1436   // For non-relocatable objects, Read all LLVM_BB_ADDR_MAP sections into a
1437   // single mapping, since they don't have any conflicts.
1438   if (SymbolizeOperands && !Obj.isRelocatableObject())
1439     ReadBBAddrMap();
1440 
1441   for (const SectionRef &Section : ToolSectionFilter(Obj)) {
1442     if (FilterSections.empty() && !DisassembleAll &&
1443         (!Section.isText() || Section.isVirtual()))
1444       continue;
1445 
1446     uint64_t SectionAddr = Section.getAddress();
1447     uint64_t SectSize = Section.getSize();
1448     if (!SectSize)
1449       continue;
1450 
1451     // For relocatable object files, read the LLVM_BB_ADDR_MAP section
1452     // corresponding to this section, if present.
1453     if (SymbolizeOperands && Obj.isRelocatableObject())
1454       ReadBBAddrMap(Section.getIndex());
1455 
1456     // Get the list of all the symbols in this section.
1457     SectionSymbolsTy &Symbols = AllSymbols[Section];
1458     std::vector<MappingSymbolPair> MappingSymbols;
1459     if (hasMappingSymbols(Obj)) {
1460       for (const auto &Symb : Symbols) {
1461         uint64_t Address = Symb.Addr;
1462         StringRef Name = Symb.Name;
1463         if (Name.startswith("$d"))
1464           MappingSymbols.emplace_back(Address - SectionAddr, 'd');
1465         if (Name.startswith("$x"))
1466           MappingSymbols.emplace_back(Address - SectionAddr, 'x');
1467         if (Name.startswith("$a"))
1468           MappingSymbols.emplace_back(Address - SectionAddr, 'a');
1469         if (Name.startswith("$t"))
1470           MappingSymbols.emplace_back(Address - SectionAddr, 't');
1471       }
1472     }
1473 
1474     llvm::sort(MappingSymbols);
1475 
1476     ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(
1477         unwrapOrError(Section.getContents(), Obj.getFileName()));
1478 
1479     std::vector<std::unique_ptr<std::string>> SynthesizedLabelNames;
1480     if (Obj.isELF() && Obj.getArch() == Triple::amdgcn) {
1481       // AMDGPU disassembler uses symbolizer for printing labels
1482       addSymbolizer(Ctx, TheTarget, TripleName, DisAsm, SectionAddr, Bytes,
1483                     Symbols, SynthesizedLabelNames);
1484     }
1485 
1486     StringRef SegmentName = getSegmentName(MachO, Section);
1487     StringRef SectionName = unwrapOrError(Section.getName(), Obj.getFileName());
1488     // If the section has no symbol at the start, just insert a dummy one.
1489     if (Symbols.empty() || Symbols[0].Addr != 0) {
1490       Symbols.insert(Symbols.begin(),
1491                      createDummySymbolInfo(Obj, SectionAddr, SectionName,
1492                                            Section.isText() ? ELF::STT_FUNC
1493                                                             : ELF::STT_OBJECT));
1494     }
1495 
1496     SmallString<40> Comments;
1497     raw_svector_ostream CommentStream(Comments);
1498 
1499     uint64_t VMAAdjustment = 0;
1500     if (shouldAdjustVA(Section))
1501       VMAAdjustment = AdjustVMA;
1502 
1503     // In executable and shared objects, r_offset holds a virtual address.
1504     // Subtract SectionAddr from the r_offset field of a relocation to get
1505     // the section offset.
1506     uint64_t RelAdjustment = Obj.isRelocatableObject() ? 0 : SectionAddr;
1507     uint64_t Size;
1508     uint64_t Index;
1509     bool PrintedSection = false;
1510     std::vector<RelocationRef> Rels = RelocMap[Section];
1511     std::vector<RelocationRef>::const_iterator RelCur = Rels.begin();
1512     std::vector<RelocationRef>::const_iterator RelEnd = Rels.end();
1513 
1514     // Loop over each chunk of code between two points where at least
1515     // one symbol is defined.
1516     for (size_t SI = 0, SE = Symbols.size(); SI != SE;) {
1517       // Advance SI past all the symbols starting at the same address,
1518       // and make an ArrayRef of them.
1519       unsigned FirstSI = SI;
1520       uint64_t Start = Symbols[SI].Addr;
1521       ArrayRef<SymbolInfoTy> SymbolsHere;
1522       while (SI != SE && Symbols[SI].Addr == Start)
1523         ++SI;
1524       SymbolsHere = ArrayRef<SymbolInfoTy>(&Symbols[FirstSI], SI - FirstSI);
1525 
1526       // Get the demangled names of all those symbols. We end up with a vector
1527       // of StringRef that holds the names we're going to use, and a vector of
1528       // std::string that stores the new strings returned by demangle(), if
1529       // any. If we don't call demangle() then that vector can stay empty.
1530       std::vector<StringRef> SymNamesHere;
1531       std::vector<std::string> DemangledSymNamesHere;
1532       if (Demangle) {
1533         // Fetch the demangled names and store them locally.
1534         for (const SymbolInfoTy &Symbol : SymbolsHere)
1535           DemangledSymNamesHere.push_back(demangle(Symbol.Name.str()));
1536         // Now we've finished modifying that vector, it's safe to make
1537         // a vector of StringRefs pointing into it.
1538         SymNamesHere.insert(SymNamesHere.begin(), DemangledSymNamesHere.begin(),
1539                             DemangledSymNamesHere.end());
1540       } else {
1541         for (const SymbolInfoTy &Symbol : SymbolsHere)
1542           SymNamesHere.push_back(Symbol.Name);
1543       }
1544 
1545       // Distinguish ELF data from code symbols, which will be used later on to
1546       // decide whether to 'disassemble' this chunk as a data declaration via
1547       // dumpELFData(), or whether to treat it as code.
1548       //
1549       // If data _and_ code symbols are defined at the same address, the code
1550       // takes priority, on the grounds that disassembling code is our main
1551       // purpose here, and it would be a worse failure to _not_ interpret
1552       // something that _was_ meaningful as code than vice versa.
1553       //
1554       // Any ELF symbol type that is not clearly data will be regarded as code.
1555       // In particular, one of the uses of STT_NOTYPE is for branch targets
1556       // inside functions, for which STT_FUNC would be inaccurate.
1557       //
1558       // So here, we spot whether there's any non-data symbol present at all,
1559       // and only set the DisassembleAsData flag if there isn't. Also, we use
1560       // this distinction to inform the decision of which symbol to print at
1561       // the head of the section, so that if we're printing code, we print a
1562       // code-related symbol name to go with it.
1563       bool DisassembleAsData = false;
1564       size_t DisplaySymIndex = SymbolsHere.size() - 1;
1565       if (Obj.isELF() && !DisassembleAll && Section.isText()) {
1566         DisassembleAsData = true; // unless we find a code symbol below
1567 
1568         for (size_t i = 0; i < SymbolsHere.size(); ++i) {
1569           uint8_t SymTy = SymbolsHere[i].Type;
1570           if (SymTy != ELF::STT_OBJECT && SymTy != ELF::STT_COMMON) {
1571             DisassembleAsData = false;
1572             DisplaySymIndex = i;
1573           }
1574         }
1575       }
1576 
1577       // Decide which symbol(s) from this collection we're going to print.
1578       std::vector<bool> SymsToPrint(SymbolsHere.size(), false);
1579       // If the user has given the --disassemble-symbols option, then we must
1580       // display every symbol in that set, and no others.
1581       if (!DisasmSymbolSet.empty()) {
1582         bool FoundAny = false;
1583         for (size_t i = 0; i < SymbolsHere.size(); ++i) {
1584           if (DisasmSymbolSet.count(SymNamesHere[i])) {
1585             SymsToPrint[i] = true;
1586             FoundAny = true;
1587           }
1588         }
1589 
1590         // And if none of the symbols here is one that the user asked for, skip
1591         // disassembling this entire chunk of code.
1592         if (!FoundAny)
1593           continue;
1594       } else {
1595         // Otherwise, print whichever symbol at this location is last in the
1596         // Symbols array, because that array is pre-sorted in a way intended to
1597         // correlate with priority of which symbol to display.
1598         SymsToPrint[DisplaySymIndex] = true;
1599       }
1600 
1601       // Now that we know we're disassembling this section, override the choice
1602       // of which symbols to display by printing _all_ of them at this address
1603       // if the user asked for all symbols.
1604       //
1605       // That way, '--show-all-symbols --disassemble-symbol=foo' will print
1606       // only the chunk of code headed by 'foo', but also show any other
1607       // symbols defined at that address, such as aliases for 'foo', or the ARM
1608       // mapping symbol preceding its code.
1609       if (ShowAllSymbols) {
1610         for (size_t i = 0; i < SymbolsHere.size(); ++i)
1611           SymsToPrint[i] = true;
1612       }
1613 
1614       if (Start < SectionAddr || StopAddress <= Start)
1615         continue;
1616 
1617       for (size_t i = 0; i < SymbolsHere.size(); ++i)
1618         FoundDisasmSymbolSet.insert(SymNamesHere[i]);
1619 
1620       // The end is the section end, the beginning of the next symbol, or
1621       // --stop-address.
1622       uint64_t End = std::min<uint64_t>(SectionAddr + SectSize, StopAddress);
1623       if (SI < SE)
1624         End = std::min(End, Symbols[SI].Addr);
1625       if (Start >= End || End <= StartAddress)
1626         continue;
1627       Start -= SectionAddr;
1628       End -= SectionAddr;
1629 
1630       if (!PrintedSection) {
1631         PrintedSection = true;
1632         outs() << "\nDisassembly of section ";
1633         if (!SegmentName.empty())
1634           outs() << SegmentName << ",";
1635         outs() << SectionName << ":\n";
1636       }
1637 
1638       outs() << '\n';
1639 
1640       for (size_t i = 0; i < SymbolsHere.size(); ++i) {
1641         if (!SymsToPrint[i])
1642           continue;
1643 
1644         const SymbolInfoTy &Symbol = SymbolsHere[i];
1645         const StringRef SymbolName = SymNamesHere[i];
1646 
1647         if (LeadingAddr)
1648           outs() << format(Is64Bits ? "%016" PRIx64 " " : "%08" PRIx64 " ",
1649                            SectionAddr + Start + VMAAdjustment);
1650         if (Obj.isXCOFF() && SymbolDescription) {
1651           outs() << getXCOFFSymbolDescription(Symbol, SymbolName) << ":\n";
1652         } else
1653           outs() << '<' << SymbolName << ">:\n";
1654       }
1655 
1656       // Don't print raw contents of a virtual section. A virtual section
1657       // doesn't have any contents in the file.
1658       if (Section.isVirtual()) {
1659         outs() << "...\n";
1660         continue;
1661       }
1662 
1663       // See if any of the symbols defined at this location triggers target-
1664       // specific disassembly behavior, e.g. of special descriptors or function
1665       // prelude information.
1666       //
1667       // We stop this loop at the first symbol that triggers some kind of
1668       // interesting behavior (if any), on the assumption that if two symbols
1669       // defined at the same address trigger two conflicting symbol handlers,
1670       // the object file is probably confused anyway, and it would make even
1671       // less sense to present the output of _both_ handlers, because that
1672       // would describe the same data twice.
1673       for (size_t SHI = 0; SHI < SymbolsHere.size(); ++SHI) {
1674         SymbolInfoTy Symbol = SymbolsHere[SHI];
1675 
1676         auto Status =
1677             DisAsm->onSymbolStart(Symbol, Size, Bytes.slice(Start, End - Start),
1678                                   SectionAddr + Start, CommentStream);
1679 
1680         if (!Status) {
1681           // If onSymbolStart returns None, that means it didn't trigger any
1682           // interesting handling for this symbol. Try the other symbols
1683           // defined at this address.
1684           continue;
1685         }
1686 
1687         if (Status.value() == MCDisassembler::Fail) {
1688           // If onSymbolStart returns Fail, that means it identified some kind
1689           // of special data at this address, but wasn't able to disassemble it
1690           // meaningfully. So we fall back to disassembling the failed region
1691           // as bytes, assuming that the target detected the failure before
1692           // printing anything.
1693           //
1694           // Return values Success or SoftFail (i.e no 'real' failure) are
1695           // expected to mean that the target has emitted its own output.
1696           //
1697           // Either way, 'Size' will have been set to the amount of data
1698           // covered by whatever prologue the target identified. So we advance
1699           // our own position to beyond that. Sometimes that will be the entire
1700           // distance to the next symbol, and sometimes it will be just a
1701           // prologue and we should start disassembling instructions from where
1702           // it left off.
1703           outs() << "// Error in decoding " << SymNamesHere[SHI]
1704                  << " : Decoding failed region as bytes.\n";
1705           for (uint64_t I = 0; I < Size; ++I) {
1706             outs() << "\t.byte\t " << format_hex(Bytes[I], 1, /*Upper=*/true)
1707                    << "\n";
1708           }
1709         }
1710         Start += Size;
1711         break;
1712       }
1713 
1714       Index = Start;
1715       if (SectionAddr < StartAddress)
1716         Index = std::max<uint64_t>(Index, StartAddress - SectionAddr);
1717 
1718       if (DisassembleAsData) {
1719         dumpELFData(SectionAddr, Index, End, Bytes);
1720         Index = End;
1721         continue;
1722       }
1723 
1724       bool DumpARMELFData = false;
1725       formatted_raw_ostream FOS(outs());
1726 
1727       std::unordered_map<uint64_t, std::string> AllLabels;
1728       std::unordered_map<uint64_t, std::vector<std::string>> BBAddrMapLabels;
1729       if (SymbolizeOperands) {
1730         collectLocalBranchTargets(Bytes, MIA, DisAsm, IP, PrimarySTI,
1731                                   SectionAddr, Index, End, AllLabels);
1732         collectBBAddrMapLabels(AddrToBBAddrMap, SectionAddr, Index, End,
1733                                BBAddrMapLabels);
1734       }
1735 
1736       while (Index < End) {
1737         // ARM and AArch64 ELF binaries can interleave data and text in the
1738         // same section. We rely on the markers introduced to understand what
1739         // we need to dump. If the data marker is within a function, it is
1740         // denoted as a word/short etc.
1741         if (!MappingSymbols.empty()) {
1742           char Kind = getMappingSymbolKind(MappingSymbols, Index);
1743           DumpARMELFData = Kind == 'd';
1744           if (SecondarySTI) {
1745             if (Kind == 'a') {
1746               STI = PrimaryIsThumb ? SecondarySTI : PrimarySTI;
1747               DisAsm = PrimaryIsThumb ? SecondaryDisAsm : PrimaryDisAsm;
1748             } else if (Kind == 't') {
1749               STI = PrimaryIsThumb ? PrimarySTI : SecondarySTI;
1750               DisAsm = PrimaryIsThumb ? PrimaryDisAsm : SecondaryDisAsm;
1751             }
1752           }
1753         }
1754 
1755         if (DumpARMELFData) {
1756           Size = dumpARMELFData(SectionAddr, Index, End, Obj, Bytes,
1757                                 MappingSymbols, *STI, FOS);
1758         } else {
1759           // When -z or --disassemble-zeroes are given we always dissasemble
1760           // them. Otherwise we might want to skip zero bytes we see.
1761           if (!DisassembleZeroes) {
1762             uint64_t MaxOffset = End - Index;
1763             // For --reloc: print zero blocks patched by relocations, so that
1764             // relocations can be shown in the dump.
1765             if (RelCur != RelEnd)
1766               MaxOffset = std::min(RelCur->getOffset() - RelAdjustment - Index,
1767                                    MaxOffset);
1768 
1769             if (size_t N =
1770                     countSkippableZeroBytes(Bytes.slice(Index, MaxOffset))) {
1771               FOS << "\t\t..." << '\n';
1772               Index += N;
1773               continue;
1774             }
1775           }
1776 
1777           // Print local label if there's any.
1778           auto Iter1 = BBAddrMapLabels.find(SectionAddr + Index);
1779           if (Iter1 != BBAddrMapLabels.end()) {
1780             for (StringRef Label : Iter1->second)
1781               FOS << "<" << Label << ">:\n";
1782           } else {
1783             auto Iter2 = AllLabels.find(SectionAddr + Index);
1784             if (Iter2 != AllLabels.end())
1785               FOS << "<" << Iter2->second << ">:\n";
1786           }
1787 
1788           // Disassemble a real instruction or a data when disassemble all is
1789           // provided
1790           MCInst Inst;
1791           ArrayRef<uint8_t> ThisBytes = Bytes.slice(Index);
1792           uint64_t ThisAddr = SectionAddr + Index;
1793           bool Disassembled = DisAsm->getInstruction(Inst, Size, ThisBytes,
1794                                                      ThisAddr, CommentStream);
1795           if (Size == 0)
1796             Size = std::min<uint64_t>(
1797                 ThisBytes.size(),
1798                 DisAsm->suggestBytesToSkip(ThisBytes, ThisAddr));
1799 
1800           LVP.update({Index, Section.getIndex()},
1801                      {Index + Size, Section.getIndex()}, Index + Size != End);
1802 
1803           IP->setCommentStream(CommentStream);
1804 
1805           PIP.printInst(
1806               *IP, Disassembled ? &Inst : nullptr, Bytes.slice(Index, Size),
1807               {SectionAddr + Index + VMAAdjustment, Section.getIndex()}, FOS,
1808               "", *STI, &SP, Obj.getFileName(), &Rels, LVP);
1809 
1810           IP->setCommentStream(llvm::nulls());
1811 
1812           // If disassembly has failed, avoid analysing invalid/incomplete
1813           // instruction information. Otherwise, try to resolve the target
1814           // address (jump target or memory operand address) and print it on the
1815           // right of the instruction.
1816           if (Disassembled && MIA) {
1817             // Branch targets are printed just after the instructions.
1818             llvm::raw_ostream *TargetOS = &FOS;
1819             uint64_t Target;
1820             bool PrintTarget =
1821                 MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target);
1822             if (!PrintTarget)
1823               if (Optional<uint64_t> MaybeTarget =
1824                       MIA->evaluateMemoryOperandAddress(
1825                           Inst, STI, SectionAddr + Index, Size)) {
1826                 Target = *MaybeTarget;
1827                 PrintTarget = true;
1828                 // Do not print real address when symbolizing.
1829                 if (!SymbolizeOperands) {
1830                   // Memory operand addresses are printed as comments.
1831                   TargetOS = &CommentStream;
1832                   *TargetOS << "0x" << Twine::utohexstr(Target);
1833                 }
1834               }
1835             if (PrintTarget) {
1836               // In a relocatable object, the target's section must reside in
1837               // the same section as the call instruction or it is accessed
1838               // through a relocation.
1839               //
1840               // In a non-relocatable object, the target may be in any section.
1841               // In that case, locate the section(s) containing the target
1842               // address and find the symbol in one of those, if possible.
1843               //
1844               // N.B. We don't walk the relocations in the relocatable case yet.
1845               std::vector<const SectionSymbolsTy *> TargetSectionSymbols;
1846               if (!Obj.isRelocatableObject()) {
1847                 auto It = llvm::partition_point(
1848                     SectionAddresses,
1849                     [=](const std::pair<uint64_t, SectionRef> &O) {
1850                       return O.first <= Target;
1851                     });
1852                 uint64_t TargetSecAddr = 0;
1853                 while (It != SectionAddresses.begin()) {
1854                   --It;
1855                   if (TargetSecAddr == 0)
1856                     TargetSecAddr = It->first;
1857                   if (It->first != TargetSecAddr)
1858                     break;
1859                   TargetSectionSymbols.push_back(&AllSymbols[It->second]);
1860                 }
1861               } else {
1862                 TargetSectionSymbols.push_back(&Symbols);
1863               }
1864               TargetSectionSymbols.push_back(&AbsoluteSymbols);
1865 
1866               // Find the last symbol in the first candidate section whose
1867               // offset is less than or equal to the target. If there are no
1868               // such symbols, try in the next section and so on, before finally
1869               // using the nearest preceding absolute symbol (if any), if there
1870               // are no other valid symbols.
1871               const SymbolInfoTy *TargetSym = nullptr;
1872               for (const SectionSymbolsTy *TargetSymbols :
1873                    TargetSectionSymbols) {
1874                 auto It = llvm::partition_point(
1875                     *TargetSymbols,
1876                     [=](const SymbolInfoTy &O) { return O.Addr <= Target; });
1877                 if (It != TargetSymbols->begin()) {
1878                   TargetSym = &*(It - 1);
1879                   break;
1880                 }
1881               }
1882 
1883               // Print the labels corresponding to the target if there's any.
1884               bool BBAddrMapLabelAvailable = BBAddrMapLabels.count(Target);
1885               bool LabelAvailable = AllLabels.count(Target);
1886               if (TargetSym != nullptr) {
1887                 uint64_t TargetAddress = TargetSym->Addr;
1888                 uint64_t Disp = Target - TargetAddress;
1889                 std::string TargetName = TargetSym->Name.str();
1890                 if (Demangle)
1891                   TargetName = demangle(TargetName);
1892 
1893                 *TargetOS << " <";
1894                 if (!Disp) {
1895                   // Always Print the binary symbol precisely corresponding to
1896                   // the target address.
1897                   *TargetOS << TargetName;
1898                 } else if (BBAddrMapLabelAvailable) {
1899                   *TargetOS << BBAddrMapLabels[Target].front();
1900                 } else if (LabelAvailable) {
1901                   *TargetOS << AllLabels[Target];
1902                 } else {
1903                   // Always Print the binary symbol plus an offset if there's no
1904                   // local label corresponding to the target address.
1905                   *TargetOS << TargetName << "+0x" << Twine::utohexstr(Disp);
1906                 }
1907                 *TargetOS << ">";
1908               } else if (BBAddrMapLabelAvailable) {
1909                 *TargetOS << " <" << BBAddrMapLabels[Target].front() << ">";
1910               } else if (LabelAvailable) {
1911                 *TargetOS << " <" << AllLabels[Target] << ">";
1912               }
1913               // By convention, each record in the comment stream should be
1914               // terminated.
1915               if (TargetOS == &CommentStream)
1916                 *TargetOS << "\n";
1917             }
1918           }
1919         }
1920 
1921         assert(Ctx.getAsmInfo());
1922         emitPostInstructionInfo(FOS, *Ctx.getAsmInfo(), *STI,
1923                                 CommentStream.str(), LVP);
1924         Comments.clear();
1925 
1926         // Hexagon does this in pretty printer
1927         if (Obj.getArch() != Triple::hexagon) {
1928           // Print relocation for instruction and data.
1929           while (RelCur != RelEnd) {
1930             uint64_t Offset = RelCur->getOffset() - RelAdjustment;
1931             // If this relocation is hidden, skip it.
1932             if (getHidden(*RelCur) || SectionAddr + Offset < StartAddress) {
1933               ++RelCur;
1934               continue;
1935             }
1936 
1937             // Stop when RelCur's offset is past the disassembled
1938             // instruction/data. Note that it's possible the disassembled data
1939             // is not the complete data: we might see the relocation printed in
1940             // the middle of the data, but this matches the binutils objdump
1941             // output.
1942             if (Offset >= Index + Size)
1943               break;
1944 
1945             // When --adjust-vma is used, update the address printed.
1946             if (RelCur->getSymbol() != Obj.symbol_end()) {
1947               Expected<section_iterator> SymSI =
1948                   RelCur->getSymbol()->getSection();
1949               if (SymSI && *SymSI != Obj.section_end() &&
1950                   shouldAdjustVA(**SymSI))
1951                 Offset += AdjustVMA;
1952             }
1953 
1954             printRelocation(FOS, Obj.getFileName(), *RelCur,
1955                             SectionAddr + Offset, Is64Bits);
1956             LVP.printAfterOtherLine(FOS, true);
1957             ++RelCur;
1958           }
1959         }
1960 
1961         Index += Size;
1962       }
1963     }
1964   }
1965   StringSet<> MissingDisasmSymbolSet =
1966       set_difference(DisasmSymbolSet, FoundDisasmSymbolSet);
1967   for (StringRef Sym : MissingDisasmSymbolSet.keys())
1968     reportWarning("failed to disassemble missing symbol " + Sym, FileName);
1969 }
1970 
1971 static void disassembleObject(ObjectFile *Obj, bool InlineRelocs) {
1972   const Target *TheTarget = getTarget(Obj);
1973 
1974   // Package up features to be passed to target/subtarget
1975   SubtargetFeatures Features = Obj->getFeatures();
1976   if (!MAttrs.empty()) {
1977     for (unsigned I = 0; I != MAttrs.size(); ++I)
1978       Features.AddFeature(MAttrs[I]);
1979   } else if (MCPU.empty() && Obj->getArch() == llvm::Triple::aarch64) {
1980     Features.AddFeature("+all");
1981   }
1982 
1983   std::unique_ptr<const MCRegisterInfo> MRI(
1984       TheTarget->createMCRegInfo(TripleName));
1985   if (!MRI)
1986     reportError(Obj->getFileName(),
1987                 "no register info for target " + TripleName);
1988 
1989   // Set up disassembler.
1990   MCTargetOptions MCOptions;
1991   std::unique_ptr<const MCAsmInfo> AsmInfo(
1992       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
1993   if (!AsmInfo)
1994     reportError(Obj->getFileName(),
1995                 "no assembly info for target " + TripleName);
1996 
1997   if (MCPU.empty())
1998     MCPU = Obj->tryGetCPUName().value_or("").str();
1999 
2000   if (isArmElf(*Obj)) {
2001     // When disassembling big-endian Arm ELF, the instruction endianness is
2002     // determined in a complex way. In relocatable objects, AAELF32 mandates
2003     // that instruction endianness matches the ELF file endianness; in
2004     // executable images, that's true unless the file header has the EF_ARM_BE8
2005     // flag, in which case instructions are little-endian regardless of data
2006     // endianness.
2007     //
2008     // We must set the big-endian-instructions SubtargetFeature to make the
2009     // disassembler read the instructions the right way round, and also tell
2010     // our own prettyprinter to retrieve the encodings the same way to print in
2011     // hex.
2012     const auto *Elf32BE = dyn_cast<ELF32BEObjectFile>(Obj);
2013 
2014     if (Elf32BE && (Elf32BE->isRelocatableObject() ||
2015                     !(Elf32BE->getPlatformFlags() & ELF::EF_ARM_BE8))) {
2016       Features.AddFeature("+big-endian-instructions");
2017       ARMPrettyPrinterInst.setInstructionEndianness(llvm::support::big);
2018     } else {
2019       ARMPrettyPrinterInst.setInstructionEndianness(llvm::support::little);
2020     }
2021   }
2022 
2023   std::unique_ptr<const MCSubtargetInfo> STI(
2024       TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString()));
2025   if (!STI)
2026     reportError(Obj->getFileName(),
2027                 "no subtarget info for target " + TripleName);
2028   std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
2029   if (!MII)
2030     reportError(Obj->getFileName(),
2031                 "no instruction info for target " + TripleName);
2032   MCContext Ctx(Triple(TripleName), AsmInfo.get(), MRI.get(), STI.get());
2033   // FIXME: for now initialize MCObjectFileInfo with default values
2034   std::unique_ptr<MCObjectFileInfo> MOFI(
2035       TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false));
2036   Ctx.setObjectFileInfo(MOFI.get());
2037 
2038   std::unique_ptr<MCDisassembler> DisAsm(
2039       TheTarget->createMCDisassembler(*STI, Ctx));
2040   if (!DisAsm)
2041     reportError(Obj->getFileName(), "no disassembler for target " + TripleName);
2042 
2043   // If we have an ARM object file, we need a second disassembler, because
2044   // ARM CPUs have two different instruction sets: ARM mode, and Thumb mode.
2045   // We use mapping symbols to switch between the two assemblers, where
2046   // appropriate.
2047   std::unique_ptr<MCDisassembler> SecondaryDisAsm;
2048   std::unique_ptr<const MCSubtargetInfo> SecondarySTI;
2049   if (isArmElf(*Obj) && !STI->checkFeatures("+mclass")) {
2050     if (STI->checkFeatures("+thumb-mode"))
2051       Features.AddFeature("-thumb-mode");
2052     else
2053       Features.AddFeature("+thumb-mode");
2054     SecondarySTI.reset(TheTarget->createMCSubtargetInfo(TripleName, MCPU,
2055                                                         Features.getString()));
2056     SecondaryDisAsm.reset(TheTarget->createMCDisassembler(*SecondarySTI, Ctx));
2057   }
2058 
2059   std::unique_ptr<const MCInstrAnalysis> MIA(
2060       TheTarget->createMCInstrAnalysis(MII.get()));
2061 
2062   int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
2063   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
2064       Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
2065   if (!IP)
2066     reportError(Obj->getFileName(),
2067                 "no instruction printer for target " + TripleName);
2068   IP->setPrintImmHex(PrintImmHex);
2069   IP->setPrintBranchImmAsAddress(true);
2070   IP->setSymbolizeOperands(SymbolizeOperands);
2071   IP->setMCInstrAnalysis(MIA.get());
2072 
2073   PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
2074   ObjectFile *DbgObj = Obj;
2075   OwningBinary<Binary> DebugBinary;
2076   if (!Obj->hasDebugInfo()) {
2077     if (Optional<OwningBinary<Binary>> DebugBinaryOpt =
2078             fetchBinaryByBuildID(*Obj)) {
2079       if (ObjectFile *FetchedObj =
2080               dyn_cast<ObjectFile>(DebugBinaryOpt->getBinary())) {
2081         if (FetchedObj->hasDebugInfo()) {
2082           DebugBinary = std::move(*DebugBinaryOpt);
2083           DbgObj = FetchedObj;
2084         }
2085       }
2086     }
2087   }
2088   SourcePrinter SP(DbgObj, TheTarget->getName());
2089 
2090   for (StringRef Opt : DisassemblerOptions)
2091     if (!IP->applyTargetSpecificCLOption(Opt))
2092       reportError(Obj->getFileName(),
2093                   "Unrecognized disassembler option: " + Opt);
2094 
2095   disassembleObject(TheTarget, *Obj, Ctx, DisAsm.get(), SecondaryDisAsm.get(),
2096                     MIA.get(), IP.get(), STI.get(), SecondarySTI.get(), PIP, SP,
2097                     InlineRelocs);
2098 }
2099 
2100 void objdump::printRelocations(const ObjectFile *Obj) {
2101   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
2102                                                  "%08" PRIx64;
2103 
2104   // Build a mapping from relocation target to a vector of relocation
2105   // sections. Usually, there is an only one relocation section for
2106   // each relocated section.
2107   MapVector<SectionRef, std::vector<SectionRef>> SecToRelSec;
2108   uint64_t Ndx;
2109   for (const SectionRef &Section : ToolSectionFilter(*Obj, &Ndx)) {
2110     if (Obj->isELF() && (ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC))
2111       continue;
2112     if (Section.relocation_begin() == Section.relocation_end())
2113       continue;
2114     Expected<section_iterator> SecOrErr = Section.getRelocatedSection();
2115     if (!SecOrErr)
2116       reportError(Obj->getFileName(),
2117                   "section (" + Twine(Ndx) +
2118                       "): unable to get a relocation target: " +
2119                       toString(SecOrErr.takeError()));
2120     SecToRelSec[**SecOrErr].push_back(Section);
2121   }
2122 
2123   for (std::pair<SectionRef, std::vector<SectionRef>> &P : SecToRelSec) {
2124     StringRef SecName = unwrapOrError(P.first.getName(), Obj->getFileName());
2125     outs() << "\nRELOCATION RECORDS FOR [" << SecName << "]:\n";
2126     uint32_t OffsetPadding = (Obj->getBytesInAddress() > 4 ? 16 : 8);
2127     uint32_t TypePadding = 24;
2128     outs() << left_justify("OFFSET", OffsetPadding) << " "
2129            << left_justify("TYPE", TypePadding) << " "
2130            << "VALUE\n";
2131 
2132     for (SectionRef Section : P.second) {
2133       for (const RelocationRef &Reloc : Section.relocations()) {
2134         uint64_t Address = Reloc.getOffset();
2135         SmallString<32> RelocName;
2136         SmallString<32> ValueStr;
2137         if (Address < StartAddress || Address > StopAddress || getHidden(Reloc))
2138           continue;
2139         Reloc.getTypeName(RelocName);
2140         if (Error E = getRelocationValueString(Reloc, ValueStr))
2141           reportError(std::move(E), Obj->getFileName());
2142 
2143         outs() << format(Fmt.data(), Address) << " "
2144                << left_justify(RelocName, TypePadding) << " " << ValueStr
2145                << "\n";
2146       }
2147     }
2148   }
2149 }
2150 
2151 void objdump::printDynamicRelocations(const ObjectFile *Obj) {
2152   // For the moment, this option is for ELF only
2153   if (!Obj->isELF())
2154     return;
2155 
2156   const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj);
2157   if (!Elf || !any_of(Elf->sections(), [](const ELFSectionRef Sec) {
2158         return Sec.getType() == ELF::SHT_DYNAMIC;
2159       })) {
2160     reportError(Obj->getFileName(), "not a dynamic object");
2161     return;
2162   }
2163 
2164   std::vector<SectionRef> DynRelSec = Obj->dynamic_relocation_sections();
2165   if (DynRelSec.empty())
2166     return;
2167 
2168   outs() << "\nDYNAMIC RELOCATION RECORDS\n";
2169   const uint32_t OffsetPadding = (Obj->getBytesInAddress() > 4 ? 16 : 8);
2170   const uint32_t TypePadding = 24;
2171   outs() << left_justify("OFFSET", OffsetPadding) << ' '
2172          << left_justify("TYPE", TypePadding) << " VALUE\n";
2173 
2174   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
2175   for (const SectionRef &Section : DynRelSec)
2176     for (const RelocationRef &Reloc : Section.relocations()) {
2177       uint64_t Address = Reloc.getOffset();
2178       SmallString<32> RelocName;
2179       SmallString<32> ValueStr;
2180       Reloc.getTypeName(RelocName);
2181       if (Error E = getRelocationValueString(Reloc, ValueStr))
2182         reportError(std::move(E), Obj->getFileName());
2183       outs() << format(Fmt.data(), Address) << ' '
2184              << left_justify(RelocName, TypePadding) << ' ' << ValueStr << '\n';
2185     }
2186 }
2187 
2188 // Returns true if we need to show LMA column when dumping section headers. We
2189 // show it only when the platform is ELF and either we have at least one section
2190 // whose VMA and LMA are different and/or when --show-lma flag is used.
2191 static bool shouldDisplayLMA(const ObjectFile &Obj) {
2192   if (!Obj.isELF())
2193     return false;
2194   for (const SectionRef &S : ToolSectionFilter(Obj))
2195     if (S.getAddress() != getELFSectionLMA(S))
2196       return true;
2197   return ShowLMA;
2198 }
2199 
2200 static size_t getMaxSectionNameWidth(const ObjectFile &Obj) {
2201   // Default column width for names is 13 even if no names are that long.
2202   size_t MaxWidth = 13;
2203   for (const SectionRef &Section : ToolSectionFilter(Obj)) {
2204     StringRef Name = unwrapOrError(Section.getName(), Obj.getFileName());
2205     MaxWidth = std::max(MaxWidth, Name.size());
2206   }
2207   return MaxWidth;
2208 }
2209 
2210 void objdump::printSectionHeaders(ObjectFile &Obj) {
2211   if (Obj.isELF() && Obj.sections().empty())
2212     createFakeELFSections(Obj);
2213 
2214   size_t NameWidth = getMaxSectionNameWidth(Obj);
2215   size_t AddressWidth = 2 * Obj.getBytesInAddress();
2216   bool HasLMAColumn = shouldDisplayLMA(Obj);
2217   outs() << "\nSections:\n";
2218   if (HasLMAColumn)
2219     outs() << "Idx " << left_justify("Name", NameWidth) << " Size     "
2220            << left_justify("VMA", AddressWidth) << " "
2221            << left_justify("LMA", AddressWidth) << " Type\n";
2222   else
2223     outs() << "Idx " << left_justify("Name", NameWidth) << " Size     "
2224            << left_justify("VMA", AddressWidth) << " Type\n";
2225 
2226   uint64_t Idx;
2227   for (const SectionRef &Section : ToolSectionFilter(Obj, &Idx)) {
2228     StringRef Name = unwrapOrError(Section.getName(), Obj.getFileName());
2229     uint64_t VMA = Section.getAddress();
2230     if (shouldAdjustVA(Section))
2231       VMA += AdjustVMA;
2232 
2233     uint64_t Size = Section.getSize();
2234 
2235     std::string Type = Section.isText() ? "TEXT" : "";
2236     if (Section.isData())
2237       Type += Type.empty() ? "DATA" : ", DATA";
2238     if (Section.isBSS())
2239       Type += Type.empty() ? "BSS" : ", BSS";
2240     if (Section.isDebugSection())
2241       Type += Type.empty() ? "DEBUG" : ", DEBUG";
2242 
2243     if (HasLMAColumn)
2244       outs() << format("%3" PRIu64 " %-*s %08" PRIx64 " ", Idx, NameWidth,
2245                        Name.str().c_str(), Size)
2246              << format_hex_no_prefix(VMA, AddressWidth) << " "
2247              << format_hex_no_prefix(getELFSectionLMA(Section), AddressWidth)
2248              << " " << Type << "\n";
2249     else
2250       outs() << format("%3" PRIu64 " %-*s %08" PRIx64 " ", Idx, NameWidth,
2251                        Name.str().c_str(), Size)
2252              << format_hex_no_prefix(VMA, AddressWidth) << " " << Type << "\n";
2253   }
2254 }
2255 
2256 void objdump::printSectionContents(const ObjectFile *Obj) {
2257   const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj);
2258 
2259   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
2260     StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName());
2261     uint64_t BaseAddr = Section.getAddress();
2262     uint64_t Size = Section.getSize();
2263     if (!Size)
2264       continue;
2265 
2266     outs() << "Contents of section ";
2267     StringRef SegmentName = getSegmentName(MachO, Section);
2268     if (!SegmentName.empty())
2269       outs() << SegmentName << ",";
2270     outs() << Name << ":\n";
2271     if (Section.isBSS()) {
2272       outs() << format("<skipping contents of bss section at [%04" PRIx64
2273                        ", %04" PRIx64 ")>\n",
2274                        BaseAddr, BaseAddr + Size);
2275       continue;
2276     }
2277 
2278     StringRef Contents = unwrapOrError(Section.getContents(), Obj->getFileName());
2279 
2280     // Dump out the content as hex and printable ascii characters.
2281     for (std::size_t Addr = 0, End = Contents.size(); Addr < End; Addr += 16) {
2282       outs() << format(" %04" PRIx64 " ", BaseAddr + Addr);
2283       // Dump line of hex.
2284       for (std::size_t I = 0; I < 16; ++I) {
2285         if (I != 0 && I % 4 == 0)
2286           outs() << ' ';
2287         if (Addr + I < End)
2288           outs() << hexdigit((Contents[Addr + I] >> 4) & 0xF, true)
2289                  << hexdigit(Contents[Addr + I] & 0xF, true);
2290         else
2291           outs() << "  ";
2292       }
2293       // Print ascii.
2294       outs() << "  ";
2295       for (std::size_t I = 0; I < 16 && Addr + I < End; ++I) {
2296         if (isPrint(static_cast<unsigned char>(Contents[Addr + I]) & 0xFF))
2297           outs() << Contents[Addr + I];
2298         else
2299           outs() << ".";
2300       }
2301       outs() << "\n";
2302     }
2303   }
2304 }
2305 
2306 void objdump::printSymbolTable(const ObjectFile &O, StringRef ArchiveName,
2307                                StringRef ArchitectureName, bool DumpDynamic) {
2308   if (O.isCOFF() && !DumpDynamic) {
2309     outs() << "\nSYMBOL TABLE:\n";
2310     printCOFFSymbolTable(cast<const COFFObjectFile>(O));
2311     return;
2312   }
2313 
2314   const StringRef FileName = O.getFileName();
2315 
2316   if (!DumpDynamic) {
2317     outs() << "\nSYMBOL TABLE:\n";
2318     for (auto I = O.symbol_begin(); I != O.symbol_end(); ++I)
2319       printSymbol(O, *I, {}, FileName, ArchiveName, ArchitectureName,
2320                   DumpDynamic);
2321     return;
2322   }
2323 
2324   outs() << "\nDYNAMIC SYMBOL TABLE:\n";
2325   if (!O.isELF()) {
2326     reportWarning(
2327         "this operation is not currently supported for this file format",
2328         FileName);
2329     return;
2330   }
2331 
2332   const ELFObjectFileBase *ELF = cast<const ELFObjectFileBase>(&O);
2333   auto Symbols = ELF->getDynamicSymbolIterators();
2334   Expected<std::vector<VersionEntry>> SymbolVersionsOrErr =
2335       ELF->readDynsymVersions();
2336   if (!SymbolVersionsOrErr) {
2337     reportWarning(toString(SymbolVersionsOrErr.takeError()), FileName);
2338     SymbolVersionsOrErr = std::vector<VersionEntry>();
2339     (void)!SymbolVersionsOrErr;
2340   }
2341   for (auto &Sym : Symbols)
2342     printSymbol(O, Sym, *SymbolVersionsOrErr, FileName, ArchiveName,
2343                 ArchitectureName, DumpDynamic);
2344 }
2345 
2346 void objdump::printSymbol(const ObjectFile &O, const SymbolRef &Symbol,
2347                           ArrayRef<VersionEntry> SymbolVersions,
2348                           StringRef FileName, StringRef ArchiveName,
2349                           StringRef ArchitectureName, bool DumpDynamic) {
2350   const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(&O);
2351   uint64_t Address = unwrapOrError(Symbol.getAddress(), FileName, ArchiveName,
2352                                    ArchitectureName);
2353   if ((Address < StartAddress) || (Address > StopAddress))
2354     return;
2355   SymbolRef::Type Type =
2356       unwrapOrError(Symbol.getType(), FileName, ArchiveName, ArchitectureName);
2357   uint32_t Flags =
2358       unwrapOrError(Symbol.getFlags(), FileName, ArchiveName, ArchitectureName);
2359 
2360   // Don't ask a Mach-O STAB symbol for its section unless you know that
2361   // STAB symbol's section field refers to a valid section index. Otherwise
2362   // the symbol may error trying to load a section that does not exist.
2363   bool IsSTAB = false;
2364   if (MachO) {
2365     DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
2366     uint8_t NType =
2367         (MachO->is64Bit() ? MachO->getSymbol64TableEntry(SymDRI).n_type
2368                           : MachO->getSymbolTableEntry(SymDRI).n_type);
2369     if (NType & MachO::N_STAB)
2370       IsSTAB = true;
2371   }
2372   section_iterator Section = IsSTAB
2373                                  ? O.section_end()
2374                                  : unwrapOrError(Symbol.getSection(), FileName,
2375                                                  ArchiveName, ArchitectureName);
2376 
2377   StringRef Name;
2378   if (Type == SymbolRef::ST_Debug && Section != O.section_end()) {
2379     if (Expected<StringRef> NameOrErr = Section->getName())
2380       Name = *NameOrErr;
2381     else
2382       consumeError(NameOrErr.takeError());
2383 
2384   } else {
2385     Name = unwrapOrError(Symbol.getName(), FileName, ArchiveName,
2386                          ArchitectureName);
2387   }
2388 
2389   bool Global = Flags & SymbolRef::SF_Global;
2390   bool Weak = Flags & SymbolRef::SF_Weak;
2391   bool Absolute = Flags & SymbolRef::SF_Absolute;
2392   bool Common = Flags & SymbolRef::SF_Common;
2393   bool Hidden = Flags & SymbolRef::SF_Hidden;
2394 
2395   char GlobLoc = ' ';
2396   if ((Section != O.section_end() || Absolute) && !Weak)
2397     GlobLoc = Global ? 'g' : 'l';
2398   char IFunc = ' ';
2399   if (O.isELF()) {
2400     if (ELFSymbolRef(Symbol).getELFType() == ELF::STT_GNU_IFUNC)
2401       IFunc = 'i';
2402     if (ELFSymbolRef(Symbol).getBinding() == ELF::STB_GNU_UNIQUE)
2403       GlobLoc = 'u';
2404   }
2405 
2406   char Debug = ' ';
2407   if (DumpDynamic)
2408     Debug = 'D';
2409   else if (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
2410     Debug = 'd';
2411 
2412   char FileFunc = ' ';
2413   if (Type == SymbolRef::ST_File)
2414     FileFunc = 'f';
2415   else if (Type == SymbolRef::ST_Function)
2416     FileFunc = 'F';
2417   else if (Type == SymbolRef::ST_Data)
2418     FileFunc = 'O';
2419 
2420   const char *Fmt = O.getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
2421 
2422   outs() << format(Fmt, Address) << " "
2423          << GlobLoc            // Local -> 'l', Global -> 'g', Neither -> ' '
2424          << (Weak ? 'w' : ' ') // Weak?
2425          << ' '                // Constructor. Not supported yet.
2426          << ' '                // Warning. Not supported yet.
2427          << IFunc              // Indirect reference to another symbol.
2428          << Debug              // Debugging (d) or dynamic (D) symbol.
2429          << FileFunc           // Name of function (F), file (f) or object (O).
2430          << ' ';
2431   if (Absolute) {
2432     outs() << "*ABS*";
2433   } else if (Common) {
2434     outs() << "*COM*";
2435   } else if (Section == O.section_end()) {
2436     if (O.isXCOFF()) {
2437       XCOFFSymbolRef XCOFFSym = cast<const XCOFFObjectFile>(O).toSymbolRef(
2438           Symbol.getRawDataRefImpl());
2439       if (XCOFF::N_DEBUG == XCOFFSym.getSectionNumber())
2440         outs() << "*DEBUG*";
2441       else
2442         outs() << "*UND*";
2443     } else
2444       outs() << "*UND*";
2445   } else {
2446     StringRef SegmentName = getSegmentName(MachO, *Section);
2447     if (!SegmentName.empty())
2448       outs() << SegmentName << ",";
2449     StringRef SectionName = unwrapOrError(Section->getName(), FileName);
2450     outs() << SectionName;
2451     if (O.isXCOFF()) {
2452       Optional<SymbolRef> SymRef =
2453           getXCOFFSymbolContainingSymbolRef(cast<XCOFFObjectFile>(O), Symbol);
2454       if (SymRef) {
2455 
2456         Expected<StringRef> NameOrErr = SymRef->getName();
2457 
2458         if (NameOrErr) {
2459           outs() << " (csect:";
2460           std::string SymName(NameOrErr.get());
2461 
2462           if (Demangle)
2463             SymName = demangle(SymName);
2464 
2465           if (SymbolDescription)
2466             SymName = getXCOFFSymbolDescription(
2467                 createSymbolInfo(O, SymRef.value()), SymName);
2468 
2469           outs() << ' ' << SymName;
2470           outs() << ") ";
2471         } else
2472           reportWarning(toString(NameOrErr.takeError()), FileName);
2473       }
2474     }
2475   }
2476 
2477   if (Common)
2478     outs() << '\t' << format(Fmt, static_cast<uint64_t>(Symbol.getAlignment()));
2479   else if (O.isXCOFF())
2480     outs() << '\t'
2481            << format(Fmt, cast<XCOFFObjectFile>(O).getSymbolSize(
2482                               Symbol.getRawDataRefImpl()));
2483   else if (O.isELF())
2484     outs() << '\t' << format(Fmt, ELFSymbolRef(Symbol).getSize());
2485 
2486   if (O.isELF()) {
2487     if (!SymbolVersions.empty()) {
2488       const VersionEntry &Ver =
2489           SymbolVersions[Symbol.getRawDataRefImpl().d.b - 1];
2490       std::string Str;
2491       if (!Ver.Name.empty())
2492         Str = Ver.IsVerDef ? ' ' + Ver.Name : '(' + Ver.Name + ')';
2493       outs() << ' ' << left_justify(Str, 12);
2494     }
2495 
2496     uint8_t Other = ELFSymbolRef(Symbol).getOther();
2497     switch (Other) {
2498     case ELF::STV_DEFAULT:
2499       break;
2500     case ELF::STV_INTERNAL:
2501       outs() << " .internal";
2502       break;
2503     case ELF::STV_HIDDEN:
2504       outs() << " .hidden";
2505       break;
2506     case ELF::STV_PROTECTED:
2507       outs() << " .protected";
2508       break;
2509     default:
2510       outs() << format(" 0x%02x", Other);
2511       break;
2512     }
2513   } else if (Hidden) {
2514     outs() << " .hidden";
2515   }
2516 
2517   std::string SymName(Name);
2518   if (Demangle)
2519     SymName = demangle(SymName);
2520 
2521   if (O.isXCOFF() && SymbolDescription)
2522     SymName = getXCOFFSymbolDescription(createSymbolInfo(O, Symbol), SymName);
2523 
2524   outs() << ' ' << SymName << '\n';
2525 }
2526 
2527 static void printUnwindInfo(const ObjectFile *O) {
2528   outs() << "Unwind info:\n\n";
2529 
2530   if (const COFFObjectFile *Coff = dyn_cast<COFFObjectFile>(O))
2531     printCOFFUnwindInfo(Coff);
2532   else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O))
2533     printMachOUnwindInfo(MachO);
2534   else
2535     // TODO: Extract DWARF dump tool to objdump.
2536     WithColor::error(errs(), ToolName)
2537         << "This operation is only currently supported "
2538            "for COFF and MachO object files.\n";
2539 }
2540 
2541 /// Dump the raw contents of the __clangast section so the output can be piped
2542 /// into llvm-bcanalyzer.
2543 static void printRawClangAST(const ObjectFile *Obj) {
2544   if (outs().is_displayed()) {
2545     WithColor::error(errs(), ToolName)
2546         << "The -raw-clang-ast option will dump the raw binary contents of "
2547            "the clang ast section.\n"
2548            "Please redirect the output to a file or another program such as "
2549            "llvm-bcanalyzer.\n";
2550     return;
2551   }
2552 
2553   StringRef ClangASTSectionName("__clangast");
2554   if (Obj->isCOFF()) {
2555     ClangASTSectionName = "clangast";
2556   }
2557 
2558   Optional<object::SectionRef> ClangASTSection;
2559   for (auto Sec : ToolSectionFilter(*Obj)) {
2560     StringRef Name;
2561     if (Expected<StringRef> NameOrErr = Sec.getName())
2562       Name = *NameOrErr;
2563     else
2564       consumeError(NameOrErr.takeError());
2565 
2566     if (Name == ClangASTSectionName) {
2567       ClangASTSection = Sec;
2568       break;
2569     }
2570   }
2571   if (!ClangASTSection)
2572     return;
2573 
2574   StringRef ClangASTContents =
2575       unwrapOrError(ClangASTSection.value().getContents(), Obj->getFileName());
2576   outs().write(ClangASTContents.data(), ClangASTContents.size());
2577 }
2578 
2579 static void printFaultMaps(const ObjectFile *Obj) {
2580   StringRef FaultMapSectionName;
2581 
2582   if (Obj->isELF()) {
2583     FaultMapSectionName = ".llvm_faultmaps";
2584   } else if (Obj->isMachO()) {
2585     FaultMapSectionName = "__llvm_faultmaps";
2586   } else {
2587     WithColor::error(errs(), ToolName)
2588         << "This operation is only currently supported "
2589            "for ELF and Mach-O executable files.\n";
2590     return;
2591   }
2592 
2593   Optional<object::SectionRef> FaultMapSection;
2594 
2595   for (auto Sec : ToolSectionFilter(*Obj)) {
2596     StringRef Name;
2597     if (Expected<StringRef> NameOrErr = Sec.getName())
2598       Name = *NameOrErr;
2599     else
2600       consumeError(NameOrErr.takeError());
2601 
2602     if (Name == FaultMapSectionName) {
2603       FaultMapSection = Sec;
2604       break;
2605     }
2606   }
2607 
2608   outs() << "FaultMap table:\n";
2609 
2610   if (!FaultMapSection) {
2611     outs() << "<not found>\n";
2612     return;
2613   }
2614 
2615   StringRef FaultMapContents =
2616       unwrapOrError(FaultMapSection->getContents(), Obj->getFileName());
2617   FaultMapParser FMP(FaultMapContents.bytes_begin(),
2618                      FaultMapContents.bytes_end());
2619 
2620   outs() << FMP;
2621 }
2622 
2623 static void printPrivateFileHeaders(const ObjectFile *O, bool OnlyFirst) {
2624   if (O->isELF()) {
2625     printELFFileHeader(O);
2626     printELFDynamicSection(O);
2627     printELFSymbolVersionInfo(O);
2628     return;
2629   }
2630   if (O->isCOFF())
2631     return printCOFFFileHeader(cast<object::COFFObjectFile>(*O));
2632   if (O->isWasm())
2633     return printWasmFileHeader(O);
2634   if (O->isMachO()) {
2635     printMachOFileHeader(O);
2636     if (!OnlyFirst)
2637       printMachOLoadCommands(O);
2638     return;
2639   }
2640   reportError(O->getFileName(), "Invalid/Unsupported object file format");
2641 }
2642 
2643 static void printFileHeaders(const ObjectFile *O) {
2644   if (!O->isELF() && !O->isCOFF())
2645     reportError(O->getFileName(), "Invalid/Unsupported object file format");
2646 
2647   Triple::ArchType AT = O->getArch();
2648   outs() << "architecture: " << Triple::getArchTypeName(AT) << "\n";
2649   uint64_t Address = unwrapOrError(O->getStartAddress(), O->getFileName());
2650 
2651   StringRef Fmt = O->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
2652   outs() << "start address: "
2653          << "0x" << format(Fmt.data(), Address) << "\n";
2654 }
2655 
2656 static void printArchiveChild(StringRef Filename, const Archive::Child &C) {
2657   Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
2658   if (!ModeOrErr) {
2659     WithColor::error(errs(), ToolName) << "ill-formed archive entry.\n";
2660     consumeError(ModeOrErr.takeError());
2661     return;
2662   }
2663   sys::fs::perms Mode = ModeOrErr.get();
2664   outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
2665   outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
2666   outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
2667   outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
2668   outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
2669   outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
2670   outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
2671   outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
2672   outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
2673 
2674   outs() << " ";
2675 
2676   outs() << format("%d/%d %6" PRId64 " ", unwrapOrError(C.getUID(), Filename),
2677                    unwrapOrError(C.getGID(), Filename),
2678                    unwrapOrError(C.getRawSize(), Filename));
2679 
2680   StringRef RawLastModified = C.getRawLastModified();
2681   unsigned Seconds;
2682   if (RawLastModified.getAsInteger(10, Seconds))
2683     outs() << "(date: \"" << RawLastModified
2684            << "\" contains non-decimal chars) ";
2685   else {
2686     // Since ctime(3) returns a 26 character string of the form:
2687     // "Sun Sep 16 01:03:52 1973\n\0"
2688     // just print 24 characters.
2689     time_t t = Seconds;
2690     outs() << format("%.24s ", ctime(&t));
2691   }
2692 
2693   StringRef Name = "";
2694   Expected<StringRef> NameOrErr = C.getName();
2695   if (!NameOrErr) {
2696     consumeError(NameOrErr.takeError());
2697     Name = unwrapOrError(C.getRawName(), Filename);
2698   } else {
2699     Name = NameOrErr.get();
2700   }
2701   outs() << Name << "\n";
2702 }
2703 
2704 // For ELF only now.
2705 static bool shouldWarnForInvalidStartStopAddress(ObjectFile *Obj) {
2706   if (const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj)) {
2707     if (Elf->getEType() != ELF::ET_REL)
2708       return true;
2709   }
2710   return false;
2711 }
2712 
2713 static void checkForInvalidStartStopAddress(ObjectFile *Obj,
2714                                             uint64_t Start, uint64_t Stop) {
2715   if (!shouldWarnForInvalidStartStopAddress(Obj))
2716     return;
2717 
2718   for (const SectionRef &Section : Obj->sections())
2719     if (ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC) {
2720       uint64_t BaseAddr = Section.getAddress();
2721       uint64_t Size = Section.getSize();
2722       if ((Start < BaseAddr + Size) && Stop > BaseAddr)
2723         return;
2724     }
2725 
2726   if (!HasStartAddressFlag)
2727     reportWarning("no section has address less than 0x" +
2728                       Twine::utohexstr(Stop) + " specified by --stop-address",
2729                   Obj->getFileName());
2730   else if (!HasStopAddressFlag)
2731     reportWarning("no section has address greater than or equal to 0x" +
2732                       Twine::utohexstr(Start) + " specified by --start-address",
2733                   Obj->getFileName());
2734   else
2735     reportWarning("no section overlaps the range [0x" +
2736                       Twine::utohexstr(Start) + ",0x" + Twine::utohexstr(Stop) +
2737                       ") specified by --start-address/--stop-address",
2738                   Obj->getFileName());
2739 }
2740 
2741 static void dumpObject(ObjectFile *O, const Archive *A = nullptr,
2742                        const Archive::Child *C = nullptr) {
2743   // Avoid other output when using a raw option.
2744   if (!RawClangAST) {
2745     outs() << '\n';
2746     if (A)
2747       outs() << A->getFileName() << "(" << O->getFileName() << ")";
2748     else
2749       outs() << O->getFileName();
2750     outs() << ":\tfile format " << O->getFileFormatName().lower() << "\n";
2751   }
2752 
2753   if (HasStartAddressFlag || HasStopAddressFlag)
2754     checkForInvalidStartStopAddress(O, StartAddress, StopAddress);
2755 
2756   // Note: the order here matches GNU objdump for compatability.
2757   StringRef ArchiveName = A ? A->getFileName() : "";
2758   if (ArchiveHeaders && !MachOOpt && C)
2759     printArchiveChild(ArchiveName, *C);
2760   if (FileHeaders)
2761     printFileHeaders(O);
2762   if (PrivateHeaders || FirstPrivateHeader)
2763     printPrivateFileHeaders(O, FirstPrivateHeader);
2764   if (SectionHeaders)
2765     printSectionHeaders(*O);
2766   if (SymbolTable)
2767     printSymbolTable(*O, ArchiveName);
2768   if (DynamicSymbolTable)
2769     printSymbolTable(*O, ArchiveName, /*ArchitectureName=*/"",
2770                      /*DumpDynamic=*/true);
2771   if (DwarfDumpType != DIDT_Null) {
2772     std::unique_ptr<DIContext> DICtx = DWARFContext::create(*O);
2773     // Dump the complete DWARF structure.
2774     DIDumpOptions DumpOpts;
2775     DumpOpts.DumpType = DwarfDumpType;
2776     DICtx->dump(outs(), DumpOpts);
2777   }
2778   if (Relocations && !Disassemble)
2779     printRelocations(O);
2780   if (DynamicRelocations)
2781     printDynamicRelocations(O);
2782   if (SectionContents)
2783     printSectionContents(O);
2784   if (Disassemble)
2785     disassembleObject(O, Relocations);
2786   if (UnwindInfo)
2787     printUnwindInfo(O);
2788 
2789   // Mach-O specific options:
2790   if (ExportsTrie)
2791     printExportsTrie(O);
2792   if (Rebase)
2793     printRebaseTable(O);
2794   if (Bind)
2795     printBindTable(O);
2796   if (LazyBind)
2797     printLazyBindTable(O);
2798   if (WeakBind)
2799     printWeakBindTable(O);
2800 
2801   // Other special sections:
2802   if (RawClangAST)
2803     printRawClangAST(O);
2804   if (FaultMapSection)
2805     printFaultMaps(O);
2806   if (Offloading)
2807     dumpOffloadBinary(*O);
2808 }
2809 
2810 static void dumpObject(const COFFImportFile *I, const Archive *A,
2811                        const Archive::Child *C = nullptr) {
2812   StringRef ArchiveName = A ? A->getFileName() : "";
2813 
2814   // Avoid other output when using a raw option.
2815   if (!RawClangAST)
2816     outs() << '\n'
2817            << ArchiveName << "(" << I->getFileName() << ")"
2818            << ":\tfile format COFF-import-file"
2819            << "\n\n";
2820 
2821   if (ArchiveHeaders && !MachOOpt && C)
2822     printArchiveChild(ArchiveName, *C);
2823   if (SymbolTable)
2824     printCOFFSymbolTable(*I);
2825 }
2826 
2827 /// Dump each object file in \a a;
2828 static void dumpArchive(const Archive *A) {
2829   Error Err = Error::success();
2830   unsigned I = -1;
2831   for (auto &C : A->children(Err)) {
2832     ++I;
2833     Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2834     if (!ChildOrErr) {
2835       if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2836         reportError(std::move(E), getFileNameForError(C, I), A->getFileName());
2837       continue;
2838     }
2839     if (ObjectFile *O = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
2840       dumpObject(O, A, &C);
2841     else if (COFFImportFile *I = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
2842       dumpObject(I, A, &C);
2843     else
2844       reportError(errorCodeToError(object_error::invalid_file_type),
2845                   A->getFileName());
2846   }
2847   if (Err)
2848     reportError(std::move(Err), A->getFileName());
2849 }
2850 
2851 /// Open file and figure out how to dump it.
2852 static void dumpInput(StringRef file) {
2853   // If we are using the Mach-O specific object file parser, then let it parse
2854   // the file and process the command line options.  So the -arch flags can
2855   // be used to select specific slices, etc.
2856   if (MachOOpt) {
2857     parseInputMachO(file);
2858     return;
2859   }
2860 
2861   // Attempt to open the binary.
2862   OwningBinary<Binary> OBinary = unwrapOrError(createBinary(file), file);
2863   Binary &Binary = *OBinary.getBinary();
2864 
2865   if (Archive *A = dyn_cast<Archive>(&Binary))
2866     dumpArchive(A);
2867   else if (ObjectFile *O = dyn_cast<ObjectFile>(&Binary))
2868     dumpObject(O);
2869   else if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Binary))
2870     parseInputMachO(UB);
2871   else if (OffloadBinary *OB = dyn_cast<OffloadBinary>(&Binary))
2872     dumpOffloadSections(*OB);
2873   else
2874     reportError(errorCodeToError(object_error::invalid_file_type), file);
2875 }
2876 
2877 template <typename T>
2878 static void parseIntArg(const llvm::opt::InputArgList &InputArgs, int ID,
2879                         T &Value) {
2880   if (const opt::Arg *A = InputArgs.getLastArg(ID)) {
2881     StringRef V(A->getValue());
2882     if (!llvm::to_integer(V, Value, 0)) {
2883       reportCmdLineError(A->getSpelling() +
2884                          ": expected a non-negative integer, but got '" + V +
2885                          "'");
2886     }
2887   }
2888 }
2889 
2890 static void invalidArgValue(const opt::Arg *A) {
2891   reportCmdLineError("'" + StringRef(A->getValue()) +
2892                      "' is not a valid value for '" + A->getSpelling() + "'");
2893 }
2894 
2895 static std::vector<std::string>
2896 commaSeparatedValues(const llvm::opt::InputArgList &InputArgs, int ID) {
2897   std::vector<std::string> Values;
2898   for (StringRef Value : InputArgs.getAllArgValues(ID)) {
2899     llvm::SmallVector<StringRef, 2> SplitValues;
2900     llvm::SplitString(Value, SplitValues, ",");
2901     for (StringRef SplitValue : SplitValues)
2902       Values.push_back(SplitValue.str());
2903   }
2904   return Values;
2905 }
2906 
2907 static void parseOtoolOptions(const llvm::opt::InputArgList &InputArgs) {
2908   MachOOpt = true;
2909   FullLeadingAddr = true;
2910   PrintImmHex = true;
2911 
2912   ArchName = InputArgs.getLastArgValue(OTOOL_arch).str();
2913   LinkOptHints = InputArgs.hasArg(OTOOL_C);
2914   if (InputArgs.hasArg(OTOOL_d))
2915     FilterSections.push_back("__DATA,__data");
2916   DylibId = InputArgs.hasArg(OTOOL_D);
2917   UniversalHeaders = InputArgs.hasArg(OTOOL_f);
2918   DataInCode = InputArgs.hasArg(OTOOL_G);
2919   FirstPrivateHeader = InputArgs.hasArg(OTOOL_h);
2920   IndirectSymbols = InputArgs.hasArg(OTOOL_I);
2921   ShowRawInsn = InputArgs.hasArg(OTOOL_j);
2922   PrivateHeaders = InputArgs.hasArg(OTOOL_l);
2923   DylibsUsed = InputArgs.hasArg(OTOOL_L);
2924   MCPU = InputArgs.getLastArgValue(OTOOL_mcpu_EQ).str();
2925   ObjcMetaData = InputArgs.hasArg(OTOOL_o);
2926   DisSymName = InputArgs.getLastArgValue(OTOOL_p).str();
2927   InfoPlist = InputArgs.hasArg(OTOOL_P);
2928   Relocations = InputArgs.hasArg(OTOOL_r);
2929   if (const Arg *A = InputArgs.getLastArg(OTOOL_s)) {
2930     auto Filter = (A->getValue(0) + StringRef(",") + A->getValue(1)).str();
2931     FilterSections.push_back(Filter);
2932   }
2933   if (InputArgs.hasArg(OTOOL_t))
2934     FilterSections.push_back("__TEXT,__text");
2935   Verbose = InputArgs.hasArg(OTOOL_v) || InputArgs.hasArg(OTOOL_V) ||
2936             InputArgs.hasArg(OTOOL_o);
2937   SymbolicOperands = InputArgs.hasArg(OTOOL_V);
2938   if (InputArgs.hasArg(OTOOL_x))
2939     FilterSections.push_back(",__text");
2940   LeadingAddr = LeadingHeaders = !InputArgs.hasArg(OTOOL_X);
2941 
2942   ChainedFixups = InputArgs.hasArg(OTOOL_chained_fixups);
2943   DyldInfo = InputArgs.hasArg(OTOOL_dyld_info);
2944 
2945   InputFilenames = InputArgs.getAllArgValues(OTOOL_INPUT);
2946   if (InputFilenames.empty())
2947     reportCmdLineError("no input file");
2948 
2949   for (const Arg *A : InputArgs) {
2950     const Option &O = A->getOption();
2951     if (O.getGroup().isValid() && O.getGroup().getID() == OTOOL_grp_obsolete) {
2952       reportCmdLineWarning(O.getPrefixedName() +
2953                            " is obsolete and not implemented");
2954     }
2955   }
2956 }
2957 
2958 static void parseObjdumpOptions(const llvm::opt::InputArgList &InputArgs) {
2959   parseIntArg(InputArgs, OBJDUMP_adjust_vma_EQ, AdjustVMA);
2960   AllHeaders = InputArgs.hasArg(OBJDUMP_all_headers);
2961   ArchName = InputArgs.getLastArgValue(OBJDUMP_arch_name_EQ).str();
2962   ArchiveHeaders = InputArgs.hasArg(OBJDUMP_archive_headers);
2963   Demangle = InputArgs.hasArg(OBJDUMP_demangle);
2964   Disassemble = InputArgs.hasArg(OBJDUMP_disassemble);
2965   DisassembleAll = InputArgs.hasArg(OBJDUMP_disassemble_all);
2966   SymbolDescription = InputArgs.hasArg(OBJDUMP_symbol_description);
2967   DisassembleSymbols =
2968       commaSeparatedValues(InputArgs, OBJDUMP_disassemble_symbols_EQ);
2969   DisassembleZeroes = InputArgs.hasArg(OBJDUMP_disassemble_zeroes);
2970   if (const opt::Arg *A = InputArgs.getLastArg(OBJDUMP_dwarf_EQ)) {
2971     DwarfDumpType = StringSwitch<DIDumpType>(A->getValue())
2972                         .Case("frames", DIDT_DebugFrame)
2973                         .Default(DIDT_Null);
2974     if (DwarfDumpType == DIDT_Null)
2975       invalidArgValue(A);
2976   }
2977   DynamicRelocations = InputArgs.hasArg(OBJDUMP_dynamic_reloc);
2978   FaultMapSection = InputArgs.hasArg(OBJDUMP_fault_map_section);
2979   Offloading = InputArgs.hasArg(OBJDUMP_offloading);
2980   FileHeaders = InputArgs.hasArg(OBJDUMP_file_headers);
2981   SectionContents = InputArgs.hasArg(OBJDUMP_full_contents);
2982   PrintLines = InputArgs.hasArg(OBJDUMP_line_numbers);
2983   InputFilenames = InputArgs.getAllArgValues(OBJDUMP_INPUT);
2984   MachOOpt = InputArgs.hasArg(OBJDUMP_macho);
2985   MCPU = InputArgs.getLastArgValue(OBJDUMP_mcpu_EQ).str();
2986   MAttrs = commaSeparatedValues(InputArgs, OBJDUMP_mattr_EQ);
2987   ShowRawInsn = !InputArgs.hasArg(OBJDUMP_no_show_raw_insn);
2988   LeadingAddr = !InputArgs.hasArg(OBJDUMP_no_leading_addr);
2989   RawClangAST = InputArgs.hasArg(OBJDUMP_raw_clang_ast);
2990   Relocations = InputArgs.hasArg(OBJDUMP_reloc);
2991   PrintImmHex =
2992       InputArgs.hasFlag(OBJDUMP_print_imm_hex, OBJDUMP_no_print_imm_hex, false);
2993   PrivateHeaders = InputArgs.hasArg(OBJDUMP_private_headers);
2994   FilterSections = InputArgs.getAllArgValues(OBJDUMP_section_EQ);
2995   SectionHeaders = InputArgs.hasArg(OBJDUMP_section_headers);
2996   ShowAllSymbols = InputArgs.hasArg(OBJDUMP_show_all_symbols);
2997   ShowLMA = InputArgs.hasArg(OBJDUMP_show_lma);
2998   PrintSource = InputArgs.hasArg(OBJDUMP_source);
2999   parseIntArg(InputArgs, OBJDUMP_start_address_EQ, StartAddress);
3000   HasStartAddressFlag = InputArgs.hasArg(OBJDUMP_start_address_EQ);
3001   parseIntArg(InputArgs, OBJDUMP_stop_address_EQ, StopAddress);
3002   HasStopAddressFlag = InputArgs.hasArg(OBJDUMP_stop_address_EQ);
3003   SymbolTable = InputArgs.hasArg(OBJDUMP_syms);
3004   SymbolizeOperands = InputArgs.hasArg(OBJDUMP_symbolize_operands);
3005   DynamicSymbolTable = InputArgs.hasArg(OBJDUMP_dynamic_syms);
3006   TripleName = InputArgs.getLastArgValue(OBJDUMP_triple_EQ).str();
3007   UnwindInfo = InputArgs.hasArg(OBJDUMP_unwind_info);
3008   Wide = InputArgs.hasArg(OBJDUMP_wide);
3009   Prefix = InputArgs.getLastArgValue(OBJDUMP_prefix).str();
3010   parseIntArg(InputArgs, OBJDUMP_prefix_strip, PrefixStrip);
3011   if (const opt::Arg *A = InputArgs.getLastArg(OBJDUMP_debug_vars_EQ)) {
3012     DbgVariables = StringSwitch<DebugVarsFormat>(A->getValue())
3013                        .Case("ascii", DVASCII)
3014                        .Case("unicode", DVUnicode)
3015                        .Default(DVInvalid);
3016     if (DbgVariables == DVInvalid)
3017       invalidArgValue(A);
3018   }
3019   parseIntArg(InputArgs, OBJDUMP_debug_vars_indent_EQ, DbgIndent);
3020 
3021   parseMachOOptions(InputArgs);
3022 
3023   // Parse -M (--disassembler-options) and deprecated
3024   // --x86-asm-syntax={att,intel}.
3025   //
3026   // Note, for x86, the asm dialect (AssemblerDialect) is initialized when the
3027   // MCAsmInfo is constructed. MCInstPrinter::applyTargetSpecificCLOption is
3028   // called too late. For now we have to use the internal cl::opt option.
3029   const char *AsmSyntax = nullptr;
3030   for (const auto *A : InputArgs.filtered(OBJDUMP_disassembler_options_EQ,
3031                                           OBJDUMP_x86_asm_syntax_att,
3032                                           OBJDUMP_x86_asm_syntax_intel)) {
3033     switch (A->getOption().getID()) {
3034     case OBJDUMP_x86_asm_syntax_att:
3035       AsmSyntax = "--x86-asm-syntax=att";
3036       continue;
3037     case OBJDUMP_x86_asm_syntax_intel:
3038       AsmSyntax = "--x86-asm-syntax=intel";
3039       continue;
3040     }
3041 
3042     SmallVector<StringRef, 2> Values;
3043     llvm::SplitString(A->getValue(), Values, ",");
3044     for (StringRef V : Values) {
3045       if (V == "att")
3046         AsmSyntax = "--x86-asm-syntax=att";
3047       else if (V == "intel")
3048         AsmSyntax = "--x86-asm-syntax=intel";
3049       else
3050         DisassemblerOptions.push_back(V.str());
3051     }
3052   }
3053   if (AsmSyntax) {
3054     const char *Argv[] = {"llvm-objdump", AsmSyntax};
3055     llvm::cl::ParseCommandLineOptions(2, Argv);
3056   }
3057 
3058   // objdump defaults to a.out if no filenames specified.
3059   if (InputFilenames.empty())
3060     InputFilenames.push_back("a.out");
3061 }
3062 
3063 int main(int argc, char **argv) {
3064   using namespace llvm;
3065   InitLLVM X(argc, argv);
3066 
3067   ToolName = argv[0];
3068   std::unique_ptr<CommonOptTable> T;
3069   OptSpecifier Unknown, HelpFlag, HelpHiddenFlag, VersionFlag;
3070 
3071   StringRef Stem = sys::path::stem(ToolName);
3072   auto Is = [=](StringRef Tool) {
3073     // We need to recognize the following filenames:
3074     //
3075     // llvm-objdump -> objdump
3076     // llvm-otool-10.exe -> otool
3077     // powerpc64-unknown-freebsd13-objdump -> objdump
3078     auto I = Stem.rfind_insensitive(Tool);
3079     return I != StringRef::npos &&
3080            (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
3081   };
3082   if (Is("otool")) {
3083     T = std::make_unique<OtoolOptTable>();
3084     Unknown = OTOOL_UNKNOWN;
3085     HelpFlag = OTOOL_help;
3086     HelpHiddenFlag = OTOOL_help_hidden;
3087     VersionFlag = OTOOL_version;
3088   } else {
3089     T = std::make_unique<ObjdumpOptTable>();
3090     Unknown = OBJDUMP_UNKNOWN;
3091     HelpFlag = OBJDUMP_help;
3092     HelpHiddenFlag = OBJDUMP_help_hidden;
3093     VersionFlag = OBJDUMP_version;
3094   }
3095 
3096   BumpPtrAllocator A;
3097   StringSaver Saver(A);
3098   opt::InputArgList InputArgs =
3099       T->parseArgs(argc, argv, Unknown, Saver,
3100                    [&](StringRef Msg) { reportCmdLineError(Msg); });
3101 
3102   if (InputArgs.size() == 0 || InputArgs.hasArg(HelpFlag)) {
3103     T->printHelp(ToolName);
3104     return 0;
3105   }
3106   if (InputArgs.hasArg(HelpHiddenFlag)) {
3107     T->printHelp(ToolName, /*ShowHidden=*/true);
3108     return 0;
3109   }
3110 
3111   // Initialize targets and assembly printers/parsers.
3112   InitializeAllTargetInfos();
3113   InitializeAllTargetMCs();
3114   InitializeAllDisassemblers();
3115 
3116   if (InputArgs.hasArg(VersionFlag)) {
3117     cl::PrintVersionMessage();
3118     if (!Is("otool")) {
3119       outs() << '\n';
3120       TargetRegistry::printRegisteredTargetsForVersion(outs());
3121     }
3122     return 0;
3123   }
3124 
3125   // Initialize debuginfod.
3126   const bool ShouldUseDebuginfodByDefault =
3127       HTTPClient::isAvailable() &&
3128       !ExitOnErr(getDefaultDebuginfodUrls()).empty();
3129   std::vector<std::string> DebugFileDirectories =
3130       InputArgs.getAllArgValues(OBJDUMP_debug_file_directory);
3131   if (InputArgs.hasFlag(OBJDUMP_debuginfod, OBJDUMP_no_debuginfod,
3132                         ShouldUseDebuginfodByDefault)) {
3133     HTTPClient::initialize();
3134     BIDFetcher =
3135         std::make_unique<DebuginfodFetcher>(std::move(DebugFileDirectories));
3136   } else {
3137     BIDFetcher =
3138         std::make_unique<BuildIDFetcher>(std::move(DebugFileDirectories));
3139   }
3140 
3141   if (Is("otool"))
3142     parseOtoolOptions(InputArgs);
3143   else
3144     parseObjdumpOptions(InputArgs);
3145 
3146   if (StartAddress >= StopAddress)
3147     reportCmdLineError("start address should be less than stop address");
3148 
3149   // Removes trailing separators from prefix.
3150   while (!Prefix.empty() && sys::path::is_separator(Prefix.back()))
3151     Prefix.pop_back();
3152 
3153   if (AllHeaders)
3154     ArchiveHeaders = FileHeaders = PrivateHeaders = Relocations =
3155         SectionHeaders = SymbolTable = true;
3156 
3157   if (DisassembleAll || PrintSource || PrintLines ||
3158       !DisassembleSymbols.empty())
3159     Disassemble = true;
3160 
3161   if (!ArchiveHeaders && !Disassemble && DwarfDumpType == DIDT_Null &&
3162       !DynamicRelocations && !FileHeaders && !PrivateHeaders && !RawClangAST &&
3163       !Relocations && !SectionHeaders && !SectionContents && !SymbolTable &&
3164       !DynamicSymbolTable && !UnwindInfo && !FaultMapSection && !Offloading &&
3165       !(MachOOpt &&
3166         (Bind || DataInCode || ChainedFixups || DyldInfo || DylibId ||
3167          DylibsUsed || ExportsTrie || FirstPrivateHeader || FunctionStarts ||
3168          IndirectSymbols || InfoPlist || LazyBind || LinkOptHints ||
3169          ObjcMetaData || Rebase || Rpaths || UniversalHeaders || WeakBind ||
3170          !FilterSections.empty()))) {
3171     T->printHelp(ToolName);
3172     return 2;
3173   }
3174 
3175   DisasmSymbolSet.insert(DisassembleSymbols.begin(), DisassembleSymbols.end());
3176 
3177   llvm::for_each(InputFilenames, dumpInput);
3178 
3179   warnOnNoMatchForSections();
3180 
3181   return EXIT_SUCCESS;
3182 }
3183