Commit 95963e62 authored by Russ Cox's avatar Russ Cox

runtime/cgo: fix for OS X 10.7

Correct a few error messages (libcgo -> runtime/cgo)
and delete old nacl_386.c file too.

Fixes #1657.

R=iant
CC=golang-dev
https://golang.org/cl/4603057
parent 2b08e952
...@@ -8,11 +8,13 @@ ...@@ -8,11 +8,13 @@
static void* threadentry(void*); static void* threadentry(void*);
static pthread_key_t k1, k2; static pthread_key_t k1, k2;
#define magic1 (0x23581321U)
static void static void
inittls(void) inittls(void)
{ {
uint32 x, y; uint32 x, y;
pthread_key_t tofree[16], k; pthread_key_t tofree[128], k;
int i, ntofree; int i, ntofree;
int havek1, havek2; int havek1, havek2;
...@@ -35,9 +37,8 @@ inittls(void) ...@@ -35,9 +37,8 @@ inittls(void)
* 0x48+4*0x108 = 0x468 and 0x48+4*0x109 = 0x46c. * 0x48+4*0x108 = 0x468 and 0x48+4*0x109 = 0x46c.
* *
* The linker and runtime hard-code these constant offsets * The linker and runtime hard-code these constant offsets
* from %gs where we expect to find m and g. The code * from %gs where we expect to find m and g.
* below verifies that the constants are correct once it has * Known to ../cmd/8l/obj.c:/468
* obtained the keys. Known to ../cmd/8l/obj.c:/468
* and to ../pkg/runtime/darwin/386/sys.s:/468 * and to ../pkg/runtime/darwin/386/sys.s:/468
* *
* This is truly disgusting and a bit fragile, but taking care * This is truly disgusting and a bit fragile, but taking care
...@@ -48,55 +49,54 @@ inittls(void) ...@@ -48,55 +49,54 @@ inittls(void)
* require an extra instruction and memory reference in * require an extra instruction and memory reference in
* every stack growth prolog and would also require * every stack growth prolog and would also require
* rewriting the code that 8c generates for extern registers. * rewriting the code that 8c generates for extern registers.
*
* Things get more disgusting on OS X 10.7 Lion.
* The 0x48 base mentioned above is the offset of the tsd
* array within the per-thread structure on Leopard and Snow Leopard.
* On Lion, the base moved a little, so while the math above
* still applies, the base is different. Thus, we cannot
* look for specific key values if we want to build binaries
* that run on both systems. Instead, forget about the
* specific key values and just allocate and initialize per-thread
* storage until we find a key that writes to the memory location
* we want. Then keep that key.
*/ */
havek1 = 0; havek1 = 0;
havek2 = 0; havek2 = 0;
ntofree = 0; ntofree = 0;
while(!havek1 || !havek2) { while(!havek1 || !havek2) {
if(pthread_key_create(&k, nil) < 0) { if(pthread_key_create(&k, nil) < 0) {
fprintf(stderr, "libcgo: pthread_key_create failed\n"); fprintf(stderr, "runtime/cgo: pthread_key_create failed\n");
abort(); abort();
} }
if(k == 0x108) { pthread_setspecific(k, (void*)magic1);
asm volatile("movl %%gs:0x468, %0" : "=r"(x));
asm volatile("movl %%gs:0x46c, %0" : "=r"(y));
if(x == magic1) {
havek1 = 1; havek1 = 1;
k1 = k; k1 = k;
continue; } else if(y == magic1) {
}
if(k == 0x109) {
havek2 = 1; havek2 = 1;
k2 = k; k2 = k;
continue; } else {
}
if(ntofree >= nelem(tofree)) { if(ntofree >= nelem(tofree)) {
fprintf(stderr, "libcgo: could not obtain pthread_keys\n"); fprintf(stderr, "runtime/cgo: could not obtain pthread_keys\n");
fprintf(stderr, "\twanted 0x108 and 0x109\n"); fprintf(stderr, "\ttried");
fprintf(stderr, "\tgot");
for(i=0; i<ntofree; i++) for(i=0; i<ntofree; i++)
fprintf(stderr, " %#lx", tofree[i]); fprintf(stderr, " %#x", (unsigned)tofree[i]);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
abort(); abort();
} }
tofree[ntofree++] = k; tofree[ntofree++] = k;
} }
pthread_setspecific(k, 0);
for(i=0; i<ntofree; i++) }
pthread_key_delete(tofree[i]);
/* /*
* We got the keys we wanted. Make sure that we observe * We got the keys we wanted. Free the others.
* updates to k1 at 0x468, to verify that the TLS array
* offset from %gs hasn't changed.
*/ */
pthread_setspecific(k1, (void*)0x12345678); for(i=0; i<ntofree; i++)
asm volatile("movl %%gs:0x468, %0" : "=r"(x)); pthread_key_delete(tofree[i]);
pthread_setspecific(k1, (void*)0x87654321);
asm volatile("movl %%gs:0x468, %0" : "=r"(y));
if(x != 0x12345678 || y != 0x87654321) {
printf("libcgo: thread-local storage %#lx not at %%gs:0x468 - x=%#x y=%#x\n", k1, x, y);
abort();
}
} }
static void static void
......
...@@ -8,24 +8,25 @@ ...@@ -8,24 +8,25 @@
static void* threadentry(void*); static void* threadentry(void*);
static pthread_key_t k1, k2; static pthread_key_t k1, k2;
#define magic1 (0x23581321345589ULL)
static void static void
inittls(void) inittls(void)
{ {
uint64 x, y; uint64 x, y;
pthread_key_t tofree[16], k; pthread_key_t tofree[128], k;
int i, ntofree; int i, ntofree;
int havek1, havek2; int havek1, havek2;
/* /*
* Same logic, code as darwin_386.c:/inittls, except that words * Same logic, code as darwin_386.c:/inittls, except that words
* are 8 bytes long now, and the thread-local storage starts at 0x60. * are 8 bytes long now, and the thread-local storage starts
* So the offsets are * at 0x60 on Leopard / Snow Leopard. So the offsets are
* 0x60+8*0x108 = 0x8a0 and 0x60+8*0x109 = 0x8a8. * 0x60+8*0x108 = 0x8a0 and 0x60+8*0x109 = 0x8a8.
* *
* The linker and runtime hard-code these constant offsets * The linker and runtime hard-code these constant offsets
* from %gs where we expect to find m and g. The code * from %gs where we expect to find m and g.
* below verifies that the constants are correct once it has * Known to ../cmd/6l/obj.c:/8a0
* obtained the keys. Known to ../cmd/6l/obj.c:/8a0
* and to ../pkg/runtime/darwin/amd64/sys.s:/8a0 * and to ../pkg/runtime/darwin/amd64/sys.s:/8a0
* *
* As disgusting as on the 386; same justification. * As disgusting as on the 386; same justification.
...@@ -35,23 +36,22 @@ inittls(void) ...@@ -35,23 +36,22 @@ inittls(void)
ntofree = 0; ntofree = 0;
while(!havek1 || !havek2) { while(!havek1 || !havek2) {
if(pthread_key_create(&k, nil) < 0) { if(pthread_key_create(&k, nil) < 0) {
fprintf(stderr, "libcgo: pthread_key_create failed\n"); fprintf(stderr, "runtime/cgo: pthread_key_create failed\n");
abort(); abort();
} }
if(k == 0x108) { pthread_setspecific(k, (void*)magic1);
asm volatile("movq %%gs:0x8a0, %0" : "=r"(x));
asm volatile("movq %%gs:0x8a8, %0" : "=r"(y));
if(x == magic1) {
havek1 = 1; havek1 = 1;
k1 = k; k1 = k;
continue; } else if(y == magic1) {
}
if(k == 0x109) {
havek2 = 1; havek2 = 1;
k2 = k; k2 = k;
continue; } else {
}
if(ntofree >= nelem(tofree)) { if(ntofree >= nelem(tofree)) {
fprintf(stderr, "libcgo: could not obtain pthread_keys\n"); fprintf(stderr, "runtime/cgo: could not obtain pthread_keys\n");
fprintf(stderr, "\twanted 0x108 and 0x109\n"); fprintf(stderr, "\ttried");
fprintf(stderr, "\tgot");
for(i=0; i<ntofree; i++) for(i=0; i<ntofree; i++)
fprintf(stderr, " %#x", (unsigned)tofree[i]); fprintf(stderr, " %#x", (unsigned)tofree[i]);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
...@@ -59,25 +59,14 @@ inittls(void) ...@@ -59,25 +59,14 @@ inittls(void)
} }
tofree[ntofree++] = k; tofree[ntofree++] = k;
} }
pthread_setspecific(k, 0);
for(i=0; i<ntofree; i++) }
pthread_key_delete(tofree[i]);
/* /*
* We got the keys we wanted. Make sure that we observe * We got the keys we wanted. Free the others.
* updates to k1 at 0x8a0, to verify that the TLS array
* offset from %gs hasn't changed.
*/ */
pthread_setspecific(k1, (void*)0x123456789abcdef0ULL); for(i=0; i<ntofree; i++)
asm volatile("movq %%gs:0x8a0, %0" : "=r"(x)); pthread_key_delete(tofree[i]);
pthread_setspecific(k2, (void*)0x0fedcba987654321);
asm volatile("movq %%gs:0x8a8, %0" : "=r"(y));
if(x != 0x123456789abcdef0ULL || y != 0x0fedcba987654321) {
printf("libcgo: thread-local storage %#x not at %%gs:0x8a0 - x=%#llx y=%#llx\n", (unsigned)k1, x, y);
abort();
}
} }
void void
......
// Copyright 2010 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 "libcgo.h"
static void
xinitcgo(void)
{
}
void (*initcgo)(void) = xinitcgo;
void
libcgo_sys_thread_start(ThreadStart *ts)
{
// unimplemented
*(int*)0 = 0;
}
...@@ -40,7 +40,7 @@ xlibcgo_thread_start(ThreadStart *arg) ...@@ -40,7 +40,7 @@ xlibcgo_thread_start(ThreadStart *arg)
/* Make our own copy that can persist after we return. */ /* Make our own copy that can persist after we return. */
ts = malloc(sizeof *ts); ts = malloc(sizeof *ts);
if(ts == nil) { if(ts == nil) {
fprintf(stderr, "libcgo: out of memory in thread_start\n"); fprintf(stderr, "runtime/cgo: out of memory in thread_start\n");
abort(); abort();
} }
*ts = *arg; *ts = *arg;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment