insns: add OBSOLETE flag for instructions removed from architecture
[nasm.git] / output / outieee.c
blobeb4bcf4111c66f3121c46b7fb8a227420e89afd1
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>
76 #include <inttypes.h>
78 #include "nasm.h"
79 #include "nasmlib.h"
80 #include "output/outform.h"
81 #include "output/outlib.h"
83 #ifdef OF_IEEE
85 #define ARRAY_BOT 0x1
87 static char ieee_infile[FILENAME_MAX];
88 static int ieee_uppercase;
90 static bool any_segs;
91 static int arrindex;
93 #define HUNKSIZE 1024 /* Size of the data hunk */
94 #define EXT_BLKSIZ 512
95 #define LDPERLINE 32 /* bytes per line in output */
97 struct ieeeSection;
99 struct LineNumber {
100 struct LineNumber *next;
101 struct ieeeSection *segment;
102 int32_t offset;
103 int32_t lineno;
106 static struct FileName {
107 struct FileName *next;
108 char *name;
109 int32_t index;
110 } *fnhead, **fntail;
112 static struct Array {
113 struct Array *next;
114 unsigned size;
115 int basetype;
116 } *arrhead, **arrtail;
118 static struct ieeePublic {
119 struct ieeePublic *next;
120 char *name;
121 int32_t offset;
122 int32_t segment; /* only if it's far-absolute */
123 int32_t index;
124 int type; /* for debug purposes */
125 } *fpubhead, **fpubtail, *last_defined;
127 static struct ieeeExternal {
128 struct ieeeExternal *next;
129 char *name;
130 int32_t commonsize;
131 } *exthead, **exttail;
133 static int externals;
135 static struct ExtBack {
136 struct ExtBack *next;
137 int index[EXT_BLKSIZ];
138 } *ebhead, **ebtail;
140 /* NOTE: the first segment MUST be the lineno segment */
141 static struct ieeeSection {
142 struct ieeeSection *next;
143 char *name;
144 struct ieeeObjData *data, *datacurr;
145 struct ieeeFixupp *fptr, *flptr;
146 int32_t index; /* the NASM segment id */
147 int32_t ieee_index; /* the OBJ-file segment index */
148 int32_t currentpos;
149 int32_t align; /* can be SEG_ABS + absolute addr */
150 int32_t startpos;
151 int32_t use32; /* is this segment 32-bit? */
152 struct ieeePublic *pubhead, **pubtail, *lochead, **loctail;
153 enum {
154 CMB_PRIVATE = 0,
155 CMB_PUBLIC = 2,
156 CMB_COMMON = 6
157 } combine;
158 } *seghead, **segtail, *ieee_seg_needs_update;
160 struct ieeeObjData {
161 struct ieeeObjData *next;
162 uint8_t data[HUNKSIZE];
165 struct ieeeFixupp {
166 struct ieeeFixupp *next;
167 enum {
168 FT_SEG = 0,
169 FT_REL = 1,
170 FT_OFS = 2,
171 FT_EXT = 3,
172 FT_WRT = 4,
173 FT_EXTREL = 5,
174 FT_EXTWRT = 6,
175 FT_EXTSEG = 7
176 } ftype;
177 int16_t size;
178 int32_t id1;
179 int32_t id2;
180 int32_t offset;
181 int32_t addend;
184 static int32_t ieee_entry_seg, ieee_entry_ofs;
185 static int checksum;
187 extern struct ofmt of_ieee;
188 static struct dfmt ladsoft_debug_form;
190 static void ieee_data_new(struct ieeeSection *);
191 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection *,
192 int, uint64_t, int32_t);
193 static void ieee_install_fixup(struct ieeeSection *, struct ieeeFixupp *);
194 static int32_t ieee_segment(char *, int, int *);
195 static void ieee_write_file(void);
196 static void ieee_write_byte(struct ieeeSection *, int);
197 static void ieee_write_word(struct ieeeSection *, int);
198 static void ieee_write_dword(struct ieeeSection *, int32_t);
199 static void ieee_putascii(char *, ...);
200 static void ieee_putcs(int);
201 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
202 static int32_t ieee_putlr(struct ieeeFixupp *);
203 static void ieee_unqualified_name(char *, char *);
206 * pup init
208 static void ieee_init(void)
210 any_segs = false;
211 fpubhead = NULL;
212 fpubtail = &fpubhead;
213 exthead = NULL;
214 exttail = &exthead;
215 externals = 1;
216 ebhead = NULL;
217 ebtail = &ebhead;
218 seghead = ieee_seg_needs_update = NULL;
219 segtail = &seghead;
220 ieee_entry_seg = NO_SEG;
221 ieee_uppercase = false;
222 checksum = 0;
225 static int ieee_set_info(enum geninfo type, char **val)
227 (void)type;
228 (void)val;
230 return 0;
234 * Rundown
236 static void ieee_cleanup(void)
238 ieee_write_file();
239 dfmt->cleanup();
240 while (seghead) {
241 struct ieeeSection *segtmp = seghead;
242 seghead = seghead->next;
243 while (segtmp->pubhead) {
244 struct ieeePublic *pubtmp = segtmp->pubhead;
245 segtmp->pubhead = pubtmp->next;
246 nasm_free(pubtmp);
248 while (segtmp->fptr) {
249 struct ieeeFixupp *fixtmp = segtmp->fptr;
250 segtmp->fptr = fixtmp->next;
251 nasm_free(fixtmp);
253 while (segtmp->data) {
254 struct ieeeObjData *dattmp = segtmp->data;
255 segtmp->data = dattmp->next;
256 nasm_free(dattmp);
258 nasm_free(segtmp);
260 while (fpubhead) {
261 struct ieeePublic *pubtmp = fpubhead;
262 fpubhead = fpubhead->next;
263 nasm_free(pubtmp);
265 while (exthead) {
266 struct ieeeExternal *exttmp = exthead;
267 exthead = exthead->next;
268 nasm_free(exttmp);
270 while (ebhead) {
271 struct ExtBack *ebtmp = ebhead;
272 ebhead = ebhead->next;
273 nasm_free(ebtmp);
278 * callback for labels
280 static void ieee_deflabel(char *name, int32_t segment,
281 int64_t offset, int is_global, char *special)
284 * We have three cases:
286 * (i) `segment' is a segment-base. If so, set the name field
287 * for the segment structure it refers to, and then
288 * return.
290 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
291 * Save the label position for later output of a PUBDEF record.
294 * (iii) `segment' is not one of our segments. Save the label
295 * position for later output of an EXTDEF.
297 struct ieeeExternal *ext;
298 struct ExtBack *eb;
299 struct ieeeSection *seg;
300 int i;
302 if (special) {
303 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
306 * First check for the double-period, signifying something
307 * unusual.
309 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
310 if (!strcmp(name, "..start")) {
311 ieee_entry_seg = segment;
312 ieee_entry_ofs = offset;
314 return;
318 * Case (i):
320 if (ieee_seg_needs_update) {
321 ieee_seg_needs_update->name = name;
322 return;
324 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
325 return;
328 * case (ii)
330 if (segment >= SEG_ABS) {
332 * SEG_ABS subcase of (ii).
334 if (is_global) {
335 struct ieeePublic *pub;
337 pub = *fpubtail = nasm_malloc(sizeof(*pub));
338 fpubtail = &pub->next;
339 pub->next = NULL;
340 pub->name = name;
341 pub->offset = offset;
342 pub->segment = segment & ~SEG_ABS;
344 return;
347 for (seg = seghead; seg && is_global; seg = seg->next)
348 if (seg->index == segment) {
349 struct ieeePublic *pub;
351 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
352 seg->pubtail = &pub->next;
353 pub->next = NULL;
354 pub->name = name;
355 pub->offset = offset;
356 pub->index = seg->ieee_index;
357 pub->segment = -1;
358 return;
362 * Case (iii).
364 if (is_global) {
365 ext = *exttail = nasm_malloc(sizeof(*ext));
366 ext->next = NULL;
367 exttail = &ext->next;
368 ext->name = name;
369 if (is_global == 2)
370 ext->commonsize = offset;
371 else
372 ext->commonsize = 0;
373 i = segment / 2;
374 eb = ebhead;
375 if (!eb) {
376 eb = *ebtail = nasm_zalloc(sizeof(*eb));
377 eb->next = NULL;
378 ebtail = &eb->next;
380 while (i > EXT_BLKSIZ) {
381 if (eb && eb->next)
382 eb = eb->next;
383 else {
384 eb = *ebtail = nasm_zalloc(sizeof(*eb));
385 eb->next = NULL;
386 ebtail = &eb->next;
388 i -= EXT_BLKSIZ;
390 eb->index[i] = externals++;
396 * Put data out
398 static void ieee_out(int32_t segto, const void *data,
399 enum out_type type, uint64_t size,
400 int32_t segment, int32_t wrt)
402 const uint8_t *ucdata;
403 int32_t ldata;
404 struct ieeeSection *seg;
407 * handle absolute-assembly (structure definitions)
409 if (segto == NO_SEG) {
410 if (type != OUT_RESERVE)
411 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
412 " space");
413 return;
417 * If `any_segs' is still false, we must define a default
418 * segment.
420 if (!any_segs) {
421 int tempint; /* ignored */
422 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
423 nasm_panic(0, "strange segment conditions in IEEE driver");
427 * Find the segment we are targetting.
429 for (seg = seghead; seg; seg = seg->next)
430 if (seg->index == segto)
431 break;
432 if (!seg)
433 nasm_panic(0, "code directed to nonexistent segment?");
435 if (type == OUT_RAWDATA) {
436 ucdata = data;
437 while (size--)
438 ieee_write_byte(seg, *ucdata++);
439 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
440 type == OUT_REL4ADR) {
441 if (type == OUT_ADDRESS)
442 size = abs((int)size);
443 else if (segment == NO_SEG)
444 nasm_error(ERR_NONFATAL, "relative call to absolute address not"
445 " supported by IEEE format");
446 ldata = *(int64_t *)data;
447 if (type == OUT_REL2ADR)
448 ldata += (size - 2);
449 if (type == OUT_REL4ADR)
450 ldata += (size - 4);
451 ieee_write_fixup(segment, wrt, seg, size, type, ldata);
452 if (size == 2)
453 ieee_write_word(seg, ldata);
454 else
455 ieee_write_dword(seg, ldata);
456 } else if (type == OUT_RESERVE) {
457 while (size--)
458 ieee_write_byte(seg, 0);
462 static void ieee_data_new(struct ieeeSection *segto)
465 if (!segto->data)
466 segto->data = segto->datacurr =
467 nasm_malloc(sizeof(*(segto->datacurr)));
468 else
469 segto->datacurr = segto->datacurr->next =
470 nasm_malloc(sizeof(*(segto->datacurr)));
471 segto->datacurr->next = NULL;
475 * this routine is unalduterated bloatware. I usually don't do this
476 * but I might as well see what it is like on a harmless program.
477 * If anyone wants to optimize this is a good canditate!
479 static void ieee_write_fixup(int32_t segment, int32_t wrt,
480 struct ieeeSection *segto, int size,
481 uint64_t realtype, int32_t offset)
483 struct ieeeSection *target;
484 struct ieeeFixupp s;
486 /* Don't put a fixup for things NASM can calculate */
487 if (wrt == NO_SEG && segment == NO_SEG)
488 return;
490 s.ftype = -1;
491 /* if it is a WRT offset */
492 if (wrt != NO_SEG) {
493 s.ftype = FT_WRT;
494 s.addend = offset;
495 if (wrt >= SEG_ABS)
496 s.id1 = -(wrt - SEG_ABS);
497 else {
498 if (wrt % 2 && realtype != OUT_REL2ADR
499 && realtype != OUT_REL4ADR) {
500 wrt--;
502 for (target = seghead; target; target = target->next)
503 if (target->index == wrt)
504 break;
505 if (target) {
506 s.id1 = target->ieee_index;
507 for (target = seghead; target; target = target->next)
508 if (target->index == segment)
509 break;
511 if (target)
512 s.id2 = target->ieee_index;
513 else {
515 * Now we assume the segment field is being used
516 * to hold an extern index
518 int32_t i = segment / 2;
519 struct ExtBack *eb = ebhead;
520 while (i > EXT_BLKSIZ) {
521 if (eb)
522 eb = eb->next;
523 else
524 break;
525 i -= EXT_BLKSIZ;
527 /* if we have an extern decide the type and make a record
529 if (eb) {
530 s.ftype = FT_EXTWRT;
531 s.addend = 0;
532 s.id2 = eb->index[i];
533 } else
534 nasm_error(ERR_NONFATAL,
535 "Source of WRT must be an offset");
538 } else
539 nasm_panic(0,
540 "unrecognised WRT value in ieee_write_fixup");
541 } else
542 nasm_error(ERR_NONFATAL, "target of WRT must be a section ");
544 s.size = size;
545 ieee_install_fixup(segto, &s);
546 return;
548 /* Pure segment fixup ? */
549 if (segment != NO_SEG) {
550 s.ftype = FT_SEG;
551 s.id1 = 0;
552 if (segment >= SEG_ABS) {
553 /* absolute far segment fixup */
554 s.id1 = -(segment - ~SEG_ABS);
555 } else if (segment % 2) {
556 /* fixup to named segment */
557 /* look it up */
558 for (target = seghead; target; target = target->next)
559 if (target->index == segment - 1)
560 break;
561 if (target)
562 s.id1 = target->ieee_index;
563 else {
565 * Now we assume the segment field is being used
566 * to hold an extern index
568 int32_t i = segment / 2;
569 struct ExtBack *eb = ebhead;
570 while (i > EXT_BLKSIZ) {
571 if (eb)
572 eb = eb->next;
573 else
574 break;
575 i -= EXT_BLKSIZ;
577 /* if we have an extern decide the type and make a record
579 if (eb) {
580 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
581 nasm_panic(0,
582 "Segment of a rel not supported in ieee_write_fixup");
583 } else {
584 /* If we want the segment */
585 s.ftype = FT_EXTSEG;
586 s.addend = 0;
587 s.id1 = eb->index[i];
590 } else
591 /* If we get here the seg value doesn't make sense */
592 nasm_panic(0,
593 "unrecognised segment value in ieee_write_fixup");
596 } else {
597 /* Assume we are offsetting directly from a section
598 * So look up the target segment
600 for (target = seghead; target; target = target->next)
601 if (target->index == segment)
602 break;
603 if (target) {
604 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
605 /* PC rel to a known offset */
606 s.id1 = target->ieee_index;
607 s.ftype = FT_REL;
608 s.size = size;
609 s.addend = offset;
610 } else {
611 /* We were offsetting from a seg */
612 s.id1 = target->ieee_index;
613 s.ftype = FT_OFS;
614 s.size = size;
615 s.addend = offset;
617 } else {
619 * Now we assume the segment field is being used
620 * to hold an extern index
622 int32_t i = segment / 2;
623 struct ExtBack *eb = ebhead;
624 while (i > EXT_BLKSIZ) {
625 if (eb)
626 eb = eb->next;
627 else
628 break;
629 i -= EXT_BLKSIZ;
631 /* if we have an extern decide the type and make a record
633 if (eb) {
634 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
635 s.ftype = FT_EXTREL;
636 s.addend = 0;
637 s.id1 = eb->index[i];
638 } else {
639 /* else we want the external offset */
640 s.ftype = FT_EXT;
641 s.addend = 0;
642 s.id1 = eb->index[i];
645 } else
646 /* If we get here the seg value doesn't make sense */
647 nasm_panic(0,
648 "unrecognised segment value in ieee_write_fixup");
651 if (size != 2 && s.ftype == FT_SEG)
652 nasm_error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
653 " segment base references");
654 s.size = size;
655 ieee_install_fixup(segto, &s);
656 return;
658 /* should never get here */
660 static void ieee_install_fixup(struct ieeeSection *seg,
661 struct ieeeFixupp *fix)
663 struct ieeeFixupp *f;
664 f = nasm_malloc(sizeof(struct ieeeFixupp));
665 memcpy(f, fix, sizeof(struct ieeeFixupp));
666 f->offset = seg->currentpos;
667 seg->currentpos += fix->size;
668 f->next = NULL;
669 if (seg->fptr)
670 seg->flptr = seg->flptr->next = f;
671 else
672 seg->fptr = seg->flptr = f;
677 * segment registry
679 static int32_t ieee_segment(char *name, int pass, int *bits)
682 * We call the label manager here to define a name for the new
683 * segment, and when our _own_ label-definition stub gets
684 * called in return, it should register the new segment name
685 * using the pointer it gets passed. That way we save memory,
686 * by sponging off the label manager.
688 if (!name) {
689 *bits = 16;
690 if (!any_segs)
691 return 0;
692 return seghead->index;
693 } else {
694 struct ieeeSection *seg;
695 int ieee_idx, attrs;
696 bool rn_error;
697 char *p;
700 * Look for segment attributes.
702 attrs = 0;
703 while (*name == '.')
704 name++; /* hack, but a documented one */
705 p = name;
706 while (*p && !nasm_isspace(*p))
707 p++;
708 if (*p) {
709 *p++ = '\0';
710 while (*p && nasm_isspace(*p))
711 *p++ = '\0';
713 while (*p) {
714 while (*p && !nasm_isspace(*p))
715 p++;
716 if (*p) {
717 *p++ = '\0';
718 while (*p && nasm_isspace(*p))
719 *p++ = '\0';
722 attrs++;
725 ieee_idx = 1;
726 for (seg = seghead; seg; seg = seg->next) {
727 ieee_idx++;
728 if (!strcmp(seg->name, name)) {
729 if (attrs > 0 && pass == 1)
730 nasm_error(ERR_WARNING, "segment attributes specified on"
731 " redeclaration of segment: ignoring");
732 if (seg->use32)
733 *bits = 32;
734 else
735 *bits = 16;
736 return seg->index;
740 *segtail = seg = nasm_malloc(sizeof(*seg));
741 seg->next = NULL;
742 segtail = &seg->next;
743 seg->index = seg_alloc();
744 seg->ieee_index = ieee_idx;
745 any_segs = true;
746 seg->name = NULL;
747 seg->currentpos = 0;
748 seg->align = 1; /* default */
749 seg->use32 = *bits == 32; /* default to user spec */
750 seg->combine = CMB_PUBLIC; /* default */
751 seg->pubhead = NULL;
752 seg->pubtail = &seg->pubhead;
753 seg->data = NULL;
754 seg->fptr = NULL;
755 seg->lochead = NULL;
756 seg->loctail = &seg->lochead;
759 * Process the segment attributes.
761 p = name;
762 while (attrs--) {
763 p += strlen(p);
764 while (!*p)
765 p++;
768 * `p' contains a segment attribute.
770 if (!nasm_stricmp(p, "private"))
771 seg->combine = CMB_PRIVATE;
772 else if (!nasm_stricmp(p, "public"))
773 seg->combine = CMB_PUBLIC;
774 else if (!nasm_stricmp(p, "common"))
775 seg->combine = CMB_COMMON;
776 else if (!nasm_stricmp(p, "use16"))
777 seg->use32 = false;
778 else if (!nasm_stricmp(p, "use32"))
779 seg->use32 = true;
780 else if (!nasm_strnicmp(p, "align=", 6)) {
781 seg->align = readnum(p + 6, &rn_error);
782 if (seg->align == 0)
783 seg->align = 1;
784 if (rn_error) {
785 seg->align = 1;
786 nasm_error(ERR_NONFATAL, "segment alignment should be"
787 " numeric");
789 switch ((int)seg->align) {
790 case 1: /* BYTE */
791 case 2: /* WORD */
792 case 4: /* DWORD */
793 case 16: /* PARA */
794 case 256: /* PAGE */
795 case 8:
796 case 32:
797 case 64:
798 case 128:
799 break;
800 default:
801 nasm_error(ERR_NONFATAL, "invalid alignment value %d",
802 seg->align);
803 seg->align = 1;
804 break;
806 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
807 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
808 if (rn_error)
809 nasm_error(ERR_NONFATAL, "argument to `absolute' segment"
810 " attribute should be numeric");
814 ieee_seg_needs_update = seg;
815 if (seg->align >= SEG_ABS)
816 define_label(name, NO_SEG, seg->align - SEG_ABS,
817 NULL, false, false);
818 else
819 define_label(name, seg->index + 1, 0L, NULL, false, false);
820 ieee_seg_needs_update = NULL;
822 if (seg->use32)
823 *bits = 32;
824 else
825 *bits = 16;
826 return seg->index;
831 * directives supported
833 static int ieee_directive(enum directives directive, char *value, int pass)
836 (void)value;
837 (void)pass;
839 switch (directive) {
840 case D_UPPERCASE:
841 ieee_uppercase = true;
842 return 1;
844 default:
845 return 0;
849 static void ieee_sectalign(int32_t seg, unsigned int value)
851 struct ieeeSection *s;
853 list_for_each(s, seghead) {
854 if (s->index == seg)
855 break;
859 * 256 is maximum there, note it may happen
860 * that align is issued on "absolute" segment
861 * it's fine since SEG_ABS > 256 and we never
862 * get escape this test
864 if (!s || !is_power2(value) || value > 256)
865 return;
867 if ((unsigned int)s->align < value)
868 s->align = value;
872 * Return segment data
874 static int32_t ieee_segbase(int32_t segment)
876 struct ieeeSection *seg;
879 * Find the segment in our list.
881 for (seg = seghead; seg; seg = seg->next)
882 if (seg->index == segment - 1)
883 break;
885 if (!seg)
886 return segment; /* not one of ours - leave it alone */
888 if (seg->align >= SEG_ABS)
889 return seg->align; /* absolute segment */
891 return segment; /* no special treatment */
895 * filename
897 static void ieee_filename(char *inname, char *outname)
899 strcpy(ieee_infile, inname);
900 standard_extension(inname, outname, ".o");
903 static void ieee_write_file(void)
905 struct tm *thetime;
906 time_t reltime;
907 struct FileName *fn;
908 struct ieeeSection *seg;
909 struct ieeePublic *pub, *loc;
910 struct ieeeExternal *ext;
911 struct ieeeObjData *data;
912 struct ieeeFixupp *fix;
913 struct Array *arr;
914 int i;
915 const bool debuginfo = (dfmt == &ladsoft_debug_form);
918 * Write the module header
920 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile), ieee_infile);
923 * Write the NASM boast comment.
925 ieee_putascii("CO0,%02X%s.\n", strlen(nasm_comment), nasm_comment);
928 * write processor-specific information
930 ieee_putascii("AD8,4,L.\n");
933 * date and time
935 time(&reltime);
936 thetime = localtime(&reltime);
937 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
938 1900 + thetime->tm_year, thetime->tm_mon + 1,
939 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
940 thetime->tm_sec);
942 * if debugging, dump file names
944 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
945 ieee_putascii("C0105,%02X%s.\n", strlen(fn->name), fn->name);
948 ieee_putascii("CO101,07ENDHEAD.\n");
950 * the standard doesn't specify when to put checksums,
951 * we'll just do it periodically.
953 ieee_putcs(false);
956 * Write the section headers
958 seg = seghead;
959 if (!debuginfo && !strcmp(seg->name, "??LINE"))
960 seg = seg->next;
961 while (seg) {
962 char buf[256];
963 char attrib;
964 switch (seg->combine) {
965 case CMB_PUBLIC:
966 default:
967 attrib = 'C';
968 break;
969 case CMB_PRIVATE:
970 attrib = 'S';
971 break;
972 case CMB_COMMON:
973 attrib = 'M';
974 break;
976 ieee_unqualified_name(buf, seg->name);
977 if (seg->align >= SEG_ABS) {
978 ieee_putascii("ST%X,A,%02X%s.\n", seg->ieee_index,
979 strlen(buf), buf);
980 ieee_putascii("ASL%X,%lX.\n", seg->ieee_index,
981 (seg->align - SEG_ABS) * 16);
982 } else {
983 ieee_putascii("ST%X,%c,%02X%s.\n", seg->ieee_index, attrib,
984 strlen(buf), buf);
985 ieee_putascii("SA%X,%lX.\n", seg->ieee_index, seg->align);
986 ieee_putascii("ASS%X,%X.\n", seg->ieee_index,
987 seg->currentpos);
989 seg = seg->next;
992 * write the start address if there is one
994 if (ieee_entry_seg) {
995 for (seg = seghead; seg; seg = seg->next)
996 if (seg->index == ieee_entry_seg)
997 break;
998 if (!seg)
999 nasm_panic(0, "Start address records are incorrect");
1000 else
1001 ieee_putascii("ASG,R%X,%lX,+.\n", seg->ieee_index,
1002 ieee_entry_ofs);
1005 ieee_putcs(false);
1007 * Write the publics
1009 i = 1;
1010 for (seg = seghead; seg; seg = seg->next) {
1011 for (pub = seg->pubhead; pub; pub = pub->next) {
1012 char buf[256];
1013 ieee_unqualified_name(buf, pub->name);
1014 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1015 if (pub->segment == -1)
1016 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1017 pub->offset);
1018 else
1019 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1020 pub->offset);
1021 if (debuginfo) {
1022 if (pub->type >= 0x100)
1023 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1024 else
1025 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1027 i++;
1030 pub = fpubhead;
1031 i = 1;
1032 while (pub) {
1033 char buf[256];
1034 ieee_unqualified_name(buf, pub->name);
1035 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1036 if (pub->segment == -1)
1037 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1038 pub->offset);
1039 else
1040 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1041 pub->offset);
1042 if (debuginfo) {
1043 if (pub->type >= 0x100)
1044 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1045 else
1046 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1048 i++;
1049 pub = pub->next;
1052 * Write the externals
1054 ext = exthead;
1055 i = 1;
1056 while (ext) {
1057 char buf[256];
1058 ieee_unqualified_name(buf, ext->name);
1059 ieee_putascii("NX%X,%02X%s.\n", i++, strlen(buf), buf);
1060 ext = ext->next;
1062 ieee_putcs(false);
1065 * IEEE doesn't have a standard pass break record
1066 * so use the ladsoft variant
1068 ieee_putascii("CO100,06ENDSYM.\n");
1071 * now put types
1073 i = ARRAY_BOT;
1074 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1075 ieee_putascii("TY%X,20,%X,%lX.\n", i++, arr->basetype,
1076 arr->size);
1079 * now put locals
1081 i = 1;
1082 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1083 for (loc = seg->lochead; loc; loc = loc->next) {
1084 char buf[256];
1085 ieee_unqualified_name(buf, loc->name);
1086 ieee_putascii("NN%X,%02X%s.\n", i, strlen(buf), buf);
1087 if (loc->segment == -1)
1088 ieee_putascii("ASN%X,R%X,%lX,+.\n", i, loc->index,
1089 loc->offset);
1090 else
1091 ieee_putascii("ASN%X,%lX,%lX,+.\n", i, loc->segment * 16,
1092 loc->offset);
1093 if (debuginfo) {
1094 if (loc->type >= 0x100)
1095 ieee_putascii("ATN%X,T%X.\n", i, loc->type - 0x100);
1096 else
1097 ieee_putascii("ATN%X,%X.\n", i, loc->type);
1099 i++;
1104 * put out section data;
1106 seg = seghead;
1107 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1108 seg = seg->next;
1109 while (seg) {
1110 if (seg->currentpos) {
1111 int32_t size, org = 0;
1112 data = seg->data;
1113 ieee_putascii("SB%X.\n", seg->ieee_index);
1114 fix = seg->fptr;
1115 while (fix) {
1116 size = HUNKSIZE - (org % HUNKSIZE);
1117 size =
1118 size + org >
1119 seg->currentpos ? seg->currentpos - org : size;
1120 size = fix->offset - org > size ? size : fix->offset - org;
1121 org = ieee_putld(org, org + size, data->data);
1122 if (org % HUNKSIZE == 0)
1123 data = data->next;
1124 if (org == fix->offset) {
1125 org += ieee_putlr(fix);
1126 fix = fix->next;
1129 while (org < seg->currentpos && data) {
1130 size =
1131 seg->currentpos - org >
1132 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1133 org = ieee_putld(org, org + size, data->data);
1134 data = data->next;
1136 ieee_putcs(false);
1139 seg = seg->next;
1142 * module end record
1144 ieee_putascii("ME.\n");
1147 static void ieee_write_byte(struct ieeeSection *seg, int data)
1149 int temp;
1150 if (!(temp = seg->currentpos++ % HUNKSIZE))
1151 ieee_data_new(seg);
1152 seg->datacurr->data[temp] = data;
1155 static void ieee_write_word(struct ieeeSection *seg, int data)
1157 ieee_write_byte(seg, data & 0xFF);
1158 ieee_write_byte(seg, (data >> 8) & 0xFF);
1161 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1163 ieee_write_byte(seg, data & 0xFF);
1164 ieee_write_byte(seg, (data >> 8) & 0xFF);
1165 ieee_write_byte(seg, (data >> 16) & 0xFF);
1166 ieee_write_byte(seg, (data >> 24) & 0xFF);
1168 static void ieee_putascii(char *format, ...)
1170 char buffer[256];
1171 int i, l;
1172 va_list ap;
1174 va_start(ap, format);
1175 vsnprintf(buffer, sizeof(buffer), format, ap);
1176 l = strlen(buffer);
1177 for (i = 0; i < l; i++)
1178 if ((uint8_t)buffer[i] > 31)
1179 checksum += buffer[i];
1180 va_end(ap);
1181 fputs(buffer, ofile);
1185 * put out a checksum record */
1186 static void ieee_putcs(int toclear)
1188 if (toclear) {
1189 ieee_putascii("CS.\n");
1190 } else {
1191 checksum += 'C';
1192 checksum += 'S';
1193 ieee_putascii("CS%02X.\n", checksum & 127);
1195 checksum = 0;
1198 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1200 int32_t val;
1201 if (start == end)
1202 return (start);
1203 val = start % HUNKSIZE;
1204 /* fill up multiple lines */
1205 while (end - start >= LDPERLINE) {
1206 int i;
1207 ieee_putascii("LD");
1208 for (i = 0; i < LDPERLINE; i++) {
1209 ieee_putascii("%02X", buf[val++]);
1210 start++;
1212 ieee_putascii(".\n");
1214 /* if no partial lines */
1215 if (start == end)
1216 return (start);
1217 /* make a partial line */
1218 ieee_putascii("LD");
1219 while (start < end) {
1220 ieee_putascii("%02X", buf[val++]);
1221 start++;
1223 ieee_putascii(".\n");
1224 return (start);
1226 static int32_t ieee_putlr(struct ieeeFixupp *p)
1229 * To deal with the vagaries of segmentation the LADsoft linker
1230 * defines two types of segments: absolute and virtual. Note that
1231 * 'absolute' in this context is a different thing from the IEEE
1232 * definition of an absolute segment type, which is also supported. If a
1233 * sement is linked in virtual mode the low limit (L-var) is
1234 * subtracted from each R,X, and P variable which appears in an
1235 * expression, so that we can have relative offsets. Meanwhile
1236 * in the ABSOLUTE mode this subtraction is not done and
1237 * so we can use absolute offsets from 0. In the LADsoft linker
1238 * this configuration is not done in the assemblker source but in
1239 * a source the linker reads. Generally this type of thing only
1240 * becomes an issue if real mode code is used. A pure 32-bit linker could
1241 * get away without defining the virtual mode...
1243 char buf[40];
1244 int32_t size = p->size;
1245 switch (p->ftype) {
1246 case FT_SEG:
1247 if (p->id1 < 0)
1248 sprintf(buf, "%"PRIX32"", -p->id1);
1249 else
1250 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1251 break;
1252 case FT_OFS:
1253 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1254 break;
1255 case FT_REL:
1256 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1257 break;
1259 case FT_WRT:
1260 if (p->id2 < 0)
1261 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1262 p->id2, -p->id1 * 16);
1263 else
1264 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1265 p->id2, p->id1);
1266 break;
1267 case FT_EXT:
1268 sprintf(buf, "X%"PRIX32"", p->id1);
1269 break;
1270 case FT_EXTREL:
1271 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1272 break;
1273 case FT_EXTSEG:
1274 /* We needed a non-ieee hack here.
1275 * We introduce the Y variable, which is the low
1276 * limit of the native segment the extern resides in
1278 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1279 break;
1280 case FT_EXTWRT:
1281 if (p->id2 < 0)
1282 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1283 -p->id1 * 16);
1284 else
1285 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1286 break;
1288 ieee_putascii("LR(%s,%"PRIX32").\n", buf, size);
1290 return (size);
1293 /* Dump all segment data (text and fixups )*/
1295 static void ieee_unqualified_name(char *dest, char *source)
1297 if (ieee_uppercase) {
1298 while (*source)
1299 *dest++ = toupper(*source++);
1300 *dest = 0;
1301 } else
1302 strcpy(dest, source);
1304 static void dbgls_init(void)
1306 int tempint;
1308 fnhead = NULL;
1309 fntail = &fnhead;
1310 arrindex = ARRAY_BOT;
1311 arrhead = NULL;
1312 arrtail = &arrhead;
1313 ieee_segment("??LINE", 2, &tempint);
1314 any_segs = false;
1316 static void dbgls_cleanup(void)
1318 struct ieeeSection *segtmp;
1319 while (fnhead) {
1320 struct FileName *fntemp = fnhead;
1321 fnhead = fnhead->next;
1322 nasm_free(fntemp->name);
1323 nasm_free(fntemp);
1325 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1326 while (segtmp->lochead) {
1327 struct ieeePublic *loctmp = segtmp->lochead;
1328 segtmp->lochead = loctmp->next;
1329 nasm_free(loctmp->name);
1330 nasm_free(loctmp);
1333 while (arrhead) {
1334 struct Array *arrtmp = arrhead;
1335 arrhead = arrhead->next;
1336 nasm_free(arrtmp);
1341 * because this routine is not bracketed in
1342 * the main program, this routine will be called even if there
1343 * is no request for debug info
1344 * so, we have to make sure the ??LINE segment is avaialbe
1345 * as the first segment when this debug format is selected
1347 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1349 struct FileName *fn;
1350 struct ieeeSection *seg;
1351 int i = 0;
1352 if (segto == NO_SEG)
1353 return;
1356 * If `any_segs' is still false, we must define a default
1357 * segment.
1359 if (!any_segs) {
1360 int tempint; /* ignored */
1361 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1362 nasm_panic(0, "strange segment conditions in OBJ driver");
1366 * Find the segment we are targetting.
1368 for (seg = seghead; seg; seg = seg->next)
1369 if (seg->index == segto)
1370 break;
1371 if (!seg)
1372 nasm_panic(0, "lineno directed to nonexistent segment?");
1374 for (fn = fnhead; fn; fn = fn->next) {
1375 if (!nasm_stricmp(lnfname, fn->name))
1376 break;
1377 i++;
1379 if (!fn) {
1380 fn = nasm_malloc(sizeof(*fn));
1381 fn->name = nasm_malloc(strlen(lnfname) + 1);
1382 fn->index = i;
1383 strcpy(fn->name, lnfname);
1384 fn->next = NULL;
1385 *fntail = fn;
1386 fntail = &fn->next;
1388 ieee_write_byte(seghead, fn->index);
1389 ieee_write_word(seghead, lineno);
1390 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1391 seg->currentpos);
1394 static void dbgls_deflabel(char *name, int32_t segment,
1395 int64_t offset, int is_global, char *special)
1397 struct ieeeSection *seg;
1399 /* Keep compiler from warning about special */
1400 (void)special;
1403 * Note: ..[^@] special symbols are filtered in labels.c
1407 * If it's a special-retry from pass two, discard it.
1409 if (is_global == 3)
1410 return;
1413 * Case (i):
1415 if (ieee_seg_needs_update)
1416 return;
1417 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
1418 return;
1420 if (segment >= SEG_ABS || segment == NO_SEG) {
1421 return;
1425 * If `any_segs' is still false, we might need to define a
1426 * default segment, if they're trying to declare a label in
1427 * `first_seg'. But the label should exist due to a prior
1428 * call to ieee_deflabel so we can skip that.
1431 for (seg = seghead; seg; seg = seg->next)
1432 if (seg->index == segment) {
1433 struct ieeePublic *loc;
1435 * Case (ii). Maybe MODPUB someday?
1437 if (!is_global) {
1438 last_defined = loc = nasm_malloc(sizeof(*loc));
1439 *seg->loctail = loc;
1440 seg->loctail = &loc->next;
1441 loc->next = NULL;
1442 loc->name = nasm_strdup(name);
1443 loc->offset = offset;
1444 loc->segment = -1;
1445 loc->index = seg->ieee_index;
1449 static void dbgls_typevalue(int32_t type)
1451 int elem = TYM_ELEMENTS(type);
1452 type = TYM_TYPE(type);
1454 if (!last_defined)
1455 return;
1457 switch (type) {
1458 case TY_BYTE:
1459 last_defined->type = 1; /* uint8_t */
1460 break;
1461 case TY_WORD:
1462 last_defined->type = 3; /* unsigned word */
1463 break;
1464 case TY_DWORD:
1465 last_defined->type = 5; /* unsigned dword */
1466 break;
1467 case TY_FLOAT:
1468 last_defined->type = 9; /* float */
1469 break;
1470 case TY_QWORD:
1471 last_defined->type = 10; /* qword */
1472 break;
1473 case TY_TBYTE:
1474 last_defined->type = 11; /* TBYTE */
1475 break;
1476 default:
1477 last_defined->type = 0x10; /* near label */
1478 break;
1481 if (elem > 1) {
1482 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
1483 int vtype = last_defined->type;
1484 arrtmp->size = elem;
1485 arrtmp->basetype = vtype;
1486 arrtmp->next = NULL;
1487 last_defined->type = arrindex++ + 0x100;
1488 *arrtail = arrtmp;
1489 arrtail = &(arrtmp->next);
1491 last_defined = NULL;
1493 static void dbgls_output(int output_type, void *param)
1495 (void)output_type;
1496 (void)param;
1498 static struct dfmt ladsoft_debug_form = {
1499 "LADsoft Debug Records",
1500 "ladsoft",
1501 dbgls_init,
1502 dbgls_linnum,
1503 dbgls_deflabel,
1504 null_debug_directive,
1505 dbgls_typevalue,
1506 dbgls_output,
1507 dbgls_cleanup,
1509 static struct dfmt *ladsoft_debug_arr[3] = {
1510 &ladsoft_debug_form,
1511 &null_debug_form,
1512 NULL
1514 struct ofmt of_ieee = {
1515 "IEEE-695 (LADsoft variant) object file format",
1516 "ieee",
1517 OFMT_TEXT,
1519 ladsoft_debug_arr,
1520 &ladsoft_debug_form,
1521 NULL,
1522 ieee_init,
1523 ieee_set_info,
1524 ieee_out,
1525 ieee_deflabel,
1526 ieee_segment,
1527 ieee_sectalign,
1528 ieee_segbase,
1529 ieee_directive,
1530 ieee_filename,
1531 ieee_cleanup
1534 #endif /* OF_IEEE */