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: linking with -linkshared fails if a package depends on a method in another pkg indirectly #22998

Closed
yunabe opened this issue Dec 5, 2017 · 7 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@yunabe
Copy link

yunabe commented Dec 5, 2017

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

go version go1.9.2 linux/amd64

Does this issue reproduce with the latest release?

Yes. go1.9.2 is the latest release as of 2017-12-04.

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/yunabe/local/gocode"
GORACE=""
GOROOT="/usr/lib/go-1.9"
GOTOOLDIR="/usr/lib/go-1.9/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build706131046=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

  1. Write a package (lib0) that defines a type and a method and uses a standard library (e.g. "fmt")
  2. Write another package (lib1) that defines an instance of the type above
  3. Write another package (lib2) that calls a method from the instance defined in (2).
  4. Build these three packages respectively with -buildmode=shared -linkshared with the same -pkgdir.

I created a git repository to reproduce this bug (yunabe/gobug22998). You can reproduce this bug by running

go get -d github.com/yunabe/gobug22998
$GOPATH/src/github.com/yunabe/gobug22998/run.sh

What did you expect to see?

libgithub.com-yunabe-gobug22998-lib2.so is created corretly and it depends on lib0.so and lib1.so

What did you see instead?

Got errors from the linker

# /tmp/go-build121402299/libgithub.com-yunabe-gobug22998-lib2.so
github.com/yunabe/gobug22998/lib2.HelloAlice: missing section for relocation target github.com/yunabe/gobug22998/lib0.(*Person).GetName
github.com/yunabe/gobug22998/lib2.HelloAlice: reloc 8 to non-elf symbol github.com/yunabe/gobug22998/lib0.(*Person).GetName (outer=github.com/yunabe/gobug22998/lib0.(*Person).GetName) 0
github.com/yunabe/gobug22998/lib2.HelloAlice: undefined: "github.com/yunabe/gobug22998/lib0.(*Person).GetName"

Notes

There is a workaround for this bug. If I add an dependency to lib0 explicitly by importing _ "github.com/yunabe/linkbug/lib0" in lib2/src.go, everything works fine. Thus, I guess link with -linkshared fails to resolve indirect dependencies to methods.

Also, if you remove import "fmt" from lib0/src.go, you can build lib2.so without hitting this bug.

@ianlancetaylor
Copy link
Contributor

CC @mwhudson

@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Dec 5, 2017
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 5, 2017
@bradfitz bradfitz changed the title Linking with -linkshared fails if a package depends on a method in another pkg indirectly cmd/link: linking with -linkshared fails if a package depends on a method in another pkg indirectly May 29, 2018
@bradfitz
Copy link
Contributor

Ping @mwhudson

@mwhudson
Copy link
Contributor

Oops, sorry for missing this. I can reproduce the problem with the supplied script.

I guess the linker needs to be fixed to load any go shared libraries that are dependencies of ones it loads. This suuuper simple patch seems to fix it:

diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go
index edf3922980..37e3086ae3 100644
--- a/src/cmd/link/internal/ld/lib.go
+++ b/src/cmd/link/internal/ld/lib.go
@@ -1646,6 +1646,7 @@ func ldshlibsyms(ctxt *Link, shlib string) {
                                dep = abs
                        }
                }
+               ldshlibsyms(ctxt, dep)
                deps = append(deps, dep)
        }
 

but that almost too easy...

@yunabe
Copy link
Author

yunabe commented May 30, 2018

I found a similar but different bug in cmd/link with -linkshared (#25065). The bug might be related to this bug and we may be able to fix it with this bug.

Thanks,
yunabe

@ianlancetaylor
Copy link
Contributor

@mwhudson Do you want to try sending in your patch?

@ianlancetaylor ianlancetaylor modified the milestones: Go1.11, Go1.12 Jun 29, 2018
@ianlancetaylor
Copy link
Contributor

I can't recreate this problem, though I don't know what would have fixed it. Is still an issue on tip?

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Dec 7, 2018
@ianlancetaylor ianlancetaylor modified the milestones: Go1.12, Go1.13 Dec 12, 2018
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Jan 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants