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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
// 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 (
"crypto/md5"
"flag"
"fmt"
"go/build"
"internal/testenv"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
var tmp, exe string // populated by buildObjdump
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 = os.MkdirTemp("", "TestObjDump")
if err != nil {
return fmt.Errorf("TempDir failed: %v", err)
}
exe = filepath.Join(tmp, "testobjdump.exe")
gotool, err := testenv.GoTool()
if err != nil {
return err
}
out, err := exec.Command(gotool, "build", "-o", exe, "cmd/objdump").CombinedOutput()
if err != nil {
os.RemoveAll(tmp)
return fmt.Errorf("go build -o %v cmd/objdump: %v\n%s", exe, err, string(out))
}
return nil
}
var x86Need = []string{ // for both 386 and AMD64
"JMP main.main(SB)",
"CALL main.Println(SB)",
"RET",
}
var amd64GnuNeed = []string{
"movq",
"callq",
"cmpb",
}
var i386GnuNeed = []string{
"mov",
"call",
"cmp",
}
var armNeed = []string{
"B main.main(SB)",
"BL main.Println(SB)",
"RET",
}
var arm64Need = []string{
"JMP main.main(SB)",
"CALL main.Println(SB)",
"RET",
}
var armGnuNeed = []string{ // for both ARM and AMR64
"ldr",
"bl",
"cmp",
}
var ppcNeed = []string{
"BR main.main(SB)",
"CALL main.Println(SB)",
"RET",
}
var ppcGnuNeed = []string{
"mflr",
"lbz",
"cmpw",
}
func mustHaveDisasm(t *testing.T) {
switch runtime.GOARCH {
case "mips", "mipsle", "mips64", "mips64le":
t.Skipf("skipping on %s, issue 12559", runtime.GOARCH)
case "riscv64":
t.Skipf("skipping on %s, issue 36738", runtime.GOARCH)
case "s390x":
t.Skipf("skipping on %s, issue 15255", runtime.GOARCH)
}
}
var target = flag.String("target", "", "test disassembly of `goos/goarch` binary")
// 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, srcfname string, printCode bool, printGnuAsm bool, flags ...string) {
mustHaveDisasm(t)
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]
}
hash := md5.Sum([]byte(fmt.Sprintf("%v-%v-%v-%v", srcfname, flags, printCode, printGnuAsm)))
hello := filepath.Join(tmp, fmt.Sprintf("hello-%x.exe", hash))
args := []string{"build", "-o", hello}
args = append(args, flags...)
args = append(args, srcfname)
cmd := exec.Command(testenv.GoToolPath(t), args...)
// "Bad line" bug #36683 is sensitive to being run in the source directory.
cmd.Dir = "testdata"
// Ensure that the source file location embedded in the binary matches our
// actual current GOROOT, instead of GOROOT_FINAL if set.
cmd.Env = append(os.Environ(), "GOROOT_FINAL=")
t.Logf("Running %v", cmd.Args)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go build %s: %v\n%s", srcfname, err, out)
}
need := []string{
"TEXT main.main(SB)",
}
if printCode {
need = append(need, ` Println("hello, world")`)
} else {
need = append(need, srcfname+":6")
}
switch goarch {
case "amd64", "386":
need = append(need, x86Need...)
case "arm":
need = append(need, armNeed...)
case "arm64":
need = append(need, arm64Need...)
case "ppc64", "ppc64le":
need = append(need, ppcNeed...)
}
if printGnuAsm {
switch goarch {
case "amd64":
need = append(need, amd64GnuNeed...)
case "386":
need = append(need, i386GnuNeed...)
case "arm", "arm64":
need = append(need, armGnuNeed...)
case "ppc64", "ppc64le":
need = append(need, ppcGnuNeed...)
}
}
args = []string{
"-s", "main.main",
hello,
}
if printCode {
args = append([]string{"-S"}, args...)
}
if printGnuAsm {
args = append([]string{"-gnu"}, args...)
}
cmd = exec.Command(exe, args...)
cmd.Dir = "testdata" // "Bad line" bug #36683 is sensitive to being run in the source directory
out, err = cmd.CombinedOutput()
t.Logf("Running %v", cmd.Args)
if err != nil {
exename := srcfname[:len(srcfname)-len(filepath.Ext(srcfname))] + ".exe"
t.Fatalf("objdump %q: %v\n%s", exename, 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 goarch == "386" {
if strings.Contains(text, "(IP)") {
t.Errorf("disassembly contains PC-Relative addressing on 386")
ok = false
}
}
if !ok {
t.Logf("full disassembly:\n%s", text)
}
}
func testGoAndCgoDisasm(t *testing.T, printCode bool, printGnuAsm bool) {
t.Parallel()
testDisasm(t, "fmthello.go", printCode, printGnuAsm)
if build.Default.CgoEnabled {
testDisasm(t, "fmthellocgo.go", printCode, printGnuAsm)
}
}
func TestDisasm(t *testing.T) {
testGoAndCgoDisasm(t, false, false)
}
func TestDisasmCode(t *testing.T) {
testGoAndCgoDisasm(t, true, false)
}
func TestDisasmGnuAsm(t *testing.T) {
testGoAndCgoDisasm(t, false, true)
}
func TestDisasmExtld(t *testing.T) {
testenv.MustHaveCGO(t)
switch runtime.GOOS {
case "plan9", "windows":
t.Skipf("skipping on %s", runtime.GOOS)
}
t.Parallel()
testDisasm(t, "fmthello.go", false, false, "-ldflags=-linkmode=external")
}
func TestDisasmGoobj(t *testing.T) {
mustHaveDisasm(t)
hello := filepath.Join(tmp, "hello.o")
args := []string{"tool", "compile", "-o", hello}
args = append(args, "testdata/fmthello.go")
out, err := exec.Command(testenv.GoToolPath(t), args...).CombinedOutput()
if err != nil {
t.Fatalf("go tool compile fmthello.go: %v\n%s", err, out)
}
need := []string{
"main(SB)",
"fmthello.go:6",
}
args = []string{
"-s", "main",
hello,
}
out, err = exec.Command(exe, args...).CombinedOutput()
if err != nil {
t.Fatalf("objdump fmthello.o: %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 runtime.GOARCH == "386" {
if strings.Contains(text, "(IP)") {
t.Errorf("disassembly contains PC-Relative addressing on 386")
ok = false
}
}
if !ok {
t.Logf("full disassembly:\n%s", text)
}
}
func TestGoobjFileNumber(t *testing.T) {
// Test that file table in Go object file is parsed correctly.
testenv.MustHaveGoBuild(t)
mustHaveDisasm(t)
t.Parallel()
tmpdir, err := os.MkdirTemp("", "TestGoobjFileNumber")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
obj := filepath.Join(tmpdir, "p.a")
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", obj)
cmd.Dir = filepath.Join("testdata/testfilenum")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("build failed: %v\n%s", err, out)
}
cmd = exec.Command(exe, obj)
out, err = cmd.CombinedOutput()
if err != nil {
t.Fatalf("objdump failed: %v\n%s", err, out)
}
text := string(out)
for _, s := range []string{"a.go", "b.go", "c.go"} {
if !strings.Contains(text, s) {
t.Errorf("output missing '%s'", s)
}
}
if t.Failed() {
t.Logf("output:\n%s", text)
}
}
|