From 59b04dbcca93fd7db7ac44790ee281ec9adfc548 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sat, 8 Sep 2018 19:45:19 +0900 Subject: [PATCH] Make many qfo int fields unsigned. I don't remember why I thought signed was a good idea, but unsigned does make more sense for most fields. --- tools/qfcc/include/obj_file.h | 46 +++++++++++++++--------------- tools/qfcc/source/dump_globals.c | 10 +++---- tools/qfcc/source/linker.c | 49 +++++++++++++++++++------------- tools/qfcc/source/obj_file.c | 20 ++++++------- 4 files changed, 67 insertions(+), 58 deletions(-) diff --git a/tools/qfcc/include/obj_file.h b/tools/qfcc/include/obj_file.h index cd9cd45f6..1fe1d3690 100644 --- a/tools/qfcc/include/obj_file.h +++ b/tools/qfcc/include/obj_file.h @@ -83,11 +83,11 @@ typedef enum qfos_type_e { */ typedef struct qfo_space_s { pr_int_t type; ///< code, string, data, entity... - pr_int_t defs; ///< index of first def - pr_int_t num_defs; ///< zero for code or string spaces - pr_int_t data; ///< byte offset in qfo + pr_uint_t defs; ///< index of first def + pr_uint_t num_defs; ///< zero for code or string spaces + pr_uint_t data; ///< byte offset in qfo pr_uint_t data_size; ///< in elements. zero for entity spaces - pr_int_t id; + pr_uint_t id; pr_int_t reserved[2]; } qfo_space_t; @@ -98,13 +98,13 @@ typedef struct qfo_def_s { string_t name; ///< def name pointer_t offset; ///< def offset (address) - pr_int_t relocs; ///< index of first reloc record - pr_int_t num_relocs; ///< number of reloc records + pr_uint_t relocs; ///< index of first reloc record + pr_uint_t num_relocs; ///< number of reloc records pr_uint_t flags; ///< \ref qfcc_qfo_QFOD "QFOD flags" string_t file; ///< source file name - pr_int_t line; ///< source line number + pr_uint_t line; ///< source line number } qfo_def_t; //@} @@ -180,7 +180,7 @@ typedef struct qfo_func_s { string_t name; ///< function name pointer_t type; ///< function type (in type data space) string_t file; ///< source file name - pr_int_t line; ///< source line number + pr_uint_t line; ///< source line number /** \name Function code location. If #code is negative, then the function is a VM builtin function. @@ -191,22 +191,22 @@ typedef struct qfo_func_s { */ pr_int_t code; - pr_int_t def; ///< def that references this function. Index + pr_uint_t def; ///< def that references this function. Index ///< to ::qfo_def_t. The data word pointed to ///< by the def stores the index of this ///< function. - pr_int_t locals_space; ///< space holding the function's local data + pr_uint_t locals_space; ///< space holding the function's local data - pr_int_t line_info; ///< Index to first ::pr_lineno_t line record. + pr_uint_t line_info; ///< Index to first ::pr_lineno_t line record. ///< Zero if there are no records. /** \name Function relocation records. XXX not sure how these work */ //@{ - pr_int_t relocs; ///< Index to first ::qfo_reloc_t reloc record. - pr_int_t num_relocs; ///< Number of reloc records. + pr_uint_t relocs; ///< Index to first ::qfo_reloc_t reloc record. + pr_uint_t num_relocs; ///< Number of reloc records. //@} pr_int_t reserved[2]; } qfo_func_t; @@ -237,8 +237,8 @@ typedef struct qfo_func_s { the referenced field def. */ typedef struct qfo_reloc_s { - pr_int_t space; ///< index of space holding data to be adjusted - pr_int_t offset; ///< offset of the relocation + pr_uint_t space; ///< index of space holding data to be adjusted + pr_uint_t offset; ///< offset of the relocation pr_int_t type; ///< type of the relocation (::reloc_type) pr_uint_t target; ///< def/func/etc this relocation is for } qfo_reloc_t; @@ -248,14 +248,14 @@ typedef struct qfo_reloc_s { typedef struct qfo_mspace_s { qfos_type_t type; qfo_def_t *defs; - int num_defs; + unsigned num_defs; union { dstatement_t *code; pr_type_t *data; char *strings; } d; unsigned data_size; - int id; + unsigned id; } qfo_mspace_t; /** In-memory representation of a QFO object file. @@ -263,16 +263,16 @@ typedef struct qfo_mspace_s { typedef struct qfo_s { void *data; ///< data buffer holding qfo file when read qfo_mspace_t *spaces; - int num_spaces; + unsigned num_spaces; qfo_reloc_t *relocs; - int num_relocs; // includes num_loose_relocs + unsigned num_relocs; // includes num_loose_relocs qfo_def_t *defs; - int num_defs; + unsigned num_defs; qfo_func_t *funcs; - int num_funcs; + unsigned num_funcs; pr_lineno_t *lines; - int num_lines; - int num_loose_relocs; // included in num_relocs + unsigned num_lines; + unsigned num_loose_relocs; // included in num_relocs } qfo_t; enum { diff --git a/tools/qfcc/source/dump_globals.c b/tools/qfcc/source/dump_globals.c index dce3bd265..e3ce6f557 100644 --- a/tools/qfcc/source/dump_globals.c +++ b/tools/qfcc/source/dump_globals.c @@ -257,8 +257,8 @@ void qfo_globals (qfo_t *qfo) { qfo_def_t *def; - int i; - int space; + unsigned i; + unsigned space; int count = 0; for (space = 0; space < qfo->num_spaces; space++) { @@ -282,7 +282,7 @@ qfo_relocs (qfo_t *qfo) qfo_reloc_t *reloc; qfo_def_t *def; qfo_func_t *func; - int i; + unsigned i; for (i = 0; i < qfo->num_relocs; i++) { reloc = qfo->relocs + i; @@ -385,8 +385,8 @@ qfo_functions (qfo_t *qfo) { qfo_def_t *def; qfo_func_t *func; - int i, d; - int space; + unsigned i, d; + unsigned space; for (i = 0; i < qfo->num_funcs; i++) { func = &qfo->funcs[i]; diff --git a/tools/qfcc/source/linker.c b/tools/qfcc/source/linker.c index 772f3fd35..952997374 100644 --- a/tools/qfcc/source/linker.c +++ b/tools/qfcc/source/linker.c @@ -147,7 +147,7 @@ static qfo_t *work; static int work_base[qfo_num_spaces]; static int work_func_base; static defref_t **work_defrefs; -static int num_work_defrefs; +static unsigned num_work_defrefs; static strpool_t *work_strings; static codespace_t *work_code; static defspace_t *work_near_data; @@ -436,7 +436,7 @@ adjust_reloc_offset (qfo_reloc_t *reloc) static int add_relocs (qfo_t *qfo, int start, int count, int target) { - int size; + unsigned size; qfo_reloc_t *ireloc; qfo_reloc_t *oreloc; @@ -448,7 +448,7 @@ add_relocs (qfo_t *qfo, int start, int count, int target) *oreloc = *ireloc; ireloc->type = -1; ireloc->offset = work->num_relocs++; - if (oreloc->space < 0 || oreloc->space >= qfo->num_spaces) { + if (oreloc->space >= qfo->num_spaces) { linker_error ("bad reloc space: %d (%d)", oreloc->space, qfo->num_spaces); oreloc->type = rel_none; @@ -806,7 +806,7 @@ update_type_space_reloc (qfo_mspace_t *space, qfo_reloc_t *reloc) static int process_type_space (qfo_t *qfo, qfo_mspace_t *space, int pass) { - int i; + unsigned i; if (pass != 0) return 0; @@ -860,7 +860,7 @@ process_type_space (qfo_t *qfo, qfo_mspace_t *space, int pass) static void process_funcs (qfo_t *qfo) { - int size; + unsigned size; qfo_func_t *func; qfot_type_t *type; @@ -888,7 +888,7 @@ process_funcs (qfo_t *qfo) static void process_lines (qfo_t *qfo) { - int size; + unsigned size; pr_lineno_t *line; if (!qfo->num_lines) @@ -920,7 +920,7 @@ process_loose_relocs (qfo_t *qfo) qfo->num_loose_relocs * sizeof (qfo_reloc_t)); while (work_num_loose_relocs < size) { reloc = work_loose_relocs + work_num_loose_relocs++; - if (reloc->space < 0 || reloc->space >= qfo->num_spaces) { + if (reloc->space >= qfo->num_spaces) { linker_error ("bad reloc space"); reloc->type = rel_none; continue; @@ -954,7 +954,7 @@ linker_add_qfo (qfo_t *qfo) process_entity_space, process_type_space, }; - int i; + unsigned i; int pass; qfo_mspace_t *space; @@ -1027,7 +1027,8 @@ linker_add_lib (const char *libname) path_t start = {path_head, "."}; path_t *path = &start; const char *path_name = 0; - int i, j; + int i; + unsigned j; int did_something; if (strncmp (libname, "-l", 2) == 0) { @@ -1101,7 +1102,7 @@ static __attribute__ ((used)) void undefined_def (qfo_def_t *def) { qfo_def_t line_def; - pr_int_t i; + pr_uint_t i; qfo_reloc_t *reloc = work->relocs + def->relocs; for (i = 0; i < def->num_relocs; i++, reloc++) { @@ -1113,17 +1114,20 @@ undefined_def (qfo_def_t *def) || reloc->type == rel_op_c_def_ofs) && work->lines) { qfo_func_t *func = work->funcs; - qfo_func_t *best = func; - pr_int_t best_dist = reloc->offset - func->code; + qfo_func_t *best = 0; + pr_uint_t best_dist; pr_lineno_t *line; - while (best_dist && func - work->funcs < work->num_funcs) { - if (func->code <= reloc->offset) { - if (best_dist < 0 - || reloc->offset - func->code < best_dist) { - best = func; - best_dist = reloc->offset - func->code; - } + while (func - work->funcs < work->num_funcs) { + if (func->code < 0) { + continue; + } + if ((pr_uint_t) func->code > reloc->offset) { + continue; + } + if (!best || reloc->offset - func->code < best_dist) { + best = func; + best_dist = reloc->offset - func->code; } func++; } @@ -1195,7 +1199,7 @@ build_qfo (void) { qfo_t *qfo; int size; - int i, j; + unsigned i, j; qfo_reloc_t *reloc; qfo_def_t **defs; @@ -1288,6 +1292,11 @@ build_qfo (void) continue; if (reloc->type != rel_def_def) continue; + if (reloc->target >= qfo->num_defs) { + linker_error ("Invalid reloc target def %d / %d.\n", + reloc->target, qfo->num_defs); + continue; + } def = qfo->defs + reloc->target; QFO_INT (qfo, reloc->space, reloc->offset) = def->offset; } diff --git a/tools/qfcc/source/obj_file.c b/tools/qfcc/source/obj_file.c index b39bea1e7..b7d5a389e 100644 --- a/tools/qfcc/source/obj_file.c +++ b/tools/qfcc/source/obj_file.c @@ -394,9 +394,9 @@ qfo_byteswap_space (void *space, int size, qfos_type_t type) int qfo_write (qfo_t *qfo, const char *filename) { - int size; + unsigned size; int space_offset; - int i; + unsigned i; byte *data; qfo_header_t *header; qfo_space_t *spaces; @@ -499,7 +499,7 @@ qfo_read (QFile *file) qfo_header_t *header; qfo_space_t *spaces; qfo_t *qfo; - int i; + unsigned i; size = Qfilesize (file); data = malloc (size); @@ -605,7 +605,7 @@ qfo_delete (qfo_t *qfo) if (qfo->data) { free (qfo->data); } else { - int i; + unsigned i; for (i = 0; i < qfo->num_spaces; i++) free (qfo->spaces[i].d.data); free (qfo->relocs); @@ -715,7 +715,7 @@ convert_def (qfo_t *qfo, const qfo_def_t *def, ddef_t *ddef) static void qfo_relocate_refs (qfo_t *qfo) { - int i; + unsigned i; qfo_reloc_t *reloc; for (i = 0, reloc = qfo->relocs; i < qfo->num_relocs; i++, reloc++) { @@ -788,10 +788,10 @@ qfo_to_progs (qfo_t *qfo, int *size) pr_type_t *type_data; dprograms_t *progs; qfo_def_t *types_def = 0; - int i, j; + unsigned i, j; unsigned locals_size = 0; int locals_start; - int big_locals = 0; + unsigned big_locals = 0; int big_func = 0; *size = RUP (sizeof (dprograms_t), 16); @@ -976,7 +976,7 @@ pr_debug_header_t * qfo_to_sym (qfo_t *qfo, int *size) { pr_debug_header_t *sym; - int i, j; + unsigned i, j; pr_auxfunction_t *auxfuncs; pr_auxfunction_t *aux; pr_lineno_t *linenos; @@ -988,7 +988,7 @@ qfo_to_sym (qfo_t *qfo, int *size) sym->version = PROG_DEBUG_VERSION; for (i = 0; i < qfo->num_funcs; i++) { qfo_func_t *func = qfo->funcs + i; - int num_locals = 0; + unsigned num_locals = 0; if (func->locals_space) num_locals = qfo->spaces[func->locals_space].num_defs; @@ -1017,7 +1017,7 @@ qfo_to_sym (qfo_t *qfo, int *size) for (i = 0, aux = auxfuncs; i < qfo->num_funcs; i++) { qfo_func_t *func = qfo->funcs + i; qfo_def_t *def = 0; - int num_locals = 0; + unsigned num_locals = 0; qfot_type_t *type; if (func->locals_space) {