1 // Copyright 2015 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.
29 var gopathInstallDir, gorootInstallDir string
31 // This is the smallest set of packages we can link into a shared
32 // library (runtime/cgo is built implicitly).
33 var minpkgs = []string{"runtime", "sync/atomic"}
34 var soname = "libruntime,sync-atomic.so"
36 var testX = flag.Bool("testx", false, "if true, pass -x to 'go' subcommands invoked by the test")
37 var testWork = flag.Bool("testwork", false, "if true, log and do not delete the temporary working directory")
39 // run runs a command and calls t.Errorf if it fails.
40 func run(t *testing.T, msg string, args ...string) {
41 c := exec.Command(args[0], args[1:]...)
42 if output, err := c.CombinedOutput(); err != nil {
43 t.Errorf("executing %s (%s) failed %s:\n%s", strings.Join(args, " "), msg, err, output)
47 // goCmd invokes the go tool with the installsuffix set up by TestMain. It calls
48 // t.Fatalf if the command fails.
49 func goCmd(t *testing.T, args ...string) string {
50 newargs := []string{args[0]}
52 newargs = append(newargs, "-x")
54 newargs = append(newargs, args[1:]...)
55 c := exec.Command("go", newargs...)
56 stderr := new(strings.Builder)
59 if testing.Verbose() && t == nil {
60 fmt.Fprintf(os.Stderr, "+ go %s\n", strings.Join(args, " "))
63 output, err := c.Output()
68 t.Fatalf("executing %s failed %v:\n%s", strings.Join(c.Args, " "), err, stderr)
70 // Panic instead of using log.Fatalf so that deferred cleanup may run in testMain.
71 log.Panicf("executing %s failed %v:\n%s", strings.Join(c.Args, " "), err, stderr)
74 if testing.Verbose() && t != nil {
75 t.Logf("go %s", strings.Join(args, " "))
80 return string(bytes.TrimSpace(output))
83 // TestMain calls testMain so that the latter can use defer (TestMain exits with os.Exit).
84 func testMain(m *testing.M) (int, error) {
85 workDir, err := ioutil.TempDir("", "shared_test")
89 if *testWork || testing.Verbose() {
90 fmt.Printf("+ mkdir -p %s\n", workDir)
93 defer os.RemoveAll(workDir)
96 // Some tests need to edit the source in GOPATH, so copy this directory to a
97 // temporary directory and chdir to that.
98 gopath := filepath.Join(workDir, "gopath")
99 modRoot, err := cloneTestdataModule(gopath)
103 if testing.Verbose() {
104 fmt.Printf("+ export GOPATH=%s\n", gopath)
105 fmt.Printf("+ cd %s\n", modRoot)
107 os.Setenv("GOPATH", gopath)
109 os.Setenv("PWD", modRoot)
111 // The test also needs to install libraries into GOROOT/pkg, so copy the
112 // subset of GOROOT that we need.
114 // TODO(golang.org/issue/28553): Rework -buildmode=shared so that it does not
115 // need to write to GOROOT.
116 goroot := filepath.Join(workDir, "goroot")
117 if err := cloneGOROOTDeps(goroot); err != nil {
120 if testing.Verbose() {
121 fmt.Fprintf(os.Stderr, "+ export GOROOT=%s\n", goroot)
123 os.Setenv("GOROOT", goroot)
125 myContext := build.Default
126 myContext.GOROOT = goroot
127 myContext.GOPATH = gopath
128 runtimeP, err := myContext.Import("runtime", ".", build.ImportComment)
130 return 0, fmt.Errorf("import failed: %v", err)
132 gorootInstallDir = runtimeP.PkgTargetRoot + "_dynlink"
134 // All tests depend on runtime being built into a shared library. Because
135 // that takes a few seconds, do it here and have all tests use the version
137 goCmd(nil, append([]string{"install", "-buildmode=shared"}, minpkgs...)...)
139 myContext.InstallSuffix = "_dynlink"
140 depP, err := myContext.Import("./depBase", ".", build.ImportComment)
142 return 0, fmt.Errorf("import failed: %v", err)
144 if depP.PkgTargetRoot == "" {
145 gopathInstallDir = filepath.Dir(goCmd(nil, "list", "-buildmode=shared", "-f", "{{.Target}}", "./depBase"))
147 gopathInstallDir = filepath.Join(depP.PkgTargetRoot, "testshared")
152 func TestMain(m *testing.M) {
153 log.SetFlags(log.Lshortfile)
156 // Some of the tests install binaries into a custom GOPATH.
157 // That won't work if GOBIN is set.
160 exitCode, err := testMain(m)
167 // cloneTestdataModule clones the packages from src/testshared into gopath.
168 // It returns the directory within gopath at which the module root is located.
169 func cloneTestdataModule(gopath string) (string, error) {
170 modRoot := filepath.Join(gopath, "src", "testshared")
171 if err := overlayDir(modRoot, "testdata"); err != nil {
174 if err := ioutil.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module testshared\n"), 0644); err != nil {
180 // cloneGOROOTDeps copies (or symlinks) the portions of GOROOT/src and
181 // GOROOT/pkg relevant to this test into the given directory.
182 // It must be run from within the testdata module.
183 func cloneGOROOTDeps(goroot string) error {
184 oldGOROOT := strings.TrimSpace(goCmd(nil, "env", "GOROOT"))
186 return fmt.Errorf("go env GOROOT returned an empty string")
189 // Before we clone GOROOT, figure out which packages we need to copy over.
190 listArgs := []string{
193 "-f", "{{if and .Standard (not .ForTest)}}{{.ImportPath}}{{end}}",
195 stdDeps := goCmd(nil, append(listArgs, minpkgs...)...)
196 testdataDeps := goCmd(nil, append(listArgs, "-test", "./...")...)
198 pkgs := append(strings.Split(strings.TrimSpace(stdDeps), "\n"),
199 strings.Split(strings.TrimSpace(testdataDeps), "\n")...)
201 var pkgRoots []string
202 for _, pkg := range pkgs {
204 for _, prev := range pkgRoots {
205 if strings.HasPrefix(pkg, prev) {
206 // We will copy in the source for pkg when we copy in prev.
212 pkgRoots = append(pkgRoots, pkg)
216 gorootDirs := []string{
220 for _, pkg := range pkgRoots {
221 gorootDirs = append(gorootDirs, filepath.Join("src", pkg))
224 for _, dir := range gorootDirs {
225 if testing.Verbose() {
226 fmt.Fprintf(os.Stderr, "+ cp -r %s %s\n", filepath.Join(goroot, dir), filepath.Join(oldGOROOT, dir))
228 if err := overlayDir(filepath.Join(goroot, dir), filepath.Join(oldGOROOT, dir)); err != nil {
236 // The shared library was built at the expected location.
237 func TestSOBuilt(t *testing.T) {
238 _, err := os.Stat(filepath.Join(gorootInstallDir, soname))
244 func hasDynTag(f *elf.File, tag elf.DynTag) bool {
245 ds := f.SectionByType(elf.SHT_DYNAMIC)
257 t = elf.DynTag(f.ByteOrder.Uint32(d[0:4]))
260 t = elf.DynTag(f.ByteOrder.Uint64(d[0:8]))
270 // The shared library does not have relocations against the text segment.
271 func TestNoTextrel(t *testing.T) {
272 sopath := filepath.Join(gorootInstallDir, soname)
273 f, err := elf.Open(sopath)
275 t.Fatal("elf.Open failed: ", err)
278 if hasDynTag(f, elf.DT_TEXTREL) {
279 t.Errorf("%s has DT_TEXTREL set", soname)
283 // The shared library does not contain symbols called ".dup"
284 // (See golang.org/issue/14841.)
285 func TestNoDupSymbols(t *testing.T) {
286 sopath := filepath.Join(gorootInstallDir, soname)
287 f, err := elf.Open(sopath)
289 t.Fatal("elf.Open failed: ", err)
292 syms, err := f.Symbols()
294 t.Errorf("error reading symbols %v", err)
297 for _, s := range syms {
298 if s.Name == ".dup" {
299 t.Fatalf("%s contains symbol called .dup", sopath)
304 // The install command should have created a "shlibname" file for the
305 // listed packages (and runtime/cgo, and math on arm) indicating the
306 // name of the shared library containing it.
307 func TestShlibnameFiles(t *testing.T) {
308 pkgs := append([]string{}, minpkgs...)
309 pkgs = append(pkgs, "runtime/cgo")
310 if runtime.GOARCH == "arm" {
311 pkgs = append(pkgs, "math")
313 for _, pkg := range pkgs {
314 shlibnamefile := filepath.Join(gorootInstallDir, pkg+".shlibname")
315 contentsb, err := ioutil.ReadFile(shlibnamefile)
317 t.Errorf("error reading shlibnamefile for %s: %v", pkg, err)
320 contents := strings.TrimSpace(string(contentsb))
321 if contents != soname {
322 t.Errorf("shlibnamefile for %s has wrong contents: %q", pkg, contents)
327 // Is a given offset into the file contained in a loaded segment?
328 func isOffsetLoaded(f *elf.File, offset uint64) bool {
329 for _, prog := range f.Progs {
330 if prog.Type == elf.PT_LOAD {
331 if prog.Off <= offset && offset < prog.Off+prog.Filesz {
339 func rnd(v int32, r int32) int32 {
352 func readwithpad(r io.Reader, sz int32) ([]byte, error) {
353 data := make([]byte, rnd(sz, 4))
354 _, err := io.ReadFull(r, data)
369 // Read all notes from f. As ELF section names are not supposed to be special, one
370 // looks for a particular note by scanning all SHT_NOTE sections looking for a note
371 // with a particular "name" and "tag".
372 func readNotes(f *elf.File) ([]*note, error) {
374 for _, sect := range f.Sections {
375 if sect.Type != elf.SHT_NOTE {
380 var namesize, descsize, tag int32
381 err := binary.Read(r, f.ByteOrder, &namesize)
386 return nil, fmt.Errorf("read namesize failed: %v", err)
388 err = binary.Read(r, f.ByteOrder, &descsize)
390 return nil, fmt.Errorf("read descsize failed: %v", err)
392 err = binary.Read(r, f.ByteOrder, &tag)
394 return nil, fmt.Errorf("read type failed: %v", err)
396 name, err := readwithpad(r, namesize)
398 return nil, fmt.Errorf("read name failed: %v", err)
400 desc, err := readwithpad(r, descsize)
402 return nil, fmt.Errorf("read desc failed: %v", err)
404 notes = append(notes, ¬e{name: string(name), tag: tag, desc: string(desc), section: sect})
410 func dynStrings(t *testing.T, path string, flag elf.DynTag) []string {
412 f, err := elf.Open(path)
414 t.Fatalf("elf.Open(%q) failed: %v", path, err)
417 dynstrings, err := f.DynString(flag)
419 t.Fatalf("DynString(%s) failed on %s: %v", flag, path, err)
424 func AssertIsLinkedToRegexp(t *testing.T, path string, re *regexp.Regexp) {
426 for _, dynstring := range dynStrings(t, path, elf.DT_NEEDED) {
427 if re.MatchString(dynstring) {
431 t.Errorf("%s is not linked to anything matching %v", path, re)
434 func AssertIsLinkedTo(t *testing.T, path, lib string) {
436 AssertIsLinkedToRegexp(t, path, regexp.MustCompile(regexp.QuoteMeta(lib)))
439 func AssertHasRPath(t *testing.T, path, dir string) {
441 for _, tag := range []elf.DynTag{elf.DT_RPATH, elf.DT_RUNPATH} {
442 for _, dynstring := range dynStrings(t, path, tag) {
443 for _, rpath := range strings.Split(dynstring, ":") {
444 if filepath.Clean(rpath) == filepath.Clean(dir) {
450 t.Errorf("%s does not have rpath %s", path, dir)
453 // Build a trivial program that links against the shared runtime and check it runs.
454 func TestTrivialExecutable(t *testing.T) {
455 goCmd(t, "install", "-linkshared", "./trivial")
456 run(t, "trivial executable", "../../bin/trivial")
457 AssertIsLinkedTo(t, "../../bin/trivial", soname)
458 AssertHasRPath(t, "../../bin/trivial", gorootInstallDir)
461 // Build a trivial program in PIE mode that links against the shared runtime and check it runs.
462 func TestTrivialExecutablePIE(t *testing.T) {
463 goCmd(t, "build", "-buildmode=pie", "-o", "trivial.pie", "-linkshared", "./trivial")
464 run(t, "trivial executable", "./trivial.pie")
465 AssertIsLinkedTo(t, "./trivial.pie", soname)
466 AssertHasRPath(t, "./trivial.pie", gorootInstallDir)
469 // Build a division test program and check it runs.
470 func TestDivisionExecutable(t *testing.T) {
471 goCmd(t, "install", "-linkshared", "./division")
472 run(t, "division executable", "../../bin/division")
475 // Build an executable that uses cgo linked against the shared runtime and check it
477 func TestCgoExecutable(t *testing.T) {
478 goCmd(t, "install", "-linkshared", "./execgo")
479 run(t, "cgo executable", "../../bin/execgo")
482 func checkPIE(t *testing.T, name string) {
483 f, err := elf.Open(name)
485 t.Fatal("elf.Open failed: ", err)
488 if f.Type != elf.ET_DYN {
489 t.Errorf("%s has type %v, want ET_DYN", name, f.Type)
491 if hasDynTag(f, elf.DT_TEXTREL) {
492 t.Errorf("%s has DT_TEXTREL set", name)
496 func TestTrivialPIE(t *testing.T) {
497 name := "trivial_pie"
498 goCmd(t, "build", "-buildmode=pie", "-o="+name, "./trivial")
499 defer os.Remove(name)
500 run(t, name, "./"+name)
504 func TestCgoPIE(t *testing.T) {
506 goCmd(t, "build", "-buildmode=pie", "-o="+name, "./execgo")
507 defer os.Remove(name)
508 run(t, name, "./"+name)
512 // Build a GOPATH package into a shared library that links against the goroot runtime
513 // and an executable that links against both.
514 func TestGopathShlib(t *testing.T) {
515 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
516 shlib := goCmd(t, "list", "-f", "{{.Shlib}}", "-buildmode=shared", "-linkshared", "./depBase")
517 AssertIsLinkedTo(t, shlib, soname)
518 goCmd(t, "install", "-linkshared", "./exe")
519 AssertIsLinkedTo(t, "../../bin/exe", soname)
520 AssertIsLinkedTo(t, "../../bin/exe", filepath.Base(shlib))
521 AssertHasRPath(t, "../../bin/exe", gorootInstallDir)
522 AssertHasRPath(t, "../../bin/exe", filepath.Dir(gopathInstallDir))
523 // And check it runs.
524 run(t, "executable linked to GOPATH library", "../../bin/exe")
527 // The shared library contains a note listing the packages it contains in a section
528 // that is not mapped into memory.
529 func testPkgListNote(t *testing.T, f *elf.File, note *note) {
530 if note.section.Flags != 0 {
531 t.Errorf("package list section has flags %v, want 0", note.section.Flags)
533 if isOffsetLoaded(f, note.section.Offset) {
534 t.Errorf("package list section contained in PT_LOAD segment")
536 if note.desc != "testshared/depBase\n" {
537 t.Errorf("incorrect package list %q, want %q", note.desc, "testshared/depBase\n")
541 // The shared library contains a note containing the ABI hash that is mapped into
542 // memory and there is a local symbol called go.link.abihashbytes that points 16
544 func testABIHashNote(t *testing.T, f *elf.File, note *note) {
545 if note.section.Flags != elf.SHF_ALLOC {
546 t.Errorf("abi hash section has flags %v, want SHF_ALLOC", note.section.Flags)
548 if !isOffsetLoaded(f, note.section.Offset) {
549 t.Errorf("abihash section not contained in PT_LOAD segment")
551 var hashbytes elf.Symbol
552 symbols, err := f.Symbols()
554 t.Errorf("error reading symbols %v", err)
557 for _, sym := range symbols {
558 if sym.Name == "go.link.abihashbytes" {
562 if hashbytes.Name == "" {
563 t.Errorf("no symbol called go.link.abihashbytes")
566 if elf.ST_BIND(hashbytes.Info) != elf.STB_LOCAL {
567 t.Errorf("%s has incorrect binding %v, want STB_LOCAL", hashbytes.Name, elf.ST_BIND(hashbytes.Info))
569 if f.Sections[hashbytes.Section] != note.section {
570 t.Errorf("%s has incorrect section %v, want %s", hashbytes.Name, f.Sections[hashbytes.Section].Name, note.section.Name)
572 if hashbytes.Value-note.section.Addr != 16 {
573 t.Errorf("%s has incorrect offset into section %d, want 16", hashbytes.Name, hashbytes.Value-note.section.Addr)
577 // A Go shared library contains a note indicating which other Go shared libraries it
578 // was linked against in an unmapped section.
579 func testDepsNote(t *testing.T, f *elf.File, note *note) {
580 if note.section.Flags != 0 {
581 t.Errorf("package list section has flags %v, want 0", note.section.Flags)
583 if isOffsetLoaded(f, note.section.Offset) {
584 t.Errorf("package list section contained in PT_LOAD segment")
586 // libdepBase.so just links against the lib containing the runtime.
587 if note.desc != soname {
588 t.Errorf("incorrect dependency list %q, want %q", note.desc, soname)
592 // The shared library contains notes with defined contents; see above.
593 func TestNotes(t *testing.T) {
594 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
595 shlib := goCmd(t, "list", "-f", "{{.Shlib}}", "-buildmode=shared", "-linkshared", "./depBase")
596 f, err := elf.Open(shlib)
601 notes, err := readNotes(f)
605 pkgListNoteFound := false
606 abiHashNoteFound := false
607 depsNoteFound := false
608 for _, note := range notes {
609 if note.name != "Go\x00\x00" {
613 case 1: // ELF_NOTE_GOPKGLIST_TAG
614 if pkgListNoteFound {
615 t.Error("multiple package list notes")
617 testPkgListNote(t, f, note)
618 pkgListNoteFound = true
619 case 2: // ELF_NOTE_GOABIHASH_TAG
620 if abiHashNoteFound {
621 t.Error("multiple abi hash notes")
623 testABIHashNote(t, f, note)
624 abiHashNoteFound = true
625 case 3: // ELF_NOTE_GODEPS_TAG
627 t.Error("multiple dependency list notes")
629 testDepsNote(t, f, note)
633 if !pkgListNoteFound {
634 t.Error("package list note not found")
636 if !abiHashNoteFound {
637 t.Error("abi hash note not found")
640 t.Error("deps note not found")
644 // Build a GOPATH package (depBase) into a shared library that links against the goroot
645 // runtime, another package (dep2) that links against the first, and an
646 // executable that links against dep2.
647 func TestTwoGopathShlibs(t *testing.T) {
648 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
649 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./dep2")
650 goCmd(t, "install", "-linkshared", "./exe2")
651 run(t, "executable linked to GOPATH library", "../../bin/exe2")
654 func TestThreeGopathShlibs(t *testing.T) {
655 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
656 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./dep2")
657 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./dep3")
658 goCmd(t, "install", "-linkshared", "./exe3")
659 run(t, "executable linked to GOPATH library", "../../bin/exe3")
662 // If gccgo is not available or not new enough, call t.Skip.
663 func requireGccgo(t *testing.T) {
666 gccgoName := os.Getenv("GCCGO")
670 gccgoPath, err := exec.LookPath(gccgoName)
672 t.Skip("gccgo not found")
674 cmd := exec.Command(gccgoPath, "-dumpversion")
675 output, err := cmd.CombinedOutput()
677 t.Fatalf("%s -dumpversion failed: %v\n%s", gccgoPath, err, output)
679 if string(output) < "5" {
680 t.Skipf("gccgo too old (%s)", strings.TrimSpace(string(output)))
683 gomod, err := exec.Command("go", "env", "GOMOD").Output()
685 t.Fatalf("go env GOMOD: %v", err)
687 if len(bytes.TrimSpace(gomod)) > 0 {
688 t.Skipf("gccgo not supported in module mode; see golang.org/issue/30344")
692 // Build a GOPATH package into a shared library with gccgo and an executable that
694 func TestGoPathShlibGccgo(t *testing.T) {
697 libgoRE := regexp.MustCompile("libgo.so.[0-9]+")
699 goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./depBase")
701 // Run 'go list' after 'go install': with gccgo, we apparently don't know the
702 // shlib location until after we've installed it.
703 shlib := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./depBase")
705 AssertIsLinkedToRegexp(t, shlib, libgoRE)
706 goCmd(t, "install", "-compiler=gccgo", "-linkshared", "./exe")
707 AssertIsLinkedToRegexp(t, "../../bin/exe", libgoRE)
708 AssertIsLinkedTo(t, "../../bin/exe", filepath.Base(shlib))
709 AssertHasRPath(t, "../../bin/exe", filepath.Dir(shlib))
710 // And check it runs.
711 run(t, "gccgo-built", "../../bin/exe")
714 // The gccgo version of TestTwoGopathShlibs: build a GOPATH package into a shared
715 // library with gccgo, another GOPATH package that depends on the first and an
716 // executable that links the second library.
717 func TestTwoGopathShlibsGccgo(t *testing.T) {
720 libgoRE := regexp.MustCompile("libgo.so.[0-9]+")
722 goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./depBase")
723 goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./dep2")
724 goCmd(t, "install", "-compiler=gccgo", "-linkshared", "./exe2")
726 // Run 'go list' after 'go install': with gccgo, we apparently don't know the
727 // shlib location until after we've installed it.
728 dep2 := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./dep2")
729 depBase := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./depBase")
731 AssertIsLinkedToRegexp(t, depBase, libgoRE)
732 AssertIsLinkedToRegexp(t, dep2, libgoRE)
733 AssertIsLinkedTo(t, dep2, filepath.Base(depBase))
734 AssertIsLinkedToRegexp(t, "../../bin/exe2", libgoRE)
735 AssertIsLinkedTo(t, "../../bin/exe2", filepath.Base(dep2))
736 AssertIsLinkedTo(t, "../../bin/exe2", filepath.Base(depBase))
738 // And check it runs.
739 run(t, "gccgo-built", "../../bin/exe2")
742 // Testing rebuilding of shared libraries when they are stale is a bit more
743 // complicated that it seems like it should be. First, we make everything "old": but
744 // only a few seconds old, or it might be older than gc (or the runtime source) and
745 // everything will get rebuilt. Then define a timestamp slightly newer than this
746 // time, which is what we set the mtime to of a file to cause it to be seen as new,
747 // and finally another slightly even newer one that we can compare files against to
748 // see if they have been rebuilt.
749 var oldTime = time.Now().Add(-9 * time.Second)
750 var nearlyNew = time.Now().Add(-6 * time.Second)
751 var stampTime = time.Now().Add(-3 * time.Second)
753 // resetFileStamps makes "everything" (bin, src, pkg from GOPATH and the
754 // test-specific parts of GOROOT) appear old.
755 func resetFileStamps() {
756 chtime := func(path string, info os.FileInfo, err error) error {
757 return os.Chtimes(path, oldTime, oldTime)
759 reset := func(path string) {
760 if err := filepath.Walk(path, chtime); err != nil {
761 log.Panicf("resetFileStamps failed: %v", err)
768 reset(gorootInstallDir)
771 // touch changes path and returns a function that changes it back.
772 // It also sets the time of the file, so that we can see if it is rewritten.
773 func touch(t *testing.T, path string) (cleanup func()) {
775 data, err := ioutil.ReadFile(path)
779 old := make([]byte, len(data))
781 if bytes.HasPrefix(data, []byte("!<arch>\n")) {
782 // Change last digit of build ID.
783 // (Content ID in the new content-based build IDs.)
784 const marker = `build id "`
785 i := bytes.Index(data, []byte(marker))
787 t.Fatal("cannot find build id in archive")
789 j := bytes.IndexByte(data[i+len(marker):], '"')
791 t.Fatal("cannot find build id in archive")
793 i += len(marker) + j - 1
800 // assume it's a text file
801 data = append(data, '\n')
804 // If the file is still a symlink from an overlay, delete it so that we will
805 // replace it with a regular file instead of overwriting the symlinked one.
806 fi, err := os.Lstat(path)
807 if err == nil && !fi.Mode().IsRegular() {
808 fi, err = os.Stat(path)
809 if err := os.Remove(path); err != nil {
817 // If we're replacing a symlink to a read-only file, make the new file
819 perm := fi.Mode().Perm() | 0200
821 if err := ioutil.WriteFile(path, data, perm); err != nil {
824 if err := os.Chtimes(path, nearlyNew, nearlyNew); err != nil {
828 if err := ioutil.WriteFile(path, old, perm); err != nil {
834 // isNew returns if the path is newer than the time stamp used by touch.
835 func isNew(t *testing.T, path string) bool {
837 fi, err := os.Stat(path)
841 return fi.ModTime().After(stampTime)
844 // Fail unless path has been rebuilt (i.e. is newer than the time stamp used by
846 func AssertRebuilt(t *testing.T, msg, path string) {
849 t.Errorf("%s was not rebuilt (%s)", msg, path)
853 // Fail if path has been rebuilt (i.e. is newer than the time stamp used by isNew)
854 func AssertNotRebuilt(t *testing.T, msg, path string) {
857 t.Errorf("%s was rebuilt (%s)", msg, path)
861 func TestRebuilding(t *testing.T) {
862 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
863 goCmd(t, "install", "-linkshared", "./exe")
864 info := strings.Fields(goCmd(t, "list", "-buildmode=shared", "-linkshared", "-f", "{{.Target}} {{.Shlib}}", "./depBase"))
866 t.Fatalf("go list failed to report Target and/or Shlib")
871 // If the source is newer than both the .a file and the .so, both are rebuilt.
872 t.Run("newsource", func(t *testing.T) {
874 cleanup := touch(t, "./depBase/dep.go")
877 goCmd(t, "install", "-linkshared", "./exe")
879 goCmd(t, "install", "-linkshared", "./exe")
880 AssertRebuilt(t, "new source", target)
881 AssertRebuilt(t, "new source", shlib)
884 // If the .a file is newer than the .so, the .so is rebuilt (but not the .a)
885 t.Run("newarchive", func(t *testing.T) {
887 AssertNotRebuilt(t, "new .a file before build", target)
888 goCmd(t, "list", "-linkshared", "-f={{.ImportPath}} {{.Stale}} {{.StaleReason}} {{.Target}}", "./depBase")
889 AssertNotRebuilt(t, "new .a file before build", target)
890 cleanup := touch(t, target)
893 goCmd(t, "install", "-v", "-linkshared", "./exe")
895 goCmd(t, "install", "-v", "-linkshared", "./exe")
896 AssertNotRebuilt(t, "new .a file", target)
897 AssertRebuilt(t, "new .a file", shlib)
901 func appendFile(t *testing.T, path, content string) {
903 f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0660)
905 t.Fatalf("os.OpenFile failed: %v", err)
910 t.Fatalf("f.Close failed: %v", err)
913 _, err = f.WriteString(content)
915 t.Fatalf("f.WriteString failed: %v", err)
919 func createFile(t *testing.T, path, content string) {
921 f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
923 t.Fatalf("os.OpenFile failed: %v", err)
925 _, err = f.WriteString(content)
926 if closeErr := f.Close(); err == nil {
930 t.Fatalf("WriteString failed: %v", err)
934 func TestABIChecking(t *testing.T) {
935 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
936 goCmd(t, "install", "-linkshared", "./exe")
938 // If we make an ABI-breaking change to depBase and rebuild libp.so but not exe,
939 // exe will abort with a complaint on startup.
940 // This assumes adding an exported function breaks ABI, which is not true in
941 // some senses but suffices for the narrow definition of ABI compatibility the
942 // toolchain uses today.
945 createFile(t, "./depBase/break.go", "package depBase\nfunc ABIBreak() {}\n")
946 defer os.Remove("./depBase/break.go")
948 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
949 c := exec.Command("../../bin/exe")
950 output, err := c.CombinedOutput()
952 t.Fatal("executing exe did not fail after ABI break")
954 scanner := bufio.NewScanner(bytes.NewReader(output))
956 const wantPrefix = "abi mismatch detected between the executable and lib"
958 if strings.HasPrefix(scanner.Text(), wantPrefix) {
963 if err = scanner.Err(); err != nil {
964 t.Errorf("scanner encountered error: %v", err)
967 t.Fatalf("exe failed, but without line %q; got output:\n%s", wantPrefix, output)
970 // Rebuilding exe makes it work again.
971 goCmd(t, "install", "-linkshared", "./exe")
972 run(t, "rebuilt exe", "../../bin/exe")
974 // If we make a change which does not break ABI (such as adding an unexported
975 // function) and rebuild libdepBase.so, exe still works, even if new function
976 // is in a file by itself.
978 createFile(t, "./depBase/dep2.go", "package depBase\nfunc noABIBreak() {}\n")
979 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase")
980 run(t, "after non-ABI breaking change", "../../bin/exe")
983 // If a package 'explicit' imports a package 'implicit', building
984 // 'explicit' into a shared library implicitly includes implicit in
985 // the shared library. Building an executable that imports both
986 // explicit and implicit builds the code from implicit into the
987 // executable rather than fetching it from the shared library. The
988 // link still succeeds and the executable still runs though.
989 func TestImplicitInclusion(t *testing.T) {
990 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./explicit")
991 goCmd(t, "install", "-linkshared", "./implicitcmd")
992 run(t, "running executable linked against library that contains same package as it", "../../bin/implicitcmd")
995 // Tests to make sure that the type fields of empty interfaces and itab
996 // fields of nonempty interfaces are unique even across modules,
997 // so that interface equality works correctly.
998 func TestInterface(t *testing.T) {
999 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./iface_a")
1000 // Note: iface_i gets installed implicitly as a dependency of iface_a.
1001 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./iface_b")
1002 goCmd(t, "install", "-linkshared", "./iface")
1003 run(t, "running type/itab uniqueness tester", "../../bin/iface")
1006 // Access a global variable from a library.
1007 func TestGlobal(t *testing.T) {
1008 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./globallib")
1009 goCmd(t, "install", "-linkshared", "./global")
1010 run(t, "global executable", "../../bin/global")
1011 AssertIsLinkedTo(t, "../../bin/global", soname)
1012 AssertHasRPath(t, "../../bin/global", gorootInstallDir)
1015 // Run a test using -linkshared of an installed shared package.
1017 func TestTestInstalledShared(t *testing.T) {
1018 goCmd(nil, "test", "-linkshared", "-test.short", "sync/atomic")
1021 // Test generated pointer method with -linkshared.
1023 func TestGeneratedMethod(t *testing.T) {
1024 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue25065")
1027 // Test use of shared library struct with generated hash function.
1029 func TestGeneratedHash(t *testing.T) {
1030 goCmd(nil, "install", "-buildmode=shared", "-linkshared", "./issue30768/issue30768lib")
1031 goCmd(nil, "test", "-linkshared", "./issue30768")