Repository: go
Issue: cmd/go: Windows -buildmode=pie fails to execute program that has CGo bindings compiled without PIE
Labels: OS-Windows,NeedsInvestigation

It is mentioned in https://github.com/golang/go/issues/35192 that people might be interested in weird bugs that pop up using `-buildmode=pie` by default so I submitted this.

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.15.2 windows/amd64
</pre>

### Does this issue reproduce with the latest release?

Yes, because `-buildmode=pie` is default for Windows in go1.15. The issue does not present if `-buildmode=exe` is used.

### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\zacks\AppData\Local\go-build
set GOENV=C:\Users\zacks\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\zacks\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\zacks\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\zacks\scoop\apps\go\current
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Users\zacks\scoop\apps\go\current\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\zacks\go\src\github.com\schollz\miti\go.mod
set CGO_CFLAGS=-IC:\msys64\mingw64\include
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-LC:\msys64\mingw64\lib
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\zacks\AppData\Local\Temp\go-build521225298=/tmp/go-build -gno-record-gcc-switches
</pre></details>

### What did you do?

First I installed [portmidi for Windows with these instructions](https://schollz.com/blog/portmidi/). Then I cloned and ran a test with go1.15.2:

```
git clone https://github.com/schollz/miti
cd miti/src/midi
$env:CGO_CFLAGS="-IC:\msys64\mingw64\include"
$env:CGO_LDFLAGS="-LC:\msys64\mingw64\lib"
go test -v
```

### What did you expect to see?

I expected to see that the default `go test` works without any extra build flags, as it did in go1.14.9:

```
> go1.14.9 test -v
=== RUN   TestMIDI
[debug] 10:14:34 midi.go:38: found 2 devices
[debug] 10:14:34 midi.go:44: device 0: 'Microsoft MIDI Mapper', i/o: false/true
[debug] 10:14:34 midi.go:44: device 1: 'Microsoft GS Wavetable Synth', i/o: false/true
[debug] 10:14:34 midi.go:58: [Microsoft MIDI Mapper] opening stream with latency 2000
--- PASS: TestMIDI (0.02s)
=== RUN   TestIn
[debug] 10:14:34 midiin.go:27: found 0 devices
[trace] 10:14:34.012062 midiin.go:31: di: &{Interface:MMSystem Name:Microsoft MIDI Mapper IsInputAvailable:false IsOutputAvailable:true IsOpened:false}
[trace] 10:14:34.012906 midiin.go:31: di: &{Interface:MMSystem Name:Microsoft GS Wavetable Synth IsInputAvailable:false IsOutputAvailable:true IsOpened:false}
[trace] 10:14:34.013423 midiin.go:33: skipping Microsoft GS Wavetable Synth
[debug] 10:14:35 midi.go:63: [Microsoft MIDI Mapper] opened stream
[trace] 10:14:35.014415 midi.go:68: [Microsoft MIDI Mapper] waiting for chord
[debug] 10:14:36 midi_test.go:31: sending finish
[debug] 10:14:36 midi_test.go:33: sent
--- PASS: TestIn (2.10s)
PASS
ok      github.com/schollz/miti/src/midi        2.640s
```


### What did you see instead?

Instead I got this runtime error when using go1.15.2

```
> go test -v
=== RUN   TestMIDI
[debug]	10:16:53 midi.go:38: found 2 devices
[debug]	10:16:53 midi.go:44: device 0: 'Microsoft MIDI Mapper', i/o: false/true
[debug]	10:16:53 midi.go:44: device 1: 'Microsoft GS Wavetable Synth', i/o: false/true
--- PASS: TestMIDI (0.02s)
=== RUN   TestIn
[debug]	10:16:53 midi.go:58: [Microsoft MIDI Mapper] opening stream with latency 2000
[debug]	10:16:53 midiin.go:27: found 0 devices
[trace]	10:16:53.218101 midiin.go:31: di: &{Interface:MMSystem Name:Microsoft MIDI Mapper IsInputAvailable:false IsOutputAvailable:true IsOpened:false}
[trace]	10:16:53.218101 midiin.go:31: di: &{Interface:MMSystem Name:Microsoft GS Wavetable Synth IsInputAvailable:false IsOutputAvailable:true IsOpened:false}
[trace]	10:16:53.218101 midiin.go:33: skipping Microsoft GS Wavetable Synth
Exception 0xc0000005 0x0 0xc05b8438 0x70c4198e
PC=0x70c4198e

runtime: unknown pc 0x70c4198e
stack: frame={sp:0x298c3ff3f0, fp:0x0} stack=[0x0,0x298c3ff740)
000000298c3ff2f0:  00007ffc78a57070  00007ffc78a555ff 
000000298c3ff300:  0000000000000000  00000000000003c7 
000000298c3ff310:  0000000000000000  00007ffcaf5295f1 
000000298c3ff320:  0000000000000000  0000000000030002 
000000298c3ff330:  0000000000000000  00007ffca80833fe 
000000298c3ff340:  0000016ac0272080  0000000000030002 
000000298c3ff350:  0000016ac020eef0  000000000000002b 
000000298c3ff360:  0000000000000000  0000016ac026f860 
000000298c3ff370:  0000000000000000  00000000000000bf 
000000298c3ff380:  0000016ac0150000  007200630069004d 
000000298c3ff390:  0066006f0073006f  0049004d00200074 
000000298c3ff3a0:  004d002000490044  0065007000700061 
000000298c3ff3b0:  0000000000400000 <sort.stable+128>  0000000000000001 
000000298c3ff3c0:  00007ffc78a6b200  00007ffc78a70048 
000000298c3ff3d0:  0000016ac0270370  00007ffcaf525d21 
000000298c3ff3e0:  00007ffc78a70048  0000016ac01f0000 
000000298c3ff3f0: <0000016ac0268760  0000000000000000 
000000298c3ff400:  0000000000000000  00007ffc78a63a02 
000000298c3ff410:  0000000000000000  00007ffc78a555ff 
000000298c3ff420:  0000016ac0270370  0000000000000000 
000000298c3ff430:  0000016ac02686f0  0000000000000001 
000000298c3ff440:  0000000000000000  00007ffc78a71101 
000000298c3ff450:  0000016ac0268760  00007ffc78a56703 
000000298c3ff460:  0000016ac02686f0  0000016ac0268760 
000000298c3ff470:  0000000000000000  0000016ac0268710 
000000298c3ff480:  0000016ac05b83e0  0000000000000000 
000000298c3ff490:  0000000000000000  0000000000000078 
000000298c3ff4a0:  0000000000000000  0000016ac05b8460 
000000298c3ff4b0:  000000298c3ff59c  00007ffc00000000 
000000298c3ff4c0:  0000016ac0270370  0000016ac0223d40 
000000298c3ff4d0:  0000050000010001  666f736f7263694d 
000000298c3ff4e0:  4d204944494d2074  0000007265707061 
runtime: unknown pc 0x70c4198e
stack: frame={sp:0x298c3ff3f0, fp:0x0} stack=[0x0,0x298c3ff740)
000000298c3ff2f0:  00007ffc78a57070  00007ffc78a555ff 
000000298c3ff300:  0000000000000000  00000000000003c7 
000000298c3ff310:  0000000000000000  00007ffcaf5295f1 
000000298c3ff320:  0000000000000000  0000000000030002 
000000298c3ff330:  0000000000000000  00007ffca80833fe 
000000298c3ff340:  0000016ac0272080  0000000000030002 
000000298c3ff350:  0000016ac020eef0  000000000000002b 
000000298c3ff360:  0000000000000000  0000016ac026f860 
000000298c3ff370:  0000000000000000  00000000000000bf 
000000298c3ff380:  0000016ac0150000  007200630069004d 
000000298c3ff390:  0066006f0073006f  0049004d00200074 
000000298c3ff3a0:  004d002000490044  0065007000700061 
000000298c3ff3b0:  0000000000400000 <sort.stable+128>  0000000000000001 
000000298c3ff3c0:  00007ffc78a6b200  00007ffc78a70048 
000000298c3ff3d0:  0000016ac0270370  00007ffcaf525d21 
000000298c3ff3e0:  00007ffc78a70048  0000016ac01f0000 
000000298c3ff3f0: <0000016ac0268760  0000000000000000 
000000298c3ff400:  0000000000000000  00007ffc78a63a02 
000000298c3ff410:  0000000000000000  00007ffc78a555ff 
000000298c3ff420:  0000016ac0270370  0000000000000000 
000000298c3ff430:  0000016ac02686f0  0000000000000001 
000000298c3ff440:  0000000000000000  00007ffc78a71101 
000000298c3ff450:  0000016ac0268760  00007ffc78a56703 
000000298c3ff460:  0000016ac02686f0  0000016ac0268760 
000000298c3ff470:  0000000000000000  0000016ac0268710 
000000298c3ff480:  0000016ac05b83e0  0000000000000000 
000000298c3ff490:  0000000000000000  0000000000000078 
000000298c3ff4a0:  0000000000000000  0000016ac05b8460 
000000298c3ff4b0:  000000298c3ff59c  00007ffc00000000 
000000298c3ff4c0:  0000016ac0270370  0000016ac0223d40 
000000298c3ff4d0:  0000050000010001  666f736f7263694d 
000000298c3ff4e0:  4d204944494d2074  0000007265707061 

goroutine 7 [syscall]:
github.com/schollz/miti/src/portmidi._Cfunc_Pm_OpenOutput(0xc000090000, 0xc000000000, 0x0, 0x1000, 0x0, 0x0, 0x7d0, 0xc000000000)
	_cgo_gotypes.go:223 +0x54
github.com/schollz/miti/src/portmidi.NewOutputStream.func1(0xc000090000, 0x0, 0x1000, 0x7d0, 0x0)
	C:/Users/zacks/go/src/github.com/schollz/miti/src/portmidi/stream.go:81 +0x94
github.com/schollz/miti/src/portmidi.NewOutputStream(0x0, 0x1000, 0x7d0, 0xc000043e88, 0x2, 0x2)
	C:/Users/zacks/go/src/github.com/schollz/miti/src/portmidi/stream.go:81 +0x69
github.com/schollz/miti/src/midi.Init.func2(0xc00000e680, 0x15, 0x0, 0x0)
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midi.go:59 +0x15d
created by github.com/schollz/miti/src/midi.Init
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midi.go:52 +0x41b

goroutine 1 [chan receive]:
testing.(*T).Run(0xc000033680, 0x4c232e, 0x6, 0x4cd668, 0x3cdc01)
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1179 +0x3be
testing.runTests.func1(0xc000033080)
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1449 +0x7f
testing.tRunner(0xc000033080, 0xc000077de0)
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1127 +0xef
testing.runTests(0xc000004500, 0x47e4c0, 0x2, 0x2, 0xbfceb6574bab4ff4, 0x8bb2e7f035, 0x59d380, 0x34f1d0)
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1447 +0x2eb
testing.(*M).Run(0xc00012e000, 0x0)
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1357 +0x24e
main.main()
	_testmain.go:45 +0x145

goroutine 8 [sleep]:
time.Sleep(0x77359400)
	C:/Users/zacks/scoop/apps/go/current/src/runtime/time.go:188 +0xc9
github.com/schollz/miti/src/midi.TestIn(0xc000033680)
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midi_test.go:30 +0xb3
testing.tRunner(0xc000033680, 0x4cd668)
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1127 +0xef
created by testing.(*T).Run
	C:/Users/zacks/scoop/apps/go/current/src/testing/testing.go:1178 +0x397

goroutine 9 [chan receive]:
github.com/schollz/miti/src/midi.ReadAll.func1(0xc000006040, 0xc0000124e0)
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midiin.go:71 +0x409
created by github.com/schollz/miti/src/midi.ReadAll
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midiin.go:25 +0xdf

goroutine 10 [chan receive]:
github.com/schollz/miti/src/midi.TestIn.func1(0xc00004a120)
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midi_test.go:26 +0x75
created by github.com/schollz/miti/src/midi.TestIn
	C:/Users/zacks/go/src/github.com/schollz/miti/src/midi/midi_test.go:24 +0xa6
rax     0x70c41980
rbx     0x0
rcx     0x16ac02686f0
rdi     0x70c41980
rsi     0x16ac02686f0
rbp     0x3c7
rsp     0x298c3ff3f0
r8      0xc05b83e0
r9      0x0
r10     0x8000
r11     0x298c3ff3b0
r12     0x7ffc78a70048
r13     0x7ffc78a6b200
r14     0x1
r15     0x400000
rip     0x70c4198e
rflags  0x10293
cs      0x33
fs      0x53
gs      0x2b
exit status 2
FAIL	github.com/schollz/miti/src/midi	1.479s
```

The error can be eliminated only when I include the `-buildmode=exe` flag now:

```
> go test -v -buildmode=exe
=== RUN   TestMIDI
[debug]	10:17:52 midi.go:38: found 2 devices
[debug]	10:17:52 midi.go:44: device 0: 'Microsoft MIDI Mapper', i/o: false/true
[debug]	10:17:52 midi.go:44: device 1: 'Microsoft GS Wavetable Synth', i/o: false/true
--- PASS: TestMIDI (0.02s)
[debug]	10:17:52 midi.go:58: [Microsoft MIDI Mapper] opening stream with latency 2000
=== RUN   TestIn
[debug]	10:17:52 midiin.go:27: found 0 devices
[trace]	10:17:52.905821 midiin.go:31: di: &{Interface:MMSystem Name:Microsoft MIDI Mapper IsInputAvailable:false IsOutputAvailable:true IsOpened:false}
[trace]	10:17:52.905821 midiin.go:31: di: &{Interface:MMSystem Name:Microsoft GS Wavetable Synth IsInputAvailable:false IsOutputAvailable:true IsOpened:false}
[trace]	10:17:52.905821 midiin.go:33: skipping Microsoft GS Wavetable Synth
[debug]	10:17:53 midi.go:63: [Microsoft MIDI Mapper] opened stream
[trace]	10:17:53.782959 midi.go:68: [Microsoft MIDI Mapper] waiting for chord
[debug]	10:17:54 midi_test.go:31: sending finish
[debug]	10:17:54 midi_test.go:33: sent
--- PASS: TestIn (2.10s)
PASS
ok  	github.com/schollz/miti/src/midi	2.653s
```