diff options
| author | Cherry Mui <cherryyz@google.com> | 2023-08-16 19:49:04 -0400 |
|---|---|---|
| committer | Cherry Mui <cherryyz@google.com> | 2023-09-20 14:46:11 +0000 |
| commit | fd54185a8d4e91eb6b34a73360cef0c51eea797e (patch) | |
| tree | d0a325ec26b100db8215299baf51a62257730e99 /src/cmd/link | |
| parent | 3857a89e7eb872fa22d569e70b7e076bec74ebbb (diff) | |
| download | go-fd54185a8d4e91eb6b34a73360cef0c51eea797e.tar.xz | |
cmd/link, runtime: initialize packages in shared build mode
Currently, for the shared build mode, we don't generate the module
inittasks. Instead, we rely on the main executable to do the
initialization, for both the executable and the shared library.
But, with the model as of CL 478916, the main executable only
has relocations to packages that are directly imported. It won't
see the dependency edges between packages within a shared library.
Therefore indirect dependencies are not included, and thus not
initialized. E.g. main imports a, which imports b, but main
doesn't directly import b. a and b are in a shared object. When
linking main, it sees main depends on a, so it generates main's
inittasks to run a's init before main's, but it doesn't know b,
so b's init doesn't run.
This CL makes it initialize all packages in a shared library when
the library is loaded, as any of them could potentially be
imported, directly or indirectly.
Also, in the runtime, when running the init functions, make sure
to go through the DSOs in dependency order. Otherwise packages
can be initialized in the wrong order.
Fixes #61973.
Change-Id: I2a090336fe9fa0d6c7e43912f3ab233c9c47e247
Reviewed-on: https://go-review.googlesource.com/c/go/+/520375
Reviewed-by: Than McIntosh <thanm@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/cmd/link')
| -rw-r--r-- | src/cmd/link/internal/ld/deadcode.go | 1 | ||||
| -rw-r--r-- | src/cmd/link/internal/ld/inittask.go | 38 |
2 files changed, 27 insertions, 12 deletions
diff --git a/src/cmd/link/internal/ld/deadcode.go b/src/cmd/link/internal/ld/deadcode.go index a051e43401..70b4a7ca30 100644 --- a/src/cmd/link/internal/ld/deadcode.go +++ b/src/cmd/link/internal/ld/deadcode.go @@ -51,6 +51,7 @@ func (d *deadcodePass) init() { s := loader.Sym(i) d.mark(s, 0) } + d.mark(d.ctxt.mainInittasks, 0) return } diff --git a/src/cmd/link/internal/ld/inittask.go b/src/cmd/link/internal/ld/inittask.go index 0699107cd0..c4c5beb55e 100644 --- a/src/cmd/link/internal/ld/inittask.go +++ b/src/cmd/link/internal/ld/inittask.go @@ -41,15 +41,21 @@ func (ctxt *Link) inittasks() { switch ctxt.BuildMode { case BuildModeExe, BuildModePIE, BuildModeCArchive, BuildModeCShared: // Normally the inittask list will be run on program startup. - ctxt.mainInittasks = ctxt.inittaskSym("main..inittask", "go:main.inittasks") + ctxt.mainInittasks = ctxt.inittaskSym([]string{"main..inittask"}, "go:main.inittasks") case BuildModePlugin: // For plugins, the list will be run on plugin load. - ctxt.mainInittasks = ctxt.inittaskSym(fmt.Sprintf("%s..inittask", objabi.PathToPrefix(*flagPluginPath)), "go:plugin.inittasks") + ctxt.mainInittasks = ctxt.inittaskSym([]string{fmt.Sprintf("%s..inittask", objabi.PathToPrefix(*flagPluginPath))}, "go:plugin.inittasks") // Make symbol local so multiple plugins don't clobber each other's inittask list. ctxt.loader.SetAttrLocal(ctxt.mainInittasks, true) case BuildModeShared: - // Nothing to do. The inittask list will be built by - // the final build (with the -linkshared option). + // For a shared library, all packages are roots. + var roots []string + for _, lib := range ctxt.Library { + roots = append(roots, fmt.Sprintf("%s..inittask", objabi.PathToPrefix(lib.Pkg))) + } + ctxt.mainInittasks = ctxt.inittaskSym(roots, "go:shlib.inittasks") + // Make symbol local so multiple plugins don't clobber each other's inittask list. + ctxt.loader.SetAttrLocal(ctxt.mainInittasks, true) default: Exitf("unhandled build mode %d", ctxt.BuildMode) } @@ -58,7 +64,7 @@ func (ctxt *Link) inittasks() { // initialize the runtime_inittasks variable. ldr := ctxt.loader if ldr.Lookup("runtime.runtime_inittasks", 0) != 0 { - t := ctxt.inittaskSym("runtime..inittask", "go:runtime.inittasks") + t := ctxt.inittaskSym([]string{"runtime..inittask"}, "go:runtime.inittasks") // This slice header is already defined in runtime/proc.go, so we update it here with new contents. sh := ldr.Lookup("runtime.runtime_inittasks", 0) @@ -72,11 +78,17 @@ func (ctxt *Link) inittasks() { } // inittaskSym builds a symbol containing pointers to all the inittasks -// that need to be run, given the root inittask symbol. -func (ctxt *Link) inittaskSym(rootName, symName string) loader.Sym { +// that need to be run, given a list of root inittask symbols. +func (ctxt *Link) inittaskSym(rootNames []string, symName string) loader.Sym { ldr := ctxt.loader - root := ldr.Lookup(rootName, 0) - if root == 0 { + var roots []loader.Sym + for _, n := range rootNames { + p := ldr.Lookup(n, 0) + if p != 0 { + roots = append(roots, p) + } + } + if len(roots) == 0 { // Nothing to do return 0 } @@ -98,13 +110,15 @@ func (ctxt *Link) inittaskSym(rootName, symName string) loader.Sym { // p's direct imports that have not yet been scheduled. m := map[loader.Sym]int{} - // Find all reachable inittask records from the root. + // Find all reachable inittask records from the roots. // Keep track of the dependency edges between them in edges. // Keep track of how many imports each package has in m. // q is the list of found but not yet explored packages. var q []loader.Sym - m[root] = 0 - q = append(q, root) + for _, p := range roots { + m[p] = 0 + q = append(q, p) + } for len(q) > 0 { x := q[len(q)-1] q = q[:len(q)-1] |
