1 //===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This program is a utility that works like binutils "objdump", that is, it 10 // dumps out a plethora of information about an object file depending on the 11 // flags. 12 // 13 // The flags and output of this program should be near identical to those of 14 // binutils objdump. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "llvm-objdump.h" 19 #include "llvm/ADT/Optional.h" 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/ADT/StringExtras.h" 22 #include "llvm/ADT/StringSet.h" 23 #include "llvm/ADT/Triple.h" 24 #include "llvm/CodeGen/FaultMaps.h" 25 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 26 #include "llvm/DebugInfo/Symbolize/Symbolize.h" 27 #include "llvm/Demangle/Demangle.h" 28 #include "llvm/MC/MCAsmInfo.h" 29 #include "llvm/MC/MCContext.h" 30 #include "llvm/MC/MCDisassembler/MCDisassembler.h" 31 #include "llvm/MC/MCDisassembler/MCRelocationInfo.h" 32 #include "llvm/MC/MCInst.h" 33 #include "llvm/MC/MCInstPrinter.h" 34 #include "llvm/MC/MCInstrAnalysis.h" 35 #include "llvm/MC/MCInstrInfo.h" 36 #include "llvm/MC/MCObjectFileInfo.h" 37 #include "llvm/MC/MCRegisterInfo.h" 38 #include "llvm/MC/MCSubtargetInfo.h" 39 #include "llvm/Object/Archive.h" 40 #include "llvm/Object/COFF.h" 41 #include "llvm/Object/COFFImportFile.h" 42 #include "llvm/Object/ELFObjectFile.h" 43 #include "llvm/Object/MachO.h" 44 #include "llvm/Object/MachOUniversal.h" 45 #include "llvm/Object/ObjectFile.h" 46 #include "llvm/Object/Wasm.h" 47 #include "llvm/Support/Casting.h" 48 #include "llvm/Support/CommandLine.h" 49 #include "llvm/Support/Debug.h" 50 #include "llvm/Support/Errc.h" 51 #include "llvm/Support/FileSystem.h" 52 #include "llvm/Support/Format.h" 53 #include "llvm/Support/GraphWriter.h" 54 #include "llvm/Support/Host.h" 55 #include "llvm/Support/InitLLVM.h" 56 #include "llvm/Support/MemoryBuffer.h" 57 #include "llvm/Support/SourceMgr.h" 58 #include "llvm/Support/StringSaver.h" 59 #include "llvm/Support/TargetRegistry.h" 60 #include "llvm/Support/TargetSelect.h" 61 #include "llvm/Support/WithColor.h" 62 #include "llvm/Support/raw_ostream.h" 63 #include <algorithm> 64 #include <cctype> 65 #include <cstring> 66 #include <system_error> 67 #include <unordered_map> 68 #include <utility> 69 70 using namespace llvm; 71 using namespace object; 72 73 cl::opt<unsigned long long> AdjustVMA( 74 "adjust-vma", 75 cl::desc("Increase the displayed address by the specified offset"), 76 cl::value_desc("offset"), cl::init(0)); 77 78 cl::opt<bool> 79 llvm::AllHeaders("all-headers", 80 cl::desc("Display all available header information")); 81 static cl::alias AllHeadersShort("x", cl::desc("Alias for --all-headers"), 82 cl::NotHidden, cl::Grouping, 83 cl::aliasopt(AllHeaders)); 84 85 static cl::list<std::string> 86 InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore); 87 88 cl::opt<bool> 89 llvm::Disassemble("disassemble", 90 cl::desc("Display assembler mnemonics for the machine instructions")); 91 static cl::alias Disassembled("d", cl::desc("Alias for --disassemble"), 92 cl::NotHidden, cl::Grouping, 93 cl::aliasopt(Disassemble)); 94 95 cl::opt<bool> 96 llvm::DisassembleAll("disassemble-all", 97 cl::desc("Display assembler mnemonics for the machine instructions")); 98 static cl::alias DisassembleAlld("D", cl::desc("Alias for --disassemble-all"), 99 cl::NotHidden, cl::Grouping, 100 cl::aliasopt(DisassembleAll)); 101 102 cl::opt<bool> llvm::Demangle("demangle", cl::desc("Demangle symbols names"), 103 cl::init(false)); 104 105 static cl::alias DemangleShort("C", cl::desc("Alias for --demangle"), 106 cl::NotHidden, cl::Grouping, 107 cl::aliasopt(llvm::Demangle)); 108 109 static cl::list<std::string> 110 DisassembleFunctions("disassemble-functions", 111 cl::CommaSeparated, 112 cl::desc("List of functions to disassemble")); 113 static StringSet<> DisasmFuncsSet; 114 115 cl::opt<bool> 116 llvm::Relocations("reloc", 117 cl::desc("Display the relocation entries in the file")); 118 static cl::alias RelocationsShort("r", cl::desc("Alias for --reloc"), 119 cl::NotHidden, cl::Grouping, 120 cl::aliasopt(llvm::Relocations)); 121 122 cl::opt<bool> 123 llvm::DynamicRelocations("dynamic-reloc", 124 cl::desc("Display the dynamic relocation entries in the file")); 125 static cl::alias DynamicRelocationsd("R", cl::desc("Alias for --dynamic-reloc"), 126 cl::NotHidden, cl::Grouping, 127 cl::aliasopt(DynamicRelocations)); 128 129 cl::opt<bool> 130 llvm::SectionContents("full-contents", 131 cl::desc("Display the content of each section")); 132 static cl::alias SectionContentsShort("s", 133 cl::desc("Alias for --full-contents"), 134 cl::NotHidden, cl::Grouping, 135 cl::aliasopt(SectionContents)); 136 137 cl::opt<bool> llvm::SymbolTable("syms", cl::desc("Display the symbol table")); 138 static cl::alias SymbolTableShort("t", cl::desc("Alias for --syms"), 139 cl::NotHidden, cl::Grouping, 140 cl::aliasopt(llvm::SymbolTable)); 141 142 cl::opt<bool> 143 llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols")); 144 145 cl::opt<bool> 146 llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info")); 147 148 cl::opt<bool> 149 llvm::Bind("bind", cl::desc("Display mach-o binding info")); 150 151 cl::opt<bool> 152 llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info")); 153 154 cl::opt<bool> 155 llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info")); 156 157 cl::opt<bool> 158 llvm::RawClangAST("raw-clang-ast", 159 cl::desc("Dump the raw binary contents of the clang AST section")); 160 161 static cl::opt<bool> 162 MachOOpt("macho", cl::desc("Use MachO specific object file parser")); 163 static cl::alias MachOm("m", cl::desc("Alias for --macho"), cl::NotHidden, 164 cl::Grouping, cl::aliasopt(MachOOpt)); 165 166 cl::opt<std::string> 167 llvm::TripleName("triple", cl::desc("Target triple to disassemble for, " 168 "see -version for available targets")); 169 170 cl::opt<std::string> 171 llvm::MCPU("mcpu", 172 cl::desc("Target a specific cpu type (-mcpu=help for details)"), 173 cl::value_desc("cpu-name"), 174 cl::init("")); 175 176 cl::opt<std::string> 177 llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, " 178 "see -version for available targets")); 179 180 cl::opt<bool> 181 llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the " 182 "headers for each section.")); 183 static cl::alias SectionHeadersShort("headers", 184 cl::desc("Alias for --section-headers"), 185 cl::NotHidden, 186 cl::aliasopt(SectionHeaders)); 187 static cl::alias SectionHeadersShorter("h", 188 cl::desc("Alias for --section-headers"), 189 cl::NotHidden, cl::Grouping, 190 cl::aliasopt(SectionHeaders)); 191 192 static cl::opt<bool> 193 ShowLMA("show-lma", 194 cl::desc("Display LMA column when dumping ELF section headers")); 195 196 cl::list<std::string> 197 llvm::FilterSections("section", cl::desc("Operate on the specified sections only. " 198 "With -macho dump segment,section")); 199 cl::alias static FilterSectionsj("j", cl::desc("Alias for --section"), 200 cl::NotHidden, 201 cl::aliasopt(llvm::FilterSections)); 202 203 cl::list<std::string> 204 llvm::MAttrs("mattr", 205 cl::CommaSeparated, 206 cl::desc("Target specific attributes"), 207 cl::value_desc("a1,+a2,-a3,...")); 208 209 cl::opt<bool> 210 llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling " 211 "instructions, do not print " 212 "the instruction bytes.")); 213 cl::opt<bool> 214 llvm::NoLeadingAddr("no-leading-addr", cl::desc("Print no leading address")); 215 216 cl::opt<bool> 217 llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information")); 218 219 static cl::alias UnwindInfoShort("u", cl::desc("Alias for --unwind-info"), 220 cl::NotHidden, cl::Grouping, 221 cl::aliasopt(UnwindInfo)); 222 223 cl::opt<bool> 224 llvm::PrivateHeaders("private-headers", 225 cl::desc("Display format specific file headers")); 226 227 cl::opt<bool> 228 llvm::FirstPrivateHeader("private-header", 229 cl::desc("Display only the first format specific file " 230 "header")); 231 232 static cl::alias PrivateHeadersShort("p", 233 cl::desc("Alias for --private-headers"), 234 cl::NotHidden, cl::Grouping, 235 cl::aliasopt(PrivateHeaders)); 236 237 cl::opt<bool> llvm::FileHeaders( 238 "file-headers", 239 cl::desc("Display the contents of the overall file header")); 240 241 static cl::alias FileHeadersShort("f", cl::desc("Alias for --file-headers"), 242 cl::NotHidden, cl::Grouping, 243 cl::aliasopt(FileHeaders)); 244 245 cl::opt<bool> 246 llvm::ArchiveHeaders("archive-headers", 247 cl::desc("Display archive header information")); 248 249 cl::alias ArchiveHeadersShort("a", cl::desc("Alias for --archive-headers"), 250 cl::NotHidden, cl::Grouping, 251 cl::aliasopt(ArchiveHeaders)); 252 253 cl::opt<bool> 254 llvm::PrintImmHex("print-imm-hex", 255 cl::desc("Use hex format for immediate values")); 256 257 cl::opt<bool> PrintFaultMaps("fault-map-section", 258 cl::desc("Display contents of faultmap section")); 259 260 cl::opt<DIDumpType> llvm::DwarfDumpType( 261 "dwarf", cl::init(DIDT_Null), cl::desc("Dump of dwarf debug sections:"), 262 cl::values(clEnumValN(DIDT_DebugFrame, "frames", ".debug_frame"))); 263 264 cl::opt<bool> PrintSource( 265 "source", 266 cl::desc( 267 "Display source inlined with disassembly. Implies disassemble object")); 268 269 cl::alias PrintSourceShort("S", cl::desc("Alias for -source"), cl::NotHidden, 270 cl::Grouping, cl::aliasopt(PrintSource)); 271 272 cl::opt<bool> PrintLines("line-numbers", 273 cl::desc("Display source line numbers with " 274 "disassembly. Implies disassemble object")); 275 276 cl::alias PrintLinesShort("l", cl::desc("Alias for -line-numbers"), 277 cl::NotHidden, cl::Grouping, 278 cl::aliasopt(PrintLines)); 279 280 cl::opt<unsigned long long> 281 StartAddress("start-address", cl::desc("Disassemble beginning at address"), 282 cl::value_desc("address"), cl::init(0)); 283 cl::opt<unsigned long long> 284 StopAddress("stop-address", 285 cl::desc("Stop disassembly at address"), 286 cl::value_desc("address"), cl::init(UINT64_MAX)); 287 288 cl::opt<bool> DisassembleZeroes( 289 "disassemble-zeroes", 290 cl::desc("Do not skip blocks of zeroes when disassembling")); 291 cl::alias DisassembleZeroesShort("z", 292 cl::desc("Alias for --disassemble-zeroes"), 293 cl::NotHidden, cl::Grouping, 294 cl::aliasopt(DisassembleZeroes)); 295 296 static cl::list<std::string> 297 DisassemblerOptions("disassembler-options", 298 cl::desc("Pass target specific disassembler options"), 299 cl::value_desc("options"), cl::CommaSeparated); 300 static cl::alias 301 DisassemblerOptionsShort("M", cl::desc("Alias for --disassembler-options"), 302 cl::NotHidden, cl::Prefix, cl::CommaSeparated, 303 cl::aliasopt(DisassemblerOptions)); 304 305 static StringRef ToolName; 306 307 typedef std::vector<std::tuple<uint64_t, StringRef, uint8_t>> SectionSymbolsTy; 308 309 SectionFilter llvm::ToolSectionFilter(llvm::object::ObjectFile const &O) { 310 return SectionFilter( 311 [](llvm::object::SectionRef const &S) { 312 if (FilterSections.empty()) 313 return true; 314 llvm::StringRef String; 315 std::error_code error = S.getName(String); 316 if (error) 317 return false; 318 return is_contained(FilterSections, String); 319 }, 320 O); 321 } 322 323 void llvm::error(std::error_code EC) { 324 if (!EC) 325 return; 326 WithColor::error(errs(), ToolName) 327 << "reading file: " << EC.message() << ".\n"; 328 errs().flush(); 329 exit(1); 330 } 331 332 LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) { 333 WithColor::error(errs(), ToolName) << Message << ".\n"; 334 errs().flush(); 335 exit(1); 336 } 337 338 void llvm::warn(StringRef Message) { 339 WithColor::warning(errs(), ToolName) << Message << ".\n"; 340 errs().flush(); 341 } 342 343 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, 344 Twine Message) { 345 WithColor::error(errs(), ToolName) 346 << "'" << File << "': " << Message << ".\n"; 347 exit(1); 348 } 349 350 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, 351 std::error_code EC) { 352 assert(EC); 353 WithColor::error(errs(), ToolName) 354 << "'" << File << "': " << EC.message() << ".\n"; 355 exit(1); 356 } 357 358 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, 359 llvm::Error E) { 360 assert(E); 361 std::string Buf; 362 raw_string_ostream OS(Buf); 363 logAllUnhandledErrors(std::move(E), OS); 364 OS.flush(); 365 WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf; 366 exit(1); 367 } 368 369 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName, 370 StringRef FileName, 371 llvm::Error E, 372 StringRef ArchitectureName) { 373 assert(E); 374 WithColor::error(errs(), ToolName); 375 if (ArchiveName != "") 376 errs() << ArchiveName << "(" << FileName << ")"; 377 else 378 errs() << "'" << FileName << "'"; 379 if (!ArchitectureName.empty()) 380 errs() << " (for architecture " << ArchitectureName << ")"; 381 std::string Buf; 382 raw_string_ostream OS(Buf); 383 logAllUnhandledErrors(std::move(E), OS); 384 OS.flush(); 385 errs() << ": " << Buf; 386 exit(1); 387 } 388 389 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName, 390 const object::Archive::Child &C, 391 llvm::Error E, 392 StringRef ArchitectureName) { 393 Expected<StringRef> NameOrErr = C.getName(); 394 // TODO: if we have a error getting the name then it would be nice to print 395 // the index of which archive member this is and or its offset in the 396 // archive instead of "???" as the name. 397 if (!NameOrErr) { 398 consumeError(NameOrErr.takeError()); 399 llvm::report_error(ArchiveName, "???", std::move(E), ArchitectureName); 400 } else 401 llvm::report_error(ArchiveName, NameOrErr.get(), std::move(E), 402 ArchitectureName); 403 } 404 405 static const Target *getTarget(const ObjectFile *Obj = nullptr) { 406 // Figure out the target triple. 407 llvm::Triple TheTriple("unknown-unknown-unknown"); 408 if (TripleName.empty()) { 409 if (Obj) 410 TheTriple = Obj->makeTriple(); 411 } else { 412 TheTriple.setTriple(Triple::normalize(TripleName)); 413 414 // Use the triple, but also try to combine with ARM build attributes. 415 if (Obj) { 416 auto Arch = Obj->getArch(); 417 if (Arch == Triple::arm || Arch == Triple::armeb) 418 Obj->setARMSubArch(TheTriple); 419 } 420 } 421 422 // Get the target specific parser. 423 std::string Error; 424 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, 425 Error); 426 if (!TheTarget) { 427 if (Obj) 428 report_error(Obj->getFileName(), "can't find target: " + Error); 429 else 430 error("can't find target: " + Error); 431 } 432 433 // Update the triple name and return the found target. 434 TripleName = TheTriple.getTriple(); 435 return TheTarget; 436 } 437 438 bool llvm::isRelocAddressLess(RelocationRef A, RelocationRef B) { 439 return A.getOffset() < B.getOffset(); 440 } 441 442 static std::error_code getRelocationValueString(const RelocationRef &Rel, 443 SmallVectorImpl<char> &Result) { 444 const ObjectFile *Obj = Rel.getObject(); 445 if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj)) 446 return getELFRelocationValueString(ELF, Rel, Result); 447 if (auto *COFF = dyn_cast<COFFObjectFile>(Obj)) 448 return getCOFFRelocationValueString(COFF, Rel, Result); 449 if (auto *Wasm = dyn_cast<WasmObjectFile>(Obj)) 450 return getWasmRelocationValueString(Wasm, Rel, Result); 451 if (auto *MachO = dyn_cast<MachOObjectFile>(Obj)) 452 return getMachORelocationValueString(MachO, Rel, Result); 453 llvm_unreachable("unknown object file format"); 454 } 455 456 /// Indicates whether this relocation should hidden when listing 457 /// relocations, usually because it is the trailing part of a multipart 458 /// relocation that will be printed as part of the leading relocation. 459 static bool getHidden(RelocationRef RelRef) { 460 auto *MachO = dyn_cast<MachOObjectFile>(RelRef.getObject()); 461 if (!MachO) 462 return false; 463 464 unsigned Arch = MachO->getArch(); 465 DataRefImpl Rel = RelRef.getRawDataRefImpl(); 466 uint64_t Type = MachO->getRelocationType(Rel); 467 468 // On arches that use the generic relocations, GENERIC_RELOC_PAIR 469 // is always hidden. 470 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) 471 return Type == MachO::GENERIC_RELOC_PAIR; 472 473 if (Arch == Triple::x86_64) { 474 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows 475 // an X86_64_RELOC_SUBTRACTOR. 476 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) { 477 DataRefImpl RelPrev = Rel; 478 RelPrev.d.a--; 479 uint64_t PrevType = MachO->getRelocationType(RelPrev); 480 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR) 481 return true; 482 } 483 } 484 485 return false; 486 } 487 488 namespace { 489 class SourcePrinter { 490 protected: 491 DILineInfo OldLineInfo; 492 const ObjectFile *Obj = nullptr; 493 std::unique_ptr<symbolize::LLVMSymbolizer> Symbolizer; 494 // File name to file contents of source 495 std::unordered_map<std::string, std::unique_ptr<MemoryBuffer>> SourceCache; 496 // Mark the line endings of the cached source 497 std::unordered_map<std::string, std::vector<StringRef>> LineCache; 498 499 private: 500 bool cacheSource(const DILineInfo& LineInfoFile); 501 502 public: 503 SourcePrinter() = default; 504 SourcePrinter(const ObjectFile *Obj, StringRef DefaultArch) : Obj(Obj) { 505 symbolize::LLVMSymbolizer::Options SymbolizerOpts( 506 DILineInfoSpecifier::FunctionNameKind::None, true, false, false, 507 DefaultArch); 508 Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts)); 509 } 510 virtual ~SourcePrinter() = default; 511 virtual void printSourceLine(raw_ostream &OS, uint64_t Address, 512 StringRef Delimiter = "; "); 513 }; 514 515 bool SourcePrinter::cacheSource(const DILineInfo &LineInfo) { 516 std::unique_ptr<MemoryBuffer> Buffer; 517 if (LineInfo.Source) { 518 Buffer = MemoryBuffer::getMemBuffer(*LineInfo.Source); 519 } else { 520 auto BufferOrError = MemoryBuffer::getFile(LineInfo.FileName); 521 if (!BufferOrError) 522 return false; 523 Buffer = std::move(*BufferOrError); 524 } 525 // Chomp the file to get lines 526 size_t BufferSize = Buffer->getBufferSize(); 527 const char *BufferStart = Buffer->getBufferStart(); 528 for (const char *Start = BufferStart, *End = BufferStart; 529 End < BufferStart + BufferSize; End++) 530 if (*End == '\n' || End == BufferStart + BufferSize - 1 || 531 (*End == '\r' && *(End + 1) == '\n')) { 532 LineCache[LineInfo.FileName].push_back(StringRef(Start, End - Start)); 533 if (*End == '\r') 534 End++; 535 Start = End + 1; 536 } 537 SourceCache[LineInfo.FileName] = std::move(Buffer); 538 return true; 539 } 540 541 void SourcePrinter::printSourceLine(raw_ostream &OS, uint64_t Address, 542 StringRef Delimiter) { 543 if (!Symbolizer) 544 return; 545 DILineInfo LineInfo = DILineInfo(); 546 auto ExpectecLineInfo = 547 Symbolizer->symbolizeCode(Obj->getFileName(), Address); 548 if (!ExpectecLineInfo) 549 consumeError(ExpectecLineInfo.takeError()); 550 else 551 LineInfo = *ExpectecLineInfo; 552 553 if ((LineInfo.FileName == "<invalid>") || OldLineInfo.Line == LineInfo.Line || 554 LineInfo.Line == 0) 555 return; 556 557 if (PrintLines) 558 OS << Delimiter << LineInfo.FileName << ":" << LineInfo.Line << "\n"; 559 if (PrintSource) { 560 if (SourceCache.find(LineInfo.FileName) == SourceCache.end()) 561 if (!cacheSource(LineInfo)) 562 return; 563 auto FileBuffer = SourceCache.find(LineInfo.FileName); 564 if (FileBuffer != SourceCache.end()) { 565 auto LineBuffer = LineCache.find(LineInfo.FileName); 566 if (LineBuffer != LineCache.end()) { 567 if (LineInfo.Line > LineBuffer->second.size()) 568 return; 569 // Vector begins at 0, line numbers are non-zero 570 OS << Delimiter << LineBuffer->second[LineInfo.Line - 1].ltrim() 571 << "\n"; 572 } 573 } 574 } 575 OldLineInfo = LineInfo; 576 } 577 578 static bool isArmElf(const ObjectFile *Obj) { 579 return (Obj->isELF() && 580 (Obj->getArch() == Triple::aarch64 || 581 Obj->getArch() == Triple::aarch64_be || 582 Obj->getArch() == Triple::arm || Obj->getArch() == Triple::armeb || 583 Obj->getArch() == Triple::thumb || 584 Obj->getArch() == Triple::thumbeb)); 585 } 586 587 static void printRelocation(const RelocationRef &Rel, uint64_t Address, 588 uint8_t AddrSize) { 589 StringRef Fmt = 590 AddrSize > 4 ? "\t\t%016" PRIx64 ": " : "\t\t\t%08" PRIx64 ": "; 591 SmallString<16> Name; 592 SmallString<32> Val; 593 Rel.getTypeName(Name); 594 error(getRelocationValueString(Rel, Val)); 595 outs() << format(Fmt.data(), Address) << Name << "\t" << Val << "\n"; 596 } 597 598 class PrettyPrinter { 599 public: 600 virtual ~PrettyPrinter() = default; 601 virtual void printInst(MCInstPrinter &IP, const MCInst *MI, 602 ArrayRef<uint8_t> Bytes, uint64_t Address, 603 raw_ostream &OS, StringRef Annot, 604 MCSubtargetInfo const &STI, SourcePrinter *SP, 605 std::vector<RelocationRef> *Rels = nullptr) { 606 if (SP && (PrintSource || PrintLines)) 607 SP->printSourceLine(OS, Address); 608 if (!NoLeadingAddr) 609 OS << format("%8" PRIx64 ":", Address); 610 if (!NoShowRawInsn) { 611 OS << "\t"; 612 dumpBytes(Bytes, OS); 613 } 614 if (MI) 615 IP.printInst(MI, OS, "", STI); 616 else 617 OS << " <unknown>"; 618 } 619 }; 620 PrettyPrinter PrettyPrinterInst; 621 class HexagonPrettyPrinter : public PrettyPrinter { 622 public: 623 void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address, 624 raw_ostream &OS) { 625 uint32_t opcode = 626 (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0]; 627 if (!NoLeadingAddr) 628 OS << format("%8" PRIx64 ":", Address); 629 if (!NoShowRawInsn) { 630 OS << "\t"; 631 dumpBytes(Bytes.slice(0, 4), OS); 632 OS << format("%08" PRIx32, opcode); 633 } 634 } 635 void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes, 636 uint64_t Address, raw_ostream &OS, StringRef Annot, 637 MCSubtargetInfo const &STI, SourcePrinter *SP, 638 std::vector<RelocationRef> *Rels) override { 639 if (SP && (PrintSource || PrintLines)) 640 SP->printSourceLine(OS, Address, ""); 641 if (!MI) { 642 printLead(Bytes, Address, OS); 643 OS << " <unknown>"; 644 return; 645 } 646 std::string Buffer; 647 { 648 raw_string_ostream TempStream(Buffer); 649 IP.printInst(MI, TempStream, "", STI); 650 } 651 StringRef Contents(Buffer); 652 // Split off bundle attributes 653 auto PacketBundle = Contents.rsplit('\n'); 654 // Split off first instruction from the rest 655 auto HeadTail = PacketBundle.first.split('\n'); 656 auto Preamble = " { "; 657 auto Separator = ""; 658 659 // Hexagon's packets require relocations to be inline rather than 660 // clustered at the end of the packet. 661 std::vector<RelocationRef>::const_iterator RelCur = Rels->begin(); 662 std::vector<RelocationRef>::const_iterator RelEnd = Rels->end(); 663 auto PrintReloc = [&]() -> void { 664 while ((RelCur != RelEnd) && (RelCur->getOffset() <= Address)) { 665 if (RelCur->getOffset() == Address) { 666 printRelocation(*RelCur, Address, 4); 667 return; 668 } 669 ++RelCur; 670 } 671 }; 672 673 while (!HeadTail.first.empty()) { 674 OS << Separator; 675 Separator = "\n"; 676 if (SP && (PrintSource || PrintLines)) 677 SP->printSourceLine(OS, Address, ""); 678 printLead(Bytes, Address, OS); 679 OS << Preamble; 680 Preamble = " "; 681 StringRef Inst; 682 auto Duplex = HeadTail.first.split('\v'); 683 if (!Duplex.second.empty()) { 684 OS << Duplex.first; 685 OS << "; "; 686 Inst = Duplex.second; 687 } 688 else 689 Inst = HeadTail.first; 690 OS << Inst; 691 HeadTail = HeadTail.second.split('\n'); 692 if (HeadTail.first.empty()) 693 OS << " } " << PacketBundle.second; 694 PrintReloc(); 695 Bytes = Bytes.slice(4); 696 Address += 4; 697 } 698 } 699 }; 700 HexagonPrettyPrinter HexagonPrettyPrinterInst; 701 702 class AMDGCNPrettyPrinter : public PrettyPrinter { 703 public: 704 void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes, 705 uint64_t Address, raw_ostream &OS, StringRef Annot, 706 MCSubtargetInfo const &STI, SourcePrinter *SP, 707 std::vector<RelocationRef> *Rels) override { 708 if (SP && (PrintSource || PrintLines)) 709 SP->printSourceLine(OS, Address); 710 711 typedef support::ulittle32_t U32; 712 713 if (MI) { 714 SmallString<40> InstStr; 715 raw_svector_ostream IS(InstStr); 716 717 IP.printInst(MI, IS, "", STI); 718 719 OS << left_justify(IS.str(), 60); 720 } else { 721 // an unrecognized encoding - this is probably data so represent it 722 // using the .long directive, or .byte directive if fewer than 4 bytes 723 // remaining 724 if (Bytes.size() >= 4) { 725 OS << format("\t.long 0x%08" PRIx32 " ", 726 static_cast<uint32_t>(*reinterpret_cast<const U32*>(Bytes.data()))); 727 OS.indent(42); 728 } else { 729 OS << format("\t.byte 0x%02" PRIx8, Bytes[0]); 730 for (unsigned int i = 1; i < Bytes.size(); i++) 731 OS << format(", 0x%02" PRIx8, Bytes[i]); 732 OS.indent(55 - (6 * Bytes.size())); 733 } 734 } 735 736 OS << format("// %012" PRIX64 ": ", Address); 737 if (Bytes.size() >=4) { 738 for (auto D : makeArrayRef(reinterpret_cast<const U32*>(Bytes.data()), 739 Bytes.size() / sizeof(U32))) 740 // D should be explicitly casted to uint32_t here as it is passed 741 // by format to snprintf as vararg. 742 OS << format("%08" PRIX32 " ", static_cast<uint32_t>(D)); 743 } else { 744 for (unsigned int i = 0; i < Bytes.size(); i++) 745 OS << format("%02" PRIX8 " ", Bytes[i]); 746 } 747 748 if (!Annot.empty()) 749 OS << "// " << Annot; 750 } 751 }; 752 AMDGCNPrettyPrinter AMDGCNPrettyPrinterInst; 753 754 class BPFPrettyPrinter : public PrettyPrinter { 755 public: 756 void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes, 757 uint64_t Address, raw_ostream &OS, StringRef Annot, 758 MCSubtargetInfo const &STI, SourcePrinter *SP, 759 std::vector<RelocationRef> *Rels) override { 760 if (SP && (PrintSource || PrintLines)) 761 SP->printSourceLine(OS, Address); 762 if (!NoLeadingAddr) 763 OS << format("%8" PRId64 ":", Address / 8); 764 if (!NoShowRawInsn) { 765 OS << "\t"; 766 dumpBytes(Bytes, OS); 767 } 768 if (MI) 769 IP.printInst(MI, OS, "", STI); 770 else 771 OS << " <unknown>"; 772 } 773 }; 774 BPFPrettyPrinter BPFPrettyPrinterInst; 775 776 PrettyPrinter &selectPrettyPrinter(Triple const &Triple) { 777 switch(Triple.getArch()) { 778 default: 779 return PrettyPrinterInst; 780 case Triple::hexagon: 781 return HexagonPrettyPrinterInst; 782 case Triple::amdgcn: 783 return AMDGCNPrettyPrinterInst; 784 case Triple::bpfel: 785 case Triple::bpfeb: 786 return BPFPrettyPrinterInst; 787 } 788 } 789 } 790 791 static uint8_t getElfSymbolType(const ObjectFile *Obj, const SymbolRef &Sym) { 792 assert(Obj->isELF()); 793 if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj)) 794 return Elf32LEObj->getSymbol(Sym.getRawDataRefImpl())->getType(); 795 if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj)) 796 return Elf64LEObj->getSymbol(Sym.getRawDataRefImpl())->getType(); 797 if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj)) 798 return Elf32BEObj->getSymbol(Sym.getRawDataRefImpl())->getType(); 799 if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj)) 800 return Elf64BEObj->getSymbol(Sym.getRawDataRefImpl())->getType(); 801 llvm_unreachable("Unsupported binary format"); 802 } 803 804 template <class ELFT> static void 805 addDynamicElfSymbols(const ELFObjectFile<ELFT> *Obj, 806 std::map<SectionRef, SectionSymbolsTy> &AllSymbols) { 807 for (auto Symbol : Obj->getDynamicSymbolIterators()) { 808 uint8_t SymbolType = Symbol.getELFType(); 809 if (SymbolType != ELF::STT_FUNC || Symbol.getSize() == 0) 810 continue; 811 812 Expected<uint64_t> AddressOrErr = Symbol.getAddress(); 813 if (!AddressOrErr) 814 report_error(Obj->getFileName(), AddressOrErr.takeError()); 815 816 Expected<StringRef> Name = Symbol.getName(); 817 if (!Name) 818 report_error(Obj->getFileName(), Name.takeError()); 819 if (Name->empty()) 820 continue; 821 822 Expected<section_iterator> SectionOrErr = Symbol.getSection(); 823 if (!SectionOrErr) 824 report_error(Obj->getFileName(), SectionOrErr.takeError()); 825 section_iterator SecI = *SectionOrErr; 826 if (SecI == Obj->section_end()) 827 continue; 828 829 AllSymbols[*SecI].emplace_back(*AddressOrErr, *Name, SymbolType); 830 } 831 } 832 833 static void 834 addDynamicElfSymbols(const ObjectFile *Obj, 835 std::map<SectionRef, SectionSymbolsTy> &AllSymbols) { 836 assert(Obj->isELF()); 837 if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj)) 838 addDynamicElfSymbols(Elf32LEObj, AllSymbols); 839 else if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj)) 840 addDynamicElfSymbols(Elf64LEObj, AllSymbols); 841 else if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj)) 842 addDynamicElfSymbols(Elf32BEObj, AllSymbols); 843 else if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj)) 844 addDynamicElfSymbols(Elf64BEObj, AllSymbols); 845 else 846 llvm_unreachable("Unsupported binary format"); 847 } 848 849 static void addPltEntries(const ObjectFile *Obj, 850 std::map<SectionRef, SectionSymbolsTy> &AllSymbols, 851 StringSaver &Saver) { 852 Optional<SectionRef> Plt = None; 853 for (const SectionRef &Section : Obj->sections()) { 854 StringRef Name; 855 if (Section.getName(Name)) 856 continue; 857 if (Name == ".plt") 858 Plt = Section; 859 } 860 if (!Plt) 861 return; 862 if (auto *ElfObj = dyn_cast<ELFObjectFileBase>(Obj)) { 863 for (auto PltEntry : ElfObj->getPltAddresses()) { 864 SymbolRef Symbol(PltEntry.first, ElfObj); 865 uint8_t SymbolType = getElfSymbolType(Obj, Symbol); 866 867 Expected<StringRef> NameOrErr = Symbol.getName(); 868 if (!NameOrErr) 869 report_error(Obj->getFileName(), NameOrErr.takeError()); 870 if (NameOrErr->empty()) 871 continue; 872 StringRef Name = Saver.save((*NameOrErr + "@plt").str()); 873 874 AllSymbols[*Plt].emplace_back(PltEntry.second, Name, SymbolType); 875 } 876 } 877 } 878 879 // Normally the disassembly output will skip blocks of zeroes. This function 880 // returns the number of zero bytes that can be skipped when dumping the 881 // disassembly of the instructions in Buf. 882 static size_t countSkippableZeroBytes(ArrayRef<uint8_t> Buf) { 883 // Find the number of leading zeroes. 884 size_t N = 0; 885 while (N < Buf.size() && !Buf[N]) 886 ++N; 887 888 // We may want to skip blocks of zero bytes, but unless we see 889 // at least 8 of them in a row. 890 if (N < 8) 891 return 0; 892 893 // We skip zeroes in multiples of 4 because do not want to truncate an 894 // instruction if it starts with a zero byte. 895 return N & ~0x3; 896 } 897 898 // Returns a map from sections to their relocations. 899 static std::map<SectionRef, std::vector<RelocationRef>> 900 getRelocsMap(llvm::object::ObjectFile const &Obj) { 901 std::map<SectionRef, std::vector<RelocationRef>> Ret; 902 for (const SectionRef &Section : ToolSectionFilter(Obj)) { 903 section_iterator RelSec = Section.getRelocatedSection(); 904 if (RelSec == Obj.section_end()) 905 continue; 906 std::vector<RelocationRef> &V = Ret[*RelSec]; 907 for (const RelocationRef &R : Section.relocations()) 908 V.push_back(R); 909 // Sort relocations by address. 910 llvm::sort(V, isRelocAddressLess); 911 } 912 return Ret; 913 } 914 915 // Used for --adjust-vma to check if address should be adjusted by the 916 // specified value for a given section. 917 // For ELF we do not adjust non-allocatable sections like debug ones, 918 // because they are not loadable. 919 // TODO: implement for other file formats. 920 static bool shouldAdjustVA(const SectionRef &Section) { 921 const ObjectFile *Obj = Section.getObject(); 922 if (isa<object::ELFObjectFileBase>(Obj)) 923 return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC; 924 return false; 925 } 926 927 static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, 928 MCContext &Ctx, MCDisassembler *DisAsm, 929 const MCInstrAnalysis *MIA, MCInstPrinter *IP, 930 const MCSubtargetInfo *STI, PrettyPrinter &PIP, 931 SourcePrinter &SP, bool InlineRelocs) { 932 std::map<SectionRef, std::vector<RelocationRef>> RelocMap; 933 if (InlineRelocs) 934 RelocMap = getRelocsMap(*Obj); 935 936 // Create a mapping from virtual address to symbol name. This is used to 937 // pretty print the symbols while disassembling. 938 std::map<SectionRef, SectionSymbolsTy> AllSymbols; 939 SectionSymbolsTy AbsoluteSymbols; 940 for (const SymbolRef &Symbol : Obj->symbols()) { 941 Expected<uint64_t> AddressOrErr = Symbol.getAddress(); 942 if (!AddressOrErr) 943 report_error(Obj->getFileName(), AddressOrErr.takeError()); 944 uint64_t Address = *AddressOrErr; 945 946 Expected<StringRef> Name = Symbol.getName(); 947 if (!Name) 948 report_error(Obj->getFileName(), Name.takeError()); 949 if (Name->empty()) 950 continue; 951 952 Expected<section_iterator> SectionOrErr = Symbol.getSection(); 953 if (!SectionOrErr) 954 report_error(Obj->getFileName(), SectionOrErr.takeError()); 955 956 uint8_t SymbolType = ELF::STT_NOTYPE; 957 if (Obj->isELF()) { 958 SymbolType = getElfSymbolType(Obj, Symbol); 959 if (SymbolType == ELF::STT_SECTION) 960 continue; 961 } 962 963 section_iterator SecI = *SectionOrErr; 964 if (SecI != Obj->section_end()) 965 AllSymbols[*SecI].emplace_back(Address, *Name, SymbolType); 966 else 967 AbsoluteSymbols.emplace_back(Address, *Name, SymbolType); 968 969 970 } 971 if (AllSymbols.empty() && Obj->isELF()) 972 addDynamicElfSymbols(Obj, AllSymbols); 973 974 BumpPtrAllocator A; 975 StringSaver Saver(A); 976 addPltEntries(Obj, AllSymbols, Saver); 977 978 // Create a mapping from virtual address to section. 979 std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses; 980 for (SectionRef Sec : Obj->sections()) 981 SectionAddresses.emplace_back(Sec.getAddress(), Sec); 982 array_pod_sort(SectionAddresses.begin(), SectionAddresses.end()); 983 984 // Linked executables (.exe and .dll files) typically don't include a real 985 // symbol table but they might contain an export table. 986 if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) { 987 for (const auto &ExportEntry : COFFObj->export_directories()) { 988 StringRef Name; 989 error(ExportEntry.getSymbolName(Name)); 990 if (Name.empty()) 991 continue; 992 uint32_t RVA; 993 error(ExportEntry.getExportRVA(RVA)); 994 995 uint64_t VA = COFFObj->getImageBase() + RVA; 996 auto Sec = std::upper_bound( 997 SectionAddresses.begin(), SectionAddresses.end(), VA, 998 [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) { 999 return LHS < RHS.first; 1000 }); 1001 if (Sec != SectionAddresses.begin()) 1002 --Sec; 1003 else 1004 Sec = SectionAddresses.end(); 1005 1006 if (Sec != SectionAddresses.end()) 1007 AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE); 1008 else 1009 AbsoluteSymbols.emplace_back(VA, Name, ELF::STT_NOTYPE); 1010 } 1011 } 1012 1013 // Sort all the symbols, this allows us to use a simple binary search to find 1014 // a symbol near an address. 1015 for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols) 1016 array_pod_sort(SecSyms.second.begin(), SecSyms.second.end()); 1017 array_pod_sort(AbsoluteSymbols.begin(), AbsoluteSymbols.end()); 1018 1019 for (const SectionRef &Section : ToolSectionFilter(*Obj)) { 1020 if (!DisassembleAll && (!Section.isText() || Section.isVirtual())) 1021 continue; 1022 1023 uint64_t SectionAddr = Section.getAddress(); 1024 uint64_t SectSize = Section.getSize(); 1025 if (!SectSize) 1026 continue; 1027 1028 // Get the list of all the symbols in this section. 1029 SectionSymbolsTy &Symbols = AllSymbols[Section]; 1030 std::vector<uint64_t> DataMappingSymsAddr; 1031 std::vector<uint64_t> TextMappingSymsAddr; 1032 if (isArmElf(Obj)) { 1033 for (const auto &Symb : Symbols) { 1034 uint64_t Address = std::get<0>(Symb); 1035 StringRef Name = std::get<1>(Symb); 1036 if (Name.startswith("$d")) 1037 DataMappingSymsAddr.push_back(Address - SectionAddr); 1038 if (Name.startswith("$x")) 1039 TextMappingSymsAddr.push_back(Address - SectionAddr); 1040 if (Name.startswith("$a")) 1041 TextMappingSymsAddr.push_back(Address - SectionAddr); 1042 if (Name.startswith("$t")) 1043 TextMappingSymsAddr.push_back(Address - SectionAddr); 1044 } 1045 } 1046 1047 llvm::sort(DataMappingSymsAddr); 1048 llvm::sort(TextMappingSymsAddr); 1049 1050 if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) { 1051 // AMDGPU disassembler uses symbolizer for printing labels 1052 std::unique_ptr<MCRelocationInfo> RelInfo( 1053 TheTarget->createMCRelocationInfo(TripleName, Ctx)); 1054 if (RelInfo) { 1055 std::unique_ptr<MCSymbolizer> Symbolizer( 1056 TheTarget->createMCSymbolizer( 1057 TripleName, nullptr, nullptr, &Symbols, &Ctx, std::move(RelInfo))); 1058 DisAsm->setSymbolizer(std::move(Symbolizer)); 1059 } 1060 } 1061 1062 StringRef SegmentName = ""; 1063 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) { 1064 DataRefImpl DR = Section.getRawDataRefImpl(); 1065 SegmentName = MachO->getSectionFinalSegmentName(DR); 1066 } 1067 StringRef SectionName; 1068 error(Section.getName(SectionName)); 1069 1070 // If the section has no symbol at the start, just insert a dummy one. 1071 if (Symbols.empty() || std::get<0>(Symbols[0]) != 0) { 1072 Symbols.insert( 1073 Symbols.begin(), 1074 std::make_tuple(SectionAddr, SectionName, 1075 Section.isText() ? ELF::STT_FUNC : ELF::STT_OBJECT)); 1076 } 1077 1078 SmallString<40> Comments; 1079 raw_svector_ostream CommentStream(Comments); 1080 1081 StringRef BytesStr; 1082 error(Section.getContents(BytesStr)); 1083 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()), 1084 BytesStr.size()); 1085 1086 uint64_t VMAAdjustment = 0; 1087 if (shouldAdjustVA(Section)) 1088 VMAAdjustment = AdjustVMA; 1089 1090 uint64_t Size; 1091 uint64_t Index; 1092 bool PrintedSection = false; 1093 std::vector<RelocationRef> Rels = RelocMap[Section]; 1094 std::vector<RelocationRef>::const_iterator RelCur = Rels.begin(); 1095 std::vector<RelocationRef>::const_iterator RelEnd = Rels.end(); 1096 // Disassemble symbol by symbol. 1097 for (unsigned SI = 0, SE = Symbols.size(); SI != SE; ++SI) { 1098 uint64_t Start = std::get<0>(Symbols[SI]) - SectionAddr; 1099 // The end is either the section end or the beginning of the next 1100 // symbol. 1101 uint64_t End = (SI == SE - 1) 1102 ? SectSize 1103 : std::get<0>(Symbols[SI + 1]) - SectionAddr; 1104 // Don't try to disassemble beyond the end of section contents. 1105 if (End > SectSize) 1106 End = SectSize; 1107 // If this symbol has the same address as the next symbol, then skip it. 1108 if (Start >= End) 1109 continue; 1110 1111 // Check if we need to skip symbol 1112 // Skip if the symbol's data is not between StartAddress and StopAddress 1113 if (End + SectionAddr < StartAddress || 1114 Start + SectionAddr > StopAddress) { 1115 continue; 1116 } 1117 1118 /// Skip if user requested specific symbols and this is not in the list 1119 if (!DisasmFuncsSet.empty() && 1120 !DisasmFuncsSet.count(std::get<1>(Symbols[SI]))) 1121 continue; 1122 1123 if (!PrintedSection) { 1124 PrintedSection = true; 1125 outs() << "Disassembly of section "; 1126 if (!SegmentName.empty()) 1127 outs() << SegmentName << ","; 1128 outs() << SectionName << ':'; 1129 } 1130 1131 // Stop disassembly at the stop address specified 1132 if (End + SectionAddr > StopAddress) 1133 End = StopAddress - SectionAddr; 1134 1135 if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) { 1136 if (std::get<2>(Symbols[SI]) == ELF::STT_AMDGPU_HSA_KERNEL) { 1137 // skip amd_kernel_code_t at the begining of kernel symbol (256 bytes) 1138 Start += 256; 1139 } 1140 if (SI == SE - 1 || 1141 std::get<2>(Symbols[SI + 1]) == ELF::STT_AMDGPU_HSA_KERNEL) { 1142 // cut trailing zeroes at the end of kernel 1143 // cut up to 256 bytes 1144 const uint64_t EndAlign = 256; 1145 const auto Limit = End - (std::min)(EndAlign, End - Start); 1146 while (End > Limit && 1147 *reinterpret_cast<const support::ulittle32_t*>(&Bytes[End - 4]) == 0) 1148 End -= 4; 1149 } 1150 } 1151 1152 outs() << '\n'; 1153 if (!NoLeadingAddr) 1154 outs() << format("%016" PRIx64 " ", 1155 SectionAddr + Start + VMAAdjustment); 1156 1157 StringRef SymbolName = std::get<1>(Symbols[SI]); 1158 if (Demangle) 1159 outs() << demangle(SymbolName) << ":\n"; 1160 else 1161 outs() << SymbolName << ":\n"; 1162 1163 // Don't print raw contents of a virtual section. A virtual section 1164 // doesn't have any contents in the file. 1165 if (Section.isVirtual()) { 1166 outs() << "...\n"; 1167 continue; 1168 } 1169 1170 #ifndef NDEBUG 1171 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); 1172 #else 1173 raw_ostream &DebugOut = nulls(); 1174 #endif 1175 1176 // Some targets (like WebAssembly) have a special prelude at the start 1177 // of each symbol. 1178 DisAsm->onSymbolStart(SymbolName, Size, Bytes.slice(Start, End - Start), 1179 SectionAddr + Start, DebugOut, CommentStream); 1180 Start += Size; 1181 1182 for (Index = Start; Index < End; Index += Size) { 1183 MCInst Inst; 1184 1185 if (Index + SectionAddr < StartAddress || 1186 Index + SectionAddr > StopAddress) { 1187 // skip byte by byte till StartAddress is reached 1188 Size = 1; 1189 continue; 1190 } 1191 // AArch64 ELF binaries can interleave data and text in the 1192 // same section. We rely on the markers introduced to 1193 // understand what we need to dump. If the data marker is within a 1194 // function, it is denoted as a word/short etc 1195 if (isArmElf(Obj) && std::get<2>(Symbols[SI]) != ELF::STT_OBJECT && 1196 !DisassembleAll) { 1197 uint64_t Stride = 0; 1198 1199 auto DAI = std::lower_bound(DataMappingSymsAddr.begin(), 1200 DataMappingSymsAddr.end(), Index); 1201 if (DAI != DataMappingSymsAddr.end() && *DAI == Index) { 1202 // Switch to data. 1203 while (Index < End) { 1204 outs() << format("%8" PRIx64 ":", SectionAddr + Index); 1205 outs() << "\t"; 1206 if (Index + 4 <= End) { 1207 Stride = 4; 1208 dumpBytes(Bytes.slice(Index, 4), outs()); 1209 outs() << "\t.word\t"; 1210 uint32_t Data = 0; 1211 if (Obj->isLittleEndian()) { 1212 const auto Word = 1213 reinterpret_cast<const support::ulittle32_t *>( 1214 Bytes.data() + Index); 1215 Data = *Word; 1216 } else { 1217 const auto Word = reinterpret_cast<const support::ubig32_t *>( 1218 Bytes.data() + Index); 1219 Data = *Word; 1220 } 1221 outs() << "0x" << format("%08" PRIx32, Data); 1222 } else if (Index + 2 <= End) { 1223 Stride = 2; 1224 dumpBytes(Bytes.slice(Index, 2), outs()); 1225 outs() << "\t\t.short\t"; 1226 uint16_t Data = 0; 1227 if (Obj->isLittleEndian()) { 1228 const auto Short = 1229 reinterpret_cast<const support::ulittle16_t *>( 1230 Bytes.data() + Index); 1231 Data = *Short; 1232 } else { 1233 const auto Short = 1234 reinterpret_cast<const support::ubig16_t *>(Bytes.data() + 1235 Index); 1236 Data = *Short; 1237 } 1238 outs() << "0x" << format("%04" PRIx16, Data); 1239 } else { 1240 Stride = 1; 1241 dumpBytes(Bytes.slice(Index, 1), outs()); 1242 outs() << "\t\t.byte\t"; 1243 outs() << "0x" << format("%02" PRIx8, Bytes.slice(Index, 1)[0]); 1244 } 1245 Index += Stride; 1246 outs() << "\n"; 1247 auto TAI = std::lower_bound(TextMappingSymsAddr.begin(), 1248 TextMappingSymsAddr.end(), Index); 1249 if (TAI != TextMappingSymsAddr.end() && *TAI == Index) 1250 break; 1251 } 1252 } 1253 } 1254 1255 // If there is a data symbol inside an ELF text section and we are only 1256 // disassembling text (applicable all architectures), 1257 // we are in a situation where we must print the data and not 1258 // disassemble it. 1259 if (Obj->isELF() && std::get<2>(Symbols[SI]) == ELF::STT_OBJECT && 1260 !DisassembleAll && Section.isText()) { 1261 // print out data up to 8 bytes at a time in hex and ascii 1262 uint8_t AsciiData[9] = {'\0'}; 1263 uint8_t Byte; 1264 int NumBytes = 0; 1265 1266 for (Index = Start; Index < End; Index += 1) { 1267 if (((SectionAddr + Index) < StartAddress) || 1268 ((SectionAddr + Index) > StopAddress)) 1269 continue; 1270 if (NumBytes == 0) { 1271 outs() << format("%8" PRIx64 ":", SectionAddr + Index); 1272 outs() << "\t"; 1273 } 1274 Byte = Bytes.slice(Index)[0]; 1275 outs() << format(" %02x", Byte); 1276 AsciiData[NumBytes] = isPrint(Byte) ? Byte : '.'; 1277 1278 uint8_t IndentOffset = 0; 1279 NumBytes++; 1280 if (Index == End - 1 || NumBytes > 8) { 1281 // Indent the space for less than 8 bytes data. 1282 // 2 spaces for byte and one for space between bytes 1283 IndentOffset = 3 * (8 - NumBytes); 1284 for (int Excess = NumBytes; Excess < 8; Excess++) 1285 AsciiData[Excess] = '\0'; 1286 NumBytes = 8; 1287 } 1288 if (NumBytes == 8) { 1289 AsciiData[8] = '\0'; 1290 outs() << std::string(IndentOffset, ' ') << " "; 1291 outs() << reinterpret_cast<char *>(AsciiData); 1292 outs() << '\n'; 1293 NumBytes = 0; 1294 } 1295 } 1296 } 1297 if (Index >= End) 1298 break; 1299 1300 // When -z or --disassemble-zeroes are given we always dissasemble them. 1301 // Otherwise we might want to skip zero bytes we see. 1302 if (!DisassembleZeroes) { 1303 uint64_t MaxOffset = End - Index; 1304 // For -reloc: print zero blocks patched by relocations, so that 1305 // relocations can be shown in the dump. 1306 if (RelCur != RelEnd) 1307 MaxOffset = RelCur->getOffset() - Index; 1308 1309 if (size_t N = 1310 countSkippableZeroBytes(Bytes.slice(Index, MaxOffset))) { 1311 outs() << "\t\t..." << '\n'; 1312 Index += N; 1313 if (Index >= End) 1314 break; 1315 } 1316 } 1317 1318 // Disassemble a real instruction or a data when disassemble all is 1319 // provided 1320 bool Disassembled = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), 1321 SectionAddr + Index, DebugOut, 1322 CommentStream); 1323 if (Size == 0) 1324 Size = 1; 1325 1326 PIP.printInst( 1327 *IP, Disassembled ? &Inst : nullptr, Bytes.slice(Index, Size), 1328 SectionAddr + Index + VMAAdjustment, outs(), "", *STI, &SP, &Rels); 1329 outs() << CommentStream.str(); 1330 Comments.clear(); 1331 1332 // Try to resolve the target of a call, tail call, etc. to a specific 1333 // symbol. 1334 if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) || 1335 MIA->isConditionalBranch(Inst))) { 1336 uint64_t Target; 1337 if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) { 1338 // In a relocatable object, the target's section must reside in 1339 // the same section as the call instruction or it is accessed 1340 // through a relocation. 1341 // 1342 // In a non-relocatable object, the target may be in any section. 1343 // 1344 // N.B. We don't walk the relocations in the relocatable case yet. 1345 auto *TargetSectionSymbols = &Symbols; 1346 if (!Obj->isRelocatableObject()) { 1347 auto SectionAddress = std::upper_bound( 1348 SectionAddresses.begin(), SectionAddresses.end(), Target, 1349 [](uint64_t LHS, 1350 const std::pair<uint64_t, SectionRef> &RHS) { 1351 return LHS < RHS.first; 1352 }); 1353 if (SectionAddress != SectionAddresses.begin()) { 1354 --SectionAddress; 1355 TargetSectionSymbols = &AllSymbols[SectionAddress->second]; 1356 } else { 1357 TargetSectionSymbols = &AbsoluteSymbols; 1358 } 1359 } 1360 1361 // Find the first symbol in the section whose offset is less than 1362 // or equal to the target. If there isn't a section that contains 1363 // the target, find the nearest preceding absolute symbol. 1364 auto TargetSym = std::upper_bound( 1365 TargetSectionSymbols->begin(), TargetSectionSymbols->end(), 1366 Target, [](uint64_t LHS, 1367 const std::tuple<uint64_t, StringRef, uint8_t> &RHS) { 1368 return LHS < std::get<0>(RHS); 1369 }); 1370 if (TargetSym == TargetSectionSymbols->begin()) { 1371 TargetSectionSymbols = &AbsoluteSymbols; 1372 TargetSym = std::upper_bound( 1373 AbsoluteSymbols.begin(), AbsoluteSymbols.end(), 1374 Target, [](uint64_t LHS, 1375 const std::tuple<uint64_t, StringRef, uint8_t> &RHS) { 1376 return LHS < std::get<0>(RHS); 1377 }); 1378 } 1379 if (TargetSym != TargetSectionSymbols->begin()) { 1380 --TargetSym; 1381 uint64_t TargetAddress = std::get<0>(*TargetSym); 1382 StringRef TargetName = std::get<1>(*TargetSym); 1383 outs() << " <" << TargetName; 1384 uint64_t Disp = Target - TargetAddress; 1385 if (Disp) 1386 outs() << "+0x" << Twine::utohexstr(Disp); 1387 outs() << '>'; 1388 } 1389 } 1390 } 1391 outs() << "\n"; 1392 1393 // Hexagon does this in pretty printer 1394 if (Obj->getArch() != Triple::hexagon) { 1395 // Print relocation for instruction. 1396 while (RelCur != RelEnd) { 1397 uint64_t Offset = RelCur->getOffset(); 1398 // If this relocation is hidden, skip it. 1399 if (getHidden(*RelCur) || ((SectionAddr + Offset) < StartAddress)) { 1400 ++RelCur; 1401 continue; 1402 } 1403 1404 // Stop when RelCur's offset is past the current instruction. 1405 if (Offset >= Index + Size) 1406 break; 1407 1408 // When --adjust-vma is used, update the address printed. 1409 if (RelCur->getSymbol() != Obj->symbol_end()) { 1410 Expected<section_iterator> SymSI = 1411 RelCur->getSymbol()->getSection(); 1412 if (SymSI && *SymSI != Obj->section_end() && 1413 (shouldAdjustVA(**SymSI))) 1414 Offset += AdjustVMA; 1415 } 1416 1417 printRelocation(*RelCur, SectionAddr + Offset, 1418 Obj->getBytesInAddress()); 1419 ++RelCur; 1420 } 1421 } 1422 } 1423 } 1424 } 1425 } 1426 1427 static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) { 1428 if (StartAddress > StopAddress) 1429 error("Start address should be less than stop address"); 1430 1431 const Target *TheTarget = getTarget(Obj); 1432 1433 // Package up features to be passed to target/subtarget 1434 SubtargetFeatures Features = Obj->getFeatures(); 1435 if (!MAttrs.empty()) 1436 for (unsigned I = 0; I != MAttrs.size(); ++I) 1437 Features.AddFeature(MAttrs[I]); 1438 1439 std::unique_ptr<const MCRegisterInfo> MRI( 1440 TheTarget->createMCRegInfo(TripleName)); 1441 if (!MRI) 1442 report_error(Obj->getFileName(), 1443 "no register info for target " + TripleName); 1444 1445 // Set up disassembler. 1446 std::unique_ptr<const MCAsmInfo> AsmInfo( 1447 TheTarget->createMCAsmInfo(*MRI, TripleName)); 1448 if (!AsmInfo) 1449 report_error(Obj->getFileName(), 1450 "no assembly info for target " + TripleName); 1451 std::unique_ptr<const MCSubtargetInfo> STI( 1452 TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString())); 1453 if (!STI) 1454 report_error(Obj->getFileName(), 1455 "no subtarget info for target " + TripleName); 1456 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo()); 1457 if (!MII) 1458 report_error(Obj->getFileName(), 1459 "no instruction info for target " + TripleName); 1460 MCObjectFileInfo MOFI; 1461 MCContext Ctx(AsmInfo.get(), MRI.get(), &MOFI); 1462 // FIXME: for now initialize MCObjectFileInfo with default values 1463 MOFI.InitMCObjectFileInfo(Triple(TripleName), false, Ctx); 1464 1465 std::unique_ptr<MCDisassembler> DisAsm( 1466 TheTarget->createMCDisassembler(*STI, Ctx)); 1467 if (!DisAsm) 1468 report_error(Obj->getFileName(), 1469 "no disassembler for target " + TripleName); 1470 1471 std::unique_ptr<const MCInstrAnalysis> MIA( 1472 TheTarget->createMCInstrAnalysis(MII.get())); 1473 1474 int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); 1475 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( 1476 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI)); 1477 if (!IP) 1478 report_error(Obj->getFileName(), 1479 "no instruction printer for target " + TripleName); 1480 IP->setPrintImmHex(PrintImmHex); 1481 1482 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName)); 1483 SourcePrinter SP(Obj, TheTarget->getName()); 1484 1485 for (StringRef Opt : DisassemblerOptions) 1486 if (!IP->applyTargetSpecificCLOption(Opt)) 1487 error("Unrecognized disassembler option: " + Opt); 1488 1489 disassembleObject(TheTarget, Obj, Ctx, DisAsm.get(), MIA.get(), IP.get(), 1490 STI.get(), PIP, SP, InlineRelocs); 1491 } 1492 1493 void llvm::printRelocations(const ObjectFile *Obj) { 1494 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : 1495 "%08" PRIx64; 1496 // Regular objdump doesn't print relocations in non-relocatable object 1497 // files. 1498 if (!Obj->isRelocatableObject()) 1499 return; 1500 1501 for (const SectionRef &Section : ToolSectionFilter(*Obj)) { 1502 if (Section.relocation_begin() == Section.relocation_end()) 1503 continue; 1504 StringRef SecName; 1505 error(Section.getName(SecName)); 1506 outs() << "RELOCATION RECORDS FOR [" << SecName << "]:\n"; 1507 for (const RelocationRef &Reloc : Section.relocations()) { 1508 uint64_t Address = Reloc.getOffset(); 1509 SmallString<32> RelocName; 1510 SmallString<32> ValueStr; 1511 if (Address < StartAddress || Address > StopAddress || getHidden(Reloc)) 1512 continue; 1513 Reloc.getTypeName(RelocName); 1514 error(getRelocationValueString(Reloc, ValueStr)); 1515 outs() << format(Fmt.data(), Address) << " " << RelocName << " " 1516 << ValueStr << "\n"; 1517 } 1518 outs() << "\n"; 1519 } 1520 } 1521 1522 void llvm::printDynamicRelocations(const ObjectFile *Obj) { 1523 // For the moment, this option is for ELF only 1524 if (!Obj->isELF()) 1525 return; 1526 1527 const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj); 1528 if (!Elf || Elf->getEType() != ELF::ET_DYN) { 1529 error("not a dynamic object"); 1530 return; 1531 } 1532 1533 std::vector<SectionRef> DynRelSec = Obj->dynamic_relocation_sections(); 1534 if (DynRelSec.empty()) 1535 return; 1536 1537 outs() << "DYNAMIC RELOCATION RECORDS\n"; 1538 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64; 1539 for (const SectionRef &Section : DynRelSec) { 1540 if (Section.relocation_begin() == Section.relocation_end()) 1541 continue; 1542 for (const RelocationRef &Reloc : Section.relocations()) { 1543 uint64_t Address = Reloc.getOffset(); 1544 SmallString<32> RelocName; 1545 SmallString<32> ValueStr; 1546 Reloc.getTypeName(RelocName); 1547 error(getRelocationValueString(Reloc, ValueStr)); 1548 outs() << format(Fmt.data(), Address) << " " << RelocName << " " 1549 << ValueStr << "\n"; 1550 } 1551 } 1552 } 1553 1554 // Returns true if we need to show LMA column when dumping section headers. We 1555 // show it only when the platform is ELF and either we have at least one section 1556 // whose VMA and LMA are different and/or when --show-lma flag is used. 1557 static bool shouldDisplayLMA(const ObjectFile *Obj) { 1558 if (!Obj->isELF()) 1559 return false; 1560 for (const SectionRef &S : ToolSectionFilter(*Obj)) 1561 if (S.getAddress() != getELFSectionLMA(S)) 1562 return true; 1563 return ShowLMA; 1564 } 1565 1566 void llvm::printSectionHeaders(const ObjectFile *Obj) { 1567 bool HasLMAColumn = shouldDisplayLMA(Obj); 1568 if (HasLMAColumn) 1569 outs() << "Sections:\n" 1570 "Idx Name Size VMA LMA " 1571 "Type\n"; 1572 else 1573 outs() << "Sections:\n" 1574 "Idx Name Size VMA Type\n"; 1575 1576 for (const SectionRef &Section : ToolSectionFilter(*Obj)) { 1577 StringRef Name; 1578 error(Section.getName(Name)); 1579 uint64_t VMA = Section.getAddress(); 1580 if (shouldAdjustVA(Section)) 1581 VMA += AdjustVMA; 1582 1583 uint64_t Size = Section.getSize(); 1584 bool Text = Section.isText(); 1585 bool Data = Section.isData(); 1586 bool BSS = Section.isBSS(); 1587 std::string Type = (std::string(Text ? "TEXT " : "") + 1588 (Data ? "DATA " : "") + (BSS ? "BSS" : "")); 1589 1590 if (HasLMAColumn) 1591 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %016" PRIx64 1592 " %s\n", 1593 (unsigned)Section.getIndex(), Name.str().c_str(), Size, 1594 VMA, getELFSectionLMA(Section), Type.c_str()); 1595 else 1596 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", 1597 (unsigned)Section.getIndex(), Name.str().c_str(), Size, 1598 VMA, Type.c_str()); 1599 } 1600 outs() << "\n"; 1601 } 1602 1603 void llvm::printSectionContents(const ObjectFile *Obj) { 1604 std::error_code EC; 1605 for (const SectionRef &Section : ToolSectionFilter(*Obj)) { 1606 StringRef Name; 1607 StringRef Contents; 1608 error(Section.getName(Name)); 1609 uint64_t BaseAddr = Section.getAddress(); 1610 uint64_t Size = Section.getSize(); 1611 if (!Size) 1612 continue; 1613 1614 outs() << "Contents of section " << Name << ":\n"; 1615 if (Section.isBSS()) { 1616 outs() << format("<skipping contents of bss section at [%04" PRIx64 1617 ", %04" PRIx64 ")>\n", 1618 BaseAddr, BaseAddr + Size); 1619 continue; 1620 } 1621 1622 error(Section.getContents(Contents)); 1623 1624 // Dump out the content as hex and printable ascii characters. 1625 for (std::size_t Addr = 0, End = Contents.size(); Addr < End; Addr += 16) { 1626 outs() << format(" %04" PRIx64 " ", BaseAddr + Addr); 1627 // Dump line of hex. 1628 for (std::size_t I = 0; I < 16; ++I) { 1629 if (I != 0 && I % 4 == 0) 1630 outs() << ' '; 1631 if (Addr + I < End) 1632 outs() << hexdigit((Contents[Addr + I] >> 4) & 0xF, true) 1633 << hexdigit(Contents[Addr + I] & 0xF, true); 1634 else 1635 outs() << " "; 1636 } 1637 // Print ascii. 1638 outs() << " "; 1639 for (std::size_t I = 0; I < 16 && Addr + I < End; ++I) { 1640 if (isPrint(static_cast<unsigned char>(Contents[Addr + I]) & 0xFF)) 1641 outs() << Contents[Addr + I]; 1642 else 1643 outs() << "."; 1644 } 1645 outs() << "\n"; 1646 } 1647 } 1648 } 1649 1650 void llvm::printSymbolTable(const ObjectFile *O, StringRef ArchiveName, 1651 StringRef ArchitectureName) { 1652 outs() << "SYMBOL TABLE:\n"; 1653 1654 if (const COFFObjectFile *Coff = dyn_cast<const COFFObjectFile>(O)) { 1655 printCOFFSymbolTable(Coff); 1656 return; 1657 } 1658 1659 for (auto I = O->symbol_begin(), E = O->symbol_end(); I != E; ++I) { 1660 // Skip printing the special zero symbol when dumping an ELF file. 1661 // This makes the output consistent with the GNU objdump. 1662 if (I == O->symbol_begin() && isa<ELFObjectFileBase>(O)) 1663 continue; 1664 1665 const SymbolRef &Symbol = *I; 1666 Expected<uint64_t> AddressOrError = Symbol.getAddress(); 1667 if (!AddressOrError) 1668 report_error(ArchiveName, O->getFileName(), AddressOrError.takeError(), 1669 ArchitectureName); 1670 uint64_t Address = *AddressOrError; 1671 if ((Address < StartAddress) || (Address > StopAddress)) 1672 continue; 1673 Expected<SymbolRef::Type> TypeOrError = Symbol.getType(); 1674 if (!TypeOrError) 1675 report_error(ArchiveName, O->getFileName(), TypeOrError.takeError(), 1676 ArchitectureName); 1677 SymbolRef::Type Type = *TypeOrError; 1678 uint32_t Flags = Symbol.getFlags(); 1679 Expected<section_iterator> SectionOrErr = Symbol.getSection(); 1680 if (!SectionOrErr) 1681 report_error(ArchiveName, O->getFileName(), SectionOrErr.takeError(), 1682 ArchitectureName); 1683 section_iterator Section = *SectionOrErr; 1684 StringRef Name; 1685 if (Type == SymbolRef::ST_Debug && Section != O->section_end()) { 1686 Section->getName(Name); 1687 } else { 1688 Expected<StringRef> NameOrErr = Symbol.getName(); 1689 if (!NameOrErr) 1690 report_error(ArchiveName, O->getFileName(), NameOrErr.takeError(), 1691 ArchitectureName); 1692 Name = *NameOrErr; 1693 } 1694 1695 bool Global = Flags & SymbolRef::SF_Global; 1696 bool Weak = Flags & SymbolRef::SF_Weak; 1697 bool Absolute = Flags & SymbolRef::SF_Absolute; 1698 bool Common = Flags & SymbolRef::SF_Common; 1699 bool Hidden = Flags & SymbolRef::SF_Hidden; 1700 1701 char GlobLoc = ' '; 1702 if (Type != SymbolRef::ST_Unknown) 1703 GlobLoc = Global ? 'g' : 'l'; 1704 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File) 1705 ? 'd' : ' '; 1706 char FileFunc = ' '; 1707 if (Type == SymbolRef::ST_File) 1708 FileFunc = 'f'; 1709 else if (Type == SymbolRef::ST_Function) 1710 FileFunc = 'F'; 1711 else if (Type == SymbolRef::ST_Data) 1712 FileFunc = 'O'; 1713 1714 const char *Fmt = O->getBytesInAddress() > 4 ? "%016" PRIx64 : 1715 "%08" PRIx64; 1716 1717 outs() << format(Fmt, Address) << " " 1718 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' ' 1719 << (Weak ? 'w' : ' ') // Weak? 1720 << ' ' // Constructor. Not supported yet. 1721 << ' ' // Warning. Not supported yet. 1722 << ' ' // Indirect reference to another symbol. 1723 << Debug // Debugging (d) or dynamic (D) symbol. 1724 << FileFunc // Name of function (F), file (f) or object (O). 1725 << ' '; 1726 if (Absolute) { 1727 outs() << "*ABS*"; 1728 } else if (Common) { 1729 outs() << "*COM*"; 1730 } else if (Section == O->section_end()) { 1731 outs() << "*UND*"; 1732 } else { 1733 if (const MachOObjectFile *MachO = 1734 dyn_cast<const MachOObjectFile>(O)) { 1735 DataRefImpl DR = Section->getRawDataRefImpl(); 1736 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR); 1737 outs() << SegmentName << ","; 1738 } 1739 StringRef SectionName; 1740 error(Section->getName(SectionName)); 1741 outs() << SectionName; 1742 } 1743 1744 outs() << '\t'; 1745 if (Common || isa<ELFObjectFileBase>(O)) { 1746 uint64_t Val = 1747 Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize(); 1748 outs() << format("\t %08" PRIx64 " ", Val); 1749 } 1750 1751 if (Hidden) 1752 outs() << ".hidden "; 1753 1754 if (Demangle) 1755 outs() << demangle(Name) << '\n'; 1756 else 1757 outs() << Name << '\n'; 1758 } 1759 } 1760 1761 static void printUnwindInfo(const ObjectFile *O) { 1762 outs() << "Unwind info:\n\n"; 1763 1764 if (const COFFObjectFile *Coff = dyn_cast<COFFObjectFile>(O)) 1765 printCOFFUnwindInfo(Coff); 1766 else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O)) 1767 printMachOUnwindInfo(MachO); 1768 else 1769 // TODO: Extract DWARF dump tool to objdump. 1770 WithColor::error(errs(), ToolName) 1771 << "This operation is only currently supported " 1772 "for COFF and MachO object files.\n"; 1773 } 1774 1775 void llvm::printExportsTrie(const ObjectFile *o) { 1776 outs() << "Exports trie:\n"; 1777 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 1778 printMachOExportsTrie(MachO); 1779 else 1780 WithColor::error(errs(), ToolName) 1781 << "This operation is only currently supported " 1782 "for Mach-O executable files.\n"; 1783 } 1784 1785 void llvm::printRebaseTable(ObjectFile *o) { 1786 outs() << "Rebase table:\n"; 1787 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 1788 printMachORebaseTable(MachO); 1789 else 1790 WithColor::error(errs(), ToolName) 1791 << "This operation is only currently supported " 1792 "for Mach-O executable files.\n"; 1793 } 1794 1795 void llvm::printBindTable(ObjectFile *o) { 1796 outs() << "Bind table:\n"; 1797 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 1798 printMachOBindTable(MachO); 1799 else 1800 WithColor::error(errs(), ToolName) 1801 << "This operation is only currently supported " 1802 "for Mach-O executable files.\n"; 1803 } 1804 1805 void llvm::printLazyBindTable(ObjectFile *o) { 1806 outs() << "Lazy bind table:\n"; 1807 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 1808 printMachOLazyBindTable(MachO); 1809 else 1810 WithColor::error(errs(), ToolName) 1811 << "This operation is only currently supported " 1812 "for Mach-O executable files.\n"; 1813 } 1814 1815 void llvm::printWeakBindTable(ObjectFile *o) { 1816 outs() << "Weak bind table:\n"; 1817 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) 1818 printMachOWeakBindTable(MachO); 1819 else 1820 WithColor::error(errs(), ToolName) 1821 << "This operation is only currently supported " 1822 "for Mach-O executable files.\n"; 1823 } 1824 1825 /// Dump the raw contents of the __clangast section so the output can be piped 1826 /// into llvm-bcanalyzer. 1827 void llvm::printRawClangAST(const ObjectFile *Obj) { 1828 if (outs().is_displayed()) { 1829 WithColor::error(errs(), ToolName) 1830 << "The -raw-clang-ast option will dump the raw binary contents of " 1831 "the clang ast section.\n" 1832 "Please redirect the output to a file or another program such as " 1833 "llvm-bcanalyzer.\n"; 1834 return; 1835 } 1836 1837 StringRef ClangASTSectionName("__clangast"); 1838 if (isa<COFFObjectFile>(Obj)) { 1839 ClangASTSectionName = "clangast"; 1840 } 1841 1842 Optional<object::SectionRef> ClangASTSection; 1843 for (auto Sec : ToolSectionFilter(*Obj)) { 1844 StringRef Name; 1845 Sec.getName(Name); 1846 if (Name == ClangASTSectionName) { 1847 ClangASTSection = Sec; 1848 break; 1849 } 1850 } 1851 if (!ClangASTSection) 1852 return; 1853 1854 StringRef ClangASTContents; 1855 error(ClangASTSection.getValue().getContents(ClangASTContents)); 1856 outs().write(ClangASTContents.data(), ClangASTContents.size()); 1857 } 1858 1859 static void printFaultMaps(const ObjectFile *Obj) { 1860 StringRef FaultMapSectionName; 1861 1862 if (isa<ELFObjectFileBase>(Obj)) { 1863 FaultMapSectionName = ".llvm_faultmaps"; 1864 } else if (isa<MachOObjectFile>(Obj)) { 1865 FaultMapSectionName = "__llvm_faultmaps"; 1866 } else { 1867 WithColor::error(errs(), ToolName) 1868 << "This operation is only currently supported " 1869 "for ELF and Mach-O executable files.\n"; 1870 return; 1871 } 1872 1873 Optional<object::SectionRef> FaultMapSection; 1874 1875 for (auto Sec : ToolSectionFilter(*Obj)) { 1876 StringRef Name; 1877 Sec.getName(Name); 1878 if (Name == FaultMapSectionName) { 1879 FaultMapSection = Sec; 1880 break; 1881 } 1882 } 1883 1884 outs() << "FaultMap table:\n"; 1885 1886 if (!FaultMapSection.hasValue()) { 1887 outs() << "<not found>\n"; 1888 return; 1889 } 1890 1891 StringRef FaultMapContents; 1892 error(FaultMapSection.getValue().getContents(FaultMapContents)); 1893 1894 FaultMapParser FMP(FaultMapContents.bytes_begin(), 1895 FaultMapContents.bytes_end()); 1896 1897 outs() << FMP; 1898 } 1899 1900 static void printPrivateFileHeaders(const ObjectFile *O, bool OnlyFirst) { 1901 if (O->isELF()) { 1902 printELFFileHeader(O); 1903 printELFDynamicSection(O); 1904 printELFSymbolVersionInfo(O); 1905 return; 1906 } 1907 if (O->isCOFF()) 1908 return printCOFFFileHeader(O); 1909 if (O->isWasm()) 1910 return printWasmFileHeader(O); 1911 if (O->isMachO()) { 1912 printMachOFileHeader(O); 1913 if (!OnlyFirst) 1914 printMachOLoadCommands(O); 1915 return; 1916 } 1917 report_error(O->getFileName(), "Invalid/Unsupported object file format"); 1918 } 1919 1920 static void printFileHeaders(const ObjectFile *O) { 1921 if (!O->isELF() && !O->isCOFF()) 1922 report_error(O->getFileName(), "Invalid/Unsupported object file format"); 1923 1924 Triple::ArchType AT = O->getArch(); 1925 outs() << "architecture: " << Triple::getArchTypeName(AT) << "\n"; 1926 Expected<uint64_t> StartAddrOrErr = O->getStartAddress(); 1927 if (!StartAddrOrErr) 1928 report_error(O->getFileName(), StartAddrOrErr.takeError()); 1929 1930 StringRef Fmt = O->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64; 1931 uint64_t Address = StartAddrOrErr.get(); 1932 outs() << "start address: " 1933 << "0x" << format(Fmt.data(), Address) << "\n\n"; 1934 } 1935 1936 static void printArchiveChild(StringRef Filename, const Archive::Child &C) { 1937 Expected<sys::fs::perms> ModeOrErr = C.getAccessMode(); 1938 if (!ModeOrErr) { 1939 WithColor::error(errs(), ToolName) << "ill-formed archive entry.\n"; 1940 consumeError(ModeOrErr.takeError()); 1941 return; 1942 } 1943 sys::fs::perms Mode = ModeOrErr.get(); 1944 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-"); 1945 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-"); 1946 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-"); 1947 outs() << ((Mode & sys::fs::group_read) ? "r" : "-"); 1948 outs() << ((Mode & sys::fs::group_write) ? "w" : "-"); 1949 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-"); 1950 outs() << ((Mode & sys::fs::others_read) ? "r" : "-"); 1951 outs() << ((Mode & sys::fs::others_write) ? "w" : "-"); 1952 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-"); 1953 1954 outs() << " "; 1955 1956 Expected<unsigned> UIDOrErr = C.getUID(); 1957 if (!UIDOrErr) 1958 report_error(Filename, UIDOrErr.takeError()); 1959 unsigned UID = UIDOrErr.get(); 1960 outs() << format("%d/", UID); 1961 1962 Expected<unsigned> GIDOrErr = C.getGID(); 1963 if (!GIDOrErr) 1964 report_error(Filename, GIDOrErr.takeError()); 1965 unsigned GID = GIDOrErr.get(); 1966 outs() << format("%-d ", GID); 1967 1968 Expected<uint64_t> Size = C.getRawSize(); 1969 if (!Size) 1970 report_error(Filename, Size.takeError()); 1971 outs() << format("%6" PRId64, Size.get()) << " "; 1972 1973 StringRef RawLastModified = C.getRawLastModified(); 1974 unsigned Seconds; 1975 if (RawLastModified.getAsInteger(10, Seconds)) 1976 outs() << "(date: \"" << RawLastModified 1977 << "\" contains non-decimal chars) "; 1978 else { 1979 // Since ctime(3) returns a 26 character string of the form: 1980 // "Sun Sep 16 01:03:52 1973\n\0" 1981 // just print 24 characters. 1982 time_t t = Seconds; 1983 outs() << format("%.24s ", ctime(&t)); 1984 } 1985 1986 StringRef Name = ""; 1987 Expected<StringRef> NameOrErr = C.getName(); 1988 if (!NameOrErr) { 1989 consumeError(NameOrErr.takeError()); 1990 Expected<StringRef> RawNameOrErr = C.getRawName(); 1991 if (!RawNameOrErr) 1992 report_error(Filename, NameOrErr.takeError()); 1993 Name = RawNameOrErr.get(); 1994 } else { 1995 Name = NameOrErr.get(); 1996 } 1997 outs() << Name << "\n"; 1998 } 1999 2000 static void dumpObject(ObjectFile *O, const Archive *A = nullptr, 2001 const Archive::Child *C = nullptr) { 2002 // Avoid other output when using a raw option. 2003 if (!RawClangAST) { 2004 outs() << '\n'; 2005 if (A) 2006 outs() << A->getFileName() << "(" << O->getFileName() << ")"; 2007 else 2008 outs() << O->getFileName(); 2009 outs() << ":\tfile format " << O->getFileFormatName() << "\n\n"; 2010 } 2011 2012 StringRef ArchiveName = A ? A->getFileName() : ""; 2013 if (FileHeaders) 2014 printFileHeaders(O); 2015 if (ArchiveHeaders && !MachOOpt && C) 2016 printArchiveChild(ArchiveName, *C); 2017 if (Disassemble) 2018 disassembleObject(O, Relocations); 2019 if (Relocations && !Disassemble) 2020 printRelocations(O); 2021 if (DynamicRelocations) 2022 printDynamicRelocations(O); 2023 if (SectionHeaders) 2024 printSectionHeaders(O); 2025 if (SectionContents) 2026 printSectionContents(O); 2027 if (SymbolTable) 2028 printSymbolTable(O, ArchiveName); 2029 if (UnwindInfo) 2030 printUnwindInfo(O); 2031 if (PrivateHeaders || FirstPrivateHeader) 2032 printPrivateFileHeaders(O, FirstPrivateHeader); 2033 if (ExportsTrie) 2034 printExportsTrie(O); 2035 if (Rebase) 2036 printRebaseTable(O); 2037 if (Bind) 2038 printBindTable(O); 2039 if (LazyBind) 2040 printLazyBindTable(O); 2041 if (WeakBind) 2042 printWeakBindTable(O); 2043 if (RawClangAST) 2044 printRawClangAST(O); 2045 if (PrintFaultMaps) 2046 printFaultMaps(O); 2047 if (DwarfDumpType != DIDT_Null) { 2048 std::unique_ptr<DIContext> DICtx = DWARFContext::create(*O); 2049 // Dump the complete DWARF structure. 2050 DIDumpOptions DumpOpts; 2051 DumpOpts.DumpType = DwarfDumpType; 2052 DICtx->dump(outs(), DumpOpts); 2053 } 2054 } 2055 2056 static void dumpObject(const COFFImportFile *I, const Archive *A, 2057 const Archive::Child *C = nullptr) { 2058 StringRef ArchiveName = A ? A->getFileName() : ""; 2059 2060 // Avoid other output when using a raw option. 2061 if (!RawClangAST) 2062 outs() << '\n' 2063 << ArchiveName << "(" << I->getFileName() << ")" 2064 << ":\tfile format COFF-import-file" 2065 << "\n\n"; 2066 2067 if (ArchiveHeaders && !MachOOpt && C) 2068 printArchiveChild(ArchiveName, *C); 2069 if (SymbolTable) 2070 printCOFFSymbolTable(I); 2071 } 2072 2073 /// Dump each object file in \a a; 2074 static void dumpArchive(const Archive *A) { 2075 Error Err = Error::success(); 2076 for (auto &C : A->children(Err)) { 2077 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); 2078 if (!ChildOrErr) { 2079 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 2080 report_error(A->getFileName(), C, std::move(E)); 2081 continue; 2082 } 2083 if (ObjectFile *O = dyn_cast<ObjectFile>(&*ChildOrErr.get())) 2084 dumpObject(O, A, &C); 2085 else if (COFFImportFile *I = dyn_cast<COFFImportFile>(&*ChildOrErr.get())) 2086 dumpObject(I, A, &C); 2087 else 2088 report_error(A->getFileName(), object_error::invalid_file_type); 2089 } 2090 if (Err) 2091 report_error(A->getFileName(), std::move(Err)); 2092 } 2093 2094 /// Open file and figure out how to dump it. 2095 static void dumpInput(StringRef file) { 2096 // If we are using the Mach-O specific object file parser, then let it parse 2097 // the file and process the command line options. So the -arch flags can 2098 // be used to select specific slices, etc. 2099 if (MachOOpt) { 2100 parseInputMachO(file); 2101 return; 2102 } 2103 2104 // Attempt to open the binary. 2105 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file); 2106 if (!BinaryOrErr) 2107 report_error(file, BinaryOrErr.takeError()); 2108 Binary &Binary = *BinaryOrErr.get().getBinary(); 2109 2110 if (Archive *A = dyn_cast<Archive>(&Binary)) 2111 dumpArchive(A); 2112 else if (ObjectFile *O = dyn_cast<ObjectFile>(&Binary)) 2113 dumpObject(O); 2114 else if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Binary)) 2115 parseInputMachO(UB); 2116 else 2117 report_error(file, object_error::invalid_file_type); 2118 } 2119 2120 int main(int argc, char **argv) { 2121 InitLLVM X(argc, argv); 2122 2123 // Initialize targets and assembly printers/parsers. 2124 llvm::InitializeAllTargetInfos(); 2125 llvm::InitializeAllTargetMCs(); 2126 llvm::InitializeAllDisassemblers(); 2127 2128 // Register the target printer for --version. 2129 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 2130 2131 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n"); 2132 2133 ToolName = argv[0]; 2134 2135 // Defaults to a.out if no filenames specified. 2136 if (InputFilenames.empty()) 2137 InputFilenames.push_back("a.out"); 2138 2139 if (AllHeaders) 2140 ArchiveHeaders = FileHeaders = PrivateHeaders = Relocations = 2141 SectionHeaders = SymbolTable = true; 2142 2143 if (DisassembleAll || PrintSource || PrintLines) 2144 Disassemble = true; 2145 2146 if (!Disassemble 2147 && !Relocations 2148 && !DynamicRelocations 2149 && !SectionHeaders 2150 && !SectionContents 2151 && !SymbolTable 2152 && !UnwindInfo 2153 && !PrivateHeaders 2154 && !FileHeaders 2155 && !FirstPrivateHeader 2156 && !ExportsTrie 2157 && !Rebase 2158 && !Bind 2159 && !LazyBind 2160 && !WeakBind 2161 && !RawClangAST 2162 && !(UniversalHeaders && MachOOpt) 2163 && !ArchiveHeaders 2164 && !(IndirectSymbols && MachOOpt) 2165 && !(DataInCode && MachOOpt) 2166 && !(LinkOptHints && MachOOpt) 2167 && !(InfoPlist && MachOOpt) 2168 && !(DylibsUsed && MachOOpt) 2169 && !(DylibId && MachOOpt) 2170 && !(ObjcMetaData && MachOOpt) 2171 && !(!FilterSections.empty() && MachOOpt) 2172 && !PrintFaultMaps 2173 && DwarfDumpType == DIDT_Null) { 2174 cl::PrintHelpMessage(); 2175 return 2; 2176 } 2177 2178 DisasmFuncsSet.insert(DisassembleFunctions.begin(), 2179 DisassembleFunctions.end()); 2180 2181 llvm::for_each(InputFilenames, dumpInput); 2182 2183 return EXIT_SUCCESS; 2184 } 2185