Commit 273b657b authored by David Crawshaw's avatar David Crawshaw

cmd/link: support -X values for main.* in plugins

Fixes #19418

Change-Id: I98205f40c1915cd68a5d20438469ba06f1efb160
Reviewed-on: https://go-review.googlesource.com/67432
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 5fe9bbcf
// Copyright 2017 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.
package main
import (
"fmt"
"os"
"plugin"
)
func main() {
p, err := plugin.Open("plugin.so")
if err != nil {
panic(err)
}
val, err := p.Lookup("Val")
if err != nil {
panic(err)
}
got := *val.(*string)
const want = "linkstr"
if got != want {
fmt.Fprintf(os.Stderr, "issue19418 value is %q, want %q\n", got, want)
os.Exit(2)
}
}
// Copyright 2017 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.
package main
var Val = "val-unset"
......@@ -66,3 +66,8 @@ GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" -o issue19534 src/issue19534/main.
GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" -buildmode=plugin -o plugin.so src/issue18584/plugin.go
GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" -o issue18584 src/issue18584/main.go
./issue18584
# Test for issue 19418
GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" -buildmode=plugin "-ldflags=-X main.Val=linkstr" -o plugin.so src/issue19418/plugin.go
GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" -o issue19418 src/issue19418/main.go
./issue19418
......@@ -1045,7 +1045,11 @@ func addstrdata1(ctxt *Link, arg string) {
if eq < 0 || dot < 0 {
Exitf("-X flag requires argument of the form importpath.name=value")
}
addstrdata(ctxt, objabi.PathToPrefix(arg[:dot])+arg[dot:eq], arg[eq+1:])
pkg := objabi.PathToPrefix(arg[:dot])
if Buildmode == BuildmodePlugin && pkg == "main" {
pkg = *flagPluginPath
}
addstrdata(ctxt, pkg+arg[dot:eq], arg[eq+1:])
}
func addstrdata(ctxt *Link, name string, value string) {
......
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