Skip to content

Commit c31231c

Browse files
committed
debug/dwarf: heuristically handle both UNIX and Windows paths
Currently debug/dwarf assumes all paths in line tables will be UNIX-style paths, which obviously isn't the case for binaries built on Windows. However, we can't simply switch from the path package to the filepath package because we don't know that we're running on the same host type that built the binary and we want this to work even if we're not. This is essentially the approach taken by GDB, which treats paths in accordance with the system GDB itself is compiled for. In fact, we can't even guess the compilation system from the type of the binary because it may have been cross-compiled. We fix this by heuristically determining whether paths are UNIX-style or DOS-style by looking for a drive letter or UNC path. If we see a DOS-style path, we use appropriate logic for determining whether the path is absolute and for joining two paths. This is helped by the fact that we should basically always be starting with an absolute path. However, it could mistake a relative UNIX-style path that begins with a directory like "C:" for an absolute DOS-style path. There doesn't seem to be any way around this. Fixes #19784. Change-Id: Ie13b546d2f1dcd8b02e668583a627b571b281588 Reviewed-on: https://go-review.googlesource.com/44017 Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Alex Brainman <[email protected]>
1 parent 6654e3e commit c31231c

File tree

5 files changed

+176
-4
lines changed

5 files changed

+176
-4
lines changed

src/debug/dwarf/export_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package dwarf
6+
7+
var PathJoin = pathJoin

src/debug/dwarf/line.go

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"io"
1111
"path"
12+
"strings"
1213
)
1314

