General push for x86-64 support, dubbed 0.99.00.
[platform/upstream/nasm.git] / rdoff / rdfload.c
1 /* rdfload.c    RDOFF Object File loader library
2  *
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.
7  *
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.
11  */
12
13 /*
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)
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "rdfload.h"
25 #include "symtab.h"
26 #include "collectn.h"
27
28 extern int rdf_errno;
29
30 rdfmodule *rdfload(const int8_t *filename)
31 {
32     rdfmodule *f;
33     int32_t bsslength = 0;
34     int8_t *hdr;
35     rdfheaderrec *r;
36
37     f = malloc(sizeof(rdfmodule));
38     if (f == NULL) {
39         rdf_errno = RDF_ERR_NOMEM;
40         return NULL;
41     }
42
43     f->symtab = symtabNew();
44     if (!f->symtab) {
45         free(f);
46         rdf_errno = RDF_ERR_NOMEM;
47         return NULL;
48     }
49
50     /* open the file */
51     if (rdfopen(&(f->f), filename)) {
52         free(f);
53         return NULL;
54     }
55
56     /* read in text and data segments, and header */
57
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);
61
62     if (!f->t || !f->d || !hdr) {
63         rdf_errno = RDF_ERR_NOMEM;
64         rdfclose(&f->f);
65         if (f->t)
66             free(f->t);
67         if (f->d)
68             free(f->d);
69         free(f);
70         return NULL;
71     }
72
73     if (rdfloadseg(&f->f, RDOFF_HEADER, hdr) ||
74         rdfloadseg(&f->f, RDOFF_CODE, f->t) ||
75         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;
82     }
83
84     rdfclose(&f->f);
85
86     /* Allocate BSS segment; step through header and count BSS records */
87
88     while ((r = rdfgetheaderrec(&f->f))) {
89         if (r->type == 5)
90             bsslength += r->b.amount;
91     }
92
93     f->b = malloc(bsslength);
94     if (bsslength && (!f->b)) {
95         free(f->t);
96         free(f->d);
97         free(f);
98         free(hdr);
99         rdf_errno = RDF_ERR_NOMEM;
100         return NULL;
101     }
102
103     rdfheaderrewind(&f->f);
104
105     f->textrel = (int32_t)f->t;
106     f->datarel = (int32_t)f->d;
107     f->bssrel = (int32_t)f->b;
108
109     return f;
110 }
111
112 int rdf_relocate(rdfmodule * m)
113 {
114     rdfheaderrec *r;
115     Collection imports;
116     symtabEnt e;
117     int32_t rel;
118     uint8_t *seg;
119
120     rdfheaderrewind(&m->f);
121     collection_init(&imports);
122
123     while ((r = rdfgetheaderrec(&m->f))) {
124         switch (r->type) {
125         case 1:                /* Relocation record */
126
127             /* calculate relocation factor */
128
129             if (r->r.refseg == 0)
130                 rel = m->textrel;
131             else if (r->r.refseg == 1)
132                 rel = m->datarel;
133             else if (r->r.refseg == 2)
134                 rel = m->bssrel;
135             else
136                 /* We currently do not support load-time linkage.
137                    This should be added some time soon... */
138
139                 return 1;       /* return error code */
140
141             if ((r->r.segment & 63) == 0)
142                 seg = m->t;
143             else if ((r->r.segment & 63) == 1)
144                 seg = m->d;
145             else
146                 continue;       /* relocation not in a loaded segment */
147
148             /* it doesn't matter in this case that the code is non-portable,
149                as the entire concept of executing a module like this is
150                non-portable */
151             switch (r->r.length) {
152             case 1:
153                 seg[r->r.offset] += (int8_t)rel;
154                 break;
155             case 2:
156                 *(uint16 *) (seg + r->r.offset) += (uint16) rel;
157                 break;
158             case 4:
159                 *(int32_t *)(seg + r->r.offset) += rel;
160                 break;
161             }
162             break;
163
164         case 3:                /* export record - add to symtab */
165             e.segment = r->e.segment;
166             e.offset = r->e.offset + (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;
173
174             strcpy(e.name, r->e.label);
175             symtabInsert(m->symtab, &e);
176             break;
177
178         case 6:                /* segment relocation */
179             fprintf(stderr, "%s: segment relocation not supported by this "
180                     "loader\n", m->f.name);
181             return 1;
182         }
183     }
184     return 0;
185 }