Skip to content

cmd/cgo: compiling position independant code with CGO_ENABLED=0 seems broken #17789

@luna-duclos

Description

@luna-duclos

### What version of Go are you using (go version)?

Linux: go version go1.7.3 linux/amd64
Windows: go version go1.7.3 windows/amd64

### What operating system and processor architecture are you using (go env)?

Linux:

GOARCH="amd64"                                                                                                                          
GOBIN=""                                                                                                                                
GOEXE=""                                                                                                                                
GOHOSTARCH="amd64"                                                                                                                      
GOHOSTOS="linux"                                                                                                                        
GOOS="linux"                                                                                                                            
GOPATH="/home/luna/src/gopath"                                                                                                          
GORACE=""                                                                                                                               
GOROOT="/usr/lib/go-1.7"                                                                                                                
GOTOOLDIR="/usr/lib/go-1.7/pkg/tool/linux_amd64"                                                                                        
CC="gcc"                                                                                                                                
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build190862436=/tmp/go-build -gno-record-gcc-switches"    
CXX="g++"                                                                                                                               
CGO_ENABLED="1"                                                                                                                         

Windows:

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:/src/gopath
set GORACE=
set GOROOT=C:\apps\Go
set GOTOOLDIR=C:\apps\Go\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\lunad\AppData\Local\Temp\go-build133903786=/tmp/go-build -gno-record-gcc-switches
set CXX=g++
set CGO_ENABLED=1

### What did you do?
I've tried building the following Go program:

package main

func main() {}

using the following command:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -buildmode=pie

### What did you expect to see?

I expected Go to build a position independant binary with no dependencies on libc, for use on alpine

### What did you see instead?

On linux:

luna@ssh-dev:~/src/gopath/src/test-pie$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -buildmode=pie
# test-pie
/usr/lib/go-1.7/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/tmp/go-link-364377506/go.o:(.data+0x0): undefined reference to `x_cgo_callers'
/tmp/go-link-364377506/go.o:(.data+0x8): undefined reference to `x_cgo_init'
/tmp/go-link-364377506/go.o:(.data+0x10): undefined reference to `x_cgo_mmap'
/tmp/go-link-364377506/go.o:(.data+0x18): undefined reference to `x_cgo_notify_runtime_init_done'
/tmp/go-link-364377506/go.o:(.data+0x20): undefined reference to `x_cgo_thread_start'
/tmp/go-link-364377506/go.o:(.data+0x28): undefined reference to `x_cgo_setenv'
/tmp/go-link-364377506/go.o:(.data+0x30): undefined reference to `x_cgo_unsetenv'
collect2: error: ld returned 1 exit status

On windows:

λ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -buildmode=pie
# test-pie
C:\apps\Go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1
gcc: error: unrecognized command line option '-rdynamic'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions