Debian Codegraph @

output/outas86.c

raw
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /* Copyright 1996-2025 The NASM Authors - All Rights Reserved */
3
4 /*
5 * outas86.c output routines for the Netwide Assembler to produce
6 * Linux as86 (bin86-0.3) 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 "outform.h"
19 #include "outlib.h"
20
21 #ifdef OF_AS86
22
23 struct Piece {
24 struct Piece *next;
25 int type; /* 0 = absolute, 1 = seg, 2 = sym */
26 int32_t offset; /* relative offset */
27 int number; /* symbol/segment number (4=bss) */
28 int32_t bytes; /* size of reloc or of absolute data */
29 bool relative; /* relative address? */
30 };
31
32 struct Symbol {
33 int32_t strpos; /* string table position of name */
34 int flags; /* symbol flags */
35 int segment; /* 4=bss at this point */
36 int32_t value; /* address, or COMMON variable size */
37 };
38
39 /*
40 * Section IDs - used in Piece.number and Symbol.segment.
41 */
42 #define SECT_TEXT 0 /* text section */
43 #define SECT_DATA 3 /* data section */
44 #define SECT_BSS 4 /* bss section */
45
46 /*
47 * Flags used in Symbol.flags.
48 */
49 #define SYM_ENTRY (1<<8)
50 #define SYM_EXPORT (1<<7)
51 #define SYM_IMPORT (1<<6)
52 #define SYM_ABSOLUTE (1<<4)
53
54 struct Section {
55 struct SAA *data;
56 uint32_t datalen, size, len;
57 int32_t index;
58 struct Piece *head, *last, **tail;
59 };
60
61 static struct Section stext, sdata;
62 static uint32_t bsslen;
63 static int32_t bssindex;
64
65 static struct SAA *syms;
66 static uint32_t nsyms;
67
68 static struct RAA *bsym;
69
70 static struct SAA *strs;
71 static size_t strslen;
72
73 static int as86_reloc_size;
74
75 static void as86_write(void);
76 static void as86_write_section(struct Section *, int);
77 static size_t as86_add_string(const char *name);
78 static void as86_sect_write(struct Section *, const uint8_t *,
79 uint32_t);
80
81 static void as86_init(void)
82 {
83 stext.data = saa_init(1L);
84 stext.datalen = 0L;
85 stext.head = stext.last = NULL;
86 stext.tail = &stext.head;
87 sdata.data = saa_init(1L);
88 sdata.datalen = 0L;
89 sdata.head = sdata.last = NULL;
90 sdata.tail = &sdata.head;
91 bsslen =
92 stext.len = stext.datalen = stext.size =
93 sdata.len = sdata.datalen = sdata.size = 0;
94 stext.index = seg_alloc();
95 sdata.index = seg_alloc();
96 bssindex = seg_alloc();
97 syms = saa_init((int32_t)sizeof(struct Symbol));
98 nsyms = 0;
99 bsym = raa_init();
100 strs = saa_init(1L);
101 strslen = 0;
102
103 /* as86 module name = input file minus extension */
104 as86_add_string(filename_set_extension(inname, ""));
105 }
106
107 static void as86_cleanup(void)
108 {
109 struct Piece *p;
110
111 as86_write();
112 saa_free(stext.data);
113 while (stext.head) {
114 p = stext.head;
115 stext.head = stext.head->next;
116 nasm_free(p);
117 }
118 saa_free(sdata.data);
119 while (sdata.head) {
120 p = sdata.head;
121 sdata.head = sdata.head->next;
122 nasm_free(p);
123 }
124 saa_free(syms);
125 raa_free(bsym);
126 saa_free(strs);
127 }
128
129 static int32_t as86_section_names(char *name, int *bits)
130 {
131 /*
132 * Default is 16 bits.
133 */
134 if (!name) {
135 *bits = 16;
136 return stext.index;
137 }
138
139 if (!strcmp(name, ".text"))
140 return stext.index;
141 else if (!strcmp(name, ".data"))
142 return sdata.index;
143 else if (!strcmp(name, ".bss"))
144 return bssindex;
145 else
146 return NO_SEG;
147 }
148
149 static size_t as86_add_string(const char *name)
150 {
151 size_t pos = strslen;
152 size_t length = strlen(name);
153
154 saa_wbytes(strs, name, length + 1);
155 strslen += 1 + length;
156
157 return pos;
158 }
159
160 static void as86_deflabel(char *name, int32_t segment, int64_t offset,
161 int is_global, char *special)
162 {
163 bool is_start = false;
164 struct Symbol *sym;
165
166 if (special)
167 nasm_nonfatal("as86 format does not support any"
168 " special symbol types");
169
170
171 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
172 if (strcmp(name, "..start")) {
173 nasm_nonfatal("unrecognised special symbol `%s'", name);
174 return;
175 } else {
176 is_start = true;
177 }
178 }
179
180 sym = saa_wstruct(syms);
181
182 sym->strpos = as86_add_string(name);
183 sym->flags = 0;
184
185 if (is_start)
186 sym->flags = SYM_ENTRY;
187
188 if (segment == NO_SEG)
189 sym->flags |= SYM_ABSOLUTE, sym->segment = 0;
190 else if (segment == stext.index)
191 sym->segment = SECT_TEXT;
192 else if (segment == sdata.index)
193 sym->segment = SECT_DATA;
194 else if (segment == bssindex)
195 sym->segment = SECT_BSS;
196 else {
197 sym->flags |= SYM_IMPORT;
198 sym->segment = 15;
199 }
200
201 if (is_global == 2)
202 sym->segment = 3; /* already have IMPORT */
203
204 if (is_global && !(sym->flags & SYM_IMPORT))
205 sym->flags |= SYM_EXPORT;
206
207 sym->value = offset;
208
209 /*
210 * define the references from external-symbol segment numbers
211 * to these symbol records.
212 */
213 if (segment != NO_SEG && segment != stext.index &&
214 segment != sdata.index && segment != bssindex)
215 bsym = raa_write(bsym, segment, nsyms);
216
217 nsyms++;
218 }
219
220 static void as86_add_piece(struct Section *sect, int type, int32_t offset,
221 int32_t segment, int32_t bytes, int relative)
222 {
223 struct Piece *p;
224
225 sect->len += bytes;
226
227 if (type == 0 && sect->last && sect->last->type == 0) {
228 sect->last->bytes += bytes;
229 return;
230 }
231
232 p = sect->last = *sect->tail = nasm_malloc(sizeof(struct Piece));
233 sect->tail = &p->next;
234 p->next = NULL;
235
236 p->type = type;
237 p->offset = offset;
238 p->bytes = bytes;
239 p->relative = relative;
240
241 if (type == 1 && segment == stext.index)
242 p->number = SECT_TEXT;
243 else if (type == 1 && segment == sdata.index)
244 p->number = SECT_DATA;
245 else if (type == 1 && segment == bssindex)
246 p->number = SECT_BSS;
247 else if (type == 1)
248 p->number = raa_read(bsym, segment), p->type = 2;
249 }
250
251 static void as86_out(const struct out_data *out)
252 {
253 OUT_LEGACY(out,segto,data,type,size,segment,wrt);
254 struct Section *s;
255 int32_t offset;
256 uint8_t mydata[4], *p;
257
258 if (wrt != NO_SEG) {
259 wrt = NO_SEG; /* continue to do _something_ */
260 nasm_nonfatal("WRT not supported by as86 output format");
261 }
262
263 if (segto == stext.index)
264 s = &stext;
265 else if (segto == sdata.index)
266 s = &sdata;
267 else if (segto == bssindex)
268 s = NULL;
269 else {
270 nasm_warn(WARN_OTHER, "attempt to assemble code in"
271 " unknown section: defaulting to `.text'");
272 s = &stext;
273 }
274
275 if (!s && type != OUT_RESERVE) {
276 nasm_warn(WARN_OTHER, "attempt to initialize memory in the"
277 " BSS section: ignored");
278 bsslen += realsize(type, size);
279 return;
280 }
281
282 memset(mydata, 0, sizeof(mydata));
283
284 if (type == OUT_RESERVE) {
285 if (s) {
286 nasm_warn(WARN_ZEROING, "uninitialized space declared in"
287 " %s section: zeroing",
288 (segto == stext.index ? "code" : "data"));
289 as86_sect_write(s, NULL, size);
290 as86_add_piece(s, 0, 0L, 0L, size, 0);
291 } else
292 bsslen += size;
293 } else if (type == OUT_RAWDATA) {
294 as86_sect_write(s, data, size);
295 as86_add_piece(s, 0, 0L, 0L, size, 0);
296 } else if (type == OUT_ADDRESS) {
297 int asize = abs((int)size);
298 if (segment != NO_SEG) {
299 if (segment % 2) {
300 nasm_nonfatal("as86 format does not support"
301 " segment base references");
302 } else {
303 offset = *(int64_t *)data;
304 as86_add_piece(s, 1, offset, segment, asize, 0);
305 }
306 } else {
307 p = mydata;
308 WRITELONG(p, *(int64_t *)data);
309 as86_sect_write(s, data, asize);
310 as86_add_piece(s, 0, 0L, 0L, asize, 0);
311 }
312 } else if (type == OUT_REL2ADR) {
313 if (segment != NO_SEG) {
314 if (segment % 2) {
315 nasm_nonfatal("as86 format does not support"
316 " segment base references");
317 } else {
318 offset = *(int64_t *)data;
319 as86_add_piece(s, 1, offset - size + 2, segment, 2L,
320 1);
321 }
322 }
323 } else if (type == OUT_REL4ADR) {
324 if (segment != NO_SEG) {
325 if (segment % 2) {
326 nasm_nonfatal("as86 format does not support"
327 " segment base references");
328 } else {
329 offset = *(int64_t *)data;
330 as86_add_piece(s, 1, offset - size + 4, segment, 4L,
331 1);
332 }
333 }
334 }
335 }
336
337 static void as86_write(void)
338 {
339 uint32_t i;
340 int32_t symlen, seglen, segsize;
341
342 /*
343 * First, go through the symbol records working out how big
344 * each will be. Also fix up BSS references at this time, and
345 * set the flags words up completely.
346 */
347 symlen = 0;
348 saa_rewind(syms);
349 for (i = 0; i < nsyms; i++) {
350 struct Symbol *sym = saa_rstruct(syms);
351 if (sym->segment == SECT_BSS)
352 sym->segment = SECT_DATA, sym->value += sdata.len;
353 sym->flags |= sym->segment;
354 if (sym->value == 0)
355 sym->flags |= 0 << 14, symlen += 4;
356 else if (sym->value >= 0 && sym->value <= 255)
357 sym->flags |= 1 << 14, symlen += 5;
358 else if (sym->value >= 0 && sym->value <= 65535L)
359 sym->flags |= 2 << 14, symlen += 6;
360 else
361 sym->flags |= 3 << 14, symlen += 8;
362 }
363
364 /*
365 * Now do the same for the segments, and get the segment size
366 * descriptor word at the same time.
367 */
368 seglen = segsize = 0;
369 if ((uint32_t)stext.len > 65535L)
370 segsize |= 0x03000000L, seglen += 4;
371 else
372 segsize |= 0x02000000L, seglen += 2;
373 if ((uint32_t)sdata.len > 65535L)
374 segsize |= 0xC0000000L, seglen += 4;
375 else
376 segsize |= 0x80000000L, seglen += 2;
377
378 /*
379 * Emit the as86 header.
380 */
381 fwriteint32_t(0x000186A3L, ofile);
382 fputc(0x2A, ofile);
383 fwriteint32_t(27 + symlen + seglen + strslen, ofile); /* header length */
384 fwriteint32_t(stext.len + sdata.len + bsslen, ofile);
385 fwriteint16_t(strslen, ofile);
386 fwriteint16_t(0, ofile); /* class = revision = 0 */
387 fwriteint32_t(0x55555555L, ofile); /* segment max sizes: always this */
388 fwriteint32_t(segsize, ofile); /* segment size descriptors */
389 if (segsize & 0x01000000L)
390 fwriteint32_t(stext.len, ofile);
391 else
392 fwriteint16_t(stext.len, ofile);
393 if (segsize & 0x40000000L)
394 fwriteint32_t(sdata.len + bsslen, ofile);
395 else
396 fwriteint16_t(sdata.len + bsslen, ofile);
397 fwriteint16_t(nsyms, ofile);
398
399 /*
400 * Write the symbol table.
401 */
402 saa_rewind(syms);
403 for (i = 0; i < nsyms; i++) {
404 struct Symbol *sym = saa_rstruct(syms);
405 fwriteint16_t(sym->strpos, ofile);
406 fwriteint16_t(sym->flags, ofile);
407 switch (sym->flags & (3 << 14)) {
408 case 0 << 14:
409 break;
410 case 1 << 14:
411 fputc(sym->value, ofile);
412 break;
413 case 2 << 14:
414 fwriteint16_t(sym->value, ofile);
415 break;
416 case 3 << 14:
417 fwriteint32_t(sym->value, ofile);
418 break;
419 }
420 }
421
422 /*
423 * Write out the string table.
424 */
425 saa_fpwrite(strs, ofile);
426
427 /*
428 * Write the program text.
429 */
430 as86_reloc_size = -1;
431 as86_write_section(&stext, SECT_TEXT);
432 as86_write_section(&sdata, SECT_DATA);
433 /*
434 * Append the BSS section to the .data section
435 */
436 if (bsslen > 65535L) {
437 fputc(0x13, ofile);
438 fwriteint32_t(bsslen, ofile);
439 } else if (bsslen > 255) {
440 fputc(0x12, ofile);
441 fwriteint16_t(bsslen, ofile);
442 } else if (bsslen) {
443 fputc(0x11, ofile);
444 fputc(bsslen, ofile);
445 }
446
447 fputc(0, ofile); /* termination */
448 }
449
450 static void as86_set_rsize(int size)
451 {
452 if (as86_reloc_size != size) {
453 switch (as86_reloc_size = size) {
454 case 1:
455 fputc(0x01, ofile);
456 break;
457 case 2:
458 fputc(0x02, ofile);
459 break;
460 case 4:
461 fputc(0x03, ofile);
462 break;
463 default:
464 nasm_panic("bizarre relocation size %d", size);
465 break;
466 }
467 }
468 }
469
470 static void as86_write_section(struct Section *sect, int index)
471 {
472 struct Piece *p;
473 uint32_t s;
474 int32_t length;
475
476 fputc(0x20 + index, ofile); /* select the right section */
477
478 saa_rewind(sect->data);
479
480 for (p = sect->head; p; p = p->next)
481 switch (p->type) {
482 case 0:
483 /*
484 * Absolute data. Emit it in chunks of at most 64
485 * bytes.
486 */
487 length = p->bytes;
488 do {
489 char buf[64];
490 int32_t tmplen = (length > 64 ? 64 : length);
491 fputc(0x40 | (tmplen & 0x3F), ofile);
492 saa_rnbytes(sect->data, buf, tmplen);
493 nasm_write(buf, tmplen, ofile);
494 length -= tmplen;
495 } while (length > 0);
496 break;
497 case 1:
498 /*
499 * A segment-type relocation. First fix up the BSS.
500 */
501 if (p->number == SECT_BSS)
502 p->number = SECT_DATA, p->offset += sdata.len;
503 as86_set_rsize(p->bytes);
504 fputc(0x80 | (p->relative ? 0x20 : 0) | p->number, ofile);
505 if (as86_reloc_size == 2)
506 fwriteint16_t(p->offset, ofile);
507 else
508 fwriteint32_t(p->offset, ofile);
509 break;
510 case 2:
511 /*
512 * A symbol-type relocation.
513 */
514 as86_set_rsize(p->bytes);
515 s = p->offset;
516 if (s > 65535L)
517 s = 3;
518 else if (s > 255)
519 s = 2;
520 else if (s > 0)
521 s = 1;
522 else
523 s = 0;
524 fputc(0xC0 |
525 (p->relative ? 0x20 : 0) |
526 (p->number > 255 ? 0x04 : 0) | s, ofile);
527 if (p->number > 255)
528 fwriteint16_t(p->number, ofile);
529 else
530 fputc(p->number, ofile);
531 switch (s) {
532 case 0:
533 break;
534 case 1:
535 fputc(p->offset, ofile);
536 break;
537 case 2:
538 fwriteint16_t(p->offset, ofile);
539 break;
540 case 3:
541 fwriteint32_t(p->offset, ofile);
542 break;
543 }
544 break;
545 }
546 }
547
548 static void as86_sect_write(struct Section *sect,
549 const uint8_t *data, uint32_t len)
550 {
551 saa_wbytes(sect->data, data, len);
552 sect->datalen += len;
553 }
554
555 extern macros_t as86_stdmac[];
556
557 const struct ofmt of_as86 = {
558 "as86 (bin86/dev86 toolchain)",
559 "as86",
560 ".o",
561 0,
562 32,
563 null_debug_arr,
564 &null_debug_form,
565 as86_stdmac,
566 as86_init,
567 null_reset,
568 as86_out,
569 as86_deflabel,
570 as86_section_names,
571 NULL,
572 null_sectalign,
573 null_segbase,
574 null_directive,
575 as86_cleanup,
576 NULL /* pragma list */
577 };
578
579 #endif /* OF_AS86 */