From 2fa45a4fcd57070967f081f3a5c33014c3f29fea Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Fri, 3 Jun 2022 07:50:58 -0400 Subject: [PATCH] cmd/link/internal/loadpe: handle _main reference properly When building CGO internal linking on windows 386, make sure to avoid rewriting references to "_main" to "main" when reading symbols during host object loading; the main routine defined by the Go runtime is still named "_main" (not "main"). If we don't do this, we wind up with an SXREF symbol named "main", which can then cause the loader to pull an actual "main" symbol out of a host archive, which is undesirable. Updates #35006. Change-Id: I3768e3617b560552f4522e9e72af879c6adf7705 Reviewed-on: https://go-review.googlesource.com/c/go/+/410124 TryBot-Result: Gopher Robot Reviewed-by: Alex Brainman Auto-Submit: Than McIntosh Run-TryBot: Than McIntosh Reviewed-by: Cherry Mui --- src/cmd/link/internal/loadpe/ldpe.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cmd/link/internal/loadpe/ldpe.go b/src/cmd/link/internal/loadpe/ldpe.go index bfe2e837c9..bc66252cfa 100644 --- a/src/cmd/link/internal/loadpe/ldpe.go +++ b/src/cmd/link/internal/loadpe/ldpe.go @@ -551,7 +551,13 @@ func (state *peLoaderState) readpesym(pesym *pe.COFFSymbol) (*loader.SymbolBuild name = strings.TrimPrefix(name, "__imp_") // __imp_Name => Name } } - if state.arch.Family == sys.I386 && name[0] == '_' { + // A note on the "_main" exclusion below: the main routine + // defined by the Go runtime is named "_main", not "main", so + // when reading references to _main from a host object we want + // to avoid rewriting "_main" to "main" in this specific + // instance. See #issuecomment-1143698749 on #35006 for more + // details on this problem. + if state.arch.Family == sys.I386 && name[0] == '_' && name != "_main" { name = name[1:] // _Name => Name } } -- 2.48.1