xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-readobj/ObjDumper.h (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 printDependentLibs() {}
57   virtual void printDynamicRelocations() { }
58   virtual void printDynamicTable() { }
59   virtual void printNeededLibraries() { }
60   virtual void printSectionAsHex(StringRef SectionName) {}
61   virtual void printHashTable() { }
62   virtual void printGnuHashTable(const object::ObjectFile *Obj) {}
63   virtual void printHashSymbols() {}
64   virtual void printLoadName() {}
65   virtual void printVersionInfo() {}
66   virtual void printGroupSections() {}
67   virtual void printHashHistograms() {}
68   virtual void printCGProfile() {}
69   virtual void printAddrsig() {}
70   virtual void printNotes() {}
71   virtual void printELFLinkerOptions() {}
72   virtual void printStackSizes() {}
73   virtual void printArchSpecificInfo() { }
74 
75   // Only implemented for PE/COFF.
76   virtual void printCOFFImports() { }
77   virtual void printCOFFExports() { }
78   virtual void printCOFFDirectives() { }
79   virtual void printCOFFBaseReloc() { }
80   virtual void printCOFFDebugDirectory() { }
81   virtual void printCOFFResources() {}
82   virtual void printCOFFLoadConfig() { }
83   virtual void printCodeViewDebugInfo() { }
84   virtual void
85   mergeCodeViewTypes(llvm::codeview::MergingTypeTableBuilder &CVIDs,
86                      llvm::codeview::MergingTypeTableBuilder &CVTypes,
87                      llvm::codeview::GlobalTypeTableBuilder &GlobalCVIDs,
88                      llvm::codeview::GlobalTypeTableBuilder &GlobalCVTypes,
89                      bool GHash) {}
90 
91   // Only implemented for MachO.
92   virtual void printMachODataInCode() { }
93   virtual void printMachOVersionMin() { }
94   virtual void printMachODysymtab() { }
95   virtual void printMachOSegment() { }
96   virtual void printMachOIndirectSymbols() { }
97   virtual void printMachOLinkerOptions() { }
98 
99   virtual void printStackMap() const = 0;
100 
101   void printSectionsAsString(const object::ObjectFile *Obj,
102                              ArrayRef<std::string> Sections);
103   void printSectionsAsHex(const object::ObjectFile *Obj,
104                           ArrayRef<std::string> Sections);
105 
106 protected:
107   ScopedPrinter &W;
108 
109 private:
110   virtual void printSymbols() {}
111   virtual void printDynamicSymbols() {}
112   virtual void printProgramHeaders() {}
113   virtual void printSectionMapping() {}
114 };
115 
116 std::error_code createCOFFDumper(const object::ObjectFile *Obj,
117                                  ScopedPrinter &Writer,
118                                  std::unique_ptr<ObjDumper> &Result);
119 
120 std::error_code createELFDumper(const object::ObjectFile *Obj,
121                                 ScopedPrinter &Writer,
122                                 std::unique_ptr<ObjDumper> &Result);
123 
124 std::error_code createMachODumper(const object::ObjectFile *Obj,
125                                   ScopedPrinter &Writer,
126                                   std::unique_ptr<ObjDumper> &Result);
127 
128 std::error_code createWasmDumper(const object::ObjectFile *Obj,
129                                  ScopedPrinter &Writer,
130                                  std::unique_ptr<ObjDumper> &Result);
131 
132 std::error_code createXCOFFDumper(const object::ObjectFile *Obj,
133                                   ScopedPrinter &Writer,
134                                   std::unique_ptr<ObjDumper> &Result);
135 
136 void dumpCOFFImportFile(const object::COFFImportFile *File,
137                         ScopedPrinter &Writer);
138 
139 void dumpCodeViewMergedTypes(ScopedPrinter &Writer,
140                              ArrayRef<ArrayRef<uint8_t>> IpiRecords,
141                              ArrayRef<ArrayRef<uint8_t>> TpiRecords);
142 
143 } // namespace llvm
144 
145 #endif
146