xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-readobj/ObjDumper.h (revision 9dba64be9536c28e4800e06512b7f29b43ade345)
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 namespace llvm {
20 namespace object {
21 class COFFImportFile;
22 class ObjectFile;
23 }
24 namespace codeview {
25 class GlobalTypeTableBuilder;
26 class MergingTypeTableBuilder;
27 } // namespace codeview
28 
29 class ScopedPrinter;
30 
31 class ObjDumper {
32 public:
33   ObjDumper(ScopedPrinter &Writer);
34   virtual ~ObjDumper();
35 
36   virtual void printFileHeaders() = 0;
37   virtual void printSectionHeaders() = 0;
38   virtual void printRelocations() = 0;
39   virtual void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) {
40     if (PrintSymbols)
41       printSymbols();
42     if (PrintDynamicSymbols)
43       printDynamicSymbols();
44   }
45   virtual void printProgramHeaders(bool PrintProgramHeaders,
46                                    cl::boolOrDefault PrintSectionMapping) {
47     if (PrintProgramHeaders)
48       printProgramHeaders();
49     if (PrintSectionMapping == cl::BOU_TRUE)
50       printSectionMapping();
51   }
52 
53   virtual void printUnwindInfo() = 0;
54 
55   // Only implemented for ELF at this time.
56   virtual void printDynamicRelocations() { }
57   virtual void printDynamicTable() { }
58   virtual void printNeededLibraries() { }
59   virtual void printSectionAsHex(StringRef SectionName) {}
60   virtual void printHashTable() { }
61   virtual void printGnuHashTable() { }
62   virtual void printHashSymbols() {}
63   virtual void printLoadName() {}
64   virtual void printVersionInfo() {}
65   virtual void printGroupSections() {}
66   virtual void printHashHistogram() {}
67   virtual void printCGProfile() {}
68   virtual void printAddrsig() {}
69   virtual void printNotes() {}
70   virtual void printELFLinkerOptions() {}
71   virtual void printStackSizes() {}
72   virtual void printArchSpecificInfo() { }
73 
74   // Only implemented for PE/COFF.
75   virtual void printCOFFImports() { }
76   virtual void printCOFFExports() { }
77   virtual void printCOFFDirectives() { }
78   virtual void printCOFFBaseReloc() { }
79   virtual void printCOFFDebugDirectory() { }
80   virtual void printCOFFResources() {}
81   virtual void printCOFFLoadConfig() { }
82   virtual void printCodeViewDebugInfo() { }
83   virtual void
84   mergeCodeViewTypes(llvm::codeview::MergingTypeTableBuilder &CVIDs,
85                      llvm::codeview::MergingTypeTableBuilder &CVTypes,
86                      llvm::codeview::GlobalTypeTableBuilder &GlobalCVIDs,
87                      llvm::codeview::GlobalTypeTableBuilder &GlobalCVTypes,
88                      bool GHash) {}
89 
90   // Only implemented for MachO.
91   virtual void printMachODataInCode() { }
92   virtual void printMachOVersionMin() { }
93   virtual void printMachODysymtab() { }
94   virtual void printMachOSegment() { }
95   virtual void printMachOIndirectSymbols() { }
96   virtual void printMachOLinkerOptions() { }
97 
98   virtual void printStackMap() const = 0;
99 
100   void printSectionsAsString(const object::ObjectFile *Obj,
101                              ArrayRef<std::string> Sections);
102   void printSectionsAsHex(const object::ObjectFile *Obj,
103                           ArrayRef<std::string> Sections);
104 
105 protected:
106   ScopedPrinter &W;
107 
108 private:
109   virtual void printSymbols() {}
110   virtual void printDynamicSymbols() {}
111   virtual void printProgramHeaders() {}
112   virtual void printSectionMapping() {}
113 };
114 
115 std::error_code createCOFFDumper(const object::ObjectFile *Obj,
116                                  ScopedPrinter &Writer,
117                                  std::unique_ptr<ObjDumper> &Result);
118 
119 std::error_code createELFDumper(const object::ObjectFile *Obj,
120                                 ScopedPrinter &Writer,
121                                 std::unique_ptr<ObjDumper> &Result);
122 
123 std::error_code createMachODumper(const object::ObjectFile *Obj,
124                                   ScopedPrinter &Writer,
125                                   std::unique_ptr<ObjDumper> &Result);
126 
127 std::error_code createWasmDumper(const object::ObjectFile *Obj,
128                                  ScopedPrinter &Writer,
129                                  std::unique_ptr<ObjDumper> &Result);
130 
131 std::error_code createXCOFFDumper(const object::ObjectFile *Obj,
132                                   ScopedPrinter &Writer,
133                                   std::unique_ptr<ObjDumper> &Result);
134 
135 void dumpCOFFImportFile(const object::COFFImportFile *File,
136                         ScopedPrinter &Writer);
137 
138 void dumpCodeViewMergedTypes(ScopedPrinter &Writer,
139                              ArrayRef<ArrayRef<uint8_t>> IpiRecords,
140                              ArrayRef<ArrayRef<uint8_t>> TpiRecords);
141 
142 } // namespace llvm
143 
144 #endif
145