Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: enabling optimizations breaks cgo #14669

Closed
dominikh opened this issue Mar 6, 2016 · 3 comments

Comments

@dominikh
Copy link
Member

commented Mar 6, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version go1.6 linux/amd64

  2. What operating system and processor architecture are you using (go env)?
    GOHOSTARCH="amd64"
    GOHOSTOS="linux"

  3. What did you do?
    (Use play.golang.org to provide a runnable example, if possible.)

    bug.go: http://play.golang.org/p/buelTjtxyA

    $ go build bug.go
    $ CGO_CFLAGS="-O0" go build bug.go       
    $ CGO_CFLAGS="-Os" go build bug.go 
    
  4. What did you expect to see?
    Three successful builds

  5. What did you see instead?
    Two successful builds, the third build fails with

    # command-line-arguments
    ./bug.go:24:8: error: enumerator value for '__cgo_enum__0' is not an integer constant
    ./bug.go:27:8: error: enumerator value for '__cgo_enum__2' is not an integer constant
    ./bug.go:30:2: error: initializer element is not constant
    ./bug.go:30:2: error: (near initialization for '__cgodebug_data[0]')
    ./bug.go:32:2: error: initializer element is not constant
    ./bug.go:32:2: error: (near initialization for '__cgodebug_data[2]')
    
  6. Additional information

    gcc version 4.9.3 (GCC)

    A successful build and a failing build with -x:

    $ go build -x bug.go                 
    WORK=/tmp/go-build161870404
    mkdir -p $WORK/command-line-arguments/_obj/
    mkdir -p $WORK/
    cd /tmp
    CGO_LDFLAGS="-g" "-O2" /usr/lib/go/pkg/tool/linux_amd64/cgo -objdir ./go-build161870404/command-line-arguments/_obj/ -importpath command-line-arguments -- -I ./go-build161870404/command-line-arguments/_obj/ bug.go
    gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -I ./go-build161870404/command-line-arguments/_obj/ -g -O2 -o ./go-build161870404/command-line-arguments/_obj/_cgo_main.o -c ./go-build161870404/command-line-arguments/_obj/_cgo_main.c
    gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -I ./go-build161870404/command-line-arguments/_obj/ -g -O2 -o ./go-build161870404/command-line-arguments/_obj/_cgo_export.o -c ./go-build161870404/command-line-arguments/_obj/_cgo_export.c
    gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -I ./go-build161870404/command-line-arguments/_obj/ -g -O2 -o ./go-build161870404/command-line-arguments/_obj/bug.cgo2.o -c ./go-build161870404/command-line-arguments/_obj/bug.cgo2.c
    gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -o ./go-build161870404/command-line-arguments/_obj/_cgo_.o ./go-build161870404/command-line-arguments/_obj/_cgo_main.o ./go-build161870404/command-line-arguments/_obj/_cgo_export.o ./go-build161870404/command-line-arguments/_obj/bug.cgo2.o -g -O2
    /usr/lib/go/pkg/tool/linux_amd64/cgo -objdir ./go-build161870404/command-line-arguments/_obj/ -dynpackage udp -dynimport ./go-build161870404/command-line-arguments/_obj/_cgo_.o -dynout ./go-build161870404/command-line-arguments/_obj/_cgo_import.go
    cd $WORK
    gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -no-pie -c trivial.c
    cd /tmp
    gcc -I . -fPIC -m64 -pthread -fmessage-length=0 -o ./go-build161870404/command-line-arguments/_obj/_all.o ./go-build161870404/command-line-arguments/_obj/_cgo_export.o ./go-build161870404/command-line-arguments/_obj/bug.cgo2.o -g -O2 -Wl,-r -nostdlib -Wl,--build-id=none
    /usr/lib/go/pkg/tool/linux_amd64/compile -o ./go-build161870404/command-line-arguments.a -trimpath ./go-build161870404 -p command-line-arguments -buildid ccf8c09685ebb4ea37cf7276dea17de98672bb26 -D _/tmp -I ./go-build161870404 -pack ./go-build161870404/command-line-arguments/_obj/_cgo_gotypes.go ./go-build161870404/command-line-arguments/_obj/bug.cgo1.go ./go-build161870404/command-line-arguments/_obj/_cgo_import.go
    pack r ./go-build161870404/command-line-arguments.a ./go-build161870404/command-line-arguments/_obj/_all.o # internal
    
    
    $ CGO_CFLAGS="-Os" go build -x bug.go
    WORK=/tmp/go-build888932299
    mkdir -p $WORK/command-line-arguments/_obj/
    mkdir -p $WORK/
    cd /tmp
    CGO_LDFLAGS="-g" "-O2" /usr/lib/go/pkg/tool/linux_amd64/cgo -objdir ./go-build888932299/command-line-arguments/_obj/ -importpath command-line-arguments -- -I ./go-build888932299/command-line-arguments/_obj/ -Os bug.go
    # command-line-arguments
    ./bug.go:24:8: error: enumerator value for '__cgo_enum__0' is not an integer constant
    ./bug.go:27:8: error: enumerator value for '__cgo_enum__2' is not an integer constant
    ./bug.go:30:2: error: initializer element is not constant
    ./bug.go:30:2: error: (near initialization for '__cgodebug_data[0]')
    ./bug.go:32:2: error: initializer element is not constant
    ./bug.go:32:2: error: (near initialization for '__cgodebug_data[2]')
    

@ianlancetaylor ianlancetaylor added this to the Go1.7 milestone Mar 7, 2016

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Mar 7, 2016

The fix may be to explicitly remove any optimization options in the compilation in Package.gccErrors in cmd/cgo/gcc.go.

@rsc

This comment has been minimized.

Copy link
Contributor

commented May 17, 2016

The suggestion to remove -O* options in gccErrors sounds reasonable to me. If the diff is small, this is still OK to do for Go 1.7.

@gopherbot

This comment has been minimized.

Copy link

commented May 19, 2016

CL https://golang.org/cl/23231 mentions this issue.

@gopherbot gopherbot closed this in b5d18b5 May 27, 2016

crawford added a commit to crawford/coreos-overlay that referenced this issue Nov 16, 2016
app-emulation/docker: disable optimizations
With optimizations enabled, seccomp fails to compile with errors like:

  error: enumerator value for '__cgo_enum__0' is not an integer constant

This is due to golang/go#14669.

@golang golang locked and limited conversation to collaborators May 27, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants
You can’t perform that action at this time.