Skip to content

Commit 3e0e3dd

Browse files
committed
[lldb] Support DW_OP_WASM_location in DWARFExpression (llvm#151010)
Add support for DW_OP_WASM_location in DWARFExpression. This PR rebases llvm#78977 and cleans up the unit test. The DWARF extensions are documented at https://yurydelendik.github.io/webassembly-dwarf/ and supported by LLVM-based toolchains such as Clang, Swift, Emscripten, and Rust. (cherry picked from commit c2548a8)
1 parent 0a0f386 commit 3e0e3dd

File tree

15 files changed

+658
-169
lines changed

15 files changed

+658
-169
lines changed

lldb/include/lldb/Expression/DWARFExpression.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ class DWARFExpression {
5252
GetVendorDWARFOpcodeSize(const DataExtractor &data,
5353
const lldb::offset_t data_offset,
5454
const uint8_t op) const = 0;
55-
virtual bool ParseVendorDWARFOpcode(uint8_t op,
56-
const DataExtractor &opcodes,
57-
lldb::offset_t &offset,
58-
Stack &stack) const = 0;
55+
virtual bool
56+
ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
57+
lldb::offset_t &offset, RegisterContext *reg_ctx,
58+
lldb::RegisterKind reg_kind, Stack &stack) const = 0;
5959

6060
Delegate(const Delegate &) = delete;
6161
Delegate &operator=(const Delegate &) = delete;
@@ -163,6 +163,10 @@ class DWARFExpression {
163163

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

166+
static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
167+
lldb::RegisterKind reg_kind,
168+
uint32_t reg_num, Value &value);
169+
166170
private:
167171
/// A data extractor capable of reading opcode bytes
168172
DataExtractor m_data;

lldb/source/Expression/DWARFExpression.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
9595
m_reg_kind = reg_kind;
9696
}
9797

98-
static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
99-
lldb::RegisterKind reg_kind,
100-
uint32_t reg_num, Value &value) {
98+
llvm::Error
99+
DWARFExpression::ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
100+
lldb::RegisterKind reg_kind,
101+
uint32_t reg_num, Value &value) {
101102
if (reg_ctx == nullptr)
102103
return llvm::createStringError("no register context in frame");
103104

@@ -2386,7 +2387,8 @@ llvm::Expected<Value> DWARFExpression::Evaluate(
23862387

23872388
default:
23882389
if (dwarf_cu) {
2389-
if (dwarf_cu->ParseVendorDWARFOpcode(op, opcodes, offset, stack)) {
2390+
if (dwarf_cu->ParseVendorDWARFOpcode(op, opcodes, offset, reg_ctx,
2391+
reg_kind, stack)) {
23902392
break;
23912393
}
23922394
}

lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,13 @@ DataExtractor ObjectFileWasm::ReadImageData(offset_t offset, uint32_t size) {
376376
DataBufferSP buffer_sp(data_up.release());
377377
data.SetData(buffer_sp, 0, buffer_sp->GetByteSize());
378378
}
379+
} else if (offset < m_data.GetByteSize()) {
380+
size =
381+
std::min(static_cast<uint64_t>(size), m_data.GetByteSize() - offset);
382+
return DataExtractor(m_data.GetDataStart() + offset, size, GetByteOrder(),
383+
GetAddressByteSize());
379384
}
380385
}
381-
382386
data.SetByteOrder(GetByteOrder());
383387
return data;
384388
}

lldb/source/Plugins/SymbolFile/DWARF/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ add_lldb_library(lldbPluginSymbolFileDWARF PLUGIN
4141
SymbolFileDWARF.cpp
4242
SymbolFileDWARFDwo.cpp
4343
SymbolFileDWARFDebugMap.cpp
44+
SymbolFileWasm.cpp
4445
UniqueDWARFASTType.cpp
4546
${SWIFT_SOURCES}
4647

lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,11 @@ DWARFUnit::GetVendorDWARFOpcodeSize(const DataExtractor &data,
736736

737737
bool DWARFUnit::ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
738738
lldb::offset_t &offset,
739+
RegisterContext *reg_ctx,
740+
lldb::RegisterKind reg_kind,
739741
std::vector<Value> &stack) const {
740742
return GetSymbolFileDWARF().ParseVendorDWARFOpcode(op, opcodes, offset,
741-
stack);
743+
reg_ctx, reg_kind, stack);
742744
}
743745

744746
bool DWARFUnit::ParseDWARFLocationList(

lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,11 @@ class DWARFUnit : public DWARFExpression::Delegate, public UserID {
164164
const lldb::offset_t data_offset,
165165
const uint8_t op) const override;
166166

167-
bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
168-
lldb::offset_t &offset,
169-
std::vector<Value> &stack) const override;
167+
virtual bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
168+
lldb::offset_t &offset,
169+
RegisterContext *reg_ctx,
170+
lldb::RegisterKind reg_kind,
171+
std::vector<Value> &stack) const override;
170172

171173
bool ParseDWARFLocationList(const DataExtractor &data,
172174
DWARFExpressionList &loc_list) const;

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
4444
#include "Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.h"
45+
#include "Plugins/SymbolFile/DWARF/SymbolFileWasm.h"
4546
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
4647
#include "lldb/Symbol/Block.h"
4748
#include "lldb/Symbol/CompileUnit.h"
@@ -328,6 +329,9 @@ llvm::StringRef SymbolFileDWARF::GetPluginDescriptionStatic() {
328329
}
329330

330331
SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFileSP objfile_sp) {
332+
if (objfile_sp->GetArchitecture().GetTriple().isWasm())
333+
return new SymbolFileWasm(std::move(objfile_sp),
334+
/*dwo_section_list*/ nullptr);
331335
return new SymbolFileDWARF(std::move(objfile_sp),
332336
/*dwo_section_list*/ nullptr);
333337
}

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ class SymbolFileDWARF : public SymbolFileCommon {
349349

350350
virtual bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes,
351351
lldb::offset_t &offset,
352+
RegisterContext *reg_ctx,
353+
lldb::RegisterKind reg_kind,
352354
std::vector<Value> &stack) const {
353355
return false;
354356
}
@@ -586,6 +588,7 @@ class SymbolFileDWARF : public SymbolFileCommon {
586588
/// an index that identifies the .DWO or .o file.
587589
std::optional<uint64_t> m_file_index;
588590
};
591+
589592
} // namespace dwarf
590593
} // namespace lldb_private::plugin
591594

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ uint64_t SymbolFileDWARFDwo::GetDebugInfoSize(bool load_all_debug_info) {
9797
}
9898

9999
bool SymbolFileDWARFDwo::ParseVendorDWARFOpcode(
100-
uint8_t op, const lldb_private::DataExtractor &opcodes,
101-
lldb::offset_t &offset, std::vector<lldb_private::Value> &stack) const {
102-
return GetBaseSymbolFile().ParseVendorDWARFOpcode(op, opcodes, offset, stack);
100+
uint8_t op, const DataExtractor &opcodes, lldb::offset_t &offset,
101+
RegisterContext *reg_ctx, lldb::RegisterKind reg_kind,
102+
std::vector<Value> &stack) const {
103+
return GetBaseSymbolFile().ParseVendorDWARFOpcode(op, opcodes, offset,
104+
reg_ctx, reg_kind, stack);
103105
}
104106

105107
llvm::DenseMap<const DWARFDebugInfoEntry *, Type *> &

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class SymbolFileDWARFDwo : public SymbolFileDWARF {
5050
uint64_t GetDebugInfoSize(bool load_all_debug_info = false) override;
5151

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

5657
void FindGlobalVariables(ConstString name,

0 commit comments

Comments
 (0)