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