-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
patch.c
565 lines (498 loc) · 14 KB
/
patch.c
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
* This file is part of RGBDS.
*
* Copyright (c) 2019, Eldred Habert and RGBDS contributors.
*
* SPDX-License-Identifier: MIT
*/
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "link/object.h"
#include "link/patch.h"
#include "link/section.h"
#include "link/symbol.h"
#include "error.h"
#include "linkdefs.h"
#include "opmath.h"
/*
* This is an "empty"-type stack. Apart from the actual values, we also remember
* whether the value is a placeholder inserted for error recovery. This allows
* us to avoid cascading errors.
*
* The best way to think about this is a stack of (value, errorFlag) pairs.
* They are only separated for reasons of memory efficiency.
*/
struct RPNStack {
int32_t *values;
bool *errorFlags;
size_t size;
size_t capacity;
} stack;
static void initRPNStack(void)
{
stack.capacity = 64;
stack.values = malloc(sizeof(*stack.values) * stack.capacity);
stack.errorFlags = malloc(sizeof(*stack.errorFlags) * stack.capacity);
if (!stack.values || !stack.errorFlags)
err("Failed to init RPN stack");
}
static void clearRPNStack(void)
{
stack.size = 0;
}
static void pushRPN(int32_t value, bool comesFromError)
{
if (stack.size >= stack.capacity) {
static const size_t increase_factor = 2;
if (stack.capacity > SIZE_MAX / increase_factor)
errx("Overflow in RPN stack resize");
stack.capacity *= increase_factor;
stack.values =
realloc(stack.values, sizeof(*stack.values) * stack.capacity);
stack.errorFlags =
realloc(stack.errorFlags, sizeof(*stack.errorFlags) * stack.capacity);
// Static analysis tools complain that the capacity might become
// zero due to overflow, but fail to realize that it's caught by
// the overflow check above. Hence the stringent check below.
if (!stack.values || !stack.errorFlags || !stack.capacity)
err("Failed to resize RPN stack");
}
stack.values[stack.size] = value;
stack.errorFlags[stack.size] = comesFromError;
stack.size++;
}
// This flag tracks whether the RPN op that is currently being evaluated
// has popped any values with the error flag set.
static bool isError = false;
static int32_t popRPN(struct FileStackNode const *node, uint32_t lineNo)
{
if (stack.size == 0)
fatal(node, lineNo, "Internal error, RPN stack empty");
stack.size--;
isError |= stack.errorFlags[stack.size];
return stack.values[stack.size];
}
static void freeRPNStack(void)
{
free(stack.values);
free(stack.errorFlags);
}
// RPN operators
static uint32_t getRPNByte(uint8_t const **expression, int32_t *size,
struct FileStackNode const *node, uint32_t lineNo)
{
if (!(*size)--)
fatal(node, lineNo, "Internal error, RPN expression overread");
return *(*expression)++;
}
static struct Symbol const *getSymbol(struct Symbol const * const *symbolList,
uint32_t index)
{
assert(index != (uint32_t)-1); // PC needs to be handled specially, not here
struct Symbol const *symbol = symbolList[index];
// If the symbol is defined elsewhere...
if (symbol->type == SYMTYPE_IMPORT)
return sym_GetSymbol(symbol->name);
return symbol;
}
/*
* Compute a patch's value from its RPN string.
* @param patch The patch to compute the value of
* @param section The section the patch is contained in
* @return The patch's value
* @return isError Set if an error occurred during evaluation, and further
* errors caused by the value should be suppressed.
*/
static int32_t computeRPNExpr(struct Patch const *patch,
struct Symbol const * const *fileSymbols)
{
// Small shortcut to avoid a lot of repetition
#define popRPN() popRPN(patch->src, patch->lineNo)
uint8_t const *expression = patch->rpnExpression;
int32_t size = patch->rpnSize;
clearRPNStack();
while (size > 0) {
enum RPNCommand command = getRPNByte(&expression, &size,
patch->src, patch->lineNo);
int32_t value;
isError = false;
// Be VERY careful with two `popRPN` in the same expression.
// C does not guarantee order of evaluation of operands!
// So, if there are two `popRPN` in the same expression, make
// sure the operation is commutative.
switch (command) {
struct Symbol const *symbol;
char const *name;
struct Section const *sect;
case RPN_ADD:
value = popRPN() + popRPN();
break;
case RPN_SUB:
value = popRPN();
value = popRPN() - value;
break;
case RPN_MUL:
value = popRPN() * popRPN();
break;
case RPN_DIV:
value = popRPN();
if (value == 0) {
if (!isError)
error(patch->src, patch->lineNo, "Division by 0");
isError = true;
popRPN();
value = INT32_MAX;
} else {
value = op_divide(popRPN(), value);
}
break;
case RPN_MOD:
value = popRPN();
if (value == 0) {
if (!isError)
error(patch->src, patch->lineNo, "Modulo by 0");
isError = true;
popRPN();
value = 0;
} else {
value = op_modulo(popRPN(), value);
}
break;
case RPN_UNSUB:
value = -popRPN();
break;
case RPN_EXP:
value = popRPN();
if (value < 0) {
if (!isError)
error(patch->src, patch->lineNo, "Exponent by negative");
isError = true;
popRPN();
value = 0;
} else {
value = op_exponent(popRPN(), value);
}
break;
case RPN_OR:
value = popRPN() | popRPN();
break;
case RPN_AND:
value = popRPN() & popRPN();
break;
case RPN_XOR:
value = popRPN() ^ popRPN();
break;
case RPN_UNNOT:
value = ~popRPN();
break;
case RPN_LOGAND:
value = popRPN();
value = popRPN() && value;
break;
case RPN_LOGOR:
value = popRPN();
value = popRPN() || value;
break;
case RPN_LOGUNNOT:
value = !popRPN();
break;
case RPN_LOGEQ:
value = popRPN() == popRPN();
break;
case RPN_LOGNE:
value = popRPN() != popRPN();
break;
case RPN_LOGGT:
value = popRPN();
value = popRPN() > value;
break;
case RPN_LOGLT:
value = popRPN();
value = popRPN() < value;
break;
case RPN_LOGGE:
value = popRPN();
value = popRPN() >= value;
break;
case RPN_LOGLE:
value = popRPN();
value = popRPN() <= value;
break;
case RPN_SHL:
value = popRPN();
value = op_shift_left(popRPN(), value);
break;
case RPN_SHR:
value = popRPN();
value = op_shift_right(popRPN(), value);
break;
case RPN_USHR:
value = popRPN();
value = op_shift_right_unsigned(popRPN(), value);
break;
case RPN_BANK_SYM:
value = 0;
for (uint8_t shift = 0; shift < 32; shift += 8)
value |= getRPNByte(&expression, &size,
patch->src, patch->lineNo) << shift;
symbol = getSymbol(fileSymbols, value);
if (!symbol) {
error(patch->src, patch->lineNo,
"Requested BANK() of symbol \"%s\", which was not found",
fileSymbols[value]->name);
isError = true;
value = 1;
} else if (!symbol->section) {
error(patch->src, patch->lineNo,
"Requested BANK() of non-label symbol \"%s\"",
fileSymbols[value]->name);
isError = true;
value = 1;
} else {
value = symbol->section->bank;
}
break;
case RPN_BANK_SECT:
// `expression` is not guaranteed to be '\0'-terminated. If it is not,
// `getRPNByte` will have a fatal internal error.
// In either case, `getRPNByte` will not free `expression`.
name = (char const *)expression;
while (getRPNByte(&expression, &size, patch->src, patch->lineNo))
;
sect = sect_GetSection(name);
if (!sect) {
error(patch->src, patch->lineNo,
"Requested BANK() of section \"%s\", which was not found",
name);
isError = true;
value = 1;
} else {
value = sect->bank;
}
break;
case RPN_BANK_SELF:
if (!patch->pcSection) {
error(patch->src, patch->lineNo,
"PC has no bank outside a section");
isError = true;
value = 1;
} else {
value = patch->pcSection->bank;
}
break;
case RPN_SIZEOF_SECT:
// This has assumptions commented in the `RPN_BANK_SECT` case above.
name = (char const *)expression;
while (getRPNByte(&expression, &size, patch->src, patch->lineNo))
;
sect = sect_GetSection(name);
if (!sect) {
error(patch->src, patch->lineNo,
"Requested SIZEOF() of section \"%s\", which was not found",
name);
isError = true;
value = 1;
} else {
value = sect->size;
}
break;
case RPN_STARTOF_SECT:
// This has assumptions commented in the `RPN_BANK_SECT` case above.
name = (char const *)expression;
while (getRPNByte(&expression, &size, patch->src, patch->lineNo))
;
sect = sect_GetSection(name);
assert(sect->offset == 0);
if (!sect) {
error(patch->src, patch->lineNo,
"Requested STARTOF() of section \"%s\", which was not found",
name);
isError = true;
value = 1;
} else {
value = sect->org;
}
break;
case RPN_HRAM:
value = popRPN();
if (!isError && (value < 0
|| (value > 0xFF && value < 0xFF00)
|| value > 0xFFFF)) {
error(patch->src, patch->lineNo,
"Value %" PRId32 " is not in HRAM range", value);
isError = true;
}
value &= 0xFF;
break;
case RPN_RST:
value = popRPN();
// Acceptable values are 0x00, 0x08, 0x10, ..., 0x38
// They can be easily checked with a bitmask
if (value & ~0x38) {
if (!isError)
error(patch->src, patch->lineNo,
"Value %" PRId32 " is not a RST vector", value);
isError = true;
}
value |= 0xC7;
break;
case RPN_CONST:
value = 0;
for (uint8_t shift = 0; shift < 32; shift += 8)
value |= getRPNByte(&expression, &size,
patch->src, patch->lineNo) << shift;
break;
case RPN_SYM:
value = 0;
for (uint8_t shift = 0; shift < 32; shift += 8)
value |= getRPNByte(&expression, &size,
patch->src, patch->lineNo) << shift;
if (value == -1) { // PC
if (!patch->pcSection) {
error(patch->src, patch->lineNo,
"PC has no value outside a section");
value = 0;
isError = true;
} else {
value = patch->pcOffset + patch->pcSection->org;
}
} else {
symbol = getSymbol(fileSymbols, value);
if (!symbol) {
error(patch->src, patch->lineNo,
"Unknown symbol \"%s\"", fileSymbols[value]->name);
isError = true;
} else {
value = symbol->value;
// Symbols attached to sections have offsets
if (symbol->section)
value += symbol->section->org;
}
}
break;
}
pushRPN(value, isError);
}
if (stack.size > 1)
error(patch->src, patch->lineNo,
"RPN stack has %zu entries on exit, not 1", stack.size);
isError = false;
return popRPN();
#undef popRPN
}
void patch_CheckAssertions(struct Assertion *assert)
{
verbosePrint("Checking assertions...\n");
initRPNStack();
while (assert) {
int32_t value = computeRPNExpr(&assert->patch,
(struct Symbol const * const *)assert->fileSymbols);
enum AssertionType type = (enum AssertionType)assert->patch.type;
if (!isError && !value) {
switch (type) {
case ASSERT_FATAL:
fatal(assert->patch.src, assert->patch.lineNo, "%s",
assert->message[0] ? assert->message
: "assert failure");
// Not reached
break; // Here so checkpatch doesn't complain
case ASSERT_ERROR:
error(assert->patch.src, assert->patch.lineNo, "%s",
assert->message[0] ? assert->message
: "assert failure");
break;
case ASSERT_WARN:
warning(assert->patch.src, assert->patch.lineNo, "%s",
assert->message[0] ? assert->message
: "assert failure");
break;
}
} else if (isError && type == ASSERT_FATAL) {
fatal(assert->patch.src, assert->patch.lineNo,
"couldn't evaluate assertion%s%s",
assert->message[0] ? ": " : "",
assert->message);
}
struct Assertion *next = assert->next;
free(assert->patch.rpnExpression);
free(assert->message);
free(assert);
assert = next;
}
freeRPNStack();
}
/*
* Applies all of a section's patches
* @param section The section to patch
* @param arg Ignored callback arg
*/
static void applyFilePatches(struct Section *section, struct Section *dataSection)
{
verbosePrint("Patching section \"%s\"...\n", section->name);
for (uint32_t patchID = 0; patchID < section->nbPatches; patchID++) {
struct Patch *patch = §ion->patches[patchID];
int32_t value = computeRPNExpr(patch,
(struct Symbol const * const *)
section->fileSymbols);
uint16_t offset = patch->offset + section->offset;
// `jr` is quite unlike the others...
if (patch->type == PATCHTYPE_JR) {
// Offset is relative to the byte *after* the operand
// PC as operand to `jr` is lower than reference PC by 2
uint16_t address = patch->pcSection->org + patch->pcOffset + 2;
int16_t jumpOffset = value - address;
if (!isError && (jumpOffset < -128 || jumpOffset > 127))
error(patch->src, patch->lineNo,
"jr target out of reach (expected -129 < %" PRId16 " < 128)",
jumpOffset);
dataSection->data[offset] = jumpOffset & 0xFF;
} else {
// Patch a certain number of bytes
struct {
uint8_t size;
int32_t min;
int32_t max;
} const types[] = {
[PATCHTYPE_BYTE] = {1, -128, 255},
[PATCHTYPE_WORD] = {2, -32768, 65536},
[PATCHTYPE_LONG] = {4, INT32_MIN, INT32_MAX}
};
if (!isError && (value < types[patch->type].min
|| value > types[patch->type].max))
error(patch->src, patch->lineNo,
"Value %#" PRIx32 "%s is not %u-bit",
value, value < 0 ? " (maybe negative?)" : "",
types[patch->type].size * 8U);
for (uint8_t i = 0; i < types[patch->type].size; i++) {
dataSection->data[offset + i] = value & 0xFF;
value >>= 8;
}
}
}
}
/*
* Applies all of a section's patches, iterating over "components" of
* unionized sections
* @param section The section to patch
* @param arg Ignored callback arg
*/
static void applyPatches(struct Section *section, void *arg)
{
if (!sect_HasData(section->type))
return;
(void)arg;
struct Section *dataSection = section;
do {
applyFilePatches(section, dataSection);
section = section->nextu;
} while (section);
}
void patch_ApplyPatches(void)
{
initRPNStack();
sect_ForEach(applyPatches, NULL);
freeRPNStack();
}