github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/src/cmd/objdump/objdump_test.go (about) 1 // Copyright 2014 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "io/ioutil" 9 "os" 10 "os/exec" 11 "path/filepath" 12 "runtime" 13 "strings" 14 "testing" 15 ) 16 17 func buildObjdump(t *testing.T) (tmp, exe string) { 18 switch runtime.GOOS { 19 case "android", "nacl": 20 t.Skipf("skipping on %s", runtime.GOOS) 21 } 22 23 tmp, err := ioutil.TempDir("", "TestObjDump") 24 if err != nil { 25 t.Fatal("TempDir failed: ", err) 26 } 27 28 exe = filepath.Join(tmp, "testobjdump.exe") 29 out, err := exec.Command("go", "build", "-o", exe, "cmd/objdump").CombinedOutput() 30 if err != nil { 31 os.RemoveAll(tmp) 32 t.Fatalf("go build -o %v cmd/objdump: %v\n%s", exe, err, string(out)) 33 } 34 return 35 } 36 37 var x86Need = []string{ 38 "fmthello.go:6", 39 "TEXT main.main(SB)", 40 "JMP main.main(SB)", 41 "CALL fmt.Println(SB)", 42 "RET", 43 } 44 45 var armNeed = []string{ 46 "fmthello.go:6", 47 "TEXT main.main(SB)", 48 //"B.LS main.main(SB)", // TODO(rsc): restore; golang.org/issue/9021 49 "BL fmt.Println(SB)", 50 "RET", 51 } 52 53 // objdump is fully cross platform: it can handle binaries 54 // from any known operating system and architecture. 55 // We could in principle add binaries to testdata and check 56 // all the supported systems during this test. However, the 57 // binaries would be about 1 MB each, and we don't want to 58 // add that much junk to the hg repository. Instead, build a 59 // binary for the current system (only) and test that objdump 60 // can handle that one. 61 62 func testDisasm(t *testing.T, flags ...string) { 63 tmp, exe := buildObjdump(t) 64 defer os.RemoveAll(tmp) 65 66 hello := filepath.Join(tmp, "hello.exe") 67 args := []string{"build", "-o", hello} 68 args = append(args, flags...) 69 args = append(args, "testdata/fmthello.go") 70 out, err := exec.Command("go", args...).CombinedOutput() 71 if err != nil { 72 t.Fatalf("go build fmthello.go: %v\n%s", err, out) 73 } 74 need := []string{ 75 "fmthello.go:6", 76 "TEXT main.main(SB)", 77 } 78 switch runtime.GOARCH { 79 case "amd64", "386": 80 need = append(need, x86Need...) 81 case "arm": 82 need = append(need, armNeed...) 83 } 84 85 out, err = exec.Command(exe, "-s", "main.main", hello).CombinedOutput() 86 if err != nil { 87 t.Fatalf("objdump fmthello.exe: %v\n%s", err, out) 88 } 89 90 text := string(out) 91 ok := true 92 for _, s := range need { 93 if !strings.Contains(text, s) { 94 t.Errorf("disassembly missing '%s'", s) 95 ok = false 96 } 97 } 98 if !ok { 99 t.Logf("full disassembly:\n%s", text) 100 } 101 } 102 103 func TestDisasm(t *testing.T) { 104 switch runtime.GOARCH { 105 case "ppc64", "ppc64le": 106 t.Skipf("skipping on %s, issue 9039", runtime.GOARCH) 107 } 108 testDisasm(t) 109 } 110 111 func TestDisasmExtld(t *testing.T) { 112 switch runtime.GOOS { 113 case "plan9", "windows": 114 t.Skipf("skipping on %s", runtime.GOOS) 115 } 116 switch runtime.GOARCH { 117 case "ppc64", "ppc64le": 118 t.Skipf("skipping on %s, no support for external linking, issue 9038", runtime.GOARCH) 119 } 120 testDisasm(t, "-ldflags=-linkmode=external") 121 }