]> Cypherpunks repositories - gostls13.git/commitdiff
runtime,cmd/dist,cmd/link: add cgo support on openbsd/arm
authorJoel Sing <joel@sing.id.au>
Sun, 16 Dec 2018 14:18:51 +0000 (01:18 +1100)
committerJoel Sing <joel@sing.id.au>
Tue, 18 Dec 2018 12:02:45 +0000 (12:02 +0000)
Add support for cgo on openbsd/arm.The gcc shipped with base OpenBSD armv7
is old/inadequate, so use clang by default.

Change-Id: I945a26d369378952d357727718e69249411e1127
Reviewed-on: https://go-review.googlesource.com/c/154381
Run-TryBot: Joel Sing <joel@sing.id.au>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/dist/build.go
src/cmd/dist/main.go
src/cmd/link/internal/ld/lib.go
src/runtime/cgo/gcc_openbsd_arm.c [new file with mode: 0644]
src/runtime/sys_openbsd_arm.s

index 8d7b14d17c65020c8fcbafd56b38e2abf6244e9b..da677c81ada5a3e7dfe4a77b6ada2bacfcd6b68f 100644 (file)
@@ -1448,7 +1448,7 @@ var cgoEnabled = map[string]bool{
        "netbsd/arm":      true,
        "openbsd/386":     true,
        "openbsd/amd64":   true,
-       "openbsd/arm":     false,
+       "openbsd/arm":     true,
        "plan9/386":       false,
        "plan9/amd64":     false,
        "plan9/arm":       false,
index bf08869afb7f2565bfa0c72f08ba2d10f3e49f79..bab8ab781ab677e5b0a1ed3f3fade778ff614d0f 100644 (file)
@@ -65,6 +65,13 @@ func main() {
        case "freebsd":
                // Since FreeBSD 10 gcc is no longer part of the base system.
                defaultclang = true
+       case "openbsd":
+               // The gcc available on OpenBSD armv7 is old/inadequate (for example, lacks
+               // __sync_fetch_and_*/__sync_*_and_fetch) and will likely be removed in the
+               // not-to-distant future - use clang instead.
+               if runtime.GOARCH == "arm" {
+                       defaultclang = true
+               }
        case "solaris":
                // Even on 64-bit platform, solaris uname -m prints i86pc.
                out := run("", CheckExit, "isainfo", "-n")
index 253a9f68475c6144e34d5bc1213821dbb6a02c60..b45397e7278a28a860a6a6e63f4f948125d42a66 100644 (file)
@@ -535,6 +535,12 @@ func (ctxt *Link) loadlib() {
                        if *flagLibGCC == "" {
                                *flagLibGCC = ctxt.findLibPathCmd("--print-libgcc-file-name", "libgcc")
                        }
+                       if runtime.GOOS == "openbsd" && *flagLibGCC == "libgcc.a" {
+                               // On OpenBSD `clang --print-libgcc-file-name` returns "libgcc.a".
+                               // In this case we fail to load libgcc.a and can encounter link
+                               // errors - see if we can find libcompiler_rt.a instead.
+                               *flagLibGCC = ctxt.findLibPathCmd("--print-file-name=libcompiler_rt.a", "libcompiler_rt")
+                       }
                        if *flagLibGCC != "none" {
                                hostArchive(ctxt, *flagLibGCC)
                        }
diff --git a/src/runtime/cgo/gcc_openbsd_arm.c b/src/runtime/cgo/gcc_openbsd_arm.c
new file mode 100644 (file)
index 0000000..9a5757f
--- /dev/null
@@ -0,0 +1,67 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include <sys/types.h>
+#include <pthread.h>
+#include <signal.h>
+#include <string.h>
+#include "libcgo.h"
+#include "libcgo_unix.h"
+
+static void* threadentry(void*);
+static void (*setg_gcc)(void*);
+
+void
+x_cgo_init(G *g, void (*setg)(void*))
+{
+       pthread_attr_t attr;
+       size_t size;
+
+       setg_gcc = setg;
+       pthread_attr_init(&attr);
+       pthread_attr_getstacksize(&attr, &size);
+       g->stacklo = (uintptr)&attr - size + 4096;
+       pthread_attr_destroy(&attr);
+}
+
+void
+_cgo_sys_thread_start(ThreadStart *ts)
+{
+       pthread_attr_t attr;
+       sigset_t ign, oset;
+       pthread_t p;
+       size_t size;
+       int err;
+
+       sigfillset(&ign);
+       pthread_sigmask(SIG_SETMASK, &ign, &oset);
+
+       pthread_attr_init(&attr);
+       pthread_attr_getstacksize(&attr, &size);
+
+       // Leave stacklo=0 and set stackhi=size; mstart will do the rest.
+       ts->g->stackhi = size;
+       err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
+
+       pthread_sigmask(SIG_SETMASK, &oset, nil);
+
+       if (err != 0) {
+               fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
+               abort();
+       }
+}
+
+extern void crosscall_arm1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
+
+static void*
+threadentry(void *v)
+{
+       ThreadStart ts;
+
+       ts = *(ThreadStart*)v;
+       free(v);
+
+       crosscall_arm1(ts.fn, setg_gcc, (void*)ts.g);
+       return nil;
+}
index 52d3638bc18c1f5e7e2e1c0021584691dab5ca99..94ac5d599d2fd01dc905b9d4ddff75d5d447383a 100644 (file)
@@ -371,8 +371,9 @@ TEXT runtime·closeonexec(SB),NOSPLIT,$0
 TEXT ·publicationBarrier(SB),NOSPLIT|NOFRAME,$0-0
        B       runtime·armPublicationBarrier(SB)
 
-// TODO(jsing): Implement.
 TEXT runtime·read_tls_fallback(SB),NOSPLIT|NOFRAME,$0
-       MOVW    $5, R0
-       MOVW    R0, (R0)
+       MOVM.WP [R1, R2, R3, R12], (R13)
+       MOVW    $330, R12               // sys___get_tcb
+       SWI     $0
+       MOVM.IAW (R13), [R1, R2, R3, R12]
        RET