Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// 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 (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
)
func loadSyms(t *testing.T) map[string]string {
cmd := exec.Command("go", "tool", "nm", os.Args[0])
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go tool nm %v: %v\n%s", os.Args[0], err, string(out))
}
syms := make(map[string]string)
scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
f := strings.Fields(scanner.Text())
if len(f) < 3 {
continue
}
syms[f[2]] = f[0]
}
if err := scanner.Err(); err != nil {
t.Fatalf("error reading symbols: %v", err)
}
return syms
}
Alex Brainman
committed
func runObjDump(t *testing.T, exe, startaddr, endaddr string) (path, lineno string) {
cmd := exec.Command(exe, os.Args[0], startaddr, endaddr)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go tool objdump %v: %v\n%s", os.Args[0], err, string(out))
}
f := strings.Split(string(out), "\n")
if len(f) < 1 {
t.Fatal("objdump output must have at least one line")
}
pathAndLineNo := f[0]
f = strings.Split(pathAndLineNo, ":")
if runtime.GOOS == "windows" {
switch len(f) {
case 2:
return f[0], f[1]
case 3:
return f[0] + ":" + f[1], f[2]
default:
t.Fatalf("no line number found in %q", pathAndLineNo)
}
}
if len(f) != 2 {
t.Fatalf("no line number found in %q", pathAndLineNo)
}
return f[0], f[1]
}
Alex Brainman
committed
func testObjDump(t *testing.T, exe, startaddr, endaddr string) {
srcPath, srcLineNo := runObjDump(t, exe, startaddr, endaddr)
fi1, err := os.Stat("objdump_test.go")
if err != nil {
t.Fatalf("Stat failed: %v", err)
}
fi2, err := os.Stat(srcPath)
if err != nil {
t.Fatalf("Stat failed: %v", err)
}
if !os.SameFile(fi1, fi2) {
t.Fatalf("objdump_test.go and %s are not same file", srcPath)
}
Alex Brainman
committed
if srcLineNo != "89" {
t.Fatalf("line number = %v; want 89", srcLineNo)
}
}
// This is line 88. The test depends on that.
func TestObjDump(t *testing.T) {
syms := loadSyms(t)
tmp, exe := buildObjdump(t)
defer os.RemoveAll(tmp)
startaddr := syms["cmd/objdump.TestObjDump"]
addr, err := strconv.ParseUint(startaddr, 16, 64)
if err != nil {
t.Fatalf("invalid start address %v: %v", startaddr, err)
}
endaddr := fmt.Sprintf("%x", addr+10)
testObjDump(t, exe, startaddr, endaddr)
testObjDump(t, exe, "0x"+startaddr, "0x"+endaddr)
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
func buildObjdump(t *testing.T) (tmp, exe string) {
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)",
"B main.main(SB)",
"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.
func TestDisasm(t *testing.T) {
if runtime.GOOS == "plan9" {
t.Skip("skipping test; see http://golang.org/issue/7947")
}
tmp, exe := buildObjdump(t)
defer os.RemoveAll(tmp)
hello := filepath.Join(tmp, "hello.exe")
out, err := exec.Command("go", "build", "-o", hello, "testdata/fmthello.go").CombinedOutput()
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...)
t.Skip("disassembler not ready on arm yet")
}
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)
}
}