2014-05-12 17:00:57 +10:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2015-05-04 01:23:56 -04:00
|
|
|
"flag"
|
2015-07-31 10:49:01 -07:00
|
|
|
"go/build"
|
2015-06-05 11:01:53 -04:00
|
|
|
"internal/testenv"
|
2014-05-12 17:00:57 +10:00
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"runtime"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2014-05-14 19:51:15 -04:00
|
|
|
func buildObjdump(t *testing.T) (tmp, exe string) {
|
2015-06-05 11:01:53 -04:00
|
|
|
testenv.MustHaveGoBuild(t)
|
2014-05-20 12:10:19 -04:00
|
|
|
|
2014-05-14 19:51:15 -04:00
|
|
|
tmp, err := ioutil.TempDir("", "TestObjDump")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal("TempDir failed: ", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exe = filepath.Join(tmp, "testobjdump.exe")
|
2016-08-30 11:08:47 -07:00
|
|
|
out, err := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, "cmd/objdump").CombinedOutput()
|
2014-05-14 19:51:15 -04:00
|
|
|
if err != nil {
|
|
|
|
|
os.RemoveAll(tmp)
|
|
|
|
|
t.Fatalf("go build -o %v cmd/objdump: %v\n%s", exe, err, string(out))
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var x86Need = []string{
|
|
|
|
|
"fmthello.go:6",
|
|
|
|
|
"TEXT main.main(SB)",
|
|
|
|
|
"JMP main.main(SB)",
|
|
|
|
|
"CALL fmt.Println(SB)",
|
|
|
|
|
"RET",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var armNeed = []string{
|
|
|
|
|
"fmthello.go:6",
|
|
|
|
|
"TEXT main.main(SB)",
|
2014-10-29 21:02:58 -04:00
|
|
|
//"B.LS main.main(SB)", // TODO(rsc): restore; golang.org/issue/9021
|
2014-05-14 19:51:15 -04:00
|
|
|
"BL fmt.Println(SB)",
|
|
|
|
|
"RET",
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-04 01:23:56 -04:00
|
|
|
var ppcNeed = []string{
|
|
|
|
|
"fmthello.go:6",
|
|
|
|
|
"TEXT main.main(SB)",
|
|
|
|
|
"BR main.main(SB)",
|
|
|
|
|
"BL fmt.Println(SB)",
|
|
|
|
|
"RET",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var target = flag.String("target", "", "test disassembly of `goos/goarch` binary")
|
|
|
|
|
|
2014-05-14 19:51:15 -04:00
|
|
|
// objdump is fully cross platform: it can handle binaries
|
|
|
|
|
// from any known operating system and architecture.
|
|
|
|
|
// We could in principle add binaries to testdata and check
|
|
|
|
|
// all the supported systems during this test. However, the
|
|
|
|
|
// binaries would be about 1 MB each, and we don't want to
|
|
|
|
|
// add that much junk to the hg repository. Instead, build a
|
|
|
|
|
// binary for the current system (only) and test that objdump
|
|
|
|
|
// can handle that one.
|
|
|
|
|
|
2014-10-28 23:25:55 -04:00
|
|
|
func testDisasm(t *testing.T, flags ...string) {
|
2014-05-14 19:51:15 -04:00
|
|
|
tmp, exe := buildObjdump(t)
|
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
2015-05-04 01:23:56 -04:00
|
|
|
goarch := runtime.GOARCH
|
|
|
|
|
if *target != "" {
|
|
|
|
|
f := strings.Split(*target, "/")
|
|
|
|
|
if len(f) != 2 {
|
|
|
|
|
t.Fatalf("-target argument must be goos/goarch")
|
|
|
|
|
}
|
|
|
|
|
defer os.Setenv("GOOS", os.Getenv("GOOS"))
|
|
|
|
|
defer os.Setenv("GOARCH", os.Getenv("GOARCH"))
|
|
|
|
|
os.Setenv("GOOS", f[0])
|
|
|
|
|
os.Setenv("GOARCH", f[1])
|
|
|
|
|
goarch = f[1]
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-14 19:51:15 -04:00
|
|
|
hello := filepath.Join(tmp, "hello.exe")
|
2014-10-28 23:25:55 -04:00
|
|
|
args := []string{"build", "-o", hello}
|
|
|
|
|
args = append(args, flags...)
|
|
|
|
|
args = append(args, "testdata/fmthello.go")
|
2016-08-30 11:08:47 -07:00
|
|
|
out, err := exec.Command(testenv.GoToolPath(t), args...).CombinedOutput()
|
2014-05-14 19:51:15 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("go build fmthello.go: %v\n%s", err, out)
|
|
|
|
|
}
|
|
|
|
|
need := []string{
|
|
|
|
|
"fmthello.go:6",
|
|
|
|
|
"TEXT main.main(SB)",
|
|
|
|
|
}
|
2015-05-04 01:23:56 -04:00
|
|
|
switch goarch {
|
2014-05-14 19:51:15 -04:00
|
|
|
case "amd64", "386":
|
|
|
|
|
need = append(need, x86Need...)
|
|
|
|
|
case "arm":
|
|
|
|
|
need = append(need, armNeed...)
|
2015-05-04 01:23:56 -04:00
|
|
|
case "ppc64", "ppc64le":
|
|
|
|
|
need = append(need, ppcNeed...)
|
2014-05-14 19:51:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err = exec.Command(exe, "-s", "main.main", hello).CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("objdump fmthello.exe: %v\n%s", err, out)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
text := string(out)
|
|
|
|
|
ok := true
|
|
|
|
|
for _, s := range need {
|
|
|
|
|
if !strings.Contains(text, s) {
|
|
|
|
|
t.Errorf("disassembly missing '%s'", s)
|
|
|
|
|
ok = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !ok {
|
|
|
|
|
t.Logf("full disassembly:\n%s", text)
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-28 23:25:55 -04:00
|
|
|
|
|
|
|
|
func TestDisasm(t *testing.T) {
|
2014-11-02 11:23:41 +11:00
|
|
|
switch runtime.GOARCH {
|
2015-03-08 14:29:44 +01:00
|
|
|
case "arm64":
|
|
|
|
|
t.Skipf("skipping on %s, issue 10106", runtime.GOARCH)
|
2015-09-10 07:13:28 -04:00
|
|
|
case "mips64", "mips64le":
|
|
|
|
|
t.Skipf("skipping on %s, issue 12559", runtime.GOARCH)
|
2016-04-12 13:38:24 -04:00
|
|
|
case "s390x":
|
|
|
|
|
t.Skipf("skipping on %s, issue 15255", runtime.GOARCH)
|
2014-11-02 11:23:41 +11:00
|
|
|
}
|
2014-10-28 23:25:55 -04:00
|
|
|
testDisasm(t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDisasmExtld(t *testing.T) {
|
|
|
|
|
switch runtime.GOOS {
|
2014-10-29 00:03:17 -04:00
|
|
|
case "plan9", "windows":
|
2014-10-28 23:25:55 -04:00
|
|
|
t.Skipf("skipping on %s", runtime.GOOS)
|
|
|
|
|
}
|
2014-11-02 11:23:41 +11:00
|
|
|
switch runtime.GOARCH {
|
2014-12-05 19:13:20 -05:00
|
|
|
case "ppc64", "ppc64le":
|
2014-11-02 11:23:41 +11:00
|
|
|
t.Skipf("skipping on %s, no support for external linking, issue 9038", runtime.GOARCH)
|
2015-03-08 14:29:44 +01:00
|
|
|
case "arm64":
|
|
|
|
|
t.Skipf("skipping on %s, issue 10106", runtime.GOARCH)
|
2015-09-10 07:13:28 -04:00
|
|
|
case "mips64", "mips64le":
|
|
|
|
|
t.Skipf("skipping on %s, issue 12559 and 12560", runtime.GOARCH)
|
2016-04-12 13:38:24 -04:00
|
|
|
case "s390x":
|
|
|
|
|
t.Skipf("skipping on %s, issue 15255", runtime.GOARCH)
|
2014-11-02 11:23:41 +11:00
|
|
|
}
|
2015-06-11 16:49:38 +03:00
|
|
|
// TODO(jsing): Reenable once openbsd/arm has external linking support.
|
2015-04-30 00:59:36 +10:00
|
|
|
if runtime.GOOS == "openbsd" && runtime.GOARCH == "arm" {
|
|
|
|
|
t.Skip("skipping on openbsd/arm, no support for external linking, issue 10619")
|
|
|
|
|
}
|
2015-07-31 10:49:01 -07:00
|
|
|
if !build.Default.CgoEnabled {
|
|
|
|
|
t.Skip("skipping because cgo is not enabled")
|
|
|
|
|
}
|
2014-10-28 23:25:55 -04:00
|
|
|
testDisasm(t, "-ldflags=-linkmode=external")
|
|
|
|
|
}
|