Skip to content

Commit f68878f

Browse files
committed
cmd/dist,runtime: support cgo on openbsd/mips64
Add support for cgo on openbsd/mips64. Fixes #43005 Change-Id: I2386204f53fa984a01a9d89f0b6c96455768f326 Reviewed-on: https://go-review.googlesource.com/c/go/+/275896 Trust: Joel Sing <[email protected]> Run-TryBot: Joel Sing <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent 92d1afe commit f68878f

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
lines changed

src/cmd/dist/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ var cgoEnabled = map[string]bool{
15901590
"openbsd/amd64": true,
15911591
"openbsd/arm": true,
15921592
"openbsd/arm64": true,
1593-
"openbsd/mips64": false,
1593+
"openbsd/mips64": true,
15941594
"plan9/386": false,
15951595
"plan9/amd64": false,
15961596
"plan9/arm": false,

src/cmd/link/internal/mips64/asm.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func elfreloc1(ctxt *ld.Link, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym,
5252
// type uint8
5353
// addend int64
5454

55+
addend := r.Xadd
56+
5557
out.Write64(uint64(sectoff))
5658

5759
elfsym := ld.ElfSymForReloc(ctxt, r.Xsym)
@@ -77,11 +79,17 @@ func elfreloc1(ctxt *ld.Link, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym,
7779
out.Write8(uint8(elf.R_MIPS_HI16))
7880
case objabi.R_ADDRMIPSTLS:
7981
out.Write8(uint8(elf.R_MIPS_TLS_TPREL_LO16))
82+
if ctxt.Target.IsOpenbsd() {
83+
// OpenBSD mips64 does not currently offset TLS by 0x7000,
84+
// as such we need to add this back to get the correct offset
85+
// via the external linker.
86+
addend += 0x7000
87+
}
8088
case objabi.R_CALLMIPS,
8189
objabi.R_JMPMIPS:
8290
out.Write8(uint8(elf.R_MIPS_26))
8391
}
84-
out.Write64(uint64(r.Xadd))
92+
out.Write64(uint64(addend))
8593

8694
return true
8795
}
@@ -124,6 +132,11 @@ func archreloc(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, r loade
124132
case objabi.R_ADDRMIPSTLS:
125133
// thread pointer is at 0x7000 offset from the start of TLS data area
126134
t := ldr.SymValue(rs) + r.Add() - 0x7000
135+
if target.IsOpenbsd() {
136+
// OpenBSD mips64 does not currently offset TLS by 0x7000,
137+
// as such we need to add this back to get the correct offset.
138+
t += 0x7000
139+
}
127140
if t < -32768 || t >= 32678 {
128141
ldr.Errorf(s, "TLS offset out of range %d", t)
129142
}

src/cmd/nm/nm_cgo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func canInternalLink() bool {
3030
}
3131
case "openbsd":
3232
switch runtime.GOARCH {
33-
case "arm64":
33+
case "arm64", "mips64":
3434
return false
3535
}
3636
case "windows":

src/runtime/cgo/gcc_openbsd_mips64.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
#include <sys/types.h>
6+
#include <pthread.h>
7+
#include <signal.h>
8+
#include <string.h>
9+
#include "libcgo.h"
10+
#include "libcgo_unix.h"
11+
12+
static void* threadentry(void*);
13+
static void (*setg_gcc)(void*);
14+
15+
void
16+
x_cgo_init(G *g, void (*setg)(void*))
17+
{
18+
pthread_attr_t attr;
19+
size_t size;
20+
21+
setg_gcc = setg;
22+
pthread_attr_init(&attr);
23+
pthread_attr_getstacksize(&attr, &size);
24+
g->stacklo = (uintptr)&attr - size + 4096;
25+
pthread_attr_destroy(&attr);
26+
}
27+
28+
void
29+
_cgo_sys_thread_start(ThreadStart *ts)
30+
{
31+
pthread_attr_t attr;
32+
sigset_t ign, oset;
33+
pthread_t p;
34+
size_t size;
35+
int err;
36+
37+
sigfillset(&ign);
38+
pthread_sigmask(SIG_SETMASK, &ign, &oset);
39+
40+
pthread_attr_init(&attr);
41+
pthread_attr_getstacksize(&attr, &size);
42+
43+
// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
44+
ts->g->stackhi = size;
45+
err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
46+
47+
pthread_sigmask(SIG_SETMASK, &oset, nil);
48+
49+
if (err != 0) {
50+
fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
51+
abort();
52+
}
53+
}
54+
55+
extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
56+
57+
static void*
58+
threadentry(void *v)
59+
{
60+
ThreadStart ts;
61+
62+
ts = *(ThreadStart*)v;
63+
free(v);
64+
65+
crosscall1(ts.fn, setg_gcc, (void*)ts.g);
66+
return nil;
67+
}

0 commit comments

Comments
 (0)