aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/testdata/script/build_trimpath_cgo.txt
blob: 3187b4d6439995e7a81aea198ae4d0a4d3f2a8d7 (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
# This test builds a cgo binary and verifies the source directory path
# does not appear in the binary, either literally or in compressed DWARF.
# TODO(golang.org/issue/36072): ideally we should build a binary from identical
# sources in different directories and verify the binary and all intermediate
# files are identical.

[short] skip
[!cgo] skip

# Check that the source path appears when -trimpath is not used.
go build -o hello.exe .
grep -q gopath[/\\]src hello.exe
go run ./list-dwarf hello.exe
stdout gopath[/\\]src

# Check that the source path does not appear when -trimpath is used.
[aix] stop # can't inspect XCOFF binaries
go build -trimpath -o hello.exe .
! grep -q gopath[/\\]src hello.exe
go run ./list-dwarf hello.exe
! stdout gopath/src


# Do the above, with the cgo (but not .c) sources in an overlay
# Check that the source path appears when -trimpath is not used.
mkdir $WORK/overlay
cp hello.go $WORK/overlay/hello.go
mkdir hello_overlay
cp hello.c hello_overlay/hello.c
go build -overlay overlay.json -o hello_overlay.exe ./hello_overlay
grep -q gopath[/\\]src hello_overlay.exe
! grep -q $WORK[/\\]overlay hello_overlay.exe
go run ./list-dwarf hello_overlay.exe
stdout gopath[/\\]src
! stdout $WORK[/\\]overlay

# Check that the source path does not appear when -trimpath is used.
go build -overlay overlay.json -trimpath -o hello_overlay.exe ./hello_overlay
! grep -q gopath[/\\]src hello_overlay.exe
! grep -q $WORK[/\\]overlay hello_overlay.exe
go run ./list-dwarf hello_overlay.exe
! stdout gopath/src
! stdout $WORK[/\\]overlay

-- go.mod --
module m

go 1.14
-- overlay.json --
{
	"Replace": {
		"hello_overlay/hello.go": "../../overlay/hello.go"
	}
}
-- hello.c --
#include <stdio.h>

void say_hello() { puts("Hello, world!\n"); }

-- hello.go --
package main

// void say_hello();
import "C"

func main() {
	C.say_hello()
}

-- list-dwarf/list-dwarf.go --
package main

import (
	"debug/dwarf"
	"fmt"
	"io"
	"log"
	"os"
	"sort"
)

func main() {
	files, err := run(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	for _, file := range files {
		fmt.Println(file)
	}
}

func run(exePath string) ([]string, error) {
	dwarfData, err := readDWARF(exePath)
	if err != nil {
		return nil, err
	}

	dwarfReader := dwarfData.Reader()
	files := make(map[string]bool)
	for {
		e, err := dwarfReader.Next()
		if err != nil {
			return nil, err
		}
		if e == nil {
			break
		}
		lr, err := dwarfData.LineReader(e)
		if err != nil {
			return nil, err
		}
		if lr == nil {
			continue
		}

		var le dwarf.LineEntry
		for {
			if err := lr.Next(&le); err != nil {
				if err == io.EOF {
					break
				}
				return nil, err
			}
			files[le.File.Name] = true
		}
	}

	sortedFiles := make([]string, 0, len(files))
	for file := range files {
		sortedFiles = append(sortedFiles, file)
	}
	sort.Strings(sortedFiles)
	return sortedFiles, nil
}
-- list-dwarf/read_darwin.go --
package main

import (
	"debug/dwarf"
	"debug/macho"
)

func readDWARF(exePath string) (*dwarf.Data, error) {
	machoFile, err := macho.Open(exePath)
	if err != nil {
		return nil, err
	}
	defer machoFile.Close()
	return machoFile.DWARF()
}
-- list-dwarf/read_elf.go --
// +build android dragonfly freebsd illumos linux netbsd openbsd solaris

package main

import (
	"debug/dwarf"
	"debug/elf"
)

func readDWARF(exePath string) (*dwarf.Data, error) {
	elfFile, err := elf.Open(exePath)
	if err != nil {
		return nil, err
	}
	defer elfFile.Close()
	return elfFile.DWARF()
}
-- list-dwarf/read_windows.go --
package main

import (
	"debug/dwarf"
	"debug/pe"
)

func readDWARF(exePath string) (*dwarf.Data, error) {
	peFile, err := pe.Open(exePath)
	if err != nil {
		return nil, err
	}
	defer peFile.Close()
	return peFile.DWARF()
}