From 88a6df72c9c056559bda14d88252ba350949bf50 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Fri, 30 Jun 2023 17:11:31 -0400 Subject: [PATCH] cmd/internal/objabi: generalize "is runtime package" check There are several implementations of "is this package path a runtime package". They all have slightly different lists because they all care about slightly different properties of building the runtime. To start converging these, we replace objabi.IsRuntimePackagePath with objabi.LookupPkgSpecial, which returns a struct we can extend with various special build properties. We'll extend this with several other flags in the following CLs. Change-Id: I21959cb8c3d18a350d6060467681c72ea49af712 Reviewed-on: https://go-review.googlesource.com/c/go/+/521698 Reviewed-by: Matthew Dempsky TryBot-Result: Gopher Robot Run-TryBot: Austin Clements --- src/cmd/asm/main.go | 2 +- src/cmd/internal/objabi/path.go | 24 ------------- src/cmd/internal/objabi/pkgspecial.go | 50 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 25 deletions(-) create mode 100644 src/cmd/internal/objabi/pkgspecial.go diff --git a/src/cmd/asm/main.go b/src/cmd/asm/main.go index e75aa8664b..84e9388cef 100644 --- a/src/cmd/asm/main.go +++ b/src/cmd/asm/main.go @@ -35,7 +35,7 @@ func main() { if architecture == nil { log.Fatalf("unrecognized architecture %s", GOARCH) } - compilingRuntime := objabi.IsRuntimePackagePath(*flags.Importpath) + compilingRuntime := objabi.LookupPkgSpecial(*flags.Importpath).AllowAsmABI ctxt := obj.Linknew(architecture.LinkArch) ctxt.Debugasm = flags.PrintOut diff --git a/src/cmd/internal/objabi/path.go b/src/cmd/internal/objabi/path.go index cb06dbe58e..2a42179a36 100644 --- a/src/cmd/internal/objabi/path.go +++ b/src/cmd/internal/objabi/path.go @@ -39,27 +39,3 @@ func PathToPrefix(s string) string { return string(p) } - -// IsRuntimePackagePath examines 'pkgpath' and returns TRUE if it -// belongs to the collection of "runtime-related" packages, including -// "runtime" itself, "reflect", "syscall", and the -// "runtime/internal/*" packages. The compiler and/or assembler in -// some cases need to be aware of when they are building such a -// package, for example to enable features such as ABI selectors in -// assembly sources. -func IsRuntimePackagePath(pkgpath string) bool { - rval := false - switch pkgpath { - case "runtime": - rval = true - case "reflect": - rval = true - case "syscall": - rval = true - case "internal/bytealg": - rval = true - default: - rval = strings.HasPrefix(pkgpath, "runtime/internal") - } - return rval -} diff --git a/src/cmd/internal/objabi/pkgspecial.go b/src/cmd/internal/objabi/pkgspecial.go new file mode 100644 index 0000000000..ac38c1b52e --- /dev/null +++ b/src/cmd/internal/objabi/pkgspecial.go @@ -0,0 +1,50 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package objabi + +import "sync" + +// PkgSpecial indicates special build properties of a given runtime-related +// package. +type PkgSpecial struct { + // AllowAsmABI indicates that assembly in this package is allowed to use ABI + // selectors in symbol names. Generally this is needed for packages that + // interact closely with the runtime package or have performance-critical + // assembly. + AllowAsmABI bool +} + +var allowAsmABIPkgs = []string{ + "runtime", + "reflect", + "syscall", + "internal/bytealg", + "runtime/internal/syscall", + "runtime/internal/startlinetest", +} + +var ( + pkgSpecials map[string]PkgSpecial + pkgSpecialsOnce sync.Once +) + +// LookupPkgSpecial returns special build properties for the given package path. +func LookupPkgSpecial(pkgPath string) PkgSpecial { + pkgSpecialsOnce.Do(func() { + // Construct pkgSpecials from various package lists. This lets us use + // more flexible logic, while keeping the final map simple, and avoids + // the init-time cost of a map. + pkgSpecials = make(map[string]PkgSpecial) + set := func(elt string, f func(*PkgSpecial)) { + s := pkgSpecials[elt] + f(&s) + pkgSpecials[elt] = s + } + for _, pkg := range allowAsmABIPkgs { + set(pkg, func(ps *PkgSpecial) { ps.AllowAsmABI = true }) + } + }) + return pkgSpecials[pkgPath] +} -- 2.50.0