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 /// Size of the global's value type, which bounds what a read can produce.
24 /// Absent for a value type whose values cannot be read.
25 std::optional<uint32_t> size;
26
27 /// Value the global is initialized with, when the initializer is a constant.
28 /// Anything else has to be evaluated against module state the object file
29 /// does not have.
30 std::optional<uint64_t> init_expr_value;
31};
32
33/// Generic Wasm object file reader.
34///
35/// This class provides a generic wasm32 reader plugin implementing the
36/// ObjectFile protocol.
37class ObjectFileWasm : public ObjectFile {
38public:
39 static void Initialize();
40 static void Terminate();
41
42 static llvm::StringRef GetPluginNameStatic() { return "wasm"; }
43 static const char *GetPluginDescriptionStatic() {
44 return "WebAssembly object file reader.";
45 }
46
47 static ObjectFile *CreateInstance(const lldb::ModuleSP &module_sp,
48 lldb::DataExtractorSP extractor_sp,
49 lldb::offset_t data_offset,
50 const FileSpec *file,
51 lldb::offset_t file_offset,
52 lldb::offset_t length);
53
54 static ObjectFile *CreateMemoryInstance(const lldb::ModuleSP &module_sp,
56 const lldb::ProcessSP &process_sp,
57 lldb::addr_t header_addr);
58
59 static ModuleSpecList
61 lldb::DataExtractorSP &extractor_sp,
62 lldb::offset_t file_offset, lldb::offset_t length);
63
64 /// PluginInterface protocol.
65 /// \{
66 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
67 /// \}
68
69 /// LLVM RTTI support
70 /// \{
71 static char ID;
72 bool isA(const void *ClassID) const override {
73 return ClassID == &ID || ObjectFile::isA(ClassID);
74 }
75 static bool classof(const ObjectFile *obj) { return obj->isA(&ID); }
76 /// \}
77
78 /// ObjectFile Protocol.
79 /// \{
80 bool ParseHeader() override;
81
82 lldb::ByteOrder GetByteOrder() const override {
83 return m_arch.GetByteOrder();
84 }
85
86 bool IsExecutable() const override { return false; }
87
88 uint32_t GetAddressByteSize() const override {
89 return m_arch.GetAddressByteSize();
90 }
91
94 }
95
96 void ParseSymtab(lldb_private::Symtab &symtab) override;
97
98 bool IsStripped() override { return !!GetExternalDebugInfoFileSpec(); }
99
100 void CreateSections(SectionList &unified_section_list) override;
101
102 void Dump(Stream *s) override;
103
104 ArchSpec GetArchitecture() override { return m_arch; }
105
106 UUID GetUUID() override { return m_uuid; }
107
108 uint32_t GetDependentModules(FileSpecList &files) override { return 0; }
109
110 Type CalculateType() override { return eTypeSharedLibrary; }
111
112 Strata CalculateStrata() override { return eStrataUser; }
113
115 bool value_is_offset) override;
116
117 /// A global has no bytes in the module to read, so serve its initial value
118 /// when there is no process to ask for the current one.
119 size_t ReadSectionData(Section *section, lldb::offset_t section_offset,
120 void *dst, size_t dst_len) override;
121
125 /// \}
126
127 /// A Wasm module that has external DWARF debug information should contain a
128 /// custom section named "external_debug_info", whose payload is an UTF-8
129 /// encoded string that points to a Wasm module that contains the debug
130 /// information for this module.
131 std::optional<FileSpec> GetExternalDebugInfoFileSpec();
132
133private:
134 ObjectFileWasm(const lldb::ModuleSP &module_sp,
135 lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset,
136 const FileSpec *file, lldb::offset_t offset,
137 lldb::offset_t length);
138 ObjectFileWasm(const lldb::ModuleSP &module_sp,
139 lldb::WritableDataBufferSP header_data_sp,
140 const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
141
142 /// Wasm section decoding routines.
143 /// \{
144 bool DecodeNextSection(lldb::offset_t *offset_ptr);
145 bool DecodeSections();
146 /// \}
147
148 /// Read a range of bytes from the Wasm module.
149 DataExtractor ReadImageData(lldb::offset_t offset, uint32_t size);
150
153 uint32_t size;
154 uint32_t id;
156 lldb::offset_t GetFileOffset() const { return offset & 0xffffffff; }
157 };
158
159 std::optional<section_info> GetSectionInfo(uint32_t section_id);
160 std::optional<section_info> GetSectionInfo(llvm::StringRef section_name);
161
162 /// Wasm section header dump routines.
163 /// \{
164 void DumpSectionHeader(llvm::raw_ostream &ostream, const section_info &sh);
165 void DumpSectionHeaders(llvm::raw_ostream &ostream);
166 /// \}
167
168 std::vector<section_info> m_sect_infos;
171 std::vector<WasmGlobal> m_globals;
172 std::vector<Symbol> m_symbols;
175};
176
177} // namespace wasm
178} // namespace lldb_private
179#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
A uniqued constant string class.
Definition ConstString.h:40
An data extractor class.
A file collection class.
A file utility class.
Definition FileSpec.h:57
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:777
@ eTypeSharedLibrary
A shared library that can be used during execution.
Definition ObjectFile.h:63
virtual bool isA(const void *ClassID) const
Definition ObjectFile.h:216
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:685
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
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.
UUID GetUUID() override
Gets the UUID for this 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:85
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.