NASM 0.91
[nasm/sigaren-mirror.git] / outbin.c
blob82c85106974a2490428793c25e9be1bf3f9ae706
1 /* outbin.c output routines for the Netwide Assembler to produce
2 * flat-form binary files
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "nasm.h"
14 #include "nasmlib.h"
15 #include "outform.h"
17 #ifdef OF_BIN
19 static FILE *fp;
20 static efunc error;
22 static struct Section {
23 struct SAA *contents;
24 long length;
25 long index;
26 } textsect, datasect;
27 static long bsslen, bssindex;
29 static struct Reloc {
30 struct Reloc *next;
31 long posn;
32 long bytes;
33 long secref;
34 long secrel;
35 struct Section *target;
36 } *relocs, **reloctail;
38 static int start_point;
40 static void add_reloc (struct Section *s, long bytes, long secref,
41 long secrel) {
42 struct Reloc *r;
44 r = *reloctail = nasm_malloc(sizeof(struct Reloc));
45 reloctail = &r->next;
46 r->next = NULL;
47 r->posn = s->length;
48 r->bytes = bytes;
49 r->secref = secref;
50 r->secrel = secrel;
51 r->target = s;
54 static void bin_init (FILE *afp, efunc errfunc, ldfunc ldef) {
55 fp = afp;
57 error = errfunc;
58 (void) ldef; /* placate optimisers */
60 start_point = 0; /* default */
61 textsect.contents = saa_init(1L);
62 datasect.contents = saa_init(1L);
63 textsect.length = datasect.length = 0;
64 textsect.index = seg_alloc();
65 datasect.index = seg_alloc();
66 bsslen = 0;
67 bssindex = seg_alloc();
68 relocs = NULL;
69 reloctail = &relocs;
72 static void bin_cleanup (void) {
73 struct Reloc *r;
74 long datapos, dataalign, bsspos;
76 datapos = (start_point + textsect.length + 3) & ~3;/* align on 4 bytes */
77 dataalign = datapos - (start_point + textsect.length);
79 saa_rewind (textsect.contents);
80 saa_rewind (datasect.contents);
82 bsspos = (datapos + datasect.length + 3) & ~3;
84 for (r = relocs; r; r = r->next) {
85 unsigned char *p, *q, mydata[4];
86 long l;
88 saa_fread (r->target->contents, r->posn, mydata, r->bytes);
89 p = q = mydata;
90 l = *p++;
91 l += ((long)*p++) << 8;
92 if (r->bytes == 4) {
93 l += ((long)*p++) << 16;
94 l += ((long)*p++) << 24;
97 if (r->secref == textsect.index)
98 l += start_point;
99 else if (r->secref == datasect.index)
100 l += datapos;
101 else if (r->secref == bssindex)
102 l += bsspos;
104 if (r->secrel == textsect.index)
105 l -= start_point;
106 else if (r->secrel == datasect.index)
107 l -= datapos;
108 else if (r->secrel == bssindex)
109 l -= bsspos;
111 if (r->bytes == 4)
112 WRITELONG(q, l);
113 else
114 WRITESHORT(q, l);
115 saa_fwrite (r->target->contents, r->posn, mydata, r->bytes);
117 saa_fpwrite (textsect.contents, fp);
118 if (datasect.length > 0) {
119 fwrite ("\0\0\0\0", dataalign, 1, fp);
120 saa_fpwrite (datasect.contents, fp);
122 fclose (fp);
123 saa_free (textsect.contents);
124 saa_free (datasect.contents);
125 while (relocs) {
126 r = relocs->next;
127 nasm_free (relocs);
128 relocs = r;
132 static void bin_out (long segto, void *data, unsigned long type,
133 long segment, long wrt) {
134 unsigned char *p, mydata[4];
135 struct Section *s;
136 long realbytes;
138 if (wrt != NO_SEG) {
139 wrt = NO_SEG; /* continue to do _something_ */
140 error (ERR_NONFATAL, "WRT not supported by binary output format");
144 * handle absolute-assembly (structure definitions)
146 if (segto == NO_SEG) {
147 if ((type & OUT_TYPMASK) != OUT_RESERVE)
148 error (ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
149 " space");
150 return;
153 if (segto == bssindex) { /* BSS */
154 if ((type & OUT_TYPMASK) != OUT_RESERVE)
155 error(ERR_WARNING, "attempt to initialise memory in the"
156 " BSS section: ignored");
157 s = NULL;
158 } else if (segto == textsect.index) {
159 s = &textsect;
160 } else if (segto == datasect.index) {
161 s = &datasect;
162 } else {
163 error(ERR_WARNING, "attempt to assemble code in"
164 " segment %d: defaulting to `.text'", segto);
165 s = &textsect;
168 if ((type & OUT_TYPMASK) == OUT_ADDRESS) {
169 if (segment != NO_SEG &&
170 segment != textsect.index &&
171 segment != datasect.index &&
172 segment != bssindex) {
173 if (segment % 2)
174 error(ERR_NONFATAL, "binary output format does not support"
175 " segment base references");
176 else
177 error(ERR_NONFATAL, "binary output format does not support"
178 " external references");
179 segment = NO_SEG;
181 if (s) {
182 if (segment != NO_SEG)
183 add_reloc (s, type & OUT_SIZMASK, segment, -1L);
184 p = mydata;
185 if ((type & OUT_SIZMASK) == 4)
186 WRITELONG (p, *(long *)data);
187 else
188 WRITESHORT (p, *(long *)data);
189 saa_wbytes (s->contents, mydata, type & OUT_SIZMASK);
190 s->length += type & OUT_SIZMASK;
191 } else
192 bsslen += type & OUT_SIZMASK;
193 } else if ((type & OUT_TYPMASK) == OUT_RAWDATA) {
194 type &= OUT_SIZMASK;
195 p = data;
196 if (s) {
197 saa_wbytes (s->contents, data, type);
198 s->length += type;
199 } else
200 bsslen += type;
201 } else if ((type & OUT_TYPMASK) == OUT_RESERVE) {
202 if (s) {
203 error(ERR_WARNING, "uninitialised space declared in"
204 " %s section: zeroing",
205 (segto == textsect.index ? "code" : "data"));
207 type &= OUT_SIZMASK;
208 if (s) {
209 saa_wbytes (s->contents, NULL, type);
210 s->length += type;
211 } else
212 bsslen += type;
213 } else if ((type & OUT_TYPMASK) == OUT_REL2ADR ||
214 (type & OUT_TYPMASK) == OUT_REL4ADR) {
215 realbytes = ((type & OUT_TYPMASK) == OUT_REL4ADR ? 4 : 2);
216 if (segment != NO_SEG &&
217 segment != textsect.index &&
218 segment != datasect.index &&
219 segment != bssindex) {
220 if (segment % 2)
221 error(ERR_NONFATAL, "binary output format does not support"
222 " segment base references");
223 else
224 error(ERR_NONFATAL, "binary output format does not support"
225 " external references");
226 segment = NO_SEG;
228 if (s) {
229 add_reloc (s, realbytes, segment, segto);
230 p = mydata;
231 if (realbytes == 4)
232 WRITELONG (p, *(long*)data - realbytes - s->length);
233 else
234 WRITESHORT (p, *(long*)data - realbytes - s->length);
235 saa_wbytes (s->contents, mydata, realbytes);
236 s->length += realbytes;
237 } else
238 bsslen += realbytes;
242 static void bin_deflabel (char *name, long segment, long offset,
243 int is_global) {
244 if (is_global == 2) {
245 error (ERR_NONFATAL, "binary output format does not support common"
246 " variables");
250 static long bin_secname (char *name, int pass, int *bits) {
252 * Default is 16 bits.
254 if (!name)
255 *bits = 16;
257 if (!name)
258 return textsect.index;
260 if (!strcmp(name, ".text"))
261 return textsect.index;
262 else if (!strcmp(name, ".data"))
263 return datasect.index;
264 else if (!strcmp(name, ".bss"))
265 return bssindex;
266 else
267 return NO_SEG;
270 static long bin_segbase (long segment) {
271 return segment;
274 static int bin_directive (char *directive, char *value, int pass) {
275 int rn_error;
277 if (!strcmp(directive, "org")) {
278 start_point = readnum (value, &rn_error);
279 if (rn_error)
280 error (ERR_NONFATAL, "argument to ORG should be numeric");
281 return 1;
282 } else
283 return 0;
286 static void bin_filename (char *inname, char *outname, efunc error) {
287 standard_extension (inname, outname, "", error);
290 struct ofmt of_bin = {
291 "flat-form binary files (e.g. DOS .COM, .SYS)",
292 "bin",
293 bin_init,
294 bin_out,
295 bin_deflabel,
296 bin_secname,
297 bin_segbase,
298 bin_directive,
299 bin_filename,
300 bin_cleanup
303 #endif /* OF_BIN */