Add regression tests for TLS.

This commit is contained in:
Doug Rabson 2004-08-03 09:04:01 +00:00
parent bd872c1ccb
commit 2546665afc
14 changed files with 429 additions and 0 deletions

View File

@ -0,0 +1,9 @@
# $FreeBSD$
SUBDIR=libxx libyy ttls1 ttls2
.if ${MACHINE_ARCH} == "i386"
SUBDIR+=ttls3
.endif
.include <bsd.subdir.mk>

View File

@ -0,0 +1 @@
#CFLAGS+= -mno-tls-direct-seg-refs

View File

@ -0,0 +1,10 @@
# $FreeBSD$
LIB= xx
SHLIB_MAJOR= 1
SRCS= xx.c
#CFLAGS+=-mtls-dialect=sun
NOMAN= t
.include <bsd.lib.mk>

View File

@ -0,0 +1,12 @@
/* $FreeBSD$ */
extern int __thread yy1;
int __thread xx1 = 1;
int __thread xx2 = 2;
int __thread xxa[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int
xxyy()
{
return yy1;
}

View File

@ -0,0 +1,10 @@
# $FreeBSD$
LIB= yy
SHLIB_MAJOR= 1
SRCS= yy.c
CFLAGS+=-fpic
NOMAN= t
.include <bsd.lib.mk>

View File

@ -0,0 +1,3 @@
/* $FreeBSD$ */
int __thread yy1 = 101;

View File

@ -0,0 +1,12 @@
# $FreeBSD$
PROG= ttls1
LDADD+= -L../libxx -lxx -Wl,--rpath=${.CURDIR}/../libxx
LDADD+= -L../libyy -lyy -Wl,--rpath=${.CURDIR}/../libyy
LDADD+= -L${.CURDIR}/../../../../lib/libc -lc
LDADD+= -Wl,--rpath=${.CURDIR}/../../../../lib/libc
LDADD+= -Wl,--dynamic-linker=${.CURDIR}/../../../../libexec/rtld-elf/ld-elf.so.1
NOMAN= t
DEBUG_FLAGS= -g
.include <bsd.prog.mk>

View File

@ -0,0 +1,15 @@
/* $FreeBSD$
#include <stdio.h>
extern int __thread xx1;
extern int __thread xx2;
extern int __thread xxa[];
int __thread a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
extern int xxyy();
int main(int argc, char** argv)
{
printf("xx1=%d, xx2=%d, xxa[5]=%d, a[5]=%d, xxyy()=%d\n",
xx1, xx2, xxa[5], a[5], xxyy());
}

View File

@ -0,0 +1,14 @@
# $FreeBSD$
PROG= ttls2
LDADD+= -lpthread
LDADD+= -Wl,--rpath=${.CURDIR}/../../../../lib/libpthread
#LDADD+= -lthr
#LDADD+= -Wl,--rpath=${.CURDIR}/../../../../lib/libthr
LDADD+= -L${.CURDIR}/../../../../lib/libc -lc
LDADD+= -Wl,--rpath=${.CURDIR}/../../../../lib/libc
LDADD+= -Wl,--dynamic-linker=${.CURDIR}/../../../../libexec/rtld-elf/ld-elf.so.1
NOMAN= t
DEBUG_FLAGS= -g
.include <bsd.prog.mk>

View File

@ -0,0 +1,36 @@
/* $FreeBSD$ */
#include <stdio.h>
#include <pthread.h>
int __thread i;
void *
foo1(void *arg)
{
printf("thread %p, &i = %p\n", pthread_self(), &i);
for (i = 0; i < 10; i++) {
printf("thread %p, i = %d\n", pthread_self(), i);
sleep(1);
}
}
void *
foo2(void *arg)
{
printf("thread %p, &i = %p\n", pthread_self(), &i);
for (i = 10; i > 0; i--) {
printf("thread %p, i = %d\n", pthread_self(), i);
sleep(1);
}
}
int main(int argc, char** argv)
{
pthread_t t1, t2;
pthread_create(&t1, 0, foo1, 0);
pthread_create(&t2, 0, foo2, 0);
pthread_join(t1, 0);
pthread_join(t2, 0);
}

View File

@ -0,0 +1,24 @@
# $FreeBSD$
all: ttls3
LDFLAGS=-shared -Bsymbolic --allow-shlib-undefined
CFLAGS+= -L${.CURDIR}/../../../../lib/libc -lc -lthr
CFLAGS+= -Wl,--rpath=${.CURDIR}/../../../../lib/libc
CFLAGS+= -Wl,--rpath=${.OBJDIR}
CFLAGS+= -Wl,--dynamic-linker=${.CURDIR}/../../../../libexec/rtld-elf/ld-elf.so.1
tls-lib: elftls.S
gcc -c -o elftls.o elftls.S
ld $(LDFLAGS) elftls.o -soname libtls.so.1 -o libtls.so.1
ln -sf libtls.so.1 libtls.so
tls-test-lib: tls-lib tls-test-lib.c
gcc -c -o tls-test.o tls-test-lib.c
ld $(LDFLAGS) tls-test.o libtls.so.1 -rpath=${.OBJDIR} -soname libtls-test.so.1 -o libtls-test.so.1
ttls3: tls-test-lib tls-test.c
gcc $(CFLAGS) -rdynamic -o ttls3 tls-test.c
clean:
rm -f *.o libtls.so* libtls-test.so* ttls3

View File

@ -0,0 +1,114 @@
/*-
* Copyright (C) 2004 NVIDIA Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
.file "elftls.S"
.globl __gl_tls_offsets
.type __gl_tls_offsets,@object
.section .tbss,"awT",@nobits
.globl __gl_tls_var0
.align 4
.type __gl_tls_var0, @object
.size __gl_tls_var0, 4
__gl_tls_var0:
.zero 4
.globl __gl_tls_var1
.align 4
.type __gl_tls_var1, @object
.size __gl_tls_var1, 4
__gl_tls_var1:
.zero 4
.globl __gl_tls_var2
.align 4
.type __gl_tls_var2, @object
.size __gl_tls_var2, 4
__gl_tls_var2:
.zero 4
.globl __gl_tls_var3
.align 4
.type __gl_tls_var3, @object
.size __gl_tls_var3, 4
__gl_tls_var3:
.zero 4
.globl __gl_tls_var4
.align 4
.type __gl_tls_var4, @object
.size __gl_tls_var4, 4
__gl_tls_var4:
.zero 4
.globl __gl_tls_var5
.align 4
.type __gl_tls_var5, @object
.size __gl_tls_var5, 4
__gl_tls_var5:
.zero 4
.globl __gl_tls_var6
.align 4
.type __gl_tls_var6, @object
.size __gl_tls_var6, 4
__gl_tls_var6:
.zero 4
.globl __gl_tls_var7
.align 4
.type __gl_tls_var7, @object
.size __gl_tls_var7, 4
__gl_tls_var7:
.zero 4
.text
.p2align 4,,15
.globl __gl_tls_init_offsets
.type __gl_tls_init_offsets, @function
__gl_tls_init_offsets:
movl $__gl_tls_offsets, %eax
movl $__gl_tls_var0@ntpoff, %ecx
movl %ecx, 0(%eax)
movl $__gl_tls_var1@ntpoff, %ecx
movl %ecx, 4(%eax)
movl $__gl_tls_var2@ntpoff, %ecx
movl %ecx, 8(%eax)
movl $__gl_tls_var3@ntpoff, %ecx
movl %ecx, 12(%eax)
movl $__gl_tls_var4@ntpoff, %ecx
movl %ecx, 16(%eax)
movl $__gl_tls_var5@ntpoff, %ecx
movl %ecx, 20(%eax)
movl $__gl_tls_var6@ntpoff, %ecx
movl %ecx, 24(%eax)
movl $__gl_tls_var7@ntpoff, %ecx
movl %ecx, 28(%eax)
ret

View File

@ -0,0 +1,110 @@
/*-
* Copyright (C) 2004 NVIDIA Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <stdio.h>
#define __G_TLS_OFFSETS_SIZE 8
unsigned long int __gl_tls_offsets[__G_TLS_OFFSETS_SIZE];
void __gl_tls_init_offsets();
#ifdef __GL_TLS_SINGLE_INSTRUCTION
#define THREAD_GETMEM(num) \
({ \
void *__value; \
__asm__ __volatile__ ( \
"movl %%gs:(%1),%0" \
: "=r" (__value) \
: "r" (__gl_tls_offsets[num]) \
); \
__value; \
})
#define THREAD_SETMEM(num, value) \
do { \
void *__value = (value); \
__asm__ __volatile__ ( \
"movl %0,%%gs:(%1)" \
: \
: "r" (__value), \
"r" (__gl_tls_offsets[num]) \
); \
} while (0)
#else
#define __GL_TLS_GET(num) \
({ \
void *__dummy, *__value; \
__asm__ __volatile__ ( \
"movl %%gs:0,%2 \n\t" \
"movl (%2,%1),%0 \n\t" \
: "=r" (__value) \
: "r" (__gl_tls_offsets[num]), \
"r" (__dummy) \
); \
__value; \
})
#define __GL_TLS_SET(num, value) \
do { \
void *__dummy, *__value = (value); \
__asm__ __volatile__ ( \
"movl %%gs:0,%2 \n\t" \
"movl %0,(%2,%1) \n\t" \
: \
: "r" (__value), \
"r" (__gl_tls_offsets[num]), \
"r" (__dummy) \
); \
} while (0)
#endif
void _init(void)
{
__gl_tls_init_offsets();
__GL_TLS_SET(0, (void *) 0xff000000);
__GL_TLS_SET(1, (void *) 0xff000001);
__GL_TLS_SET(2, (void *) 0xff000002);
__GL_TLS_SET(3, (void *) 0xff000003);
__GL_TLS_SET(4, (void *) 0xff000004);
__GL_TLS_SET(5, (void *) 0xff000005);
__GL_TLS_SET(6, (void *) 0xff000006);
__GL_TLS_SET(7, (void *) 0xff000007);
}
void __gl_tls_test(void)
{
printf("__GL_TLS_GET(0) = %p\n", __GL_TLS_GET(0));
printf("__GL_TLS_GET(1) = %p\n", __GL_TLS_GET(1));
printf("__GL_TLS_GET(2) = %p\n", __GL_TLS_GET(2));
printf("__GL_TLS_GET(3) = %p\n", __GL_TLS_GET(3));
printf("__GL_TLS_GET(4) = %p\n", __GL_TLS_GET(4));
printf("__GL_TLS_GET(5) = %p\n", __GL_TLS_GET(5));
printf("__GL_TLS_GET(6) = %p\n", __GL_TLS_GET(6));
printf("__GL_TLS_GET(7) = %p\n", __GL_TLS_GET(7));
}

View File

@ -0,0 +1,59 @@
/*-
* Copyright (C) 2004 NVIDIA Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char **argv)
{
void *handle;
void (*__gl_tls_test)(void);
const char *error;
handle = dlopen("libtls-test.so.1", RTLD_NOW);
if (!handle) {
error = dlerror();
printf("dlopen failed (%s)!\n", error);
exit(1);
}
dlerror();
__gl_tls_test = dlsym(handle, "__gl_tls_test");
error = dlerror();
if (error) {
dlclose(handle);
printf("dlsym failed (%s)!\n", error);
exit(1);
}
__gl_tls_test(); /* print TLS values */
dlclose(handle);
return 0;
}