Make sure the start and size of the TLS segment are aligned.
[external/binutils.git] / gold / testsuite / tls_test.cc
1 // tls_test.cc -- test TLS variables for gold
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 // This provides a set of test functions for TLS variables.  The
24 // functions are called by a main function in tls_test_main.cc.  This
25 // lets us test TLS access from a shared library.  We currently don't
26 // bother to test TLS access between two different files, on the
27 // theory that that is no more complicated than ordinary variable
28 // access between files.
29
30 // We start two threads, and stop the second one.  Then we run the
31 // first thread through the following cases.  Then we let the second
32 // thread continue, and run it through the same set of cases.  All the
33 // actual thread manipulation is in tls_test_main.cc.
34
35 // 1  Access to an uninitialized global thread variable.
36 // 2  Access to an uninitialized static thread variable.
37 // 3  Access to an initialized global thread variable.
38 // 4  Access to an initialized static thread variable.
39 // 5  Taking the address of a global thread variable.
40 // 6  Taking the address of a static thread variable.
41 // 8  Like test 1, but with the thread variable defined in another file.
42 // 9  Like test 3, but with the thread variable defined in another file.
43 // 10 Like test 5, but with the thread variable defined in another file.
44 // last  Verify that the above tests left the variables set correctly.
45
46
47 #include <cstdio>
48 #include "tls_test.h"
49
50 #define CHECK_EQ_OR_RETURN(var, expected)                               \
51   do                                                                    \
52     {                                                                   \
53       if ((var) != (expected))                                          \
54         {                                                               \
55           printf(#var ": expected %d, found %d\n", expected, var);      \
56           return false;                                                 \
57         }                                                               \
58     }                                                                   \
59   while (0)
60
61 __thread int v1;
62 static __thread int v2;
63
64 // We don't use these pointers, but putting them in tests alignment on
65 // a 64-bit target.
66 __thread char* p1;
67 char dummy;
68 __thread char* p2 = &dummy;
69
70 __thread int v3 = 3;
71 static __thread int v4 = 4;
72 __thread int v5;
73 static __thread int v6;
74
75 bool
76 t1()
77 {
78   CHECK_EQ_OR_RETURN(v1, 0);
79   v1 = 10;
80   return true;
81 }
82
83 bool
84 t2()
85 {
86   CHECK_EQ_OR_RETURN(v2, 0);
87   v2 = 20;
88   return true;
89 }
90
91 bool
92 t3()
93 {
94   CHECK_EQ_OR_RETURN(v3, 3);
95   v3 = 30;
96   return true;
97 }
98
99 bool
100 t4()
101 {
102   CHECK_EQ_OR_RETURN(v4, 4);
103   v4 = 40;
104   return true;
105 }
106
107 // For test 5 the main function calls f5b(f5a()), then calls t5().
108
109 int*
110 f5a()
111 {
112   return &v5;
113 }
114
115 void
116 f5b(int* p)
117 {
118   *p = 50;
119 }
120
121 bool
122 t5()
123 {
124   CHECK_EQ_OR_RETURN(v5, 50);
125   return true;
126 }
127
128 // For test 6 the main function calls f6b(f6a()), then calls t6().
129
130 int*
131 f6a()
132 {
133   return &v6;
134 }
135
136 void
137 f6b(int* p)
138 {
139   *p = 60;
140 }
141
142 bool
143 t6()
144 {
145   CHECK_EQ_OR_RETURN(v6, 60);
146   return true;
147 }
148
149 // The slot for t7() is unused.
150
151 bool
152 t8()
153 {
154   CHECK_EQ_OR_RETURN(o1, 0);
155   o1 = -10;
156   return true;
157 }
158
159 bool
160 t9()
161 {
162   CHECK_EQ_OR_RETURN(o2, -2);
163   o2 = -20;
164   return true;
165 }
166
167 // For test 10 the main function calls f10b(f10a()), then calls t10().
168
169 int*
170 f10a()
171 {
172   return &o3;
173 }
174
175 void
176 f10b(int* p)
177 {
178   *p = -30;
179 }
180
181 bool
182 t10()
183 {
184   CHECK_EQ_OR_RETURN(o3, -30);
185   return true;
186 }
187
188 bool
189 t_last()
190 {
191   CHECK_EQ_OR_RETURN(v1, 10);
192   CHECK_EQ_OR_RETURN(v2, 20);
193   CHECK_EQ_OR_RETURN(v3, 30);
194   CHECK_EQ_OR_RETURN(v4, 40);
195   CHECK_EQ_OR_RETURN(v5, 50);
196   CHECK_EQ_OR_RETURN(v6, 60);
197   CHECK_EQ_OR_RETURN(o1, -10);
198   CHECK_EQ_OR_RETURN(o2, -20);
199   CHECK_EQ_OR_RETURN(o3, -30);
200   return true;
201 }