cmd/link: move cgo export map from loadcgo to setCgoAttr

Currently, both loadcgo and setCgoAttr do some processing of
cgo_export_static and cgo_export_dynamic cgo directives, which means
they both have to parse them. There's no reason to do this in loadcgo,
so move all directive processing to setCgoAttr.

For #40724.

Change-Id: Icb3cdf7ef3517e866dd220e40a5f5dec7fd47e2b
Reviewed-on: https://go-review.googlesource.com/c/go/+/309339
Trust: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Austin Clements 2021-04-11 17:29:17 -04:00
parent 6208b10d1e
commit 007e247af1
2 changed files with 6 additions and 24 deletions

View file

@ -101,29 +101,6 @@ func loadcgo(ctxt *Link, file string, pkg string, p string) {
return
}
// Find cgo_export symbols. They are roots in the deadcode pass.
for _, f := range directives {
switch f[0] {
case "cgo_export_static", "cgo_export_dynamic":
if len(f) < 2 || len(f) > 3 {
continue
}
local := f[1]
switch ctxt.BuildMode {
case BuildModeCShared, BuildModeCArchive, BuildModePlugin:
if local == "main" {
continue
}
}
local = expandpkg(local, pkg)
if f[0] == "cgo_export_static" {
ctxt.cgo_export_static[local] = true
} else {
ctxt.cgo_export_dynamic[local] = true
}
}
}
// Record the directives. We'll process them later after Symbols are created.
ctxt.cgodata = append(ctxt.cgodata, cgodata{file, pkg, directives})
}
@ -254,11 +231,16 @@ func setCgoAttr(ctxt *Link, file string, pkg string, directives [][]string, host
return
}
// Mark exported symbols and also add them to
// the lists used for roots in the deadcode pass.
if f[0] == "cgo_export_static" {
l.SetAttrCgoExportStatic(s, true)
ctxt.cgo_export_static[local] = true
} else {
l.SetAttrCgoExportDynamic(s, true)
ctxt.cgo_export_dynamic[local] = true
}
continue
case "cgo_dynamic_linker":

View file

@ -57,7 +57,7 @@ func afterErrorAction() {
// Logging an error means that on exit cmd/link will delete any
// output file and return a non-zero error code.
//
// TODO: remove. Use ctxt.Errof instead.
// TODO: remove. Use ctxt.Errorf instead.
// All remaining calls use nil as first arg.
func Errorf(dummy *int, format string, args ...interface{}) {
format += "\n"