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 bool PrintImmHex;
70extern bool PrintLines;
71extern bool PrintSource;
72extern bool PrivateHeaders;
73extern bool Relocations;
74extern bool SectionHeaders;
75extern bool SectionContents;
76extern bool ShowRawInsn;
77extern bool SymbolDescription;
78extern bool TracebackTable;
79extern bool SymbolTable;
80extern std::string TripleName;
81extern bool UnwindInfo;
82
83extern StringSet<> FoundSectionSet;
84
85class Dumper {
86 const object::ObjectFile &O;
87 StringSet<> Warnings;
88
89protected:
90 llvm::raw_ostream &OS;
91 std::function<Error(const Twine &Msg)> WarningHandler;
92
93public:
94 Dumper(const object::ObjectFile &O);
95 virtual ~Dumper() = default;
96
97 void reportUniqueWarning(Error Err);
98 void reportUniqueWarning(const Twine &Msg);
99
100 virtual void printPrivateHeaders();
101 virtual void printDynamicRelocations() {}
102 void printSymbolTable(StringRef ArchiveName,
103 StringRef ArchitectureName = StringRef(),
104 bool DumpDynamic = false);
105 void printSymbol(const object::SymbolRef &Symbol,
106 ArrayRef<object::VersionEntry> SymbolVersions,
107 StringRef FileName, StringRef ArchiveName,
108 StringRef ArchitectureName, bool DumpDynamic);
109 void printRelocations();
110};
111
112std::unique_ptr<Dumper> createCOFFDumper(const object::COFFObjectFile &Obj);
113std::unique_ptr<Dumper> createELFDumper(const object::ELFObjectFileBase &Obj);
114std::unique_ptr<Dumper> createMachODumper(const object::MachOObjectFile &Obj);
115std::unique_ptr<Dumper> createWasmDumper(const object::WasmObjectFile &Obj);
116std::unique_ptr<Dumper> createXCOFFDumper(const object::XCOFFObjectFile &Obj);
117std::unique_ptr<Dumper>
118createDXContainerDumper(const object::DXContainerObjectFile &Obj);
119
120// Various helper functions.
121
122/// Creates a SectionFilter with a standard predicate that conditionally skips
123/// sections when the --section objdump flag is provided.
124///
125/// Idx is an optional output parameter that keeps track of which section index
126/// this is. This may be different than the actual section number, as some
127/// sections may be filtered (e.g. symbol tables).
128object::SectionFilter ToolSectionFilter(const llvm::object::ObjectFile &O,
129 uint64_t *Idx = nullptr);
130
131bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
132void printSectionHeaders(object::ObjectFile &O);
133void printSectionContents(const object::ObjectFile *O);
134[[noreturn]] void reportError(StringRef File, const Twine &Message);
135[[noreturn]] void reportError(Error E, StringRef FileName,
136 StringRef ArchiveName = "",
137 StringRef ArchitectureName = "");
138void reportWarning(const Twine &Message, StringRef File);
139
140template <typename T, typename... Ts>
141T unwrapOrError(Expected<T> EO, Ts &&...Args) {
142 if (EO)
143 return std::move(*EO);
144 reportError(EO.takeError(), std::forward<Ts>(Args)...);
145}
146
147void invalidArgValue(const opt::Arg *A);
148
149std::string getFileNameForError(const object::Archive::Child &C,
150 unsigned Index);
151SymbolInfoTy createSymbolInfo(const object::ObjectFile &Obj,
152 const object::SymbolRef &Symbol,
153 bool IsMappingSymbol = false);
154unsigned getInstStartColumn(const MCSubtargetInfo &STI);
155void printRawData(llvm::ArrayRef<uint8_t> Bytes, uint64_t Address,
156 llvm::formatted_raw_ostream &OS,
157 llvm::MCSubtargetInfo const &STI);
158
159} // namespace objdump
160} // end namespace llvm
161
162#endif
163