aboutsummaryrefslogtreecommitdiff
path: root/gold/testsuite/basic_test.cc
blob: 0c032892cda53a3d48dfe2c80ec37df68f22eda4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// basic_test.cc -- a test case for gold

// Copyright (C) 2006-2015 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.

// This file is part of gold.

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.

// The goal of this program is to produce as many different types of
// relocations as we can in a stand-alone program that does not use
// TLS.  This program is compiled without optimization.

// 1  Code reference to global data.
// 2  Code reference to static data.
// 3  Code reference to BSS data.
// 4  Code reference to offset within global data.
// 5  Code reference to offset within static data.
// 6  Code reference to offset within BSS data.
// 7  Switch statement with a table of destinations.
// 8  Taking the address of a label (a gcc extension).
// 9  Taking the address of a nested function (a gcc extension).
// 10 Data reference to global data.
// 11 Data reference to static data.
// 12 Data reference to BSS data.
// 13 Data reference to offset within global data.
// 14 Data reference to offset within static data.
// 15 Data reference to offset within BSS data.
// 16 Virtual table.
// 17 Inline function.
// 18 Call through pointer to method.
// 19 Initialize variable to pointer to method.
// 20 Global constructor and destructor.

// 1 Code reference to global data.
int t1 = 11;

// 2 Code reference to static data.
static int t2 = 22;

// 3 Code reference to BSS data (initialized after program starts, to
// 33).
int t3;

// 4 Code reference to offset within global data.
char t4[] = "Hello, world";

// 5 Code reference to offset within static data.
static char t5[] = "Hello, world";

// 6 Code reference to offset within BSS data (initialized after
// program starts, to contents of t4).
char t6[13];

// Test cases 1 through 6.

bool
t1_6()
{
  return (t1 == 11
	  && t2 == 22
	  && t3 == 33
	  && t4[5] == ','
	  && t5[7] == 'w'
	  && t6[9] == 'r');
}

// 7  Switch statement with a table of destinations.

int
t7(int i)
{
  switch (i)
    {
    case 0:
      return 12;
    case 1:
      return 34;
    case 2:
      return 56;
    case 3:
      return 78;
    case 4:
      return 90;
    case 5:
      return 13;
    case 6:
      return 0;
    case 7:
      return 57;
    case 8:
      return 79;
    case 9:
      return 81;
    default:
      return 144;
    }
}

// 8  Taking the address of a label (a gcc extension).

int
t8(int i)
{
  for (int j = 0; j < 10; ++j)
    {
      void* p;
      if (i + j > 6)
	p = &&lab1;
      else
	p = &&lab2;
      if (j == 7)
	goto *p;
    }
  return 15;
 lab1:
  return 0;
 lab2:
  return 12;
}

// 9  Taking the address of a nested function (a gcc extension).
// Disabled because this is only supported in C, not C++.

int
t9a(int (*pfn)(int))
{
  return (*pfn)(10) - 10;
}

int
t9(int i)
{
#if 0
  int
  t9c(int j)
  {
    return i + j;
  }
  return t9a(&t9c);
#else
  return i;
#endif
}

// 10 Data reference to global data.
int* t10 = &t1;

// 11 Data reference to static data.
int* t11 = &t2;

// 12 Data reference to BSS data.
int* t12 = &t3;

// 13 Data reference to offset within global data.
char* t13 = &t4[6];

// 14 Data reference to offset within static data.
char* t14 = &t5[8];

// 15 Data reference to offset within BSS data.
char* t15 = &t6[10];

// Test cases 10 through 15.

bool
t10_15()
{
  return (*t10 == 11
	  && *t11 == 22
	  && *t12 == 33
	  && *t13 == ' '
	  && *t14 == 'o'
	  && *t15 == 'l');
}

// 16 Virtual table.

class t16a
{
 public:
  virtual
  ~t16a()
  { }
  virtual int
  t()
  { return 83; }
};

class t16b : public t16a
{
 public:
  virtual int
  t()
  { return 92; }
};

t16b t16v;

bool
t16()
{
  return t16v.t() == 92;
}

// 17 Inline function.

inline int
t17a()
{
  return 74;
}

bool
t17()
{
  return t17a() == 74;
}

// 18 Call through pointer to method.

class t18a
{
 public:
  int
  ta()
  { return 65; }

  int
  tb()
  { return 90; }
};

t18a t18v;

int
t18f(int (t18a::* p)())
{
  return (t18v.*p)();
}

bool
t18()
{
  return t18f(&t18a::ta) == 65;
}

// 19 Initialize variable to pointer to method.

int (t18a::* t19v)() = &t18a::tb;

bool
t19()
{
  return (t18v.*t19v)() == 90;
}

// 20 Global constructor and destructor.

class t20a
{
 public:
  t20a()
    : i(96)
  { }
  ~t20a()
  { }
  int
  get() const
  { return this->i; }
 private:
  int i;
};

t20a t20v;

bool
t20()
{
  return t20v.get() == 96;
}

// Main function.  Initialize variables and call test functions.

int
main()
{
  t3 = 33;
  for (int i = 0; i < 13; ++i)
    t6[i] = t4[i];

  if (t1_6()
      && t7(6) == 0
      && t8(0) == 0
      && t9(5) == 5
      && t10_15()
      && t16()
      && t17()
      && t18()
      && t19()
      && t20())
    return 0;
  else
    return 1;
}