Commit 83746fd5 authored by Russ Cox's avatar Russ Cox

cmd/link: use current GOROOT for source file paths for standard library

This CL changes the source file information in the
standard library's .a files to say "$GOROOT/src/runtime/chan.go"
(with a literal "$GOROOT") instead of spelling out the actual directory.
The linker then substitutes the actual $GOROOT (or $GOROOT_FINAL)
as appropriate.

If people download a binary distribution to an alternate location,
following the instructions at https://golang.org/doc/install#install,
the code before this CL would end up with source paths pointing to
/usr/local/go no matter where the actual sources were.
Now the source paths for built binaries will point to the actual sources
(hopefully).

The source line information in distributed binaries is not affected:
those will still say /usr/local/go. But binaries people build themselves
(their own programs, not the go distribution programs) will be correct.

Fixing this path also fixes the lookup of the runtime-gdb.py file.

Fixes #5533.

Change-Id: I03729baae3fbd8cd636e016275ee5ad2606e4663
Reviewed-on: https://go-review.googlesource.com/18200
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent dc5315c3
...@@ -74,15 +74,15 @@ func (h *LineHist) setFile(stk *LineStack, file string) { ...@@ -74,15 +74,15 @@ func (h *LineHist) setFile(stk *LineStack, file string) {
abs = filepath.Join(h.Dir, file) abs = filepath.Join(h.Dir, file)
} }
// Remove leading TrimPathPrefix, or else rewrite $GOROOT to $GOROOT_FINAL. // Remove leading TrimPathPrefix, or else rewrite $GOROOT to literal $GOROOT.
if h.TrimPathPrefix != "" && hasPathPrefix(abs, h.TrimPathPrefix) { if h.TrimPathPrefix != "" && hasPathPrefix(abs, h.TrimPathPrefix) {
if abs == h.TrimPathPrefix { if abs == h.TrimPathPrefix {
abs = "" abs = ""
} else { } else {
abs = abs[len(h.TrimPathPrefix)+1:] abs = abs[len(h.TrimPathPrefix)+1:]
} }
} else if h.GOROOT_FINAL != "" && h.GOROOT_FINAL != h.GOROOT && hasPathPrefix(abs, h.GOROOT) { } else if hasPathPrefix(abs, h.GOROOT) {
abs = h.GOROOT_FINAL + abs[len(h.GOROOT):] abs = "$GOROOT" + abs[len(h.GOROOT):]
} }
if abs == "" { if abs == "" {
abs = "??" abs = "??"
......
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
"cmd/internal/obj" "cmd/internal/obj"
"fmt" "fmt"
"log" "log"
"os"
"path/filepath"
) )
// funcpctab writes to dst a pc-value table mapping the code in func to the values // funcpctab writes to dst a pc-value table mapping the code in func to the values
...@@ -150,6 +152,7 @@ func renumberfiles(ctxt *Link, files []*LSym, d *Pcdata) { ...@@ -150,6 +152,7 @@ func renumberfiles(ctxt *Link, files []*LSym, d *Pcdata) {
f.Value = int64(ctxt.Nhistfile) f.Value = int64(ctxt.Nhistfile)
f.Type = obj.SFILEPATH f.Type = obj.SFILEPATH
f.Next = ctxt.Filesyms f.Next = ctxt.Filesyms
f.Name = expandGoroot(f.Name)
ctxt.Filesyms = f ctxt.Filesyms = f
} }
} }
...@@ -376,6 +379,18 @@ func pclntab() { ...@@ -376,6 +379,18 @@ func pclntab() {
} }
} }
func expandGoroot(s string) string {
const n = len("$GOROOT")
if len(s) >= n+1 && s[:n] == "$GOROOT" && (s[n] == '/' || s[n] == '\\') {
root := goroot
if final := os.Getenv("GOROOT_FINAL"); final != "" {
root = final
}
return filepath.ToSlash(filepath.Join(root, s[n:]))
}
return s
}
const ( const (
BUCKETSIZE = 256 * MINFUNC BUCKETSIZE = 256 * MINFUNC
SUBBUCKETS = 16 SUBBUCKETS = 16
......
...@@ -87,6 +87,7 @@ func TestGdbPython(t *testing.T) { ...@@ -87,6 +87,7 @@ func TestGdbPython(t *testing.T) {
args := []string{"-nx", "-q", "--batch", "-iex", args := []string{"-nx", "-q", "--batch", "-iex",
fmt.Sprintf("add-auto-load-safe-path %s/src/runtime", runtime.GOROOT()), fmt.Sprintf("add-auto-load-safe-path %s/src/runtime", runtime.GOROOT()),
"-ex", "info auto-load python-scripts",
"-ex", "br main.go:10", "-ex", "br main.go:10",
"-ex", "run", "-ex", "run",
"-ex", "echo BEGIN info goroutines\n", "-ex", "echo BEGIN info goroutines\n",
...@@ -129,7 +130,10 @@ func TestGdbPython(t *testing.T) { ...@@ -129,7 +130,10 @@ func TestGdbPython(t *testing.T) {
t.Skipf("skipping because GOROOT=%s does not exist", runtime.GOROOT()) t.Skipf("skipping because GOROOT=%s does not exist", runtime.GOROOT())
} }
t.Fatalf("failed to load Go runtime support: %s", firstLine) _, file, _, _ := runtime.Caller(1)
t.Logf("package testing source file: %s", file)
t.Fatalf("failed to load Go runtime support: %s\n%s", firstLine, got)
} }
// Extract named BEGIN...END blocks from output // Extract named BEGIN...END blocks from output
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment