LLDB mainline
ObjectFileWasm.h
Go to the documentation of this file.
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 "WasmAddress.h"
15#include <optional>
16
17namespace lldb_private {
18namespace wasm {
19
20/// A global declared by the module itself. Imported globals occupy the low
21/// indices of the same index space and have no entry here.
22struct WasmGlobal {
23 uint8_t value_type = 0;
24 bool is_mutable = false;
25
26 /// Size of the global's value type, which bounds what a read can produce.
27 /// Absent for a value type whose values cannot be read.
28 std::optional<uint32_t> size;
29
30 /// Value the global is initialized with, when the initializer is a constant.
31 /// Anything else has to be evaluated against module state the object file
32 /// does not have.
33 std::optional<uint64_t> init_expr_value;
34};
35
36/// Generic Wasm object file reader.
37///
38/// This class provides a generic wasm32 reader plugin implementing the
39/// ObjectFile protocol.
40class ObjectFileWasm : public ObjectFile {
41public:
42 static void Initialize();
43 static void Terminate();
44
45 static llvm::StringRef GetPluginNameStatic() { return "wasm"; }
46 static const char *GetPluginDescriptionStatic() {
47 return "WebAssembly object file reader.";
48 }
49
50 static ObjectFile *CreateInstance(const lldb::ModuleSP &module_sp,
51 lldb::DataExtractorSP extractor_sp,
52 lldb::offset_t data_offset,
53 const FileSpec *file,
54 lldb::offset_t file_offset,
55 lldb::offset_t length);
56
57 static ObjectFile *CreateMemoryInstance(const lldb::ModuleSP &module_sp,
59 const lldb::ProcessSP &process_sp,
60 lldb::addr_t header_addr);
61
62 static ModuleSpecList
64 lldb::DataExtractorSP &extractor_sp,
65 lldb::offset_t file_offset, lldb::offset_t length);
66
67 /// PluginInterface protocol.
68 /// \{
69 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
70 /// \}
71
72 /// LLVM RTTI support
73 /// \{
74 static char ID;
75 bool isA(const void *ClassID) const override {
76 return ClassID == &ID || ObjectFile::isA(ClassID);
77 }
78 static bool classof(const ObjectFile *obj) { return obj->isA(&ID); }
79 /// \}
80
81 /// ObjectFile Protocol.
82 /// \{
83 bool ParseHeader() override;
84
85 lldb::ByteOrder GetByteOrder() const override {
86 return m_arch.GetByteOrder();
87 }
88
89 bool IsExecutable() const override { return false; }
90
91 uint32_t GetAddressByteSize() const override {
92 return m_arch.GetAddressByteSize();
93 }
94
97 }
98
99 void ParseSymtab(lldb_private::Symtab &symtab) override;
100
101 bool IsStripped() override { return !!GetExternalDebugInfoFileSpec(); }
102
103 void CreateSections(SectionList &unified_section_list) override;
104
105 void Dump(Stream *s) override;
106
107 ArchSpec GetArchitecture() override { return m_arch; }
108
109 UUID GetUUID() override;
110
111 uint32_t GetDependentModules(FileSpecList &files) override { return 0; }
112
113 Type CalculateType() override { return eTypeSharedLibrary; }
114
115 Strata CalculateStrata() override { return eStrataUser; }
116
118 bool value_is_offset) override;
119
120 /// A global has no bytes in the module to read, so serve its initial value
121 /// when there is no process to ask for the current one.
122 size_t ReadSectionData(Section *section, lldb::offset_t section_offset,
123 void *dst, size_t dst_len) override;
124
128 /// \}
129
130 /// A Wasm module that has external DWARF debug information should contain a
131 /// custom section named "external_debug_info", whose payload is an UTF-8
132 /// encoded string that points to a Wasm module that contains the debug
133 /// information for this module.
134 std::optional<FileSpec> GetExternalDebugInfoFileSpec();
135
136private:
137 ObjectFileWasm(const lldb::ModuleSP &module_sp,
138 lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset,
139 const FileSpec *file, lldb::offset_t offset,
140 lldb::offset_t length);
141 ObjectFileWasm(const lldb::ModuleSP &module_sp,
142 lldb::WritableDataBufferSP header_data_sp,
143 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
144
145 /// Wasm section decoding routines.
146 /// \{
147 bool DecodeNextSection(lldb::offset_t *offset_ptr);
148 bool DecodeSections();
149 /// \}
150
151 /// Read a range of bytes from the Wasm module.
152 DataExtractor ReadImageData(lldb::offset_t offset, uint32_t size);
153
156 uint32_t size;
157 uint32_t id;
158 std::string name;
159 lldb::offset_t GetFileOffset() const { return offset & 0xffffffff; }
160 };
161
162 std::optional<section_info> GetSectionInfo(uint32_t section_id);
163 std::optional<section_info> GetSectionInfo(llvm::StringRef section_name);
164
165 /// Wasm section header dump routines.
166 /// \{
167 void DumpSectionHeader(llvm::raw_ostream &ostream, const section_info &sh);
168 void DumpSectionHeaders(llvm::raw_ostream &ostream);
169 /// \}
170
171 std::vector<section_info> m_sect_infos;
174 std::vector<WasmGlobal> m_globals;
175 std::vector<Symbol> m_symbols;
178};
179
180} // namespace wasm
181} // namespace lldb_private
182#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_WASM_OBJECTFILEWASM_H
A section + offset based address class.
Definition Address.h:62
An architecture specification class.
Definition ArchSpec.h:32
An data extractor class.
A file collection class.
A file utility class.
Definition FileSpec.h:56
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
const lldb::addr_t m_memory_addr
Set if the object file only exists in memory.
Definition ObjectFile.h:798
@ eTypeSharedLibrary
A shared library that can be used during execution.
Definition ObjectFile.h:63
virtual bool isA(const void *ClassID) const
Definition ObjectFile.h:217
ObjectFile(const lldb::ModuleSP &module_sp, const FileSpec *file_spec_ptr, lldb::offset_t file_offset, lldb::offset_t length, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset)
Construct with a parent module, offset, and header data.
bool IsInMemory() const
Returns true if the object file exists only in memory.
Definition ObjectFile.h:695
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Represents UUID's of various sizes.
Definition UUID.h:27
ArchSpec GetArchitecture() override
Get the ArchSpec for this object file.
std::optional< FileSpec > GetExternalDebugInfoFileSpec()
A Wasm module that has external DWARF debug information should contain a custom section named "extern...
size_t ReadSectionData(Section *section, lldb::offset_t section_offset, void *dst, size_t dst_len) override
A global has no bytes in the module to read, so serve its initial value when there is no process to a...
static bool classof(const ObjectFile *obj)
bool SetLoadAddress(lldb_private::Target &target, lldb::addr_t value, bool value_is_offset) override
Sets the load address for an entire module, assuming a rigid slide of sections, if possible in the im...
bool DecodeNextSection(lldb::offset_t *offset_ptr)
Wasm section decoding routines.
lldb::ByteOrder GetByteOrder() const override
Gets whether endian swapping should occur when extracting data from this object file.
void CreateSections(SectionList &unified_section_list) override
ObjectFileWasm(const lldb::ModuleSP &module_sp, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset, const FileSpec *file, lldb::offset_t offset, lldb::offset_t length)
std::optional< section_info > GetSectionInfo(uint32_t section_id)
void Dump(Stream *s) override
Dump a description of this object to a Stream.
static llvm::StringRef GetPluginNameStatic()
std::vector< section_info > m_sect_infos
UUID GetUUID() override
Gets the UUID for this object file.
void DumpSectionHeader(llvm::raw_ostream &ostream, const section_info &sh)
Wasm section header dump routines.
void DumpSectionHeaders(llvm::raw_ostream &ostream)
static ObjectFile * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset, const FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
static char ID
LLVM RTTI support.
void ParseSymtab(lldb_private::Symtab &symtab) override
Parse the symbol table into the provides symbol table object.
uint32_t GetDependentModules(FileSpecList &files) override
Extract the dependent modules from an object file.
bool IsExecutable() const override
Tells whether this object file is capable of being the main executable for a process.
static ModuleSpecList GetModuleSpecifications(const FileSpec &file, lldb::DataExtractorSP &extractor_sp, lldb::offset_t file_offset, lldb::offset_t length)
lldb_private::Address GetBaseAddress() override
Returns base address of this object file.
uint32_t GetAddressByteSize() const override
Gets the address size in bytes for the current object file.
static ObjectFile * CreateMemoryInstance(const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
std::vector< WasmGlobal > m_globals
bool ParseHeader() override
ObjectFile Protocol.
Strata CalculateStrata() override
The object file should be able to calculate the strata of the object file.
AddressClass GetAddressClass(lldb::addr_t file_addr) override
Get the address type given a file address in an object file.
bool IsStripped() override
Detect if this object file has been stripped of local symbols.
bool isA(const void *ClassID) const override
Type CalculateType() override
The object file should be able to calculate its type by looking at its file header and possibly the s...
llvm::StringRef GetPluginName() override
PluginInterface protocol.
static const char * GetPluginDescriptionStatic()
DataExtractor ReadImageData(lldb::offset_t offset, uint32_t size)
Read a range of bytes from the Wasm module.
A class that represents a running process on the host machine.
uint64_t offset_t
Definition lldb-types.h:86
std::shared_ptr< lldb_private::Process > ProcessSP
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::Module > ModuleSP
A global declared by the module itself.
std::optional< uint32_t > size
Size of the global's value type, which bounds what a read can produce.
std::optional< uint64_t > init_expr_value
Value the global is initialized with, when the initializer is a constant.