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