LLDB mainline
ObjectFileWasm.cpp File Reference
#include "ObjectFileWasm.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Section.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/DataBufferHeap.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/BinaryFormat/Wasm.h"
#include "llvm/Support/CheckedArithmetic.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Format.h"
#include <cstring>
#include <optional>

Go to the source code of this file.

Classes

struct  WasmFunction
struct  WasmImports
 The number of imports of each kind. More...
struct  WasmSegment

Functions

static llvm::Expected< uint32_t > GetULEB32 (DataExtractor &data, lldb::offset_t &offset)
 Helper to read a 32-bit ULEB using LLDB's DataExtractor.
static llvm::Expected< uint32_t > GetULEB32 (llvm::DataExtractor &data, llvm::DataExtractor::Cursor &c)
 Helper to read a 32-bit ULEB using LLVM's DataExtractor.
static llvm::Expected< std::string > GetWasmString (llvm::DataExtractor &data, llvm::DataExtractor::Cursor &c)
 Helper to read a Wasm string, whcih is encoded as a vector of UTF-8 codes.
static lldb::offset_t GetWasmOffsetFromInitExpr (DataExtractor &data, lldb::offset_t &offset)
 An "init expr" refers to a constant expression used to determine the initial value of certain elements within a module during instantiation.
static bool ValidateModuleHeader (llvm::ArrayRef< uint8_t > data)
 Checks whether the data buffer starts with a valid Wasm module header.
static llvm::Expected< WasmImportsParseImports (DataExtractor &import_data)
static llvm::Expected< uint32_t > GetFunctionCodeOffset (DataExtractor &data, lldb::offset_t offset)
 Get the offset in the function to the first instruction.
static llvm::Expected< std::vector< WasmFunction > > ParseFunctions (DataExtractor &data)
static llvm::Expected< std::vector< WasmSegment > > ParseData (DataExtractor &data)
static llvm::Expected< uint64_t > ParseMemoryMinSize (DataExtractor &data)
 Parse the minimum size in bytes of the module's first linear memory.
static std::optional< uint32_t > GetWasmValueTypeSize (uint8_t type)
 Size in bytes of a WebAssembly value type, or nothing for the types whose values cannot be read.
static std::optional< uint64_t > ParseGlobalInitValue (DataExtractor &data, lldb::offset_t &offset)
 Parse the init expr that gives a global its initial value.
static llvm::Expected< std::vector< WasmGlobal > > ParseGlobals (DataExtractor &data)
 Parse the module's own globals, which start at the number of imported ones.
static llvm::Expected< std::vector< Symbol > > ParseNames (SectionSP code_section_sp, SectionSP global_section_sp, DataExtractor &name_data, const std::vector< WasmFunction > &functions, std::vector< WasmSegment > &segments, const std::vector< WasmGlobal > &globals, uint32_t num_imported_functions, uint32_t num_imported_globals)
static SectionType GetSectionTypeFromName (llvm::StringRef Name)
static SectionType GetSegmentTypeFromName (llvm::StringRef Name)
 A section attribute on a data variable lands in a named data segment on wasm, not a top-level custom section, so formatter sections appear as segment names rather than section names.

Variables

static const uint32_t kWasmHeaderSize
static constexpr lldb::addr_t kWasmGlobalFileAddress
 File address the synthetic global section is based at.

Function Documentation

◆ GetFunctionCodeOffset()

llvm::Expected< uint32_t > GetFunctionCodeOffset ( DataExtractor & data,
lldb::offset_t offset )
static

Get the offset in the function to the first instruction.

Definition at line 405 of file ObjectFileWasm.cpp.

References lldb_private::DataExtractor::GetU8(), and lldb_private::DataExtractor::GetULEB128().

Referenced by ParseFunctions().

◆ GetSectionTypeFromName()

SectionType GetSectionTypeFromName ( llvm::StringRef Name)
static

◆ GetSegmentTypeFromName()

SectionType GetSegmentTypeFromName ( llvm::StringRef Name)
static

A section attribute on a data variable lands in a named data segment on wasm, not a top-level custom section, so formatter sections appear as segment names rather than section names.

Definition at line 776 of file ObjectFileWasm.cpp.

References lldb::eSectionTypeData, lldb::eSectionTypeLLDBFormatters, and lldb::eSectionTypeLLDBTypeSummaries.

Referenced by lldb_private::wasm::ObjectFileWasm::CreateSections().

◆ GetULEB32() [1/2]

llvm::Expected< uint32_t > GetULEB32 ( DataExtractor & data,
lldb::offset_t & offset )
inlinestatic

Helper to read a 32-bit ULEB using LLDB's DataExtractor.

Definition at line 48 of file ObjectFileWasm.cpp.

References GetULEB32().

Referenced by GetULEB32(), GetWasmString(), ParseData(), ParseFunctions(), ParseGlobals(), ParseImports(), ParseMemoryMinSize(), and ParseNames().

◆ GetULEB32() [2/2]

llvm::Expected< uint32_t > GetULEB32 ( llvm::DataExtractor & data,
llvm::DataExtractor::Cursor & c )
inlinestatic

Helper to read a 32-bit ULEB using LLVM's DataExtractor.

Definition at line 58 of file ObjectFileWasm.cpp.

