Skip to content

Commit 273b657

Browse files
committed
cmd/link: support -X values for main.* in plugins
Fixes golang#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>
1 parent 5fe9bbc commit 273b657

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 main
6+
7+
import (
8+
"fmt"
9+
"os"
10+
"plugin"
11+
)
12+
13+
func main() {
14+
p, err := plugin.Open("plugin.so")
15+
if err != nil {
16+
panic(err)
17+
}
18+
19+
val, err := p.Lookup("Val")
20+
if err != nil {
21+
panic(err)
22+
}
23+
got := *val.(*string)
24+
const want = "linkstr"
25+
if got != want {
26+
fmt.Fprintf(os.Stderr, "issue19418 value is %q, want %q\n", got, want)
27+
os.Exit(2)
28+
}
29+
}
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 main
6+
7+
var Val = "val-unset"

misc/cgo/testplugin/test.bash

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" -o issue19534 src/issue19534/main.
6666
GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" -buildmode=plugin -o plugin.so src/issue18584/plugin.go
6767
GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" -o issue18584 src/issue18584/main.go
6868
./issue18584
69+
70+
# Test for issue 19418
71+
GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" -buildmode=plugin "-ldflags=-X main.Val=linkstr" -o plugin.so src/issue19418/plugin.go
72+
GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" -o issue19418 src/issue19418/main.go
73+
./issue19418

src/cmd/link/internal/ld/data.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,11 @@ func addstrdata1(ctxt *Link, arg string) {
10451045
if eq < 0 || dot < 0 {
10461046
Exitf("-X flag requires argument of the form importpath.name=value")
10471047
}
1048-
addstrdata(ctxt, objabi.PathToPrefix(arg[:dot])+arg[dot:eq], arg[eq+1:])
1048+
pkg := objabi.PathToPrefix(arg[:dot])
1049+
if Buildmode == BuildmodePlugin && pkg == "main" {
1050+
pkg = *flagPluginPath
1051+
}
1052+
addstrdata(ctxt, pkg+arg[dot:eq], arg[eq+1:])
10491053
}
10501054

10511055
func addstrdata(ctxt *Link, name string, value string) {

0 commit comments

Comments
 (0)