From 87f252aaa53f8ce5305d84c95a8751592f647dc2 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Wed, 19 Sep 2007 21:40:37 -0700 Subject: [PATCH] Make nasm_malloc() et al available from inside ndisasm Clean up nasmlib to remove functions irrelevant for ndisasm; make nasm_malloc() etc usable inside ndisasm. --- Makefile.in | 27 +++++----- Mkfiles/msvc.mak | 27 +++++----- exprlib.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ nasm.h | 55 +++++--------------- nasmlib.c | 146 ---------------------------------------------------- nasmlib.h | 58 +++++++++++++++------ ndisasm.c | 14 +++++ 7 files changed, 253 insertions(+), 228 deletions(-) create mode 100644 exprlib.c diff --git a/Makefile.in b/Makefile.in index 0dc6aed..1a43f55 100644 --- a/Makefile.in +++ b/Makefile.in @@ -58,7 +58,7 @@ NASM = nasm.$(O) nasmlib.$(O) float.$(O) insnsa.$(O) assemble.$(O) \ output/outobj.$(O) output/outas86.$(O) output/outrdf2.$(O) \ output/outdbg.$(O) output/outieee.$(O) output/outmacho.$(O) \ preproc.$(O) pptok.$(O) \ - listing.$(O) eval.$(O) stdscan.$(O) tokhash.$(O) + listing.$(O) eval.$(O) exprlib.$(O) stdscan.$(O) tokhash.$(O) NDISASM = ndisasm.$(O) disasm.$(O) sync.$(O) nasmlib.$(O) insnsd.$(O) @@ -211,16 +211,19 @@ assemble.$(O): assemble.c assemble.h compiler.h config.h insns.h insnsi.h \ nasm.h nasmlib.h pptok.h preproc.h regflags.c regs.h regvals.c version.h crc64.$(O): crc64.c disasm.$(O): disasm.c compiler.h config.h disasm.h insns.h insnsi.h insnsn.c \ - names.c nasm.h regdis.c regs.c regs.h sync.h version.h + names.c nasm.h nasmlib.h regdis.c regs.c regs.h sync.h version.h eval.$(O): eval.c compiler.h config.h eval.h insnsi.h labels.h nasm.h \ nasmlib.h regs.h version.h -float.$(O): float.c compiler.h config.h insnsi.h nasm.h regs.h version.h -hashtbl.$(O): hashtbl.c compiler.h config.h hashtbl.h insnsi.h nasm.h \ - nasmlib.h regs.h version.h -insnsa.$(O): insnsa.c compiler.h config.h insns.h insnsi.h nasm.h regs.h \ +exprlib.$(O): exprlib.c compiler.h config.h insnsi.h nasm.h nasmlib.h regs.h \ version.h -insnsd.$(O): insnsd.c compiler.h config.h insns.h insnsi.h nasm.h regs.h \ +float.$(O): float.c compiler.h config.h insnsi.h nasm.h nasmlib.h regs.h \ version.h +hashtbl.$(O): hashtbl.c compiler.h config.h hashtbl.h insnsi.h nasm.h \ + nasmlib.h regs.h version.h +insnsa.$(O): insnsa.c compiler.h config.h insns.h insnsi.h nasm.h nasmlib.h \ + regs.h version.h +insnsd.$(O): insnsd.c compiler.h config.h insns.h insnsi.h nasm.h nasmlib.h \ + regs.h version.h insnsn.$(O): insnsn.c labels.$(O): labels.c compiler.h config.h hashtbl.h insnsi.h nasm.h \ nasmlib.h regs.h version.h @@ -235,8 +238,8 @@ nasmlib.$(O): nasmlib.c compiler.h config.h insns.h insnsi.h nasm.h \ nasmlib.h regs.h version.h ndisasm.$(O): ndisasm.c compiler.h config.h disasm.h insns.h insnsi.h nasm.h \ nasmlib.h regs.h sync.h version.h -outform.$(O): outform.c compiler.h config.h insnsi.h nasm.h outform.h regs.h \ - version.h +outform.$(O): outform.c compiler.h config.h insnsi.h nasm.h nasmlib.h \ + outform.h regs.h version.h output/outaout.$(O): output/outaout.c compiler.h config.h insnsi.h nasm.h \ nasmlib.h outform.h regs.h stdscan.h version.h output/outas86.$(O): output/outas86.c compiler.h config.h insnsi.h nasm.h \ @@ -272,6 +275,6 @@ regs.$(O): regs.c regvals.$(O): regvals.c stdscan.$(O): stdscan.c compiler.h config.h insns.h insnsi.h nasm.h \ nasmlib.h regs.h stdscan.h version.h -sync.$(O): sync.c sync.h -tokhash.$(O): tokhash.c compiler.h config.h insns.h insnsi.h nasm.h regs.h \ - version.h +sync.$(O): sync.c compiler.h config.h nasmlib.h sync.h +tokhash.$(O): tokhash.c compiler.h config.h insns.h insnsi.h nasm.h \ + nasmlib.h regs.h version.h diff --git a/Mkfiles/msvc.mak b/Mkfiles/msvc.mak index d70973d..96b5449 100644 --- a/Mkfiles/msvc.mak +++ b/Mkfiles/msvc.mak @@ -38,7 +38,7 @@ NASM = nasm.$(O) nasmlib.$(O) float.$(O) insnsa.$(O) assemble.$(O) \ output/outobj.$(O) output/outas86.$(O) output/outrdf2.$(O) \ output/outdbg.$(O) output/outieee.$(O) output/outmacho.$(O) \ preproc.$(O) pptok.$(O) \ - listing.$(O) eval.$(O) stdscan.$(O) tokhash.$(O) + listing.$(O) eval.$(O) exprlib.$(O) stdscan.$(O) tokhash.$(O) NDISASM = ndisasm.$(O) disasm.$(O) sync.$(O) nasmlib.$(O) insnsd.$(O) @@ -169,16 +169,19 @@ assemble.$(O): assemble.c assemble.h compiler.h config.h insns.h insnsi.h \ nasm.h nasmlib.h pptok.h preproc.h regflags.c regs.h regvals.c version.h crc64.$(O): crc64.c disasm.$(O): disasm.c compiler.h config.h disasm.h insns.h insnsi.h insnsn.c \ - names.c nasm.h regdis.c regs.c regs.h sync.h version.h + names.c nasm.h nasmlib.h regdis.c regs.c regs.h sync.h version.h eval.$(O): eval.c compiler.h config.h eval.h insnsi.h labels.h nasm.h \ nasmlib.h regs.h version.h -float.$(O): float.c compiler.h config.h insnsi.h nasm.h regs.h version.h -hashtbl.$(O): hashtbl.c compiler.h config.h hashtbl.h insnsi.h nasm.h \ - nasmlib.h regs.h version.h -insnsa.$(O): insnsa.c compiler.h config.h insns.h insnsi.h nasm.h regs.h \ +exprlib.$(O): exprlib.c compiler.h config.h insnsi.h nasm.h nasmlib.h regs.h \ version.h -insnsd.$(O): insnsd.c compiler.h config.h insns.h insnsi.h nasm.h regs.h \ +float.$(O): float.c compiler.h config.h insnsi.h nasm.h nasmlib.h regs.h \ version.h +hashtbl.$(O): hashtbl.c compiler.h config.h hashtbl.h insnsi.h nasm.h \ + nasmlib.h regs.h version.h +insnsa.$(O): insnsa.c compiler.h config.h insns.h insnsi.h nasm.h nasmlib.h \ + regs.h version.h +insnsd.$(O): insnsd.c compiler.h config.h insns.h insnsi.h nasm.h nasmlib.h \ + regs.h version.h insnsn.$(O): insnsn.c labels.$(O): labels.c compiler.h config.h hashtbl.h insnsi.h nasm.h \ nasmlib.h regs.h version.h @@ -193,8 +196,8 @@ nasmlib.$(O): nasmlib.c compiler.h config.h insns.h insnsi.h nasm.h \ nasmlib.h regs.h version.h ndisasm.$(O): ndisasm.c compiler.h config.h disasm.h insns.h insnsi.h nasm.h \ nasmlib.h regs.h sync.h version.h -outform.$(O): outform.c compiler.h config.h insnsi.h nasm.h outform.h regs.h \ - version.h +outform.$(O): outform.c compiler.h config.h insnsi.h nasm.h nasmlib.h \ + outform.h regs.h version.h output/outaout.$(O): output/outaout.c compiler.h config.h insnsi.h nasm.h \ nasmlib.h outform.h regs.h stdscan.h version.h output/outas86.$(O): output/outas86.c compiler.h config.h insnsi.h nasm.h \ @@ -230,6 +233,6 @@ regs.$(O): regs.c regvals.$(O): regvals.c stdscan.$(O): stdscan.c compiler.h config.h insns.h insnsi.h nasm.h \ nasmlib.h regs.h stdscan.h version.h -sync.$(O): sync.c sync.h -tokhash.$(O): tokhash.c compiler.h config.h insns.h insnsi.h nasm.h regs.h \ - version.h +sync.$(O): sync.c compiler.h config.h nasmlib.h sync.h +tokhash.$(O): tokhash.c compiler.h config.h insns.h insnsi.h nasm.h \ + nasmlib.h regs.h version.h diff --git a/exprlib.c b/exprlib.c new file mode 100644 index 0000000..2f03ff0 --- /dev/null +++ b/exprlib.c @@ -0,0 +1,154 @@ +/* + * exprlib.c + * + * Library routines to manipulate expression data types. + */ + +#include "nasm.h" + +/* + * Return TRUE if the argument is a simple scalar. (Or a far- + * absolute, which counts.) + */ +int is_simple(expr * vect) +{ + while (vect->type && !vect->value) + vect++; + if (!vect->type) + return 1; + if (vect->type != EXPR_SIMPLE) + return 0; + do { + vect++; + } while (vect->type && !vect->value); + if (vect->type && vect->type < EXPR_SEGBASE + SEG_ABS) + return 0; + return 1; +} + +/* + * Return TRUE if the argument is a simple scalar, _NOT_ a far- + * absolute. + */ +int is_really_simple(expr * vect) +{ + while (vect->type && !vect->value) + vect++; + if (!vect->type) + return 1; + if (vect->type != EXPR_SIMPLE) + return 0; + do { + vect++; + } while (vect->type && !vect->value); + if (vect->type) + return 0; + return 1; +} + +/* + * Return TRUE if the argument is relocatable (i.e. a simple + * scalar, plus at most one segment-base, plus possibly a WRT). + */ +int is_reloc(expr * vect) +{ + while (vect->type && !vect->value) /* skip initial value-0 terms */ + vect++; + if (!vect->type) /* trivially return TRUE if nothing */ + return 1; /* is present apart from value-0s */ + if (vect->type < EXPR_SIMPLE) /* FALSE if a register is present */ + return 0; + if (vect->type == EXPR_SIMPLE) { /* skip over a pure number term... */ + do { + vect++; + } while (vect->type && !vect->value); + if (!vect->type) /* ...returning TRUE if that's all */ + return 1; + } + if (vect->type == EXPR_WRT) { /* skip over a WRT term... */ + do { + vect++; + } while (vect->type && !vect->value); + if (!vect->type) /* ...returning TRUE if that's all */ + return 1; + } + if (vect->value != 0 && vect->value != 1) + return 0; /* segment base multiplier non-unity */ + do { /* skip over _one_ seg-base term... */ + vect++; + } while (vect->type && !vect->value); + if (!vect->type) /* ...returning TRUE if that's all */ + return 1; + return 0; /* And return FALSE if there's more */ +} + +/* + * Return TRUE if the argument contains an `unknown' part. + */ +int is_unknown(expr * vect) +{ + while (vect->type && vect->type < EXPR_UNKNOWN) + vect++; + return (vect->type == EXPR_UNKNOWN); +} + +/* + * Return TRUE if the argument contains nothing but an `unknown' + * part. + */ +int is_just_unknown(expr * vect) +{ + while (vect->type && !vect->value) + vect++; + return (vect->type == EXPR_UNKNOWN); +} + +/* + * Return the scalar part of a relocatable vector. (Including + * simple scalar vectors - those qualify as relocatable.) + */ +int64_t reloc_value(expr * vect) +{ + while (vect->type && !vect->value) + vect++; + if (!vect->type) + return 0; + if (vect->type == EXPR_SIMPLE) + return vect->value; + else + return 0; +} + +/* + * Return the segment number of a relocatable vector, or NO_SEG for + * simple scalars. + */ +int32_t reloc_seg(expr * vect) +{ + while (vect->type && (vect->type == EXPR_WRT || !vect->value)) + vect++; + if (vect->type == EXPR_SIMPLE) { + do { + vect++; + } while (vect->type && (vect->type == EXPR_WRT || !vect->value)); + } + if (!vect->type) + return NO_SEG; + else + return vect->type - EXPR_SEGBASE; +} + +/* + * Return the WRT segment number of a relocatable vector, or NO_SEG + * if no WRT part is present. + */ +int32_t reloc_wrt(expr * vect) +{ + while (vect->type && vect->type < EXPR_WRT) + vect++; + if (vect->type == EXPR_WRT) { + return vect->value; + } else + return NO_SEG; +} + diff --git a/nasm.h b/nasm.h index f4afad3..a7c26c8 100644 --- a/nasm.h +++ b/nasm.h @@ -15,6 +15,7 @@ #include #include "version.h" /* generated NASM version macros */ #include "compiler.h" +#include "nasmlib.h" #include "insnsi.h" /* For enum opcode */ #ifndef NULL @@ -61,48 +62,6 @@ struct ofmt; /* - * ------------------------- - * Error reporting functions - * ------------------------- - */ - -/* - * An error reporting function should look like this. - */ -typedef void (*efunc) (int severity, const char *fmt, ...); - -/* - * These are the error severity codes which get passed as the first - * argument to an efunc. - */ - -#define ERR_DEBUG 0x00000008 /* put out debugging message */ -#define ERR_WARNING 0x00000000 /* warn only: no further action */ -#define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */ -#define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */ -#define ERR_PANIC 0x00000003 /* internal error: panic instantly - * and dump core for reference */ -#define ERR_MASK 0x0000000F /* mask off the above codes */ -#define ERR_NOFILE 0x00000010 /* don't give source file name/line */ -#define ERR_USAGE 0x00000020 /* print a usage message */ -#define ERR_PASS1 0x00000040 /* only print this error on pass one */ - -/* - * These codes define specific types of suppressible warning. - */ - -#define ERR_WARN_MASK 0x0000FF00 /* the mask for this feature */ -#define ERR_WARN_SHR 8 /* how far to shift right */ - -#define ERR_WARN_MNP 0x00000100 /* macro-num-parameters warning */ -#define ERR_WARN_MSR 0x00000200 /* macro self-reference */ -#define ERR_WARN_OL 0x00000300 /* orphan label (no colon, and - * alone on line) */ -#define ERR_WARN_NOV 0x00000400 /* numeric overflow */ -#define ERR_WARN_GNUELF 0x00000500 /* using GNU ELF extensions */ -#define ERR_WARN_MAX 5 /* the highest numbered one */ - -/* * ----------------------- * Other function typedefs * ----------------------- @@ -243,6 +202,18 @@ typedef struct { } expr; /* + * Library routines to manipulate expression data types. + */ +int is_reloc(expr *); +int is_simple(expr *); +int is_really_simple(expr *); +int is_unknown(expr *); +int is_just_unknown(expr *); +int64_t reloc_value(expr *); +int32_t reloc_seg(expr *); +int32_t reloc_wrt(expr *); + +/* * The evaluator can also return hints about which of two registers * used in an expression should be the base register. See also the * `operand' structure. diff --git a/nasmlib.c b/nasmlib.c index b96fe12..59971c9 100644 --- a/nasmlib.c +++ b/nasmlib.c @@ -703,152 +703,6 @@ const char *prefix_name(int token) } /* - * Return TRUE if the argument is a simple scalar. (Or a far- - * absolute, which counts.) - */ -int is_simple(expr * vect) -{ - while (vect->type && !vect->value) - vect++; - if (!vect->type) - return 1; - if (vect->type != EXPR_SIMPLE) - return 0; - do { - vect++; - } while (vect->type && !vect->value); - if (vect->type && vect->type < EXPR_SEGBASE + SEG_ABS) - return 0; - return 1; -} - -/* - * Return TRUE if the argument is a simple scalar, _NOT_ a far- - * absolute. - */ -int is_really_simple(expr * vect) -{ - while (vect->type && !vect->value) - vect++; - if (!vect->type) - return 1; - if (vect->type != EXPR_SIMPLE) - return 0; - do { - vect++; - } while (vect->type && !vect->value); - if (vect->type) - return 0; - return 1; -} - -/* - * Return TRUE if the argument is relocatable (i.e. a simple - * scalar, plus at most one segment-base, plus possibly a WRT). - */ -int is_reloc(expr * vect) -{ - while (vect->type && !vect->value) /* skip initial value-0 terms */ - vect++; - if (!vect->type) /* trivially return TRUE if nothing */ - return 1; /* is present apart from value-0s */ - if (vect->type < EXPR_SIMPLE) /* FALSE if a register is present */ - return 0; - if (vect->type == EXPR_SIMPLE) { /* skip over a pure number term... */ - do { - vect++; - } while (vect->type && !vect->value); - if (!vect->type) /* ...returning TRUE if that's all */ - return 1; - } - if (vect->type == EXPR_WRT) { /* skip over a WRT term... */ - do { - vect++; - } while (vect->type && !vect->value); - if (!vect->type) /* ...returning TRUE if that's all */ - return 1; - } - if (vect->value != 0 && vect->value != 1) - return 0; /* segment base multiplier non-unity */ - do { /* skip over _one_ seg-base term... */ - vect++; - } while (vect->type && !vect->value); - if (!vect->type) /* ...returning TRUE if that's all */ - return 1; - return 0; /* And return FALSE if there's more */ -} - -/* - * Return TRUE if the argument contains an `unknown' part. - */ -int is_unknown(expr * vect) -{ - while (vect->type && vect->type < EXPR_UNKNOWN) - vect++; - return (vect->type == EXPR_UNKNOWN); -} - -/* - * Return TRUE if the argument contains nothing but an `unknown' - * part. - */ -int is_just_unknown(expr * vect) -{ - while (vect->type && !vect->value) - vect++; - return (vect->type == EXPR_UNKNOWN); -} - -/* - * Return the scalar part of a relocatable vector. (Including - * simple scalar vectors - those qualify as relocatable.) - */ -int64_t reloc_value(expr * vect) -{ - while (vect->type && !vect->value) - vect++; - if (!vect->type) - return 0; - if (vect->type == EXPR_SIMPLE) - return vect->value; - else - return 0; -} - -/* - * Return the segment number of a relocatable vector, or NO_SEG for - * simple scalars. - */ -int32_t reloc_seg(expr * vect) -{ - while (vect->type && (vect->type == EXPR_WRT || !vect->value)) - vect++; - if (vect->type == EXPR_SIMPLE) { - do { - vect++; - } while (vect->type && (vect->type == EXPR_WRT || !vect->value)); - } - if (!vect->type) - return NO_SEG; - else - return vect->type - EXPR_SEGBASE; -} - -/* - * Return the WRT segment number of a relocatable vector, or NO_SEG - * if no WRT part is present. - */ -int32_t reloc_wrt(expr * vect) -{ - while (vect->type && vect->type < EXPR_WRT) - vect++; - if (vect->type == EXPR_WRT) { - return vect->value; - } else - return NO_SEG; -} - -/* * Binary search. */ int bsi(char *string, const char **array, int size) diff --git a/nasmlib.h b/nasmlib.h index 82a35e3..4334209 100644 --- a/nasmlib.h +++ b/nasmlib.h @@ -23,13 +23,54 @@ /* #define LOGALLOC */ /* + * ------------------------- + * Error reporting functions + * ------------------------- + */ + +/* + * An error reporting function should look like this. + */ +typedef void (*efunc) (int severity, const char *fmt, ...); + +/* + * These are the error severity codes which get passed as the first + * argument to an efunc. + */ + +#define ERR_DEBUG 0x00000008 /* put out debugging message */ +#define ERR_WARNING 0x00000000 /* warn only: no further action */ +#define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */ +#define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */ +#define ERR_PANIC 0x00000003 /* internal error: panic instantly + * and dump core for reference */ +#define ERR_MASK 0x0000000F /* mask off the above codes */ +#define ERR_NOFILE 0x00000010 /* don't give source file name/line */ +#define ERR_USAGE 0x00000020 /* print a usage message */ +#define ERR_PASS1 0x00000040 /* only print this error on pass one */ + +/* + * These codes define specific types of suppressible warning. + */ + +#define ERR_WARN_MASK 0x0000FF00 /* the mask for this feature */ +#define ERR_WARN_SHR 8 /* how far to shift right */ + +#define ERR_WARN_MNP 0x00000100 /* macro-num-parameters warning */ +#define ERR_WARN_MSR 0x00000200 /* macro self-reference */ +#define ERR_WARN_OL 0x00000300 /* orphan label (no colon, and + * alone on line) */ +#define ERR_WARN_NOV 0x00000400 /* numeric overflow */ +#define ERR_WARN_GNUELF 0x00000500 /* using GNU ELF extensions */ +#define ERR_WARN_MAX 5 /* the highest numbered one */ + +/* * Wrappers around malloc, realloc and free. nasm_malloc will * fatal-error and die rather than return NULL; nasm_realloc will * do likewise, and will also guarantee to work right on being * passed a NULL pointer; nasm_free will do nothing if it is passed * a NULL pointer. */ -#ifdef NASM_NASM_H /* need efunc defined for this */ void nasm_set_malloc_error(efunc); #ifndef LOGALLOC void *nasm_malloc(size_t); @@ -49,7 +90,6 @@ char *nasm_strndup_log(char *, int, char *, size_t); #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x) #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y) #endif -#endif /* * ANSI doesn't guarantee the presence of `stricmp' or @@ -234,20 +274,6 @@ void saa_fread(struct SAA *s, int32_t posn, void *p, int32_t len); /* fixup * void saa_fwrite(struct SAA *s, int32_t posn, void *p, int32_t len); /* fixup */ void saa_fpwrite(struct SAA *, FILE *); -#ifdef NASM_NASM_H -/* - * Library routines to manipulate expression data types. - */ -int is_reloc(expr *); -int is_simple(expr *); -int is_really_simple(expr *); -int is_unknown(expr *); -int is_just_unknown(expr *); -int64_t reloc_value(expr *); -int32_t reloc_seg(expr *); -int32_t reloc_wrt(expr *); -#endif - /* * Binary search routine. Returns index into `array' of an entry * matching `string', or <0 if no match. `array' is taken to diff --git a/ndisasm.c b/ndisasm.c index f979383..ea4dc53 100644 --- a/ndisasm.c +++ b/ndisasm.c @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -36,6 +37,17 @@ static const char *help = static void output_ins(uint32_t, uint8_t *, int, char *); static void skip(uint32_t dist, FILE * fp); +static void ndisasm_error(int severity, const char *fmt, ...) +{ + va_list va; + + va_start(va, fmt); + vfprintf(stderr, fmt, va); + + if (severity & ERR_FATAL) + exit(1); +} + int main(int argc, char **argv) { char buffer[INSN_MAX * 2], *p, *ep, *q; @@ -53,6 +65,8 @@ int main(int argc, char **argv) int32_t offset; FILE *fp; + nasm_set_malloc_error(ndisasm_error); + offset = 0; init_sync(); -- 2.7.4