two_file_test_1.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // two_file_test_1.cc -- a two file test case for gold, file 1 of 2
  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 tests references between files. This is file 1, and
  18. // two_file_test_2.cc is file 2. We test in several different ways:
  19. // Files 1 and 2 linked together in executable.
  20. // File 1 in executable, file 2 in shared library.
  21. // File 1 in shared library, file 2 in executable.
  22. // Files 1 and 2 linked together in shared library.
  23. // Files 1 and 2 in different shared libraries.
  24. // We test the following cases.
  25. // 1 Code in file 1 calls code in file 2.
  26. // 2 Code in file 1 refers to global data in file 2.
  27. // 3 Code in file 1 referes to common symbol in file 2.
  28. // 4 Code in file 1 refers to offset within global data in file 2.
  29. // 5 Code in file 1 refers to offset within common symbol in file 2.
  30. // 6 Data in file 1 refers to global data in file 2.
  31. // 7 Data in file 1 refers to common symbol in file 2.
  32. // 8 Data in file 1 refers to offset within global data in file 2.
  33. // 9 Data in file 1 refers to offset within common symbol in file 2.
  34. // 10 Data in file 1 refers to function in file 2.
  35. // 11 Pass function pointer from file 1 to file 2.
  36. // 12 Compare address of function for equality in both files.
  37. // 13 Compare address of inline function for equality in both files.
  38. // 14 Compare string constants in file 1 and file 2.
  39. // 15 Compare wide string constants in file 1 and file 2.
  40. // 16 Call a function directly after its address has been taken.
  41. // 17 File 1 checks array of string constants defined in file 2.
  42. // 18 File 1 checks string constants referenced in code in file 2.
  43. #include "two_file_test.h"
  44. // 1 Code in file 1 calls code in file 2.
  45. bool
  46. t1()
  47. {
  48. return t1_2() == 123;
  49. }
  50. // 2 Code in file 1 refers to global data in file 2.
  51. bool
  52. t2()
  53. {
  54. return v2 == 456;
  55. }
  56. // 3 Code in file 1 referes to common symbol in file 2.
  57. bool
  58. t3()
  59. {
  60. return v3 == 789;
  61. }
  62. // 4 Code in file 1 refers to offset within global data in file 2.
  63. bool
  64. t4()
  65. {
  66. return v4[5] == ',';
  67. }
  68. // 5 Code in file 1 refers to offset within common symbol in file 2.
  69. bool
  70. t5()
  71. {
  72. return v5[7] == 'w';
  73. }
  74. // 6 Data in file 1 refers to global data in file 2.
  75. int* p6 = &v2;
  76. bool
  77. t6()
  78. {
  79. return *p6 == 456;
  80. }
  81. // 7 Data in file 1 refers to common symbol in file 2.
  82. int* p7 = &v3;
  83. bool
  84. t7()
  85. {
  86. return *p7 == 789;
  87. }
  88. // 8 Data in file 1 refers to offset within global data in file 2.
  89. char* p8 = &v4[6];
  90. bool
  91. t8()
  92. {
  93. return *p8 == ' ';
  94. }
  95. // 9 Data in file 1 refers to offset within common symbol in file 2.
  96. char* p9 = &v5[8];
  97. bool
  98. t9()
  99. {
  100. return *p9 == 'o';
  101. }
  102. // 10 Data in file 1 refers to function in file 2.
  103. int (*pfn)() = &f10;
  104. bool
  105. t10()
  106. {
  107. return (*pfn)() == 135;
  108. }
  109. // 11 Pass function pointer from file 1 to file 2.
  110. int
  111. f11a()
  112. {
  113. return 246;
  114. }
  115. bool
  116. t11()
  117. {
  118. return f11b(&f11a) == 246;
  119. }
  120. // 12 Compare address of function for equality in both files.
  121. bool
  122. t12()
  123. {
  124. return &t12 == f12();
  125. }
  126. // 13 Compare address of inline function for equality in both files.
  127. bool
  128. t13()
  129. {
  130. return &f13i == f13();
  131. }
  132. // 14 Compare string constants in file 1 and file 2.
  133. bool
  134. t14()
  135. {
  136. const char* s1 = TEST_STRING_CONSTANT;
  137. const char* s2 = f14();
  138. while (*s1 != '\0')
  139. if (*s1++ != *s2++)
  140. return false;
  141. return *s2 == '\0';
  142. }
  143. // 15 Compare wide string constants in file 1 and file 2.
  144. bool
  145. t15()
  146. {
  147. const wchar_t* s1 = TEST_WIDE_STRING_CONSTANT;
  148. const wchar_t* s2 = f15();
  149. while (*s1 != '\0')
  150. if (*s1++ != *s2++)
  151. return false;
  152. return *s2 == '\0';
  153. }
  154. // 16 Call a function directly after its address has been taken.
  155. bool
  156. t16()
  157. {
  158. return f10() == 135;
  159. }
  160. // 17 File 1 checks array of string constants defined in file 2.
  161. bool
  162. t17()
  163. {
  164. char c = 'a';
  165. for (int i = 0; i < T17_COUNT; ++i)
  166. {
  167. if (t17data[i][0] != c || t17data[i][1] != '\0')
  168. return false;
  169. ++c;
  170. }
  171. return true;
  172. }
  173. // 18 File 1 checks string constants referenced in code in file 2.
  174. bool
  175. t18()
  176. {
  177. char c = 'a';
  178. for (int i = 0; i < T17_COUNT; ++i)
  179. {
  180. const char* s = f18(i);
  181. if (s[0] != c || s[1] != '\0')
  182. return false;
  183. ++c;
  184. }
  185. return true;
  186. }