NASM 0.98.03
[nasm/avx512.git] / rdoff / rdfload.c
blob5b981034e4d7b7e432685858bdd257cba1c24383
1 /* rdfload.c RDOFF Object File loader library
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 long
9 * as acknowledgement is given in an appropriate manner to its authors,
10 * with instructions of how to obtain a copy via ftp.
14 * TODO: this has been modified from previous version only in very
15 * simplistic ways. Needs to be improved drastically, especially:
16 * - support for more than the 2 standard segments
17 * - support for segment relocations (hard to do in ANSI C)
20 #include <stdlib.h>
21 #include <stdio.h>
23 #include "rdfload.h"
24 #include "symtab.h"
25 #include "rdoff.h"
26 #include "collectn.h"
28 extern int rdf_errno;
30 rdfmodule * rdfload(const char *filename)
32 rdfmodule * f = malloc(sizeof(rdfmodule));
33 long bsslength = 0;
34 char * hdr;
35 rdfheaderrec *r;
37 if (f == NULL)
39 rdf_errno = 6; /* out of memory */
40 return NULL;
43 f->symtab = symtabNew();
44 if (!f->symtab)
46 free(f);
47 rdf_errno = 6;
48 return NULL;
51 /* open the file */
52 if ( rdfopen( &(f->f), filename ) ) {
53 free(f);
54 return NULL;
57 /* read in text and data segments, and header */
59 f->t = malloc (f->f.seg[0].length);
60 f->d = malloc (f->f.seg[1].length); /* BSS seg allocated later */
61 hdr = malloc (f->f.header_len);
63 if (! f->t || ! f->d || !hdr) {
64 rdf_errno = 6;
65 rdfclose(&f->f);
66 if (f->t) free(f->t);
67 if (f->d) free(f->d);
68 free(f);
69 return NULL;
72 if ( rdfloadseg (&f->f,RDOFF_HEADER,hdr) ||
73 rdfloadseg (&f->f,RDOFF_CODE,f->t) ||
74 rdfloadseg (&f->f,RDOFF_DATA,f->d) )
76 rdfclose(&f->f);
77 free(f->t);
78 free(f->d);
79 free(f);
80 free(hdr);
81 return NULL;
84 rdfclose(&f->f);
86 /* Allocate BSS segment; step through header and count BSS records */
88 while ( ( r = rdfgetheaderrec (&f->f) ) )
90 if (r->type == 5)
91 bsslength += r->b.amount;
94 f->b = malloc ( bsslength );
95 if (! f->b )
97 free(f->t);
98 free(f->d);
99 free(f);
100 free(hdr);
101 rdf_errno = 6;
102 return NULL;
105 rdfheaderrewind (&f->f);
107 f->textrel = (long)f->t;
108 f->datarel = (long)f->d;
109 f->bssrel = (long)f->b;
111 return f;
114 int rdf_relocate(rdfmodule * m)
116 rdfheaderrec * r;
117 Collection imports;
118 symtabEnt e;
119 long rel;
120 unsigned char * seg;
122 rdfheaderrewind ( & m->f );
123 collection_init(&imports);
125 while ( (r = rdfgetheaderrec ( & m->f ) ) )
127 switch (r->type)
129 case 1: /* Relocation record */
131 /* calculate relocation factor */
133 if (r->r.refseg == 0) rel = m->textrel;
134 else if (r->r.refseg == 1) rel = m->datarel;
135 else if (r->r.refseg == 2) rel = m->bssrel;
136 else
137 /* We currently do not support load-time linkage.
138 This should be added some time soon... */
140 return 1; /* return error code */
142 if ((r->r.segment & 63) == 0) seg = m->t;
143 else if ((r->r.segment & 63) == 1) seg = m->d;
144 else
145 continue; /* relocation not in a loaded segment */
147 /* it doesn't matter in this case that the code is non-portable,
148 as the entire concept of executing a module like this is
149 non-portable */
150 switch(r->r.length) {
151 case 1:
152 seg[r->r.offset] += (char) rel;
153 break;
154 case 2:
155 *(int16 *)(seg + r->r.offset) += (int16) rel;
156 break;
157 case 4:
158 *(long *)(seg + r->r.offset) += rel;
159 break;
161 break;
163 case 3: /* export record - add to symtab */
164 e.segment = r->e.segment;
165 e.offset = r->e.offset +
166 (e.segment == 0 ? m->textrel : /* 0 -> code */
167 e.segment == 1 ? m->datarel : /* 1 -> data */
168 m->bssrel) ; /* 2 -> bss */
169 e.flags = 0;
170 e.name = malloc(strlen(r->e.label) + 1);
171 if (! e.name)
172 return 1;
174 strcpy(e.name,r->e.label);
175 symtabInsert(m->symtab,&e);
176 break;
178 case 6: /* segment relocation */
179 fprintf(stderr, "%s: segment relocation not supported by this "
180 "loader\n", m->f.name);
181 return 1;
184 return 0;