NASM 0.98.08
[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 long
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 <stdlib.h>
21 #include <stdio.h>
22
23 #include "rdfload.h"
24 #include "symtab.h"
25 #include "rdoff.h"
26 #include "collectn.h"
27
28 extern int rdf_errno;
29
30 rdfmodule * rdfload(const char *filename)
31 {
32     rdfmodule   * f;
33     long        bsslength = 0;
34     char        * hdr;
35     rdfheaderrec *r;
36
37     f = malloc(sizeof(rdfmodule));
38     if (f == NULL)
39     {
40         rdf_errno = 6;          /* out of memory */
41         return NULL;
42     }
43
44     f->symtab = symtabNew();
45     if (!f->symtab)
46     {
47         free(f);
48         rdf_errno = 6;
49         return NULL;
50     }
51
52     /* open the file */
53     if ( rdfopen( &(f->f), filename ) ) {
54         free(f);
55         return NULL;
56     }
57
58     /* read in text and data segments, and header */
59     
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);
63
64     if (! f->t || ! f->d || !hdr) {
65         rdf_errno = 6;
66         rdfclose(&f->f);
67         if (f->t) free(f->t);
68         if (f->d) 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     {
77         rdfclose(&f->f);
78         free(f->t);
79         free(f->d);
80         free(f);
81         free(hdr);
82         return NULL;
83     }
84
85     rdfclose(&f->f);
86
87     /* Allocate BSS segment; step through header and count BSS records */
88
89     while ( ( r = rdfgetheaderrec (&f->f) ) )
90     {
91         if (r->type == 5)
92             bsslength += r->b.amount;
93     }
94
95     f->b = malloc ( bsslength );
96     if (bsslength && (!f->b))
97     {
98         free(f->t);
99         free(f->d);
100         free(f);
101         free(hdr);
102         rdf_errno = 6;
103         return NULL;
104     }
105
106     rdfheaderrewind (&f->f);
107
108     f->textrel = (long)f->t;
109     f->datarel = (long)f->d;
110     f->bssrel  = (long)f->b;
111
112     return f;
113 }
114
115 int rdf_relocate(rdfmodule * m)
116 {
117     rdfheaderrec        * r;
118     Collection          imports;    
119     symtabEnt           e;
120     long                rel;
121     unsigned char       * seg;
122     
123     rdfheaderrewind ( & m->f );
124     collection_init(&imports);
125
126     while ( (r = rdfgetheaderrec ( & m->f ) ) )
127     {
128         switch (r->type)
129         {
130         case 1:         /* Relocation record */
131
132             /* calculate relocation factor */
133
134             if (r->r.refseg == 0) rel = m->textrel;
135             else if (r->r.refseg == 1) rel = m->datarel;
136             else if (r->r.refseg == 2) rel = m->bssrel;
137             else
138                 /* We currently do not support load-time linkage.
139                    This should be added some time soon... */
140
141                 return 1;       /* return error code */
142
143             if ((r->r.segment & 63) == 0) seg = m->t;
144             else if ((r->r.segment & 63) == 1) 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] += (char) rel;
154                 break;
155             case 2:
156                 *(int16 *)(seg + r->r.offset) += (int16) rel;
157                 break;
158             case 4:
159                 *(long *)(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 + 
167                        (e.segment == 0 ? m->textrel : /* 0 -> code */
168                         e.segment == 1 ? m->datarel : /* 1 -> data */
169                                          m->bssrel) ; /* 2 -> bss  */
170             e.flags = 0;
171             e.name = malloc(strlen(r->e.label) + 1);
172             if (! e.name)
173                 return 1;
174
175             strcpy(e.name,r->e.label);
176             symtabInsert(m->symtab,&e);
177             break;
178
179         case 6:                 /* segment relocation */
180             fprintf(stderr, "%s: segment relocation not supported by this "
181                     "loader\n", m->f.name);
182             return 1;
183         }
184     }    
185     return 0;
186 }