From 795e8c2317a6a4d728a59caa7550312cdceab367 Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Wed, 19 Jun 2019 15:14:54 -0400 Subject: [PATCH] cmd/go: address DWARF linker issues with -buildmode=plugin on Darwin Assorted fixups in the linker needed to enable turning back on DWARF generation when building plugins for Darwin. Includes: - don't suppress import of runtime/cgo in the linker for Darwin if we are linking in plugin mode - in calcCompUnitRanges handle the case where we encounter linker-generated functions that have no associated Unit (and also have no DWARF) - generalize a guard in relocsym() include so as to avoid triggering a spurious error on go.info symbols in plugin mode Updates #21647. Updates #27502. Change-Id: I317fea97bef2f3461e31498e63f9fd6d8b8f4b23 Reviewed-on: https://go-review.googlesource.com/c/go/+/182959 Run-TryBot: Than McIntosh TryBot-Result: Gobot Gobot Reviewed-by: Cherry Zhang Reviewed-by: Emmanuel Odeke --- src/cmd/link/internal/ld/data.go | 4 ++-- src/cmd/link/internal/ld/dwarf.go | 5 +++++ src/cmd/link/internal/ld/lib.go | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/cmd/link/internal/ld/data.go b/src/cmd/link/internal/ld/data.go index 3c24717433..32d1111ea3 100644 --- a/src/cmd/link/internal/ld/data.go +++ b/src/cmd/link/internal/ld/data.go @@ -157,8 +157,8 @@ func relocsym(ctxt *Link, s *sym.Symbol) { if r.Sym != nil && ((r.Sym.Type == sym.Sxxx && !r.Sym.Attr.VisibilityHidden()) || r.Sym.Type == sym.SXREF) { // When putting the runtime but not main into a shared library // these symbols are undefined and that's OK. - if ctxt.BuildMode == BuildModeShared { - if r.Sym.Name == "main.main" || r.Sym.Name == "main..inittask" { + if ctxt.BuildMode == BuildModeShared || ctxt.BuildMode == BuildModePlugin { + if r.Sym.Name == "main.main" || (ctxt.BuildMode != BuildModePlugin && r.Sym.Name == "main..inittask") { r.Sym.Type = sym.SDYNIMPORT } else if strings.HasPrefix(r.Sym.Name, "go.info.") { // Skip go.info symbols. They are only needed to communicate diff --git a/src/cmd/link/internal/ld/dwarf.go b/src/cmd/link/internal/ld/dwarf.go index f5af90b028..f9cb0e98c3 100644 --- a/src/cmd/link/internal/ld/dwarf.go +++ b/src/cmd/link/internal/ld/dwarf.go @@ -944,6 +944,11 @@ func calcCompUnitRanges(ctxt *Link) { if s.FuncInfo == nil { continue } + // Skip linker-created functions (ex: runtime.addmoduledata), since they + // don't have DWARF to begin with. + if s.Unit == nil { + continue + } unit := s.Unit // Update PC ranges. // diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go index 0bd9a8c735..09a5f2a711 100644 --- a/src/cmd/link/internal/ld/lib.go +++ b/src/cmd/link/internal/ld/lib.go @@ -437,7 +437,7 @@ func (ctxt *Link) loadlib() { // We now have enough information to determine the link mode. determineLinkMode(ctxt) - if ctxt.LinkMode == LinkExternal && !iscgo && ctxt.LibraryByPkg["runtime/cgo"] == nil && !(objabi.GOOS == "darwin" && (ctxt.Arch.Family == sys.AMD64 || ctxt.Arch.Family == sys.I386)) { + if ctxt.LinkMode == LinkExternal && !iscgo && ctxt.LibraryByPkg["runtime/cgo"] == nil && !(objabi.GOOS == "darwin" && ctxt.BuildMode != BuildModePlugin && (ctxt.Arch.Family == sys.AMD64 || ctxt.Arch.Family == sys.I386)) { // This indicates a user requested -linkmode=external. // The startup code uses an import of runtime/cgo to decide // whether to initialize the TLS. So give it one. This could -- 2.50.0