1 # This test builds a cgo binary and verifies the source directory path
2 # does not appear in the binary, either literally or in compressed DWARF.
3 # TODO(golang.org/issue/36072): ideally we should build a binary from identical
4 # sources in different directories and verify the binary and all intermediate
5 # files are identical.
6
7 [short] skip
8 [!cgo] skip
9
10 # Check that the source path appears when -trimpath is not used.
11 go build -o hello.exe .
12 grep -q gopath[/\\]src hello.exe
13 go run ./list-dwarf hello.exe
14 stdout gopath[/\\]src
15
16 # Check that the source path does not appear when -trimpath is used.
17 [GOOS:aix] stop # can't inspect XCOFF binaries
18 go build -trimpath -o hello.exe .
19 ! grep -q gopath[/\\]src hello.exe
20 go run ./list-dwarf hello.exe
21 ! stdout gopath/src
22
23
24 # Do the above, with the cgo (but not .c) sources in an overlay
25 # Check that the source path appears when -trimpath is not used.
26 mkdir $WORK/overlay
27 cp hello.go $WORK/overlay/hello.go
28 mkdir hello_overlay
29 cp hello.c hello_overlay/hello.c
30 go build -overlay overlay.json -o hello_overlay.exe ./hello_overlay
31 grep -q gopath[/\\]src hello_overlay.exe
32 ! grep -q $WORK[/\\]overlay hello_overlay.exe
33 go run ./list-dwarf hello_overlay.exe
34 stdout gopath[/\\]src
35 ! stdout $WORK[/\\]overlay
36
37 # Check that the source path does not appear when -trimpath is used.
38 go build -overlay overlay.json -trimpath -o hello_overlay.exe ./hello_overlay
39 ! grep -q gopath[/\\]src hello_overlay.exe
40 ! grep -q $WORK[/\\]overlay hello_overlay.exe
41 go run ./list-dwarf hello_overlay.exe
42 ! stdout gopath/src
43 ! stdout $WORK[/\\]overlay
44
45 -- go.mod --
46 module m
47
48 go 1.14
49 -- overlay.json --
50 {
51 "Replace": {
52 "hello_overlay/hello.go": "../../overlay/hello.go"
53 }
54 }
55 -- hello.c --
56 #include <stdio.h>
57
58 void say_hello() { puts("Hello, world!\n"); }
59
60 -- hello.go --
61 package main
62
63 // void say_hello();
64 import "C"
65
66 func main() {
67 C.say_hello()
68 }
69
70 -- list-dwarf/list-dwarf.go --
71 package main
72
73 import (
74 "debug/dwarf"
75 "fmt"
76 "io"
77 "log"
78 "os"
79 "sort"
80 )
81
82 func main() {
83 files, err := run(os.Args[1])
84 if err != nil {
85 log.Fatal(err)
86 }
87 for _, file := range files {
88 fmt.Println(file)
89 }
90 }
91
92 func run(exePath string) ([]string, error) {
93 dwarfData, err := readDWARF(exePath)
94 if err != nil {
95 return nil, err
96 }
97
98 dwarfReader := dwarfData.Reader()
99 files := make(map[string]bool)
100 for {
101 e, err := dwarfReader.Next()
102 if err != nil {
103 return nil, err
104 }
105 if e == nil {
106 break
107 }
108 lr, err := dwarfData.LineReader(e)
109 if err != nil {
110 return nil, err
111 }
112 if lr == nil {
113 continue
114 }
115
116 var le dwarf.LineEntry
117 for {
118 if err := lr.Next(&le); err != nil {
119 if err == io.EOF {
120 break
121 }
122 return nil, err
123 }
124 if le.EndSequence {
125 continue
126 }
127 files[le.File.Name] = true
128 }
129 }
130
131 sortedFiles := make([]string, 0, len(files))
132 for file := range files {
133 sortedFiles = append(sortedFiles, file)
134 }
135 sort.Strings(sortedFiles)
136 return sortedFiles, nil
137 }
138 -- list-dwarf/read_darwin.go --
139 package main
140
141 import (
142 "debug/dwarf"
143 "debug/macho"
144 )
145
146 func readDWARF(exePath string) (*dwarf.Data, error) {
147 machoFile, err := macho.Open(exePath)
148 if err != nil {
149 return nil, err
150 }
151 defer machoFile.Close()
152 return machoFile.DWARF()
153 }
154 -- list-dwarf/read_elf.go --
155 // +build android dragonfly freebsd illumos linux netbsd openbsd solaris
156
157 package main
158
159 import (
160 "debug/dwarf"
161 "debug/elf"
162 )
163
164 func readDWARF(exePath string) (*dwarf.Data, error) {
165 elfFile, err := elf.Open(exePath)
166 if err != nil {
167 return nil, err
168 }
169 defer elfFile.Close()
170 return elfFile.DWARF()
171 }
172 -- list-dwarf/read_windows.go --
173 package main
174
175 import (
176 "debug/dwarf"
177 "debug/pe"
178 )
179
180 func readDWARF(exePath string) (*dwarf.Data, error) {
181 peFile, err := pe.Open(exePath)
182 if err != nil {
183 return nil, err
184 }
185 defer peFile.Close()
186 return peFile.DWARF()
187 }
188
View as plain text