From ecfec2c61a1d3945ee382b95f1ce2dca97223234 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 11 Jul 2011 23:38:02 -0400 Subject: [release-branch.r58] runtime/cgo: fix for OS X 10.7 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ««« CL 4603057 / 0905a2ca94c6 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 »»» R=adg CC=golang-dev https://golang.org/cl/4698041 --- src/pkg/runtime/cgo/darwin_386.c | 72 +++++++++++++++++++------------------- src/pkg/runtime/cgo/darwin_amd64.c | 65 ++++++++++++++-------------------- src/pkg/runtime/cgo/nacl_386.c | 19 ---------- src/pkg/runtime/cgo/util.c | 2 +- 4 files changed, 64 insertions(+), 94 deletions(-) delete mode 100644 src/pkg/runtime/cgo/nacl_386.c diff --git a/src/pkg/runtime/cgo/darwin_386.c b/src/pkg/runtime/cgo/darwin_386.c index 4fc7eb4e0a..13184f3217 100644 --- a/src/pkg/runtime/cgo/darwin_386.c +++ b/src/pkg/runtime/cgo/darwin_386.c @@ -8,11 +8,13 @@ static void* threadentry(void*); static pthread_key_t k1, k2; +#define magic1 (0x23581321U) + static void inittls(void) { uint32 x, y; - pthread_key_t tofree[16], k; + pthread_key_t tofree[128], k; int i, ntofree; int havek1, havek2; @@ -35,9 +37,8 @@ inittls(void) * 0x48+4*0x108 = 0x468 and 0x48+4*0x109 = 0x46c. * * The linker and runtime hard-code these constant offsets - * from %gs where we expect to find m and g. The code - * below verifies that the constants are correct once it has - * obtained the keys. Known to ../cmd/8l/obj.c:/468 + * from %gs where we expect to find m and g. + * Known to ../cmd/8l/obj.c:/468 * and to ../pkg/runtime/darwin/386/sys.s:/468 * * This is truly disgusting and a bit fragile, but taking care @@ -48,55 +49,54 @@ inittls(void) * require an extra instruction and memory reference in * every stack growth prolog and would also require * 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; havek2 = 0; ntofree = 0; while(!havek1 || !havek2) { 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(); } - 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; k1 = k; - continue; - } - if(k == 0x109) { + } else if(y == magic1) { havek2 = 1; k2 = k; - continue; + } else { + if(ntofree >= nelem(tofree)) { + fprintf(stderr, "runtime/cgo: could not obtain pthread_keys\n"); + fprintf(stderr, "\ttried"); + for(i=0; i= nelem(tofree)) { - fprintf(stderr, "libcgo: could not obtain pthread_keys\n"); - fprintf(stderr, "\twanted 0x108 and 0x109\n"); - fprintf(stderr, "\tgot"); - for(i=0; i= nelem(tofree)) { - fprintf(stderr, "libcgo: could not obtain pthread_keys\n"); - fprintf(stderr, "\twanted 0x108 and 0x109\n"); - fprintf(stderr, "\tgot"); - for(i=0; i= nelem(tofree)) { + fprintf(stderr, "runtime/cgo: could not obtain pthread_keys\n"); + fprintf(stderr, "\ttried"); + for(i=0; i