From a9899087d3ab38091789963bf0c4a6f90448f31b Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Thu, 11 Jul 2002 21:04:09 +0000 Subject: [PATCH] fix a bunch of silly bugs and get the linked output written (hangs, though) --- tools/qfcc/include/linker.h | 2 +- tools/qfcc/source/def.c | 3 +- tools/qfcc/source/linker.c | 56 +++++++++++++++++++----------------- tools/qfcc/source/obj_file.c | 18 ++++++++++++ tools/qfcc/source/qfcc.c | 8 +++++- 5 files changed, 57 insertions(+), 30 deletions(-) diff --git a/tools/qfcc/include/linker.h b/tools/qfcc/include/linker.h index bc3585344..3865ae7a6 100644 --- a/tools/qfcc/include/linker.h +++ b/tools/qfcc/include/linker.h @@ -34,6 +34,6 @@ void linker_begin (void); void linker_add_object_file (const char *filename); -void linker_finish (void); +struct qfo_s *linker_finish (void); #endif//__linker_h diff --git a/tools/qfcc/source/def.c b/tools/qfcc/source/def.c index c52457cfc..273643362 100644 --- a/tools/qfcc/source/def.c +++ b/tools/qfcc/source/def.c @@ -126,7 +126,8 @@ defspace_adddata (defspace_t *space, pr_type_t *data, int size) space->max_size * sizeof (pr_type_t)); } if (data) - memcpy (space->data, data, space->max_size * sizeof (pr_type_t)); + memcpy (space->data + space->size, data, size * sizeof (pr_type_t)); + space->size += size; } scope_t * diff --git a/tools/qfcc/source/linker.c b/tools/qfcc/source/linker.c index 135519c16..eab2ef65a 100644 --- a/tools/qfcc/source/linker.c +++ b/tools/qfcc/source/linker.c @@ -47,6 +47,7 @@ static const char rcsid[] = #include "emit.h" #include "expr.h" #include "immediate.h" +#include "linker.h" #include "obj_file.h" #include "qfcc.h" #include "reloc.h" @@ -93,7 +94,7 @@ defs_get_key (void *_def, void *unused) { qfo_def_t *def = (qfo_def_t *) _def; - return G_GETSTR (def->name); + return strings->strings + def->name; } static void @@ -101,7 +102,7 @@ add_strings (qfo_t *qfo) { int i; - for (i = 0; i < qfo->strings_size; i += strlen (qfo->strings + i)) + for (i = 0; i < qfo->strings_size; i += strlen (qfo->strings + i) + 1) strpool_addstr (strings, qfo->strings + i); } @@ -171,29 +172,25 @@ add_defs (qfo_t *qfo) qfo_def_t *d; if (def->flags & QFOD_GLOBAL) { - if ((d = Hash_Find (defined_defs, G_GETSTR (def->name)))) { + if ((d = Hash_Find (defined_defs, + strings->strings + def->name))) { pr.source_file = def->file; pr.source_line = def->line; - error (0, "%s redefined", G_GETSTR (def->name)); + error (0, "%s redefined", strings->strings + def->name); } } - if (def->basic_type == ev_string && def->ofs - && QFO_var (qfo, string, def->ofs)) { - string_t s; - s = strpool_addstr (strings, QFO_STRING (qfo, def->ofs)); - QFO_var (qfo, string, def->ofs) = s; - } if (def->ofs) def->ofs += data_base; if (def->flags & QFOD_GLOBAL) { - while ((d = Hash_Find (extern_defs, G_GETSTR (def->name)))) { - Hash_Del (extern_defs, G_GETSTR (d->name)); + while ((d = Hash_Find (extern_defs, + strings->strings + def->name))) { + Hash_Del (extern_defs, strings->strings + d->name); if (d->full_type != def->full_type) { pr.source_file = def->file; pr.source_line = def->line; - error (0, "type mismatch %s %s", - G_GETSTR (def->full_type), - G_GETSTR (d->full_type)); + error (0, "type mismatch `%s' `%s'", + type_strings->strings + def->full_type, + type_strings->strings + d->full_type); } } Hash_Add (defined_defs, def); @@ -339,6 +336,21 @@ linker_add_object_file (const char *filename) fixup_relocs (qfo); qfo_delete (qfo); +} + +qfo_t * +linker_finish (void) +{ + qfo_def_t **undef_defs, **def; + qfo_t *qfo; + + undef_defs = (qfo_def_t **) Hash_GetList (extern_defs); + for (def = undef_defs; *def; def++) { + pr.source_file = (*def)->file; + pr.source_line = (*def)->line; + error (0, "undefined symbol %s", strings->strings + (*def)->name); + return 0; + } qfo = qfo_new (); qfo_add_code (qfo, code->code, code->size); @@ -348,17 +360,7 @@ linker_add_object_file (const char *filename) qfo_add_relocs (qfo, relocs.relocs, relocs.num_relocs); qfo_add_defs (qfo, defs.defs, defs.num_defs); qfo_add_functions (qfo, funcs.funcs, funcs.num_funcs); + qfo_add_lines (qfo, lines.lines, lines.num_lines); qfo_add_types (qfo, type_strings->strings, type_strings->size); -} - -void -linker_finish (void) -{ - qfo_def_t **undef_defs, **def; - undef_defs = (qfo_def_t **) Hash_GetList (extern_defs); - for (def = undef_defs; *def; def++) { - pr.source_file = (*def)->file; - pr.source_line = (*def)->line; - error (0, "undefined symbol %s", G_GETSTR ((*def)->name)); - } + return qfo; } diff --git a/tools/qfcc/source/obj_file.c b/tools/qfcc/source/obj_file.c index e521fe2d8..88204cb02 100644 --- a/tools/qfcc/source/obj_file.c +++ b/tools/qfcc/source/obj_file.c @@ -525,6 +525,8 @@ qfo_new (void) void qfo_add_code (qfo_t *qfo, dstatement_t *code, int code_size) { + if (!code_size) + return; qfo->code = malloc (code_size * sizeof (dstatement_t)); qfo->code_size = code_size; memcpy (qfo->code, code, code_size * sizeof (dstatement_t)); @@ -533,6 +535,8 @@ qfo_add_code (qfo_t *qfo, dstatement_t *code, int code_size) void qfo_add_data (qfo_t *qfo, pr_type_t *data, int data_size) { + if (!data_size) + return; qfo->data = malloc (data_size * sizeof (pr_type_t)); qfo->data_size = data_size; memcpy (qfo->data, data, data_size * sizeof (pr_type_t)); @@ -541,6 +545,8 @@ qfo_add_data (qfo_t *qfo, pr_type_t *data, int data_size) void qfo_add_far_data (qfo_t *qfo, pr_type_t *far_data, int far_data_size) { + if (!far_data_size) + return; qfo->far_data = malloc (far_data_size * sizeof (pr_type_t)); qfo->far_data_size = far_data_size; memcpy (qfo->far_data, far_data, far_data_size * sizeof (pr_type_t)); @@ -549,6 +555,8 @@ qfo_add_far_data (qfo_t *qfo, pr_type_t *far_data, int far_data_size) void qfo_add_strings (qfo_t *qfo, const char *strings, int strings_size) { + if (!strings_size) + return; qfo->strings = malloc (strings_size); qfo->strings_size = strings_size; memcpy (qfo->strings, strings, strings_size); @@ -557,6 +565,8 @@ qfo_add_strings (qfo_t *qfo, const char *strings, int strings_size) void qfo_add_relocs (qfo_t *qfo, qfo_reloc_t *relocs, int num_relocs) { + if (!num_relocs) + return; qfo->relocs = malloc (num_relocs * sizeof (qfo_reloc_t)); qfo->num_relocs = num_relocs; memcpy (qfo->relocs, relocs, num_relocs * sizeof (qfo_reloc_t)); @@ -565,6 +575,8 @@ qfo_add_relocs (qfo_t *qfo, qfo_reloc_t *relocs, int num_relocs) void qfo_add_defs (qfo_t *qfo, qfo_def_t *defs, int num_defs) { + if (!num_defs) + return; qfo->defs = malloc (num_defs * sizeof (qfo_def_t)); qfo->num_defs = num_defs; memcpy (qfo->defs, defs, num_defs * sizeof (qfo_def_t)); @@ -573,6 +585,8 @@ qfo_add_defs (qfo_t *qfo, qfo_def_t *defs, int num_defs) void qfo_add_functions (qfo_t *qfo, qfo_function_t *functions, int num_functions) { + if (!num_functions) + return; qfo->functions = malloc (num_functions * sizeof (qfo_function_t)); qfo->num_functions = num_functions; memcpy (qfo->functions, functions, num_functions * sizeof (qfo_function_t)); @@ -581,6 +595,8 @@ qfo_add_functions (qfo_t *qfo, qfo_function_t *functions, int num_functions) void qfo_add_lines (qfo_t *qfo, pr_lineno_t *lines, int num_lines) { + if (!num_lines) + return; qfo->lines = malloc (num_lines * sizeof (pr_lineno_t)); qfo->num_lines = num_lines; memcpy (qfo->lines, lines, num_lines * sizeof (pr_lineno_t)); @@ -589,6 +605,8 @@ qfo_add_lines (qfo_t *qfo, pr_lineno_t *lines, int num_lines) void qfo_add_types (qfo_t *qfo, const char *types, int types_size) { + if (!types_size) + return; qfo->types = malloc (types_size); qfo->types_size = types_size; memcpy (qfo->types, types, types_size); diff --git a/tools/qfcc/source/qfcc.c b/tools/qfcc/source/qfcc.c index d96f11489..e7b3fd0b7 100644 --- a/tools/qfcc/source/qfcc.c +++ b/tools/qfcc/source/qfcc.c @@ -540,11 +540,17 @@ separate_compile (void) } } if (!err && !options.compile) { + qfo_t *qfo; linker_begin (); for (file = source_files; *file; file++) { linker_add_object_file (*file); } - linker_finish (); + qfo = linker_finish (); + if (qfo) { + qfo_to_progs (qfo, &pr); + finish_compilation (); + WriteData (0); + } } return err; }