1//===- implTest.cpp -------------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "StaticResolverTest.h"
10#include <mcld/Support/TargetSelect.h>
11#include <mcld/LD/StaticResolver.h>
12#include <mcld/LD/ResolveInfo.h>
13#include <mcld/LinkerConfig.h>
14
15#include <mcld/Support/FileSystem.h>
16
17using namespace mcld;
18using namespace mcldtest;
19
20//===----------------------------------------------------------------------===//
21// StaticResolverTest
22//===----------------------------------------------------------------------===//
23// Constructor can do set-up work for all test here.
24StaticResolverTest::StaticResolverTest()
25  : m_pResolver(NULL), m_pConfig(NULL) {
26  // create testee. modify it if need
27  m_pResolver = new StaticResolver();
28
29  m_pConfig = new LinkerConfig("arm-none-linux-gnueabi");
30}
31
32// Destructor can do clean-up work that doesn't throw exceptions here.
33StaticResolverTest::~StaticResolverTest()
34{
35  delete m_pResolver;
36  delete m_pConfig;
37}
38
39// SetUp() will be called immediately before each test.
40void StaticResolverTest::SetUp()
41{
42}
43
44// TearDown() will be called immediately after each test.
45void StaticResolverTest::TearDown()
46{
47}
48
49//==========================================================================//
50// Testcases
51//
52TEST_F( StaticResolverTest, MDEF ) {
53  ResolveInfo* old_sym = ResolveInfo::Create("abc");
54  ResolveInfo* new_sym = ResolveInfo::Create("abc");
55  new_sym->setDesc(ResolveInfo::Define);
56  old_sym->setDesc(ResolveInfo::Define);
57  ASSERT_TRUE( mcld::ResolveInfo::Define == new_sym->desc());
58  ASSERT_TRUE( mcld::ResolveInfo::Define == old_sym->desc());
59  ASSERT_TRUE( mcld::ResolveInfo::define_flag == new_sym->info());
60  ASSERT_TRUE( mcld::ResolveInfo::define_flag == old_sym->info());
61  bool override = true;
62  bool result = m_pResolver->resolve(*old_sym, *new_sym, override);
63  ASSERT_TRUE(result);
64  ASSERT_FALSE( override );
65}
66
67TEST_F( StaticResolverTest, DynDefAfterDynUndef ) {
68  ResolveInfo* old_sym = ResolveInfo::Create("abc");
69  ResolveInfo* new_sym = ResolveInfo::Create("abc");
70
71  new_sym->setBinding(ResolveInfo::Global);
72  old_sym->setBinding(ResolveInfo::Global);
73  new_sym->setDesc(ResolveInfo::Undefined);
74  old_sym->setDesc(ResolveInfo::Define);
75  new_sym->setSource(true);
76  old_sym->setSource(true);
77
78  new_sym->setSize(0);
79
80  old_sym->setSize(1);
81
82  ASSERT_TRUE( mcld::ResolveInfo::Global == new_sym->binding());
83  ASSERT_TRUE( mcld::ResolveInfo::Global == old_sym->binding());
84  ASSERT_TRUE( mcld::ResolveInfo::Undefined == new_sym->desc());
85  ASSERT_TRUE( mcld::ResolveInfo::Define    == old_sym->desc());
86
87  bool override = false;
88  bool result = m_pResolver->resolve(*old_sym, *new_sym, override);
89  ASSERT_TRUE(result);
90  ASSERT_FALSE( override );
91  ASSERT_TRUE(1 == old_sym->size());
92}
93
94TEST_F( StaticResolverTest, DynDefAfterDynDef ) {
95  ResolveInfo* old_sym = ResolveInfo::Create("abc");
96  ResolveInfo* new_sym = ResolveInfo::Create("abc");
97
98  new_sym->setBinding(ResolveInfo::Global);
99  old_sym->setBinding(ResolveInfo::Global);
100  new_sym->setDesc(ResolveInfo::Define);
101  old_sym->setDesc(ResolveInfo::Define);
102  new_sym->setSource(true);
103  old_sym->setSource(true);
104
105  new_sym->setSize(0);
106
107  old_sym->setSize(1);
108
109  ASSERT_TRUE( mcld::ResolveInfo::Global == new_sym->binding());
110  ASSERT_TRUE( mcld::ResolveInfo::Global == old_sym->binding());
111  ASSERT_TRUE( mcld::ResolveInfo::Define == new_sym->desc());
112  ASSERT_TRUE( mcld::ResolveInfo::Define == old_sym->desc());
113
114  bool override = false;
115  bool result = m_pResolver->resolve(*old_sym, *new_sym, override);
116  ASSERT_TRUE(result);
117  ASSERT_FALSE( override );
118  ASSERT_TRUE(1 == old_sym->size());
119}
120
121TEST_F( StaticResolverTest, DynUndefAfterDynUndef ) {
122  ResolveInfo* old_sym = ResolveInfo::Create("abc");
123  ResolveInfo* new_sym = ResolveInfo::Create("abc");
124
125  new_sym->setBinding(ResolveInfo::Global);
126  old_sym->setBinding(ResolveInfo::Global);
127  new_sym->setDesc(ResolveInfo::Undefined);
128  old_sym->setDesc(ResolveInfo::Undefined);
129  new_sym->setSource(true);
130  old_sym->setSource(true);
131
132  new_sym->setSize(0);
133
134  old_sym->setSize(1);
135
136  ASSERT_TRUE( mcld::ResolveInfo::Global    == new_sym->binding());
137  ASSERT_TRUE( mcld::ResolveInfo::Global    == old_sym->binding());
138  ASSERT_TRUE( mcld::ResolveInfo::Undefined == new_sym->desc());
139  ASSERT_TRUE( mcld::ResolveInfo::Undefined ==  old_sym->desc());
140
141  bool override = false;
142  bool result = m_pResolver->resolve(*old_sym, *new_sym, override);
143  ASSERT_TRUE(result);
144  ASSERT_FALSE( override );
145  ASSERT_TRUE(1 == old_sym->size());
146}
147
148TEST_F( StaticResolverTest, OverrideWeakByGlobal )
149{
150  ResolveInfo* old_sym = ResolveInfo::Create("abc");
151  ResolveInfo* new_sym = ResolveInfo::Create("abc");
152
153  new_sym->setBinding(ResolveInfo::Global);
154  old_sym->setBinding(ResolveInfo::Weak);
155  new_sym->setSize(0);
156  old_sym->setSize(1);
157
158  ASSERT_TRUE( mcld::ResolveInfo::Global == new_sym->binding());
159  ASSERT_TRUE( mcld::ResolveInfo::Weak == old_sym->binding());
160
161  ASSERT_TRUE( mcld::ResolveInfo::global_flag == new_sym->info());
162  ASSERT_TRUE( mcld::ResolveInfo::weak_flag == old_sym->info());
163  bool override = false;
164  bool result = m_pResolver->resolve(*old_sym, *new_sym, override);
165  ASSERT_TRUE(result);
166  ASSERT_TRUE( override );
167  ASSERT_TRUE(0 == old_sym->size());
168}
169
170TEST_F( StaticResolverTest, DynWeakAfterDynDef ) {
171  ResolveInfo* old_sym = ResolveInfo::Create("abc");
172  ResolveInfo* new_sym = ResolveInfo::Create("abc");
173
174  old_sym->setBinding(ResolveInfo::Weak);
175  new_sym->setBinding(ResolveInfo::Global);
176
177  new_sym->setSource(true);
178  old_sym->setSource(true);
179
180  old_sym->setDesc(ResolveInfo::Define);
181  new_sym->setDesc(ResolveInfo::Define);
182
183  new_sym->setSize(0);
184
185  old_sym->setSize(1);
186
187  ASSERT_TRUE( mcld::ResolveInfo::Weak   == old_sym->binding());
188  ASSERT_TRUE( mcld::ResolveInfo::Global == new_sym->binding());
189  ASSERT_TRUE( mcld::ResolveInfo::Define == old_sym->desc());
190  ASSERT_TRUE( mcld::ResolveInfo::Define == new_sym->desc());
191
192  bool override = false;
193  bool result = m_pResolver->resolve(*old_sym, *new_sym, override);
194  ASSERT_TRUE(result);
195  ASSERT_FALSE( override );
196  ASSERT_TRUE(1 == old_sym->size());
197}
198
199TEST_F( StaticResolverTest, MarkByBiggerCommon )
200{
201  ResolveInfo* old_sym = ResolveInfo::Create("abc");
202  ResolveInfo* new_sym = ResolveInfo::Create("abc");
203
204  new_sym->setDesc(ResolveInfo::Common);
205  old_sym->setDesc(ResolveInfo::Common);
206  new_sym->setSize(999);
207  old_sym->setSize(0);
208
209  ASSERT_TRUE( mcld::ResolveInfo::Common == new_sym->desc());
210  ASSERT_TRUE( mcld::ResolveInfo::Common == old_sym->desc());
211
212  ASSERT_TRUE( mcld::ResolveInfo::common_flag == new_sym->info());
213  ASSERT_TRUE( mcld::ResolveInfo::common_flag == old_sym->info());
214  bool override = true;
215  bool result = m_pResolver->resolve(*old_sym, *new_sym, override);
216  ASSERT_TRUE(result);
217  ASSERT_FALSE( override );
218  ASSERT_TRUE(999 == old_sym->size());
219}
220
221TEST_F( StaticResolverTest, OverrideByBiggerCommon )
222{
223  ResolveInfo* old_sym = ResolveInfo::Create("abc");
224  ResolveInfo* new_sym = ResolveInfo::Create("abc");
225
226  new_sym->setDesc(ResolveInfo::Common);
227  old_sym->setDesc(ResolveInfo::Common);
228  old_sym->setBinding(ResolveInfo::Weak);
229  new_sym->setSize(999);
230  old_sym->setSize(0);
231
232  ASSERT_TRUE( ResolveInfo::Common == new_sym->desc());
233  ASSERT_TRUE( ResolveInfo::Common == old_sym->desc());
234  ASSERT_TRUE( ResolveInfo::Weak == old_sym->binding());
235
236  ASSERT_TRUE( ResolveInfo::common_flag == new_sym->info());
237  ASSERT_TRUE( (ResolveInfo::weak_flag | ResolveInfo::common_flag) == old_sym->info());
238
239  bool override = false;
240  bool result = m_pResolver->resolve(*old_sym, *new_sym, override);
241  ASSERT_TRUE(result);
242  ASSERT_TRUE( override );
243  ASSERT_TRUE(999 == old_sym->size());
244}
245
246TEST_F( StaticResolverTest, OverrideCommonByDefine)
247{
248  ResolveInfo* old_sym = ResolveInfo::Create("abc");
249  ResolveInfo* new_sym = ResolveInfo::Create("abc");
250
251  old_sym->setDesc(ResolveInfo::Common);
252  old_sym->setSize(0);
253
254  new_sym->setDesc(ResolveInfo::Define);
255  new_sym->setSize(999);
256
257  ASSERT_TRUE( ResolveInfo::Define == new_sym->desc());
258  ASSERT_TRUE( ResolveInfo::Common == old_sym->desc());
259
260  ASSERT_TRUE( ResolveInfo::define_flag == new_sym->info());
261  ASSERT_TRUE( ResolveInfo::common_flag == old_sym->info());
262
263  bool override = false;
264  bool result = m_pResolver->resolve(*old_sym, *new_sym, override);
265  ASSERT_TRUE(result);
266  ASSERT_TRUE( override );
267  ASSERT_TRUE(999 == old_sym->size());
268}
269
270TEST_F( StaticResolverTest, SetUpDesc)
271{
272  ResolveInfo* sym = ResolveInfo::Create("abc");
273
274  sym->setIsSymbol(true);
275
276//  ASSERT_FALSE( sym->isSymbol() );
277  ASSERT_TRUE( sym->isSymbol() );
278  ASSERT_TRUE( sym->isGlobal() );
279  ASSERT_FALSE( sym->isWeak() );
280  ASSERT_FALSE( sym->isLocal() );
281  ASSERT_FALSE( sym->isDefine() );
282  ASSERT_TRUE( sym->isUndef() );
283  ASSERT_FALSE( sym->isDyn() );
284  ASSERT_FALSE( sym->isCommon() );
285  ASSERT_FALSE( sym->isIndirect() );
286  ASSERT_TRUE( ResolveInfo::NoType == sym->type());
287  ASSERT_TRUE( 0 == sym->desc() );
288  ASSERT_TRUE( 0 == sym->binding() );
289  ASSERT_TRUE( 0 == sym->other() );
290
291  sym->setIsSymbol(false);
292  ASSERT_FALSE( sym->isSymbol() );
293//  ASSERT_TRUE( sym->isSymbol() );
294  ASSERT_TRUE( sym->isGlobal() );
295  ASSERT_FALSE( sym->isWeak() );
296  ASSERT_FALSE( sym->isLocal() );
297  ASSERT_FALSE( sym->isDefine() );
298  ASSERT_TRUE( sym->isUndef() );
299  ASSERT_FALSE( sym->isDyn() );
300  ASSERT_FALSE( sym->isCommon() );
301  ASSERT_FALSE( sym->isIndirect() );
302  ASSERT_TRUE( ResolveInfo::NoType == sym->type());
303  ASSERT_TRUE( 0 == sym->desc() );
304  ASSERT_TRUE( 0 == sym->binding() );
305  ASSERT_TRUE( 0 == sym->other() );
306
307  sym->setDesc(ResolveInfo::Define);
308  ASSERT_FALSE( sym->isSymbol() );
309//  ASSERT_TRUE( sym->isSymbol() );
310  ASSERT_TRUE( sym->isGlobal() );
311  ASSERT_FALSE( sym->isWeak() );
312  ASSERT_FALSE( sym->isLocal() );
313  ASSERT_TRUE( sym->isDefine() );
314  ASSERT_FALSE( sym->isUndef() );
315  ASSERT_FALSE( sym->isDyn() );
316  ASSERT_FALSE( sym->isCommon() );
317  ASSERT_FALSE( sym->isIndirect() );
318  ASSERT_TRUE( ResolveInfo::NoType == sym->type());
319  ASSERT_TRUE( ResolveInfo::Define == sym->desc() );
320  ASSERT_TRUE( 0 == sym->binding() );
321  ASSERT_TRUE( 0 == sym->other() );
322
323  sym->setDesc(ResolveInfo::Common);
324  ASSERT_FALSE( sym->isSymbol() );
325//  ASSERT_TRUE( sym->isSymbol() );
326  ASSERT_TRUE( sym->isGlobal() );
327  ASSERT_FALSE( sym->isWeak() );
328  ASSERT_FALSE( sym->isLocal() );
329  ASSERT_FALSE( sym->isDyn() );
330  ASSERT_FALSE( sym->isDefine() );
331  ASSERT_FALSE( sym->isUndef() );
332  ASSERT_TRUE( sym->isCommon() );
333  ASSERT_FALSE( sym->isIndirect() );
334  ASSERT_TRUE( ResolveInfo::NoType == sym->type());
335  ASSERT_TRUE( ResolveInfo::Common == sym->desc() );
336  ASSERT_TRUE( 0 == sym->binding() );
337  ASSERT_TRUE( 0 == sym->other() );
338
339  sym->setDesc(ResolveInfo::Indirect);
340  ASSERT_FALSE( sym->isSymbol() );
341  ASSERT_TRUE( sym->isGlobal() );
342  ASSERT_FALSE( sym->isWeak() );
343  ASSERT_FALSE( sym->isLocal() );
344  ASSERT_FALSE( sym->isDyn() );
345  ASSERT_FALSE( sym->isDefine() );
346  ASSERT_FALSE( sym->isUndef() );
347  ASSERT_FALSE( sym->isCommon() );
348  ASSERT_TRUE( sym->isIndirect() );
349  ASSERT_TRUE( ResolveInfo::NoType == sym->type());
350  ASSERT_TRUE( ResolveInfo::Indirect == sym->desc() );
351  ASSERT_TRUE( 0 == sym->binding() );
352  ASSERT_TRUE( 0 == sym->other() );
353
354  sym->setDesc(ResolveInfo::Undefined);
355  ASSERT_FALSE( sym->isSymbol() );
356  ASSERT_TRUE( sym->isGlobal() );
357  ASSERT_FALSE( sym->isWeak() );
358  ASSERT_FALSE( sym->isLocal() );
359  ASSERT_FALSE( sym->isDyn() );
360  ASSERT_TRUE( sym->isUndef() );
361  ASSERT_FALSE( sym->isDefine() );
362  ASSERT_FALSE( sym->isCommon() );
363  ASSERT_FALSE( sym->isIndirect() );
364  ASSERT_TRUE( ResolveInfo::NoType == sym->type());
365  ASSERT_TRUE( 0 == sym->desc() );
366  ASSERT_TRUE( 0 == sym->binding() );
367  ASSERT_TRUE( 0 == sym->other() );
368}
369
370TEST_F( StaticResolverTest, SetUpBinding)
371{
372  ResolveInfo* sym = ResolveInfo::Create("abc");
373
374  sym->setIsSymbol(true);
375
376//  ASSERT_FALSE( sym->isSymbol() );
377  ASSERT_TRUE( sym->isSymbol() );
378  ASSERT_TRUE( sym->isGlobal() );
379  ASSERT_FALSE( sym->isWeak() );
380  ASSERT_FALSE( sym->isLocal() );
381  ASSERT_FALSE( sym->isDefine() );
382  ASSERT_TRUE( sym->isUndef() );
383  ASSERT_FALSE( sym->isDyn() );
384  ASSERT_FALSE( sym->isCommon() );
385  ASSERT_FALSE( sym->isIndirect() );
386  ASSERT_TRUE( ResolveInfo::NoType == sym->type());
387  ASSERT_TRUE( 0 == sym->desc() );
388  ASSERT_TRUE( 0 == sym->binding() );
389  ASSERT_TRUE( 0 == sym->other() );
390
391  sym->setBinding(ResolveInfo::Global);
392  ASSERT_TRUE( sym->isSymbol() );
393  ASSERT_TRUE( sym->isGlobal() );
394  ASSERT_FALSE( sym->isWeak() );
395  ASSERT_FALSE( sym->isLocal() );
396  ASSERT_FALSE( sym->isDefine() );
397  ASSERT_TRUE( sym->isUndef() );
398  ASSERT_FALSE( sym->isDyn() );
399  ASSERT_FALSE( sym->isCommon() );
400  ASSERT_FALSE( sym->isIndirect() );
401  ASSERT_TRUE( ResolveInfo::NoType == sym->type());
402  ASSERT_TRUE( 0 == sym->desc() );
403  ASSERT_TRUE( ResolveInfo::Global == sym->binding() );
404  ASSERT_TRUE( 0 == sym->other() );
405
406  sym->setBinding(ResolveInfo::Weak);
407  ASSERT_TRUE( sym->isSymbol() );
408  ASSERT_FALSE( sym->isGlobal() );
409  ASSERT_TRUE( sym->isWeak() );
410  ASSERT_FALSE( sym->isLocal() );
411  ASSERT_FALSE( sym->isDyn() );
412  ASSERT_FALSE( sym->isDefine() );
413  ASSERT_TRUE( sym->isUndef() );
414  ASSERT_FALSE( sym->isCommon() );
415  ASSERT_FALSE( sym->isIndirect() );
416  ASSERT_TRUE( ResolveInfo::NoType == sym->type());
417  ASSERT_TRUE( 0 == sym->desc() );
418  ASSERT_TRUE( ResolveInfo::Weak == sym->binding() );
419  ASSERT_TRUE( 0 == sym->other() );
420
421  sym->setBinding(ResolveInfo::Local);
422  ASSERT_TRUE( sym->isSymbol() );
423  ASSERT_FALSE( sym->isGlobal() );
424  ASSERT_FALSE( sym->isWeak() );
425  ASSERT_TRUE( sym->isLocal() );
426  ASSERT_FALSE( sym->isDyn() );
427  ASSERT_FALSE( sym->isDefine() );
428  ASSERT_TRUE( sym->isUndef() );
429  ASSERT_FALSE( sym->isCommon() );
430  ASSERT_FALSE( sym->isIndirect() );
431  ASSERT_TRUE( ResolveInfo::NoType == sym->type());
432  ASSERT_TRUE( 0 == sym->desc() );
433  ASSERT_TRUE( ResolveInfo::Local == sym->binding() );
434  ASSERT_TRUE( 0 == sym->other() );
435}
436
437