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