1 //===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===//
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
7 //===----------------------------------------------------------------------===//
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
13 // The flags and output of this program should be near identical to those of
16 //===----------------------------------------------------------------------===//
18 #include "llvm-objdump.h"
21 #include "MachODump.h"
23 #include "XCOFFDump.h"
24 #include "llvm/ADT/IndexedMap.h"
25 #include "llvm/ADT/Optional.h"
26 #include "llvm/ADT/SmallSet.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/SetOperations.h"
29 #include "llvm/ADT/StringExtras.h"
30 #include "llvm/ADT/StringSet.h"
31 #include "llvm/ADT/Triple.h"
32 #include "llvm/CodeGen/FaultMaps.h"
33 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
34 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
35 #include "llvm/Demangle/Demangle.h"
36 #include "llvm/MC/MCAsmInfo.h"
37 #include "llvm/MC/MCContext.h"
38 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
39 #include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
40 #include "llvm/MC/MCInst.h"
41 #include "llvm/MC/MCInstPrinter.h"
42 #include "llvm/MC/MCInstrAnalysis.h"
43 #include "llvm/MC/MCInstrInfo.h"
44 #include "llvm/MC/MCObjectFileInfo.h"
45 #include "llvm/MC/MCRegisterInfo.h"
46 #include "llvm/MC/MCSubtargetInfo.h"
47 #include "llvm/MC/MCTargetOptions.h"
48 #include "llvm/Object/Archive.h"
49 #include "llvm/Object/COFF.h"
50 #include "llvm/Object/COFFImportFile.h"
51 #include "llvm/Object/ELFObjectFile.h"
52 #include "llvm/Object/MachO.h"
53 #include "llvm/Object/MachOUniversal.h"
54 #include "llvm/Object/ObjectFile.h"
55 #include "llvm/Object/Wasm.h"
56 #include "llvm/Support/Casting.h"
57 #include "llvm/Support/CommandLine.h"
58 #include "llvm/Support/Debug.h"
59 #include "llvm/Support/Errc.h"
60 #include "llvm/Support/FileSystem.h"
61 #include "llvm/Support/Format.h"
62 #include "llvm/Support/FormatVariadic.h"
63 #include "llvm/Support/GraphWriter.h"
64 #include "llvm/Support/Host.h"
65 #include "llvm/Support/InitLLVM.h"
66 #include "llvm/Support/MemoryBuffer.h"
67 #include "llvm/Support/SourceMgr.h"
68 #include "llvm/Support/StringSaver.h"
69 #include "llvm/Support/TargetRegistry.h"
70 #include "llvm/Support/TargetSelect.h"
71 #include "llvm/Support/WithColor.h"
72 #include "llvm/Support/raw_ostream.h"
76 #include <system_error>
77 #include <unordered_map>
81 using namespace llvm::object;
82 using namespace llvm::objdump;
84 #define DEBUG_TYPE "objdump"
86 static cl::OptionCategory ObjdumpCat("llvm-objdump Options");
88 static cl::opt<uint64_t> AdjustVMA(
90 cl::desc("Increase the displayed address by the specified offset"),
91 cl::value_desc("offset"), cl::init(0), cl::cat(ObjdumpCat));
94 AllHeaders("all-headers",
95 cl::desc("Display all available header information"),
97 static cl::alias AllHeadersShort("x", cl::desc("Alias for --all-headers"),
98 cl::NotHidden, cl::Grouping,
99 cl::aliasopt(AllHeaders));
101 static cl::opt<std::string>
102 ArchName("arch-name",
103 cl::desc("Target arch to disassemble for, "
104 "see -version for available targets"),
105 cl::cat(ObjdumpCat));
108 objdump::ArchiveHeaders("archive-headers",
109 cl::desc("Display archive header information"),
110 cl::cat(ObjdumpCat));
111 static cl::alias ArchiveHeadersShort("a",
112 cl::desc("Alias for --archive-headers"),
113 cl::NotHidden, cl::Grouping,
114 cl::aliasopt(ArchiveHeaders));
116 cl::opt<bool> objdump::Demangle("demangle", cl::desc("Demangle symbols names"),
117 cl::init(false), cl::cat(ObjdumpCat));
118 static cl::alias DemangleShort("C", cl::desc("Alias for --demangle"),
119 cl::NotHidden, cl::Grouping,
120 cl::aliasopt(Demangle));
122 cl::opt<bool> objdump::Disassemble(
124 cl::desc("Display assembler mnemonics for the machine instructions"),
125 cl::cat(ObjdumpCat));
126 static cl::alias DisassembleShort("d", cl::desc("Alias for --disassemble"),
127 cl::NotHidden, cl::Grouping,
128 cl::aliasopt(Disassemble));
130 cl::opt<bool> objdump::DisassembleAll(
132 cl::desc("Display assembler mnemonics for the machine instructions"),
133 cl::cat(ObjdumpCat));
134 static cl::alias DisassembleAllShort("D",
135 cl::desc("Alias for --disassemble-all"),
136 cl::NotHidden, cl::Grouping,
137 cl::aliasopt(DisassembleAll));
139 cl::opt<bool> objdump::SymbolDescription(
140 "symbol-description",
141 cl::desc("Add symbol description for disassembly. This "
142 "option is for XCOFF files only"),
143 cl::init(false), cl::cat(ObjdumpCat));
145 static cl::list<std::string>
146 DisassembleSymbols("disassemble-symbols", cl::CommaSeparated,
147 cl::desc("List of symbols to disassemble. "
148 "Accept demangled names when --demangle is "
149 "specified, otherwise accept mangled names"),
150 cl::cat(ObjdumpCat));
152 static cl::opt<bool> DisassembleZeroes(
153 "disassemble-zeroes",
154 cl::desc("Do not skip blocks of zeroes when disassembling"),
155 cl::cat(ObjdumpCat));
157 DisassembleZeroesShort("z", cl::desc("Alias for --disassemble-zeroes"),
158 cl::NotHidden, cl::Grouping,
159 cl::aliasopt(DisassembleZeroes));
161 static cl::list<std::string>
162 DisassemblerOptions("disassembler-options",
163 cl::desc("Pass target specific disassembler options"),
164 cl::value_desc("options"), cl::CommaSeparated,
165 cl::cat(ObjdumpCat));
167 DisassemblerOptionsShort("M", cl::desc("Alias for --disassembler-options"),
168 cl::NotHidden, cl::Grouping, cl::Prefix,
170 cl::aliasopt(DisassemblerOptions));
172 cl::opt<DIDumpType> objdump::DwarfDumpType(
173 "dwarf", cl::init(DIDT_Null), cl::desc("Dump of dwarf debug sections:"),
174 cl::values(clEnumValN(DIDT_DebugFrame, "frames", ".debug_frame")),
175 cl::cat(ObjdumpCat));
177 static cl::opt<bool> DynamicRelocations(
179 cl::desc("Display the dynamic relocation entries in the file"),
180 cl::cat(ObjdumpCat));
181 static cl::alias DynamicRelocationShort("R",
182 cl::desc("Alias for --dynamic-reloc"),
183 cl::NotHidden, cl::Grouping,
184 cl::aliasopt(DynamicRelocations));
187 FaultMapSection("fault-map-section",
188 cl::desc("Display contents of faultmap section"),
189 cl::cat(ObjdumpCat));
192 FileHeaders("file-headers",
193 cl::desc("Display the contents of the overall file header"),
194 cl::cat(ObjdumpCat));
195 static cl::alias FileHeadersShort("f", cl::desc("Alias for --file-headers"),
196 cl::NotHidden, cl::Grouping,
197 cl::aliasopt(FileHeaders));
200 objdump::SectionContents("full-contents",
201 cl::desc("Display the content of each section"),
202 cl::cat(ObjdumpCat));
203 static cl::alias SectionContentsShort("s",
204 cl::desc("Alias for --full-contents"),
205 cl::NotHidden, cl::Grouping,
206 cl::aliasopt(SectionContents));
208 static cl::list<std::string> InputFilenames(cl::Positional,
209 cl::desc("<input object files>"),
211 cl::cat(ObjdumpCat));
214 PrintLines("line-numbers",
215 cl::desc("Display source line numbers with "
216 "disassembly. Implies disassemble object"),
217 cl::cat(ObjdumpCat));
218 static cl::alias PrintLinesShort("l", cl::desc("Alias for --line-numbers"),
219 cl::NotHidden, cl::Grouping,
220 cl::aliasopt(PrintLines));
222 static cl::opt<bool> MachOOpt("macho",
223 cl::desc("Use MachO specific object file parser"),
224 cl::cat(ObjdumpCat));
225 static cl::alias MachOm("m", cl::desc("Alias for --macho"), cl::NotHidden,
226 cl::Grouping, cl::aliasopt(MachOOpt));
228 cl::opt<std::string> objdump::MCPU(
229 "mcpu", cl::desc("Target a specific cpu type (-mcpu=help for details)"),
230 cl::value_desc("cpu-name"), cl::init(""), cl::cat(ObjdumpCat));
232 cl::list<std::string> objdump::MAttrs("mattr", cl::CommaSeparated,
233 cl::desc("Target specific attributes"),
234 cl::value_desc("a1,+a2,-a3,..."),
235 cl::cat(ObjdumpCat));
237 cl::opt<bool> objdump::NoShowRawInsn(
240 "When disassembling instructions, do not print the instruction bytes."),
241 cl::cat(ObjdumpCat));
243 cl::opt<bool> objdump::NoLeadingAddr("no-leading-addr",
244 cl::desc("Print no leading address"),
245 cl::cat(ObjdumpCat));
247 static cl::opt<bool> RawClangAST(
249 cl::desc("Dump the raw binary contents of the clang AST section"),
250 cl::cat(ObjdumpCat));
253 objdump::Relocations("reloc",
254 cl::desc("Display the relocation entries in the file"),
255 cl::cat(ObjdumpCat));
256 static cl::alias RelocationsShort("r", cl::desc("Alias for --reloc"),
257 cl::NotHidden, cl::Grouping,
258 cl::aliasopt(Relocations));
261 objdump::PrintImmHex("print-imm-hex",
262 cl::desc("Use hex format for immediate values"),
263 cl::cat(ObjdumpCat));
266 objdump::PrivateHeaders("private-headers",
267 cl::desc("Display format specific file headers"),
268 cl::cat(ObjdumpCat));
269 static cl::alias PrivateHeadersShort("p",
270 cl::desc("Alias for --private-headers"),
271 cl::NotHidden, cl::Grouping,
272 cl::aliasopt(PrivateHeaders));
274 cl::list<std::string>
275 objdump::FilterSections("section",
276 cl::desc("Operate on the specified sections only. "
277 "With -macho dump segment,section"),
278 cl::cat(ObjdumpCat));
279 static cl::alias FilterSectionsj("j", cl::desc("Alias for --section"),
280 cl::NotHidden, cl::Grouping, cl::Prefix,
281 cl::aliasopt(FilterSections));
283 cl::opt<bool> objdump::SectionHeaders(
285 cl::desc("Display summaries of the headers for each section."),
286 cl::cat(ObjdumpCat));
287 static cl::alias SectionHeadersShort("headers",
288 cl::desc("Alias for --section-headers"),
290 cl::aliasopt(SectionHeaders));
291 static cl::alias SectionHeadersShorter("h",
292 cl::desc("Alias for --section-headers"),
293 cl::NotHidden, cl::Grouping,
294 cl::aliasopt(SectionHeaders));
298 cl::desc("Display LMA column when dumping ELF section headers"),
299 cl::cat(ObjdumpCat));
301 static cl::opt<bool> PrintSource(
304 "Display source inlined with disassembly. Implies disassemble object"),
305 cl::cat(ObjdumpCat));
306 static cl::alias PrintSourceShort("S", cl::desc("Alias for -source"),
307 cl::NotHidden, cl::Grouping,
308 cl::aliasopt(PrintSource));
310 static cl::opt<uint64_t>
311 StartAddress("start-address", cl::desc("Disassemble beginning at address"),
312 cl::value_desc("address"), cl::init(0), cl::cat(ObjdumpCat));
313 static cl::opt<uint64_t> StopAddress("stop-address",
314 cl::desc("Stop disassembly at address"),
315 cl::value_desc("address"),
316 cl::init(UINT64_MAX), cl::cat(ObjdumpCat));
318 cl::opt<bool> objdump::SymbolTable("syms", cl::desc("Display the symbol table"),
319 cl::cat(ObjdumpCat));
320 static cl::alias SymbolTableShort("t", cl::desc("Alias for --syms"),
321 cl::NotHidden, cl::Grouping,
322 cl::aliasopt(SymbolTable));
324 static cl::opt<bool> DynamicSymbolTable(
326 cl::desc("Display the contents of the dynamic symbol table"),
327 cl::cat(ObjdumpCat));
328 static cl::alias DynamicSymbolTableShort("T",
329 cl::desc("Alias for --dynamic-syms"),
330 cl::NotHidden, cl::Grouping,
331 cl::aliasopt(DynamicSymbolTable));
333 cl::opt<std::string> objdump::TripleName(
336 "Target triple to disassemble for, see -version for available targets"),
337 cl::cat(ObjdumpCat));
339 cl::opt<bool> objdump::UnwindInfo("unwind-info",
340 cl::desc("Display unwind information"),
341 cl::cat(ObjdumpCat));
342 static cl::alias UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
343 cl::NotHidden, cl::Grouping,
344 cl::aliasopt(UnwindInfo));
347 Wide("wide", cl::desc("Ignored for compatibility with GNU objdump"),
348 cl::cat(ObjdumpCat));
349 static cl::alias WideShort("w", cl::Grouping, cl::aliasopt(Wide));
351 enum DebugVarsFormat {
357 static cl::opt<DebugVarsFormat> DbgVariables(
358 "debug-vars", cl::init(DVDisabled),
359 cl::desc("Print the locations (in registers or memory) of "
360 "source-level variables alongside disassembly"),
362 cl::values(clEnumValN(DVUnicode, "", "unicode"),
363 clEnumValN(DVUnicode, "unicode", "unicode"),
364 clEnumValN(DVASCII, "ascii", "unicode")),
365 cl::cat(ObjdumpCat));
368 DbgIndent("debug-vars-indent", cl::init(40),
369 cl::desc("Distance to indent the source-level variable display, "
370 "relative to the start of the disassembly"),
371 cl::cat(ObjdumpCat));
374 HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
376 static StringSet<> DisasmSymbolSet;
377 StringSet<> objdump::FoundSectionSet;
378 static StringRef ToolName;
381 struct FilterResult {
382 // True if the section should not be skipped.
385 // True if the index counter should be incremented, even if the section should
386 // be skipped. For example, sections may be skipped if they are not included
387 // in the --section flag, but we still want those to count toward the section
393 static FilterResult checkSectionFilter(object::SectionRef S) {
394 if (FilterSections.empty())
395 return {/*Keep=*/true, /*IncrementIndex=*/true};
397 Expected<StringRef> SecNameOrErr = S.getName();
399 consumeError(SecNameOrErr.takeError());
400 return {/*Keep=*/false, /*IncrementIndex=*/false};
402 StringRef SecName = *SecNameOrErr;
404 // StringSet does not allow empty key so avoid adding sections with
405 // no name (such as the section with index 0) here.
406 if (!SecName.empty())
407 FoundSectionSet.insert(SecName);
409 // Only show the section if it's in the FilterSections list, but always
410 // increment so the indexing is stable.
411 return {/*Keep=*/is_contained(FilterSections, SecName),
412 /*IncrementIndex=*/true};
415 SectionFilter objdump::ToolSectionFilter(object::ObjectFile const &O,
417 // Start at UINT64_MAX so that the first index returned after an increment is
418 // zero (after the unsigned wrap).
421 return SectionFilter(
422 [Idx](object::SectionRef S) {
423 FilterResult Result = checkSectionFilter(S);
424 if (Idx != nullptr && Result.IncrementIndex)
431 std::string objdump::getFileNameForError(const object::Archive::Child &C,
433 Expected<StringRef> NameOrErr = C.getName();
435 return std::string(NameOrErr.get());
436 // If we have an error getting the name then we print the index of the archive
437 // member. Since we are already in an error state, we just ignore this error.
438 consumeError(NameOrErr.takeError());
439 return "<file index: " + std::to_string(Index) + ">";
442 void objdump::reportWarning(Twine Message, StringRef File) {
443 // Output order between errs() and outs() matters especially for archive
444 // files where the output is per member object.
446 WithColor::warning(errs(), ToolName)
447 << "'" << File << "': " << Message << "\n";
450 LLVM_ATTRIBUTE_NORETURN void objdump::reportError(StringRef File,
453 WithColor::error(errs(), ToolName) << "'" << File << "': " << Message << "\n";
457 LLVM_ATTRIBUTE_NORETURN void objdump::reportError(Error E, StringRef FileName,
458 StringRef ArchiveName,
459 StringRef ArchitectureName) {
462 WithColor::error(errs(), ToolName);
463 if (ArchiveName != "")
464 errs() << ArchiveName << "(" << FileName << ")";
466 errs() << "'" << FileName << "'";
467 if (!ArchitectureName.empty())
468 errs() << " (for architecture " << ArchitectureName << ")";
470 logAllUnhandledErrors(std::move(E), errs());
474 static void reportCmdLineWarning(Twine Message) {
475 WithColor::warning(errs(), ToolName) << Message << "\n";
478 LLVM_ATTRIBUTE_NORETURN static void reportCmdLineError(Twine Message) {
479 WithColor::error(errs(), ToolName) << Message << "\n";
483 static void warnOnNoMatchForSections() {
484 SetVector<StringRef> MissingSections;
485 for (StringRef S : FilterSections) {
486 if (FoundSectionSet.count(S))
488 // User may specify a unnamed section. Don't warn for it.
490 MissingSections.insert(S);
493 // Warn only if no section in FilterSections is matched.
494 for (StringRef S : MissingSections)
495 reportCmdLineWarning("section '" + S +
496 "' mentioned in a -j/--section option, but not "
497 "found in any input file");
500 static const Target *getTarget(const ObjectFile *Obj) {
501 // Figure out the target triple.
502 Triple TheTriple("unknown-unknown-unknown");
503 if (TripleName.empty()) {
504 TheTriple = Obj->makeTriple();
506 TheTriple.setTriple(Triple::normalize(TripleName));
507 auto Arch = Obj->getArch();
508 if (Arch == Triple::arm || Arch == Triple::armeb)
509 Obj->setARMSubArch(TheTriple);
512 // Get the target specific parser.
514 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
517 reportError(Obj->getFileName(), "can't find target: " + Error);
519 // Update the triple name and return the found target.
520 TripleName = TheTriple.getTriple();
524 bool objdump::isRelocAddressLess(RelocationRef A, RelocationRef B) {
525 return A.getOffset() < B.getOffset();
528 static Error getRelocationValueString(const RelocationRef &Rel,
529 SmallVectorImpl<char> &Result) {
530 const ObjectFile *Obj = Rel.getObject();
531 if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
532 return getELFRelocationValueString(ELF, Rel, Result);
533 if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
534 return getCOFFRelocationValueString(COFF, Rel, Result);
535 if (auto *Wasm = dyn_cast<WasmObjectFile>(Obj))
536 return getWasmRelocationValueString(Wasm, Rel, Result);
537 if (auto *MachO = dyn_cast<MachOObjectFile>(Obj))
538 return getMachORelocationValueString(MachO, Rel, Result);
539 if (auto *XCOFF = dyn_cast<XCOFFObjectFile>(Obj))
540 return getXCOFFRelocationValueString(XCOFF, Rel, Result);
541 llvm_unreachable("unknown object file format");
544 /// Indicates whether this relocation should hidden when listing
545 /// relocations, usually because it is the trailing part of a multipart
546 /// relocation that will be printed as part of the leading relocation.
547 static bool getHidden(RelocationRef RelRef) {
548 auto *MachO = dyn_cast<MachOObjectFile>(RelRef.getObject());
552 unsigned Arch = MachO->getArch();
553 DataRefImpl Rel = RelRef.getRawDataRefImpl();
554 uint64_t Type = MachO->getRelocationType(Rel);
556 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
558 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc)
559 return Type == MachO::GENERIC_RELOC_PAIR;
561 if (Arch == Triple::x86_64) {
562 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
563 // an X86_64_RELOC_SUBTRACTOR.
564 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
565 DataRefImpl RelPrev = Rel;
567 uint64_t PrevType = MachO->getRelocationType(RelPrev);
568 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
578 /// Get the column at which we want to start printing the instruction
579 /// disassembly, taking into account anything which appears to the left of it.
580 unsigned getInstStartColumn(const MCSubtargetInfo &STI) {
581 return NoShowRawInsn ? 16 : STI.getTargetTriple().isX86() ? 40 : 24;
584 /// Stores a single expression representing the location of a source-level
585 /// variable, along with the PC range for which that expression is valid.
586 struct LiveVariable {
587 DWARFLocationExpression LocExpr;
590 const DWARFDie FuncDie;
592 LiveVariable(const DWARFLocationExpression &LocExpr, const char *VarName,
593 DWARFUnit *Unit, const DWARFDie FuncDie)
594 : LocExpr(LocExpr), VarName(VarName), Unit(Unit), FuncDie(FuncDie) {}
596 bool liveAtAddress(object::SectionedAddress Addr) {
597 if (LocExpr.Range == None)
599 return LocExpr.Range->SectionIndex == Addr.SectionIndex &&
600 LocExpr.Range->LowPC <= Addr.Address &&
601 LocExpr.Range->HighPC > Addr.Address;
604 void print(raw_ostream &OS, const MCRegisterInfo &MRI) const {
605 DataExtractor Data({LocExpr.Expr.data(), LocExpr.Expr.size()},
606 Unit->getContext().isLittleEndian(), 0);
607 DWARFExpression Expression(Data, Unit->getAddressByteSize());
608 Expression.printCompact(OS, MRI);
612 /// Helper class for printing source variable locations alongside disassembly.
613 class LiveVariablePrinter {
614 // Information we want to track about one column in which we are printing a
615 // variable live range.
617 unsigned VarIdx = NullVarIdx;
619 bool LiveOut = false;
620 bool MustDrawLabel = false;
622 bool isActive() const { return VarIdx != NullVarIdx; }
624 static constexpr unsigned NullVarIdx = std::numeric_limits<unsigned>::max();
627 // All live variables we know about in the object/image file.
628 std::vector<LiveVariable> LiveVariables;
630 // The columns we are currently drawing.
631 IndexedMap<Column> ActiveCols;
633 const MCRegisterInfo &MRI;
634 const MCSubtargetInfo &STI;
636 void addVariable(DWARFDie FuncDie, DWARFDie VarDie) {
637 uint64_t FuncLowPC, FuncHighPC, SectionIndex;
638 FuncDie.getLowAndHighPC(FuncLowPC, FuncHighPC, SectionIndex);
639 const char *VarName = VarDie.getName(DINameKind::ShortName);
640 DWARFUnit *U = VarDie.getDwarfUnit();
642 Expected<DWARFLocationExpressionsVector> Locs =
643 VarDie.getLocations(dwarf::DW_AT_location);
645 // If the variable doesn't have any locations, just ignore it. We don't
646 // report an error or warning here as that could be noisy on optimised
648 consumeError(Locs.takeError());
652 for (const DWARFLocationExpression &LocExpr : *Locs) {
654 LiveVariables.emplace_back(LocExpr, VarName, U, FuncDie);
656 // If the LocExpr does not have an associated range, it is valid for
657 // the whole of the function.
658 // TODO: technically it is not valid for any range covered by another
659 // LocExpr, does that happen in reality?
660 DWARFLocationExpression WholeFuncExpr{
661 DWARFAddressRange(FuncLowPC, FuncHighPC, SectionIndex),
663 LiveVariables.emplace_back(WholeFuncExpr, VarName, U, FuncDie);
668 void addFunction(DWARFDie D) {
669 for (const DWARFDie &Child : D.children()) {
670 if (Child.getTag() == dwarf::DW_TAG_variable ||
671 Child.getTag() == dwarf::DW_TAG_formal_parameter)
672 addVariable(D, Child);
678 // Get the column number (in characters) at which the first live variable
679 // line should be printed.
680 unsigned getIndentLevel() const {
681 return DbgIndent + getInstStartColumn(STI);
684 // Indent to the first live-range column to the right of the currently
685 // printed line, and return the index of that column.
686 // TODO: formatted_raw_ostream uses "column" to mean a number of characters
687 // since the last \n, and we use it to mean the number of slots in which we
688 // put live variable lines. Pick a less overloaded word.
689 unsigned moveToFirstVarColumn(formatted_raw_ostream &OS) {
690 // Logical column number: column zero is the first column we print in, each
691 // logical column is 2 physical columns wide.
692 unsigned FirstUnprintedLogicalColumn =
693 std::max((int)(OS.getColumn() - getIndentLevel() + 1) / 2, 0);
694 // Physical column number: the actual column number in characters, with
695 // zero being the left-most side of the screen.
696 unsigned FirstUnprintedPhysicalColumn =
697 getIndentLevel() + FirstUnprintedLogicalColumn * 2;
699 if (FirstUnprintedPhysicalColumn > OS.getColumn())
700 OS.PadToColumn(FirstUnprintedPhysicalColumn);
702 return FirstUnprintedLogicalColumn;
705 unsigned findFreeColumn() {
706 for (unsigned ColIdx = 0; ColIdx < ActiveCols.size(); ++ColIdx)
707 if (!ActiveCols[ColIdx].isActive())
710 size_t OldSize = ActiveCols.size();
711 ActiveCols.grow(std::max<size_t>(OldSize * 2, 1));
716 LiveVariablePrinter(const MCRegisterInfo &MRI, const MCSubtargetInfo &STI)
717 : LiveVariables(), ActiveCols(Column()), MRI(MRI), STI(STI) {}
720 for (const LiveVariable &LV : LiveVariables) {
721 dbgs() << LV.VarName << " @ " << LV.LocExpr.Range << ": ";
722 LV.print(dbgs(), MRI);
727 void addCompileUnit(DWARFDie D) {
728 if (D.getTag() == dwarf::DW_TAG_subprogram)
731 for (const DWARFDie &Child : D.children())
735 /// Update to match the state of the instruction between ThisAddr and
736 /// NextAddr. In the common case, any live range active at ThisAddr is
737 /// live-in to the instruction, and any live range active at NextAddr is
738 /// live-out of the instruction. If IncludeDefinedVars is false, then live
739 /// ranges starting at NextAddr will be ignored.
740 void update(object::SectionedAddress ThisAddr,
741 object::SectionedAddress NextAddr, bool IncludeDefinedVars) {
742 // First, check variables which have already been assigned a column, so
743 // that we don't change their order.
744 SmallSet<unsigned, 8> CheckedVarIdxs;
745 for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx) {
746 if (!ActiveCols[ColIdx].isActive())
748 CheckedVarIdxs.insert(ActiveCols[ColIdx].VarIdx);
749 LiveVariable &LV = LiveVariables[ActiveCols[ColIdx].VarIdx];
750 ActiveCols[ColIdx].LiveIn = LV.liveAtAddress(ThisAddr);
751 ActiveCols[ColIdx].LiveOut = LV.liveAtAddress(NextAddr);
752 LLVM_DEBUG(dbgs() << "pass 1, " << ThisAddr.Address << "-"
753 << NextAddr.Address << ", " << LV.VarName << ", Col "
754 << ColIdx << ": LiveIn=" << ActiveCols[ColIdx].LiveIn
755 << ", LiveOut=" << ActiveCols[ColIdx].LiveOut << "\n");
757 if (!ActiveCols[ColIdx].LiveIn && !ActiveCols[ColIdx].LiveOut)
758 ActiveCols[ColIdx].VarIdx = Column::NullVarIdx;
761 // Next, look for variables which don't already have a column, but which
763 if (IncludeDefinedVars) {
764 for (unsigned VarIdx = 0, End = LiveVariables.size(); VarIdx < End;
766 if (CheckedVarIdxs.count(VarIdx))
768 LiveVariable &LV = LiveVariables[VarIdx];
769 bool LiveIn = LV.liveAtAddress(ThisAddr);
770 bool LiveOut = LV.liveAtAddress(NextAddr);
771 if (!LiveIn && !LiveOut)
774 unsigned ColIdx = findFreeColumn();
775 LLVM_DEBUG(dbgs() << "pass 2, " << ThisAddr.Address << "-"
776 << NextAddr.Address << ", " << LV.VarName << ", Col "
777 << ColIdx << ": LiveIn=" << LiveIn
778 << ", LiveOut=" << LiveOut << "\n");
779 ActiveCols[ColIdx].VarIdx = VarIdx;
780 ActiveCols[ColIdx].LiveIn = LiveIn;
781 ActiveCols[ColIdx].LiveOut = LiveOut;
782 ActiveCols[ColIdx].MustDrawLabel = true;
787 enum class LineChar {
796 const char *getLineChar(LineChar C) const {
797 bool IsASCII = DbgVariables == DVASCII;
799 case LineChar::RangeStart:
800 return IsASCII ? "^" : u8"\u2548";
801 case LineChar::RangeMid:
802 return IsASCII ? "|" : u8"\u2503";
803 case LineChar::RangeEnd:
804 return IsASCII ? "v" : u8"\u253b";
805 case LineChar::LabelVert:
806 return IsASCII ? "|" : u8"\u2502";
807 case LineChar::LabelCornerNew:
808 return IsASCII ? "/" : u8"\u250c";
809 case LineChar::LabelCornerActive:
810 return IsASCII ? "|" : u8"\u2520";
811 case LineChar::LabelHoriz:
812 return IsASCII ? "-" : u8"\u2500";
814 llvm_unreachable("Unhandled LineChar enum");
817 /// Print live ranges to the right of an existing line. This assumes the
818 /// line is not an instruction, so doesn't start or end any live ranges, so
819 /// we only need to print active ranges or empty columns. If AfterInst is
820 /// true, this is being printed after the last instruction fed to update(),
821 /// otherwise this is being printed before it.
822 void printAfterOtherLine(formatted_raw_ostream &OS, bool AfterInst) {
823 if (ActiveCols.size()) {
824 unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);
825 for (size_t ColIdx = FirstUnprintedColumn, End = ActiveCols.size();
826 ColIdx < End; ++ColIdx) {
827 if (ActiveCols[ColIdx].isActive()) {
828 if ((AfterInst && ActiveCols[ColIdx].LiveOut) ||
829 (!AfterInst && ActiveCols[ColIdx].LiveIn))
830 OS << getLineChar(LineChar::RangeMid);
831 else if (!AfterInst && ActiveCols[ColIdx].LiveOut)
832 OS << getLineChar(LineChar::LabelVert);
842 /// Print any live variable range info needed to the right of a
843 /// non-instruction line of disassembly. This is where we print the variable
844 /// names and expressions, with thin line-drawing characters connecting them
845 /// to the live range which starts at the next instruction. If MustPrint is
846 /// true, we have to print at least one line (with the continuation of any
847 /// already-active live ranges) because something has already been printed
848 /// earlier on this line.
849 void printBetweenInsts(formatted_raw_ostream &OS, bool MustPrint) {
850 bool PrintedSomething = false;
851 for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx) {
852 if (ActiveCols[ColIdx].isActive() && ActiveCols[ColIdx].MustDrawLabel) {
853 // First we need to print the live range markers for any active
854 // columns to the left of this one.
855 OS.PadToColumn(getIndentLevel());
856 for (unsigned ColIdx2 = 0; ColIdx2 < ColIdx; ++ColIdx2) {
857 if (ActiveCols[ColIdx2].isActive()) {
858 if (ActiveCols[ColIdx2].MustDrawLabel &&
859 !ActiveCols[ColIdx2].LiveIn)
860 OS << getLineChar(LineChar::LabelVert) << " ";
862 OS << getLineChar(LineChar::RangeMid) << " ";
867 // Then print the variable name and location of the new live range,
868 // with box drawing characters joining it to the live range line.
869 OS << getLineChar(ActiveCols[ColIdx].LiveIn
870 ? LineChar::LabelCornerActive
871 : LineChar::LabelCornerNew)
872 << getLineChar(LineChar::LabelHoriz) << " ";
873 WithColor(OS, raw_ostream::GREEN)
874 << LiveVariables[ActiveCols[ColIdx].VarIdx].VarName;
877 WithColor ExprColor(OS, raw_ostream::CYAN);
878 LiveVariables[ActiveCols[ColIdx].VarIdx].print(OS, MRI);
881 // If there are any columns to the right of the expression we just
882 // printed, then continue their live range lines.
883 unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);
884 for (unsigned ColIdx2 = FirstUnprintedColumn, End = ActiveCols.size();
885 ColIdx2 < End; ++ColIdx2) {
886 if (ActiveCols[ColIdx2].isActive() && ActiveCols[ColIdx2].LiveIn)
887 OS << getLineChar(LineChar::RangeMid) << " ";
893 PrintedSomething = true;
897 for (unsigned ColIdx = 0, End = ActiveCols.size(); ColIdx < End; ++ColIdx)
898 if (ActiveCols[ColIdx].isActive())
899 ActiveCols[ColIdx].MustDrawLabel = false;
901 // If we must print something (because we printed a line/column number),
902 // but don't have any new variables to print, then print a line which
903 // just continues any existing live ranges.
904 if (MustPrint && !PrintedSomething)
905 printAfterOtherLine(OS, false);
908 /// Print the live variable ranges to the right of a disassembled instruction.
909 void printAfterInst(formatted_raw_ostream &OS) {
910 if (!ActiveCols.size())
912 unsigned FirstUnprintedColumn = moveToFirstVarColumn(OS);
913 for (unsigned ColIdx = FirstUnprintedColumn, End = ActiveCols.size();
914 ColIdx < End; ++ColIdx) {
915 if (!ActiveCols[ColIdx].isActive())
917 else if (ActiveCols[ColIdx].LiveIn && ActiveCols[ColIdx].LiveOut)
918 OS << getLineChar(LineChar::RangeMid) << " ";
919 else if (ActiveCols[ColIdx].LiveOut)
920 OS << getLineChar(LineChar::RangeStart) << " ";
921 else if (ActiveCols[ColIdx].LiveIn)
922 OS << getLineChar(LineChar::RangeEnd) << " ";
924 llvm_unreachable("var must be live in or out!");
929 class SourcePrinter {
931 DILineInfo OldLineInfo;
932 const ObjectFile *Obj = nullptr;
933 std::unique_ptr<symbolize::LLVMSymbolizer> Symbolizer;
934 // File name to file contents of source.
935 std::unordered_map<std::string, std::unique_ptr<MemoryBuffer>> SourceCache;
936 // Mark the line endings of the cached source.
937 std::unordered_map<std::string, std::vector<StringRef>> LineCache;
938 // Keep track of missing sources.
939 StringSet<> MissingSources;
940 // Only emit 'no debug info' warning once.
941 bool WarnedNoDebugInfo;
944 bool cacheSource(const DILineInfo& LineInfoFile);
946 void printLines(formatted_raw_ostream &OS, const DILineInfo &LineInfo,
947 StringRef Delimiter, LiveVariablePrinter &LVP);
949 void printSources(formatted_raw_ostream &OS, const DILineInfo &LineInfo,
950 StringRef ObjectFilename, StringRef Delimiter,
951 LiveVariablePrinter &LVP);
954 SourcePrinter() = default;
955 SourcePrinter(const ObjectFile *Obj, StringRef DefaultArch)
956 : Obj(Obj), WarnedNoDebugInfo(false) {
957 symbolize::LLVMSymbolizer::Options SymbolizerOpts;
958 SymbolizerOpts.PrintFunctions =
959 DILineInfoSpecifier::FunctionNameKind::LinkageName;
960 SymbolizerOpts.Demangle = Demangle;
961 SymbolizerOpts.DefaultArch = std::string(DefaultArch);
962 Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts));
964 virtual ~SourcePrinter() = default;
965 virtual void printSourceLine(formatted_raw_ostream &OS,
966 object::SectionedAddress Address,
967 StringRef ObjectFilename,
968 LiveVariablePrinter &LVP,
969 StringRef Delimiter = "; ");
972 bool SourcePrinter::cacheSource(const DILineInfo &LineInfo) {
973 std::unique_ptr<MemoryBuffer> Buffer;
974 if (LineInfo.Source) {
975 Buffer = MemoryBuffer::getMemBuffer(*LineInfo.Source);
977 auto BufferOrError = MemoryBuffer::getFile(LineInfo.FileName);
978 if (!BufferOrError) {
979 if (MissingSources.insert(LineInfo.FileName).second)
980 reportWarning("failed to find source " + LineInfo.FileName,
984 Buffer = std::move(*BufferOrError);
986 // Chomp the file to get lines
987 const char *BufferStart = Buffer->getBufferStart(),
988 *BufferEnd = Buffer->getBufferEnd();
989 std::vector<StringRef> &Lines = LineCache[LineInfo.FileName];
990 const char *Start = BufferStart;
991 for (const char *I = BufferStart; I != BufferEnd; ++I)
993 Lines.emplace_back(Start, I - Start - (BufferStart < I && I[-1] == '\r'));
996 if (Start < BufferEnd)
997 Lines.emplace_back(Start, BufferEnd - Start);
998 SourceCache[LineInfo.FileName] = std::move(Buffer);
1002 void SourcePrinter::printSourceLine(formatted_raw_ostream &OS,
1003 object::SectionedAddress Address,
1004 StringRef ObjectFilename,
1005 LiveVariablePrinter &LVP,
1006 StringRef Delimiter) {
1010 DILineInfo LineInfo = DILineInfo();
1011 auto ExpectedLineInfo = Symbolizer->symbolizeCode(*Obj, Address);
1012 std::string ErrorMessage;
1013 if (!ExpectedLineInfo)
1014 ErrorMessage = toString(ExpectedLineInfo.takeError());
1016 LineInfo = *ExpectedLineInfo;
1018 if (LineInfo.FileName == DILineInfo::BadString) {
1019 if (!WarnedNoDebugInfo) {
1020 std::string Warning =
1021 "failed to parse debug information for " + ObjectFilename.str();
1022 if (!ErrorMessage.empty())
1023 Warning += ": " + ErrorMessage;
1024 reportWarning(Warning, ObjectFilename);
1025 WarnedNoDebugInfo = true;
1030 printLines(OS, LineInfo, Delimiter, LVP);
1032 printSources(OS, LineInfo, ObjectFilename, Delimiter, LVP);
1033 OldLineInfo = LineInfo;
1036 void SourcePrinter::printLines(formatted_raw_ostream &OS,
1037 const DILineInfo &LineInfo, StringRef Delimiter,
1038 LiveVariablePrinter &LVP) {
1039 bool PrintFunctionName = LineInfo.FunctionName != DILineInfo::BadString &&
1040 LineInfo.FunctionName != OldLineInfo.FunctionName;
1041 if (PrintFunctionName) {
1042 OS << Delimiter << LineInfo.FunctionName;
1043 // If demangling is successful, FunctionName will end with "()". Print it
1044 // only if demangling did not run or was unsuccessful.
1045 if (!StringRef(LineInfo.FunctionName).endswith("()"))
1049 if (LineInfo.FileName != DILineInfo::BadString && LineInfo.Line != 0 &&
1050 (OldLineInfo.Line != LineInfo.Line ||
1051 OldLineInfo.FileName != LineInfo.FileName || PrintFunctionName)) {
1052 OS << Delimiter << LineInfo.FileName << ":" << LineInfo.Line;
1053 LVP.printBetweenInsts(OS, true);
1057 void SourcePrinter::printSources(formatted_raw_ostream &OS,
1058 const DILineInfo &LineInfo,
1059 StringRef ObjectFilename, StringRef Delimiter,
1060 LiveVariablePrinter &LVP) {
1061 if (LineInfo.FileName == DILineInfo::BadString || LineInfo.Line == 0 ||
1062 (OldLineInfo.Line == LineInfo.Line &&
1063 OldLineInfo.FileName == LineInfo.FileName))
1066 if (SourceCache.find(LineInfo.FileName) == SourceCache.end())
1067 if (!cacheSource(LineInfo))
1069 auto LineBuffer = LineCache.find(LineInfo.FileName);
1070 if (LineBuffer != LineCache.end()) {
1071 if (LineInfo.Line > LineBuffer->second.size()) {
1074 "debug info line number {0} exceeds the number of lines in {1}",
1075 LineInfo.Line, LineInfo.FileName),
1079 // Vector begins at 0, line numbers are non-zero
1080 OS << Delimiter << LineBuffer->second[LineInfo.Line - 1];
1081 LVP.printBetweenInsts(OS, true);
1085 static bool isAArch64Elf(const ObjectFile *Obj) {
1086 const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj);
1087 return Elf && Elf->getEMachine() == ELF::EM_AARCH64;
1090 static bool isArmElf(const ObjectFile *Obj) {
1091 const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj);
1092 return Elf && Elf->getEMachine() == ELF::EM_ARM;
1095 static bool hasMappingSymbols(const ObjectFile *Obj) {
1096 return isArmElf(Obj) || isAArch64Elf(Obj);
1099 static void printRelocation(formatted_raw_ostream &OS, StringRef FileName,
1100 const RelocationRef &Rel, uint64_t Address,
1102 StringRef Fmt = Is64Bits ? "\t\t%016" PRIx64 ": " : "\t\t\t%08" PRIx64 ": ";
1103 SmallString<16> Name;
1104 SmallString<32> Val;
1105 Rel.getTypeName(Name);
1106 if (Error E = getRelocationValueString(Rel, Val))
1107 reportError(std::move(E), FileName);
1108 OS << format(Fmt.data(), Address) << Name << "\t" << Val;
1111 class PrettyPrinter {
1113 virtual ~PrettyPrinter() = default;
1115 printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
1116 object::SectionedAddress Address, formatted_raw_ostream &OS,
1117 StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
1118 StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
1119 LiveVariablePrinter &LVP) {
1120 if (SP && (PrintSource || PrintLines))
1121 SP->printSourceLine(OS, Address, ObjectFilename, LVP);
1122 LVP.printBetweenInsts(OS, false);
1124 size_t Start = OS.tell();
1126 OS << format("%8" PRIx64 ":", Address.Address);
1127 if (!NoShowRawInsn) {
1129 dumpBytes(Bytes, OS);
1132 // The output of printInst starts with a tab. Print some spaces so that
1133 // the tab has 1 column and advances to the target tab stop.
1134 unsigned TabStop = getInstStartColumn(STI);
1135 unsigned Column = OS.tell() - Start;
1136 OS.indent(Column < TabStop - 1 ? TabStop - 1 - Column : 7 - Column % 8);
1139 // See MCInstPrinter::printInst. On targets where a PC relative immediate
1140 // is relative to the next instruction and the length of a MCInst is
1141 // difficult to measure (x86), this is the address of the next
1144 Address.Address + (STI.getTargetTriple().isX86() ? Bytes.size() : 0);
1145 IP.printInst(MI, Addr, "", STI, OS);
1147 OS << "\t<unknown>";
1150 PrettyPrinter PrettyPrinterInst;
1152 class HexagonPrettyPrinter : public PrettyPrinter {
1154 void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
1155 formatted_raw_ostream &OS) {
1157 (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
1159 OS << format("%8" PRIx64 ":", Address);
1160 if (!NoShowRawInsn) {
1162 dumpBytes(Bytes.slice(0, 4), OS);
1163 OS << format("\t%08" PRIx32, opcode);
1166 void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
1167 object::SectionedAddress Address, formatted_raw_ostream &OS,
1168 StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
1169 StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
1170 LiveVariablePrinter &LVP) override {
1171 if (SP && (PrintSource || PrintLines))
1172 SP->printSourceLine(OS, Address, ObjectFilename, LVP, "");
1174 printLead(Bytes, Address.Address, OS);
1180 raw_string_ostream TempStream(Buffer);
1181 IP.printInst(MI, Address.Address, "", STI, TempStream);
1183 StringRef Contents(Buffer);
1184 // Split off bundle attributes
1185 auto PacketBundle = Contents.rsplit('\n');
1186 // Split off first instruction from the rest
1187 auto HeadTail = PacketBundle.first.split('\n');
1188 auto Preamble = " { ";
1189 auto Separator = "";
1191 // Hexagon's packets require relocations to be inline rather than
1192 // clustered at the end of the packet.
1193 std::vector<RelocationRef>::const_iterator RelCur = Rels->begin();
1194 std::vector<RelocationRef>::const_iterator RelEnd = Rels->end();
1195 auto PrintReloc = [&]() -> void {
1196 while ((RelCur != RelEnd) && (RelCur->getOffset() <= Address.Address)) {
1197 if (RelCur->getOffset() == Address.Address) {
1198 printRelocation(OS, ObjectFilename, *RelCur, Address.Address, false);
1205 while (!HeadTail.first.empty()) {
1208 if (SP && (PrintSource || PrintLines))
1209 SP->printSourceLine(OS, Address, ObjectFilename, LVP, "");
1210 printLead(Bytes, Address.Address, OS);
1214 auto Duplex = HeadTail.first.split('\v');
1215 if (!Duplex.second.empty()) {
1218 Inst = Duplex.second;
1221 Inst = HeadTail.first;
1223 HeadTail = HeadTail.second.split('\n');
1224 if (HeadTail.first.empty())
1225 OS << " } " << PacketBundle.second;
1227 Bytes = Bytes.slice(4);
1228 Address.Address += 4;
1232 HexagonPrettyPrinter HexagonPrettyPrinterInst;
1234 class AMDGCNPrettyPrinter : public PrettyPrinter {
1236 void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
1237 object::SectionedAddress Address, formatted_raw_ostream &OS,
1238 StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
1239 StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
1240 LiveVariablePrinter &LVP) override {
1241 if (SP && (PrintSource || PrintLines))
1242 SP->printSourceLine(OS, Address, ObjectFilename, LVP);
1245 SmallString<40> InstStr;
1246 raw_svector_ostream IS(InstStr);
1248 IP.printInst(MI, Address.Address, "", STI, IS);
1250 OS << left_justify(IS.str(), 60);
1252 // an unrecognized encoding - this is probably data so represent it
1253 // using the .long directive, or .byte directive if fewer than 4 bytes
1255 if (Bytes.size() >= 4) {
1256 OS << format("\t.long 0x%08" PRIx32 " ",
1257 support::endian::read32<support::little>(Bytes.data()));
1260 OS << format("\t.byte 0x%02" PRIx8, Bytes[0]);
1261 for (unsigned int i = 1; i < Bytes.size(); i++)
1262 OS << format(", 0x%02" PRIx8, Bytes[i]);
1263 OS.indent(55 - (6 * Bytes.size()));
1267 OS << format("// %012" PRIX64 ":", Address.Address);
1268 if (Bytes.size() >= 4) {
1269 // D should be casted to uint32_t here as it is passed by format to
1270 // snprintf as vararg.
1271 for (uint32_t D : makeArrayRef(
1272 reinterpret_cast<const support::little32_t *>(Bytes.data()),
1274 OS << format(" %08" PRIX32, D);
1276 for (unsigned char B : Bytes)
1277 OS << format(" %02" PRIX8, B);
1281 OS << " // " << Annot;
1284 AMDGCNPrettyPrinter AMDGCNPrettyPrinterInst;
1286 class BPFPrettyPrinter : public PrettyPrinter {
1288 void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
1289 object::SectionedAddress Address, formatted_raw_ostream &OS,
1290 StringRef Annot, MCSubtargetInfo const &STI, SourcePrinter *SP,
1291 StringRef ObjectFilename, std::vector<RelocationRef> *Rels,
1292 LiveVariablePrinter &LVP) override {
1293 if (SP && (PrintSource || PrintLines))
1294 SP->printSourceLine(OS, Address, ObjectFilename, LVP);
1296 OS << format("%8" PRId64 ":", Address.Address / 8);
1297 if (!NoShowRawInsn) {
1299 dumpBytes(Bytes, OS);
1302 IP.printInst(MI, Address.Address, "", STI, OS);
1304 OS << "\t<unknown>";
1307 BPFPrettyPrinter BPFPrettyPrinterInst;
1309 PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
1310 switch(Triple.getArch()) {
1312 return PrettyPrinterInst;
1313 case Triple::hexagon:
1314 return HexagonPrettyPrinterInst;
1315 case Triple::amdgcn:
1316 return AMDGCNPrettyPrinterInst;
1319 return BPFPrettyPrinterInst;
1324 static uint8_t getElfSymbolType(const ObjectFile *Obj, const SymbolRef &Sym) {
1325 assert(Obj->isELF());
1326 if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
1327 return Elf32LEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1328 if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
1329 return Elf64LEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1330 if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
1331 return Elf32BEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1332 if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj))
1333 return Elf64BEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1334 llvm_unreachable("Unsupported binary format");
1337 template <class ELFT> static void
1338 addDynamicElfSymbols(const ELFObjectFile<ELFT> *Obj,
1339 std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
1340 for (auto Symbol : Obj->getDynamicSymbolIterators()) {
1341 uint8_t SymbolType = Symbol.getELFType();
1342 if (SymbolType == ELF::STT_SECTION)
1345 uint64_t Address = unwrapOrError(Symbol.getAddress(), Obj->getFileName());
1346 // ELFSymbolRef::getAddress() returns size instead of value for common
1347 // symbols which is not desirable for disassembly output. Overriding.
1348 if (SymbolType == ELF::STT_COMMON)
1349 Address = Obj->getSymbol(Symbol.getRawDataRefImpl())->st_value;
1351 StringRef Name = unwrapOrError(Symbol.getName(), Obj->getFileName());
1355 section_iterator SecI =
1356 unwrapOrError(Symbol.getSection(), Obj->getFileName());
1357 if (SecI == Obj->section_end())
1360 AllSymbols[*SecI].emplace_back(Address, Name, SymbolType);
1365 addDynamicElfSymbols(const ObjectFile *Obj,
1366 std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
1367 assert(Obj->isELF());
1368 if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
1369 addDynamicElfSymbols(Elf32LEObj, AllSymbols);
1370 else if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
1371 addDynamicElfSymbols(Elf64LEObj, AllSymbols);
1372 else if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
1373 addDynamicElfSymbols(Elf32BEObj, AllSymbols);
1374 else if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj))
1375 addDynamicElfSymbols(Elf64BEObj, AllSymbols);
1377 llvm_unreachable("Unsupported binary format");
1380 static void addPltEntries(const ObjectFile *Obj,
1381 std::map<SectionRef, SectionSymbolsTy> &AllSymbols,
1382 StringSaver &Saver) {
1383 Optional<SectionRef> Plt = None;
1384 for (const SectionRef &Section : Obj->sections()) {
1385 Expected<StringRef> SecNameOrErr = Section.getName();
1386 if (!SecNameOrErr) {
1387 consumeError(SecNameOrErr.takeError());
1390 if (*SecNameOrErr == ".plt")
1395 if (auto *ElfObj = dyn_cast<ELFObjectFileBase>(Obj)) {
1396 for (auto PltEntry : ElfObj->getPltAddresses()) {
1397 SymbolRef Symbol(PltEntry.first, ElfObj);
1398 uint8_t SymbolType = getElfSymbolType(Obj, Symbol);
1400 StringRef Name = unwrapOrError(Symbol.getName(), Obj->getFileName());
1402 AllSymbols[*Plt].emplace_back(
1403 PltEntry.second, Saver.save((Name + "@plt").str()), SymbolType);
1408 // Normally the disassembly output will skip blocks of zeroes. This function
1409 // returns the number of zero bytes that can be skipped when dumping the
1410 // disassembly of the instructions in Buf.
1411 static size_t countSkippableZeroBytes(ArrayRef<uint8_t> Buf) {
1412 // Find the number of leading zeroes.
1414 while (N < Buf.size() && !Buf[N])
1417 // We may want to skip blocks of zero bytes, but unless we see
1418 // at least 8 of them in a row.
1422 // We skip zeroes in multiples of 4 because do not want to truncate an
1423 // instruction if it starts with a zero byte.
1427 // Returns a map from sections to their relocations.
1428 static std::map<SectionRef, std::vector<RelocationRef>>
1429 getRelocsMap(object::ObjectFile const &Obj) {
1430 std::map<SectionRef, std::vector<RelocationRef>> Ret;
1431 uint64_t I = (uint64_t)-1;
1432 for (SectionRef Sec : Obj.sections()) {
1434 Expected<section_iterator> RelocatedOrErr = Sec.getRelocatedSection();
1435 if (!RelocatedOrErr)
1436 reportError(Obj.getFileName(),
1437 "section (" + Twine(I) +
1438 "): failed to get a relocated section: " +
1439 toString(RelocatedOrErr.takeError()));
1441 section_iterator Relocated = *RelocatedOrErr;
1442 if (Relocated == Obj.section_end() || !checkSectionFilter(*Relocated).Keep)
1444 std::vector<RelocationRef> &V = Ret[*Relocated];
1445 for (const RelocationRef &R : Sec.relocations())
1447 // Sort relocations by address.
1448 llvm::stable_sort(V, isRelocAddressLess);
1453 // Used for --adjust-vma to check if address should be adjusted by the
1454 // specified value for a given section.
1455 // For ELF we do not adjust non-allocatable sections like debug ones,
1456 // because they are not loadable.
1457 // TODO: implement for other file formats.
1458 static bool shouldAdjustVA(const SectionRef &Section) {
1459 const ObjectFile *Obj = Section.getObject();
1461 return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC;
1466 typedef std::pair<uint64_t, char> MappingSymbolPair;
1467 static char getMappingSymbolKind(ArrayRef<MappingSymbolPair> MappingSymbols,
1470 partition_point(MappingSymbols, [Address](const MappingSymbolPair &Val) {
1471 return Val.first <= Address;
1473 // Return zero for any address before the first mapping symbol; this means
1474 // we should use the default disassembly mode, depending on the target.
1475 if (It == MappingSymbols.begin())
1477 return (It - 1)->second;
1480 static uint64_t dumpARMELFData(uint64_t SectionAddr, uint64_t Index,
1481 uint64_t End, const ObjectFile *Obj,
1482 ArrayRef<uint8_t> Bytes,
1483 ArrayRef<MappingSymbolPair> MappingSymbols,
1485 support::endianness Endian =
1486 Obj->isLittleEndian() ? support::little : support::big;
1487 OS << format("%8" PRIx64 ":\t", SectionAddr + Index);
1488 if (Index + 4 <= End) {
1489 dumpBytes(Bytes.slice(Index, 4), OS);
1491 << format_hex(support::endian::read32(Bytes.data() + Index, Endian),
1495 if (Index + 2 <= End) {
1496 dumpBytes(Bytes.slice(Index, 2), OS);
1497 OS << "\t\t.short\t"
1498 << format_hex(support::endian::read16(Bytes.data() + Index, Endian),
1502 dumpBytes(Bytes.slice(Index, 1), OS);
1503 OS << "\t\t.byte\t" << format_hex(Bytes[0], 4);
1507 static void dumpELFData(uint64_t SectionAddr, uint64_t Index, uint64_t End,
1508 ArrayRef<uint8_t> Bytes) {
1509 // print out data up to 8 bytes at a time in hex and ascii
1510 uint8_t AsciiData[9] = {'\0'};
1514 for (; Index < End; ++Index) {
1516 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1517 Byte = Bytes.slice(Index)[0];
1518 outs() << format(" %02x", Byte);
1519 AsciiData[NumBytes] = isPrint(Byte) ? Byte : '.';
1521 uint8_t IndentOffset = 0;
1523 if (Index == End - 1 || NumBytes > 8) {
1524 // Indent the space for less than 8 bytes data.
1525 // 2 spaces for byte and one for space between bytes
1526 IndentOffset = 3 * (8 - NumBytes);
1527 for (int Excess = NumBytes; Excess < 8; Excess++)
1528 AsciiData[Excess] = '\0';
1531 if (NumBytes == 8) {
1532 AsciiData[8] = '\0';
1533 outs() << std::string(IndentOffset, ' ') << " ";
1534 outs() << reinterpret_cast<char *>(AsciiData);
1541 SymbolInfoTy objdump::createSymbolInfo(const ObjectFile *Obj,
1542 const SymbolRef &Symbol) {
1543 const StringRef FileName = Obj->getFileName();
1544 const uint64_t Addr = unwrapOrError(Symbol.getAddress(), FileName);
1545 const StringRef Name = unwrapOrError(Symbol.getName(), FileName);
1547 if (Obj->isXCOFF() && SymbolDescription) {
1548 const auto *XCOFFObj = cast<XCOFFObjectFile>(Obj);
1549 DataRefImpl SymbolDRI = Symbol.getRawDataRefImpl();
1551 const uint32_t SymbolIndex = XCOFFObj->getSymbolIndex(SymbolDRI.p);
1552 Optional<XCOFF::StorageMappingClass> Smc =
1553 getXCOFFSymbolCsectSMC(XCOFFObj, Symbol);
1554 return SymbolInfoTy(Addr, Name, Smc, SymbolIndex,
1555 isLabel(XCOFFObj, Symbol));
1557 return SymbolInfoTy(Addr, Name,
1558 Obj->isELF() ? getElfSymbolType(Obj, Symbol)
1559 : (uint8_t)ELF::STT_NOTYPE);
1562 static SymbolInfoTy createDummySymbolInfo(const ObjectFile *Obj,
1563 const uint64_t Addr, StringRef &Name,
1565 if (Obj->isXCOFF() && SymbolDescription)
1566 return SymbolInfoTy(Addr, Name, None, None, false);
1568 return SymbolInfoTy(Addr, Name, Type);
1571 static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
1572 MCContext &Ctx, MCDisassembler *PrimaryDisAsm,
1573 MCDisassembler *SecondaryDisAsm,
1574 const MCInstrAnalysis *MIA, MCInstPrinter *IP,
1575 const MCSubtargetInfo *PrimarySTI,
1576 const MCSubtargetInfo *SecondarySTI,
1578 SourcePrinter &SP, bool InlineRelocs) {
1579 const MCSubtargetInfo *STI = PrimarySTI;
1580 MCDisassembler *DisAsm = PrimaryDisAsm;
1581 bool PrimaryIsThumb = false;
1583 PrimaryIsThumb = STI->checkFeatures("+thumb-mode");
1585 std::map<SectionRef, std::vector<RelocationRef>> RelocMap;
1587 RelocMap = getRelocsMap(*Obj);
1588 bool Is64Bits = Obj->getBytesInAddress() > 4;
1590 // Create a mapping from virtual address to symbol name. This is used to
1591 // pretty print the symbols while disassembling.
1592 std::map<SectionRef, SectionSymbolsTy> AllSymbols;
1593 SectionSymbolsTy AbsoluteSymbols;
1594 const StringRef FileName = Obj->getFileName();
1595 const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj);
1596 for (const SymbolRef &Symbol : Obj->symbols()) {
1597 StringRef Name = unwrapOrError(Symbol.getName(), FileName);
1598 if (Name.empty() && !(Obj->isXCOFF() && SymbolDescription))
1601 if (Obj->isELF() && getElfSymbolType(Obj, Symbol) == ELF::STT_SECTION)
1604 // Don't ask a Mach-O STAB symbol for its section unless you know that
1605 // STAB symbol's section field refers to a valid section index. Otherwise
1606 // the symbol may error trying to load a section that does not exist.
1608 DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
1609 uint8_t NType = (MachO->is64Bit() ?
1610 MachO->getSymbol64TableEntry(SymDRI).n_type:
1611 MachO->getSymbolTableEntry(SymDRI).n_type);
1612 if (NType & MachO::N_STAB)
1616 section_iterator SecI = unwrapOrError(Symbol.getSection(), FileName);
1617 if (SecI != Obj->section_end())
1618 AllSymbols[*SecI].push_back(createSymbolInfo(Obj, Symbol));
1620 AbsoluteSymbols.push_back(createSymbolInfo(Obj, Symbol));
1623 if (AllSymbols.empty() && Obj->isELF())
1624 addDynamicElfSymbols(Obj, AllSymbols);
1627 StringSaver Saver(A);
1628 addPltEntries(Obj, AllSymbols, Saver);
1630 // Create a mapping from virtual address to section. An empty section can
1631 // cause more than one section at the same address. Sort such sections to be
1632 // before same-addressed non-empty sections so that symbol lookups prefer the
1633 // non-empty section.
1634 std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
1635 for (SectionRef Sec : Obj->sections())
1636 SectionAddresses.emplace_back(Sec.getAddress(), Sec);
1637 llvm::stable_sort(SectionAddresses, [](const auto &LHS, const auto &RHS) {
1638 if (LHS.first != RHS.first)
1639 return LHS.first < RHS.first;
1640 return LHS.second.getSize() < RHS.second.getSize();
1643 // Linked executables (.exe and .dll files) typically don't include a real
1644 // symbol table but they might contain an export table.
1645 if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) {
1646 for (const auto &ExportEntry : COFFObj->export_directories()) {
1648 if (Error E = ExportEntry.getSymbolName(Name))
1649 reportError(std::move(E), Obj->getFileName());
1654 if (Error E = ExportEntry.getExportRVA(RVA))
1655 reportError(std::move(E), Obj->getFileName());
1657 uint64_t VA = COFFObj->getImageBase() + RVA;
1658 auto Sec = partition_point(
1659 SectionAddresses, [VA](const std::pair<uint64_t, SectionRef> &O) {
1660 return O.first <= VA;
1662 if (Sec != SectionAddresses.begin()) {
1664 AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE);
1666 AbsoluteSymbols.emplace_back(VA, Name, ELF::STT_NOTYPE);
1670 // Sort all the symbols, this allows us to use a simple binary search to find
1671 // Multiple symbols can have the same address. Use a stable sort to stabilize
1673 StringSet<> FoundDisasmSymbolSet;
1674 for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
1675 stable_sort(SecSyms.second);
1676 stable_sort(AbsoluteSymbols);
1678 std::unique_ptr<DWARFContext> DICtx;
1679 LiveVariablePrinter LVP(*Ctx.getRegisterInfo(), *STI);
1681 if (DbgVariables != DVDisabled) {
1682 DICtx = DWARFContext::create(*Obj);
1683 for (const std::unique_ptr<DWARFUnit> &CU : DICtx->compile_units())
1684 LVP.addCompileUnit(CU->getUnitDIE(false));
1687 LLVM_DEBUG(LVP.dump());
1689 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
1690 if (FilterSections.empty() && !DisassembleAll &&
1691 (!Section.isText() || Section.isVirtual()))
1694 uint64_t SectionAddr = Section.getAddress();
1695 uint64_t SectSize = Section.getSize();
1699 // Get the list of all the symbols in this section.
1700 SectionSymbolsTy &Symbols = AllSymbols[Section];
1701 std::vector<MappingSymbolPair> MappingSymbols;
1702 if (hasMappingSymbols(Obj)) {
1703 for (const auto &Symb : Symbols) {
1704 uint64_t Address = Symb.Addr;
1705 StringRef Name = Symb.Name;
1706 if (Name.startswith("$d"))
1707 MappingSymbols.emplace_back(Address - SectionAddr, 'd');
1708 if (Name.startswith("$x"))
1709 MappingSymbols.emplace_back(Address - SectionAddr, 'x');
1710 if (Name.startswith("$a"))
1711 MappingSymbols.emplace_back(Address - SectionAddr, 'a');
1712 if (Name.startswith("$t"))
1713 MappingSymbols.emplace_back(Address - SectionAddr, 't');
1717 llvm::sort(MappingSymbols);
1719 if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) {
1720 // AMDGPU disassembler uses symbolizer for printing labels
1721 std::unique_ptr<MCRelocationInfo> RelInfo(
1722 TheTarget->createMCRelocationInfo(TripleName, Ctx));
1724 std::unique_ptr<MCSymbolizer> Symbolizer(
1725 TheTarget->createMCSymbolizer(
1726 TripleName, nullptr, nullptr, &Symbols, &Ctx, std::move(RelInfo)));
1727 DisAsm->setSymbolizer(std::move(Symbolizer));
1731 StringRef SegmentName = "";
1733 DataRefImpl DR = Section.getRawDataRefImpl();
1734 SegmentName = MachO->getSectionFinalSegmentName(DR);
1737 StringRef SectionName = unwrapOrError(Section.getName(), Obj->getFileName());
1738 // If the section has no symbol at the start, just insert a dummy one.
1739 if (Symbols.empty() || Symbols[0].Addr != 0) {
1740 Symbols.insert(Symbols.begin(),
1741 createDummySymbolInfo(Obj, SectionAddr, SectionName,
1742 Section.isText() ? ELF::STT_FUNC
1743 : ELF::STT_OBJECT));
1746 SmallString<40> Comments;
1747 raw_svector_ostream CommentStream(Comments);
1749 ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(
1750 unwrapOrError(Section.getContents(), Obj->getFileName()));
1752 uint64_t VMAAdjustment = 0;
1753 if (shouldAdjustVA(Section))
1754 VMAAdjustment = AdjustVMA;
1758 bool PrintedSection = false;
1759 std::vector<RelocationRef> Rels = RelocMap[Section];
1760 std::vector<RelocationRef>::const_iterator RelCur = Rels.begin();
1761 std::vector<RelocationRef>::const_iterator RelEnd = Rels.end();
1762 // Disassemble symbol by symbol.
1763 for (unsigned SI = 0, SE = Symbols.size(); SI != SE; ++SI) {
1764 std::string SymbolName = Symbols[SI].Name.str();
1766 SymbolName = demangle(SymbolName);
1768 // Skip if --disassemble-symbols is not empty and the symbol is not in
1770 if (!DisasmSymbolSet.empty() && !DisasmSymbolSet.count(SymbolName))
1773 uint64_t Start = Symbols[SI].Addr;
1774 if (Start < SectionAddr || StopAddress <= Start)
1777 FoundDisasmSymbolSet.insert(SymbolName);
1779 // The end is the section end, the beginning of the next symbol, or
1781 uint64_t End = std::min<uint64_t>(SectionAddr + SectSize, StopAddress);
1783 End = std::min(End, Symbols[SI + 1].Addr);
1784 if (Start >= End || End <= StartAddress)
1786 Start -= SectionAddr;
1789 if (!PrintedSection) {
1790 PrintedSection = true;
1791 outs() << "\nDisassembly of section ";
1792 if (!SegmentName.empty())
1793 outs() << SegmentName << ",";
1794 outs() << SectionName << ":\n";
1797 if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) {
1798 if (Symbols[SI].Type == ELF::STT_AMDGPU_HSA_KERNEL) {
1799 // skip amd_kernel_code_t at the begining of kernel symbol (256 bytes)
1803 Symbols[SI + 1].Type == ELF::STT_AMDGPU_HSA_KERNEL) {
1804 // cut trailing zeroes at the end of kernel
1805 // cut up to 256 bytes
1806 const uint64_t EndAlign = 256;
1807 const auto Limit = End - (std::min)(EndAlign, End - Start);
1808 while (End > Limit &&
1809 *reinterpret_cast<const support::ulittle32_t*>(&Bytes[End - 4]) == 0)
1816 outs() << format(Is64Bits ? "%016" PRIx64 " " : "%08" PRIx64 " ",
1817 SectionAddr + Start + VMAAdjustment);
1818 if (Obj->isXCOFF() && SymbolDescription) {
1819 outs() << getXCOFFSymbolDescription(Symbols[SI], SymbolName) << ":\n";
1821 outs() << '<' << SymbolName << ">:\n";
1823 // Don't print raw contents of a virtual section. A virtual section
1824 // doesn't have any contents in the file.
1825 if (Section.isVirtual()) {
1830 auto Status = DisAsm->onSymbolStart(Symbols[SI], Size,
1831 Bytes.slice(Start, End - Start),
1832 SectionAddr + Start, CommentStream);
1833 // To have round trippable disassembly, we fall back to decoding the
1834 // remaining bytes as instructions.
1836 // If there is a failure, we disassemble the failed region as bytes before
1837 // falling back. The target is expected to print nothing in this case.
1839 // If there is Success or SoftFail i.e no 'real' failure, we go ahead by
1840 // Size bytes before falling back.
1841 // So if the entire symbol is 'eaten' by the target:
1842 // Start += Size // Now Start = End and we will never decode as
1845 // Right now, most targets return None i.e ignore to treat a symbol
1846 // separately. But WebAssembly decodes preludes for some symbols.
1848 if (Status.hasValue()) {
1849 if (Status.getValue() == MCDisassembler::Fail) {
1850 outs() << "// Error in decoding " << SymbolName
1851 << " : Decoding failed region as bytes.\n";
1852 for (uint64_t I = 0; I < Size; ++I) {
1853 outs() << "\t.byte\t " << format_hex(Bytes[I], 1, /*Upper=*/true)
1864 if (SectionAddr < StartAddress)
1865 Index = std::max<uint64_t>(Index, StartAddress - SectionAddr);
1867 // If there is a data/common symbol inside an ELF text section and we are
1868 // only disassembling text (applicable all architectures), we are in a
1869 // situation where we must print the data and not disassemble it.
1870 if (Obj->isELF() && !DisassembleAll && Section.isText()) {
1871 uint8_t SymTy = Symbols[SI].Type;
1872 if (SymTy == ELF::STT_OBJECT || SymTy == ELF::STT_COMMON) {
1873 dumpELFData(SectionAddr, Index, End, Bytes);
1878 bool CheckARMELFData = hasMappingSymbols(Obj) &&
1879 Symbols[SI].Type != ELF::STT_OBJECT &&
1881 bool DumpARMELFData = false;
1882 formatted_raw_ostream FOS(outs());
1883 while (Index < End) {
1884 // ARM and AArch64 ELF binaries can interleave data and text in the
1885 // same section. We rely on the markers introduced to understand what
1886 // we need to dump. If the data marker is within a function, it is
1887 // denoted as a word/short etc.
1888 if (CheckARMELFData) {
1889 char Kind = getMappingSymbolKind(MappingSymbols, Index);
1890 DumpARMELFData = Kind == 'd';
1893 STI = PrimaryIsThumb ? SecondarySTI : PrimarySTI;
1894 DisAsm = PrimaryIsThumb ? SecondaryDisAsm : PrimaryDisAsm;
1895 } else if (Kind == 't') {
1896 STI = PrimaryIsThumb ? PrimarySTI : SecondarySTI;
1897 DisAsm = PrimaryIsThumb ? PrimaryDisAsm : SecondaryDisAsm;
1902 if (DumpARMELFData) {
1903 Size = dumpARMELFData(SectionAddr, Index, End, Obj, Bytes,
1904 MappingSymbols, FOS);
1906 // When -z or --disassemble-zeroes are given we always dissasemble
1907 // them. Otherwise we might want to skip zero bytes we see.
1908 if (!DisassembleZeroes) {
1909 uint64_t MaxOffset = End - Index;
1910 // For --reloc: print zero blocks patched by relocations, so that
1911 // relocations can be shown in the dump.
1912 if (RelCur != RelEnd)
1913 MaxOffset = RelCur->getOffset() - Index;
1916 countSkippableZeroBytes(Bytes.slice(Index, MaxOffset))) {
1917 FOS << "\t\t..." << '\n';
1923 // Disassemble a real instruction or a data when disassemble all is
1927 DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
1928 SectionAddr + Index, CommentStream);
1932 LVP.update({Index, Section.getIndex()},
1933 {Index + Size, Section.getIndex()}, Index + Size != End);
1936 *IP, Disassembled ? &Inst : nullptr, Bytes.slice(Index, Size),
1937 {SectionAddr + Index + VMAAdjustment, Section.getIndex()}, FOS,
1938 "", *STI, &SP, Obj->getFileName(), &Rels, LVP);
1939 FOS << CommentStream.str();
1942 // If disassembly has failed, avoid analysing invalid/incomplete
1943 // instruction information. Otherwise, try to resolve the target
1944 // address (jump target or memory operand address) and print it on the
1945 // right of the instruction.
1946 if (Disassembled && MIA) {
1949 MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target);
1951 if (Optional<uint64_t> MaybeTarget =
1952 MIA->evaluateMemoryOperandAddress(
1953 Inst, SectionAddr + Index, Size)) {
1954 Target = *MaybeTarget;
1956 FOS << " # " << Twine::utohexstr(Target);
1959 // In a relocatable object, the target's section must reside in
1960 // the same section as the call instruction or it is accessed
1961 // through a relocation.
1963 // In a non-relocatable object, the target may be in any section.
1964 // In that case, locate the section(s) containing the target
1965 // address and find the symbol in one of those, if possible.
1967 // N.B. We don't walk the relocations in the relocatable case yet.
1968 std::vector<const SectionSymbolsTy *> TargetSectionSymbols;
1969 if (!Obj->isRelocatableObject()) {
1970 auto It = llvm::partition_point(
1972 [=](const std::pair<uint64_t, SectionRef> &O) {
1973 return O.first <= Target;
1975 uint64_t TargetSecAddr = 0;
1976 while (It != SectionAddresses.begin()) {
1978 if (TargetSecAddr == 0)
1979 TargetSecAddr = It->first;
1980 if (It->first != TargetSecAddr)
1982 TargetSectionSymbols.push_back(&AllSymbols[It->second]);
1985 TargetSectionSymbols.push_back(&Symbols);
1987 TargetSectionSymbols.push_back(&AbsoluteSymbols);
1989 // Find the last symbol in the first candidate section whose
1990 // offset is less than or equal to the target. If there are no
1991 // such symbols, try in the next section and so on, before finally
1992 // using the nearest preceding absolute symbol (if any), if there
1993 // are no other valid symbols.
1994 const SymbolInfoTy *TargetSym = nullptr;
1995 for (const SectionSymbolsTy *TargetSymbols :
1996 TargetSectionSymbols) {
1997 auto It = llvm::partition_point(
1999 [=](const SymbolInfoTy &O) { return O.Addr <= Target; });
2000 if (It != TargetSymbols->begin()) {
2001 TargetSym = &*(It - 1);
2006 if (TargetSym != nullptr) {
2007 uint64_t TargetAddress = TargetSym->Addr;
2008 std::string TargetName = TargetSym->Name.str();
2010 TargetName = demangle(TargetName);
2012 FOS << " <" << TargetName;
2013 uint64_t Disp = Target - TargetAddress;
2015 FOS << "+0x" << Twine::utohexstr(Disp);
2022 LVP.printAfterInst(FOS);
2025 // Hexagon does this in pretty printer
2026 if (Obj->getArch() != Triple::hexagon) {
2027 // Print relocation for instruction and data.
2028 while (RelCur != RelEnd) {
2029 uint64_t Offset = RelCur->getOffset();
2030 // If this relocation is hidden, skip it.
2031 if (getHidden(*RelCur) || SectionAddr + Offset < StartAddress) {
2036 // Stop when RelCur's offset is past the disassembled
2037 // instruction/data. Note that it's possible the disassembled data
2038 // is not the complete data: we might see the relocation printed in
2039 // the middle of the data, but this matches the binutils objdump
2041 if (Offset >= Index + Size)
2044 // When --adjust-vma is used, update the address printed.
2045 if (RelCur->getSymbol() != Obj->symbol_end()) {
2046 Expected<section_iterator> SymSI =
2047 RelCur->getSymbol()->getSection();
2048 if (SymSI && *SymSI != Obj->section_end() &&
2049 shouldAdjustVA(**SymSI))
2050 Offset += AdjustVMA;
2053 printRelocation(FOS, Obj->getFileName(), *RelCur,
2054 SectionAddr + Offset, Is64Bits);
2055 LVP.printAfterOtherLine(FOS, true);
2064 StringSet<> MissingDisasmSymbolSet =
2065 set_difference(DisasmSymbolSet, FoundDisasmSymbolSet);
2066 for (StringRef Sym : MissingDisasmSymbolSet.keys())
2067 reportWarning("failed to disassemble missing symbol " + Sym, FileName);
2070 static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
2071 const Target *TheTarget = getTarget(Obj);
2073 // Package up features to be passed to target/subtarget
2074 SubtargetFeatures Features = Obj->getFeatures();
2075 if (!MAttrs.empty())
2076 for (unsigned I = 0; I != MAttrs.size(); ++I)
2077 Features.AddFeature(MAttrs[I]);
2079 std::unique_ptr<const MCRegisterInfo> MRI(
2080 TheTarget->createMCRegInfo(TripleName));
2082 reportError(Obj->getFileName(),
2083 "no register info for target " + TripleName);
2085 // Set up disassembler.
2086 MCTargetOptions MCOptions;
2087 std::unique_ptr<const MCAsmInfo> AsmInfo(
2088 TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
2090 reportError(Obj->getFileName(),
2091 "no assembly info for target " + TripleName);
2092 std::unique_ptr<const MCSubtargetInfo> STI(
2093 TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString()));
2095 reportError(Obj->getFileName(),
2096 "no subtarget info for target " + TripleName);
2097 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
2099 reportError(Obj->getFileName(),
2100 "no instruction info for target " + TripleName);
2101 MCObjectFileInfo MOFI;
2102 MCContext Ctx(AsmInfo.get(), MRI.get(), &MOFI);
2103 // FIXME: for now initialize MCObjectFileInfo with default values
2104 MOFI.InitMCObjectFileInfo(Triple(TripleName), false, Ctx);
2106 std::unique_ptr<MCDisassembler> DisAsm(
2107 TheTarget->createMCDisassembler(*STI, Ctx));
2109 reportError(Obj->getFileName(), "no disassembler for target " + TripleName);
2111 // If we have an ARM object file, we need a second disassembler, because
2112 // ARM CPUs have two different instruction sets: ARM mode, and Thumb mode.
2113 // We use mapping symbols to switch between the two assemblers, where
2115 std::unique_ptr<MCDisassembler> SecondaryDisAsm;
2116 std::unique_ptr<const MCSubtargetInfo> SecondarySTI;
2117 if (isArmElf(Obj) && !STI->checkFeatures("+mclass")) {
2118 if (STI->checkFeatures("+thumb-mode"))
2119 Features.AddFeature("-thumb-mode");
2121 Features.AddFeature("+thumb-mode");
2122 SecondarySTI.reset(TheTarget->createMCSubtargetInfo(TripleName, MCPU,
2123 Features.getString()));
2124 SecondaryDisAsm.reset(TheTarget->createMCDisassembler(*SecondarySTI, Ctx));
2127 std::unique_ptr<const MCInstrAnalysis> MIA(
2128 TheTarget->createMCInstrAnalysis(MII.get()));
2130 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
2131 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
2132 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
2134 reportError(Obj->getFileName(),
2135 "no instruction printer for target " + TripleName);
2136 IP->setPrintImmHex(PrintImmHex);
2137 IP->setPrintBranchImmAsAddress(true);
2139 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
2140 SourcePrinter SP(Obj, TheTarget->getName());
2142 for (StringRef Opt : DisassemblerOptions)
2143 if (!IP->applyTargetSpecificCLOption(Opt))
2144 reportError(Obj->getFileName(),
2145 "Unrecognized disassembler option: " + Opt);
2147 disassembleObject(TheTarget, Obj, Ctx, DisAsm.get(), SecondaryDisAsm.get(),
2148 MIA.get(), IP.get(), STI.get(), SecondarySTI.get(), PIP,
2152 void objdump::printRelocations(const ObjectFile *Obj) {
2153 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
2155 // Regular objdump doesn't print relocations in non-relocatable object
2157 if (!Obj->isRelocatableObject())
2160 // Build a mapping from relocation target to a vector of relocation
2161 // sections. Usually, there is an only one relocation section for
2162 // each relocated section.
2163 MapVector<SectionRef, std::vector<SectionRef>> SecToRelSec;
2165 for (const SectionRef &Section : ToolSectionFilter(*Obj, &Ndx)) {
2166 if (Section.relocation_begin() == Section.relocation_end())
2168 Expected<section_iterator> SecOrErr = Section.getRelocatedSection();
2170 reportError(Obj->getFileName(),
2171 "section (" + Twine(Ndx) +
2172 "): unable to get a relocation target: " +
2173 toString(SecOrErr.takeError()));
2174 SecToRelSec[**SecOrErr].push_back(Section);
2177 for (std::pair<SectionRef, std::vector<SectionRef>> &P : SecToRelSec) {
2178 StringRef SecName = unwrapOrError(P.first.getName(), Obj->getFileName());
2179 outs() << "RELOCATION RECORDS FOR [" << SecName << "]:\n";
2180 uint32_t OffsetPadding = (Obj->getBytesInAddress() > 4 ? 16 : 8);
2181 uint32_t TypePadding = 24;
2182 outs() << left_justify("OFFSET", OffsetPadding) << " "
2183 << left_justify("TYPE", TypePadding) << " "
2186 for (SectionRef Section : P.second) {
2187 for (const RelocationRef &Reloc : Section.relocations()) {
2188 uint64_t Address = Reloc.getOffset();
2189 SmallString<32> RelocName;
2190 SmallString<32> ValueStr;
2191 if (Address < StartAddress || Address > StopAddress || getHidden(Reloc))
2193 Reloc.getTypeName(RelocName);
2194 if (Error E = getRelocationValueString(Reloc, ValueStr))
2195 reportError(std::move(E), Obj->getFileName());
2197 outs() << format(Fmt.data(), Address) << " "
2198 << left_justify(RelocName, TypePadding) << " " << ValueStr
2206 void objdump::printDynamicRelocations(const ObjectFile *Obj) {
2207 // For the moment, this option is for ELF only
2211 const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj);
2212 if (!Elf || Elf->getEType() != ELF::ET_DYN) {
2213 reportError(Obj->getFileName(), "not a dynamic object");
2217 std::vector<SectionRef> DynRelSec = Obj->dynamic_relocation_sections();
2218 if (DynRelSec.empty())
2221 outs() << "DYNAMIC RELOCATION RECORDS\n";
2222 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
2223 for (const SectionRef &Section : DynRelSec)
2224 for (const RelocationRef &Reloc : Section.relocations()) {
2225 uint64_t Address = Reloc.getOffset();
2226 SmallString<32> RelocName;
2227 SmallString<32> ValueStr;
2228 Reloc.getTypeName(RelocName);
2229 if (Error E = getRelocationValueString(Reloc, ValueStr))
2230 reportError(std::move(E), Obj->getFileName());
2231 outs() << format(Fmt.data(), Address) << " " << RelocName << " "
2232 << ValueStr << "\n";
2236 // Returns true if we need to show LMA column when dumping section headers. We
2237 // show it only when the platform is ELF and either we have at least one section
2238 // whose VMA and LMA are different and/or when --show-lma flag is used.
2239 static bool shouldDisplayLMA(const ObjectFile *Obj) {
2242 for (const SectionRef &S : ToolSectionFilter(*Obj))
2243 if (S.getAddress() != getELFSectionLMA(S))
2248 static size_t getMaxSectionNameWidth(const ObjectFile *Obj) {
2249 // Default column width for names is 13 even if no names are that long.
2250 size_t MaxWidth = 13;
2251 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
2252 StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName());
2253 MaxWidth = std::max(MaxWidth, Name.size());
2258 void objdump::printSectionHeaders(const ObjectFile *Obj) {
2259 size_t NameWidth = getMaxSectionNameWidth(Obj);
2260 size_t AddressWidth = 2 * Obj->getBytesInAddress();
2261 bool HasLMAColumn = shouldDisplayLMA(Obj);
2263 outs() << "Sections:\n"
2265 << left_justify("Name", NameWidth) << " Size "
2266 << left_justify("VMA", AddressWidth) << " "
2267 << left_justify("LMA", AddressWidth) << " Type\n";
2269 outs() << "Sections:\n"
2271 << left_justify("Name", NameWidth) << " Size "
2272 << left_justify("VMA", AddressWidth) << " Type\n";
2275 for (const SectionRef &Section : ToolSectionFilter(*Obj, &Idx)) {
2276 StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName());
2277 uint64_t VMA = Section.getAddress();
2278 if (shouldAdjustVA(Section))
2281 uint64_t Size = Section.getSize();
2283 std::string Type = Section.isText() ? "TEXT" : "";
2284 if (Section.isData())
2285 Type += Type.empty() ? "DATA" : " DATA";
2286 if (Section.isBSS())
2287 Type += Type.empty() ? "BSS" : " BSS";
2290 outs() << format("%3" PRIu64 " %-*s %08" PRIx64 " ", Idx, NameWidth,
2291 Name.str().c_str(), Size)
2292 << format_hex_no_prefix(VMA, AddressWidth) << " "
2293 << format_hex_no_prefix(getELFSectionLMA(Section), AddressWidth)
2294 << " " << Type << "\n";
2296 outs() << format("%3" PRIu64 " %-*s %08" PRIx64 " ", Idx, NameWidth,
2297 Name.str().c_str(), Size)
2298 << format_hex_no_prefix(VMA, AddressWidth) << " " << Type << "\n";
2303 void objdump::printSectionContents(const ObjectFile *Obj) {
2304 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
2305 StringRef Name = unwrapOrError(Section.getName(), Obj->getFileName());
2306 uint64_t BaseAddr = Section.getAddress();
2307 uint64_t Size = Section.getSize();
2311 outs() << "Contents of section " << Name << ":\n";
2312 if (Section.isBSS()) {
2313 outs() << format("<skipping contents of bss section at [%04" PRIx64
2314 ", %04" PRIx64 ")>\n",
2315 BaseAddr, BaseAddr + Size);
2319 StringRef Contents = unwrapOrError(Section.getContents(), Obj->getFileName());
2321 // Dump out the content as hex and printable ascii characters.
2322 for (std::size_t Addr = 0, End = Contents.size(); Addr < End; Addr += 16) {
2323 outs() << format(" %04" PRIx64 " ", BaseAddr + Addr);
2324 // Dump line of hex.
2325 for (std::size_t I = 0; I < 16; ++I) {
2326 if (I != 0 && I % 4 == 0)
2329 outs() << hexdigit((Contents[Addr + I] >> 4) & 0xF, true)
2330 << hexdigit(Contents[Addr + I] & 0xF, true);
2336 for (std::size_t I = 0; I < 16 && Addr + I < End; ++I) {
2337 if (isPrint(static_cast<unsigned char>(Contents[Addr + I]) & 0xFF))
2338 outs() << Contents[Addr + I];
2347 void objdump::printSymbolTable(const ObjectFile *O, StringRef ArchiveName,
2348 StringRef ArchitectureName, bool DumpDynamic) {
2349 if (O->isCOFF() && !DumpDynamic) {
2350 outs() << "SYMBOL TABLE:\n";
2351 printCOFFSymbolTable(cast<const COFFObjectFile>(O));
2355 const StringRef FileName = O->getFileName();
2358 outs() << "SYMBOL TABLE:\n";
2359 for (auto I = O->symbol_begin(); I != O->symbol_end(); ++I)
2360 printSymbol(O, *I, FileName, ArchiveName, ArchitectureName, DumpDynamic);
2364 outs() << "DYNAMIC SYMBOL TABLE:\n";
2367 "this operation is not currently supported for this file format",
2372 const ELFObjectFileBase *ELF = cast<const ELFObjectFileBase>(O);
2373 for (auto I = ELF->getDynamicSymbolIterators().begin();
2374 I != ELF->getDynamicSymbolIterators().end(); ++I)
2375 printSymbol(O, *I, FileName, ArchiveName, ArchitectureName, DumpDynamic);
2378 void objdump::printSymbol(const ObjectFile *O, const SymbolRef &Symbol,
2379 StringRef FileName, StringRef ArchiveName,
2380 StringRef ArchitectureName, bool DumpDynamic) {
2381 const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(O);
2382 uint64_t Address = unwrapOrError(Symbol.getAddress(), FileName, ArchiveName,
2384 if ((Address < StartAddress) || (Address > StopAddress))
2386 SymbolRef::Type Type =
2387 unwrapOrError(Symbol.getType(), FileName, ArchiveName, ArchitectureName);
2389 unwrapOrError(Symbol.getFlags(), FileName, ArchiveName, ArchitectureName);
2391 // Don't ask a Mach-O STAB symbol for its section unless you know that
2392 // STAB symbol's section field refers to a valid section index. Otherwise
2393 // the symbol may error trying to load a section that does not exist.
2394 bool IsSTAB = false;
2396 DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
2398 (MachO->is64Bit() ? MachO->getSymbol64TableEntry(SymDRI).n_type
2399 : MachO->getSymbolTableEntry(SymDRI).n_type);
2400 if (NType & MachO::N_STAB)
2403 section_iterator Section = IsSTAB
2405 : unwrapOrError(Symbol.getSection(), FileName,
2406 ArchiveName, ArchitectureName);
2409 if (Type == SymbolRef::ST_Debug && Section != O->section_end()) {
2410 if (Expected<StringRef> NameOrErr = Section->getName())
2413 consumeError(NameOrErr.takeError());
2416 Name = unwrapOrError(Symbol.getName(), FileName, ArchiveName,
2420 bool Global = Flags & SymbolRef::SF_Global;
2421 bool Weak = Flags & SymbolRef::SF_Weak;
2422 bool Absolute = Flags & SymbolRef::SF_Absolute;
2423 bool Common = Flags & SymbolRef::SF_Common;
2424 bool Hidden = Flags & SymbolRef::SF_Hidden;
2427 if ((Section != O->section_end() || Absolute) && !Weak)
2428 GlobLoc = Global ? 'g' : 'l';
2431 if (ELFSymbolRef(Symbol).getELFType() == ELF::STT_GNU_IFUNC)
2433 if (ELFSymbolRef(Symbol).getBinding() == ELF::STB_GNU_UNIQUE)
2440 else if (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
2443 char FileFunc = ' ';
2444 if (Type == SymbolRef::ST_File)
2446 else if (Type == SymbolRef::ST_Function)
2448 else if (Type == SymbolRef::ST_Data)
2451 const char *Fmt = O->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
2453 outs() << format(Fmt, Address) << " "
2454 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
2455 << (Weak ? 'w' : ' ') // Weak?
2456 << ' ' // Constructor. Not supported yet.
2457 << ' ' // Warning. Not supported yet.
2458 << IFunc // Indirect reference to another symbol.
2459 << Debug // Debugging (d) or dynamic (D) symbol.
2460 << FileFunc // Name of function (F), file (f) or object (O).
2464 } else if (Common) {
2466 } else if (Section == O->section_end()) {
2470 DataRefImpl DR = Section->getRawDataRefImpl();
2471 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
2472 outs() << SegmentName << ",";
2474 StringRef SectionName = unwrapOrError(Section->getName(), FileName);
2475 outs() << SectionName;
2478 if (Common || O->isELF()) {
2480 Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
2481 outs() << '\t' << format(Fmt, Val);
2485 uint8_t Other = ELFSymbolRef(Symbol).getOther();
2487 case ELF::STV_DEFAULT:
2489 case ELF::STV_INTERNAL:
2490 outs() << " .internal";
2492 case ELF::STV_HIDDEN:
2493 outs() << " .hidden";
2495 case ELF::STV_PROTECTED:
2496 outs() << " .protected";
2499 outs() << format(" 0x%02x", Other);
2502 } else if (Hidden) {
2503 outs() << " .hidden";
2507 outs() << ' ' << demangle(std::string(Name)) << '\n';
2509 outs() << ' ' << Name << '\n';
2512 static void printUnwindInfo(const ObjectFile *O) {
2513 outs() << "Unwind info:\n\n";
2515 if (const COFFObjectFile *Coff = dyn_cast<COFFObjectFile>(O))
2516 printCOFFUnwindInfo(Coff);
2517 else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O))
2518 printMachOUnwindInfo(MachO);
2520 // TODO: Extract DWARF dump tool to objdump.
2521 WithColor::error(errs(), ToolName)
2522 << "This operation is only currently supported "
2523 "for COFF and MachO object files.\n";
2526 /// Dump the raw contents of the __clangast section so the output can be piped
2527 /// into llvm-bcanalyzer.
2528 static void printRawClangAST(const ObjectFile *Obj) {
2529 if (outs().is_displayed()) {
2530 WithColor::error(errs(), ToolName)
2531 << "The -raw-clang-ast option will dump the raw binary contents of "
2532 "the clang ast section.\n"
2533 "Please redirect the output to a file or another program such as "
2534 "llvm-bcanalyzer.\n";
2538 StringRef ClangASTSectionName("__clangast");
2539 if (Obj->isCOFF()) {
2540 ClangASTSectionName = "clangast";
2543 Optional<object::SectionRef> ClangASTSection;
2544 for (auto Sec : ToolSectionFilter(*Obj)) {
2546 if (Expected<StringRef> NameOrErr = Sec.getName())
2549 consumeError(NameOrErr.takeError());
2551 if (Name == ClangASTSectionName) {
2552 ClangASTSection = Sec;
2556 if (!ClangASTSection)
2559 StringRef ClangASTContents = unwrapOrError(
2560 ClangASTSection.getValue().getContents(), Obj->getFileName());
2561 outs().write(ClangASTContents.data(), ClangASTContents.size());
2564 static void printFaultMaps(const ObjectFile *Obj) {
2565 StringRef FaultMapSectionName;
2568 FaultMapSectionName = ".llvm_faultmaps";
2569 } else if (Obj->isMachO()) {
2570 FaultMapSectionName = "__llvm_faultmaps";
2572 WithColor::error(errs(), ToolName)
2573 << "This operation is only currently supported "
2574 "for ELF and Mach-O executable files.\n";
2578 Optional<object::SectionRef> FaultMapSection;
2580 for (auto Sec : ToolSectionFilter(*Obj)) {
2582 if (Expected<StringRef> NameOrErr = Sec.getName())
2585 consumeError(NameOrErr.takeError());
2587 if (Name == FaultMapSectionName) {
2588 FaultMapSection = Sec;
2593 outs() << "FaultMap table:\n";
2595 if (!FaultMapSection.hasValue()) {
2596 outs() << "<not found>\n";
2600 StringRef FaultMapContents =
2601 unwrapOrError(FaultMapSection.getValue().getContents(), Obj->getFileName());
2602 FaultMapParser FMP(FaultMapContents.bytes_begin(),
2603 FaultMapContents.bytes_end());
2608 static void printPrivateFileHeaders(const ObjectFile *O, bool OnlyFirst) {
2610 printELFFileHeader(O);
2611 printELFDynamicSection(O);
2612 printELFSymbolVersionInfo(O);
2616 return printCOFFFileHeader(O);
2618 return printWasmFileHeader(O);
2620 printMachOFileHeader(O);
2622 printMachOLoadCommands(O);
2625 reportError(O->getFileName(), "Invalid/Unsupported object file format");
2628 static void printFileHeaders(const ObjectFile *O) {
2629 if (!O->isELF() && !O->isCOFF())
2630 reportError(O->getFileName(), "Invalid/Unsupported object file format");
2632 Triple::ArchType AT = O->getArch();
2633 outs() << "architecture: " << Triple::getArchTypeName(AT) << "\n";
2634 uint64_t Address = unwrapOrError(O->getStartAddress(), O->getFileName());
2636 StringRef Fmt = O->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
2637 outs() << "start address: "
2638 << "0x" << format(Fmt.data(), Address) << "\n\n";
2641 static void printArchiveChild(StringRef Filename, const Archive::Child &C) {
2642 Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
2644 WithColor::error(errs(), ToolName) << "ill-formed archive entry.\n";
2645 consumeError(ModeOrErr.takeError());
2648 sys::fs::perms Mode = ModeOrErr.get();
2649 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
2650 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
2651 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
2652 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
2653 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
2654 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
2655 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
2656 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
2657 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
2661 outs() << format("%d/%d %6" PRId64 " ", unwrapOrError(C.getUID(), Filename),
2662 unwrapOrError(C.getGID(), Filename),
2663 unwrapOrError(C.getRawSize(), Filename));
2665 StringRef RawLastModified = C.getRawLastModified();
2667 if (RawLastModified.getAsInteger(10, Seconds))
2668 outs() << "(date: \"" << RawLastModified
2669 << "\" contains non-decimal chars) ";
2671 // Since ctime(3) returns a 26 character string of the form:
2672 // "Sun Sep 16 01:03:52 1973\n\0"
2673 // just print 24 characters.
2675 outs() << format("%.24s ", ctime(&t));
2678 StringRef Name = "";
2679 Expected<StringRef> NameOrErr = C.getName();
2681 consumeError(NameOrErr.takeError());
2682 Name = unwrapOrError(C.getRawName(), Filename);
2684 Name = NameOrErr.get();
2686 outs() << Name << "\n";
2689 // For ELF only now.
2690 static bool shouldWarnForInvalidStartStopAddress(ObjectFile *Obj) {
2691 if (const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj)) {
2692 if (Elf->getEType() != ELF::ET_REL)
2698 static void checkForInvalidStartStopAddress(ObjectFile *Obj,
2699 uint64_t Start, uint64_t Stop) {
2700 if (!shouldWarnForInvalidStartStopAddress(Obj))
2703 for (const SectionRef &Section : Obj->sections())
2704 if (ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC) {
2705 uint64_t BaseAddr = Section.getAddress();
2706 uint64_t Size = Section.getSize();
2707 if ((Start < BaseAddr + Size) && Stop > BaseAddr)
2711 if (StartAddress.getNumOccurrences() == 0)
2712 reportWarning("no section has address less than 0x" +
2713 Twine::utohexstr(Stop) + " specified by --stop-address",
2714 Obj->getFileName());
2715 else if (StopAddress.getNumOccurrences() == 0)
2716 reportWarning("no section has address greater than or equal to 0x" +
2717 Twine::utohexstr(Start) + " specified by --start-address",
2718 Obj->getFileName());
2720 reportWarning("no section overlaps the range [0x" +
2721 Twine::utohexstr(Start) + ",0x" + Twine::utohexstr(Stop) +
2722 ") specified by --start-address/--stop-address",
2723 Obj->getFileName());
2726 static void dumpObject(ObjectFile *O, const Archive *A = nullptr,
2727 const Archive::Child *C = nullptr) {
2728 // Avoid other output when using a raw option.
2732 outs() << A->getFileName() << "(" << O->getFileName() << ")";
2734 outs() << O->getFileName();
2735 outs() << ":\tfile format " << O->getFileFormatName().lower() << "\n\n";
2738 if (StartAddress.getNumOccurrences() || StopAddress.getNumOccurrences())
2739 checkForInvalidStartStopAddress(O, StartAddress, StopAddress);
2741 // Note: the order here matches GNU objdump for compatability.
2742 StringRef ArchiveName = A ? A->getFileName() : "";
2743 if (ArchiveHeaders && !MachOOpt && C)
2744 printArchiveChild(ArchiveName, *C);
2746 printFileHeaders(O);
2747 if (PrivateHeaders || FirstPrivateHeader)
2748 printPrivateFileHeaders(O, FirstPrivateHeader);
2750 printSectionHeaders(O);
2752 printSymbolTable(O, ArchiveName);
2753 if (DynamicSymbolTable)
2754 printSymbolTable(O, ArchiveName, /*ArchitectureName=*/"",
2755 /*DumpDynamic=*/true);
2756 if (DwarfDumpType != DIDT_Null) {
2757 std::unique_ptr<DIContext> DICtx = DWARFContext::create(*O);
2758 // Dump the complete DWARF structure.
2759 DIDumpOptions DumpOpts;
2760 DumpOpts.DumpType = DwarfDumpType;
2761 DICtx->dump(outs(), DumpOpts);
2763 if (Relocations && !Disassemble)
2764 printRelocations(O);
2765 if (DynamicRelocations)
2766 printDynamicRelocations(O);
2767 if (SectionContents)
2768 printSectionContents(O);
2770 disassembleObject(O, Relocations);
2774 // Mach-O specific options:
2776 printExportsTrie(O);
2778 printRebaseTable(O);
2782 printLazyBindTable(O);
2784 printWeakBindTable(O);
2786 // Other special sections:
2788 printRawClangAST(O);
2789 if (FaultMapSection)
2793 static void dumpObject(const COFFImportFile *I, const Archive *A,
2794 const Archive::Child *C = nullptr) {
2795 StringRef ArchiveName = A ? A->getFileName() : "";
2797 // Avoid other output when using a raw option.
2800 << ArchiveName << "(" << I->getFileName() << ")"
2801 << ":\tfile format COFF-import-file"
2804 if (ArchiveHeaders && !MachOOpt && C)
2805 printArchiveChild(ArchiveName, *C);
2807 printCOFFSymbolTable(I);
2810 /// Dump each object file in \a a;
2811 static void dumpArchive(const Archive *A) {
2812 Error Err = Error::success();
2814 for (auto &C : A->children(Err)) {
2816 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2818 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2819 reportError(std::move(E), getFileNameForError(C, I), A->getFileName());
2822 if (ObjectFile *O = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
2823 dumpObject(O, A, &C);
2824 else if (COFFImportFile *I = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
2825 dumpObject(I, A, &C);
2827 reportError(errorCodeToError(object_error::invalid_file_type),
2831 reportError(std::move(Err), A->getFileName());
2834 /// Open file and figure out how to dump it.
2835 static void dumpInput(StringRef file) {
2836 // If we are using the Mach-O specific object file parser, then let it parse
2837 // the file and process the command line options. So the -arch flags can
2838 // be used to select specific slices, etc.
2840 parseInputMachO(file);
2844 // Attempt to open the binary.
2845 OwningBinary<Binary> OBinary = unwrapOrError(createBinary(file), file);
2846 Binary &Binary = *OBinary.getBinary();
2848 if (Archive *A = dyn_cast<Archive>(&Binary))
2850 else if (ObjectFile *O = dyn_cast<ObjectFile>(&Binary))
2852 else if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Binary))
2853 parseInputMachO(UB);
2855 reportError(errorCodeToError(object_error::invalid_file_type), file);
2858 int main(int argc, char **argv) {
2859 using namespace llvm;
2860 InitLLVM X(argc, argv);
2861 const cl::OptionCategory *OptionFilters[] = {&ObjdumpCat, &MachOCat};
2862 cl::HideUnrelatedOptions(OptionFilters);
2864 // Initialize targets and assembly printers/parsers.
2865 InitializeAllTargetInfos();
2866 InitializeAllTargetMCs();
2867 InitializeAllDisassemblers();
2869 // Register the target printer for --version.
2870 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
2872 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n", nullptr,
2874 /*LongOptionsUseDoubleDash=*/true);
2876 if (StartAddress >= StopAddress)
2877 reportCmdLineError("start address should be less than stop address");
2881 // Defaults to a.out if no filenames specified.
2882 if (InputFilenames.empty())
2883 InputFilenames.push_back("a.out");
2886 ArchiveHeaders = FileHeaders = PrivateHeaders = Relocations =
2887 SectionHeaders = SymbolTable = true;
2889 if (DisassembleAll || PrintSource || PrintLines ||
2890 !DisassembleSymbols.empty())
2893 if (!ArchiveHeaders && !Disassemble && DwarfDumpType == DIDT_Null &&
2894 !DynamicRelocations && !FileHeaders && !PrivateHeaders && !RawClangAST &&
2895 !Relocations && !SectionHeaders && !SectionContents && !SymbolTable &&
2896 !DynamicSymbolTable && !UnwindInfo && !FaultMapSection &&
2898 (Bind || DataInCode || DylibId || DylibsUsed || ExportsTrie ||
2899 FirstPrivateHeader || IndirectSymbols || InfoPlist || LazyBind ||
2900 LinkOptHints || ObjcMetaData || Rebase || UniversalHeaders ||
2901 WeakBind || !FilterSections.empty()))) {
2902 cl::PrintHelpMessage();
2906 DisasmSymbolSet.insert(DisassembleSymbols.begin(), DisassembleSymbols.end());
2908 llvm::for_each(InputFilenames, dumpInput);
2910 warnOnNoMatchForSections();
2912 return EXIT_SUCCESS;