-
Notifications
You must be signed in to change notification settings - Fork 4
/
mapfile.c
597 lines (479 loc) · 12.6 KB
/
mapfile.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//
// $Id$
// $Revision$
// $Date$
// $Author$
// $HeadURL$
//
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include "m8cdis.h"
#include "mapfile.h"
#include "util.h"
//
// Define for debugging to dump symbol list by section
//
#undef OPT_DUMPSYMBOLLIST
//
//
//
symbol_t *mapfileAddToSymbolList (section_t *sl, symbol_t *symbol)
{
symbol_t *newSymbol;
assert (sl);
newSymbol = calloc (1, sizeof (symbol_t));
memcpy (newSymbol, symbol, sizeof (symbol_t));
newSymbol->next = sl->symbolList;
sl->symbolList = newSymbol;
return newSymbol;
}
//
//
//
#if OPT_DUMPSYMBOLLIST
static void mapfileDumpSymbolList (section_t *section)
{
symbol_t *slp = section->symbolList;
while (slp)
{
printf ("%30s %04x\n", slp->label, slp->address);
slp = slp->next;
}
}
#endif
//
//
//
typedef struct compareList_s
{
char *label;
char fillChar;
}
compareList_t;
static compareList_t compareList [] =
{
{ "lit", 'L' },
{ "func_lit", 'L' },
{ "psoc_config", 'C' },
{ "UserModules", 'I' },
{ "text", 'I' },
{ "TOP", 'I' },
{ "E2PROM_AREA", 'E' },
};
static char mapfileSectionNameToFillChar (char *label)
{
unsigned int i;
for (i = 0; i < arrsizeof (compareList); i++)
if (!strcasecmp (label, compareList [i].label))
return compareList [i].fillChar;
return 'U';
}
//
//
//
section_t *mapfileAddToSectionList (section_t **sl, section_t *section)
{
section_t *slLast = *sl;
assert (sl != NULL);
if (!*sl)
{
if (!(*sl = slLast = calloc (1, sizeof (section_t))))
{
perror ("mapfileAddToSectionList: calloc");
return NULL;
}
}
else
{
while (slLast->next)
slLast = slLast->next;
slLast->next = calloc (1, sizeof (section_t));
slLast = slLast->next;
}
memcpy (slLast, section, sizeof (section_t));
return slLast;
}
//
//
//
typedef enum
{
STATE_WANT_AREA_TAG = 0,
STATE_WANT_AREA_DASH_LINE_AS_ADDR_SIZE,
STATE_WANT_AREA_DASH_LINE_AS_START_END,
STATE_WANT_SECTION_AS_ADDR_SIZE,
STATE_WANT_SECTION_AS_START_END,
STATE_WANT_ADDRESS_TAG,
STATE_WANT_ADDRESS_DASH_LINE,
STATE_WANT_ADDRESS_ENTRY,
}
state_e;
static section_t *mapfileGet (FILE *fp)
{
char buffer [512];
state_e state = STATE_WANT_AREA_TAG;
int lineNumber = 0;
section_t *sectionList = NULL;
section_t *sl = NULL;
while (fgets (buffer, sizeof (buffer), fp))
{
++lineNumber;
utilDeleteLeadingWhiteSpace (buffer);
utilDeleteTrailingWhiteSpace (buffer);
switch (state)
{
case STATE_WANT_AREA_TAG :
{
if (!strncmp (buffer, "Area ", 5))
{
char *area, *startOrAddr, *sizeOrEnd;
area = strtok (buffer, " \t");
startOrAddr = strtok (NULL, " \t");
sizeOrEnd = strtok (NULL, " \t");
if (!strcasecmp (startOrAddr, "Addr") && !strcasecmp (sizeOrEnd, "Size"))
state = STATE_WANT_AREA_DASH_LINE_AS_ADDR_SIZE;
else if (!strcasecmp (startOrAddr, "Start") && !strcasecmp (sizeOrEnd, "End"))
state = STATE_WANT_AREA_DASH_LINE_AS_START_END;
else if (!strcasecmp (startOrAddr, "Definitions"))
state = STATE_WANT_AREA_TAG;
else
{
fprintf (stderr, "Eeek! Can't figure out what sort of area line is at line %d\n", lineNumber);
return 0;
}
}
else
state = STATE_WANT_AREA_TAG;
}
break;
case STATE_WANT_AREA_DASH_LINE_AS_ADDR_SIZE :
state = (!strncmp (buffer, "-----", 5) ? STATE_WANT_SECTION_AS_ADDR_SIZE : STATE_WANT_AREA_DASH_LINE_AS_ADDR_SIZE);
break;
case STATE_WANT_AREA_DASH_LINE_AS_START_END :
state = (!strncmp (buffer, "-----", 5) ? STATE_WANT_SECTION_AS_START_END : STATE_WANT_AREA_DASH_LINE_AS_START_END);
break;
case STATE_WANT_SECTION_AS_ADDR_SIZE :
{
char *label, *addr, *size, *attr;
section_t section;
label = strtok (buffer, " \t"); // Get section name
addr = strtok (NULL, " \t"); // Get section address
size = strtok (NULL, " \t"); // Get section size
strtok (NULL, " \t"); // Skip =
strtok (NULL, " \t"); // Skip decimal
strtok (NULL, " \t"); // Skip bytes
attr = strtok (NULL, " \t"); // Get attribute list
if (mapfileFindSection (sectionList, label))
{
fprintf (stderr, "Eeek! Section \"%s\" is duplicated. Aborting.\n", label);
return NULL;
}
memset (§ion, 0, sizeof (section));
section.label = strdup (label);
section.address = utilHtoi (addr);
section.size = utilHtoi (size);
section.mapFill = mapfileSectionNameToFillChar (label);
if (strcasestr (attr, "rom"))
section.memtype = MEMTYPE_ROM;
else if (strcasestr (attr, "ram"))
section.memtype = MEMTYPE_RAM;
else
section.memtype = MEMTYPE_UNKNOWN;
if (!(sl = mapfileAddToSectionList (§ionList, §ion)))
return NULL;
state = STATE_WANT_ADDRESS_TAG;
}
break;
case STATE_WANT_SECTION_AS_START_END :
{
char *label, *start, *end, *attr;
section_t section;
label = strtok (buffer, " \t"); // Get section name
start = strtok (NULL, " \t"); // Get section start
end = strtok (NULL, " \t"); // Get section end
strtok (NULL, " \t"); // Skip =
strtok (NULL, " \t"); // Skip decimal
strtok (NULL, " \t"); // Skip bytes
attr = strtok (NULL, " \t"); // Get attribute list
if (mapfileFindSection (sectionList, label))
{
fprintf (stderr, "Eeek! Section \"%s\" is duplicated. Aborting.\n", label);
return NULL;
}
memset (§ion, 0, sizeof (section));
section.label = strdup (label);
section.address = utilHtoi (start);
section.size = utilHtoi (end) - section.address;
section.mapFill = mapfileSectionNameToFillChar (label);
if (strcasestr (attr, "rom"))
section.memtype = MEMTYPE_ROM;
else if (strcasestr (attr, "ram"))
section.memtype = MEMTYPE_RAM;
else
section.memtype = MEMTYPE_UNKNOWN;
if (!(sl = mapfileAddToSectionList (§ionList, §ion)))
return NULL;
state = STATE_WANT_ADDRESS_TAG;
}
break;
case STATE_WANT_ADDRESS_TAG :
state = (!strncmp (buffer, "Addr ", 5) ? STATE_WANT_ADDRESS_DASH_LINE : STATE_WANT_ADDRESS_TAG);
break;
case STATE_WANT_ADDRESS_DASH_LINE :
state = (!strncmp (buffer, "-----", 5) ? STATE_WANT_ADDRESS_ENTRY : STATE_WANT_ADDRESS_DASH_LINE);
break;
case STATE_WANT_ADDRESS_ENTRY :
{
if (strlen (buffer) > 1)
{
char *label, *addr;
symbol_t symbol;
addr = strtok (buffer, " \t");
label = strtok (NULL, " \t");
memset (&symbol, 0, sizeof (symbol_t));
symbol.label = strdup (label);
symbol.address = utilHtoi (addr);
if (!mapfileAddToSymbolList (sl, &symbol))
return NULL;
state = STATE_WANT_ADDRESS_ENTRY;
}
else
state = STATE_WANT_AREA_TAG;
}
break;
default :
fprintf (stderr, "mapfileGet: Unexpected state value %d. Aborting.\n", state);
return NULL;
}
}
return sectionList;
}
//
//
//
section_t *mapfileParse (char *filename)
{
FILE *fp;
section_t *section = NULL;
if (!(fp = fopen (filename, "r")))
{
printf ("Can't open %s: %d/%s\n", filename, errno, strerror (errno));
return NULL;
}
section = mapfileGet (fp);
fclose (fp);
return section;
}
//
//
//
section_t *mapfileFindSection (section_t *sl, char *label)
{
while (sl)
{
if (!strcmp (sl->label, label))
return sl;
sl = sl->next;
}
return NULL;
}
section_t *mapfileAddSpecialSymbols (section_t **sectionList, mapfileExtraSymbols_t *es)
{
while (es->label)
{
symbol_t *symbol;
if ((symbol = es->symbolList))
{
section_t section;
section_t *sl;
if ((sl = mapfileFindSection (*sectionList, es->label)) && ((*sectionList)->memtype != es->memtype))
{
printf ("Cannot add a section with the same name and a different type (%s)\n", es->label);
return NULL;
}
else
{
memset (§ion, 0, sizeof (section_t));
section.label = strdup (es->label);
section.address = es->address;
section.size = es->size;
section.memtype = es->memtype;
section.mapFill = es->mapFill;
if (!(sl = mapfileAddToSectionList (sectionList, §ion)))
return NULL;
}
while (symbol->label)
{
symbol_t tempSym;
memset (&tempSym, 0, sizeof (symbol_t));
tempSym.label = strdup (symbol->label);
tempSym.address = symbol->address;
if (!mapfileAddToSymbolList (sl, &tempSym))
return NULL;
symbol++;
}
}
es++;
}
return *sectionList;
}
//
//
//
int mapfileFreeSections (section_t *sl)
{
section_t *slTemp;
while (sl)
{
while (sl->symbolList)
{
symbol_t *s;
if (sl->symbolList->label)
free (sl->symbolList->label);
s = sl->symbolList->next;
free (sl->symbolList);
sl->symbolList = s;
}
if (sl->label)
free (sl->label);
slTemp = sl->next;
free (sl);
sl = slTemp;
}
return 1;
}
//
//
//
void mapfileDisplaySectionList (section_t *slp)
{
printf ("Section listing\n");
while (slp)
{
printf (" %30s %04x %04x %s\n", slp->label, slp->address, slp->size, (slp->memtype == 1) ? "rom" : (slp->memtype == 2) ? "ram" : "Unknown!");
#if OPT_DUMPSYMBOLLIST
mapfileDumpSymbolList (slp);
#endif
slp = slp->next;
}
printf ("\n");
}
//
//
//
static void *mapfileCalloc (size_t bytes)
{
void *p;
if (!(p = calloc (1, bytes)))
{
perror ("mapfileCalloc: calloc");
return NULL;
}
return p;
}
symbol_t *mapfileInsertSymbolSorted (symbol_t *symbolList, symbol_t *newSymbol)
{
if (!symbolList)
{
if (!(symbolList = mapfileCalloc (sizeof (symbol_t))))
return NULL;
symbolList->label = newSymbol->label;
symbolList->address = newSymbol->address;
}
else
{
symbol_t *sl = symbolList;
symbol_t *slPrevious = NULL;
while (sl)
{
if (newSymbol->address < sl->address)
{
if (sl == symbolList)
{
symbol_t *tal;
if (!(tal = mapfileCalloc (sizeof (symbol_t))))
return NULL;
tal->label = newSymbol->label;
tal->address = newSymbol->address;
tal->next = symbolList;
return tal;
}
else
{
symbol_t *tal;
if (!(tal = mapfileCalloc (sizeof (symbol_t))))
return NULL;
tal->label = newSymbol->label;
tal->address = newSymbol->address;
tal->next = slPrevious->next;
slPrevious->next = tal;
return symbolList;
}
}
slPrevious = sl;
sl = sl->next;
}
if (!sl)
{
symbol_t *tal;
if (!(tal = mapfileCalloc (sizeof (symbol_t))))
return NULL;
tal->label = newSymbol->label;
tal->address = newSymbol->address;
slPrevious->next = tal;
}
}
return symbolList;
}
//
//
//
symbol_t *mapfileSectionToSymbols (section_t *section, memtype_e memtype)
{
symbol_t *symbolList = NULL;
while (section)
{
if ((section->memtype == memtype) && section->symbolList)
{
symbol_t *sl = section->symbolList;
while (sl)
{
if (!(symbolList = mapfileInsertSymbolSorted (symbolList, sl)))
return NULL;
sl = sl->next;
}
}
section = section->next;
}
return symbolList;
}
int mapfileFreeSymbols (symbol_t *symbolList)
{
symbol_t *ap;
while (symbolList)
{
ap = symbolList->next;
free (symbolList);
symbolList = ap;
}
return 1;
}
//
//
//
void mapfileDisplaySymbolList (symbol_t *slp, char *type)
{
printf ("%s symbol table (sorted by address)\n", type);
while (slp)
{
printf (" %04x %s\n", slp->address, slp->label);
slp = slp->next;
}
printf ("\n");
}