xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-readobj/ObjDumper.h (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
10b57cec5SDimitry Andric //===-- ObjDumper.h ---------------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #ifndef LLVM_TOOLS_LLVM_READOBJ_OBJDUMPER_H
100b57cec5SDimitry Andric #define LLVM_TOOLS_LLVM_READOBJ_OBJDUMPER_H
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include <memory>
130b57cec5SDimitry Andric #include <system_error>
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
160b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
170b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
180b57cec5SDimitry Andric 
19e8d8bef9SDimitry Andric #include <unordered_set>
20e8d8bef9SDimitry Andric 
210b57cec5SDimitry Andric namespace llvm {
220b57cec5SDimitry Andric namespace object {
23*0eae32dcSDimitry Andric class Archive;
240b57cec5SDimitry Andric class COFFImportFile;
250b57cec5SDimitry Andric class ObjectFile;
26e8d8bef9SDimitry Andric class XCOFFObjectFile;
27e8d8bef9SDimitry Andric class ELFObjectFileBase;
280b57cec5SDimitry Andric }
290b57cec5SDimitry Andric namespace codeview {
300b57cec5SDimitry Andric class GlobalTypeTableBuilder;
310b57cec5SDimitry Andric class MergingTypeTableBuilder;
320b57cec5SDimitry Andric } // namespace codeview
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric class ScopedPrinter;
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric class ObjDumper {
370b57cec5SDimitry Andric public:
38e8d8bef9SDimitry Andric   ObjDumper(ScopedPrinter &Writer, StringRef ObjName);
390b57cec5SDimitry Andric   virtual ~ObjDumper();
400b57cec5SDimitry Andric 
41e8d8bef9SDimitry Andric   virtual bool canDumpContent() { return true; }
42e8d8bef9SDimitry Andric 
43*0eae32dcSDimitry Andric   virtual void printFileSummary(StringRef FileStr, object::ObjectFile &Obj,
44*0eae32dcSDimitry Andric                                 ArrayRef<std::string> InputFilenames,
45*0eae32dcSDimitry Andric                                 const object::Archive *A);
460b57cec5SDimitry Andric   virtual void printFileHeaders() = 0;
470b57cec5SDimitry Andric   virtual void printSectionHeaders() = 0;
480b57cec5SDimitry Andric   virtual void printRelocations() = 0;
490b57cec5SDimitry Andric   virtual void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) {
500b57cec5SDimitry Andric     if (PrintSymbols)
510b57cec5SDimitry Andric       printSymbols();
520b57cec5SDimitry Andric     if (PrintDynamicSymbols)
530b57cec5SDimitry Andric       printDynamicSymbols();
540b57cec5SDimitry Andric   }
550b57cec5SDimitry Andric   virtual void printProgramHeaders(bool PrintProgramHeaders,
560b57cec5SDimitry Andric                                    cl::boolOrDefault PrintSectionMapping) {
570b57cec5SDimitry Andric     if (PrintProgramHeaders)
580b57cec5SDimitry Andric       printProgramHeaders();
590b57cec5SDimitry Andric     if (PrintSectionMapping == cl::BOU_TRUE)
600b57cec5SDimitry Andric       printSectionMapping();
610b57cec5SDimitry Andric   }
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   virtual void printUnwindInfo() = 0;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   // Only implemented for ELF at this time.
66480093f4SDimitry Andric   virtual void printDependentLibs() {}
670b57cec5SDimitry Andric   virtual void printDynamicRelocations() { }
680b57cec5SDimitry Andric   virtual void printDynamicTable() { }
690b57cec5SDimitry Andric   virtual void printNeededLibraries() { }
700b57cec5SDimitry Andric   virtual void printSectionAsHex(StringRef SectionName) {}
710b57cec5SDimitry Andric   virtual void printHashTable() { }
72e8d8bef9SDimitry Andric   virtual void printGnuHashTable() {}
730b57cec5SDimitry Andric   virtual void printHashSymbols() {}
740b57cec5SDimitry Andric   virtual void printLoadName() {}
750b57cec5SDimitry Andric   virtual void printVersionInfo() {}
760b57cec5SDimitry Andric   virtual void printGroupSections() {}
775ffd83dbSDimitry Andric   virtual void printHashHistograms() {}
780b57cec5SDimitry Andric   virtual void printCGProfile() {}
79fe6060f1SDimitry Andric   virtual void printBBAddrMaps() {}
800b57cec5SDimitry Andric   virtual void printAddrsig() {}
810b57cec5SDimitry Andric   virtual void printNotes() {}
820b57cec5SDimitry Andric   virtual void printELFLinkerOptions() {}
838bcb0991SDimitry Andric   virtual void printStackSizes() {}
84e8d8bef9SDimitry Andric   virtual void printSectionDetails() {}
858bcb0991SDimitry Andric   virtual void printArchSpecificInfo() {}
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   // Only implemented for PE/COFF.
880b57cec5SDimitry Andric   virtual void printCOFFImports() { }
890b57cec5SDimitry Andric   virtual void printCOFFExports() { }
900b57cec5SDimitry Andric   virtual void printCOFFDirectives() { }
910b57cec5SDimitry Andric   virtual void printCOFFBaseReloc() { }
920b57cec5SDimitry Andric   virtual void printCOFFDebugDirectory() { }
93e8d8bef9SDimitry Andric   virtual void printCOFFTLSDirectory() {}
940b57cec5SDimitry Andric   virtual void printCOFFResources() {}
950b57cec5SDimitry Andric   virtual void printCOFFLoadConfig() { }
960b57cec5SDimitry Andric   virtual void printCodeViewDebugInfo() { }
970b57cec5SDimitry Andric   virtual void
980b57cec5SDimitry Andric   mergeCodeViewTypes(llvm::codeview::MergingTypeTableBuilder &CVIDs,
990b57cec5SDimitry Andric                      llvm::codeview::MergingTypeTableBuilder &CVTypes,
1000b57cec5SDimitry Andric                      llvm::codeview::GlobalTypeTableBuilder &GlobalCVIDs,
1010b57cec5SDimitry Andric                      llvm::codeview::GlobalTypeTableBuilder &GlobalCVTypes,
1020b57cec5SDimitry Andric                      bool GHash) {}
1030b57cec5SDimitry Andric 
104349cc55cSDimitry Andric   // Only implement for XCOFF
105349cc55cSDimitry Andric   virtual void printAuxiliaryHeader() {}
106349cc55cSDimitry Andric 
1070b57cec5SDimitry Andric   // Only implemented for MachO.
1080b57cec5SDimitry Andric   virtual void printMachODataInCode() { }
1090b57cec5SDimitry Andric   virtual void printMachOVersionMin() { }
1100b57cec5SDimitry Andric   virtual void printMachODysymtab() { }
1110b57cec5SDimitry Andric   virtual void printMachOSegment() { }
1120b57cec5SDimitry Andric   virtual void printMachOIndirectSymbols() { }
1130b57cec5SDimitry Andric   virtual void printMachOLinkerOptions() { }
1140b57cec5SDimitry Andric 
115fe6060f1SDimitry Andric   // Currently only implemented for XCOFF.
116fe6060f1SDimitry Andric   virtual void printStringTable() { }
117fe6060f1SDimitry Andric 
1180b57cec5SDimitry Andric   virtual void printStackMap() const = 0;
1190b57cec5SDimitry Andric 
120349cc55cSDimitry Andric   void printAsStringList(StringRef StringContent, size_t StringDataOffset = 0);
121fe6060f1SDimitry Andric 
122e8d8bef9SDimitry Andric   void printSectionsAsString(const object::ObjectFile &Obj,
1230b57cec5SDimitry Andric                              ArrayRef<std::string> Sections);
124e8d8bef9SDimitry Andric   void printSectionsAsHex(const object::ObjectFile &Obj,
1250b57cec5SDimitry Andric                           ArrayRef<std::string> Sections);
1260b57cec5SDimitry Andric 
127e8d8bef9SDimitry Andric   std::function<Error(const Twine &Msg)> WarningHandler;
128e8d8bef9SDimitry Andric   void reportUniqueWarning(Error Err) const;
129e8d8bef9SDimitry Andric   void reportUniqueWarning(const Twine &Msg) const;
130e8d8bef9SDimitry Andric 
1310b57cec5SDimitry Andric protected:
1320b57cec5SDimitry Andric   ScopedPrinter &W;
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric private:
1350b57cec5SDimitry Andric   virtual void printSymbols() {}
1360b57cec5SDimitry Andric   virtual void printDynamicSymbols() {}
1370b57cec5SDimitry Andric   virtual void printProgramHeaders() {}
1380b57cec5SDimitry Andric   virtual void printSectionMapping() {}
139e8d8bef9SDimitry Andric 
140e8d8bef9SDimitry Andric   std::unordered_set<std::string> Warnings;
1410b57cec5SDimitry Andric };
1420b57cec5SDimitry Andric 
143e8d8bef9SDimitry Andric std::unique_ptr<ObjDumper> createCOFFDumper(const object::COFFObjectFile &Obj,
144e8d8bef9SDimitry Andric                                             ScopedPrinter &Writer);
1450b57cec5SDimitry Andric 
146e8d8bef9SDimitry Andric std::unique_ptr<ObjDumper> createELFDumper(const object::ELFObjectFileBase &Obj,
147e8d8bef9SDimitry Andric                                            ScopedPrinter &Writer);
1480b57cec5SDimitry Andric 
149e8d8bef9SDimitry Andric std::unique_ptr<ObjDumper> createMachODumper(const object::MachOObjectFile &Obj,
150e8d8bef9SDimitry Andric                                              ScopedPrinter &Writer);
1510b57cec5SDimitry Andric 
152e8d8bef9SDimitry Andric std::unique_ptr<ObjDumper> createWasmDumper(const object::WasmObjectFile &Obj,
153e8d8bef9SDimitry Andric                                             ScopedPrinter &Writer);
1540b57cec5SDimitry Andric 
155e8d8bef9SDimitry Andric std::unique_ptr<ObjDumper> createXCOFFDumper(const object::XCOFFObjectFile &Obj,
156e8d8bef9SDimitry Andric                                              ScopedPrinter &Writer);
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric void dumpCOFFImportFile(const object::COFFImportFile *File,
1590b57cec5SDimitry Andric                         ScopedPrinter &Writer);
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric void dumpCodeViewMergedTypes(ScopedPrinter &Writer,
1620b57cec5SDimitry Andric                              ArrayRef<ArrayRef<uint8_t>> IpiRecords,
1630b57cec5SDimitry Andric                              ArrayRef<ArrayRef<uint8_t>> TpiRecords);
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric } // namespace llvm
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric #endif
168