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