|
| 1 | +// Copyright 2022 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package ld |
| 6 | + |
| 7 | +import ( |
| 8 | + "cmd/internal/objabi" |
| 9 | + "cmd/link/internal/loader" |
| 10 | + "cmd/link/internal/sym" |
| 11 | + "fmt" |
| 12 | + "sort" |
| 13 | +) |
| 14 | + |
| 15 | +// Inittasks finds inittask records, figures out a good |
| 16 | +// order to execute them in, and emits that order for the |
| 17 | +// runtime to use. |
| 18 | +// |
| 19 | +// An inittask represents the initialization code that needs |
| 20 | +// to be run for a package. For package p, the p..inittask |
| 21 | +// symbol contains a list of init functions to run, both |
| 22 | +// explicit user init functions and implicit compiler-generated |
| 23 | +// init functions for initializing global variables like maps. |
| 24 | +// |
| 25 | +// In addition, inittask records have dependencies between each |
| 26 | +// other, mirroring the import dependencies. So if package p |
| 27 | +// imports package q, then there will be a dependency p -> q. |
| 28 | +// We can't initialize package p until after package q has |
| 29 | +// already been initialized. |
| 30 | +// |
| 31 | +// Package dependencies are encoded with relocations. If package |
| 32 | +// p imports package q, then package p's inittask record will |
| 33 | +// have a R_INITORDER relocation pointing to package q's inittask |
| 34 | +// record. See cmd/compile/internal/pkginit/init.go. |
| 35 | +// |
| 36 | +// This function computes an ordering of all of the inittask |
| 37 | +// records so that the order respects all the dependencies, |
| 38 | +// and given that restriction, orders the inittasks in |
| 39 | +// lexicographic order. |
| 40 | +func (ctxt *Link) inittasks() { |
| 41 | + switch ctxt.BuildMode { |
| 42 | + case BuildModeExe, BuildModePIE, BuildModeCArchive, BuildModeCShared: |
| 43 | + // Normally the inittask list will be run on program startup. |
| 44 | + ctxt.mainInittasks = ctxt.inittaskSym("main..inittask", "go:main.inittasks") |
| 45 | + case BuildModePlugin: |
| 46 | + // For plugins, the list will be run on plugin load. |
| 47 | + ctxt.mainInittasks = ctxt.inittaskSym(fmt.Sprintf("%s..inittask", objabi.PathToPrefix(*flagPluginPath)), "go:plugin.inittasks") |
| 48 | + // Make symbol local so multiple plugins don't clobber each other's inittask list. |
| 49 | + ctxt.loader.SetAttrLocal(ctxt.mainInittasks, true) |
| 50 | + case BuildModeShared: |
| 51 | + // Nothing to do. The inittask list will be built by |
| 52 | + // the final build (with the -linkshared option). |
| 53 | + default: |
| 54 | + Exitf("unhandled build mode %d", ctxt.BuildMode) |
| 55 | + } |
| 56 | + |
| 57 | + // If the runtime is one of the packages we are building, |
| 58 | + // initialize the runtime_inittasks variable. |
| 59 | + ldr := ctxt.loader |
| 60 | + if ldr.Lookup("runtime.runtime_inittasks", 0) != 0 { |
| 61 | + t := ctxt.inittaskSym("runtime..inittask", "go:runtime.inittasks") |
| 62 | + |
| 63 | + // This slice header is already defined in runtime/proc.go, so we update it here with new contents. |
| 64 | + sh := ldr.Lookup("runtime.runtime_inittasks", 0) |
| 65 | + sb := ldr.MakeSymbolUpdater(sh) |
| 66 | + sb.SetSize(0) |
| 67 | + sb.SetType(sym.SNOPTRDATA) // Could be SRODATA, but see issue 58857. |
| 68 | + sb.AddAddr(ctxt.Arch, t) |
| 69 | + sb.AddUint(ctxt.Arch, uint64(ldr.SymSize(t)/int64(ctxt.Arch.PtrSize))) |
| 70 | + sb.AddUint(ctxt.Arch, uint64(ldr.SymSize(t)/int64(ctxt.Arch.PtrSize))) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// inittaskSym builds a symbol containing pointers to all the inittasks |
| 75 | +// that need to be run, given the root inittask symbol. |
| 76 | +func (ctxt *Link) inittaskSym(rootName, symName string) loader.Sym { |
| 77 | + ldr := ctxt.loader |
| 78 | + root := ldr.Lookup(rootName, 0) |
| 79 | + if root == 0 { |
| 80 | + // Nothing to do |
| 81 | + return 0 |
| 82 | + } |
| 83 | + |
| 84 | + // Edges record dependencies between packages. |
| 85 | + // {from,to} is in edges if from's package imports to's package. |
| 86 | + // This list is used to implement reverse edge lookups. |
| 87 | + type edge struct { |
| 88 | + from, to loader.Sym |
| 89 | + } |
| 90 | + var edges []edge |
| 91 | + |
| 92 | + // List of packages that are ready to schedule. We use a lexicographic |
| 93 | + // ordered heap to pick the lexically earliest uninitialized but |
| 94 | + // inititalizeable package at each step. |
| 95 | + var h lexHeap |
| 96 | + |
| 97 | + // m maps from an inittask symbol for package p to the number of |
| 98 | + // p's direct imports that have not yet been scheduled. |
| 99 | + m := map[loader.Sym]int{} |
| 100 | + |
| 101 | + // Find all reachable inittask records from the root. |
| 102 | + // Keep track of the dependency edges between them in edges. |
| 103 | + // Keep track of how many imports each package has in m. |
| 104 | + // q is the list of found but not yet explored packages. |
| 105 | + var q []loader.Sym |
| 106 | + m[root] = 0 |
| 107 | + q = append(q, root) |
| 108 | + for len(q) > 0 { |
| 109 | + x := q[len(q)-1] |
| 110 | + q = q[:len(q)-1] |
| 111 | + relocs := ldr.Relocs(x) |
| 112 | + n := relocs.Count() |
| 113 | + ndeps := 0 |
| 114 | + for i := 0; i < n; i++ { |
| 115 | + r := relocs.At(i) |
| 116 | + if r.Type() != objabi.R_INITORDER { |
| 117 | + continue |
| 118 | + } |
| 119 | + ndeps++ |
| 120 | + s := r.Sym() |
| 121 | + edges = append(edges, edge{from: x, to: s}) |
| 122 | + if _, ok := m[s]; ok { |
| 123 | + continue // already found |
| 124 | + } |
| 125 | + q = append(q, s) |
| 126 | + m[s] = 0 // mark as found |
| 127 | + } |
| 128 | + m[x] = ndeps |
| 129 | + if ndeps == 0 { |
| 130 | + h.push(ldr, x) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Sort edges so we can look them up by edge destination. |
| 135 | + sort.Slice(edges, func(i, j int) bool { |
| 136 | + return edges[i].to < edges[j].to |
| 137 | + }) |
| 138 | + |
| 139 | + // Figure out the schedule. |
| 140 | + sched := ldr.MakeSymbolBuilder(symName) |
| 141 | + sched.SetType(sym.SNOPTRDATA) // Could be SRODATA, but see isue 58857. |
| 142 | + for !h.empty() { |
| 143 | + // Pick the lexicographically first initializable package. |
| 144 | + s := h.pop(ldr) |
| 145 | + |
| 146 | + // Add s to the schedule. |
| 147 | + if ldr.SymSize(s) > 8 { |
| 148 | + // Note: don't add s if it has no functions to run. We need |
| 149 | + // s during linking to compute an ordering, but the runtime |
| 150 | + // doesn't need to know about it. About 1/2 of stdlib packages |
| 151 | + // fit in this bucket. |
| 152 | + sched.AddAddr(ctxt.Arch, s) |
| 153 | + } |
| 154 | + |
| 155 | + // Find all incoming edges into s. |
| 156 | + a := sort.Search(len(edges), func(i int) bool { return edges[i].to >= s }) |
| 157 | + b := sort.Search(len(edges), func(i int) bool { return edges[i].to > s }) |
| 158 | + |
| 159 | + // Decrement the import count for all packages that import s. |
| 160 | + // If the count reaches 0, that package is now ready to schedule. |
| 161 | + for _, e := range edges[a:b] { |
| 162 | + m[e.from]-- |
| 163 | + if m[e.from] == 0 { |
| 164 | + h.push(ldr, e.from) |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + for s, n := range m { |
| 170 | + if n != 0 { |
| 171 | + Exitf("inittask for %s is not schedulable %d", ldr.SymName(s), n) |
| 172 | + } |
| 173 | + } |
| 174 | + return sched.Sym() |
| 175 | +} |
0 commit comments