2002-06-21 20:46:56 +00:00
|
|
|
/*
|
|
|
|
obj_file.c
|
|
|
|
|
|
|
|
qfcc object file support
|
|
|
|
|
2002-07-05 20:02:10 +00:00
|
|
|
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
|
2002-06-21 20:46:56 +00:00
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/6/21
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2011-01-09 10:41:24 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] = "$Id$";
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2002-06-21 20:46:56 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/dstring.h"
|
|
|
|
#include "QF/qendian.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakeio.h"
|
2002-06-21 20:46:56 +00:00
|
|
|
|
2011-01-09 10:41:24 +00:00
|
|
|
#include "codespace.h"
|
2002-06-24 22:36:04 +00:00
|
|
|
#include "debug.h"
|
2002-06-21 20:46:56 +00:00
|
|
|
#include "def.h"
|
2011-01-17 13:33:33 +00:00
|
|
|
#include "defspace.h"
|
2002-07-08 20:31:59 +00:00
|
|
|
#include "emit.h"
|
2003-08-22 19:48:14 +00:00
|
|
|
#include "expr.h"
|
2002-06-21 20:46:56 +00:00
|
|
|
#include "function.h"
|
|
|
|
#include "immediate.h"
|
|
|
|
#include "obj_file.h"
|
2011-03-02 10:06:43 +00:00
|
|
|
#include "obj_type.h"
|
2003-02-23 23:43:43 +00:00
|
|
|
#include "options.h"
|
2002-06-21 20:46:56 +00:00
|
|
|
#include "qfcc.h"
|
2002-07-05 20:02:10 +00:00
|
|
|
#include "reloc.h"
|
2011-02-27 02:37:01 +00:00
|
|
|
#include "statements.h"
|
2002-07-05 20:02:10 +00:00
|
|
|
#include "strpool.h"
|
2011-02-23 05:50:10 +00:00
|
|
|
#include "symtab.h"
|
2002-06-21 20:46:56 +00:00
|
|
|
#include "type.h"
|
|
|
|
|
2011-02-23 05:50:10 +00:00
|
|
|
static int
|
|
|
|
count_relocs (reloc_t *r)
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
|
|
|
|
for (count = 0; r; r = r->next)
|
|
|
|
count++;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
|
|
|
qfo_def_flags (def_t *def)
|
|
|
|
{
|
|
|
|
unsigned flags = 0;
|
|
|
|
if (def->initialized)
|
|
|
|
flags |= QFOD_INITIALIZED;
|
|
|
|
if (def->constant)
|
|
|
|
flags |= QFOD_CONSTANT;
|
|
|
|
if (def->global)
|
|
|
|
flags |= QFOD_GLOBAL;
|
|
|
|
if (def->external)
|
|
|
|
flags |= QFOD_EXTERNAL;
|
|
|
|
if (def->local)
|
|
|
|
flags |= QFOD_LOCAL;
|
|
|
|
if (def->system)
|
|
|
|
flags |= QFOD_SYSTEM;
|
|
|
|
if (def->nosave)
|
|
|
|
flags |= QFOD_NOSAVE;
|
|
|
|
return flags;
|
|
|
|
}
|
2002-06-21 20:46:56 +00:00
|
|
|
|
2011-02-27 02:37:01 +00:00
|
|
|
static void
|
|
|
|
qfo_encode_one_reloc (reloc_t *reloc, qfo_reloc_t **qfo_reloc, pr_int_t target)
|
|
|
|
{
|
|
|
|
qfo_reloc_t *q;
|
|
|
|
|
|
|
|
q = (*qfo_reloc)++;
|
|
|
|
if (reloc->space) // op_* relocs do not have a space (code is implied)
|
|
|
|
q->space = reloc->space->qfo_space;
|
|
|
|
q->offset = reloc->offset;
|
|
|
|
q->type = reloc->type;
|
|
|
|
q->target = target;
|
|
|
|
}
|
|
|
|
|
2011-02-22 06:41:35 +00:00
|
|
|
static int
|
2011-02-27 02:37:01 +00:00
|
|
|
qfo_encode_relocs (reloc_t *relocs, qfo_reloc_t **qfo_relocs, pr_int_t target)
|
2002-06-21 20:46:56 +00:00
|
|
|
{
|
2011-02-22 06:41:35 +00:00
|
|
|
int count;
|
2011-02-23 05:50:10 +00:00
|
|
|
reloc_t *r;
|
2011-02-22 06:41:35 +00:00
|
|
|
|
2011-02-23 05:50:10 +00:00
|
|
|
for (count = 0, r = relocs; r; r = r->next) {
|
2011-02-22 06:41:35 +00:00
|
|
|
count++;
|
2011-02-27 02:37:01 +00:00
|
|
|
qfo_encode_one_reloc (r, qfo_relocs, target);
|
2011-02-23 05:50:10 +00:00
|
|
|
}
|
2011-02-22 06:41:35 +00:00
|
|
|
return count;
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-02-23 05:50:10 +00:00
|
|
|
qfo_encode_defs (qfo_t *qfo, def_t *defs, qfo_def_t **qfo_defs,
|
|
|
|
qfo_reloc_t **qfo_relocs)
|
2002-06-21 20:46:56 +00:00
|
|
|
{
|
2011-02-22 06:41:35 +00:00
|
|
|
int count;
|
2011-02-23 05:50:10 +00:00
|
|
|
def_t *d;
|
|
|
|
qfo_def_t *q;
|
2011-02-22 06:41:35 +00:00
|
|
|
|
2011-02-23 05:50:10 +00:00
|
|
|
for (count = 0, d = defs; d; d = d->next) {
|
2011-02-22 06:41:35 +00:00
|
|
|
count++;
|
2011-02-23 05:50:10 +00:00
|
|
|
q = (*qfo_defs)++;
|
|
|
|
d->qfo_def = q - qfo->defs;
|
|
|
|
// defs in the type data space do not have types
|
|
|
|
if (d->type)
|
|
|
|
q->type = d->type->type_def->offset;
|
|
|
|
q->name = ReuseString (d->name);
|
|
|
|
q->offset = d->offset;
|
|
|
|
q->relocs = *qfo_relocs - qfo->relocs;
|
2011-02-27 02:37:01 +00:00
|
|
|
q->num_relocs = qfo_encode_relocs (d->relocs, qfo_relocs,
|
|
|
|
q - qfo->defs);
|
2011-02-23 05:50:10 +00:00
|
|
|
q->flags = qfo_def_flags (d);
|
|
|
|
q->file = d->file;
|
|
|
|
q->line = d->line;
|
|
|
|
}
|
2011-02-22 06:41:35 +00:00
|
|
|
return count;
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
|
|
|
|
2002-07-23 05:44:45 +00:00
|
|
|
static void
|
2011-02-23 05:50:10 +00:00
|
|
|
qfo_count_space_stuff (qfo_t *qfo, defspace_t *space)
|
|
|
|
{
|
|
|
|
def_t *d;
|
|
|
|
|
|
|
|
for (d = space->defs; d; d = d->next) {
|
|
|
|
qfo->num_defs++;
|
|
|
|
qfo->num_relocs += count_relocs (d->relocs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
qfo_count_function_stuff (qfo_t *qfo, function_t *functions)
|
|
|
|
{
|
|
|
|
function_t *func;
|
|
|
|
|
|
|
|
for (func = functions; func; func = func->next) {
|
|
|
|
qfo->num_funcs++;
|
|
|
|
qfo->num_relocs += count_relocs (func->refs);
|
|
|
|
if (func->symtab && func->symtab->space) {
|
|
|
|
qfo->num_spaces++;
|
|
|
|
qfo_count_space_stuff (qfo, func->symtab->space);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
qfo_count_stuff (qfo_t *qfo, pr_info_t *pr)
|
|
|
|
{
|
|
|
|
qfo_count_space_stuff (qfo, pr->near_data);
|
|
|
|
qfo_count_space_stuff (qfo, pr->far_data);
|
|
|
|
qfo_count_space_stuff (qfo, pr->entity_data);
|
|
|
|
qfo_count_space_stuff (qfo, pr->type_data);
|
|
|
|
qfo_count_function_stuff (qfo, pr->func_head);
|
|
|
|
qfo->num_relocs += count_relocs (pr->relocs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
qfo_init_string_space (qfo_t *qfo, qfo_mspace_t *space, strpool_t *strings)
|
2002-07-23 05:44:45 +00:00
|
|
|
{
|
2011-02-23 05:50:10 +00:00
|
|
|
strings->qfo_space = space - qfo->spaces;
|
2011-02-22 06:41:35 +00:00
|
|
|
space->type = qfos_string;
|
|
|
|
space->num_defs = 0;
|
|
|
|
space->defs = 0;
|
|
|
|
space->d.strings = strings->strings;
|
|
|
|
space->data_size = strings->size;
|
2011-02-28 14:16:19 +00:00
|
|
|
space->id = qfo_strings_space;
|
2002-07-23 05:44:45 +00:00
|
|
|
}
|
|
|
|
|
2002-06-21 20:46:56 +00:00
|
|
|
static void
|
2011-02-23 05:50:10 +00:00
|
|
|
qfo_init_code_space (qfo_t *qfo, qfo_mspace_t *space, codespace_t *code)
|
2002-06-21 20:46:56 +00:00
|
|
|
{
|
2011-02-23 05:50:10 +00:00
|
|
|
code->qfo_space = space - qfo->spaces;
|
2011-02-22 06:41:35 +00:00
|
|
|
space->type = qfos_code;
|
|
|
|
space->num_defs = 0;
|
|
|
|
space->defs = 0;
|
|
|
|
space->d.code = code->code;
|
|
|
|
space->data_size = code->size;
|
2011-02-28 14:16:19 +00:00
|
|
|
space->id = qfo_code_space;
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-02-23 05:50:10 +00:00
|
|
|
qfo_init_data_space (qfo_t *qfo, qfo_def_t **defs, qfo_reloc_t **relocs,
|
|
|
|
qfo_mspace_t *space, defspace_t *data)
|
2002-06-21 20:46:56 +00:00
|
|
|
{
|
2011-02-23 05:50:10 +00:00
|
|
|
data->qfo_space = space - qfo->spaces;
|
2011-02-22 06:41:35 +00:00
|
|
|
space->type = qfos_data;
|
2011-02-23 05:50:10 +00:00
|
|
|
space->defs = *defs;
|
|
|
|
space->num_defs = qfo_encode_defs (qfo, data->defs, defs, relocs);
|
2011-02-22 06:41:35 +00:00
|
|
|
space->d.data = data->data;
|
|
|
|
space->data_size = data->size;
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-02-23 05:50:10 +00:00
|
|
|
qfo_init_entity_space (qfo_t *qfo, qfo_def_t **defs, qfo_reloc_t **relocs,
|
|
|
|
qfo_mspace_t *space, defspace_t *data)
|
2002-06-21 20:46:56 +00:00
|
|
|
{
|
2011-02-23 05:50:10 +00:00
|
|
|
data->qfo_space = space - qfo->spaces;
|
2011-02-22 06:41:35 +00:00
|
|
|
space->type = qfos_entity;
|
2011-02-23 05:50:10 +00:00
|
|
|
space->defs = *defs;
|
|
|
|
space->num_defs = qfo_encode_defs (qfo, data->defs, defs, relocs);
|
2011-02-22 06:41:35 +00:00
|
|
|
space->d.data = 0;
|
|
|
|
space->data_size = data->size;
|
2011-02-28 14:16:19 +00:00
|
|
|
space->id = qfo_entity_space;
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 06:40:34 +00:00
|
|
|
static void
|
2011-02-23 05:50:10 +00:00
|
|
|
qfo_init_type_space (qfo_t *qfo, qfo_def_t **defs, qfo_reloc_t **relocs,
|
|
|
|
qfo_mspace_t *space, defspace_t *data)
|
2002-07-16 06:40:34 +00:00
|
|
|
{
|
2011-02-23 05:50:10 +00:00
|
|
|
data->qfo_space = space - qfo->spaces;
|
2011-02-22 06:41:35 +00:00
|
|
|
space->type = qfos_type;
|
2011-02-23 05:50:10 +00:00
|
|
|
space->defs = *defs;
|
|
|
|
space->num_defs = qfo_encode_defs (qfo, data->defs, defs, relocs);
|
2011-02-22 06:41:35 +00:00
|
|
|
space->d.data = data->data;
|
|
|
|
space->data_size = data->size;
|
2011-02-28 14:16:19 +00:00
|
|
|
space->id = qfo_type_space;
|
2002-07-16 06:40:34 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 05:50:10 +00:00
|
|
|
static void
|
|
|
|
qfo_encode_functions (qfo_t *qfo, qfo_def_t **defs, qfo_reloc_t **relocs,
|
|
|
|
qfo_mspace_t *space, function_t *functions)
|
|
|
|
{
|
|
|
|
function_t *f;
|
|
|
|
qfo_func_t *q;
|
|
|
|
|
|
|
|
for (f = functions, q = qfo->funcs; f; f = f->next, q++) {
|
|
|
|
q->name = f->s_name;
|
|
|
|
q->type = f->def->type->type_def->offset;
|
|
|
|
q->file = f->s_file;
|
|
|
|
q->line = f->def->line;
|
|
|
|
q->code = f->code;
|
|
|
|
if (f->builtin) // FIXME redundant
|
|
|
|
q->code = -f->builtin;
|
|
|
|
q->def = f->def->qfo_def;
|
|
|
|
if (f->symtab && f->symtab->space) {
|
2011-02-28 14:16:19 +00:00
|
|
|
space->id = f->symtab->space->qfo_space;
|
2011-02-23 05:50:10 +00:00
|
|
|
qfo_init_data_space (qfo, defs, relocs, space++, f->symtab->space);
|
|
|
|
q->locals_space = f->symtab->space->qfo_space;
|
|
|
|
}
|
|
|
|
if (f->aux)
|
|
|
|
q->line_info = f->aux->line_info;
|
|
|
|
q->relocs = *relocs - qfo->relocs;
|
2011-02-27 02:37:01 +00:00
|
|
|
q->num_relocs = qfo_encode_relocs (f->refs, relocs, q - qfo->funcs);
|
2011-02-23 05:50:10 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-22 06:41:35 +00:00
|
|
|
|
2002-07-11 20:17:26 +00:00
|
|
|
qfo_t *
|
|
|
|
qfo_from_progs (pr_info_t *pr)
|
2002-06-21 20:46:56 +00:00
|
|
|
{
|
2011-02-23 05:50:10 +00:00
|
|
|
qfo_t *qfo;
|
|
|
|
qfo_def_t *def;
|
|
|
|
qfo_reloc_t *reloc;
|
2011-02-27 02:37:01 +00:00
|
|
|
reloc_t *r;
|
2011-02-23 05:50:10 +00:00
|
|
|
|
|
|
|
qfo = calloc (1, sizeof (qfo_t));
|
|
|
|
qfo->num_spaces = qfo_num_spaces; // certain spaces are always present
|
|
|
|
qfo_count_stuff (qfo, pr);
|
|
|
|
qfo->spaces = calloc (qfo->num_spaces, sizeof (qfo_mspace_t));
|
|
|
|
qfo->relocs = calloc (qfo->num_relocs, sizeof (qfo_reloc_t));
|
|
|
|
qfo->defs = calloc (qfo->num_defs, sizeof (qfo_def_t));
|
|
|
|
qfo->funcs = calloc (qfo->num_funcs, sizeof (qfo_func_t));
|
|
|
|
|
|
|
|
def = qfo->defs;
|
|
|
|
reloc = qfo->relocs;
|
|
|
|
|
|
|
|
qfo_init_code_space (qfo, &qfo->spaces[qfo_code_space], pr->code);
|
|
|
|
qfo_init_data_space (qfo, &def, &reloc, &qfo->spaces[qfo_near_data_space],
|
|
|
|
pr->near_data);
|
2011-02-28 14:16:19 +00:00
|
|
|
qfo->spaces[qfo_near_data_space].id = qfo_near_data_space;
|
2011-02-23 05:50:10 +00:00
|
|
|
qfo_init_data_space (qfo, &def, &reloc, &qfo->spaces[qfo_far_data_space],
|
|
|
|
pr->far_data);
|
2011-02-28 14:16:19 +00:00
|
|
|
qfo->spaces[qfo_far_data_space].id = qfo_far_data_space;
|
2011-02-23 05:50:10 +00:00
|
|
|
qfo_init_entity_space (qfo, &def, &reloc, &qfo->spaces[qfo_entity_space],
|
|
|
|
pr->entity_data);
|
|
|
|
qfo_init_type_space (qfo, &def, &reloc, &qfo->spaces[qfo_type_space],
|
|
|
|
pr->type_data);
|
|
|
|
|
|
|
|
qfo_encode_functions (qfo, &def, &reloc, qfo->spaces + qfo_num_spaces,
|
|
|
|
pr->func_head);
|
|
|
|
qfo->lines = pr->linenos;
|
|
|
|
qfo->num_lines = pr->num_linenos;
|
|
|
|
// strings must be done last because encoding defs and functions adds the
|
|
|
|
// names
|
|
|
|
qfo_init_string_space (qfo, &qfo->spaces[qfo_strings_space], pr->strings);
|
|
|
|
|
2011-02-24 06:21:06 +00:00
|
|
|
qfo->num_loose_relocs = qfo->num_relocs - (reloc - qfo->relocs);
|
2011-02-27 02:37:01 +00:00
|
|
|
for (r = pr->relocs; r; r = r->next) {
|
2011-03-04 12:29:10 +00:00
|
|
|
if (r->type == rel_def_op) {
|
2011-02-27 02:37:01 +00:00
|
|
|
qfo_encode_one_reloc (r, &reloc, r->label->dest->offset);
|
2011-03-04 12:29:10 +00:00
|
|
|
} else if (r->type == rel_def_string) {
|
|
|
|
def_t d;
|
|
|
|
d.space = r->space;
|
|
|
|
d.offset = r->offset;
|
|
|
|
qfo_encode_one_reloc (r, &reloc, D_STRING (&d));
|
|
|
|
} else {
|
2011-02-27 02:37:01 +00:00
|
|
|
qfo_encode_one_reloc (r, &reloc, 0);
|
2011-03-04 12:29:10 +00:00
|
|
|
}
|
2011-02-27 02:37:01 +00:00
|
|
|
}
|
2011-02-23 05:50:10 +00:00
|
|
|
|
|
|
|
return qfo;
|
|
|
|
}
|
2011-02-22 06:41:35 +00:00
|
|
|
|
2011-02-23 05:50:10 +00:00
|
|
|
static int
|
|
|
|
qfo_space_size (qfo_mspace_t *space)
|
|
|
|
{
|
|
|
|
switch (space->type) {
|
|
|
|
case qfos_null:
|
|
|
|
return 0;
|
|
|
|
case qfos_code:
|
|
|
|
return space->data_size * sizeof (*space->d.code);
|
|
|
|
case qfos_data:
|
|
|
|
// data size > 0 but d.data == null -> all data is zero
|
|
|
|
if (!space->d.data)
|
|
|
|
return 0;
|
|
|
|
return space->data_size * sizeof (*space->d.data);
|
|
|
|
case qfos_string:
|
|
|
|
return space->data_size * sizeof (*space->d.strings);
|
|
|
|
case qfos_entity:
|
|
|
|
return 0;
|
|
|
|
case qfos_type:
|
|
|
|
return space->data_size * sizeof (*space->d.data);
|
|
|
|
}
|
2011-02-22 06:41:35 +00:00
|
|
|
return 0;
|
2002-07-11 20:17:26 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 05:50:10 +00:00
|
|
|
static void
|
2011-02-23 09:10:38 +00:00
|
|
|
qfo_byteswap_space (void *space, int size, qfos_type_t type)
|
2011-02-23 05:50:10 +00:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
pr_type_t *val;
|
|
|
|
dstatement_t *s;
|
|
|
|
switch (type) {
|
|
|
|
case qfos_null:
|
|
|
|
case qfos_string:
|
|
|
|
break;
|
|
|
|
case qfos_code:
|
|
|
|
for (s = (dstatement_t *) space, c = 0; c < size; c++, s++) {
|
|
|
|
s->op = LittleShort (s->op);
|
|
|
|
s->a = LittleShort (s->a);
|
|
|
|
s->b = LittleShort (s->b);
|
|
|
|
s->c = LittleShort (s->c);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qfos_data:
|
|
|
|
case qfos_entity:
|
|
|
|
case qfos_type:
|
|
|
|
for (val = (pr_type_t *) space, c = 0; c < size; c++, val++)
|
|
|
|
val->integer_var = LittleLong (val->integer_var);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-11 20:17:26 +00:00
|
|
|
int
|
|
|
|
qfo_write (qfo_t *qfo, const char *filename)
|
|
|
|
{
|
2011-02-23 05:50:10 +00:00
|
|
|
int size;
|
|
|
|
int space_offset;
|
|
|
|
int i;
|
|
|
|
byte *data;
|
|
|
|
qfo_header_t *header;
|
|
|
|
qfo_space_t *spaces;
|
|
|
|
qfo_reloc_t *relocs;
|
|
|
|
qfo_def_t *defs;
|
|
|
|
qfo_func_t *funcs;
|
|
|
|
pr_lineno_t *lines;
|
|
|
|
byte *space_data;
|
|
|
|
QFile *file;
|
|
|
|
|
|
|
|
file = Qopen (filename, options.gzip ? "wbz9" : "wb");
|
|
|
|
if (!file)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
size = sizeof (qfo_header_t);
|
|
|
|
size += sizeof (qfo_space_t) * qfo->num_spaces;
|
|
|
|
size += sizeof (qfo_reloc_t) * qfo->num_relocs;
|
|
|
|
size += sizeof (qfo_def_t) * qfo->num_defs;
|
|
|
|
size += sizeof (qfo_func_t) * qfo->num_funcs;
|
|
|
|
size += sizeof (pr_lineno_t) * qfo->num_lines;
|
|
|
|
space_offset = size;
|
|
|
|
for (i = 0; i < qfo->num_spaces; i++)
|
|
|
|
size += RUP (qfo_space_size (qfo->spaces + i), 16);
|
|
|
|
data = calloc (1, size);
|
|
|
|
header = (qfo_header_t *) data;
|
|
|
|
memcpy (header->qfo, QFO, 4);
|
|
|
|
header->version = LittleLong (QFO_VERSION);
|
|
|
|
header->num_spaces = LittleLong (qfo->num_spaces);
|
|
|
|
header->num_relocs = LittleLong (qfo->num_relocs);
|
|
|
|
header->num_defs = LittleLong (qfo->num_defs);
|
|
|
|
header->num_funcs = LittleLong (qfo->num_funcs);
|
|
|
|
header->num_lines = LittleLong (qfo->num_lines);
|
2011-02-24 06:21:06 +00:00
|
|
|
header->num_loose_relocs = LittleLong (qfo->num_loose_relocs);
|
2011-02-23 05:50:10 +00:00
|
|
|
spaces = (qfo_space_t *) &header[1];
|
|
|
|
relocs = (qfo_reloc_t *) &spaces[qfo->num_spaces];
|
|
|
|
defs = (qfo_def_t *) &relocs[qfo->num_relocs];
|
|
|
|
funcs = (qfo_func_t *) &defs[qfo->num_defs];
|
|
|
|
lines = (pr_lineno_t *) &funcs[qfo->num_funcs];
|
|
|
|
space_data = data + space_offset;
|
|
|
|
for (i = 0; i < qfo->num_spaces; i++) {
|
|
|
|
spaces[i].type = LittleLong (qfo->spaces[i].type);
|
|
|
|
if (qfo->spaces[i].defs)
|
|
|
|
spaces[i].defs = LittleLong (qfo->spaces[i].defs - qfo->defs);
|
|
|
|
spaces[i].num_defs = LittleLong (qfo->spaces[i].num_defs);
|
|
|
|
if (qfo->spaces[i].d.data) {
|
|
|
|
int space_size = qfo_space_size (qfo->spaces + i);
|
|
|
|
spaces[i].data = LittleLong (space_data - data);
|
|
|
|
memcpy (space_data, qfo->spaces[i].d.data, space_size);
|
|
|
|
qfo_byteswap_space (space_data, qfo->spaces[i].data_size,
|
|
|
|
qfo->spaces[i].type);
|
|
|
|
space_data += RUP (space_size, 16);
|
|
|
|
}
|
|
|
|
spaces[i].data_size = LittleLong (qfo->spaces[i].data_size);
|
2011-02-28 14:16:19 +00:00
|
|
|
spaces[i].id = LittleLong (qfo->spaces[i].id);
|
2011-02-23 05:50:10 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < qfo->num_relocs; i++) {
|
|
|
|
relocs[i].space = LittleLong (qfo->relocs[i].space);
|
|
|
|
relocs[i].offset = LittleLong (qfo->relocs[i].offset);
|
|
|
|
relocs[i].type = LittleLong (qfo->relocs[i].type);
|
2011-02-27 02:37:01 +00:00
|
|
|
relocs[i].target = LittleLong (qfo->relocs[i].target);
|
2011-02-23 05:50:10 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < qfo->num_defs; i++) {
|
|
|
|
defs[i].type = LittleLong (qfo->defs[i].type);
|
|
|
|
defs[i].name = LittleLong (qfo->defs[i].name);
|
|
|
|
defs[i].offset = LittleLong (qfo->defs[i].offset);
|
|
|
|
defs[i].relocs = LittleLong (qfo->defs[i].relocs);
|
|
|
|
defs[i].num_relocs = LittleLong (qfo->defs[i].num_relocs);
|
|
|
|
defs[i].flags = LittleLong (qfo->defs[i].flags);
|
|
|
|
defs[i].file = LittleLong (qfo->defs[i].file);
|
|
|
|
defs[i].line = LittleLong (qfo->defs[i].line);
|
|
|
|
}
|
|
|
|
for (i = 0; i < qfo->num_funcs; i++) {
|
|
|
|
funcs[i].name = LittleLong (qfo->funcs[i].name);
|
|
|
|
funcs[i].type = LittleLong (qfo->funcs[i].type);
|
|
|
|
funcs[i].file = LittleLong (qfo->funcs[i].file);
|
|
|
|
funcs[i].line = LittleLong (qfo->funcs[i].line);
|
|
|
|
funcs[i].code = LittleLong (qfo->funcs[i].code);
|
|
|
|
funcs[i].def = LittleLong (qfo->funcs[i].def);
|
|
|
|
funcs[i].locals_space = LittleLong (qfo->funcs[i].locals_space);
|
|
|
|
funcs[i].line_info = LittleLong (qfo->funcs[i].line_info);
|
|
|
|
funcs[i].relocs = LittleLong (qfo->funcs[i].relocs);
|
|
|
|
funcs[i].num_relocs = LittleLong (qfo->funcs[i].num_relocs);
|
|
|
|
}
|
|
|
|
for (i = 0; i < qfo->num_lines; i++) {
|
|
|
|
lines[i].fa.addr = LittleLong (qfo->lines[i].fa.addr);
|
|
|
|
lines[i].line = LittleLong (qfo->lines[i].line);
|
|
|
|
}
|
|
|
|
|
|
|
|
Qwrite (file, data, size);
|
|
|
|
free (data);
|
|
|
|
Qclose (file);
|
2002-06-22 05:10:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
qfo_t *
|
2002-08-27 07:16:28 +00:00
|
|
|
qfo_read (QFile *file)
|
2002-06-22 05:10:34 +00:00
|
|
|
{
|
2011-02-23 09:10:38 +00:00
|
|
|
char *data;
|
|
|
|
int size;
|
|
|
|
qfo_header_t *header;
|
|
|
|
qfo_space_t *spaces;
|
|
|
|
qfo_t *qfo;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
size = Qfilesize (file);
|
|
|
|
data = malloc (size);
|
|
|
|
Qread (file, data, size);
|
|
|
|
|
|
|
|
header = (qfo_header_t *) data;
|
|
|
|
header->version = LittleLong (header->version);
|
|
|
|
if (memcmp (header->qfo, QFO, 4) || header->version != QFO_VERSION) {
|
|
|
|
fprintf (stderr, "not a valid qfo file\n");
|
|
|
|
free (data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
qfo = calloc (1, sizeof (qfo_t));
|
|
|
|
|
|
|
|
qfo->num_spaces = LittleLong (header->num_spaces);
|
|
|
|
qfo->num_relocs = LittleLong (header->num_relocs);
|
|
|
|
qfo->num_defs = LittleLong (header->num_defs);
|
|
|
|
qfo->num_funcs = LittleLong (header->num_funcs);
|
|
|
|
qfo->num_lines = LittleLong (header->num_lines);
|
2011-02-24 06:21:06 +00:00
|
|
|
qfo->num_loose_relocs = LittleLong (header->num_loose_relocs);
|
2011-02-23 09:10:38 +00:00
|
|
|
|
|
|
|
spaces = (qfo_space_t *) &header[1];
|
|
|
|
qfo->data = data;
|
|
|
|
qfo->spaces = calloc (sizeof (qfo_mspace_t), qfo->num_spaces);
|
|
|
|
qfo->relocs = (qfo_reloc_t *) &spaces[qfo->num_spaces];
|
|
|
|
qfo->defs = (qfo_def_t *) &qfo->relocs[qfo->num_relocs];
|
|
|
|
qfo->funcs = (qfo_func_t *) &qfo->defs[qfo->num_defs];
|
|
|
|
qfo->lines = (pr_lineno_t *) &qfo->funcs[qfo->num_funcs];
|
|
|
|
|
|
|
|
for (i = 0; i < qfo->num_spaces; i++) {
|
|
|
|
qfo->spaces[i].type = LittleLong (spaces[i].type);
|
|
|
|
qfo->spaces[i].num_defs = LittleLong (spaces[i].num_defs);
|
2011-02-28 14:16:59 +00:00
|
|
|
if (qfo->spaces[i].num_defs)
|
|
|
|
qfo->spaces[i].defs = qfo->defs + LittleLong (spaces[i].defs);
|
2011-02-23 09:10:38 +00:00
|
|
|
qfo->spaces[i].data_size = LittleLong (spaces[i].data_size);
|
|
|
|
if (spaces[i].data) {
|
|
|
|
qfo->spaces[i].d.strings = data + LittleLong (spaces[i].data);
|
|
|
|
qfo_byteswap_space (qfo->spaces[i].d.data,
|
|
|
|
qfo->spaces[i].data_size, qfo->spaces[i].type);
|
|
|
|
}
|
2011-02-28 14:16:19 +00:00
|
|
|
qfo->spaces[i].id = LittleLong (spaces[i].id);
|
2011-02-23 09:10:38 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < qfo->num_relocs; i++) {
|
|
|
|
qfo->relocs[i].space = LittleLong (qfo->relocs[i].space);
|
|
|
|
qfo->relocs[i].offset = LittleLong (qfo->relocs[i].offset);
|
|
|
|
qfo->relocs[i].type = LittleLong (qfo->relocs[i].type);
|
2011-02-27 02:37:01 +00:00
|
|
|
qfo->relocs[i].target = LittleLong (qfo->relocs[i].target);
|
2011-02-23 09:10:38 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < qfo->num_defs; i++) {
|
|
|
|
qfo->defs[i].type = LittleLong (qfo->defs[i].type);
|
|
|
|
qfo->defs[i].name = LittleLong (qfo->defs[i].name);
|
|
|
|
qfo->defs[i].offset = LittleLong (qfo->defs[i].offset);
|
|
|
|
qfo->defs[i].relocs = LittleLong (qfo->defs[i].relocs);
|
|
|
|
qfo->defs[i].num_relocs = LittleLong (qfo->defs[i].num_relocs);
|
|
|
|
qfo->defs[i].flags = LittleLong (qfo->defs[i].flags);
|
|
|
|
qfo->defs[i].file = LittleLong (qfo->defs[i].file);
|
|
|
|
qfo->defs[i].line = LittleLong (qfo->defs[i].line);
|
|
|
|
}
|
|
|
|
for (i = 0; i < qfo->num_funcs; i++) {
|
|
|
|
qfo->funcs[i].name = LittleLong (qfo->funcs[i].name);
|
|
|
|
qfo->funcs[i].type = LittleLong (qfo->funcs[i].type);
|
|
|
|
qfo->funcs[i].file = LittleLong (qfo->funcs[i].file);
|
|
|
|
qfo->funcs[i].line = LittleLong (qfo->funcs[i].line);
|
|
|
|
qfo->funcs[i].code = LittleLong (qfo->funcs[i].code);
|
|
|
|
qfo->funcs[i].def = LittleLong (qfo->funcs[i].def);
|
|
|
|
qfo->funcs[i].locals_space = LittleLong (qfo->funcs[i].locals_space);
|
|
|
|
qfo->funcs[i].relocs = LittleLong (qfo->funcs[i].relocs);
|
|
|
|
qfo->funcs[i].num_relocs = LittleLong (qfo->funcs[i].num_relocs);
|
|
|
|
}
|
|
|
|
for (i = 0; i < qfo->num_lines; i++) {
|
|
|
|
qfo->lines[i].fa.addr = LittleLong (qfo->lines[i].fa.addr);
|
|
|
|
qfo->lines[i].line = LittleLong (qfo->lines[i].line);
|
|
|
|
}
|
|
|
|
return qfo;
|
2002-06-21 20:46:56 +00:00
|
|
|
}
|
2002-07-05 20:02:10 +00:00
|
|
|
|
2002-11-05 19:12:51 +00:00
|
|
|
qfo_t *
|
|
|
|
qfo_open (const char *filename)
|
2002-07-21 07:12:17 +00:00
|
|
|
{
|
2011-02-23 09:10:38 +00:00
|
|
|
qfo_t *qfo;
|
|
|
|
QFile *file;
|
|
|
|
|
|
|
|
file = Qopen (filename, "rbz");
|
|
|
|
if (!file) {
|
|
|
|
perror (filename);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
qfo = qfo_read (file);
|
|
|
|
Qclose (file);
|
|
|
|
return qfo;
|
2002-07-05 20:02:10 +00:00
|
|
|
}
|
|
|
|
|
2002-07-11 17:06:15 +00:00
|
|
|
qfo_t *
|
|
|
|
qfo_new (void)
|
|
|
|
{
|
2011-02-24 03:18:35 +00:00
|
|
|
return calloc (1, sizeof (qfo_t));
|
2002-07-11 17:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
qfo_delete (qfo_t *qfo)
|
|
|
|
{
|
2011-02-23 09:10:38 +00:00
|
|
|
if (qfo->data) {
|
|
|
|
free (qfo->data);
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < qfo->num_spaces; i++)
|
|
|
|
free (qfo->spaces->d.data);
|
|
|
|
free (qfo->relocs);
|
|
|
|
free (qfo->defs);
|
|
|
|
free (qfo->funcs);
|
|
|
|
free (qfo->lines);
|
|
|
|
}
|
|
|
|
free (qfo->spaces);
|
|
|
|
free (qfo);
|
2002-07-11 17:06:15 +00:00
|
|
|
}
|
2011-03-02 10:06:43 +00:00
|
|
|
|
|
|
|
static etype_t
|
|
|
|
get_def_type (qfo_t *qfo, pointer_t type)
|
|
|
|
{
|
|
|
|
qfot_type_t *type_def;
|
|
|
|
if (type < 0 || type >= qfo->spaces[qfo_type_space].data_size)
|
|
|
|
return ev_void;
|
|
|
|
type_def = QFO_POINTER (qfo, qfo_type_space, qfot_type_t, type);
|
|
|
|
switch ((ty_type_e)type_def->ty) {
|
|
|
|
case ty_none:
|
|
|
|
// field, pointer and function types store their basic type in
|
|
|
|
// the same location.
|
|
|
|
return type_def->t.type;
|
|
|
|
case ty_struct:
|
|
|
|
case ty_union:
|
|
|
|
return ev_invalid;
|
|
|
|
case ty_enum:
|
|
|
|
return ev_integer; // FIXME v6 progs should be float
|
|
|
|
case ty_array:
|
|
|
|
case ty_class:
|
|
|
|
return ev_invalid;
|
|
|
|
}
|
|
|
|
return ev_invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static etype_t
|
|
|
|
get_type_size (qfo_t *qfo, pointer_t type)
|
|
|
|
{
|
|
|
|
qfot_type_t *type_def;
|
|
|
|
int i, size;
|
|
|
|
if (type < 0 || type >= qfo->spaces[qfo_type_space].data_size)
|
|
|
|
return 1;
|
|
|
|
type_def = QFO_POINTER (qfo, qfo_type_space, qfot_type_t, type);
|
|
|
|
switch ((ty_type_e)type_def->ty) {
|
|
|
|
case ty_none:
|
|
|
|
// field, pointer and function types store their basic type in
|
|
|
|
// the same location.
|
|
|
|
return pr_type_size[type_def->t.type];
|
|
|
|
case ty_struct:
|
|
|
|
for (i = size = 0; i < type_def->t.strct.num_fields; i++)
|
|
|
|
size += get_type_size (qfo, type_def->t.strct.fields[i].type);
|
|
|
|
return size;
|
|
|
|
case ty_union:
|
|
|
|
for (i = size = 0; i < type_def->t.strct.num_fields; i++) {
|
|
|
|
int s;
|
|
|
|
s = get_type_size (qfo, type_def->t.strct.fields[i].type);
|
|
|
|
if (s > size)
|
|
|
|
size = s;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
case ty_enum:
|
|
|
|
return pr_type_size[ev_integer];
|
|
|
|
case ty_array:
|
|
|
|
return type_def->t.array.size
|
|
|
|
* get_type_size (qfo, type_def->t.array.type);
|
|
|
|
case ty_class:
|
|
|
|
return 0; // FIXME
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
function_params (qfo_t *qfo, qfo_func_t *func, dfunction_t *df)
|
|
|
|
{
|
|
|
|
qfot_type_t *type;
|
|
|
|
int num_params;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (func->type < 0 || func->type >= qfo->spaces[qfo_type_space].data_size)
|
|
|
|
return;
|
|
|
|
type = QFO_POINTER (qfo, qfo_type_space, qfot_type_t, func->type);
|
|
|
|
if (type->ty != ty_none && type->t.type != ev_func)
|
|
|
|
return;
|
|
|
|
df->numparms = num_params = type->t.func.num_params;
|
|
|
|
if (num_params < 0)
|
|
|
|
num_params = ~num_params;
|
|
|
|
for (i = 0; i < num_params; i++)
|
|
|
|
df->parm_size[i] = get_type_size (qfo, type->t.func.param_types[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
convert_def (qfo_t *qfo, const qfo_def_t *def, ddef_t *ddef)
|
|
|
|
{
|
|
|
|
ddef->type = get_def_type (qfo, def->type);
|
|
|
|
ddef->ofs = def->offset;
|
|
|
|
ddef->s_name = def->name;
|
|
|
|
if (!(def->flags & QFOD_NOSAVE)
|
|
|
|
&& !(def->flags & QFOD_CONSTANT)
|
|
|
|
&& (def->flags & QFOD_GLOBAL)
|
|
|
|
&& ddef->type != ev_func
|
|
|
|
&& ddef->type != ev_field)
|
|
|
|
ddef->type |= DEF_SAVEGLOBAL;
|
|
|
|
}
|
|
|
|
|
2011-03-04 23:41:14 +00:00
|
|
|
static void
|
|
|
|
qfo_relocate_refs (qfo_t *qfo)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
qfo_reloc_t *reloc;
|
|
|
|
|
|
|
|
for (i = 0, reloc = qfo->relocs; i < qfo->num_relocs; i++, reloc++) {
|
|
|
|
// this will be valid only for *_def relocs
|
|
|
|
qfo_def_t *def = qfo->defs + reloc->target;
|
|
|
|
|
|
|
|
switch (reloc->type) {
|
|
|
|
case rel_none:
|
|
|
|
break;
|
|
|
|
case rel_op_a_def:
|
|
|
|
QFO_STATEMENT (qfo, reloc->offset)->a = def->offset;
|
|
|
|
break;
|
|
|
|
case rel_op_b_def:
|
|
|
|
QFO_STATEMENT (qfo, reloc->offset)->b = def->offset;
|
|
|
|
break;
|
|
|
|
case rel_op_c_def:
|
|
|
|
QFO_STATEMENT (qfo, reloc->offset)->c = def->offset;
|
|
|
|
break;
|
|
|
|
case rel_op_a_op:
|
|
|
|
case rel_op_b_op:
|
|
|
|
case rel_op_c_op:
|
|
|
|
// these should never appear in a qfo file
|
|
|
|
break;
|
|
|
|
case rel_def_op:
|
|
|
|
//FIXME how?
|
|
|
|
break;
|
|
|
|
case rel_def_def:
|
|
|
|
QFO_INT (qfo, reloc->space, reloc->offset) = def->offset;
|
|
|
|
break;
|
|
|
|
case rel_def_func:
|
2011-03-05 07:00:39 +00:00
|
|
|
QFO_INT (qfo, reloc->space, reloc->offset) = reloc->target + 1;
|
2011-03-04 23:41:14 +00:00
|
|
|
break;
|
|
|
|
case rel_def_string:
|
|
|
|
QFO_INT (qfo, reloc->space, reloc->offset) = reloc->target;
|
|
|
|
break;
|
|
|
|
case rel_def_field:
|
|
|
|
//FIXME how?
|
|
|
|
break;
|
|
|
|
case rel_op_a_def_ofs:
|
|
|
|
QFO_STATEMENT (qfo, reloc->offset)->a += def->offset;
|
|
|
|
break;
|
|
|
|
case rel_op_b_def_ofs:
|
|
|
|
QFO_STATEMENT (qfo, reloc->offset)->b += def->offset;
|
|
|
|
break;
|
|
|
|
case rel_op_c_def_ofs:
|
|
|
|
QFO_STATEMENT (qfo, reloc->offset)->c += def->offset;
|
|
|
|
break;
|
|
|
|
case rel_def_def_ofs:
|
|
|
|
QFO_INT (qfo, reloc->space, reloc->offset) += def->offset;
|
|
|
|
break;
|
|
|
|
case rel_def_field_ofs:
|
|
|
|
//FIXME how?
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-02 10:06:43 +00:00
|
|
|
dprograms_t *
|
2011-03-02 11:51:00 +00:00
|
|
|
qfo_to_progs (qfo_t *qfo, int *size)
|
2011-03-02 10:06:43 +00:00
|
|
|
{
|
2011-03-04 11:14:02 +00:00
|
|
|
byte *data;
|
2011-03-02 10:06:43 +00:00
|
|
|
char *strings;
|
|
|
|
dstatement_t *statements;
|
|
|
|
dfunction_t *functions;
|
|
|
|
ddef_t *globaldefs;
|
|
|
|
ddef_t *fielddefs;
|
|
|
|
pr_type_t *globals;
|
2011-03-04 14:31:09 +00:00
|
|
|
pr_type_t *locals;
|
|
|
|
pr_type_t *far_data;
|
|
|
|
pr_type_t *type_data;
|
2011-03-02 10:06:43 +00:00
|
|
|
dprograms_t *progs;
|
2011-03-04 14:31:09 +00:00
|
|
|
int i, j;
|
2011-03-02 10:06:43 +00:00
|
|
|
int locals_size = 0;
|
|
|
|
int locals_start;
|
|
|
|
int big_locals = 0;
|
|
|
|
|
2011-03-04 11:14:02 +00:00
|
|
|
*size = RUP (sizeof (dprograms_t), 16);
|
2011-03-02 11:51:00 +00:00
|
|
|
progs = calloc (1, *size);
|
2011-03-02 10:06:43 +00:00
|
|
|
progs->version = options.code.progsversion;
|
|
|
|
progs->numstatements = qfo->spaces[qfo_code_space].data_size;
|
|
|
|
progs->numglobaldefs = qfo->spaces[qfo_near_data_space].num_defs;
|
2011-03-05 06:59:14 +00:00
|
|
|
//FIXME ddef offsets are 16 bits
|
|
|
|
progs->numglobaldefs += qfo->spaces[qfo_far_data_space].num_defs;
|
2011-03-02 10:06:43 +00:00
|
|
|
progs->numfielddefs = qfo->spaces[qfo_entity_space].num_defs;
|
2011-03-04 11:29:40 +00:00
|
|
|
progs->numfunctions = qfo->num_funcs + 1;
|
2011-03-02 10:06:43 +00:00
|
|
|
progs->numstrings = qfo->spaces[qfo_strings_space].data_size;
|
|
|
|
progs->numglobals = qfo->spaces[qfo_near_data_space].data_size;
|
|
|
|
progs->numglobals += qfo->spaces[qfo_far_data_space].data_size;
|
|
|
|
progs->numglobals += qfo->spaces[qfo_type_space].data_size;
|
|
|
|
locals_start = qfo->spaces[qfo_near_data_space].data_size;
|
|
|
|
for (i = qfo_num_spaces; i < qfo->num_spaces; i++) {
|
|
|
|
if (options.code.local_merging) {
|
|
|
|
if (locals_size < qfo->spaces[i].data_size)
|
|
|
|
locals_size = qfo->spaces[i].data_size;
|
|
|
|
big_locals = i;
|
|
|
|
} else {
|
|
|
|
locals_size += qfo->spaces[i].data_size;
|
|
|
|
}
|
|
|
|
}
|
2011-03-04 11:14:02 +00:00
|
|
|
progs->numglobals += locals_size;
|
|
|
|
progs->numglobals = RUP (progs->numglobals, 16 / sizeof (pr_type_t));
|
2011-03-02 10:06:43 +00:00
|
|
|
progs->entityfields = qfo->spaces[qfo_entity_space].data_size;
|
2011-03-02 11:51:00 +00:00
|
|
|
*size += progs->numstatements * sizeof (dstatement_t);
|
|
|
|
*size += progs->numglobaldefs * sizeof (ddef_t);
|
|
|
|
*size += progs->numfielddefs * sizeof (ddef_t);
|
|
|
|
*size += progs->numfunctions * sizeof (dfunction_t);
|
2011-03-04 11:14:02 +00:00
|
|
|
*size += RUP (progs->numstrings * sizeof (char), 16);
|
|
|
|
*size += progs->numglobals * sizeof (pr_type_t);
|
|
|
|
|
2011-03-02 11:51:00 +00:00
|
|
|
progs = realloc (progs, *size);
|
2011-03-04 11:14:02 +00:00
|
|
|
data = (byte *) progs;
|
|
|
|
memset (progs + 1, 0, *size - sizeof (dprograms_t));
|
|
|
|
data += RUP (sizeof (dprograms_t), 16);
|
2011-03-02 10:06:43 +00:00
|
|
|
|
2011-03-04 11:14:02 +00:00
|
|
|
progs->ofs_strings = data - (byte *) progs;
|
|
|
|
strings = (char *) data;
|
|
|
|
data += RUP (progs->numstrings * sizeof (char), 16);
|
2011-03-02 10:06:43 +00:00
|
|
|
|
2011-03-04 11:14:02 +00:00
|
|
|
progs->ofs_statements = data - (byte *) progs;
|
|
|
|
statements = (dstatement_t *) data;
|
|
|
|
data += progs->numstatements * sizeof (dstatement_t);
|
2011-03-02 10:06:43 +00:00
|
|
|
|
2011-03-04 11:14:02 +00:00
|
|
|
progs->ofs_functions = data - (byte *) progs;
|
|
|
|
functions = (dfunction_t *) data;
|
2011-03-04 11:29:40 +00:00
|
|
|
functions++; // skip over null function
|
2011-03-04 14:31:09 +00:00
|
|
|
data += progs->numfunctions * sizeof (dfunction_t);
|
|
|
|
|
|
|
|
progs->ofs_globaldefs = data - (byte *) progs;
|
|
|
|
globaldefs = (ddef_t *) data;
|
|
|
|
data += progs->numglobaldefs * sizeof (ddef_t);
|
|
|
|
|
|
|
|
progs->ofs_fielddefs = data - (byte *) progs;
|
|
|
|
fielddefs = (ddef_t *) (globaldefs + progs->numglobaldefs);
|
|
|
|
data += progs->numfielddefs * sizeof (ddef_t);
|
|
|
|
|
|
|
|
progs->ofs_globals = data - (byte *) progs;
|
|
|
|
globals = (pr_type_t*) data;
|
|
|
|
locals = globals + qfo->spaces[qfo_near_data_space].data_size;
|
|
|
|
far_data = locals + locals_size;
|
|
|
|
type_data = far_data + qfo->spaces[qfo_far_data_space].data_size;
|
|
|
|
|
|
|
|
memcpy (strings, qfo->spaces[qfo_strings_space].d.strings,
|
|
|
|
qfo->spaces[qfo_strings_space].data_size * sizeof (char));
|
2011-03-04 14:37:46 +00:00
|
|
|
qfo->spaces[qfo_strings_space].d.strings = strings;
|
2011-03-04 14:31:09 +00:00
|
|
|
|
|
|
|
memcpy (statements, qfo->spaces[qfo_code_space].d.code,
|
|
|
|
qfo->spaces[qfo_code_space].data_size * sizeof (dstatement_t));
|
2011-03-04 14:37:46 +00:00
|
|
|
qfo->spaces[qfo_code_space].d.code = statements;
|
2011-03-04 14:31:09 +00:00
|
|
|
|
2011-03-02 10:06:43 +00:00
|
|
|
for (i = 0; i < qfo->num_funcs; i++) {
|
|
|
|
dfunction_t *df = functions + i;
|
|
|
|
qfo_func_t *qf = qfo->funcs + i;
|
2011-03-04 14:37:46 +00:00
|
|
|
qfo_mspace_t *space = qfo->spaces + qf->locals_space;
|
|
|
|
|
2011-03-04 09:45:43 +00:00
|
|
|
df->first_statement = qf->code;
|
2011-03-02 10:06:43 +00:00
|
|
|
df->parm_start = locals_start;
|
2011-03-04 14:37:46 +00:00
|
|
|
df->locals = space->data_size;
|
|
|
|
// finalize the offsets of the local defs
|
|
|
|
for (j = 0; j < space->num_defs; j++)
|
|
|
|
space->defs[j].offset += locals_start;
|
2011-03-02 10:06:43 +00:00
|
|
|
if (!options.code.local_merging)
|
|
|
|
locals_start += df->locals;
|
|
|
|
df->profile = 0;
|
|
|
|
df->s_name = qf->name;
|
|
|
|
df->s_file = qf->file;
|
|
|
|
function_params (qfo, qf, df);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < qfo->spaces[qfo_near_data_space].num_defs; i++) {
|
|
|
|
convert_def (qfo, qfo->spaces[qfo_near_data_space].defs + i,
|
2011-03-05 06:59:14 +00:00
|
|
|
globaldefs++);
|
2011-03-02 10:06:43 +00:00
|
|
|
}
|
|
|
|
|
2011-03-05 06:59:14 +00:00
|
|
|
//FIXME ddef offsets are 16 bits
|
2011-03-04 14:37:46 +00:00
|
|
|
for (i = 0; i < qfo->spaces[qfo_far_data_space].num_defs; i++) {
|
2011-03-05 06:59:14 +00:00
|
|
|
qfo_def_t *def = qfo->spaces[qfo_far_data_space].defs + i;
|
|
|
|
def->offset += far_data - globals;
|
|
|
|
convert_def (qfo, def, globaldefs++);
|
2011-03-04 14:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < qfo->spaces[qfo_type_space].num_defs; i++) {
|
|
|
|
qfo->spaces[qfo_type_space].defs[i].offset += type_data - globals;
|
|
|
|
}
|
|
|
|
|
2011-03-02 10:06:43 +00:00
|
|
|
for (i = 0; i < qfo->spaces[qfo_entity_space].num_defs; i++) {
|
|
|
|
convert_def (qfo, qfo->spaces[qfo_near_data_space].defs + i,
|
|
|
|
fielddefs + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy near data
|
|
|
|
memcpy (globals, qfo->spaces[qfo_near_data_space].d.data,
|
|
|
|
qfo->spaces[qfo_near_data_space].data_size * sizeof (pr_type_t));
|
|
|
|
qfo->spaces[qfo_near_data_space].d.data = globals;
|
|
|
|
// lcear locals data
|
2011-03-04 14:31:09 +00:00
|
|
|
memset (locals, 0, locals_size * sizeof (pr_type_t));
|
2011-03-02 10:06:43 +00:00
|
|
|
// copy far data
|
2011-03-04 14:31:09 +00:00
|
|
|
memcpy (far_data, qfo->spaces[qfo_far_data_space].d.data,
|
2011-03-02 10:06:43 +00:00
|
|
|
qfo->spaces[qfo_far_data_space].data_size * sizeof (pr_type_t));
|
2011-03-04 14:31:09 +00:00
|
|
|
qfo->spaces[qfo_far_data_space].d.data = far_data;
|
2011-03-02 10:06:43 +00:00
|
|
|
// copy type data
|
2011-03-04 14:31:09 +00:00
|
|
|
memcpy (type_data, qfo->spaces[qfo_type_space].d.data,
|
2011-03-02 10:06:43 +00:00
|
|
|
qfo->spaces[qfo_type_space].data_size * sizeof (pr_type_t));
|
2011-03-04 14:31:09 +00:00
|
|
|
qfo->spaces[qfo_type_space].d.data = type_data;
|
2011-03-04 23:41:14 +00:00
|
|
|
|
|
|
|
qfo_relocate_refs (qfo);
|
|
|
|
|
2011-03-02 10:06:43 +00:00
|
|
|
return progs;
|
|
|
|
}
|
2011-03-04 10:02:47 +00:00
|
|
|
|
|
|
|
pr_debug_header_t *
|
|
|
|
qfo_to_sym (qfo_t *qfo, int *size)
|
|
|
|
{
|
|
|
|
pr_debug_header_t *sym;
|
|
|
|
int i, j;
|
|
|
|
pr_auxfunction_t *auxfuncs;
|
2011-03-05 06:40:08 +00:00
|
|
|
pr_auxfunction_t *aux;
|
2011-03-04 10:02:47 +00:00
|
|
|
pr_lineno_t *linenos;
|
|
|
|
ddef_t *locals, *ld;
|
|
|
|
|
|
|
|
*size = sizeof (pr_debug_header_t);
|
|
|
|
sym = calloc (1, *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;
|
|
|
|
|
|
|
|
if (func->locals_space)
|
|
|
|
num_locals = qfo->spaces[func->locals_space].num_defs;
|
|
|
|
if (!func->line_info && !num_locals)
|
|
|
|
continue;
|
|
|
|
sym->num_auxfunctions++;
|
|
|
|
sym->num_locals += num_locals;
|
|
|
|
}
|
|
|
|
sym->num_linenos = qfo->num_lines;
|
|
|
|
|
|
|
|
*size += sym->num_auxfunctions * sizeof (pr_auxfunction_t);
|
|
|
|
*size += sym->num_linenos * sizeof (pr_lineno_t);
|
|
|
|
*size += sym->num_locals * sizeof (ddef_t);
|
|
|
|
sym = realloc (sym, *size);
|
|
|
|
|
|
|
|
auxfuncs = (pr_auxfunction_t *)(sym + 1);
|
|
|
|
linenos = (pr_lineno_t *)(auxfuncs + sym->num_auxfunctions);
|
|
|
|
locals = (ddef_t *)(linenos + sym->num_linenos);
|
|
|
|
|
|
|
|
sym->auxfunctions = (char *) auxfuncs - (char *) sym;
|
|
|
|
sym->linenos = (char *) linenos - (char *) sym;
|
|
|
|
sym->locals = (char *) locals - (char *) sym;
|
|
|
|
|
|
|
|
ld = locals;
|
|
|
|
|
2011-03-05 06:40:08 +00:00
|
|
|
for (i = 0, aux = auxfuncs; i < qfo->num_funcs; i++) {
|
2011-03-04 10:02:47 +00:00
|
|
|
qfo_func_t *func = qfo->funcs + i;
|
|
|
|
qfo_def_t *def = 0;
|
|
|
|
int num_locals = 0;
|
|
|
|
qfot_type_t *type;
|
|
|
|
|
|
|
|
if (func->locals_space) {
|
|
|
|
num_locals = qfo->spaces[func->locals_space].num_defs;
|
|
|
|
def = qfo->spaces[func->locals_space].defs;
|
|
|
|
}
|
|
|
|
if (!func->line_info && !num_locals)
|
|
|
|
continue;
|
2011-03-05 06:40:08 +00:00
|
|
|
memset (aux, 0, sizeof (*aux));
|
|
|
|
aux->function = i + 1;
|
|
|
|
aux->source_line = func->line;
|
|
|
|
aux->line_info = func->line_info;
|
|
|
|
if (func->line_info)
|
|
|
|
qfo->lines[func->line_info].fa.func = aux - auxfuncs;
|
2011-03-04 10:02:47 +00:00
|
|
|
if (num_locals) {
|
2011-03-05 06:40:08 +00:00
|
|
|
aux->local_defs = ld - locals;
|
2011-03-04 10:02:47 +00:00
|
|
|
for (j = 0; j < num_locals; j++)
|
|
|
|
convert_def (qfo, def++, ld++);
|
|
|
|
}
|
2011-03-05 06:40:08 +00:00
|
|
|
aux->num_locals = num_locals;
|
2011-03-04 10:02:47 +00:00
|
|
|
//FIXME check type
|
|
|
|
type = QFO_POINTER (qfo, qfo_type_space, qfot_type_t, func->type);
|
2011-03-05 06:40:08 +00:00
|
|
|
aux->return_type = type->t.func.return_type;
|
|
|
|
aux++;
|
2011-03-04 10:02:47 +00:00
|
|
|
}
|
|
|
|
memcpy (linenos, qfo->lines, qfo->num_lines * sizeof (pr_lineno_t));
|
|
|
|
return sym;
|
|
|
|
}
|