Skip to content

runtime: buildmode=c-shared segfaults on linux/arm (ARMv7) #12846

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

Closed
moutend opened this issue Oct 5, 2015 · 9 comments
Closed

runtime: buildmode=c-shared segfaults on linux/arm (ARMv7) #12846

moutend opened this issue Oct 5, 2015 · 9 comments

Comments

@moutend
Copy link

moutend commented Oct 5, 2015

  • What version of Go are you using (go version)?

    % go version
    go version go1.5.1 linux/arm

  • What operating system and processor architecture are you using?

    % echo $GOOS/$GOARCH $GOARM
    linux/arm 7

My machine is Rasberry Pi 2.

% uname -a
Linux raspberrypi 4.1.7-v7+ #817 SMP PREEMPT Sat Sep 19 15:32:00 BST 2015 armv7l GNU/Linux
% gcc --version | head -n 1
gcc (Raspbian 4.8.4-1) 4.8.4
  • What did you do?

I tried to build a c-shared object on Raspberry Pi 2, based on the articles below.

Here is Go code exports Sum function.

sum.go

sum.go

package main

import "C"

//export Sum
func Sum(a, b int) int {
        return a + b
}

func main() {}

And the following C code calls Sum function.

main.c

#include <stdio.h>
#include "sum.h"

int main() {
  printf("1 + 2 = %d\n", Sum(1, 2));
  return 0;
}

To build a shared object, I execute the commands below.

% go build -buildmode=c-shared -o ./sum.so ./sum.go
% gcc -c ./main.c
% gcc ./main.o ./sum.so

Then I run ./a.out, it segfaults.

% ./a.out
zsh: segmentation fault  ./a.out

What did I something wrong? Thanks.


FYI, buildmode=c-archive also occurs an error.

% go build -buildmode=c-archive -o ./sum.a ./sum.go
% gcc ./main.o ./sum.a
./sum.a(000001.o): In function `x_cgo_sys_thread_create':
/home/pi/tmp/go1.5/src/runtime/cgo/gcc_libinit.c:20: undefined reference to `pthread_create'
./sum.a(000001.o): In function `_cgo_sys_thread_start':
/home/pi/tmp/go1.5/src/runtime/cgo/gcc_linux_arm.c:25: undefined reference to `pthread_sigmask'
/home/pi/tmp/go1.5/src/runtime/cgo/gcc_linux_arm.c:33: undefined reference to `pthread_attr_getstacksize'
/home/pi/tmp/go1.5/src/runtime/cgo/gcc_linux_arm.c:36: undefined reference to `pthread_create'
/home/pi/tmp/go1.5/src/runtime/cgo/gcc_linux_arm.c:38: undefined reference to `pthread_sigmask'
./sum.a(000001.o): In function `x_cgo_init':
/home/pi/tmp/go1.5/src/runtime/cgo/gcc_linux_arm.c:66: undefined reference to `pthread_attr_getstacksize'
collect2: error: ld returned 1 exit status

So ./sum.a works fine when I passed gcc -pthread option.

% gcc -pthread ./main.o ./sum.a
% ./a.out
1 + 2 = 3

However, ./sum.so segfaults even though I passed gcc -pthread.

% gcc -pthread ./main.o ./sum.so
% ./a.out
zsh: segmentation fault  ./a.out
@ianlancetaylor
Copy link
Member

CC @mwhudson

@mwhudson
Copy link
Contributor

mwhudson commented Oct 5, 2015

Can you try this on tip?

@moutend
Copy link
Author

moutend commented Oct 6, 2015

@mwhudson What do you mean?

@mwhudson
Copy link
Contributor

mwhudson commented Oct 6, 2015

I mean with a version of go built from current git, not the 1.5.1 release. I've changed some things around how c-shared works on ARM since the release and it would be interesting to know if they help.

@moutend
Copy link
Author

moutend commented Oct 6, 2015

Oh I'm sorry, I forgot to switch the branch to Go 1.5. Should I close this issue and re-post?

@mwhudson
Copy link
Contributor

mwhudson commented Oct 6, 2015

Are you saying your code succeeds with Go 1.5.1 but fails with Go top? Or the other way around? Or something else.

No need for a new issue.

@moutend
Copy link
Author

moutend commented Oct 6, 2015

Are you saying your code succeeds with Go 1.5.1 but fails with Go top?

The code does not succeeds with Go 1.5.1. I just tried the code on Go 1.5.1 because it is current release version. Now I test the code by using the latest master branch, the shared object sum.so works fine.

% go version
go version devel +0a486b8 Tue Oct 6 01:11:30 2015 +0000 linux/arm
% go build -buildmode=c-shared -o sum.so sum.go
% gcc ./main.o ./sum.so
% ./a.out
1 + 2 = 3

Thank you.

@moutend moutend closed this as completed Oct 6, 2015
@spetrovic77
Copy link
Contributor

I've run into the same issue with Go 1.5.1 on linux/arm, and my solution was to add:

-installsuffix=blah

to my "go build" command. Why that is required is a mystery to me, but it is required. (It's not required for linux/amd64 nor darwin/amd64, which we use extensively.)

I bet iant@ knows why this is required as he added these flags to tests in "misc/cgo/testcshared", which is how I found out the solution to my woes. (It fails in "save_g" otherwise, trying to access the G pointer from TLS.)

@ianlancetaylor
Copy link
Member

Using -installsuffix will force code to be recompiled. We may be a little confused about when it is needed with other options. If you still need to use it on tip, please open a different issue about that.

@golang golang locked and limited conversation to collaborators Oct 9, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants