xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-readobj/ObjDumper.h (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1 //===-- ObjDumper.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_READOBJ_OBJDUMPER_H
10 #define LLVM_TOOLS_LLVM_READOBJ_OBJDUMPER_H
11 
12 #include <functional>
13 #include <memory>
14 #include <system_error>
15 
16 #include "llvm/ADT/STLFunctionalExtras.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/CommandLine.h"
22 
23 #include <unordered_set>
24 
25 namespace llvm {
26 namespace object {
27 class Archive;
28 class COFFImportFile;
29 class ObjectFile;
30 class XCOFFObjectFile;
31 class ELFObjectFileBase;
32 } // namespace object
33 namespace codeview {
34 class GlobalTypeTableBuilder;
35 class MergingTypeTableBuilder;
36 } // namespace codeview
37 
38 class ScopedPrinter;
39 
40 // Comparator to compare symbols.
41 // Usage: the caller registers predicates (i.e., how to compare the symbols) by
42 // calling addPredicate(). The order in which predicates are registered is also
43 // their priority.
44 class SymbolComparator {
45 public:
46   using CompPredicate =
47       std::function<bool(object::SymbolRef, object::SymbolRef)>;
48 
49   // Each Obj format has a slightly different way of retrieving a symbol's info
50   // So we defer the predicate's impl to each format.
51   void addPredicate(CompPredicate Pred) { Predicates.push_back(Pred); }
52 
53   bool operator()(object::SymbolRef LHS, object::SymbolRef RHS) {
54     for (CompPredicate Pred : Predicates) {
55       if (Pred(LHS, RHS))
56         return true;
57       if (Pred(RHS, LHS))
58         return false;
59     }
60     return false;
61   }
62 
63 private:
64   SmallVector<CompPredicate, 2> Predicates;
65 };
66 
67 class ObjDumper {
68 public:
69   ObjDumper(ScopedPrinter &Writer, StringRef ObjName);
70   virtual ~ObjDumper();
71 
72   virtual bool canDumpContent() { return true; }
73 
74   virtual void printFileSummary(StringRef FileStr, object::ObjectFile &Obj,
75                                 ArrayRef<std::string> InputFilenames,
76                                 const object::Archive *A);
77   virtual void printFileHeaders() = 0;
78   virtual void printSectionHeaders() = 0;
79   virtual void printRelocations() = 0;
80   virtual void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) {
81     if (PrintSymbols)
82       printSymbols();
83     if (PrintDynamicSymbols)
84       printDynamicSymbols();
85   }
86   virtual void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols,
87                             std::optional<SymbolComparator> SymComp) {
88     if (SymComp) {
89       if (PrintSymbols)
90         printSymbols(SymComp);
91       if (PrintDynamicSymbols)
92         printDynamicSymbols(SymComp);
93     } else {
94       printSymbols(PrintSymbols, PrintDynamicSymbols);
95     }
96   }
97   virtual void printProgramHeaders(bool PrintProgramHeaders,
98                                    cl::boolOrDefault PrintSectionMapping) {
99     if (PrintProgramHeaders)
100       printProgramHeaders();
101     if (PrintSectionMapping == cl::BOU_TRUE)
102       printSectionMapping();
103   }
104 
105   virtual void printUnwindInfo() = 0;
106 
107   // Symbol comparison functions.
108   virtual bool canCompareSymbols() const { return false; }
109   virtual bool compareSymbolsByName(object::SymbolRef LHS,
110                                     object::SymbolRef RHS) const {
111     return true;
112   }
113   virtual bool compareSymbolsByType(object::SymbolRef LHS,
114                                     object::SymbolRef RHS) const {
115     return true;
116   }
117 
118   // Only implemented for ELF at this time.
119   virtual void printDependentLibs() {}
120   virtual void printDynamicRelocations() { }
121   virtual void printDynamicTable() { }
122   virtual void printNeededLibraries() { }
123   virtual void printSectionAsHex(StringRef SectionName) {}
124   virtual void printHashTable() { }
125   virtual void printGnuHashTable() {}
126   virtual void printHashSymbols() {}
127   virtual void printLoadName() {}
128   virtual void printVersionInfo() {}
129   virtual void printGroupSections() {}
130   virtual void printHashHistograms() {}
131   virtual void printCGProfile() {}
132   virtual void printBBAddrMaps() {}
133   virtual void printAddrsig() {}
134   virtual void printNotes() {}
135   virtual void printELFLinkerOptions() {}
136   virtual void printStackSizes() {}
137   virtual void printSectionDetails() {}
138   virtual void printArchSpecificInfo() {}
139 
140   // Only implemented for PE/COFF.
141   virtual void printCOFFImports() { }
142   virtual void printCOFFExports() { }
143   virtual void printCOFFDirectives() { }
144   virtual void printCOFFBaseReloc() { }
145   virtual void printCOFFDebugDirectory() { }
146   virtual void printCOFFTLSDirectory() {}
147   virtual void printCOFFResources() {}
148   virtual void printCOFFLoadConfig() { }
149   virtual void printCodeViewDebugInfo() { }
150   virtual void
151   mergeCodeViewTypes(llvm::codeview::MergingTypeTableBuilder &CVIDs,
152                      llvm::codeview::MergingTypeTableBuilder &CVTypes,
153                      llvm::codeview::GlobalTypeTableBuilder &GlobalCVIDs,
154                      llvm::codeview::GlobalTypeTableBuilder &GlobalCVTypes,
155                      bool GHash) {}
156 
157   // Only implemented for XCOFF.
158   virtual void printStringTable() {}
159   virtual void printAuxiliaryHeader() {}
160   virtual void printExceptionSection() {}
161   virtual void printLoaderSection(bool PrintHeader, bool PrintSymbols,
162                                   bool PrintRelocations) {}
163 
164   // Only implemented for MachO.
165   virtual void printMachODataInCode() { }
166   virtual void printMachOVersionMin() { }
167   virtual void printMachODysymtab() { }
168   virtual void printMachOSegment() { }
169   virtual void printMachOIndirectSymbols() { }
170   virtual void printMachOLinkerOptions() { }
171 
172   virtual void printStackMap() const = 0;
173 
174   void printAsStringList(StringRef StringContent, size_t StringDataOffset = 0);
175 
176   void printSectionsAsString(const object::ObjectFile &Obj,
177                              ArrayRef<std::string> Sections);
178   void printSectionsAsHex(const object::ObjectFile &Obj,
179                           ArrayRef<std::string> Sections);
180 
181   std::function<Error(const Twine &Msg)> WarningHandler;
182   void reportUniqueWarning(Error Err) const;
183   void reportUniqueWarning(const Twine &Msg) const;
184 
185 protected:
186   ScopedPrinter &W;
187 
188 private:
189   virtual void printSymbols() {}
190   virtual void printSymbols(std::optional<SymbolComparator> Comp) {}
191   virtual void printDynamicSymbols() {}
192   virtual void printDynamicSymbols(std::optional<SymbolComparator> Comp) {}
193   virtual void printProgramHeaders() {}
194   virtual void printSectionMapping() {}
195 
196   std::unordered_set<std::string> Warnings;
197 };
198 
199 std::unique_ptr<ObjDumper> createCOFFDumper(const object::COFFObjectFile &Obj,
200                                             ScopedPrinter &Writer);
201 
202 std::unique_ptr<ObjDumper> createELFDumper(const object::ELFObjectFileBase &Obj,
203                                            ScopedPrinter &Writer);
204 
205 std::unique_ptr<ObjDumper> createMachODumper(const object::MachOObjectFile &Obj,
206                                              ScopedPrinter &Writer);
207 
208 std::unique_ptr<ObjDumper> createWasmDumper(const object::WasmObjectFile &Obj,
209                                             ScopedPrinter &Writer);
210 
211 std::unique_ptr<ObjDumper> createXCOFFDumper(const object::XCOFFObjectFile &Obj,
212                                              ScopedPrinter &Writer);
213 
214 void dumpCOFFImportFile(const object::COFFImportFile *File,
215                         ScopedPrinter &Writer);
216 
217 void dumpCodeViewMergedTypes(ScopedPrinter &Writer,
218                              ArrayRef<ArrayRef<uint8_t>> IpiRecords,
219                              ArrayRef<ArrayRef<uint8_t>> TpiRecords);
220 
221 } // namespace llvm
222 
223 #endif
224