A few more AVX2 spec instructions
[nasm-cyr.git] / rdoff / rdfload.c
blob4fd3dbd0ee354f05f64b91b7c0c900eb3bc0ff7f
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 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 * rdfload.c RDOFF Object File loader library
39 * TODO: this has been modified from previous version only in very
40 * simplistic ways. Needs to be improved drastically, especially:
41 * - support for more than the 2 standard segments
42 * - support for segment relocations (hard to do in ANSI C)
45 #include "compiler.h"
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #include "rdfload.h"
52 #include "symtab.h"
53 #include "collectn.h"
55 extern int rdf_errno;
57 rdfmodule *rdfload(const char *filename)
59 rdfmodule *f;
60 int32_t bsslength = 0;
61 char *hdr;
62 rdfheaderrec *r;
64 f = malloc(sizeof(rdfmodule));
65 if (f == NULL) {
66 rdf_errno = RDF_ERR_NOMEM;
67 return NULL;
70 f->symtab = symtabNew();
71 if (!f->symtab) {
72 free(f);
73 rdf_errno = RDF_ERR_NOMEM;
74 return NULL;
77 /* open the file */
78 if (rdfopen(&(f->f), filename)) {
79 free(f);
80 return NULL;
83 /* read in text and data segments, and header */
85 f->t = malloc(f->f.seg[0].length);
86 f->d = malloc(f->f.seg[1].length); /* BSS seg allocated later */
87 hdr = malloc(f->f.header_len);
89 if (!f->t || !f->d || !hdr) {
90 rdf_errno = RDF_ERR_NOMEM;
91 rdfclose(&f->f);
92 if (f->t)
93 free(f->t);
94 if (f->d)
95 free(f->d);
96 free(f);
97 return NULL;
100 if (rdfloadseg(&f->f, RDOFF_HEADER, hdr) ||
101 rdfloadseg(&f->f, RDOFF_CODE, f->t) ||
102 rdfloadseg(&f->f, RDOFF_DATA, f->d)) {
103 rdfclose(&f->f);
104 free(f->t);
105 free(f->d);
106 free(f);
107 free(hdr);
108 return NULL;
111 rdfclose(&f->f);
113 /* Allocate BSS segment; step through header and count BSS records */
115 while ((r = rdfgetheaderrec(&f->f))) {
116 if (r->type == 5)
117 bsslength += r->b.amount;
120 f->b = malloc(bsslength);
121 if (bsslength && (!f->b)) {
122 free(f->t);
123 free(f->d);
124 free(f);
125 free(hdr);
126 rdf_errno = RDF_ERR_NOMEM;
127 return NULL;
130 rdfheaderrewind(&f->f);
132 f->textrel = (int32_t)(size_t)f->t;
133 f->datarel = (int32_t)(size_t)f->d;
134 f->bssrel = (int32_t)(size_t)f->b;
136 return f;
139 int rdf_relocate(rdfmodule * m)
141 rdfheaderrec *r;
142 Collection imports;
143 symtabEnt e;
144 int32_t rel;
145 uint8_t *seg;
147 rdfheaderrewind(&m->f);
148 collection_init(&imports);
150 while ((r = rdfgetheaderrec(&m->f))) {
151 switch (r->type) {
152 case 1: /* Relocation record */
154 /* calculate relocation factor */
156 if (r->r.refseg == 0)
157 rel = m->textrel;
158 else if (r->r.refseg == 1)
159 rel = m->datarel;
160 else if (r->r.refseg == 2)
161 rel = m->bssrel;
162 else
163 /* We currently do not support load-time linkage.
164 This should be added some time soon... */
166 return 1; /* return error code */
168 if ((r->r.segment & 63) == 0)
169 seg = m->t;
170 else if ((r->r.segment & 63) == 1)
171 seg = m->d;
172 else
173 continue; /* relocation not in a loaded segment */
175 /* it doesn't matter in this case that the code is non-portable,
176 as the entire concept of executing a module like this is
177 non-portable */
178 switch (r->r.length) {
179 case 1:
180 seg[r->r.offset] += (char)rel;
181 break;
182 case 2:
183 *(uint16_t *) (seg + r->r.offset) += (uint16_t) rel;
184 break;
185 case 4:
186 *(int32_t *)(seg + r->r.offset) += rel;
187 break;
189 break;
191 case 3: /* export record - add to symtab */
192 e.segment = r->e.segment;
193 e.offset = r->e.offset + (e.segment == 0 ? m->textrel : /* 0 -> code */
194 e.segment == 1 ? m->datarel : /* 1 -> data */
195 m->bssrel); /* 2 -> bss */
196 e.flags = 0;
197 e.name = malloc(strlen(r->e.label) + 1);
198 if (!e.name)
199 return 1;
201 strcpy(e.name, r->e.label);
202 symtabInsert(m->symtab, &e);
203 break;
205 case 6: /* segment relocation */
206 fprintf(stderr, "%s: segment relocation not supported by this "
207 "loader\n", m->f.name);
208 return 1;
211 return 0;