-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: SIGSEGV using C++ files in package on Darwin #5629
Labels
Milestone
Comments
hi, actually, now, on my mac development machine: $ uname -a Darwin macpro4.somewhere.net 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386 with tip: $ go version go version devel +8bf13ae02c8e Mon Aug 19 16:22:33 2013 +1000 darwin/amd64 and with gcc-apple: $ g++ --version i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646) (dot 1) 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. none of the solutions work. ie: with -lstdc++, or with -static-libstdc++ I get the SEGV from the original report. -s |
On GNU/Linux (Ubuntu Precise) this test case works when use -lstdc++, whether or not I use -static-libstdc++. Note that -static-libstdc++ does nothing by itself, it just means that if libstdc++ is linked in, it will be linked in statically. Your crash on Darwin suggests that the global constructors were not run. What happens if you do go run -ldflags "-extld g++" run.go ? |
Ian, with tip: $ go version go version devel +a71616f65cb1 Tue Sep 03 21:23:52 2013 -0700 darwin/amd64 I still get: $ ./doit WORK=/var/folders/-Y/-YM5iRWlGwud5f9CdH77Ck+++TM/-Tmp-/go-build130503768 ttt mkdir -p $WORK/ttt/_obj/ mkdir -p $WORK/ cd /Users/binet/tmp/go-tip/code/src/ttt /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/ttt/_obj/ -- -I $WORK/ttt/_obj/ ttt.go /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/ttt/_obj/ -I /Users/binet/tmp/go-tip/go/pkg/darwin_amd64 -o $WORK/ttt/_obj/_cgo_defun.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/ttt/_obj/_cgo_defun.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -print-libgcc-file-name gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/_obj/ -o $WORK/ttt/_obj/_cgo_main.o -c $WORK/ttt/_obj/_cgo_main.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/_obj/ -o $WORK/ttt/_obj/_cgo_export.o -c $WORK/ttt/_obj/_cgo_export.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/_obj/ -o $WORK/ttt/_obj/ttt.cgo2.o -c $WORK/ttt/_obj/ttt.cgo2.c g++ -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/_obj/ -o $WORK/ttt/_obj/ttt.cxx.o -c ./ttt.cxx g++ -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/ttt/_obj/_cgo_.o $WORK/ttt/_obj/_cgo_main.o $WORK/ttt/_obj/_cgo_export.o $WORK/ttt/_obj/ttt.cgo2.o $WORK/ttt/_obj/ttt.cxx.o -lstdc++ /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/ttt/_obj/ -dynimport $WORK/ttt/_obj/_cgo_.o -dynout $WORK/ttt/_obj/_cgo_import.c /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/ttt/_obj/ -I /Users/binet/tmp/go-tip/go/pkg/darwin_amd64 -o $WORK/ttt/_obj/_cgo_import.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/ttt/_obj/_cgo_import.c g++ -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/ttt/_obj/_all.o $WORK/ttt/_obj/_cgo_export.o $WORK/ttt/_obj/ttt.cgo2.o $WORK/ttt/_obj/ttt.cxx.o -Wl,-r -nostdlib /usr/lib/gcc/i686-apple-darwin10/4.2.1/x86_64/libgcc.a /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6g -o $WORK/ttt/_obj/_go_.6 -p ttt -D _/Users/binet/tmp/go-tip/code/src/ttt -I $WORK $WORK/ttt/_obj/_cgo_gotypes.go $WORK/ttt/_obj/ttt.cgo1.go /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/pack grcP $WORK $WORK/ttt.a $WORK/ttt/_obj/_go_.6 $WORK/ttt/_obj/_cgo_import.6 $WORK/ttt/_obj/_cgo_defun.6 $WORK/ttt/_obj/_all.o mkdir -p /Users/binet/tmp/go-tip/code/pkg/darwin_amd64/ cp $WORK/ttt.a /Users/binet/tmp/go-tip/code/pkg/darwin_amd64/ttt.a ttt/ttt-run mkdir -p $WORK/ttt/ttt-run/_obj/ mkdir -p $WORK/ttt/ttt-run/_obj/exe/ cd /Users/binet/tmp/go-tip/code/src/ttt/ttt-run /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6g -o $WORK/ttt/ttt-run/_obj/_go_.6 -p ttt/ttt-run -complete -D _/Users/binet/tmp/go-tip/code/src/ttt/ttt-run -I $WORK -I /Users/binet/tmp/go-tip/code/pkg/darwin_amd64 ./run.go /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/pack grcP $WORK $WORK/ttt/ttt-run.a $WORK/ttt/ttt-run/_obj/_go_.6 cd . /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6l -o $WORK/ttt/ttt-run/_obj/exe/a.out -L $WORK -L /Users/binet/tmp/go-tip/code/pkg/darwin_amd64 -extld g++ $WORK/ttt/ttt-run.a mkdir -p /Users/binet/tmp/go-tip/code/bin/ cp $WORK/ttt/ttt-run/_obj/exe/a.out /Users/binet/tmp/go-tip/code/bin/ttt-run foo SIGSEGV: segmentation violation PC=0x7fff87c0468a signal arrived during cgo execution ttt._Cfunc_MyPrint(0xae220) ttt/_obj/_cgo_defun.c:46 +0x2f ttt.Ttt() ttt/_obj/ttt.cgo1.go:17 +0x45 main.main() /Users/binet/dev/go/gocode/src/ttt/ttt-run/run.go:8 +0x18 goroutine 2 [syscall]: goroutine 3 [runnable]: rax 0x0 rbx 0x7fff70cf5b00 rcx 0x1d4f40 rdx 0x7fff70cf5b00 rdi 0x7fff5fbff4e0 rsi 0x7fff70cf5b00 rbp 0x7fff5fbff4c0 rsp 0x7fff5fbff4b0 r8 0x2 r9 0x823c0 r10 0x9c990 r11 0x93ce8 r12 0x7fff5fbff4e0 r13 0xe r14 0x1320c1a1a6573400 r15 0xc5e28 rip 0x7fff87c0468a rflags 0x10202 cs 0x27 fs 0x0 gs 0x0 zsh: exit 2 ./doit which, with gdb: (gdb) r Starting program: /Users/binet/dev/go/gocode/bin/ttt-run Reading symbols for shared libraries ++. done foo Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: 0xffffffffffffffe8 0x00007fff87c0468a in std::ostream::sentry::sentry () (gdb) bt #0 0x00007fff87c0468a in std::ostream::sentry::sentry () #1 0x00007fff87c04b0d in std::__ostream_insert<char, std::char_traits<char> > () Die: DW_TAG_unspecified_type (abbrev = 10, offset = 141) has children: FALSE attributes: DW_AT_name (DW_FORM_string) string: "<unspecified>" Die: DW_TAG_unspecified_type (abbrev = 10, offset = 141) has children: FALSE attributes: DW_AT_name (DW_FORM_string) string: "<unspecified>" Die: DW_TAG_unspecified_type (abbrev = 10, offset = 141) has children: FALSE attributes: DW_AT_name (DW_FORM_string) string: "<unspecified>" Dwarf Error: Cannot find type of die [in module /Users/binet/dev/go/gocode/bin/ttt-run] |
Thanks for trying. As far as I can see std::cout is not getting initialized. This is going to need investigation by somebody who knows more C++ on Darwin. Although I don't see any evidence of this in the GCC source code, it's possible that your g++ only works correctly if the main function is compiled by g++. One way to test this would be to add a call to __main() in your MyPrint function before referring to std::cout. (If that fails to link with an undefined reference to __main, then that is not the problem, but I have no other idea what the problem might be.) Labels changed: added os-macosx. Status changed to HelpWanted. |
ok, I tried to apply this patch to the ttt.cxx file: --- ttt.cxx.orig 2013-09-04 20:29:15.000000000 +0200 +++ ttt.cxx 2013-09-04 20:27:52.000000000 +0200 @@ -3,7 +3,9 @@ #include <iostream> extern "C" { + void __main(); void MyPrint() { + __main(); std::cout << "hello from c++" << std::endl; } } and indeed, got a failure at link time: $ ./doit WORK=/var/folders/-Y/-YM5iRWlGwud5f9CdH77Ck+++TM/-Tmp-/go-build256475887 ttt mkdir -p $WORK/ttt/_obj/ mkdir -p $WORK/ cd /Users/binet/tmp/go-tip/code/src/ttt /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/ttt/_obj/ -- -I $WORK/ttt/_obj/ ttt.go /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/ttt/_obj/ -I /Users/binet/tmp/go-tip/go/pkg/darwin_amd64 -o $WORK/ttt/_obj/_cgo_defun.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/ttt/_obj/_cgo_defun.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -print-libgcc-file-name gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/_obj/ -o $WORK/ttt/_obj/_cgo_main.o -c $WORK/ttt/_obj/_cgo_main.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/_obj/ -o $WORK/ttt/_obj/_cgo_export.o -c $WORK/ttt/_obj/_cgo_export.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/_obj/ -o $WORK/ttt/_obj/ttt.cgo2.o -c $WORK/ttt/_obj/ttt.cgo2.c g++ -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/_obj/ -o $WORK/ttt/_obj/ttt.cxx.o -c ./ttt.cxx g++ -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/ttt/_obj/_cgo_.o $WORK/ttt/_obj/_cgo_main.o $WORK/ttt/_obj/_cgo_export.o $WORK/ttt/_obj/ttt.cgo2.o $WORK/ttt/_obj/ttt.cxx.o -lstdc++ # ttt Undefined symbols: "___main", referenced from: _MyPrint in ttt.cxx.o ld: symbol(s) not found collect2: ld returned 1 exit status |
This issue was updated by revision f0ff63e. R=golang-dev, minux.ma, rsc CC=golang-dev https://golang.org/cl/13394045 |
hi, same basic outcome (even though one can clearly see -extld=g++ being automatically inserted): $ go version go version devel +ca80c16cf523 Mon Sep 09 12:50:49 2013 -0700 darwin/amd64 $ ./doit WORK=/var/folders/-Y/-YM5iRWlGwud5f9CdH77Ck+++TM/-Tmp-/go-build217132290 ttt mkdir -p $WORK/ttt/_obj/ mkdir -p $WORK/ cd /Users/binet/tmp/go-tip/code/src/ttt /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/ttt/_obj/ -- -I $WORK/ttt/_obj/ ttt.go /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/ttt/_obj/ -I /Users/binet/tmp/go-tip/go/pkg/darwin_amd64 -o $WORK/ttt/_obj/_cgo_defun.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/ttt/_obj/_cgo_defun.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -print-libgcc-file-name gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/_obj/ -o $WORK/ttt/_obj/_cgo_main.o -c $WORK/ttt/_obj/_cgo_main.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/_obj/ -o $WORK/ttt/_obj/_cgo_export.o -c $WORK/ttt/_obj/_cgo_export.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/_obj/ -o $WORK/ttt/_obj/ttt.cgo2.o -c $WORK/ttt/_obj/ttt.cgo2.c g++ -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/_obj/ -o $WORK/ttt/_obj/ttt.cxx.o -c ./ttt.cxx g++ -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/ttt/_obj/_cgo_.o $WORK/ttt/_obj/_cgo_main.o $WORK/ttt/_obj/_cgo_export.o $WORK/ttt/_obj/ttt.cgo2.o $WORK/ttt/_obj/ttt.cxx.o /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/ttt/_obj/ -dynimport $WORK/ttt/_obj/_cgo_.o -dynout $WORK/ttt/_obj/_cgo_import.c /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/ttt/_obj/ -I /Users/binet/tmp/go-tip/go/pkg/darwin_amd64 -o $WORK/ttt/_obj/_cgo_import.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/ttt/_obj/_cgo_import.c g++ -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/ttt/_obj/_all.o $WORK/ttt/_obj/_cgo_export.o $WORK/ttt/_obj/ttt.cgo2.o $WORK/ttt/_obj/ttt.cxx.o -Wl,-r -nostdlib /usr/lib/gcc/i686-apple-darwin10/4.2.1/x86_64/libgcc.a /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6g -o $WORK/ttt/_obj/_go_.6 -p ttt -D _/Users/binet/tmp/go-tip/code/src/ttt -I $WORK $WORK/ttt/_obj/_cgo_gotypes.go $WORK/ttt/_obj/ttt.cgo1.go /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/pack grcP $WORK $WORK/ttt.a $WORK/ttt/_obj/_go_.6 $WORK/ttt/_obj/_cgo_import.6 $WORK/ttt/_obj/_cgo_defun.6 $WORK/ttt/_obj/_all.o mkdir -p /Users/binet/tmp/go-tip/code/pkg/darwin_amd64/ cp $WORK/ttt.a /Users/binet/tmp/go-tip/code/pkg/darwin_amd64/ttt.a ttt/ttt-run mkdir -p $WORK/ttt/ttt-run/_obj/ mkdir -p $WORK/ttt/ttt-run/_obj/exe/ cd /Users/binet/tmp/go-tip/code/src/ttt/ttt-run /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/ttt/ttt-run/_obj/ -- -I $WORK/ttt/ttt-run/_obj/ run.go /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/ttt/ttt-run/_obj/ -I /Users/binet/tmp/go-tip/go/pkg/darwin_amd64 -o $WORK/ttt/ttt-run/_obj/_cgo_defun.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/ttt/ttt-run/_obj/_cgo_defun.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/ttt-run/_obj/ -o $WORK/ttt/ttt-run/_obj/_cgo_main.o -c $WORK/ttt/ttt-run/_obj/_cgo_main.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/ttt-run/_obj/ -o $WORK/ttt/ttt-run/_obj/_cgo_export.o -c $WORK/ttt/ttt-run/_obj/_cgo_export.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I $WORK/ttt/ttt-run/_obj/ -o $WORK/ttt/ttt-run/_obj/run.cgo2.o -c $WORK/ttt/ttt-run/_obj/run.cgo2.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/ttt/ttt-run/_obj/_cgo_.o $WORK/ttt/ttt-run/_obj/_cgo_main.o $WORK/ttt/ttt-run/_obj/_cgo_export.o $WORK/ttt/ttt-run/_obj/run.cgo2.o /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/ttt/ttt-run/_obj/ -dynimport $WORK/ttt/ttt-run/_obj/_cgo_.o -dynout $WORK/ttt/ttt-run/_obj/_cgo_import.c /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/ttt/ttt-run/_obj/ -I /Users/binet/tmp/go-tip/go/pkg/darwin_amd64 -o $WORK/ttt/ttt-run/_obj/_cgo_import.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/ttt/ttt-run/_obj/_cgo_import.c gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/ttt/ttt-run/_obj/_all.o $WORK/ttt/ttt-run/_obj/_cgo_export.o $WORK/ttt/ttt-run/_obj/run.cgo2.o -Wl,-r -nostdlib /usr/lib/gcc/i686-apple-darwin10/4.2.1/x86_64/libgcc.a /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6g -o $WORK/ttt/ttt-run/_obj/_go_.6 -p ttt/ttt-run -D _/Users/binet/tmp/go-tip/code/src/ttt/ttt-run -I $WORK -I /Users/binet/tmp/go-tip/code/pkg/darwin_amd64 $WORK/ttt/ttt-run/_obj/_cgo_gotypes.go $WORK/ttt/ttt-run/_obj/run.cgo1.go /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/pack grcP $WORK $WORK/ttt/ttt-run.a $WORK/ttt/ttt-run/_obj/_go_.6 $WORK/ttt/ttt-run/_obj/_cgo_import.6 $WORK/ttt/ttt-run/_obj/_cgo_defun.6 $WORK/ttt/ttt-run/_obj/_all.o cd . /Users/binet/tmp/go-tip/go/pkg/tool/darwin_amd64/6l -o $WORK/ttt/ttt-run/_obj/exe/a.out -L $WORK -L /Users/binet/tmp/go-tip/code/pkg/darwin_amd64 -extld=g++ $WORK/ttt/ttt-run.a mkdir -p /Users/binet/tmp/go-tip/code/bin/ cp $WORK/ttt/ttt-run/_obj/exe/a.out /Users/binet/tmp/go-tip/code/bin/ttt-run foo SIGSEGV: segmentation violation PC=0x7fff87c0468a signal arrived during cgo execution runtime.cgocall(0x78b04, 0x350ef8) /Users/binet/tmp/go-tip/go/src/pkg/runtime/cgocall.c:149 +0x11b fp=0x350ee0 ttt._Cfunc_MyPrint(0xb7180) ttt/_obj/_cgo_defun.c:43 +0x31 fp=0x350ef8 ttt.T() /Users/binet/tmp/go-tip/code/src/ttt/ttt.go:10 +0x47 fp=0x350f40 main.main() /Users/binet/tmp/go-tip/code/src/ttt/ttt-run/run.go:10 +0x1a fp=0x350f48 runtime.main() /Users/binet/tmp/go-tip/go/src/pkg/runtime/proc.c:212 +0x11f fp=0x350fa0 runtime.goexit() /Users/binet/tmp/go-tip/go/src/pkg/runtime/proc.c:1386 fp=0x350fa8 goroutine 3 [syscall]: runtime.goexit() /Users/binet/tmp/go-tip/go/src/pkg/runtime/proc.c:1386 rax 0x0 rbx 0x7fff70cf5b00 rcx 0x350ef8 rdx 0x7fff70cf5b00 rdi 0x7fff5fbff4a0 rsi 0x7fff70cf5b00 rbp 0x7fff5fbff480 rsp 0x7fff5fbff470 r8 0xc210001120 r9 0x350e90 r10 0x2dadb395ca70 r11 0x607b7a00 r12 0x7fff5fbff4a0 r13 0xe r14 0x13225530194c1200 r15 0xde630 rip 0x7fff87c0468a rflags 0x10202 cs 0x27 fs 0x0 gs 0x0 zsh: exit 2 ./doit |
I ran the script given in the original report and it produces a binary that works just fine. I suspect this was fixed by Ian's change yesterday that uses g++ as the external linker when C++ is involved. changeset: 17997:eeb4127a4d00 user: Ian Lance Taylor <iant@golang.org> date: Thu Sep 05 12:55:21 2013 -0700 summary: cmd/ld: emit relocations for .debug_frame in external link mode Status changed to Fixed. |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Attachments:
The text was updated successfully, but these errors were encountered: