From 03c4f90afa42cc8a7ed842ff0fc9d3c18a06dd2d Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Mon, 27 Oct 2008 22:19:59 -0700 Subject: [PATCH] output: add common file outlib.c for common functions; realsize() Add a common file, outlib.c, for output formats. Add the function realsize() instead of open-coded variants in almost every backend. Signed-off-by: H. Peter Anvin --- Makefile.in | 6 ++++-- Mkfiles/msvc.mak | 6 ++++-- Mkfiles/netware.mak | 6 ++++-- Mkfiles/openwcom.mak | 6 ++++-- Mkfiles/owlinux.mak | 6 ++++-- outlib.c | 23 +++++++++++++++++++++++ outlib.h | 9 +++++++++ output/outaout.c | 7 ++----- output/outas86.c | 7 ++----- output/outbin.c | 16 ++-------------- output/outcoff.c | 7 ++----- output/outelf32.c | 7 ++----- output/outelf64.c | 16 ++-------------- output/outmacho.c | 18 ++---------------- output/outobj.c | 3 +-- 15 files changed, 67 insertions(+), 76 deletions(-) create mode 100644 outlib.c create mode 100644 outlib.h diff --git a/Makefile.in b/Makefile.in index 3126bd13..6dd61e3c 100644 --- a/Makefile.in +++ b/Makefile.in @@ -62,7 +62,7 @@ X = @EXEEXT@ NASM = nasm.$(O) nasmlib.$(O) raa.$(O) saa.$(O) \ float.$(O) insnsa.$(O) insnsb.$(O) \ assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) \ - outform.$(O) output/outbin.$(O) \ + outform.$(O) outlib.$(O) output/outbin.$(O) \ output/outaout.$(O) output/outcoff.$(O) \ output/outelf32.$(O) output/outelf64.$(O) \ output/outobj.$(O) output/outas86.$(O) output/outrdf2.$(O) \ @@ -279,6 +279,8 @@ ndisasm.$(O): ndisasm.c compiler.h config.h disasm.h insns.h insnsi.h nasm.h \ nasmlib.h pptok.h preproc.h regs.h sync.h tokens.h version.h outform.$(O): outform.c compiler.h config.h insnsi.h nasm.h nasmlib.h \ outform.h pptok.h preproc.h regs.h version.h +outlib.$(O): outlib.c compiler.h config.h insnsi.h nasm.h nasmlib.h outlib.h \ + pptok.h preproc.h regs.h version.h output/outaout.$(O): output/outaout.c compiler.h config.h insnsi.h nasm.h \ nasmlib.h outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h \ version.h @@ -292,7 +294,7 @@ output/outcoff.$(O): output/outcoff.c compiler.h config.h insnsi.h nasm.h \ output/outdbg.$(O): output/outdbg.c compiler.h config.h insnsi.h nasm.h \ nasmlib.h outform.h pptok.h preproc.h regs.h version.h output/outelf32.$(O): output/outelf32.c compiler.h config.h insnsi.h nasm.h \ - nasmlib.h outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h \ + nasmlib.h outform.h outlib.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h \ version.h output/outelf64.$(O): output/outelf64.c compiler.h config.h insnsi.h nasm.h \ nasmlib.h outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h \ diff --git a/Mkfiles/msvc.mak b/Mkfiles/msvc.mak index 594bce69..94ac97a4 100644 --- a/Mkfiles/msvc.mak +++ b/Mkfiles/msvc.mak @@ -37,7 +37,7 @@ X = .exe NASM = nasm.$(O) nasmlib.$(O) raa.$(O) saa.$(O) \ float.$(O) insnsa.$(O) insnsb.$(O) \ assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) \ - outform.$(O) output/outbin.$(O) \ + outform.$(O) outlib.$(O) output/outbin.$(O) \ output/outaout.$(O) output/outcoff.$(O) \ output/outelf32.$(O) output/outelf64.$(O) \ output/outobj.$(O) output/outas86.$(O) output/outrdf2.$(O) \ @@ -218,6 +218,8 @@ ndisasm.$(O): ndisasm.c compiler.h disasm.h insns.h insnsi.h nasm.h \ nasmlib.h pptok.h preproc.h regs.h sync.h tokens.h version.h outform.$(O): outform.c compiler.h insnsi.h nasm.h nasmlib.h outform.h \ pptok.h preproc.h regs.h version.h +outlib.$(O): outlib.c compiler.h insnsi.h nasm.h nasmlib.h outlib.h pptok.h \ + preproc.h regs.h version.h output/outaout.$(O): output/outaout.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h output/outas86.$(O): output/outas86.c compiler.h insnsi.h nasm.h nasmlib.h \ @@ -230,7 +232,7 @@ output/outcoff.$(O): output/outcoff.c compiler.h insnsi.h nasm.h nasmlib.h \ output/outdbg.$(O): output/outdbg.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h pptok.h preproc.h regs.h version.h output/outelf32.$(O): output/outelf32.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h + outform.h outlib.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h output/outelf64.$(O): output/outelf64.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h output/outieee.$(O): output/outieee.c compiler.h insnsi.h nasm.h nasmlib.h \ diff --git a/Mkfiles/netware.mak b/Mkfiles/netware.mak index 873043b2..f98bd02e 100644 --- a/Mkfiles/netware.mak +++ b/Mkfiles/netware.mak @@ -33,7 +33,7 @@ O = o NASM = nasm.o nasmlib.o raa.o saa.o \ float.o insnsa.o insnsb.o \ assemble.o labels.o hashtbl.o crc64.o parser.o \ - outform.o outbin.o \ + outform.o outlib.o outbin.o \ outaout.o outcoff.o \ outelf32.o outelf64.o \ outobj.o outas86.o outrdf2.o \ @@ -159,6 +159,8 @@ ndisasm.o: ndisasm.c compiler.h config.h disasm.h insns.h insnsi.h nasm.h \ nasmlib.h pptok.h preproc.h regs.h sync.h tokens.h version.h outform.o: outform.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ pptok.h preproc.h regs.h version.h +outlib.o: outlib.c compiler.h config.h insnsi.h nasm.h nasmlib.h outlib.h \ + pptok.h preproc.h regs.h version.h outaout.o: outaout.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h outas86.o: outas86.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ @@ -170,7 +172,7 @@ outcoff.o: outcoff.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ outdbg.o: outdbg.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ pptok.h preproc.h regs.h version.h outelf32.o: outelf32.c compiler.h config.h insnsi.h nasm.h nasmlib.h \ - outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h + outform.h outlib.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h outelf64.o: outelf64.c compiler.h config.h insnsi.h nasm.h nasmlib.h \ outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h outieee.o: outieee.c compiler.h config.h insnsi.h nasm.h nasmlib.h outform.h \ diff --git a/Mkfiles/openwcom.mak b/Mkfiles/openwcom.mak index b10ceb54..7361abbf 100644 --- a/Mkfiles/openwcom.mak +++ b/Mkfiles/openwcom.mak @@ -49,7 +49,7 @@ X = .exe NASM = nasm.$(O) nasmlib.$(O) raa.$(O) saa.$(O) & float.$(O) insnsa.$(O) insnsb.$(O) & assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) & - outform.$(O) output\outbin.$(O) & + outform.$(O) outlib.$(O) output\outbin.$(O) & output\outaout.$(O) output\outcoff.$(O) & output\outelf32.$(O) output\outelf64.$(O) & output\outobj.$(O) output\outas86.$(O) output\outrdf2.$(O) & @@ -247,6 +247,8 @@ ndisasm.$(O): ndisasm.c compiler.h disasm.h insns.h insnsi.h nasm.h & nasmlib.h pptok.h preproc.h regs.h sync.h tokens.h version.h outform.$(O): outform.c compiler.h insnsi.h nasm.h nasmlib.h outform.h & pptok.h preproc.h regs.h version.h +outlib.$(O): outlib.c compiler.h insnsi.h nasm.h nasmlib.h outlib.h pptok.h & + preproc.h regs.h version.h output\outaout.$(O): output\outaout.c compiler.h insnsi.h nasm.h nasmlib.h & outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h output\outas86.$(O): output\outas86.c compiler.h insnsi.h nasm.h nasmlib.h & @@ -259,7 +261,7 @@ output\outcoff.$(O): output\outcoff.c compiler.h insnsi.h nasm.h nasmlib.h & output\outdbg.$(O): output\outdbg.c compiler.h insnsi.h nasm.h nasmlib.h & outform.h pptok.h preproc.h regs.h version.h output\outelf32.$(O): output\outelf32.c compiler.h insnsi.h nasm.h nasmlib.h & - outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h + outform.h outlib.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h output\outelf64.$(O): output\outelf64.c compiler.h insnsi.h nasm.h nasmlib.h & outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h output\outieee.$(O): output\outieee.c compiler.h insnsi.h nasm.h nasmlib.h & diff --git a/Mkfiles/owlinux.mak b/Mkfiles/owlinux.mak index 1c7d44d7..4e6c8bc3 100644 --- a/Mkfiles/owlinux.mak +++ b/Mkfiles/owlinux.mak @@ -60,7 +60,7 @@ X = .exe NASM = nasm.$(O) nasmlib.$(O) raa.$(O) saa.$(O) \ float.$(O) insnsa.$(O) insnsb.$(O) \ assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) \ - outform.$(O) output/outbin.$(O) \ + outform.$(O) outlib.$(O) output/outbin.$(O) \ output/outaout.$(O) output/outcoff.$(O) \ output/outelf32.$(O) output/outelf64.$(O) \ output/outobj.$(O) output/outas86.$(O) output/outrdf2.$(O) \ @@ -257,6 +257,8 @@ ndisasm.$(O): ndisasm.c compiler.h disasm.h insns.h insnsi.h nasm.h \ nasmlib.h pptok.h preproc.h regs.h sync.h tokens.h version.h outform.$(O): outform.c compiler.h insnsi.h nasm.h nasmlib.h outform.h \ pptok.h preproc.h regs.h version.h +outlib.$(O): outlib.c compiler.h insnsi.h nasm.h nasmlib.h outlib.h pptok.h \ + preproc.h regs.h version.h output/outaout.$(O): output/outaout.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h output/outas86.$(O): output/outas86.c compiler.h insnsi.h nasm.h nasmlib.h \ @@ -269,7 +271,7 @@ output/outcoff.$(O): output/outcoff.c compiler.h insnsi.h nasm.h nasmlib.h \ output/outdbg.$(O): output/outdbg.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h pptok.h preproc.h regs.h version.h output/outelf32.$(O): output/outelf32.c compiler.h insnsi.h nasm.h nasmlib.h \ - outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h + outform.h outlib.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h output/outelf64.$(O): output/outelf64.c compiler.h insnsi.h nasm.h nasmlib.h \ outform.h pptok.h preproc.h raa.h regs.h saa.h stdscan.h version.h output/outieee.$(O): output/outieee.c compiler.h insnsi.h nasm.h nasmlib.h \ diff --git a/outlib.c b/outlib.c new file mode 100644 index 00000000..cf687216 --- /dev/null +++ b/outlib.c @@ -0,0 +1,23 @@ +/* + * libout.c + * + * Common routines for the output backends. + */ + +#include "compiler.h" +#include "nasm.h" +#include "outlib.h" + +uint64_t realsize(enum out_type type, uint64_t size) +{ + switch (type) { + case OUT_REL2ADR: + return 2; + case OUT_REL4ADR: + return 4; + case OUT_REL8ADR: + return 8; + default: + return size; + } +} diff --git a/outlib.h b/outlib.h new file mode 100644 index 00000000..03524b97 --- /dev/null +++ b/outlib.h @@ -0,0 +1,9 @@ +#ifndef NASM_OUTLIB_H +#define NSAM_OUTLIB_H + +#include "nasm.h" + +uint64_t realsize(enum out_type type, uint64_t size); + +#endif /* LIBOUT_H */ + diff --git a/output/outaout.c b/output/outaout.c index 48a2cffd..467b20e3 100644 --- a/output/outaout.c +++ b/output/outaout.c @@ -21,6 +21,7 @@ #include "raa.h" #include "stdscan.h" #include "outform.h" +#include "outlib.h" #if defined OF_AOUT || defined OF_AOUTB @@ -603,11 +604,7 @@ static void aout_out(int32_t segto, const void *data, if (!s && type != OUT_RESERVE) { error(ERR_WARNING, "attempt to initialize memory in the" " BSS section: ignored"); - if (type == OUT_REL2ADR) - size = 2; - else if (type == OUT_REL4ADR) - size = 4; - sbss.len += size; + sbss.len += realsize(type, size); return; } diff --git a/output/outas86.c b/output/outas86.c index b59fe6b3..307f10b2 100644 --- a/output/outas86.c +++ b/output/outas86.c @@ -20,6 +20,7 @@ #include "saa.h" #include "raa.h" #include "outform.h" +#include "outlib.h" #ifdef OF_AS86 @@ -294,11 +295,7 @@ static void as86_out(int32_t segto, const void *data, if (!s && type != OUT_RESERVE) { error(ERR_WARNING, "attempt to initialize memory in the" " BSS section: ignored"); - if (type == OUT_REL2ADR) - size = 2; - else if (type == OUT_REL4ADR) - size = 4; - bsslen += size; + bsslen += realsize(type, size); return; } diff --git a/output/outbin.c b/output/outbin.c index cfa0aa60..f6be9643 100644 --- a/output/outbin.c +++ b/output/outbin.c @@ -59,6 +59,7 @@ #include "labels.h" #include "eval.h" #include "outform.h" +#include "outlib.h" #ifdef OF_BIN @@ -799,20 +800,7 @@ static void bin_out(int32_t segto, const void *data, } else if (type == OUT_REL2ADR || type == OUT_REL4ADR || type == OUT_REL8ADR) { int64_t addr = *(int64_t *)data - size; - switch (type) { - case OUT_REL2ADR: - size = 2; - break; - case OUT_REL4ADR: - size = 4; - break; - case OUT_REL8ADR: - size = 8; - break; - default: - size = 0; /* Shut up warning */ - break; - } + size = realsize(type, size); if (segment != NO_SEG && !find_section_by_index(segment)) { if (segment % 2) error(ERR_NONFATAL, "binary output format does not support" diff --git a/output/outcoff.c b/output/outcoff.c index 31e5cbef..16d2719f 100644 --- a/output/outcoff.c +++ b/output/outcoff.c @@ -21,6 +21,7 @@ #include "saa.h" #include "raa.h" #include "outform.h" +#include "outlib.h" #if defined(OF_COFF) || defined(OF_WIN32) || defined(OF_WIN64) @@ -527,11 +528,7 @@ static void coff_out(int32_t segto, const void *data, if (!s->data && type != OUT_RESERVE) { error(ERR_WARNING, "attempt to initialize memory in" " BSS section `%s': ignored", s->name); - if (type == OUT_REL2ADR) - size = 2; - else if (type == OUT_REL4ADR) - size = 4; - s->len += size; + s->len += realsize(type, size); return; } diff --git a/output/outelf32.c b/output/outelf32.c index bff264a3..9f424a4e 100644 --- a/output/outelf32.c +++ b/output/outelf32.c @@ -21,6 +21,7 @@ #include "raa.h" #include "stdscan.h" #include "outform.h" +#include "outlib.h" #ifdef OF_ELF32 @@ -889,11 +890,7 @@ static void elf_out(int32_t segto, const void *data, if (s->type == SHT_NOBITS && type != OUT_RESERVE) { error(ERR_WARNING, "attempt to initialize memory in" " BSS section `%s': ignored", s->name); - if (type == OUT_REL2ADR) - size = 2; - else if (type == OUT_REL4ADR) - size = 4; - s->len += size; + s->len += realsize(type, size); return; } diff --git a/output/outelf64.c b/output/outelf64.c index d2bf3c47..b7caa164 100644 --- a/output/outelf64.c +++ b/output/outelf64.c @@ -20,6 +20,7 @@ #include "raa.h" #include "stdscan.h" #include "outform.h" +#include "outlib.h" /* Definitions in lieu of elf.h */ #define SHT_NULL 0 /* Inactive section header */ @@ -964,20 +965,7 @@ static void elf_out(int32_t segto, const void *data, if (s->type == SHT_NOBITS && type != OUT_RESERVE) { error(ERR_WARNING, "attempt to initialize memory in" " BSS section `%s': ignored", s->name); - switch (type) { - case OUT_REL2ADR: - size = 2; - break; - case OUT_REL4ADR: - size = 4; - break; - case OUT_REL8ADR: - size = 8; - break; - default: - break; /* size is already set */ - } - s->len += size; + s->len += realsize(type, size); return; } diff --git a/output/outmacho.c b/output/outmacho.c index 5e9ad8e1..06ae8ee8 100644 --- a/output/outmacho.c +++ b/output/outmacho.c @@ -23,7 +23,7 @@ #include "saa.h" #include "raa.h" #include "outform.h" -#include "compiler.h" +#include "outlib.h" #if defined(OF_MACHO) @@ -427,21 +427,7 @@ static void macho_output(int32_t secto, const void *data, if (s == sbss && type != OUT_RESERVE) { error(ERR_WARNING, "attempt to initialize memory in the" " BSS section: ignored"); - - switch (type) { - case OUT_REL2ADR: - size = 2; - break; - - case OUT_REL4ADR: - size = 4; - break; - - default: - break; - } - - s->size += size; + s->size += realsize(type, size); return; } diff --git a/output/outobj.c b/output/outobj.c index e248ac31..4e18e9dc 100644 --- a/output/outobj.c +++ b/output/outobj.c @@ -1068,8 +1068,7 @@ static void obj_out(int32_t segto, const void *data, if (type == OUT_REL2ADR) { ldata += (size - 2); size = 2; - } - if (type == OUT_REL4ADR) { + } else if (type == OUT_REL4ADR) { ldata += (size - 4); size = 4; }