1 /* rdoff.c library of routines for manipulating rdoff files
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the license given in the file "LICENSE"
6 * distributed in the NASM archive.
8 * Permission to use this file in your own projects is granted, as int32_t
9 * as acknowledgement is given in an appropriate manner to its authors,
10 * with instructions of how to obtain a copy via ftp.
13 /* TODO: The functions in this module assume they are running
14 * on a little-endian machine. This should be fixed to
30 #define newstr(str) strcpy(malloc(strlen(str) + 1),str)
31 #define newstrcat(s1,s2) strcat(strcpy(malloc(strlen(s1) + strlen(s2) + 1), \
35 * Comment this out to allow the module to read & write header record types
36 * that it isn't aware of. With this defined, unrecognised header records
37 * will generate error number 8, reported as 'unknown extended header record'.
42 /* ========================================================================
43 * Code for memory buffers (for delayed writing of header until we know
45 * ======================================================================== */
47 memorybuffer
*newmembuf()
51 t
= malloc(sizeof(memorybuffer
));
60 void membufwrite(memorybuffer
* const b
, void *data
, int bytes
)
66 if (b
->next
) { /* memory buffer full - use next buffer */
67 membufwrite(b
->next
, data
, bytes
);
71 if ((bytes
< 0 && b
->length
- bytes
> BUF_BLOCK_LEN
)
72 || (bytes
> 0 && b
->length
+ bytes
> BUF_BLOCK_LEN
)) {
74 /* buffer full and no next allocated... allocate and initialise next
76 b
->next
= newmembuf();
77 membufwrite(b
->next
, data
, bytes
);
82 case -4: /* convert to little-endian */
84 b
->buffer
[b
->length
++] = l
& 0xFF;
86 b
->buffer
[b
->length
++] = l
& 0xFF;
88 b
->buffer
[b
->length
++] = l
& 0xFF;
90 b
->buffer
[b
->length
++] = l
& 0xFF;
94 w
= *(uint16_t *) data
;
95 b
->buffer
[b
->length
++] = w
& 0xFF;
97 b
->buffer
[b
->length
++] = w
& 0xFF;
103 b
->buffer
[b
->length
++] = *c
++;
108 void membufdump(memorybuffer
* b
, FILE * fp
)
113 fwrite(b
->buffer
, 1, b
->length
, fp
);
115 membufdump(b
->next
, fp
);
118 int membuflength(memorybuffer
* b
)
122 return b
->length
+ membuflength(b
->next
);
125 void freemembuf(memorybuffer
* b
)
133 /* =========================================================================
134 General purpose routines and variables used by the library functions
135 ========================================================================= */
138 * translateint32_t() and translateint16_t()
140 * translate from little endian to local representation
142 int32_t translateint32_t(int32_t in
)
156 uint16_t translateint16_t(uint16_t in
)
162 r
= (i
[1] << 8) + i
[0];
168 static char *knownsegtypes
[8] = {
169 "NULL", "text", "data", "object comment",
170 "linked comment", "loader comment",
171 "symbolic debug", "line number debug"
174 /* Get a textual string describing the segment type */
175 char *translatesegmenttype(uint16_t type
)
178 return knownsegtypes
[type
];
182 return "reserved - Moscow";
184 return "reserved - system dependant";
186 return "reserved - other";
188 return "invalid type code";
189 return "type code out of range";
192 /* This signature is written to the start of RDOFF files */
193 const char *RDOFFId
= RDOFF2_SIGNATURE
;
195 /* Error messages. Must correspond to the codes defined in rdoff.h */
196 const char *rdf_errors
[11] = {
197 /* 0 */ "no error occurred",
198 /* 1 */ "could not open file",
199 /* 2 */ "invalid file format",
200 /* 3 */ "error reading file",
201 /* 4 */ "unknown error",
202 /* 5 */ "header not read",
203 /* 6 */ "out of memory",
204 /* 7 */ "RDOFF v1 not supported",
205 /* 8 */ "unknown extended header record",
206 /* 9 */ "header record of known type but unknown length",
207 /* 10 */ "no such segment"
212 /* ========================================================================
213 The library functions
214 ======================================================================== */
216 int rdfopen(rdffile
* f
, const char *name
)
220 fp
= fopen(name
, "rb");
222 return rdf_errno
= RDF_ERR_OPEN
;
224 return rdfopenhere(f
, fp
, NULL
, name
);
227 int rdfopenhere(rdffile
* f
, FILE * fp
, int *refcount
, const char *name
)
234 if (translateint32_t(0x01020304) != 0x01020304) {
235 /* fix this to be portable! */
236 fputs("*** this program requires a little endian machine\n",
238 fprintf(stderr
, "01020304h = %08"PRIx32
"h\n", translateint32_t(0x01020304));
245 fread(buf
, 6, 1, f
->fp
); /* read header */
248 if (strcmp(buf
, RDOFFId
)) {
250 if (!strcmp(buf
, "RDOFF1"))
251 return rdf_errno
= RDF_ERR_VER
;
252 return rdf_errno
= RDF_ERR_FORMAT
;
255 if (fread(&l
, 1, 4, f
->fp
) != 4
256 || fread(&f
->header_len
, 1, 4, f
->fp
) != 4) {
258 return rdf_errno
= RDF_ERR_READ
;
261 f
->header_ofs
= ftell(f
->fp
);
262 f
->eof_offset
= f
->header_ofs
+ translateint32_t(l
) - 4;
264 if (fseek(f
->fp
, f
->header_len
, SEEK_CUR
)) {
266 return rdf_errno
= RDF_ERR_FORMAT
; /* seek past end of file...? */
269 if (fread(&s
, 1, 2, f
->fp
) != 2) {
271 return rdf_errno
= RDF_ERR_READ
;
277 f
->seg
[f
->nsegs
].type
= s
;
278 if (fread(&f
->seg
[f
->nsegs
].number
, 1, 2, f
->fp
) != 2 ||
279 fread(&f
->seg
[f
->nsegs
].reserved
, 1, 2, f
->fp
) != 2 ||
280 fread(&f
->seg
[f
->nsegs
].length
, 1, 4, f
->fp
) != 4) {
282 return rdf_errno
= RDF_ERR_READ
;
285 f
->seg
[f
->nsegs
].offset
= ftell(f
->fp
);
286 if (fseek(f
->fp
, f
->seg
[f
->nsegs
].length
, SEEK_CUR
)) {
288 return rdf_errno
= RDF_ERR_FORMAT
;
292 if (fread(&s
, 1, 2, f
->fp
) != 2) {
294 return rdf_errno
= RDF_ERR_READ
;
298 if (f
->eof_offset
!= ftell(f
->fp
) + 8) { /* +8 = skip null segment header */
299 fprintf(stderr
, "warning: eof_offset [%"PRId32
"] and actual eof offset "
300 "[%ld] don't match\n", f
->eof_offset
, ftell(f
->fp
) + 8);
302 fseek(f
->fp
, initpos
, SEEK_SET
);
303 f
->header_loc
= NULL
;
305 f
->name
= newstr(name
);
306 f
->refcount
= refcount
;
312 int rdfclose(rdffile
* f
)
314 if (!f
->refcount
|| !--(*f
->refcount
)) {
324 * Print the message for last error (from rdf_errno)
326 void rdfperror(const char *app
, const char *name
)
328 fprintf(stderr
, "%s:%s: %s\n", app
, name
, rdf_errors
[rdf_errno
]);
329 if (rdf_errno
== RDF_ERR_OPEN
|| rdf_errno
== RDF_ERR_READ
) {
335 * Find the segment by its number.
336 * Returns segment array index, or -1 if segment with such number was not found.
338 int rdffindsegment(rdffile
* f
, int segno
)
341 for (i
= 0; i
< f
->nsegs
; i
++)
342 if (f
->seg
[i
].number
== segno
)
348 * Load the segment. Returns status.
350 int rdfloadseg(rdffile
* f
, int segment
, void *buffer
)
357 fpos
= f
->header_ofs
;
358 slen
= f
->header_len
;
359 f
->header_loc
= (uint8_t *) buffer
;
363 if (segment
< f
->nsegs
) {
364 fpos
= f
->seg
[segment
].offset
;
365 slen
= f
->seg
[segment
].length
;
366 f
->seg
[segment
].data
= (uint8_t *) buffer
;
368 return rdf_errno
= RDF_ERR_SEGMENT
;
372 if (fseek(f
->fp
, fpos
, SEEK_SET
))
373 return rdf_errno
= RDF_ERR_UNKNOWN
;
375 if (fread(buffer
, 1, slen
, f
->fp
) != slen
)
376 return rdf_errno
= RDF_ERR_READ
;
381 /* Macros for reading integers from header in memory */
383 #define RI8(v) v = f->header_loc[f->header_fp++]
384 #define RI16(v) { v = (f->header_loc[f->header_fp] + \
385 (f->header_loc[f->header_fp+1] << 8)); \
388 #define RI32(v) { v = (f->header_loc[f->header_fp] + \
389 (f->header_loc[f->header_fp+1] << 8) + \
390 (f->header_loc[f->header_fp+2] << 16) + \
391 (f->header_loc[f->header_fp+3] << 24)); \
394 #define RS(str,max) { for(i=0;i<max;i++){\
395 RI8(str[i]); if (!str[i]) break;} str[i]=0; }
398 * Read a header record.
399 * Returns the address of record, or NULL in case of error.
401 rdfheaderrec
*rdfgetheaderrec(rdffile
* f
)
403 static rdfheaderrec r
;
406 if (!f
->header_loc
) {
407 rdf_errno
= RDF_ERR_HEADER
;
411 if (f
->header_fp
>= f
->header_len
)
418 case RDFREC_RELOC
: /* Relocation record */
419 case RDFREC_SEGRELOC
:
420 if (r
.r
.reclen
!= 8) {
421 rdf_errno
= RDF_ERR_RECLEN
;
430 case RDFREC_IMPORT
: /* Imported symbol record */
431 case RDFREC_FARIMPORT
:
434 RS(r
.i
.label
, EXIM_LABEL_MAX
);
437 case RDFREC_GLOBAL
: /* Exported symbol record */
441 RS(r
.e
.label
, EXIM_LABEL_MAX
);
444 case RDFREC_DLL
: /* DLL record */
445 RS(r
.d
.libname
, MODLIB_NAME_MAX
);
448 case RDFREC_BSS
: /* BSS reservation record */
449 if (r
.r
.reclen
!= 4) {
450 rdf_errno
= RDF_ERR_RECLEN
;
456 case RDFREC_MODNAME
: /* Module name record */
457 RS(r
.m
.modname
, MODLIB_NAME_MAX
);
460 case RDFREC_COMMON
: /* Common variable */
464 RS(r
.c
.label
, EXIM_LABEL_MAX
);
469 rdf_errno
= RDF_ERR_RECTYPE
; /* unknown header record */
472 for (i
= 0; i
< r
.g
.reclen
; i
++)
480 * Rewind to the beginning of the file
482 void rdfheaderrewind(rdffile
* f
)
487 rdf_headerbuf
*rdfnewheader(void)
489 rdf_headerbuf
*hb
= malloc(sizeof(rdf_headerbuf
));
493 hb
->buf
= newmembuf();
500 int rdfaddheader(rdf_headerbuf
* h
, rdfheaderrec
* r
)
502 #ifndef STRICT_ERRORS
505 membufwrite(h
->buf
, &r
->type
, 1);
506 membufwrite(h
->buf
, &r
->g
.reclen
, 1);
509 case RDFREC_GENERIC
: /* generic */
510 membufwrite(h
->buf
, &r
->g
.data
, r
->g
.reclen
);
513 case RDFREC_SEGRELOC
:
514 membufwrite(h
->buf
, &r
->r
.segment
, 1);
515 membufwrite(h
->buf
, &r
->r
.offset
, -4);
516 membufwrite(h
->buf
, &r
->r
.length
, 1);
517 membufwrite(h
->buf
, &r
->r
.refseg
, -2); /* 9 bytes written */
520 case RDFREC_IMPORT
: /* import */
521 case RDFREC_FARIMPORT
:
522 membufwrite(h
->buf
, &r
->i
.flags
, 1);
523 membufwrite(h
->buf
, &r
->i
.segment
, -2);
524 membufwrite(h
->buf
, &r
->i
.label
, strlen(r
->i
.label
) + 1);
527 case RDFREC_GLOBAL
: /* export */
528 membufwrite(h
->buf
, &r
->e
.flags
, 1);
529 membufwrite(h
->buf
, &r
->e
.segment
, 1);
530 membufwrite(h
->buf
, &r
->e
.offset
, -4);
531 membufwrite(h
->buf
, &r
->e
.label
, strlen(r
->e
.label
) + 1);
534 case RDFREC_DLL
: /* DLL */
535 membufwrite(h
->buf
, &r
->d
.libname
, strlen(r
->d
.libname
) + 1);
538 case RDFREC_BSS
: /* BSS */
539 membufwrite(h
->buf
, &r
->b
.amount
, -4);
542 case RDFREC_MODNAME
: /* Module name */
543 membufwrite(h
->buf
, &r
->m
.modname
, strlen(r
->m
.modname
) + 1);
548 return rdf_errno
= RDF_ERR_RECTYPE
;
550 for (i
= 0; i
< r
->g
.reclen
; i
++)
551 membufwrite(h
->buf
, r
->g
.data
[i
], 1);
557 int rdfaddsegment(rdf_headerbuf
* h
, int32_t seglength
)
560 h
->seglength
+= seglength
;
564 int rdfwriteheader(FILE * fp
, rdf_headerbuf
* h
)
568 fwrite(RDOFFId
, 1, strlen(RDOFFId
), fp
);
570 l
= membuflength(h
->buf
);
571 l2
= l
+ 14 + 10 * h
->nsegments
+ h
->seglength
;
572 l
= translateint32_t(l
);
573 l2
= translateint32_t(l2
);
574 fwrite(&l2
, 4, 1, fp
); /* object length */
575 fwrite(&l
, 4, 1, fp
); /* header length */
577 membufdump(h
->buf
, fp
);
579 return 0; /* no error handling in here... CHANGE THIS! */
582 void rdfdoneheader(rdf_headerbuf
* h
)