1415
// A LineReader reads a sequence of LineEntry structures from a DWARF
@@ -247,10 +248,10 @@ func (r *LineReader) readHeader() error {
247248
if len(directory) == 0 {
248249
break
249250
}
250-
if !path.IsAbs(directory) {
251+
if !pathIsAbs(directory) {
251252
// Relative paths are implicitly relative to
252253
// the compilation directory.
253-
directory = path.Join(r.directories[0], directory)
254+
directory = pathJoin(r.directories[0], directory)
254255
}
255256
r.directories = append(r.directories, directory)
256257
}
@@ -283,11 +284,11 @@ func (r *LineReader) readFileEntry() (bool, error) {
283284
}
284285
off := r.buf.off
285286
dirIndex := int(r.buf.uint())
286-
if !path.IsAbs(name) {
287+
if !pathIsAbs(name) {
287288
if dirIndex >= len(r.directories) {
288289
return false, DecodeError{"line", off, "directory index too large"}
289290
}
290-
name = path.Join(r.directories[dirIndex], name)
291+
name = pathJoin(r.directories[dirIndex], name)
291292
}
292293
mtime := r.buf.uint()
293294
length := int(r.buf.uint())
@@ -588,3 +589,68 @@ func (r *LineReader) SeekPC(pc uint64, entry *LineEntry) error {
588589
*entry = next
589590
}
590591
}
592+
593+
// pathIsAbs returns whether path is an absolute path (or "full path
594+
// name" in DWARF parlance). This is in "whatever form makes sense for
595+
// the host system", so this accepts both UNIX-style and DOS-style
596+
// absolute paths. We avoid the filepath package because we want this
597+
// to behave the same regardless of our host system and because we
598+
// don't know what system the paths came from.
599+
func pathIsAbs(path string) bool {
600+
_, path = splitDrive(path)
601+
return len(path) > 0 && (path[0] == '/' || path[0] == '\\')
602+
}
603+
604+
// pathJoin joins dirname and filename. filename must be relative.
605+
// DWARF paths can be UNIX-style or DOS-style, so this handles both.
606+
func pathJoin(dirname, filename string) string {
607+
if len(dirname) == 0 {
608+
return filename
609+
}
610+
// dirname should be absolute, which means we can determine
611+
// whether it's a DOS path reasonably reliably by looking for
612+
// a drive letter or UNC path.
613+
drive, dirname := splitDrive(dirname)
614+
if drive == "" {
615+
// UNIX-style path.
616+
return path.Join(dirname, filename)
617+
}
618+
// DOS-style path.
619+
drive2, filename := splitDrive(filename)
620+
if drive2 != "" {
621+
if strings.ToLower(drive) != strings.ToLower(drive2) {
622+
// Different drives. There's not much we can
623+
// do here, so just ignore the directory.
624+
return drive2 + filename
625+
}
626+
// Drives are the same. Ignore drive on filename.
627+
}
628+
if !(strings.HasSuffix(dirname, "/") || strings.HasSuffix(dirname, `\`)) && dirname != "" {
629+
dirname += `\`
630+
}
631+
return drive + dirname + filename
632+
}
633+
634+
// splitDrive splits the DOS drive letter or UNC share point from
635+
// path, if any. path == drive + rest
636+
func splitDrive(path string) (drive, rest string) {
637+
if len(path) >= 2 && path[1] == ':' {
638+
if c := path[0]; 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' {
639+
return path[:2], path[2:]
640+
}
641+
}
642+
if len(path) > 3 && (path[0] == '\\' || path[0] == '/') && (path[1] == '\\' || path[1] == '/') {
643+
// Normalize the path so we can search for just \ below.
644+
npath := strings.Replace(path, "/", `\`, -1)
645+
// Get the host part, which must be non-empty.
646+
slash1 := strings.IndexByte(npath[2:], '\\') + 2
647+
if slash1 > 2 {
648+
// Get the mount-point part, which must be non-empty.
649+
slash2 := strings.IndexByte(npath[slash1+1:], '\\') + slash1 + 1
650+
if slash2 > slash1 {
651+
return path[:slash2], path[slash2:]
652+
}
653+
}
654+
}
655+
return "", path
656+
}

src/debug/dwarf/line_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package dwarf_test
77
import (
88
. "debug/dwarf"
99
"io"
10+
"strings"
1011
"testing"
1112
)
1213

@@ -46,6 +47,46 @@ func TestLineELFGCC(t *testing.T) {
4647
testLineTable(t, want, elfData(t, "testdata/line-gcc.elf"))
4748
}
4849

50+
func TestLineGCCWindows(t *testing.T) {
51+
// Generated by:
52+
// > gcc --version
53+
// gcc (tdm64-1) 4.9.2
54+
// > gcc -g -o line-gcc-win.bin line1.c C:\workdir\go\src\debug\dwarf\testdata\line2.c
55+
56+
toWindows := func(lf *LineFile) *LineFile {
57+
lf2 := *lf
58+
lf2.Name = strings.Replace(lf2.Name, "/home/austin/go.dev/", "C:\\workdir\\go\\", -1)
59+
lf2.Name = strings.Replace(lf2.Name, "/", "\\", -1)
60+
return &lf2
61+
}
62+
file1C := toWindows(file1C)
63+
file1H := toWindows(file1H)
64+
file2C := toWindows(file2C)
65+
66+
// Line table based on objdump --dwarf=rawline,decodedline
67+
want := []LineEntry{
68+
{Address: 0x401530, File: file1H, Line: 2, IsStmt: true},
69+
{Address: 0x401538, File: file1H, Line: 5, IsStmt: true},
70+
{Address: 0x401541, File: file1H, Line: 6, IsStmt: true, Discriminator: 3},
71+
{Address: 0x40154b, File: file1H, Line: 5, IsStmt: true, Discriminator: 3},
72+
{Address: 0x40154f, File: file1H, Line: 5, IsStmt: false, Discriminator: 1},
73+
{Address: 0x401555, File: file1H, Line: 7, IsStmt: true},
74+
{Address: 0x40155b, File: file1C, Line: 6, IsStmt: true},
75+
{Address: 0x401563, File: file1C, Line: 6, IsStmt: true},
76+
{Address: 0x401568, File: file1C, Line: 7, IsStmt: true},
77+
{Address: 0x40156d, File: file1C, Line: 8, IsStmt: true},
78+
{Address: 0x401572, File: file1C, Line: 9, IsStmt: true},
79+
{Address: 0x401578, EndSequence: true},
80+
81+
{Address: 0x401580, File: file2C, Line: 4, IsStmt: true},
82+
{Address: 0x401588, File: file2C, Line: 5, IsStmt: true},
83+
{Address: 0x401595, File: file2C, Line: 6, IsStmt: true},
84+
{Address: 0x40159b, EndSequence: true},
85+
}
86+
87+
testLineTable(t, want, peData(t, "testdata/line-gcc-win.bin"))
88+
}
89+
4990
func TestLineELFClang(t *testing.T) {
5091
// Generated by:
5192
// # clang --version | head -n1
@@ -183,6 +224,11 @@ func testLineTable(t *testing.T, want []LineEntry, d *Data) {
183224
}
184225
t.Fatal("lr.Next:", err)
185226
}
227+
// Ignore sources from the Windows build environment.
228+
if strings.HasPrefix(line.File.Name, "C:\\crossdev\\") ||
229+
strings.HasPrefix(line.File.Name, "C:/crossdev/") {
230+
continue
231+
}
186232
got = append(got, line)
187233
}
188234
}
@@ -227,3 +273,42 @@ func dumpLines(t *testing.T, lines []LineEntry) {
227273
t.Logf(" %+v File:%+v", l, l.File)
228274
}
229275
}
276+
277+
type joinTest struct {
278+
dirname, filename string
279+
path string
280+
}
281+
282+
var joinTests = []joinTest{
283+
{"a", "b", "a/b"},
284+
{"a", "", "a"},
285+
{"", "b", "b"},
286+
{"/a", "b", "/a/b"},
287+
{"/a/", "b", "/a/b"},
288+
289+
{`C:\Windows\`, `System32`, `C:\Windows\System32`},
290+
{`C:\Windows\`, ``, `C:\Windows\`},
291+
{`C:\`, `Windows`, `C:\Windows`},
292+
{`C:\Windows\`, `C:System32`, `C:\Windows\System32`},
293+
{`C:\Windows`, `a/b`, `C:\Windows\a/b`},
294+
{`\\host\share\`, `foo`, `\\host\share\foo`},
295+
{`\\host\share\`, `foo\bar`, `\\host\share\foo\bar`},
296+
{`//host/share/`, `foo/bar`, `//host/share/foo/bar`},
297+
298+
// The following are "best effort". We shouldn't see relative
299+
// base directories in DWARF, but these test that pathJoin
300+
// doesn't fail miserably if it sees one.
301+
{`C:`, `a`, `C:a`},
302+
{`C:`, `a\b`, `C:a\b`},
303+
{`C:.`, `a`, `C:.\a`},
304+
{`C:a`, `b`, `C:a\b`},
305+
}
306+
307+
func TestPathJoin(t *testing.T) {
308+
for _, test := range joinTests {
309+
got := PathJoin(test.dirname, test.filename)
310+
if test.path != got {
311+
t.Errorf("pathJoin(%q, %q) = %q, want %q", test.dirname, test.filename, got, test.path)
312+
}
313+
}
314+
}
130 KB
Binary file not shown.

src/debug/dwarf/type_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
. "debug/dwarf"
99
"debug/elf"
1010
"debug/macho"
11+
"debug/pe"
1112
"testing"
1213
)
1314

@@ -67,6 +68,19 @@ func machoData(t *testing.T, name string) *Data {
6768
return d
6869
}
6970

71+
func peData(t *testing.T, name string) *Data {
72+
f, err := pe.Open(name)
73+
if err != nil {
74+
t.Fatal(err)
75+
}
76+
77+
d, err := f.DWARF()
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
return d
82+
}
83+
7084
func TestTypedefsELF(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf"), "elf") }
7185

7286
func TestTypedefsMachO(t *testing.T) {

0 commit comments

Comments
 (0)