blob: 771e94a23f49db3a61242cb94938674e15ebd74a [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
34class SectionFilterIterator {
35public:
36 SectionFilterIterator(FilterPredicate P,
37 llvm::object::section_iterator const &I,
38 llvm::object::section_iterator const &E)
39 : Predicate(std::move(P)), Iterator(I), End(E) {
40 ScanPredicate();
41 }
42 const llvm::object::SectionRef &operator*() const { return *Iterator; }
43 SectionFilterIterator &operator++() {
44 ++Iterator;
45 ScanPredicate();
46 return *this;
47 }
48 bool operator!=(SectionFilterIterator const &Other) const {
49 return Iterator != Other.Iterator;
50 }
51
52private:
53 void ScanPredicate() {
54 while (Iterator != End && !Predicate(*Iterator)) {
55 ++Iterator;
56 }
57 }
58 FilterPredicate Predicate;
59 llvm::object::section_iterator Iterator;
60 llvm::object::section_iterator End;
61};
62
63class SectionFilter {
64public:
65 SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
66 : Predicate(std::move(P)), Object(O) {}
67 SectionFilterIterator begin() {
68 return SectionFilterIterator(Predicate, Object.section_begin(),
69 Object.section_end());
70 }
71 SectionFilterIterator end() {
72 return SectionFilterIterator(Predicate, Object.section_end(),
73 Object.section_end());
74 }
75
76private:
77 FilterPredicate Predicate;
78 llvm::object::ObjectFile const &Object;
79};
80
Benjamin Kramer43a772e2011-09-19 17:56:04 +000081// Various helper functions.
George Rimarc1964882019-01-18 11:33:26 +000082SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O);
83
Fangrui Songf67de6c2019-04-08 16:24:08 +000084Error getELFRelocationValueString(const object::ELFObjectFileBase *Obj,
85 const object::RelocationRef &Rel,
86 llvm::SmallVectorImpl<char> &Result);
87Error getCOFFRelocationValueString(const object::COFFObjectFile *Obj,
88 const object::RelocationRef &Rel,
89 llvm::SmallVectorImpl<char> &Result);
90Error getWasmRelocationValueString(const object::WasmObjectFile *Obj,
91 const object::RelocationRef &RelRef,
92 llvm::SmallVectorImpl<char> &Result);
93Error getMachORelocationValueString(const object::MachOObjectFile *Obj,
94 const object::RelocationRef &RelRef,
95 llvm::SmallVectorImpl<char> &Result);
George Rimarc1964882019-01-18 11:33:26 +000096
George Rimar87fa2e62019-01-28 14:11:35 +000097uint64_t getELFSectionLMA(const object::ELFSectionRef& Sec);
98
George Rimar73a27232019-01-15 09:19:18 +000099bool isRelocAddressLess(object::RelocationRef A, object::RelocationRef B);
100void parseInputMachO(StringRef Filename);
101void parseInputMachO(object::MachOUniversalBinary *UB);
102void printCOFFUnwindInfo(const object::COFFObjectFile *O);
103void printMachOUnwindInfo(const object::MachOObjectFile *O);
104void printMachOExportsTrie(const object::MachOObjectFile *O);
105void printMachORebaseTable(object::MachOObjectFile *O);
106void printMachOBindTable(object::MachOObjectFile *O);
107void printMachOLazyBindTable(object::MachOObjectFile *O);
108void printMachOWeakBindTable(object::MachOObjectFile *O);
109void printELFFileHeader(const object::ObjectFile *O);
Paul Semel0913dcd2018-07-25 11:09:20 +0000110void printELFDynamicSection(const object::ObjectFile *Obj);
Xing GUO56d651d2019-02-25 13:13:19 +0000111void printELFSymbolVersionInfo(const object::ObjectFile *Obj);
George Rimar73a27232019-01-15 09:19:18 +0000112void printCOFFFileHeader(const object::ObjectFile *O);
113void printCOFFSymbolTable(const object::COFFImportFile *I);
114void printCOFFSymbolTable(const object::COFFObjectFile *O);
115void printMachOFileHeader(const object::ObjectFile *O);
116void printMachOLoadCommands(const object::ObjectFile *O);
117void printWasmFileHeader(const object::ObjectFile *O);
118void printExportsTrie(const object::ObjectFile *O);
119void printRebaseTable(object::ObjectFile *O);
120void printBindTable(object::ObjectFile *O);
121void printLazyBindTable(object::ObjectFile *O);
122void printWeakBindTable(object::ObjectFile *O);
123void printRawClangAST(const object::ObjectFile *O);
124void printRelocations(const object::ObjectFile *O);
125void printDynamicRelocations(const object::ObjectFile *O);
126void printSectionHeaders(const object::ObjectFile *O);
127void printSectionContents(const object::ObjectFile *O);
128void printSymbolTable(const object::ObjectFile *O, StringRef ArchiveName,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000129 StringRef ArchitectureName = StringRef());
George Rimarf0f38d92019-08-21 11:07:31 +0000130LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Twine Message);
131LLVM_ATTRIBUTE_NORETURN void reportError(Error E, StringRef File);
Fangrui Songe7834bd2019-04-07 08:19:55 +0000132LLVM_ATTRIBUTE_NORETURN void
George Rimarf0f38d92019-08-21 11:07:31 +0000133reportError(Error E, StringRef FileName, StringRef ArchiveName,
134 StringRef ArchitectureName = StringRef());
135void reportWarning(Twine Message, StringRef File);
Fangrui Songe7834bd2019-04-07 08:19:55 +0000136
137template <typename T, typename... Ts>
138T unwrapOrError(Expected<T> EO, Ts &&... Args) {
139 if (EO)
140 return std::move(*EO);
George Rimarf0f38d92019-08-21 11:07:31 +0000141 reportError(EO.takeError(), std::forward<Ts>(Args)...);
Fangrui Songe7834bd2019-04-07 08:19:55 +0000142}
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000143
George Rimarb232d562019-08-20 13:19:16 +0000144std::string getFileNameForError(const object::Archive::Child &C,
145 unsigned Index);
146
Rui Ueyamac2bed422013-09-27 21:04:00 +0000147} // end namespace llvm
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000148
149#endif