1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2014 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * rdoff.c library of routines for manipulating rdoff files
38 /* TODO: The functions in this module assume they are running
39 * on a little-endian machine. This should be fixed to
53 * Comment this out to allow the module to read & write header record types
54 * that it isn't aware of. With this defined, unrecognised header records
55 * will generate error number 8, reported as 'unknown extended header record'.
60 /* ========================================================================
61 * Code for memory buffers (for delayed writing of header until we know
63 * ======================================================================== */
65 static memorybuffer
*newmembuf(void)
69 t
= nasm_malloc(sizeof(memorybuffer
));
78 static void membufwrite(memorybuffer
* const b
, void *data
, int bytes
)
84 if (b
->next
) { /* memory buffer full - use next buffer */
85 membufwrite(b
->next
, data
, bytes
);
89 if ((bytes
< 0 && b
->length
- bytes
> BUF_BLOCK_LEN
)
90 || (bytes
> 0 && b
->length
+ bytes
> BUF_BLOCK_LEN
)) {
92 /* buffer full and no next allocated... allocate and initialise next
94 b
->next
= newmembuf();
95 membufwrite(b
->next
, data
, bytes
);
100 case -4: /* convert to little-endian */
101 l
= *(int32_t *)data
;
102 b
->buffer
[b
->length
++] = l
& 0xFF;
104 b
->buffer
[b
->length
++] = l
& 0xFF;
106 b
->buffer
[b
->length
++] = l
& 0xFF;
108 b
->buffer
[b
->length
++] = l
& 0xFF;
112 w
= *(uint16_t *) data
;
113 b
->buffer
[b
->length
++] = w
& 0xFF;
115 b
->buffer
[b
->length
++] = w
& 0xFF;
121 b
->buffer
[b
->length
++] = *c
++;
126 static void membufdump(memorybuffer
* b
, FILE * fp
)
131 nasm_write(b
->buffer
, b
->length
, fp
);
132 membufdump(b
->next
, fp
);
135 static int membuflength(memorybuffer
* b
)
139 return b
->length
+ membuflength(b
->next
);
142 static void freemembuf(memorybuffer
* b
)
150 /* =========================================================================
151 General purpose routines and variables used by the library functions
152 ========================================================================= */
155 * translateint32_t() and translateint16_t()
157 * translate from little endian to local representation
159 int32_t translateint32_t(int32_t in
)
173 uint16_t translateint16_t(uint16_t in
)
179 r
= (i
[1] << 8) + i
[0];
185 static char *knownsegtypes
[8] = {
186 "NULL", "text", "data", "object comment",
187 "linked comment", "loader comment",
188 "symbolic debug", "line number debug"
191 /* Get a textual string describing the segment type */
192 char *translatesegmenttype(uint16_t type
)
195 return knownsegtypes
[type
];
199 return "reserved - Moscow";
201 return "reserved - system dependant";
203 return "reserved - other";
205 return "invalid type code";
206 return "type code out of range";
209 /* This signature is written to the start of RDOFF files */
210 const char *RDOFFId
= RDOFF2_SIGNATURE
;
212 /* Error messages. Must correspond to the codes defined in rdoff.h */
213 const char *rdf_errors
[11] = {
214 /* 0 */ "no error occurred",
215 /* 1 */ "could not open file",
216 /* 2 */ "invalid file format",
217 /* 3 */ "error reading file",
218 /* 4 */ "unknown error",
219 /* 5 */ "header not read",
220 /* 6 */ "out of memory",
221 /* 7 */ "RDOFF v1 not supported",
222 /* 8 */ "unknown extended header record",
223 /* 9 */ "header record of known type but unknown length",
224 /* 10 */ "no such segment"
229 /* ========================================================================
230 * Hook for nasm_error() to work
231 * ======================================================================== */
232 static void rdoff_verror(int severity
, const char *fmt
, va_list val
)
234 vfprintf(stderr
, fmt
, val
);
236 if ((severity
& ERR_MASK
) >= ERR_FATAL
)
240 void rdoff_init(void)
242 nasm_set_verror(rdoff_verror
);
245 /* ========================================================================
246 The library functions
247 ======================================================================== */
249 int rdfopen(rdffile
* f
, const char *name
)
253 fp
= fopen(name
, "rb");
255 return rdf_errno
= RDF_ERR_OPEN
;
257 return rdfopenhere(f
, fp
, NULL
, name
);
260 int rdfopenhere(rdffile
* f
, FILE * fp
, int *refcount
, const char *name
)
267 if (translateint32_t(0x01020304) != 0x01020304) {
268 /* fix this to be portable! */
269 fputs("*** this program requires a little endian machine\n",
271 fprintf(stderr
, "01020304h = %08"PRIx32
"h\n", translateint32_t(0x01020304));
279 if (fread(buf
, 1, 6, f
->fp
) != 6) {
281 return rdf_errno
= RDF_ERR_READ
;
285 if (strcmp(buf
, RDOFFId
)) {
287 if (!strcmp(buf
, "RDOFF1"))
288 return rdf_errno
= RDF_ERR_VER
;
289 return rdf_errno
= RDF_ERR_FORMAT
;
292 if (fread(&l
, 1, 4, f
->fp
) != 4
293 || fread(&f
->header_len
, 1, 4, f
->fp
) != 4) {
295 return rdf_errno
= RDF_ERR_READ
;
298 f
->header_ofs
= ftell(f
->fp
);
299 f
->eof_offset
= f
->header_ofs
+ translateint32_t(l
) - 4;
301 if (fseek(f
->fp
, f
->header_len
, SEEK_CUR
)) {
303 return rdf_errno
= RDF_ERR_FORMAT
; /* seek past end of file...? */
306 if (fread(&s
, 1, 2, f
->fp
) != 2) {
308 return rdf_errno
= RDF_ERR_READ
;
314 f
->seg
[f
->nsegs
].type
= s
;
315 if (fread(&f
->seg
[f
->nsegs
].number
, 1, 2, f
->fp
) != 2 ||
316 fread(&f
->seg
[f
->nsegs
].reserved
, 1, 2, f
->fp
) != 2 ||
317 fread(&f
->seg
[f
->nsegs
].length
, 1, 4, f
->fp
) != 4) {
319 return rdf_errno
= RDF_ERR_READ
;
322 f
->seg
[f
->nsegs
].offset
= ftell(f
->fp
);
323 if (fseek(f
->fp
, f
->seg
[f
->nsegs
].length
, SEEK_CUR
)) {
325 return rdf_errno
= RDF_ERR_FORMAT
;
329 if (fread(&s
, 1, 2, f
->fp
) != 2) {
331 return rdf_errno
= RDF_ERR_READ
;
335 if (f
->eof_offset
!= ftell(f
->fp
) + 8) { /* +8 = skip null segment header */
336 fprintf(stderr
, "warning: eof_offset [%"PRId32
"] and actual eof offset "
337 "[%ld] don't match\n", f
->eof_offset
, ftell(f
->fp
) + 8);
339 fseek(f
->fp
, initpos
, SEEK_SET
);
340 f
->header_loc
= NULL
;
342 f
->name
= nasm_strdup(name
);
343 f
->refcount
= refcount
;
349 int rdfclose(rdffile
* f
)
351 if (!f
->refcount
|| !--(*f
->refcount
)) {
361 * Print the message for last error (from rdf_errno)
363 void rdfperror(const char *app
, const char *name
)
365 fprintf(stderr
, "%s:%s: %s\n", app
, name
, rdf_errors
[rdf_errno
]);
366 if (rdf_errno
== RDF_ERR_OPEN
|| rdf_errno
== RDF_ERR_READ
) {
372 * Find the segment by its number.
373 * Returns segment array index, or -1 if segment with such number was not found.
375 int rdffindsegment(rdffile
* f
, int segno
)
378 for (i
= 0; i
< f
->nsegs
; i
++)
379 if (f
->seg
[i
].number
== segno
)
385 * Load the segment. Returns status.
387 int rdfloadseg(rdffile
* f
, int segment
, void *buffer
)
394 fpos
= f
->header_ofs
;
395 slen
= f
->header_len
;
396 f
->header_loc
= (uint8_t *) buffer
;
400 if (segment
< f
->nsegs
) {
401 fpos
= f
->seg
[segment
].offset
;
402 slen
= f
->seg
[segment
].length
;
403 f
->seg
[segment
].data
= (uint8_t *) buffer
;
405 return rdf_errno
= RDF_ERR_SEGMENT
;
409 if (fseek(f
->fp
, fpos
, SEEK_SET
))
410 return rdf_errno
= RDF_ERR_UNKNOWN
;
412 if (fread(buffer
, 1, slen
, f
->fp
) != slen
)
413 return rdf_errno
= RDF_ERR_READ
;
418 /* Macros for reading integers from header in memory */
420 #define RI8(v) v = f->header_loc[f->header_fp++]
421 #define RI16(v) { v = (f->header_loc[f->header_fp] + \
422 (f->header_loc[f->header_fp+1] << 8)); \
425 #define RI32(v) { v = (f->header_loc[f->header_fp] + \
426 (f->header_loc[f->header_fp+1] << 8) + \
427 (f->header_loc[f->header_fp+2] << 16) + \
428 (f->header_loc[f->header_fp+3] << 24)); \
431 #define RS(str,max) { for(i=0;i<max;i++){\
432 RI8(str[i]); if (!str[i]) break;} str[i]=0; }
435 * Read a header record.
436 * Returns the address of record, or NULL in case of error.
438 rdfheaderrec
*rdfgetheaderrec(rdffile
* f
)
440 static rdfheaderrec r
;
443 if (!f
->header_loc
) {
444 rdf_errno
= RDF_ERR_HEADER
;
448 if (f
->header_fp
>= f
->header_len
)
455 case RDFREC_RELOC
: /* Relocation record */
456 case RDFREC_SEGRELOC
:
457 if (r
.r
.reclen
!= 8) {
458 rdf_errno
= RDF_ERR_RECLEN
;
467 case RDFREC_IMPORT
: /* Imported symbol record */
468 case RDFREC_FARIMPORT
:
471 RS(r
.i
.label
, EXIM_LABEL_MAX
);
474 case RDFREC_GLOBAL
: /* Exported symbol record */
478 RS(r
.e
.label
, EXIM_LABEL_MAX
);
481 case RDFREC_DLL
: /* DLL record */
482 RS(r
.d
.libname
, MODLIB_NAME_MAX
);
485 case RDFREC_BSS
: /* BSS reservation record */
486 if (r
.r
.reclen
!= 4) {
487 rdf_errno
= RDF_ERR_RECLEN
;
493 case RDFREC_MODNAME
: /* Module name record */
494 RS(r
.m
.modname
, MODLIB_NAME_MAX
);
497 case RDFREC_COMMON
: /* Common variable */
501 RS(r
.c
.label
, EXIM_LABEL_MAX
);
506 rdf_errno
= RDF_ERR_RECTYPE
; /* unknown header record */
509 for (i
= 0; i
< r
.g
.reclen
; i
++)
517 * Rewind to the beginning of the file
519 void rdfheaderrewind(rdffile
* f
)
524 rdf_headerbuf
*rdfnewheader(void)
526 rdf_headerbuf
*hb
= nasm_malloc(sizeof(rdf_headerbuf
));
530 hb
->buf
= newmembuf();
537 int rdfaddheader(rdf_headerbuf
* h
, rdfheaderrec
* r
)
539 #ifndef STRICT_ERRORS
542 membufwrite(h
->buf
, &r
->type
, 1);
543 membufwrite(h
->buf
, &r
->g
.reclen
, 1);
546 case RDFREC_GENERIC
: /* generic */
547 membufwrite(h
->buf
, &r
->g
.data
, r
->g
.reclen
);
550 case RDFREC_SEGRELOC
:
551 membufwrite(h
->buf
, &r
->r
.segment
, 1);
552 membufwrite(h
->buf
, &r
->r
.offset
, -4);
553 membufwrite(h
->buf
, &r
->r
.length
, 1);
554 membufwrite(h
->buf
, &r
->r
.refseg
, -2); /* 9 bytes written */
557 case RDFREC_IMPORT
: /* import */
558 case RDFREC_FARIMPORT
:
559 membufwrite(h
->buf
, &r
->i
.flags
, 1);
560 membufwrite(h
->buf
, &r
->i
.segment
, -2);
561 membufwrite(h
->buf
, &r
->i
.label
, strlen(r
->i
.label
) + 1);
564 case RDFREC_GLOBAL
: /* export */
565 membufwrite(h
->buf
, &r
->e
.flags
, 1);
566 membufwrite(h
->buf
, &r
->e
.segment
, 1);
567 membufwrite(h
->buf
, &r
->e
.offset
, -4);
568 membufwrite(h
->buf
, &r
->e
.label
, strlen(r
->e
.label
) + 1);
571 case RDFREC_DLL
: /* DLL */
572 membufwrite(h
->buf
, &r
->d
.libname
, strlen(r
->d
.libname
) + 1);
575 case RDFREC_BSS
: /* BSS */
576 membufwrite(h
->buf
, &r
->b
.amount
, -4);
579 case RDFREC_MODNAME
: /* Module name */
580 membufwrite(h
->buf
, &r
->m
.modname
, strlen(r
->m
.modname
) + 1);
585 return rdf_errno
= RDF_ERR_RECTYPE
;
587 for (i
= 0; i
< r
->g
.reclen
; i
++)
588 membufwrite(h
->buf
, r
->g
.data
[i
], 1);
594 int rdfaddsegment(rdf_headerbuf
* h
, int32_t seglength
)
597 h
->seglength
+= seglength
;
601 int rdfwriteheader(FILE * fp
, rdf_headerbuf
* h
)
605 nasm_write(RDOFFId
, strlen(RDOFFId
), fp
);
607 l
= membuflength(h
->buf
);
608 l2
= l
+ 14 + 10 * h
->nsegments
+ h
->seglength
;
609 fwriteint32_t(l
, fp
);
610 fwriteint32_t(l2
, fp
);
612 membufdump(h
->buf
, fp
);
614 return 0; /* no error handling in here... CHANGE THIS! */
617 void rdfdoneheader(rdf_headerbuf
* h
)