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
55 #define newstr(str) strcpy(malloc(strlen(str) + 1),str)
56 #define newstrcat(s1,s2) strcat(strcpy(malloc(strlen(s1) + strlen(s2) + 1), \
60 * Comment this out to allow the module to read & write header record types
61 * that it isn't aware of. With this defined, unrecognised header records
62 * will generate error number 8, reported as 'unknown extended header record'.
67 /* ========================================================================
68 * Code for memory buffers (for delayed writing of header until we know
70 * ======================================================================== */
72 static memorybuffer
*newmembuf()
76 t
= malloc(sizeof(memorybuffer
));
85 static void membufwrite(memorybuffer
* const b
, void *data
, int bytes
)
91 if (b
->next
) { /* memory buffer full - use next buffer */
92 membufwrite(b
->next
, data
, bytes
);
96 if ((bytes
< 0 && b
->length
- bytes
> BUF_BLOCK_LEN
)
97 || (bytes
> 0 && b
->length
+ bytes
> BUF_BLOCK_LEN
)) {
99 /* buffer full and no next allocated... allocate and initialise next
101 b
->next
= newmembuf();
102 membufwrite(b
->next
, data
, bytes
);
107 case -4: /* convert to little-endian */
108 l
= *(int32_t *)data
;
109 b
->buffer
[b
->length
++] = l
& 0xFF;
111 b
->buffer
[b
->length
++] = l
& 0xFF;
113 b
->buffer
[b
->length
++] = l
& 0xFF;
115 b
->buffer
[b
->length
++] = l
& 0xFF;
119 w
= *(uint16_t *) data
;
120 b
->buffer
[b
->length
++] = w
& 0xFF;
122 b
->buffer
[b
->length
++] = w
& 0xFF;
128 b
->buffer
[b
->length
++] = *c
++;
133 static void membufdump(memorybuffer
* b
, FILE * fp
)
138 fwrite(b
->buffer
, 1, b
->length
, fp
);
140 membufdump(b
->next
, fp
);
143 static int membuflength(memorybuffer
* b
)
147 return b
->length
+ membuflength(b
->next
);
150 static void freemembuf(memorybuffer
* b
)
158 /* =========================================================================
159 General purpose routines and variables used by the library functions
160 ========================================================================= */
163 * translateint32_t() and translateint16_t()
165 * translate from little endian to local representation
167 int32_t translateint32_t(int32_t in
)
181 uint16_t translateint16_t(uint16_t in
)
187 r
= (i
[1] << 8) + i
[0];
193 static char *knownsegtypes
[8] = {
194 "NULL", "text", "data", "object comment",
195 "linked comment", "loader comment",
196 "symbolic debug", "line number debug"
199 /* Get a textual string describing the segment type */
200 char *translatesegmenttype(uint16_t type
)
203 return knownsegtypes
[type
];
207 return "reserved - Moscow";
209 return "reserved - system dependant";
211 return "reserved - other";
213 return "invalid type code";
214 return "type code out of range";
217 /* This signature is written to the start of RDOFF files */
218 const char *RDOFFId
= RDOFF2_SIGNATURE
;
220 /* Error messages. Must correspond to the codes defined in rdoff.h */
221 const char *rdf_errors
[11] = {
222 /* 0 */ "no error occurred",
223 /* 1 */ "could not open file",
224 /* 2 */ "invalid file format",
225 /* 3 */ "error reading file",
226 /* 4 */ "unknown error",
227 /* 5 */ "header not read",
228 /* 6 */ "out of memory",
229 /* 7 */ "RDOFF v1 not supported",
230 /* 8 */ "unknown extended header record",
231 /* 9 */ "header record of known type but unknown length",
232 /* 10 */ "no such segment"
237 /* ========================================================================
238 The library functions
239 ======================================================================== */
241 int rdfopen(rdffile
* f
, const char *name
)
245 fp
= fopen(name
, "rb");
247 return rdf_errno
= RDF_ERR_OPEN
;
249 return rdfopenhere(f
, fp
, NULL
, name
);
252 int rdfopenhere(rdffile
* f
, FILE * fp
, int *refcount
, const char *name
)
259 if (translateint32_t(0x01020304) != 0x01020304) {
260 /* fix this to be portable! */
261 fputs("*** this program requires a little endian machine\n",
263 fprintf(stderr
, "01020304h = %08"PRIx32
"h\n", translateint32_t(0x01020304));
270 fread(buf
, 6, 1, f
->fp
); /* read header */
273 if (strcmp(buf
, RDOFFId
)) {
275 if (!strcmp(buf
, "RDOFF1"))
276 return rdf_errno
= RDF_ERR_VER
;
277 return rdf_errno
= RDF_ERR_FORMAT
;
280 if (fread(&l
, 1, 4, f
->fp
) != 4
281 || fread(&f
->header_len
, 1, 4, f
->fp
) != 4) {
283 return rdf_errno
= RDF_ERR_READ
;
286 f
->header_ofs
= ftell(f
->fp
);
287 f
->eof_offset
= f
->header_ofs
+ translateint32_t(l
) - 4;
289 if (fseek(f
->fp
, f
->header_len
, SEEK_CUR
)) {
291 return rdf_errno
= RDF_ERR_FORMAT
; /* seek past end of file...? */
294 if (fread(&s
, 1, 2, f
->fp
) != 2) {
296 return rdf_errno
= RDF_ERR_READ
;
302 f
->seg
[f
->nsegs
].type
= s
;
303 if (fread(&f
->seg
[f
->nsegs
].number
, 1, 2, f
->fp
) != 2 ||
304 fread(&f
->seg
[f
->nsegs
].reserved
, 1, 2, f
->fp
) != 2 ||
305 fread(&f
->seg
[f
->nsegs
].length
, 1, 4, f
->fp
) != 4) {
307 return rdf_errno
= RDF_ERR_READ
;
310 f
->seg
[f
->nsegs
].offset
= ftell(f
->fp
);
311 if (fseek(f
->fp
, f
->seg
[f
->nsegs
].length
, SEEK_CUR
)) {
313 return rdf_errno
= RDF_ERR_FORMAT
;
317 if (fread(&s
, 1, 2, f
->fp
) != 2) {
319 return rdf_errno
= RDF_ERR_READ
;
323 if (f
->eof_offset
!= ftell(f
->fp
) + 8) { /* +8 = skip null segment header */
324 fprintf(stderr
, "warning: eof_offset [%"PRId32
"] and actual eof offset "
325 "[%ld] don't match\n", f
->eof_offset
, ftell(f
->fp
) + 8);
327 fseek(f
->fp
, initpos
, SEEK_SET
);
328 f
->header_loc
= NULL
;
330 f
->name
= newstr(name
);
331 f
->refcount
= refcount
;
337 int rdfclose(rdffile
* f
)
339 if (!f
->refcount
|| !--(*f
->refcount
)) {
349 * Print the message for last error (from rdf_errno)
351 void rdfperror(const char *app
, const char *name
)
353 fprintf(stderr
, "%s:%s: %s\n", app
, name
, rdf_errors
[rdf_errno
]);
354 if (rdf_errno
== RDF_ERR_OPEN
|| rdf_errno
== RDF_ERR_READ
) {
360 * Find the segment by its number.
361 * Returns segment array index, or -1 if segment with such number was not found.
363 int rdffindsegment(rdffile
* f
, int segno
)
366 for (i
= 0; i
< f
->nsegs
; i
++)
367 if (f
->seg
[i
].number
== segno
)
373 * Load the segment. Returns status.
375 int rdfloadseg(rdffile
* f
, int segment
, void *buffer
)
382 fpos
= f
->header_ofs
;
383 slen
= f
->header_len
;
384 f
->header_loc
= (uint8_t *) buffer
;
388 if (segment
< f
->nsegs
) {
389 fpos
= f
->seg
[segment
].offset
;
390 slen
= f
->seg
[segment
].length
;
391 f
->seg
[segment
].data
= (uint8_t *) buffer
;
393 return rdf_errno
= RDF_ERR_SEGMENT
;
397 if (fseek(f
->fp
, fpos
, SEEK_SET
))
398 return rdf_errno
= RDF_ERR_UNKNOWN
;
400 if (fread(buffer
, 1, slen
, f
->fp
) != slen
)
401 return rdf_errno
= RDF_ERR_READ
;
406 /* Macros for reading integers from header in memory */
408 #define RI8(v) v = f->header_loc[f->header_fp++]
409 #define RI16(v) { v = (f->header_loc[f->header_fp] + \
410 (f->header_loc[f->header_fp+1] << 8)); \
413 #define RI32(v) { v = (f->header_loc[f->header_fp] + \
414 (f->header_loc[f->header_fp+1] << 8) + \
415 (f->header_loc[f->header_fp+2] << 16) + \
416 (f->header_loc[f->header_fp+3] << 24)); \
419 #define RS(str,max) { for(i=0;i<max;i++){\
420 RI8(str[i]); if (!str[i]) break;} str[i]=0; }
423 * Read a header record.
424 * Returns the address of record, or NULL in case of error.
426 rdfheaderrec
*rdfgetheaderrec(rdffile
* f
)
428 static rdfheaderrec r
;
431 if (!f
->header_loc
) {
432 rdf_errno
= RDF_ERR_HEADER
;
436 if (f
->header_fp
>= f
->header_len
)
443 case RDFREC_RELOC
: /* Relocation record */
444 case RDFREC_SEGRELOC
:
445 if (r
.r
.reclen
!= 8) {
446 rdf_errno
= RDF_ERR_RECLEN
;
455 case RDFREC_IMPORT
: /* Imported symbol record */
456 case RDFREC_FARIMPORT
:
459 RS(r
.i
.label
, EXIM_LABEL_MAX
);
462 case RDFREC_GLOBAL
: /* Exported symbol record */
466 RS(r
.e
.label
, EXIM_LABEL_MAX
);
469 case RDFREC_DLL
: /* DLL record */
470 RS(r
.d
.libname
, MODLIB_NAME_MAX
);
473 case RDFREC_BSS
: /* BSS reservation record */
474 if (r
.r
.reclen
!= 4) {
475 rdf_errno
= RDF_ERR_RECLEN
;
481 case RDFREC_MODNAME
: /* Module name record */
482 RS(r
.m
.modname
, MODLIB_NAME_MAX
);
485 case RDFREC_COMMON
: /* Common variable */
489 RS(r
.c
.label
, EXIM_LABEL_MAX
);
494 rdf_errno
= RDF_ERR_RECTYPE
; /* unknown header record */
497 for (i
= 0; i
< r
.g
.reclen
; i
++)
505 * Rewind to the beginning of the file
507 void rdfheaderrewind(rdffile
* f
)
512 rdf_headerbuf
*rdfnewheader(void)
514 rdf_headerbuf
*hb
= malloc(sizeof(rdf_headerbuf
));
518 hb
->buf
= newmembuf();
525 int rdfaddheader(rdf_headerbuf
* h
, rdfheaderrec
* r
)
527 #ifndef STRICT_ERRORS
530 membufwrite(h
->buf
, &r
->type
, 1);
531 membufwrite(h
->buf
, &r
->g
.reclen
, 1);
534 case RDFREC_GENERIC
: /* generic */
535 membufwrite(h
->buf
, &r
->g
.data
, r
->g
.reclen
);
538 case RDFREC_SEGRELOC
:
539 membufwrite(h
->buf
, &r
->r
.segment
, 1);
540 membufwrite(h
->buf
, &r
->r
.offset
, -4);
541 membufwrite(h
->buf
, &r
->r
.length
, 1);
542 membufwrite(h
->buf
, &r
->r
.refseg
, -2); /* 9 bytes written */
545 case RDFREC_IMPORT
: /* import */
546 case RDFREC_FARIMPORT
:
547 membufwrite(h
->buf
, &r
->i
.flags
, 1);
548 membufwrite(h
->buf
, &r
->i
.segment
, -2);
549 membufwrite(h
->buf
, &r
->i
.label
, strlen(r
->i
.label
) + 1);
552 case RDFREC_GLOBAL
: /* export */
553 membufwrite(h
->buf
, &r
->e
.flags
, 1);
554 membufwrite(h
->buf
, &r
->e
.segment
, 1);
555 membufwrite(h
->buf
, &r
->e
.offset
, -4);
556 membufwrite(h
->buf
, &r
->e
.label
, strlen(r
->e
.label
) + 1);
559 case RDFREC_DLL
: /* DLL */
560 membufwrite(h
->buf
, &r
->d
.libname
, strlen(r
->d
.libname
) + 1);
563 case RDFREC_BSS
: /* BSS */
564 membufwrite(h
->buf
, &r
->b
.amount
, -4);
567 case RDFREC_MODNAME
: /* Module name */
568 membufwrite(h
->buf
, &r
->m
.modname
, strlen(r
->m
.modname
) + 1);
573 return rdf_errno
= RDF_ERR_RECTYPE
;
575 for (i
= 0; i
< r
->g
.reclen
; i
++)
576 membufwrite(h
->buf
, r
->g
.data
[i
], 1);
582 int rdfaddsegment(rdf_headerbuf
* h
, int32_t seglength
)
585 h
->seglength
+= seglength
;
589 int rdfwriteheader(FILE * fp
, rdf_headerbuf
* h
)
593 fwrite(RDOFFId
, 1, strlen(RDOFFId
), fp
);
595 l
= membuflength(h
->buf
);
596 l2
= l
+ 14 + 10 * h
->nsegments
+ h
->seglength
;
597 l
= translateint32_t(l
);
598 l2
= translateint32_t(l2
);
599 fwrite(&l2
, 4, 1, fp
); /* object length */
600 fwrite(&l
, 4, 1, fp
); /* header length */
602 membufdump(h
->buf
, fp
);
604 return 0; /* no error handling in here... CHANGE THIS! */
607 void rdfdoneheader(rdf_headerbuf
* h
)