Skip to content

[lldb] Support DW_OP_WASM_location in DWARFExpression #151010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lldb/include/lldb/Expression/DWARFExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ class DWARFExpression {
GetVendorDWARFOpcodeSize(const DataExtractor &data,
const lldb::offset_t data_offset,
const uint8_t op) const = 0;
virtual bool ParseVendorDWARFOpcode(uint8_t op,
const DataExtractor &opcodes,
lldb::offset_t &offset,
Stack &stack) const = 0;
virtual bool
ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
lldb::offset_t &offset, RegisterContext *reg_ctx,
lldb::RegisterKind reg_kind, Stack &stack) const = 0;

Delegate(const Delegate &) = delete;
Delegate &operator=(const Delegate &) = delete;
Expand Down Expand Up @@ -163,6 +163,10 @@ class DWARFExpression {

bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op) const;

static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
lldb::RegisterKind reg_kind,
uint32_t reg_num, Value &value);

private:
/// A data extractor capable of reading opcode bytes
DataExtractor m_data;
Expand Down
10 changes: 6 additions & 4 deletions lldb/source/Expression/DWARFExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
m_reg_kind = reg_kind;
}

static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
lldb::RegisterKind reg_kind,
uint32_t reg_num, Value &value) {
llvm::Error
DWARFExpression::ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
lldb::RegisterKind reg_kind,
uint32_t reg_num, Value &value) {
if (reg_ctx == nullptr)
return llvm::createStringError("no register context in frame");

Expand Down Expand Up @@ -2302,7 +2303,8 @@ llvm::Expected<Value> DWARFExpression::Evaluate(

default:
if (dwarf_cu) {
if (dwarf_cu->ParseVendorDWARFOpcode(op, opcodes, offset, stack)) {
if (dwarf_cu->ParseVendorDWARFOpcode(op, opcodes, offset, reg_ctx,
reg_kind, stack)) {
break;
}
}
Expand Down
6 changes: 5 additions & 1 deletion lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,13 @@ DataExtractor ObjectFileWasm::ReadImageData(offset_t offset, uint32_t size) {
DataBufferSP buffer_sp(data_up.release());
data.SetData(buffer_sp, 0, buffer_sp->GetByteSize());
}
} else if (offset < m_data.GetByteSize()) {
size =
std::min(static_cast<uint64_t>(size), m_data.GetByteSize() - offset);
return DataExtractor(m_data.GetDataStart() + offset, size, GetByteOrder(),
GetAddressByteSize());
}
}

data.SetByteOrder(GetByteOrder());
return data;
}
Expand Down
1 change: 1 addition & 0 deletions lldb/source/Plugins/SymbolFile/DWARF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ add_lldb_library(lldbPluginSymbolFileDWARF PLUGIN
SymbolFileDWARF.cpp
SymbolFileDWARFDwo.cpp
SymbolFileDWARFDebugMap.cpp
SymbolFileWasm.cpp
UniqueDWARFASTType.cpp

LINK_COMPONENTS
Expand Down
4 changes: 3 additions & 1 deletion lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,11 @@ DWARFUnit::GetVendorDWARFOpcodeSize(const DataExtractor &data,

bool DWARFUnit::ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
lldb::offset_t &offset,
RegisterContext *reg_ctx,
lldb::RegisterKind reg_kind,
std::vector<Value> &stack) const {
return GetSymbolFileDWARF().ParseVendorDWARFOpcode(op, opcodes, offset,
stack);
reg_ctx, reg_kind, stack);
}

bool DWARFUnit::ParseDWARFLocationList(
Expand Down
8 changes: 5 additions & 3 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ class DWARFUnit : public DWARFExpression::Delegate, public UserID {
const lldb::offset_t data_offset,
const uint8_t op) const override;

bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
lldb::offset_t &offset,
std::vector<Value> &stack) const override;
virtual bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
lldb::offset_t &offset,
RegisterContext *reg_ctx,
lldb::RegisterKind reg_kind,
std::vector<Value> &stack) const override;

bool ParseDWARFLocationList(const DataExtractor &data,
DWARFExpressionList &loc_list) const;
Expand Down
4 changes: 4 additions & 0 deletions lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
#include "Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.h"
#include "Plugins/SymbolFile/DWARF/SymbolFileWasm.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "lldb/Symbol/Block.h"
#include "lldb/Symbol/CompileUnit.h"
Expand Down Expand Up @@ -327,6 +328,9 @@ llvm::StringRef SymbolFileDWARF::GetPluginDescriptionStatic() {
}

SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFileSP objfile_sp) {
if (objfile_sp->GetArchitecture().GetTriple().isWasm())
return new SymbolFileWasm(std::move(objfile_sp),
/*dwo_section_list*/ nullptr);
return new SymbolFileDWARF(std::move(objfile_sp),
/*dwo_section_list*/ nullptr);
}
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ class SymbolFileDWARF : public SymbolFileCommon {

virtual bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
lldb::offset_t &offset,
RegisterContext *reg_ctx,
lldb::RegisterKind reg_kind,
std::vector<Value> &stack) const {
return false;
}
Expand Down Expand Up @@ -556,6 +558,7 @@ class SymbolFileDWARF : public SymbolFileCommon {
/// an index that identifies the .DWO or .o file.
std::optional<uint64_t> m_file_index;
};

} // namespace dwarf
} // namespace lldb_private::plugin

Expand Down
8 changes: 5 additions & 3 deletions lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ uint64_t SymbolFileDWARFDwo::GetDebugInfoSize(bool load_all_debug_info) {
}

bool SymbolFileDWARFDwo::ParseVendorDWARFOpcode(
uint8_t op, const lldb_private::DataExtractor &opcodes,
lldb::offset_t &offset, std::vector<lldb_private::Value> &stack) const {
return GetBaseSymbolFile().ParseVendorDWARFOpcode(op, opcodes, offset, stack);
uint8_t op, const DataExtractor &opcodes, lldb::offset_t &offset,
RegisterContext *reg_ctx, lldb::RegisterKind reg_kind,
std::vector<Value> &stack) const {
return GetBaseSymbolFile().ParseVendorDWARFOpcode(op, opcodes, offset,
reg_ctx, reg_kind, stack);
}

llvm::DenseMap<const DWARFDebugInfoEntry *, Type *> &
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class SymbolFileDWARFDwo : public SymbolFileDWARF {
uint64_t GetDebugInfoSize(bool load_all_debug_info = false) override;

bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
lldb::offset_t &offset,
lldb::offset_t &offset, RegisterContext *reg_ctx,
lldb::RegisterKind reg_kind,
std::vector<Value> &stack) const override;

