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

all: add cgo support to the riscv port #36641

Closed
4a6f656c opened this issue Jan 19, 2020 · 17 comments
Closed

all: add cgo support to the riscv port #36641

4a6f656c opened this issue Jan 19, 2020 · 17 comments
Labels
arch-riscv Issues solely affecting the riscv64 architecture. FeatureRequest FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.

Comments

@4a6f656c
Copy link
Contributor

The riscv port does not have support for cgo - this should be added at some point in the future (hence this tracking bug).

@ALTree ALTree added FeatureRequest NeedsFix The path to resolution is known, but the work has not been done. labels Jan 20, 2020
@tklauser tklauser added the arch-riscv Issues solely affecting the riscv64 architecture. label Jan 20, 2020
@gopherbot
Copy link

Change https://golang.org/cl/215838 mentions this issue: cmd/dist: mark cgo as disabled on linux/riscv64

gopherbot pushed a commit that referenced this issue Jan 22, 2020
cgo is not currently supported on this platform.

Updates #27532 and #36641

Change-Id: I4b35f887e869ebc5c156dd754b1c79897a8c5800
Reviewed-on: https://go-review.googlesource.com/c/go/+/215838
Reviewed-by: Cherry Zhang <cherryyz@google.com>
@mwhudson
Copy link
Contributor

Is anyone actively working on this? We (Canonical) want it and may be able to spend some time on it (but maybe not very much).

@4a6f656c
Copy link
Contributor Author

@mwhudson - I'm not aware of anyone currently working on this. The bigger issue/blocker is the lack of external (or internal cgo) linking support. Once that is implemented cgo should be reasonably straight forward.

lucaskanashiro added a commit to lucaskanashiro/debian-containerd that referenced this issue May 15, 2020
As this plugin needs cgo and riscv64 doesn't support. These changes are
based on this Debian commit:

https://salsa.debian.org/go-team/packages/containerd/-/commit/e3c6a7bb

This is the upstream bug report:

golang/go#36641
@4a6f656c
Copy link
Contributor Author

I have cgo working on linux/riscv64 - I'll attempt to get this cleaned up and ready for the next development cycle:

$ uname -a
Linux ubuntu 5.3.0-24-generic #26+riscv64.2-Ubuntu SMP Tue Jan 7 12:04:36 UTC 2020 riscv64 riscv64 riscv64 GNU/Linux
$ cat cgo.go
package main

import "runtime"

/*
#include <stdio.h>

void hello(char *goos, char *goarch) {
        printf("Hi from cgo on %s/%s!\n", goos, goarch);
}
*/
import "C"

func main() {
        goos := C.CString(runtime.GOOS)
        goarch := C.CString(runtime.GOARCH)
        C.hello(goos, goarch)
}
$ ~/src/go/bin/go build
$ ./cgo 
Hi from cgo on linux/riscv64!

@4a6f656c
Copy link
Contributor Author

There is a branch here that adds cgo support for linux/riscv64 on Go 1.14.2.

@carlosedp
Copy link

I've been testing it on some projects for a while and both runc (Container Runtime) and SQLite module for Go build and work fine.

Runc only needed an adjust in the Makefile since it also uses buildmode=pie. I removed it and it built and worked perfectly.

Amazing work @4a6f656c :)

@mwhudson
Copy link
Contributor

I think buildmode=pie support is just a matter of marking it as enabled in a few places: https://git.launchpad.net/~usd-import-team/ubuntu/+source/golang-1.14/commit/?h=applied/ubuntu/groovy-proposed&id=05db5462189beacd08bb3ac6bd2be2cf7e7fd9dd

@4a6f656c
Copy link
Contributor Author

@carlosedp - thanks, glad to hear it.

FWIW there is a commit on the top of https://github.com/4a6f656c/go/tree/riscv64-cgo that enables -buildmode=pie (essentially the same diff as @mwhudson references).

@carlosedp
Copy link

I've tested it by building runc with buildmode=pie and it works as expected.
Thanks @4a6f656c !

@carlosedp
Copy link

Hi @4a6f656c , any news on upstreaming CGO/PIE support for the next Go release? I believe the 1.16 freeze starts in november.

Thanks!

@4a6f656c
Copy link
Contributor Author

