llvm-objdump.h revision 360784
1//
2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3// See https://llvm.org/LICENSE.txt for license information.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
8#ifndef LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
9#define LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
10
11#include "llvm/DebugInfo/DIContext.h"
12#include "llvm/Support/CommandLine.h"
13#include "llvm/Support/Compiler.h"
14#include "llvm/Support/DataTypes.h"
15#include "llvm/Object/Archive.h"
16
17namespace llvm {
18class StringRef;
19
20namespace object {
21class COFFObjectFile;
22class COFFImportFile;
23class ELFObjectFileBase;
24class ELFSectionRef;
25class MachOObjectFile;
26class MachOUniversalBinary;
27class RelocationRef;
28}
29
30extern cl::opt<bool> Demangle;
31
32typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
33
34/// A filtered iterator for SectionRefs that skips sections based on some given
35/// predicate.
36class SectionFilterIterator {
37public:
38  SectionFilterIterator(FilterPredicate P,
39                        llvm::object::section_iterator const &I,
40                        llvm::object::section_iterator const &E)
41      : Predicate(std::move(P)), Iterator(I), End(E) {
42    ScanPredicate();
43  }
44  const llvm::object::SectionRef &operator*() const { return *Iterator; }
45  SectionFilterIterator &operator++() {
46    ++Iterator;
47    ScanPredicate();
48    return *this;
49  }
50  bool operator!=(SectionFilterIterator const &Other) const {
51    return Iterator != Other.Iterator;
52  }
53
54private:
55  void ScanPredicate() {
56    while (Iterator != End && !Predicate(*Iterator)) {
57      ++Iterator;
58    }
59  }
60  FilterPredicate Predicate;
61  llvm::object::section_iterator Iterator;
62  llvm::object::section_iterator End;
63};
64
65/// Creates an iterator range of SectionFilterIterators for a given Object and
66/// predicate.
67class SectionFilter {
68public:
69  SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
70      : Predicate(std::move(P)), Object(O) {}
71  SectionFilterIterator begin() {
72    return SectionFilterIterator(Predicate, Object.section_begin(),
73                                 Object.section_end());
74  }
75  SectionFilterIterator end() {
76    return SectionFilterIterator(Predicate, Object.section_end(),
77                                 Object.section_end());
78  }
79
80private:
81  FilterPredicate Predicate;
82  llvm::object::ObjectFile const &Object;
83};
84
85// Various helper functions.
86
87/// Creates a SectionFilter with a standard predicate that conditionally skips
88/// sections when the --section objdump flag is provided.
89///
90/// Idx is an optional output parameter that keeps track of which section index
91/// this is. This may be different than the actual section number, as some
92/// sections may be filtered (e.g. symbol tables).
93SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O,
94                                uint64_t *Idx = nullptr);
95
96Error getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
97                                  const object::RelocationRef &Rel,
98                                  llvm::SmallVectorImpl<char> &Result);
99Error getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
100                                   const object::RelocationRef &Rel,
101                                   llvm::SmallVectorImpl<char> &Result);
102Error getWasmRelocationValueString(const object::WasmObjectFile *Obj,
103                                   const object::RelocationRef &RelRef,
104                                   llvm::SmallVectorImpl<char> &Result);
105Error getMachORelocationValueString(const object::MachOObjectFile *Obj,
106                                    const object::RelocationRef &RelRef,
107                                    llvm::SmallVectorImpl<char> &Result);
108
109uint64_t getELFSectionLMA(const object::ELFSectionRef& Sec);
110
111bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
112void parseInputMachO(StringRef Filename);
113void parseInputMachO(object::MachOUniversalBinary *UB);
114void printCOFFUnwindInfo(const object::COFFObjectFile *O);
115void printMachOUnwindInfo(const object::MachOObjectFile *O);
116void printMachOExportsTrie(const object::MachOObjectFile *O);
117void printMachORebaseTable(object::MachOObjectFile *O);
118void printMachOBindTable(object::MachOObjectFile *O);
119void printMachOLazyBindTable(object::MachOObjectFile *O);
120void printMachOWeakBindTable(object::MachOObjectFile *O);
121void printELFFileHeader(const object::ObjectFile *O);
122void printELFDynamicSection(const object::ObjectFile *Obj);
123void printELFSymbolVersionInfo(const object::ObjectFile *Obj);
124void printCOFFFileHeader(const object::ObjectFile *O);
125void printCOFFSymbolTable(const object::COFFImportFile *I);
126void printCOFFSymbolTable(const object::COFFObjectFile *O);
127void printMachOFileHeader(const object::ObjectFile *O);
128void printMachOLoadCommands(const object::ObjectFile *O);
129void printWasmFileHeader(const object::ObjectFile *O);
130void printExportsTrie(const object::ObjectFile *O);
131void printRebaseTable(object::ObjectFile *O);
132void printBindTable(object::ObjectFile *O);
133void printLazyBindTable(object::ObjectFile *O);
134void printWeakBindTable(object::ObjectFile *O);
135void printRawClangAST(const object::ObjectFile *O);
136void printRelocations(const object::ObjectFile *O);
137void printDynamicRelocations(const object::ObjectFile *O);
138void printSectionHeaders(const object::ObjectFile *O);
139void printSectionContents(const object::ObjectFile *O);
140void printSymbolTable(const object::ObjectFile *O, StringRef ArchiveName,
141                      StringRef ArchitectureName = StringRef());
142LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Twine Message);
143LLVM_ATTRIBUTE_NORETURN void reportError(Error E, StringRef FileName,
144                                         StringRef ArchiveName = "",
145                                         StringRef ArchitectureName = "");
146void reportWarning(Twine Message, StringRef File);
147
148template <typename T, typename... Ts>
149T unwrapOrError(Expected<T> EO, Ts &&... Args) {
150  if (EO)
151    return std::move(*EO);
152  reportError(EO.takeError(), std::forward<Ts>(Args)...);
153}
154
155std::string getFileNameForError(const object::Archive::Child &C,
156                                unsigned Index);
157
158} // end namespace llvm
159
160#endif
161