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