blob: 7ce1e0c30bf2147d35382f672f72da74d11e3901 [file] [log] [blame]
// Copyright 2017 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 ld
import (
objfilepkg "cmd/internal/objfile" // renamed to avoid conflict with objfile function
"debug/dwarf"
"internal/testenv"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
)
func TestRuntimeTypeDIEs(t *testing.T) {
testenv.MustHaveGoBuild(t)
if runtime.GOOS == "plan9" {
t.Skip("skipping on plan9; no DWARF symbol table in executables")
}
dir, err := ioutil.TempDir("", "TestRuntimeTypeDIEs")
if err != nil {
t.Fatalf("could not create directory: %v", err)
}
defer os.RemoveAll(dir)
f := gobuild(t, dir, `package main; func main() { }`)
defer f.Close()
dwarf, err := f.DWARF()
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
want := map[string]bool{
"runtime._type": true,
"runtime.arraytype": true,
"runtime.chantype": true,
"runtime.functype": true,
"runtime.maptype": true,
"runtime.ptrtype": true,
"runtime.slicetype": true,
"runtime.structtype": true,
"runtime.interfacetype": true,
"runtime.itab": true,
"runtime.imethod": true,
}
found := findTypes(t, dwarf, want)
if len(found) != len(want) {
t.Errorf("found %v, want %v", found, want)
}
}
func findTypes(t *testing.T, dw *dwarf.Data, want map[string]bool) (found map[string]bool) {
found = make(map[string]bool)
rdr := dw.Reader()
for entry, err := rdr.Next(); entry != nil; entry, err = rdr.Next() {
if err != nil {
t.Fatalf("error reading DWARF: %v", err)
}
switch entry.Tag {
case dwarf.TagTypedef:
if name, ok := entry.Val(dwarf.AttrName).(string); ok && want[name] {
found[name] = true
}
}
}
return
}
func gobuild(t *testing.T, dir string, testfile string) *objfilepkg.File {
src := filepath.Join(dir, "test.go")
dst := filepath.Join(dir, "out")
if err := ioutil.WriteFile(src, []byte(testfile), 0666); err != nil {
t.Fatal(err)
}
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", dst, src)
if b, err := cmd.CombinedOutput(); err != nil {
t.Logf("build: %s\n", b)
t.Fatalf("build error: %v", err)
}
f, err := objfilepkg.Open(dst)
if err != nil {
t.Fatal(err)
}
return f
}