diff --git a/src/cmd/link/internal/ld/main.go b/src/cmd/link/internal/ld/main.go index 1d2a764db90..af1f6d763d9 100644 --- a/src/cmd/link/internal/ld/main.go +++ b/src/cmd/link/internal/ld/main.go @@ -39,6 +39,7 @@ import ( "flag" "log" "os" + "os/exec" "runtime" "runtime/pprof" "strings" @@ -98,6 +99,8 @@ var ( benchmarkFlag = flag.String("benchmark", "", "set to 'mem' or 'cpu' to enable phase benchmarking") benchmarkFileFlag = flag.String("benchmarkprofile", "", "emit phase profiles to `base`_phase.{cpu,mem}prof") + + flagGo115Newobj = flag.Bool("go115newobj", true, "use new object file format") ) // Main is the main entry point for the linker code. @@ -137,6 +140,10 @@ func Main(arch *sys.Arch, theArch Arch) { objabi.Flagparse(usage) + if !*flagGo115Newobj { + oldlink() + } + switch *flagHeadType { case "": case "windowsgui": @@ -401,3 +408,48 @@ func startProfile() { }) } } + +// Invoke the old linker and exit. +func oldlink() { + linker := os.Args[0] + if strings.HasSuffix(linker, "link") { + linker = linker[:len(linker)-4] + "oldlink" + } else if strings.HasSuffix(linker, "link.exe") { + linker = linker[:len(linker)-8] + "oldlink.exe" + } else { + log.Fatal("cannot find oldlink. arg0=", linker) + } + + // Copy args, filter out -go115newobj flag + args := make([]string, 0, len(os.Args)-1) + skipNext := false + for i, a := range os.Args { + if i == 0 { + continue // skip arg0 + } + if skipNext { + skipNext = false + continue + } + if a == "-go115newobj" { + skipNext = true + continue + } + if strings.HasPrefix(a, "-go115newobj=") { + continue + } + args = append(args, a) + } + + cmd := exec.Command(linker, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + if err == nil { + os.Exit(0) + } + if _, ok := err.(*exec.ExitError); ok { + os.Exit(2) // would be nice to use ExitError.ExitCode(), but that is too new + } + log.Fatal("invoke oldlink failed:", err) +} diff --git a/src/cmd/link/link_test.go b/src/cmd/link/link_test.go index 4f792bd1f19..5e19cb5de18 100644 --- a/src/cmd/link/link_test.go +++ b/src/cmd/link/link_test.go @@ -447,3 +447,27 @@ func TestStrictDup(t *testing.T) { t.Errorf("unexpected output:\n%s", out) } } + +func TestOldLink(t *testing.T) { + // Test that old object file format still works. + // TODO(go115newobj): delete. + + testenv.MustHaveGoBuild(t) + + tmpdir, err := ioutil.TempDir("", "TestOldLink") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpdir) + + src := filepath.Join(tmpdir, "main.go") + err = ioutil.WriteFile(src, []byte("package main; func main(){}\n"), 0666) + if err != nil { + t.Fatal(err) + } + + cmd := exec.Command(testenv.GoToolPath(t), "run", "-gcflags=all=-go115newobj=false", "-asmflags=all=-go115newobj=false", "-ldflags=-go115newobj=false", src) + if out, err := cmd.CombinedOutput(); err != nil { + t.Errorf("%v: %v:\n%s", cmd.Args, err, out) + } +}