output: add common file outlib.c for common functions; realsize()
authorH. Peter Anvin <hpa@zytor.com>
Tue, 28 Oct 2008 05:19:59 +0000 (22:19 -0700)
committerH. Peter Anvin <hpa@zytor.com>
Tue, 28 Oct 2008 05:19:59 +0000 (22:19 -0700)
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 <hpa@zytor.com>
15 files changed:
Makefile.in
Mkfiles/msvc.mak
Mkfiles/netware.mak
Mkfiles/openwcom.mak
Mkfiles/owlinux.mak
outlib.c [new file with mode: 0644]
outlib.h [new file with mode: 0644]
output/outaout.c
output/outas86.c
output/outbin.c
output/outcoff.c
output/outelf32.c
output/outelf64.c
output/outmacho.c
output/outobj.c

index 3126bd1..6dd61e3 100644 (file)
@@ -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 \
index 594bce6..94ac97a 100644 (file)
@@ -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 \
index 873043b..f98bd02 100644 (file)
@@ -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 \
index b10ceb5..7361abb 100644 (file)
@@ -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 &
index 1c7d44d..4e6c8bc 100644 (file)
@@ -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 (file)
index 0000000..cf68721
--- /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 (file)
index 0000000..03524b9
--- /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 */
+
index 48a2cff..467b20e 100644 (file)
@@ -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;
     }
 
index b59fe6b..307f10b 100644 (file)
@@ -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;
     }
 
index cfa0aa6..f6be964 100644 (file)
@@ -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"
index 31e5cbe..16d2719 100644 (file)
@@ -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;
     }
 
index bff264a..9f424a4 100644 (file)
@@ -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;
     }
 
index d2bf3c4..b7caa16 100644 (file)
@@ -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;
     }
 
index 5e9ad8e..06ae8ee 100644 (file)
@@ -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;
     }
 
index e248ac3..4e18e9d 100644 (file)
@@ -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;
         }