Skip to content

Commit 60cfc85

Browse files
authored
DWARF: Update debug_ranges (#2612)
Pretty straightforward given all we have so far. Note that fannkuch3_manyopts has an example of a sequence of ranges of which some must be skipped while others must not, showing we handle that by skipping the bad ones and updating the remaining. That is, if that we have a sequence of two (begin, end) spans [(10, 20), (30, 40)] It's possible (10, 20) maps in the new binary to (110, 120) while (30, 40) was eliminated by the optimizer and we have nothing valid to map it to. In that case we emit [(110, 120)]
1 parent 70a6817 commit 60cfc85

File tree

6 files changed

+94
-46
lines changed

6 files changed

+94
-46
lines changed

src/wasm/wasm-debug.cpp

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ struct AddrExprMap {
368368
}
369369
}
370370

371-
372371
Expression* getStart(BinaryLocation addr) const {
373372
auto iter = startMap.find(addr);
374373
if (iter != startMap.end()) {
@@ -498,6 +497,10 @@ struct LocationUpdater {
498497
return 0;
499498
}
500499

500+
bool hasOldExprEndAddr(BinaryLocation oldAddr) const {
501+
return oldExprAddrMap.getEnd(oldAddr);
502+
}
503+
501504
BinaryLocation getNewFuncStartAddr(BinaryLocation oldAddr) const {
502505
if (auto* func = oldFuncAddrMap.getStart(oldAddr)) {
503506
// The function might have been optimized away, check.
@@ -612,7 +615,6 @@ static void updateDebugLines(llvm::DWARFYAML::Data& data,
612615
} else if (locationUpdater.hasOldExtraAddr(oldAddr)) {
613616
newAddr = locationUpdater.getNewExtraAddr(oldAddr);
614617
}
615-
// TODO: last 'end' of a function
616618
if (newAddr) {
617619
// LLVM sometimes emits the same address more than once. We should
618620
// probably investigate that.
@@ -764,6 +766,55 @@ static void updateCompileUnits(const BinaryenDWARFInfo& info,
764766
});
765767
}
766768

769+
static void updateRanges(llvm::DWARFYAML::Data& yaml,
770+
const LocationUpdater& locationUpdater) {
771+
// In each range section, try to update the start and end. If we no longer
772+
// have something to map them to, we must skip that part.
773+
size_t skip = 0;
774+
for (size_t i = 0; i < yaml.Ranges.size(); i++) {
775+
auto& range = yaml.Ranges[i];
776+
BinaryLocation oldStart = range.Start, oldEnd = range.End, newStart = 0,
777+
newEnd = 0;
778+
// If this was not an end marker, try to find what it should be updated to.
779+
if (oldStart != 0 && oldEnd != 0) {
780+
if (locationUpdater.hasOldExprAddr(oldStart)) {
781+
newStart = locationUpdater.getNewExprAddr(oldStart);
782+
} else if (locationUpdater.hasOldFuncStartAddr(oldStart)) {
783+
newStart = locationUpdater.getNewFuncStartAddr(oldStart);
784+
}
785+
if (locationUpdater.hasOldExprEndAddr(oldEnd)) {
786+
newEnd = locationUpdater.getNewExprEndAddr(oldEnd);
787+
} else if (locationUpdater.hasOldFuncEndAddr(oldEnd)) {
788+
newEnd = locationUpdater.getNewFuncEndAddr(oldEnd);
789+
}
790+
if (newStart == 0 || newEnd == 0) {
791+
// This part of the range no longer has a mapping, so we must skip it.
792+
skip++;
793+
continue;
794+
}
795+
// The range start and end markers have been preserved. However, TODO
796+
// instructions in the middle may have moved around, making the range no
797+
// longer contiguous, we should check that, and possibly split/merge
798+
// the range. Or, we may need to have tracking in the IR for this.
799+
} else {
800+
// This was not a valid range in the old binary. It was either two 0's
801+
// (an end marker) or an invalid value that should be ignored. Either way,
802+
// write an end marker and finish the current section of ranges, filling
803+
// it out to the original size (we must fill it out as indexes into
804+
// the ranges section are not updated - we could update them and then
805+
// pack the section, as an optimization TODO).
806+
while (skip) {
807+
auto& writtenRange = yaml.Ranges[i - skip];
808+
writtenRange.Start = writtenRange.End = 0;
809+
skip--;
810+
}
811+
}
812+
auto& writtenRange = yaml.Ranges[i - skip];
813+
writtenRange.Start = newStart;
814+
writtenRange.End = newEnd;
815+
}
816+
}
817+
767818
void writeDWARFSections(Module& wasm, const BinaryLocations& newLocations) {
768819
BinaryenDWARFInfo info(wasm);
769820

@@ -779,6 +830,8 @@ void writeDWARFSections(Module& wasm, const BinaryLocations& newLocations) {
779830

780831
updateCompileUnits(info, data, locationUpdater);
781832

833+
updateRanges(data, locationUpdater);
834+
782835
// Convert to binary sections.
783836
auto newSections =
784837
EmitDebugSections(data, false /* EmitFixups for debug_info */);

test/passes/fannkuch0.bin.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,9 +2420,9 @@ Abbrev table for offset: 0x00000000
24202420
DW_AT_comp_dir [DW_FORM_strp] ( .debug_str[0x000000a8] = "/home/alon/Dev/emscripten")
24212421
DW_AT_low_pc [DW_FORM_addr] (0x0000000000000000)
24222422
DW_AT_ranges [DW_FORM_sec_offset] (0x00000000
2423-
[0x00000006, 0x0000088c)
2424-
[0x0000088e, 0x000009dc)
2425-
[0x000009de, 0x00001042))
2423+
[0x00000006, 0x00000872)
2424+
[0x00000874, 0x000009a0)
2425+
[0x000009a2, 0x00000fde))
24262426

24272427
0x00000026: DW_TAG_pointer_type [2]
24282428
DW_AT_type [DW_FORM_ref4] (cu + 0x002b => {0x0000002b} "worker_args")
@@ -5188,9 +5188,9 @@ file_names[ 3]:
51885188
0x00000191: "cleanup"
51895189

51905190
.debug_ranges contents:
5191-
00000000 00000006 0000088c
5192-
00000000 0000088e 000009dc
5193-
00000000 000009de 00001042
5191+
00000000 00000006 00000872
5192+
00000000 00000874 000009a0
5193+
00000000 000009a2 00000fde
51945194
00000000 <End of list>
51955195
(module
51965196
(type $i32_=>_i32 (func (param i32) (result i32)))

test/passes/fannkuch3.bin.txt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,8 +2469,8 @@ Abbrev table for offset: 0x00000000
24692469
DW_AT_comp_dir [DW_FORM_strp] ( .debug_str[0x000000a9] = "/usr/local/google/home/azakai/Dev/2-binaryen")
24702470
DW_AT_low_pc [DW_FORM_addr] (0x0000000000000000)
24712471
DW_AT_ranges [DW_FORM_sec_offset] (0x00000040
2472-
[0x00000006, 0x0000039d)
2473-
[0x0000039f, 0x000006e1))
2472+
[0x00000006, 0x00000389)
2473+
[0x0000038b, 0x00000686))
24742474

24752475
0x00000026: DW_TAG_pointer_type [2]
24762476
DW_AT_type [DW_FORM_ref4] (cu + 0x002b => {0x0000002b} "worker_args")
@@ -2678,10 +2678,10 @@ Abbrev table for offset: 0x00000000
26782678

26792679
0x00000159: DW_TAG_lexical_block [14] *
26802680
DW_AT_ranges [DW_FORM_sec_offset] (0x00000000
2681-
[0x00000185, 0x000001c3)
2682-
[0x000001ed, 0x000001f6)
2683-
[0x0000030e, 0x0000034c)
2684-
[0x00000376, 0x0000037f))
2681+
[0x00000175, 0x000001b3)
2682+
[0x000001dd, 0x000001e6)
2683+
[0x00000302, 0x00000340)
2684+
[0x0000036a, 0x00000373))
26852685

26862686
0x0000015e: DW_TAG_variable [12]
26872687
DW_AT_name [DW_FORM_strp] ( .debug_str[0x00000163] = "p0")
@@ -2927,8 +2927,8 @@ Abbrev table for offset: 0x00000000
29272927

29282928
0x000002e3: DW_TAG_lexical_block [14] *
29292929
DW_AT_ranges [DW_FORM_sec_offset] (0x00000028
2930-
[0x00000517, 0x0000055e)
2931-
[0x000005de, 0x0000062b))
2930+
[0x000004d9, 0x00000520)
2931+
[0x0000059e, 0x000005eb))
29322932

29332933
0x000002e8: DW_TAG_variable [26]
29342934
DW_AT_location [DW_FORM_sec_offset] (0x000003bc:
@@ -4778,16 +4778,16 @@ file_names[ 4]:
47784778
0x000001ad: "char"
47794779

47804780
.debug_ranges contents:
4781-
00000000 00000185 000001c3
4782-
00000000 000001ed 000001f6
4783-
00000000 0000030e 0000034c
4784-
00000000 00000376 0000037f
4781+
00000000 00000175 000001b3
4782+
00000000 000001dd 000001e6
4783+
00000000 00000302 00000340
4784+
00000000 0000036a 00000373
47854785
00000000 <End of list>
4786-
00000028 00000517 0000055e
4787-
00000028 000005de 0000062b
4786+
00000028 000004d9 00000520
4787+
00000028 0000059e 000005eb
47884788
00000028 <End of list>
4789-
00000040 00000006 0000039d
4790-
00000040 0000039f 000006e1
4789+
00000040 00000006 00000389
4790+
00000040 0000038b 00000686
47914791
00000040 <End of list>
47924792
(module
47934793
(type $i32_=>_i32 (func (param i32) (result i32)))

test/passes/fannkuch3_manyopts.bin.txt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,8 +2469,8 @@ Abbrev table for offset: 0x00000000
24692469
DW_AT_comp_dir [DW_FORM_strp] ( .debug_str[0x000000a9] = "/usr/local/google/home/azakai/Dev/2-binaryen")
24702470
DW_AT_low_pc [DW_FORM_addr] (0x0000000000000000)
24712471
DW_AT_ranges [DW_FORM_sec_offset] (0x00000040
2472-
[0x00000006, 0x0000039d)
2473-
[0x0000039f, 0x000006e1))
2472+
[0x00000007, 0x0000035a)
2473+
[0x0000035c, 0x0000062b))
24742474

24752475
0x00000026: DW_TAG_pointer_type [2]
24762476
DW_AT_type [DW_FORM_ref4] (cu + 0x002b => {0x0000002b} "worker_args")
@@ -2677,11 +2677,7 @@ Abbrev table for offset: 0x00000000
26772677
DW_AT_type [DW_FORM_ref4] (cu + 0x0059 => {0x00000059} "int")
26782678

26792679
0x00000159: DW_TAG_lexical_block [14] *
2680-
DW_AT_ranges [DW_FORM_sec_offset] (0x00000000
2681-
[0x00000185, 0x000001c3)
2682-
[0x000001ed, 0x000001f6)
2683-
[0x0000030e, 0x0000034c)
2684-
[0x00000376, 0x0000037f))
2680+
DW_AT_ranges [DW_FORM_sec_offset] (0x00000000)
26852681

26862682
0x0000015e: DW_TAG_variable [12]
26872683
DW_AT_name [DW_FORM_strp] ( .debug_str[0x00000163] = "p0")
@@ -2927,8 +2923,7 @@ Abbrev table for offset: 0x00000000
29272923

29282924
0x000002e3: DW_TAG_lexical_block [14] *
29292925
DW_AT_ranges [DW_FORM_sec_offset] (0x00000028
2930-
[0x00000517, 0x0000055e)
2931-
[0x000005de, 0x0000062b))
2926+
[0x00000553, 0x0000059d))
29322927

29332928
0x000002e8: DW_TAG_variable [26]
29342929
DW_AT_location [DW_FORM_sec_offset] (0x000003bc:
@@ -3712,16 +3707,16 @@ file_names[ 4]:
37123707
0x000001ad: "char"
37133708

37143709
.debug_ranges contents:
3715-
00000000 00000185 000001c3
3716-
00000000 000001ed 000001f6
3717-
00000000 0000030e 0000034c
3718-
00000000 00000376 0000037f
37193710
00000000 <End of list>
3720-
00000028 00000517 0000055e
3721-
00000028 000005de 0000062b
3711+
00000008 <End of list>
3712+
00000010 <End of list>
3713+
00000018 <End of list>
3714+
00000020 <End of list>
3715+
00000028 00000553 0000059d
37223716
00000028 <End of list>
3723-
00000040 00000006 0000039d
3724-
00000040 0000039f 000006e1
3717+
00000038 <End of list>
3718+
00000040 00000007 0000035a
3719+
00000040 0000035c 0000062b
37253720
00000040 <End of list>
37263721
(module
37273722
(type $i32_=>_i32 (func (param i32) (result i32)))

test/passes/fib2.bin.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ Abbrev table for offset: 0x00000000
382382
DW_AT_low_pc [DW_FORM_addr] (0x0000000000000000)
383383
DW_AT_ranges [DW_FORM_sec_offset] (0x00000000
384384
[0x00000005, 0x0000003d)
385-
[0x0000003e, 0x00000048))
385+
[0x0000003e, 0x00000044))
386386

387387
0x00000026: DW_TAG_subprogram [2] *
388388
DW_AT_low_pc [DW_FORM_addr] (0x0000000000000005)
@@ -610,7 +610,7 @@ file_names[ 1]:
610610

611611
.debug_ranges contents:
612612
00000000 00000005 0000003d
613-
00000000 0000003e 00000048
613+
00000000 0000003e 00000044
614614
00000000 <End of list>
615615
(module
616616
(type $none_=>_none (func))

test/passes/ignore_missing_func.bin.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ Abbrev table for offset: 0x00000000
622622
DW_AT_comp_dir [DW_FORM_strp] ( .debug_str[0x0000009b] = "/home/alon/Dev/emscripten")
623623
DW_AT_low_pc [DW_FORM_addr] (0x0000000000000000)
624624
DW_AT_ranges [DW_FORM_sec_offset] (0x00000000
625-
[0x00000005, 0x0000005f))
625+
[0x00000005, 0x0000005b))
626626

627627
0x00000026: DW_TAG_variable [2]
628628
DW_AT_name [DW_FORM_strp] ( .debug_str[0x000000b5] = "quine")
@@ -817,9 +817,9 @@ file_names[ 1]:
817817
0x000000e9: "x"
818818

819819
.debug_ranges contents:
820-
00000000 00000005 0000005f
820+
00000000 00000005 0000005b
821821
00000000 <End of list>
822-
00000010 00000060 000000c4
822+
00000010 0000005c 000000ad
823823
00000010 <End of list>
824824
(module
825825
(type $none_=>_none (func))

0 commit comments

Comments
 (0)