Skip to content

Commit 9c83383

Browse files
aarzilliheschi
authored andcommitted
cmd/link: move dwarf part of DWARF generation before type name mangling
Splits part of dwarfgeneratedebugsyms into a new function, dwarfGenerateDebugInfo which is called between deadcode elimination and type name mangling. This function takes care of collecting and processing the DIEs for all functions and package-level variables and also generates DIEs for all types used in the program. Fixes #23733 Change-Id: I75ef0608fbed2dffc3be7a477f1b03e7e740ec61 Reviewed-on: https://go-review.googlesource.com/111237 Run-TryBot: Heschi Kreinick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 669fa8f commit 9c83383

File tree

9 files changed

+276
-164
lines changed

9 files changed

+276
-164
lines changed
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Usage:
6+
//
7+
// checkdwarf <exe> <suffix>
8+
//
9+
// Opens <exe>, which must be an executable or a library and checks that
10+
// there is an entry in .debug_info whose name ends in <suffix>
11+
12+
package main
13+
14+
import (
15+
"debug/dwarf"
16+
"debug/elf"
17+
"debug/macho"
18+
"debug/pe"
19+
"fmt"
20+
"os"
21+
"strings"
22+
)
23+
24+
func usage() {
25+
fmt.Fprintf(os.Stderr, "checkdwarf executable-or-library DIE-suffix\n")
26+
}
27+
28+
type dwarfer interface {
29+
DWARF() (*dwarf.Data, error)
30+
}
31+
32+
func openElf(path string) dwarfer {
33+
exe, err := elf.Open(path)
34+
if err != nil {
35+
return nil
36+
}
37+
return exe
38+
}
39+
40+
func openMacho(path string) dwarfer {
41+
exe, err := macho.Open(path)
42+
if err != nil {
43+
return nil
44+
}
45+
return exe
46+
}
47+
48+
func openPE(path string) dwarfer {
49+
exe, err := pe.Open(path)
50+
if err != nil {
51+
return nil
52+
}
53+
return exe
54+
}
55+
56+
func main() {
57+
if len(os.Args) != 3 {
58+
usage()
59+
}
60+
61+
exePath := os.Args[1]
62+
dieSuffix := os.Args[2]
63+
64+
var exe dwarfer
65+
66+
for _, openfn := range []func(string) dwarfer{openMacho, openPE, openElf} {
67+
exe = openfn(exePath)
68+
if exe != nil {
69+
break
70+
}
71+
}
72+
73+
if exe == nil {
74+
fmt.Fprintf(os.Stderr, "could not open %s", exePath)
75+
os.Exit(1)
76+
}
77+
78+
data, err := exe.DWARF()
79+
if err != nil {
80+
fmt.Fprintf(os.Stderr, "error opening DWARF: %v", err)
81+
os.Exit(1)
82+
}
83+
84+
rdr := data.Reader()
85+
for {
86+
e, err := rdr.Next()
87+
if err != nil {
88+
fmt.Fprintf(os.Stderr, "error reading DWARF: %v", err)
89+
os.Exit(1)
90+
}
91+
if e == nil {
92+
break
93+
}
94+
name, hasname := e.Val(dwarf.AttrName).(string)
95+
if !hasname {
96+
continue
97+
}
98+
if strings.HasSuffix(name, dieSuffix) {
99+
// found
100+
os.Exit(0)
101+
}
102+
}
103+
104+
fmt.Fprintf(os.Stderr, "no entry with a name ending in %q was found", dieSuffix)
105+
os.Exit(1)
106+
}

misc/cgo/testplugin/test.bash

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" -buildmode=plugin -o=unnamed1.so u
3232
GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" -buildmode=plugin -o=unnamed2.so unnamed2/main.go
3333
GOPATH=$(pwd) go build -gcflags "$GO_GCFLAGS" host
3434

35+
# test that DWARF sections are emitted for plugins and programs importing "plugin"
36+
go run src/checkdwarf/main.go plugin2.so plugin2.UnexportedNameReuse
37+
go run src/checkdwarf/main.go host main.main
38+
3539
LD_LIBRARY_PATH=$(pwd) ./host
3640

3741
# Test that types and itabs get properly uniqified.

src/cmd/link/internal/ld/data.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,7 @@ func (ctxt *Link) dodata() {
16021602
datap = append(datap, data[symn]...)
16031603
}
16041604

1605-
dwarfgeneratedebugsyms(ctxt)
1605+
dwarfGenerateDebugSyms(ctxt)
16061606

16071607
var i int
16081608
for ; i < len(dwarfp); i++ {

0 commit comments

Comments
 (0)