basic_test.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // basic_test.cc -- a test case for gold
  2. // Copyright (C) 2006-2022 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. // The goal of this program is to produce as many different types of
  18. // relocations as we can in a stand-alone program that does not use
  19. // TLS. This program is compiled without optimization.
  20. // 1 Code reference to global data.
  21. // 2 Code reference to static data.
  22. // 3 Code reference to BSS data.
  23. // 4 Code reference to offset within global data.
  24. // 5 Code reference to offset within static data.
  25. // 6 Code reference to offset within BSS data.
  26. // 7 Switch statement with a table of destinations.
  27. // 8 Taking the address of a label (a gcc extension).
  28. // 9 Taking the address of a nested function (a gcc extension).
  29. // 10 Data reference to global data.
  30. // 11 Data reference to static data.
  31. // 12 Data reference to BSS data.
  32. // 13 Data reference to offset within global data.
  33. // 14 Data reference to offset within static data.
  34. // 15 Data reference to offset within BSS data.
  35. // 16 Virtual table.
  36. // 17 Inline function.
  37. // 18 Call through pointer to method.
  38. // 19 Initialize variable to pointer to method.
  39. // 20 Global constructor and destructor.
  40. // 1 Code reference to global data.
  41. int t1 = 11;
  42. // 2 Code reference to static data.
  43. static int t2 = 22;
  44. // 3 Code reference to BSS data (initialized after program starts, to
  45. // 33).
  46. int t3;
  47. // 4 Code reference to offset within global data.
  48. char t4[] = "Hello, world";
  49. // 5 Code reference to offset within static data.
  50. static char t5[] = "Hello, world";
  51. // 6 Code reference to offset within BSS data (initialized after
  52. // program starts, to contents of t4).
  53. char t6[13];
  54. // Test cases 1 through 6.
  55. bool
  56. t1_6()
  57. {
  58. return (t1 == 11
  59. && t2 == 22
  60. && t3 == 33
  61. && t4[5] == ','
  62. && t5[7] == 'w'
  63. && t6[9] == 'r');
  64. }
  65. // 7 Switch statement with a table of destinations.
  66. int
  67. t7(int i)
  68. {
  69. switch (i)
  70. {
  71. case 0:
  72. return 12;
  73. case 1:
  74. return 34;
  75. case 2:
  76. return 56;
  77. case 3:
  78. return 78;
  79. case 4:
  80. return 90;
  81. case 5:
  82. return 13;
  83. case 6:
  84. return 0;
  85. case 7:
  86. return 57;
  87. case 8:
  88. return 79;
  89. case 9:
  90. return 81;
  91. default:
  92. return 144;
  93. }
  94. }
  95. // 8 Taking the address of a label (a gcc extension).
  96. int
  97. t8(int i)
  98. {
  99. for (int j = 0; j < 10; ++j)
  100. {
  101. void* p;
  102. if (i + j > 6)
  103. p = &&lab1;
  104. else
  105. p = &&lab2;
  106. if (j == 7)
  107. goto *p;
  108. }
  109. return 15;
  110. lab1:
  111. return 0;
  112. lab2:
  113. return 12;
  114. }
  115. // 9 Taking the address of a nested function (a gcc extension).
  116. // Disabled because this is only supported in C, not C++.
  117. int
  118. t9a(int (*pfn)(int))
  119. {
  120. return (*pfn)(10) - 10;
  121. }
  122. int
  123. t9(int i)
  124. {
  125. #if 0
  126. int
  127. t9c(int j)
  128. {
  129. return i + j;
  130. }
  131. return t9a(&t9c);
  132. #else
  133. return i;
  134. #endif
  135. }
  136. // 10 Data reference to global data.
  137. int* t10 = &t1;
  138. // 11 Data reference to static data.
  139. int* t11 = &t2;
  140. // 12 Data reference to BSS data.
  141. int* t12 = &t3;
  142. // 13 Data reference to offset within global data.
  143. char* t13 = &t4[6];
  144. // 14 Data reference to offset within static data.
  145. char* t14 = &t5[8];
  146. // 15 Data reference to offset within BSS data.
  147. char* t15 = &t6[10];
  148. // Test cases 10 through 15.
  149. bool
  150. t10_15()
  151. {
  152. return (*t10 == 11
  153. && *t11 == 22
  154. && *t12 == 33
  155. && *t13 == ' '
  156. && *t14 == 'o'
  157. && *t15 == 'l');
  158. }
  159. // 16 Virtual table.
  160. class t16a
  161. {
  162. public:
  163. virtual
  164. ~t16a()
  165. { }
  166. virtual int
  167. t()
  168. { return 83; }
  169. };
  170. class t16b : public t16a
  171. {
  172. public:
  173. virtual int
  174. t()
  175. { return 92; }
  176. };
  177. t16b t16v;
  178. bool
  179. t16()
  180. {
  181. return t16v.t() == 92;
  182. }
  183. // 17 Inline function.
  184. inline int
  185. t17a()
  186. {
  187. return 74;
  188. }
  189. bool
  190. t17()
  191. {
  192. return t17a() == 74;
  193. }
  194. // 18 Call through pointer to method.
  195. class t18a
  196. {
  197. public:
  198. int
  199. ta()
  200. { return 65; }
  201. int
  202. tb()
  203. { return 90; }
  204. };
  205. t18a t18v;
  206. int
  207. t18f(int (t18a::* p)())
  208. {
  209. return (t18v.*p)();
  210. }
  211. bool
  212. t18()
  213. {
  214. return t18f(&t18a::ta) == 65;
  215. }
  216. // 19 Initialize variable to pointer to method.
  217. int (t18a::* t19v)() = &t18a::tb;
  218. bool
  219. t19()
  220. {
  221. return (t18v.*t19v)() == 90;
  222. }
  223. // 20 Global constructor and destructor.
  224. class t20a
  225. {
  226. public:
  227. t20a()
  228. : i(96)
  229. { }
  230. ~t20a()
  231. { }
  232. int
  233. get() const
  234. { return this->i; }
  235. private:
  236. int i;
  237. };
  238. t20a t20v;
  239. bool
  240. t20()
  241. {
  242. return t20v.get() == 96;
  243. }
  244. // Main function. Initialize variables and call test functions.
  245. int
  246. main()
  247. {
  248. t3 = 33;
  249. for (int i = 0; i < 13; ++i)
  250. t6[i] = t4[i];
  251. if (t1_6()
  252. && t7(6) == 0
  253. && t8(0) == 0
  254. && t9(5) == 5
  255. && t10_15()
  256. && t16()
  257. && t17()
  258. && t18()
  259. && t19()
  260. && t20())
  261. return 0;
  262. else
  263. return 1;
  264. }