1*fe013be4SDimitry Andric //===-- ObjectFileJSON.h -------------------------------------- -*- C++ -*-===//
2*fe013be4SDimitry Andric //
3*fe013be4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*fe013be4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*fe013be4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*fe013be4SDimitry Andric //
7*fe013be4SDimitry Andric //===----------------------------------------------------------------------===//
8*fe013be4SDimitry Andric 
9*fe013be4SDimitry Andric #ifndef LLDB_SOURCE_PLUGINS_OBJECTFILE_JSON_OBJECTFILEJSON_H
10*fe013be4SDimitry Andric #define LLDB_SOURCE_PLUGINS_OBJECTFILE_JSON_OBJECTFILEJSON_H
11*fe013be4SDimitry Andric 
12*fe013be4SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
13*fe013be4SDimitry Andric #include "lldb/Utility/ArchSpec.h"
14*fe013be4SDimitry Andric #include "llvm/Support/JSON.h"
15*fe013be4SDimitry Andric 
16*fe013be4SDimitry Andric namespace lldb_private {
17*fe013be4SDimitry Andric 
18*fe013be4SDimitry Andric class ObjectFileJSON : public ObjectFile {
19*fe013be4SDimitry Andric public:
20*fe013be4SDimitry Andric   static void Initialize();
21*fe013be4SDimitry Andric   static void Terminate();
22*fe013be4SDimitry Andric 
GetPluginNameStatic()23*fe013be4SDimitry Andric   static llvm::StringRef GetPluginNameStatic() { return "JSON"; }
24*fe013be4SDimitry Andric 
GetPluginDescriptionStatic()25*fe013be4SDimitry Andric   static const char *GetPluginDescriptionStatic() {
26*fe013be4SDimitry Andric     return "JSON object file reader.";
27*fe013be4SDimitry Andric   }
28*fe013be4SDimitry Andric 
29*fe013be4SDimitry Andric   static ObjectFile *
30*fe013be4SDimitry Andric   CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
31*fe013be4SDimitry Andric                  lldb::offset_t data_offset, const FileSpec *file,
32*fe013be4SDimitry Andric                  lldb::offset_t file_offset, lldb::offset_t length);
33*fe013be4SDimitry Andric 
34*fe013be4SDimitry Andric   static ObjectFile *CreateMemoryInstance(const lldb::ModuleSP &module_sp,
35*fe013be4SDimitry Andric                                           lldb::WritableDataBufferSP data_sp,
36*fe013be4SDimitry Andric                                           const lldb::ProcessSP &process_sp,
37*fe013be4SDimitry Andric                                           lldb::addr_t header_addr);
38*fe013be4SDimitry Andric 
39*fe013be4SDimitry Andric   static size_t GetModuleSpecifications(const FileSpec &file,
40*fe013be4SDimitry Andric                                         lldb::DataBufferSP &data_sp,
41*fe013be4SDimitry Andric                                         lldb::offset_t data_offset,
42*fe013be4SDimitry Andric                                         lldb::offset_t file_offset,
43*fe013be4SDimitry Andric                                         lldb::offset_t length,
44*fe013be4SDimitry Andric                                         ModuleSpecList &specs);
45*fe013be4SDimitry Andric 
GetPluginName()46*fe013be4SDimitry Andric   llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
47*fe013be4SDimitry Andric 
48*fe013be4SDimitry Andric   // LLVM RTTI support
49*fe013be4SDimitry Andric   static char ID;
isA(const void * ClassID)50*fe013be4SDimitry Andric   bool isA(const void *ClassID) const override {
51*fe013be4SDimitry Andric     return ClassID == &ID || ObjectFile::isA(ClassID);
52*fe013be4SDimitry Andric   }
classof(const ObjectFile * obj)53*fe013be4SDimitry Andric   static bool classof(const ObjectFile *obj) { return obj->isA(&ID); }
54*fe013be4SDimitry Andric 
55*fe013be4SDimitry Andric   bool ParseHeader() override;
56*fe013be4SDimitry Andric 
GetByteOrder()57*fe013be4SDimitry Andric   lldb::ByteOrder GetByteOrder() const override {
58*fe013be4SDimitry Andric     return m_arch.GetByteOrder();
59*fe013be4SDimitry Andric   }
60*fe013be4SDimitry Andric 
IsExecutable()61*fe013be4SDimitry Andric   bool IsExecutable() const override { return false; }
62*fe013be4SDimitry Andric 
GetAddressByteSize()63*fe013be4SDimitry Andric   uint32_t GetAddressByteSize() const override {
64*fe013be4SDimitry Andric     return m_arch.GetAddressByteSize();
65*fe013be4SDimitry Andric   }
66*fe013be4SDimitry Andric 
GetAddressClass(lldb::addr_t file_addr)67*fe013be4SDimitry Andric   AddressClass GetAddressClass(lldb::addr_t file_addr) override {
68*fe013be4SDimitry Andric     return AddressClass::eInvalid;
69*fe013be4SDimitry Andric   }
70*fe013be4SDimitry Andric 
71*fe013be4SDimitry Andric   void ParseSymtab(lldb_private::Symtab &symtab) override;
72*fe013be4SDimitry Andric 
IsStripped()73*fe013be4SDimitry Andric   bool IsStripped() override { return false; }
74*fe013be4SDimitry Andric 
75*fe013be4SDimitry Andric   void CreateSections(SectionList &unified_section_list) override;
76*fe013be4SDimitry Andric 
Dump(Stream * s)77*fe013be4SDimitry Andric   void Dump(Stream *s) override {}
78*fe013be4SDimitry Andric 
GetArchitecture()79*fe013be4SDimitry Andric   ArchSpec GetArchitecture() override { return m_arch; }
80*fe013be4SDimitry Andric 
GetUUID()81*fe013be4SDimitry Andric   UUID GetUUID() override { return m_uuid; }
82*fe013be4SDimitry Andric 
GetDependentModules(FileSpecList & files)83*fe013be4SDimitry Andric   uint32_t GetDependentModules(FileSpecList &files) override { return 0; }
84*fe013be4SDimitry Andric 
CalculateType()85*fe013be4SDimitry Andric   Type CalculateType() override { return m_type; }
86*fe013be4SDimitry Andric 
CalculateStrata()87*fe013be4SDimitry Andric   Strata CalculateStrata() override { return eStrataUser; }
88*fe013be4SDimitry Andric 
89*fe013be4SDimitry Andric   static bool MagicBytesMatch(lldb::DataBufferSP data_sp, lldb::addr_t offset,
90*fe013be4SDimitry Andric                               lldb::addr_t length);
91*fe013be4SDimitry Andric 
92*fe013be4SDimitry Andric   struct Header {
93*fe013be4SDimitry Andric     std::string triple;
94*fe013be4SDimitry Andric     std::string uuid;
95*fe013be4SDimitry Andric     std::optional<ObjectFile::Type> type;
96*fe013be4SDimitry Andric   };
97*fe013be4SDimitry Andric 
98*fe013be4SDimitry Andric   struct Body {
99*fe013be4SDimitry Andric     std::vector<JSONSection> sections;
100*fe013be4SDimitry Andric     std::vector<JSONSymbol> symbols;
101*fe013be4SDimitry Andric   };
102*fe013be4SDimitry Andric 
103*fe013be4SDimitry Andric private:
104*fe013be4SDimitry Andric   ArchSpec m_arch;
105*fe013be4SDimitry Andric   UUID m_uuid;
106*fe013be4SDimitry Andric   ObjectFile::Type m_type;
107*fe013be4SDimitry Andric   std::optional<uint64_t> m_size;
108*fe013be4SDimitry Andric   std::vector<JSONSymbol> m_symbols;
109*fe013be4SDimitry Andric   std::vector<JSONSection> m_sections;
110*fe013be4SDimitry Andric 
111*fe013be4SDimitry Andric   ObjectFileJSON(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
112*fe013be4SDimitry Andric                  lldb::offset_t data_offset, const FileSpec *file,
113*fe013be4SDimitry Andric                  lldb::offset_t offset, lldb::offset_t length, ArchSpec arch,
114*fe013be4SDimitry Andric                  UUID uuid, Type type, std::vector<JSONSymbol> symbols,
115*fe013be4SDimitry Andric                  std::vector<JSONSection> sections);
116*fe013be4SDimitry Andric };
117*fe013be4SDimitry Andric 
118*fe013be4SDimitry Andric bool fromJSON(const llvm::json::Value &value, ObjectFileJSON::Header &header,
119*fe013be4SDimitry Andric               llvm::json::Path path);
120*fe013be4SDimitry Andric 
121*fe013be4SDimitry Andric bool fromJSON(const llvm::json::Value &value, ObjectFileJSON::Body &body,
122*fe013be4SDimitry Andric               llvm::json::Path path);
123*fe013be4SDimitry Andric 
124*fe013be4SDimitry Andric } // namespace lldb_private
125*fe013be4SDimitry Andric #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_JSON_OBJECTFILEJSON_H
126