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