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