xref: /llvm-project/llvm/tools/llvm-objdump/llvm-objdump.cpp (revision 02b5d3bc3bb9108e8d18308395120f7e3161dbe8)
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);
1194     const uint64_t ThisAddr = SectionAddr + Index;
1195     DisAsm->getInstruction(Inst, Size, ThisBytes, ThisAddr, nulls());
1196     if (Size == 0)
1197       Size = std::min<uint64_t>(ThisBytes.size(),
1198                                 DisAsm->suggestBytesToSkip(ThisBytes, Index));
1199     Index += Size;
1200   }
1201   ArrayRef<uint64_t> LabelAddrsRef = SymbolizerPtr->getReferencedAddresses();
1202   // Copy and sort to remove duplicates.
1203   std::vector<uint64_t> LabelAddrs;
1204   LabelAddrs.insert(LabelAddrs.end(), LabelAddrsRef.begin(),
1205                     LabelAddrsRef.end());
1206   llvm::sort(LabelAddrs);
1207   LabelAddrs.resize(std::unique(LabelAddrs.begin(), LabelAddrs.end()) -
1208                     LabelAddrs.begin());
1209   // Add the labels.
1210   for (unsigned LabelNum = 0; LabelNum != LabelAddrs.size(); ++LabelNum) {
1211     auto Name = std::make_unique<std::string>();
1212     *Name = (Twine("L") + Twine(LabelNum)).str();
1213     SynthesizedLabelNames.push_back(std::move(Name));
1214     Symbols.push_back(SymbolInfoTy(
1215         LabelAddrs[LabelNum], *SynthesizedLabelNames.back(), ELF::STT_NOTYPE));
1216   }
1217   llvm::stable_sort(Symbols);
1218   // Recreate the symbolizer with the new symbols list.
1219   RelInfo.reset(Target->createMCRelocationInfo(TripleName, Ctx));
1220   Symbolizer.reset(Target->createMCSymbolizer(
1221       TripleName, nullptr, nullptr, &Symbols, &Ctx, std::move(RelInfo)));
1222   DisAsm->setSymbolizer(std::move(Symbolizer));
1223 }
1224 
1225 static StringRef getSegmentName(const MachOObjectFile *MachO,
1226                                 const SectionRef &Section) {
1227   if (MachO) {
1228     DataRefImpl DR = Section.getRawDataRefImpl();
1229     StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
1230     return SegmentName;
1231   }
1232   return "";
1233 }
1234 
1235 static void emitPostInstructionInfo(formatted_raw_ostream &FOS,
1236                                     const MCAsmInfo &MAI,
1237                                     const MCSubtargetInfo &STI,
1238                                     StringRef Comments,
1239                                     LiveVariablePrinter &LVP) {
1240   do {
1241     if (!Comments.empty()) {
1242       // Emit a line of comments.
1243       StringRef Comment;
1244       std::tie(Comment, Comments) = Comments.split('\n');
1245       // MAI.getCommentColumn() assumes that instructions are printed at the
1246       // position of 8, while getInstStartColumn() returns the actual position.
1247       unsigned CommentColumn =
1248           MAI.getCommentColumn() - 8 + getInstStartColumn(STI);
1249       FOS.PadToColumn(CommentColumn);
1250       FOS << MAI.getCommentString() << ' ' << Comment;
1251     }
1252     LVP.printAfterInst(FOS);
1253     FOS << '\n';
1254   } while (!Comments.empty());
1255   FOS.flush();
1256 }
1257 
1258 static void createFakeELFSections(ObjectFile &Obj) {
1259   assert(Obj.isELF());
1260   if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(&Obj))
1261     Elf32LEObj->createFakeSections();
1262   else if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(&Obj))
1263     Elf64LEObj->createFakeSections();
1264   else if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(&Obj))
1265     Elf32BEObj->createFakeSections();
1266   else if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(&Obj))
1267     Elf64BEObj->createFakeSections();
1268   else
1269     llvm_unreachable("Unsupported binary format");
1270 }
1271 
1272 // Tries to fetch a more complete version of the given object file using its
1273 // Build ID. Returns None if nothing was found.
1274 static Optional<OwningBinary<Binary>>
1275 fetchBinaryByBuildID(const ObjectFile &Obj) {
1276   Optional<object::BuildIDRef> BuildID = getBuildID(&Obj);
1277   if (!BuildID)
1278     return None;
1279   Optional<std::string> Path = BIDFetcher->fetch(*BuildID);
1280   if (!Path)
1281     return None;
1282   Expected<OwningBinary<Binary>> DebugBinary = createBinary(*Path);
1283   if (!DebugBinary) {
1284     reportWarning(toString(DebugBinary.takeError()), *Path);
1285     return None;
1286   }
1287   return std::move(*DebugBinary);
1288 }
1289 
1290 static void disassembleObject(const Target *TheTarget, ObjectFile &Obj,
1291                               const ObjectFile &DbgObj, MCContext &Ctx,
1292                               MCDisassembler *PrimaryDisAsm,
1293                               MCDisassembler *SecondaryDisAsm,
1294                               const MCInstrAnalysis *MIA, MCInstPrinter *IP,
1295                               const MCSubtargetInfo *PrimarySTI,
1296                               const MCSubtargetInfo *SecondarySTI,
1297                               PrettyPrinter &PIP, SourcePrinter &SP,
1298                               bool InlineRelocs) {
1299   const MCSubtargetInfo *STI = PrimarySTI;
1300   MCDisassembler *DisAsm = PrimaryDisAsm;
1301   bool PrimaryIsThumb = false;
1302   if (isArmElf(Obj))
1303     PrimaryIsThumb = STI->checkFeatures("+thumb-mode");
1304 
1305   std::map<SectionRef, std::vector<RelocationRef>> RelocMap;
1306   if (InlineRelocs)
1307     RelocMap = getRelocsMap(Obj);
1308   bool Is64Bits = Obj.getBytesInAddress() > 4;
1309 
1310   // Create a mapping from virtual address to symbol name.  This is used to
1311   // pretty print the symbols while disassembling.
1312   std::map<SectionRef, SectionSymbolsTy> AllSymbols;
1313   SectionSymbolsTy AbsoluteSymbols;
1314   const StringRef FileName = Obj.getFileName();
1315   const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(&Obj);
1316   for (const SymbolRef &Symbol : Obj.symbols()) {
1317     Expected<StringRef> NameOrErr = Symbol.getName();
1318     if (!NameOrErr) {
1319       reportWarning(toString(NameOrErr.takeError()), FileName);
1320       continue;
1321     }
1322     if (NameOrErr->empty() && !(Obj.isXCOFF() && SymbolDescription))
1323       continue;
1324 
1325     if (Obj.isELF() && getElfSymbolType(Obj, Symbol) == ELF::STT_SECTION)
1326       continue;
1327 
1328     if (MachO) {
1329       // __mh_(execute|dylib|dylinker|bundle|preload|object)_header are special
1330       // symbols that support MachO header introspection. They do not bind to
1331       // code locations and are irrelevant for disassembly.
1332       if (NameOrErr->startswith("__mh_") && NameOrErr->endswith("_header"))
1333         continue;
1334       // Don't ask a Mach-O STAB symbol for its section unless you know that
1335       // STAB symbol's section field refers to a valid section index. Otherwise
1336       // the symbol may error trying to load a section that does not exist.
1337       DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
1338       uint8_t NType = (MachO->is64Bit() ?
1339                        MachO->getSymbol64TableEntry(SymDRI).n_type:
1340                        MachO->getSymbolTableEntry(SymDRI).n_type);
1341       if (NType & MachO::N_STAB)
1342         continue;
1343     }
1344 
1345     section_iterator SecI = unwrapOrError(Symbol.getSection(), FileName);
1346     if (SecI != Obj.section_end())
1347       AllSymbols[*SecI].push_back(createSymbolInfo(Obj, Symbol));
1348     else
1349       AbsoluteSymbols.push_back(createSymbolInfo(Obj, Symbol));
1350   }
1351 
1352   if (AllSymbols.empty() && Obj.isELF())
1353     addDynamicElfSymbols(cast<ELFObjectFileBase>(Obj), AllSymbols);
1354 
1355   if (Obj.isWasm())
1356     addMissingWasmCodeSymbols(cast<WasmObjectFile>(Obj), AllSymbols);
1357 
1358   if (Obj.isELF() && Obj.sections().empty())
1359     createFakeELFSections(Obj);
1360 
1361   BumpPtrAllocator A;
1362   StringSaver Saver(A);
1363   addPltEntries(Obj, AllSymbols, Saver);
1364 
1365   // Create a mapping from virtual address to section. An empty section can
1366   // cause more than one section at the same address. Sort such sections to be
1367   // before same-addressed non-empty sections so that symbol lookups prefer the
1368   // non-empty section.
1369   std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
1370   for (SectionRef Sec : Obj.sections())
1371     SectionAddresses.emplace_back(Sec.getAddress(), Sec);
1372   llvm::stable_sort(SectionAddresses, [](const auto &LHS, const auto &RHS) {
1373     if (LHS.first != RHS.first)
1374       return LHS.first < RHS.first;
1375     return LHS.second.getSize() < RHS.second.getSize();
1376   });
1377 
1378   // Linked executables (.exe and .dll files) typically don't include a real
1379   // symbol table but they might contain an export table.
1380   if (const auto *COFFObj = dyn_cast<COFFObjectFile>(&Obj)) {
1381     for (const auto &ExportEntry : COFFObj->export_directories()) {
1382       StringRef Name;
1383       if (Error E = ExportEntry.getSymbolName(Name))
1384         reportError(std::move(E), Obj.getFileName());
1385       if (Name.empty())
1386         continue;
1387 
1388       uint32_t RVA;
1389       if (Error E = ExportEntry.getExportRVA(RVA))
1390         reportError(std::move(E), Obj.getFileName());
1391 
1392       uint64_t VA = COFFObj->getImageBase() + RVA;
1393       auto Sec = partition_point(
1394           SectionAddresses, [VA](const std::pair<uint64_t, SectionRef> &O) {
1395             return O.first <= VA;
1396           });
1397       if (Sec != SectionAddresses.begin()) {
1398         --Sec;
1399         AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE);
1400       } else
1401         AbsoluteSymbols.emplace_back(VA, Name, ELF::STT_NOTYPE);
1402     }
1403   }
1404 
1405   // Sort all the symbols, this allows us to use a simple binary search to find
1406   // Multiple symbols can have the same address. Use a stable sort to stabilize
1407   // the output.
1408   StringSet<> FoundDisasmSymbolSet;
1409   for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
1410     llvm::stable_sort(SecSyms.second);
1411   llvm::stable_sort(AbsoluteSymbols);
1412 
1413   std::unique_ptr<DWARFContext> DICtx;
1414   LiveVariablePrinter LVP(*Ctx.getRegisterInfo(), *STI);
1415 
1416   if (DbgVariables != DVDisabled) {
1417     DICtx = DWARFContext::create(DbgObj);
1418     for (const std::unique_ptr<DWARFUnit> &CU : DICtx->compile_units())
1419       LVP.addCompileUnit(CU->getUnitDIE(false));
1420   }
1421 
1422   LLVM_DEBUG(LVP.dump());
1423 
1424   std::unordered_map<uint64_t, BBAddrMap> AddrToBBAddrMap;
1425   auto ReadBBAddrMap = [&](Optional<unsigned> SectionIndex = None) {
1426     AddrToBBAddrMap.clear();
1427     if (const auto *Elf = dyn_cast<ELFObjectFileBase>(&Obj)) {
1428       auto BBAddrMapsOrErr = Elf->readBBAddrMap(SectionIndex);
1429       if (!BBAddrMapsOrErr)
1430           reportWarning(toString(BBAddrMapsOrErr.takeError()),
1431                         Obj.getFileName());
1432       for (auto &FunctionBBAddrMap : *BBAddrMapsOrErr)
1433         AddrToBBAddrMap.emplace(FunctionBBAddrMap.Addr,
1434                                 std::move(FunctionBBAddrMap));
1435     }
1436   };
1437 
1438   // For non-relocatable objects, Read all LLVM_BB_ADDR_MAP sections into a
1439   // single mapping, since they don't have any conflicts.
1440   if (SymbolizeOperands && !Obj.isRelocatableObject())
1441     ReadBBAddrMap();
1442 
1443   for (const SectionRef &Section : ToolSectionFilter(Obj)) {
1444     if (FilterSections.empty() && !DisassembleAll &&
1445         (!Section.isText() || Section.isVirtual()))
1446       continue;
1447 
1448     uint64_t SectionAddr = Section.getAddress();
1449     uint64_t SectSize = Section.getSize();
1450     if (!SectSize)
1451       continue;
1452 
1453     // For relocatable object files, read the LLVM_BB_ADDR_MAP section
1454     // corresponding to this section, if present.
1455     if (SymbolizeOperands && Obj.isRelocatableObject())
1456       ReadBBAddrMap(Section.getIndex());
1457 
1458     // Get the list of all the symbols in this section.
1459     SectionSymbolsTy &Symbols = AllSymbols[Section];
1460     std::vector<MappingSymbolPair> MappingSymbols;
1461     if (hasMappingSymbols(Obj)) {
1462       for (const auto &Symb : Symbols) {
1463         uint64_t Address = Symb.Addr;
1464         StringRef Name = Symb.Name;
1465         if (Name.startswith("$d"))
1466           MappingSymbols.emplace_back(Address - SectionAddr, 'd');
1467         if (Name.startswith("$x"))
1468           MappingSymbols.emplace_back(Address - SectionAddr, 'x');
1469         if (Name.startswith("$a"))
1470           MappingSymbols.emplace_back(Address - SectionAddr, 'a');
1471         if (Name.startswith("$t"))
1472           MappingSymbols.emplace_back(Address - SectionAddr, 't');
1473       }
1474     }
1475 
1476     llvm::sort(MappingSymbols);
1477 
1478     ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(
1479         unwrapOrError(Section.getContents(), Obj.getFileName()));
1480 
1481     std::vector<std::unique_ptr<std::string>> SynthesizedLabelNames;
1482     if (Obj.isELF() && Obj.getArch() == Triple::amdgcn) {
1483       // AMDGPU disassembler uses symbolizer for printing labels
1484       addSymbolizer(Ctx, TheTarget, TripleName, DisAsm, SectionAddr, Bytes,
1485                     Symbols, SynthesizedLabelNames);
1486     }
1487 
1488     StringRef SegmentName = getSegmentName(MachO, Section);
1489     StringRef SectionName = unwrapOrError(Section.getName(), Obj.getFileName());
1490     // If the section has no symbol at the start, just insert a dummy one.
1491     if (Symbols.empty() || Symbols[0].Addr != 0) {
1492       Symbols.insert(Symbols.begin(),
1493                      createDummySymbolInfo(Obj, SectionAddr, SectionName,
1494                                            Section.isText() ? ELF::STT_FUNC
1495                                                             : ELF::STT_OBJECT));
1496     }
1497 
1498     SmallString<40> Comments;
1499     raw_svector_ostream CommentStream(Comments);
1500 
1501     uint64_t VMAAdjustment = 0;
1502     if (shouldAdjustVA(Section))
1503       VMAAdjustment = AdjustVMA;
1504 
1505     // In executable and shared objects, r_offset holds a virtual address.
1506     // Subtract SectionAddr from the r_offset field of a relocation to get
1507     // the section offset.
1508     uint64_t RelAdjustment = Obj.isRelocatableObject() ? 0 : SectionAddr;
1509     uint64_t Size;
1510     uint64_t Index;
1511     bool PrintedSection = false;
1512     std::vector<RelocationRef> Rels = RelocMap[Section];
1513     std::vector<RelocationRef>::const_iterator RelCur = Rels.begin();
1514     std::vector<RelocationRef>::const_iterator RelEnd = Rels.end();
1515 
1516     // Loop over each chunk of code between two points where at least
1517     // one symbol is defined.
1518     for (size_t SI = 0, SE = Symbols.size(); SI != SE;) {
1519       // Advance SI past all the symbols starting at the same address,
1520       // and make an ArrayRef of them.
1521       unsigned FirstSI = SI;
1522       uint64_t Start = Symbols[SI].Addr;
1523       ArrayRef<SymbolInfoTy> SymbolsHere;
1524       while (SI != SE && Symbols[SI].Addr == Start)
1525         ++SI;
1526       SymbolsHere = ArrayRef<SymbolInfoTy>(&Symbols[FirstSI], SI - FirstSI);
1527 
1528       // Get the demangled names of all those symbols. We end up with a vector
1529       // of StringRef that holds the names we're going to use, and a vector of
1530       // std::string that stores the new strings returned by demangle(), if
1531       // any. If we don't call demangle() then that vector can stay empty.
1532       std::vector<StringRef> SymNamesHere;
1533       std::vector<std::string> DemangledSymNamesHere;
1534       if (Demangle) {
1535         // Fetch the demangled names and store them locally.
1536         for (const SymbolInfoTy &Symbol : SymbolsHere)
1537           DemangledSymNamesHere.push_back(demangle(Symbol.Name.str()));
1538         // Now we've finished modifying that vector, it's safe to make
1539         // a vector of StringRefs pointing into it.
1540         SymNamesHere.insert(SymNamesHere.begin(), DemangledSymNamesHere.begin(),
1541                             DemangledSymNamesHere.end());
1542       } else {
1543         for (const SymbolInfoTy &Symbol : SymbolsHere)
1544           SymNamesHere.push_back(Symbol.Name);
1545       }
1546 
1547       // Distinguish ELF data from code symbols, which will be used later on to
1548       // decide whether to 'disassemble' this chunk as a data declaration via
1549       // dumpELFData(), or whether to treat it as code.
1550       //
1551       // If data _and_ code symbols are defined at the same address, the code
1552       // takes priority, on the grounds that disassembling code is our main
1553       // purpose here, and it would be a worse failure to _not_ interpret
1554       // something that _was_ meaningful as code than vice versa.
1555       //
1556       // Any ELF symbol type that is not clearly data will be regarded as code.
1557       // In particular, one of the uses of STT_NOTYPE is for branch targets
1558       // inside functions, for which STT_FUNC would be inaccurate.
1559       //
1560       // So here, we spot whether there's any non-data symbol present at all,
1561       // and only set the DisassembleAsData flag if there isn't. Also, we use
1562       // this distinction to inform the decision of which symbol to print at
1563       // the head of the section, so that if we're printing code, we print a
1564       // code-related symbol name to go with it.
1565       bool DisassembleAsData = false;
1566       size_t DisplaySymIndex = SymbolsHere.size() - 1;
1567       if (Obj.isELF() && !DisassembleAll && Section.isText()) {
1568         DisassembleAsData = true; // unless we find a code symbol below
1569 
1570         for (size_t i = 0; i < SymbolsHere.size(); ++i) {
1571           uint8_t SymTy = SymbolsHere[i].Type;
1572           if (SymTy != ELF::STT_OBJECT && SymTy != ELF::STT_COMMON) {
1573             DisassembleAsData = false;
1574             DisplaySymIndex = i;
1575           }
1576         }
1577       }
1578 
1579       // Decide which symbol(s) from this collection we're going to print.
1580       std::vector<bool> SymsToPrint(SymbolsHere.size(), false);
1581       // If the user has given the --disassemble-symbols option, then we must
1582       // display every symbol in that set, and no others.
1583       if (!DisasmSymbolSet.empty()) {
1584         bool FoundAny = false;
1585         for (size_t i = 0; i < SymbolsHere.size(); ++i) {
1586           if (DisasmSymbolSet.count(SymNamesHere[i])) {
1587             SymsToPrint[i] = true;
1588             FoundAny = true;
1589           }
1590         }
1591 
1592         // And if none of the symbols here is one that the user asked for, skip
1593         // disassembling this entire chunk of code.
1594         if (!FoundAny)
1595           continue;
1596       } else {
1597         // Otherwise, print whichever symbol at this location is last in the
1598         // Symbols array, because that array is pre-sorted in a way intended to
1599         // correlate with priority of which symbol to display.
1600         SymsToPrint[DisplaySymIndex] = true;
1601       }
1602 
1603       // Now that we know we're disassembling this section, override the choice
1604       // of which symbols to display by printing _all_ of them at this address
1605       // if the user asked for all symbols.
1606       //
1607       // That way, '--show-all-symbols --disassemble-symbol=foo' will print
1608       // only the chunk of code headed by 'foo', but also show any other
1609       // symbols defined at that address, such as aliases for 'foo', or the ARM
1610       // mapping symbol preceding its code.
1611       if (ShowAllSymbols) {
1612         for (size_t i = 0; i < SymbolsHere.size(); ++i)
1613           SymsToPrint[i] = true;
1614       }
1615 
1616       if (Start < SectionAddr || StopAddress <= Start)
1617         continue;
1618 
1619       for (size_t i = 0; i < SymbolsHere.size(); ++i)
1620         FoundDisasmSymbolSet.insert(SymNamesHere[i]);
1621 
1622       // The end is the section end, the beginning of the next symbol, or
1623       // --stop-address.
1624       uint64_t End = std::min<uint64_t>(SectionAddr + SectSize, StopAddress);
1625       if (SI < SE)
1626         End = std::min(End, Symbols[SI].Addr);
1627       if (Start >= End || End <= StartAddress)
1628         continue;
1629       Start -= SectionAddr;
1630       End -= SectionAddr;
1631 
1632       if (!PrintedSection) {
1633         PrintedSection = true;
1634         outs() << "\nDisassembly of section ";
1635         if (!SegmentName.empty())
1636           outs() << SegmentName << ",";
1637         outs() << SectionName << ":\n";
1638       }
1639 
1640       outs() << '\n';
1641 
1642       for (size_t i = 0; i < SymbolsHere.size(); ++i) {
1643         if (!SymsToPrint[i])
1644           continue;
1645 
1646         const SymbolInfoTy &Symbol = SymbolsHere[i];
1647         const StringRef SymbolName = SymNamesHere[i];
1648 
1649         if (LeadingAddr)
1650           outs() << format(Is64Bits ? "%016" PRIx64 " " : "%08" PRIx64 " ",
1651                            SectionAddr + Start + VMAAdjustment);
1652         if (Obj.isXCOFF() && SymbolDescription) {
1653           outs() << getXCOFFSymbolDescription(Symbol, SymbolName) << ":\n";
1654         } else
1655           outs() << '<' << SymbolName << ">:\n";
1656       }
1657 
1658       // Don't print raw contents of a virtual section. A virtual section
1659       // doesn't have any contents in the file.
1660       if (Section.isVirtual()) {
1661         outs() << "...\n";
1662         continue;
1663       }
1664 
1665       // See if any of the symbols defined at this location triggers target-
1666       // specific disassembly behavior, e.g. of special descriptors or function
1667       // prelude information.
1668       //
1669       // We stop this loop at the first symbol that triggers some kind of
1670       // interesting behavior (if any), on the assumption that if two symbols
1671       // defined at the same address trigger two conflicting symbol handlers,
1672       // the object file is probably confused anyway, and it would make even
1673       // less sense to present the output of _both_ handlers, because that
1674       // would describe the same data twice.
1675       for (size_t SHI = 0; SHI < SymbolsHere.size(); ++SHI) {
1676         SymbolInfoTy Symbol = SymbolsHere[SHI];
1677 
1678         auto Status =
1679             DisAsm->onSymbolStart(Symbol, Size, Bytes.slice(Start, End - Start),
1680                                   SectionAddr + Start, CommentStream);
1681 
1682         if (!Status) {
1683           // If onSymbolStart returns None, that means it didn't trigger any
1684           // interesting handling for this symbol. Try the other symbols
1685           // defined at this address.
1686           continue;
1687         }
1688 
1689         if (Status.value() == MCDisassembler::Fail) {
1690           // If onSymbolStart returns Fail, that means it identified some kind
1691           // of special data at this address, but wasn't able to disassemble it
1692           // meaningfully. So we fall back to disassembling the failed region
1693           // as bytes, assuming that the target detected the failure before
1694           // printing anything.
1695           //
1696           // Return values Success or SoftFail (i.e no 'real' failure) are
1697           // expected to mean that the target has emitted its own output.
1698           //
1699           // Either way, 'Size' will have been set to the amount of data
1700           // covered by whatever prologue the target identified. So we advance
1701           // our own position to beyond that. Sometimes that will be the entire
1702           // distance to the next symbol, and sometimes it will be just a
1703           // prologue and we should start disassembling instructions from where
1704           // it left off.
1705           outs() << "// Error in decoding " << SymNamesHere[SHI]
1706                  << " : Decoding failed region as bytes.\n";
1707           for (uint64_t I = 0; I < Size; ++I) {
1708             outs() << "\t.byte\t " << format_hex(Bytes[I], 1, /*Upper=*/true)
1709                    << "\n";
1710           }
1711         }
1712         Start += Size;
1713         break;
1714       }
1715 
1716       Index = Start;
1717       if (SectionAddr < StartAddress)
1718         Index = std::max<uint64_t>(Index, StartAddress - SectionAddr);
1719 
1720       if (DisassembleAsData) {
1721         dumpELFData(SectionAddr, Index, End, Bytes);
1722         Index = End;
1723         continue;
1724       }
1725 
1726       bool DumpARMELFData = false;
1727       formatted_raw_ostream FOS(outs());
1728 
1729       std::unordered_map<uint64_t, std::string> AllLabels;
1730       std::unordered_map<uint64_t, std::vector<std::string>> BBAddrMapLabels;
1731       if (SymbolizeOperands) {
1732         collectLocalBranchTargets(Bytes, MIA, DisAsm, IP, PrimarySTI,
1733                                   SectionAddr, Index, End, AllLabels);
1734         collectBBAddrMapLabels(AddrToBBAddrMap, SectionAddr, Index, End,
1735                                BBAddrMapLabels);
1736       }
1737 
1738       while (Index < End) {
1739         // ARM and AArch64 ELF binaries can interleave data and text in the
1740         // same section. We rely on the markers introduced to understand what
1741         // we need to dump. If the data marker is within a function, it is
1742         // denoted as a word/short etc.
1743         if (!MappingSymbols.empty()) {
1744           char Kind = getMappingSymbolKind(MappingSymbols, Index);
1745           DumpARMELFData = Kind == 'd';
1746           if (SecondarySTI) {
1747             if (Kind == 'a') {
1748               STI = PrimaryIsThumb ? SecondarySTI : PrimarySTI;
1749               DisAsm = PrimaryIsThumb ? SecondaryDisAsm : PrimaryDisAsm;
1750             } else if (Kind == 't') {
1751               STI = PrimaryIsThumb ? PrimarySTI : SecondarySTI;
1752               DisAsm = PrimaryIsThumb ? PrimaryDisAsm : SecondaryDisAsm;
1753             }
1754           }
1755         }
1756 
1757         if (DumpARMELFData) {
1758           Size = dumpARMELFData(SectionAddr, Index, End, Obj, Bytes,
1759                                 MappingSymbols, *STI, FOS);
1760         } else {
1761           // When -z or --disassemble-zeroes are given we always dissasemble
1762           // them. Otherwise we might want to skip zero bytes we see.
1763           if (!DisassembleZeroes) {
1764             uint64_t MaxOffset = End - Index;
1765             // For --reloc: print zero blocks patched by relocations, so that
1766             // relocations can be shown in the dump.
1767             if (RelCur != RelEnd)
1768               MaxOffset = std::min(RelCur->getOffset() - RelAdjustment - Index,
1769                                    MaxOffset);
1770 
1771             if (size_t N =
1772                     countSkippableZeroBytes(Bytes.slice(Index, MaxOffset))) {
1773               FOS << "\t\t..." << '\n';
1774               Index += N;
1775               continue;
1776             }
1777           }
1778 
1779           // Print local label if there's any.
1780           auto Iter1 = BBAddrMapLabels.find(SectionAddr + Index);
1781           if (Iter1 != BBAddrMapLabels.end()) {
1782             for (StringRef Label : Iter1->second)
1783               FOS << "<" << Label << ">:\n";
1784           } else {
1785             auto Iter2 = AllLabels.find(SectionAddr + Index);
1786             if (Iter2 != AllLabels.end())
1787               FOS << "<" << Iter2->second << ">:\n";
1788           }
1789 
1790           // Disassemble a real instruction or a data when disassemble all is
1791           // provided
1792           MCInst Inst;
1793           ArrayRef<uint8_t> ThisBytes = Bytes.slice(Index);
1794           uint64_t ThisAddr = SectionAddr + Index;
1795           bool Disassembled = DisAsm->getInstruction(Inst, Size, ThisBytes,
1796                                                      ThisAddr, CommentStream);
1797           if (Size == 0)
1798             Size = std::min<uint64_t>(
1799                 ThisBytes.size(),
1800                 DisAsm->suggestBytesToSkip(ThisBytes, ThisAddr));
1801 
1802           LVP.update({Index, Section.getIndex()},
1803                      {Index + Size, Section.getIndex()}, Index + Size != End);
1804 
1805           IP->setCommentStream(CommentStream);
1806 
1807           PIP.printInst(
1808               *IP, Disassembled ? &Inst : nullptr, Bytes.slice(Index, Size),
1809               {SectionAddr + Index + VMAAdjustment, Section.getIndex()}, FOS,
1810               "", *STI, &SP, Obj.getFileName(), &Rels, LVP);
1811 
1812           IP->setCommentStream(llvm::nulls());
1813 
1814           // If disassembly has failed, avoid analysing invalid/incomplete
1815           // instruction information. Otherwise, try to resolve the target
1816           // address (jump target or memory operand address) and print it on the
1817           // right of the instruction.
1818           if (Disassembled && MIA) {
1819             // Branch targets are printed just after the instructions.
1820             llvm::raw_ostream *TargetOS = &FOS;
1821             uint64_t Target;
1822             bool PrintTarget =
1823                 MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target);
1824             if (!PrintTarget)
1825               if (Optional<uint64_t> MaybeTarget =
1826                       MIA->evaluateMemoryOperandAddress(
1827                           Inst, STI, SectionAddr + Index, Size)) {
1828                 Target = *MaybeTarget;
1829                 PrintTarget = true;
1830                 // Do not print real address when symbolizing.
1831                 if (!SymbolizeOperands) {
1832                   // Memory operand addresses are printed as comments.
1833                   TargetOS = &CommentStream;
1834                   *TargetOS << "0x" << Twine::utohexstr(Target);
1835                 }
1836               }
1837             if (PrintTarget) {
1838               // In a relocatable object, the target's section must reside in
1839               // the same section as the call instruction or it is accessed
1840               // through a relocation.
1841               //
1842               // In a non-relocatable object, the target may be in any section.
1843               // In that case, locate the section(s) containing the target
1844               // address and find the symbol in one of those, if possible.
1845               //
1846               // N.B. We don't walk the relocations in the relocatable case yet.
1847               std::vector<const SectionSymbolsTy *> TargetSectionSymbols;
1848               if (!Obj.isRelocatableObject()) {
1849                 auto It = llvm::partition_point(
1850                     SectionAddresses,
1851                     [=](const std::pair<uint64_t, SectionRef> &O) {
1852                       return O.first <= Target;
1853                     });
1854                 uint64_t TargetSecAddr = 0;
1855                 while (It != SectionAddresses.begin()) {
1856                   --It;
1857                   if (TargetSecAddr == 0)
1858                     TargetSecAddr = It->first;
1859                   if (It->first != TargetSecAddr)
1860                     break;
1861                   TargetSectionSymbols.push_back(&AllSymbols[It->second]);
1862                 }
1863               } else {
1864                 TargetSectionSymbols.push_back(&Symbols);
1865               }
1866               TargetSectionSymbols.push_back(&AbsoluteSymbols);
1867 
1868               // Find the last symbol in the first candidate section whose
1869               // offset is less than or equal to the target. If there are no
1870               // such symbols, try in the next section and so on, before finally
1871               // using the nearest preceding absolute symbol (if any), if there
1872               // are no other valid symbols.
1873               const SymbolInfoTy *TargetSym = nullptr;
1874               for (const SectionSymbolsTy *TargetSymbols :
1875                    TargetSectionSymbols) {
1876                 auto It = llvm::partition_point(
1877                     *TargetSymbols,
1878                     [=](const SymbolInfoTy &O) { return O.Addr <= Target; });
1879                 if (It != TargetSymbols->begin()) {
1880                   TargetSym = &*(It - 1);
1881                   break;
1882                 }
1883               }
1884 
1885               // Print the labels corresponding to the target if there's any.
1886               bool BBAddrMapLabelAvailable = BBAddrMapLabels.count(Target);
1887               bool LabelAvailable = AllLabels.count(Target);
1888               if (TargetSym != nullptr) {
1889                 uint64_t TargetAddress = TargetSym->Addr;
1890                 uint64_t Disp = Target - TargetAddress;
1891                 std::string TargetName = TargetSym->Name.str();
1892                 if (Demangle)
1893                   TargetName = demangle(TargetName);
1894 
1895                 *TargetOS << " <";
1896                 if (!Disp) {
1897                   // Always Print the binary symbol precisely corresponding to
1898                   // the target address.
1899                   *TargetOS << TargetName;
1900                 } else if (BBAddrMapLabelAvailable) {
1901                   *TargetOS << BBAddrMapLabels[Target].front();
1902                 } else if (LabelAvailable) {
1903                   *TargetOS << AllLabels[Target];
1904                 } else {
1905                   // Always Print the binary symbol plus an offset if there's no
1906                   // local label corresponding to the target address.
1907                   *TargetOS << TargetName << "+0x" << Twine::utohexstr(Disp);
1908                 }
1909                 *TargetOS << ">";
1910               } else if (BBAddrMapLabelAvailable) {
1911                 *TargetOS << " <" << BBAddrMapLabels[Target].front() << ">";
1912               } else if (LabelAvailable) {
1913                 *TargetOS << " <" << AllLabels[Target] << ">";
1914               }
1915               // By convention, each record in the comment stream should be
1916               // terminated.
1917               if (TargetOS == &CommentStream)
1918                 *TargetOS << "\n";
1919             }
1920           }
1921         }
1922 
1923         assert(Ctx.getAsmInfo());
1924         emitPostInstructionInfo(FOS, *Ctx.getAsmInfo(), *STI,
1925                                 CommentStream.str(), LVP);
1926         Comments.clear();
1927 
1928         // Hexagon does this in pretty printer
1929         if (Obj.getArch() != Triple::hexagon) {
1930           // Print relocation for instruction and data.
1931           while (RelCur != RelEnd) {
1932             uint64_t Offset = RelCur->getOffset() - RelAdjustment;
1933             // If this relocation is hidden, skip it.
1934             if (getHidden(*RelCur) || SectionAddr + Offset < StartAddress) {
1935               ++RelCur;
1936               continue;
1937             }
1938 
1939             // Stop when RelCur's offset is past the disassembled
1940             // instruction/data. Note that it's possible the disassembled data
1941             // is not the complete data: we might see the relocation printed in
1942             // the middle of the data, but this matches the binutils objdump
1943             // output.
1944             if (Offset >= Index + Size)
1945               break;
1946 
1947             // When --adjust-vma is used, update the address printed.
1948             if (RelCur->getSymbol() != Obj.symbol_end()) {
1949               Expected<section_iterator> SymSI =
1950                   RelCur->getSymbol()->getSection();
1951               if (SymSI && *SymSI != Obj.section_end() &&
1952                   shouldAdjustVA(**SymSI))
1953                 Offset += AdjustVMA;
1954             }
1955 
1956             printRelocation(FOS, Obj.getFileName(), *RelCur,
1957                             SectionAddr + Offset, Is64Bits);
1958             LVP.printAfterOtherLine(FOS, true);
1959             ++RelCur;
1960           }
1961         }
1962 
1963         Index += Size;
1964       }
1965     }
1966   }
1967   StringSet<> MissingDisasmSymbolSet =
1968       set_difference(DisasmSymbolSet, FoundDisasmSymbolSet);
1969   for (StringRef Sym : MissingDisasmSymbolSet.keys())
1970     reportWarning("failed to disassemble missing symbol " + Sym, FileName);
1971 }
1972 
1973 static void disassembleObject(ObjectFile *Obj, bool InlineRelocs) {
1974   // If information useful for showing the disassembly is missing, try to find a
1975   // more complete binary and disassemble that instead.
1976   OwningBinary<Binary> FetchedBinary;
1977   if (Obj->symbols().empty()) {
1978     if (Optional<OwningBinary<Binary>> FetchedBinaryOpt =
1979             fetchBinaryByBuildID(*Obj)) {
1980       if (auto *O = dyn_cast<ObjectFile>(FetchedBinaryOpt->getBinary())) {
1981         if (!O->symbols().empty() ||
1982             (!O->sections().empty() && Obj->sections().empty())) {
1983           FetchedBinary = std::move(*FetchedBinaryOpt);
1984           Obj = O;
1985         }
1986       }
1987     }
1988   }
1989 
1990   const Target *TheTarget = getTarget(Obj);
1991 
1992   // Package up features to be passed to target/subtarget
1993   SubtargetFeatures Features = Obj->getFeatures();
1994   if (!MAttrs.empty()) {
1995     for (unsigned I = 0; I != MAttrs.size(); ++I)
1996       Features.AddFeature(MAttrs[I]);
1997   } else if (MCPU.empty() && Obj->getArch() == llvm::Triple::aarch64) {
1998     Features.AddFeature("+all");
1999   }
2000 
2001   std::unique_ptr<const MCRegisterInfo> MRI(
2002       TheTarget->createMCRegInfo(TripleName));
2003   if (!MRI)
2004     reportError(Obj->getFileName(),
2005                 "no register info for target " + TripleName);
2006 
2007   // Set up disassembler.
2008   MCTargetOptions MCOptions;
2009   std::unique_ptr<const MCAsmInfo> AsmInfo(
2010       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
2011   if (!AsmInfo)
2012     reportError(Obj->getFileName(),
2013                 "no assembly info for target " + TripleName);
2014 
2015   if (MCPU.empty())
2016     MCPU = Obj->tryGetCPUName().value_or("").str();
2017 
2018   if (isArmElf(*Obj)) {
2019     // When disassembling big-endian Arm ELF, the instruction endianness is
2020     // determined in a complex way. In relocatable objects, AAELF32 mandates
2021     // that instruction endianness matches the ELF file endianness; in
2022     // executable images, that's true unless the file header has the EF_ARM_BE8
2023     // flag, in which case instructions are little-endian regardless of data
2024     // endianness.
2025     //
2026     // We must set the big-endian-instructions SubtargetFeature to make the
2027     // disassembler read the instructions the right way round, and also tell
2028     // our own prettyprinter to retrieve the encodings the same way to print in
2029     // hex.
2030     const auto *Elf32BE = dyn_cast<ELF32BEObjectFile>(Obj);
2031 
2032     if (Elf32BE && (Elf32BE->isRelocatableObject() ||
2033                     !(Elf32BE->getPlatformFlags() & ELF::EF_ARM_BE8))) {
2034       Features.AddFeature("+big-endian-instructions");
2035       ARMPrettyPrinterInst.setInstructionEndianness(llvm::support::big);
2036     } else {
2037       ARMPrettyPrinterInst.setInstructionEndianness(llvm::support::little);
2038     }
2039   }
2040 
2041   std::unique_ptr<const MCSubtargetInfo> STI(
2042       TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString()));
2043   if (!STI)
2044     reportError(Obj->getFileName(),
2045                 "no subtarget info for target " + TripleName);
2046   std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
2047   if (!MII)
2048     reportError(Obj->getFileName(),
2049                 "no instruction info for target " + TripleName);
2050   MCContext Ctx(Triple(TripleName), AsmInfo.get(), MRI.get(), STI.get());
2051   // FIXME: for now initialize MCObjectFileInfo with default values
2052   std::unique_ptr<MCObjectFileInfo> MOFI(
2053       TheTarget->createMCObjectFileInfo(Ctx, /*PIC=*/false));
2054   Ctx.setObjectFileInfo(MOFI.get());
2055 
2056   std::unique_ptr<MCDisassembler> DisAsm(
2057       TheTarget->createMCDisassembler(*STI, Ctx));
2058   if (!DisAsm)
2059     reportError(Obj->getFileName(), "no disassembler for target " + TripleName);
2060 
2061   // If we have an ARM object file, we need a second disassembler, because
2062   // ARM CPUs have two different instruction sets: ARM mode, and Thumb mode.
2063   // We use mapping symbols to switch between the two assemblers, where
2064   // appropriate.
2065   std::unique_ptr<MCDisassembler> SecondaryDisAsm;
2066   std::unique_ptr<const MCSubtargetInfo> SecondarySTI;
2067   if (isArmElf(*Obj) && !STI->checkFeatures("+mclass")) {
2068     if (STI->checkFeatures("+thumb-mode"))
2069       Features.AddFeature("-thumb-mode");
2070     else
2071       Features.AddFeature("+thumb-mode");
2072     SecondarySTI.reset(TheTarget->createMCSubtargetInfo(TripleName, MCPU,
2073                                                         Features.getString()));
2074     SecondaryDisAsm.reset(TheTarget->createMCDisassembler(*SecondarySTI, Ctx));
2075   }
2076 
2077   std::unique_ptr<const MCInstrAnalysis> MIA(
2078       TheTarget->createMCInstrAnalysis(MII.get()));
2079 
2080   int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
2081   std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
2082       Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
2083   if (!IP)
2084     reportError(Obj->getFileName(),
2085                 "no instruction printer for target " + TripleName);
2086   IP->setPrintImmHex(PrintImmHex);
2087   IP->setPrintBranchImmAsAddress(true);
2088   IP->setSymbolizeOperands(SymbolizeOperands);
2089   IP->setMCInstrAnalysis(MIA.get());
2090 
2091   PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
2092 
2093   const ObjectFile *DbgObj = Obj;
2094   if (!FetchedBinary.getBinary() && !Obj->hasDebugInfo()) {
2095     if (Optional<OwningBinary<Binary>> DebugBinaryOpt =
2096             fetchBinaryByBuildID(*Obj)) {
2097       if (auto *FetchedObj =
2098               dyn_cast<const ObjectFile>(DebugBinaryOpt->getBinary())) {
2099         if (FetchedObj->hasDebugInfo()) {
2100           FetchedBinary = std::move(*DebugBinaryOpt);
2101           DbgObj = FetchedObj;
2102         }
2103       }
2104     }
2105   }
2106 
2107   std::unique_ptr<object::Binary> DSYMBinary;
2108   std::unique_ptr<MemoryBuffer> DSYMBuf;
2109   if (!DbgObj->hasDebugInfo()) {
2110     if (const MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*Obj)) {
2111       DbgObj = objdump::getMachODSymObject(MachOOF, Obj->getFileName(),
2112                                            DSYMBinary, DSYMBuf);
2113       if (!DbgObj)
2114         return;
2115     }
2116   }
2117 
2118   SourcePrinter SP(DbgObj, TheTarget->getName());
2119 
2120   for (StringRef Opt : DisassemblerOptions)
2121     if (!IP->applyTargetSpecificCLOption(Opt))
2122       reportError(Obj->getFileName(),
2123                   "Unrecognized disassembler option: " + Opt);
2124 
2125   disassembleObject(TheTarget, *Obj, *DbgObj, Ctx, DisAsm.get(),
2126                     SecondaryDisAsm.get(), MIA.get(), IP.get(), STI.get(),
2127                     SecondarySTI.get(), PIP, SP, InlineRelocs);
2128 }
2129 
2130 void objdump::printRelocations(const ObjectFile *Obj) {
2131   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
2132                                                  "%08" PRIx64;
2133 
2134   // Build a mapping from relocation target to a vector of relocation
2135   // sections. Usually, there is an only one relocation section for
2136   // each relocated section.
2137   MapVector<SectionRef, std::vector<SectionRef>> SecToRelSec;
2138   uint64_t Ndx;
2139   for (const SectionRef &Section : ToolSectionFilter(*Obj, &Ndx)) {
2140     if (Obj->isELF() && (ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC))
2141       continue;
2142     if (Section.relocation_begin() == Section.relocation_end())
2143       continue;
2144     Expected<section_iterator> SecOrErr = Section.getRelocatedSection();
2145     if (!SecOrErr)
2146       reportError(Obj->getFileName(),
2147                   "section (" + Twine(Ndx) +
2148                       "): unable to get a relocation target: " +
2149                       toString(SecOrErr.takeError()));
2150     SecToRelSec[**SecOrErr].push_back(Section);
2151   }
2152 
2153   for (std::pair<SectionRef, std::vector<SectionRef>> &P : SecToRelSec) {
2154     StringRef SecName = unwrapOrError(P.first.getName(), Obj->getFileName());
2155     outs() << "\nRELOCATION RECORDS FOR [" << SecName << "]:\n";
2156     uint32_t OffsetPadding = (Obj->getBytesInAddress() > 4 ? 16 : 8);
2157     uint32_t TypePadding = 24;
2158     outs() << left_justify("OFFSET", OffsetPadding) << " "
2159            << left_justify("TYPE", TypePadding) << " "
2160            << "VALUE\n";
2161 
2162     for (SectionRef Section : P.second) {
2163       for (const RelocationRef &Reloc : Section.relocations()) {
2164         uint64_t Address = Reloc.getOffset();
2165         SmallString<32> RelocName;
2166         SmallString<32> ValueStr;
2167         if (Address < StartAddress || Address > StopAddress || getHidden(Reloc))
2168           continue;
2169         Reloc.getTypeName(RelocName);
2170         if (Error E = getRelocationValueString(Reloc, ValueStr))
2171           reportError(std::move(E), Obj->getFileName());
2172 
2173         outs() << format(Fmt.data(), Address) << " "
2174                << left_justify(RelocName, TypePadding) << " " << ValueStr
2175                << "\n";
2176       }
2177     }
2178   }
2179 }
2180 
2181 void objdump::printDynamicRelocations(const ObjectFile *Obj) {
2182   // For the moment, this option is for ELF only
2183   if (!Obj->isELF())
2184     return;
2185 
2186   const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj);
2187   if (!Elf || !any_of(Elf->sections(), [](const ELFSectionRef Sec) {
2188         return Sec.getType() == ELF::SHT_DYNAMIC;
2189       })) {
2190     reportError(Obj->getFileName(), "not a dynamic object");
2191     return;
2192   }
2193 
2194   std::vector<SectionRef> DynRelSec = Obj->dynamic_relocation_sections();
2195   if (DynRelSec.empty())
2196     return;
2197 
2198   outs() << "\nDYNAMIC RELOCATION RECORDS\n";
2199   const uint32_t OffsetPadding = (Obj->getBytesInAddress() > 4 ? 16 : 8);
2200   const uint32_t TypePadding = 24;
2201   outs() << left_justify("OFFSET", OffsetPadding) << ' '
2202          << left_justify("TYPE", TypePadding) << " VALUE\n";
2203 
2204   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
2205   for (const SectionRef &Section : DynRelSec)
2206     for (const RelocationRef &Reloc : Section.relocations()) {
2207       uint64_t Address = Reloc.getOffset();
2208       SmallString<32> RelocName;
2209       SmallString<32> ValueStr;
2210       Reloc.getTypeName(RelocName);
2211       if (Error E = getRelocationValueString(Reloc, ValueStr))
2212         reportError(std::move(E), Obj->getFileName());
2213       outs() << format(Fmt.data(), Address) << ' '
2214              << left_justify(RelocName, TypePadding) << ' ' << ValueStr << '\n';
2215     }
2216 }
2217 
2218 // Returns true if we need to show LMA column when dumping section headers. We
2219 // show it only when the platform is ELF and either we have at least one section
2220 // whose VMA and LMA are different and/or when --show-lma flag is used.
2221 static bool shouldDisplayLMA(const ObjectFile &Obj) {
2222   if (!Obj.isELF())
2223     return false;
2224   for (const SectionRef &S : ToolSectionFilter(Obj))
2225     if (S.getAddress() != getELFSectionLMA(S))
2226       return true;
2227   return ShowLMA;
2228 }
2229 
2230 static size_t getMaxSectionNameWidth(const ObjectFile &Obj) {
2231   // Default column width for names is 13 even if no names are that long.
2232   size_t MaxWidth = 13;
2233   for (const SectionRef &Section : ToolSectionFilter(Obj)) {
2234     StringRef Name = unwrapOrError(Section.getName(), Obj.getFileName());
2235     MaxWidth = std::max(MaxWidth, Name.size());
2236   }
2237   return MaxWidth;
2238 }
2239 
2240 void objdump::printSectionHeaders(ObjectFile &Obj) {
2241   if (Obj.isELF() && Obj.sections().empty())
2242     createFakeELFSections(Obj);
2243 
2244   size_t NameWidth = getMaxSectionNameWidth(Obj);
2245   size_t AddressWidth = 2 * Obj.getBytesInAddress();
2246   bool HasLMAColumn = shouldDisplayLMA(Obj);
2247   outs() << "\nSections:\n";
2248   if (HasLMAColumn)
2249     outs() << "Idx " << left_justify("Name", NameWidth) << " Size     "
2250            << left_justify("VMA", AddressWidth) << " "
2251            << left_justify("LMA", AddressWidth) << " Type\n";
2252   else
2253     outs() << "Idx " << left_justify("Name", NameWidth) << " Size     "
2254            << left_justify("VMA", AddressWidth) << " Type\n";
2255 
2256   uint64_t Idx;
2257   for (const SectionRef &Section : ToolSectionFilter(Obj, &Idx)) {
2258     StringRef Name = unwrapOrError(Section.getName(), Obj.getFileName());
2259     uint64_t VMA = Section.getAddress();
2260     if (shouldAdjustVA(Section))
2261       VMA += AdjustVMA;
2262 
2263     uint64_t Size = Section.getSize();
2264 
2265     std::string Type = Section.isText() ? "TEXT" : "";
2266     if (Section.isData())
2267       Type += Type.empty() ? "DATA" : ", DATA";
2268     if (Section.isBSS())
2269       Type += Type.empty() ? "BSS" : ", BSS";
2270     if (Section.isDebugSection())
2271       Type += Type.empty() ? "DEBUG" : ", DEBUG";
2272 
2273     if (HasLMAColumn)
2274       outs() << format("%3" PRIu64 " %-*s %08" PRIx64 " ", Idx, NameWidth,
2275                        Name.str().c_str(), Size)
2276              << format_hex_no_prefix(VMA, AddressWidth) << " "
2277              << format_hex_no_prefix(getELFSectionLMA(Section), AddressWidth)
2278              << " " << Type << "\n";
2279     else
2280       outs() << format("%3" PRIu64 " %-*s %08" PRIx64 " ", Idx, NameWidth,
2281                        Name.str().c_str(), Size)
2282              << format_hex_no_prefix(VMA, AddressWidth) << " " << Type << "\n";
2283   }
2284 }
2285 
2286 void objdump::printSectionContents(const ObjectFile *Obj) {
2287   const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj);
2288 
2289   for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
2290     StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName());
2291     uint64_t BaseAddr = Section.getAddress();
2292     uint64_t Size = Section.getSize();
2293     if (!Size)
2294       continue;
2295 
2296     outs() << "Contents of section ";
2297     StringRef SegmentName = getSegmentName(MachO, Section);
2298     if (!SegmentName.empty())
2299       outs() << SegmentName << ",";
2300     outs() << Name << ":\n";
2301     if (Section.isBSS()) {
2302       outs() << format("<skipping contents of bss section at [%04" PRIx64
2303                        ", %04" PRIx64 ")>\n",
2304                        BaseAddr, BaseAddr + Size);
2305       continue;
2306     }
2307 
2308     StringRef Contents = unwrapOrError(Section.getContents(), Obj->getFileName());
2309 
2310     // Dump out the content as hex and printable ascii characters.
2311     for (std::size_t Addr = 0, End = Contents.size(); Addr < End; Addr += 16) {
2312       outs() << format(" %04" PRIx64 " ", BaseAddr + Addr);
2313       // Dump line of hex.
2314       for (std::size_t I = 0; I < 16; ++I) {
2315         if (I != 0 && I % 4 == 0)
2316           outs() << ' ';
2317         if (Addr + I < End)
2318           outs() << hexdigit((Contents[Addr + I] >> 4) & 0xF, true)
2319                  << hexdigit(Contents[Addr + I] & 0xF, true);
2320         else
2321           outs() << "  ";
2322       }
2323       // Print ascii.
2324       outs() << "  ";
2325       for (std::size_t I = 0; I < 16 && Addr + I < End; ++I) {
2326         if (isPrint(static_cast<unsigned char>(Contents[Addr + I]) & 0xFF))
2327           outs() << Contents[Addr + I];
2328         else
2329           outs() << ".";
2330       }
2331       outs() << "\n";
2332     }
2333   }
2334 }
2335 
2336 void objdump::printSymbolTable(const ObjectFile &O, StringRef ArchiveName,
2337                                StringRef ArchitectureName, bool DumpDynamic) {
2338   if (O.isCOFF() && !DumpDynamic) {
2339     outs() << "\nSYMBOL TABLE:\n";
2340     printCOFFSymbolTable(cast<const COFFObjectFile>(O));
2341     return;
2342   }
2343 
2344   const StringRef FileName = O.getFileName();
2345 
2346   if (!DumpDynamic) {
2347     outs() << "\nSYMBOL TABLE:\n";
2348     for (auto I = O.symbol_begin(); I != O.symbol_end(); ++I)
2349       printSymbol(O, *I, {}, FileName, ArchiveName, ArchitectureName,
2350                   DumpDynamic);
2351     return;
2352   }
2353 
2354   outs() << "\nDYNAMIC SYMBOL TABLE:\n";
2355   if (!O.isELF()) {
2356     reportWarning(
2357         "this operation is not currently supported for this file format",
2358         FileName);
2359     return;
2360   }
2361 
2362   const ELFObjectFileBase *ELF = cast<const ELFObjectFileBase>(&O);
2363   auto Symbols = ELF->getDynamicSymbolIterators();
2364   Expected<std::vector<VersionEntry>> SymbolVersionsOrErr =
2365       ELF->readDynsymVersions();
2366   if (!SymbolVersionsOrErr) {
2367     reportWarning(toString(SymbolVersionsOrErr.takeError()), FileName);
2368     SymbolVersionsOrErr = std::vector<VersionEntry>();
2369     (void)!SymbolVersionsOrErr;
2370   }
2371   for (auto &Sym : Symbols)
2372     printSymbol(O, Sym, *SymbolVersionsOrErr, FileName, ArchiveName,
2373                 ArchitectureName, DumpDynamic);
2374 }
2375 
2376 void objdump::printSymbol(const ObjectFile &O, const SymbolRef &Symbol,
2377                           ArrayRef<VersionEntry> SymbolVersions,
2378                           StringRef FileName, StringRef ArchiveName,
2379                           StringRef ArchitectureName, bool DumpDynamic) {
2380   const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(&O);
2381   uint64_t Address = unwrapOrError(Symbol.getAddress(), FileName, ArchiveName,
2382                                    ArchitectureName);
2383   if ((Address < StartAddress) || (Address > StopAddress))
2384     return;
2385   SymbolRef::Type Type =
2386       unwrapOrError(Symbol.getType(), FileName, ArchiveName, ArchitectureName);
2387   uint32_t Flags =
2388       unwrapOrError(Symbol.getFlags(), FileName, ArchiveName, ArchitectureName);
2389 
2390   // Don't ask a Mach-O STAB symbol for its section unless you know that
2391   // STAB symbol's section field refers to a valid section index. Otherwise
2392   // the symbol may error trying to load a section that does not exist.
2393   bool IsSTAB = false;
2394   if (MachO) {
2395     DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
2396     uint8_t NType =
2397         (MachO->is64Bit() ? MachO->getSymbol64TableEntry(SymDRI).n_type
2398                           : MachO->getSymbolTableEntry(SymDRI).n_type);
2399     if (NType & MachO::N_STAB)
2400       IsSTAB = true;
2401   }
2402   section_iterator Section = IsSTAB
2403                                  ? O.section_end()
2404                                  : unwrapOrError(Symbol.getSection(), FileName,
2405                                                  ArchiveName, ArchitectureName);
2406 
2407   StringRef Name;
2408   if (Type == SymbolRef::ST_Debug && Section != O.section_end()) {
2409     if (Expected<StringRef> NameOrErr = Section->getName())
2410       Name = *NameOrErr;
2411     else
2412       consumeError(NameOrErr.takeError());
2413 
2414   } else {
2415     Name = unwrapOrError(Symbol.getName(), FileName, ArchiveName,
2416                          ArchitectureName);
2417   }
2418 
2419   bool Global = Flags & SymbolRef::SF_Global;
2420   bool Weak = Flags & SymbolRef::SF_Weak;
2421   bool Absolute = Flags & SymbolRef::SF_Absolute;
2422   bool Common = Flags & SymbolRef::SF_Common;
2423   bool Hidden = Flags & SymbolRef::SF_Hidden;
2424 
2425   char GlobLoc = ' ';
2426   if ((Section != O.section_end() || Absolute) && !Weak)
2427     GlobLoc = Global ? 'g' : 'l';
2428   char IFunc = ' ';
2429   if (O.isELF()) {
2430     if (ELFSymbolRef(Symbol).getELFType() == ELF::STT_GNU_IFUNC)
2431       IFunc = 'i';
2432     if (ELFSymbolRef(Symbol).getBinding() == ELF::STB_GNU_UNIQUE)
2433       GlobLoc = 'u';
2434   }
2435 
2436   char Debug = ' ';
2437   if (DumpDynamic)
2438     Debug = 'D';
2439   else if (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
2440     Debug = 'd';
2441 
2442   char FileFunc = ' ';
2443   if (Type == SymbolRef::ST_File)
2444     FileFunc = 'f';
2445   else if (Type == SymbolRef::ST_Function)
2446     FileFunc = 'F';
2447   else if (Type == SymbolRef::ST_Data)
2448     FileFunc = 'O';
2449 
2450   const char *Fmt = O.getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
2451 
2452   outs() << format(Fmt, Address) << " "
2453          << GlobLoc            // Local -> 'l', Global -> 'g', Neither -> ' '
2454          << (Weak ? 'w' : ' ') // Weak?
2455          << ' '                // Constructor. Not supported yet.
2456          << ' '                // Warning. Not supported yet.
2457          << IFunc              // Indirect reference to another symbol.
2458          << Debug              // Debugging (d) or dynamic (D) symbol.
2459          << FileFunc           // Name of function (F), file (f) or object (O).
2460          << ' ';
2461   if (Absolute) {
2462     outs() << "*ABS*";
2463   } else if (Common) {
2464     outs() << "*COM*";
2465   } else if (Section == O.section_end()) {
2466     if (O.isXCOFF()) {
2467       XCOFFSymbolRef XCOFFSym = cast<const XCOFFObjectFile>(O).toSymbolRef(
2468           Symbol.getRawDataRefImpl());
2469       if (XCOFF::N_DEBUG == XCOFFSym.getSectionNumber())
2470         outs() << "*DEBUG*";
2471       else
2472         outs() << "*UND*";
2473     } else
2474       outs() << "*UND*";
2475   } else {
2476     StringRef SegmentName = getSegmentName(MachO, *Section);
2477     if (!SegmentName.empty())
2478       outs() << SegmentName << ",";
2479     StringRef SectionName = unwrapOrError(Section->getName(), FileName);
2480     outs() << SectionName;
2481     if (O.isXCOFF()) {
2482       Optional<SymbolRef> SymRef =
2483           getXCOFFSymbolContainingSymbolRef(cast<XCOFFObjectFile>(O), Symbol);
2484       if (SymRef) {
2485 
2486         Expected<StringRef> NameOrErr = SymRef->getName();
2487 
2488         if (NameOrErr) {
2489           outs() << " (csect:";
2490           std::string SymName(NameOrErr.get());
2491 
2492           if (Demangle)
2493             SymName = demangle(SymName);
2494 
2495           if (SymbolDescription)
2496             SymName = getXCOFFSymbolDescription(
2497                 createSymbolInfo(O, SymRef.value()), SymName);
2498 
2499           outs() << ' ' << SymName;
2500           outs() << ") ";
2501         } else
2502           reportWarning(toString(NameOrErr.takeError()), FileName);
2503       }
2504     }
2505   }
2506 
2507   if (Common)
2508     outs() << '\t' << format(Fmt, static_cast<uint64_t>(Symbol.getAlignment()));
2509   else if (O.isXCOFF())
2510     outs() << '\t'
2511            << format(Fmt, cast<XCOFFObjectFile>(O).getSymbolSize(
2512                               Symbol.getRawDataRefImpl()));
2513   else if (O.isELF())
2514     outs() << '\t' << format(Fmt, ELFSymbolRef(Symbol).getSize());
2515 
2516   if (O.isELF()) {
2517     if (!SymbolVersions.empty()) {
2518       const VersionEntry &Ver =
2519           SymbolVersions[Symbol.getRawDataRefImpl().d.b - 1];
2520       std::string Str;
2521       if (!Ver.Name.empty())
2522         Str = Ver.IsVerDef ? ' ' + Ver.Name : '(' + Ver.Name + ')';
2523       outs() << ' ' << left_justify(Str, 12);
2524     }
2525 
2526     uint8_t Other = ELFSymbolRef(Symbol).getOther();
2527     switch (Other) {
2528     case ELF::STV_DEFAULT:
2529       break;
2530     case ELF::STV_INTERNAL:
2531       outs() << " .internal";
2532       break;
2533     case ELF::STV_HIDDEN:
2534       outs() << " .hidden";
2535       break;
2536     case ELF::STV_PROTECTED:
2537       outs() << " .protected";
2538       break;
2539     default:
2540       outs() << format(" 0x%02x", Other);
2541       break;
2542     }
2543   } else if (Hidden) {
2544     outs() << " .hidden";
2545   }
2546 
2547   std::string SymName(Name);
2548   if (Demangle)
2549     SymName = demangle(SymName);
2550 
2551   if (O.isXCOFF() && SymbolDescription)
2552     SymName = getXCOFFSymbolDescription(createSymbolInfo(O, Symbol), SymName);
2553 
2554   outs() << ' ' << SymName << '\n';
2555 }
2556 
2557 static void printUnwindInfo(const ObjectFile *O) {
2558   outs() << "Unwind info:\n\n";
2559 
2560   if (const COFFObjectFile *Coff = dyn_cast<COFFObjectFile>(O))
2561     printCOFFUnwindInfo(Coff);
2562   else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O))
2563     printMachOUnwindInfo(MachO);
2564   else
2565     // TODO: Extract DWARF dump tool to objdump.
2566     WithColor::error(errs(), ToolName)
2567         << "This operation is only currently supported "
2568            "for COFF and MachO object files.\n";
2569 }
2570 
2571 /// Dump the raw contents of the __clangast section so the output can be piped
2572 /// into llvm-bcanalyzer.
2573 static void printRawClangAST(const ObjectFile *Obj) {
2574   if (outs().is_displayed()) {
2575     WithColor::error(errs(), ToolName)
2576         << "The -raw-clang-ast option will dump the raw binary contents of "
2577            "the clang ast section.\n"
2578            "Please redirect the output to a file or another program such as "
2579            "llvm-bcanalyzer.\n";
2580     return;
2581   }
2582 
2583   StringRef ClangASTSectionName("__clangast");
2584   if (Obj->isCOFF()) {
2585     ClangASTSectionName = "clangast";
2586   }
2587 
2588   Optional<object::SectionRef> ClangASTSection;
2589   for (auto Sec : ToolSectionFilter(*Obj)) {
2590     StringRef Name;
2591     if (Expected<StringRef> NameOrErr = Sec.getName())
2592       Name = *NameOrErr;
2593     else
2594       consumeError(NameOrErr.takeError());
2595 
2596     if (Name == ClangASTSectionName) {
2597       ClangASTSection = Sec;
2598       break;
2599     }
2600   }
2601   if (!ClangASTSection)
2602     return;
2603 
2604   StringRef ClangASTContents =
2605       unwrapOrError(ClangASTSection.value().getContents(), Obj->getFileName());
2606   outs().write(ClangASTContents.data(), ClangASTContents.size());
2607 }
2608 
2609 static void printFaultMaps(const ObjectFile *Obj) {
2610   StringRef FaultMapSectionName;
2611 
2612   if (Obj->isELF()) {
2613     FaultMapSectionName = ".llvm_faultmaps";
2614   } else if (Obj->isMachO()) {
2615     FaultMapSectionName = "__llvm_faultmaps";
2616   } else {
2617     WithColor::error(errs(), ToolName)
2618         << "This operation is only currently supported "
2619            "for ELF and Mach-O executable files.\n";
2620     return;
2621   }
2622 
2623   Optional<object::SectionRef> FaultMapSection;
2624 
2625   for (auto Sec : ToolSectionFilter(*Obj)) {
2626     StringRef Name;
2627     if (Expected<StringRef> NameOrErr = Sec.getName())
2628       Name = *NameOrErr;
2629     else
2630       consumeError(NameOrErr.takeError());
2631 
2632     if (Name == FaultMapSectionName) {
2633       FaultMapSection = Sec;
2634       break;
2635     }
2636   }
2637 
2638   outs() << "FaultMap table:\n";
2639 
2640   if (!FaultMapSection) {
2641     outs() << "<not found>\n";
2642     return;
2643   }
2644 
2645   StringRef FaultMapContents =
2646       unwrapOrError(FaultMapSection->getContents(), Obj->getFileName());
2647   FaultMapParser FMP(FaultMapContents.bytes_begin(),
2648                      FaultMapContents.bytes_end());
2649 
2650   outs() << FMP;
2651 }
2652 
2653 static void printPrivateFileHeaders(const ObjectFile *O, bool OnlyFirst) {
2654   if (O->isELF()) {
2655     printELFFileHeader(O);
2656     printELFDynamicSection(O);
2657     printELFSymbolVersionInfo(O);
2658     return;
2659   }
2660   if (O->isCOFF())
2661     return printCOFFFileHeader(cast<object::COFFObjectFile>(*O));
2662   if (O->isWasm())
2663     return printWasmFileHeader(O);
2664   if (O->isMachO()) {
2665     printMachOFileHeader(O);
2666     if (!OnlyFirst)
2667       printMachOLoadCommands(O);
2668     return;
2669   }
2670   reportError(O->getFileName(), "Invalid/Unsupported object file format");
2671 }
2672 
2673 static void printFileHeaders(const ObjectFile *O) {
2674   if (!O->isELF() && !O->isCOFF())
2675     reportError(O->getFileName(), "Invalid/Unsupported object file format");
2676 
2677   Triple::ArchType AT = O->getArch();
2678   outs() << "architecture: " << Triple::getArchTypeName(AT) << "\n";
2679   uint64_t Address = unwrapOrError(O->getStartAddress(), O->getFileName());
2680 
2681   StringRef Fmt = O->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
2682   outs() << "start address: "
2683          << "0x" << format(Fmt.data(), Address) << "\n";
2684 }
2685 
2686 static void printArchiveChild(StringRef Filename, const Archive::Child &C) {
2687   Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
2688   if (!ModeOrErr) {
2689     WithColor::error(errs(), ToolName) << "ill-formed archive entry.\n";
2690     consumeError(ModeOrErr.takeError());
2691     return;
2692   }
2693   sys::fs::perms Mode = ModeOrErr.get();
2694   outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
2695   outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
2696   outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
2697   outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
2698   outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
2699   outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
2700   outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
2701   outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
2702   outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
2703 
2704   outs() << " ";
2705 
2706   outs() << format("%d/%d %6" PRId64 " ", unwrapOrError(C.getUID(), Filename),
2707                    unwrapOrError(C.getGID(), Filename),
2708                    unwrapOrError(C.getRawSize(), Filename));
2709 
2710   StringRef RawLastModified = C.getRawLastModified();
2711   unsigned Seconds;
2712   if (RawLastModified.getAsInteger(10, Seconds))
2713     outs() << "(date: \"" << RawLastModified
2714            << "\" contains non-decimal chars) ";
2715   else {
2716     // Since ctime(3) returns a 26 character string of the form:
2717     // "Sun Sep 16 01:03:52 1973\n\0"
2718     // just print 24 characters.
2719     time_t t = Seconds;
2720     outs() << format("%.24s ", ctime(&t));
2721   }
2722 
2723   StringRef Name = "";
2724   Expected<StringRef> NameOrErr = C.getName();
2725   if (!NameOrErr) {
2726     consumeError(NameOrErr.takeError());
2727     Name = unwrapOrError(C.getRawName(), Filename);
2728   } else {
2729     Name = NameOrErr.get();
2730   }
2731   outs() << Name << "\n";
2732 }
2733 
2734 // For ELF only now.
2735 static bool shouldWarnForInvalidStartStopAddress(ObjectFile *Obj) {
2736   if (const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj)) {
2737     if (Elf->getEType() != ELF::ET_REL)
2738       return true;
2739   }
2740   return false;
2741 }
2742 
2743 static void checkForInvalidStartStopAddress(ObjectFile *Obj,
2744                                             uint64_t Start, uint64_t Stop) {
2745   if (!shouldWarnForInvalidStartStopAddress(Obj))
2746     return;
2747 
2748   for (const SectionRef &Section : Obj->sections())
2749     if (ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC) {
2750       uint64_t BaseAddr = Section.getAddress();
2751       uint64_t Size = Section.getSize();
2752       if ((Start < BaseAddr + Size) && Stop > BaseAddr)
2753         return;
2754     }
2755 
2756   if (!HasStartAddressFlag)
2757     reportWarning("no section has address less than 0x" +
2758                       Twine::utohexstr(Stop) + " specified by --stop-address",
2759                   Obj->getFileName());
2760   else if (!HasStopAddressFlag)
2761     reportWarning("no section has address greater than or equal to 0x" +
2762                       Twine::utohexstr(Start) + " specified by --start-address",
2763                   Obj->getFileName());
2764   else
2765     reportWarning("no section overlaps the range [0x" +
2766                       Twine::utohexstr(Start) + ",0x" + Twine::utohexstr(Stop) +
2767                       ") specified by --start-address/--stop-address",
2768                   Obj->getFileName());
2769 }
2770 
2771 static void dumpObject(ObjectFile *O, const Archive *A = nullptr,
2772                        const Archive::Child *C = nullptr) {
2773   // Avoid other output when using a raw option.
2774   if (!RawClangAST) {
2775     outs() << '\n';
2776     if (A)
2777       outs() << A->getFileName() << "(" << O->getFileName() << ")";
2778     else
2779       outs() << O->getFileName();
2780     outs() << ":\tfile format " << O->getFileFormatName().lower() << "\n";
2781   }
2782 
2783   if (HasStartAddressFlag || HasStopAddressFlag)
2784     checkForInvalidStartStopAddress(O, StartAddress, StopAddress);
2785 
2786   // Note: the order here matches GNU objdump for compatability.
2787   StringRef ArchiveName = A ? A->getFileName() : "";
2788   if (ArchiveHeaders && !MachOOpt && C)
2789     printArchiveChild(ArchiveName, *C);
2790   if (FileHeaders)
2791     printFileHeaders(O);
2792   if (PrivateHeaders || FirstPrivateHeader)
2793     printPrivateFileHeaders(O, FirstPrivateHeader);
2794   if (SectionHeaders)
2795     printSectionHeaders(*O);
2796   if (SymbolTable)
2797     printSymbolTable(*O, ArchiveName);
2798   if (DynamicSymbolTable)
2799     printSymbolTable(*O, ArchiveName, /*ArchitectureName=*/"",
2800                      /*DumpDynamic=*/true);
2801   if (DwarfDumpType != DIDT_Null) {
2802     std::unique_ptr<DIContext> DICtx = DWARFContext::create(*O);
2803     // Dump the complete DWARF structure.
2804     DIDumpOptions DumpOpts;
2805     DumpOpts.DumpType = DwarfDumpType;
2806     DICtx->dump(outs(), DumpOpts);
2807   }
2808   if (Relocations && !Disassemble)
2809     printRelocations(O);
2810   if (DynamicRelocations)
2811     printDynamicRelocations(O);
2812   if (SectionContents)
2813     printSectionContents(O);
2814   if (Disassemble)
2815     disassembleObject(O, Relocations);
2816   if (UnwindInfo)
2817     printUnwindInfo(O);
2818 
2819   // Mach-O specific options:
2820   if (ExportsTrie)
2821     printExportsTrie(O);
2822   if (Rebase)
2823     printRebaseTable(O);
2824   if (Bind)
2825     printBindTable(O);
2826   if (LazyBind)
2827     printLazyBindTable(O);
2828   if (WeakBind)
2829     printWeakBindTable(O);
2830 
2831   // Other special sections:
2832   if (RawClangAST)
2833     printRawClangAST(O);
2834   if (FaultMapSection)
2835     printFaultMaps(O);
2836   if (Offloading)
2837     dumpOffloadBinary(*O);
2838 }
2839 
2840 static void dumpObject(const COFFImportFile *I, const Archive *A,
2841                        const Archive::Child *C = nullptr) {
2842   StringRef ArchiveName = A ? A->getFileName() : "";
2843 
2844   // Avoid other output when using a raw option.
2845   if (!RawClangAST)
2846     outs() << '\n'
2847            << ArchiveName << "(" << I->getFileName() << ")"
2848            << ":\tfile format COFF-import-file"
2849            << "\n\n";
2850 
2851   if (ArchiveHeaders && !MachOOpt && C)
2852     printArchiveChild(ArchiveName, *C);
2853   if (SymbolTable)
2854     printCOFFSymbolTable(*I);
2855 }
2856 
2857 /// Dump each object file in \a a;
2858 static void dumpArchive(const Archive *A) {
2859   Error Err = Error::success();
2860   unsigned I = -1;
2861   for (auto &C : A->children(Err)) {
2862     ++I;
2863     Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2864     if (!ChildOrErr) {
2865       if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2866         reportError(std::move(E), getFileNameForError(C, I), A->getFileName());
2867       continue;
2868     }
2869     if (ObjectFile *O = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
2870       dumpObject(O, A, &C);
2871     else if (COFFImportFile *I = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
2872       dumpObject(I, A, &C);
2873     else
2874       reportError(errorCodeToError(object_error::invalid_file_type),
2875                   A->getFileName());
2876   }
2877   if (Err)
2878     reportError(std::move(Err), A->getFileName());
2879 }
2880 
2881 /// Open file and figure out how to dump it.
2882 static void dumpInput(StringRef file) {
2883   // If we are using the Mach-O specific object file parser, then let it parse
2884   // the file and process the command line options.  So the -arch flags can
2885   // be used to select specific slices, etc.
2886   if (MachOOpt) {
2887     parseInputMachO(file);
2888     return;
2889   }
2890 
2891   // Attempt to open the binary.
2892   OwningBinary<Binary> OBinary = unwrapOrError(createBinary(file), file);
2893   Binary &Binary = *OBinary.getBinary();
2894 
2895   if (Archive *A = dyn_cast<Archive>(&Binary))
2896     dumpArchive(A);
2897   else if (ObjectFile *O = dyn_cast<ObjectFile>(&Binary))
2898     dumpObject(O);
2899   else if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Binary))
2900     parseInputMachO(UB);
2901   else if (OffloadBinary *OB = dyn_cast<OffloadBinary>(&Binary))
2902     dumpOffloadSections(*OB);
2903   else
2904     reportError(errorCodeToError(object_error::invalid_file_type), file);
2905 }
2906 
2907 template <typename T>
2908 static void parseIntArg(const llvm::opt::InputArgList &InputArgs, int ID,
2909                         T &Value) {
2910   if (const opt::Arg *A = InputArgs.getLastArg(ID)) {
2911     StringRef V(A->getValue());
2912     if (!llvm::to_integer(V, Value, 0)) {
2913       reportCmdLineError(A->getSpelling() +
2914                          ": expected a non-negative integer, but got '" + V +
2915                          "'");
2916     }
2917   }
2918 }
2919 
2920 static object::BuildID parseBuildIDArg(const opt::Arg *A) {
2921   StringRef V(A->getValue());
2922   std::string Bytes;
2923   if (!tryGetFromHex(V, Bytes))
2924     reportCmdLineError(A->getSpelling() + ": expected a build ID, but got '" +
2925                        V + "'");
2926   ArrayRef<uint8_t> BuildID(reinterpret_cast<const uint8_t *>(Bytes.data()),
2927                             Bytes.size());
2928   return object::BuildID(BuildID.begin(), BuildID.end());
2929 }
2930 
2931 static void invalidArgValue(const opt::Arg *A) {
2932   reportCmdLineError("'" + StringRef(A->getValue()) +
2933                      "' is not a valid value for '" + A->getSpelling() + "'");
2934 }
2935 
2936 static std::vector<std::string>
2937 commaSeparatedValues(const llvm::opt::InputArgList &InputArgs, int ID) {
2938   std::vector<std::string> Values;
2939   for (StringRef Value : InputArgs.getAllArgValues(ID)) {
2940     llvm::SmallVector<StringRef, 2> SplitValues;
2941     llvm::SplitString(Value, SplitValues, ",");
2942     for (StringRef SplitValue : SplitValues)
2943       Values.push_back(SplitValue.str());
2944   }
2945   return Values;
2946 }
2947 
2948 static void parseOtoolOptions(const llvm::opt::InputArgList &InputArgs) {
2949   MachOOpt = true;
2950   FullLeadingAddr = true;
2951   PrintImmHex = true;
2952 
2953   ArchName = InputArgs.getLastArgValue(OTOOL_arch).str();
2954   LinkOptHints = InputArgs.hasArg(OTOOL_C);
2955   if (InputArgs.hasArg(OTOOL_d))
2956     FilterSections.push_back("__DATA,__data");
2957   DylibId = InputArgs.hasArg(OTOOL_D);
2958   UniversalHeaders = InputArgs.hasArg(OTOOL_f);
2959   DataInCode = InputArgs.hasArg(OTOOL_G);
2960   FirstPrivateHeader = InputArgs.hasArg(OTOOL_h);
2961   IndirectSymbols = InputArgs.hasArg(OTOOL_I);
2962   ShowRawInsn = InputArgs.hasArg(OTOOL_j);
2963   PrivateHeaders = InputArgs.hasArg(OTOOL_l);
2964   DylibsUsed = InputArgs.hasArg(OTOOL_L);
2965   MCPU = InputArgs.getLastArgValue(OTOOL_mcpu_EQ).str();
2966   ObjcMetaData = InputArgs.hasArg(OTOOL_o);
2967   DisSymName = InputArgs.getLastArgValue(OTOOL_p).str();
2968   InfoPlist = InputArgs.hasArg(OTOOL_P);
2969   Relocations = InputArgs.hasArg(OTOOL_r);
2970   if (const Arg *A = InputArgs.getLastArg(OTOOL_s)) {
2971     auto Filter = (A->getValue(0) + StringRef(",") + A->getValue(1)).str();
2972     FilterSections.push_back(Filter);
2973   }
2974   if (InputArgs.hasArg(OTOOL_t))
2975     FilterSections.push_back("__TEXT,__text");
2976   Verbose = InputArgs.hasArg(OTOOL_v) || InputArgs.hasArg(OTOOL_V) ||
2977             InputArgs.hasArg(OTOOL_o);
2978   SymbolicOperands = InputArgs.hasArg(OTOOL_V);
2979   if (InputArgs.hasArg(OTOOL_x))
2980     FilterSections.push_back(",__text");
2981   LeadingAddr = LeadingHeaders = !InputArgs.hasArg(OTOOL_X);
2982 
2983   ChainedFixups = InputArgs.hasArg(OTOOL_chained_fixups);
2984   DyldInfo = InputArgs.hasArg(OTOOL_dyld_info);
2985 
2986   InputFilenames = InputArgs.getAllArgValues(OTOOL_INPUT);
2987   if (InputFilenames.empty())
2988     reportCmdLineError("no input file");
2989 
2990   for (const Arg *A : InputArgs) {
2991     const Option &O = A->getOption();
2992     if (O.getGroup().isValid() && O.getGroup().getID() == OTOOL_grp_obsolete) {
2993       reportCmdLineWarning(O.getPrefixedName() +
2994                            " is obsolete and not implemented");
2995     }
2996   }
2997 }
2998 
2999 static void parseObjdumpOptions(const llvm::opt::InputArgList &InputArgs) {
3000   parseIntArg(InputArgs, OBJDUMP_adjust_vma_EQ, AdjustVMA);
3001   AllHeaders = InputArgs.hasArg(OBJDUMP_all_headers);
3002   ArchName = InputArgs.getLastArgValue(OBJDUMP_arch_name_EQ).str();
3003   ArchiveHeaders = InputArgs.hasArg(OBJDUMP_archive_headers);
3004   Demangle = InputArgs.hasArg(OBJDUMP_demangle);
3005   Disassemble = InputArgs.hasArg(OBJDUMP_disassemble);
3006   DisassembleAll = InputArgs.hasArg(OBJDUMP_disassemble_all);
3007   SymbolDescription = InputArgs.hasArg(OBJDUMP_symbol_description);
3008   DisassembleSymbols =
3009       commaSeparatedValues(InputArgs, OBJDUMP_disassemble_symbols_EQ);
3010   DisassembleZeroes = InputArgs.hasArg(OBJDUMP_disassemble_zeroes);
3011   if (const opt::Arg *A = InputArgs.getLastArg(OBJDUMP_dwarf_EQ)) {
3012     DwarfDumpType = StringSwitch<DIDumpType>(A->getValue())
3013                         .Case("frames", DIDT_DebugFrame)
3014                         .Default(DIDT_Null);
3015     if (DwarfDumpType == DIDT_Null)
3016       invalidArgValue(A);
3017   }
3018   DynamicRelocations = InputArgs.hasArg(OBJDUMP_dynamic_reloc);
3019   FaultMapSection = InputArgs.hasArg(OBJDUMP_fault_map_section);
3020   Offloading = InputArgs.hasArg(OBJDUMP_offloading);
3021   FileHeaders = InputArgs.hasArg(OBJDUMP_file_headers);
3022   SectionContents = InputArgs.hasArg(OBJDUMP_full_contents);
3023   PrintLines = InputArgs.hasArg(OBJDUMP_line_numbers);
3024   InputFilenames = InputArgs.getAllArgValues(OBJDUMP_INPUT);
3025   MachOOpt = InputArgs.hasArg(OBJDUMP_macho);
3026   MCPU = InputArgs.getLastArgValue(OBJDUMP_mcpu_EQ).str();
3027   MAttrs = commaSeparatedValues(InputArgs, OBJDUMP_mattr_EQ);
3028   ShowRawInsn = !InputArgs.hasArg(OBJDUMP_no_show_raw_insn);
3029   LeadingAddr = !InputArgs.hasArg(OBJDUMP_no_leading_addr);
3030   RawClangAST = InputArgs.hasArg(OBJDUMP_raw_clang_ast);
3031   Relocations = InputArgs.hasArg(OBJDUMP_reloc);
3032   PrintImmHex =
3033       InputArgs.hasFlag(OBJDUMP_print_imm_hex, OBJDUMP_no_print_imm_hex, false);
3034   PrivateHeaders = InputArgs.hasArg(OBJDUMP_private_headers);
3035   FilterSections = InputArgs.getAllArgValues(OBJDUMP_section_EQ);
3036   SectionHeaders = InputArgs.hasArg(OBJDUMP_section_headers);
3037   ShowAllSymbols = InputArgs.hasArg(OBJDUMP_show_all_symbols);
3038   ShowLMA = InputArgs.hasArg(OBJDUMP_show_lma);
3039   PrintSource = InputArgs.hasArg(OBJDUMP_source);
3040   parseIntArg(InputArgs, OBJDUMP_start_address_EQ, StartAddress);
3041   HasStartAddressFlag = InputArgs.hasArg(OBJDUMP_start_address_EQ);
3042   parseIntArg(InputArgs, OBJDUMP_stop_address_EQ, StopAddress);
3043   HasStopAddressFlag = InputArgs.hasArg(OBJDUMP_stop_address_EQ);
3044   SymbolTable = InputArgs.hasArg(OBJDUMP_syms);
3045   SymbolizeOperands = InputArgs.hasArg(OBJDUMP_symbolize_operands);
3046   DynamicSymbolTable = InputArgs.hasArg(OBJDUMP_dynamic_syms);
3047   TripleName = InputArgs.getLastArgValue(OBJDUMP_triple_EQ).str();
3048   UnwindInfo = InputArgs.hasArg(OBJDUMP_unwind_info);
3049   Wide = InputArgs.hasArg(OBJDUMP_wide);
3050   Prefix = InputArgs.getLastArgValue(OBJDUMP_prefix).str();
3051   parseIntArg(InputArgs, OBJDUMP_prefix_strip, PrefixStrip);
3052   if (const opt::Arg *A = InputArgs.getLastArg(OBJDUMP_debug_vars_EQ)) {
3053     DbgVariables = StringSwitch<DebugVarsFormat>(A->getValue())
3054                        .Case("ascii", DVASCII)
3055                        .Case("unicode", DVUnicode)
3056                        .Default(DVInvalid);
3057     if (DbgVariables == DVInvalid)
3058       invalidArgValue(A);
3059   }
3060   parseIntArg(InputArgs, OBJDUMP_debug_vars_indent_EQ, DbgIndent);
3061 
3062   parseMachOOptions(InputArgs);
3063 
3064   // Parse -M (--disassembler-options) and deprecated
3065   // --x86-asm-syntax={att,intel}.
3066   //
3067   // Note, for x86, the asm dialect (AssemblerDialect) is initialized when the
3068   // MCAsmInfo is constructed. MCInstPrinter::applyTargetSpecificCLOption is
3069   // called too late. For now we have to use the internal cl::opt option.
3070   const char *AsmSyntax = nullptr;
3071   for (const auto *A : InputArgs.filtered(OBJDUMP_disassembler_options_EQ,
3072                                           OBJDUMP_x86_asm_syntax_att,
3073                                           OBJDUMP_x86_asm_syntax_intel)) {
3074     switch (A->getOption().getID()) {
3075     case OBJDUMP_x86_asm_syntax_att:
3076       AsmSyntax = "--x86-asm-syntax=att";
3077       continue;
3078     case OBJDUMP_x86_asm_syntax_intel:
3079       AsmSyntax = "--x86-asm-syntax=intel";
3080       continue;
3081     }
3082 
3083     SmallVector<StringRef, 2> Values;
3084     llvm::SplitString(A->getValue(), Values, ",");
3085     for (StringRef V : Values) {
3086       if (V == "att")
3087         AsmSyntax = "--x86-asm-syntax=att";
3088       else if (V == "intel")
3089         AsmSyntax = "--x86-asm-syntax=intel";
3090       else
3091         DisassemblerOptions.push_back(V.str());
3092     }
3093   }
3094   if (AsmSyntax) {
3095     const char *Argv[] = {"llvm-objdump", AsmSyntax};
3096     llvm::cl::ParseCommandLineOptions(2, Argv);
3097   }
3098 
3099   // Look up any provided build IDs, then append them to the input filenames.
3100   for (const opt::Arg *A : InputArgs.filtered(OBJDUMP_build_id)) {
3101     object::BuildID BuildID = parseBuildIDArg(A);
3102     Optional<std::string> Path = BIDFetcher->fetch(BuildID);
3103     if (!Path) {
3104       reportCmdLineError(A->getSpelling() + ": could not find build ID '" +
3105                          A->getValue() + "'");
3106     }
3107     InputFilenames.push_back(std::move(*Path));
3108   }
3109 
3110   // objdump defaults to a.out if no filenames specified.
3111   if (InputFilenames.empty())
3112     InputFilenames.push_back("a.out");
3113 }
3114 
3115 int main(int argc, char **argv) {
3116   using namespace llvm;
3117   InitLLVM X(argc, argv);
3118 
3119   ToolName = argv[0];
3120   std::unique_ptr<CommonOptTable> T;
3121   OptSpecifier Unknown, HelpFlag, HelpHiddenFlag, VersionFlag;
3122 
3123   StringRef Stem = sys::path::stem(ToolName);
3124   auto Is = [=](StringRef Tool) {
3125     // We need to recognize the following filenames:
3126     //
3127     // llvm-objdump -> objdump
3128     // llvm-otool-10.exe -> otool
3129     // powerpc64-unknown-freebsd13-objdump -> objdump
3130     auto I = Stem.rfind_insensitive(Tool);
3131     return I != StringRef::npos &&
3132            (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()]));
3133   };
3134   if (Is("otool")) {
3135     T = std::make_unique<OtoolOptTable>();
3136     Unknown = OTOOL_UNKNOWN;
3137     HelpFlag = OTOOL_help;
3138     HelpHiddenFlag = OTOOL_help_hidden;
3139     VersionFlag = OTOOL_version;
3140   } else {
3141     T = std::make_unique<ObjdumpOptTable>();
3142     Unknown = OBJDUMP_UNKNOWN;
3143     HelpFlag = OBJDUMP_help;
3144     HelpHiddenFlag = OBJDUMP_help_hidden;
3145     VersionFlag = OBJDUMP_version;
3146   }
3147 
3148   BumpPtrAllocator A;
3149   StringSaver Saver(A);
3150   opt::InputArgList InputArgs =
3151       T->parseArgs(argc, argv, Unknown, Saver,
3152                    [&](StringRef Msg) { reportCmdLineError(Msg); });
3153 
3154   if (InputArgs.size() == 0 || InputArgs.hasArg(HelpFlag)) {
3155     T->printHelp(ToolName);
3156     return 0;
3157   }
3158   if (InputArgs.hasArg(HelpHiddenFlag)) {
3159     T->printHelp(ToolName, /*ShowHidden=*/true);
3160     return 0;
3161   }
3162 
3163   // Initialize targets and assembly printers/parsers.
3164   InitializeAllTargetInfos();
3165   InitializeAllTargetMCs();
3166   InitializeAllDisassemblers();
3167 
3168   if (InputArgs.hasArg(VersionFlag)) {
3169     cl::PrintVersionMessage();
3170     if (!Is("otool")) {
3171       outs() << '\n';
3172       TargetRegistry::printRegisteredTargetsForVersion(outs());
3173     }
3174     return 0;
3175   }
3176 
3177   // Initialize debuginfod.
3178   const bool ShouldUseDebuginfodByDefault =
3179       InputArgs.hasArg(OBJDUMP_build_id) ||
3180       (HTTPClient::isAvailable() &&
3181        !ExitOnErr(getDefaultDebuginfodUrls()).empty());
3182   std::vector<std::string> DebugFileDirectories =
3183       InputArgs.getAllArgValues(OBJDUMP_debug_file_directory);
3184   if (InputArgs.hasFlag(OBJDUMP_debuginfod, OBJDUMP_no_debuginfod,
3185                         ShouldUseDebuginfodByDefault)) {
3186     HTTPClient::initialize();
3187     BIDFetcher =
3188         std::make_unique<DebuginfodFetcher>(std::move(DebugFileDirectories));
3189   } else {
3190     BIDFetcher =
3191         std::make_unique<BuildIDFetcher>(std::move(DebugFileDirectories));
3192   }
3193 
3194   if (Is("otool"))
3195     parseOtoolOptions(InputArgs);
3196   else
3197     parseObjdumpOptions(InputArgs);
3198 
3199   if (StartAddress >= StopAddress)
3200     reportCmdLineError("start address should be less than stop address");
3201 
3202   // Removes trailing separators from prefix.
3203   while (!Prefix.empty() && sys::path::is_separator(Prefix.back()))
3204     Prefix.pop_back();
3205 
3206   if (AllHeaders)
3207     ArchiveHeaders = FileHeaders = PrivateHeaders = Relocations =
3208         SectionHeaders = SymbolTable = true;
3209 
3210   if (DisassembleAll || PrintSource || PrintLines ||
3211       !DisassembleSymbols.empty())
3212     Disassemble = true;
3213 
3214   if (!ArchiveHeaders && !Disassemble && DwarfDumpType == DIDT_Null &&
3215       !DynamicRelocations && !FileHeaders && !PrivateHeaders && !RawClangAST &&
3216       !Relocations && !SectionHeaders && !SectionContents && !SymbolTable &&
3217       !DynamicSymbolTable && !UnwindInfo && !FaultMapSection && !Offloading &&
3218       !(MachOOpt &&
3219         (Bind || DataInCode || ChainedFixups || DyldInfo || DylibId ||
3220          DylibsUsed || ExportsTrie || FirstPrivateHeader || FunctionStarts ||
3221          IndirectSymbols || InfoPlist || LazyBind || LinkOptHints ||
3222          ObjcMetaData || Rebase || Rpaths || UniversalHeaders || WeakBind ||
3223          !FilterSections.empty()))) {
3224     T->printHelp(ToolName);
3225     return 2;
3226   }
3227 
3228   DisasmSymbolSet.insert(DisassembleSymbols.begin(), DisassembleSymbols.end());
3229 
3230   llvm::for_each(InputFilenames, dumpInput);
3231 
3232   warnOnNoMatchForSections();
3233 
3234   return EXIT_SUCCESS;
3235 }
3236