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 (
|
|
|
|
|
"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) {
|
2014-07-08 13:43:22 -04:00
|
|
|
switch runtime.GOOS {
|
|
|
|
|
case "android", "nacl":
|
|
|
|
|
t.Skipf("skipping on %s", runtime.GOOS)
|
2015-02-27 07:06:34 -05:00
|
|
|
case "darwin":
|
|
|
|
|
if runtime.GOARCH == "arm" {
|
|
|
|
|
t.Skipf("skipping on %s/%s", runtime.GOOS, runtime.GOARCH)
|
|
|
|
|
}
|
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")
|
|
|
|
|
out, err := exec.Command("go", "build", "-o", exe, "cmd/objdump").CombinedOutput()
|
|
|
|
|
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",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
out, err := exec.Command("go", 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)",
|
|
|
|
|
}
|
|
|
|
|
switch runtime.GOARCH {
|
|
|
|
|
case "amd64", "386":
|
|
|
|
|
need = append(need, x86Need...)
|
|
|
|
|
case "arm":
|
|
|
|
|
need = append(need, armNeed...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2014-12-05 19:13:20 -05:00
|
|
|
case "ppc64", "ppc64le":
|
2014-11-02 11:23:41 +11:00
|
|
|
t.Skipf("skipping on %s, issue 9039", runtime.GOARCH)
|
|
|
|
|
}
|
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)
|
|
|
|
|
}
|
2014-10-28 23:25:55 -04:00
|
|
|
testDisasm(t, "-ldflags=-linkmode=external")
|
|
|
|
|
}
|