tls_test.cc 4.4 KB

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