|
|
1.1 root 1: /*
2: * Hatari - gst2ascii.c
3: *
1.1.1.2 ! root 4: * Copyright (C) 2013-2014 by Eero Tamminen
1.1 root 5: *
6: * This file is distributed under the GNU General Public License, version 2
7: * or at your option any later version. Read the file gpl.txt for details.
8: *
9: * Convert DRI/GST symbol table in a binary into ASCII symbols file accepted
10: * by Hatari debugger and its profiler data post-processor. This will also
11: * allow manual editing of the symbol table (removing irrelevant labels or
12: * adding missing symbols for functions).
13: */
14:
15: #include <ctype.h>
16: #include <stdio.h>
17: #include <string.h>
18: #include <stdlib.h>
19: #include <stdint.h>
20: #include <stdbool.h>
21: #if defined(__MINT__) /* assume MiNT/lib is always big-endian */
22: # define SDL_SwapBE16(x) x
23: # define SDL_SwapBE32(x) x
24: #else
25: # include <SDL_endian.h>
26: #endif
27: #include <assert.h>
28:
29: typedef enum {
30: SYMTYPE_TEXT = 1,
31: SYMTYPE_DATA = 2,
32: SYMTYPE_BSS = 4
33: } symtype_t;
34:
35: typedef struct {
36: char *name;
37: uint32_t address;
38: symtype_t type;
39: } symbol_t;
40:
41: typedef struct {
42: int count; /* final symbol count */
43: int symbols; /* initial symbol count */
44: symbol_t *addresses; /* items sorted by address */
45: symbol_t *names; /* items sorted by symbol name */
46: } symbol_list_t;
47:
48: typedef struct {
49: uint32_t offset;
50: uint32_t end;
51: } prg_section_t;
52:
53: /* ------------------ options & usage ------------------ */
54:
55: #ifdef WIN32
56: #define PATHSEP '\\'
57: #else
58: #define PATHSEP '/'
59: #endif
60:
61: #define ARRAYSIZE(x) (int)(sizeof(x)/sizeof(x[0]))
62:
63: static const char *PrgPath;
64:
65: static struct {
66: symtype_t notypes;
67: bool no_obj;
68: bool no_local;
69: bool sort_name;
70: } Options;
71:
72: /*
73: * Show program usage, given error message
74: * return empty list
75: */
76: static symbol_list_t* usage(const char *msg)
77: {
78: const struct {
79: const char opt;
80: const char *desc;
81: } OptInfo[] = {
82: { 'n', "sort by name (not address)" },
83: { 'b', "no BSS symbols" },
84: { 'd', "no DATA symbols" },
85: { 't', "no TEXT symbols" },
86: { 'l', "no local (.L*) symbols" },
87: { 'o', "no object symbols (filenames or GCC internals)" },
88: };
89: const char *name;
90: int i;
91:
92: if ((name = strrchr(PrgPath, PATHSEP))) {
93: name++;
94: } else {
95: name = PrgPath;
96: }
97: fprintf(stderr,
98: "\n"
99: "Usage: %s [options] <Atari program>\n"
100: "\n"
101: "Outputs given program DRI/GST symbol table content\n"
102: "in ASCII format accepted by Hatari debugger and\n"
103: "its profiler data post-processor.\n"
104: "\n"
105: "Options:\n", name);
106: for (i = 0; i < ARRAYSIZE(OptInfo); i++) {
107: fprintf(stderr, "\t-%c\t%s\n", OptInfo[i].opt, OptInfo[i].desc);
108: }
109: if (msg) {
110: fprintf(stderr, "\nERROR: %s!\n", msg);
111: }
112: return NULL;
113: }
114:
115: /* ------------------ load and free functions ------------------ */
116:
117: /**
118: * compare function for qsort() to sort according to symbol address
119: */
120: static int symbols_by_address(const void *s1, const void *s2)
121: {
122: uint32_t addr1 = ((const symbol_t*)s1)->address;
123: uint32_t addr2 = ((const symbol_t*)s2)->address;
124:
125: if (addr1 < addr2) {
126: return -1;
127: }
128: if (addr1 > addr2) {
129: return 1;
130: }
131: fprintf(stderr, "WARNING: symbols '%s' & '%s' have the same 0x%x address.\n",
132: ((const symbol_t*)s1)->name, ((const symbol_t*)s2)->name, addr1);
133: return 0;
134: }
135:
136: /**
137: * compare function for qsort() to sort according to symbol name
138: */
139: static int symbols_by_name(const void *s1, const void *s2)
140: {
141: const char* name1 = ((const symbol_t*)s1)->name;
142: const char* name2 = ((const symbol_t*)s2)->name;
143: int ret;
144:
145: ret = strcmp(name1, name2);
146: if (!ret) {
147: fprintf(stderr, "WARNING: addresses 0x%x & 0x%x have the same '%s' name.\n",
148: ((const symbol_t*)s1)->address, ((const symbol_t*)s2)->address, name1);
149: }
150: return ret;
151: }
152:
153: /**
154: * Allocate symbol list & names for given number of items.
155: * Return allocated list or NULL on failure.
156: */
157: static symbol_list_t* symbol_list_alloc(int symbols)
158: {
159: symbol_list_t *list;
160:
161: if (!symbols) {
162: return NULL;
163: }
164: list = calloc(1, sizeof(symbol_list_t));
165: if (list) {
166: list->names = malloc(symbols * sizeof(symbol_t));
167: if (!list->names) {
168: free(list);
169: list = NULL;
170: }
171: }
172: return list;
173: }
174:
175: /**
176: * Free symbol list & names.
177: */
178: static void symbol_list_free(symbol_list_t *list)
179: {
180: if (list) {
181: if (list->names) {
182: free(list->names);
183: }
184: free(list);
185: }
186: }
187:
188: /**
1.1.1.2 ! root 189: * Return symbol type identifier char
! 190: */
! 191: static char symbol_char(int type)
! 192: {
! 193: switch (type) {
! 194: case SYMTYPE_TEXT: return 'T';
! 195: case SYMTYPE_DATA: return 'D';
! 196: case SYMTYPE_BSS: return 'B';
! 197: default: return '?';
! 198: }
! 199: }
! 200:
! 201: #define INVALID_SYMBOL_OFFSETS ((symbol_list_t*)1)
! 202:
! 203: /**
1.1 root 204: * Load symbols of given type and the symbol address addresses from
205: * DRI/GST format symbol table, and add given offsets to the addresses:
206: * http://toshyp.atari.org/en/005005.html
207: * Return symbols list or NULL for failure.
208: */
209: static symbol_list_t* symbols_load_dri(FILE *fp, prg_section_t *sections, uint32_t tablesize)
210: {
1.1.1.2 ! root 211: int i, count, symbols, outside;
1.1 root 212: int notypes, dtypes, locals, ofiles;
213: prg_section_t *section;
214: symbol_list_t *list;
215: symtype_t symtype;
216: #define DRI_ENTRY_SIZE 14
217: char name[23];
218: uint16_t symid;
219: uint32_t address;
220:
221: if (tablesize % DRI_ENTRY_SIZE) {
222: fprintf(stderr, "ERROR: invalid DRI/GST symbol table size %d!\n", tablesize);
223: return NULL;
224: }
225: symbols = tablesize / DRI_ENTRY_SIZE;
226: if (!(list = symbol_list_alloc(symbols))) {
227: return NULL;
228: }
229:
1.1.1.2 ! root 230: outside = dtypes = notypes = ofiles = locals = count = 0;
1.1 root 231: for (i = 1; i <= symbols; i++) {
232: /* read DRI symbol table slot */
233: if (fread(name, 8, 1, fp) != 1 ||
234: fread(&symid, sizeof(symid), 1, fp) != 1 ||
235: fread(&address, sizeof(address), 1, fp) != 1) {
236: break;
237: }
238: address = SDL_SwapBE32(address);
239: symid = SDL_SwapBE16(symid);
240:
241: /* GST extended DRI symbol format? */
242: if ((symid & 0x0048)) {
243: /* next slot is rest of name */
244: i += 1;
245: if (fread(name+8, 14, 1, fp) != 1) {
246: break;
247: }
248: name[22] = '\0';
249: } else {
250: name[8] = '\0';
251: }
252:
253: /* check section */
254: switch (symid & 0xf00) {
255: case 0x0200:
256: symtype = SYMTYPE_TEXT;
257: section = &(sections[0]);
258: break;
259: case 0x0400:
260: symtype = SYMTYPE_DATA;
261: section = &(sections[1]);
262: break;
263: case 0x0100:
264: symtype = SYMTYPE_BSS;
265: section = &(sections[2]);
266: break;
267: default:
268: if ((symid & 0xe000) == 0xe000) {
269: dtypes++;
270: continue;
271: }
272: fprintf(stderr, "WARNING: ignoring symbol '%s' in slot %d of unknown type 0x%x.\n", name, i, symid);
273: continue;
274: }
275: if (Options.notypes & symtype) {
276: notypes++;
277: continue;
278: }
279: if (Options.no_local) {
280: if (name[0] == '.' && name[1] == 'L') {
281: locals++;
282: continue;
283: }
284: }
285: if (Options.no_obj) {
286: const char *gcc_sym[] = {
287: "___gnu_compiled_c",
288: "gcc2_compiled."
289: };
290: int j, len = strlen(name);
291: /* object / file name? */
292: if (len > 2 && ((name[len-2] == '.' && name[len-1] == 'o') || strchr(name, '/'))) {
293: ofiles++;
294: continue;
295: }
296: /* useless symbols GCC (v2) seems to add to every object? */
297: for (j = 0; j < ARRAYSIZE(gcc_sym); j++) {
298: if (strcmp(name, gcc_sym[j]) == 0) {
299: ofiles++;
300: j = -1;
301: break;
302: }
303: }
304: if (j < 0) {
305: continue;
306: }
307: }
308: address += section->offset;
309: if (address > section->end) {
1.1.1.2 ! root 310: /* VBCC has 1 symbol outside of its section */
! 311: if (++outside > 2) {
! 312: /* potentially buggy version of VBCC vlink used */
! 313: fprintf(stderr, "ERROR: too many invalid offsets, skipping rest of symbols!\n");
! 314: symbol_list_free(list);
! 315: return INVALID_SYMBOL_OFFSETS;
! 316: }
! 317: fprintf(stderr, "WARNING: ignoring symbol '%s' of %c type in slot %d with invalid offset 0x%x (>= 0x%x).\n",
! 318: name, symbol_char(symtype), i, address, section->end);
1.1 root 319: continue;
320: }
321: list->names[count].address = address;
322: list->names[count].type = symtype;
323: list->names[count].name = strdup(name);
324: assert(list->names[count].name);
325: count++;
326: }
327: if (i <= symbols) {
328: perror("ERROR: reading symbol failed");
329: symbol_list_free(list);
330: return NULL;
331: }
332: if (notypes) {
333: fprintf(stderr, "NOTE: ignored %d unwanted symbol types.\n", notypes);
334: }
335: if (dtypes) {
336: fprintf(stderr, "NOTE: ignored %d globally defined equated values.\n", dtypes);
337: }
338: if (locals) {
339: fprintf(stderr, "NOTE: ignored %d unnamed / local symbols (= name starts with '.L').\n", locals);
340: }
341: if (ofiles) {
342: /* object file path names most likely get truncated and
343: * as result cause unnecessary symbol name conflicts in
344: * addition to object file addresses conflicting with
345: * first symbol in the object file.
346: */
347: fprintf(stderr, "NOTE: ignored %d object symbols (= name has '/', ends in '.o' or is GCC internal).\n", ofiles);
348: }
349: list->symbols = symbols;
350: list->count = count;
351: return list;
352: }
353:
354: /**
355: * Parse program header and use symbol table format specific loader
356: * loader function to load the symbols.
357: * Return symbols list or NULL for failure.
358: */
359: static symbol_list_t* symbols_load_binary(FILE *fp)
360: {
1.1.1.2 ! root 361: Uint32 textlen, datalen, bsslen, tablesize, tabletype, prgflags;
1.1 root 362: prg_section_t sections[3];
363: int offset, reads = 0;
1.1.1.2 ! root 364: Uint16 relocflag;
! 365: const char *info;
! 366: symbol_list_t* symbols;
1.1 root 367:
368: /* get TEXT, DATA & BSS section sizes */
369: reads += fread(&textlen, sizeof(textlen), 1, fp);
370: textlen = SDL_SwapBE32(textlen);
371: reads += fread(&datalen, sizeof(datalen), 1, fp);
372: datalen = SDL_SwapBE32(datalen);
373: reads += fread(&bsslen, sizeof(bsslen), 1, fp);
374: bsslen = SDL_SwapBE32(bsslen);
375:
376: /* get symbol table size & type and check that all reads succeeded */
377: reads += fread(&tablesize, sizeof(tablesize), 1, fp);
378: tablesize = SDL_SwapBE32(tablesize);
379: if (!tablesize) {
380: fprintf(stderr, "ERROR: symbol table missing from the program!\n");
381: return NULL;
382: }
383: reads += fread(&tabletype, sizeof(tabletype), 1, fp);
384: tabletype = SDL_SwapBE32(tabletype);
1.1.1.2 ! root 385:
! 386: /* get program header and whether there's reloc table */
! 387: reads += fread(&prgflags, sizeof(prgflags), 1, fp);
! 388: prgflags = SDL_SwapBE32(prgflags);
! 389: reads += fread(&relocflag, sizeof(relocflag), 1, fp);
! 390: relocflag = SDL_SwapBE32(relocflag);
! 391:
! 392: if (reads != 7) {
1.1 root 393: fprintf(stderr, "ERROR: program header reading failed!\n");
394: return NULL;
395: }
396:
1.1.1.2 ! root 397: /* symbols already have suitable offsets, so only acceptable end position needs to be calculated */
! 398: sections[0].offset = 0;
! 399: sections[0].end = textlen;
! 400: sections[1].offset = 0;
! 401: sections[1].end = datalen;
! 402: sections[2].offset = 0;
! 403: sections[2].end = bsslen;
! 404:
1.1 root 405: /* go to start of symbol table */
406: offset = 0x1C + textlen + datalen;
407: if (fseek(fp, offset, SEEK_SET) < 0) {
408: perror("ERROR: seeking to symbol table failed");
409: return NULL;
410: }
411:
412: switch (tabletype) {
413: case 0x4D694E54: /* "MiNT" */
1.1.1.2 ! root 414: info = "GCC/MiNT executable, GST symbol table.";
! 415: break;
1.1 root 416: case 0x0:
1.1.1.2 ! root 417: info = "TOS executable, DRI / GST symbol table.";
! 418: break;
1.1 root 419: default:
420: fprintf(stderr, "ERROR: unknown executable type 0x%x at offset 0x%x!\n", tabletype, offset);
1.1.1.2 ! root 421: return NULL;
1.1 root 422: }
1.1.1.2 ! root 423: fprintf(stderr, "0x%x program flags, reloc=%d, %s\n", prgflags, relocflag, info);
! 424:
! 425: fprintf(stderr, "Trying to load symbol table at offset 0x%x...\n", offset);
! 426: symbols = symbols_load_dri(fp, sections, tablesize);
! 427:
! 428: if (symbols == INVALID_SYMBOL_OFFSETS && fseek(fp, offset, SEEK_SET) == 0) {
! 429: fprintf(stderr, "Re-trying with TEXT-relative BSS/DATA section offsets...\n");
! 430: sections[1].end += textlen;
! 431: sections[2].end += (textlen + datalen);
! 432: symbols = symbols_load_dri(fp, sections, tablesize);
! 433: if (symbols) {
! 434: fprintf(stderr, "Load symbols without giving separate BSS/DATA offsets (they're TEXT relative).\n");
! 435: }
! 436: } else {
! 437: fprintf(stderr, "Load symbols with 'symbols <filename> TEXT DATA BSS' after starting the program.\n");
! 438: }
! 439: if (!symbols || symbols == INVALID_SYMBOL_OFFSETS) {
! 440: fprintf(stderr, "\n\n*** Try with 'nm -n <program>' (Atari/cross-compiler tool) instead ***\n\n");
! 441: return NULL;
! 442: }
! 443: return symbols;
1.1 root 444: }
445:
446: /**
447: * Load symbols of given type and the symbol address addresses from
448: * the given file and add given offsets to the addresses.
449: * Return symbols list or NULL for failure.
450: */
451: static symbol_list_t* symbols_load(const char *filename)
452: {
453: symbol_list_t *list;
454: uint16_t magic;
455: FILE *fp;
456:
457: fprintf(stderr, "Reading symbols from program '%s' symbol table...\n", filename);
458: if (!(fp = fopen(filename, "r"))) {
459: return usage("opening program file failed");
460: }
461: if (fread(&magic, sizeof(magic), 1, fp) != 1) {
462: return usage("reading program file failed");
463: }
464:
465: if (SDL_SwapBE16(magic) != 0x601A) {
466: return usage("file isn't an Atari program file");
467: }
468: list = symbols_load_binary(fp);
469: fclose(fp);
470:
471: if (!list) {
472: return usage("no symbols, or reading them failed");
473: }
474:
475: if (list->count < list->symbols) {
476: if (!list->count) {
477: return usage("no valid symbols in program, symbol table loading failed");
478: }
479: /* parsed less than there were "content" lines */
480: list->names = realloc(list->names, list->count * sizeof(symbol_t));
481: assert(list->names);
482: }
483:
484: /* copy name list to address list */
485: list->addresses = malloc(list->count * sizeof(symbol_t));
486: assert(list->addresses);
487: memcpy(list->addresses, list->names, list->count * sizeof(symbol_t));
488:
489: /* sort both lists, with different criteria */
490: qsort(list->addresses, list->count, sizeof(symbol_t), symbols_by_address);
491: qsort(list->names, list->count, sizeof(symbol_t), symbols_by_name);
492: return list;
493: }
494:
495:
496: /* ---------------- symbol showing & option parsing ------------------ */
497:
498: /**
499: * Show symbols sorted by selected option
500: */
501: static int symbols_show(symbol_list_t* list)
502: {
503: symbol_t *entry, *entries;
504: char symchar;
505: int i;
506:
507: if (!list) {
508: fprintf(stderr, "No symbols!\n");
509: return 1;
510: }
511: if (Options.sort_name) {
512: entries = list->names;
513: } else {
514: entries = list->addresses;
515: }
516: for (entry = entries, i = 0; i < list->count; i++, entry++) {
517: switch (entry->type) {
518: case SYMTYPE_TEXT:
519: symchar = 'T';
520: break;
521: case SYMTYPE_DATA:
522: symchar = 'D';
523: break;
524: case SYMTYPE_BSS:
525: symchar = 'B';
526: break;
527: default:
528: symchar = '?';
529: }
530: fprintf(stdout, "0x%08x %c %s\n",
531: entry->address, symchar, entry->name);
532: }
533:
534: fprintf(stderr, "%d symbols processed.\n", list->count);
535: return 0;
536: }
537:
538: /**
539: * parse program options and then call symbol load+save
540: */
541: int main(int argc, const char *argv[])
542: {
543: int i;
544:
545: PrgPath = *argv;
546: for (i = 1; i+1 < argc; i++) {
547: if (argv[i][0] != '-') {
548: break;
549: }
1.1.1.2 ! root 550: switch(tolower((unsigned char)argv[i][1])) {
1.1 root 551: case 'n':
552: Options.sort_name = true;
553: case 'b':
554: Options.notypes |= SYMTYPE_BSS;
555: break;
556: case 'd':
557: Options.notypes |= SYMTYPE_DATA;
558: break;
559: case 't':
560: Options.notypes |= SYMTYPE_TEXT;
561: break;
562: case 'l':
563: Options.no_local = true;
564: break;
565: case 'o':
566: Options.no_obj = true;
567: break;
568: default:
569: usage("unknown option");
570: return 1;
571: }
572: }
573: if (i+1 != argc) {
574: usage("incorrect number of arguments");
575: return 1;
576: }
577: return symbols_show(symbols_load(argv[i]));
578: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.