Update dependencies
[nasm/avx512.git] / rdoff / rdfload.c
blob2f8bf6b17276242101fee1392d02c8a893a9689d
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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "rdfload.h"
25 #include "symtab.h"
26 #include "collectn.h"
28 extern int rdf_errno;
30 rdfmodule * rdfload(const char *filename)
32 rdfmodule * f;
33 long bsslength = 0;
34 char * hdr;
35 rdfheaderrec *r;
37 f = malloc(sizeof(rdfmodule));
38 if (f == NULL) {
39 rdf_errno = RDF_ERR_NOMEM;
40 return NULL;
43 f->symtab = symtabNew();
44 if (!f->symtab) {
45 free(f);
46 rdf_errno = RDF_ERR_NOMEM;
47 return NULL;
50 /* open the file */
51 if ( rdfopen( &(f->f), filename ) ) {
52 free(f);
53 return NULL;
56 /* read in text and data segments, and header */
58 f->t = malloc (f->f.seg[0].length);
59 f->d = malloc (f->f.seg[1].length); /* BSS seg allocated later */
60 hdr = malloc (f->f.header_len);
62 if (! f->t || ! f->d || !hdr) {
63 rdf_errno = RDF_ERR_NOMEM;
64 rdfclose(&f->f);
65 if (f->t) free(f->t);
66 if (f->d) free(f->d);
67 free(f);
68 return NULL;
71 if ( rdfloadseg (&f->f,RDOFF_HEADER,hdr) ||
72 rdfloadseg (&f->f,RDOFF_CODE,f->t) ||
73 rdfloadseg (&f->f,RDOFF_DATA,f->d) )
75 rdfclose(&f->f);
76 free(f->t);
77 free(f->d);
78 free(f);
79 free(hdr);
80 return NULL;
83 rdfclose(&f->f);
85 /* Allocate BSS segment; step through header and count BSS records */
87 while ((r = rdfgetheaderrec(&f->f))) {
88 if (r->type == 5)
89 bsslength += r->b.amount;
92 f->b = malloc ( bsslength );
93 if (bsslength && (!f->b)) {
94 free(f->t);
95 free(f->d);
96 free(f);
97 free(hdr);
98 rdf_errno = RDF_ERR_NOMEM;
99 return NULL;
102 rdfheaderrewind (&f->f);
104 f->textrel = (long)f->t;
105 f->datarel = (long)f->d;
106 f->bssrel = (long)f->b;
108 return f;
111 int rdf_relocate(rdfmodule * m)
113 rdfheaderrec * r;
114 Collection imports;
115 symtabEnt e;
116 long rel;
117 unsigned char * seg;
119 rdfheaderrewind (&m->f);
120 collection_init(&imports);
122 while ((r = rdfgetheaderrec(&m->f))) {
123 switch (r->type) {
124 case 1: /* Relocation record */
126 /* calculate relocation factor */
128 if (r->r.refseg == 0) rel = m->textrel;
129 else if (r->r.refseg == 1) rel = m->datarel;
130 else if (r->r.refseg == 2) rel = m->bssrel;
131 else
132 /* We currently do not support load-time linkage.
133 This should be added some time soon... */
135 return 1; /* return error code */
137 if ((r->r.segment & 63) == 0) seg = m->t;
138 else if ((r->r.segment & 63) == 1) seg = m->d;
139 else
140 continue; /* relocation not in a loaded segment */
142 /* it doesn't matter in this case that the code is non-portable,
143 as the entire concept of executing a module like this is
144 non-portable */
145 switch(r->r.length) {
146 case 1:
147 seg[r->r.offset] += (char) rel;
148 break;
149 case 2:
150 *(uint16 *)(seg + r->r.offset) += (uint16) rel;
151 break;
152 case 4:
153 *(long *)(seg + r->r.offset) += rel;
154 break;
156 break;
158 case 3: /* export record - add to symtab */
159 e.segment = r->e.segment;
160 e.offset = r->e.offset +
161 (e.segment == 0 ? m->textrel : /* 0 -> code */
162 e.segment == 1 ? m->datarel : /* 1 -> data */
163 m->bssrel) ; /* 2 -> bss */
164 e.flags = 0;
165 e.name = malloc(strlen(r->e.label) + 1);
166 if (! e.name)
167 return 1;
169 strcpy(e.name,r->e.label);
170 symtabInsert(m->symtab,&e);
171 break;
173 case 6: /* segment relocation */
174 fprintf(stderr, "%s: segment relocation not supported by this "
175 "loader\n", m->f.name);
176 return 1;
179 return 0;