--- hatari/tools/debugger/gst2ascii.c 2019/04/09 08:54:52 1.1.1.2 +++ hatari/tools/debugger/gst2ascii.c 2019/04/09 08:57:17 1.1.1.4 @@ -1,7 +1,7 @@ /* * Hatari - gst2ascii.c * - * Copyright (C) 2013-2014 by Eero Tamminen + * Copyright (C) 2013-2015 by Eero Tamminen * * This file is distributed under the GNU General Public License, version 2 * or at your option any later version. Read the file gpl.txt for details. @@ -58,7 +58,7 @@ typedef struct { #define PATHSEP '/' #endif -#define ARRAYSIZE(x) (int)(sizeof(x)/sizeof(x[0])) +#define ARRAY_SIZE(x) (int)(sizeof(x)/sizeof(x[0])) static const char *PrgPath; @@ -103,7 +103,7 @@ static symbol_list_t* usage(const char * "its profiler data post-processor.\n" "\n" "Options:\n", name); - for (i = 0; i < ARRAYSIZE(OptInfo); i++) { + for (i = 0; i < ARRAY_SIZE(OptInfo); i++) { fprintf(stderr, "\t-%c\t%s\n", OptInfo[i].opt, OptInfo[i].desc); } if (msg) { @@ -294,7 +294,7 @@ static symbol_list_t* symbols_load_dri(F continue; } /* useless symbols GCC (v2) seems to add to every object? */ - for (j = 0; j < ARRAYSIZE(gcc_sym); j++) { + for (j = 0; j < ARRAY_SIZE(gcc_sym); j++) { if (strcmp(name, gcc_sym[j]) == 0) { ofiles++; j = -1; @@ -352,6 +352,54 @@ static symbol_list_t* symbols_load_dri(F } /** + * Print program header information. + * Return false for unrecognized symbol table type. + */ +static bool symbols_print_prg_info(Uint32 tabletype, Uint32 prgflags, Uint16 relocflag) +{ + static const struct { + Uint32 flag; + const char *name; + } flags[] = { + { 0x0001, "FASTLOAD" }, + { 0x0002, "TTRAMLOAD" }, + { 0x0004, "TTRAMMEM" }, + { 0x0008, "MINIMUM" }, /* MagiC */ + { 0x1000, "SHAREDTEXT" } + }; + const char *info; + int i; + + switch (tabletype) { + case 0x4D694E54: /* "MiNT" */ + info = "GCC/MiNT executable, GST symbol table"; + break; + case 0x0: + info = "TOS executable, DRI / GST symbol table"; + break; + default: + fprintf(stderr, "ERROR: unknown executable type 0x%x!\n", tabletype); + return false; + } + fprintf(stderr, "%s, reloc=%d, program flags:", info, relocflag); + /* bit flags */ + for (i = 0; i < ARRAY_SIZE(flags); i++) { + if (prgflags & flags[i].flag) { + fprintf(stderr, " %s", flags[i].name); + } + } + /* memory protection flags */ + switch((prgflags >> 4) & 3) { + case 0: info = "PRIVATE"; break; + case 1: info = "GLOBAL"; break; + case 2: info = "SUPER"; break; + case 3: info = "READONLY"; break; + } + fprintf(stderr, " %s (0x%x)\n", info, prgflags); + return true; +} + +/** * Parse program header and use symbol table format specific loader * loader function to load the symbols. * Return symbols list or NULL for failure. @@ -362,7 +410,6 @@ static symbol_list_t* symbols_load_binar prg_section_t sections[3]; int offset, reads = 0; Uint16 relocflag; - const char *info; symbol_list_t* symbols; /* get TEXT, DATA & BSS section sizes */ @@ -376,10 +423,6 @@ static symbol_list_t* symbols_load_binar /* get symbol table size & type and check that all reads succeeded */ reads += fread(&tablesize, sizeof(tablesize), 1, fp); tablesize = SDL_SwapBE32(tablesize); - if (!tablesize) { - fprintf(stderr, "ERROR: symbol table missing from the program!\n"); - return NULL; - } reads += fread(&tabletype, sizeof(tabletype), 1, fp); tabletype = SDL_SwapBE32(tabletype); @@ -393,6 +436,13 @@ static symbol_list_t* symbols_load_binar fprintf(stderr, "ERROR: program header reading failed!\n"); return NULL; } + if (!symbols_print_prg_info(tabletype, prgflags, relocflag)) { + return NULL; + } + if (!tablesize) { + fprintf(stderr, "ERROR: symbol table missing from the program!\n"); + return NULL; + } /* symbols already have suitable offsets, so only acceptable end position needs to be calculated */ sections[0].offset = 0; @@ -408,20 +458,6 @@ static symbol_list_t* symbols_load_binar perror("ERROR: seeking to symbol table failed"); return NULL; } - - switch (tabletype) { - case 0x4D694E54: /* "MiNT" */ - info = "GCC/MiNT executable, GST symbol table."; - break; - case 0x0: - info = "TOS executable, DRI / GST symbol table."; - break; - default: - fprintf(stderr, "ERROR: unknown executable type 0x%x at offset 0x%x!\n", tabletype, offset); - return NULL; - } - fprintf(stderr, "0x%x program flags, reloc=%d, %s\n", prgflags, relocflag, info); - fprintf(stderr, "Trying to load symbol table at offset 0x%x...\n", offset); symbols = symbols_load_dri(fp, sections, tablesize); @@ -455,7 +491,7 @@ static symbol_list_t* symbols_load(const FILE *fp; fprintf(stderr, "Reading symbols from program '%s' symbol table...\n", filename); - if (!(fp = fopen(filename, "r"))) { + if (!(fp = fopen(filename, "rb"))) { return usage("opening program file failed"); } if (fread(&magic, sizeof(magic), 1, fp) != 1) {