fix local-dynamic model TLS on mips and powerpc

the TLS ABI spec for mips, powerpc, and some other (presently
unsupported) RISC archs has the return value of __tls_get_addr offset
by +0x8000 and the result of DTPOFF relocations offset by -0x8000. I
had previously assumed this part of the ABI was actually just an
implementation detail, since the adjustments cancel out. however, when
the local dynamic model is used for accessing TLS that's known to be
in the same DSO, either of the following may happen:

1. the -0x8000 offset may already be applied to the argument structure
passed to __tls_get_addr at ld time, without any opportunity for
runtime relocations.

2. __tls_get_addr may be used with a zero offset argument to obtain a
base address for the module's TLS, to which the caller then applies
immediate offsets for individual objects accessed using the local
dynamic model. since the immediate offsets have the -0x8000 adjustment
applied to them, the base address they use needs to include the
+0x8000 offset.

it would be possible, but more complex, to store the pointers in the
dtv[] array with the +0x8000 offset pre-applied, to avoid the runtime
cost of adding 0x8000 on each call to __tls_get_addr. this change
could be made later if measurements show that it would help.
This commit is contained in:
Rich Felker 2015-06-25 22:22:00 +00:00
parent ce337daa00
commit 6ba5517a46
5 changed files with 13 additions and 5 deletions

View File

@ -13,4 +13,6 @@ static inline struct pthread *__pthread_self()
#define TLS_ABOVE_TP
#define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + 0x7000)
#define DTP_OFFSET 0x8000
#define CANCEL_REG_IP (3-(union {int __i; char __b;}){1}.__b)

View File

@ -12,6 +12,8 @@ static inline struct pthread *__pthread_self()
#define TLS_ABOVE_TP
#define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + 0x7000)
#define DTP_OFFSET 0x8000
// offset of the PC register in mcontext_t, divided by the system wordsize
// the kernel calls the ip "nip", it's the first saved value after the 32
// GPRs.

View File

@ -94,6 +94,10 @@ struct __timer {
#define CANARY canary
#endif
#ifndef DTP_OFFSET
#define DTP_OFFSET 0
#endif
#define SIGTIMER 32
#define SIGCANCEL 33
#define SIGSYNCCALL 34

View File

@ -337,7 +337,7 @@ static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stri
*reloc_addr = def.dso->tls_id;
break;
case REL_DTPOFF:
*reloc_addr = tls_val + addend;
*reloc_addr = tls_val + addend - DTP_OFFSET;
break;
#ifdef TLS_ABOVE_TP
case REL_TPOFF:
@ -1102,7 +1102,7 @@ void *__tls_get_new(size_t *v)
__block_all_sigs(&set);
if (v[0]<=(size_t)self->dtv[0]) {
__restore_sigs(&set);
return (char *)self->dtv[v[0]]+v[1];
return (char *)self->dtv[v[0]]+v[1]+DTP_OFFSET;
}
/* This is safe without any locks held because, if the caller
@ -1135,7 +1135,7 @@ void *__tls_get_new(size_t *v)
if (p->tls_id == v[0]) break;
}
__restore_sigs(&set);
return mem + v[1];
return mem + v[1] + DTP_OFFSET;
}
static void update_tls_size()

View File

@ -8,9 +8,9 @@ void *__tls_get_addr(size_t *v)
__attribute__((__visibility__("hidden")))
void *__tls_get_new(size_t *);
if (v[0]<=(size_t)self->dtv[0])
return (char *)self->dtv[v[0]]+v[1];
return (char *)self->dtv[v[0]]+v[1]+DTP_OFFSET;
return __tls_get_new(v);
#else
return (char *)self->dtv[1]+v[1];
return (char *)self->dtv[1]+v[1]+DTP_OFFSET;
#endif
}