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: -buildmode=pie -linkshared panic at runtime #47873

Closed
kiap opened this issue Aug 21, 2021 · 6 comments
Closed

cmd/link: -buildmode=pie -linkshared panic at runtime #47873

kiap opened this issue Aug 21, 2021 · 6 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@kiap
Copy link

kiap commented Aug 21, 2021

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

$ go version

go version go1.17 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

Linux x86_64 both CentOS 7.9 and RHEL 8.4

go env Output
$ go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/usr/src/.cache"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/usr/src/dev/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/usr/src/dev"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/src/dev"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/src/dev/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/usr/src/dev/src/hello/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build919601769=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Building a simple hello program with shared library

go install -v -ldflags '-s -w' -buildmode=shared -linkshared -pkgdir /usr/src/lib std
go install -v -ldflags '-s -w' -buildmode=pie -linkshared -pkgdir /usr/src/lib hello

package main

import  (
              "fmt"
)
func main() {
           fmt.Printf("Hello, World!\n")
}

What did you expect to see?

Hello, World!

What did you see instead?

fatal error: unreachable method called. linker bug?

goroutine 1 [running]:
runtime.throw({0x7f77de5db273, 0xc00034c708})
/usr/src/dev/src/runtime/panic.go:1198 +0x71 fp=0xc00035aeb8 sp=0xc00035ae88 pc=0x7f77ddead251
runtime.unreachableMethod()
/usr/src/dev/src/runtime/iface.go:561 +0x25 fp=0xc00035aed8 sp=0xc00035aeb8 pc=0x7f77dde77ea5
fmt.Fprintf({0x5616a7877b48, 0xc0003ae008}, {0x5616a76771e0, 0xe}, {0x0, 0x0, 0x0})
/usr/src/dev/src/fmt/print.go:205 +0x9b fp=0xc00035af38 sp=0xc00035aed8 pc=0x7f77ddfc0ddb
fmt.Printf(...)
/usr/src/dev/src/fmt/print.go:213
()
src/hello/hello.go:10 +0x3e fp=0xc00035af80 sp=0xc00035af38 pc=0x5616a767711e
runtime.main()
/usr/src/dev/src/runtime/proc.go:255 +0x282 fp=0xc00035afe0 sp=0xc00035af80 pc=0x7f77ddeb0902
runtime.goexit()
/usr/src/dev/src/runtime/asm_amd64.s:1581 +0x1 fp=0xc00035afe8 sp=0xc00035afe0 pc=0x7f77ddee93a1

@kiap kiap changed the title cmd/link: -buildmode=pie -linkshared Error at Runtime cmd/link: -buildmode=pie -linkshared patic at runtime Aug 21, 2021
@kiap kiap changed the title cmd/link: -buildmode=pie -linkshared patic at runtime cmd/link: -buildmode=pie -linkshared panic at runtime Aug 22, 2021
@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 24, 2021
@toothrot toothrot added this to the Backlog milestone Aug 24, 2021
@toothrot
Copy link
Contributor

Possibly related: #25970

@ianlancetaylor @cherrymui

@zhouguangyuan0718
Copy link
Contributor

zhouguangyuan0718 commented Sep 13, 2021

Function fmt.Printf is inlined. The actually called function is fmt.Fprintf, and the first arg is an ifcae, the data of it is os.Stdout. the tab of it is go.itab.*os.File,io.Writer.
I add flag "-dumpdep" to linker:

_ -> main.main
_ -> main..inittask
_ -> runtime.unreachableMethod
main.main -> os.Stdout
main.main -> go.itab.*os.File,io.Writer
main.main -> go.string."Hello, World!\n"
main.main -> fmt.Fprintf
main.main -> runtime.morestack_noctxt
main.main -> gclocals·33cdeccccebe80329f1fdbee7f5874cb
 -> go.info.fmt.Printf$abstract
go.itab.*os.File,io.Writer -> type.io.Writer
go.itab.*os.File,io.Writer -> type.*os.File 
main..inittask -> fmt..inittask
go.info.fmt.Printf$abstract -> go.info.string
go.info.fmt.Printf$abstract -> go.info.[]interface {}
go.info.fmt.Printf$abstract -> go.info.int
go.info.fmt.Printf$abstract -> go.info.error

It shows that type.*os.File is marked as "UsedInIface" in deadcode. But the symbol "type.*os.File" is in libstd.so. the method 'Write' of type.*os.File don't be marked as reachable. So the field fun of 'go.itab.*os.File,io.Writer' will be a weak reference. In function relocsym, the fun of 'go.itab.*os.File,io.Writer' will be reloced to "runtime.unreachableMethod"
It seems like this appears after https://go-review.googlesource.com/c/go/+/268479

@gopherbot
Copy link

Change https://golang.org/cl/350189 mentions this issue: cmd/link: disable weak reference in itab if build with "-linkshared"

@zhouguangyuan0718
Copy link
Contributor

Can you review this CL when you're free?
This cl is blocking our project. If this cl can be approved, we will cherry-pick this to our own golang-1.17.
@ianlancetaylor @cherrymui

@zhouguangyuan0718
Copy link
Contributor

@gopherbot please consider this for backport to 1.17, maybe we need fix it for 1.17.

@gopherbot
Copy link

Backport issue(s) opened: #49086 (for 1.17).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Oct 28, 2021
@dmitshur dmitshur modified the milestones: Backlog, Go1.18 Oct 28, 2021
@golang golang locked and limited conversation to collaborators Oct 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants