Skip to content

Commit 873c664

Browse files
committed
compiler: add //go:linkname support for globals
We previously used our own //go:extern but //go:linkname is probably the better choice since it's used in the math/bits package.
1 parent 17bb1fe commit 873c664

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

compiler/symbol.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -689,20 +689,25 @@ func (c *compilerContext) getGlobalInfo(g *ssa.Global) globalInfo {
689689
// Check for //go: pragmas, which may change the link name (among others).
690690
doc := c.astComments[info.linkName]
691691
if doc != nil {
692-
info.parsePragmas(doc)
692+
info.parsePragmas(doc, g)
693693
}
694694
return info
695695
}
696696

697697
// Parse //go: pragma comments from the source. In particular, it parses the
698698
// //go:extern pragma on globals.
699-
func (info *globalInfo) parsePragmas(doc *ast.CommentGroup) {
699+
func (info *globalInfo) parsePragmas(doc *ast.CommentGroup, g *ssa.Global) {
700700
for _, comment := range doc.List {
701701
if !strings.HasPrefix(comment.Text, "//go:") {
702702
continue
703703
}
704704
parts := strings.Fields(comment.Text)
705705
switch parts[0] {
706+
case "//go:linkname":
707+
if len(parts) == 3 && g.Name() == parts[1] {
708+
info.linkName = parts[2]
709+
info.extern = true
710+
}
706711
case "//go:extern":
707712
info.extern = true
708713
if len(parts) == 2 {

compiler/testdata/pragma.go

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ package main
22

33
import _ "unsafe"
44

5+
// Use the go:linkname mechanism to link this global to a different package.
6+
// This is used in math/bits.
7+
//
8+
//go:linkname linknamedGlobal runtime.testLinknamedGlobal
9+
var linknamedGlobal int
10+
511
// Creates an external global with name extern_global.
612
//
713
//go:extern extern_global

compiler/testdata/pragma.ll

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ source_filename = "pragma.go"
33
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
44
target triple = "wasm32-unknown-wasi"
55

6+
@runtime.testLinknamedGlobal = external global i32, align 4
67
@extern_global = external global [0 x i8], align 1
78
@main.alignedGlobal = hidden global [4 x i32] zeroinitializer, align 32
89
@main.alignedGlobal16 = hidden global [4 x i32] zeroinitializer, align 16

0 commit comments

Comments
 (0)