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 <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin 2008-10-27 22:19:59 -07:00
parent 67c637236a
commit 03c4f90afa
15 changed files with 67 additions and 76 deletions

View 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 \

View 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 \

View 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 \

View 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 &

View 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 \

23
outlib.c Normal file
View File

@ -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;
}
}

9
outlib.h Normal file
View File

@ -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 */

View 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;
}

View 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;
}

View 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"

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}