2009-06-29 08:33:50 +08:00
|
|
|
/* ----------------------------------------------------------------------- *
|
2017-04-18 04:52:19 +08:00
|
|
|
*
|
2014-11-26 04:35:03 +08:00
|
|
|
* Copyright 1996-2014 The NASM Authors - All Rights Reserved
|
2009-06-29 08:33:50 +08:00
|
|
|
* See the file AUTHORS included with the NASM distribution for
|
|
|
|
* the specific copyright holders.
|
2002-05-01 04:51:32 +08:00
|
|
|
*
|
2009-06-29 08:33:50 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
2002-05-01 04:58:18 +08:00
|
|
|
*
|
2009-06-29 08:33:50 +08:00
|
|
|
* * 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.
|
2017-04-18 04:52:19 +08:00
|
|
|
*
|
2009-06-29 08:33:50 +08:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rdoff.c library of routines for manipulating rdoff files
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* TODO: The functions in this module assume they are running
|
|
|
|
* on a little-endian machine. This should be fixed to
|
|
|
|
* make it portable.
|
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2002-05-01 04:52:08 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2004-09-15 14:54:34 +08:00
|
|
|
|
2017-04-18 04:52:19 +08:00
|
|
|
#include "rdfutils.h"
|
2002-05-01 04:52:08 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
/*
|
|
|
|
* Comment this out to allow the module to read & write header record types
|
|
|
|
* that it isn't aware of. With this defined, unrecognised header records
|
|
|
|
* will generate error number 8, reported as 'unknown extended header record'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define STRICT_ERRORS
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/* ========================================================================
|
|
|
|
* Code for memory buffers (for delayed writing of header until we know
|
2007-04-12 10:40:54 +08:00
|
|
|
* how int32_t it is).
|
2002-05-01 04:51:32 +08:00
|
|
|
* ======================================================================== */
|
|
|
|
|
2016-07-15 05:15:41 +08:00
|
|
|
static memorybuffer *newmembuf(void)
|
2002-05-01 04:58:18 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
memorybuffer *t;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2017-04-18 04:52:19 +08:00
|
|
|
t = nasm_malloc(sizeof(memorybuffer));
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!t)
|
|
|
|
return NULL;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
t->length = 0;
|
|
|
|
t->next = NULL;
|
|
|
|
return t;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2014-11-26 04:35:03 +08:00
|
|
|
static void membufwrite(memorybuffer * const b, void *data, int bytes)
|
2002-05-01 04:58:18 +08:00
|
|
|
{
|
2007-04-13 00:54:50 +08:00
|
|
|
uint16_t w;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t l;
|
2007-11-19 04:01:05 +08:00
|
|
|
char *c;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (b->next) { /* memory buffer full - use next buffer */
|
|
|
|
membufwrite(b->next, data, bytes);
|
|
|
|
return;
|
2002-05-01 04:58:18 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
if ((bytes < 0 && b->length - bytes > BUF_BLOCK_LEN)
|
2005-01-16 06:15:51 +08:00
|
|
|
|| (bytes > 0 && b->length + bytes > BUF_BLOCK_LEN)) {
|
|
|
|
|
|
|
|
/* buffer full and no next allocated... allocate and initialise next
|
|
|
|
* buffer */
|
|
|
|
b->next = newmembuf();
|
|
|
|
membufwrite(b->next, data, bytes);
|
|
|
|
return;
|
2002-05-01 04:58:18 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
switch (bytes) {
|
|
|
|
case -4: /* convert to little-endian */
|
2007-04-12 10:40:54 +08:00
|
|
|
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;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
case -2:
|
2007-04-13 00:54:50 +08:00
|
|
|
w = *(uint16_t *) data;
|
2005-01-16 06:15:51 +08:00
|
|
|
b->buffer[b->length++] = w & 0xFF;
|
|
|
|
w >>= 8;
|
|
|
|
b->buffer[b->length++] = w & 0xFF;
|
|
|
|
break;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
default:
|
2007-11-19 04:01:05 +08:00
|
|
|
c = data;
|
|
|
|
while (bytes--)
|
|
|
|
b->buffer[b->length++] = *c++;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 04:35:03 +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;
|
|
|
|
|
2017-04-18 04:52:19 +08:00
|
|
|
nasm_write(b->buffer, b->length, fp);
|
2005-01-16 06:15:51 +08:00
|
|
|
membufdump(b->next, fp);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2014-11-26 04:35:03 +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;
|
2002-05-01 04:58:18 +08:00
|
|
|
return b->length + membuflength(b->next);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2014-11-26 04:35:03 +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;
|
2002-05-01 04:58:18 +08:00
|
|
|
freemembuf(b->next);
|
2017-04-18 04:52:19 +08:00
|
|
|
nasm_free(b);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* =========================================================================
|
|
|
|
General purpose routines and variables used by the library functions
|
|
|
|
========================================================================= */
|
|
|
|
|
2007-10-20 05:42:29 +08:00
|
|
|
/*
|
2007-04-12 10:40:54 +08:00
|
|
|
* translateint32_t() and translateint16_t()
|
2002-05-01 04:58:18 +08:00
|
|
|
*
|
2007-10-20 05:42:29 +08:00
|
|
|
* translate from little endian to local representation
|
2002-05-01 04:58:18 +08:00
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t translateint32_t(int32_t in)
|
2002-05-01 04:58:18 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t r;
|
|
|
|
uint8_t *i;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
i = (uint8_t *)∈
|
2002-05-01 04:58:18 +08:00
|
|
|
r = i[3];
|
|
|
|
r = (r << 8) + i[2];
|
|
|
|
r = (r << 8) + i[1];
|
|
|
|
r = (r << 8) + *i;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
return r;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-04-13 00:54:50 +08:00
|
|
|
uint16_t translateint16_t(uint16_t in)
|
2002-05-01 04:58:18 +08:00
|
|
|
{
|
2007-04-13 00:54:50 +08:00
|
|
|
uint16_t r;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t *i;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
i = (uint8_t *)∈
|
2002-05-01 04:58:18 +08:00
|
|
|
r = (i[1] << 8) + i[0];
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2004-09-15 14:54:34 +08:00
|
|
|
/* Segment types */
|
2007-04-14 00:47:53 +08:00
|
|
|
static char *knownsegtypes[8] = {
|
2004-09-15 14:54:34 +08:00
|
|
|
"NULL", "text", "data", "object comment",
|
|
|
|
"linked comment", "loader comment",
|
|
|
|
"symbolic debug", "line number debug"
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Get a textual string describing the segment type */
|
2007-04-14 00:47:53 +08:00
|
|
|
char *translatesegmenttype(uint16_t type)
|
2004-09-15 14:54:34 +08:00
|
|
|
{
|
|
|
|
if (type < 8)
|
2005-01-16 06:15:51 +08:00
|
|
|
return knownsegtypes[type];
|
2004-09-15 14:54:34 +08:00
|
|
|
if (type < 0x0020)
|
2005-01-16 06:15:51 +08:00
|
|
|
return "reserved";
|
2004-09-15 14:54:34 +08:00
|
|
|
if (type < 0x1000)
|
2005-01-16 06:15:51 +08:00
|
|
|
return "reserved - Moscow";
|
2004-09-15 14:54:34 +08:00
|
|
|
if (type < 0x8000)
|
2005-01-16 06:15:51 +08:00
|
|
|
return "reserved - system dependant";
|
2004-09-15 14:54:34 +08:00
|
|
|
if (type < 0xFFFF)
|
2005-01-16 06:15:51 +08:00
|
|
|
return "reserved - other";
|
2004-09-15 14:54:34 +08:00
|
|
|
if (type == 0xFFFF)
|
2005-01-16 06:15:51 +08:00
|
|
|
return "invalid type code";
|
2004-09-15 14:54:34 +08:00
|
|
|
return "type code out of range";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This signature is written to the start of RDOFF files */
|
2007-04-14 00:47:53 +08:00
|
|
|
const char *RDOFFId = RDOFF2_SIGNATURE;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2004-09-15 14:54:34 +08:00
|
|
|
/* Error messages. Must correspond to the codes defined in rdoff.h */
|
2007-04-14 00:47:53 +08:00
|
|
|
const char *rdf_errors[11] = {
|
2005-01-16 06:15:51 +08:00
|
|
|
/* 0 */ "no error occurred",
|
|
|
|
/* 1 */ "could not open file",
|
|
|
|
/* 2 */ "invalid file format",
|
|
|
|
/* 3 */ "error reading file",
|
|
|
|
/* 4 */ "unknown error",
|
|
|
|
/* 5 */ "header not read",
|
|
|
|
/* 6 */ "out of memory",
|
|
|
|
/* 7 */ "RDOFF v1 not supported",
|
|
|
|
/* 8 */ "unknown extended header record",
|
|
|
|
/* 9 */ "header record of known type but unknown length",
|
|
|
|
/* 10 */ "no such segment"
|
2004-09-15 14:54:34 +08:00
|
|
|
};
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
int rdf_errno = 0;
|
|
|
|
|
2017-04-18 04:52:19 +08:00
|
|
|
/* ========================================================================
|
|
|
|
* Hook for nasm_error() to work
|
|
|
|
* ======================================================================== */
|
|
|
|
static void rdoff_verror(int severity, const char *fmt, va_list val)
|
|
|
|
{
|
|
|
|
vfprintf(stderr, fmt, val);
|
|
|
|
|
|
|
|
if ((severity & ERR_MASK) >= ERR_FATAL)
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdoff_init(void)
|
|
|
|
{
|
|
|
|
nasm_set_verror(rdoff_verror);
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/* ========================================================================
|
|
|
|
The library functions
|
|
|
|
======================================================================== */
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
int rdfopen(rdffile * f, const char *name)
|
2002-05-01 04:52:08 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
FILE *fp;
|
2002-05-01 04:52:08 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
fp = fopen(name, "rb");
|
2004-09-15 14:54:34 +08:00
|
|
|
if (!fp)
|
2005-01-16 06:15:51 +08:00
|
|
|
return rdf_errno = RDF_ERR_OPEN;
|
2002-05-01 04:52:08 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
return rdfopenhere(f, fp, NULL, name);
|
2002-05-01 04:52:08 +08:00
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
int rdfopenhere(rdffile * f, FILE * fp, int *refcount, const char *name)
|
2002-05-01 04:51:32 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char buf[8];
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t initpos;
|
|
|
|
int32_t l;
|
2007-04-13 00:54:50 +08:00
|
|
|
uint16_t s;
|
2004-09-15 14:54:34 +08:00
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
if (translateint32_t(0x01020304) != 0x01020304) {
|
2004-09-15 14:54:34 +08:00
|
|
|
/* fix this to be portable! */
|
2005-01-16 06:15:51 +08:00
|
|
|
fputs("*** this program requires a little endian machine\n",
|
|
|
|
stderr);
|
2007-04-14 09:24:14 +08:00
|
|
|
fprintf(stderr, "01020304h = %08"PRIx32"h\n", translateint32_t(0x01020304));
|
2004-09-15 14:54:34 +08:00
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
f->fp = fp;
|
|
|
|
initpos = ftell(fp);
|
|
|
|
|
2017-04-18 05:25:13 +08:00
|
|
|
/* read header */
|
|
|
|
if (fread(buf, 1, 6, f->fp) != 6) {
|
|
|
|
fclose(f->fp);
|
|
|
|
return rdf_errno = RDF_ERR_READ;
|
|
|
|
}
|
2004-09-15 14:54:34 +08:00
|
|
|
buf[6] = 0;
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (strcmp(buf, RDOFFId)) {
|
|
|
|
fclose(f->fp);
|
|
|
|
if (!strcmp(buf, "RDOFF1"))
|
|
|
|
return rdf_errno = RDF_ERR_VER;
|
|
|
|
return rdf_errno = RDF_ERR_FORMAT;
|
2004-09-15 14:54:34 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (fread(&l, 1, 4, f->fp) != 4
|
|
|
|
|| fread(&f->header_len, 1, 4, f->fp) != 4) {
|
|
|
|
fclose(f->fp);
|
|
|
|
return rdf_errno = RDF_ERR_READ;
|
2004-09-15 14:54:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
f->header_ofs = ftell(f->fp);
|
2007-04-12 10:40:54 +08:00
|
|
|
f->eof_offset = f->header_ofs + translateint32_t(l) - 4;
|
2004-09-15 14:54:34 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (fseek(f->fp, f->header_len, SEEK_CUR)) {
|
|
|
|
fclose(f->fp);
|
|
|
|
return rdf_errno = RDF_ERR_FORMAT; /* seek past end of file...? */
|
2004-09-15 14:54:34 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (fread(&s, 1, 2, f->fp) != 2) {
|
|
|
|
fclose(f->fp);
|
|
|
|
return rdf_errno = RDF_ERR_READ;
|
2004-09-15 14:54:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
f->nsegs = 0;
|
|
|
|
|
|
|
|
while (s != 0) {
|
2005-01-16 06:15:51 +08:00
|
|
|
f->seg[f->nsegs].type = s;
|
|
|
|
if (fread(&f->seg[f->nsegs].number, 1, 2, f->fp) != 2 ||
|
|
|
|
fread(&f->seg[f->nsegs].reserved, 1, 2, f->fp) != 2 ||
|
|
|
|
fread(&f->seg[f->nsegs].length, 1, 4, f->fp) != 4) {
|
|
|
|
fclose(f->fp);
|
|
|
|
return rdf_errno = RDF_ERR_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
f->seg[f->nsegs].offset = ftell(f->fp);
|
|
|
|
if (fseek(f->fp, f->seg[f->nsegs].length, SEEK_CUR)) {
|
|
|
|
fclose(f->fp);
|
|
|
|
return rdf_errno = RDF_ERR_FORMAT;
|
|
|
|
}
|
|
|
|
f->nsegs++;
|
|
|
|
|
|
|
|
if (fread(&s, 1, 2, f->fp) != 2) {
|
|
|
|
fclose(f->fp);
|
|
|
|
return rdf_errno = RDF_ERR_READ;
|
|
|
|
}
|
2004-09-15 14:54:34 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (f->eof_offset != ftell(f->fp) + 8) { /* +8 = skip null segment header */
|
2007-04-14 09:24:14 +08:00
|
|
|
fprintf(stderr, "warning: eof_offset [%"PRId32"] and actual eof offset "
|
2005-01-16 06:15:51 +08:00
|
|
|
"[%ld] don't match\n", f->eof_offset, ftell(f->fp) + 8);
|
2004-09-15 14:54:34 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
fseek(f->fp, initpos, SEEK_SET);
|
2004-09-15 14:54:34 +08:00
|
|
|
f->header_loc = NULL;
|
|
|
|
|
2017-04-18 04:52:19 +08:00
|
|
|
f->name = nasm_strdup(name);
|
2004-09-15 14:54:34 +08:00
|
|
|
f->refcount = refcount;
|
2005-01-16 06:15:51 +08:00
|
|
|
if (refcount)
|
|
|
|
(*refcount)++;
|
2004-09-15 14:54:34 +08:00
|
|
|
return RDF_OK;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
int rdfclose(rdffile * f)
|
2002-05-01 04:51:32 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
if (!f->refcount || !--(*f->refcount)) {
|
|
|
|
fclose(f->fp);
|
|
|
|
f->fp = NULL;
|
2004-09-15 14:54:34 +08:00
|
|
|
}
|
2017-04-18 04:52:19 +08:00
|
|
|
nasm_free(f->name);
|
2002-05-01 04:52:08 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-09-15 14:54:34 +08:00
|
|
|
/*
|
|
|
|
* Print the message for last error (from rdf_errno)
|
|
|
|
*/
|
2007-04-14 00:47:53 +08:00
|
|
|
void rdfperror(const char *app, const char *name)
|
2002-05-01 04:51:32 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
fprintf(stderr, "%s:%s: %s\n", app, name, rdf_errors[rdf_errno]);
|
2004-09-15 14:54:34 +08:00
|
|
|
if (rdf_errno == RDF_ERR_OPEN || rdf_errno == RDF_ERR_READ) {
|
2005-01-16 06:15:51 +08:00
|
|
|
perror(app);
|
2004-09-15 14:54:34 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2004-09-15 14:54:34 +08:00
|
|
|
/*
|
|
|
|
* Find the segment by its number.
|
|
|
|
* Returns segment array index, or -1 if segment with such number was not found.
|
|
|
|
*/
|
2002-05-01 04:58:18 +08:00
|
|
|
int rdffindsegment(rdffile * f, int segno)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < f->nsegs; i++)
|
2005-01-16 06:15:51 +08:00
|
|
|
if (f->seg[i].number == segno)
|
|
|
|
return i;
|
2002-05-01 04:58:18 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-09-15 14:54:34 +08:00
|
|
|
/*
|
|
|
|
* Load the segment. Returns status.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
int rdfloadseg(rdffile * f, int segment, void *buffer)
|
2002-05-01 04:51:32 +08:00
|
|
|
{
|
2007-11-19 04:01:05 +08:00
|
|
|
int32_t fpos;
|
|
|
|
size_t slen;
|
2004-09-15 14:54:34 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
switch (segment) {
|
|
|
|
case RDOFF_HEADER:
|
|
|
|
fpos = f->header_ofs;
|
|
|
|
slen = f->header_len;
|
2007-04-13 00:54:50 +08:00
|
|
|
f->header_loc = (uint8_t *) buffer;
|
2005-01-16 06:15:51 +08:00
|
|
|
f->header_fp = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (segment < f->nsegs) {
|
|
|
|
fpos = f->seg[segment].offset;
|
|
|
|
slen = f->seg[segment].length;
|
2007-04-13 00:54:50 +08:00
|
|
|
f->seg[segment].data = (uint8_t *) buffer;
|
2005-01-16 06:15:51 +08:00
|
|
|
} else {
|
|
|
|
return rdf_errno = RDF_ERR_SEGMENT;
|
|
|
|
}
|
2004-09-15 14:54:34 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (fseek(f->fp, fpos, SEEK_SET))
|
|
|
|
return rdf_errno = RDF_ERR_UNKNOWN;
|
|
|
|
|
|
|
|
if (fread(buffer, 1, slen, f->fp) != slen)
|
|
|
|
return rdf_errno = RDF_ERR_READ;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2004-09-15 14:54:34 +08:00
|
|
|
return RDF_OK;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Macros for reading integers from header in memory */
|
|
|
|
|
|
|
|
#define RI8(v) v = f->header_loc[f->header_fp++]
|
|
|
|
#define RI16(v) { v = (f->header_loc[f->header_fp] + \
|
|
|
|
(f->header_loc[f->header_fp+1] << 8)); \
|
|
|
|
f->header_fp += 2; }
|
|
|
|
|
|
|
|
#define RI32(v) { v = (f->header_loc[f->header_fp] + \
|
|
|
|
(f->header_loc[f->header_fp+1] << 8) + \
|
|
|
|
(f->header_loc[f->header_fp+2] << 16) + \
|
|
|
|
(f->header_loc[f->header_fp+3] << 24)); \
|
|
|
|
f->header_fp += 4; }
|
|
|
|
|
|
|
|
#define RS(str,max) { for(i=0;i<max;i++){\
|
|
|
|
RI8(str[i]); if (!str[i]) break;} str[i]=0; }
|
|
|
|
|
2004-09-15 14:54:34 +08:00
|
|
|
/*
|
|
|
|
* Read a header record.
|
|
|
|
* Returns the address of record, or NULL in case of error.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
rdfheaderrec *rdfgetheaderrec(rdffile * f)
|
2002-05-01 04:51:32 +08:00
|
|
|
{
|
2004-09-15 14:54:34 +08:00
|
|
|
static rdfheaderrec r;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!f->header_loc) {
|
2005-01-16 06:15:51 +08:00
|
|
|
rdf_errno = RDF_ERR_HEADER;
|
|
|
|
return NULL;
|
2004-09-15 14:54:34 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (f->header_fp >= f->header_len)
|
|
|
|
return 0;
|
2004-09-15 14:54:34 +08:00
|
|
|
|
|
|
|
RI8(r.type);
|
|
|
|
RI8(r.g.reclen);
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
switch (r.type) {
|
|
|
|
case RDFREC_RELOC: /* Relocation record */
|
|
|
|
case RDFREC_SEGRELOC:
|
|
|
|
if (r.r.reclen != 8) {
|
|
|
|
rdf_errno = RDF_ERR_RECLEN;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
RI8(r.r.segment);
|
|
|
|
RI32(r.r.offset);
|
|
|
|
RI8(r.r.length);
|
|
|
|
RI16(r.r.refseg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RDFREC_IMPORT: /* Imported symbol record */
|
|
|
|
case RDFREC_FARIMPORT:
|
|
|
|
RI8(r.i.flags);
|
|
|
|
RI16(r.i.segment);
|
|
|
|
RS(r.i.label, EXIM_LABEL_MAX);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RDFREC_GLOBAL: /* Exported symbol record */
|
|
|
|
RI8(r.e.flags);
|
|
|
|
RI8(r.e.segment);
|
|
|
|
RI32(r.e.offset);
|
|
|
|
RS(r.e.label, EXIM_LABEL_MAX);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RDFREC_DLL: /* DLL record */
|
|
|
|
RS(r.d.libname, MODLIB_NAME_MAX);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RDFREC_BSS: /* BSS reservation record */
|
|
|
|
if (r.r.reclen != 4) {
|
|
|
|
rdf_errno = RDF_ERR_RECLEN;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
RI32(r.b.amount);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RDFREC_MODNAME: /* Module name record */
|
|
|
|
RS(r.m.modname, MODLIB_NAME_MAX);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RDFREC_COMMON: /* Common variable */
|
|
|
|
RI16(r.c.segment);
|
|
|
|
RI32(r.c.size);
|
|
|
|
RI16(r.c.align);
|
|
|
|
RS(r.c.label, EXIM_LABEL_MAX);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2002-05-01 04:58:18 +08:00
|
|
|
#ifdef STRICT_ERRORS
|
2005-01-16 06:15:51 +08:00
|
|
|
rdf_errno = RDF_ERR_RECTYPE; /* unknown header record */
|
|
|
|
return NULL;
|
2002-05-01 04:58:18 +08:00
|
|
|
#else
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < r.g.reclen; i++)
|
|
|
|
RI8(r.g.data[i]);
|
2002-05-01 04:58:18 +08:00
|
|
|
#endif
|
2004-09-15 14:54:34 +08:00
|
|
|
}
|
|
|
|
return &r;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2004-09-15 14:54:34 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Rewind to the beginning of the file
|
2005-01-16 06:15:51 +08:00
|
|
|
*/
|
|
|
|
void rdfheaderrewind(rdffile * f)
|
2002-05-01 04:51:32 +08:00
|
|
|
{
|
2004-09-15 14:54:34 +08:00
|
|
|
f->header_fp = 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
rdf_headerbuf *rdfnewheader(void)
|
2002-05-01 04:51:32 +08:00
|
|
|
{
|
2017-04-18 04:52:19 +08:00
|
|
|
rdf_headerbuf *hb = nasm_malloc(sizeof(rdf_headerbuf));
|
2005-01-16 06:15:51 +08:00
|
|
|
if (hb == NULL)
|
|
|
|
return NULL;
|
2002-05-01 04:58:18 +08:00
|
|
|
|
|
|
|
hb->buf = newmembuf();
|
|
|
|
hb->nsegments = 0;
|
|
|
|
hb->seglength = 0;
|
|
|
|
|
|
|
|
return hb;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int rdfaddheader(rdf_headerbuf * h, rdfheaderrec * r)
|
|
|
|
{
|
2002-05-01 04:58:18 +08:00
|
|
|
#ifndef STRICT_ERRORS
|
|
|
|
int i;
|
|
|
|
#endif
|
2005-01-16 06:15:51 +08:00
|
|
|
membufwrite(h->buf, &r->type, 1);
|
|
|
|
membufwrite(h->buf, &r->g.reclen, 1);
|
2002-05-01 04:58:18 +08:00
|
|
|
|
2004-09-15 14:54:34 +08:00
|
|
|
switch (r->type) {
|
2005-01-16 06:15:51 +08:00
|
|
|
case RDFREC_GENERIC: /* generic */
|
|
|
|
membufwrite(h->buf, &r->g.data, r->g.reclen);
|
|
|
|
break;
|
2002-12-04 08:49:37 +08:00
|
|
|
case RDFREC_RELOC:
|
|
|
|
case RDFREC_SEGRELOC:
|
2005-01-16 06:15:51 +08:00
|
|
|
membufwrite(h->buf, &r->r.segment, 1);
|
|
|
|
membufwrite(h->buf, &r->r.offset, -4);
|
|
|
|
membufwrite(h->buf, &r->r.length, 1);
|
|
|
|
membufwrite(h->buf, &r->r.refseg, -2); /* 9 bytes written */
|
|
|
|
break;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
case RDFREC_IMPORT: /* import */
|
2002-12-04 08:49:37 +08:00
|
|
|
case RDFREC_FARIMPORT:
|
2005-01-16 06:15:51 +08:00
|
|
|
membufwrite(h->buf, &r->i.flags, 1);
|
|
|
|
membufwrite(h->buf, &r->i.segment, -2);
|
|
|
|
membufwrite(h->buf, &r->i.label, strlen(r->i.label) + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RDFREC_GLOBAL: /* export */
|
|
|
|
membufwrite(h->buf, &r->e.flags, 1);
|
|
|
|
membufwrite(h->buf, &r->e.segment, 1);
|
|
|
|
membufwrite(h->buf, &r->e.offset, -4);
|
|
|
|
membufwrite(h->buf, &r->e.label, strlen(r->e.label) + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RDFREC_DLL: /* DLL */
|
|
|
|
membufwrite(h->buf, &r->d.libname, strlen(r->d.libname) + 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RDFREC_BSS: /* BSS */
|
|
|
|
membufwrite(h->buf, &r->b.amount, -4);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RDFREC_MODNAME: /* Module name */
|
|
|
|
membufwrite(h->buf, &r->m.modname, strlen(r->m.modname) + 1);
|
|
|
|
break;
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
default:
|
2002-05-01 04:58:18 +08:00
|
|
|
#ifdef STRICT_ERRORS
|
2005-01-16 06:15:51 +08:00
|
|
|
return rdf_errno = RDF_ERR_RECTYPE;
|
2002-05-01 04:58:18 +08:00
|
|
|
#else
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < r->g.reclen; i++)
|
|
|
|
membufwrite(h->buf, r->g.data[i], 1);
|
2002-05-01 04:58:18 +08:00
|
|
|
#endif
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
int rdfaddsegment(rdf_headerbuf * h, int32_t seglength)
|
2002-05-01 04:58:18 +08:00
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
h->nsegments++;
|
2002-05-01 04:58:18 +08:00
|
|
|
h->seglength += seglength;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
int rdfwriteheader(FILE * fp, rdf_headerbuf * h)
|
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t l, l2;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2017-04-18 04:52:19 +08:00
|
|
|
nasm_write(RDOFFId, strlen(RDOFFId), fp);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
l = membuflength(h->buf);
|
|
|
|
l2 = l + 14 + 10 * h->nsegments + h->seglength;
|
2017-04-18 04:52:19 +08:00
|
|
|
fwriteint32_t(l, fp);
|
|
|
|
fwriteint32_t(l2, fp);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:58:18 +08:00
|
|
|
membufdump(h->buf, fp);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
return 0; /* no error handling in here... CHANGE THIS! */
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void rdfdoneheader(rdf_headerbuf * h)
|
|
|
|
{
|
2002-05-01 04:58:18 +08:00
|
|
|
freemembuf(h->buf);
|
2017-04-18 04:52:19 +08:00
|
|
|
nasm_free(h);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|