Skip to content

Commit bfc57b3

Browse files
committed
[lldb] Create sections fro Wasm segments
This is a continuation of llvm#153494. In a WebAssembly file, the "name" section contains names for the segments in the data section (WASM_NAMES_DATA_SEGMENT). We already parse these as sections, as with this PR, we also create sub-sections for the data segments.
1 parent 177f27d commit bfc57b3

File tree

2 files changed

+64
-26
lines changed

2 files changed

+64
-26
lines changed

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

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,16 @@ ParseFunctions(SectionSP code_section_sp) {
281281
return functions;
282282
}
283283

284-
static llvm::Expected<std::vector<AddressRange>>
284+
struct WasmSegment {
285+
WasmSegment(SectionSP section_sp, lldb::offset_t offset, uint32_t size,
286+
uint32_t flags)
287+
: address_range(section_sp, offset, size), flags(flags) {};
288+
std::string name;
289+
AddressRange address_range;
290+
uint32_t flags = 0;
291+
};
292+
293+
static llvm::Expected<std::vector<WasmSegment>>
285294
ParseData(SectionSP data_section_sp) {
286295
DataExtractor data;
287296
data_section_sp->GetSectionData(data);
@@ -292,7 +301,7 @@ ParseData(SectionSP data_section_sp) {
292301
if (segment_count > std::numeric_limits<uint32_t>::max())
293302
return llvm::createStringError("segment count overflows uint32_t");
294303

295-
std::vector<AddressRange> segments;
304+
std::vector<WasmSegment> segments;
296305
segments.reserve(segment_count);
297306

298307
for (uint32_t i = 0; i < segment_count; ++i) {
@@ -304,7 +313,7 @@ ParseData(SectionSP data_section_sp) {
304313
if (flags > std::numeric_limits<uint32_t>::max())
305314
return llvm::createStringError("segment size overflows uint32_t");
306315

307-
segments.emplace_back(data_section_sp, offset, segment_size);
316+
segments.emplace_back(data_section_sp, offset, segment_size, flags);
308317

309318
std::optional<lldb::offset_t> next_offset =
310319
llvm::checkedAddUnsigned(offset, segment_size);
@@ -319,7 +328,7 @@ ParseData(SectionSP data_section_sp) {
319328
static llvm::Expected<std::vector<Symbol>>
320329
ParseNames(SectionSP name_section_sp,
321330
const std::vector<AddressRange> &function_ranges,
322-
const std::vector<AddressRange> &segment_ranges) {
331+
std::vector<WasmSegment> &segments) {
323332
DataExtractor name_section_data;
324333
name_section_sp->GetSectionData(name_section_data);
325334

@@ -358,12 +367,14 @@ ParseNames(SectionSP name_section_sp,
358367
for (uint64_t i = 0; c && i < count; ++i) {
359368
const uint64_t idx = data.getULEB128(c);
360369
const std::optional<std::string> name = GetWasmString(data, c);
361-
if (!name || idx >= segment_ranges.size())
370+
if (!name || idx >= segments.size())
362371
continue;
372+
// Update the segment name.
373+
segments[i].name = *name;
363374
symbols.emplace_back(
364375
symbols.size(), Mangled(*name), lldb::eSymbolTypeData,
365376
/*external=*/false, /*is_debug=*/false, /*is_trampoline=*/false,
366-
/*is_artificial=*/false, segment_ranges[idx],
377+
/*is_artificial=*/false, segments[i].address_range,
367378
/*size_is_valid=*/true, /*contains_linker_annotations=*/false,
368379
/*flags=*/0);
369380
}
@@ -391,33 +402,34 @@ void ObjectFileWasm::ParseSymtab(Symtab &symtab) {
391402

392403
// The name section contains names and indexes. First parse the data from the
393404
// relevant sections so we can access it by its index.
394-
std::vector<AddressRange> function_ranges;
395-
std::vector<AddressRange> segment_ranges;
405+
std::vector<AddressRange> functions;
406+
std::vector<WasmSegment> segments;
396407

397408
// Parse the code section.
398409
if (SectionSP code_section_sp =
399410
m_sections_up->FindSectionByType(lldb::eSectionTypeCode, false)) {
400-
llvm::Expected<std::vector<AddressRange>> functions =
411+
llvm::Expected<std::vector<AddressRange>> maybe_functions =
401412
ParseFunctions(code_section_sp);
402-
if (!functions) {
403-
LLDB_LOG_ERROR(log, functions.takeError(),
413+
if (!maybe_functions) {
414+
LLDB_LOG_ERROR(log, maybe_functions.takeError(),
404415
"Failed to parse Wasm code section: {0}");
405416
return;
406417
}
407-
function_ranges = *functions;
418+
functions = *maybe_functions;
408419
}
409420

410421
// Parse the data section.
411-
if (SectionSP data_section_sp =
412-
m_sections_up->FindSectionByType(lldb::eSectionTypeData, false)) {
413-
llvm::Expected<std::vector<AddressRange>> segments =
422+
SectionSP data_section_sp =
423+
m_sections_up->FindSectionByType(lldb::eSectionTypeData, false);
424+
if (data_section_sp) {
425+
llvm::Expected<std::vector<WasmSegment>> maybe_segments =
414426
ParseData(data_section_sp);
415-
if (!segments) {
416-
LLDB_LOG_ERROR(log, segments.takeError(),
427+
if (!maybe_segments) {
428+
LLDB_LOG_ERROR(log, maybe_segments.takeError(),
417429
"Failed to parse Wasm data section: {0}");
418430
return;
419431
}
420-
segment_ranges = *segments;
432+
segments = *maybe_segments;
421433
}
422434

423435
// Parse the name section.
@@ -429,7 +441,7 @@ void ObjectFileWasm::ParseSymtab(Symtab &symtab) {
429441
}
430442

431443
llvm::Expected<std::vector<Symbol>> symbols =
432-
ParseNames(name_section_sp, function_ranges, segment_ranges);
444+
ParseNames(name_section_sp, functions, segments);
433445
if (!symbols) {
434446
LLDB_LOG_ERROR(log, symbols.takeError(), "Failed to parse Wasm names: {0}");
435447
return;
@@ -438,6 +450,26 @@ void ObjectFileWasm::ParseSymtab(Symtab &symtab) {
438450
for (const Symbol &symbol : *symbols)
439451
symtab.AddSymbol(symbol);
440452

453+
lldb::user_id_t segment_id = 0;
454+
for (const WasmSegment &segment : segments) {
455+
const lldb::addr_t segment_addr =
456+
segment.address_range.GetBaseAddress().GetFileAddress();
457+
const size_t segment_size = segment.address_range.GetByteSize();
458+
SectionSP segment_sp = std::make_shared<Section>(
459+
/*parent_section_sp=*/data_section_sp, GetModule(),
460+
/*obj_file=*/this,
461+
++segment_id << 8, // 1-based segment index, shifted by 8 bits to avoid
462+
// collision with section IDs.
463+
ConstString(segment.name), eSectionTypeData,
464+
/*file_vm_addr=*/segment_addr,
465+
/*vm_size=*/segment_size,
466+
/*file_offset=*/segment_addr,
467+
/*file_size=*/segment_size,
468+
/*log2align=*/0, segment.flags);
469+
m_sections_up->AddSection(segment_sp);
470+
GetModule()->GetSectionList()->AddSection(segment_sp);
471+
}
472+
441473
symtab.Finalize();
442474
}
443475

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# RUN: yaml2obj %S/Inputs/simple.wasm.yaml -o %t.wasm
2-
# RUN: %lldb %t.wasm -o 'image dump symtab'
2+
# RUN: %lldb %t.wasm -o 'image dump symtab' -o 'image dump sections' | FileCheck %s
33

4-
# CHECK: Code 0x0000000000000002 0x0000000000000002 {{.*}} __wasm_call_ctors
5-
# CHECK: Code 0x0000000000000005 0x0000000000000029 {{.*}} add
6-
# CHECK: Code 0x000000000000002f 0x000000000000004c {{.*}} __original_main
7-
# CHECK: Code 0x000000000000007c 0x0000000000000009 {{.*}} main
8-
# CHECK: Data 0x000000000000022f 0x0000000000000041 {{.*}} .rodata
9-
# CHECK: Data 0x0000000000000270 0x0000000000000000 {{.*}} .data
4+
CHECK: Code 0x0000000000000002 0x0000000000000002 0x00000000 __wasm_call_ctors
5+
CHECK: Code 0x0000000000000005 0x0000000000000029 0x00000000 add
6+
CHECK: Code 0x000000000000002f 0x000000000000004c 0x00000000 __original_main
7+
CHECK: Code 0x000000000000007c 0x0000000000000009 0x00000000 main
8+
CHECK: Data 0x000000000000022f 0x0000000000000041 0x00000000 .rodata
9+
CHECK: Data 0x0000000000000270 0x0000000000000000 0x00000000 .data
10+
11+
CHECK: 0x0000000000000001 code {{.*}} 0x000001a1 0x00000085 0x00000000 symtab-wasm.test.tmp.wasm.code
12+
CHECK: 0x0000000000000003 data {{.*}} 0x0000022c 0x0000001a 0x00000000 symtab-wasm.test.tmp.wasm.data
13+
CHECK: 0x0000000000000040 wasm-name {{.*}} 0x00000251 0x00000059 0x00000000 symtab-wasm.test.tmp.wasm.name
14+
CHECK: 0x0000000000000100 data {{.*}} 0x0000022f 0x00000041 0x00000000 symtab-wasm.test.tmp.wasm.data..rodata
15+
CHECK: 0x0000000000000200 data {{.*}} 0x00000270 0x00000000 0x00000000 symtab-wasm.test.tmp.wasm.data..data

0 commit comments

Comments
 (0)