| |
| |
| |
|
|
| package pkginit |
|
|
| import ( |
| "cmd/compile/internal/base" |
| "cmd/compile/internal/ir" |
| "cmd/compile/internal/noder" |
| "cmd/compile/internal/objw" |
| "cmd/compile/internal/staticinit" |
| "cmd/compile/internal/typecheck" |
| "cmd/compile/internal/types" |
| "cmd/internal/obj" |
| "cmd/internal/objabi" |
| "cmd/internal/src" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| func MakeTask() { |
| var deps []*obj.LSym |
| var fns []*obj.LSym |
|
|
| |
| for _, pkg := range typecheck.Target.Imports { |
| n, ok := pkg.Lookup(".inittask").Def.(*ir.Name) |
| if !ok { |
| continue |
| } |
| if n.Op() != ir.ONAME || n.Class != ir.PEXTERN { |
| base.Fatalf("bad inittask: %v", n) |
| } |
| deps = append(deps, n.Linksym()) |
| } |
| if base.Flag.ASan { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| for _, n := range typecheck.Target.Externs { |
| if canInstrumentGlobal(n) { |
| name := n.Sym().Name |
| InstrumentGlobalsMap[name] = n |
| InstrumentGlobalsSlice = append(InstrumentGlobalsSlice, n) |
| } |
| } |
| ni := len(InstrumentGlobalsMap) |
| if ni != 0 { |
| |
| pos := base.AutogeneratedPos |
| base.Pos = pos |
|
|
| sym := noder.Renameinit() |
| fnInit := ir.NewFunc(pos, pos, sym, types.NewSignature(nil, nil, nil)) |
| typecheck.DeclFunc(fnInit) |
|
|
| |
| globals := instrumentGlobals(fnInit) |
|
|
| |
| |
| asancall := ir.NewCallExpr(base.Pos, ir.OCALL, typecheck.LookupRuntime("asanregisterglobals"), nil) |
| asancall.Args.Append(typecheck.ConvNop(typecheck.NodAddr( |
| ir.NewIndexExpr(base.Pos, globals, ir.NewInt(base.Pos, 0))), types.Types[types.TUNSAFEPTR])) |
| asancall.Args.Append(typecheck.DefaultLit(ir.NewInt(base.Pos, int64(ni)), types.Types[types.TUINTPTR])) |
|
|
| fnInit.Body.Append(asancall) |
| typecheck.FinishFuncBody() |
| ir.CurFunc = fnInit |
| typecheck.Stmts(fnInit.Body) |
| ir.CurFunc = nil |
|
|
| typecheck.Target.Inits = append(typecheck.Target.Inits, fnInit) |
| } |
| } |
|
|
| |
| for _, fn := range typecheck.Target.Inits { |
| if fn.Sym().Name == "init" { |
| |
| |
| |
| s := staticinit.Schedule{ |
| Plans: make(map[ir.Node]*staticinit.Plan), |
| Temps: make(map[ir.Node]*ir.Name), |
| } |
| for _, n := range fn.Body { |
| s.StaticInit(n) |
| } |
| fn.Body = s.Out |
| ir.WithFunc(fn, func() { |
| typecheck.Stmts(fn.Body) |
| }) |
|
|
| if len(fn.Body) == 0 { |
| fn.Body = []ir.Node{ir.NewBlockStmt(src.NoXPos, nil)} |
| } |
| } |
|
|
| |
| if len(fn.Body) == 1 { |
| if stmt := fn.Body[0]; stmt.Op() == ir.OBLOCK && len(stmt.(*ir.BlockStmt).List) == 0 { |
| continue |
| } |
| } |
| fns = append(fns, fn.Nname.Linksym()) |
| } |
|
|
| if len(deps) == 0 && len(fns) == 0 && types.LocalPkg.Path != "main" && types.LocalPkg.Path != "runtime" { |
| return |
| } |
|
|
| |
| sym := typecheck.Lookup(".inittask") |
| task := ir.NewNameAt(base.Pos, sym, types.Types[types.TUINT8]) |
| task.Class = ir.PEXTERN |
| sym.Def = task |
| lsym := task.Linksym() |
| ot := 0 |
| ot = objw.Uint32(lsym, ot, 0) |
| ot = objw.Uint32(lsym, ot, uint32(len(fns))) |
| for _, f := range fns { |
| ot = objw.SymPtr(lsym, ot, f, 0) |
| } |
|
|
| |
| |
| |
| for _, d := range deps { |
| lsym.AddRel(base.Ctxt, obj.Reloc{Type: objabi.R_INITORDER, Sym: d}) |
| } |
| |
| |
| objw.Global(lsym, int32(ot), obj.NOPTR) |
| } |
|
|