blob: 43ce02ae0bc27e48b09445667356a73a74eda275 [file] [log] [blame]
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001//
Chandler Carruth2946cd72019-01-19 08:50:56 +00002// 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
Benjamin Kramer43a772e2011-09-19 17:56:04 +00005//
6//===----------------------------------------------------------------------===//
7
Benjamin Kramera7c40ef2014-08-13 16:26:38 +00008#ifndef LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
9#define LLVM_TOOLS_LLVM_OBJDUMP_LLVM_OBJDUMP_H
Benjamin Kramer43a772e2011-09-19 17:56:04 +000010
Igor Laevsky03a670c2016-01-26 15:09:42 +000011#include "llvm/DebugInfo/DIContext.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000012#include "llvm/Support/CommandLine.h"
Davide Italianoed9d95b2015-12-29 13:41:02 +000013#include "llvm/Support/Compiler.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000014#include "llvm/Support/DataTypes.h"
Kevin Enderbyac9e1552016-05-17 17:10:12 +000015#include "llvm/Object/Archive.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000016
17namespace llvm {
Mehdi Aminib550cb12016-04-18 09:17:29 +000018class StringRef;
19
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000020namespace object {
George Rimarc1964882019-01-18 11:33:26 +000021class COFFObjectFile;
22class COFFImportFile;
23class ELFObjectFileBase;
George Rimar87fa2e62019-01-28 14:11:35 +000024class ELFSectionRef;
George Rimarc1964882019-01-18 11:33:26 +000025class MachOObjectFile;
26class MachOUniversalBinary;
George Rimarc1964882019-01-18 11:33:26 +000027class RelocationRef;
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000028}
Michael J. Spencer0c6ec482012-12-05 20:12:35 +000029
Zachary Turner030ad372018-08-20 22:18:21 +000030extern cl::opt<bool> Demangle;
Benjamin Kramer43a772e2011-09-19 17:56:04 +000031
George Rimarc1964882019-01-18 11:33:26 +000032typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
33
Jordan Rupprecht98a2ae72019-10-17 21:55:43 +000034/// A filtered iterator for SectionRefs that skips sections based on some given
35/// predicate.
George Rimarc1964882019-01-18 11:33:26 +000036class 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
Jordan Rupprecht98a2ae72019-10-17 21:55:43 +000065/// Creates an iterator range of SectionFilterIterators for a given Object and
66/// predicate.
George Rimarc1964882019-01-18 11:33:26 +000067class 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
Benjamin Kramer43a772e2011-09-19 17:56:04 +000085// Various helper functions.
Jordan Rupprecht98a2ae72019-10-17 21:55:43 +000086
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);
George Rimarc1964882019-01-18 11:33:26 +000095
Fangrui Songf67de6c2019-04-08 16:24:08 +000096Error 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);
George Rimarc1964882019-01-18 11:33:26 +0000108
George Rimar87fa2e62019-01-28 14:11:35 +0000109uint64_t getELFSectionLMA(const object::ELFSectionRef& Sec);
110
George Rimar73a27232019-01-15 09:19:18 +0000111bool 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);
Paul Semel0913dcd2018-07-25 11:09:20 +0000122void printELFDynamicSection(const object::ObjectFile *Obj);
Xing GUO56d651d2019-02-25 13:13:19 +0000123void printELFSymbolVersionInfo(const object::ObjectFile *Obj);
George Rimar73a27232019-01-15 09:19:18 +0000124void 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,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000141 StringRef ArchitectureName = StringRef());
George Rimarf0f38d92019-08-21 11:07:31 +0000142LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Twine Message);
George Rimardd591bd2019-08-27 10:03:45 +0000143LLVM_ATTRIBUTE_NORETURN void reportError(Error E, StringRef FileName,
144 StringRef ArchiveName = "",
145 StringRef ArchitectureName = "");
George Rimarf0f38d92019-08-21 11:07:31 +0000146void reportWarning(Twine Message, StringRef File);
Fangrui Songe7834bd2019-04-07 08:19:55 +0000147
148template <typename T, typename... Ts>
149T unwrapOrError(Expected<T> EO, Ts &&... Args) {
150 if (EO)
151 return std::move(*EO);
George Rimarf0f38d92019-08-21 11:07:31 +0000152 reportError(EO.takeError(), std::forward<Ts>(Args)...);
Fangrui Songe7834bd2019-04-07 08:19:55 +0000153}
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000154
George Rimarb232d562019-08-20 13:19:16 +0000155std::string getFileNameForError(const object::Archive::Child &C,
156 unsigned Index);
157
Rui Ueyamac2bed422013-09-27 21:04:00 +0000158} // end namespace llvm
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000159
160#endif