-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.cpp
More file actions
454 lines (427 loc) · 17.1 KB
/
Copy pathLoader.cpp
File metadata and controls
454 lines (427 loc) · 17.1 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//
// HAC/65 6502 Inferencing Disassembler
//
// This work is licensed under the MIT License <https://opensource.org/licenses/MIT>
// Copyright 2018 David Hinson <https://github.com/dhinson919>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Portions of this work are derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm
//
#include <fstream>
#include <iomanip>
#include "IHac65.hpp"
#include "Loader.hpp"
namespace Hac65
{
uint16_t
Loader::JsonValueToUint16 (const json &json) const
{
uint16_t result{0};
if (json.is_number())
result = json;
else if (json.is_string())
result = FlexIntToUint16(json.get<std::string>());
else
{
std::ostringstream text;
text << "malformed value: " << json << std::endl;
throw Hac65Exception(text.str());
}
return result;
}
void
Loader::LoadAroJson (const std::string &architecture, const json &aroJson)
{
for (auto topItor{std::begin(aroJson)}; topItor != std::end(aroJson); ++topItor)
{
const auto &topKey{topItor.key()};
if (topKey == "origin")
{
const auto &topValue{topItor.value()};
if (!topValue.is_number() && !topValue.is_string())
{
std::ostringstream text;
text << "malformed origin spec: " << topValue << std::endl;
throw OverlayError(text.str());
}
Address address{static_cast<Address>(JsonValueToUint16(topValue))};
if (!_pAnalyzer->HasOriginAddress())
_pAnalyzer->DeclareOriginAddress(address);
}
else if (topKey == "equates")
{
const auto &topValue{topItor.value()};
if (!topValue.is_object())
{
std::ostringstream text;
text << "malformed equates spec: " << topValue << std::endl;
throw OverlayError(text.str());
}
for (auto equateItor{std::begin(topValue)}; equateItor != std::end(topValue); ++equateItor)
{
const auto &equate{equateItor.key()};
const auto &value{equateItor.value()};
uint16_t equateInt{JsonValueToUint16(value)};
_pAnalyzer->DeclareEquate(equate, equateInt);
}
}
else if (topKey == "code_labels")
{
const auto &topValue{topItor.value()};
if (!topValue.is_object())
{
std::ostringstream text;
text << "malformed code labels spec: " << topValue << std::endl;
throw OverlayError(text.str());
}
for (auto labelItor{std::begin(topValue)}; labelItor != std::end(topValue); ++labelItor)
{
const auto &label{labelItor.key()};
const auto &addressJson{labelItor.value()};
Address address{static_cast<Address>(JsonValueToUint16(addressJson))};
_pAnalyzer->DeclareCodeLabel(label, address);
}
}
else if (topKey == "data_labels")
{
const auto &topValue{topItor.value()};
if (!topValue.is_object())
{
std::ostringstream text;
text << "malformed data labels spec: " << topValue << std::endl;
throw OverlayError(text.str());
}
for (auto labelItor{std::begin(topValue)}; labelItor != std::end(topValue); ++labelItor)
{
const auto &label{labelItor.key()};
const auto &addressJson{labelItor.value()};
Address address{static_cast<Address>(JsonValueToUint16(addressJson))};
_pAnalyzer->DeclareDataLabel(label, address);
}
}
else if (topKey == "structures")
{
const auto &topValue{topItor.value()};
if (!topValue.is_object())
{
std::ostringstream text;
text << "malformed structures spec: " << topValue << std::endl;
throw OverlayError(text.str());
}
for (auto structureItor{std::begin(topValue)}; structureItor != std::end(topValue); ++structureItor)
{
const auto &structure{structureItor.value()};
if (!structure.is_object())
{
std::ostringstream text;
text << "malformed structure spec: " << structure << std::endl;
throw OverlayError(text.str());
}
const std::string &structureKey{structureItor.key()};
auto kind{LookupStructureKind(structureKey)};
if (kind != SK__Unknown)
{
const auto &tables{structureItor.value()};
if (!tables.is_object())
{
std::ostringstream text;
text << "malformed tables spec: " << tables << std::endl;
throw OverlayError(text.str());
}
for (auto tableItor{std::begin(tables)}; tableItor != std::end(tables); ++tableItor)
{
const auto &addressJson{tableItor.key()};
Address address{static_cast<Address>(JsonValueToUint16(addressJson))};
auto count{JsonValueToUint16(tableItor.value())};
switch (kind)
{
case SK_NormalVectorTable:
_pAnalyzer->DeclareNormalVectorTable(address, count);
break;
case SK_IndirectVectorTable:
_pAnalyzer->DeclareIndirectVectorTable(address, count);
break;
case SK_KeyedVectorTable:
_pAnalyzer->DeclareKeyedVectorTable(address, count);
break;
case SK_KeyedIndirectVectorTable:
_pAnalyzer->DeclareKeyedIndirectVectorTable(address, count);
break;
case SK_KeyedIndirectMinusOneVectorTable:
_pAnalyzer->DeclareKeyedIndirectMinusOneVectorTable(address, count);
break;
case SK_JumpVectorTable:
_pAnalyzer->DeclareJumpVectorTable(address, count);
break;
case SK_MinusOneVectorTable:
_pAnalyzer->DeclareMinusOneVectorTable(address, count);
break;
case SK_SplitVectorTable:
_pAnalyzer->DeclareSplitVectorTable(address, count);
break;
default:
{
std::ostringstream text;
text << "unsupported vector table kind: " << kind << std::endl;
throw OverlayError(text.str());
}
break;
}
}
}
else
{
std::ostringstream text;
text << "unknown vector table kind: " << structureKey << std::endl;
throw OverlayError(text.str());
}
}
}
else if (topKey == "expert")
{
const auto &topValue{topItor.value()};
if (!topValue.is_object())
{
std::ostringstream text;
text << "malformed expert spec: " << topValue << std::endl;
throw OverlayError(text.str());
}
for (auto expertItor{std::begin(topValue)}; expertItor != std::end(topValue); ++expertItor)
{
const auto &expertKey{expertItor.key()};
const auto &expertValue{expertItor.value()};
if (expertKey == "lands")
{
if (!expertValue.is_array())
{
std::ostringstream text;
text << "malformed lands spec: " << expertValue << std::endl;
throw OverlayError(text.str());
}
for (auto landsItor{std::begin(expertValue)}; landsItor != std::end(expertValue); ++landsItor)
{
const auto &landValue{landsItor.value()};
if (!landValue.is_number() && !landValue.is_string())
{
std::ostringstream text;
text << "malformed land value: " << landValue << std::endl;
throw OverlayError(text.str());
}
Address address{static_cast<Address>(JsonValueToUint16(landValue))};
_pAnalyzer->DeclareLand(address);
}
}
else if (expertKey == "leaps")
{
if (!expertValue.is_array())
{
std::ostringstream text;
text << "malformed leaps spec: " << expertValue << std::endl;
throw OverlayError(text.str());
}
for (auto leapsItor{std::begin(expertValue)}; leapsItor != std::end(expertValue); ++leapsItor)
{
const auto &leapValue{leapsItor.value()};
if (!leapValue.is_number() && !leapValue.is_string())
{
std::ostringstream text;
text << "malformed leap value: " << leapValue << std::endl;
throw OverlayError(text.str());
}
Address address{static_cast<Address>(JsonValueToUint16(leapValue))};
_pAnalyzer->DeclareLeap(address);
}
}
else
{
std::ostringstream text;
text << "unknown expert spec: " << expertKey << std::endl;
throw OverlayError(text.str());
}
}
}
else
{
std::ostringstream text;
text << "unknown spec: " << topKey << std::endl;
throw OverlayError(text.str());
}
}
_overlays.push_front({architecture, aroJson});
}
const char *kIncludeDirectiveSyntax{R"(^\@(include)[\s]*["][A-Za-z0-9._-]{1,20}["])"};
void
Loader::LoadAroStream (std::ifstream &aroStream, const std::string &architecture, int depth)
{
if (depth > 10)
{
std::ostringstream text;
text << "max architecture overlay depth of 10 exceeded by " << architecture << std::endl;
throw OverlayError(text.str());
}
std::stringstream jsonStream;
std::string line;
while (std::getline(aroStream, line))
{
auto commentPos{line.find('#')};
if (commentPos != std::string::npos)
line = line.substr(0, commentPos);
std::regex include(kIncludeDirectiveSyntax, std::regex_constants::icase);
std::smatch match;
if (std::regex_search(line, match, include))
{
auto startQuote{match.str().find('"') + 1};
auto endQuote{match.str().rfind('"')};
std::string nextArchitecture{match.str().substr(startQuote, endQuote - startQuote)};
LoadAroFile(nextArchitecture, depth + 1);
}
else
{
if (line[0] == '@')
{
std::ostringstream text;
text << "invalid architecture overlay directive '" << line << "' in " << architecture << std::endl;
throw OverlayError(text.str());
}
else
jsonStream << line.c_str() << std::endl;
}
}
json aroJson;
try
{
jsonStream >> aroJson;
}
catch (json::exception &exc)
{
std::ostringstream text;
text << "architecture overlay " << architecture << ": " <<
exc.what() << std::endl;
throw OverlayError(text.str());
}
try
{
LoadAroJson(architecture, aroJson);
}
catch (Hac65Exception &exc)
{
std::ostringstream text;
text << "architecture overlay " << architecture << ": " <<
exc.what() << std::endl;
throw OverlayError(text.str());
}
}
bool
Loader::LoadBuiltinArchitecture (const std::string &architecture)
{
bool result{false};
auto itor = kBuiltinArchitectures.find(architecture);
if (itor != std::end(kBuiltinArchitectures))
{
const json &aroJson{itor->second};
LoadAroJson(architecture, aroJson);
result = true;
}
return result;
}
void
Loader::LoadAroFile (const std::string &architecture, int depth)
{
const auto aroFilename{architecture + ".aro"};
std::ifstream arcStream(aroFilename);
bool isOpen{arcStream.is_open()};
if (isOpen)
LoadAroStream(arcStream, architecture, depth);
else if (!LoadBuiltinArchitecture(architecture))
{
std::ostringstream text;
text << "cannot find .aro file for '" << architecture << '\'';
throw OverlayError(text.str());
}
}
void
Loader::LoadArchitecture ()
{
const auto architecture{GetArchitecture()};
int depth{0};
LoadAroFile(architecture, depth + 1);
}
void
Loader::LoadObjectFile ()
{
std::ifstream objectFile(_objectFilename, std::ios::in | std::ios::binary | std::ios::ate);
if (objectFile.is_open())
{
std::streamoff objectFileSize{objectFile.tellg()};
auto startPosition{GetStartPosition()};
if (startPosition >= objectFileSize)
{
std::ostringstream text;
text << std::hex << std::uppercase;
text << "invalid start position $" << startPosition <<
" (exceeds object file size $" << objectFileSize << ")" << std::endl;
throw UsageError(text.str());
}
auto endPosition{GetEndPosition()};
if (endPosition == -1)
endPosition = objectFileSize - 1;
if (endPosition < startPosition)
{
std::ostringstream text;
text << std::hex << std::uppercase;
text << "invalid start position $" << startPosition <<
" (exceeds end position $" << endPosition << ")" << std::endl;
throw UsageError(text.str());
}
if (endPosition >= objectFileSize)
{
std::ostringstream text;
text << std::hex << std::uppercase;
text << "invalid end position $" << endPosition <<
" (exceeds object file size $" << objectFileSize << ")" << std::endl;
throw UsageError(text.str());
}
_objectSize = endPosition - startPosition + 1;
if (_objectSize > kMaxObjectSize)
{
std::ostringstream text;
text << std::hex << std::uppercase;
text << "invalid object size $" << _objectSize <<
" (exceeds max object size $" << kMaxObjectSize << ")" << std::endl;
throw UsageError(text.str());
}
_object.resize(static_cast<size_t>(_objectSize));
objectFile.seekg(startPosition, std::ios::beg);
objectFile.read(reinterpret_cast<char *>(_object.data()), _objectSize);
objectFile.close();
_objectMd5.update(_object.data(), static_cast<MD5::size_type>(_objectSize));
_objectMd5.finalize();
}
else
{
std::ostringstream text;
text << "cannot find object-file '" << _objectFilename << '\'';
throw UsageError(text.str());
}
}
void
Loader::Load (std::shared_ptr<IAnalyzer> pAnalyzer)
{
_pAnalyzer = std::move(pAnalyzer);
LoadArchitecture();
LoadObjectFile();
_pAnalyzer->SetAssembly(std::move(_object));
}
}