Better (but not *good!*) handling of 64-bit addressing in ndisasm
[nasm.git] / rdoff / rdfload.c
blob03164b53cc38314cc43be1967957e9dd5bdd7a22
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 int32_t
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 "compiler.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "rdfload.h"
27 #include "symtab.h"
28 #include "collectn.h"
30 extern int rdf_errno;
32 rdfmodule *rdfload(const char *filename)
34 rdfmodule *f;
35 int32_t bsslength = 0;
36 char *hdr;
37 rdfheaderrec *r;
39 f = malloc(sizeof(rdfmodule));
40 if (f == NULL) {
41 rdf_errno = RDF_ERR_NOMEM;
42 return NULL;
45 f->symtab = symtabNew();
46 if (!f->symtab) {
47 free(f);
48 rdf_errno = RDF_ERR_NOMEM;
49 return NULL;
52 /* open the file */
53 if (rdfopen(&(f->f), filename)) {
54 free(f);
55 return NULL;
58 /* read in text and data segments, and header */
60 f->t = malloc(f->f.seg[0].length);
61 f->d = malloc(f->f.seg[1].length); /* BSS seg allocated later */
62 hdr = malloc(f->f.header_len);
64 if (!f->t || !f->d || !hdr) {
65 rdf_errno = RDF_ERR_NOMEM;
66 rdfclose(&f->f);
67 if (f->t)
68 free(f->t);
69 if (f->d)
70 free(f->d);
71 free(f);
72 return NULL;
75 if (rdfloadseg(&f->f, RDOFF_HEADER, hdr) ||
76 rdfloadseg(&f->f, RDOFF_CODE, f->t) ||
77 rdfloadseg(&f->f, RDOFF_DATA, f->d)) {
78 rdfclose(&f->f);
79 free(f->t);
80 free(f->d);
81 free(f);
82 free(hdr);
83 return NULL;
86 rdfclose(&f->f);
88 /* Allocate BSS segment; step through header and count BSS records */
90 while ((r = rdfgetheaderrec(&f->f))) {
91 if (r->type == 5)
92 bsslength += r->b.amount;
95 f->b = malloc(bsslength);
96 if (bsslength && (!f->b)) {
97 free(f->t);
98 free(f->d);
99 free(f);
100 free(hdr);
101 rdf_errno = RDF_ERR_NOMEM;
102 return NULL;
105 rdfheaderrewind(&f->f);
107 f->textrel = (int32_t)(size_t)f->t;
108 f->datarel = (int32_t)(size_t)f->d;
109 f->bssrel = (int32_t)(size_t)f->b;
111 return f;
114 int rdf_relocate(rdfmodule * m)
116 rdfheaderrec *r;
117 Collection imports;
118 symtabEnt e;
119 int32_t rel;
120 uint8_t *seg;
122 rdfheaderrewind(&m->f);
123 collection_init(&imports);
125 while ((r = rdfgetheaderrec(&m->f))) {
126 switch (r->type) {
127 case 1: /* Relocation record */
129 /* calculate relocation factor */
131 if (r->r.refseg == 0)
132 rel = m->textrel;
133 else if (r->r.refseg == 1)
134 rel = m->datarel;
135 else if (r->r.refseg == 2)
136 rel = m->bssrel;
137 else
138 /* We currently do not support load-time linkage.
139 This should be added some time soon... */
141 return 1; /* return error code */
143 if ((r->r.segment & 63) == 0)
144 seg = m->t;
145 else if ((r->r.segment & 63) == 1)
146 seg = m->d;
147 else
148 continue; /* relocation not in a loaded segment */
150 /* it doesn't matter in this case that the code is non-portable,
151 as the entire concept of executing a module like this is
152 non-portable */
153 switch (r->r.length) {
154 case 1:
155 seg[r->r.offset] += (char)rel;
156 break;
157 case 2:
158 *(uint16_t *) (seg + r->r.offset) += (uint16_t) rel;
159 break;
160 case 4:
161 *(int32_t *)(seg + r->r.offset) += rel;
162 break;
164 break;
166 case 3: /* export record - add to symtab */
167 e.segment = r->e.segment;
168 e.offset = r->e.offset + (e.segment == 0 ? m->textrel : /* 0 -> code */
169 e.segment == 1 ? m->datarel : /* 1 -> data */
170 m->bssrel); /* 2 -> bss */
171 e.flags = 0;
172 e.name = malloc(strlen(r->e.label) + 1);
173 if (!e.name)
174 return 1;
176 strcpy(e.name, r->e.label);
177 symtabInsert(m->symtab, &e);
178 break;
180 case 6: /* segment relocation */
181 fprintf(stderr, "%s: segment relocation not supported by this "
182 "loader\n", m->f.name);
183 return 1;
186 return 0;