| |
| |
| |
|
|
| package main |
|
|
| import ( |
| "bufio" |
| "bytes" |
| "debug/elf" |
| "debug/macho" |
| "debug/pe" |
| "errors" |
| "internal/abi" |
| "internal/platform" |
| "internal/testenv" |
| "internal/xcoff" |
| "os" |
| "os/exec" |
| "path/filepath" |
| "regexp" |
| "runtime" |
| "strconv" |
| "strings" |
| "testing" |
| "unsafe" |
|
|
| imacho "cmd/internal/macho" |
| "cmd/internal/objfile" |
| "cmd/internal/sys" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| func TestMain(m *testing.M) { |
| |
| |
| |
| if os.Getenv("LINK_TEST_TOOLEXEC") != "" { |
| if strings.TrimSuffix(filepath.Base(os.Args[1]), ".exe") == "link" { |
| |
| |
| os.Args = os.Args[1:] |
| main() |
| os.Exit(0) |
| } |
| |
| cmd := exec.Command(os.Args[1], os.Args[2:]...) |
| cmd.Stdin = os.Stdin |
| cmd.Stdout = os.Stdout |
| cmd.Stderr = os.Stderr |
| if err := cmd.Run(); err != nil { |
| os.Exit(1) |
| } |
| os.Exit(0) |
| } |
|
|
| |
| |
| if os.Getenv("LINK_TEST_EXEC_LINKER") != "" { |
| main() |
| os.Exit(0) |
| } |
|
|
| if testExe, err := os.Executable(); err == nil { |
| |
| testLinker = testExe |
| } |
|
|
| |
| |
| os.Exit(m.Run()) |
| } |
|
|
| |
| |
| var testLinker string |
|
|
| |
| |
| |
| |
| |
| func goCmd(t *testing.T, args ...string) *exec.Cmd { |
| goArgs := []string{args[0], "-toolexec", testenv.Executable(t)} |
| args = append(goArgs, args[1:]...) |
| cmd := testenv.Command(t, testenv.GoToolPath(t), args...) |
| cmd = testenv.CleanCmdEnv(cmd) |
| cmd.Env = append(cmd.Env, "LINK_TEST_TOOLEXEC=1") |
| return cmd |
| } |
|
|
| |
| |
| |
| func linkCmd(t *testing.T, args ...string) *exec.Cmd { |
| |
| args = append([]string{"link"}, args...) |
| cmd := testenv.Command(t, testenv.Executable(t), args...) |
| cmd = testenv.CleanCmdEnv(cmd) |
| cmd.Env = append(cmd.Env, "LINK_TEST_TOOLEXEC=1") |
| return cmd |
| } |
|
|
| var AuthorPaidByTheColumnInch struct { |
| fog int `text:"London. Michaelmas term lately over, and the Lord Chancellor sitting in Lincoln’s Inn Hall. Implacable November weather. As much mud in the streets as if the waters had but newly retired from the face of the earth, and it would not be wonderful to meet a Megalosaurus, forty feet long or so, waddling like an elephantine lizard up Holborn Hill. Smoke lowering down from chimney-pots, making a soft black drizzle, with flakes of soot in it as big as full-grown snowflakes—gone into mourning, one might imagine, for the death of the sun. Dogs, undistinguishable in mire. Horses, scarcely better; splashed to their very blinkers. Foot passengers, jostling one another’s umbrellas in a general infection of ill temper, and losing their foot-hold at street-corners, where tens of thousands of other foot passengers have been slipping and sliding since the day broke (if this day ever broke), adding new deposits to the crust upon crust of mud, sticking at those points tenaciously to the pavement, and accumulating at compound interest. Fog everywhere. Fog up the river, where it flows among green aits and meadows; fog down the river, where it rolls defiled among the tiers of shipping and the waterside pollutions of a great (and dirty) city. Fog on the Essex marshes, fog on the Kentish heights. Fog creeping into the cabooses of collier-brigs; fog lying out on the yards and hovering in the rigging of great ships; fog drooping on the gunwales of barges and small boats. Fog in the eyes and throats of ancient Greenwich pensioners, wheezing by the firesides of their wards; fog in the stem and bowl of the afternoon pipe of the wrathful skipper, down in his close cabin; fog cruelly pinching the toes and fingers of his shivering little ‘prentice boy on deck. Chance people on the bridges peeping over the parapets into a nether sky of fog, with fog all round them, as if they were up in a balloon and hanging in the misty clouds. Gas looming through the fog in divers places in the streets, much as the sun may, from the spongey fields, be seen to loom by husbandman and ploughboy. Most of the shops lighted two hours before their time—as the gas seems to know, for it has a haggard and unwilling look. The raw afternoon is rawest, and the dense fog is densest, and the muddy streets are muddiest near that leaden-headed old obstruction, appropriate ornament for the threshold of a leaden-headed old corporation, Temple Bar. And hard by Temple Bar, in Lincoln’s Inn Hall, at the very heart of the fog, sits the Lord High Chancellor in his High Court of Chancery."` |
|
|
| wind int `text:"It was grand to see how the wind awoke, and bent the trees, and drove the rain before it like a cloud of smoke; and to hear the solemn thunder, and to see the lightning; and while thinking with awe of the tremendous powers by which our little lives are encompassed, to consider how beneficent they are, and how upon the smallest flower and leaf there was already a freshness poured from all this seeming rage, which seemed to make creation new again."` |
|
|
| jarndyce int `text:"Jarndyce and Jarndyce drones on. This scarecrow of a suit has, over the course of time, become so complicated, that no man alive knows what it means. The parties to it understand it least; but it has been observed that no two Chancery lawyers can talk about it for five minutes, without coming to a total disagreement as to all the premises. Innumerable children have been born into the cause; innumerable young people have married into it; innumerable old people have died out of it. Scores of persons have deliriously found themselves made parties in Jarndyce and Jarndyce, without knowing how or why; whole families have inherited legendary hatreds with the suit. The little plaintiff or defendant, who was promised a new rocking-horse when Jarndyce and Jarndyce should be settled, has grown up, possessed himself of a real horse, and trotted away into the other world. Fair wards of court have faded into mothers and grandmothers; a long procession of Chancellors has come in and gone out; the legion of bills in the suit have been transformed into mere bills of mortality; there are not three Jarndyces left upon the earth perhaps, since old Tom Jarndyce in despair blew his brains out at a coffee-house in Chancery Lane; but Jarndyce and Jarndyce still drags its dreary length before the Court, perennially hopeless."` |
|
|
| principle int `text:"The one great principle of the English law is, to make business for itself. There is no other principle distinctly, certainly, and consistently maintained through all its narrow turnings. Viewed by this light it becomes a coherent scheme, and not the monstrous maze the laity are apt to think it. Let them but once clearly perceive that its grand principle is to make business for itself at their expense, and surely they will cease to grumble."` |
| } |
|
|
| func TestLargeSymName(t *testing.T) { |
| |
| |
| |
| _ = AuthorPaidByTheColumnInch |
| } |
|
|
| func TestIssue21703(t *testing.T) { |
| t.Parallel() |
|
|
| testenv.MustHaveGoBuild(t) |
| |
| |
| testenv.MustInternalLink(t, testenv.NoSpecialBuildTypes) |
|
|
| const source = ` |
| package main |
| const X = "\n!\n" |
| func main() {} |
| ` |
|
|
| tmpdir := t.TempDir() |
| main := filepath.Join(tmpdir, "main.go") |
|
|
| err := os.WriteFile(main, []byte(source), 0666) |
| if err != nil { |
| t.Fatalf("failed to write main.go: %v\n", err) |
| } |
|
|
| importcfgfile := filepath.Join(tmpdir, "importcfg") |
| testenv.WriteImportcfg(t, importcfgfile, nil, main) |
|
|
| cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-p=main", "main.go") |
| cmd.Dir = tmpdir |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("failed to compile main.go: %v, output: %s\n", err, out) |
| } |
|
|
| cmd = linkCmd(t, "-importcfg="+importcfgfile, "main.o") |
| cmd.Dir = tmpdir |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| if runtime.GOOS == "android" && runtime.GOARCH == "arm64" { |
| testenv.SkipFlaky(t, 58806) |
| } |
| t.Fatalf("failed to link main.o: %v, output: %s\n", err, out) |
| } |
| } |
|
|
| |
| |
| |
| |
| func TestIssue28429(t *testing.T) { |
| t.Parallel() |
|
|
| testenv.MustHaveGoBuild(t) |
| |
| |
| testenv.MustInternalLink(t, testenv.NoSpecialBuildTypes) |
|
|
| tmpdir := t.TempDir() |
|
|
| write := func(name, content string) { |
| err := os.WriteFile(filepath.Join(tmpdir, name), []byte(content), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
| } |
|
|
| runGo := func(args ...string) { |
| cmd := testenv.Command(t, testenv.GoToolPath(t), args...) |
| cmd.Dir = tmpdir |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("'go %s' failed: %v, output: %s", |
| strings.Join(args, " "), err, out) |
| } |
| } |
|
|
| |
| write("main.go", "package main; func main() {}") |
| importcfgfile := filepath.Join(tmpdir, "importcfg") |
| testenv.WriteImportcfg(t, importcfgfile, nil, filepath.Join(tmpdir, "main.go")) |
| runGo("tool", "compile", "-importcfg="+importcfgfile, "-p=main", "main.go") |
| runGo("tool", "pack", "c", "main.a", "main.o") |
|
|
| |
| |
| write(".facts", "this is not an object file") |
| runGo("tool", "pack", "r", "main.a", ".facts") |
|
|
| |
| |
| cmd := linkCmd(t, "-importcfg="+importcfgfile, "main.a") |
| cmd.Dir = tmpdir |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| if runtime.GOOS == "android" && runtime.GOARCH == "arm64" { |
| testenv.SkipFlaky(t, 58806) |
| } |
| t.Fatalf("linker failed: %v, output %s", err, out) |
| } |
| } |
|
|
| func TestUnresolved(t *testing.T) { |
| testenv.MustHaveGoBuild(t) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| write := func(name, content string) { |
| err := os.WriteFile(filepath.Join(tmpdir, name), []byte(content), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
| } |
|
|
| |
| |
| |
| |
|
|
| write("go.mod", "module testunresolved\n") |
| write("main.go", `package main |
| |
| func main() { |
| x() |
| } |
| |
| func x() |
| `) |
| write("main.s", ` |
| TEXT ·x(SB),0,$0 |
| MOVD zero<>(SB), AX |
| MOVD zero(SB), AX |
| MOVD ·zero(SB), AX |
| RET |
| `) |
| cmd := goCmd(t, "build") |
| cmd.Dir = tmpdir |
| cmd.Env = append(cmd.Env, |
| "GOARCH=amd64", "GOOS=linux", "GOPATH="+filepath.Join(tmpdir, "_gopath")) |
| out, err := cmd.CombinedOutput() |
| if err == nil { |
| t.Fatalf("expected build to fail, but it succeeded") |
| } |
| out = regexp.MustCompile("(?m)^#.*\n").ReplaceAll(out, nil) |
| got := string(out) |
| want := `main.x: relocation target zero not defined |
| main.x: relocation target zero not defined |
| main.x: relocation target main.zero not defined |
| ` |
| if want != got { |
| t.Fatalf("want:\n%sgot:\n%s", want, got) |
| } |
| } |
|
|
| func TestIssue33979(t *testing.T) { |
| testenv.MustHaveGoBuild(t) |
| testenv.MustHaveCGO(t) |
| |
| |
| testenv.MustInternalLink(t, testenv.SpecialBuildTypes{Cgo: true}) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| write := func(name, content string) { |
| err := os.WriteFile(filepath.Join(tmpdir, name), []byte(content), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
| } |
|
|
| run := func(name string, args ...string) string { |
| cmd := testenv.Command(t, name, args...) |
| cmd.Dir = tmpdir |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("'go %s' failed: %v, output: %s", strings.Join(args, " "), err, out) |
| } |
| return string(out) |
| } |
| runGo := func(args ...string) string { |
| return run(testenv.GoToolPath(t), args...) |
| } |
|
|
| |
| |
| |
| |
|
|
| write("main.go", `package main |
| func main() { |
| x() |
| } |
| func x() |
| `) |
| |
| write("x.s", ` |
| TEXT ·x(SB),0,$0 |
| CALL foo(SB) |
| RET |
| `) |
| write("x.c", ` |
| void undefined(); |
| |
| void foo() { |
| undefined(); |
| } |
| `) |
|
|
| cc := strings.TrimSpace(runGo("env", "CC")) |
| cflags := strings.Fields(runGo("env", "GOGCCFLAGS")) |
|
|
| importcfgfile := filepath.Join(tmpdir, "importcfg") |
| testenv.WriteImportcfg(t, importcfgfile, nil, "runtime") |
|
|
| |
| runGo("tool", "asm", "-p=main", "-gensymabis", "-o", "symabis", "x.s") |
| runGo("tool", "compile", "-importcfg="+importcfgfile, "-symabis", "symabis", "-p=main", "-o", "x1.o", "main.go") |
| runGo("tool", "asm", "-p=main", "-o", "x2.o", "x.s") |
| run(cc, append(cflags, "-c", "-o", "x3.o", "x.c")...) |
| runGo("tool", "pack", "c", "x.a", "x1.o", "x2.o", "x3.o") |
|
|
| |
| cmd := linkCmd(t, "-importcfg="+importcfgfile, "-linkmode=internal", "x.a") |
| cmd.Dir = tmpdir |
| out, err := cmd.CombinedOutput() |
| if err == nil { |
| t.Fatalf("expected link to fail, but it succeeded") |
| } |
| re := regexp.MustCompile(`(?m)^main\(.*text\): relocation target undefined not defined$`) |
| if !re.Match(out) { |
| t.Fatalf("got:\n%q\nwant:\n%s", out, re) |
| } |
| } |
|
|
| func TestBuildForTvOS(t *testing.T) { |
| testenv.MustHaveCGO(t) |
| testenv.MustHaveGoBuild(t) |
|
|
| |
| if runtime.GOOS != "darwin" { |
| t.Skip("skipping on non-darwin platform") |
| } |
| if testing.Short() && testenv.Builder() == "" { |
| t.Skip("skipping in -short mode with $GO_BUILDER_NAME empty") |
| } |
| if err := testenv.Command(t, "xcrun", "--help").Run(); err != nil { |
| t.Skipf("error running xcrun, required for iOS cross build: %v", err) |
| } |
|
|
| t.Parallel() |
|
|
| sdkPath, err := testenv.Command(t, "xcrun", "--sdk", "appletvos", "--show-sdk-path").Output() |
| if err != nil { |
| t.Skip("failed to locate appletvos SDK, skipping") |
| } |
| CC := []string{ |
| "clang", |
| "-arch", |
| "arm64", |
| "-isysroot", strings.TrimSpace(string(sdkPath)), |
| "-mtvos-version-min=12.0", |
| "-fembed-bitcode", |
| } |
| CGO_LDFLAGS := []string{"-framework", "CoreFoundation"} |
| lib := filepath.Join("testdata", "testBuildFortvOS", "lib.go") |
| tmpDir := t.TempDir() |
|
|
| ar := filepath.Join(tmpDir, "lib.a") |
| cmd := goCmd(t, "build", "-buildmode=c-archive", "-o", ar, lib) |
| env := []string{ |
| "CGO_ENABLED=1", |
| "GOOS=ios", |
| "GOARCH=arm64", |
| "CC=" + strings.Join(CC, " "), |
| "CGO_CFLAGS=", |
| "CGO_LDFLAGS=" + strings.Join(CGO_LDFLAGS, " "), |
| } |
| cmd.Env = append(cmd.Env, env...) |
| t.Logf("%q %v", env, cmd) |
| if out, err := cmd.CombinedOutput(); err != nil { |
| t.Fatalf("%v: %v:\n%s", cmd.Args, err, out) |
| } |
|
|
| link := testenv.Command(t, CC[0], CC[1:]...) |
| link.Args = append(link.Args, CGO_LDFLAGS...) |
| link.Args = append(link.Args, "-o", filepath.Join(tmpDir, "a.out")) |
| link.Args = append(link.Args, ar, filepath.Join("testdata", "testBuildFortvOS", "main.m")) |
| t.Log(link) |
| if out, err := link.CombinedOutput(); err != nil { |
| t.Fatalf("%v: %v:\n%s", link.Args, err, out) |
| } |
| } |
|
|
| var testXFlagSrc = ` |
| package main |
| var X = "hello" |
| var Z = [99999]int{99998:12345} // make it large enough to be mmaped |
| func main() { println(X) } |
| ` |
|
|
| func TestXFlag(t *testing.T) { |
| testenv.MustHaveGoBuild(t) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "main.go") |
| err := os.WriteFile(src, []byte(testXFlagSrc), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
|
|
| cmd := goCmd(t, "build", "-ldflags=-X=main.X=meow", "-o", filepath.Join(tmpdir, "main"), src) |
| if out, err := cmd.CombinedOutput(); err != nil { |
| t.Errorf("%v: %v:\n%s", cmd.Args, err, out) |
| } |
| } |
|
|
| var trivialSrc = ` |
| package main |
| func main() { } |
| ` |
|
|
| func TestMachOBuildVersion(t *testing.T) { |
| testenv.MustHaveGoBuild(t) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "main.go") |
| err := os.WriteFile(src, []byte(trivialSrc), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
|
|
| exe := filepath.Join(tmpdir, "main") |
| cmd := goCmd(t, "build", "-ldflags=-linkmode=internal", "-o", exe, src) |
| cmd.Env = append(cmd.Env, |
| "CGO_ENABLED=0", |
| "GOOS=darwin", |
| "GOARCH=amd64", |
| ) |
| if out, err := cmd.CombinedOutput(); err != nil { |
| t.Fatalf("%v: %v:\n%s", cmd.Args, err, out) |
| } |
| exef, err := os.Open(exe) |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer exef.Close() |
| exem, err := macho.NewFile(exef) |
| if err != nil { |
| t.Fatal(err) |
| } |
| found := false |
| checkMin := func(ver uint32) { |
| major, minor, patch := (ver>>16)&0xff, (ver>>8)&0xff, (ver>>0)&0xff |
| if major < 12 { |
| t.Errorf("LC_BUILD_VERSION version %d.%d.%d < 12.0.0", major, minor, patch) |
| } |
| } |
| for _, cmd := range exem.Loads { |
| raw := cmd.Raw() |
| type_ := exem.ByteOrder.Uint32(raw) |
| if type_ != imacho.LC_BUILD_VERSION { |
| continue |
| } |
| osVer := exem.ByteOrder.Uint32(raw[12:]) |
| checkMin(osVer) |
| sdkVer := exem.ByteOrder.Uint32(raw[16:]) |
| checkMin(sdkVer) |
| found = true |
| break |
| } |
| if !found { |
| t.Errorf("no LC_BUILD_VERSION load command found") |
| } |
| } |
|
|
| func TestMachOUUID(t *testing.T) { |
| testenv.MustHaveGoBuild(t) |
| if runtime.GOOS != "darwin" { |
| t.Skip("this is only for darwin") |
| } |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "main.go") |
| err := os.WriteFile(src, []byte(trivialSrc), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
|
|
| extractUUID := func(exe string) string { |
| exem, err := macho.Open(exe) |
| if err != nil { |
| t.Fatal(err) |
| } |
| defer exem.Close() |
| for _, cmd := range exem.Loads { |
| raw := cmd.Raw() |
| type_ := exem.ByteOrder.Uint32(raw) |
| if type_ != imacho.LC_UUID { |
| continue |
| } |
| return string(raw[8:24]) |
| } |
| return "" |
| } |
|
|
| tests := []struct{ name, ldflags, expect string }{ |
| {"default", "", "gobuildid"}, |
| {"gobuildid", "-B=gobuildid", "gobuildid"}, |
| {"specific", "-B=0x0123456789ABCDEF0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF\x01\x23\x45\x67\x89\xAB\xCD\xEF"}, |
| {"none", "-B=none", ""}, |
| } |
| if testenv.HasCGO() { |
| for _, test := range tests { |
| t1 := test |
| t1.name += "_external" |
| t1.ldflags += " -linkmode=external" |
| tests = append(tests, t1) |
| } |
| } |
| for _, test := range tests { |
| t.Run(test.name, func(t *testing.T) { |
| exe := filepath.Join(tmpdir, test.name) |
| cmd := goCmd(t, "build", "-ldflags="+test.ldflags, "-o", exe, src) |
| if out, err := cmd.CombinedOutput(); err != nil { |
| t.Fatalf("%v: %v:\n%s", cmd.Args, err, out) |
| } |
| uuid := extractUUID(exe) |
| if test.expect == "gobuildid" { |
| |
| |
| if uuid == "" { |
| t.Fatal("expect nonempty UUID, got empty") |
| } |
| |
| if uuid[6]>>4 != 3 { |
| t.Errorf("expect v3 UUID, got %X (version %d)", uuid, uuid[6]>>4) |
| } |
| } else if uuid != test.expect { |
| t.Errorf("UUID mismatch: got %X, want %X", uuid, test.expect) |
| } |
| }) |
| } |
| } |
|
|
| const Issue34788src = ` |
| |
| package blah |
| |
| func Blah(i int) int { |
| a := [...]int{1, 2, 3, 4, 5, 6, 7, 8} |
| return a[i&7] |
| } |
| ` |
|
|
| func TestIssue34788Android386TLSSequence(t *testing.T) { |
| testenv.MustHaveGoBuild(t) |
|
|
| |
| |
| |
| if runtime.GOARCH != "amd64" || |
| (runtime.GOOS != "darwin" && runtime.GOOS != "linux") { |
| t.Skip("skipping on non-{linux,darwin}/amd64 platform") |
| } |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "blah.go") |
| err := os.WriteFile(src, []byte(Issue34788src), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
|
|
| obj := filepath.Join(tmpdir, "blah.o") |
| cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p=blah", "-o", obj, src) |
| cmd.Env = append(os.Environ(), "GOARCH=386", "GOOS=android") |
| if out, err := cmd.CombinedOutput(); err != nil { |
| t.Fatalf("failed to compile blah.go: %v, output: %s\n", err, out) |
| } |
|
|
| |
| cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "objdump", obj) |
| out, oerr := cmd.CombinedOutput() |
| if oerr != nil { |
| t.Fatalf("failed to objdump blah.o: %v, output: %s\n", oerr, out) |
| } |
|
|
| |
| scanner := bufio.NewScanner(bytes.NewReader(out)) |
| for scanner.Scan() { |
| line := scanner.Text() |
| if strings.Contains(line, "R_TLS_LE") { |
| t.Errorf("objdump output contains unexpected R_TLS_LE reloc: %s", line) |
| } |
| } |
| } |
|
|
| const testStrictDupGoSrc = ` |
| package main |
| func f() |
| func main() { f() } |
| ` |
|
|
| const testStrictDupAsmSrc1 = ` |
| #include "textflag.h" |
| TEXT ·f(SB), NOSPLIT|DUPOK, $0-0 |
| RET |
| ` |
|
|
| const testStrictDupAsmSrc2 = ` |
| #include "textflag.h" |
| TEXT ·f(SB), NOSPLIT|DUPOK, $0-0 |
| JMP 0(PC) |
| ` |
|
|
| const testStrictDupAsmSrc3 = ` |
| #include "textflag.h" |
| GLOBL ·rcon(SB), RODATA|DUPOK, $64 |
| ` |
|
|
| const testStrictDupAsmSrc4 = ` |
| #include "textflag.h" |
| GLOBL ·rcon(SB), RODATA|DUPOK, $32 |
| ` |
|
|
| func TestStrictDup(t *testing.T) { |
| |
| testenv.MustHaveGoBuild(t) |
|
|
| asmfiles := []struct { |
| fname string |
| payload string |
| }{ |
| {"a", testStrictDupAsmSrc1}, |
| {"b", testStrictDupAsmSrc2}, |
| {"c", testStrictDupAsmSrc3}, |
| {"d", testStrictDupAsmSrc4}, |
| } |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "x.go") |
| err := os.WriteFile(src, []byte(testStrictDupGoSrc), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
| for _, af := range asmfiles { |
| src = filepath.Join(tmpdir, af.fname+".s") |
| err = os.WriteFile(src, []byte(af.payload), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
| } |
| src = filepath.Join(tmpdir, "go.mod") |
| err = os.WriteFile(src, []byte("module teststrictdup\n"), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
|
|
| cmd := goCmd(t, "build", "-ldflags=-strictdups=1") |
| cmd.Dir = tmpdir |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("linking with -strictdups=1 failed: %v\n%s", err, string(out)) |
| } |
| if !bytes.Contains(out, []byte("mismatched payload")) { |
| t.Errorf("unexpected output:\n%s", out) |
| } |
|
|
| cmd = goCmd(t, "build", "-ldflags=-strictdups=2") |
| cmd.Dir = tmpdir |
| out, err = cmd.CombinedOutput() |
| if err == nil { |
| t.Errorf("linking with -strictdups=2 did not fail") |
| } |
| |
| |
| if !(bytes.Contains(out, []byte("mismatched payload: new length")) || |
| bytes.Contains(out, []byte("mismatched payload: same length but different contents"))) || |
| !bytes.Contains(out, []byte("mismatched payload: different sizes")) { |
| t.Errorf("unexpected output:\n%s", out) |
| } |
| } |
|
|
| const testFuncAlignSrc = ` |
| package main |
| import ( |
| "fmt" |
| ) |
| func alignPc() |
| var alignPcFnAddr uintptr |
| |
| func main() { |
| if alignPcFnAddr % 512 != 0 { |
| fmt.Printf("expected 512 bytes alignment, got %v\n", alignPcFnAddr) |
| } else { |
| fmt.Printf("PASS") |
| } |
| } |
| ` |
|
|
| var testFuncAlignAsmSources = map[string]string{ |
| "arm64": ` |
| #include "textflag.h" |
| |
| TEXT ·alignPc(SB),NOSPLIT, $0-0 |
| MOVD $2, R0 |
| PCALIGN $512 |
| MOVD $3, R1 |
| RET |
| |
| GLOBL ·alignPcFnAddr(SB),RODATA,$8 |
| DATA ·alignPcFnAddr(SB)/8,$·alignPc(SB) |
| `, |
| "loong64": ` |
| #include "textflag.h" |
| |
| TEXT ·alignPc(SB),NOSPLIT, $0-0 |
| MOVV $2, R4 |
| PCALIGN $512 |
| MOVV $3, R5 |
| RET |
| |
| GLOBL ·alignPcFnAddr(SB),RODATA,$8 |
| DATA ·alignPcFnAddr(SB)/8,$·alignPc(SB) |
| `, |
| } |
|
|
| |
| |
| func TestFuncAlign(t *testing.T) { |
| testFuncAlignAsmSrc := testFuncAlignAsmSources[runtime.GOARCH] |
| if len(testFuncAlignAsmSrc) == 0 || runtime.GOOS != "linux" { |
| t.Skip("skipping on non-linux/{arm64,loong64} platform") |
| } |
| testenv.MustHaveGoBuild(t) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "go.mod") |
| err := os.WriteFile(src, []byte("module cmd/link/TestFuncAlign/falign"), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
| src = filepath.Join(tmpdir, "falign.go") |
| err = os.WriteFile(src, []byte(testFuncAlignSrc), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
| src = filepath.Join(tmpdir, "falign.s") |
| err = os.WriteFile(src, []byte(testFuncAlignAsmSrc), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
|
|
| cmd := goCmd(t, "build", "-o", "falign") |
| cmd.Dir = tmpdir |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("build failed: %v", err) |
| } |
| cmd = testenv.Command(t, tmpdir+"/falign") |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("failed to run with err %v, output: %s", err, out) |
| } |
| if string(out) != "PASS" { |
| t.Errorf("unexpected output: %s\n", out) |
| } |
| } |
|
|
| const testFuncAlignOptionSrc = ` |
| package main |
| //go:noinline |
| func foo() { |
| } |
| //go:noinline |
| func bar() { |
| } |
| //go:noinline |
| func baz() { |
| } |
| func main() { |
| foo() |
| bar() |
| baz() |
| } |
| ` |
|
|
| |
| func TestFuncAlignOption(t *testing.T) { |
| testenv.MustHaveGoBuild(t) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "falign.go") |
| err := os.WriteFile(src, []byte(testFuncAlignOptionSrc), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
|
|
| alignTest := func(align uint64) { |
| exeName := "falign.exe" |
| cmd := goCmd(t, "build", "-ldflags=-funcalign="+strconv.FormatUint(align, 10), "-o", exeName, "falign.go") |
| cmd.Dir = tmpdir |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("build failed: %v \n%s", err, out) |
| } |
| exe := filepath.Join(tmpdir, exeName) |
| cmd = testenv.Command(t, exe) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("failed to run with err %v, output: %s", err, out) |
| } |
|
|
| |
| f, err := objfile.Open(exe) |
| if err != nil { |
| t.Fatalf("failed to open file:%v\n", err) |
| } |
| defer f.Close() |
|
|
| fname := map[string]bool{"_main.foo": false, |
| "_main.bar": false, |
| "_main.baz": false} |
| syms, err := f.Symbols() |
| if err != nil { |
| t.Errorf("failed to get symbols with err %v", err) |
| } |
| for _, s := range syms { |
| fn := s.Name |
| if _, ok := fname[fn]; !ok { |
| fn = "_" + s.Name |
| if _, ok := fname[fn]; !ok { |
| continue |
| } |
| } |
| if s.Addr%align != 0 { |
| t.Fatalf("unaligned function: %s %x. Expected alignment: %d\n", fn, s.Addr, align) |
| } |
| fname[fn] = true |
| } |
| for k, v := range fname { |
| if !v { |
| t.Fatalf("function %s not found\n", k) |
| } |
| } |
| } |
| alignTest(16) |
| alignTest(32) |
| } |
|
|
| const testTrampSrc = ` |
| package main |
| import "fmt" |
| func main() { |
| fmt.Println("hello") |
| |
| defer func(){ |
| if e := recover(); e == nil { |
| panic("did not panic") |
| } |
| }() |
| f1() |
| } |
| |
| // Test deferreturn trampolines. See issue #39049. |
| func f1() { defer f2() } |
| func f2() { panic("XXX") } |
| ` |
|
|
| func TestTrampoline(t *testing.T) { |
| |
| |
| |
| |
| buildmodes := []string{"default"} |
| switch runtime.GOARCH { |
| case "arm", "arm64", "ppc64", "loong64": |
| case "ppc64le": |
| |
| buildmodes = append(buildmodes, "pie") |
| default: |
| t.Skipf("trampoline insertion is not implemented on %s", runtime.GOARCH) |
| } |
|
|
| testenv.MustHaveGoBuild(t) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "hello.go") |
| err := os.WriteFile(src, []byte(testTrampSrc), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
| exe := filepath.Join(tmpdir, "hello.exe") |
|
|
| for _, mode := range buildmodes { |
| cmd := goCmd(t, "build", "-buildmode="+mode, "-ldflags=-debugtramp=2", "-o", exe, src) |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("build (%s) failed: %v\n%s", mode, err, out) |
| } |
| cmd = testenv.Command(t, exe) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("executable failed to run (%s): %v\n%s", mode, err, out) |
| } |
| if string(out) != "hello\n" { |
| t.Errorf("unexpected output (%s):\n%s", mode, out) |
| } |
|
|
| out, err = testenv.Command(t, testenv.GoToolPath(t), "tool", "nm", exe).CombinedOutput() |
| if err != nil { |
| t.Errorf("nm failure: %s\n%s\n", err, string(out)) |
| } |
| if ok, _ := regexp.Match("T runtime.deferreturn(\\+0)?-tramp0", out); !ok { |
| t.Errorf("Trampoline T runtime.deferreturn(+0)?-tramp0 is missing") |
| } |
| } |
| } |
|
|
| const testTrampCgoSrc = ` |
| package main |
| |
| // #include <stdio.h> |
| // void CHello() { printf("hello\n"); fflush(stdout); } |
| import "C" |
| |
| func main() { |
| C.CHello() |
| } |
| ` |
|
|
| func TestTrampolineCgo(t *testing.T) { |
| |
| |
| |
| |
| buildmodes := []string{"default"} |
| switch runtime.GOARCH { |
| case "arm", "arm64", "ppc64", "loong64": |
| case "ppc64le": |
| |
| buildmodes = append(buildmodes, "pie") |
| default: |
| t.Skipf("trampoline insertion is not implemented on %s", runtime.GOARCH) |
| } |
|
|
| testenv.MustHaveGoBuild(t) |
| testenv.MustHaveCGO(t) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "hello.go") |
| err := os.WriteFile(src, []byte(testTrampCgoSrc), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
| exe := filepath.Join(tmpdir, "hello.exe") |
|
|
| for _, mode := range buildmodes { |
| cmd := goCmd(t, "build", "-buildmode="+mode, "-ldflags=-debugtramp=2", "-o", exe, src) |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("build (%s) failed: %v\n%s", mode, err, out) |
| } |
| cmd = testenv.Command(t, exe) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("executable failed to run (%s): %v\n%s", mode, err, out) |
| } |
| if string(out) != "hello\n" && string(out) != "hello\r\n" { |
| t.Errorf("unexpected output (%s):\n%s", mode, out) |
| } |
|
|
| |
|
|
| if !testenv.CanInternalLink(true) { |
| continue |
| } |
| cmd = goCmd(t, "build", "-buildmode="+mode, "-ldflags=-debugtramp=2 -linkmode=internal", "-o", exe, src) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("build (%s) failed: %v\n%s", mode, err, out) |
| } |
| cmd = testenv.Command(t, exe) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("executable failed to run (%s): %v\n%s", mode, err, out) |
| } |
| if string(out) != "hello\n" && string(out) != "hello\r\n" { |
| t.Errorf("unexpected output (%s):\n%s", mode, out) |
| } |
| } |
| } |
|
|
| func TestIndexMismatch(t *testing.T) { |
| |
| |
| |
| testenv.MustHaveGoBuild(t) |
| |
| |
| testenv.MustInternalLink(t, testenv.NoSpecialBuildTypes) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| aSrc := filepath.Join("testdata", "testIndexMismatch", "a.go") |
| bSrc := filepath.Join("testdata", "testIndexMismatch", "b.go") |
| mSrc := filepath.Join("testdata", "testIndexMismatch", "main.go") |
| aObj := filepath.Join(tmpdir, "a.o") |
| mObj := filepath.Join(tmpdir, "main.o") |
| exe := filepath.Join(tmpdir, "main.exe") |
|
|
| importcfgFile := filepath.Join(tmpdir, "runtime.importcfg") |
| testenv.WriteImportcfg(t, importcfgFile, nil, "runtime") |
| importcfgWithAFile := filepath.Join(tmpdir, "witha.importcfg") |
| testenv.WriteImportcfg(t, importcfgWithAFile, map[string]string{"a": aObj}, "runtime") |
|
|
| |
| cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgFile, "-p=a", "-o", aObj, aSrc) |
| t.Log(cmd) |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("compiling a.go failed: %v\n%s", err, out) |
| } |
| cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgWithAFile, "-p=main", "-I", tmpdir, "-o", mObj, mSrc) |
| t.Log(cmd) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("compiling main.go failed: %v\n%s", err, out) |
| } |
| cmd = linkCmd(t, "-importcfg="+importcfgWithAFile, "-L", tmpdir, "-o", exe, mObj) |
| t.Log(cmd) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| if runtime.GOOS == "android" && runtime.GOARCH == "arm64" { |
| testenv.SkipFlaky(t, 58806) |
| } |
| t.Errorf("linking failed: %v\n%s", err, out) |
| } |
|
|
| |
| |
| cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgFile, "-p=a", "-o", aObj, bSrc) |
| t.Log(cmd) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("compiling a.go failed: %v\n%s", err, out) |
| } |
| cmd = linkCmd(t, "-importcfg="+importcfgWithAFile, "-L", tmpdir, "-o", exe, mObj) |
| t.Log(cmd) |
| out, err = cmd.CombinedOutput() |
| if err == nil { |
| t.Fatalf("linking didn't fail") |
| } |
| if !bytes.Contains(out, []byte("fingerprint mismatch")) { |
| t.Errorf("did not see expected error message. out:\n%s", out) |
| } |
| } |
|
|
| func TestPErsrcBinutils(t *testing.T) { |
| |
| testenv.MustHaveGoBuild(t) |
|
|
| if (runtime.GOARCH != "386" && runtime.GOARCH != "amd64") || runtime.GOOS != "windows" { |
| |
| t.Skipf("this is only for windows/amd64 and windows/386") |
| } |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| pkgdir := filepath.Join("testdata", "pe-binutils") |
| exe := filepath.Join(tmpdir, "a.exe") |
| cmd := goCmd(t, "build", "-o", exe) |
| cmd.Dir = pkgdir |
| |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("building failed: %v, output:\n%s", err, out) |
| } |
|
|
| |
| b, err := os.ReadFile(exe) |
| if err != nil { |
| t.Fatalf("reading output failed: %v", err) |
| } |
| if !bytes.Contains(b, []byte("Hello Gophers!")) { |
| t.Fatalf("binary does not contain expected content") |
| } |
| } |
|
|
| func TestPErsrcLLVM(t *testing.T) { |
| |
| testenv.MustHaveGoBuild(t) |
|
|
| if runtime.GOOS != "windows" { |
| t.Skipf("this is a windows-only test") |
| } |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| pkgdir := filepath.Join("testdata", "pe-llvm") |
| exe := filepath.Join(tmpdir, "a.exe") |
| cmd := goCmd(t, "build", "-o", exe) |
| cmd.Dir = pkgdir |
| |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("building failed: %v, output:\n%s", err, out) |
| } |
|
|
| |
| b, err := os.ReadFile(exe) |
| if err != nil { |
| t.Fatalf("reading output failed: %v", err) |
| } |
| if !bytes.Contains(b, []byte("resname RCDATA a.rc")) { |
| t.Fatalf("binary does not contain expected content") |
| } |
| } |
|
|
| func TestContentAddressableSymbols(t *testing.T) { |
| |
| testenv.MustHaveGoBuild(t) |
|
|
| t.Parallel() |
|
|
| src := filepath.Join("testdata", "testHashedSyms", "p.go") |
| cmd := goCmd(t, "run", src) |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("command %s failed: %v\n%s", cmd, err, out) |
| } |
| } |
|
|
| func TestReadOnly(t *testing.T) { |
| |
| testenv.MustHaveGoBuild(t) |
|
|
| t.Parallel() |
|
|
| src := filepath.Join("testdata", "testRO", "x.go") |
| cmd := goCmd(t, "run", src) |
| out, err := cmd.CombinedOutput() |
| if err == nil { |
| t.Errorf("running test program did not fail. output:\n%s", out) |
| } |
| } |
|
|
| const testIssue38554Src = ` |
| package main |
| |
| type T [10<<20]byte |
| |
| //go:noinline |
| func f() T { |
| return T{} // compiler will make a large stmp symbol, but not used. |
| } |
| |
| func main() { |
| x := f() |
| println(x[1]) |
| } |
| ` |
|
|
| func TestIssue38554(t *testing.T) { |
| testenv.MustHaveGoBuild(t) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "x.go") |
| err := os.WriteFile(src, []byte(testIssue38554Src), 0666) |
| if err != nil { |
| t.Fatalf("failed to write source file: %v", err) |
| } |
| exe := filepath.Join(tmpdir, "x.exe") |
| cmd := goCmd(t, "build", "-o", exe, src) |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("build failed: %v\n%s", err, out) |
| } |
|
|
| fi, err := os.Stat(exe) |
| if err != nil { |
| t.Fatalf("failed to stat output file: %v", err) |
| } |
|
|
| |
| |
| |
| const want = 5 << 20 |
| if got := fi.Size(); got > want { |
| t.Errorf("binary too big: got %d, want < %d", got, want) |
| } |
| } |
|
|
| const testIssue42396src = ` |
| package main |
| |
| //go:noinline |
| //go:nosplit |
| func callee(x int) { |
| } |
| |
| func main() { |
| callee(9) |
| } |
| ` |
|
|
| func TestIssue42396(t *testing.T) { |
| testenv.MustHaveGoBuild(t) |
|
|
| if !platform.RaceDetectorSupported(runtime.GOOS, runtime.GOARCH) { |
| t.Skip("no race detector support") |
| } |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "main.go") |
| err := os.WriteFile(src, []byte(testIssue42396src), 0666) |
| if err != nil { |
| t.Fatalf("failed to write source file: %v", err) |
| } |
| exe := filepath.Join(tmpdir, "main.exe") |
| cmd := goCmd(t, "build", "-gcflags=-race", "-o", exe, src) |
| out, err := cmd.CombinedOutput() |
| if err == nil { |
| t.Fatalf("build unexpectedly succeeded") |
| } |
|
|
| |
| |
| if strings.Contains(string(out), "panic:") { |
| t.Fatalf("build should not fail with panic:\n%s", out) |
| } |
| const want = "reference to undefined builtin" |
| if !strings.Contains(string(out), want) { |
| t.Fatalf("error message incorrect: expected it to contain %q but instead got:\n%s\n", want, out) |
| } |
| } |
|
|
| const testLargeRelocSrc = ` |
| package main |
| |
| var x = [1<<25]byte{1<<23: 23, 1<<24: 24} |
| |
| var addr = [...]*byte{ |
| &x[1<<23-1], |
| &x[1<<23], |
| &x[1<<23+1], |
| &x[1<<24-1], |
| &x[1<<24], |
| &x[1<<24+1], |
| } |
| |
| func main() { |
| // check relocations in instructions |
| check(x[1<<23-1], 0) |
| check(x[1<<23], 23) |
| check(x[1<<23+1], 0) |
| check(x[1<<24-1], 0) |
| check(x[1<<24], 24) |
| check(x[1<<24+1], 0) |
| |
| // check absolute address relocations in data |
| check(*addr[0], 0) |
| check(*addr[1], 23) |
| check(*addr[2], 0) |
| check(*addr[3], 0) |
| check(*addr[4], 24) |
| check(*addr[5], 0) |
| } |
| |
| func check(x, y byte) { |
| if x != y { |
| panic("FAIL") |
| } |
| } |
| ` |
|
|
| func TestLargeReloc(t *testing.T) { |
| |
| |
| |
| testenv.MustHaveGoBuild(t) |
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "x.go") |
| err := os.WriteFile(src, []byte(testLargeRelocSrc), 0666) |
| if err != nil { |
| t.Fatalf("failed to write source file: %v", err) |
| } |
| cmd := goCmd(t, "run", src) |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("build failed: %v. output:\n%s", err, out) |
| } |
|
|
| if testenv.HasCGO() { |
| cmd = goCmd(t, "run", "-ldflags=-linkmode=external", src) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("build failed: %v. output:\n%s", err, out) |
| } |
| } |
| } |
|
|
| func TestUnlinkableObj(t *testing.T) { |
| |
| testenv.MustHaveGoBuild(t) |
| t.Parallel() |
|
|
| if true { |
| t.Skip("TODO(mdempsky): Fix ICE when importing unlinkable objects for GOEXPERIMENT=unified") |
| } |
|
|
| tmpdir := t.TempDir() |
|
|
| xSrc := filepath.Join(tmpdir, "x.go") |
| pSrc := filepath.Join(tmpdir, "p.go") |
| xObj := filepath.Join(tmpdir, "x.o") |
| pObj := filepath.Join(tmpdir, "p.o") |
| exe := filepath.Join(tmpdir, "x.exe") |
| importcfgfile := filepath.Join(tmpdir, "importcfg") |
| testenv.WriteImportcfg(t, importcfgfile, map[string]string{"p": pObj}) |
| err := os.WriteFile(xSrc, []byte("package main\nimport _ \"p\"\nfunc main() {}\n"), 0666) |
| if err != nil { |
| t.Fatalf("failed to write source file: %v", err) |
| } |
| err = os.WriteFile(pSrc, []byte("package p\n"), 0666) |
| if err != nil { |
| t.Fatalf("failed to write source file: %v", err) |
| } |
| cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-o", pObj, pSrc) |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("compile p.go failed: %v. output:\n%s", err, out) |
| } |
| cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-p=main", "-o", xObj, xSrc) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("compile x.go failed: %v. output:\n%s", err, out) |
| } |
| cmd = linkCmd(t, "-importcfg="+importcfgfile, "-o", exe, xObj) |
| out, err = cmd.CombinedOutput() |
| if err == nil { |
| t.Fatalf("link did not fail") |
| } |
| if !bytes.Contains(out, []byte("unlinkable object")) { |
| t.Errorf("did not see expected error message. out:\n%s", out) |
| } |
|
|
| |
| cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-p=p", "-o", pObj, pSrc) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("compile p.go failed: %v. output:\n%s", err, out) |
| } |
| cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-importcfg="+importcfgfile, "-o", xObj, xSrc) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("compile failed: %v. output:\n%s", err, out) |
| } |
|
|
| cmd = linkCmd(t, "-importcfg="+importcfgfile, "-o", exe, xObj) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("link failed: %v. output:\n%s", err, out) |
| } |
| } |
|
|
| func TestExtLinkCmdlineDeterminism(t *testing.T) { |
| |
| testenv.MustHaveGoBuild(t) |
| testenv.MustHaveCGO(t) |
| t.Parallel() |
|
|
| |
| testSrc := ` |
| package main |
| import "C" |
| //export F1 |
| func F1() {} |
| //export F2 |
| func F2() {} |
| //export F3 |
| func F3() {} |
| func main() {} |
| ` |
|
|
| tmpdir := t.TempDir() |
| src := filepath.Join(tmpdir, "x.go") |
| if err := os.WriteFile(src, []byte(testSrc), 0666); err != nil { |
| t.Fatal(err) |
| } |
| exe := filepath.Join(tmpdir, "x.exe") |
|
|
| |
| |
| linktmp := filepath.Join(tmpdir, "linktmp") |
| if err := os.Mkdir(linktmp, 0777); err != nil { |
| t.Fatal(err) |
| } |
|
|
| |
| |
| ldflags := "-ldflags=-v -linkmode=external -tmpdir=" + linktmp |
| var out0 []byte |
| for i := 0; i < 5; i++ { |
| cmd := goCmd(t, "build", ldflags, "-o", exe, src) |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("build failed: %v, output:\n%s", err, out) |
| } |
| if err := os.Remove(exe); err != nil { |
| t.Fatal(err) |
| } |
|
|
| |
| j := bytes.Index(out, []byte("\nhost link:")) |
| if j == -1 { |
| t.Fatalf("host link step not found, output:\n%s", out) |
| } |
| out = out[j+1:] |
| k := bytes.Index(out, []byte("\n")) |
| if k == -1 { |
| t.Fatalf("no newline after host link, output:\n%s", out) |
| } |
| out = out[:k] |
|
|
| |
| |
| fs := bytes.Fields(out) |
| for i, f := range fs { |
| if bytes.Equal(f, []byte(`"-o"`)) && i+1 < len(fs) { |
| fs[i+1] = []byte("a.out") |
| break |
| } |
| } |
| out = bytes.Join(fs, []byte{' '}) |
|
|
| if i == 0 { |
| out0 = out |
| continue |
| } |
| if !bytes.Equal(out0, out) { |
| t.Fatalf("output differ:\n%s\n==========\n%s", out0, out) |
| } |
| } |
| } |
|
|
| |
| |
| func TestResponseFile(t *testing.T) { |
| t.Parallel() |
|
|
| testenv.MustHaveGoBuild(t) |
|
|
| |
| |
| testenv.MustHaveCGO(t) |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "x.go") |
| if err := os.WriteFile(src, []byte(`package main; import "C"; func main() {}`), 0666); err != nil { |
| t.Fatal(err) |
| } |
|
|
| |
| |
|
|
| cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", "output", "x.go") |
| cmd.Dir = tmpdir |
|
|
| |
| var sb strings.Builder |
| sb.WriteString(`'-ldflags=all="-extldflags=`) |
| for i := 0; i < sys.ExecArgLengthLimit/len("-g"); i++ { |
| if i > 0 { |
| sb.WriteString(" ") |
| } |
| sb.WriteString("-g") |
| } |
| sb.WriteString(`"'`) |
| cmd = testenv.CleanCmdEnv(cmd) |
| cmd.Env = append(cmd.Env, "GOFLAGS="+sb.String()) |
|
|
| out, err := cmd.CombinedOutput() |
| if len(out) > 0 { |
| t.Logf("%s", out) |
| } |
| if err != nil { |
| t.Error(err) |
| } |
| } |
|
|
| func TestDynimportVar(t *testing.T) { |
| |
| |
| if runtime.GOOS != "darwin" { |
| t.Skip("skip on non-darwin platform") |
| } |
|
|
| testenv.MustHaveGoBuild(t) |
| testenv.MustHaveCGO(t) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
| exe := filepath.Join(tmpdir, "a.exe") |
| src := filepath.Join("testdata", "dynimportvar", "main.go") |
|
|
| for _, mode := range []string{"internal", "external"} { |
| cmd := goCmd(t, "build", "-ldflags=-linkmode="+mode, "-o", exe, src) |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("build (linkmode=%s) failed: %v\n%s", mode, err, out) |
| } |
| cmd = testenv.Command(t, exe) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("executable failed to run (%s): %v\n%s", mode, err, out) |
| } |
| } |
| } |
|
|
| const helloSrc = ` |
| package main |
| var X = 42 |
| var Y int |
| func main() { println("hello", X, Y) } |
| ` |
|
|
| func TestFlagS(t *testing.T) { |
| |
| testenv.MustHaveGoBuild(t) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
| exe := filepath.Join(tmpdir, "a.exe") |
| src := filepath.Join(tmpdir, "a.go") |
| err := os.WriteFile(src, []byte(helloSrc), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
|
|
| modes := []string{"auto"} |
| if testenv.HasCGO() { |
| modes = append(modes, "external") |
| } |
|
|
| |
| syms := []string{"main.main", "main.X", "main.Y"} |
|
|
| for _, mode := range modes { |
| cmd := goCmd(t, "build", "-ldflags=-s -linkmode="+mode, "-o", exe, src) |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("build (linkmode=%s) failed: %v\n%s", mode, err, out) |
| } |
| cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "nm", exe) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| if _, ok := errors.AsType[*exec.ExitError](err); !ok { |
| |
| |
| |
| t.Errorf("(mode=%s) go tool nm failed: %v\n%s", mode, err, out) |
| } |
| } |
| for _, s := range syms { |
| if bytes.Contains(out, []byte(s)) { |
| t.Errorf("(mode=%s): unexpected symbol %s", mode, s) |
| } |
| } |
| } |
| } |
|
|
| func TestRandLayout(t *testing.T) { |
| |
| |
| testenv.MustHaveGoBuild(t) |
|
|
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join(tmpdir, "hello.go") |
| err := os.WriteFile(src, []byte(trivialSrc), 0666) |
| if err != nil { |
| t.Fatal(err) |
| } |
|
|
| var syms [2]string |
| for i, seed := range []string{"123", "456"} { |
| exe := filepath.Join(tmpdir, "hello"+seed+".exe") |
| cmd := goCmd(t, "build", "-ldflags=-randlayout="+seed, "-o", exe, src) |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("seed=%v: build failed: %v\n%s", seed, err, out) |
| } |
| cmd = testenv.Command(t, exe) |
| err = cmd.Run() |
| if err != nil { |
| t.Fatalf("seed=%v: executable failed to run: %v\n%s", seed, err, out) |
| } |
| cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "nm", exe) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("seed=%v: fail to run \"go tool nm\": %v\n%s", seed, err, out) |
| } |
| syms[i] = string(out) |
| } |
| if syms[0] == syms[1] { |
| t.Errorf("randlayout with different seeds produced same layout:\n%s\n===\n\n%s", syms[0], syms[1]) |
| } |
| } |
|
|
| func TestCheckLinkname(t *testing.T) { |
| |
| testenv.MustHaveGoBuild(t) |
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| tests := []struct { |
| src string |
| ok bool |
| }{ |
| |
| {"ok.go", true}, |
| |
| {"push.go", true}, |
| |
| |
| {"textvar", true}, |
| |
| {"coro.go", false}, |
| {"coro_var.go", false}, |
| |
| {"coro_asm", false}, |
| |
| {"coro2.go", false}, |
| |
| {"builtin.go", false}, |
| {"addmoduledata.go", false}, |
| {"freegc.go", false}, |
| |
| {"fastrand.go", true}, |
| {"badlinkname.go", true}, |
| } |
| for _, test := range tests { |
| test := test |
| t.Run(test.src, func(t *testing.T) { |
| t.Parallel() |
| src := "./testdata/linkname/" + test.src |
| exe := filepath.Join(tmpdir, test.src+".exe") |
| cmd := goCmd(t, "build", "-o", exe, src) |
| out, err := cmd.CombinedOutput() |
| if test.ok && err != nil { |
| t.Errorf("build failed unexpectedly: %v:\n%s", err, out) |
| } |
| if !test.ok && err == nil { |
| t.Errorf("build succeeded unexpectedly: %v:\n%s", err, out) |
| } |
| }) |
| } |
| } |
|
|
| func TestLinknameBSS(t *testing.T) { |
| |
| |
| testenv.MustHaveGoBuild(t) |
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
|
|
| src := filepath.Join("testdata", "linkname", "sched.go") |
| exe := filepath.Join(tmpdir, "sched.exe") |
| cmd := goCmd(t, "build", "-o", exe, src) |
| out, err := cmd.CombinedOutput() |
| if err != nil { |
| t.Fatalf("build failed unexpectedly: %v:\n%s", err, out) |
| } |
|
|
| |
| f, err := objfile.Open(exe) |
| if err != nil { |
| t.Fatalf("fail to open executable: %v", err) |
| } |
| defer f.Close() |
| syms, err := f.Symbols() |
| if err != nil { |
| t.Fatalf("fail to get symbols: %v", err) |
| } |
| found := false |
| for _, s := range syms { |
| if s.Name == "runtime.sched" || s.Name == "_runtime.sched" { |
| found = true |
| if s.Size < 100 { |
| |
| |
| |
| t.Errorf("runtime.sched symbol size too small: want > 100, got %d", s.Size) |
| } |
| } |
| } |
| if !found { |
| t.Errorf("runtime.sched symbol not found") |
| } |
|
|
| |
| cmd = testenv.Command(t, exe) |
| out, err = cmd.CombinedOutput() |
| if err != nil { |
| t.Errorf("executable failed to run: %v\n%s", err, out) |
| } |
| } |
|
|
| |
| |
| func setValueFromBytes[T any](p *T, s []byte) { |
| copy(unsafe.Slice((*byte)(unsafe.Pointer(p)), unsafe.Sizeof(*p)), s) |
| } |
|
|
| |
| |
| |
| func TestFuncdataPlacement(t *testing.T) { |
| testenv.MustHaveGoBuild(t) |
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
| src := filepath.Join(tmpdir, "x.go") |
| if err := os.WriteFile(src, []byte(trivialSrc), 0o444); err != nil { |
| t.Fatal(err) |
| } |
|
|
| exe := filepath.Join(tmpdir, "x.exe") |
| cmd := goCmd(t, "build", "-o", exe, src) |
| if out, err := cmd.CombinedOutput(); err != nil { |
| t.Fatalf("build failed; %v, output:\n%s", err, out) |
| } |
|
|
| |
| |
| |
| |
| |
|
|
| ef, _ := elf.Open(exe) |
| mf, _ := macho.Open(exe) |
| pf, _ := pe.Open(exe) |
| xf, _ := xcoff.Open(exe) |
| |
| if ef == nil && mf == nil && pf == nil && xf == nil { |
| t.Skip("unrecognized executable file format") |
| } |
|
|
| const moddataSymName = "runtime.firstmoduledata" |
| const gofuncSymName = "go:func.*" |
| var ( |
| pclntab []byte |
| pclntabAddr uint64 |
| pclntabEnd uint64 |
| moddataAddr uint64 |
| moddataBytes []byte |
| gofuncAddr uint64 |
| imageBase uint64 |
| ) |
| switch { |
| case ef != nil: |
| defer ef.Close() |
|
|
| syms, err := ef.Symbols() |
| if err != nil { |
| t.Fatal(err) |
| } |
| for _, sym := range syms { |
| switch sym.Name { |
| case moddataSymName: |
| moddataAddr = sym.Value |
| case gofuncSymName: |
| gofuncAddr = sym.Value |
| } |
| } |
|
|
| for _, sec := range ef.Sections { |
| if sec.Name == ".gopclntab" { |
| data, err := sec.Data() |
| if err != nil { |
| t.Fatal(err) |
| } |
| pclntab = data |
| pclntabAddr = sec.Addr |
| pclntabEnd = sec.Addr + sec.Size |
| } |
| if sec.Flags&elf.SHF_ALLOC != 0 && moddataAddr >= sec.Addr && moddataAddr < sec.Addr+sec.Size { |
| data, err := sec.Data() |
| if err != nil { |
| t.Fatal(err) |
| } |
| moddataBytes = data[moddataAddr-sec.Addr:] |
| } |
| } |
|
|
| case mf != nil: |
| defer mf.Close() |
|
|
| for _, sym := range mf.Symtab.Syms { |
| switch sym.Name { |
| case moddataSymName: |
| moddataAddr = sym.Value |
| case gofuncSymName: |
| gofuncAddr = sym.Value |
| } |
| } |
|
|
| for _, sec := range mf.Sections { |
| if sec.Name == "__gopclntab" { |
| data, err := sec.Data() |
| if err != nil { |
| t.Fatal(err) |
| } |
| pclntab = data |
| pclntabAddr = sec.Addr |
| pclntabEnd = sec.Addr + sec.Size |
| } |
| if moddataAddr >= sec.Addr && moddataAddr < sec.Addr+sec.Size { |
| data, err := sec.Data() |
| if err != nil { |
| t.Fatal(err) |
| } |
| moddataBytes = data[moddataAddr-sec.Addr:] |
| } |
| } |
|
|
| case pf != nil: |
| defer pf.Close() |
|
|
| switch ohdr := pf.OptionalHeader.(type) { |
| case *pe.OptionalHeader32: |
| imageBase = uint64(ohdr.ImageBase) |
| case *pe.OptionalHeader64: |
| imageBase = ohdr.ImageBase |
| } |
|
|
| var moddataSym, gofuncSym, pclntabSym, epclntabSym *pe.Symbol |
| for _, sym := range pf.Symbols { |
| switch sym.Name { |
| case moddataSymName: |
| moddataSym = sym |
| case gofuncSymName: |
| gofuncSym = sym |
| case "runtime.pclntab": |
| pclntabSym = sym |
| case "runtime.epclntab": |
| epclntabSym = sym |
| } |
| } |
|
|
| if moddataSym == nil { |
| t.Fatalf("could not find symbol %s", moddataSymName) |
| } |
| if gofuncSym == nil { |
| t.Fatalf("could not find symbol %s", gofuncSymName) |
| } |
| if pclntabSym == nil { |
| t.Fatal("could not find symbol runtime.pclntab") |
| } |
| if epclntabSym == nil { |
| t.Fatal("could not find symbol runtime.epclntab") |
| } |
|
|
| sec := pf.Sections[moddataSym.SectionNumber-1] |
| data, err := sec.Data() |
| if err != nil { |
| t.Fatal(err) |
| } |
| moddataBytes = data[moddataSym.Value:] |
| moddataAddr = uint64(sec.VirtualAddress + moddataSym.Value) |
|
|
| sec = pf.Sections[gofuncSym.SectionNumber-1] |
| gofuncAddr = uint64(sec.VirtualAddress + gofuncSym.Value) |
|
|
| if pclntabSym.SectionNumber != epclntabSym.SectionNumber { |
| t.Fatalf("runtime.pclntab section %d != runtime.epclntab section %d", pclntabSym.SectionNumber, epclntabSym.SectionNumber) |
| } |
| sec = pf.Sections[pclntabSym.SectionNumber-1] |
| data, err = sec.Data() |
| if err != nil { |
| t.Fatal(err) |
| } |
| pclntab = data[pclntabSym.Value:epclntabSym.Value] |
| pclntabAddr = uint64(sec.VirtualAddress + pclntabSym.Value) |
| pclntabEnd = uint64(sec.VirtualAddress + epclntabSym.Value) |
|
|
| case xf != nil: |
| defer xf.Close() |
|
|
| var moddataSym, gofuncSym, pclntabSym, epclntabSym *xcoff.Symbol |
| for _, sym := range xf.Symbols { |
| switch sym.Name { |
| case moddataSymName: |
| moddataSym = sym |
| case gofuncSymName: |
| gofuncSym = sym |
| case "runtime.pclntab": |
| pclntabSym = sym |
| case "runtime.epclntab": |
| epclntabSym = sym |
| } |
| } |
|
|
| if moddataSym == nil { |
| t.Fatalf("could not find symbol %s", moddataSymName) |
| } |
| if gofuncSym == nil { |
| t.Fatalf("could not find symbol %s", gofuncSymName) |
| } |
| if pclntabSym == nil { |
| t.Fatal("could not find symbol runtime.pclntab") |
| } |
| if epclntabSym == nil { |
| t.Fatal("could not find symbol runtime.epclntab") |
| } |
|
|
| sec := xf.Sections[moddataSym.SectionNumber-1] |
| data, err := sec.Data() |
| if err != nil { |
| t.Fatal(err) |
| } |
| moddataBytes = data[moddataSym.Value:] |
| moddataAddr = uint64(sec.VirtualAddress + moddataSym.Value) |
|
|
| sec = xf.Sections[gofuncSym.SectionNumber-1] |
| gofuncAddr = uint64(sec.VirtualAddress + gofuncSym.Value) |
|
|
| if pclntabSym.SectionNumber != epclntabSym.SectionNumber { |
| t.Fatalf("runtime.pclntab section %d != runtime.epclntab section %d", pclntabSym.SectionNumber, epclntabSym.SectionNumber) |
| } |
| sec = xf.Sections[pclntabSym.SectionNumber-1] |
| data, err = sec.Data() |
| if err != nil { |
| t.Fatal(err) |
| } |
| pclntab = data[pclntabSym.Value:epclntabSym.Value] |
| pclntabAddr = uint64(sec.VirtualAddress + pclntabSym.Value) |
| pclntabEnd = uint64(sec.VirtualAddress + epclntabSym.Value) |
|
|
| default: |
| panic("can't happen") |
| } |
|
|
| if len(pclntab) == 0 { |
| t.Fatal("could not find pclntab section") |
| } |
| if moddataAddr == 0 { |
| t.Fatalf("could not find %s symbol", moddataSymName) |
| } |
| if gofuncAddr == 0 { |
| t.Fatalf("could not find %s symbol", gofuncSymName) |
| } |
| if gofuncAddr < pclntabAddr || gofuncAddr >= pclntabEnd { |
| t.Fatalf("%s out of range: value %#x not between %#x and %#x", gofuncSymName, gofuncAddr, pclntabAddr, pclntabEnd) |
| } |
| if len(moddataBytes) == 0 { |
| t.Fatal("could not find module data") |
| } |
|
|
| |
| type moddataSlice struct { |
| addr uintptr |
| len int |
| cap int |
| } |
|
|
| |
| |
| |
| type moddataType struct { |
| pcHeader uintptr |
| funcnametab moddataSlice |
| cutab moddataSlice |
| filetab moddataSlice |
| pctab moddataSlice |
| pclntable moddataSlice |
| ftab moddataSlice |
| findfunctab uintptr |
| minpc, maxpc uintptr |
|
|
| text, etext uintptr |
| noptrdata, enoptrdata uintptr |
| data, edata uintptr |
| bss, ebss uintptr |
| noptrbss, enoptrbss uintptr |
| covctrs, ecovctrs uintptr |
| end, gcdata, gcbss uintptr |
| types, etypes uintptr |
| rodata uintptr |
| gofunc uintptr |
| } |
|
|
| |
| |
| |
| |
| var moddata moddataType |
| setValueFromBytes(&moddata, moddataBytes) |
|
|
| ftabAddr := uint64(moddata.ftab.addr) - imageBase |
| if ftabAddr < pclntabAddr || ftabAddr >= pclntabEnd { |
| t.Fatalf("ftab address %#x not between %#x and %#x", ftabAddr, pclntabAddr, pclntabEnd) |
| } |
|
|
| |
| type functab struct { |
| entryoff uint32 |
| funcoff uint32 |
| } |
| |
| |
| ftabLen := moddata.ftab.len - 1 |
| ftab := make([]functab, ftabLen) |
| copy(ftab, unsafe.Slice((*functab)(unsafe.Pointer(&pclntab[ftabAddr-pclntabAddr])), ftabLen)) |
|
|
| ftabBase := uint64(moddata.pclntable.addr) - imageBase |
|
|
| |
| type funcEntry struct { |
| entryOff uint32 |
| nameOff int32 |
|
|
| args int32 |
| deferreturn uint32 |
|
|
| pcsp uint32 |
| pcfile uint32 |
| pcln uint32 |
| npcdata uint32 |
| cuOffset uint32 |
| startLine int32 |
| funcID abi.FuncID |
| flag abi.FuncFlag |
| _ [1]byte |
| nfuncdata uint8 |
| } |
|
|
| for i, ftabEntry := range ftab { |
| funcAddr := ftabBase + uint64(ftabEntry.funcoff) |
| if funcAddr < pclntabAddr || funcAddr >= pclntabEnd { |
| t.Errorf("ftab entry %d address %#x not between %#x and %#x", i, funcAddr, pclntabAddr, pclntabEnd) |
| continue |
| } |
|
|
| var fe funcEntry |
| setValueFromBytes(&fe, pclntab[funcAddr-pclntabAddr:]) |
|
|
| funcdataVals := funcAddr + uint64(unsafe.Sizeof(fe)) + uint64(fe.npcdata*4) |
| for j := range fe.nfuncdata { |
| var funcdataVal uint32 |
| setValueFromBytes(&funcdataVal, pclntab[funcdataVals+uint64(j)*4-pclntabAddr:]) |
| if funcdataVal == ^uint32(0) { |
| continue |
| } |
| funcdataAddr := gofuncAddr + uint64(funcdataVal) |
| if funcdataAddr < pclntabAddr || funcdataAddr >= pclntabEnd { |
| t.Errorf("ftab entry %d funcdata %d address %#x not between %#x and %#x", i, j, funcdataAddr, pclntabAddr, pclntabEnd) |
| } |
| } |
| } |
|
|
| if uint64(moddata.findfunctab)-imageBase < pclntabAddr || uint64(moddata.findfunctab)-imageBase >= pclntabEnd { |
| t.Errorf("findfunctab address %#x not between %#x and %#x", moddata.findfunctab, pclntabAddr, pclntabEnd) |
| } |
| } |
|
|
| |
| func TestModuledataPlacement(t *testing.T) { |
| testenv.MustHaveGoBuild(t) |
| t.Parallel() |
|
|
| tmpdir := t.TempDir() |
| src := filepath.Join(tmpdir, "x.go") |
| if err := os.WriteFile(src, []byte(trivialSrc), 0o444); err != nil { |
| t.Fatal(err) |
| } |
|
|
| exe := filepath.Join(tmpdir, "x.exe") |
| cmd := goCmd(t, "build", "-o", exe, src) |
| if out, err := cmd.CombinedOutput(); err != nil { |
| t.Fatalf("build failed; %v, output:\n%s", err, out) |
| } |
|
|
| ef, _ := elf.Open(exe) |
| mf, _ := macho.Open(exe) |
| pf, _ := pe.Open(exe) |
| xf, _ := xcoff.Open(exe) |
| |
| if ef == nil && mf == nil && pf == nil && xf == nil { |
| t.Skip("unrecognized executable file format") |
| } |
|
|
| const moddataSymName = "runtime.firstmoduledata" |
| switch { |
| case ef != nil: |
| defer ef.Close() |
|
|
| syms, err := ef.Symbols() |
| if err != nil { |
| t.Fatal(err) |
| } |
| for _, sym := range syms { |
| if sym.Name == moddataSymName { |
| sec := ef.Sections[sym.Section] |
| if sec.Name != ".go.module" { |
| t.Errorf("moduledata in section %s, not .go.module", sec.Name) |
| } |
| if sym.Value != sec.Addr { |
| t.Errorf("moduledata address %#x != section start address %#x", sym.Value, sec.Addr) |
| } |
| break |
| } |
| } |
|
|
| case mf != nil: |
| defer mf.Close() |
|
|
| for _, sym := range mf.Symtab.Syms { |
| if sym.Name == moddataSymName { |
| if sym.Sect == 0 { |
| t.Error("moduledata not in a section") |
| } else { |
| sec := mf.Sections[sym.Sect-1] |
| if sec.Name != "__go_module" { |
| t.Errorf("moduledata in section %s, not __go.module", sec.Name) |
| } |
| if sym.Value != sec.Addr { |
| t.Errorf("moduledata address %#x != section start address %#x", sym.Value, sec.Addr) |
| } |
| } |
| break |
| } |
| } |
|
|
| case pf != nil, xf != nil: |
| if pf != nil { |
| defer pf.Close() |
| } |
| if xf != nil { |
| defer xf.Close() |
| } |
|
|
| |
| |
| |
| } |
| } |
|
|