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/link: [cgo] Linker error with Go tip on darwin, ld: illegal text-relocation #9411

Closed
gwenn opened this issue Dec 21, 2014 · 6 comments

Comments

Projects
None yet
7 participants
@gwenn
Copy link

commented Dec 21, 2014

With the following code:

package main

/*
#include <stdlib.h>
typedef void (*destructor)(void*);
static void my_func(destructor d, char *p) {
    d(p);
}
*/
import "C"

func main() {
    C.my_func(C.destructor(C.free), C.CString("test"))
}

I get the following error:

$ go build cgo_bug.go
# command-line-arguments
ld: illegal text-relocation to '_free' in /usr/lib/libpthread.dylib from 'main.init' in /var/folders/07/ks1g8j_n40v01c_gc7bzx_z00000gn/T//go-link-gyr5vG/go.o for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
/usr/local/opt/go/libexec/pkg/tool/darwin_amd64/6l: running clang failed: unsuccessful exit status 0x100
$ go version
go version default darwin/amd64

@ianlancetaylor ianlancetaylor changed the title [cgo] Linker error with Go tip on darwin, ld: illegal text-relocation cmd/ld: [cgo] Linker error with Go tip on darwin, ld: illegal text-relocation Dec 21, 2014

@ianlancetaylor ianlancetaylor added this to the Go1.5 milestone Dec 21, 2014

@quarnster

This comment has been minimized.

Copy link

commented Dec 22, 2014

If it's of any help in tracking it down, the following work around appears to do fine:

package main

/*
#include <stdlib.h>
typedef void (*destructor)(void*);
destructor std_free() {
    return free;
}
static void my_func(destructor d, char *p) {
    d(p);
}
*/
import "C"

func main() {
    C.my_func(C.destructor(C.std_free()), C.CString("test"))
}

EDIT: of course I meant to actually call std_free not just reference it

@minux

This comment has been minimized.

Copy link
Member

commented Jan 2, 2015

The problem is that Go tip generate write barriers for the variable _Cfpvar_fp_free:
//go:linkname __cgo_free free
//go:cgo_import_static free
var __cgo_free byte
var _Cfpvar_fp_free unsafe.Pointer = (unsafe.Pointer)(unsafe.Pointer(&__cgo_free))

using LEAQ instruction (see the LEQA at offset 0x55):
0x004a 00074 (gotypes.go:33) LEAQ ""._Cfpvar_fp_free+0(SB),BX
0x0051 00081 (gotypes.go:33) MOVQ BX,(SP)
0x0055 00085 (gotypes.go:33) LEAQ free+0(SB),BX
0x005c 00092 (gotypes.go:33) MOVQ BX,8(SP)
0x0061 00097 (gotypes.go:33) PCDATA $0,$0
0x0061 00097 (gotypes.go:33) CALL ,runtime.writebarrierptr(SB)
...
rel 88+4 t=9 free+0 // 9 is R_PCREL

Mach-O doesn't support this kind of pc relative relocation of a dynamic
symbol.

I see multiple possible ways to fix this, for example:

  1. use static initialization for static addresses, as they can't possibly affect GC,
    thus no need for write barriers
  2. encode the LEAQ externalSymbol+0(SB), REG as MOVABS $address, REG

Suggestions?

@quarnster

This comment has been minimized.

Copy link

commented Mar 30, 2015

Mach-O doesn't support this kind of pc relative relocation of a dynamic symbol.

Is this the same issue as #8791?

@rsc rsc added OS-Darwin and removed os-macosx labels Apr 10, 2015

harukasan added a commit to pixiv/go-libjpeg that referenced this issue May 18, 2015

Fix #8 linker error in golang 1.5 on darwin.
This commit adds a workaround that go-libjpeg can not build with golang
1.5 (devel).

Here is the linker issue of golang:
golang/go#9411

@rsc rsc changed the title cmd/ld: [cgo] Linker error with Go tip on darwin, ld: illegal text-relocation cmd/link: [cgo] Linker error with Go tip on darwin, ld: illegal text-relocation Jun 8, 2015

@gopherbot

This comment has been minimized.

Copy link

commented Jun 29, 2015

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

@gwenn

This comment has been minimized.

Copy link
Author

commented Jul 5, 2015

By applying the patch, https://github.com/gwenn/goreadline builds correctly.
Should I close this issue ?

@mirtchovski

This comment has been minimized.

Copy link
Contributor

commented Jul 5, 2015

i can confirm that the patch fixes the problem I reported in #10015, a duplicate of this one.

@rsc rsc closed this in 9f90f31 Jul 7, 2015

andlabs added a commit to andlabs/ui that referenced this issue Dec 13, 2015

Added OS X building. Go 1.5 required because of golang/go#9411 which …
…affects this. Also set up proper multithreading on OS X. Currently crashes due to similar issue I've had with libui.

@golang golang locked and limited conversation to collaborators Jul 11, 2016

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