]> git.saurik.com Git - apple/boot.git/blame - i386/nasm/outaout.c
boot-83.2.tar.gz
[apple/boot.git] / i386 / nasm / outaout.c
CommitLineData
14c7c974
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/* outaout.c output routines for the Netwide Assembler to produce
25 * Linux a.out object files
26 *
27 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
28 * Julian Hall. All rights reserved. The software is
29 * redistributable under the licence given in the file "Licence"
30 * distributed in the NASM archive.
31 */
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <ctype.h>
37
38#include "nasm.h"
39#include "nasmlib.h"
40#include "outform.h"
41
42#if defined OF_AOUT || defined OF_AOUTB
43
44#define RELTYPE_ABSOLUTE 0x00
45#define RELTYPE_RELATIVE 0x01
46#define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
47#define RELTYPE_GOTOFF 0x10
48#define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
49#define RELTYPE_PLT 0x21
50#define RELTYPE_SYMFLAG 0x08
51
52struct Reloc {
53 struct Reloc *next;
54 long address; /* relative to _start_ of section */
55 long symbol; /* symbol number or -ve section id */
56 int bytes; /* 2 or 4 */
57 int reltype; /* see above */
58};
59
60struct Symbol {
61 long strpos; /* string table position of name */
62 int type; /* symbol type - see flags below */
63 long value; /* address, or COMMON variable size */
64 long size; /* size for data or function exports */
65 long segment; /* back-reference used by gsym_reloc */
66 struct Symbol *next; /* list of globals in each section */
67 struct Symbol *nextfwd; /* list of unresolved-size symbols */
68 char *name; /* for unresolved-size symbols */
69 long symnum; /* index into symbol table */
70};
71
72/*
73 * Section IDs - used in Reloc.symbol when negative, and in
74 * Symbol.type when positive.
75 */
76#define SECT_ABS 2 /* absolute value */
77#define SECT_TEXT 4 /* text section */
78#define SECT_DATA 6 /* data section */
79#define SECT_BSS 8 /* bss section */
80#define SECT_MASK 0xE /* mask out any of the above */
81
82/*
83 * More flags used in Symbol.type.
84 */
85#define SYM_GLOBAL 1 /* it's a global symbol */
86#define SYM_DATA 0x100 /* used for shared libs */
87#define SYM_FUNCTION 0x200 /* used for shared libs */
88#define SYM_WITH_SIZE 0x4000 /* not output; internal only */
89
90/*
91 * Bit more explanation of symbol types: SECT_xxx denotes a local
92 * symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
93 * this module. Just SYM_GLOBAL, with zero value, denotes an
94 * external symbol referenced in this module. And just SYM_GLOBAL,
95 * but with a non-zero value, declares a C `common' variable, of
96 * size `value'.
97 */
98
99struct Section {
100 struct SAA *data;
101 unsigned long len, size, nrelocs;
102 long index;
103 struct Reloc *head, **tail;
104 struct Symbol *gsyms, *asym;
105};
106
107static struct Section stext, sdata, sbss;
108
109static struct SAA *syms;
110static unsigned long nsyms;
111
112static struct RAA *bsym;
113
114static struct SAA *strs;
115static unsigned long strslen;
116
117static struct Symbol *fwds;
118
119static FILE *aoutfp;
120static efunc error;
121static evalfunc evaluate;
122
123static int bsd;
124static int is_pic;
125
126static void aout_write(void);
127static void aout_write_relocs(struct Reloc *);
128static void aout_write_syms(void);
129static void aout_sect_write(struct Section *, unsigned char *, unsigned long);
130static void aout_pad_sections(void);
131static void aout_fixup_relocs(struct Section *);
132
133/*
134 * Special section numbers which are used to define special
135 * symbols, which can be used with WRT to provide PIC relocation
136 * types.
137 */
138static long aout_gotpc_sect, aout_gotoff_sect;
139static long aout_got_sect, aout_plt_sect;
140static long aout_sym_sect;
141
142static void aoutg_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval) {
143 aoutfp = fp;
144 error = errfunc;
145 evaluate = eval;
146 (void) ldef; /* placate optimisers */
147 stext.data = saa_init(1L); stext.head = NULL; stext.tail = &stext.head;
148 sdata.data = saa_init(1L); sdata.head = NULL; sdata.tail = &sdata.head;
149 stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
150 stext.nrelocs = sdata.nrelocs = 0;
151 stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
152 stext.index = seg_alloc();
153 sdata.index = seg_alloc();
154 sbss.index = seg_alloc();
155 stext.asym = sdata.asym = sbss.asym = NULL;
156 syms = saa_init((long)sizeof(struct Symbol));
157 nsyms = 0;
158 bsym = raa_init();
159 strs = saa_init(1L);
160 strslen = 0;
161 fwds = NULL;
162}
163
164#ifdef OF_AOUT
165
166static void aout_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval) {
167 bsd = FALSE;
168 aoutg_init (fp, errfunc, ldef, eval);
169
170 aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
171 aout_plt_sect = aout_sym_sect = NO_SEG;
172}
173
174#endif
175
176#ifdef OF_AOUTB
177
178extern struct ofmt of_aoutb;
179
180static void aoutb_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval) {
181 bsd = TRUE;
182 aoutg_init (fp, errfunc, ldef, eval);
183
184 is_pic = 0x00; /* may become 0x40 */
185
186 aout_gotpc_sect = seg_alloc();
187 ldef("..gotpc", aout_gotpc_sect+1, 0L, NULL, FALSE,FALSE,&of_aoutb,error);
188 aout_gotoff_sect = seg_alloc();
189 ldef("..gotoff", aout_gotoff_sect+1, 0L,NULL,FALSE,FALSE,&of_aoutb,error);
190 aout_got_sect = seg_alloc();
191 ldef("..got", aout_got_sect+1, 0L, NULL, FALSE,FALSE,&of_aoutb,error);
192 aout_plt_sect = seg_alloc();
193 ldef("..plt", aout_plt_sect+1, 0L, NULL, FALSE,FALSE,&of_aoutb,error);
194 aout_sym_sect = seg_alloc();
195 ldef("..sym", aout_sym_sect+1, 0L, NULL, FALSE,FALSE,&of_aoutb,error);
196}
197
198#endif
199
200static void aout_cleanup(void) {
201 struct Reloc *r;
202
203 aout_pad_sections();
204 aout_fixup_relocs(&stext);
205 aout_fixup_relocs(&sdata);
206 aout_write();
207 fclose (aoutfp);
208 saa_free (stext.data);
209 while (stext.head) {
210 r = stext.head;
211 stext.head = stext.head->next;
212 nasm_free (r);
213 }
214 saa_free (sdata.data);
215 while (sdata.head) {
216 r = sdata.head;
217 sdata.head = sdata.head->next;
218 nasm_free (r);
219 }
220 saa_free (syms);
221 raa_free (bsym);
222 saa_free (strs);
223}
224
225static long aout_section_names (char *name, int pass, int *bits) {
226 /*
227 * Default to 32 bits.
228 */
229 if (!name)
230 *bits = 32;
231
232 if (!name)
233 return stext.index;
234
235 if (!strcmp(name, ".text"))
236 return stext.index;
237 else if (!strcmp(name, ".data"))
238 return sdata.index;
239 else if (!strcmp(name, ".bss"))
240 return sbss.index;
241 else
242 return NO_SEG;
243}
244
245static void aout_deflabel (char *name, long segment, long offset,
246 int is_global, char *special) {
247 int pos = strslen+4;
248 struct Symbol *sym;
249 int special_used = FALSE;
250
251 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
252 /*
253 * This is a NASM special symbol. We never allow it into
254 * the a.out symbol table, even if it's a valid one. If it
255 * _isn't_ a valid one, we should barf immediately.
256 */
257 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
258 strcmp(name, "..got") && strcmp(name, "..plt") &&
259 strcmp(name, "..sym"))
260 error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
261 return;
262 }
263
264 if (is_global == 3) {
265 struct Symbol **s;
266 /*
267 * Fix up a forward-reference symbol size from the first
268 * pass.
269 */
270 for (s = &fwds; *s; s = &(*s)->nextfwd)
271 if (!strcmp((*s)->name, name)) {
272 struct tokenval tokval;
273 expr *e;
274 char *p = special;
275
276 while (*p && !isspace(*p)) p++;
277 while (*p && isspace(*p)) p++;
278 stdscan_reset();
279 stdscan_bufptr = p;
280 tokval.t_type = TOKEN_INVALID;
281 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
282 if (e) {
283 if (!is_simple(e))
284 error (ERR_NONFATAL, "cannot use relocatable"
285 " expression as symbol size");
286 else
287 (*s)->size = reloc_value(e);
288 }
289
290 /*
291 * Remove it from the list of unresolved sizes.
292 */
293 nasm_free ((*s)->name);
294 *s = (*s)->nextfwd;
295 return;
296 }
297 return; /* it wasn't an important one */
298 }
299
300 saa_wbytes (strs, name, (long)(1+strlen(name)));
301 strslen += 1+strlen(name);
302
303 sym = saa_wstruct (syms);
304
305 sym->strpos = pos;
306 sym->type = is_global ? SYM_GLOBAL : 0;
307 sym->segment = segment;
308 if (segment == NO_SEG)
309 sym->type |= SECT_ABS;
310 else if (segment == stext.index) {
311 sym->type |= SECT_TEXT;
312 if (is_global) {
313 sym->next = stext.gsyms;
314 stext.gsyms = sym;
315 } else if (!stext.asym)
316 stext.asym = sym;
317 } else if (segment == sdata.index) {
318 sym->type |= SECT_DATA;
319 if (is_global) {
320 sym->next = sdata.gsyms;
321 sdata.gsyms = sym;
322 } else if (!sdata.asym)
323 sdata.asym = sym;
324 } else if (segment == sbss.index) {
325 sym->type |= SECT_BSS;
326 if (is_global) {
327 sym->next = sbss.gsyms;
328 sbss.gsyms = sym;
329 } else if (!sbss.asym)
330 sbss.asym = sym;
331 } else
332 sym->type = SYM_GLOBAL;
333 if (is_global == 2)
334 sym->value = offset;
335 else
336 sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
337
338 if (is_global && sym->type != SYM_GLOBAL) {
339 /*
340 * Global symbol exported _from_ this module. We must check
341 * the special text for type information.
342 */
343
344 if (special) {
345 int n = strcspn(special, " ");
346
347 if (!nasm_strnicmp(special, "function", n))
348 sym->type |= SYM_FUNCTION;
349 else if (!nasm_strnicmp(special, "data", n) ||
350 !nasm_strnicmp(special, "object", n))
351 sym->type |= SYM_DATA;
352 else
353 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
354 n, special);
355 if (special[n]) {
356 struct tokenval tokval;
357 expr *e;
358 int fwd = FALSE;
359
360 if (!bsd) {
361 error(ERR_NONFATAL, "Linux a.out does not support"
362 " symbol size information");
363 } else {
364 while (special[n] && isspace(special[n]))
365 n++;
366 /*
367 * We have a size expression; attempt to
368 * evaluate it.
369 */
370 sym->type |= SYM_WITH_SIZE;
371 stdscan_reset();
372 stdscan_bufptr = special+n;
373 tokval.t_type = TOKEN_INVALID;
374 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error, NULL);
375 if (fwd) {
376 sym->nextfwd = fwds;
377 fwds = sym;
378 sym->name = nasm_strdup(name);
379 } else if (e) {
380 if (!is_simple(e))
381 error (ERR_NONFATAL, "cannot use relocatable"
382 " expression as symbol size");
383 else
384 sym->size = reloc_value(e);
385 }
386 }
387 }
388 special_used = TRUE;
389 }
390 }
391
392 /*
393 * define the references from external-symbol segment numbers
394 * to these symbol records.
395 */
396 if (segment != NO_SEG && segment != stext.index &&
397 segment != sdata.index && segment != sbss.index)
398 bsym = raa_write (bsym, segment, nsyms);
399 sym->symnum = nsyms;
400
401 nsyms++;
402 if (sym->type & SYM_WITH_SIZE)
403 nsyms++; /* and another for the size */
404
405 if (special && !special_used)
406 error(ERR_NONFATAL, "no special symbol features supported here");
407}
408
409static void aout_add_reloc (struct Section *sect, long segment,
410 int reltype, int bytes) {
411 struct Reloc *r;
412
413 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
414 sect->tail = &r->next;
415 r->next = NULL;
416
417 r->address = sect->len;
418 r->symbol = (segment == NO_SEG ? -SECT_ABS :
419 segment == stext.index ? -SECT_TEXT :
420 segment == sdata.index ? -SECT_DATA :
421 segment == sbss.index ? -SECT_BSS :
422 raa_read(bsym, segment));
423 r->reltype = reltype;
424 if (r->symbol >= 0)
425 r->reltype |= RELTYPE_SYMFLAG;
426 r->bytes = bytes;
427
428 sect->nrelocs++;
429}
430
431/*
432 * This routine deals with ..got and ..sym relocations: the more
433 * complicated kinds. In shared-library writing, some relocations
434 * with respect to global symbols must refer to the precise symbol
435 * rather than referring to an offset from the base of the section
436 * _containing_ the symbol. Such relocations call to this routine,
437 * which searches the symbol list for the symbol in question.
438 *
439 * RELTYPE_GOT references require the _exact_ symbol address to be
440 * used; RELTYPE_ABSOLUTE references can be at an offset from the
441 * symbol. The boolean argument `exact' tells us this.
442 *
443 * Return value is the adjusted value of `addr', having become an
444 * offset from the symbol rather than the section. Should always be
445 * zero when returning from an exact call.
446 *
447 * Limitation: if you define two symbols at the same place,
448 * confusion will occur.
449 *
450 * Inefficiency: we search, currently, using a linked list which
451 * isn't even necessarily sorted.
452 */
453static long aout_add_gsym_reloc (struct Section *sect,
454 long segment, long offset,
455 int type, int bytes, int exact) {
456 struct Symbol *sym, *sm, *shead;
457 struct Reloc *r;
458
459 /*
460 * First look up the segment to find whether it's text, data,
461 * bss or an external symbol.
462 */
463 shead = NULL;
464 if (segment == stext.index)
465 shead = stext.gsyms;
466 else if (segment == sdata.index)
467 shead = sdata.gsyms;
468 else if (segment == sbss.index)
469 shead = sbss.gsyms;
470 if (!shead) {
471 if (exact && offset != 0)
472 error (ERR_NONFATAL, "unable to find a suitable global symbol"
473 " for this reference");
474 else
475 aout_add_reloc (sect, segment, type, bytes);
476 return offset;
477 }
478
479 if (exact) {
480 /*
481 * Find a symbol pointing _exactly_ at this one.
482 */
483 for (sym = shead; sym; sym = sym->next)
484 if (sym->value == offset)
485 break;
486 } else {
487 /*
488 * Find the nearest symbol below this one.
489 */
490 sym = NULL;
491 for (sm = shead; sm; sm = sm->next)
492 if (sm->value <= offset && (!sym || sm->value > sym->value))
493 sym = sm;
494 }
495 if (!sym && exact) {
496 error (ERR_NONFATAL, "unable to find a suitable global symbol"
497 " for this reference");
498 return 0;
499 }
500
501 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
502 sect->tail = &r->next;
503 r->next = NULL;
504
505 r->address = sect->len;
506 r->symbol = sym->symnum;
507 r->reltype = type | RELTYPE_SYMFLAG;
508 r->bytes = bytes;
509
510 sect->nrelocs++;
511
512 return offset - sym->value;
513}
514
515/*
516 * This routine deals with ..gotoff relocations. These _must_ refer
517 * to a symbol, due to a perversity of *BSD's PIC implementation,
518 * and it must be a non-global one as well; so we store `asym', the
519 * first nonglobal symbol defined in each section, and always work
520 * from that. Relocation type is always RELTYPE_GOTOFF.
521 *
522 * Return value is the adjusted value of `addr', having become an
523 * offset from the `asym' symbol rather than the section.
524 */
525static long aout_add_gotoff_reloc (struct Section *sect, long segment,
526 long offset, int bytes) {
527 struct Reloc *r;
528 struct Symbol *asym;
529
530 /*
531 * First look up the segment to find whether it's text, data,
532 * bss or an external symbol.
533 */
534 asym = NULL;
535 if (segment == stext.index)
536 asym = stext.asym;
537 else if (segment == sdata.index)
538 asym = sdata.asym;
539 else if (segment == sbss.index)
540 asym = sbss.asym;
541 if (!asym)
542 error (ERR_NONFATAL, "`..gotoff' relocations require a non-global"
543 " symbol in the section");
544
545 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
546 sect->tail = &r->next;
547 r->next = NULL;
548
549 r->address = sect->len;
550 r->symbol = asym->symnum;
551 r->reltype = RELTYPE_GOTOFF;
552 r->bytes = bytes;
553
554 sect->nrelocs++;
555
556 return offset - asym->value;
557}
558
559static void aout_out (long segto, void *data, unsigned long type,
560 long segment, long wrt) {
561 struct Section *s;
562 long realbytes = type & OUT_SIZMASK;
563 long addr;
564 unsigned char mydata[4], *p;
565
566 type &= OUT_TYPMASK;
567
568 /*
569 * handle absolute-assembly (structure definitions)
570 */
571 if (segto == NO_SEG) {
572 if (type != OUT_RESERVE)
573 error (ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
574 " space");
575 return;
576 }
577
578 if (segto == stext.index)
579 s = &stext;
580 else if (segto == sdata.index)
581 s = &sdata;
582 else if (segto == sbss.index)
583 s = NULL;
584 else {
585 error(ERR_WARNING, "attempt to assemble code in"
586 " segment %d: defaulting to `.text'", segto);
587 s = &stext;
588 }
589
590 if (!s && type != OUT_RESERVE) {
591 error(ERR_WARNING, "attempt to initialise memory in the"
592 " BSS section: ignored");
593 if (type == OUT_REL2ADR)
594 realbytes = 2;
595 else if (type == OUT_REL4ADR)
596 realbytes = 4;
597 sbss.len += realbytes;
598 return;
599 }
600
601 if (type == OUT_RESERVE) {
602 if (s) {
603 error(ERR_WARNING, "uninitialised space declared in"
604 " %s section: zeroing",
605 (segto == stext.index ? "code" : "data"));
606 aout_sect_write (s, NULL, realbytes);
607 } else
608 sbss.len += realbytes;
609 } else if (type == OUT_RAWDATA) {
610 if (segment != NO_SEG)
611 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
612 aout_sect_write (s, data, realbytes);
613 } else if (type == OUT_ADDRESS) {
614 addr = *(long *)data;
615 if (segment != NO_SEG) {
616 if (segment % 2) {
617 error(ERR_NONFATAL, "a.out format does not support"
618 " segment base references");
619 } else {
620 if (wrt == NO_SEG) {
621 aout_add_reloc (s, segment, RELTYPE_ABSOLUTE, realbytes);
622 } else if (!bsd) {
623 error (ERR_NONFATAL, "Linux a.out format does not support"
624 " any use of WRT");
625 wrt = NO_SEG; /* we can at least _try_ to continue */
626 } else if (wrt == aout_gotpc_sect+1) {
627 is_pic = 0x40;
628 aout_add_reloc (s, segment, RELTYPE_GOTPC, realbytes);
629 } else if (wrt == aout_gotoff_sect+1) {
630 is_pic = 0x40;
631 addr = aout_add_gotoff_reloc (s, segment,
632 addr, realbytes);
633 } else if (wrt == aout_got_sect+1) {
634 is_pic = 0x40;
635 addr = aout_add_gsym_reloc (s, segment, addr, RELTYPE_GOT,
636 realbytes, TRUE);
637 } else if (wrt == aout_sym_sect+1) {
638 addr = aout_add_gsym_reloc (s, segment, addr,
639 RELTYPE_ABSOLUTE, realbytes,
640 FALSE);
641 } else if (wrt == aout_plt_sect+1) {
642 is_pic = 0x40;
643 error(ERR_NONFATAL, "a.out format cannot produce non-PC-"
644 "relative PLT references");
645 } else {
646 error (ERR_NONFATAL, "a.out format does not support this"
647 " use of WRT");
648 wrt = NO_SEG; /* we can at least _try_ to continue */
649 }
650 }
651 }
652 p = mydata;
653 if (realbytes == 2)
654 WRITESHORT (p, addr);
655 else
656 WRITELONG (p, addr);
657 aout_sect_write (s, mydata, realbytes);
658 } else if (type == OUT_REL2ADR) {
659 if (segment == segto)
660 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
661 if (segment != NO_SEG && segment % 2) {
662 error(ERR_NONFATAL, "a.out format does not support"
663 " segment base references");
664 } else {
665 if (wrt == NO_SEG) {
666 aout_add_reloc (s, segment, RELTYPE_RELATIVE, 2);
667 } else if (!bsd) {
668 error (ERR_NONFATAL, "Linux a.out format does not support"
669 " any use of WRT");
670 wrt = NO_SEG; /* we can at least _try_ to continue */
671 } else if (wrt == aout_plt_sect+1) {
672 is_pic = 0x40;
673 aout_add_reloc (s, segment, RELTYPE_PLT, 2);
674 } else if (wrt == aout_gotpc_sect+1 ||
675 wrt == aout_gotoff_sect+1 ||
676 wrt == aout_got_sect+1) {
677 error(ERR_NONFATAL, "a.out format cannot produce PC-"
678 "relative GOT references");
679 } else {
680 error (ERR_NONFATAL, "a.out format does not support this"
681 " use of WRT");
682 wrt = NO_SEG; /* we can at least _try_ to continue */
683 }
684 }
685 p = mydata;
686 WRITESHORT (p, *(long*)data-(realbytes + s->len));
687 aout_sect_write (s, mydata, 2L);
688 } else if (type == OUT_REL4ADR) {
689 if (segment == segto)
690 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
691 if (segment != NO_SEG && segment % 2) {
692 error(ERR_NONFATAL, "a.out format does not support"
693 " segment base references");
694 } else {
695 if (wrt == NO_SEG) {
696 aout_add_reloc (s, segment, RELTYPE_RELATIVE, 4);
697 } else if (!bsd) {
698 error (ERR_NONFATAL, "Linux a.out format does not support"
699 " any use of WRT");
700 wrt = NO_SEG; /* we can at least _try_ to continue */
701 } else if (wrt == aout_plt_sect+1) {
702 is_pic = 0x40;
703 aout_add_reloc (s, segment, RELTYPE_PLT, 4);
704 } else if (wrt == aout_gotpc_sect+1 ||
705 wrt == aout_gotoff_sect+1 ||
706 wrt == aout_got_sect+1) {
707 error(ERR_NONFATAL, "a.out format cannot produce PC-"
708 "relative GOT references");
709 } else {
710 error (ERR_NONFATAL, "a.out format does not support this"
711 " use of WRT");
712 wrt = NO_SEG; /* we can at least _try_ to continue */
713 }
714 }
715 p = mydata;
716 WRITELONG (p, *(long*)data-(realbytes + s->len));
717 aout_sect_write (s, mydata, 4L);
718 }
719}
720
721static void aout_pad_sections(void) {
722 static unsigned char pad[] = { 0x90, 0x90, 0x90, 0x90 };
723 /*
724 * Pad each of the text and data sections with NOPs until their
725 * length is a multiple of four. (NOP == 0x90.) Also increase
726 * the length of the BSS section similarly.
727 */
728 aout_sect_write (&stext, pad, (-(long)stext.len) & 3);
729 aout_sect_write (&sdata, pad, (-(long)sdata.len) & 3);
730 sbss.len = (sbss.len + 3) & ~3;
731}
732
733/*
734 * a.out files have the curious property that all references to
735 * things in the data or bss sections are done by addresses which
736 * are actually relative to the start of the _text_ section, in the
737 * _file_. (No relation to what happens after linking. No idea why
738 * this should be so. It's very strange.) So we have to go through
739 * the relocation table, _after_ the final size of each section is
740 * known, and fix up the relocations pointed to.
741 */
742static void aout_fixup_relocs(struct Section *sect) {
743 struct Reloc *r;
744
745 saa_rewind (sect->data);
746 for (r = sect->head; r; r = r->next) {
747 unsigned char *p, *q, blk[4];
748 long l;
749
750 saa_fread (sect->data, r->address, blk, (long)r->bytes);
751 p = q = blk;
752 l = *p++;
753 if (r->bytes > 1) {
754 l += ((long)*p++) << 8;
755 if (r->bytes == 4) {
756 l += ((long)*p++) << 16;
757 l += ((long)*p++) << 24;
758 }
759 }
760 if (r->symbol == -SECT_DATA)
761 l += stext.len;
762 else if (r->symbol == -SECT_BSS)
763 l += stext.len + sdata.len;
764 if (r->bytes == 4)
765 WRITELONG(q, l);
766 else if (r->bytes == 2)
767 WRITESHORT(q, l);
768 else
769 *q++ = l & 0xFF;
770 saa_fwrite (sect->data, r->address, blk, (long)r->bytes);
771 }
772}
773
774static void aout_write(void) {
775 /*
776 * Emit the a.out header.
777 */
778 /* OMAGIC, M_386 or MID_I386, no flags */
779 fwritelong (bsd ? 0x07018600 | is_pic : 0x640107L, aoutfp);
780 fwritelong (stext.len, aoutfp);
781 fwritelong (sdata.len, aoutfp);
782 fwritelong (sbss.len, aoutfp);
783 fwritelong (nsyms * 12, aoutfp); /* length of symbol table */
784 fwritelong (0L, aoutfp); /* object files have no entry point */
785 fwritelong (stext.nrelocs * 8, aoutfp); /* size of text relocs */
786 fwritelong (sdata.nrelocs * 8, aoutfp); /* size of data relocs */
787
788 /*
789 * Write out the code section and the data section.
790 */
791 saa_fpwrite (stext.data, aoutfp);
792 saa_fpwrite (sdata.data, aoutfp);
793
794 /*
795 * Write out the relocations.
796 */
797 aout_write_relocs (stext.head);
798 aout_write_relocs (sdata.head);
799
800 /*
801 * Write the symbol table.
802 */
803 aout_write_syms ();
804
805 /*
806 * And the string table.
807 */
808 fwritelong (strslen+4, aoutfp); /* length includes length count */
809 saa_fpwrite (strs, aoutfp);
810}
811
812static void aout_write_relocs (struct Reloc *r) {
813 while (r) {
814 unsigned long word2;
815
816 fwritelong (r->address, aoutfp);
817
818 if (r->symbol >= 0)
819 word2 = r->symbol;
820 else
821 word2 = -r->symbol;
822 word2 |= r->reltype << 24;
823 word2 |= (r->bytes == 1 ? 0 :
824 r->bytes == 2 ? 0x2000000L : 0x4000000L);
825 fwritelong (word2, aoutfp);
826
827 r = r->next;
828 }
829}
830
831static void aout_write_syms (void) {
832 int i;
833
834 saa_rewind (syms);
835 for (i=0; i<nsyms; i++) {
836 struct Symbol *sym = saa_rstruct(syms);
837 fwritelong (sym->strpos, aoutfp);
838 fwritelong ((long)sym->type & ~SYM_WITH_SIZE, aoutfp);
839 /*
840 * Fix up the symbol value now we know the final section
841 * sizes.
842 */
843 if ((sym->type & SECT_MASK) == SECT_DATA)
844 sym->value += stext.len;
845 if ((sym->type & SECT_MASK) == SECT_BSS)
846 sym->value += stext.len + sdata.len;
847 fwritelong (sym->value, aoutfp);
848 /*
849 * Output a size record if necessary.
850 */
851 if (sym->type & SYM_WITH_SIZE) {
852 fwritelong(sym->strpos, aoutfp);
853 fwritelong(0x0DL, aoutfp); /* special value: means size */
854 fwritelong(sym->size, aoutfp);
855 i++; /* use up another of `nsyms' */
856 }
857 }
858}
859
860static void aout_sect_write (struct Section *sect,
861 unsigned char *data, unsigned long len) {
862 saa_wbytes (sect->data, data, len);
863 sect->len += len;
864}
865
866static long aout_segbase (long segment) {
867 return segment;
868}
869
870static int aout_directive (char *directive, char *value, int pass) {
871 return 0;
872}
873
874static void aout_filename (char *inname, char *outname, efunc error) {
875 standard_extension (inname, outname, ".o", error);
876}
877
878static char *aout_stdmac[] = {
879 "%define __SECT__ [section .text]",
880 NULL
881};
882
883#endif /* OF_AOUT || OF_AOUTB */
884
885#ifdef OF_AOUT
886
887struct ofmt of_aout = {
888 "Linux a.out object files",
889 "aout",
890 aout_stdmac,
891 aout_init,
892 aout_out,
893 aout_deflabel,
894 aout_section_names,
895 aout_segbase,
896 aout_directive,
897 aout_filename,
898 aout_cleanup
899};
900
901#endif
902
903#ifdef OF_AOUTB
904
905struct ofmt of_aoutb = {
906 "NetBSD/FreeBSD a.out object files",
907 "aoutb",
908 aout_stdmac,
909 aoutb_init,
910 aout_out,
911 aout_deflabel,
912 aout_section_names,
913 aout_segbase,
914 aout_directive,
915 aout_filename,
916 aout_cleanup
917};
918
919#endif