RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
symbol.cc
1
2#include <cppunit/extensions/HelperMacros.h>
3#include <cppunit/TestFixture.h>
4
5#include "../../src/linker/symbol.h"
6
7using namespace Linker;
8
9namespace UnitTests
10{
11
12class TestSymbolName : public CppUnit::TestFixture
13{
14 CPPUNIT_TEST_SUITE(TestSymbolName);
15 CPPUNIT_TEST(testLoadName);
16 CPPUNIT_TEST(testLoadLibraryName);
17 CPPUNIT_TEST(testLoadOrdinalOrHint);
18 CPPUNIT_TEST(testGetLocalName);
19 CPPUNIT_TEST(testGetImportedName);
20 CPPUNIT_TEST(testGetImportedOrdinal);
21 CPPUNIT_TEST(testEquality);
22 CPPUNIT_TEST_SUITE_END();
23private:
24 SymbolName * internalSymbol;
25 SymbolName * importedSymbolByName;
26 SymbolName * importedSymbolWithHint;
27 SymbolName * importedSymbolByOrdinal;
28
29 void testLoadName();
30 void testLoadLibraryName();
31 void testLoadOrdinalOrHint();
32
33 void testGetLocalName();
34 void testGetImportedName();
35 void testGetImportedOrdinal();
36 void testEquality();
37public:
38 void setUp();
39 void tearDown();
40};
41
42void TestSymbolName::testLoadName()
43{
44 std::string result;
45
46 CPPUNIT_ASSERT(internalSymbol->LoadName(result));
47 CPPUNIT_ASSERT_EQUAL(std::string("InternalName"), result);
48
49 CPPUNIT_ASSERT(importedSymbolByName->LoadName(result));
50 CPPUNIT_ASSERT_EQUAL(std::string("ImportedName"), result);
51
52 CPPUNIT_ASSERT(importedSymbolWithHint->LoadName(result));
53 CPPUNIT_ASSERT_EQUAL(std::string("ImportedName"), result);
54
55 CPPUNIT_ASSERT(!importedSymbolByOrdinal->LoadName(result));
56}
57
58void TestSymbolName::testLoadLibraryName()
59{
60 std::string result;
61
62 CPPUNIT_ASSERT(!internalSymbol->LoadLibraryName(result));
63
64 CPPUNIT_ASSERT(importedSymbolByName->LoadLibraryName(result));
65 CPPUNIT_ASSERT_EQUAL(std::string("LibraryName"), result);
66
67 result = "";
68 CPPUNIT_ASSERT(importedSymbolWithHint->LoadLibraryName(result));
69 CPPUNIT_ASSERT_EQUAL(std::string("LibraryName"), result);
70
71 result = "";
72 CPPUNIT_ASSERT(importedSymbolByOrdinal->LoadLibraryName(result));
73 CPPUNIT_ASSERT_EQUAL(std::string("LibraryName"), result);
74}
75
76void TestSymbolName::testLoadOrdinalOrHint()
77{
78 uint16_t result;
79
80 CPPUNIT_ASSERT(!internalSymbol->LoadOrdinalOrHint(result));
81
82 CPPUNIT_ASSERT(!importedSymbolByName->LoadOrdinalOrHint(result));
83
84 result = -1;
85 CPPUNIT_ASSERT(importedSymbolWithHint->LoadOrdinalOrHint(result));
86 CPPUNIT_ASSERT_EQUAL(uint16_t(1234), result);
87
88 result = -1;
89 CPPUNIT_ASSERT(importedSymbolByOrdinal->LoadOrdinalOrHint(result));
90 CPPUNIT_ASSERT_EQUAL(uint16_t(1234), result);
91}
92
93void TestSymbolName::testGetLocalName()
94{
95 std::string result;
96
97 CPPUNIT_ASSERT(internalSymbol->GetLocalName(result));
98 CPPUNIT_ASSERT_EQUAL(std::string("InternalName"), result);
99
100 CPPUNIT_ASSERT(!importedSymbolByName->GetLocalName(result));
101
102 CPPUNIT_ASSERT(!importedSymbolWithHint->GetLocalName(result));
103
104 CPPUNIT_ASSERT(!importedSymbolByOrdinal->GetLocalName(result));
105}
106
107void TestSymbolName::testGetImportedName()
108{
109 std::string libraryResult;
110 std::string symbolResult;
111 uint16_t hintResult;
112
113 CPPUNIT_ASSERT(!internalSymbol->GetImportedName(libraryResult, symbolResult));
114
115 CPPUNIT_ASSERT(importedSymbolByName->GetImportedName(libraryResult, symbolResult));
116 CPPUNIT_ASSERT_EQUAL(std::string("ImportedName"), symbolResult);
117 CPPUNIT_ASSERT_EQUAL(std::string("LibraryName"), libraryResult);
118
119 symbolResult = libraryResult = "";
120 CPPUNIT_ASSERT(importedSymbolWithHint->GetImportedName(libraryResult, symbolResult));
121 CPPUNIT_ASSERT_EQUAL(std::string("ImportedName"), symbolResult);
122 CPPUNIT_ASSERT_EQUAL(std::string("LibraryName"), libraryResult);
123
124 CPPUNIT_ASSERT(!importedSymbolByOrdinal->GetImportedName(libraryResult, symbolResult));
125
126 //
127
128 CPPUNIT_ASSERT(!internalSymbol->GetImportedName(libraryResult, symbolResult, hintResult));
129
130 symbolResult = libraryResult = "";
131 CPPUNIT_ASSERT(importedSymbolByName->GetImportedName(libraryResult, symbolResult, hintResult));
132 CPPUNIT_ASSERT_EQUAL(std::string("ImportedName"), symbolResult);
133 CPPUNIT_ASSERT_EQUAL(std::string("LibraryName"), libraryResult);
134
135 symbolResult = libraryResult = "";
136 hintResult = -1;
137 CPPUNIT_ASSERT(importedSymbolWithHint->GetImportedName(libraryResult, symbolResult, hintResult));
138 CPPUNIT_ASSERT_EQUAL(std::string("ImportedName"), symbolResult);
139 CPPUNIT_ASSERT_EQUAL(std::string("LibraryName"), libraryResult);
140 CPPUNIT_ASSERT_EQUAL(uint16_t(1234), hintResult);
141
142 CPPUNIT_ASSERT(!importedSymbolByOrdinal->GetImportedName(libraryResult, symbolResult, hintResult));
143}
144
145void TestSymbolName::testGetImportedOrdinal()
146{
147 std::string libraryResult;
148 uint16_t hintResult;
149
150 CPPUNIT_ASSERT(!internalSymbol->GetImportedOrdinal(libraryResult, hintResult));
151
152 CPPUNIT_ASSERT(!importedSymbolByName->GetImportedOrdinal(libraryResult, hintResult));
153
154 CPPUNIT_ASSERT(!importedSymbolWithHint->GetImportedOrdinal(libraryResult, hintResult));
155
156 hintResult = -1;
157 CPPUNIT_ASSERT(importedSymbolByOrdinal->GetImportedOrdinal(libraryResult, hintResult));
158 CPPUNIT_ASSERT_EQUAL(std::string("LibraryName"), libraryResult);
159 CPPUNIT_ASSERT_EQUAL(uint16_t(1234), hintResult);
160}
161
162void TestSymbolName::testEquality()
163{
164 CPPUNIT_ASSERT_EQUAL(*internalSymbol, SymbolName("InternalName"));
165 CPPUNIT_ASSERT_EQUAL(*importedSymbolByName, SymbolName("LibraryName", "ImportedName"));
166 CPPUNIT_ASSERT_EQUAL(*importedSymbolWithHint, SymbolName("LibraryName", "ImportedName", 1234));
167 CPPUNIT_ASSERT_EQUAL(*importedSymbolByOrdinal, SymbolName("LibraryName", 1234));
168
169 CPPUNIT_ASSERT(SymbolName("abc") != SymbolName("def"));
170 CPPUNIT_ASSERT(SymbolName("abc") != SymbolName("lib", "abc"));
171 CPPUNIT_ASSERT(SymbolName("abc") != SymbolName("lib", "abc", 1234));
172 CPPUNIT_ASSERT(SymbolName("abc") != SymbolName("lib", 1234));
173
174 CPPUNIT_ASSERT(SymbolName("lib", "abc") != SymbolName("bil", "abc"));
175 CPPUNIT_ASSERT(SymbolName("lib", "abc") != SymbolName("lib", "def"));
176 CPPUNIT_ASSERT(SymbolName("lib", "abc") != SymbolName("lib", "abc", 1234));
177 CPPUNIT_ASSERT(SymbolName("lib", "abc") != SymbolName("lib", 1234));
178
179 CPPUNIT_ASSERT(SymbolName("lib", "abc", 1234) != SymbolName("bil", "abc", 1234));
180 CPPUNIT_ASSERT(SymbolName("lib", "abc", 1234) != SymbolName("lib", "def", 1234));
181 CPPUNIT_ASSERT(SymbolName("lib", "abc", 1234) != SymbolName("lib", "abc", 5678));
182 CPPUNIT_ASSERT(SymbolName("lib", "abc", 1234) != SymbolName("lib", 1234));
183
184 CPPUNIT_ASSERT(SymbolName("lib", 1234) != SymbolName("bil", 1234));
185 CPPUNIT_ASSERT(SymbolName("lib", 1234) != SymbolName("lib", 5678));
186}
187
188void TestSymbolName::setUp()
189{
190 internalSymbol = new SymbolName("InternalName");
191 importedSymbolByName = new SymbolName("LibraryName", "ImportedName");
192 importedSymbolWithHint = new SymbolName("LibraryName", "ImportedName", 1234);
193 importedSymbolByOrdinal = new SymbolName("LibraryName", 1234);
194}
195
196void TestSymbolName::tearDown()
197{
198 delete internalSymbol;
199 delete importedSymbolByName;
200 delete importedSymbolWithHint;
201 delete importedSymbolByOrdinal;
202}
203
204class TestExportedSymbol : public CppUnit::TestFixture
205{
206 CPPUNIT_TEST_SUITE(TestExportedSymbol);
207 CPPUNIT_TEST(testIsExportedByOrdinal);
208 CPPUNIT_TEST(testLoadName);
209 CPPUNIT_TEST(testLoadOrdinalOrHint);
210 CPPUNIT_TEST(testGetExportedByName);
211 CPPUNIT_TEST(testGetExportedByOrdinal);
212 CPPUNIT_TEST(testEquality);
213 CPPUNIT_TEST(testComparison);
214 CPPUNIT_TEST_SUITE_END();
215private:
216 ExportedSymbol * exportedSymbolByName;
217 ExportedSymbol * exportedSymbolWithHint;
218 ExportedSymbol * exportedSymbolByOrdinal;
219
220 void testIsExportedByOrdinal();
221 void testLoadName();
222 void testLoadOrdinalOrHint();
223 void testGetExportedByName();
224 void testGetExportedByOrdinal();
225 void testEquality();
226 bool equal(const ExportedSymbol& a, const ExportedSymbol& b);
227 bool unequal(const ExportedSymbol& a, const ExportedSymbol& b);
228 void testComparison();
229public:
230 void setUp();
231 void tearDown();
232};
233
234void TestExportedSymbol::testIsExportedByOrdinal()
235{
236 CPPUNIT_ASSERT(!exportedSymbolByName->IsExportedByOrdinal());
237 CPPUNIT_ASSERT(!exportedSymbolWithHint->IsExportedByOrdinal());
238 CPPUNIT_ASSERT(exportedSymbolByOrdinal->IsExportedByOrdinal());
239}
240
241void TestExportedSymbol::testLoadName()
242{
243 std::string result;
244
245 CPPUNIT_ASSERT(exportedSymbolByName->LoadName(result));
246 CPPUNIT_ASSERT_EQUAL(std::string("ExportedName"), result);
247
248 result = "";
249 CPPUNIT_ASSERT(exportedSymbolWithHint->LoadName(result));
250 CPPUNIT_ASSERT_EQUAL(std::string("ExportedName"), result);
251
252 result = "";
253 CPPUNIT_ASSERT(exportedSymbolByOrdinal->LoadName(result));
254 CPPUNIT_ASSERT_EQUAL(std::string("InternalName"), result);
255}
256
257void TestExportedSymbol::testLoadOrdinalOrHint()
258{
259 uint16_t result;
260
261 CPPUNIT_ASSERT(!exportedSymbolByName->LoadOrdinalOrHint(result));
262
263 result = -1;
264 CPPUNIT_ASSERT(exportedSymbolWithHint->LoadOrdinalOrHint(result));
265 CPPUNIT_ASSERT_EQUAL(uint16_t(1234), result);
266
267 result = -1;
268 CPPUNIT_ASSERT(exportedSymbolByOrdinal->LoadOrdinalOrHint(result));
269 CPPUNIT_ASSERT_EQUAL(uint16_t(1234), result);
270}
271
272void TestExportedSymbol::testGetExportedByName()
273{
274 std::string nameResult;
275 uint16_t hintResult;
276
277 CPPUNIT_ASSERT(exportedSymbolByName->GetExportedByName(nameResult));
278 CPPUNIT_ASSERT_EQUAL(std::string("ExportedName"), nameResult);
279
280 nameResult = "";
281 CPPUNIT_ASSERT(exportedSymbolWithHint->GetExportedByName(nameResult));
282 CPPUNIT_ASSERT_EQUAL(std::string("ExportedName"), nameResult);
283
284 CPPUNIT_ASSERT(!exportedSymbolByOrdinal->GetExportedByName(nameResult));
285
286 //
287
288 nameResult = "";
289 CPPUNIT_ASSERT(exportedSymbolByName->GetExportedByName(nameResult, hintResult));
290 CPPUNIT_ASSERT_EQUAL(std::string("ExportedName"), nameResult);
291
292 nameResult = "";
293 hintResult = -1;
294 CPPUNIT_ASSERT(exportedSymbolWithHint->GetExportedByName(nameResult, hintResult));
295 CPPUNIT_ASSERT_EQUAL(std::string("ExportedName"), nameResult);
296 CPPUNIT_ASSERT_EQUAL(uint16_t(1234), hintResult);
297
298 CPPUNIT_ASSERT(!exportedSymbolByOrdinal->GetExportedByName(nameResult, hintResult));
299}
300
301void TestExportedSymbol::testGetExportedByOrdinal()
302{
303 std::string nameResult;
304 uint16_t hintResult;
305
306 CPPUNIT_ASSERT(!exportedSymbolByName->GetExportedByOrdinal(hintResult));
307
308 CPPUNIT_ASSERT(!exportedSymbolWithHint->GetExportedByOrdinal(hintResult));
309
310 CPPUNIT_ASSERT(exportedSymbolByOrdinal->GetExportedByOrdinal(hintResult));
311 CPPUNIT_ASSERT_EQUAL(uint16_t(1234), hintResult);
312
313 //
314
315 CPPUNIT_ASSERT(!exportedSymbolByName->GetExportedByOrdinal(hintResult, nameResult));
316
317 CPPUNIT_ASSERT(!exportedSymbolWithHint->GetExportedByOrdinal(hintResult, nameResult));
318
319 nameResult = "";
320 hintResult = -1;
321 CPPUNIT_ASSERT(exportedSymbolByOrdinal->GetExportedByOrdinal(hintResult, nameResult));
322 CPPUNIT_ASSERT_EQUAL(uint16_t(1234), hintResult);
323 CPPUNIT_ASSERT_EQUAL(std::string("InternalName"), nameResult);
324}
325
326void TestExportedSymbol::testEquality()
327{
328 CPPUNIT_ASSERT_EQUAL(*exportedSymbolByName, ExportedSymbol("ExportedName"));
329 CPPUNIT_ASSERT_EQUAL(*exportedSymbolWithHint, ExportedSymbol("ExportedName", 1234));
330 CPPUNIT_ASSERT_EQUAL(*exportedSymbolByOrdinal, ExportedSymbol(1234, "InternalName"));
331
332 CPPUNIT_ASSERT(ExportedSymbol("abc") != ExportedSymbol("def"));
333 CPPUNIT_ASSERT(ExportedSymbol("abc") != ExportedSymbol("abc", 1234));
334 CPPUNIT_ASSERT(ExportedSymbol("abc") != ExportedSymbol(1234, "abc"));
335
336 CPPUNIT_ASSERT(ExportedSymbol("abc", 1234) != ExportedSymbol("def", 1234));
337 CPPUNIT_ASSERT(ExportedSymbol("abc", 1234) != ExportedSymbol("abc", 5678));
338 CPPUNIT_ASSERT(ExportedSymbol("abc", 1234) != ExportedSymbol(1234, "abc"));
339
340 CPPUNIT_ASSERT(ExportedSymbol(1234, "abc") != ExportedSymbol(5678, "abc"));
341 CPPUNIT_ASSERT(ExportedSymbol(1234, "abc") != ExportedSymbol(1234, "def"));
342 CPPUNIT_ASSERT(ExportedSymbol(1234, "abc") != ExportedSymbol("abc", 1234));
343}
344
345bool TestExportedSymbol::equal(const ExportedSymbol& a, const ExportedSymbol& b)
346{
347 return a <= b && a >= b;
348}
349
350bool TestExportedSymbol::unequal(const ExportedSymbol& a, const ExportedSymbol& b)
351{
352 return a < b || a > b;
353}
354
355void TestExportedSymbol::testComparison()
356{
357 CPPUNIT_ASSERT(unequal(ExportedSymbol("abc"), ExportedSymbol("def")));
358 CPPUNIT_ASSERT(unequal(ExportedSymbol("abc"), ExportedSymbol("abc", 1234)));
359 CPPUNIT_ASSERT(unequal(ExportedSymbol("abc"), ExportedSymbol(1234, "abc")));
360
361 CPPUNIT_ASSERT(unequal(ExportedSymbol("abc", 1234), ExportedSymbol("def", 1234)));
362 CPPUNIT_ASSERT(unequal(ExportedSymbol("abc", 1234), ExportedSymbol("abc", 5678)));
363 CPPUNIT_ASSERT(unequal(ExportedSymbol("abc", 1234), ExportedSymbol(1234, "abc")));
364
365 CPPUNIT_ASSERT(unequal(ExportedSymbol(1234, "abc"), ExportedSymbol(5678, "abc")));
366 CPPUNIT_ASSERT(unequal(ExportedSymbol(1234, "abc"), ExportedSymbol(1234, "def")));
367 CPPUNIT_ASSERT(unequal(ExportedSymbol(1234, "abc"), ExportedSymbol("abc", 1234)));
368}
369
370void TestExportedSymbol::setUp()
371{
372 exportedSymbolByName = new ExportedSymbol("ExportedName");
373 exportedSymbolWithHint = new ExportedSymbol("ExportedName", 1234);
374 exportedSymbolByOrdinal = new ExportedSymbol(1234, "InternalName");
375}
376
377void TestExportedSymbol::tearDown()
378{
379 delete exportedSymbolByName;
380 delete exportedSymbolWithHint;
381 delete exportedSymbolByOrdinal;
382}
383
384}
385
Represents a symbol to be exported from the module.
Definition symbol.h:119
bool GetExportedByName(std::string &result) const
For symbols exported by name, returns the name.
Definition symbol.cc:167
bool LoadName(std::string &result) const
Returns the name of the symbol.
Definition symbol.cc:148
bool LoadOrdinalOrHint(uint16_t &result) const
Returns the hint or ordinal of the symbol.
Definition symbol.cc:154
bool GetExportedByOrdinal(uint16_t &result) const
For symbols exported by ordinal, returns the ordinal.
Definition symbol.cc:197
Represents an (imported or internal) symbol name, which can be more complex than a string.
Definition symbol.h:18
bool GetImportedOrdinal(std::string &result_library, uint16_t &result_ordinal) const
For symbols imported by ordinal, returns the library and ordinal.
Definition symbol.cc:92
bool GetLocalName(std::string &result) const
For local symbols, returns the name.
Definition symbol.cc:45
bool LoadName(std::string &result) const
Retrieves the name of the symbol, if it has one.
Definition symbol.cc:6
bool LoadOrdinalOrHint(uint16_t &result) const
Retrieves the ordinal of symbols imported by ordinal, or the hint for imported symbols with a hint.
Definition symbol.cc:32
bool LoadLibraryName(std::string &result) const
Retrieves the name of the library, if it is imported.
Definition symbol.cc:19
bool GetImportedName(std::string &result_library, std::string &result_name) const
For symbols imported by name, returns the library and name.
Definition symbol.cc:60
Definition symbol.cc:205
Definition symbol.cc:13