Add TLS support for AMD64, i386 and SH3.

This material is based upon work partially supported by
The NetBSD Foundation under a contract with Joerg Sonnenberger.
This commit is contained in:
joerg 2011-03-12 22:54:36 +00:00
parent 8fe5cd0494
commit 854e6cd69f
8 changed files with 179 additions and 14 deletions

View File

@ -1,11 +1,12 @@
/* $NetBSD: mdreloc.c,v 1.32 2010/08/06 16:33:18 joerg Exp $ */
/* $NetBSD: mdreloc.c,v 1.33 2011/03/12 22:54:36 joerg Exp $ */
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: mdreloc.c,v 1.32 2010/08/06 16:33:18 joerg Exp $");
__RCSID("$NetBSD: mdreloc.c,v 1.33 2011/03/12 22:54:36 joerg Exp $");
#endif /* not lint */
#include <sys/types.h>
#include <sys/ucontext.h>
#include "debug.h"
#include "rtld.h"
@ -119,6 +120,47 @@ _rtld_relocate_nonplt_objects(Obj_Entry *obj)
rdbg(("COPY (avoid in main)"));
break;
case R_TYPE(TLS_TPOFF):
def = _rtld_find_symdef(symnum, obj, &defobj, false);
if (def == NULL)
return -1;
if (!defobj->tls_done &&
_rtld_tls_offset_allocate(obj))
return -1;
*where = (Elf_Addr)(def->st_value - defobj->tlsoffset);
rdbg(("TLS_TPOFF %s in %s --> %p",
obj->strtab + obj->symtab[symnum].st_name,
obj->path, (void *)*where));
break;
case R_TYPE(TLS_DTPMOD32):
def = _rtld_find_symdef(symnum, obj, &defobj, false);
if (def == NULL)
return -1;
*where = (Elf_Addr)(defobj->tlsindex);
rdbg(("TLS_DTPMOD32 %s in %s --> %p",
obj->strtab + obj->symtab[symnum].st_name,
obj->path, (void *)*where));
break;
case R_TYPE(TLS_DTPOFF32):
def = _rtld_find_symdef(symnum, obj, &defobj, false);
if (def == NULL)
return -1;
*where = (Elf_Addr)(def->st_value);
rdbg(("TLS_DTPOFF32 %s in %s --> %p",
obj->strtab + obj->symtab[symnum].st_name,
obj->path, (void *)*where));
break;
default:
rdbg(("sym = %lu, type = %lu, offset = %p, "
"contents = %p, symbol = %s",
@ -214,3 +256,25 @@ _rtld_relocate_plt_objects(const Obj_Entry *obj)
}
return err;
}
/*
* i386 specific GNU variant of __tls_get_addr using register based
* argument passing.
*/
#define DTV_MAX_INDEX(dtv) ((size_t)((dtv)[-1]))
__dso_public __attribute__((__regparm__(1))) void *
___tls_get_addr(void *arg_)
{
size_t *arg = (size_t *)arg_;
void **dtv;
struct tls_tcb *tcb = __lwp_getprivate_fast();
size_t idx = arg[0], offset = arg[1];
dtv = tcb->tcb_dtv;
if (__predict_true(idx < DTV_MAX_INDEX(dtv) && dtv[idx] != NULL))
return (uint8_t *)dtv[idx] + offset;
return _rtld_tls_get_addr(tcb, idx, offset);
}

View File

