]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: support -X values for main.* in plugins
authorDavid Crawshaw <crawshaw@golang.org>
Mon, 2 Oct 2017 00:28:53 +0000 (20:28 -0400)
committerDavid Crawshaw <crawshaw@golang.org>
Wed, 4 Oct 2017 00:46:06 +0000 (00:46 +0000)
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: Ian Lance Taylor <iant@golang.org>
misc/cgo/testplugin/src/issue19418/main.go [new file with mode: 0644]
misc/cgo/testplugin/src/issue19418/plugin.go [new file with mode: 0644]
misc/cgo/testplugin/test.bash
src/cmd/link/internal/ld/data.go

diff --git a/misc/cgo/testplugin/src/issue19418/main.go b/misc/cgo/testplugin/src/issue19418/main.go
new file mode 100644 (file)
index 0000000..2ec9f9a
--- /dev/null
@@ -0,0 +1,29 @@
+// 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)
+       }
+}
diff --git a/misc/cgo/testplugin/src/issue19418/plugin.go b/misc/cgo/testplugin/src/issue19418/plugin.go
new file mode 100644 (file)
index 0000000..fe93b16
--- /dev/null
@@ -0,0 +1,7 @@
+// 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"
index a52aa4603704ce4b66a0a170f57c4d30ca8c92f0..ae3368b45f60aad7058b61b48aaff2be4068d303 100755 (executable)
@@ -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
index c4ffa20a57816332e5c3d21daf95d4038af1b095..36b97a3deae00dfad3d597620c7b2a50a701d665 100644 (file)
@@ -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) {