-
Notifications
You must be signed in to change notification settings - Fork 0
/
sn.cpp
502 lines (409 loc) · 10.6 KB
/
sn.cpp
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include <algorithm>
#include <string>
#include <vector>
#include <cstring>
#include <err.h>
#include "mapped_file.h"
#include "sn.h"
typedef mapped_file::iterator iter;
template<class T>
uint8_t read_8(T &iter) {
uint8_t tmp = *iter;
++iter;
return tmp;
}
template<class T>
uint16_t read_16(T &iter) {
uint16_t tmp = 0;
tmp |= *iter << 0;
++iter;
tmp |= *iter << 8;
++iter;
return tmp;
}
template<class T>
uint32_t read_32(T &iter) {
uint32_t tmp = 0;
tmp |= *iter << 0;
++iter;
tmp |= *iter << 8;
++iter;
tmp |= *iter << 16;
++iter;
tmp |= *iter << 24;
++iter;
return tmp;
}
template<class T>
std::string read_cstring(T &iter) {
std::string s;
for(;;) {
uint8_t c = *iter;
++iter;
if (!c) break;
s.push_back(c);
}
return s;
}
template<class T>
std::string read_pstring(T &iter) {
std::string s;
unsigned size = *iter;
++iter;
s.reserve(size);
while (size--) {
uint8_t c = *iter;
++iter;
s.push_back(c);
}
return s;
}
[[maybe_unused]] static std::vector<uint8_t> &append(std::vector<uint8_t> &v, const std::vector<uint8_t> &w) {
v.insert(v.end(), w.begin(), w.end());
return v;
}
template<class InputIt>
static std::vector<uint8_t> &append(std::vector<uint8_t> &v, InputIt first, InputIt last) {
v.insert(v.end(), first, last);
return v;
}
sn_file *sn_unit::find_file(const std::string &name) {
auto iter = std::find_if(files.begin(), files.end(), [&name](const auto &x){
return x.name == name;
});
return iter == files.end() ? nullptr : &*iter;
}
sn_file *sn_unit::find_file(unsigned id) {
auto iter = std::find_if(files.begin(), files.end(), [id](const auto &x){
return x.file_id == id;
});
return iter == files.end() ? nullptr : &*iter;
}
sn_group *sn_unit::find_group(const std::string &name) {
auto iter = std::find_if(groups.begin(), groups.end(), [&name](const auto &x){
return x.name == name;
});
return iter == groups.end() ? nullptr : &*iter;
}
sn_group *sn_unit::find_group(unsigned id) {
auto iter = std::find_if(groups.begin(), groups.end(), [id](const auto &x){
return x.group_id == id;
});
return iter == groups.end() ? nullptr : &*iter;
}
sn_section *sn_unit::find_section(const std::string &name) {
auto iter = std::find_if(sections.begin(), sections.end(), [&name](const auto &x){
return x.name == name;
});
return iter == sections.end() ? nullptr : &*iter;
}
sn_section *sn_unit::find_section(unsigned id) {
auto iter = std::find_if(sections.begin(), sections.end(), [id](const auto &x){
return x.section_id == id;
});
return iter == sections.end() ? nullptr : &*iter;
}
sn_symbol *sn_unit::find_extern(const std::string &name) {
auto iter = std::find_if(externs.begin(), externs.end(), [&name](const auto &x){
return x.name == name;
});
return iter == externs.end() ? nullptr : &*iter;
}
sn_symbol *sn_unit::find_extern(unsigned id) {
auto iter = std::find_if(externs.begin(), externs.end(), [id](const auto &x){
return x.symbol_id == id;
});
return iter == externs.end() ? nullptr : &*iter;
}
// std::unordered_map<std::string, symbol> symbol_table;
/*
* Symbols...
* opcode = 0x0c
* if section id == 0, this is an absolute value
* (eg, ABC EQU $1234)
* labels are exported with section id/offset
* symbol for bss section is identical;
* SET/= are exported w/ section id (bug?)
*/
inline std::runtime_error eof(void) {
return std::runtime_error("Unexpected EOF");
}
inline std::runtime_error bad_opcode(std::string msg, unsigned opcode) {
char buffer[16];
snprintf(buffer, sizeof(buffer), ": $%02x", opcode & 0xff);
msg.append(buffer);
return std::runtime_error(msg);
}
iter parse_reloc(iter it, iter end, sn_reloc &out) {
unsigned tokens = 1;
if (std::distance(it, end) < 4) throw eof();
out.type = *it++;
out.address = read_16(it);
while(tokens) {
uint32_t value;
--tokens;
if (it == end) throw eof();
unsigned op = *it++;
switch(op) {
case OP_EQ:
case OP_NE:
case OP_LE:
case OP_LT:
case OP_GE:
case OP_GT:
case OP_ADD:
case OP_SUB:
case OP_MUL:
case OP_DIV:
case OP_AND:
case OP_OR:
case OP_XOR:
case OP_LSHIFT:
case OP_RSHIFT:
case OP_MOD:
out.expr.emplace_back(expr_token{ op, 0 });
tokens += 2;
break;
case V_CONST:
if (std::distance(it, end) < 4) throw eof();
value = read_32(it);
out.expr.emplace_back(expr_token{op, value});
break;
case V_SECTION:
case V_EXTERN:
if (std::distance(it, end) < 2) throw eof();
value = read_16(it);
out.expr.emplace_back(expr_token{op, value});
break;
default:
throw bad_opcode("Unknown relocation expression opcode", op);
}
}
return it;
}
iter parse_file(iter it, iter end, sn_file &file) {
if (std::distance(it, end) < 3) throw eof();
file.file_id = read_16(it);
unsigned l = *it;
if (std::distance(it, end) < l + 1) throw eof();
file.name = read_pstring(it);
return it;
}
iter parse_group(iter it, iter end, sn_group &group) {
if (std::distance(it, end) < 4) throw eof();
group.group_id = read_16(it);
group.flags = *it++;
unsigned l = *it;
if (std::distance(it, end) < l + 1) throw eof();
group.name = read_pstring(it);
return it;
}
iter parse_section(iter it, iter end, sn_section §ion) {
if (std::distance(it, end) < 6) throw eof();
section.section_id = read_16(it);
section.group_id = read_16(it);
section.flags = *it++;
unsigned l = *it;
if (std::distance(it, end) < l + 1) throw eof();
section.name = read_pstring(it);
return it;
}
iter parse_global_symbol(iter it, iter end, sn_symbol &symbol) {
if (std::distance(it, end) < 9) throw eof();
symbol.symbol_id = read_16(it);
symbol.section_id = read_16(it);
symbol.value = read_32(it);
unsigned l = *it;
if (std::distance(it, end) < l + 1) throw eof();
symbol.name = read_pstring(it);
return it;
}
iter parse_local_symbol(iter it, iter end, sn_symbol &symbol) {
if (std::distance(it, end) < 7) throw eof();
symbol.symbol_id = 0;
symbol.section_id = read_16(it);
symbol.value = read_32(it);
unsigned l = *it;
if (std::distance(it, end) < l + 1) throw eof();
symbol.name = read_pstring(it);
return it;
}
iter parse_extern_symbol(iter it, iter end, sn_symbol &symbol) {
if (std::distance(it, end) < 3) throw eof();
symbol.symbol_id = read_16(it);
unsigned l = *it;
if (std::distance(it, end) < l + 1) throw eof();
symbol.name = read_pstring(it);
return it;
}
iter skip_local_symbol(iter it, iter end) {
if (std::distance(it, end) < 7) throw eof();
it += 6; // uint16_t section_id, uint32_t offset
unsigned l = *it++;
if (std::distance(it, end) < l) throw eof();
it += l;
return it;
}
void sn_parse_unit(const std::string &path, sn_unit &unit) {
// if (verbose) printf("Linking %s\n", path.c_str());
std::error_code ec;
mapped_file mf(path, mapped_file::readonly, ec);
if (ec) {
errx(1, "Unable to open %s: %s", path.c_str(), ec.message().c_str());
}
unsigned current_file = 0;
unsigned current_line = 0;
unsigned current_section = 0;
sn_section *current = nullptr;
unit.filename = path;
auto it = mf.begin();
auto end = mf.end();
if (std::distance(it, end) < 7) throw eof();
if (memcmp(it, "LNK\x02", 4)) throw std::runtime_error("Not an SN Object File");
it += 6;
try {
while (it < end) {
unsigned op = *it++;
switch(op) {
case 0x00:
if (it == end) return;
throw std::runtime_error("Unexpected EOF segment");
case 0x02: {
// data block
if (!current)
throw std::runtime_error("No active section");
if (std::distance(it, end) < 2)
throw eof();
unsigned size = read_16(it);
if (std::distance(it, end) < size)
throw eof();
append(current->data, it, it + size);
it += size;
break;
}
case 0x06: {
// switch section.
if (std::distance(it, end) < 2)
throw eof();
current_section = read_16(it);
current = nullptr;
for (auto &s : unit.sections) {
if (s.section_id == current_section) {
current = &s;
break;
}
}
if (!current) {
throw std::runtime_error("No active section");
}
break;
}
case 0x08: {
// ds reserved space.
if (!current)
throw std::runtime_error("No active section");
if (std::distance(it, end) < 4)
throw eof();
uint32_t size = read_32(it);
current->data.resize(current->data.size() + size, 0x00);
// current->bss_size += size;
break;
}
case 0x0a: {
if (!current)
throw std::runtime_error("No active section");
auto &reloc = current->relocs.emplace_back();
it = parse_reloc(it, end, reloc);
reloc.file_id = current_file;
reloc.line = current_line;
break;
}
case 0x0c: {
// global symbol.
auto &symbol = unit.globals.emplace_back();
it = parse_global_symbol(it, end, symbol);
break;
}
case 0x0e: {
// extern symbol
auto &symbol = unit.externs.emplace_back();
it = parse_extern_symbol(it, end, symbol);
break;
}
case 0x12: {
// non-global symbol (included with /g)
auto &symbol = unit.locals.emplace_back();
it = parse_local_symbol(it, end, symbol);
// it = skip_local_symbol(it, end);
break;
}
case 0x10: {
auto §ion = unit.sections.emplace_back();
it = parse_section(it, end, section);
// re-calc current section since it may have re-allocated.
if (current) {
current = nullptr;
for (auto &s : unit.sections) {
if (s.section_id == current_section) {
current = &s;
break;
}
}
}
break;
}
case 0x14: {
auto &group = unit.groups.emplace_back();
it = parse_group(it, end, group);
break;
}
case 0x1c: {
auto &file = unit.files.emplace_back();
it = parse_file(it, end, file);
break;
}
case 0x1e: {
if (std::distance(it, end) < 6)
throw eof();
// set the line number.
current_file = read_16(it);
current_line = read_32(it);
break;
}
case 0x22:
current_line++;
break;
case 0x24:
if (it == end) eof();
current_line += *it++;
break;
case 0x2c: {
// unknown /z file record.
if (std::distance(it, end) < 3)
throw eof();
it += 3;
break;
}
case 0x28:
// local symbol record.
it = skip_local_symbol(it, end);
break;
case 0x2a: // 32-bit regs?
case 0x18: // 16-bit regs?
case 0x16: // 8-bit regs?
// regs pc=$12345678, etc
// uint8_t unknown, uint32_t value, uint8_t register, uint8_t unknown.
// register mapping: 00=a; 02=x; 04=y; 06=s;08=pc;0b=p;0c=d;0e=db
if (std::distance(it, end) < 7)
throw eof();
it += 7;
break;
default:
throw bad_opcode("Unknown opcode", op);
}
}
throw eof();
} catch (std::runtime_error &e) {
errx(1, "%s: %s at offset $%lx", path.c_str(), e.what(), std::distance(mf.begin(), it) - 1);
}
}