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
28 #define newstr(str) strcpy(malloc(strlen(str) + 1),str)
29 #define newstrcat(s1,s2) strcat(strcpy(malloc(strlen(s1) + strlen(s2) + 1), \
33 * Comment this out to allow the module to read & write header record types
34 * that it isn't aware of. With this defined, unrecognised header records
35 * will generate error number 8, reported as 'unknown extended header record'.
40 /* ========================================================================
41 * Code for memory buffers (for delayed writing of header until we know
43 * ======================================================================== */
45 memorybuffer
*newmembuf()
49 t
= malloc(sizeof(memorybuffer
));
58 void membufwrite(memorybuffer
* const b
, void *data
, int bytes
)
63 if (b
->next
) { /* memory buffer full - use next buffer */
64 membufwrite(b
->next
, data
, bytes
);
68 if ((bytes
< 0 && b
->length
- bytes
> BUF_BLOCK_LEN
)
69 || (bytes
> 0 && b
->length
+ bytes
> BUF_BLOCK_LEN
)) {
71 /* buffer full and no next allocated... allocate and initialise next
73 b
->next
= newmembuf();
74 membufwrite(b
->next
, data
, bytes
);
79 case -4: /* convert to little-endian */
81 b
->buffer
[b
->length
++] = l
& 0xFF;
83 b
->buffer
[b
->length
++] = l
& 0xFF;
85 b
->buffer
[b
->length
++] = l
& 0xFF;
87 b
->buffer
[b
->length
++] = l
& 0xFF;
91 w
= *(uint16_t *) data
;
92 b
->buffer
[b
->length
++] = w
& 0xFF;
94 b
->buffer
[b
->length
++] = w
& 0xFF;
99 b
->buffer
[b
->length
++] = *(*(uint8_t **)&data
);
101 (*(uint8_t **)&data
)++;
107 void membufdump(memorybuffer
* b
, FILE * fp
)
112 fwrite(b
->buffer
, 1, b
->length
, fp
);
114 membufdump(b
->next
, fp
);
117 int membuflength(memorybuffer
* b
)
121 return b
->length
+ membuflength(b
->next
);
124 void freemembuf(memorybuffer
* b
)
132 /* =========================================================================
133 General purpose routines and variables used by the library functions
134 ========================================================================= */
137 * translateint32_t() and translateint16_t()
139 * translate from little endian to local representation
141 int32_t translateint32_t(int32_t in
)
155 uint16_t translateint16_t(uint16_t in
)
161 r
= (i
[1] << 8) + i
[0];
167 static char *knownsegtypes
[8] = {
168 "NULL", "text", "data", "object comment",
169 "linked comment", "loader comment",
170 "symbolic debug", "line number debug"
173 /* Get a textual string describing the segment type */
174 char *translatesegmenttype(uint16_t type
)
177 return knownsegtypes
[type
];
181 return "reserved - Moscow";
183 return "reserved - system dependant";
185 return "reserved - other";
187 return "invalid type code";
188 return "type code out of range";
191 /* This signature is written to the start of RDOFF files */
192 const char *RDOFFId
= RDOFF2_SIGNATURE
;
194 /* Error messages. Must correspond to the codes defined in rdoff.h */
195 const char *rdf_errors
[11] = {
196 /* 0 */ "no error occurred",
197 /* 1 */ "could not open file",
198 /* 2 */ "invalid file format",
199 /* 3 */ "error reading file",
200 /* 4 */ "unknown error",
201 /* 5 */ "header not read",
202 /* 6 */ "out of memory",
203 /* 7 */ "RDOFF v1 not supported",
204 /* 8 */ "unknown extended header record",
205 /* 9 */ "header record of known type but unknown length",
206 /* 10 */ "no such segment"
211 /* ========================================================================
212 The library functions
213 ======================================================================== */
215 int rdfopen(rdffile
* f
, const char *name
)
219 fp
= fopen(name
, "rb");
221 return rdf_errno
= RDF_ERR_OPEN
;
223 return rdfopenhere(f
, fp
, NULL
, name
);
226 int rdfopenhere(rdffile
* f
, FILE * fp
, int *refcount
, const char *name
)
233 if (translateint32_t(0x01020304) != 0x01020304) {
234 /* fix this to be portable! */
235 fputs("*** this program requires a little endian machine\n",
237 fprintf(stderr
, "01020304h = %08"PRIx32
"h\n", translateint32_t(0x01020304));
244 fread(buf
, 6, 1, f
->fp
); /* read header */
247 if (strcmp(buf
, RDOFFId
)) {
249 if (!strcmp(buf
, "RDOFF1"))
250 return rdf_errno
= RDF_ERR_VER
;
251 return rdf_errno
= RDF_ERR_FORMAT
;
254 if (fread(&l
, 1, 4, f
->fp
) != 4
255 || fread(&f
->header_len
, 1, 4, f
->fp
) != 4) {
257 return rdf_errno
= RDF_ERR_READ
;
260 f
->header_ofs
= ftell(f
->fp
);
261 f
->eof_offset
= f
->header_ofs
+ translateint32_t(l
) - 4;
263 if (fseek(f
->fp
, f
->header_len
, SEEK_CUR
)) {
265 return rdf_errno
= RDF_ERR_FORMAT
; /* seek past end of file...? */
268 if (fread(&s
, 1, 2, f
->fp
) != 2) {
270 return rdf_errno
= RDF_ERR_READ
;
276 f
->seg
[f
->nsegs
].type
= s
;
277 if (fread(&f
->seg
[f
->nsegs
].number
, 1, 2, f
->fp
) != 2 ||
278 fread(&f
->seg
[f
->nsegs
].reserved
, 1, 2, f
->fp
) != 2 ||
279 fread(&f
->seg
[f
->nsegs
].length
, 1, 4, f
->fp
) != 4) {
281 return rdf_errno
= RDF_ERR_READ
;
284 f
->seg
[f
->nsegs
].offset
= ftell(f
->fp
);
285 if (fseek(f
->fp
, f
->seg
[f
->nsegs
].length
, SEEK_CUR
)) {
287 return rdf_errno
= RDF_ERR_FORMAT
;
291 if (fread(&s
, 1, 2, f
->fp
) != 2) {
293 return rdf_errno
= RDF_ERR_READ
;
297 if (f
->eof_offset
!= ftell(f
->fp
) + 8) { /* +8 = skip null segment header */
298 fprintf(stderr
, "warning: eof_offset [%"PRId32
"] and actual eof offset "
299 "[%ld] don't match\n", f
->eof_offset
, ftell(f
->fp
) + 8);
301 fseek(f
->fp
, initpos
, SEEK_SET
);
302 f
->header_loc
= NULL
;
304 f
->name
= newstr(name
);
305 f
->refcount
= refcount
;
311 int rdfclose(rdffile
* f
)
313 if (!f
->refcount
|| !--(*f
->refcount
)) {
323 * Print the message for last error (from rdf_errno)
325 void rdfperror(const char *app
, const char *name
)
327 fprintf(stderr
, "%s:%s: %s\n", app
, name
, rdf_errors
[rdf_errno
]);
328 if (rdf_errno
== RDF_ERR_OPEN
|| rdf_errno
== RDF_ERR_READ
) {
334 * Find the segment by its number.
335 * Returns segment array index, or -1 if segment with such number was not found.
337 int rdffindsegment(rdffile
* f
, int segno
)
340 for (i
= 0; i
< f
->nsegs
; i
++)
341 if (f
->seg
[i
].number
== segno
)
347 * Load the segment. Returns status.
349 int rdfloadseg(rdffile
* f
, int segment
, void *buffer
)
355 fpos
= f
->header_ofs
;
356 slen
= f
->header_len
;
357 f
->header_loc
= (uint8_t *) buffer
;
361 if (segment
< f
->nsegs
) {
362 fpos
= f
->seg
[segment
].offset
;
363 slen
= f
->seg
[segment
].length
;
364 f
->seg
[segment
].data
= (uint8_t *) buffer
;
366 return rdf_errno
= RDF_ERR_SEGMENT
;
370 if (fseek(f
->fp
, fpos
, SEEK_SET
))
371 return rdf_errno
= RDF_ERR_UNKNOWN
;
373 if (fread(buffer
, 1, slen
, f
->fp
) != slen
)
374 return rdf_errno
= RDF_ERR_READ
;
379 /* Macros for reading integers from header in memory */
381 #define RI8(v) v = f->header_loc[f->header_fp++]
382 #define RI16(v) { v = (f->header_loc[f->header_fp] + \
383 (f->header_loc[f->header_fp+1] << 8)); \
386 #define RI32(v) { v = (f->header_loc[f->header_fp] + \
387 (f->header_loc[f->header_fp+1] << 8) + \
388 (f->header_loc[f->header_fp+2] << 16) + \
389 (f->header_loc[f->header_fp+3] << 24)); \
392 #define RS(str,max) { for(i=0;i<max;i++){\
393 RI8(str[i]); if (!str[i]) break;} str[i]=0; }
396 * Read a header record.
397 * Returns the address of record, or NULL in case of error.
399 rdfheaderrec
*rdfgetheaderrec(rdffile
* f
)
401 static rdfheaderrec r
;
404 if (!f
->header_loc
) {
405 rdf_errno
= RDF_ERR_HEADER
;
409 if (f
->header_fp
>= f
->header_len
)
416 case RDFREC_RELOC
: /* Relocation record */
417 case RDFREC_SEGRELOC
:
418 if (r
.r
.reclen
!= 8) {
419 rdf_errno
= RDF_ERR_RECLEN
;
428 case RDFREC_IMPORT
: /* Imported symbol record */
429 case RDFREC_FARIMPORT
:
432 RS(r
.i
.label
, EXIM_LABEL_MAX
);
435 case RDFREC_GLOBAL
: /* Exported symbol record */
439 RS(r
.e
.label
, EXIM_LABEL_MAX
);
442 case RDFREC_DLL
: /* DLL record */
443 RS(r
.d
.libname
, MODLIB_NAME_MAX
);
446 case RDFREC_BSS
: /* BSS reservation record */
447 if (r
.r
.reclen
!= 4) {
448 rdf_errno
= RDF_ERR_RECLEN
;
454 case RDFREC_MODNAME
: /* Module name record */
455 RS(r
.m
.modname
, MODLIB_NAME_MAX
);
458 case RDFREC_COMMON
: /* Common variable */
462 RS(r
.c
.label
, EXIM_LABEL_MAX
);
467 rdf_errno
= RDF_ERR_RECTYPE
; /* unknown header record */
470 for (i
= 0; i
< r
.g
.reclen
; i
++)
478 * Rewind to the beginning of the file
480 void rdfheaderrewind(rdffile
* f
)
485 rdf_headerbuf
*rdfnewheader(void)
487 rdf_headerbuf
*hb
= malloc(sizeof(rdf_headerbuf
));
491 hb
->buf
= newmembuf();
498 int rdfaddheader(rdf_headerbuf
* h
, rdfheaderrec
* r
)
500 #ifndef STRICT_ERRORS
503 membufwrite(h
->buf
, &r
->type
, 1);
504 membufwrite(h
->buf
, &r
->g
.reclen
, 1);
507 case RDFREC_GENERIC
: /* generic */
508 membufwrite(h
->buf
, &r
->g
.data
, r
->g
.reclen
);
511 case RDFREC_SEGRELOC
:
512 membufwrite(h
->buf
, &r
->r
.segment
, 1);
513 membufwrite(h
->buf
, &r
->r
.offset
, -4);
514 membufwrite(h
->buf
, &r
->r
.length
, 1);
515 membufwrite(h
->buf
, &r
->r
.refseg
, -2); /* 9 bytes written */
518 case RDFREC_IMPORT
: /* import */
519 case RDFREC_FARIMPORT
:
520 membufwrite(h
->buf
, &r
->i
.flags
, 1);
521 membufwrite(h
->buf
, &r
->i
.segment
, -2);
522 membufwrite(h
->buf
, &r
->i
.label
, strlen(r
->i
.label
) + 1);
525 case RDFREC_GLOBAL
: /* export */
526 membufwrite(h
->buf
, &r
->e
.flags
, 1);
527 membufwrite(h
->buf
, &r
->e
.segment
, 1);
528 membufwrite(h
->buf
, &r
->e
.offset
, -4);
529 membufwrite(h
->buf
, &r
->e
.label
, strlen(r
->e
.label
) + 1);
532 case RDFREC_DLL
: /* DLL */
533 membufwrite(h
->buf
, &r
->d
.libname
, strlen(r
->d
.libname
) + 1);
536 case RDFREC_BSS
: /* BSS */
537 membufwrite(h
->buf
, &r
->b
.amount
, -4);
540 case RDFREC_MODNAME
: /* Module name */
541 membufwrite(h
->buf
, &r
->m
.modname
, strlen(r
->m
.modname
) + 1);
546 return rdf_errno
= RDF_ERR_RECTYPE
;
548 for (i
= 0; i
< r
->g
.reclen
; i
++)
549 membufwrite(h
->buf
, r
->g
.data
[i
], 1);
555 int rdfaddsegment(rdf_headerbuf
* h
, int32_t seglength
)
558 h
->seglength
+= seglength
;
562 int rdfwriteheader(FILE * fp
, rdf_headerbuf
* h
)
566 fwrite(RDOFFId
, 1, strlen(RDOFFId
), fp
);
568 l
= membuflength(h
->buf
);
569 l2
= l
+ 14 + 10 * h
->nsegments
+ h
->seglength
;
570 l
= translateint32_t(l
);
571 l2
= translateint32_t(l2
);
572 fwrite(&l2
, 4, 1, fp
); /* object length */
573 fwrite(&l
, 4, 1, fp
); /* header length */
575 membufdump(h
->buf
, fp
);
577 return 0; /* no error handling in here... CHANGE THIS! */
580 void rdfdoneheader(rdf_headerbuf
* h
)