aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/nm/nm_test.go
blob: 382446e9fe1bea01e42368845e783715d5d46509 (plain)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// 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 (
	"fmt"
	"internal/obscuretestdata"
	"internal/testenv"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
	"testing"
	"text/template"
)

var testnmpath string // path to nm command created for testing purposes

// The TestMain function creates a nm command for testing purposes and
// deletes it after the tests have been run.
func TestMain(m *testing.M) {
	os.Exit(testMain(m))
}

func testMain(m *testing.M) int {
	if !testenv.HasGoBuild() {
		return 0
	}

	tmpDir, err := ioutil.TempDir("", "TestNM")
	if err != nil {
		fmt.Println("TempDir failed:", err)
		return 2
	}
	defer os.RemoveAll(tmpDir)

	testnmpath = filepath.Join(tmpDir, "testnm.exe")
	gotool, err := testenv.GoTool()
	if err != nil {
		fmt.Println("GoTool failed:", err)
		return 2
	}
	out, err := exec.Command(gotool, "build", "-o", testnmpath, "cmd/nm").CombinedOutput()
	if err != nil {
		fmt.Printf("go build -o %v cmd/nm: %v\n%s", testnmpath, err, string(out))
		return 2
	}

	return m.Run()
}

func TestNonGoExecs(t *testing.T) {
	t.Parallel()
	testfiles := []string{
		"debug/elf/testdata/gcc-386-freebsd-exec",
		"debug/elf/testdata/gcc-amd64-linux-exec",
		"debug/macho/testdata/gcc-386-darwin-exec.base64",   // golang.org/issue/34986
		"debug/macho/testdata/gcc-amd64-darwin-exec.base64", // golang.org/issue/34986
		// "debug/pe/testdata/gcc-amd64-mingw-exec", // no symbols!
		"debug/pe/testdata/gcc-386-mingw-exec",
		"debug/plan9obj/testdata/amd64-plan9-exec",
		"debug/plan9obj/testdata/386-plan9-exec",
		"internal/xcoff/testdata/gcc-ppc64-aix-dwarf2-exec",
	}
	for _, f := range testfiles {
		exepath := filepath.Join(runtime.GOROOT(), "src", f)
		if strings.HasSuffix(f, ".base64") {
			tf, err := obscuretestdata.DecodeToTempFile(exepath)
			if err != nil {
				t.Errorf("obscuretestdata.DecodeToTempFile(%s): %v", exepath, err)
				continue
			}
			defer os.Remove(tf)
			exepath = tf
		}

		cmd := exec.Command(testnmpath, exepath)
		out, err := cmd.CombinedOutput()
		if err != nil {
			t.Errorf("go tool nm %v: %v\n%s", exepath, err, string(out))
		}
	}
}

func testGoExec(t *testing.T, iscgo, isexternallinker bool) {
	t.Parallel()
	tmpdir, err := ioutil.TempDir("", "TestGoExec")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpdir)

	src := filepath.Join(tmpdir, "a.go")
	file, err := os.Create(src)
	if err != nil {
		t.Fatal(err)
	}
	err = template.Must(template.New("main").Parse(testexec)).Execute(file, iscgo)
	if e := file.Close(); err == nil {
		err = e
	}
	if err != nil {
		t.Fatal(err)
	}

	exe := filepath.Join(tmpdir, "a.exe")
	args := []string{"build", "-o", exe}
	if iscgo {
		linkmode := "internal"
		if isexternallinker {
			linkmode = "external"
		}
		args = append(args, "-ldflags", "-linkmode="+linkmode)
	}
	args = append(args, src)
	out, err := exec.Command(testenv.GoToolPath(t), args...).CombinedOutput()
	if err != nil {
		t.Fatalf("building test executable failed: %s %s", err, out)
	}

	out, err = exec.Command(exe).CombinedOutput()
	if err != nil {
		t.Fatalf("running test executable failed: %s %s", err, out)
	}
	names := make(map[string]string)
	for _, line := range strings.Split(string(out), "\n") {
		if line == "" {
			continue
		}
		f := strings.Split(line, "=")
		if len(f) != 2 {
			t.Fatalf("unexpected output line: %q", line)
		}
		names["main."+f[0]] = f[1]
	}

	runtimeSyms := map[string]string{
		"runtime.text":      "T",
		"runtime.etext":     "T",
		"runtime.rodata":    "R",
		"runtime.erodata":   "R",
		"runtime.epclntab":  "R",
		"runtime.noptrdata": "D",
	}

	if runtime.GOOS == "aix" && iscgo {
		// pclntab is moved to .data section on AIX.
		runtimeSyms["runtime.epclntab"] = "D"
	}

	out, err = exec.Command(testnmpath, exe).CombinedOutput()
	if err != nil {
		t.Fatalf("go tool nm: %v\n%s", err, string(out))
	}

	relocated := func(code string) bool {
		if runtime.GOOS == "aix" {
			// On AIX, .data and .bss addresses are changed by the loader.
			// Therefore, the values returned by the exec aren't the same
			// than the ones inside the symbol table.
			// In case of cgo, .text symbols are also changed.
			switch code {
			case "T", "t", "R", "r":
				return iscgo
			case "D", "d", "B", "b":
				return true
			}
		}
		if runtime.GOOS == "windows" {
			return true
		}
		if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
			return true // On darwin/arm64 everything is PIE
		}
		return false
	}

	dups := make(map[string]bool)
	for _, line := range strings.Split(string(out), "\n") {
		f := strings.Fields(line)
		if len(f) < 3 {
			continue
		}
		name := f[2]
		if addr, found := names[name]; found {
			if want, have := addr, "0x"+f[0]; have != want {
				if !relocated(f[1]) {
					t.Errorf("want %s address for %s symbol, but have %s", want, name, have)
				}
			}
			delete(names, name)
		}
		if _, found := dups[name]; found {
			t.Errorf("duplicate name of %q is found", name)
		}
		if stype, found := runtimeSyms[name]; found {
			if runtime.GOOS == "plan9" && stype == "R" {
				// no read-only data segment symbol on Plan 9
				stype = "D"
			}
			if want, have := stype, strings.ToUpper(f[1]); have != want {
				t.Errorf("want %s type for %s symbol, but have %s", want, name, have)
			}
			delete(runtimeSyms, name)
		}
	}
	if len(names) > 0 {
		t.Errorf("executable is missing %v symbols", names)
	}
	if len(runtimeSyms) > 0 {
		t.Errorf("executable is missing %v symbols", runtimeSyms)
	}
}

