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 licence given in the file "Licence"
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
)
65 if (b
->next
) { /* memory buffer full - use next buffer */
66 membufwrite(b
->next
, data
, bytes
);
70 if ((bytes
< 0 && b
->length
- bytes
> BUF_BLOCK_LEN
)
71 || (bytes
> 0 && b
->length
+ bytes
> BUF_BLOCK_LEN
)) {
73 /* buffer full and no next allocated... allocate and initialise next
75 b
->next
= newmembuf();
76 membufwrite(b
->next
, data
, bytes
);
81 case -4: /* convert to little-endian */
83 b
->buffer
[b
->length
++] = l
& 0xFF;
85 b
->buffer
[b
->length
++] = l
& 0xFF;
87 b
->buffer
[b
->length
++] = l
& 0xFF;
89 b
->buffer
[b
->length
++] = l
& 0xFF;
93 w
= *(uint16_t *) data
;
94 b
->buffer
[b
->length
++] = w
& 0xFF;
96 b
->buffer
[b
->length
++] = w
& 0xFF;
101 b
->buffer
[b
->length
++] = *(*(uint8_t **)&data
);
103 (*(uint8_t **)&data
)++;
109 void membufdump(memorybuffer
* b
, FILE * fp
)
114 fwrite(b
->buffer
, 1, b
->length
, fp
);
116 membufdump(b
->next
, fp
);
119 int membuflength(memorybuffer
* b
)
123 return b
->length
+ membuflength(b
->next
);
126 void freemembuf(memorybuffer
* b
)
134 /* =========================================================================
135 General purpose routines and variables used by the library functions
136 ========================================================================= */
139 * translateint32_t() and translateint16_t()
141 * translate from little endian to local representation
143 int32_t translateint32_t(int32_t in
)
157 uint16_t translateint16_t(uint16_t in
)
163 r
= (i
[1] << 8) + i
[0];
169 static char *knownsegtypes
[8] = {
170 "NULL", "text", "data", "object comment",
171 "linked comment", "loader comment",
172 "symbolic debug", "line number debug"
175 /* Get a textual string describing the segment type */
176 char *translatesegmenttype(uint16_t type
)
179 return knownsegtypes
[type
];
183 return "reserved - Moscow";
185 return "reserved - system dependant";
187 return "reserved - other";
189 return "invalid type code";
190 return "type code out of range";
193 /* This signature is written to the start of RDOFF files */
194 const char *RDOFFId
= RDOFF2_SIGNATURE
;
196 /* Error messages. Must correspond to the codes defined in rdoff.h */
197 const char *rdf_errors
[11] = {
198 /* 0 */ "no error occurred",
199 /* 1 */ "could not open file",
200 /* 2 */ "invalid file format",
201 /* 3 */ "error reading file",
202 /* 4 */ "unknown error",
203 /* 5 */ "header not read",
204 /* 6 */ "out of memory",
205 /* 7 */ "RDOFF v1 not supported",
206 /* 8 */ "unknown extended header record",
207 /* 9 */ "header record of known type but unknown length",
208 /* 10 */ "no such segment"
213 /* ========================================================================
214 The library functions
215 ======================================================================== */
217 int rdfopen(rdffile
* f
, const char *name
)
221 fp
= fopen(name
, "rb");
223 return rdf_errno
= RDF_ERR_OPEN
;
225 return rdfopenhere(f
, fp
, NULL
, name
);
228 int rdfopenhere(rdffile
* f
, FILE * fp
, int *refcount
, const char *name
)
235 if (translateint32_t(0x01020304) != 0x01020304) {
236 /* fix this to be portable! */
237 fputs("*** this program requires a little endian machine\n",
239 fprintf(stderr
, "01020304h = %08"PRIx32
"h\n", translateint32_t(0x01020304));
246 fread(buf
, 6, 1, f
->fp
); /* read header */
249 if (strcmp(buf
, RDOFFId
)) {
251 if (!strcmp(buf
, "RDOFF1"))
252 return rdf_errno
= RDF_ERR_VER
;
253 return rdf_errno
= RDF_ERR_FORMAT
;
256 if (fread(&l
, 1, 4, f
->fp
) != 4
257 || fread(&f
->header_len
, 1, 4, f
->fp
) != 4) {
259 return rdf_errno
= RDF_ERR_READ
;
262 f
->header_ofs
= ftell(f
->fp
);
263 f
->eof_offset
= f
->header_ofs
+ translateint32_t(l
) - 4;
265 if (fseek(f
->fp
, f
->header_len
, SEEK_CUR
)) {
267 return rdf_errno
= RDF_ERR_FORMAT
; /* seek past end of file...? */
270 if (fread(&s
, 1, 2, f
->fp
) != 2) {
272 return rdf_errno
= RDF_ERR_READ
;
278 f
->seg
[f
->nsegs
].type
= s
;
279 if (fread(&f
->seg
[f
->nsegs
].number
, 1, 2, f
->fp
) != 2 ||
280 fread(&f
->seg
[f
->nsegs
].reserved
, 1, 2, f
->fp
) != 2 ||
281 fread(&f
->seg
[f
->nsegs
].length
, 1, 4, f
->fp
) != 4) {
283 return rdf_errno
= RDF_ERR_READ
;
286 f
->seg
[f
->nsegs
].offset
= ftell(f
->fp
);
287 if (fseek(f
->fp
, f
->seg
[f
->nsegs
].length
, SEEK_CUR
)) {
289 return rdf_errno
= RDF_ERR_FORMAT
;
293 if (fread(&s
, 1, 2, f
->fp
) != 2) {
295 return rdf_errno
= RDF_ERR_READ
;
299 if (f
->eof_offset
!= ftell(f
->fp
) + 8) { /* +8 = skip null segment header */
300 fprintf(stderr
, "warning: eof_offset [%"PRId32
"] and actual eof offset "
301 "[%ld] don't match\n", f
->eof_offset
, ftell(f
->fp
) + 8);
303 fseek(f
->fp
, initpos
, SEEK_SET
);
304 f
->header_loc
= NULL
;
306 f
->name
= newstr(name
);
307 f
->refcount
= refcount
;
313 int rdfclose(rdffile
* f
)
315 if (!f
->refcount
|| !--(*f
->refcount
)) {
325 * Print the message for last error (from rdf_errno)
327 void rdfperror(const char *app
, const char *name
)
329 fprintf(stderr
, "%s:%s: %s\n", app
, name
, rdf_errors
[rdf_errno
]);
330 if (rdf_errno
== RDF_ERR_OPEN
|| rdf_errno
== RDF_ERR_READ
) {
336 * Find the segment by its number.
337 * Returns segment array index, or -1 if segment with such number was not found.
339 int rdffindsegment(rdffile
* f
, int segno
)
342 for (i
= 0; i
< f
->nsegs
; i
++)
343 if (f
->seg
[i
].number
== segno
)
349 * Load the segment. Returns status.
351 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
)