File size: 12,679 Bytes
61bba11 | 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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | // 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 main
import (
"bytes"
cmddwarf "cmd/internal/dwarf"
"cmd/internal/objfile"
"cmd/internal/quoted"
"debug/dwarf"
"internal/platform"
"internal/testenv"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"testing"
)
func testDWARF(t *testing.T, buildmode string, expectDWARF bool, env ...string) {
testenv.MustHaveCGO(t)
testenv.MustHaveGoBuild(t)
if !platform.ExecutableHasDWARF(runtime.GOOS, runtime.GOARCH) {
t.Skipf("skipping on %s/%s: no DWARF symbol table in executables", runtime.GOOS, runtime.GOARCH)
}
t.Parallel()
for _, prog := range []string{"testprog", "testprogcgo"} {
prog := prog
expectDWARF := expectDWARF
if runtime.GOOS == "aix" && prog == "testprogcgo" {
extld := os.Getenv("CC")
if extld == "" {
extld = "gcc"
}
extldArgs, err := quoted.Split(extld)
if err != nil {
t.Fatal(err)
}
expectDWARF, err = cmddwarf.IsDWARFEnabledOnAIXLd(extldArgs)
if err != nil {
t.Fatal(err)
}
}
t.Run(prog, func(t *testing.T) {
t.Parallel()
tmpDir := t.TempDir()
exe := filepath.Join(tmpDir, prog+".exe")
dir := "../../runtime/testdata/" + prog
cmd := goCmd(t, "build", "-o", exe)
if buildmode != "" {
cmd.Args = append(cmd.Args, "-buildmode", buildmode)
}
cmd.Args = append(cmd.Args, dir)
cmd.Env = append(cmd.Env, env...)
cmd.Env = append(cmd.Env, "CGO_CFLAGS=") // ensure CGO_CFLAGS does not contain any flags. Issue #35459
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go build -o %v %v: %v\n%s", exe, dir, err, out)
}
if buildmode == "c-archive" {
// Extract the archive and use the go.o object within.
ar := os.Getenv("AR")
if ar == "" {
ar = "ar"
}
cmd := testenv.Command(t, ar, "-x", exe)
cmd.Dir = tmpDir
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("%s -x %s: %v\n%s", ar, exe, err, out)
}
exe = filepath.Join(tmpDir, "go.o")
}
darwinSymbolTestIsTooFlaky := true // Turn this off, it is too flaky -- See #32218
if runtime.GOOS == "darwin" && !darwinSymbolTestIsTooFlaky {
if _, err = exec.LookPath("symbols"); err == nil {
// Ensure Apple's tooling can parse our object for symbols.
out, err = testenv.Command(t, "symbols", exe).CombinedOutput()
if err != nil {
t.Fatalf("symbols %v: %v: %s", filepath.Base(exe), err, out)
} else {
if bytes.HasPrefix(out, []byte("Unable to find file")) {
// This failure will cause the App Store to reject our binaries.
t.Fatalf("symbols %v: failed to parse file", filepath.Base(exe))
} else if bytes.Contains(out, []byte(", Empty]")) {
t.Fatalf("symbols %v: parsed as empty", filepath.Base(exe))
}
}
}
}
f, err := objfile.Open(exe)
if err != nil {
t.Fatal(err)
}
defer f.Close()
syms, err := f.Symbols()
if err != nil {
t.Fatal(err)
}
var addr uint64
for _, sym := range syms {
if sym.Name == "main.main" {
addr = sym.Addr
break
}
}
if addr == 0 {
t.Fatal("cannot find main.main in symbols")
}
d, err := f.DWARF()
if err != nil {
if expectDWARF {
t.Fatal(err)
}
return
} else {
if !expectDWARF {
t.Fatal("unexpected DWARF section")
}
}
// TODO: We'd like to use filepath.Join here.
// Also related: golang.org/issue/19784.
wantFile := path.Join(prog, "main.go")
wantLine := 24
r := d.Reader()
entry, err := r.SeekPC(addr)
if err != nil {
t.Fatal(err)
}
lr, err := d.LineReader(entry)
if err != nil {
t.Fatal(err)
}
var line dwarf.LineEntry
if err := lr.SeekPC(addr, &line); err == dwarf.ErrUnknownPC {
t.Fatalf("did not find file:line for %#x (main.main)", addr)
} else if err != nil {
t.Fatal(err)
}
if !strings.HasSuffix(line.File.Name, wantFile) || line.Line != wantLine {
t.Errorf("%#x is %s:%d, want %s:%d", addr, line.File.Name, line.Line, filepath.Join("...", wantFile), wantLine)
}
if buildmode != "c-archive" {
testModuledata(t, d)
}
})
}
}
// testModuledata makes sure that runtime.firstmoduledata exists
// and has a type. Issue #76731.
func testModuledata(t *testing.T, d *dwarf.Data) {
const symName = "runtime.firstmoduledata"
r := d.Reader()
for {
e, err := r.Next()
if err != nil {
t.Error(err)
return
}
if e == nil {
t.Errorf("did not find DWARF entry for %s", symName)
return
}
switch e.Tag {
case dwarf.TagVariable:
// carry on after switch
case dwarf.TagCompileUnit, dwarf.TagSubprogram:
continue
default:
r.SkipChildren()
continue
}
nameIdx, typeIdx := -1, -1
for i := range e.Field {
f := &e.Field[i]
switch f.Attr {
case dwarf.AttrName:
nameIdx = i
case dwarf.AttrType:
typeIdx = i
}
}
if nameIdx == -1 {
// unnamed variable?
r.SkipChildren()
continue
}
nameStr, ok := e.Field[nameIdx].Val.(string)
if !ok {
// variable name is not a string?
r.SkipChildren()
continue
}
if nameStr != symName {
r.SkipChildren()
continue
}
if typeIdx == -1 {
t.Errorf("%s has no DWARF type", symName)
return
}
off, ok := e.Field[typeIdx].Val.(dwarf.Offset)
if !ok {
t.Errorf("unexpected Go type %T for DWARF type for %s; expected %T", e.Field[typeIdx].Val, symName, dwarf.Offset(0))
return
}
typeInfo, err := d.Type(off)
if err != nil {
t.Error(err)
return
}
typeName := typeInfo.Common().Name
if want := "runtime.moduledata"; typeName != want {
t.Errorf("type of %s is %s, expected %s", symName, typeName, want)
}
for {
typedef, ok := typeInfo.(*dwarf.TypedefType)
if !ok {
break
}
typeInfo = typedef.Type
}
if _, ok := typeInfo.(*dwarf.StructType); !ok {
t.Errorf("type of %s is %T, expected %T", symName, typeInfo, dwarf.StructType{})
}
return
}
}
func TestDWARF(t *testing.T) {
testDWARF(t, "", true)
if !testing.Short() {
if runtime.GOOS == "windows" {
t.Skip("skipping Windows/c-archive; see Issue 35512 for more.")
}
if !platform.BuildModeSupported(runtime.Compiler, "c-archive", runtime.GOOS, runtime.GOARCH) {
t.Skipf("skipping c-archive test on unsupported platform %s-%s", runtime.GOOS, runtime.GOARCH)
}
t.Run("c-archive", func(t *testing.T) {
testDWARF(t, "c-archive", true)
})
}
}
func TestDWARFiOS(t *testing.T) {
// Normally we run TestDWARF on native platform. But on iOS we don't have
// go build, so we do this test with a cross build.
// Only run this on darwin/amd64, where we can cross build for iOS.
if testing.Short() {
t.Skip("skipping in short mode")
}
if runtime.GOARCH != "amd64" || runtime.GOOS != "darwin" {
t.Skip("skipping on non-darwin/amd64 platform")
}
if err := testenv.Command(t, "xcrun", "--help").Run(); err != nil {
t.Skipf("error running xcrun, required for iOS cross build: %v", err)
}
// Check to see if the ios tools are installed. It's possible to have the command line tools
// installed without the iOS sdk.
if output, err := testenv.Command(t, "xcodebuild", "-showsdks").CombinedOutput(); err != nil {
t.Skipf("error running xcodebuild, required for iOS cross build: %v", err)
} else if !strings.Contains(string(output), "iOS SDK") {
t.Skipf("iOS SDK not detected.")
}
cc := "CC=" + runtime.GOROOT() + "/misc/ios/clangwrap.sh"
// iOS doesn't allow unmapped segments, so iOS executables don't have DWARF.
t.Run("exe", func(t *testing.T) {
testDWARF(t, "", false, cc, "CGO_ENABLED=1", "GOOS=ios", "GOARCH=arm64")
})
// However, c-archive iOS objects have embedded DWARF.
t.Run("c-archive", func(t *testing.T) {
testDWARF(t, "c-archive", true, cc, "CGO_ENABLED=1", "GOOS=ios", "GOARCH=arm64")
})
}
// This test ensures that variables promoted to the heap, specifically
// function return parameters, have correct location lists generated.
//
// TODO(deparker): This test is intentionally limited to GOOS=="linux"
// and scoped to net.sendFile, which was the function reported originally in
// issue #65405. There is relevant discussion in https://go-review.googlesource.com/c/go/+/684377
// pertaining to these limitations. There are other missing location lists which must be fixed
// particularly in functions where `linkname` is involved.
func TestDWARFLocationList(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("skipping test on non-linux OS")
}
testenv.MustHaveCGO(t)
testenv.MustHaveGoBuild(t)
if !platform.ExecutableHasDWARF(runtime.GOOS, runtime.GOARCH) {
t.Skipf("skipping on %s/%s: no DWARF symbol table in executables", runtime.GOOS, runtime.GOARCH)
}
t.Parallel()
tmpDir := t.TempDir()
exe := filepath.Join(tmpDir, "issue65405.exe")
dir := "./testdata/dwarf/issue65405"
cmd := goCmd(t, "build", "-gcflags=all=-N -l", "-o", exe, dir)
cmd.Env = append(cmd.Env, "CGO_CFLAGS=")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go build -o %v %v: %v\n%s", exe, dir, err, out)
}
f, err := objfile.Open(exe)
if err != nil {
t.Fatal(err)
}
defer f.Close()
d, err := f.DWARF()
if err != nil {
t.Fatal(err)
}
// Find the net.sendFile function and check its return parameter location list
reader := d.Reader()
for {
entry, err := reader.Next()
if err != nil {
t.Fatal(err)
}
if entry == nil {
break
}
// Look for the net.sendFile subprogram
if entry.Tag == dwarf.TagSubprogram {
fnName, ok := entry.Val(dwarf.AttrName).(string)
if !ok || fnName != "net.sendFile" {
reader.SkipChildren()
continue
}
for {
paramEntry, err := reader.Next()
if err != nil {
t.Fatal(err)
}
if paramEntry == nil || paramEntry.Tag == 0 {
break
}
if paramEntry.Tag == dwarf.TagFormalParameter {
paramName, _ := paramEntry.Val(dwarf.AttrName).(string)
// Check if this parameter has a location attribute
if loc := paramEntry.Val(dwarf.AttrLocation); loc != nil {
switch locData := loc.(type) {
case []byte:
if len(locData) == 0 {
t.Errorf("%s return parameter %q has empty location list", fnName, paramName)
return
}
case int64:
// Location list offset - this means it has a location list
if locData == 0 {
t.Errorf("%s return parameter %q has zero location list offset", fnName, paramName)
return
}
default:
t.Errorf("%s return parameter %q has unexpected location type %T: %v", fnName, paramName, locData, locData)
}
} else {
t.Errorf("%s return parameter %q has no location attribute", fnName, paramName)
}
}
}
}
}
}
func TestFlagW(t *testing.T) {
testenv.MustHaveGoBuild(t)
if runtime.GOOS == "aix" {
t.Skip("internal/xcoff cannot parse file without symbol table")
}
if !platform.ExecutableHasDWARF(runtime.GOOS, runtime.GOARCH) {
t.Skipf("skipping on %s/%s: no DWARF symbol table in executables", runtime.GOOS, runtime.GOARCH)
}
t.Parallel()
tmpdir := t.TempDir()
src := filepath.Join(tmpdir, "a.go")
err := os.WriteFile(src, []byte(helloSrc), 0666)
if err != nil {
t.Fatal(err)
}
type testCase struct {
flag string
wantDWARF bool
}
tests := []testCase{
{"-w", false}, // -w flag disables DWARF
{"-s", false}, // -s implies -w
{"-s -w=0", true}, // -w=0 negates the implied -w
}
if testenv.HasCGO() && runtime.GOOS != "solaris" && runtime.GOOS != "illumos" { // Solaris linker doesn't support the -S flag
tests = append(tests,
testCase{"-w -linkmode=external", false},
testCase{"-s -linkmode=external", false},
// Some external linkers don't have a way to preserve DWARF
// without emitting the symbol table. Skip this case for now.
// I suppose we can post- process, e.g. with objcopy.
//testCase{"-s -w=0 -linkmode=external", true},
)
}
for _, test := range tests {
name := strings.ReplaceAll(test.flag, " ", "_")
t.Run(name, func(t *testing.T) {
ldflags := "-ldflags=" + test.flag
exe := filepath.Join(t.TempDir(), "a.exe")
cmd := goCmd(t, "build", ldflags, "-o", exe, src)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("build failed: %v\n%s", err, out)
}
f, err := objfile.Open(exe)
if err != nil {
t.Fatal(err)
}
defer f.Close()
d, err := f.DWARF()
if test.wantDWARF {
if err != nil {
t.Errorf("want binary with DWARF, got error %v", err)
}
} else {
if d != nil {
t.Errorf("want binary with no DWARF, got DWARF")
}
}
})
}
}
|