2002-05-13 19:37:36 +00:00
|
|
|
/*
|
2011-01-24 23:54:02 +00:00
|
|
|
qfprogs.c
|
2002-05-13 19:37:36 +00:00
|
|
|
|
2011-01-24 23:54:02 +00:00
|
|
|
Progs dumping, main file.
|
2002-05-13 19:37:36 +00:00
|
|
|
|
2011-01-24 23:54:02 +00:00
|
|
|
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
|
2002-05-13 19:37:36 +00:00
|
|
|
|
2011-01-24 23:54:02 +00:00
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2002/05/13
|
2002-05-13 19:37:36 +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
|
|
|
|
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
# include <io.h>
|
|
|
|
#endif
|
2003-03-12 22:31:44 +00:00
|
|
|
#include <string.h>
|
2002-05-16 23:44:53 +00:00
|
|
|
#include <getopt.h>
|
2002-05-13 19:37:36 +00:00
|
|
|
#include <sys/types.h>
|
2004-01-20 05:57:39 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
# include <fcntl.h>
|
|
|
|
#else
|
|
|
|
# include <sys/fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2002-08-09 04:06:17 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2002-05-13 19:37:36 +00:00
|
|
|
|
|
|
|
#include "QF/cmd.h"
|
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/hash.h"
|
2012-05-06 12:35:42 +00:00
|
|
|
#include "QF/mathlib.h"
|
2002-05-13 19:37:36 +00:00
|
|
|
#include "QF/pr_comp.h"
|
|
|
|
#include "QF/progs.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakeio.h"
|
2002-05-13 19:37:36 +00:00
|
|
|
#include "QF/sys.h"
|
2002-06-12 03:01:30 +00:00
|
|
|
#include "QF/va.h"
|
2002-05-13 19:37:36 +00:00
|
|
|
#include "QF/zone.h"
|
|
|
|
|
2004-01-29 07:43:05 +00:00
|
|
|
#include "obj_file.h"
|
2011-02-26 15:36:45 +00:00
|
|
|
#include "obj_type.h"
|
2003-01-06 18:28:13 +00:00
|
|
|
#include "qfprogs.h"
|
2004-01-30 07:17:28 +00:00
|
|
|
#include "reloc.h"
|
|
|
|
|
|
|
|
const char *reloc_names[] = {
|
|
|
|
"none",
|
|
|
|
"op_a_def",
|
|
|
|
"op_b_def",
|
|
|
|
"op_c_def",
|
|
|
|
"op_a_op",
|
|
|
|
"op_b_op",
|
|
|
|
"op_c_op",
|
|
|
|
"def_op",
|
|
|
|
"def_def",
|
|
|
|
"def_func",
|
|
|
|
"def_string",
|
|
|
|
"def_field",
|
|
|
|
"op_a_def_ofs",
|
|
|
|
"op_b_def_ofs",
|
|
|
|
"op_c_def_ofs",
|
|
|
|
"def_def_ofs",
|
2011-03-06 06:33:10 +00:00
|
|
|
"def_field_ofs",
|
2004-01-30 07:17:28 +00:00
|
|
|
};
|
2002-05-16 23:44:53 +00:00
|
|
|
|
2003-04-23 20:10:51 +00:00
|
|
|
int sorted = 0;
|
|
|
|
int verbosity = 0;
|
|
|
|
|
2002-05-16 23:44:53 +00:00
|
|
|
static const struct option long_options[] = {
|
|
|
|
{"disassemble", no_argument, 0, 'd'},
|
2002-06-09 16:28:19 +00:00
|
|
|
{"fields", no_argument, 0, 'f'},
|
|
|
|
{"functions", no_argument, 0, 'F'},
|
2010-12-24 08:08:48 +00:00
|
|
|
{"globals", no_argument, 0, 'g'},
|
2010-12-24 08:33:57 +00:00
|
|
|
{"help", no_argument, 0, 'h'},
|
2002-09-07 06:47:16 +00:00
|
|
|
{"lines", no_argument, 0, 'l'},
|
2002-08-20 14:31:47 +00:00
|
|
|
{"modules", no_argument, 0, 'M'},
|
2010-12-24 08:08:48 +00:00
|
|
|
{"numeric", no_argument, 0, 'n'},
|
2003-03-12 22:31:44 +00:00
|
|
|
{"path", required_argument, 0, 'P'},
|
2010-12-24 08:08:48 +00:00
|
|
|
{"relocs", no_argument, 0, 'r'},
|
|
|
|
{"strings", no_argument, 0, 's'},
|
2012-11-07 11:32:14 +00:00
|
|
|
{"types", no_argument, 0, 't'},
|
2002-10-16 17:31:15 +00:00
|
|
|
{"verbose", no_argument, 0, 'v'},
|
2002-05-16 23:44:53 +00:00
|
|
|
{NULL, 0, NULL, 0},
|
|
|
|
};
|
2002-05-13 19:37:36 +00:00
|
|
|
|
2010-12-24 08:08:48 +00:00
|
|
|
static const char *short_options =
|
2010-12-24 08:33:57 +00:00
|
|
|
"d" // disassemble
|
|
|
|
"F" // functions
|
|
|
|
"f" // fields
|
|
|
|
"g" // globals
|
|
|
|
"h" // help
|
|
|
|
"l" // lines
|
|
|
|
"M" // modules
|
|
|
|
"n" // numeric
|
|
|
|
"P:" // path
|
|
|
|
"r" // relocs
|
|
|
|
"s" // strings
|
2012-11-07 11:32:14 +00:00
|
|
|
"t" // types
|
2010-12-24 08:33:57 +00:00
|
|
|
"v" // verbose
|
2010-12-24 08:08:48 +00:00
|
|
|
;
|
|
|
|
|
2002-05-13 19:37:36 +00:00
|
|
|
static edict_t *edicts;
|
|
|
|
static int num_edicts;
|
|
|
|
static int reserved_edicts = 1;
|
|
|
|
static progs_t pr;
|
2004-01-29 07:43:05 +00:00
|
|
|
static qfo_t *qfo;
|
|
|
|
|
2003-03-12 22:31:44 +00:00
|
|
|
static const char *source_path = "";
|
2002-10-16 17:31:15 +00:00
|
|
|
|
2002-05-13 19:37:36 +00:00
|
|
|
static hashtab_t *func_tab;
|
|
|
|
|
2010-12-24 08:33:57 +00:00
|
|
|
static void __attribute__((noreturn))
|
|
|
|
usage (int status)
|
|
|
|
{
|
|
|
|
printf ("%s - QuakeForge progs utility\n", "qfprogs");
|
|
|
|
printf ("Usage: %s [options] [files]\n", "qfprogs");
|
|
|
|
printf (
|
|
|
|
" -d, --disassemble Dump code disassembly.\n"
|
2011-01-06 09:48:54 +00:00
|
|
|
" -f, --fields Dump entity fields.\n"
|
|
|
|
" -F, --functions Dump functions.\n"
|
2010-12-24 08:33:57 +00:00
|
|
|
" -g, --globals Dump global variables.\n"
|
|
|
|
" -h, --help Display this help and exit\n"
|
|
|
|
" -l, --lines Dump line number information.\n"
|
|
|
|
" -M, --modules Dump Objective-QuakeC data.\n"
|
|
|
|
" -n, --numeric Sort globals by address.\n"
|
|
|
|
" -P, --path DIR Source path.\n"
|
|
|
|
" -r, --relocs Dump reloc information.\n"
|
|
|
|
" -s, --strings Dump static strings.\n"
|
2012-11-07 11:32:14 +00:00
|
|
|
" -t, --types Dump type encodings.\n"
|
2010-12-24 08:33:57 +00:00
|
|
|
" -v, --verbose Display more output than usual.\n"
|
|
|
|
);
|
|
|
|
exit (status);
|
|
|
|
}
|
|
|
|
|
2002-08-27 07:16:28 +00:00
|
|
|
static QFile *
|
2002-06-11 17:24:37 +00:00
|
|
|
open_file (const char *path, int *len)
|
|
|
|
{
|
2002-09-27 04:27:19 +00:00
|
|
|
QFile *file = Qopen (path, "rbz");
|
2002-06-11 17:24:37 +00:00
|
|
|
|
2003-01-29 20:32:44 +00:00
|
|
|
if (!file)
|
2002-06-11 17:24:37 +00:00
|
|
|
return 0;
|
2002-09-27 04:27:19 +00:00
|
|
|
*len = Qfilesize (file);
|
|
|
|
return file;
|
2002-06-11 17:24:37 +00:00
|
|
|
}
|
|
|
|
|
2003-03-12 22:31:44 +00:00
|
|
|
static void
|
|
|
|
file_error (progs_t *pr, const char *name)
|
|
|
|
{
|
|
|
|
perror (name);
|
|
|
|
}
|
|
|
|
|
2002-06-11 17:24:37 +00:00
|
|
|
static void *
|
2020-02-21 12:17:28 +00:00
|
|
|
load_file (progs_t *pr, const char *name, off_t *_size)
|
2002-06-11 17:24:37 +00:00
|
|
|
{
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *file;
|
2002-06-11 17:24:37 +00:00
|
|
|
int size;
|
2004-11-02 07:15:00 +00:00
|
|
|
char *sym;
|
2002-06-11 17:24:37 +00:00
|
|
|
|
|
|
|
file = open_file (name, &size);
|
|
|
|
if (!file) {
|
2002-06-12 03:01:30 +00:00
|
|
|
file = open_file (va ("%s.gz", name), &size);
|
2003-03-12 22:31:44 +00:00
|
|
|
if (!file)
|
2002-06-12 03:01:30 +00:00
|
|
|
return 0;
|
2002-06-11 17:24:37 +00:00
|
|
|
}
|
2004-11-02 07:15:00 +00:00
|
|
|
sym = malloc (size + 1);
|
|
|
|
sym[size] = 0;
|
2002-06-11 17:24:37 +00:00
|
|
|
Qread (file, sym, size);
|
2020-02-21 12:17:28 +00:00
|
|
|
*_size = size;
|
2002-06-11 17:24:37 +00:00
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2002-05-13 19:37:36 +00:00
|
|
|
static void *
|
|
|
|
allocate_progs_mem (progs_t *pr, int size)
|
|
|
|
{
|
|
|
|
return malloc (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_progs_mem (progs_t *pr, void *mem)
|
|
|
|
{
|
|
|
|
free (mem);
|
|
|
|
}
|
|
|
|
|
2007-04-04 11:22:48 +00:00
|
|
|
static uintptr_t
|
2012-07-18 13:34:37 +00:00
|
|
|
func_hash (const void *func, void *unused)
|
2002-05-13 19:37:36 +00:00
|
|
|
{
|
|
|
|
return ((dfunction_t *) func)->first_statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-07-18 13:34:37 +00:00
|
|
|
func_compare (const void *f1, const void *f2, void *unused)
|
2002-05-13 19:37:36 +00:00
|
|
|
{
|
|
|
|
return ((dfunction_t *) f1)->first_statement
|
|
|
|
== ((dfunction_t *) f2)->first_statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
dfunction_t *
|
|
|
|
func_find (int st_ofs)
|
|
|
|
{
|
|
|
|
dfunction_t f;
|
|
|
|
|
|
|
|
f.first_statement = st_ofs;
|
|
|
|
return Hash_FindElement (func_tab, &f);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_qf (void)
|
|
|
|
{
|
2011-09-11 05:56:47 +00:00
|
|
|
Sys_Init ();
|
2002-05-13 19:37:36 +00:00
|
|
|
|
2020-02-23 05:39:02 +00:00
|
|
|
Cvar_Get ("pr_debug", va ("%d", 1+verbosity), 0, 0, "");
|
2003-03-12 22:31:44 +00:00
|
|
|
Cvar_Get ("pr_source_path", source_path, 0, 0, "");
|
2002-05-13 19:37:36 +00:00
|
|
|
PR_Init_Cvars ();
|
|
|
|
|
|
|
|
pr.edicts = &edicts;
|
|
|
|
pr.num_edicts = &num_edicts;
|
|
|
|
pr.reserved_edicts = &reserved_edicts;
|
2003-03-12 22:31:44 +00:00
|
|
|
pr.file_error = file_error;
|
2002-06-11 17:24:37 +00:00
|
|
|
pr.load_file = load_file;
|
2002-05-13 19:37:36 +00:00
|
|
|
pr.allocate_progs_mem = allocate_progs_mem;
|
|
|
|
pr.free_progs_mem = free_progs_mem;
|
|
|
|
|
2020-02-23 06:56:36 +00:00
|
|
|
PR_Init (&pr);
|
|
|
|
|
2002-05-13 19:37:36 +00:00
|
|
|
func_tab = Hash_NewTable (1021, 0, 0, 0);
|
|
|
|
Hash_SetHashCompare (func_tab, func_hash, func_compare);
|
|
|
|
}
|
2011-02-26 15:36:45 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static int
|
2002-05-13 19:37:36 +00:00
|
|
|
load_progs (const char *name)
|
|
|
|
{
|
2002-08-27 07:16:28 +00:00
|
|
|
QFile *file;
|
2020-02-25 12:23:13 +00:00
|
|
|
int size;
|
|
|
|
pr_uint_t i;
|
2004-01-29 07:43:05 +00:00
|
|
|
char buff[5];
|
|
|
|
|
|
|
|
Hash_FlushTable (func_tab);
|
2002-05-13 19:37:36 +00:00
|
|
|
|
|
|
|
file = open_file (name, &size);
|
|
|
|
if (!file) {
|
|
|
|
perror (name);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-01-29 07:43:05 +00:00
|
|
|
Qread (file, buff, 4);
|
|
|
|
buff[4] = 0;
|
|
|
|
Qseek (file, 0, SEEK_SET);
|
2020-02-23 05:39:02 +00:00
|
|
|
qfo = 0;
|
2004-01-29 07:43:05 +00:00
|
|
|
if (!strcmp (buff, QFO)) {
|
|
|
|
qfo = qfo_read (file);
|
|
|
|
Qclose (file);
|
|
|
|
|
|
|
|
if (!qfo)
|
|
|
|
return 0;
|
2003-01-29 20:32:44 +00:00
|
|
|
|
2020-02-23 05:39:02 +00:00
|
|
|
return 1;
|
2004-01-29 07:43:05 +00:00
|
|
|
} else {
|
|
|
|
pr.progs_name = name;
|
2018-10-11 01:06:48 +00:00
|
|
|
pr.max_edicts = 1;
|
|
|
|
pr.zone_size = 0;
|
|
|
|
PR_LoadProgsFile (&pr, file, size);
|
2004-01-29 07:43:05 +00:00
|
|
|
Qclose (file);
|
|
|
|
|
|
|
|
if (!pr.progs)
|
|
|
|
return 0;
|
2003-01-29 20:32:44 +00:00
|
|
|
|
2007-04-09 06:16:34 +00:00
|
|
|
PR_ResolveGlobals (&pr);
|
2004-01-29 07:43:05 +00:00
|
|
|
PR_LoadDebug (&pr);
|
|
|
|
}
|
2002-05-13 19:37:36 +00:00
|
|
|
for (i = 0; i < pr.progs->numfunctions; i++) {
|
2004-01-29 07:43:05 +00:00
|
|
|
// don't bother with builtins
|
|
|
|
if (pr.pr_functions[i].first_statement > 0)
|
2002-05-13 19:37:36 +00:00
|
|
|
Hash_AddElement (func_tab, &pr.pr_functions[i]);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-01-30 07:17:28 +00:00
|
|
|
typedef struct {
|
|
|
|
void (*progs) (progs_t *pr);
|
|
|
|
void (*qfo) (qfo_t *qfo);
|
|
|
|
} operation_t;
|
|
|
|
|
|
|
|
operation_t operations[] = {
|
2007-04-09 13:33:59 +00:00
|
|
|
{disassemble_progs, 0}, // disassemble
|
2007-04-09 07:47:51 +00:00
|
|
|
{dump_globals, qfo_globals}, // globals
|
2020-02-23 06:08:31 +00:00
|
|
|
{dump_strings, qfo_strings}, // strings
|
2020-02-23 05:56:50 +00:00
|
|
|
{dump_fields, qfo_fields}, // fields
|
2008-08-01 13:54:24 +00:00
|
|
|
{dump_functions, qfo_functions}, // functions
|
2020-02-23 05:39:02 +00:00
|
|
|
{dump_lines, qfo_lines}, // lines
|
2007-04-09 13:33:59 +00:00
|
|
|
{dump_modules, 0}, // modules
|
|
|
|
{0, qfo_relocs}, // relocs
|
2012-11-15 02:58:13 +00:00
|
|
|
{dump_types, qfo_types}, // types
|
2004-01-30 07:17:28 +00:00
|
|
|
};
|
|
|
|
|
2002-05-13 19:37:36 +00:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2002-05-16 23:44:53 +00:00
|
|
|
int c;
|
2004-01-30 07:17:28 +00:00
|
|
|
operation_t *func = &operations[0];
|
2002-05-16 23:44:53 +00:00
|
|
|
|
2010-12-24 08:08:48 +00:00
|
|
|
while ((c = getopt_long (argc, argv, short_options,
|
|
|
|
long_options, 0)) != EOF) {
|
2002-05-16 23:44:53 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
2004-01-30 07:17:28 +00:00
|
|
|
func = &operations[0];
|
2002-05-16 23:44:53 +00:00
|
|
|
break;
|
2010-12-24 08:35:33 +00:00
|
|
|
case 'F':
|
|
|
|
func = &operations[4];
|
2002-05-16 23:44:53 +00:00
|
|
|
break;
|
2002-06-09 16:28:19 +00:00
|
|
|
case 'f':
|
2004-01-30 07:17:28 +00:00
|
|
|
func = &operations[3];
|
2002-06-09 16:28:19 +00:00
|
|
|
break;
|
2010-12-24 08:35:33 +00:00
|
|
|
case 'g':
|
|
|
|
func = &operations[1];
|
2002-06-09 16:28:19 +00:00
|
|
|
break;
|
2010-12-24 08:35:33 +00:00
|
|
|
case 'h':
|
|
|
|
usage (0);
|
2002-09-07 06:47:16 +00:00
|
|
|
case 'l':
|
2004-01-30 07:17:28 +00:00
|
|
|
func = &operations[5];
|
2002-09-07 06:47:16 +00:00
|
|
|
break;
|
2002-08-20 14:31:47 +00:00
|
|
|
case 'M':
|
2004-01-30 07:17:28 +00:00
|
|
|
func = &operations[6];
|
2002-08-20 14:31:47 +00:00
|
|
|
break;
|
2010-12-24 08:35:33 +00:00
|
|
|
case 'n':
|
|
|
|
sorted = 1;
|
2007-04-09 13:33:59 +00:00
|
|
|
break;
|
2003-03-12 22:31:44 +00:00
|
|
|
case 'P':
|
|
|
|
source_path = strdup (optarg);
|
2003-03-13 17:31:01 +00:00
|
|
|
break;
|
2010-12-24 08:35:33 +00:00
|
|
|
case 'r':
|
|
|
|
func = &operations[7];
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
func = &operations[2];
|
|
|
|
break;
|
2012-11-07 11:32:14 +00:00
|
|
|
case 't':
|
|
|
|
func = &operations[8];
|
|
|
|
break;
|
2002-10-16 17:31:15 +00:00
|
|
|
case 'v':
|
|
|
|
verbosity++;
|
|
|
|
break;
|
2002-05-16 23:44:53 +00:00
|
|
|
default:
|
2010-12-24 08:33:57 +00:00
|
|
|
usage (1);
|
2002-05-16 23:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-16 17:31:15 +00:00
|
|
|
init_qf ();
|
2002-05-16 23:44:53 +00:00
|
|
|
while (optind < argc) {
|
2003-01-29 20:32:44 +00:00
|
|
|
if (!load_progs (argv[optind++]))
|
|
|
|
return 1;
|
2004-01-30 07:17:28 +00:00
|
|
|
if (qfo && func->qfo)
|
|
|
|
func->qfo (qfo);
|
2020-02-23 05:39:02 +00:00
|
|
|
else if (!qfo && func->progs)
|
2004-01-30 07:17:28 +00:00
|
|
|
func->progs (&pr);
|
2007-04-09 13:33:59 +00:00
|
|
|
else
|
|
|
|
fprintf (stderr, "can't process %s\n", argv[optind - 1]);
|
2002-05-13 19:37:36 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|