@ -1,16 +1,17 @@
/* $NetBSD: mdreloc.c,v 1.28 2010/08/06 16:33:18 joerg Exp $ */
/* $NetBSD: mdreloc.c,v 1.29 2011/03/12 22:54:36 joerg Exp $ */
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: mdreloc.c,v 1.28 2010/08/06 16:33:18 joerg Exp $");
__RCSID("$NetBSD: mdreloc.c,v 1.29 2011/03/12 22:54:36 joerg Exp $");
#endif /* not lint */
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: mdreloc.c,v 1.28 2010/08/06 16:33:18 joerg Exp $");
__RCSID("$NetBSD: mdreloc.c,v 1.29 2011/03/12 22:54:36 joerg Exp $");
#endif /* not lint */
#include <sys/types.h>
#include <sys/tls.h>
#include "debug.h"
#include "rtld.h"
@ -154,6 +155,49 @@ _rtld_relocate_nonplt_objects(Obj_Entry *obj)
rdbg(("COPY (avoid in main)"));
break;
case R_TYPE(TLS_DTPOFF32):
def = _rtld_find_symdef(symnum, obj, &defobj, false);
if (def == NULL)
return -1;
*where = (Elf_Addr)(def->st_value);
rdbg(("TLS_DTPOFF32 %s in %s --> %p",
obj->strtab + obj->symtab[symnum].st_name,
obj->path, (void *)*where));
break;
case R_TYPE(TLS_DTPMOD32):
def = _rtld_find_symdef(symnum, obj, &defobj, false);
if (def == NULL)
return -1;
*where = (Elf_Addr)(defobj->tlsindex);
rdbg(("TLS_DTPMOD32 %s in %s --> %p",
obj->strtab + obj->symtab[symnum].st_name,
obj->path, (void *)*where));
break;
case R_TYPE(TLS_TPOFF32):
def = _rtld_find_symdef(symnum, obj, &defobj, false);
if (def == NULL)
return -1;
if (!defobj->tls_done &&
_rtld_tls_offset_allocate(obj))
return -1;
*where = (Elf_Addr)def->st_value +
rela->r_addend + defobj->tlsoffset +
sizeof(struct tls_tcb);
rdbg(("TLS_TPOFF32 %s in %s --> %p",
obj->strtab + obj->symtab[symnum].st_name,
obj->path, (void *)*where));
break;
default:
rdbg(("sym = %lu, type = %lu, offset = %p, "
"addend = %p, contents = %p, symbol = %s",

View File

@ -1,4 +1,4 @@
/* $NetBSD: mdreloc.c,v 1.38 2010/08/06 16:33:19 joerg Exp $ */
/* $NetBSD: mdreloc.c,v 1.39 2011/03/12 22:54:36 joerg Exp $ */
/*
* Copyright (c) 2001 Wasabi Systems, Inc.
@ -68,7 +68,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: mdreloc.c,v 1.38 2010/08/06 16:33:19 joerg Exp $");
__RCSID("$NetBSD: mdreloc.c,v 1.39 2011/03/12 22:54:36 joerg Exp $");
#endif /* not lint */
#include <sys/types.h>
@ -161,7 +161,7 @@ _rtld_relocate_nonplt_objects(Obj_Entry *obj)
*where32 = tmp32;
rdbg(("32/32S %s in %s --> %p in %s",
obj->strtab + obj->symtab[symnum].st_name,
obj->path, (void *)(unsigned long)*where32,
obj->path, (void *)(uintptr_t)*where32,
defobj->path));
break;
case R_TYPE(64): /* word64 S + A */
@ -211,6 +211,50 @@ _rtld_relocate_nonplt_objects(Obj_Entry *obj)
(void *)*where64));
break;
case R_TYPE(TPOFF64):
def = _rtld_find_symdef(symnum, obj, &defobj, false);
if (def == NULL)
return -1;
if (!defobj->tls_done &&
_rtld_tls_offset_allocate(obj))
return -1;
*where64 = (Elf64_Addr)(def->st_value -
defobj->tlsoffset + rela->r_addend);
rdbg(("TPOFF64 %s in %s --> %p",
obj->strtab + obj->symtab[symnum].st_name,
obj->path, (void *)*where64));
break;
case R_TYPE(DTPMOD64):
def = _rtld_find_symdef(symnum, obj, &defobj, false);
if (def == NULL)
return -1;
*where64 = (Elf64_Addr)defobj->tlsindex;
rdbg(("DTPMOD64 %s in %s --> %p",
obj->strtab + obj->symtab[symnum].st_name,
obj->path, (void *)*where64));
break;
case R_TYPE(DTPOFF64):
def = _rtld_find_symdef(symnum, obj, &defobj, false);
if (def == NULL)
return -1;
*where64 = (Elf64_Addr)(def->st_value + rela->r_addend);
rdbg(("DTPOFF64 %s in %s --> %p",
obj->strtab + obj->symtab[symnum].st_name,
obj->path, (void *)*where64));
break;
case R_TYPE(COPY):
rdbg(("COPY"));
break;

