]>
Commit | Line | Data |
---|---|---|
14c7c974 A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
f083c6c3 A |
6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
7 | * | |
8 | * This file contains Original Code and/or Modifications of Original Code | |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14c7c974 A |
14 | * |
15 | * The Original Code and all software distributed under the License are | |
f083c6c3 | 16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
14c7c974 A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
f083c6c3 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
14c7c974 A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* outrdf.c output routines for the Netwide Assembler to produce | |
26 | * RDOFF format object files (which are intended mainly | |
27 | * for use in proprietary projects, as the code to load and | |
28 | * execute them is very simple). They will also be used | |
29 | * for device drivers and possibly some executable files | |
30 | * in the MOSCOW operating system. See Rdoff.txt for | |
31 | * details. | |
32 | * | |
33 | * The Netwide Assembler is copyright (C) 1996 Simon Tatham and | |
34 | * Julian Hall. All rights reserved. The software is | |
35 | * redistributable under the licence given in the file "Licence" | |
36 | * distributed in the NASM archive. | |
37 | */ | |
38 | ||
39 | #include <stdio.h> | |
40 | #include <stdlib.h> | |
41 | #include <string.h> | |
42 | #include <ctype.h> | |
43 | #include <assert.h> | |
44 | ||
45 | #include "nasm.h" | |
46 | #include "nasmlib.h" | |
47 | #include "outform.h" | |
48 | ||
49 | /* VERBOSE_WARNINGS: define this to add some extra warnings... */ | |
50 | #define VERBOSE_WARNINGS | |
51 | ||
52 | #ifdef OF_RDF | |
53 | ||
54 | typedef short int16; /* not sure if this will be required to be altered | |
55 | at all... best to typedef it just in case */ | |
56 | ||
57 | static const char *RDOFFId = "RDOFF1"; /* written to start of RDOFF files */ | |
58 | ||
59 | /* the records that can be found in the RDOFF header */ | |
60 | ||
61 | /* Note that whenever a segment is referred to in the RDOFF file, its number | |
62 | * is always half of the segment number that NASM uses to refer to it; this | |
63 | * is because NASM only allocates even numbered segments, so as to not | |
64 | * waste any of the 16 bits of segment number written to the file - this | |
65 | * allows up to 65533 external labels to be defined; otherwise it would be | |
66 | * 32764. */ | |
67 | ||
68 | struct RelocRec { | |
69 | char type; /* must be 1 */ | |
70 | char segment; /* only 0 for code, or 1 for data supported, | |
71 | * but add 64 for relative refs (ie do not require | |
72 | * reloc @ loadtime, only linkage) */ | |
73 | long offset; /* from start of segment in which reference is loc'd */ | |
74 | char length; /* 1 2 or 4 bytes */ | |
75 | int16 refseg; /* segment to which reference refers to */ | |
76 | }; | |
77 | ||
78 | struct ImportRec { | |
79 | char type; /* must be 2 */ | |
80 | int16 segment; /* segment number allocated to the label for reloc | |
81 | * records - label is assumed to be at offset zero | |
82 | * in this segment, so linker must fix up with offset | |
83 | * of segment and of offset within segment */ | |
84 | char label[33]; /* zero terminated... should be written to file until | |
85 | * the zero, but not after it - max len = 32 chars */ | |
86 | }; | |
87 | ||
88 | struct ExportRec { | |
89 | char type; /* must be 3 */ | |
90 | char segment; /* segment referred to (0/1) */ | |
91 | long offset; /* offset within segment */ | |
92 | char label[33]; /* zero terminated as above. max len = 32 chars */ | |
93 | }; | |
94 | ||
95 | struct DLLRec { | |
96 | char type; /* must be 4 */ | |
97 | char libname[128]; /* name of library to link with at load time */ | |
98 | }; | |
99 | ||
100 | struct BSSRec { | |
101 | char type; /* must be 5 */ | |
102 | long amount; /* number of bytes BSS to reserve */ | |
103 | }; | |
104 | ||
105 | /* code for managing buffers needed to seperate code and data into individual | |
106 | * sections until they are ready to be written to the file. | |
107 | * We'd better hope that it all fits in memory else we're buggered... */ | |
108 | ||
109 | #define BUF_BLOCK_LEN 4088 /* selected to match page size (4096) | |
110 | * on 80x86 machines for efficiency */ | |
111 | ||
112 | typedef struct memorybuffer { | |
113 | int length; | |
114 | char buffer[BUF_BLOCK_LEN]; | |
115 | struct memorybuffer *next; | |
116 | } memorybuffer; | |
117 | ||
118 | static memorybuffer * newmembuf(void){ | |
119 | memorybuffer * t; | |
120 | ||
121 | t = nasm_malloc(sizeof(memorybuffer)); | |
122 | ||
123 | t->length = 0; | |
124 | t->next = NULL; | |
125 | return t; | |
126 | } | |
127 | ||
128 | static void membufwrite(memorybuffer *b, void *data, int bytes) { | |
129 | int16 w; | |
130 | long l; | |
131 | ||
132 | if (b->next) { /* memory buffer full - use next buffer */ | |
133 | membufwrite(b->next,data,bytes); | |
134 | return; | |
135 | } | |
136 | if ((bytes < 0 && b->length - bytes > BUF_BLOCK_LEN) | |
137 | || (bytes > 0 && b->length + bytes > BUF_BLOCK_LEN)) { | |
138 | ||
139 | /* buffer full and no next allocated... allocate and initialise next | |
140 | * buffer */ | |
141 | ||
142 | b->next = newmembuf(); | |
143 | membufwrite(b->next,data,bytes); | |
144 | return; | |
145 | } | |
146 | ||
147 | switch(bytes) { | |
148 | case -4: /* convert to little-endian */ | |
149 | l = * (long *) data ; | |
150 | b->buffer[b->length++] = l & 0xFF; | |
151 | l >>= 8 ; | |
152 | b->buffer[b->length++] = l & 0xFF; | |
153 | l >>= 8 ; | |
154 | b->buffer[b->length++] = l & 0xFF; | |
155 | l >>= 8 ; | |
156 | b->buffer[b->length++] = l & 0xFF; | |
157 | break; | |
158 | ||
159 | case -2: | |
160 | w = * (int16 *) data ; | |
161 | b->buffer[b->length++] = w & 0xFF; | |
162 | w >>= 8 ; | |
163 | b->buffer[b->length++] = w & 0xFF; | |
164 | break; | |
165 | ||
166 | default: | |
167 | while(bytes--) { | |
168 | b->buffer[b->length++] = *(* (unsigned char **) &data); | |
169 | ||
170 | (* (unsigned char **) &data)++ ; | |
171 | } | |
172 | break; | |
173 | } | |
174 | } | |
175 | ||
176 | static void membufdump(memorybuffer *b,FILE *fp) | |
177 | { | |
178 | if (!b) return; | |
179 | ||
180 | fwrite (b->buffer, 1, b->length, fp); | |
181 | ||
182 | membufdump(b->next,fp); | |
183 | } | |
184 | ||
185 | static int membuflength(memorybuffer *b) | |
186 | { | |
187 | if (!b) return 0; | |
188 | return b->length + membuflength(b->next); | |
189 | } | |
190 | ||
191 | static void freemembuf(memorybuffer *b) | |
192 | { | |
193 | if (!b) return; | |
194 | freemembuf(b->next); | |
195 | nasm_free(b); | |
196 | } | |
197 | ||
198 | /*********************************************************************** | |
199 | * Actual code to deal with RDOFF ouput format begins here... | |
200 | */ | |
201 | ||
202 | /* global variables set during the initialisation phase */ | |
203 | ||
204 | static memorybuffer *seg[2]; /* seg 0 = code, seg 1 = data */ | |
205 | static memorybuffer *header; /* relocation/import/export records */ | |
206 | ||
207 | static FILE *ofile; | |
208 | ||
209 | static efunc error; | |
210 | ||
211 | static int segtext,segdata,segbss; | |
212 | static long bsslength; | |
213 | ||
214 | static void rdf_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval) | |
215 | { | |
216 | ofile = fp; | |
217 | error = errfunc; | |
218 | seg[0] = newmembuf(); | |
219 | seg[1] = newmembuf(); | |
220 | header = newmembuf(); | |
221 | segtext = seg_alloc(); | |
222 | segdata = seg_alloc(); | |
223 | segbss = seg_alloc(); | |
224 | if (segtext != 0 || segdata != 2 || segbss != 4) | |
225 | error(ERR_PANIC,"rdf segment numbers not allocated as expected (%d,%d,%d)", | |
226 | segtext,segdata,segbss); | |
227 | bsslength=0; | |
228 | } | |
229 | ||
230 | static long rdf_section_names(char *name, int pass, int *bits) | |
231 | { | |
232 | /* | |
233 | * Default is 32 bits. | |
234 | */ | |
235 | if (!name) | |
236 | *bits = 32; | |
237 | ||
238 | if (!name) return 0; | |
239 | if (!strcmp(name, ".text")) return 0; | |
240 | else if (!strcmp(name, ".data")) return 2; | |
241 | else if (!strcmp(name, ".bss")) return 4; | |
242 | else | |
243 | return NO_SEG; | |
244 | } | |
245 | ||
246 | static void write_reloc_rec(struct RelocRec *r) | |
247 | { | |
248 | if (r->refseg != NO_SEG && (r->refseg & 1)) | |
249 | error (ERR_NONFATAL, "RDF format does not support segment base" | |
250 | " references"); | |
251 | ||
252 | r->refseg >>= 1; /* adjust segment nos to RDF rather than NASM */ | |
253 | ||
254 | membufwrite(header,&r->type,1); | |
255 | membufwrite(header,&r->segment,1); | |
256 | membufwrite(header,&r->offset,-4); | |
257 | membufwrite(header,&r->length,1); | |
258 | membufwrite(header,&r->refseg,-2); /* 9 bytes written */ | |
259 | } | |
260 | ||
261 | static void write_export_rec(struct ExportRec *r) | |
262 | { | |
263 | r->segment >>= 1; | |
264 | ||
265 | membufwrite(header,&r->type,1); | |
266 | membufwrite(header,&r->segment,1); | |
267 | membufwrite(header,&r->offset,-4); | |
268 | membufwrite(header,r->label,strlen(r->label) + 1); | |
269 | } | |
270 | ||
271 | static void write_import_rec(struct ImportRec *r) | |
272 | { | |
273 | r->segment >>= 1; | |
274 | ||
275 | membufwrite(header,&r->type,1); | |
276 | membufwrite(header,&r->segment,-2); | |
277 | membufwrite(header,r->label,strlen(r->label) + 1); | |
278 | } | |
279 | ||
280 | static void write_bss_rec(struct BSSRec *r) | |
281 | { | |
282 | membufwrite(header,&r->type,1); | |
283 | membufwrite(header,&r->amount,-4); | |
284 | } | |
285 | ||
286 | static void write_dll_rec(struct DLLRec *r) | |
287 | { | |
288 | membufwrite(header,&r->type,1); | |
289 | membufwrite(header,r->libname,strlen(r->libname) + 1); | |
290 | } | |
291 | ||
292 | static void rdf_deflabel(char *name, long segment, long offset, | |
293 | int is_global, char *special) | |
294 | { | |
295 | struct ExportRec r; | |
296 | struct ImportRec ri; | |
297 | #ifdef VERBOSE_WARNINGS | |
298 | static int warned_common = 0; | |
299 | #endif | |
300 | ||
301 | if (special) | |
302 | error (ERR_NONFATAL, "RDOFF format does not support any" | |
303 | " special symbol types"); | |
304 | ||
305 | if (name[0] == '.' && name[1] == '.' && name[2] != '@') { | |
306 | error (ERR_NONFATAL, "unrecognised special symbol `%s'", name); | |
307 | return; | |
308 | } | |
309 | ||
310 | if (is_global == 2) { | |
311 | #ifdef VERBOSE_WARNINGS | |
312 | if (!warned_common) { | |
313 | error(ERR_WARNING,"common declarations not supported: using extern"); | |
314 | warned_common = 1; | |
315 | } | |
316 | #endif | |
317 | is_global = 1; | |
318 | } | |
319 | ||
320 | if (segment > 4) { /* EXTERN declaration */ | |
321 | ri.type = 2; | |
322 | ri.segment = segment; | |
323 | strncpy(ri.label,name,32); | |
324 | ri.label[32] = 0; | |
325 | write_import_rec(&ri); | |
326 | } else if (is_global) { | |
327 | r.type = 3; | |
328 | r.segment = segment; | |
329 | r.offset = offset; | |
330 | strncpy(r.label,name,32); | |
331 | r.label[32] = 0; | |
332 | write_export_rec(&r); | |
333 | } | |
334 | } | |
335 | ||
336 | static void rdf_out (long segto, void *data, unsigned long type, | |
337 | long segment, long wrt) | |
338 | { | |
339 | long bytes = type & OUT_SIZMASK; | |
340 | struct RelocRec rr; | |
341 | unsigned char databuf[4],*pd; | |
342 | ||
343 | segto >>= 1; /* convert NASM segment no to RDF number */ | |
344 | ||
345 | if (segto != 0 && segto != 1 && segto != 2) { | |
346 | error(ERR_NONFATAL,"specified segment not supported by rdf output format"); | |
347 | return; | |
348 | } | |
349 | ||
350 | if (wrt != NO_SEG) { | |
351 | wrt = NO_SEG; /* continue to do _something_ */ | |
352 | error (ERR_NONFATAL, "WRT not supported by rdf output format"); | |
353 | } | |
354 | ||
355 | type &= OUT_TYPMASK; | |
356 | ||
357 | if (segto == 2 && type != OUT_RESERVE) | |
358 | { | |
359 | error(ERR_NONFATAL, "BSS segments may not be initialised"); | |
360 | ||
361 | /* just reserve the space for now... */ | |
362 | ||
363 | if (type == OUT_REL2ADR) | |
364 | bytes = 2; | |
365 | else | |
366 | bytes = 4; | |
367 | type = OUT_RESERVE; | |
368 | } | |
369 | ||
370 | if (type == OUT_RESERVE) { | |
371 | if (segto == 2) /* BSS segment space reserverd */ | |
372 | bsslength += bytes; | |
373 | else | |
374 | while (bytes --) | |
375 | membufwrite(seg[segto],databuf,1); | |
376 | } | |
377 | else if (type == OUT_RAWDATA) { | |
378 | if (segment != NO_SEG) | |
379 | error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG"); | |
380 | membufwrite(seg[segto],data,bytes); | |
381 | } | |
382 | else if (type == OUT_ADDRESS) { | |
383 | ||
384 | /* if segment == NO_SEG then we are writing an address of an | |
385 | object within the same segment - do not produce reloc rec. */ | |
386 | ||
387 | if (segment != NO_SEG) | |
388 | { | |
389 | ||
390 | /* it's an address, so we must write a relocation record */ | |
391 | ||
392 | rr.type = 1; /* type signature */ | |
393 | rr.segment = segto; /* segment we're currently in */ | |
394 | rr.offset = membuflength(seg[segto]); /* current offset */ | |
395 | rr.length = bytes; /* length of reference */ | |
396 | rr.refseg = segment; /* segment referred to */ | |
397 | write_reloc_rec(&rr); | |
398 | } | |
399 | ||
400 | pd = databuf; /* convert address to little-endian */ | |
401 | if (bytes == 2) | |
402 | WRITESHORT (pd, *(long *)data); | |
403 | else | |
404 | WRITELONG (pd, *(long *)data); | |
405 | ||
406 | membufwrite(seg[segto],databuf,bytes); | |
407 | ||
408 | } | |
409 | else if (type == OUT_REL2ADR) | |
410 | { | |
411 | if (segment == segto) | |
412 | error(ERR_PANIC, "intra-segment OUT_REL2ADR"); | |
413 | if (segment != NO_SEG && segment % 2) { | |
414 | error(ERR_NONFATAL, "rdf format does not support segment base refs"); | |
415 | } | |
416 | ||
417 | rr.type = 1; /* type signature */ | |
418 | rr.segment = segto+64; /* segment we're currently in + rel flag */ | |
419 | rr.offset = membuflength(seg[segto]); /* current offset */ | |
420 | rr.length = 2; /* length of reference */ | |
421 | rr.refseg = segment; /* segment referred to */ | |
422 | write_reloc_rec(&rr); | |
423 | ||
424 | /* work out what to put in the code: offset of the end of this operand, | |
425 | * subtracted from any data specified, so that loader can just add | |
426 | * address of imported symbol onto it to get address relative to end of | |
427 | * instruction: import_address + data(offset) - end_of_instrn */ | |
428 | ||
429 | rr.offset = *(long *)data -(rr.offset + bytes); | |
430 | ||
431 | membufwrite(seg[segto],&rr.offset,-2); | |
432 | } | |
433 | else if (type == OUT_REL4ADR) | |
434 | { | |
435 | if (segment == segto) | |
436 | error(ERR_PANIC, "intra-segment OUT_REL4ADR"); | |
437 | if (segment != NO_SEG && segment % 2) { | |
438 | error(ERR_NONFATAL, "rdf format does not support segment base refs"); | |
439 | } | |
440 | ||
441 | rr.type = 1; /* type signature */ | |
442 | rr.segment = segto+64; /* segment we're currently in + rel tag */ | |
443 | rr.offset = membuflength(seg[segto]); /* current offset */ | |
444 | rr.length = 4; /* length of reference */ | |
445 | rr.refseg = segment; /* segment referred to */ | |
446 | write_reloc_rec(&rr); | |
447 | ||
448 | rr.offset = *(long *)data -(rr.offset + bytes); | |
449 | membufwrite(seg[segto],&rr.offset,-4); | |
450 | } | |
451 | } | |
452 | ||
453 | static void rdf_cleanup (void) { | |
454 | long l; | |
455 | unsigned char b[4],*d; | |
456 | struct BSSRec bs; | |
457 | ||
458 | ||
459 | /* should write imported & exported symbol declarations to header here */ | |
460 | ||
461 | /* generate the output file... */ | |
462 | fwrite(RDOFFId,6,1,ofile); /* file type magic number */ | |
463 | ||
464 | if (bsslength != 0) /* reserve BSS */ | |
465 | { | |
466 | bs.type = 5; | |
467 | bs.amount = bsslength; | |
468 | write_bss_rec(&bs); | |
469 | } | |
470 | ||
471 | l = membuflength(header);d=b; | |
472 | WRITELONG(d,l); | |
473 | ||
474 | fwrite(b,4,1,ofile); /* write length of header */ | |
475 | membufdump(header,ofile); /* dump header */ | |
476 | ||
477 | l = membuflength(seg[0]);d=b; /* code segment */ | |
478 | WRITELONG(d,l); | |
479 | ||
480 | fwrite(b,4,1,ofile); | |
481 | membufdump(seg[0],ofile); | |
482 | ||
483 | l = membuflength(seg[1]);d=b; /* data segment */ | |
484 | WRITELONG(d,l); | |
485 | ||
486 | fwrite(b,4,1,ofile); | |
487 | membufdump(seg[1],ofile); | |
488 | ||
489 | freemembuf(header); | |
490 | freemembuf(seg[0]); | |
491 | freemembuf(seg[1]); | |
492 | fclose(ofile); | |
493 | } | |
494 | ||
495 | static long rdf_segbase (long segment) { | |
496 | return segment; | |
497 | } | |
498 | ||
499 | static int rdf_directive (char *directive, char *value, int pass) { | |
500 | struct DLLRec r; | |
501 | ||
502 | if (! strcmp(directive, "library")) { | |
503 | if (pass == 1) { | |
504 | r.type = 4; | |
505 | strcpy(r.libname, value); | |
506 | write_dll_rec(&r); | |
507 | } | |
508 | return 1; | |
509 | } | |
510 | ||
511 | return 0; | |
512 | } | |
513 | ||
514 | static void rdf_filename (char *inname, char *outname, efunc error) { | |
515 | standard_extension(inname,outname,".rdf",error); | |
516 | } | |
517 | ||
518 | static char *rdf_stdmac[] = { | |
519 | "%define __SECT__ [section .text]", | |
520 | "%imacro library 1+.nolist", | |
521 | "[library %1]", | |
522 | "%endmacro", | |
523 | NULL | |
524 | }; | |
525 | ||
526 | struct ofmt of_rdf = { | |
527 | "Relocatable Dynamic Object File Format v1.1", | |
528 | "rdf", | |
529 | rdf_stdmac, | |
530 | rdf_init, | |
531 | rdf_out, | |
532 | rdf_deflabel, | |
533 | rdf_section_names, | |
534 | rdf_segbase, | |
535 | rdf_directive, | |
536 | rdf_filename, | |
537 | rdf_cleanup | |
538 | }; | |
539 | ||
540 | #endif /* OF_RDF */ |