xref: /llvm-project/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.h (revision a3939e159fc9528b097672794035a1cdfda520e8)
1 //===-- ObjectFileWasm.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 LLDB_SOURCE_PLUGINS_OBJECTFILE_WASM_OBJECTFILEWASM_H
10 #define LLDB_SOURCE_PLUGINS_OBJECTFILE_WASM_OBJECTFILEWASM_H
11 
12 #include "lldb/Symbol/ObjectFile.h"
13 #include "lldb/Utility/ArchSpec.h"
14 
15 namespace lldb_private {
16 namespace wasm {
17 
18 /// Generic Wasm object file reader.
19 ///
20 /// This class provides a generic wasm32 reader plugin implementing the
21 /// ObjectFile protocol.
22 class ObjectFileWasm : public ObjectFile {
23 public:
24   static void Initialize();
25   static void Terminate();
26 
27   static ConstString GetPluginNameStatic();
28   static const char *GetPluginDescriptionStatic() {
29     return "WebAssembly object file reader.";
30   }
31 
32   static ObjectFile *
33   CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
34                  lldb::offset_t data_offset, const FileSpec *file,
35                  lldb::offset_t file_offset, lldb::offset_t length);
36 
37   static ObjectFile *CreateMemoryInstance(const lldb::ModuleSP &module_sp,
38                                           lldb::DataBufferSP &data_sp,
39                                           const lldb::ProcessSP &process_sp,
40                                           lldb::addr_t header_addr);
41 
42   static size_t GetModuleSpecifications(const FileSpec &file,
43                                         lldb::DataBufferSP &data_sp,
44                                         lldb::offset_t data_offset,
45                                         lldb::offset_t file_offset,
46                                         lldb::offset_t length,
47                                         ModuleSpecList &specs);
48 
49   /// PluginInterface protocol.
50   /// \{
51   llvm::StringRef GetPluginName() override {
52     return GetPluginNameStatic().GetStringRef();
53   }
54   /// \}
55 
56   /// LLVM RTTI support
57   /// \{
58   static char ID;
59   bool isA(const void *ClassID) const override {
60     return ClassID == &ID || ObjectFile::isA(ClassID);
61   }
62   static bool classof(const ObjectFile *obj) { return obj->isA(&ID); }
63   /// \}
64 
65   /// ObjectFile Protocol.
66   /// \{
67   bool ParseHeader() override;
68 
69   lldb::ByteOrder GetByteOrder() const override {
70     return m_arch.GetByteOrder();
71   }
72 
73   bool IsExecutable() const override { return false; }
74 
75   uint32_t GetAddressByteSize() const override {
76     return m_arch.GetAddressByteSize();
77   }
78 
79   AddressClass GetAddressClass(lldb::addr_t file_addr) override {
80     return AddressClass::eInvalid;
81   }
82 
83   Symtab *GetSymtab() override;
84 
85   bool IsStripped() override { return !!GetExternalDebugInfoFileSpec(); }
86 
87   void CreateSections(SectionList &unified_section_list) override;
88 
89   void Dump(Stream *s) override;
90 
91   ArchSpec GetArchitecture() override { return m_arch; }
92 
93   UUID GetUUID() override { return m_uuid; }
94 
95   uint32_t GetDependentModules(FileSpecList &files) override { return 0; }
96 
97   Type CalculateType() override { return eTypeSharedLibrary; }
98 
99   Strata CalculateStrata() override { return eStrataUser; }
100 
101   bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value,
102                       bool value_is_offset) override;
103 
104   lldb_private::Address GetBaseAddress() override {
105     return IsInMemory() ? Address(m_memory_addr) : Address(0);
106   }
107   /// \}
108 
109   /// A Wasm module that has external DWARF debug information should contain a
110   /// custom section named "external_debug_info", whose payload is an UTF-8
111   /// encoded string that points to a Wasm module that contains the debug
112   /// information for this module.
113   llvm::Optional<FileSpec> GetExternalDebugInfoFileSpec();
114 
115 private:
116   ObjectFileWasm(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
117                  lldb::offset_t data_offset, const FileSpec *file,
118                  lldb::offset_t offset, lldb::offset_t length);
119   ObjectFileWasm(const lldb::ModuleSP &module_sp,
120                  lldb::DataBufferSP &header_data_sp,
121                  const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
122 
123   /// Wasm section decoding routines.
124   /// \{
125   bool DecodeNextSection(lldb::offset_t *offset_ptr);
126   bool DecodeSections();
127   /// \}
128 
129   /// Read a range of bytes from the Wasm module.
130   DataExtractor ReadImageData(lldb::offset_t offset, uint32_t size);
131 
132   typedef struct section_info {
133     lldb::offset_t offset;
134     uint32_t size;
135     uint32_t id;
136     ConstString name;
137   } section_info_t;
138 
139   /// Wasm section header dump routines.
140   /// \{
141   void DumpSectionHeader(llvm::raw_ostream &ostream, const section_info_t &sh);
142   void DumpSectionHeaders(llvm::raw_ostream &ostream);
143   /// \}
144 
145   std::vector<section_info_t> m_sect_infos;
146   ArchSpec m_arch;
147   UUID m_uuid;
148 };
149 
150 } // namespace wasm
151 } // namespace lldb_private
152 #endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_WASM_OBJECTFILEWASM_H
153