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