<ul dir="auto">
<li>What version of Go are you using (go version)?<br>
<code class="notranslate">go version go1.4.2 darwin/amd64</code><br>
MacOSX-10.6.8</li>
<li>What did you do?</li>
</ul>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$ cd $GOPATH/src/github.com/sbinet
$ git clone git://github.com/sbinet/test-cgo
$ cd test-cgo
$ make
/bin/rm -f pkg/my/*.so
(cd pkg/my &amp;&amp; cc -shared -o libmy.so lib.c)
# github.com/sbinet/test-cgo/pcgo
github.com/sbinet/test-cgo/pkg._Cvar_Global: unsupported relocation for dynamic symbol Global (type=1 stype=32)
github.com/sbinet/test-cgo/pkg._Cvar_Global: unhandled relocation for Global (type 32 rtype 1)
make: *** [install] Error 2"><pre class="notranslate">$ <span class="pl-c1">cd</span> <span class="pl-smi">$GOPATH</span>/src/github.com/sbinet
$ git clone git://github.com/sbinet/test-cgo
$ <span class="pl-c1">cd</span> test-cgo
$ make
/bin/rm -f pkg/my/<span class="pl-k">*</span>.so
(cd pkg/my <span class="pl-k">&amp;&amp;</span> cc -shared -o libmy.so lib.c)
<span class="pl-c"><span class="pl-c">#</span> github.com/sbinet/test-cgo/pcgo</span>
github.com/sbinet/test-cgo/pkg._Cvar_Global: unsupported relocation <span class="pl-k">for</span> dynamic symbol Global (type=1 stype=32)
github.com/sbinet/test-cgo/pkg._Cvar_Global: unhandled relocation <span class="pl-k">for</span> Global (type 32 rtype 1)
make: <span class="pl-k">***</span> [install] Error 2</pre></div>
<p dir="auto">note that if, in <code class="notranslate">test-cgo/pkg/my/lib.h</code> I change:</p>
<div class="highlight highlight-source-c notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#define API_DATA(RTYPE) extern RTYPE
//#define API_DATA(RTYPE) RTYPE"><pre class="notranslate"><span class="pl-k">#define</span> <span class="pl-en">API_DATA</span>(<span class="pl-c1">RTYPE</span>) extern RTYPE
<span class="pl-c">//#define API_DATA(RTYPE) RTYPE</span></pre></div>
<p dir="auto">to:</p>
<div class="highlight highlight-source-c notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="//#define API_DATA(RTYPE) extern RTYPE
#define API_DATA(RTYPE) RTYPE"><pre class="notranslate"><span class="pl-c">//#define API_DATA(RTYPE) extern RTYPE</span>
<span class="pl-k">#define</span> <span class="pl-en">API_DATA</span>(<span class="pl-c1">RTYPE</span>) RTYPE</pre></div>
<p dir="auto">everything compiles and runs:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$ make &amp;&amp; (cd ./pkg/my &amp;&amp; pcgo)
/bin/rm -f pkg/my/*.so
(cd pkg/my &amp;&amp; cc -shared -o libmy.so lib.c)
&gt;&gt;&gt;
Global: 42
Global: 42
&lt;&lt;&lt;"><pre class="notranslate">$ make <span class="pl-k">&amp;&amp;</span> (cd ./pkg/my <span class="pl-k">&amp;&amp;</span> pcgo)
/bin/rm -f pkg/my/<span class="pl-k">*</span>.so
(cd pkg/my <span class="pl-k">&amp;&amp;</span> cc -shared -o libmy.so lib.c)
&gt;&gt;&gt;
Global: 42
Global: 42
&lt;&lt;&lt;</pre></div>
<p dir="auto">Note that on <code class="notranslate">master</code>, I get instead:</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="$ go version devel +48469a2 Mon Feb 23 07:54:13 2015 +0000 darwin/amd64
$ make &amp;&amp; (cd ./pkg/my &amp;&amp; pcgo)
&gt;&gt;&gt;
Global: 42
fatal error: unexpected signal during runtime execution
[signal 0xb code=0x1 addr=0xb01dfacedebac1e pc=0x1dbf23]

runtime stack:
runtime.throw(0x126e70, 0x2a)
    /Users/binet/dev/go/root/go/src/runtime/panic.go:511 +0xa0
runtime.sigpanic()
    /Users/binet/dev/go/root/go/src/runtime/sigpanic_unix.go:12 +0x62

goroutine 1 [syscall, locked to thread]:
runtime.cgocall_errno(0xac120, 0xc208037ed0, 0xc200000000)
    /Users/binet/dev/go/root/go/src/runtime/cgocall.go:130 +0x10c fp=0xc208037eb0 sp=0xc208037e88
github.com/sbinet/test-cgo/pkg._Cfunc_Get(0x25ff0000077a25ff, 0x0)
    /Users/binet/dev/go/root/path/src/github.com/sbinet/test-cgo/pkg/:24 +0x3c fp=0xc208037ed0 sp=0xc208037eb0
github.com/sbinet/test-cgo/pkg.Print()
    /Users/binet/dev/go/root/path/src/github.com/sbinet/test-cgo/pkg/pkg.go:14 +0x10c fp=0xc208037f58 sp=0xc208037ed0
main.main()
    /Users/binet/dev/go/root/path/src/github.com/sbinet/test-cgo/pcgo/main.go:11 +0x4e fp=0xc208037fa0 sp=0xc208037f58
runtime.main()
    /Users/binet/dev/go/root/go/src/runtime/proc.go:88 +0x1d2 fp=0xc208037fe0 sp=0xc208037fa0
runtime.goexit()
    /Users/binet/dev/go/root/go/src/runtime/asm_amd64.s:2466 +0x1 fp=0xc208037fe8 sp=0xc208037fe0

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /Users/binet/dev/go/root/go/src/runtime/asm_amd64.s:2466 +0x1
zsh: exit 2     (; cd pkg/my &amp;&amp; pcgo; )"><pre class="notranslate">$ go version devel +48469a2 Mon Feb 23 07:54:13 2015 +0000 darwin/amd64
$ make <span class="pl-k">&amp;&amp;</span> (cd ./pkg/my <span class="pl-k">&amp;&amp;</span> pcgo)
&gt;&gt;&gt;
Global: 42
fatal error: unexpected signal during runtime execution
[signal 0xb code<span class="pl-k">=</span>0x1 addr<span class="pl-k">=</span>0xb01dfacedebac1e pc<span class="pl-k">=</span>0x1dbf23]

runtime stack:
runtime.throw(0x126e70, 0x2a)
    /Users/binet/dev/go/root/go/src/runtime/panic.go:511 +0xa0
<span class="pl-en">runtime.sigpanic</span>()
    /Users/binet/dev/go/root/go/src/runtime/sigpanic_unix.go:12 +0x62

goroutine 1 [syscall, locked to thread]:
runtime.cgocall_errno(0xac120, 0xc208037ed0, 0xc200000000)
    /Users/binet/dev/go/root/go/src/runtime/cgocall.go:130 +0x10c fp=0xc208037eb0 sp=0xc208037e88
github.com/sbinet/test-cgo/pkg._Cfunc_Get(0x25ff0000077a25ff, 0x0)
    /Users/binet/dev/go/root/path/src/github.com/sbinet/test-cgo/pkg/:24 +0x3c fp=0xc208037ed0 sp=0xc208037eb0
<span class="pl-en">github.com/sbinet/test-cgo/pkg.Print</span>()
    /Users/binet/dev/go/root/path/src/github.com/sbinet/test-cgo/pkg/pkg.go:14 +0x10c fp=0xc208037f58 sp=0xc208037ed0
<span class="pl-en">main.main</span>()
    /Users/binet/dev/go/root/path/src/github.com/sbinet/test-cgo/pcgo/main.go:11 +0x4e fp=0xc208037fa0 sp=0xc208037f58
<span class="pl-en">runtime.main</span>()
    /Users/binet/dev/go/root/go/src/runtime/proc.go:88 +0x1d2 fp=0xc208037fe0 sp=0xc208037fa0
<span class="pl-en">runtime.goexit</span>()
    /Users/binet/dev/go/root/go/src/runtime/asm_amd64.s:2466 +0x1 fp=0xc208037fe8 sp=0xc208037fe0

goroutine 17 [syscall, locked to thread]:
<span class="pl-en">runtime.goexit</span>()
    /Users/binet/dev/go/root/go/src/runtime/asm_amd64.s:2466 +0x1
zsh: <span class="pl-c1">exit</span> 2     (<span class="pl-k">;</span> <span class="pl-c1">cd</span> pkg/my <span class="pl-k">&amp;&amp;</span> pcgo<span class="pl-k">;</span> )</pre></div>
<p dir="auto">Such <code class="notranslate">extern</code> global symbols are heavily used in <em>e.g.</em> the <code class="notranslate">CPython</code> API.</p>
<p dir="auto">To recap: the <code class="notranslate">pkg</code> builds fine, it's when somebody uses that cgo-based package that all hell breaks loose.</p>
<pre class="notranslate">(This is somewhat related to <a href="https://golang.org/issue/5699" rel="nofollow">issue #5699</a> but I was told to file a new one; things got
more interesting since.)

$ cat a.go
package main; import "fmt"
// #cgo LDFLAGS: -framework Foundation -framework AppKit
// extern void *cgoptr;
import "C"
func main() { fmt.Printf("pointer from cgo: %p\n", C.cgoptr) }
$ cat a.m
#include &lt;AppKit/NSKeyValueBinding.h&gt;
void *cgoptr = &amp;NSObservedObjectKey;

All of the following:

$ CC=gcc   CGO_ENABLED=1 GOARCH=amd64 go build -x
$ CC=gcc   CGO_ENABLED=1 GOARCH=386   go build -x
$ CC=clang CGO_ENABLED=1 GOARCH=amd64 go build -x
$ CC=clang CGO_ENABLED=1 GOARCH=386   go build -x

will yield the following during the final link:

main(__DATA/__data): unexpected reloc for dynamic symbol NSObservedObjectKey
main(__DATA/__data): unhandled relocation for NSObservedObjectKey (type 32 rtype 1)

This happens for any Objective-C anything.

For larger projects, the link failures become more spectacular. Attached are the results
of building
<a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/andlabs/ui/commit/93914ecb8cc7bf4bf6eb8199f96fc3a685fc44cf/hovercard" href="https://github.com/andlabs/ui/commit/93914ecb8cc7bf4bf6eb8199f96fc3a685fc44cf">andlabs/ui@<tt>93914ec</tt></a> (this
specific commit) with a modified cmd/ld that does not terminate after 20 error messages
(this should probably be an option like it is for the compilers but I'll leave that for
another issue). I don't know why the 386 one has a different error from the rest; it
should probably be a third issue...

go version:
go version devel +b10044024a6d Thu May 15 15:55:31 2014 +1000 darwin/amd64

gcc version:
i686-apple-darwin10-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM
build 2336.1.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.

clang version:
Apple clang version 3.0 (tags/Apple/clang-211.10.1) (based on LLVM 3.0svn)
Target: x86_64-apple-darwin10.8.0
Thread model: posix

This is with Xcode 4.2 for Snow Leopard.

Thanks.</pre>
<p dir="auto">Attachments:</p>
<ol dir="auto">
<li><a href="https://storage.googleapis.com/go-attachment/8009/0/newerr.386" rel="nofollow">newerr.386</a> (10200 bytes)</li>
<li><a href="https://storage.googleapis.com/go-attachment/8009/0/newerr.amd64" rel="nofollow">newerr.amd64</a> (17599 bytes)</li>
</ol>