Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin: ambiguous/undocumented ldflags="-pluginpath ... -X ..." #19418

Closed
fsenart opened this issue Mar 6, 2017 · 2 comments
Closed

plugin: ambiguous/undocumented ldflags="-pluginpath ... -X ..." #19418

fsenart opened this issue Mar 6, 2017 · 2 comments

Comments

@fsenart
Copy link

fsenart commented Mar 6, 2017

Please answer these questions before submitting your issue. Thanks!

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

go version go1.8 linux/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/fsenart/projects"
GORACE=""
GOROOT="/home/fsenart/tools/go1.8"
GOTOOLDIR="/home/fsenart/tools/go1.8/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build076969156=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

$GOPATH/src/issue_ldflags/host.go

// +build host

package main

import "plugin"

var hostDummy string = "EMPTY"

func main() {
	p, err := plugin.Open("guest.so")
	if err != nil {
		panic(err)
	}
	f, err := p.Lookup("F")
	if err != nil {
		panic(err)
	}

	println(hostDummy)

	f.(func())()
}

$GOPATH/src/issue_ldflags/guest.go

// +build guest

package main

var guestDummy string = "EMPTY"

func F() {
	println(guestDummy)
}

$GOPATH/src/issue_ldflags/Makefile

scenario_1:
	# INSIDE GOPATH & compile WITHOUT specifying source file
	@echo Host
	@echo ===
	@go build -tags host -ldflags='-X main.hostDummy=dummyHost' -o host
	@nm host | grep hostDummy
	@echo Guest
	@echo =====
	@go build -tags guest -buildmode=plugin -ldflags='-X $(shell basename $(CURDIR)).guestDummy=dummyGuest' -o guest.so
	@nm guest.so | grep guestDummy
	@echo Run
	@echo ===
	@./host
	@echo ===
	# WITH custom plugin path
	# https://github.com/golang/go/blob/master/src/cmd/go/internal/work/build.go#L2412
	@go build -tags guest -buildmode=plugin -ldflags='-pluginpath=custompluginpath -X custompluginpath.guestDummy=dummyGuest' -o guest.so
	@nm guest.so | grep guestDummy
	@echo Run
	@echo ===
	@./host

scenario_2:
	# INSIDE GOPATH & compile WITH specifying source file
	@echo Host
	@echo ===
	@go build -tags host -ldflags='-X main.hostDummy=dummyHost' -o host host.go
	@nm host | grep hostDummy
	@echo Guest
	@echo =====
	# If not seeing 2 lines, change the Makefile with the seen id.
	# https://github.com/golang/go/blob/master/src/cmd/go/internal/work/build.go#L2410
	@go build -tags guest -buildmode=plugin -ldflags='-X plugin/unnamed-1edb35ed362d12b8c6c92ff2cc31ad97e4131d95.guestDummy=dummyGuest' -o guest.so guest.go
	@nm guest.so | grep guestDummy
	@echo Run
	@echo ===
	@./host
	@echo ===
	# WITH custom plugin path
	# https://github.com/golang/go/blob/master/src/cmd/go/internal/work/build.go#L2412
	@go build -tags guest -buildmode=plugin -ldflags='-pluginpath=custompluginpath -X custompluginpath.guestDummy=dummyGuest' -o guest.so guest.go
	@nm guest.so | grep guestDummy
	@echo Run
	@echo ===
	@./host

scenario_3:
	# OUTSIDE GOPATH & compile WITHOUT specifying source file
	@echo Host
	@echo ===
	@GOPATH=/nowhere go build -tags host -ldflags='-X main.hostDummy=dummyHost' -o host
	@nm host | grep hostDummy
	@echo Guest
	@echo =====
	@GOPATH=/nowhere go build -tags guest -buildmode=plugin -ldflags='-X _$(CURDIR).guestDummy=dummyGuest' -o guest.so
	@nm guest.so | grep guestDummy
	@echo Run
	@echo ===
	@./host
	@echo ===
	# WITH custom plugin path
	# https://github.com/golang/go/blob/master/src/cmd/go/internal/work/build.go#L2412
	@GOPATH=/nowhere go build -tags guest -buildmode=plugin -ldflags='-pluginpath=custompluginpath -X custompluginpath.guestDummy=dummyGuest' -o guest.so
	@nm guest.so | grep guestDummy
	@echo Run
	@echo ===
	@./host

scenario_4:
	# OUTSIDE GOPATH & compile WITH specifying source file
	@echo Host
	@echo ===
	@GOPATH=/nowhere go build -tags host -ldflags='-X main.hostDummy=dummyHost' -o host host.go
	@nm host | grep hostDummy
	@echo Guest
	@echo =====
	# https://github.com/golang/go/blob/master/src/cmd/go/internal/work/build.go#L2410
	# If not seeing 2 lines, change the Makefile with the seen id.
	@GOPATH=/nowhere go build -tags guest -buildmode=plugin -ldflags='-X plugin/unnamed-1edb35ed362d12b8c6c92ff2cc31ad97e4131d95.guestDummy=dummyGuest' -o guest.so guest.go
	@nm guest.so | grep guestDummy
	@echo Run
	@echo ===
	@./host
	@echo ===
	# WITH custom plugin path
	# https://github.com/golang/go/blob/master/src/cmd/go/internal/work/build.go#L2412
	@GOPATH=/nowhere go build -tags guest -buildmode=plugin -ldflags='-pluginpath=custompluginpath -X custompluginpath.guestDummy=dummyGuest' -o guest.so guest.go
	@nm guest.so | grep guestDummy
	@echo Run
	@echo ===
	@./host

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

What did you expect to see?

I expect to be able to use -ldflags="-X importpath.guestDummy" to set a build time variable when compiling with -buildmode=plugin ; as I can do when compiling regular binaries with -ldflags="-X main.hostDummy"

What did you see instead?

In the context of plugins importpath is variable and depends on the building context. I found that I can have a fixed behavior when using -pluginpath ldflags. But this flag is not well documented:

  -pluginpath string
    	full path name for plugin

Can I use it confidently is this context? is this the good pattern to set a build time variable in plugins?

@crawshaw
Copy link
Member

Ideally no-one but cmd/go sets -pluginpath. But -X is widely used. I suppose for plugins we could rewrite in cmd/link any value passed in as '-X main.val' to '-X whateverthepluginnameis.val'.

@crawshaw crawshaw self-assigned this Sep 29, 2017
@gopherbot
Copy link

Change https://golang.org/cl/67432 mentions this issue: cmd/link: support -X values for main.* in plugins

@golang golang locked and limited conversation to collaborators Oct 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants