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