xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-readobj/ObjDumper.h (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
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 <memory>
13 #include <system_error>
14 
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Object/ObjectFile.h"
17 #include "llvm/Support/CommandLine.h"
18 
19 #include <unordered_set>
20 
21 namespace llvm {
22 namespace object {
23 class Archive;
24 class COFFImportFile;
25 class ObjectFile;
26 class XCOFFObjectFile;
27 class ELFObjectFileBase;
28 }
29 namespace codeview {
30 class GlobalTypeTableBuilder;
31 class MergingTypeTableBuilder;
32 } // namespace codeview
33 
34 class ScopedPrinter;
35 
36 class ObjDumper {
37 public:
38   ObjDumper(ScopedPrinter &Writer, StringRef ObjName);
39   virtual ~ObjDumper();
40 
41   virtual bool canDumpContent() { return true; }
42 
43   virtual void printFileSummary(StringRef FileStr, object::ObjectFile &Obj,
44                                 ArrayRef<std::string> InputFilenames,
45                                 const object::Archive *A);
46   virtual void printFileHeaders() = 0;
47   virtual void printSectionHeaders() = 0;
48   virtual void printRelocations() = 0;
49   virtual void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) {
50     if (PrintSymbols)
51       printSymbols();
52     if (PrintDynamicSymbols)
53       printDynamicSymbols();
54   }
55   virtual void printProgramHeaders(bool PrintProgramHeaders,
56                                    cl::boolOrDefault PrintSectionMapping) {
57     if (PrintProgramHeaders)
58       printProgramHeaders();
59     if (PrintSectionMapping == cl::BOU_TRUE)
60       printSectionMapping();
61   }
62 
63   virtual void printUnwindInfo() = 0;
64 
65   // Only implemented for ELF at this time.
66   virtual void printDependentLibs() {}
67   virtual void printDynamicRelocations() { }
68   virtual void printDynamicTable() { }
69   virtual void printNeededLibraries() { }
70   virtual void printSectionAsHex(StringRef SectionName) {}
71   virtual void printHashTable() { }
72   virtual void printGnuHashTable() {}
73   virtual void printHashSymbols() {}
74   virtual void printLoadName() {}
75   virtual void printVersionInfo() {}
76   virtual void printGroupSections() {}
77   virtual void printHashHistograms() {}
78   virtual void printCGProfile() {}
79   virtual void printBBAddrMaps() {}
80   virtual void printAddrsig() {}
81   virtual void printNotes() {}
82   virtual void printELFLinkerOptions() {}
83   virtual void printStackSizes() {}
84   virtual void printSectionDetails() {}
85   virtual void printArchSpecificInfo() {}
86 
87   // Only implemented for PE/COFF.
88   virtual void printCOFFImports() { }
89   virtual void printCOFFExports() { }
90   virtual void printCOFFDirectives() { }
91   virtual void printCOFFBaseReloc() { }
92   virtual void printCOFFDebugDirectory() { }
93   virtual void printCOFFTLSDirectory() {}
94   virtual void printCOFFResources() {}
95   virtual void printCOFFLoadConfig() { }
96   virtual void printCodeViewDebugInfo() { }
97   virtual void
98   mergeCodeViewTypes(llvm::codeview::MergingTypeTableBuilder &CVIDs,
99                      llvm::codeview::MergingTypeTableBuilder &CVTypes,
100                      llvm::codeview::GlobalTypeTableBuilder &GlobalCVIDs,
101                      llvm::codeview::GlobalTypeTableBuilder &GlobalCVTypes,
102                      bool GHash) {}
103 
104   // Only implement for XCOFF
105   virtual void printAuxiliaryHeader() {}
106 
107   // Only implemented for MachO.
108   virtual void printMachODataInCode() { }
109   virtual void printMachOVersionMin() { }
110   virtual void printMachODysymtab() { }
111   virtual void printMachOSegment() { }
112   virtual void printMachOIndirectSymbols() { }
113   virtual void printMachOLinkerOptions() { }
114 
115   // Currently only implemented for XCOFF.
116   virtual void printStringTable() { }
117 
118   virtual void printStackMap() const = 0;
119 
120   void printAsStringList(StringRef StringContent, size_t StringDataOffset = 0);
121 
122   void printSectionsAsString(const object::ObjectFile &Obj,
123                              ArrayRef<std::string> Sections);
124   void printSectionsAsHex(const object::ObjectFile &Obj,
125                           ArrayRef<std::string> Sections);
126 
127   std::function<Error(const Twine &Msg)> WarningHandler;
128   void reportUniqueWarning(Error Err) const;
129   void reportUniqueWarning(const Twine &Msg) const;
130 
131 protected:
132   ScopedPrinter &W;
133 
134 private:
135   virtual void printSymbols() {}
136   virtual void printDynamicSymbols() {}
137   virtual void printProgramHeaders() {}
138   virtual void printSectionMapping() {}
139 
140   std::unordered_set<std::string> Warnings;
141 };
142 
143 std::unique_ptr<ObjDumper> createCOFFDumper(const object::COFFObjectFile &Obj,
144                                             ScopedPrinter &Writer);
145 
146 std::unique_ptr<ObjDumper> createELFDumper(const object::ELFObjectFileBase &Obj,
147                                            ScopedPrinter &Writer);
148 
149 std::unique_ptr<ObjDumper> createMachODumper(const object::MachOObjectFile &Obj,
150                                              ScopedPrinter &Writer);
151 
152 std::unique_ptr<ObjDumper> createWasmDumper(const object::WasmObjectFile &Obj,
153                                             ScopedPrinter &Writer);
154 
155 std::unique_ptr<ObjDumper> createXCOFFDumper(const object::XCOFFObjectFile &Obj,
156                                              ScopedPrinter &Writer);
157 
158 void dumpCOFFImportFile(const object::COFFImportFile *File,
159                         ScopedPrinter &Writer);
160 
161 void dumpCodeViewMergedTypes(ScopedPrinter &Writer,
162                              ArrayRef<ArrayRef<uint8_t>> IpiRecords,
163                              ArrayRef<ArrayRef<uint8_t>> TpiRecords);
164 
165 } // namespace llvm
166 
167 #endif
168