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