Skip to content

cmd/link: attach buildinfo to wasm binary #73742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cmd/link/internal/wasm/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ var dataSects []wasmDataSect

func asmb(ctxt *ld.Link, ldr *loader.Loader) {
sections := []*sym.Section{
ldr.SymSect(ldr.Lookup("go:buildinfo", 0)),
ldr.SymSect(ldr.Lookup("runtime.rodata", 0)),
ldr.SymSect(ldr.Lookup("runtime.typelink", 0)),
ldr.SymSect(ldr.Lookup("runtime.itablink", 0)),
Expand Down
41 changes: 41 additions & 0 deletions src/cmd/link/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1673,3 +1673,44 @@ func TestLinknameBSS(t *testing.T) {
t.Errorf("executable failed to run: %v\n%s", err, out)
}
}

func TestWasmBuildinfo(t *testing.T) {
testenv.MustHaveGoBuild(t)
t.Parallel()

tmpdir := t.TempDir()
src := filepath.Join(tmpdir, "hello.go")

err := os.WriteFile(src, []byte(`package main; func main() { println("hello") }`), 0666)
if err != nil {
t.Fatal(err)
}

exe := filepath.Join(tmpdir, "hello.wasm")
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", exe, src)

env := []string{"GOOS=js", "GOARCH=wasm"}
for _, v := range os.Environ() {
if strings.HasPrefix(v, "GOOS=") || strings.HasPrefix(v, "GOARCH=") {
continue
}
env = append(env, v)
}
cmd.Env = env

out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("build failed: %v\n%s", err, out)
}

const magic = "\xff Go buildinf:"

data, err := os.ReadFile(exe)
if err != nil {
t.Fatalf("failed to read output file: %v", err)
}

if !bytes.Contains(data, []byte(magic)) {
t.Fatalf("output does not contain buildinfo magic: %q", out)
}
}