1 /* ----------------------------------------------------------------------- *
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
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)
57 rdfmodule *rdfload(const char *filename)
60 int32_t bsslength = 0;
64 f = malloc(sizeof(rdfmodule));
66 rdf_errno = RDF_ERR_NOMEM;
70 f->symtab = symtabNew();
73 rdf_errno = RDF_ERR_NOMEM;
78 if (rdfopen(&(f->f), filename)) {
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;
101 if (rdfloadseg(&f->f, RDOFF_HEADER, hdr) ||
102 rdfloadseg(&f->f, RDOFF_CODE, f->t) ||
103 rdfloadseg(&f->f, RDOFF_DATA, f->d)) {
114 /* Allocate BSS segment; step through header and count BSS records */
116 while ((r = rdfgetheaderrec(&f->f))) {
118 bsslength += r->b.amount;
121 f->b = malloc(bsslength);
122 if (bsslength && (!f->b)) {
127 rdf_errno = RDF_ERR_NOMEM;
131 rdfheaderrewind(&f->f);
133 f->textrel = (int32_t)(size_t)f->t;
134 f->datarel = (int32_t)(size_t)f->d;
135 f->bssrel = (int32_t)(size_t)f->b;
140 int rdf_relocate(rdfmodule * m)
148 rdfheaderrewind(&m->f);
149 collection_init(&imports);
151 while ((r = rdfgetheaderrec(&m->f))) {
153 case 1: /* Relocation record */
155 /* calculate relocation factor */
157 if (r->r.refseg == 0)
159 else if (r->r.refseg == 1)
161 else if (r->r.refseg == 2)
164 /* We currently do not support load-time linkage.
165 This should be added some time soon... */
167 return 1; /* return error code */
169 if ((r->r.segment & 63) == 0)
171 else if ((r->r.segment & 63) == 1)
174 continue; /* relocation not in a loaded segment */
176 /* it doesn't matter in this case that the code is non-portable,
177 as the entire concept of executing a module like this is
179 switch (r->r.length) {
181 seg[r->r.offset] += (char)rel;
184 *(uint16_t *) (seg + r->r.offset) += (uint16_t) rel;
187 *(int32_t *)(seg + r->r.offset) += rel;
192 case 3: /* export record - add to symtab */
193 e.segment = r->e.segment;
194 e.offset = r->e.offset + (e.segment == 0 ? m->textrel : /* 0 -> code */
195 e.segment == 1 ? m->datarel : /* 1 -> data */
196 m->bssrel); /* 2 -> bss */
198 e.name = malloc(strlen(r->e.label) + 1);
202 strcpy(e.name, r->e.label);
203 symtabInsert(m->symtab, &e);
206 case 6: /* segment relocation */
207 fprintf(stderr, "%s: segment relocation not supported by this "
208 "loader\n", m->f.name);