From e256e640604bff7916ef09451da7f4a6423152a6 Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Fri, 7 Mar 2025 14:16:28 -0500 Subject: debug/dwarf: fix problem with DWARF 5 and Seek method When clients use debug/dwarf to examine DWARF 5 binaries, we can run into problems when the Seek() method is used to skip ahead from a DIE in one compilation unit to a DIE in another unit. The problem here is that it is common for DWARF 5 comp units to have attributes (ex: DW_AT_addr_base) whose value must be applied as an offset when reading certain forms (ex: DW_FORM_addrx) within that unit. The existing implementation didn't have a good way to recover these attrs following the Seek call, and had to essentially punt in this case, resulting in incorrect attr values. This patch adds new support for reading and caching the key comp unit DIE attributes (DW_AT_addr_base, DW_AT_loclists_base, etc) prior to visiting any of the DIE entries in a unit, storing the cache values of these attrs the main table of units. This base attribute reading/caching behavior also happens (where needed) after Seek calls. Should resolve delve issue 3861. Supercedes Go pull request 70400. Updates #26379. Fixes #57046. Change-Id: I536a57e2ba4fc55132d91c7f36f67a91ac408dc3 Reviewed-on: https://go-review.googlesource.com/c/go/+/655976 Reviewed-by: Alessandro Arzilli Reviewed-by: Cherry Mui Reviewed-by: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI --- src/debug/dwarf/testdata/issue57046-clang.elf5 | Bin 0 -> 19360 bytes src/debug/dwarf/testdata/issue57046_part1.c | 42 +++++++++++++++++++++++++ src/debug/dwarf/testdata/issue57046_part2.c | 10 ++++++ 3 files changed, 52 insertions(+) create mode 100755 src/debug/dwarf/testdata/issue57046-clang.elf5 create mode 100644 src/debug/dwarf/testdata/issue57046_part1.c create mode 100644 src/debug/dwarf/testdata/issue57046_part2.c (limited to 'src/debug/dwarf/testdata') diff --git a/src/debug/dwarf/testdata/issue57046-clang.elf5 b/src/debug/dwarf/testdata/issue57046-clang.elf5 new file mode 100755 index 0000000000..009af83135 Binary files /dev/null and b/src/debug/dwarf/testdata/issue57046-clang.elf5 differ diff --git a/src/debug/dwarf/testdata/issue57046_part1.c b/src/debug/dwarf/testdata/issue57046_part1.c new file mode 100644 index 0000000000..8866ca66e1 --- /dev/null +++ b/src/debug/dwarf/testdata/issue57046_part1.c @@ -0,0 +1,42 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Part 1 of the sources for issue 57046 test case. + +// Build instructions: +// +// clang-16 -O -g -gdwarf-5 -c issue57046_part1.c +// clang-16 -O -g -gdwarf-5 -c issue57046_part2.c +// clang-16 -o issue57046-clang.elf5 issue57046_part1.o issue57046_part2.o + +#include +#include +#include + +extern const char *mom(); + +int gadgety() { + const char *ev = getenv("PATH"); + int n = strlen(ev); + int s1 = (int)ev[0]; + int s2 = (int)ev[1]; + int s3 = (int)ev[2]; + for (int i = 0; i < strlen(ev); i++) { + if (s1 == 101) { + int t = s1; + s1 = s3; + s3 = t; + } + if (ev[i] == 99) { + printf("%d\n", i); + } + } + s2 *= 2; + return n + s1 + s2; +} + +int main(int argc, char **argv) { + printf("Hi %s %d\n", mom(), gadgety()); + return 0; +} diff --git a/src/debug/dwarf/testdata/issue57046_part2.c b/src/debug/dwarf/testdata/issue57046_part2.c new file mode 100644 index 0000000000..2f4e9f0d24 --- /dev/null +++ b/src/debug/dwarf/testdata/issue57046_part2.c @@ -0,0 +1,10 @@ +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Part 2 of the sources for issue 57046 test case. +// See part 1 for build instructions. + +const char *mom() { + return "mom"; +} -- cgit v1.3