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"
|
2016-11-02 22:56:10 -07:00
|
|
|
"fmt"
|
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"
|
|
|
|
|
)
|
|
|
|
|
|
2016-11-02 22:56:10 -07:00
|
|
|
var tmp, exe string // populated by buildObjdump
|
2014-05-20 12:10:19 -04:00
|
|
|
|
2016-11-02 22:56:10 -07:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
|
if !testenv.HasGoBuild() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var exitcode int
|
|
|
|
|
if err := buildObjdump(); err == nil {
|
|
|
|
|
exitcode = m.Run()
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
exitcode = 1
|
|
|
|
|
}
|
|
|
|
|
os.RemoveAll(tmp)
|
|
|
|
|
os.Exit(exitcode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func buildObjdump() error {
|
|
|
|
|
var err error
|
|
|
|
|
tmp, err = ioutil.TempDir("", "TestObjDump")
|
2014-05-14 19:51:15 -04:00
|
|
|
if err != nil {
|
2016-11-02 22:56:10 -07:00
|
|
|
return fmt.Errorf("TempDir failed: %v", err)
|
2014-05-14 19:51:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exe = filepath.Join(tmp, "testobjdump.exe")
|
2016-11-02 22:56:10 -07:00
|
|
|
gotool, err := testenv.GoTool()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
out, err := exec.Command(gotool, "build", "-o", exe, "cmd/objdump").CombinedOutput()
|
2014-05-14 19:51:15 -04:00
|
|
|
if err != nil {
|
|
|
|
|
os.RemoveAll(tmp)
|
2016-11-02 22:56:10 -07:00
|
|
|
return fmt.Errorf("go build -o %v cmd/objdump: %v\n%s", exe, err, string(out))
|
2014-05-14 19:51:15 -04:00
|
|
|
}
|
2016-11-02 22:56:10 -07:00
|
|
|
|
|
|
|
|
return nil
|
2014-05-14 19:51:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var x86Need = []string{
|
|
|
|
|
"JMP main.main(SB)",
|
2017-02-19 14:01:42 -05:00
|
|
|
"CALL main.Println(SB)",
|
2014-05-14 19:51:15 -04:00
|
|
|
"RET",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var armNeed = []string{
|
2014-10-29 21:02:58 -04:00
|
|
|
//"B.LS main.main(SB)", // TODO(rsc): restore; golang.org/issue/9021
|
2017-02-19 14:01:42 -05:00
|
|
|
"BL main.Println(SB)",
|
2014-05-14 19:51:15 -04:00
|
|
|
"RET",
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-04 01:23:56 -04:00
|
|
|
var ppcNeed = []string{
|
|
|
|
|
"BR main.main(SB)",
|
2017-02-19 14:01:42 -05:00
|
|
|
"CALL main.Println(SB)",
|
2015-05-04 01:23:56 -04:00
|
|
|
"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.
|
|
|
|
|
|
cmd/objdump: print Go code alongside assembly
Added -S flag to print go source file line above corresponding disassembly:
$ go tool objdump -S -s main.main fmthello
TEXT main.main(SB) /home/rugginoso/Documents/src/go/src/cmd/objdump/testdata/fmthello.go
func main() {
0x47d450 64488b0c25f8ffffff FS MOVQ FS:0xfffffff8, CX
0x47d459 483b6110 CMPQ 0x10(CX), SP
0x47d45d 7631 JBE 0x47d490
0x47d45f 4883ec18 SUBQ $0x18, SP
0x47d463 48896c2410 MOVQ BP, 0x10(SP)
0x47d468 488d6c2410 LEAQ 0x10(SP), BP
Println("hello, world")
0x47d46d 488d0563b00200 LEAQ 0x2b063(IP), AX
0x47d474 48890424 MOVQ AX, 0(SP)
0x47d478 48c74424080c000000 MOVQ $0xc, 0x8(SP)
0x47d481 e81a000000 CALL main.Println(SB)
}
0x47d486 488b6c2410 MOVQ 0x10(SP), BP
0x47d48b 4883c418 ADDQ $0x18, SP
0x47d48f c3 RET
func main() {
0x47d490 e8ebf1fcff CALL runtime.morestack_noctxt(SB)
0x47d495 ebb9 JMP main.main(SB)
Execution time:
$ time go tool objdump testdata/fmthello > /dev/null
real 0m0.430s
user 0m0.440s
sys 0m0.000s
$ time go tool objdump -S testdata/fmthello > /dev/null
real 0m0.471s
user 0m0.476s
sys 0m0.012s
Fixes #18245
Change-Id: I9b2f8338f9ee443c1352efd270d3ba85e3dd9b78
Reviewed-on: https://go-review.googlesource.com/37953
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2017-03-08 16:45:23 +01:00
|
|
|
func testDisasm(t *testing.T, printCode bool, flags ...string) {
|
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{
|
|
|
|
|
"TEXT main.main(SB)",
|
|
|
|
|
}
|
cmd/objdump: print Go code alongside assembly
Added -S flag to print go source file line above corresponding disassembly:
$ go tool objdump -S -s main.main fmthello
TEXT main.main(SB) /home/rugginoso/Documents/src/go/src/cmd/objdump/testdata/fmthello.go
func main() {
0x47d450 64488b0c25f8ffffff FS MOVQ FS:0xfffffff8, CX
0x47d459 483b6110 CMPQ 0x10(CX), SP
0x47d45d 7631 JBE 0x47d490
0x47d45f 4883ec18 SUBQ $0x18, SP
0x47d463 48896c2410 MOVQ BP, 0x10(SP)
0x47d468 488d6c2410 LEAQ 0x10(SP), BP
Println("hello, world")
0x47d46d 488d0563b00200 LEAQ 0x2b063(IP), AX
0x47d474 48890424 MOVQ AX, 0(SP)
0x47d478 48c74424080c000000 MOVQ $0xc, 0x8(SP)
0x47d481 e81a000000 CALL main.Println(SB)
}
0x47d486 488b6c2410 MOVQ 0x10(SP), BP
0x47d48b 4883c418 ADDQ $0x18, SP
0x47d48f c3 RET
func main() {
0x47d490 e8ebf1fcff CALL runtime.morestack_noctxt(SB)
0x47d495 ebb9 JMP main.main(SB)
Execution time:
$ time go tool objdump testdata/fmthello > /dev/null
real 0m0.430s
user 0m0.440s
sys 0m0.000s
$ time go tool objdump -S testdata/fmthello > /dev/null
real 0m0.471s
user 0m0.476s
sys 0m0.012s
Fixes #18245
Change-Id: I9b2f8338f9ee443c1352efd270d3ba85e3dd9b78
Reviewed-on: https://go-review.googlesource.com/37953
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2017-03-08 16:45:23 +01:00
|
|
|
|
|
|
|
|
if printCode {
|
|
|
|
|
need = append(need, ` Println("hello, world")`)
|
|
|
|
|
} else {
|
|
|
|
|
need = append(need, "fmthello.go:6")
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
cmd/objdump: print Go code alongside assembly
Added -S flag to print go source file line above corresponding disassembly:
$ go tool objdump -S -s main.main fmthello
TEXT main.main(SB) /home/rugginoso/Documents/src/go/src/cmd/objdump/testdata/fmthello.go
func main() {
0x47d450 64488b0c25f8ffffff FS MOVQ FS:0xfffffff8, CX
0x47d459 483b6110 CMPQ 0x10(CX), SP
0x47d45d 7631 JBE 0x47d490
0x47d45f 4883ec18 SUBQ $0x18, SP
0x47d463 48896c2410 MOVQ BP, 0x10(SP)
0x47d468 488d6c2410 LEAQ 0x10(SP), BP
Println("hello, world")
0x47d46d 488d0563b00200 LEAQ 0x2b063(IP), AX
0x47d474 48890424 MOVQ AX, 0(SP)
0x47d478 48c74424080c000000 MOVQ $0xc, 0x8(SP)
0x47d481 e81a000000 CALL main.Println(SB)
}
0x47d486 488b6c2410 MOVQ 0x10(SP), BP
0x47d48b 4883c418 ADDQ $0x18, SP
0x47d48f c3 RET
func main() {
0x47d490 e8ebf1fcff CALL runtime.morestack_noctxt(SB)
0x47d495 ebb9 JMP main.main(SB)
Execution time:
$ time go tool objdump testdata/fmthello > /dev/null
real 0m0.430s
user 0m0.440s
sys 0m0.000s
$ time go tool objdump -S testdata/fmthello > /dev/null
real 0m0.471s
user 0m0.476s
sys 0m0.012s
Fixes #18245
Change-Id: I9b2f8338f9ee443c1352efd270d3ba85e3dd9b78
Reviewed-on: https://go-review.googlesource.com/37953
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2017-03-08 16:45:23 +01:00
|
|
|
args = []string{
|
|
|
|
|
"-s", "main.main",
|
|
|
|
|
hello,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if printCode {
|
|
|
|
|
args = append([]string{"-S"}, args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err = exec.Command(exe, args...).CombinedOutput()
|
2014-05-14 19:51:15 -04:00
|
|
|
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)
|
2016-10-18 23:51:19 +02:00
|
|
|
case "mips", "mipsle", "mips64", "mips64le":
|
2015-09-10 07:13:28 -04:00
|
|
|
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
|
|
|
}
|
cmd/objdump: print Go code alongside assembly
Added -S flag to print go source file line above corresponding disassembly:
$ go tool objdump -S -s main.main fmthello
TEXT main.main(SB) /home/rugginoso/Documents/src/go/src/cmd/objdump/testdata/fmthello.go
func main() {
0x47d450 64488b0c25f8ffffff FS MOVQ FS:0xfffffff8, CX
0x47d459 483b6110 CMPQ 0x10(CX), SP
0x47d45d 7631 JBE 0x47d490
0x47d45f 4883ec18 SUBQ $0x18, SP
0x47d463 48896c2410 MOVQ BP, 0x10(SP)
0x47d468 488d6c2410 LEAQ 0x10(SP), BP
Println("hello, world")
0x47d46d 488d0563b00200 LEAQ 0x2b063(IP), AX
0x47d474 48890424 MOVQ AX, 0(SP)
0x47d478 48c74424080c000000 MOVQ $0xc, 0x8(SP)
0x47d481 e81a000000 CALL main.Println(SB)
}
0x47d486 488b6c2410 MOVQ 0x10(SP), BP
0x47d48b 4883c418 ADDQ $0x18, SP
0x47d48f c3 RET
func main() {
0x47d490 e8ebf1fcff CALL runtime.morestack_noctxt(SB)
0x47d495 ebb9 JMP main.main(SB)
Execution time:
$ time go tool objdump testdata/fmthello > /dev/null
real 0m0.430s
user 0m0.440s
sys 0m0.000s
$ time go tool objdump -S testdata/fmthello > /dev/null
real 0m0.471s
user 0m0.476s
sys 0m0.012s
Fixes #18245
Change-Id: I9b2f8338f9ee443c1352efd270d3ba85e3dd9b78
Reviewed-on: https://go-review.googlesource.com/37953
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2017-03-08 16:45:23 +01:00
|
|
|
testDisasm(t, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDisasmCode(t *testing.T) {
|
|
|
|
|
switch runtime.GOARCH {
|
|
|
|
|
case "arm64":
|
|
|
|
|
t.Skipf("skipping on %s, issue 10106", runtime.GOARCH)
|
|
|
|
|
case "mips", "mipsle", "mips64", "mips64le":
|
|
|
|
|
t.Skipf("skipping on %s, issue 12559", runtime.GOARCH)
|
|
|
|
|
case "s390x":
|
|
|
|
|
t.Skipf("skipping on %s, issue 15255", runtime.GOARCH)
|
|
|
|
|
}
|
|
|
|
|
testDisasm(t, true)
|
2014-10-28 23:25:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2016-10-25 15:13:07 -05:00
|
|
|
case "ppc64":
|
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)
|
2016-12-13 22:54:19 +01:00
|
|
|
case "mips64", "mips64le", "mips", "mipsle":
|
2015-09-10 07:13:28 -04:00
|
|
|
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")
|
|
|
|
|
}
|
cmd/objdump: print Go code alongside assembly
Added -S flag to print go source file line above corresponding disassembly:
$ go tool objdump -S -s main.main fmthello
TEXT main.main(SB) /home/rugginoso/Documents/src/go/src/cmd/objdump/testdata/fmthello.go
func main() {
0x47d450 64488b0c25f8ffffff FS MOVQ FS:0xfffffff8, CX
0x47d459 483b6110 CMPQ 0x10(CX), SP
0x47d45d 7631 JBE 0x47d490
0x47d45f 4883ec18 SUBQ $0x18, SP
0x47d463 48896c2410 MOVQ BP, 0x10(SP)
0x47d468 488d6c2410 LEAQ 0x10(SP), BP
Println("hello, world")
0x47d46d 488d0563b00200 LEAQ 0x2b063(IP), AX
0x47d474 48890424 MOVQ AX, 0(SP)
0x47d478 48c74424080c000000 MOVQ $0xc, 0x8(SP)
0x47d481 e81a000000 CALL main.Println(SB)
}
0x47d486 488b6c2410 MOVQ 0x10(SP), BP
0x47d48b 4883c418 ADDQ $0x18, SP
0x47d48f c3 RET
func main() {
0x47d490 e8ebf1fcff CALL runtime.morestack_noctxt(SB)
0x47d495 ebb9 JMP main.main(SB)
Execution time:
$ time go tool objdump testdata/fmthello > /dev/null
real 0m0.430s
user 0m0.440s
sys 0m0.000s
$ time go tool objdump -S testdata/fmthello > /dev/null
real 0m0.471s
user 0m0.476s
sys 0m0.012s
Fixes #18245
Change-Id: I9b2f8338f9ee443c1352efd270d3ba85e3dd9b78
Reviewed-on: https://go-review.googlesource.com/37953
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
2017-03-08 16:45:23 +01:00
|
|
|
testDisasm(t, false, "-ldflags=-linkmode=external")
|
2014-10-28 23:25:55 -04:00
|
|
|
}
|