func TestGoExec(t *testing.T) {
	testGoExec(t, false, false)
}

func testGoLib(t *testing.T, iscgo bool) {
	t.Parallel()
	tmpdir, err := ioutil.TempDir("", "TestGoLib")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpdir)

	gopath := filepath.Join(tmpdir, "gopath")
	libpath := filepath.Join(gopath, "src", "mylib")

	err = os.MkdirAll(libpath, 0777)
	if err != nil {
		t.Fatal(err)
	}
	src := filepath.Join(libpath, "a.go")
	file, err := os.Create(src)
	if err != nil {
		t.Fatal(err)
	}
	err = template.Must(template.New("mylib").Parse(testlib)).Execute(file, iscgo)
	if e := file.Close(); err == nil {
		err = e
	}
	if err == nil {
		err = ioutil.WriteFile(filepath.Join(libpath, "go.mod"), []byte("module mylib\n"), 0666)
	}
	if err != nil {
		t.Fatal(err)
	}

	args := []string{"install", "mylib"}
	cmd := exec.Command(testenv.GoToolPath(t), args...)
	cmd.Dir = libpath
	cmd.Env = append(os.Environ(), "GOPATH="+gopath)
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatalf("building test lib failed: %s %s", err, out)
	}
	pat := filepath.Join(gopath, "pkg", "*", "mylib.a")
	ms, err := filepath.Glob(pat)
	if err != nil {
		t.Fatal(err)
	}
	if len(ms) == 0 {
		t.Fatalf("cannot found paths for pattern %s", pat)
	}
	mylib := ms[0]

	out, err = exec.Command(testnmpath, mylib).CombinedOutput()
	if err != nil {
		t.Fatalf("go tool nm: %v\n%s", err, string(out))
	}
	type symType struct {
		Type  string
		Name  string
		CSym  bool
		Found bool
	}
	var syms = []symType{
		{"B", "mylib.Testdata", false, false},
		{"T", "mylib.Testfunc", false, false},
	}
	if iscgo {
		syms = append(syms, symType{"B", "mylib.TestCgodata", false, false})
		syms = append(syms, symType{"T", "mylib.TestCgofunc", false, false})
		if runtime.GOOS == "darwin" || runtime.GOOS == "ios" || (runtime.GOOS == "windows" && runtime.GOARCH == "386") {
			syms = append(syms, symType{"D", "_cgodata", true, false})
			syms = append(syms, symType{"T", "_cgofunc", true, false})
		} else if runtime.GOOS == "aix" {
			syms = append(syms, symType{"D", "cgodata", true, false})
			syms = append(syms, symType{"T", ".cgofunc", true, false})
		} else {
			syms = append(syms, symType{"D", "cgodata", true, false})
			syms = append(syms, symType{"T", "cgofunc", true, false})
		}
	}

	for _, line := range strings.Split(string(out), "\n") {
		f := strings.Fields(line)
		var typ, name string
		var csym bool
		if iscgo {
			if len(f) < 4 {
				continue
			}
			csym = !strings.Contains(f[0], "_go_.o")
			typ = f[2]
			name = f[3]
		} else {
			if len(f) < 3 {
				continue
			}
			typ = f[1]
			name = f[2]
		}
		for i := range syms {
			sym := &syms[i]
			if sym.Type == typ && sym.Name == name && sym.CSym == csym {
				if sym.Found {
					t.Fatalf("duplicate symbol %s %s", sym.Type, sym.Name)
				}
				sym.Found = true
			}
		}
	}
	for _, sym := range syms {
		if !sym.Found {
			t.Errorf("cannot found symbol %s %s", sym.Type, sym.Name)
		}
	}
}

func TestGoLib(t *testing.T) {
	testGoLib(t, false)
}

const testexec = `
package main

import "fmt"
{{if .}}import "C"
{{end}}

func main() {
	testfunc()
}

var testdata uint32

func testfunc() {
	fmt.Printf("main=%p\n", main)
	fmt.Printf("testfunc=%p\n", testfunc)
	fmt.Printf("testdata=%p\n", &testdata)
}
`

const testlib = `
package mylib

{{if .}}
// int cgodata = 5;
// void cgofunc(void) {}
import "C"

var TestCgodata = C.cgodata

func TestCgofunc() {
	C.cgofunc()
}
{{end}}

var Testdata uint32

func Testfunc() {}
`