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