|
6 | 6 | "os"
|
7 | 7 | "os/exec"
|
8 | 8 | "path/filepath"
|
| 9 | + "strings" |
9 | 10 | "testing"
|
10 | 11 | )
|
11 | 12 |
|
@@ -70,3 +71,48 @@ func main() {}
|
70 | 71 | t.Fatalf("failed to link main.o: %v, output: %s\n", err, out)
|
71 | 72 | }
|
72 | 73 | }
|
| 74 | + |
| 75 | +// TestIssue28429 ensures that the linker does not attempt to link |
| 76 | +// sections not named *.o. Such sections may be used by a build system |
| 77 | +// to, for example, save facts produced by a modular static analysis |
| 78 | +// such as golang.org/x/tools/go/analysis. |
| 79 | +func TestIssue28429(t *testing.T) { |
| 80 | + testenv.MustHaveGoBuild(t) |
| 81 | + |
| 82 | + tmpdir, err := ioutil.TempDir("", "issue28429-") |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("failed to create temp dir: %v", err) |
| 85 | + } |
| 86 | + defer os.RemoveAll(tmpdir) |
| 87 | + |
| 88 | + write := func(name, content string) { |
| 89 | + err := ioutil.WriteFile(filepath.Join(tmpdir, name), []byte(content), 0666) |
| 90 | + if err != nil { |
| 91 | + t.Fatal(err) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + runGo := func(args ...string) { |
| 96 | + cmd := exec.Command(testenv.GoToolPath(t), args...) |
| 97 | + cmd.Dir = tmpdir |
| 98 | + out, err := cmd.CombinedOutput() |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("'go %s' failed: %v, output: %s", |
| 101 | + strings.Join(args, " "), err, out) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // Compile a main package. |
| 106 | + write("main.go", "package main; func main() {}") |
| 107 | + runGo("tool", "compile", "-p", "main", "main.go") |
| 108 | + runGo("tool", "pack", "c", "main.a", "main.o") |
| 109 | + |
| 110 | + // Add an extra section with a short, non-.o name. |
| 111 | + // This simulates an alternative build system. |
| 112 | + write(".facts", "this is not an object file") |
| 113 | + runGo("tool", "pack", "r", "main.a", ".facts") |
| 114 | + |
| 115 | + // Verify that the linker does not attempt |
| 116 | + // to compile the extra section. |
| 117 | + runGo("tool", "link", "main.a") |
| 118 | +} |
0 commit comments