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/Compiler.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/FormattedStream.h"
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 } // namespace object
41 
42 namespace objdump {
43 
44 enum DebugVarsFormat { DVDisabled, DVUnicode, DVASCII, DVInvalid };
45 
46 extern bool ArchiveHeaders;
47 extern int DbgIndent;
48 extern DebugVarsFormat DbgVariables;
49 extern bool Demangle;
50 extern bool Disassemble;
51 extern bool DisassembleAll;
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 public:
80   Dumper(const object::ObjectFile &O) : O(O) {}
81   virtual ~Dumper() {}
82 
83   void reportUniqueWarning(Error Err);
84   void reportUniqueWarning(const Twine &Msg);
85 
86   virtual void printPrivateHeaders(bool MachOOnlyFirst);
87   virtual void printDynamicRelocations() {}
88   void printSymbolTable(StringRef ArchiveName,
89                         StringRef ArchitectureName = StringRef(),
90                         bool DumpDynamic = false);
91   void printSymbol(const object::SymbolRef &Symbol,
92                    ArrayRef<object::VersionEntry> SymbolVersions,
93                    StringRef FileName, StringRef ArchiveName,
94                    StringRef ArchitectureName, bool DumpDynamic);
95   void printRelocations();
96 };
97 
98 std::unique_ptr<Dumper> createCOFFDumper(const object::COFFObjectFile &Obj);
99 std::unique_ptr<Dumper> createELFDumper(const object::ELFObjectFileBase &Obj);
100 std::unique_ptr<Dumper> createMachODumper(const object::MachOObjectFile &Obj);
101 std::unique_ptr<Dumper> createWasmDumper(const object::WasmObjectFile &Obj);
102 std::unique_ptr<Dumper> createXCOFFDumper(const object::XCOFFObjectFile &Obj);
103 
104 // Various helper functions.
105 
106 /// Creates a SectionFilter with a standard predicate that conditionally skips
107 /// sections when the --section objdump flag is provided.
108 ///
109 /// Idx is an optional output parameter that keeps track of which section index
110 /// this is. This may be different than the actual section number, as some
111 /// sections may be filtered (e.g. symbol tables).
112 object::SectionFilter ToolSectionFilter(const llvm::object::ObjectFile &O,
113                                         uint64_t *Idx = nullptr);
114 
115 bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
116 void printSectionHeaders(object::ObjectFile &O);
117 void printSectionContents(const object::ObjectFile *O);
118 [[noreturn]] void reportError(StringRef File, const Twine &Message);
119 [[noreturn]] void reportError(Error E, StringRef FileName,
120                               StringRef ArchiveName = "",
121                               StringRef ArchitectureName = "");
122 void reportWarning(const Twine &Message, StringRef File);
123 
124 template <typename T, typename... Ts>
125 T unwrapOrError(Expected<T> EO, Ts &&... Args) {
126   if (EO)
127     return std::move(*EO);
128   reportError(EO.takeError(), std::forward<Ts>(Args)...);
129 }
130 
131 void invalidArgValue(const opt::Arg *A);
132 
133 std::string getFileNameForError(const object::Archive::Child &C,
134                                 unsigned Index);
135 SymbolInfoTy createSymbolInfo(const object::ObjectFile &Obj,
136                               const object::SymbolRef &Symbol);
137 unsigned getInstStartColumn(const MCSubtargetInfo &STI);
138 void printRawData(llvm::ArrayRef<uint8_t> Bytes, uint64_t Address,
139                   llvm::formatted_raw_ostream &OS,
140                   llvm::MCSubtargetInfo const &STI);
141 
142 } // namespace objdump
143 } // end namespace llvm
144 
145 #endif
146