Skip to content

cmd/cgo: does not replace references to paths in GOROOT/ with $GOROOT #21825

@orivej

Description

@orivej

Normally Go compiles its own sources such that debug info pathnames look like $GOROOT/src/package/path.go rather than /absolute/path/to/go/src/package/path.go. However, this does not happen when compiling sources with cgo. In particular src/plugin/plugin_dlopen.go produces this temporary file plugin_dlopen.cgo1.go:

//line /tmp/nds-install-go_1_9/share/go/src/plugin/plugin_dlopen.go:43
func lastIndexByte(s string, c byte) int {
…

and after unpacking the resulting plugin.a (ar x plugin.a) and disassemly (go tool objdump _go_.o) it can be seen that pure Go sources are refered through $GOROOT, unlike cgo sources:

…
TEXT %22%22.Open(SB) gofile..$GOROOT/src/plugin/plugin.go
…
TEXT %22%22.lastIndexByte(SB) gofile../tmp/nds-install-go_1_9/share/go/src/plugin/plugin_dlopen.go
…

I would like the second line to be:

TEXT %22%22.lastIndexByte(SB) gofile..$GOROOT/src/plugin/plugin_dlopen.go

Here is a possible fix that replaces absolute path names in *.cgo1.go:

diff --git a/src/go/printer/printer.go b/src/go/printer/printer.go
index dbb4bbd90c..16a0b84fa5 100644
--- a/src/go/printer/printer.go
+++ b/src/go/printer/printer.go
@@ -11,6 +11,7 @@ import (
 	"go/token"
 	"io"
 	"os"
+	"runtime"
 	"strconv"
 	"strings"
 	"text/tabwriter"
@@ -207,7 +208,8 @@ func (p *printer) lineFor(pos token.Pos) int {
 func (p *printer) writeLineDirective(pos token.Position) {
 	if pos.IsValid() && (p.out.Line != pos.Line || p.out.Filename != pos.Filename) {
 		p.output = append(p.output, tabwriter.Escape) // protect '\n' in //line from tabwriter interpretation
-		p.output = append(p.output, fmt.Sprintf("//line %s:%d\n", pos.Filename, pos.Line)...)
+		filename := strings.Replace(pos.Filename, runtime.GOROOT(), "$GOROOT", -1)
+		p.output = append(p.output, fmt.Sprintf("//line %s:%d\n", filename, pos.Line)...)
 		p.output = append(p.output, tabwriter.Escape)
 		// p.out must match the //line directive
 		p.out.Filename = pos.Filename

What version of Go are you using (go version)?

go1.9

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

NixOS Linux amd64

What did you do?

I tried to build journalbeat that uses the plugin package with Go 1.9. It failed Nix check that executables should not refer to Go root via an absolute pathname.

Metadata

Metadata

Assignees

No one assigned

    Labels

    CherryPickApprovedUsed during the release process for point releasesFrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.release-blocker

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions