Commit 56493f7154 for golang
commit 56493f71546da7fc75f8fe6f6fb208a5cadeae54
Author: Matthew Suozzo <msuozzo@google.com>
Date: Mon Nov 9 19:43:51 2026 +0000
debug/dwarf: fix DWARF 5 line table path resolution
In DWARF 5 line tables, directory entry 0 is the compilation directory
and additional relative directory entries are relative to it, while file
entries may already be full path names. readHeader stored DWARF 5
directory entries without resolving relative paths, and readLNCT called
pathJoin even when the path was already absolute.
Join relative DWARF 5 directory entries past entry 0 onto entry 0 in
readHeader, and skip pathJoin in readLNCT when pathIsAbs(path) is true,
matching the DWARF 2-4 handling.
Fixes #82944
Change-Id: Ifd28c2f0ab30c711ad18c85e528c9afe6a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/861882
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Bypass: David Chase <drchase@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Than McIntosh <thanm@golang.org>
diff --git a/src/debug/dwarf/line.go b/src/debug/dwarf/line.go
index 5a6daa6428..8270a65341 100644
--- a/src/debug/dwarf/line.go
+++ b/src/debug/dwarf/line.go
@@ -301,6 +301,10 @@ func (r *LineReader) readHeader(compDir string) error {
if err != nil {
return err
}
+ if i > 0 && !pathIsAbs(dir) {
+ // Relative paths are relative to the compilation directory in entry 0.
+ dir = pathJoin(r.directories[0], dir)
+ }
r.directories[i] = dir
}
fileFormat := r.readLNCTFormat()
@@ -436,7 +440,7 @@ func (r *LineReader) readLNCT(s []lnctForm, dwarf64 bool) (path string, mtime ui
}
}
- if dir != "" && path != "" {
+ if dir != "" && path != "" && !pathIsAbs(path) {
path = pathJoin(dir, path)
}
diff --git a/src/debug/dwarf/line_test.go b/src/debug/dwarf/line_test.go
index e90b8251ae..0e1614ecd4 100644
--- a/src/debug/dwarf/line_test.go
+++ b/src/debug/dwarf/line_test.go
@@ -6,6 +6,7 @@ package dwarf_test
import (
. "debug/dwarf"
+ "encoding/binary"
"io"
"strings"
"testing"
@@ -155,6 +156,99 @@ func TestLineELFClang(t *testing.T) {
testLineTable(t, want, files, elfData(t, "testdata/line-clang.elf"))
}
+func TestLineELFClangDwarf5(t *testing.T) {
+ cfile1C := &LineFile{Name: "/usr/local/google/home/iant/go/src/debug/dwarf/testdata/line1.c"}
+ cfile1H := &LineFile{Name: "/usr/local/google/home/iant/go/src/debug/dwarf/testdata/line1.h"}
+ cfile2C := &LineFile{Name: "/usr/local/google/home/iant/go/src/debug/dwarf/testdata/line2.c"}
+
+ want := []LineEntry{
+ {Address: 0x401130, File: cfile1C, Line: 6, IsStmt: true},
+ {Address: 0x401134, File: cfile1C, Line: 7, Column: 2, IsStmt: true, PrologueEnd: true},
+ {Address: 0x401139, File: cfile1C, Line: 8, Column: 2, IsStmt: true},
+ {Address: 0x401142, File: cfile1C, Line: 9, Column: 1, IsStmt: true},
+ {Address: 0x401150, File: cfile1H, Line: 2, IsStmt: true},
+ {Address: 0x401154, File: cfile1H, Line: 5, Column: 8, IsStmt: true, PrologueEnd: true},
+ {Address: 0x40115b, File: cfile1H, Line: 5, Column: 15, IsStmt: false},
+ {Address: 0x40115f, File: cfile1H, Line: 5, Column: 2, IsStmt: false},
+ {Address: 0x401165, File: cfile1H, Line: 6, Column: 3, IsStmt: true},
+ {Address: 0x401169, File: cfile1H, Line: 6, Column: 10, IsStmt: false},
+ {Address: 0x40116e, File: cfile1H, Line: 5, Column: 22, IsStmt: true},
+ {Address: 0x401177, File: cfile1H, Line: 5, Column: 2, IsStmt: false},
+ {Address: 0x40117c, File: cfile1H, Line: 7, Column: 1, IsStmt: true},
+ {Address: 0x40117e, EndSequence: true},
+
+ {Address: 0x401180, File: cfile2C, Line: 4, IsStmt: true},
+ {Address: 0x401184, File: cfile2C, Line: 5, Column: 2, IsStmt: true, PrologueEnd: true},
+ {Address: 0x401195, File: cfile2C, Line: 6, Column: 1, IsStmt: true},
+ {Address: 0x401197, EndSequence: true},
+ }
+ files := [][]*LineFile{{cfile1C, cfile1H}, {cfile2C}}
+
+ testLineTable(t, want, files, elfData(t, "testdata/line-clang-dwarf5.elf"))
+}
+
+func TestLineDWARF5Paths(t *testing.T) {
+ // DWARF 5 §6.2.4 puts the compilation directory in directory 0,
+ // resolves additional relative directories against it, and leaves
+ // full file path names unprefixed.
+ dirs := []string{"/work/build", "../include", "/opt/inc"}
+ files := []struct {
+ name string
+ dir byte
+ want string
+ }{
+ {name: "main.c", dir: 0, want: "/work/build/main.c"},
+ {name: "a.h", dir: 1, want: "/work/include/a.h"},
+ {name: "b.h", dir: 2, want: "/opt/inc/b.h"},
+ {name: "/abs/c.h", dir: 0, want: "/abs/c.h"},
+ {name: "/abs/d.h", dir: 1, want: "/abs/d.h"},
+ {name: `C:\abs\e.h`, dir: 1, want: `C:\abs\e.h`},
+ }
+
+ hdr := []byte{
+ 1, 1, 1, 0xfb, 14, // min_inst_length, max_ops, default_is_stmt, line_base, line_range
+ 13, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, // opcode_base, standard_opcode_lengths
+ 1, 0x01, 0x08, byte(len(dirs)), // DW_LNCT_path, DW_FORM_string
+ }
+ for _, d := range dirs {
+ hdr = append(append(hdr, d...), 0)
+ }
+ hdr = append(hdr, 2, 0x01, 0x08, 0x02, 0x0f, byte(len(files))) // +DW_LNCT_directory_index, DW_FORM_udata
+ for _, f := range files {
+ hdr = append(append(hdr, f.name...), 0, f.dir)
+ }
+ unit := []byte{5, 0, 8, 0} // version 5, address_size, seg_sel_size
+ unit = binary.LittleEndian.AppendUint32(unit, uint32(len(hdr)))
+ line := binary.LittleEndian.AppendUint32(nil, uint32(len(unit)+len(hdr)))
+ line = append(append(line, unit...), hdr...)
+
+ // Omit DW_AT_comp_dir so directories are resolved against entry 0.
+ abbrev := []byte{1, 0x11, 0, 0x10, 0x17, 0, 0, 0}
+ info := []byte{13, 0, 0, 0, 5, 0, 1, 8, 0, 0, 0, 0, 1, 0, 0, 0, 0}
+
+ d, err := New(abbrev, nil, nil, info, line, nil, nil, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ cu, err := d.Reader().Next()
+ if err != nil {
+ t.Fatal(err)
+ }
+ lr, err := d.LineReader(cu)
+ if err != nil {
+ t.Fatal(err)
+ }
+ got := lr.Files()
+ if len(got) != len(files) {
+ t.Fatalf("len(Files()) = %d, want %d", len(got), len(files))
+ }
+ for i, f := range files {
+ if got[i].Name != f.want {
+ t.Errorf("Files()[%d] (%q, dir %d) = %q, want %q", i, f.name, f.dir, got[i].Name, f.want)
+ }
+ }
+}
+
func TestLineRnglists(t *testing.T) {
// Test a newer file, generated by clang.
file := &LineFile{Name: "/usr/local/google/home/iant/foo.c"}