nasm/output/outrdf.c

570 lines
17 KiB
C
Raw Normal View History

/* ----------------------------------------------------------------------- *
*
* Copyright 1996-2009 The NASM Authors - All Rights Reserved
* See the file AUTHORS included with the NASM distribution for
* the specific copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ----------------------------------------------------------------------- */
/*
* outrdf.c output routines for the Netwide Assembler to produce
2002-05-01 04:51:32 +08:00
* RDOFF format object files (which are intended mainly
* for use in proprietary projects, as the code to load and
* execute them is very simple). They will also be used
* for device drivers and possibly some executable files
* in the MOSCOW operating system. See Rdoff.txt for
* details.
*/
#include "compiler.h"
2002-05-01 04:51:32 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "nasm.h"
#include "nasmlib.h"
#include "output/outform.h"
2002-05-01 04:51:32 +08:00
2002-05-01 04:52:08 +08:00
/* VERBOSE_WARNINGS: define this to add some extra warnings... */
2005-01-16 06:15:51 +08:00
#define VERBOSE_WARNINGS
2002-05-01 04:52:08 +08:00
2002-05-01 04:51:32 +08:00
#ifdef OF_RDF
typedef int16_t int16; /* not sure if this will be required to be altered
2005-01-16 06:15:51 +08:00
at all... best to typedef it just in case */
2002-05-01 04:51:32 +08:00
static const char *RDOFFId = "RDOFF1"; /* written to start of RDOFF files */
2002-05-01 04:51:32 +08:00
/* the records that can be found in the RDOFF header */
/* Note that whenever a segment is referred to in the RDOFF file, its number
* is always half of the segment number that NASM uses to refer to it; this
* is because NASM only allocates even numbered segments, so as to not
* waste any of the 16 bits of segment number written to the file - this
* allows up to 65533 external labels to be defined; otherwise it would be
* 32764. */
struct RelocRec {
char type; /* must be 1 */
char segment; /* only 0 for code, or 1 for data supported,
2005-01-16 06:15:51 +08:00
* but add 64 for relative refs (ie do not require
* reloc @ loadtime, only linkage) */
int32_t offset; /* from start of segment in which reference is loc'd */
char length; /* 1 2 or 4 bytes */
2005-01-16 06:15:51 +08:00
int16 refseg; /* segment to which reference refers to */
2002-05-01 04:51:32 +08:00
};
struct ImportRec {
char type; /* must be 2 */
2005-01-16 06:15:51 +08:00
int16 segment; /* segment number allocated to the label for reloc
* records - label is assumed to be at offset zero
* in this segment, so linker must fix up with offset
* of segment and of offset within segment */
char label[33]; /* zero terminated... should be written to file until
2005-01-16 06:15:51 +08:00
* the zero, but not after it - max len = 32 chars */
2002-05-01 04:51:32 +08:00
};
struct ExportRec {
char type; /* must be 3 */
char segment; /* segment referred to (0/1) */
int32_t offset; /* offset within segment */
char label[33]; /* zero terminated as above. max len = 32 chars */
2002-05-01 04:51:32 +08:00
};
struct DLLRec {
char type; /* must be 4 */
char libname[128]; /* name of library to link with at load time */
2002-05-01 04:51:32 +08:00
};
struct BSSRec {
char type; /* must be 5 */
int32_t amount; /* number of bytes BSS to reserve */
2002-05-01 04:51:32 +08:00
};
/* code for managing buffers needed to seperate code and data into individual
* sections until they are ready to be written to the file.
* We'd better hope that it all fits in memory else we're buggered... */
2005-01-16 06:15:51 +08:00
#define BUF_BLOCK_LEN 4088 /* selected to match page size (4096)
* on 80x86 machines for efficiency */
2002-05-01 04:51:32 +08:00
typedef struct memorybuffer {
2005-01-16 06:15:51 +08:00
int length;
char buffer[BUF_BLOCK_LEN];
2005-01-16 06:15:51 +08:00
struct memorybuffer *next;
2002-05-01 04:51:32 +08:00
} memorybuffer;
2005-01-16 06:15:51 +08:00
static memorybuffer *newmembuf(void)
{
memorybuffer *t;
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
t = nasm_malloc(sizeof(memorybuffer));
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
t->length = 0;
t->next = NULL;
return t;
2002-05-01 04:51:32 +08:00
}
2005-01-16 06:15:51 +08:00
static void membufwrite(memorybuffer * b, void *data, int bytes)
{
int16 w;
int32_t l;
2005-01-16 06:15:51 +08:00
if (b->next) { /* memory buffer full - use next buffer */
membufwrite(b->next, data, bytes);
return;
}
if ((bytes < 0 && b->length - bytes > BUF_BLOCK_LEN)
|| (bytes > 0 && b->length + bytes > BUF_BLOCK_LEN)) {
/* buffer full and no next allocated... allocate and initialize next
2005-01-16 06:15:51 +08:00
* buffer */
b->next = newmembuf();
membufwrite(b->next, data, bytes);
return;
}
switch (bytes) {
case -4: /* convert to little-endian */
l = *(int32_t *)data;
2005-01-16 06:15:51 +08:00
b->buffer[b->length++] = l & 0xFF;
l >>= 8;
b->buffer[b->length++] = l & 0xFF;
l >>= 8;
b->buffer[b->length++] = l & 0xFF;
l >>= 8;
b->buffer[b->length++] = l & 0xFF;
break;
case -2:
w = *(int16 *) data;
b->buffer[b->length++] = w & 0xFF;
w >>= 8;
b->buffer[b->length++] = w & 0xFF;
break;
default:
while (bytes--) {
b->buffer[b->length++] = *(*(uint8_t **)&data);
2005-01-16 06:15:51 +08:00
(*(uint8_t **)&data)++;
2005-01-16 06:15:51 +08:00
}
break;
2002-05-01 04:51:32 +08:00
}
}
2005-01-16 06:15:51 +08:00
static void membufdump(memorybuffer * b, FILE * fp)
2002-05-01 04:51:32 +08:00
{
2005-01-16 06:15:51 +08:00
if (!b)
return;
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
fwrite(b->buffer, 1, b->length, fp);
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
membufdump(b->next, fp);
2002-05-01 04:51:32 +08:00
}
2005-01-16 06:15:51 +08:00
static int membuflength(memorybuffer * b)
2002-05-01 04:51:32 +08:00
{
2005-01-16 06:15:51 +08:00
if (!b)
return 0;
return b->length + membuflength(b->next);
2002-05-01 04:51:32 +08:00
}
2005-01-16 06:15:51 +08:00
static void freemembuf(memorybuffer * b)
2002-05-01 04:51:32 +08:00
{
2005-01-16 06:15:51 +08:00
if (!b)
return;
freemembuf(b->next);
nasm_free(b);
2002-05-01 04:51:32 +08:00
}
/***********************************************************************
* Actual code to deal with RDOFF ouput format begins here...
*/
/* global variables set during the initialisation phase */
2005-01-16 06:15:51 +08:00
static memorybuffer *seg[2]; /* seg 0 = code, seg 1 = data */
static memorybuffer *header; /* relocation/import/export records */
2002-05-01 04:51:32 +08:00
2002-05-01 04:51:53 +08:00
static FILE *ofile;
2002-05-01 04:51:32 +08:00
static efunc error;
2005-01-16 06:15:51 +08:00
static int segtext, segdata, segbss;
static int32_t bsslength;
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
static void rdf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
2002-05-01 04:51:32 +08:00
{
maxbits = 64;
2005-01-16 06:15:51 +08:00
ofile = fp;
error = errfunc;
seg[0] = newmembuf();
seg[1] = newmembuf();
header = newmembuf();
segtext = seg_alloc();
segdata = seg_alloc();
segbss = seg_alloc();
if (segtext != 0 || segdata != 2 || segbss != 4)
error(ERR_PANIC,
"rdf segment numbers not allocated as expected (%d,%d,%d)",
segtext, segdata, segbss);
bsslength = 0;
2002-05-01 04:51:32 +08:00
}
static int32_t rdf_section_names(char *name, int pass, int *bits)
2002-05-01 04:51:32 +08:00
{
2005-01-16 06:15:51 +08:00
/*
* Default is 32 bits.
*/
if (!name)
*bits = 32;
if (!name)
return 0;
if (!strcmp(name, ".text"))
return 0;
else if (!strcmp(name, ".data"))
return 2;
else if (!strcmp(name, ".bss"))
return 4;
else
return NO_SEG;
2002-05-01 04:51:32 +08:00
}
static void write_reloc_rec(struct RelocRec *r)
{
2005-01-16 06:15:51 +08:00
if (r->refseg != NO_SEG && (r->refseg & 1))
error(ERR_NONFATAL, "RDF format does not support segment base"
" references");
2002-05-01 04:53:16 +08:00
2005-01-16 06:15:51 +08:00
r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
membufwrite(header, &r->type, 1);
membufwrite(header, &r->segment, 1);
membufwrite(header, &r->offset, -4);
membufwrite(header, &r->length, 1);
membufwrite(header, &r->refseg, -2); /* 9 bytes written */
2002-05-01 04:51:32 +08:00
}
static void write_export_rec(struct ExportRec *r)
{
2005-01-16 06:15:51 +08:00
r->segment >>= 1;
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
membufwrite(header, &r->type, 1);
membufwrite(header, &r->segment, 1);
membufwrite(header, &r->offset, -4);
membufwrite(header, r->label, strlen(r->label) + 1);
2002-05-01 04:51:32 +08:00
}
static void write_import_rec(struct ImportRec *r)
{
2005-01-16 06:15:51 +08:00
r->segment >>= 1;
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
membufwrite(header, &r->type, 1);
membufwrite(header, &r->segment, -2);
membufwrite(header, r->label, strlen(r->label) + 1);
2002-05-01 04:51:32 +08:00
}
static void write_bss_rec(struct BSSRec *r)
{
2005-01-16 06:15:51 +08:00
membufwrite(header, &r->type, 1);
membufwrite(header, &r->amount, -4);
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:52:08 +08:00
static void write_dll_rec(struct DLLRec *r)
{
2005-01-16 06:15:51 +08:00
membufwrite(header, &r->type, 1);
membufwrite(header, r->libname, strlen(r->libname) + 1);
2002-05-01 04:52:08 +08:00
}
static void rdf_deflabel(char *name, int32_t segment, int32_t offset,
int is_global, char *special)
2002-05-01 04:51:32 +08:00
{
2005-01-16 06:15:51 +08:00
struct ExportRec r;
struct ImportRec ri;
2002-05-01 04:52:08 +08:00
#ifdef VERBOSE_WARNINGS
2005-01-16 06:15:51 +08:00
static int warned_common = 0;
2002-05-01 04:52:08 +08:00
#endif
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
if (special)
error(ERR_NONFATAL, "RDOFF format does not support any"
" special symbol types");
2002-05-01 04:52:49 +08:00
2005-01-16 06:15:51 +08:00
if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
return;
}
2002-05-01 04:52:26 +08:00
2005-01-16 06:15:51 +08:00
if (is_global == 2) {
2002-05-01 04:52:08 +08:00
#ifdef VERBOSE_WARNINGS
2005-01-16 06:15:51 +08:00
if (!warned_common) {
error(ERR_WARNING,
"common declarations not supported: using extern");
warned_common = 1;
}
2002-05-01 04:52:08 +08:00
#endif
2005-01-16 06:15:51 +08:00
is_global = 1;
}
if (segment > 4) { /* EXTERN declaration */
ri.type = 2;
ri.segment = segment;
strncpy(ri.label, name, 32);
ri.label[32] = 0;
write_import_rec(&ri);
} else if (is_global) {
r.type = 3;
r.segment = segment;
r.offset = offset;
strncpy(r.label, name, 32);
r.label[32] = 0;
write_export_rec(&r);
}
2002-05-01 04:51:32 +08:00
}
static void rdf_out(int32_t segto, void *data, uint32_t type,
int32_t segment, int32_t wrt)
2002-05-01 04:51:32 +08:00
{
int32_t bytes = type & OUT_SIZMASK;
2005-01-16 06:15:51 +08:00
struct RelocRec rr;
uint8_t databuf[8], *pd;
2005-01-16 06:15:51 +08:00
if (segto == NO_SEG) {
if ((type & OUT_TYPMASK) != OUT_RESERVE)
error(ERR_NONFATAL,
"attempt to assemble code in ABSOLUTE space");
return;
2002-05-01 04:51:32 +08:00
}
2005-01-16 06:15:51 +08:00
segto >>= 1; /* convert NASM segment no to RDF number */
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
if (segto != 0 && segto != 1 && segto != 2) {
error(ERR_NONFATAL,
"specified segment not supported by rdf output format");
return;
2002-05-01 04:51:32 +08:00
}
2005-01-16 06:15:51 +08:00
if (wrt != NO_SEG) {
wrt = NO_SEG; /* continue to do _something_ */
error(ERR_NONFATAL, "WRT not supported by rdf output format");
2002-05-01 04:51:32 +08:00
}
2005-01-16 06:15:51 +08:00
type &= OUT_TYPMASK;
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
if (segto == 2 && type != OUT_RESERVE) {
error(ERR_NONFATAL, "BSS segments may not be initialized");
2005-01-16 06:15:51 +08:00
/* just reserve the space for now... */
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
if (type == OUT_REL2ADR)
bytes = 2;
else
bytes = 4;
type = OUT_RESERVE;
}
if (type == OUT_RESERVE) {
if (segto == 2) /* BSS segment space reserverd */
bsslength += bytes;
else
while (bytes--)
membufwrite(seg[segto], databuf, 1);
} else if (type == OUT_RAWDATA) {
if (segment != NO_SEG)
error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
membufwrite(seg[segto], data, bytes);
} else if (type == OUT_ADDRESS) {
/* if segment == NO_SEG then we are writing an address of an
object within the same segment - do not produce reloc rec. */
if (segment != NO_SEG) {
/* it's an address, so we must write a relocation record */
rr.type = 1; /* type signature */
rr.segment = segto; /* segment we're currently in */
rr.offset = membuflength(seg[segto]); /* current offset */
rr.length = bytes; /* length of reference */
rr.refseg = segment; /* segment referred to */
write_reloc_rec(&rr);
}
pd = databuf; /* convert address to little-endian */
WRITEADDR(pd, *(int64_t *)data, bytes);
2005-01-16 06:15:51 +08:00
membufwrite(seg[segto], databuf, bytes);
} else if (type == OUT_REL2ADR) {
if (segment == segto)
error(ERR_PANIC, "intra-segment OUT_REL2ADR");
if (segment != NO_SEG && segment % 2) {
error(ERR_NONFATAL,
"rdf format does not support segment base refs");
}
rr.type = 1; /* type signature */
rr.segment = segto + 64; /* segment we're currently in + rel flag */
rr.offset = membuflength(seg[segto]); /* current offset */
rr.length = 2; /* length of reference */
rr.refseg = segment; /* segment referred to */
write_reloc_rec(&rr);
/* work out what to put in the code: offset of the end of this operand,
* subtracted from any data specified, so that loader can just add
* address of imported symbol onto it to get address relative to end of
* instruction: import_address + data(offset) - end_of_instrn */
rr.offset = *(int64_t *)data - (rr.offset + bytes);
2005-01-16 06:15:51 +08:00
membufwrite(seg[segto], &rr.offset, -2);
} else if (type == OUT_REL4ADR) {
if ((segment == segto) && (globalbits != 64))
2005-01-16 06:15:51 +08:00
error(ERR_PANIC, "intra-segment OUT_REL4ADR");
if (segment != NO_SEG && segment % 2) {
error(ERR_NONFATAL,
"rdf format does not support segment base refs");
}
rr.type = 1; /* type signature */
rr.segment = segto + 64; /* segment we're currently in + rel tag */
rr.offset = membuflength(seg[segto]); /* current offset */
rr.length = 4; /* length of reference */
rr.refseg = segment; /* segment referred to */
write_reloc_rec(&rr);
rr.offset = *(int64_t *)data - (rr.offset + bytes);
2005-01-16 06:15:51 +08:00
membufwrite(seg[segto], &rr.offset, -4);
}
}
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
static void rdf_cleanup(int debuginfo)
{
int32_t l;
uint8_t b[4], *d;
2005-01-16 06:15:51 +08:00
struct BSSRec bs;
2002-05-01 04:53:55 +08:00
2005-01-16 06:15:51 +08:00
(void)debuginfo;
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
/* should write imported & exported symbol declarations to header here */
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
/* generate the output file... */
fwrite(RDOFFId, 6, 1, ofile); /* file type magic number */
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
if (bsslength != 0) { /* reserve BSS */
bs.type = 5;
bs.amount = bsslength;
write_bss_rec(&bs);
}
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
l = membuflength(header);
d = b;
WRITELONG(d, l);
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
fwrite(b, 4, 1, ofile); /* write length of header */
membufdump(header, ofile); /* dump header */
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
l = membuflength(seg[0]);
d = b; /* code segment */
WRITELONG(d, l);
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
fwrite(b, 4, 1, ofile);
membufdump(seg[0], ofile);
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
l = membuflength(seg[1]);
d = b; /* data segment */
WRITELONG(d, l);
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
fwrite(b, 4, 1, ofile);
membufdump(seg[1], ofile);
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
freemembuf(header);
freemembuf(seg[0]);
freemembuf(seg[1]);
2002-05-01 04:51:32 +08:00
}
static int32_t rdf_segbase(int32_t segment)
2005-01-16 06:15:51 +08:00
{
2002-05-01 04:53:16 +08:00
return segment;
2002-05-01 04:51:32 +08:00
}
static int rdf_directive(char *directive, char *value, int pass)
2005-01-16 06:15:51 +08:00
{
2002-05-01 04:52:08 +08:00
struct DLLRec r;
2005-01-16 06:15:51 +08:00
if (!strcmp(directive, "library")) {
if (pass == 1) {
r.type = 4;
strcpy(r.libname, value);
write_dll_rec(&r);
}
return 1;
2002-05-01 04:52:08 +08:00
}
return 0;
2002-05-01 04:51:32 +08:00
}
static void rdf_filename(char *inname, char *outname, efunc error)
2005-01-16 06:15:51 +08:00
{
standard_extension(inname, outname, ".rdf", error);
2002-05-01 04:51:32 +08:00
}
extern macros_t rdf_stdmac[];
2002-05-01 04:52:49 +08:00
static int rdf_set_info(enum geninfo type, char **val)
2002-05-01 04:53:55 +08:00
{
return 0;
}
2002-05-01 04:51:32 +08:00
struct ofmt of_rdf = {
2005-01-16 06:15:51 +08:00
"Relocatable Dynamic Object File Format v1.1",
2002-05-01 04:53:55 +08:00
#ifdef OF_RDF2
2005-01-16 06:15:51 +08:00
"oldrdf",
2002-05-01 04:53:55 +08:00
#else
2005-01-16 06:15:51 +08:00
"rdf",
2002-05-01 04:53:55 +08:00
#endif
0,
2005-01-16 06:15:51 +08:00
null_debug_arr,
&null_debug_form,
rdf_stdmac,
rdf_init,
rdf_set_info,
rdf_out,
rdf_deflabel,
rdf_section_names,
rdf_segbase,
rdf_directive,
rdf_filename,
rdf_cleanup
2002-05-01 04:51:32 +08:00
};
2005-01-16 06:15:51 +08:00
#endif /* OF_RDF */