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