2002-07-03 20:16:32 +00:00
|
|
|
/*
|
2011-01-09 10:41:24 +00:00
|
|
|
linker.c
|
2002-07-03 20:16:32 +00:00
|
|
|
|
|
|
|
qc object file linking
|
|
|
|
|
2002-07-05 20:02:10 +00:00
|
|
|
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
|
2011-02-24 06:24:35 +00:00
|
|
|
Copyright (C) 2011 Bill Currie <bill@taniwha.org>
|
2002-07-03 20:16:32 +00:00
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/7/3
|
2011-02-24 06:24:35 +00:00
|
|
|
Date: 2011/2/24
|
2002-07-03 20:16:32 +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
|
|
|
|
2011-01-09 10:41:24 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] = "$Id$";
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2002-07-03 21:32:03 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2002-07-21 07:12:17 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
# include <io.h>
|
|
|
|
#endif
|
2002-07-03 20:16:32 +00:00
|
|
|
#include <stdlib.h>
|
2002-09-16 17:37:22 +00:00
|
|
|
#include <stdarg.h>
|
2002-07-21 07:12:17 +00:00
|
|
|
#include <fcntl.h>
|
2002-10-31 22:58:59 +00:00
|
|
|
#include <errno.h>
|
2002-07-03 20:16:32 +00:00
|
|
|
|
2002-09-16 17:37:22 +00:00
|
|
|
#include "QF/dstring.h"
|
2002-07-03 20:16:32 +00:00
|
|
|
#include "QF/hash.h"
|
2002-07-21 07:12:17 +00:00
|
|
|
#include "QF/pakfile.h"
|
2002-11-01 18:05:12 +00:00
|
|
|
#include "QF/va.h"
|
2002-07-03 20:16:32 +00:00
|
|
|
|
2011-01-09 10:41:24 +00:00
|
|
|
#include "codespace.h"
|
2002-07-03 20:16:32 +00:00
|
|
|
#include "def.h"
|
2011-01-17 13:33:33 +00:00
|
|
|
#include "defspace.h"
|
2011-01-24 12:54:57 +00:00
|
|
|
#include "diagnostic.h"
|
2002-07-08 20:31:59 +00:00
|
|
|
#include "emit.h"
|
2002-07-03 20:16:32 +00:00
|
|
|
#include "expr.h"
|
|
|
|
#include "immediate.h"
|
2002-07-11 21:04:09 +00:00
|
|
|
#include "linker.h"
|
2002-07-03 20:16:32 +00:00
|
|
|
#include "obj_file.h"
|
2002-07-12 04:50:31 +00:00
|
|
|
#include "options.h"
|
2002-07-03 20:16:32 +00:00
|
|
|
#include "qfcc.h"
|
2002-07-11 17:06:15 +00:00
|
|
|
#include "reloc.h"
|
2002-07-05 20:02:10 +00:00
|
|
|
#include "strpool.h"
|
2002-09-17 18:42:35 +00:00
|
|
|
#include "type.h"
|
2002-07-03 20:16:32 +00:00
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
static void def_error (qfo_def_t *def, const char *fmt, ...)
|
|
|
|
__attribute__ ((used, format (printf, 2, 3)));
|
|
|
|
static void def_warning (qfo_def_t *def, const char *fmt, ...)
|
|
|
|
__attribute__ ((used, format (printf, 2, 3)));
|
2003-02-21 22:31:40 +00:00
|
|
|
|
2003-04-22 15:29:32 +00:00
|
|
|
typedef struct builtin_sym_s {
|
|
|
|
const char *name;
|
2003-04-22 20:11:16 +00:00
|
|
|
type_t *type;
|
2003-04-25 17:00:22 +00:00
|
|
|
unsigned flags;
|
2003-04-22 15:29:32 +00:00
|
|
|
} builtin_sym_t;
|
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
static builtin_sym_t builtin_symbols[] __attribute__ ((used)) = {
|
2003-04-25 17:00:22 +00:00
|
|
|
{".zero", &type_zero, QFOD_NOSAVE},
|
|
|
|
{".return", &type_param, QFOD_NOSAVE},
|
|
|
|
{".param_0", &type_param, QFOD_NOSAVE},
|
|
|
|
{".param_1", &type_param, QFOD_NOSAVE},
|
|
|
|
{".param_2", &type_param, QFOD_NOSAVE},
|
|
|
|
{".param_3", &type_param, QFOD_NOSAVE},
|
|
|
|
{".param_4", &type_param, QFOD_NOSAVE},
|
|
|
|
{".param_5", &type_param, QFOD_NOSAVE},
|
|
|
|
{".param_6", &type_param, QFOD_NOSAVE},
|
|
|
|
{".param_7", &type_param, QFOD_NOSAVE},
|
2003-04-22 15:29:32 +00:00
|
|
|
};
|
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
static qfo_t *work;
|
|
|
|
static strpool_t *work_strings;
|
|
|
|
static codespace_t *work_code;
|
|
|
|
static defspace_t *work_near_data;
|
|
|
|
static defspace_t *work_far_data;
|
|
|
|
static defspace_t *work_entity_data;
|
|
|
|
static defspace_t *work_type_data;
|
2003-02-21 22:31:40 +00:00
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
/** Add a string to the working qfo string pool.
|
2003-02-21 22:31:40 +00:00
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
If the string is already in the string pool, the already existing string
|
|
|
|
index will be returned instead of adding a second copy of the string.
|
2002-07-03 20:16:32 +00:00
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
The strings space in the working qfo is kept up to date.
|
2002-07-03 20:16:32 +00:00
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
\param str The string to add.
|
|
|
|
\return The string offset in the working qfo string pool.
|
|
|
|
*/
|
|
|
|
static string_t
|
|
|
|
add_string (const char *str)
|
2002-07-11 17:06:15 +00:00
|
|
|
{
|
2011-02-24 03:18:35 +00:00
|
|
|
string_t new;
|
|
|
|
new = strpool_addstr (work_strings, str);
|
|
|
|
work->spaces[qfo_strings_space].d.strings = work_strings->strings;
|
|
|
|
work->spaces[qfo_strings_space].data_size = work_strings->size;
|
|
|
|
return new;
|
2002-07-11 17:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-02-24 03:18:35 +00:00
|
|
|
add_qfo_strings (qfo_mspace_t *strings)
|
2002-07-11 17:06:15 +00:00
|
|
|
{
|
2011-02-24 03:18:35 +00:00
|
|
|
const char *str = strings->d.strings;
|
2002-07-11 17:06:15 +00:00
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
while (str - strings->d.strings < strings->data_size) {
|
|
|
|
add_string (str);
|
|
|
|
while (str - strings->d.strings < strings->data_size && *str)
|
|
|
|
str++;
|
|
|
|
str++; // advance past the terminating nul
|
2002-07-11 17:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-02-24 03:18:35 +00:00
|
|
|
add_code (qfo_mspace_t *code)
|
2002-07-03 20:16:32 +00:00
|
|
|
{
|
2011-02-24 03:18:35 +00:00
|
|
|
codespace_addcode (work_code, code->d.code, code->data_size);
|
|
|
|
work->spaces[qfo_code_space].d.code = work_code->code;
|
|
|
|
work->spaces[qfo_code_space].data_size = work_code->size;
|
2002-07-14 05:40:38 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 21:59:47 +00:00
|
|
|
static void
|
2011-02-24 03:18:35 +00:00
|
|
|
add_data (int space, qfo_mspace_t *data)
|
2002-07-16 21:59:47 +00:00
|
|
|
{
|
2011-02-24 03:18:35 +00:00
|
|
|
defspace_t *work_spaces[qfo_num_spaces] = {
|
|
|
|
0, 0, 0,
|
|
|
|
work_near_data,
|
|
|
|
work_far_data,
|
|
|
|
work_entity_data,
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (space < 0 || space >= qfo_num_spaces || !work_spaces[space])
|
|
|
|
internal_error (0, "bad space for add_data (): %d", space);
|
|
|
|
defspace_add_data (work_spaces[space], data->d.data, data->data_size);
|
|
|
|
work->spaces[space].d.data = work_spaces[space]->data;
|
|
|
|
work->spaces[space].data_size = work_spaces[space]->size;
|
2002-07-16 21:59:47 +00:00
|
|
|
}
|
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
#define QFOSTR(q,s) ((q)->spaces[qfo_strings_space].d.strings + (s))
|
|
|
|
#define WORKSTR(q,s) QFOSTR (work, s)
|
2002-07-12 20:26:51 +00:00
|
|
|
|
|
|
|
static void
|
2011-02-24 03:18:35 +00:00
|
|
|
update_relocs (qfo_t *qfo)
|
2002-07-03 20:16:32 +00:00
|
|
|
{
|
2002-07-11 17:06:15 +00:00
|
|
|
int i;
|
2011-02-24 03:18:35 +00:00
|
|
|
qfo_reloc_t *reloc;
|
2002-07-03 20:16:32 +00:00
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
for (reloc = qfo->relocs, i = 0; i < qfo->num_relocs; i++, reloc++) {
|
|
|
|
if (!reloc->space) {
|
|
|
|
// code space is implied
|
|
|
|
reloc->offset += work->spaces[qfo_code_space].data_size;
|
|
|
|
} else if (reloc->space < 0 || reloc->space >= qfo->num_spaces) {
|
|
|
|
//FIXME proper diagnostic
|
|
|
|
fprintf (stderr, "bad reloc space: %d", reloc->space);
|
|
|
|
reloc->type = rel_none;
|
|
|
|
} else if (reloc->space < qfo_num_spaces) {
|
|
|
|
reloc->offset += work->spaces[reloc->space].data_size;
|
|
|
|
} else {
|
|
|
|
reloc->space += work->num_spaces;
|
|
|
|
}
|
2011-02-27 02:37:01 +00:00
|
|
|
reloc->target += work->num_defs; //FIXME wrong
|
2002-07-11 17:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-11 20:17:26 +00:00
|
|
|
static void
|
2011-02-24 03:18:35 +00:00
|
|
|
update_defs (qfo_t *qfo)
|
2002-07-11 20:17:26 +00:00
|
|
|
{
|
2011-02-24 03:18:35 +00:00
|
|
|
int space;
|
2002-07-11 20:17:26 +00:00
|
|
|
int i;
|
2011-02-24 03:18:35 +00:00
|
|
|
qfo_def_t *def;
|
2002-07-11 20:17:26 +00:00
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
for (space = 0; space < qfo->num_spaces; space++) {
|
|
|
|
if (space == qfo_type_space)
|
|
|
|
continue; // complicated. handle separately
|
|
|
|
for (def = qfo->spaces[space].defs, i = 0;
|
|
|
|
i < qfo->spaces[space].num_defs; i++, def++) {
|
|
|
|
//XXX type handled later
|
|
|
|
def->name = add_string (QFOSTR (qfo, def->name));
|
|
|
|
if (space < qfo_num_spaces)
|
|
|
|
def->offset += work->spaces[space].data_size;
|
|
|
|
def->relocs += work->num_relocs;
|
|
|
|
def->file = add_string (QFOSTR (qfo, def->file));
|
|
|
|
}
|
2002-07-11 20:17:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-11 17:06:15 +00:00
|
|
|
static void
|
2011-02-24 03:18:35 +00:00
|
|
|
update_funcs (qfo_t *qfo)
|
2002-07-11 17:06:15 +00:00
|
|
|
{
|
2002-07-16 06:40:34 +00:00
|
|
|
int i;
|
2011-02-24 03:18:35 +00:00
|
|
|
qfo_func_t *func;
|
2002-07-11 17:06:15 +00:00
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
for (func = qfo->funcs, i = 0; i < qfo->num_funcs; i++, func++) {
|
|
|
|
func->name = add_string (QFOSTR (qfo, func->name));
|
|
|
|
//XXX type handled later
|
|
|
|
func->file = add_string (QFOSTR (qfo, func->file));
|
|
|
|
if (func->code > 0)
|
|
|
|
func->code += work->spaces[qfo_code_space].data_size;
|
|
|
|
func->def += work->num_defs;
|
|
|
|
if (!func->locals_space) {
|
|
|
|
// no locals (builtin function?)
|
|
|
|
} else if (func->locals_space < qfo_num_spaces) {
|
|
|
|
//FIXME proper diagnostic
|
|
|
|
fprintf (stderr, "function with weird locals: setting to none\n");
|
|
|
|
func->locals_space = 0;
|
|
|
|
} else {
|
|
|
|
func->locals_space += work->num_spaces - qfo_num_spaces;
|
2002-07-11 17:06:15 +00:00
|
|
|
}
|
2011-02-24 03:18:35 +00:00
|
|
|
func->line_info += work->num_lines;
|
|
|
|
func->relocs += work->num_relocs;
|
2002-07-16 06:40:34 +00:00
|
|
|
}
|
2002-07-03 20:16:32 +00:00
|
|
|
}
|
|
|
|
|
2002-07-16 17:35:40 +00:00
|
|
|
static void
|
2011-02-24 03:18:35 +00:00
|
|
|
update_lines (qfo_t *qfo)
|
2002-07-16 17:35:40 +00:00
|
|
|
{
|
2011-02-24 03:18:35 +00:00
|
|
|
int i;
|
|
|
|
pr_lineno_t *lineno;
|
2002-07-16 17:35:40 +00:00
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
for (lineno = qfo->lines, i = 0; i < qfo->num_lines; i++, lineno++) {
|
|
|
|
if (lineno->line)
|
|
|
|
lineno->fa.addr += work->spaces[qfo_code_space].data_size;
|
|
|
|
else
|
|
|
|
lineno->fa.func += work->num_funcs;
|
2002-07-12 20:26:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
static __attribute__ ((used)) void
|
2003-04-22 15:29:32 +00:00
|
|
|
define_def (const char *name, etype_t basic_type, const char *full_type,
|
2003-04-25 17:00:22 +00:00
|
|
|
unsigned flags, int size, int v)
|
2002-07-14 05:40:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-07-03 20:16:32 +00:00
|
|
|
void
|
|
|
|
linker_begin (void)
|
|
|
|
{
|
2011-02-24 03:18:35 +00:00
|
|
|
work = qfo_new ();
|
|
|
|
work->spaces = calloc (qfo_num_spaces, sizeof (qfo_mspace_t));
|
|
|
|
work->num_spaces = qfo_num_spaces;
|
|
|
|
|
|
|
|
// adding data will take care of connecting the work qfo spaces with
|
|
|
|
// the actual space data
|
|
|
|
work_strings = strpool_new ();
|
|
|
|
work_code = codespace_new ();
|
2011-02-24 04:07:36 +00:00
|
|
|
work_near_data = defspace_new ();
|
|
|
|
work_far_data = defspace_new ();
|
|
|
|
work_entity_data = defspace_new ();
|
|
|
|
work_type_data = defspace_new ();
|
2002-07-03 20:16:32 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-07-21 07:12:17 +00:00
|
|
|
linker_add_qfo (qfo_t *qfo)
|
2002-07-03 20:16:32 +00:00
|
|
|
{
|
2011-02-24 03:18:35 +00:00
|
|
|
update_relocs (qfo);
|
|
|
|
update_defs (qfo);
|
|
|
|
update_funcs (qfo);
|
|
|
|
update_lines (qfo);
|
|
|
|
|
|
|
|
add_qfo_strings (&qfo->spaces[qfo_strings_space]);
|
|
|
|
add_code (&qfo->spaces[qfo_code_space]);
|
|
|
|
add_data (qfo_near_data_space, &qfo->spaces[qfo_near_data_space]);
|
|
|
|
add_data (qfo_far_data_space, &qfo->spaces[qfo_far_data_space]);
|
|
|
|
add_data (qfo_entity_space, &qfo->spaces[qfo_entity_space]);
|
|
|
|
//FIXME handle type data
|
2002-07-21 07:12:17 +00:00
|
|
|
}
|
|
|
|
|
2002-08-15 18:53:03 +00:00
|
|
|
int
|
2002-07-21 07:12:17 +00:00
|
|
|
linker_add_object_file (const char *filename)
|
|
|
|
{
|
|
|
|
qfo_t *qfo;
|
|
|
|
|
|
|
|
qfo = qfo_open (filename);
|
|
|
|
if (!qfo)
|
2002-08-15 18:53:03 +00:00
|
|
|
return 1;
|
2011-02-24 03:18:35 +00:00
|
|
|
if (qfo->num_spaces < qfo_num_spaces
|
|
|
|
|| qfo->spaces[qfo_null_space].type != qfos_null
|
|
|
|
|| qfo->spaces[qfo_strings_space].type != qfos_string
|
|
|
|
|| qfo->spaces[qfo_code_space].type != qfos_code
|
|
|
|
|| qfo->spaces[qfo_near_data_space].type != qfos_data
|
|
|
|
|| qfo->spaces[qfo_far_data_space].type != qfos_data
|
|
|
|
|| qfo->spaces[qfo_entity_space].type != qfos_entity
|
|
|
|
|| qfo->spaces[qfo_type_space].type != qfos_type) {
|
|
|
|
//FIXME proper diagnostic
|
|
|
|
fprintf (stderr, "%s: missing or mangled standard spaces", filename);
|
|
|
|
return 1;
|
|
|
|
}
|
2002-07-21 07:12:17 +00:00
|
|
|
|
|
|
|
if (options.verbosity >= 2)
|
|
|
|
puts (filename);
|
|
|
|
|
|
|
|
linker_add_qfo (qfo);
|
2002-07-11 17:06:15 +00:00
|
|
|
|
|
|
|
qfo_delete (qfo);
|
2002-08-15 18:53:03 +00:00
|
|
|
return 0;
|
2002-07-03 20:16:32 +00:00
|
|
|
}
|
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
typedef struct path_s {
|
|
|
|
struct path_s *next;
|
|
|
|
const char *path;
|
|
|
|
} path_t;
|
|
|
|
|
|
|
|
static path_t *path_head;
|
|
|
|
static path_t **path_tail = &path_head;
|
|
|
|
|
2002-08-15 18:53:03 +00:00
|
|
|
int
|
2002-07-21 07:12:17 +00:00
|
|
|
linker_add_lib (const char *libname)
|
|
|
|
{
|
2002-11-04 02:24:28 +00:00
|
|
|
pack_t *pack = 0;
|
2002-11-01 18:05:12 +00:00
|
|
|
path_t start = {path_head, "."};
|
|
|
|
path_t *path = &start;
|
2002-11-04 02:24:28 +00:00
|
|
|
const char *path_name = 0;
|
2002-07-21 07:12:17 +00:00
|
|
|
int i, j;
|
|
|
|
int did_something;
|
|
|
|
|
2002-11-01 18:05:12 +00:00
|
|
|
if (strncmp (libname, "-l", 2) == 0) {
|
|
|
|
while (path) {
|
|
|
|
path_name = va ("%s/lib%s.a", path->path, libname + 2);
|
|
|
|
pack = pack_open (path_name);
|
|
|
|
if (pack)
|
|
|
|
break;
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
if (errno)
|
|
|
|
perror (libname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
path = path->next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
path_name = libname;
|
|
|
|
pack = pack_open (path_name);
|
|
|
|
}
|
|
|
|
|
2002-10-31 22:58:59 +00:00
|
|
|
if (!pack) {
|
|
|
|
if (errno)
|
|
|
|
perror (libname);
|
2002-08-15 18:53:03 +00:00
|
|
|
return 1;
|
2002-10-31 22:58:59 +00:00
|
|
|
}
|
2002-11-01 18:05:12 +00:00
|
|
|
|
|
|
|
if (options.verbosity > 1)
|
|
|
|
puts (path_name);
|
|
|
|
|
2002-07-21 07:12:17 +00:00
|
|
|
do {
|
|
|
|
did_something = 0;
|
|
|
|
for (i = 0; i < pack->numfiles; i++) {
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *f;
|
2002-07-21 07:12:17 +00:00
|
|
|
qfo_t *qfo;
|
|
|
|
|
2002-11-01 18:05:12 +00:00
|
|
|
f = Qsubopen (path_name, pack->files[i].filepos,
|
2002-09-27 04:27:19 +00:00
|
|
|
pack->files[i].filelen, 1);
|
2002-07-21 07:12:17 +00:00
|
|
|
qfo = qfo_read (f);
|
|
|
|
Qclose (f);
|
|
|
|
|
2002-08-15 18:53:03 +00:00
|
|
|
if (!qfo)
|
|
|
|
return 1;
|
|
|
|
|
2002-07-21 07:12:17 +00:00
|
|
|
for (j = 0; j < qfo->num_defs; j++) {
|
2011-02-17 06:02:09 +00:00
|
|
|
// qfo_def_t *def = qfo->defs + j;
|
|
|
|
// if ((def->flags & QFOD_GLOBAL)
|
|
|
|
// && !(def->flags & QFOD_EXTERNAL)
|
|
|
|
// && Hash_Find (extern_defs, qfo->strings + def->name)) {
|
|
|
|
// if (options.verbosity >= 2)
|
|
|
|
// printf ("adding %s because of %s\n",
|
|
|
|
// pack->files[i].name, qfo->strings + def->name);
|
|
|
|
// linker_add_qfo (qfo);
|
|
|
|
// did_something = 1;
|
|
|
|
// break;
|
|
|
|
// }
|
2002-07-21 07:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qfo_delete (qfo);
|
|
|
|
}
|
|
|
|
} while (did_something);
|
|
|
|
pack_del (pack);
|
2002-08-15 18:53:03 +00:00
|
|
|
return 0;
|
2002-07-21 07:12:17 +00:00
|
|
|
}
|
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
static __attribute__ ((used)) void
|
2003-08-06 21:36:07 +00:00
|
|
|
undefined_def (qfo_def_t *def)
|
|
|
|
{
|
2011-02-24 03:18:35 +00:00
|
|
|
#if 0
|
2003-08-06 21:36:07 +00:00
|
|
|
qfo_def_t line_def;
|
2007-04-07 12:35:03 +00:00
|
|
|
pr_int_t i;
|
2003-08-06 21:36:07 +00:00
|
|
|
qfo_reloc_t *reloc = relocs.relocs + def->relocs;
|
|
|
|
|
|
|
|
for (i = 0; i < def->num_relocs; i++, reloc++) {
|
2003-08-21 02:21:30 +00:00
|
|
|
if ((reloc->type == rel_op_a_def
|
|
|
|
|| reloc->type == rel_op_b_def
|
|
|
|
|| reloc->type == rel_op_c_def
|
|
|
|
|| reloc->type == rel_op_a_def_ofs
|
|
|
|
|| reloc->type == rel_op_b_def_ofs
|
|
|
|
|| reloc->type == rel_op_c_def_ofs)
|
2003-08-20 22:36:02 +00:00
|
|
|
&& lines.lines) {
|
2003-08-06 21:36:07 +00:00
|
|
|
qfo_func_t *func = funcs.funcs;
|
|
|
|
qfo_func_t *best = func;
|
2011-02-17 06:02:09 +00:00
|
|
|
pr_int_t best_dist = reloc->offset - func->code;
|
2003-08-06 21:36:07 +00:00
|
|
|
pr_lineno_t *line;
|
|
|
|
|
|
|
|
while (best_dist && func - funcs.funcs < funcs.num_funcs) {
|
2011-02-17 06:02:09 +00:00
|
|
|
if (func->code <= reloc->offset) {
|
|
|
|
if (best_dist < 0 || reloc->offset - func->code < best_dist) {
|
2003-08-06 21:36:07 +00:00
|
|
|
best = func;
|
2011-02-17 06:02:09 +00:00
|
|
|
best_dist = reloc->offset - func->code;
|
2003-08-06 21:36:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func++;
|
|
|
|
}
|
|
|
|
line = lines.lines + best->line_info;
|
|
|
|
line_def.file = best->file;
|
|
|
|
line_def.line = best->line;
|
2004-07-13 19:14:01 +00:00
|
|
|
if (!line->line
|
2007-04-07 12:35:03 +00:00
|
|
|
&& line->fa.func == (pr_uint_t) (best - funcs.funcs)) {
|
2003-08-06 21:36:07 +00:00
|
|
|
while (line - lines.lines < lines.num_lines - 1 && line[1].line
|
2011-02-17 06:02:09 +00:00
|
|
|
&& line[1].fa.addr <= (pr_uint_t) reloc->offset)
|
2003-08-06 21:36:07 +00:00
|
|
|
line++;
|
|
|
|
line_def.line = line->line + best->line;
|
|
|
|
}
|
|
|
|
def_error (&line_def, "undefined symbol %s", STRING (def->name));
|
|
|
|
} else {
|
|
|
|
def_error (def, "undefined symbol %s", STRING (def->name));
|
|
|
|
}
|
|
|
|
}
|
2011-02-24 03:18:35 +00:00
|
|
|
#endif
|
2003-08-06 21:36:07 +00:00
|
|
|
}
|
|
|
|
|
2002-07-11 21:04:09 +00:00
|
|
|
qfo_t *
|
2002-07-03 20:16:32 +00:00
|
|
|
linker_finish (void)
|
|
|
|
{
|
2011-02-24 03:18:35 +00:00
|
|
|
#if 0
|
2003-02-21 22:31:40 +00:00
|
|
|
defref_t **undef_defs, **defref;
|
2002-07-11 21:04:09 +00:00
|
|
|
qfo_t *qfo;
|
|
|
|
|
2002-07-12 04:50:31 +00:00
|
|
|
if (!options.partial_link) {
|
2002-07-14 05:40:38 +00:00
|
|
|
int did_self = 0, did_this = 0;
|
|
|
|
|
2003-02-21 22:31:40 +00:00
|
|
|
undef_defs = (defref_t **) Hash_GetList (extern_defs);
|
|
|
|
for (defref = undef_defs; *defref; defref++) {
|
|
|
|
qfo_def_t *def = deref_def (*defref, &global_defs);
|
|
|
|
const char *name = STRING (def->name);
|
2002-07-14 05:40:38 +00:00
|
|
|
|
|
|
|
if (strcmp (name, ".self") == 0 && !did_self) {
|
2003-02-24 20:10:33 +00:00
|
|
|
defref_t *_d = Hash_Find (defined_defs, "self");
|
|
|
|
if (_d) {
|
2011-02-17 06:02:09 +00:00
|
|
|
// qfo_def_t *d = deref_def (_d, &global_defs);
|
|
|
|
// if (d->basic_type == ev_entity)
|
|
|
|
// def_warning (d, "@self and self used together");
|
2003-02-24 20:10:33 +00:00
|
|
|
}
|
2003-04-25 17:00:22 +00:00
|
|
|
define_def (".self", ev_entity, "E", 0, 1, 0);
|
2002-07-14 05:40:38 +00:00
|
|
|
did_self = 1;
|
|
|
|
} else if (strcmp (name, ".this") == 0 && !did_this) {
|
2002-07-16 21:59:47 +00:00
|
|
|
entity_base = 0;
|
2003-04-25 17:00:22 +00:00
|
|
|
define_def (".this", ev_field, "F@", QFOD_NOSAVE, 1,
|
|
|
|
entity->size);
|
2011-01-17 13:33:33 +00:00
|
|
|
defspace_add_data (entity, 0, 1);
|
2002-07-14 05:40:38 +00:00
|
|
|
did_this = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (undef_defs);
|
2003-02-21 22:31:40 +00:00
|
|
|
undef_defs = (defref_t **) Hash_GetList (extern_defs);
|
|
|
|
for (defref = undef_defs; *defref; defref++) {
|
|
|
|
qfo_def_t *def = deref_def (*defref, &global_defs);
|
2003-08-06 21:36:07 +00:00
|
|
|
undefined_def (def);
|
2002-07-12 04:50:31 +00:00
|
|
|
}
|
2002-07-14 05:40:38 +00:00
|
|
|
free (undef_defs);
|
2002-07-12 04:50:31 +00:00
|
|
|
if (pr.error_count)
|
|
|
|
return 0;
|
2002-07-03 21:32:03 +00:00
|
|
|
}
|
2002-07-11 21:04:09 +00:00
|
|
|
|
2002-07-12 20:26:51 +00:00
|
|
|
merge_defgroups ();
|
|
|
|
|
2002-07-13 02:24:06 +00:00
|
|
|
fixup_relocs ();
|
2002-07-12 20:26:51 +00:00
|
|
|
|
2002-07-11 21:04:09 +00:00
|
|
|
qfo = qfo_new ();
|
|
|
|
qfo_add_code (qfo, code->code, code->size);
|
|
|
|
qfo_add_data (qfo, data->data, data->size);
|
|
|
|
qfo_add_far_data (qfo, far_data->data, far_data->size);
|
|
|
|
qfo_add_strings (qfo, strings->strings, strings->size);
|
2002-07-16 06:40:34 +00:00
|
|
|
qfo_add_relocs (qfo, final_relocs.relocs, final_relocs.num_relocs);
|
2002-07-11 21:04:09 +00:00
|
|
|
qfo_add_defs (qfo, defs.defs, defs.num_defs);
|
2002-07-15 21:37:52 +00:00
|
|
|
qfo_add_funcs (qfo, funcs.funcs, funcs.num_funcs);
|
2002-07-11 21:04:09 +00:00
|
|
|
qfo_add_lines (qfo, lines.lines, lines.num_lines);
|
|
|
|
qfo_add_types (qfo, type_strings->strings, type_strings->size);
|
2011-02-17 06:02:09 +00:00
|
|
|
// qfo->entity_fields = entity->size;
|
2002-07-11 21:04:09 +00:00
|
|
|
return qfo;
|
2011-02-24 03:18:35 +00:00
|
|
|
#endif
|
|
|
|
return work;
|
2002-07-03 20:16:32 +00:00
|
|
|
}
|
2002-11-01 18:05:12 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
linker_add_path (const char *path)
|
|
|
|
{
|
|
|
|
path_t *p = malloc (sizeof (path_t));
|
|
|
|
p->next = 0;
|
|
|
|
p->path = path;
|
|
|
|
*path_tail = p;
|
|
|
|
path_tail = &p->next;
|
|
|
|
}
|
2003-05-07 15:00:17 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
def_error (qfo_def_t *def, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
static dstring_t *string;
|
|
|
|
|
|
|
|
if (!string)
|
|
|
|
string = dstring_new ();
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
dvsprintf (string, fmt, args);
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
pr.source_file = def->file;
|
|
|
|
pr.source_line = def->line;
|
2011-02-24 03:18:35 +00:00
|
|
|
// pr.strings = strings;
|
2003-05-07 15:00:17 +00:00
|
|
|
error (0, "%s", string->str);
|
|
|
|
}
|
|
|
|
|
2011-02-24 03:18:35 +00:00
|
|
|
static void
|
|
|
|
def_warning (qfo_def_t *def, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
static dstring_t *string;
|
|
|
|
|
|
|
|
if (!string)
|
|
|
|
string = dstring_new ();
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
dvsprintf (string, fmt, args);
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
pr.source_file = def->file;
|
|
|
|
pr.source_line = def->line;
|
2011-02-17 06:02:09 +00:00
|
|
|
// pr.strings = strings;
|
2011-02-24 03:18:35 +00:00
|
|
|
warning (0, "%s", string->str);
|
|
|
|
}
|