View File

@ -1,4 +1,4 @@
/* $NetBSD: rtld.h,v 1.102 2011/03/10 14:27:31 joerg Exp $ */
/* $NetBSD: rtld.h,v 1.103 2011/03/12 22:54:36 joerg Exp $ */
/*
* Copyright 1996 John D. Polstra.
@ -375,6 +375,10 @@ extern size_t _rtld_tls_dtv_generation;
extern size_t _rtld_tls_max_index;
__dso_public extern void *__tls_get_addr(void *);
#ifdef __i386__
__dso_public extern void *___tls_get_addr(void *)
__attribute__((__regparm__(1)));
#endif
#endif
/* map_object.c */

View File

@ -1,4 +1,4 @@
/* $NetBSD: symbol.c,v 1.55 2011/03/09 23:10:07 joerg Exp $ */
/* $NetBSD: symbol.c,v 1.56 2011/03/12 22:54:36 joerg Exp $ */
/*
* Copyright 1996 John D. Polstra.
@ -40,7 +40,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: symbol.c,v 1.55 2011/03/09 23:10:07 joerg Exp $");
__RCSID("$NetBSD: symbol.c,v 1.56 2011/03/12 22:54:36 joerg Exp $");
#endif /* not lint */
#include <err.h>
@ -97,6 +97,9 @@ _rtld_is_exported(const Elf_Sym *def)
(fptr_t)_rtld_tls_allocate,
(fptr_t)_rtld_tls_free,
(fptr_t)__tls_get_addr,
#ifdef __i386__
(fptr_t)___tls_get_addr,
#endif
#endif
NULL
};

View File

@ -1,4 +1,4 @@
/* $NetBSD: types.h,v 1.36 2011/02/24 04:28:45 joerg Exp $ */
/* $NetBSD: types.h,v 1.37 2011/03/12 22:54:37 joerg Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@ -86,6 +86,8 @@ typedef volatile unsigned char __cpu_simple_lock_t;
#define __HAVE_ATOMIC_AS_MEMBAR
#define __HAVE_CPU_LWP_SETPRIVATE
#define __HAVE___LWP_GETPRIVATE_FAST
#define __HAVE_TLS_VARIANT_II
#define __HAVE_COMMON___TLS_GET_ADDR
#define __HAVE_INTR_CONTROL
#ifdef _KERNEL_OPT

View File

@ -1,4 +1,4 @@
/* $NetBSD: types.h,v 1.71 2011/02/24 04:28:46 joerg Exp $ */
/* $NetBSD: types.h,v 1.72 2011/03/12 22:54:37 joerg Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@ -113,6 +113,8 @@ typedef volatile unsigned char __cpu_simple_lock_t;
#define __HAVE_CPU_LWP_SETPRIVATE
#define __HAVE_INTR_CONTROL
#define __HAVE___LWP_GETPRIVATE_FAST
#define __HAVE_TLS_VARIANT_II
#define __HAVE_COMMON___TLS_GET_ADDR
#if defined(_KERNEL)
#define __HAVE_RAS

View File

@ -1,4 +1,4 @@
/* $NetBSD: types.h,v 1.31 2011/02/24 04:28:47 joerg Exp $ */
/* $NetBSD: types.h,v 1.32 2011/03/12 22:54:37 joerg Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@ -79,5 +79,7 @@ typedef volatile unsigned char __cpu_simple_lock_t;
#define __HAVE_CPU_LWP_SETPRIVATE
#define __HAVE___LWP_GETPRIVATE_FAST
#define __HAVE_COMMON___TLS_GET_ADDR
#define __HAVE_TLS_VARIANT_I
#endif /* !_SH3_TYPES_H_ */