Debian Codegraph @

output/outaout.c

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