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 "go/build" 9 "internal/testenv" 10 "io/ioutil" 11 "os" 12 "os/exec" 13 "path/filepath" 14 "runtime" 15 "strings" 16 "testing" 17 ) 18 19 func buildObjdump(t *testing.T) (tmp, exe string) { 20 testenv.MustHaveGoBuild(t) 21 22 tmp, err := ioutil.TempDir("", "TestObjDump") 23 if err != nil { 24 t.Fatal("TempDir failed: ", err) 25 } 26 27 exe = filepath.Join(tmp, "testobjdump.exe") 28 out, err := exec.Command("go", "build", "-o", exe, "cmd/objdump").CombinedOutput() 29 if err != nil { 30 os.RemoveAll(tmp) 31 t.Fatalf("go build -o %v cmd/objdump: %v\n%s", exe, err, string(out)) 32 } 33 return 34 } 35 36 var x86Need = []string{ 37 "fmthello.go:6", 38 "TEXT main.main(SB)", 39 "JMP main.main(SB)", 40 "CALL fmt.Println(SB)", 41 "RET", 42 } 43 44 var armNeed = []string{ 45 "fmthello.go:6", 46 "TEXT main.main(SB)", 47 //"B.LS main.main(SB)", // TODO(rsc): restore; golang.org/issue/9021 48 "BL fmt.Println(SB)", 49 "RET", 50 } 51 52 // objdump is fully cross platform: it can handle binaries 53 // from any known operating system and architecture. 54 // We could in principle add binaries to testdata and check 55 // all the supported systems during this test. However, the 56 // binaries would be about 1 MB each, and we don't want to 57 // add that much junk to the hg repository. Instead, build a 58 // binary for the current system (only) and test that objdump 59 // can handle that one. 60 61 func testDisasm(t *testing.T, flags ...string) { 62 tmp, exe := buildObjdump(t) 63 defer os.RemoveAll(tmp) 64 65 hello := filepath.Join(tmp, "hello.exe") 66 args := []string{"build", "-o", hello} 67 args = append(args, flags...) 68 args = append(args, "testdata/fmthello.go") 69 out, err := exec.Command("go", args...).CombinedOutput() 70 if err != nil { 71 t.Fatalf("go build fmthello.go: %v\n%s", err, out) 72 } 73 need := []string{ 74 "fmthello.go:6", 75 "TEXT main.main(SB)", 76 } 77 switch runtime.GOARCH { 78 case "amd64", "386": 79 need = append(need, x86Need...) 80 case "arm": 81 need = append(need, armNeed...) 82 } 83 84 out, err = exec.Command(exe, "-s", "main.main", hello).CombinedOutput() 85 if err != nil { 86 t.Fatalf("objdump fmthello.exe: %v\n%s", err, out) 87 } 88 89 text := string(out) 90 ok := true 91 for _, s := range need { 92 if !strings.Contains(text, s) { 93 t.Errorf("disassembly missing '%s'", s) 94 ok = false 95 } 96 } 97 if !ok { 98 t.Logf("full disassembly:\n%s", text) 99 } 100 } 101 102 func TestDisasm(t *testing.T) { 103 switch runtime.GOARCH { 104 case "ppc64", "ppc64le": 105 t.Skipf("skipping on %s, issue 9039", runtime.GOARCH) 106 case "arm64": 107 t.Skipf("skipping on %s, issue 10106", runtime.GOARCH) 108 } 109 testDisasm(t) 110 } 111 112 func TestDisasmExtld(t *testing.T) { 113 switch runtime.GOOS { 114 case "plan9", "windows": 115 t.Skipf("skipping on %s", runtime.GOOS) 116 } 117 switch runtime.GOARCH { 118 case "ppc64", "ppc64le": 119 t.Skipf("skipping on %s, no support for external linking, issue 9038", runtime.GOARCH) 120 case "arm64": 121 t.Skipf("skipping on %s, issue 10106", runtime.GOARCH) 122 } 123 // TODO(jsing): Reenable once openbsd/arm has external linking support. 124 if runtime.GOOS == "openbsd" && runtime.GOARCH == "arm" { 125 t.Skip("skipping on openbsd/arm, no support for external linking, issue 10619") 126 } 127 if !build.Default.CgoEnabled { 128 t.Skip("skipping because cgo is not enabled") 129 } 130 testDisasm(t, "-ldflags=-linkmode=external") 131 } 132