2002-06-07 17:29:30 +00:00
|
|
|
/*
|
2002-10-22 14:53:18 +00:00
|
|
|
reloc.c
|
2002-06-07 17:29:30 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
relocation support
|
2002-06-07 17:29:30 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
|
2002-06-07 17:29:30 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/06/07
|
2002-06-07 17:29:30 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2002-06-27 15:13:43 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2002-06-07 17:29:30 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-12-06 11:52:53 +00:00
|
|
|
#include "QF/alloc.h"
|
|
|
|
|
2020-06-21 14:15:17 +00:00
|
|
|
#include "tools/qfcc/include/codespace.h"
|
|
|
|
#include "tools/qfcc/include/def.h"
|
|
|
|
#include "tools/qfcc/include/defspace.h"
|
|
|
|
#include "tools/qfcc/include/diagnostic.h"
|
|
|
|
#include "tools/qfcc/include/emit.h"
|
|
|
|
#include "tools/qfcc/include/expr.h"
|
|
|
|
#include "tools/qfcc/include/function.h"
|
|
|
|
#include "tools/qfcc/include/qfcc.h"
|
|
|
|
#include "tools/qfcc/include/reloc.h"
|
2002-06-07 17:29:30 +00:00
|
|
|
|
2013-03-08 13:16:31 +00:00
|
|
|
static reloc_t *refs_freelist;
|
2002-06-26 22:10:59 +00:00
|
|
|
|
2011-02-12 13:34:38 +00:00
|
|
|
static const char *reloc_name[] = {
|
|
|
|
"rel_none",
|
|
|
|
"rel_op_a_def",
|
|
|
|
"rel_op_b_def",
|
|
|
|
"rel_op_c_def",
|
|
|
|
"rel_op_a_op",
|
|
|
|
"rel_op_b_op",
|
|
|
|
"rel_op_c_op",
|
|
|
|
"rel_def_op",
|
|
|
|
"rel_def_def",
|
|
|
|
"rel_def_func",
|
|
|
|
"rel_def_string",
|
|
|
|
"rel_def_field",
|
|
|
|
"rel_op_a_def_ofs",
|
|
|
|
"rel_op_b_def_ofs",
|
|
|
|
"rel_op_c_def_ofs",
|
|
|
|
"rel_def_def_ofs",
|
|
|
|
"rel_def_field_ofs",
|
|
|
|
};
|
|
|
|
|
2022-04-26 06:10:00 +00:00
|
|
|
#define RELOC(r) (r)->space->data[(r)->offset].value
|
2011-01-24 06:41:43 +00:00
|
|
|
|
2002-06-07 17:29:30 +00:00
|
|
|
void
|
2011-01-24 06:41:43 +00:00
|
|
|
relocate_refs (reloc_t *reloc, int offset)
|
2002-06-07 17:29:30 +00:00
|
|
|
{
|
|
|
|
int o;
|
|
|
|
|
2011-01-24 06:41:43 +00:00
|
|
|
while (reloc) {
|
2011-02-12 13:34:38 +00:00
|
|
|
debug (0, "reloc %s:%x %s %x",
|
|
|
|
reloc->space ? (reloc->space == pr.near_data ? "near" : "far")
|
|
|
|
: "code",
|
|
|
|
reloc->offset, reloc_name[reloc->type], offset);
|
2011-01-24 06:41:43 +00:00
|
|
|
switch (reloc->type) {
|
2002-06-07 17:29:30 +00:00
|
|
|
case rel_none:
|
|
|
|
break;
|
|
|
|
case rel_op_a_def:
|
2011-01-24 06:41:43 +00:00
|
|
|
if (offset > 65535)
|
2002-06-07 17:29:30 +00:00
|
|
|
error (0, "def offset too large");
|
|
|
|
else
|
2011-01-24 06:41:43 +00:00
|
|
|
pr.code->code[reloc->offset].a = offset;
|
2002-06-07 17:29:30 +00:00
|
|
|
break;
|
|
|
|
case rel_op_b_def:
|
2011-01-24 06:41:43 +00:00
|
|
|
if (offset > 65535)
|
2002-06-07 17:29:30 +00:00
|
|
|
error (0, "def offset too large");
|
|
|
|
else
|
2011-01-24 06:41:43 +00:00
|
|
|
pr.code->code[reloc->offset].b = offset;
|
2002-06-07 17:29:30 +00:00
|
|
|
break;
|
|
|
|
case rel_op_c_def:
|
2011-01-24 06:41:43 +00:00
|
|
|
if (offset > 65535)
|
2002-06-07 17:29:30 +00:00
|
|
|
error (0, "def offset too large");
|
|
|
|
else
|
2011-01-24 06:41:43 +00:00
|
|
|
pr.code->code[reloc->offset].c = offset;
|
2002-06-07 17:29:30 +00:00
|
|
|
break;
|
|
|
|
case rel_op_a_op:
|
2011-01-24 06:41:43 +00:00
|
|
|
o = offset - reloc->offset;
|
2002-06-07 17:29:30 +00:00
|
|
|
if (o < -32768 || o > 32767)
|
|
|
|
error (0, "relative offset too large");
|
|
|
|
else
|
2011-01-24 06:41:43 +00:00
|
|
|
pr.code->code[reloc->offset].a = o;
|
2002-06-07 17:29:30 +00:00
|
|
|
break;
|
|
|
|
case rel_op_b_op:
|
2011-01-24 06:41:43 +00:00
|
|
|
o = offset - reloc->offset;
|
2002-06-07 17:29:30 +00:00
|
|
|
if (o < -32768 || o > 32767)
|
|
|
|
error (0, "relative offset too large");
|
|
|
|
else
|
2011-01-24 06:41:43 +00:00
|
|
|
pr.code->code[reloc->offset].b = o;
|
2002-06-07 17:29:30 +00:00
|
|
|
break;
|
|
|
|
case rel_op_c_op:
|
2011-01-24 06:41:43 +00:00
|
|
|
o = offset - reloc->offset;
|
2002-06-07 17:29:30 +00:00
|
|
|
if (o < -32768 || o > 32767)
|
|
|
|
error (0, "relative offset too large");
|
|
|
|
else
|
2011-01-24 06:41:43 +00:00
|
|
|
pr.code->code[reloc->offset].c = o;
|
2002-06-07 17:29:30 +00:00
|
|
|
break;
|
|
|
|
case rel_def_op:
|
2011-01-24 06:41:43 +00:00
|
|
|
if (offset > pr.code->size) {
|
2003-07-24 17:51:24 +00:00
|
|
|
error (0, "invalid statement offset: %d >= %d, %d",
|
2011-01-24 06:41:43 +00:00
|
|
|
offset, pr.code->size, reloc->offset);
|
2003-07-24 17:51:24 +00:00
|
|
|
} else
|
2011-02-12 13:34:38 +00:00
|
|
|
RELOC (reloc) = offset;
|
2002-06-07 17:29:30 +00:00
|
|
|
break;
|
|
|
|
case rel_def_def:
|
2002-06-21 20:46:56 +00:00
|
|
|
case rel_def_func:
|
2011-02-12 13:34:38 +00:00
|
|
|
RELOC (reloc) = offset;
|
2002-06-07 17:29:30 +00:00
|
|
|
break;
|
2002-07-11 17:06:15 +00:00
|
|
|
case rel_def_string:
|
|
|
|
break;
|
2002-07-16 21:59:47 +00:00
|
|
|
case rel_def_field:
|
|
|
|
break;
|
2003-08-21 02:21:30 +00:00
|
|
|
case rel_op_a_def_ofs:
|
2011-01-24 06:41:43 +00:00
|
|
|
o = offset + pr.code->code[reloc->offset].a;
|
2003-08-21 02:21:30 +00:00
|
|
|
if (o < 0 || o > 65535)
|
|
|
|
error (0, "def offset out of range");
|
|
|
|
else
|
2011-01-24 06:41:43 +00:00
|
|
|
pr.code->code[reloc->offset].a = o;
|
2003-08-21 02:21:30 +00:00
|
|
|
break;
|
|
|
|
case rel_op_b_def_ofs:
|
2011-01-24 06:41:43 +00:00
|
|
|
o = offset + pr.code->code[reloc->offset].b;
|
2003-08-21 02:21:30 +00:00
|
|
|
if (o < 0 || o > 65535)
|
|
|
|
error (0, "def offset out of range");
|
|
|
|
else
|
2011-01-24 06:41:43 +00:00
|
|
|
pr.code->code[reloc->offset].b = o;
|
2003-08-21 02:21:30 +00:00
|
|
|
break;
|
|
|
|
case rel_op_c_def_ofs:
|
2011-01-24 06:41:43 +00:00
|
|
|
o = offset + pr.code->code[reloc->offset].c;
|
2003-08-21 02:21:30 +00:00
|
|
|
if (o < 0 || o > 65535)
|
|
|
|
error (0, "def offset out of range");
|
|
|
|
else
|
2011-01-24 06:41:43 +00:00
|
|
|
pr.code->code[reloc->offset].c = o;
|
2003-08-21 02:21:30 +00:00
|
|
|
break;
|
|
|
|
case rel_def_def_ofs:
|
2011-02-12 13:34:38 +00:00
|
|
|
RELOC (reloc) += offset;
|
2003-08-21 02:21:30 +00:00
|
|
|
break;
|
2004-11-12 10:49:00 +00:00
|
|
|
case rel_def_field_ofs:
|
2012-12-02 11:09:54 +00:00
|
|
|
//FIXME what is correct here?
|
2022-01-18 04:21:06 +00:00
|
|
|
//RELOC (reloc) += pr.data->data[offset].int_var;
|
2022-04-26 06:10:00 +00:00
|
|
|
RELOC (reloc) += PR_PTR (int, &pr.near_data->data[offset]);
|
2004-11-12 10:49:00 +00:00
|
|
|
break;
|
2002-06-07 17:29:30 +00:00
|
|
|
}
|
2011-01-24 06:41:43 +00:00
|
|
|
reloc = reloc->next;
|
2002-06-07 17:29:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
reloc_t *
|
2011-02-12 13:34:38 +00:00
|
|
|
new_reloc (defspace_t *space, int offset, reloc_type type)
|
2002-06-07 17:29:30 +00:00
|
|
|
{
|
2002-06-26 22:10:59 +00:00
|
|
|
reloc_t *ref;
|
2002-06-07 17:29:30 +00:00
|
|
|
|
2002-06-27 22:48:28 +00:00
|
|
|
ALLOC (16384, reloc_t, refs, ref);
|
2011-02-12 13:34:38 +00:00
|
|
|
ref->space = space;
|
2011-01-24 06:41:43 +00:00
|
|
|
ref->offset = offset;
|
2002-06-07 17:29:30 +00:00
|
|
|
ref->type = type;
|
2003-07-24 17:51:24 +00:00
|
|
|
ref->line = pr.source_line;
|
|
|
|
ref->file = pr.source_file;
|
2011-02-09 08:16:49 +00:00
|
|
|
ref->return_address = __builtin_return_address (0);
|
2002-06-07 17:29:30 +00:00
|
|
|
return ref;
|
|
|
|
}
|
2002-07-13 06:09:03 +00:00
|
|
|
|
2003-08-21 02:21:30 +00:00
|
|
|
void
|
2011-01-24 06:41:43 +00:00
|
|
|
reloc_op_def (def_t *def, int offset, int field)
|
2003-08-21 02:21:30 +00:00
|
|
|
{
|
2011-02-12 13:34:38 +00:00
|
|
|
reloc_t *ref = new_reloc (0, offset, rel_op_a_def + field);
|
2011-02-09 08:16:49 +00:00
|
|
|
ref->return_address = __builtin_return_address (0);
|
2011-01-24 06:41:43 +00:00
|
|
|
ref->next = def->relocs;
|
|
|
|
def->relocs = ref;
|
2003-08-21 02:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-01-24 06:41:43 +00:00
|
|
|
reloc_op_def_ofs (def_t *def, int offset, int field)
|
2003-08-21 02:21:30 +00:00
|
|
|
{
|
2011-02-12 13:34:38 +00:00
|
|
|
reloc_t *ref = new_reloc (0, offset, rel_op_a_def_ofs + field);
|
2011-02-09 08:16:49 +00:00
|
|
|
ref->return_address = __builtin_return_address (0);
|
2011-01-24 06:41:43 +00:00
|
|
|
ref->next = def->relocs;
|
|
|
|
def->relocs = ref;
|
2003-08-21 02:21:30 +00:00
|
|
|
}
|
|
|
|
|
2002-07-13 06:09:03 +00:00
|
|
|
void
|
2020-03-05 02:03:23 +00:00
|
|
|
reloc_def_def (def_t *def, const def_t *location)
|
2002-07-13 06:09:03 +00:00
|
|
|
{
|
2011-02-12 13:34:38 +00:00
|
|
|
reloc_t *ref;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2011-02-12 13:34:38 +00:00
|
|
|
ref = new_reloc (location->space, location->offset, rel_def_def);
|
2011-02-09 08:16:49 +00:00
|
|
|
ref->return_address = __builtin_return_address (0);
|
2011-01-24 06:41:43 +00:00
|
|
|
ref->next = def->relocs;
|
|
|
|
def->relocs = ref;
|
2002-07-13 06:09:03 +00:00
|
|
|
}
|
|
|
|
|
2003-08-21 02:21:30 +00:00
|
|
|
void
|
2020-03-05 02:03:23 +00:00
|
|
|
reloc_def_def_ofs (def_t *def, const def_t *location)
|
2003-08-21 02:21:30 +00:00
|
|
|
{
|
2011-02-12 13:34:38 +00:00
|
|
|
reloc_t *ref;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2011-02-12 13:34:38 +00:00
|
|
|
ref = new_reloc (location->space, location->offset, rel_def_def_ofs);
|
2011-02-09 08:16:49 +00:00
|
|
|
ref->return_address = __builtin_return_address (0);
|
2011-01-24 06:41:43 +00:00
|
|
|
ref->next = def->relocs;
|
|
|
|
def->relocs = ref;
|
2003-08-21 02:21:30 +00:00
|
|
|
}
|
|
|
|
|
2002-07-13 06:09:03 +00:00
|
|
|
void
|
2020-03-05 02:03:23 +00:00
|
|
|
reloc_def_func (function_t *func, const def_t *location)
|
2002-07-13 06:09:03 +00:00
|
|
|
{
|
2011-02-12 13:34:38 +00:00
|
|
|
reloc_t *ref;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2011-02-12 13:34:38 +00:00
|
|
|
ref = new_reloc (location->space, location->offset, rel_def_func);
|
2011-02-09 08:16:49 +00:00
|
|
|
ref->return_address = __builtin_return_address (0);
|
2002-07-13 06:09:03 +00:00
|
|
|
ref->next = func->refs;
|
|
|
|
func->refs = ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-03-05 02:03:23 +00:00
|
|
|
reloc_def_string (const def_t *location)
|
2002-07-13 06:09:03 +00:00
|
|
|
{
|
2011-02-12 13:34:38 +00:00
|
|
|
reloc_t *ref;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2011-02-12 13:34:38 +00:00
|
|
|
ref = new_reloc (location->space, location->offset, rel_def_string);
|
2011-02-09 08:16:49 +00:00
|
|
|
ref->return_address = __builtin_return_address (0);
|
2002-07-13 06:09:03 +00:00
|
|
|
ref->next = pr.relocs;
|
|
|
|
pr.relocs = ref;
|
|
|
|
}
|
2002-07-16 21:59:47 +00:00
|
|
|
|
|
|
|
void
|
2020-03-05 02:03:23 +00:00
|
|
|
reloc_def_field (def_t *def, const def_t *location)
|
2002-07-16 21:59:47 +00:00
|
|
|
{
|
2011-02-12 13:34:38 +00:00
|
|
|
reloc_t *ref;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2011-02-12 13:34:38 +00:00
|
|
|
ref = new_reloc (location->space, location->offset, rel_def_field);
|
2011-02-09 08:16:49 +00:00
|
|
|
ref->return_address = __builtin_return_address (0);
|
2011-01-24 06:41:43 +00:00
|
|
|
ref->next = def->relocs;
|
|
|
|
def->relocs = ref;
|
2002-07-16 21:59:47 +00:00
|
|
|
}
|
2003-08-22 19:48:14 +00:00
|
|
|
|
2004-11-12 10:49:00 +00:00
|
|
|
void
|
2020-03-05 02:03:23 +00:00
|
|
|
reloc_def_field_ofs (def_t *def, const def_t *location)
|
2004-11-12 10:49:00 +00:00
|
|
|
{
|
2011-02-12 13:34:38 +00:00
|
|
|
reloc_t *ref;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2011-02-12 13:34:38 +00:00
|
|
|
ref = new_reloc (location->space, location->offset, rel_def_field_ofs);
|
2011-02-09 08:16:49 +00:00
|
|
|
ref->return_address = __builtin_return_address (0);
|
2011-01-24 06:41:43 +00:00
|
|
|
ref->next = def->relocs;
|
|
|
|
def->relocs = ref;
|
2004-11-12 10:49:00 +00:00
|
|
|
}
|
|
|
|
|
2003-08-22 19:48:14 +00:00
|
|
|
void
|
2020-03-05 02:03:23 +00:00
|
|
|
reloc_def_op (const ex_label_t *label, const def_t *location)
|
2003-08-22 19:48:14 +00:00
|
|
|
{
|
2011-02-12 13:34:38 +00:00
|
|
|
reloc_t *ref;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2011-02-12 13:34:38 +00:00
|
|
|
ref = new_reloc (location->space, location->offset, rel_def_op);
|
2011-02-09 08:16:49 +00:00
|
|
|
ref->return_address = __builtin_return_address (0);
|
2003-08-22 19:48:14 +00:00
|
|
|
ref->next = pr.relocs;
|
|
|
|
ref->label = label;
|
|
|
|
pr.relocs = ref;
|
|
|
|
}
|
2012-11-12 07:14:09 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
reloc_attach_relocs (reloc_t *relocs, reloc_t **location)
|
|
|
|
{
|
|
|
|
reloc_t *r;
|
|
|
|
|
|
|
|
if (!relocs)
|
|
|
|
return;
|
|
|
|
for (r = relocs; r->next; r = r->next)
|
|
|
|
;
|
|
|
|
r->next = *location;
|
|
|
|
*location = relocs;
|
|
|
|
}
|