◆ GetWasmOffsetFromInitExpr()

lldb::offset_t GetWasmOffsetFromInitExpr ( DataExtractor & data,
lldb::offset_t & offset )
static

An "init expr" refers to a constant expression used to determine the initial value of certain elements within a module during instantiation.

These expressions are restricted to operations that can be evaluated at module instantiation time. Currently we only support simple constant opcodes.

Definition at line 86 of file ObjectFileWasm.cpp.

References lldb_private::DataExtractor::GetFloat(), lldb_private::DataExtractor::GetSLEB128(), lldb_private::DataExtractor::GetU8(), lldb_private::DataExtractor::GetULEB128(), LLDB_INVALID_OFFSET, and lldb_private::DataExtractor::ValidOffset().

Referenced by ParseData().

◆ GetWasmString()

llvm::Expected< std::string > GetWasmString ( llvm::DataExtractor & data,
llvm::DataExtractor::Cursor & c )
inlinestatic

Helper to read a Wasm string, whcih is encoded as a vector of UTF-8 codes.

Definition at line 69 of file ObjectFileWasm.cpp.

References GetULEB32().

Referenced by lldb_private::wasm::ObjectFileWasm::DecodeNextSection(), lldb_private::wasm::ObjectFileWasm::GetExternalDebugInfoFileSpec(), ParseImports(), and ParseNames().

◆ GetWasmValueTypeSize()

std::optional< uint32_t > GetWasmValueTypeSize ( uint8_t type)
static

Size in bytes of a WebAssembly value type, or nothing for the types whose values cannot be read.

Definition at line 552 of file ObjectFileWasm.cpp.

Referenced by ParseGlobals().

◆ ParseData()

llvm::Expected< std::vector< WasmSegment > > ParseData ( DataExtractor & data)
static

◆ ParseFunctions()

llvm::Expected< std::vector< WasmFunction > > ParseFunctions ( DataExtractor & data)
static

◆ ParseGlobalInitValue()

std::optional< uint64_t > ParseGlobalInitValue ( DataExtractor & data,
lldb::offset_t & offset )
static

Parse the init expr that gives a global its initial value.

The result is the bit pattern of the value, so an operand that has to be evaluated against module state yields nothing.

Definition at line 568 of file ObjectFileWasm.cpp.

References lldb_private::DataExtractor::GetSLEB128(), lldb_private::DataExtractor::GetU32(), lldb_private::DataExtractor::GetU64(), lldb_private::DataExtractor::GetU8(), lldb_private::DataExtractor::GetULEB128(), and lldb_private::DataExtractor::ValidOffset().

Referenced by ParseGlobals().

◆ ParseGlobals()

llvm::Expected< std::vector< WasmGlobal > > ParseGlobals ( DataExtractor & data)
static

◆ ParseImports()

◆ ParseMemoryMinSize()

llvm::Expected< uint64_t > ParseMemoryMinSize ( DataExtractor & data)
static

Parse the minimum size in bytes of the module's first linear memory.

This is the memory guaranteed to exist at instantiation, and so the upper bound of the static data region.

Definition at line 531 of file ObjectFileWasm.cpp.

References lldb_private::DataExtractor::GetU8(), and GetULEB32().

Referenced by lldb_private::wasm::ObjectFileWasm::CreateSections().

◆ ParseNames()

llvm::Expected< std::vector< Symbol > > ParseNames ( SectionSP code_section_sp,
SectionSP global_section_sp,
DataExtractor & name_data,
const std::vector< WasmFunction > & functions,
std::vector< WasmSegment > & segments,
const std::vector< WasmGlobal > & globals,
uint32_t num_imported_functions,
uint32_t num_imported_globals )
static

◆ ValidateModuleHeader()

bool ValidateModuleHeader ( llvm::ArrayRef< uint8_t > data)
static

Checks whether the data buffer starts with a valid Wasm module header.

Definition at line 124 of file ObjectFileWasm.cpp.

References kWasmHeaderSize.

Referenced by lldb_private::wasm::ObjectFileWasm::CreateInstance(), lldb_private::wasm::ObjectFileWasm::CreateMemoryInstance(), and lldb_private::wasm::ObjectFileWasm::GetModuleSpecifications().

Variable Documentation

◆ kWasmGlobalFileAddress

lldb::addr_t kWasmGlobalFileAddress
staticconstexpr
Initial value:
=
uint64_t(WasmAddressType::Global) << kWasmAddressTypeShift
static constexpr uint32_t kWasmAddressTypeShift
Definition WasmAddress.h:46

File address the synthetic global section is based at.

Code and linear memory are both addressed from zero, so the globals need a range of their own to keep a global index from naming a code or data address as well.

Definition at line 44 of file ObjectFileWasm.cpp.

Referenced by lldb_private::wasm::ObjectFileWasm::CreateSections().

◆ kWasmHeaderSize

const uint32_t kWasmHeaderSize
static
Initial value:
=
sizeof(llvm::wasm::WasmMagic) + sizeof(llvm::wasm::WasmVersion)

Definition at line 38 of file ObjectFileWasm.cpp.

Referenced by lldb_private::wasm::ObjectFileWasm::DecodeSections(), and ValidateModuleHeader().