doc/Makefile.in: drop nasmdoc.ps as a usable documentation file
[nasm.git] / output / outieee.c
blob80c078523fab418c95b3b9a4ed6fb6f10ab1d296
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outieee.c output routines for the Netwide Assembler to produce
36 * IEEE-std object files
39 /* notes: I have tried to make this correspond to the IEEE version
40 * of the standard, specifically the primary ASCII version. It should
41 * be trivial to create the binary version given this source (which is
42 * one of MANY things that have to be done to make this correspond to
43 * the hp-microtek version of the standard).
45 * 16-bit support is assumed to use 24-bit addresses
46 * The linker can sort out segmentation-specific stuff
47 * if it keeps track of externals
48 * in terms of being relative to section bases
50 * A non-standard variable type, the 'Yn' variable, has been introduced.
51 * Basically it is a reference to extern 'n'- denoting the low limit
52 * (L-variable) of the section that extern 'n' is defined in. Like the
53 * x variable, there may be no explicit assignment to it, it is derived
54 * from the public definition corresponding to the extern name. This
55 * is required because the one thing the mufom guys forgot to do well was
56 * take into account segmented architectures.
58 * I use comment classes for various things and these are undefined by
59 * the standard.
61 * Debug info should be considered totally non-standard (local labels are
62 * standard but linenum records are not covered by the standard.
63 * Type defs have the standard format but absolute meanings for ordinal
64 * types are not covered by the standard.)
66 * David Lindauer, LADsoft
68 #include "compiler.h"
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <time.h>
74 #include <stdarg.h> /* Note: we need the ANSI version of stdarg.h */
75 #include <ctype.h>
77 #include "nasm.h"
78 #include "nasmlib.h"
79 #include "error.h"
80 #include "ver.h"
82 #include "outform.h"
83 #include "outlib.h"
85 #ifdef OF_IEEE
87 #define ARRAY_BOT 0x1
89 static char ieee_infile[FILENAME_MAX];
90 static int ieee_uppercase;
92 static bool any_segs;
93 static int arrindex;
95 #define HUNKSIZE 1024 /* Size of the data hunk */
96 #define EXT_BLKSIZ 512
97 #define LDPERLINE 32 /* bytes per line in output */
99 struct ieeeSection;
101 struct LineNumber {
102 struct LineNumber *next;
103 struct ieeeSection *segment;
104 int32_t offset;
105 int32_t lineno;
108 static struct FileName {
109 struct FileName *next;
110 char *name;
111 int32_t index;
112 } *fnhead, **fntail;
114 static struct Array {
115 struct Array *next;
116 unsigned size;
117 int basetype;
118 } *arrhead, **arrtail;
120 static struct ieeePublic {
121 struct ieeePublic *next;
122 char *name;
123 int32_t offset;
124 int32_t segment; /* only if it's far-absolute */
125 int32_t index;
126 int type; /* for debug purposes */
127 } *fpubhead, **fpubtail, *last_defined;
129 static struct ieeeExternal {
130 struct ieeeExternal *next;
131 char *name;
132 int32_t commonsize;
133 } *exthead, **exttail;
135 static int externals;
137 static struct ExtBack {
138 struct ExtBack *next;
139 int index[EXT_BLKSIZ];
140 } *ebhead, **ebtail;
142 /* NOTE: the first segment MUST be the lineno segment */
143 static struct ieeeSection {
144 struct ieeeSection *next;
145 char *name;
146 struct ieeeObjData *data, *datacurr;
147 struct ieeeFixupp *fptr, *flptr;
148 int32_t index; /* the NASM segment id */
149 int32_t ieee_index; /* the OBJ-file segment index */
150 int32_t currentpos;
151 int32_t align; /* can be SEG_ABS + absolute addr */
152 int32_t startpos;
153 int32_t use32; /* is this segment 32-bit? */
154 struct ieeePublic *pubhead, **pubtail, *lochead, **loctail;
155 enum {
156 CMB_PRIVATE = 0,
157 CMB_PUBLIC = 2,
158 CMB_COMMON = 6
159 } combine;
160 } *seghead, **segtail, *ieee_seg_needs_update;
162 struct ieeeObjData {
163 struct ieeeObjData *next;
164 uint8_t data[HUNKSIZE];
167 struct ieeeFixupp {
168 struct ieeeFixupp *next;
169 enum {
170 FT_SEG = 0,
171 FT_REL = 1,
172 FT_OFS = 2,
173 FT_EXT = 3,
174 FT_WRT = 4,
175 FT_EXTREL = 5,
176 FT_EXTWRT = 6,
177 FT_EXTSEG = 7
178 } ftype;
179 int16_t size;
180 int32_t id1;
181 int32_t id2;
182 int32_t offset;
183 int32_t addend;
186 static int32_t ieee_entry_seg, ieee_entry_ofs;
187 static int checksum;
189 extern const struct ofmt of_ieee;
190 static const struct dfmt ladsoft_debug_form;
192 static void ieee_data_new(struct ieeeSection *);
193 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection *,
194 int, uint64_t, int32_t);
195 static void ieee_install_fixup(struct ieeeSection *, struct ieeeFixupp *);
196 static int32_t ieee_segment(char *, int, int *);
197 static void ieee_write_file(void);
198 static void ieee_write_byte(struct ieeeSection *, int);
199 static void ieee_write_word(struct ieeeSection *, int);
200 static void ieee_write_dword(struct ieeeSection *, int32_t);
201 static void ieee_putascii(char *, ...);
202 static void ieee_putcs(int);
203 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
204 static int32_t ieee_putlr(struct ieeeFixupp *);
205 static void ieee_unqualified_name(char *, char *);
208 * pup init
210 static void ieee_init(void)
212 any_segs = false;
213 fpubhead = NULL;
214 fpubtail = &fpubhead;
215 exthead = NULL;
216 exttail = &exthead;
217 externals = 1;
218 ebhead = NULL;
219 ebtail = &ebhead;
220 seghead = ieee_seg_needs_update = NULL;
221 segtail = &seghead;
222 ieee_entry_seg = NO_SEG;
223 ieee_uppercase = false;
224 checksum = 0;
228 * Rundown
230 static void ieee_cleanup(void)
232 ieee_write_file();
233 dfmt->cleanup();
234 while (seghead) {
235 struct ieeeSection *segtmp = seghead;
236 seghead = seghead->next;
237 while (segtmp->pubhead) {
238 struct ieeePublic *pubtmp = segtmp->pubhead;
239 segtmp->pubhead = pubtmp->next;
240 nasm_free(pubtmp);
242 while (segtmp->fptr) {
243 struct ieeeFixupp *fixtmp = segtmp->fptr;
244 segtmp->fptr = fixtmp->next;
245 nasm_free(fixtmp);
247 while (segtmp->data) {
248 struct ieeeObjData *dattmp = segtmp->data;
249 segtmp->data = dattmp->next;
250 nasm_free(dattmp);
252 nasm_free(segtmp);
254 while (fpubhead) {
255 struct ieeePublic *pubtmp = fpubhead;
256 fpubhead = fpubhead->next;
257 nasm_free(pubtmp);
259 while (exthead) {
260 struct ieeeExternal *exttmp = exthead;
261 exthead = exthead->next;
262 nasm_free(exttmp);
264 while (ebhead) {
265 struct ExtBack *ebtmp = ebhead;
266 ebhead = ebhead->next;
267 nasm_free(ebtmp);
272 * callback for labels
274 static void ieee_deflabel(char *name, int32_t segment,
275 int64_t offset, int is_global, char *special)
278 * We have three cases:
280 * (i) `segment' is a segment-base. If so, set the name field
281 * for the segment structure it refers to, and then
282 * return.
284 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
285 * Save the label position for later output of a PUBDEF record.
288 * (iii) `segment' is not one of our segments. Save the label
289 * position for later output of an EXTDEF.
291 struct ieeeExternal *ext;
292 struct ExtBack *eb;
293 struct ieeeSection *seg;
294 int i;
296 if (special) {
297 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
300 * First check for the double-period, signifying something
301 * unusual.
303 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
304 if (!strcmp(name, "..start")) {
305 ieee_entry_seg = segment;
306 ieee_entry_ofs = offset;
308 return;
312 * Case (i):
314 if (ieee_seg_needs_update) {
315 ieee_seg_needs_update->name = name;
316 return;
318 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
319 return;
322 * case (ii)
324 if (segment >= SEG_ABS) {
326 * SEG_ABS subcase of (ii).
328 if (is_global) {
329 struct ieeePublic *pub;
331 pub = *fpubtail = nasm_malloc(sizeof(*pub));
332 fpubtail = &pub->next;
333 pub->next = NULL;
334 pub->name = name;
335 pub->offset = offset;
336 pub->segment = segment & ~SEG_ABS;
338 return;
341 for (seg = seghead; seg && is_global; seg = seg->next)
342 if (seg->index == segment) {
343 struct ieeePublic *pub;
345 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
346 seg->pubtail = &pub->next;
347 pub->next = NULL;
348 pub->name = name;
349 pub->offset = offset;
350 pub->index = seg->ieee_index;
351 pub->segment = -1;
352 return;
356 * Case (iii).
358 if (is_global) {
359 ext = *exttail = nasm_malloc(sizeof(*ext));
360 ext->next = NULL;
361 exttail = &ext->next;
362 ext->name = name;
363 if (is_global == 2)
364 ext->commonsize = offset;
365 else
366 ext->commonsize = 0;
367 i = segment / 2;
368 eb = ebhead;
369 if (!eb) {
370 eb = *ebtail = nasm_zalloc(sizeof(*eb));
371 eb->next = NULL;
372 ebtail = &eb->next;
374 while (i > EXT_BLKSIZ) {
375 if (eb && eb->next)
376 eb = eb->next;
377 else {
378 eb = *ebtail = nasm_zalloc(sizeof(*eb));
379 eb->next = NULL;
380 ebtail = &eb->next;
382 i -= EXT_BLKSIZ;
384 eb->index[i] = externals++;
390 * Put data out
392 static void ieee_out(int32_t segto, const void *data,
393 enum out_type type, uint64_t size,
394 int32_t segment, int32_t wrt)
396 const uint8_t *ucdata;
397 int32_t ldata;
398 struct ieeeSection *seg;
401 * handle absolute-assembly (structure definitions)
403 if (segto == NO_SEG) {
404 if (type != OUT_RESERVE)
405 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
406 " space");
407 return;
411 * If `any_segs' is still false, we must define a default
412 * segment.
414 if (!any_segs) {
415 int tempint; /* ignored */
416 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
417 nasm_panic(0, "strange segment conditions in IEEE driver");
421 * Find the segment we are targetting.
423 for (seg = seghead; seg; seg = seg->next)
424 if (seg->index == segto)
425 break;
426 if (!seg)
427 nasm_panic(0, "code directed to nonexistent segment?");
429 if (type == OUT_RAWDATA) {
430 ucdata = data;
431 while (size--)
432 ieee_write_byte(seg, *ucdata++);
433 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
434 type == OUT_REL4ADR) {
435 if (type == OUT_ADDRESS)
436 size = abs((int)size);
437 else if (segment == NO_SEG)
438 nasm_error(ERR_NONFATAL, "relative call to absolute address not"
439 " supported by IEEE format");
440 ldata = *(int64_t *)data;
441 if (type == OUT_REL2ADR)
442 ldata += (size - 2);
443 if (type == OUT_REL4ADR)
444 ldata += (size - 4);
445 ieee_write_fixup(segment, wrt, seg, size, type, ldata);
446 if (size == 2)
447 ieee_write_word(seg, ldata);
448 else
449 ieee_write_dword(seg, ldata);
450 } else if (type == OUT_RESERVE) {
451 while (size--)
452 ieee_write_byte(seg, 0);
456 static void ieee_data_new(struct ieeeSection *segto)
459 if (!segto->data)
460 segto->data = segto->datacurr =
461 nasm_malloc(sizeof(*(segto->datacurr)));
462 else
463 segto->datacurr = segto->datacurr->next =
464 nasm_malloc(sizeof(*(segto->datacurr)));
465 segto->datacurr->next = NULL;
469 * this routine is unalduterated bloatware. I usually don't do this
470 * but I might as well see what it is like on a harmless program.
471 * If anyone wants to optimize this is a good canditate!
473 static void ieee_write_fixup(int32_t segment, int32_t wrt,
474 struct ieeeSection *segto, int size,
475 uint64_t realtype, int32_t offset)
477 struct ieeeSection *target;
478 struct ieeeFixupp s;
480 /* Don't put a fixup for things NASM can calculate */
481 if (wrt == NO_SEG && segment == NO_SEG)
482 return;
484 s.ftype = -1;
485 /* if it is a WRT offset */
486 if (wrt != NO_SEG) {
487 s.ftype = FT_WRT;
488 s.addend = offset;
489 if (wrt >= SEG_ABS)
490 s.id1 = -(wrt - SEG_ABS);
491 else {
492 if (wrt % 2 && realtype != OUT_REL2ADR
493 && realtype != OUT_REL4ADR) {
494 wrt--;
496 for (target = seghead; target; target = target->next)
497 if (target->index == wrt)
498 break;
499 if (target) {
500 s.id1 = target->ieee_index;
501 for (target = seghead; target; target = target->next)
502 if (target->index == segment)
503 break;
505 if (target)
506 s.id2 = target->ieee_index;
507 else {
509 * Now we assume the segment field is being used
510 * to hold an extern index
512 int32_t i = segment / 2;
513 struct ExtBack *eb = ebhead;
514 while (i > EXT_BLKSIZ) {
515 if (eb)
516 eb = eb->next;
517 else
518 break;
519 i -= EXT_BLKSIZ;
521 /* if we have an extern decide the type and make a record
523 if (eb) {
524 s.ftype = FT_EXTWRT;
525 s.addend = 0;
526 s.id2 = eb->index[i];
527 } else
528 nasm_error(ERR_NONFATAL,
529 "Source of WRT must be an offset");
532 } else
533 nasm_panic(0,
534 "unrecognised WRT value in ieee_write_fixup");
535 } else
536 nasm_error(ERR_NONFATAL, "target of WRT must be a section ");
538 s.size = size;
539 ieee_install_fixup(segto, &s);
540 return;
542 /* Pure segment fixup ? */
543 if (segment != NO_SEG) {
544 s.ftype = FT_SEG;
545 s.id1 = 0;
546 if (segment >= SEG_ABS) {
547 /* absolute far segment fixup */
548 s.id1 = -(segment - ~SEG_ABS);
549 } else if (segment % 2) {
550 /* fixup to named segment */
551 /* look it up */
552 for (target = seghead; target; target = target->next)
553 if (target->index == segment - 1)
554 break;
555 if (target)
556 s.id1 = target->ieee_index;
557 else {
559 * Now we assume the segment field is being used
560 * to hold an extern index
562 int32_t i = segment / 2;
563 struct ExtBack *eb = ebhead;
564 while (i > EXT_BLKSIZ) {
565 if (eb)
566 eb = eb->next;
567 else
568 break;
569 i -= EXT_BLKSIZ;
571 /* if we have an extern decide the type and make a record
573 if (eb) {
574 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
575 nasm_panic(0,
576 "Segment of a rel not supported in ieee_write_fixup");
577 } else {
578 /* If we want the segment */
579 s.ftype = FT_EXTSEG;
580 s.addend = 0;
581 s.id1 = eb->index[i];
584 } else
585 /* If we get here the seg value doesn't make sense */
586 nasm_panic(0,
587 "unrecognised segment value in ieee_write_fixup");
590 } else {
591 /* Assume we are offsetting directly from a section
592 * So look up the target segment
594 for (target = seghead; target; target = target->next)
595 if (target->index == segment)
596 break;
597 if (target) {
598 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
599 /* PC rel to a known offset */
600 s.id1 = target->ieee_index;
601 s.ftype = FT_REL;
602 s.size = size;
603 s.addend = offset;
604 } else {
605 /* We were offsetting from a seg */
606 s.id1 = target->ieee_index;
607 s.ftype = FT_OFS;
608 s.size = size;
609 s.addend = offset;
611 } else {
613 * Now we assume the segment field is being used
614 * to hold an extern index
616 int32_t i = segment / 2;
617 struct ExtBack *eb = ebhead;
618 while (i > EXT_BLKSIZ) {
619 if (eb)
620 eb = eb->next;
621 else
622 break;
623 i -= EXT_BLKSIZ;
625 /* if we have an extern decide the type and make a record
627 if (eb) {
628 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
629 s.ftype = FT_EXTREL;
630 s.addend = 0;
631 s.id1 = eb->index[i];
632 } else {
633 /* else we want the external offset */
634 s.ftype = FT_EXT;
635 s.addend = 0;
636 s.id1 = eb->index[i];
639 } else
640 /* If we get here the seg value doesn't make sense */
641 nasm_panic(0,
642 "unrecognised segment value in ieee_write_fixup");
645 if (size != 2 && s.ftype == FT_SEG)
646 nasm_error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
647 " segment base references");
648 s.size = size;
649 ieee_install_fixup(segto, &s);
650 return;
652 /* should never get here */
654 static void ieee_install_fixup(struct ieeeSection *seg,
655 struct ieeeFixupp *fix)
657 struct ieeeFixupp *f;
658 f = nasm_malloc(sizeof(struct ieeeFixupp));
659 memcpy(f, fix, sizeof(struct ieeeFixupp));
660 f->offset = seg->currentpos;
661 seg->currentpos += fix->size;
662 f->next = NULL;
663 if (seg->fptr)
664 seg->flptr = seg->flptr->next = f;
665 else
666 seg->fptr = seg->flptr = f;
671 * segment registry
673 static int32_t ieee_segment(char *name, int pass, int *bits)
676 * We call the label manager here to define a name for the new
677 * segment, and when our _own_ label-definition stub gets
678 * called in return, it should register the new segment name
679 * using the pointer it gets passed. That way we save memory,
680 * by sponging off the label manager.
682 if (!name) {
683 *bits = 16;
684 if (!any_segs)
685 return 0;
686 return seghead->index;
687 } else {
688 struct ieeeSection *seg;
689 int ieee_idx, attrs;
690 bool rn_error;
691 char *p;
694 * Look for segment attributes.
696 attrs = 0;
697 while (*name == '.')
698 name++; /* hack, but a documented one */
699 p = name;
700 while (*p && !nasm_isspace(*p))
701 p++;
702 if (*p) {
703 *p++ = '\0';
704 while (*p && nasm_isspace(*p))
705 *p++ = '\0';
707 while (*p) {
708 while (*p && !nasm_isspace(*p))
709 p++;
710 if (*p) {
711 *p++ = '\0';
712 while (*p && nasm_isspace(*p))
713 *p++ = '\0';
716 attrs++;
719 ieee_idx = 1;
720 for (seg = seghead; seg; seg = seg->next) {
721 ieee_idx++;
722 if (!strcmp(seg->name, name)) {
723 if (attrs > 0 && pass == 1)
724 nasm_error(ERR_WARNING, "segment attributes specified on"
725 " redeclaration of segment: ignoring");
726 if (seg->use32)
727 *bits = 32;
728 else
729 *bits = 16;
730 return seg->index;
734 *segtail = seg = nasm_malloc(sizeof(*seg));
735 seg->next = NULL;
736 segtail = &seg->next;
737 seg->index = seg_alloc();
738 seg->ieee_index = ieee_idx;
739 any_segs = true;
740 seg->name = NULL;
741 seg->currentpos = 0;
742 seg->align = 1; /* default */
743 seg->use32 = *bits == 32; /* default to user spec */
744 seg->combine = CMB_PUBLIC; /* default */
745 seg->pubhead = NULL;
746 seg->pubtail = &seg->pubhead;
747 seg->data = NULL;
748 seg->fptr = NULL;
749 seg->lochead = NULL;
750 seg->loctail = &seg->lochead;
753 * Process the segment attributes.
755 p = name;
756 while (attrs--) {
757 p += strlen(p);
758 while (!*p)
759 p++;
762 * `p' contains a segment attribute.
764 if (!nasm_stricmp(p, "private"))
765 seg->combine = CMB_PRIVATE;
766 else if (!nasm_stricmp(p, "public"))
767 seg->combine = CMB_PUBLIC;
768 else if (!nasm_stricmp(p, "common"))
769 seg->combine = CMB_COMMON;
770 else if (!nasm_stricmp(p, "use16"))
771 seg->use32 = false;
772 else if (!nasm_stricmp(p, "use32"))
773 seg->use32 = true;
774 else if (!nasm_strnicmp(p, "align=", 6)) {
775 seg->align = readnum(p + 6, &rn_error);
776 if (seg->align == 0)
777 seg->align = 1;
778 if (rn_error) {
779 seg->align = 1;
780 nasm_error(ERR_NONFATAL, "segment alignment should be"
781 " numeric");
783 switch (seg->align) {
784 case 1: /* BYTE */
785 case 2: /* WORD */
786 case 4: /* DWORD */
787 case 16: /* PARA */
788 case 256: /* PAGE */
789 case 8:
790 case 32:
791 case 64:
792 case 128:
793 break;
794 default:
795 nasm_error(ERR_NONFATAL, "invalid alignment value %d",
796 seg->align);
797 seg->align = 1;
798 break;
800 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
801 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
802 if (rn_error)
803 nasm_error(ERR_NONFATAL, "argument to `absolute' segment"
804 " attribute should be numeric");
808 ieee_seg_needs_update = seg;
809 if (seg->align >= SEG_ABS)
810 define_label(name, NO_SEG, seg->align - SEG_ABS,
811 NULL, false, false);
812 else
813 define_label(name, seg->index + 1, 0L, NULL, false, false);
814 ieee_seg_needs_update = NULL;
816 if (seg->use32)
817 *bits = 32;
818 else
819 *bits = 16;
820 return seg->index;
825 * directives supported
827 static enum directive_result
828 ieee_directive(enum directive directive, char *value, int pass)
831 (void)value;
832 (void)pass;
834 switch (directive) {
835 case D_UPPERCASE:
836 ieee_uppercase = true;
837 return DIRR_OK;
839 default:
840 return DIRR_UNKNOWN;
844 static void ieee_sectalign(int32_t seg, unsigned int value)
846 struct ieeeSection *s;
848 list_for_each(s, seghead) {
849 if (s->index == seg)
850 break;
854 * 256 is maximum there, note it may happen
855 * that align is issued on "absolute" segment
856 * it's fine since SEG_ABS > 256 and we never
857 * get escape this test
859 if (!s || !is_power2(value) || value > 256)
860 return;
862 if ((unsigned int)s->align < value)
863 s->align = value;
867 * Return segment data
869 static int32_t ieee_segbase(int32_t segment)
871 struct ieeeSection *seg;
874 * Find the segment in our list.
876 for (seg = seghead; seg; seg = seg->next)
877 if (seg->index == segment - 1)
878 break;
880 if (!seg)
881 return segment; /* not one of ours - leave it alone */
883 if (seg->align >= SEG_ABS)
884 return seg->align; /* absolute segment */
886 return segment; /* no special treatment */
890 * filename
892 static void ieee_filename(char *inname, char *outname)
894 strcpy(ieee_infile, inname);
895 standard_extension(inname, outname, ".o");
898 static void ieee_write_file(void)
900 struct tm *thetime;
901 time_t reltime;
902 struct FileName *fn;
903 struct ieeeSection *seg;
904 struct ieeePublic *pub, *loc;
905 struct ieeeExternal *ext;
906 struct ieeeObjData *data;
907 struct ieeeFixupp *fix;
908 struct Array *arr;
909 int i;
910 const bool debuginfo = (dfmt == &ladsoft_debug_form);
913 * Write the module header
915 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile), ieee_infile);
918 * Write the NASM boast comment.
920 ieee_putascii("CO0,%02X%s.\n", strlen(nasm_comment), nasm_comment);
923 * write processor-specific information
925 ieee_putascii("AD8,4,L.\n");
928 * date and time
930 time(&reltime);
931 thetime = localtime(&reltime);
932 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
933 1900 + thetime->tm_year, thetime->tm_mon + 1,
934 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
935 thetime->tm_sec);
937 * if debugging, dump file names
939 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
940 ieee_putascii("C0105,%02X%s.\n", strlen(fn->name), fn->name);
943 ieee_putascii("CO101,07ENDHEAD.\n");
945 * the standard doesn't specify when to put checksums,
946 * we'll just do it periodically.
948 ieee_putcs(false);
951 * Write the section headers
953 seg = seghead;
954 if (!debuginfo && !strcmp(seg->name, "??LINE"))
955 seg = seg->next;
956 while (seg) {
957 char buf[256];
958 char attrib;
959 switch (seg->combine) {
960 case CMB_PUBLIC:
961 default:
962 attrib = 'C';
963 break;
964 case CMB_PRIVATE:
965 attrib = 'S';
966 break;
967 case CMB_COMMON:
968 attrib = 'M';
969 break;
971 ieee_unqualified_name(buf, seg->name);
972 if (seg->align >= SEG_ABS) {
973 ieee_putascii("ST%X,A,%02X%s.\n", seg->ieee_index,
974 strlen(buf), buf);
975 ieee_putascii("ASL%X,%lX.\n", seg->ieee_index,
976 (seg->align - SEG_ABS) * 16);
977 } else {
978 ieee_putascii("ST%X,%c,%02X%s.\n", seg->ieee_index, attrib,
979 strlen(buf), buf);
980 ieee_putascii("SA%X,%lX.\n", seg->ieee_index, seg->align);
981 ieee_putascii("ASS%X,%X.\n", seg->ieee_index,
982 seg->currentpos);
984 seg = seg->next;
987 * write the start address if there is one
989 if (ieee_entry_seg) {
990 for (seg = seghead; seg; seg = seg->next)
991 if (seg->index == ieee_entry_seg)
992 break;
993 if (!seg)
994 nasm_panic(0, "Start address records are incorrect");
995 else
996 ieee_putascii("ASG,R%X,%lX,+.\n", seg->ieee_index,
997 ieee_entry_ofs);
1000 ieee_putcs(false);
1002 * Write the publics
1004 i = 1;
1005 for (seg = seghead; seg; seg = seg->next) {
1006 for (pub = seg->pubhead; pub; pub = pub->next) {
1007 char buf[256];
1008 ieee_unqualified_name(buf, pub->name);
1009 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1010 if (pub->segment == -1)
1011 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1012 pub->offset);
1013 else
1014 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1015 pub->offset);
1016 if (debuginfo) {
1017 if (pub->type >= 0x100)
1018 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1019 else
1020 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1022 i++;
1025 pub = fpubhead;
1026 i = 1;
1027 while (pub) {
1028 char buf[256];
1029 ieee_unqualified_name(buf, pub->name);
1030 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1031 if (pub->segment == -1)
1032 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1033 pub->offset);
1034 else
1035 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1036 pub->offset);
1037 if (debuginfo) {
1038 if (pub->type >= 0x100)
1039 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1040 else
1041 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1043 i++;
1044 pub = pub->next;
1047 * Write the externals
1049 ext = exthead;
1050 i = 1;
1051 while (ext) {
1052 char buf[256];
1053 ieee_unqualified_name(buf, ext->name);
1054 ieee_putascii("NX%X,%02X%s.\n", i++, strlen(buf), buf);
1055 ext = ext->next;
1057 ieee_putcs(false);
1060 * IEEE doesn't have a standard pass break record
1061 * so use the ladsoft variant
1063 ieee_putascii("CO100,06ENDSYM.\n");
1066 * now put types
1068 i = ARRAY_BOT;
1069 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1070 ieee_putascii("TY%X,20,%X,%lX.\n", i++, arr->basetype,
1071 arr->size);
1074 * now put locals
1076 i = 1;
1077 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1078 for (loc = seg->lochead; loc; loc = loc->next) {
1079 char buf[256];
1080 ieee_unqualified_name(buf, loc->name);
1081 ieee_putascii("NN%X,%02X%s.\n", i, strlen(buf), buf);
1082 if (loc->segment == -1)
1083 ieee_putascii("ASN%X,R%X,%lX,+.\n", i, loc->index,
1084 loc->offset);
1085 else
1086 ieee_putascii("ASN%X,%lX,%lX,+.\n", i, loc->segment * 16,
1087 loc->offset);
1088 if (debuginfo) {
1089 if (loc->type >= 0x100)
1090 ieee_putascii("ATN%X,T%X.\n", i, loc->type - 0x100);
1091 else
1092 ieee_putascii("ATN%X,%X.\n", i, loc->type);
1094 i++;
1099 * put out section data;
1101 seg = seghead;
1102 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1103 seg = seg->next;
1104 while (seg) {
1105 if (seg->currentpos) {
1106 int32_t size, org = 0;
1107 data = seg->data;
1108 ieee_putascii("SB%X.\n", seg->ieee_index);
1109 fix = seg->fptr;
1110 while (fix) {
1111 size = HUNKSIZE - (org % HUNKSIZE);
1112 size =
1113 size + org >
1114 seg->currentpos ? seg->currentpos - org : size;
1115 size = fix->offset - org > size ? size : fix->offset - org;
1116 org = ieee_putld(org, org + size, data->data);
1117 if (org % HUNKSIZE == 0)
1118 data = data->next;
1119 if (org == fix->offset) {
1120 org += ieee_putlr(fix);
1121 fix = fix->next;
1124 while (org < seg->currentpos && data) {
1125 size =
1126 seg->currentpos - org >
1127 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1128 org = ieee_putld(org, org + size, data->data);
1129 data = data->next;
1131 ieee_putcs(false);
1134 seg = seg->next;
1137 * module end record
1139 ieee_putascii("ME.\n");
1142 static void ieee_write_byte(struct ieeeSection *seg, int data)
1144 int temp;
1145 if (!(temp = seg->currentpos++ % HUNKSIZE))
1146 ieee_data_new(seg);
1147 seg->datacurr->data[temp] = data;
1150 static void ieee_write_word(struct ieeeSection *seg, int data)
1152 ieee_write_byte(seg, data & 0xFF);
1153 ieee_write_byte(seg, (data >> 8) & 0xFF);
1156 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1158 ieee_write_byte(seg, data & 0xFF);
1159 ieee_write_byte(seg, (data >> 8) & 0xFF);
1160 ieee_write_byte(seg, (data >> 16) & 0xFF);
1161 ieee_write_byte(seg, (data >> 24) & 0xFF);
1163 static void ieee_putascii(char *format, ...)
1165 char buffer[256];
1166 int i, l;
1167 va_list ap;
1169 va_start(ap, format);
1170 vsnprintf(buffer, sizeof(buffer), format, ap);
1171 l = strlen(buffer);
1172 for (i = 0; i < l; i++)
1173 if ((uint8_t)buffer[i] > 31)
1174 checksum += buffer[i];
1175 va_end(ap);
1176 fputs(buffer, ofile);
1180 * put out a checksum record */
1181 static void ieee_putcs(int toclear)
1183 if (toclear) {
1184 ieee_putascii("CS.\n");
1185 } else {
1186 checksum += 'C';
1187 checksum += 'S';
1188 ieee_putascii("CS%02X.\n", checksum & 127);
1190 checksum = 0;
1193 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1195 int32_t val;
1196 if (start == end)
1197 return (start);
1198 val = start % HUNKSIZE;
1199 /* fill up multiple lines */
1200 while (end - start >= LDPERLINE) {
1201 int i;
1202 ieee_putascii("LD");
1203 for (i = 0; i < LDPERLINE; i++) {
1204 ieee_putascii("%02X", buf[val++]);
1205 start++;
1207 ieee_putascii(".\n");
1209 /* if no partial lines */
1210 if (start == end)
1211 return (start);
1212 /* make a partial line */
1213 ieee_putascii("LD");
1214 while (start < end) {
1215 ieee_putascii("%02X", buf[val++]);
1216 start++;
1218 ieee_putascii(".\n");
1219 return (start);
1221 static int32_t ieee_putlr(struct ieeeFixupp *p)
1224 * To deal with the vagaries of segmentation the LADsoft linker
1225 * defines two types of segments: absolute and virtual. Note that
1226 * 'absolute' in this context is a different thing from the IEEE
1227 * definition of an absolute segment type, which is also supported. If a
1228 * sement is linked in virtual mode the low limit (L-var) is
1229 * subtracted from each R,X, and P variable which appears in an
1230 * expression, so that we can have relative offsets. Meanwhile
1231 * in the ABSOLUTE mode this subtraction is not done and
1232 * so we can use absolute offsets from 0. In the LADsoft linker
1233 * this configuration is not done in the assemblker source but in
1234 * a source the linker reads. Generally this type of thing only
1235 * becomes an issue if real mode code is used. A pure 32-bit linker could
1236 * get away without defining the virtual mode...
1238 char buf[40];
1239 int32_t size = p->size;
1240 switch (p->ftype) {
1241 case FT_SEG:
1242 if (p->id1 < 0)
1243 sprintf(buf, "%"PRIX32"", -p->id1);
1244 else
1245 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1246 break;
1247 case FT_OFS:
1248 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1249 break;
1250 case FT_REL:
1251 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1252 break;
1254 case FT_WRT:
1255 if (p->id2 < 0)
1256 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1257 p->id2, -p->id1 * 16);
1258 else
1259 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1260 p->id2, p->id1);
1261 break;
1262 case FT_EXT:
1263 sprintf(buf, "X%"PRIX32"", p->id1);
1264 break;
1265 case FT_EXTREL:
1266 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1267 break;
1268 case FT_EXTSEG:
1269 /* We needed a non-ieee hack here.
1270 * We introduce the Y variable, which is the low
1271 * limit of the native segment the extern resides in
1273 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1274 break;
1275 case FT_EXTWRT:
1276 if (p->id2 < 0)
1277 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1278 -p->id1 * 16);
1279 else
1280 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1281 break;
1283 ieee_putascii("LR(%s,%"PRIX32").\n", buf, size);
1285 return (size);
1288 /* Dump all segment data (text and fixups )*/
1290 static void ieee_unqualified_name(char *dest, char *source)
1292 if (ieee_uppercase) {
1293 while (*source)
1294 *dest++ = toupper(*source++);
1295 *dest = 0;
1296 } else
1297 strcpy(dest, source);
1299 static void dbgls_init(void)
1301 int tempint;
1303 fnhead = NULL;
1304 fntail = &fnhead;
1305 arrindex = ARRAY_BOT;
1306 arrhead = NULL;
1307 arrtail = &arrhead;
1308 ieee_segment("??LINE", 2, &tempint);
1309 any_segs = false;
1311 static void dbgls_cleanup(void)
1313 struct ieeeSection *segtmp;
1314 while (fnhead) {
1315 struct FileName *fntemp = fnhead;
1316 fnhead = fnhead->next;
1317 nasm_free(fntemp->name);
1318 nasm_free(fntemp);
1320 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1321 while (segtmp->lochead) {
1322 struct ieeePublic *loctmp = segtmp->lochead;
1323 segtmp->lochead = loctmp->next;
1324 nasm_free(loctmp->name);
1325 nasm_free(loctmp);
1328 while (arrhead) {
1329 struct Array *arrtmp = arrhead;
1330 arrhead = arrhead->next;
1331 nasm_free(arrtmp);
1336 * because this routine is not bracketed in
1337 * the main program, this routine will be called even if there
1338 * is no request for debug info
1339 * so, we have to make sure the ??LINE segment is avaialbe
1340 * as the first segment when this debug format is selected
1342 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1344 struct FileName *fn;
1345 struct ieeeSection *seg;
1346 int i = 0;
1347 if (segto == NO_SEG)
1348 return;
1351 * If `any_segs' is still false, we must define a default
1352 * segment.
1354 if (!any_segs) {
1355 int tempint; /* ignored */
1356 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1357 nasm_panic(0, "strange segment conditions in OBJ driver");
1361 * Find the segment we are targetting.
1363 for (seg = seghead; seg; seg = seg->next)
1364 if (seg->index == segto)
1365 break;
1366 if (!seg)
1367 nasm_panic(0, "lineno directed to nonexistent segment?");
1369 for (fn = fnhead; fn; fn = fn->next) {
1370 if (!nasm_stricmp(lnfname, fn->name))
1371 break;
1372 i++;
1374 if (!fn) {
1375 fn = nasm_malloc(sizeof(*fn));
1376 fn->name = nasm_malloc(strlen(lnfname) + 1);
1377 fn->index = i;
1378 strcpy(fn->name, lnfname);
1379 fn->next = NULL;
1380 *fntail = fn;
1381 fntail = &fn->next;
1383 ieee_write_byte(seghead, fn->index);
1384 ieee_write_word(seghead, lineno);
1385 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1386 seg->currentpos);
1389 static void dbgls_deflabel(char *name, int32_t segment,
1390 int64_t offset, int is_global, char *special)
1392 struct ieeeSection *seg;
1394 /* Keep compiler from warning about special */
1395 (void)special;
1398 * Note: ..[^@] special symbols are filtered in labels.c
1402 * If it's a special-retry from pass two, discard it.
1404 if (is_global == 3)
1405 return;
1408 * Case (i):
1410 if (ieee_seg_needs_update)
1411 return;
1412 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
1413 return;
1415 if (segment >= SEG_ABS || segment == NO_SEG) {
1416 return;
1420 * If `any_segs' is still false, we might need to define a
1421 * default segment, if they're trying to declare a label in
1422 * `first_seg'. But the label should exist due to a prior
1423 * call to ieee_deflabel so we can skip that.
1426 for (seg = seghead; seg; seg = seg->next)
1427 if (seg->index == segment) {
1428 struct ieeePublic *loc;
1430 * Case (ii). Maybe MODPUB someday?
1432 if (!is_global) {
1433 last_defined = loc = nasm_malloc(sizeof(*loc));
1434 *seg->loctail = loc;
1435 seg->loctail = &loc->next;
1436 loc->next = NULL;
1437 loc->name = nasm_strdup(name);
1438 loc->offset = offset;
1439 loc->segment = -1;
1440 loc->index = seg->ieee_index;
1444 static void dbgls_typevalue(int32_t type)
1446 int elem = TYM_ELEMENTS(type);
1447 type = TYM_TYPE(type);
1449 if (!last_defined)
1450 return;
1452 switch (type) {
1453 case TY_BYTE:
1454 last_defined->type = 1; /* uint8_t */
1455 break;
1456 case TY_WORD:
1457 last_defined->type = 3; /* unsigned word */
1458 break;
1459 case TY_DWORD:
1460 last_defined->type = 5; /* unsigned dword */
1461 break;
1462 case TY_FLOAT:
1463 last_defined->type = 9; /* float */
1464 break;
1465 case TY_QWORD:
1466 last_defined->type = 10; /* qword */
1467 break;
1468 case TY_TBYTE:
1469 last_defined->type = 11; /* TBYTE */
1470 break;
1471 default:
1472 last_defined->type = 0x10; /* near label */
1473 break;
1476 if (elem > 1) {
1477 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
1478 int vtype = last_defined->type;
1479 arrtmp->size = elem;
1480 arrtmp->basetype = vtype;
1481 arrtmp->next = NULL;
1482 last_defined->type = arrindex++ + 0x100;
1483 *arrtail = arrtmp;
1484 arrtail = &(arrtmp->next);
1486 last_defined = NULL;
1488 static void dbgls_output(int output_type, void *param)
1490 (void)output_type;
1491 (void)param;
1493 static const struct dfmt ladsoft_debug_form = {
1494 "LADsoft Debug Records",
1495 "ladsoft",
1496 dbgls_init,
1497 dbgls_linnum,
1498 dbgls_deflabel,
1499 null_debug_directive,
1500 dbgls_typevalue,
1501 dbgls_output,
1502 dbgls_cleanup,
1503 NULL /* pragma list */
1505 static const struct dfmt * const ladsoft_debug_arr[3] = {
1506 &ladsoft_debug_form,
1507 &null_debug_form,
1508 NULL
1510 const struct ofmt of_ieee = {
1511 "IEEE-695 (LADsoft variant) object file format",
1512 "ieee",
1513 OFMT_TEXT,
1515 ladsoft_debug_arr,
1516 &ladsoft_debug_form,
1517 NULL,
1518 ieee_init,
1519 nasm_do_legacy_output,
1520 ieee_out,
1521 ieee_deflabel,
1522 ieee_segment,
1523 ieee_sectalign,
1524 ieee_segbase,
1525 ieee_directive,
1526 ieee_filename,
1527 ieee_cleanup,
1528 NULL /* pragma list */
1531 #endif /* OF_IEEE */