1 // Copyright 2021 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 base 6 7 import ( 8 "cmd/internal/obj" 9 ) 10 11 // ReservedImports are import paths used internally for generated 12 // symbols by the compiler. 13 // 14 // The linker uses the magic symbol prefixes "go:" and "type:". 15 // Avoid potential confusion between import paths and symbols 16 // by rejecting these reserved imports for now. Also, people 17 // "can do weird things in GOPATH and we'd prefer they didn't 18 // do _that_ weird thing" (per rsc). See also #4257. 19 var ReservedImports = map[string]bool{ 20 "go": true, 21 "type": true, 22 } 23 24 var Ctxt *obj.Link 25 26 // TODO(mdempsky): These should probably be obj.Link methods. 27 28 // PkgLinksym returns the linker symbol for name within the given 29 // package prefix. For user packages, prefix should be the package 30 // path encoded with objabi.PathToPrefix. 31 func PkgLinksym(prefix, name string, abi obj.ABI) *obj.LSym { 32 if name == "_" { 33 // TODO(mdempsky): Cleanup callers and Fatalf instead. 34 return linksym(prefix, "_", abi) 35 } 36 sep := "." 37 if ReservedImports[prefix] { 38 sep = ":" 39 } 40 return linksym(prefix, prefix+sep+name, abi) 41 } 42 43 // Linkname returns the linker symbol for the given name as it might 44 // appear within a //go:linkname directive. 45 func Linkname(name string, abi obj.ABI) *obj.LSym { 46 return linksym("_", name, abi) 47 } 48 49 // linksym is an internal helper function for implementing the above 50 // exported APIs. 51 func linksym(pkg, name string, abi obj.ABI) *obj.LSym { 52 return Ctxt.LookupABIInit(name, abi, func(r *obj.LSym) { r.Pkg = pkg }) 53 } 54