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

cmd/link: undefined builtin error with shared linkage + crypto/sha256 reference #43107

Closed
thanm opened this issue Dec 9, 2020 · 2 comments
Closed
Assignees
Milestone

Comments

@thanm
Copy link
Member

@thanm thanm commented Dec 9, 2020

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

Using tip/mainline.

$ go version
go version devel +89f465c2b5 Wed Dec 9 21:05:24 2020 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
linux/amd64

What did you do?

Given this Go program:

package main

import "crypto/sha256"

var Glob interface{}

func main() {
	Glob = sha256.New()
}

If you build it with shared linkage, as in the script below:

#!/bin/sh
GOROOT=`go env GOROOT`
TGT=$GOROOT/pkg/linux_amd64_dynlink/libruntime,sync-atomic.so
rm -f $TGT err.txt err2.txt
MODE=-linkmode=external
LDFLAGS="-ldflags=$MODE"
# Build shared.
go install -buildmode=shared runtime sync/atomic 1> err.txt 2>&1
if [ $? != 0 ]; then
  echo "*** sync/atomic build failed"
  exit 1
fi
#
rm -f refcrypto
go build $LDFLAGS -o refcrypto -linkshared refcrypto.go 1> err2.txt 2>&1
if [ $? != 0 ]; then
  echo "*** refcrypto -linkshared build failed"
  exit 1
fi

What did you expect to see?

Clean build

What did you see instead?

Build fails with error

2020/12/09 17:28:44 reference to undefined builtin "runtime.morestack_noctxt" from package "crypto/sha256"

Related CL https://go-review.googlesource.com/c/go/+/267881.

@thanm thanm added this to the Go1.16 milestone Dec 9, 2020
@thanm thanm self-assigned this Dec 9, 2020
@thanm
Copy link
Member Author

@thanm thanm commented Dec 10, 2020

Interesting bug. The problem appears to be related to this code:

		if !ctxt.Flag_linkshared {
			// Assign special index for builtin symbols.
			// Don't do it when linking against shared libraries, as the runtime
			// may be in a different library.
			if i := goobj.BuiltinIdx(rs.Name, int(rs.ABI())); i != -1 {
				rs.PkgIdx = goobj.PkgIdxBuiltin
				rs.SymIdx = int32(i)
				rs.Set(AttrIndexed, true)
				return
			}
		}

The wrinkle here is that while the go command passes "-linkshared" to the compiler when building, it doesn't pass -linkshared to the assembler, meaning that we still do the builtin assignment for the assembly code in crypto/sha256. Hence the stray builtin reference.

@gopherbot
Copy link

@gopherbot gopherbot commented Dec 10, 2020

Change https://golang.org/cl/276932 mentions this issue: cmd/go, cmd/asm: pass -linkshared to assembler for shared linkage builds

@gopherbot gopherbot closed this in 56b783a Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants