| 1 | //===--- llvm-objdump.h -----------------------------------------*- C++ -*-===// |
| 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 | #ifndef LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H |
| 10 | #define LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H |
| 11 | |
| 12 | #include "llvm/ADT/StringSet.h" |
| 13 | #include "llvm/DebugInfo/DIContext.h" |
| 14 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 15 | #include "llvm/MC/MCSubtargetInfo.h" |
| 16 | #include "llvm/Object/Archive.h" |
| 17 | #include "llvm/Object/ObjectFile.h" |
| 18 | #include "llvm/Support/FormattedStream.h" |
| 19 | #include "llvm/Support/LibraryOptionsContext.h" |
| 20 | #include <functional> |
| 21 | #include <memory> |
| 22 | |
| 23 | namespace llvm { |
| 24 | class StringRef; |
| 25 | class Twine; |
| 26 | |
| 27 | namespace opt { |
| 28 | class Arg; |
| 29 | } // namespace opt |
| 30 | |
| 31 | namespace object { |
| 32 | class RelocationRef; |
| 33 | struct VersionEntry; |
| 34 | |
| 35 | class COFFObjectFile; |
| 36 | class ELFObjectFileBase; |
| 37 | class MachOObjectFile; |
| 38 | class WasmObjectFile; |
| 39 | class XCOFFObjectFile; |
| 40 | class DXContainer; |
| 41 | } // namespace object |
| 42 | |
| 43 | namespace objdump { |
| 44 | |
| 45 | enum DebugFormat { DFASCII, DFDisabled, DFInvalid, DFLimitsOnly, DFUnicode }; |
| 46 | |
| 47 | enum class ColorOutput { |
| 48 | Auto, |
| 49 | Enable, |
| 50 | Disable, |
| 51 | Invalid, |
| 52 | }; |
| 53 | |
| 54 | extern bool ; |
| 55 | extern int DbgIndent; |
| 56 | extern DebugFormat DbgVariables; |
| 57 | extern DebugFormat DbgInlinedFunctions; |
| 58 | extern bool Demangle; |
| 59 | extern bool Disassemble; |
| 60 | extern bool DisassembleAll; |
| 61 | extern std::vector<std::string> DisassemblerOptions; |
| 62 | extern ColorOutput DisassemblyColor; |
| 63 | extern DIDumpType DwarfDumpType; |
| 64 | extern std::vector<std::string> FilterSections; |
| 65 | extern bool LeadingAddr; |
| 66 | extern cl::LibraryOptionsContext LibraryOptions; |
| 67 | extern std::vector<std::string> MAttrs; |
| 68 | extern std::string MCPU; |
| 69 | extern std::string Prefix; |
| 70 | extern uint32_t PrefixStrip; |
| 71 | extern std::vector<std::string> SourceDirs; |
| 72 | extern std::vector<std::pair<std::string, std::string>> SubstitutePaths; |
| 73 | extern bool PrintImmHex; |
| 74 | extern bool PrintLines; |
| 75 | extern bool PrintSource; |
| 76 | extern bool ; |
| 77 | extern bool Relocations; |
| 78 | extern bool ; |
| 79 | extern bool SectionContents; |
| 80 | extern bool ShowRawInsn; |
| 81 | extern bool SymbolDescription; |
| 82 | extern bool TracebackTable; |
| 83 | extern bool SymbolTable; |
| 84 | extern std::string TripleName; |
| 85 | extern bool UnwindInfo; |
| 86 | extern bool UnwindShowWODPool; |
| 87 | |
| 88 | extern StringSet<> FoundSectionSet; |
| 89 | |
| 90 | class Dumper { |
| 91 | const object::ObjectFile &O; |
| 92 | StringSet<> Warnings; |
| 93 | |
| 94 | protected: |
| 95 | llvm::raw_ostream &OS; |
| 96 | std::function<Error(const Twine &Msg)> WarningHandler; |
| 97 | |
| 98 | public: |
| 99 | Dumper(const object::ObjectFile &O); |
| 100 | virtual ~Dumper() = default; |
| 101 | |
| 102 | void reportUniqueWarning(Error Err); |
| 103 | void reportUniqueWarning(const Twine &Msg); |
| 104 | |
| 105 | virtual void (); |
| 106 | virtual void printDynamicRelocations() {} |
| 107 | void printSymbolTable(StringRef ArchiveName, |
| 108 | StringRef ArchitectureName = StringRef(), |
| 109 | bool DumpDynamic = false); |
| 110 | void printSymbol(const object::SymbolRef &Symbol, |
| 111 | ArrayRef<object::VersionEntry> SymbolVersions, |
| 112 | StringRef FileName, StringRef ArchiveName, |
| 113 | StringRef ArchitectureName, bool DumpDynamic); |
| 114 | void printRelocations(); |
| 115 | }; |
| 116 | |
| 117 | std::unique_ptr<Dumper> createCOFFDumper(const object::COFFObjectFile &Obj); |
| 118 | std::unique_ptr<Dumper> createELFDumper(const object::ELFObjectFileBase &Obj); |
| 119 | std::unique_ptr<Dumper> createMachODumper(const object::MachOObjectFile &Obj); |
| 120 | std::unique_ptr<Dumper> createWasmDumper(const object::WasmObjectFile &Obj); |
| 121 | std::unique_ptr<Dumper> createXCOFFDumper(const object::XCOFFObjectFile &Obj); |
| 122 | std::unique_ptr<Dumper> |
| 123 | createDXContainerDumper(const object::DXContainerObjectFile &Obj); |
| 124 | |
| 125 | // Various helper functions. |
| 126 | |
| 127 | /// Creates a SectionFilter with a standard predicate that conditionally skips |
| 128 | /// sections when the --section objdump flag is provided. |
| 129 | /// |
| 130 | /// Idx is an optional output parameter that keeps track of which section index |
| 131 | /// this is. This may be different than the actual section number, as some |
| 132 | /// sections may be filtered (e.g. symbol tables). |
| 133 | object::SectionFilter ToolSectionFilter(const llvm::object::ObjectFile &O, |
| 134 | uint64_t *Idx = nullptr); |
| 135 | |
| 136 | bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B); |
| 137 | void (object::ObjectFile &O); |
| 138 | void printSectionContents(const object::ObjectFile *O); |
| 139 | [[noreturn]] void reportError(StringRef File, const Twine &Message); |
| 140 | [[noreturn]] void reportError(Error E, StringRef FileName, |
| 141 | StringRef ArchiveName = "" , |
| 142 | StringRef ArchitectureName = "" ); |
| 143 | void reportWarning(const Twine &Message, StringRef File); |
| 144 | |
| 145 | template <typename T, typename... Ts> |
| 146 | T unwrapOrError(Expected<T> EO, Ts &&...Args) { |
| 147 | if (EO) |
| 148 | return std::move(*EO); |
| 149 | reportError(EO.takeError(), std::forward<Ts>(Args)...); |
| 150 | } |
| 151 | |
| 152 | void invalidArgValue(const opt::Arg *A); |
| 153 | |
| 154 | std::string getFileNameForError(const object::Archive::Child &C, |
| 155 | unsigned Index); |
| 156 | SymbolInfoTy createSymbolInfo(const object::ObjectFile &Obj, |
| 157 | const object::SymbolRef &Symbol, |
| 158 | bool IsMappingSymbol = false); |
| 159 | unsigned getInstStartColumn(const MCSubtargetInfo &STI); |
| 160 | void printRawData(llvm::ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 161 | llvm::formatted_raw_ostream &OS, |
| 162 | llvm::MCSubtargetInfo const &STI); |
| 163 | |
| 164 | } // namespace objdump |
| 165 | } // end namespace llvm |
| 166 | |
| 167 | #endif |
| 168 | |