NASM 0.96
[nasm/avx512.git] / rdoff / rdfload.c
blobb8483444249e3025a6c208cf03a8ca7970697454
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 long
9 * as acknowledgement is given in an appropriate manner to its authors,
10 * with instructions of how to obtain a copy via ftp.
13 #include <stdlib.h>
14 #include <stdio.h>
16 #include "rdfload.h"
17 #include "symtab.h"
18 #include "rdoff.h"
19 #include "collectn.h"
21 extern int rdf_errno;
23 rdfmodule * rdfload(const char *filename)
25 rdfmodule * f = malloc(sizeof(rdfmodule));
26 long bsslength = 0;
27 char * hdr;
28 rdfheaderrec *r;
30 if (f == NULL)
32 rdf_errno = 6; /* out of memory */
33 return NULL;
36 f->symtab = symtabNew();
37 if (!f->symtab)
39 free(f);
40 rdf_errno = 6;
41 return NULL;
44 /* open the file */
45 if ( rdfopen( &(f->f), filename ) ) {
46 free(f);
47 return NULL;
50 /* read in text and data segments, and header */
52 f->t = malloc (f->f.code_len);
53 f->d = malloc (f->f.data_len); /* BSS seg allocated later */
54 hdr = malloc (f->f.header_len);
56 if (! f->t || ! f->d || !hdr) {
57 rdf_errno = 6;
58 rdfclose(&f->f);
59 if (f->t) free(f->t);
60 if (f->d) free(f->d);
61 free(f);
62 return NULL;
65 if ( rdfloadseg (&f->f,RDOFF_HEADER,hdr) ||
66 rdfloadseg (&f->f,RDOFF_CODE,f->t) ||
67 rdfloadseg (&f->f,RDOFF_DATA,f->d) )
69 rdfclose(&f->f);
70 free(f->t);
71 free(f->d);
72 free(f);
73 free(hdr);
74 return NULL;
77 rdfclose(&f->f);
79 /* Allocate BSS segment; step through header and count BSS records */
81 while ( ( r = rdfgetheaderrec (&f->f) ) )
83 if (r->type == 5)
84 bsslength += r->b.amount;
87 f->b = malloc ( bsslength );
88 if (! f->b )
90 free(f->t);
91 free(f->d);
92 free(f);
93 free(hdr);
94 rdf_errno = 6;
95 return NULL;
98 rdfheaderrewind (&f->f);
100 f->textrel = (long)f->t;
101 f->datarel = (long)f->d;
102 f->bssrel = (long)f->b;
104 return f;
107 int rdf_relocate(rdfmodule * m)
109 rdfheaderrec * r;
110 Collection imports;
111 symtabEnt e;
112 long rel;
113 unsigned char * seg;
115 rdfheaderrewind ( & m->f );
116 collection_init(&imports);
118 while ( (r = rdfgetheaderrec ( & m->f ) ) )
120 switch (r->type)
122 case 1: /* Relocation record */
124 /* calculate relocation factor */
126 if (r->r.refseg == 0) rel = m->textrel;
127 else if (r->r.refseg == 1) rel = m->datarel;
128 else if (r->r.refseg == 2) rel = m->bssrel;
129 else
130 /* We currently do not support load-time linkage.
131 This should be added some time soon... */
133 return 1; /* return error code */
135 if ((r->r.segment & 63) == 0) seg = m->t;
136 else if ((r->r.segment & 63) == 1) seg = m->d;
137 else
138 return 1;
140 /* it doesn't matter in this case that the code is non-portable,
141 as the entire concept of executing a module like this is
142 non-portable */
143 switch(r->r.length) {
144 case 1:
145 seg[r->r.offset] += (char) rel;
146 break;
147 case 2:
148 *(int16 *)(seg + r->r.offset) += (int16) rel;
149 break;
150 case 4:
151 *(long *)(seg + r->r.offset) += rel;
152 break;
154 break;
156 case 3: /* export record - add to symtab */
157 e.segment = r->e.segment;
158 e.offset = r->e.offset +
159 (e.segment == 0 ? m->textrel : /* 0 -> code */
160 e.segment == 1 ? m->datarel : /* 1 -> data */
161 m->bssrel) ; /* 2 -> bss */
162 e.flags = 0;
163 e.name = malloc(strlen(r->e.label) + 1);
164 if (! e.name)
165 return 1;
167 strcpy(e.name,r->e.label);
168 symtabInsert(m->symtab,&e);
169 break;
172 return 0;