xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-objdump/llvm-objdump.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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   llvm::raw_ostream &OS;
81   std::function<Error(const Twine &Msg)> WarningHandler;
82 
83 public:
84   Dumper(const object::ObjectFile &O);
~Dumper()85   virtual ~Dumper() {}
86 
87   void reportUniqueWarning(Error Err);
88   void reportUniqueWarning(const Twine &Msg);
89 
90   virtual void printPrivateHeaders();
printDynamicRelocations()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>
unwrapOrError(Expected<T> EO,Ts &&...Args)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