4a6f656c commented Sep 2, 2020

Hi @4a6f656c , any news on upstreaming CGO/PIE support for the next Go release? I believe the 1.16 freeze starts in november.

The first step is to get external linking support - a review for that is currently pending:

https://go-review.googlesource.com/c/go/+/243517

@gopherbot
Copy link

Change https://golang.org/cl/263477 mentions this issue: cmd/compile,cmd/internal/obj/riscv: move g register on riscv64

@gopherbot
Copy link

Change https://golang.org/cl/263478 mentions this issue: cmd/link,cmd/internal/obj/riscv: add TLS support for linux/riscv64

@gopherbot
Copy link

Change https://golang.org/cl/263598 mentions this issue: cmd/dist,cmd/go,runtime: add support for cgo on linux/riscv64

@gopherbot
Copy link

Change https://golang.org/cl/263601 mentions this issue: cmd/link: add loadelf support for riscv64

ricardosalveti added a commit to ricardosalveti/meta-lmp that referenced this issue Oct 21, 2020
Backport patches from
https://github.com/4a6f656c/go/commits/riscv64-cgo-1.15 for risc-v
support for cgo.

Upstreaming is also in progress via
golang/go#36641.

Signed-off-by: Ricardo Salveti <ricardo@foundries.io>
gopherbot pushed a commit that referenced this issue Oct 21, 2020
The original riscv64 port used the thread pointer (TP aka X4) register for
the g pointer, however this register is also used when TLS support is
required, resulting in a conflict (for example, when a signal is received
we have no way of readily knowing if X4 contains a pointer to the TCB or
a pointer to a g).

In order to support cgo, free up the X4 register by moving g to X27.
This unfortunately means that the X4 register is unused in non-cgo mode,
however the alternative is to not support cgo on this platform.

Update #36641

Change-Id: Idcaf3e8ccbe42972a1b8943aeefde7149d9c960a
Reviewed-on: https://go-review.googlesource.com/c/go/+/263477
Trust: Joel Sing <joel@sing.id.au>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
gopherbot pushed a commit that referenced this issue Oct 27, 2020
Add support for Thread Local Storage (TLS) for linux/riscv64 with external
linking, using the initial-exec model.

Update #36641

Change-Id: I3106ef9a29cde73215830b00deff43dbec1c76e0
Reviewed-on: https://go-review.googlesource.com/c/go/+/263478
Trust: Joel Sing <joel@sing.id.au>
Run-TryBot: Joel Sing <joel@sing.id.au>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
gopherbot pushed a commit that referenced this issue Oct 29, 2020
Update #36641

Change-Id: I8618da30d8940a56d6cc86a37a2f54b31ee029e5
Reviewed-on: https://go-review.googlesource.com/c/go/+/263601
Trust: Joel Sing <joel@sing.id.au>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Run-TryBot: Joel Sing <joel@sing.id.au>
TryBot-Result: Go Bot <gobot@golang.org>
@DavidBurela
Copy link

Just a note to thank you for completing and closing this issue.
I hit this exact RISC-V cgo issues on a repo that I was trying to compile https://github.com/ethereum/go-ethereum

It wouldn't compile in 1.15.5 due to

/usr/bin/ld: $WORK/b009/_x008.o: in function `x_cgo_thread_start':
/usr/lib/go-1.15/src/runtime/cgo/gcc_util.c:21: undefined reference to `_cgo_sys_thread_start'
collect2: error: ld returned 1 exit status

which was the same error message I could repro with @4a6f656c sample cgo.go file above.

I pulled the golang source, built master, and can confirm that cgo works fine on RISC-V and I was able to successfully compile the project. Thanks heaps for your hard work resolving this.

@gopherbot
Copy link

Change https://golang.org/cl/274478 mentions this issue: doc/go1.16: document riscv64 port changes

gopherbot pushed a commit that referenced this issue Dec 7, 2020
For #36641
For #40700

Change-Id: Ib268559a2ce7839372dbf273d95876d8d4521a45
Reviewed-on: https://go-review.googlesource.com/c/go/+/274478
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Joel Sing <joel@sing.id.au>
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-riscv Issues solely affecting the riscv64 architecture. FeatureRequest FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants