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: fails with anonymous struct fields #6472

Closed
mkb218 opened this issue Sep 24, 2013 · 9 comments

Comments

@mkb218
Copy link

commented Sep 24, 2013

What steps will reproduce the problem?
`go run` with the following program: http://play.golang.org/p/ftVTmkOKfH


What is the expected output?
{0} (OK on 1.1.2)

What do you see instead?
cgo fails to compile with
# command-line-arguments
./sndfilebug.go:16: s.loops undefined (type *_Ctype_SF_INSTRUMENT has no field or method
loops)

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g

Which operating system are you using?
OS X 10.8
Darwin HOSTNAME 12.5.0 Darwin Kernel Version 12.5.0: Mon Jul 29 16:33:49 PDT 2013;
root:xnu-2050.48.11~1/RELEASE_X86_64 x86_64

Which version are you using?  (run 'go version')
go version devel +ec7b58d6a977 Mon Sep 23 14:15:20 2013 -0700 darwin/amd64

Please provide any additional information below.
This does indeed work on 10.7 (using llvm-gcc 4.2.1) while 10.8 is using Apple LLVM
version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)

Here are the commands from the ill-fated machine:
WORK=/var/folders/kt/3lc03rx15mx7h00cbp1l91hh0000gp/T/go-build629844114
mkdir -p $WORK/command-line-arguments/_obj/
mkdir -p $WORK/command-line-arguments/_obj/exe/
cd /Users/mkane/code/testcode/go
/Users/mkane/go-tip/pkg/tool/darwin_amd64/cgo -objdir $WORK/command-line-arguments/_obj/
-- -I $WORK/command-line-arguments/_obj/ -I/opt/local/include sndfilebug.go
/Users/mkane/go-tip/pkg/tool/darwin_amd64/6c -F -V -w -I
$WORK/command-line-arguments/_obj/ -I /Users/mkane/go-tip/pkg/darwin_amd64 -o
$WORK/command-line-arguments/_obj/_cgo_defun.6 -D GOOS_darwin -D GOARCH_amd64
$WORK/command-line-arguments/_obj/_cgo_defun.c
clang -I . -g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments
-fno-common -print-libgcc-file-name
clang -I . -g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments
-fno-common -I $WORK/command-line-arguments/_obj/ -I/opt/local/include -o
$WORK/command-line-arguments/_obj/_cgo_main.o -c
$WORK/command-line-arguments/_obj/_cgo_main.c
clang -I . -g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments
-fno-common -I $WORK/command-line-arguments/_obj/ -I/opt/local/include -o
$WORK/command-line-arguments/_obj/_cgo_export.o -c
$WORK/command-line-arguments/_obj/_cgo_export.c
clang -I . -g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments
-fno-common -I $WORK/command-line-arguments/_obj/ -I/opt/local/include -o
$WORK/command-line-arguments/_obj/sndfilebug.cgo2.o -c
$WORK/command-line-arguments/_obj/sndfilebug.cgo2.c
clang -I . -g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments
-fno-common -o $WORK/command-line-arguments/_obj/_cgo_.o
$WORK/command-line-arguments/_obj/_cgo_main.o
$WORK/command-line-arguments/_obj/_cgo_export.o
$WORK/command-line-arguments/_obj/sndfilebug.cgo2.o -L/opt/local/lib
/Users/mkane/go-tip/pkg/tool/darwin_amd64/cgo -objdir $WORK/command-line-arguments/_obj/
-dynimport $WORK/command-line-arguments/_obj/_cgo_.o -dynout
$WORK/command-line-arguments/_obj/_cgo_import.c
/Users/mkane/go-tip/pkg/tool/darwin_amd64/6c -F -V -w -I
$WORK/command-line-arguments/_obj/ -I /Users/mkane/go-tip/pkg/darwin_amd64 -o
$WORK/command-line-arguments/_obj/_cgo_import.6 -D GOOS_darwin -D GOARCH_amd64
$WORK/command-line-arguments/_obj/_cgo_import.c
clang -I . -g -O2 -fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments
-fno-common -o $WORK/command-line-arguments/_obj/_all.o
$WORK/command-line-arguments/_obj/_cgo_export.o
$WORK/command-line-arguments/_obj/sndfilebug.cgo2.o -L/opt/local/lib -Wl,-r -nostdlib
/Users/mkane/go-tip/pkg/tool/darwin_amd64/6g -o $WORK/command-line-arguments/_obj/_go_.6
-p command-line-arguments -D _/Users/mkane/code/testcode/go -I $WORK
$WORK/command-line-arguments/_obj/_cgo_gotypes.go
$WORK/command-line-arguments/_obj/sndfilebug.cgo1.go
# command-line-arguments
./sndfilebug.go:16[/var/folders/kt/3lc03rx15mx7h00cbp1l91hh0000gp/T/go-build629844114/command-line-arguments/_obj/sndfilebug.cgo1.go:14]:
s.loops undefined (type *_Ctype_SF_INSTRUMENT has no field or method loops)
@quarnster

This comment has been minimized.

Copy link

commented Sep 24, 2013

Comment 1:

FYI, as a workaround until it's fixed proper you can set the environment variable CC=gcc:
20:50 /tmp $ go run sndfile.go
# command-line-arguments
./sndfile.go:16: s.loops undefined (type *_Ctype_SF_INSTRUMENT has no field or method
loops)
20:50 /tmp $ CC=gcc go run sndfile.go
{0}
20:50 /tmp $
@minux

This comment has been minimized.

Copy link
Member

commented Sep 24, 2013

Comment 2:

Labels changed: added priority-soon, removed priority-triage.

Status changed to Accepted.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Sep 24, 2013

Comment 3:

Labels changed: added go1.2maybe.

@lukescott

This comment has been minimized.

Copy link

commented Sep 24, 2013

Comment 4:

This is a problem with LLVM. Setting`CC=gcc` doesn't work because gcc on OS X is a gcc
front-end backed by LLVM.
For now you can avoid LLVM and use plain GNU gcc by installing it with homebrew `brew
install gcc48`, then use it by prefixing your build command `CC=gcc-4.9 go build`. For
C++ code do `CXX=g++-4.8 go build` instead.
When building go you can set the default C compiler like this `CC=gcc-4.8 ./all.bash`.
Since revision 1607e9e9e6de you can also set the default C++ compiler: `CC=gcc-4.8
CXX=g++-4.8 ./all.bash`.
@mkb218

This comment has been minimized.

Copy link
Author

commented Sep 24, 2013

Comment 5:

Setting "CC=gcc" works fine on 10.8.
@quarnster

This comment has been minimized.

Copy link

commented Sep 24, 2013

Comment 6:

> This is a problem with LLVM.
I disagree, it's a problem with clang, not LLVM. My paste from above was running on my
MBP with the "regular" OS X llvm backed gcc, no need to involve homebrew. Just to be
verbose:
21:39 /tmp $ go run sndfile.go
# command-line-arguments
./sndfile.go:16: s.loops undefined (type *_Ctype_SF_INSTRUMENT has no field or method
loops)
21:39 /tmp $ cc --version
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.3.0
Thread model: posix
21:39 /tmp $ CC=gcc go run sndfile.go
{0}
21:39 /tmp $ gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM
build 2336.11.00)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21:39 /tmp $ which gcc
/usr/bin/gcc
21:40 /tmp $ ls -l /usr/bin/gcc
lrwxr-xr-x  1 root  wheel  12 21 Apr 10:45 /usr/bin/gcc@ -> llvm-gcc-4.2
21:40 /tmp $ ls -l /usr/bin/llv
llvm-cpp-4.2  llvm-g++      llvm-g++-4.2  llvm-gcc      llvm-gcc-4.2
21:40 /tmp $ ls -l /usr/bin/llvm-gcc
llvm-gcc      llvm-gcc-4.2
21:40 /tmp $ ls -l /usr/bin/llvm-gcc-4.2
lrwxr-xr-x  1 root  wheel  32 21 Apr 10:45 /usr/bin/llvm-gcc-4.2@ ->
../llvm-gcc-4.2/bin/llvm-gcc-4.2
21:40 /tmp $ ls -l /usr/llvm-gcc-4.2/bin/ll
llvm-c++-4.2  llvm-cpp-4.2  llvm-g++-4.2  llvm-gcc-4.2
21:40 /tmp $ ls -l /usr/llvm-gcc-4.2/bin/llvm-gcc-4.2
-rwxr-xr-x  1 root  wheel  117168 21 Apr 10:45 /usr/llvm-gcc-4.2/bin/llvm-gcc-4.2*
@rsc

This comment has been minimized.

Copy link
Contributor

commented Oct 9, 2013

Comment 8:

This is (of course) clang's fault. Here is an interesting dump:
g% cat x.c
typedef struct s1 {
    struct s2 {
        int mode;
    } loops[16];
} s1;
typedef struct s3 {
    struct {
        int mode;
    } loops[16];
} s3;
s1 x;
s3 y;
g% clang -ggdb -c x.c
g% dwarfdump x.o
----------------------------------------------------------------------
 File: x.o (x86_64)
----------------------------------------------------------------------
.debug_info contents:
0x00000000: Compile Unit: length = 0x000000e3  version = 0x0002  abbr_offset =
0x00000000  addr_size = 0x08  (next CU at 0x000000e7)
0x0000000b: TAG_compile_unit [1] *
             AT_producer( "Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)" )
             AT_language( DW_LANG_C99 )
             AT_name( "x.c" )
             AT_entry_pc( 0x0000000000000000 )
             AT_stmt_list( 0x00000000 )
             AT_comp_dir( "/Users/rsc" )
0x00000026:     TAG_base_type [2]  
                 AT_name( "int" )
                 AT_encoding( DW_ATE_signed )
                 AT_byte_size( 0x04 )
0x0000002d:     TAG_structure_type [3] *
                 AT_name( "s2" )
                 AT_byte_size( 0x04 )
                 AT_decl_file( "/Users/rsc/x.c" )
                 AT_decl_line( 2 )
0x00000035:         TAG_member [4]  
                     AT_name( "mode" )
                     AT_type( {0x00000026} ( int ) )
                     AT_decl_file( "/Users/rsc/x.c" )
                     AT_decl_line( 3 )
                     AT_data_member_location( +0 )
                     AT_accessibility( DW_ACCESS_public )
0x00000044:         NULL
0x00000045:     TAG_base_type [5]  
                 AT_byte_size( 0x04 )
                 AT_encoding( DW_ATE_signed )
0x00000048:     TAG_array_type [6] *
                 AT_type( {0x0000002d} ( s2 ) )
0x0000004d:         TAG_subrange_type [7]  
                     AT_type( {0x00000045} ( base  ) )
                     AT_upper_bound( 0x0f )
0x00000053:         NULL
0x00000054:     TAG_structure_type [3] *
                 AT_name( "s1" )
                 AT_byte_size( 0x40 )
                 AT_decl_file( "/Users/rsc/x.c" )
                 AT_decl_line( 1 )
0x0000005c:         TAG_member [4]  
                     AT_name( "loops" )
                     AT_type( {0x00000048} ( s2[] ) )
                     AT_decl_file( "/Users/rsc/x.c" )
                     AT_decl_line( 4 )
                     AT_data_member_location( +0 )
                     AT_accessibility( DW_ACCESS_public )
0x0000006b:         NULL
0x0000006c:     TAG_typedef [8]  
                 AT_type( {0x00000054} ( s1 ) )
                 AT_name( "s1" )
                 AT_decl_file( "/Users/rsc/x.c" )
                 AT_decl_line( 5 )
0x00000077:     TAG_variable [9]  
                 AT_name( "x" )
                 AT_type( {0x0000006c} ( s1 ) )
                 AT_external( 0x01 )
                 AT_decl_file( "/Users/rsc/x.c" )
                 AT_decl_line( 13 )
                 AT_location( [0x0000000000000000] )
0x0000008d:     TAG_array_type [6] *
                 AT_type( {0x000000a1} ( struct  ) )
0x00000092:         TAG_subrange_type [7]  
                     AT_type( {0x00000045} ( base  ) )
                     AT_upper_bound( 0x0f )
0x00000098:         NULL
0x00000099:     TAG_structure_type [3] *
                 AT_name( "s3" )
                 AT_byte_size( 0x40 )
                 AT_decl_file( "/Users/rsc/x.c" )
                 AT_decl_line( 7 )
0x000000a1:         TAG_structure_type [10] *
                     AT_byte_size( 0x04 )
                     AT_decl_file( "/Users/rsc/x.c" )
                     AT_decl_line( 8 )
0x000000a5:             TAG_member [4]  
                         AT_name( "mode" )
                         AT_type( {0x00000026} ( int ) )
                         AT_decl_file( "/Users/rsc/x.c" )
                         AT_decl_line( 9 )
                         AT_data_member_location( +0 )
                         AT_accessibility( DW_ACCESS_public )
0x000000b4:             NULL
0x000000b5:         TAG_member [4]  
                     AT_name( "loops" )
                     AT_type( {0x0000008d} ( struct [] ) )
                     AT_decl_file( "/Users/rsc/x.c" )
                     AT_decl_line( 10 )
                     AT_data_member_location( +0 )
                     AT_accessibility( DW_ACCESS_public )
0x000000c4:         NULL
0x000000c5:     TAG_typedef [8]  
                 AT_type( {0x00000099} ( s3 ) )
                 AT_name( "s3" )
                 AT_decl_file( "/Users/rsc/x.c" )
                 AT_decl_line( 11 )
0x000000d0:     TAG_variable [9]  
                 AT_name( "y" )
                 AT_type( {0x000000c5} ( s3 ) )
                 AT_external( 0x01 )
                 AT_decl_file( "/Users/rsc/x.c" )
                 AT_decl_line( 14 )
                 AT_location( [0x0000000000000000] )
0x000000e6:     NULL
g% 
The named version (s1/s2) is fine: s1 is at 0x54...0x6b and s2 is at 0x2d...0x44. The
unnamed version (s3) contains the anonymous inner struct definition at offsets
0xa1...0xb4 nested *inside* the outer struct definition at 0x99...0xc4. Despite this,
the array of 16 of those structs has a type outside the outer struct (0x8d...0x98). We
could argue about whether it's even valid to place the inner struct where it is, but we
can handle it now that we know to expect it.
Sigh.

Labels changed: added go1.2, removed go1.2maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Oct 9, 2013

Comment 9:

Issue #6368 has been merged into this issue.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Oct 9, 2013

Comment 10:

This issue was closed by revision 0965459.

Status changed to Fixed.

@mkb218 mkb218 added fixed labels Oct 9, 2013

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015

@rsc rsc removed the go1.2 label Apr 14, 2015

@golang golang locked and limited conversation to collaborators Jun 25, 2016

This issue was closed.

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