void FindGlobalVariables(ConstString name,
Expand Down
100 changes: 100 additions & 0 deletions lldb/source/Plugins/SymbolFile/DWARF/SymbolFileWasm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "SymbolFileWasm.h"
#include "Plugins/SymbolFile/DWARF/LogChannelDWARF.h"
#include "Utility/WasmVirtualRegisters.h"
#include "lldb/Utility/LLDBLog.h"

using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::plugin::dwarf;

SymbolFileWasm::SymbolFileWasm(ObjectFileSP objfile_sp,
SectionList *dwo_section_list)
: SymbolFileDWARF(objfile_sp, dwo_section_list) {}

SymbolFileWasm::~SymbolFileWasm() = default;

lldb::offset_t
SymbolFileWasm::GetVendorDWARFOpcodeSize(const DataExtractor &data,
const lldb::offset_t data_offset,
const uint8_t op) const {
if (op != llvm::dwarf::DW_OP_WASM_location)
return LLDB_INVALID_OFFSET;

lldb::offset_t offset = data_offset;
const uint8_t wasm_op = data.GetU8(&offset);
switch (wasm_op) {
case 0: // LOCAL
case 1: // GLOBAL_FIXED
case 2: // OPERAND_STACK
data.GetULEB128(&offset);
break;
case 3: // GLOBAL_RELOC
data.GetU32(&offset);
break;
default:
return LLDB_INVALID_OFFSET;
}

return offset - data_offset;
}

bool SymbolFileWasm::ParseVendorDWARFOpcode(uint8_t op,
const DataExtractor &opcodes,
lldb::offset_t &offset,
RegisterContext *reg_ctx,
lldb::RegisterKind reg_kind,
std::vector<Value> &stack) const {
if (op != llvm::dwarf::DW_OP_WASM_location)
return false;

uint32_t index = 0;
uint8_t tag = eWasmTagNotAWasmLocation;

/// |DWARF Location Index | WebAssembly Construct |
/// |---------------------|-----------------------|
/// |0 | Local |
/// |1 or 3 | Global |
/// |2 | Operand Stack |
const uint8_t wasm_op = opcodes.GetU8(&offset);
switch (wasm_op) {
case 0: // LOCAL
index = opcodes.GetULEB128(&offset);
tag = eWasmTagLocal;
break;
case 1: // GLOBAL_FIXED
index = opcodes.GetULEB128(&offset);
tag = eWasmTagGlobal;
break;
case 2: // OPERAND_STACK
index = opcodes.GetULEB128(&offset);
tag = eWasmTagOperandStack;
break;
case 3: // GLOBAL_RELOC
index = opcodes.GetU32(&offset);
tag = eWasmTagGlobal;
break;
default:
return false;
}

const uint32_t reg_num = GetWasmRegister(tag, index);

Value tmp;
llvm::Error error = DWARFExpression::ReadRegisterValueAsScalar(
reg_ctx, reg_kind, reg_num, tmp);
if (error) {
LLDB_LOG_ERROR(GetLog(DWARFLog::DebugInfo), std::move(error), "{0}");
return false;
}

stack.push_back(tmp);
return true;
}
34 changes: 34 additions & 0 deletions lldb/source/Plugins/SymbolFile/DWARF/SymbolFileWasm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEWASM_H
#define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEWASM_H

#include "SymbolFileDWARF.h"

namespace lldb_private::plugin {
namespace dwarf {
class SymbolFileWasm : public SymbolFileDWARF {
public:
SymbolFileWasm(lldb::ObjectFileSP objfile_sp, SectionList *dwo_section_list);

~SymbolFileWasm() override;

lldb::offset_t GetVendorDWARFOpcodeSize(const DataExtractor &data,
const lldb::offset_t data_offset,
const uint8_t op) const override;

bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
lldb::offset_t &offset, RegisterContext *reg_ctx,
lldb::RegisterKind reg_kind,
std::vector<Value> &stack) const override;
};
} // namespace dwarf
} // namespace lldb_private::plugin

#endif
53 changes: 53 additions & 0 deletions lldb/source/Utility/WasmVirtualRegisters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_SOURCE_UTILITY_WASM_VIRTUAL_REGISTERS_H
#define LLDB_SOURCE_UTILITY_WASM_VIRTUAL_REGISTERS_H

#include "lldb/lldb-private.h"

namespace lldb_private {

// LLDB doesn't have an address space to represents WebAssembly locals,
// globals and operand stacks. We encode these elements into virtual
// registers:
//
// | tag: 2 bits | index: 30 bits |
//
// Where tag is:
// 0: Not a Wasm ___location
// 1: Local
// 2: Global
// 3: Operand stack value
enum WasmVirtualRegisterKinds {
eWasmTagNotAWasmLocation = 0,
eWasmTagLocal = 1,
eWasmTagGlobal = 2,
eWasmTagOperandStack = 3,
};

static const uint32_t kWasmVirtualRegisterTagMask = 0x03;
static const uint32_t kWasmVirtualRegisterIndexMask = 0x3fffffff;
static const uint32_t kWasmVirtualRegisterTagShift = 30;

inline uint32_t GetWasmVirtualRegisterTag(size_t reg) {
return (reg >> kWasmVirtualRegisterTagShift) & kWasmVirtualRegisterTagMask;
}

inline uint32_t GetWasmVirtualRegisterIndex(size_t reg) {
return reg & kWasmVirtualRegisterIndexMask;
}

inline uint32_t GetWasmRegister(uint8_t tag, uint32_t index) {
return ((tag & kWasmVirtualRegisterTagMask) << kWasmVirtualRegisterTagShift) |
(index & kWasmVirtualRegisterIndexMask);
}

} // namespace lldb_private

#endif
2 changes: 2 additions & 0 deletions lldb/unittests/Expression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ add_lldb_unittest(ExpressionTests
LINK_LIBS
lldbCore
lldbPluginObjectFileELF
lldbPluginObjectFileWasm
lldbPluginSymbolVendorWasm
lldbPluginPlatformLinux
lldbPluginExpressionParserClang
lldbPluginTypeSystemClang
Expand Down
Loading
Loading