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