test: nasm-t -- Add pushseg test
[nasm.git] / rdoff / rdoff.c
blob77fec399bec6ac86b160692f7b748f6e0423d90e
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
9 * conditions are met:
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
40 * make it portable.
43 #include "compiler.h"
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <errno.h>
50 #include "rdfutils.h"
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'.
58 #define STRICT_ERRORS
60 /* ========================================================================
61 * Code for memory buffers (for delayed writing of header until we know
62 * how int32_t it is).
63 * ======================================================================== */
65 static memorybuffer *newmembuf(void)
67 memorybuffer *t;
69 t = nasm_malloc(sizeof(memorybuffer));
70 if (!t)
71 return NULL;
73 t->length = 0;
74 t->next = NULL;
75 return t;
78 static void membufwrite(memorybuffer * const b, void *data, int bytes)
80 uint16_t w;
81 int32_t l;
82 char *c;
84 if (b->next) { /* memory buffer full - use next buffer */
85 membufwrite(b->next, data, bytes);
86 return;
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
93 * buffer */
94 b->next = newmembuf();
95 membufwrite(b->next, data, bytes);
96 return;
99 switch (bytes) {
100 case -4: /* convert to little-endian */
101 l = *(int32_t *)data;
102 b->buffer[b->length++] = l & 0xFF;
103 l >>= 8;
104 b->buffer[b->length++] = l & 0xFF;
105 l >>= 8;
106 b->buffer[b->length++] = l & 0xFF;
107 l >>= 8;
108 b->buffer[b->length++] = l & 0xFF;
109 break;
111 case -2:
112 w = *(uint16_t *) data;
113 b->buffer[b->length++] = w & 0xFF;
114 w >>= 8;
115 b->buffer[b->length++] = w & 0xFF;
116 break;
118 default:
119 c = data;
120 while (bytes--)
121 b->buffer[b->length++] = *c++;
122 break;
126 static void membufdump(memorybuffer * b, FILE * fp)
128 if (!b)
129 return;
131 nasm_write(b->buffer, b->length, fp);
132 membufdump(b->next, fp);
135 static int membuflength(memorybuffer * b)
137 if (!b)
138 return 0;
139 return b->length + membuflength(b->next);
142 static void freemembuf(memorybuffer * b)
144 if (!b)
145 return;
146 freemembuf(b->next);
147 nasm_free(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)
161 int32_t r;
162 uint8_t *i;
164 i = (uint8_t *)&in;
165 r = i[3];
166 r = (r << 8) + i[2];
167 r = (r << 8) + i[1];
168 r = (r << 8) + *i;
170 return r;
173 uint16_t translateint16_t(uint16_t in)
175 uint16_t r;
176 uint8_t *i;
178 i = (uint8_t *)&in;
179 r = (i[1] << 8) + i[0];
181 return r;
184 /* Segment types */
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)
194 if (type < 8)
195 return knownsegtypes[type];
196 if (type < 0x0020)
197 return "reserved";
198 if (type < 0x1000)
199 return "reserved - Moscow";
200 if (type < 0x8000)
201 return "reserved - system dependant";
202 if (type < 0xFFFF)
203 return "reserved - other";
204 if (type == 0xFFFF)
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"
227 int rdf_errno = 0;
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)
237 exit(1);
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)
251 FILE *fp;
253 fp = fopen(name, "rb");
254 if (!fp)
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)
262 char buf[8];
263 int32_t initpos;
264 int32_t l;
265 uint16_t s;
267 if (translateint32_t(0x01020304) != 0x01020304) {
268 /* fix this to be portable! */
269 fputs("*** this program requires a little endian machine\n",
270 stderr);
271 fprintf(stderr, "01020304h = %08"PRIx32"h\n", translateint32_t(0x01020304));
272 exit(3);
275 f->fp = fp;
276 initpos = ftell(fp);
278 /* read header */
279 if (fread(buf, 1, 6, f->fp) != 6) {
280 fclose(f->fp);
281 return rdf_errno = RDF_ERR_READ;
283 buf[6] = 0;
285 if (strcmp(buf, RDOFFId)) {
286 fclose(f->fp);
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) {
294 fclose(f->fp);
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)) {
302 fclose(f->fp);
303 return rdf_errno = RDF_ERR_FORMAT; /* seek past end of file...? */
306 if (fread(&s, 1, 2, f->fp) != 2) {
307 fclose(f->fp);
308 return rdf_errno = RDF_ERR_READ;
311 f->nsegs = 0;
313 while (s != 0) {
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) {
318 fclose(f->fp);
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)) {
324 fclose(f->fp);
325 return rdf_errno = RDF_ERR_FORMAT;
327 f->nsegs++;
329 if (fread(&s, 1, 2, f->fp) != 2) {
330 fclose(f->fp);
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;
344 if (refcount)
345 (*refcount)++;
346 return RDF_OK;
349 int rdfclose(rdffile * f)
351 if (!f->refcount || !--(*f->refcount)) {
352 fclose(f->fp);
353 f->fp = NULL;
355 nasm_free(f->name);
357 return 0;
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) {
367 perror(app);
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)
377 int i;
378 for (i = 0; i < f->nsegs; i++)
379 if (f->seg[i].number == segno)
380 return i;
381 return -1;
385 * Load the segment. Returns status.
387 int rdfloadseg(rdffile * f, int segment, void *buffer)
389 int32_t fpos;
390 size_t slen;
392 switch (segment) {
393 case RDOFF_HEADER:
394 fpos = f->header_ofs;
395 slen = f->header_len;
396 f->header_loc = (uint8_t *) buffer;
397 f->header_fp = 0;
398 break;
399 default:
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;
404 } else {
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;
415 return RDF_OK;
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)); \
423 f->header_fp += 2; }
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)); \
429 f->header_fp += 4; }
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;
441 int i;
443 if (!f->header_loc) {
444 rdf_errno = RDF_ERR_HEADER;
445 return NULL;
448 if (f->header_fp >= f->header_len)
449 return 0;
451 RI8(r.type);
452 RI8(r.g.reclen);
454 switch (r.type) {
455 case RDFREC_RELOC: /* Relocation record */
456 case RDFREC_SEGRELOC:
457 if (r.r.reclen != 8) {
458 rdf_errno = RDF_ERR_RECLEN;
459 return NULL;
461 RI8(r.r.segment);
462 RI32(r.r.offset);
463 RI8(r.r.length);
464 RI16(r.r.refseg);
465 break;
467 case RDFREC_IMPORT: /* Imported symbol record */
468 case RDFREC_FARIMPORT:
469 RI8(r.i.flags);
470 RI16(r.i.segment);
471 RS(r.i.label, EXIM_LABEL_MAX);
472 break;
474 case RDFREC_GLOBAL: /* Exported symbol record */
475 RI8(r.e.flags);
476 RI8(r.e.segment);
477 RI32(r.e.offset);
478 RS(r.e.label, EXIM_LABEL_MAX);
479 break;
481 case RDFREC_DLL: /* DLL record */
482 RS(r.d.libname, MODLIB_NAME_MAX);
483 break;
485 case RDFREC_BSS: /* BSS reservation record */
486 if (r.r.reclen != 4) {
487 rdf_errno = RDF_ERR_RECLEN;
488 return NULL;
490 RI32(r.b.amount);
491 break;
493 case RDFREC_MODNAME: /* Module name record */
494 RS(r.m.modname, MODLIB_NAME_MAX);
495 break;
497 case RDFREC_COMMON: /* Common variable */
498 RI16(r.c.segment);
499 RI32(r.c.size);
500 RI16(r.c.align);
501 RS(r.c.label, EXIM_LABEL_MAX);
502 break;
504 default:
505 #ifdef STRICT_ERRORS
506 rdf_errno = RDF_ERR_RECTYPE; /* unknown header record */
507 return NULL;
508 #else
509 for (i = 0; i < r.g.reclen; i++)
510 RI8(r.g.data[i]);
511 #endif
513 return &r;
517 * Rewind to the beginning of the file
519 void rdfheaderrewind(rdffile * f)
521 f->header_fp = 0;
524 rdf_headerbuf *rdfnewheader(void)
526 rdf_headerbuf *hb = nasm_malloc(sizeof(rdf_headerbuf));
527 if (hb == NULL)
528 return NULL;
530 hb->buf = newmembuf();
531 hb->nsegments = 0;
532 hb->seglength = 0;
534 return hb;
537 int rdfaddheader(rdf_headerbuf * h, rdfheaderrec * r)
539 #ifndef STRICT_ERRORS
540 int i;
541 #endif
542 membufwrite(h->buf, &r->type, 1);
543 membufwrite(h->buf, &r->g.reclen, 1);
545 switch (r->type) {
546 case RDFREC_GENERIC: /* generic */
547 membufwrite(h->buf, &r->g.data, r->g.reclen);
548 break;
549 case RDFREC_RELOC:
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 */
555 break;
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);
562 break;
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);
569 break;
571 case RDFREC_DLL: /* DLL */
572 membufwrite(h->buf, &r->d.libname, strlen(r->d.libname) + 1);
573 break;
575 case RDFREC_BSS: /* BSS */
576 membufwrite(h->buf, &r->b.amount, -4);
577 break;
579 case RDFREC_MODNAME: /* Module name */
580 membufwrite(h->buf, &r->m.modname, strlen(r->m.modname) + 1);
581 break;
583 default:
584 #ifdef STRICT_ERRORS
585 return rdf_errno = RDF_ERR_RECTYPE;
586 #else
587 for (i = 0; i < r->g.reclen; i++)
588 membufwrite(h->buf, r->g.data[i], 1);
589 #endif
591 return 0;
594 int rdfaddsegment(rdf_headerbuf * h, int32_t seglength)
596 h->nsegments++;
597 h->seglength += seglength;
598 return 0;
601 int rdfwriteheader(FILE * fp, rdf_headerbuf * h)
603 int32_t l, l2;
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)
619 freemembuf(h->buf);
620 nasm_free(h);