output: remove ABSOLUTE handling, OUT_RAWDATA asserts
[nasm.git] / output / outieee.c
blobf2ff61f14ea079afadcd76aacd47e8769d7af939
1 /* ----------------------------------------------------------------------- *
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 strlcpy(ieee_infile, inname, sizeof(ieee_infile));
213 any_segs = false;
214 fpubhead = NULL;
215 fpubtail = &fpubhead;
216 exthead = NULL;
217 exttail = &exthead;
218 externals = 1;
219 ebhead = NULL;
220 ebtail = &ebhead;
221 seghead = ieee_seg_needs_update = NULL;
222 segtail = &seghead;
223 ieee_entry_seg = NO_SEG;
224 ieee_uppercase = false;
225 checksum = 0;
229 * Rundown
231 static void ieee_cleanup(void)
233 ieee_write_file();
234 dfmt->cleanup();
235 while (seghead) {
236 struct ieeeSection *segtmp = seghead;
237 seghead = seghead->next;
238 while (segtmp->pubhead) {
239 struct ieeePublic *pubtmp = segtmp->pubhead;
240 segtmp->pubhead = pubtmp->next;
241 nasm_free(pubtmp);
243 while (segtmp->fptr) {
244 struct ieeeFixupp *fixtmp = segtmp->fptr;
245 segtmp->fptr = fixtmp->next;
246 nasm_free(fixtmp);
248 while (segtmp->data) {
249 struct ieeeObjData *dattmp = segtmp->data;
250 segtmp->data = dattmp->next;
251 nasm_free(dattmp);
253 nasm_free(segtmp);
255 while (fpubhead) {
256 struct ieeePublic *pubtmp = fpubhead;
257 fpubhead = fpubhead->next;
258 nasm_free(pubtmp);
260 while (exthead) {
261 struct ieeeExternal *exttmp = exthead;
262 exthead = exthead->next;
263 nasm_free(exttmp);
265 while (ebhead) {
266 struct ExtBack *ebtmp = ebhead;
267 ebhead = ebhead->next;
268 nasm_free(ebtmp);
273 * callback for labels
275 static void ieee_deflabel(char *name, int32_t segment,
276 int64_t offset, int is_global, char *special)
279 * We have three cases:
281 * (i) `segment' is a segment-base. If so, set the name field
282 * for the segment structure it refers to, and then
283 * return.
285 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
286 * Save the label position for later output of a PUBDEF record.
289 * (iii) `segment' is not one of our segments. Save the label
290 * position for later output of an EXTDEF.
292 struct ieeeExternal *ext;
293 struct ExtBack *eb;
294 struct ieeeSection *seg;
295 int i;
297 if (special) {
298 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
301 * First check for the double-period, signifying something
302 * unusual.
304 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
305 if (!strcmp(name, "..start")) {
306 ieee_entry_seg = segment;
307 ieee_entry_ofs = offset;
309 return;
313 * Case (i):
315 if (ieee_seg_needs_update) {
316 ieee_seg_needs_update->name = name;
317 return;
319 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
320 return;
323 * case (ii)
325 if (segment >= SEG_ABS) {
327 * SEG_ABS subcase of (ii).
329 if (is_global) {
330 struct ieeePublic *pub;
332 pub = *fpubtail = nasm_malloc(sizeof(*pub));
333 fpubtail = &pub->next;
334 pub->next = NULL;
335 pub->name = name;
336 pub->offset = offset;
337 pub->segment = segment & ~SEG_ABS;
339 return;
342 for (seg = seghead; seg && is_global; seg = seg->next)
343 if (seg->index == segment) {
344 struct ieeePublic *pub;
346 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
347 seg->pubtail = &pub->next;
348 pub->next = NULL;
349 pub->name = name;
350 pub->offset = offset;
351 pub->index = seg->ieee_index;
352 pub->segment = -1;
353 return;
357 * Case (iii).
359 if (is_global) {
360 ext = *exttail = nasm_malloc(sizeof(*ext));
361 ext->next = NULL;
362 exttail = &ext->next;
363 ext->name = name;
364 if (is_global == 2)
365 ext->commonsize = offset;
366 else
367 ext->commonsize = 0;
368 i = segment / 2;
369 eb = ebhead;
370 if (!eb) {
371 eb = *ebtail = nasm_zalloc(sizeof(*eb));
372 eb->next = NULL;
373 ebtail = &eb->next;
375 while (i > EXT_BLKSIZ) {
376 if (eb && eb->next)
377 eb = eb->next;
378 else {
379 eb = *ebtail = nasm_zalloc(sizeof(*eb));
380 eb->next = NULL;
381 ebtail = &eb->next;
383 i -= EXT_BLKSIZ;
385 eb->index[i] = externals++;
391 * Put data out
393 static void ieee_out(int32_t segto, const void *data,
394 enum out_type type, uint64_t size,
395 int32_t segment, int32_t wrt)
397 const uint8_t *ucdata;
398 int32_t ldata;
399 struct ieeeSection *seg;
402 * If `any_segs' is still false, we must define a default
403 * segment.
405 if (!any_segs) {
406 int tempint; /* ignored */
407 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
408 nasm_panic(0, "strange segment conditions in IEEE driver");
412 * Find the segment we are targetting.
414 for (seg = seghead; seg; seg = seg->next)
415 if (seg->index == segto)
416 break;
417 if (!seg)
418 nasm_panic(0, "code directed to nonexistent segment?");
420 if (type == OUT_RAWDATA) {
421 ucdata = data;
422 while (size--)
423 ieee_write_byte(seg, *ucdata++);
424 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
425 type == OUT_REL4ADR) {
426 if (type == OUT_ADDRESS)
427 size = abs((int)size);
428 else if (segment == NO_SEG)
429 nasm_error(ERR_NONFATAL, "relative call to absolute address not"
430 " supported by IEEE format");
431 ldata = *(int64_t *)data;
432 if (type == OUT_REL2ADR)
433 ldata += (size - 2);
434 if (type == OUT_REL4ADR)
435 ldata += (size - 4);
436 ieee_write_fixup(segment, wrt, seg, size, type, ldata);
437 if (size == 2)
438 ieee_write_word(seg, ldata);
439 else
440 ieee_write_dword(seg, ldata);
441 } else if (type == OUT_RESERVE) {
442 while (size--)
443 ieee_write_byte(seg, 0);
447 static void ieee_data_new(struct ieeeSection *segto)
450 if (!segto->data)
451 segto->data = segto->datacurr =
452 nasm_malloc(sizeof(*(segto->datacurr)));
453 else
454 segto->datacurr = segto->datacurr->next =
455 nasm_malloc(sizeof(*(segto->datacurr)));
456 segto->datacurr->next = NULL;
460 * this routine is unalduterated bloatware. I usually don't do this
461 * but I might as well see what it is like on a harmless program.
462 * If anyone wants to optimize this is a good canditate!
464 static void ieee_write_fixup(int32_t segment, int32_t wrt,
465 struct ieeeSection *segto, int size,
466 uint64_t realtype, int32_t offset)
468 struct ieeeSection *target;
469 struct ieeeFixupp s;
471 /* Don't put a fixup for things NASM can calculate */
472 if (wrt == NO_SEG && segment == NO_SEG)
473 return;
475 s.ftype = -1;
476 /* if it is a WRT offset */
477 if (wrt != NO_SEG) {
478 s.ftype = FT_WRT;
479 s.addend = offset;
480 if (wrt >= SEG_ABS)
481 s.id1 = -(wrt - SEG_ABS);
482 else {
483 if (wrt % 2 && realtype != OUT_REL2ADR
484 && realtype != OUT_REL4ADR) {
485 wrt--;
487 for (target = seghead; target; target = target->next)
488 if (target->index == wrt)
489 break;
490 if (target) {
491 s.id1 = target->ieee_index;
492 for (target = seghead; target; target = target->next)
493 if (target->index == segment)
494 break;
496 if (target)
497 s.id2 = target->ieee_index;
498 else {
500 * Now we assume the segment field is being used
501 * to hold an extern index
503 int32_t i = segment / 2;
504 struct ExtBack *eb = ebhead;
505 while (i > EXT_BLKSIZ) {
506 if (eb)
507 eb = eb->next;
508 else
509 break;
510 i -= EXT_BLKSIZ;
512 /* if we have an extern decide the type and make a record
514 if (eb) {
515 s.ftype = FT_EXTWRT;
516 s.addend = 0;
517 s.id2 = eb->index[i];
518 } else
519 nasm_error(ERR_NONFATAL,
520 "Source of WRT must be an offset");
523 } else
524 nasm_panic(0,
525 "unrecognised WRT value in ieee_write_fixup");
526 } else
527 nasm_error(ERR_NONFATAL, "target of WRT must be a section ");
529 s.size = size;
530 ieee_install_fixup(segto, &s);
531 return;
533 /* Pure segment fixup ? */
534 if (segment != NO_SEG) {
535 s.ftype = FT_SEG;
536 s.id1 = 0;
537 if (segment >= SEG_ABS) {
538 /* absolute far segment fixup */
539 s.id1 = -(segment - ~SEG_ABS);
540 } else if (segment % 2) {
541 /* fixup to named segment */
542 /* look it up */
543 for (target = seghead; target; target = target->next)
544 if (target->index == segment - 1)
545 break;
546 if (target)
547 s.id1 = target->ieee_index;
548 else {
550 * Now we assume the segment field is being used
551 * to hold an extern index
553 int32_t i = segment / 2;
554 struct ExtBack *eb = ebhead;
555 while (i > EXT_BLKSIZ) {
556 if (eb)
557 eb = eb->next;
558 else
559 break;
560 i -= EXT_BLKSIZ;
562 /* if we have an extern decide the type and make a record
564 if (eb) {
565 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
566 nasm_panic(0,
567 "Segment of a rel not supported in ieee_write_fixup");
568 } else {
569 /* If we want the segment */
570 s.ftype = FT_EXTSEG;
571 s.addend = 0;
572 s.id1 = eb->index[i];
575 } else
576 /* If we get here the seg value doesn't make sense */
577 nasm_panic(0,
578 "unrecognised segment value in ieee_write_fixup");
581 } else {
582 /* Assume we are offsetting directly from a section
583 * So look up the target segment
585 for (target = seghead; target; target = target->next)
586 if (target->index == segment)
587 break;
588 if (target) {
589 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
590 /* PC rel to a known offset */
591 s.id1 = target->ieee_index;
592 s.ftype = FT_REL;
593 s.size = size;
594 s.addend = offset;
595 } else {
596 /* We were offsetting from a seg */
597 s.id1 = target->ieee_index;
598 s.ftype = FT_OFS;
599 s.size = size;
600 s.addend = offset;
602 } else {
604 * Now we assume the segment field is being used
605 * to hold an extern index
607 int32_t i = segment / 2;
608 struct ExtBack *eb = ebhead;
609 while (i > EXT_BLKSIZ) {
610 if (eb)
611 eb = eb->next;
612 else
613 break;
614 i -= EXT_BLKSIZ;
616 /* if we have an extern decide the type and make a record
618 if (eb) {
619 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
620 s.ftype = FT_EXTREL;
621 s.addend = 0;
622 s.id1 = eb->index[i];
623 } else {
624 /* else we want the external offset */
625 s.ftype = FT_EXT;
626 s.addend = 0;
627 s.id1 = eb->index[i];
630 } else
631 /* If we get here the seg value doesn't make sense */
632 nasm_panic(0,
633 "unrecognised segment value in ieee_write_fixup");
636 if (size != 2 && s.ftype == FT_SEG)
637 nasm_error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
638 " segment base references");
639 s.size = size;
640 ieee_install_fixup(segto, &s);
641 return;
643 /* should never get here */
645 static void ieee_install_fixup(struct ieeeSection *seg,
646 struct ieeeFixupp *fix)
648 struct ieeeFixupp *f;
649 f = nasm_malloc(sizeof(struct ieeeFixupp));
650 memcpy(f, fix, sizeof(struct ieeeFixupp));
651 f->offset = seg->currentpos;
652 seg->currentpos += fix->size;
653 f->next = NULL;
654 if (seg->fptr)
655 seg->flptr = seg->flptr->next = f;
656 else
657 seg->fptr = seg->flptr = f;
662 * segment registry
664 static int32_t ieee_segment(char *name, int pass, int *bits)
667 * We call the label manager here to define a name for the new
668 * segment, and when our _own_ label-definition stub gets
669 * called in return, it should register the new segment name
670 * using the pointer it gets passed. That way we save memory,
671 * by sponging off the label manager.
673 if (!name) {
674 *bits = 16;
675 if (!any_segs)
676 return 0;
677 return seghead->index;
678 } else {
679 struct ieeeSection *seg;
680 int ieee_idx, attrs;
681 bool rn_error;
682 char *p;
685 * Look for segment attributes.
687 attrs = 0;
688 while (*name == '.')
689 name++; /* hack, but a documented one */
690 p = name;
691 while (*p && !nasm_isspace(*p))
692 p++;
693 if (*p) {
694 *p++ = '\0';
695 while (*p && nasm_isspace(*p))
696 *p++ = '\0';
698 while (*p) {
699 while (*p && !nasm_isspace(*p))
700 p++;
701 if (*p) {
702 *p++ = '\0';
703 while (*p && nasm_isspace(*p))
704 *p++ = '\0';
707 attrs++;
710 ieee_idx = 1;
711 for (seg = seghead; seg; seg = seg->next) {
712 ieee_idx++;
713 if (!strcmp(seg->name, name)) {
714 if (attrs > 0 && pass == 1)
715 nasm_error(ERR_WARNING, "segment attributes specified on"
716 " redeclaration of segment: ignoring");
717 if (seg->use32)
718 *bits = 32;
719 else
720 *bits = 16;
721 return seg->index;
725 *segtail = seg = nasm_malloc(sizeof(*seg));
726 seg->next = NULL;
727 segtail = &seg->next;
728 seg->index = seg_alloc();
729 seg->ieee_index = ieee_idx;
730 any_segs = true;
731 seg->name = NULL;
732 seg->currentpos = 0;
733 seg->align = 1; /* default */
734 seg->use32 = *bits == 32; /* default to user spec */
735 seg->combine = CMB_PUBLIC; /* default */
736 seg->pubhead = NULL;
737 seg->pubtail = &seg->pubhead;
738 seg->data = NULL;
739 seg->fptr = NULL;
740 seg->lochead = NULL;
741 seg->loctail = &seg->lochead;
744 * Process the segment attributes.
746 p = name;
747 while (attrs--) {
748 p += strlen(p);
749 while (!*p)
750 p++;
753 * `p' contains a segment attribute.
755 if (!nasm_stricmp(p, "private"))
756 seg->combine = CMB_PRIVATE;
757 else if (!nasm_stricmp(p, "public"))
758 seg->combine = CMB_PUBLIC;
759 else if (!nasm_stricmp(p, "common"))
760 seg->combine = CMB_COMMON;
761 else if (!nasm_stricmp(p, "use16"))
762 seg->use32 = false;
763 else if (!nasm_stricmp(p, "use32"))
764 seg->use32 = true;
765 else if (!nasm_strnicmp(p, "align=", 6)) {
766 seg->align = readnum(p + 6, &rn_error);
767 if (seg->align == 0)
768 seg->align = 1;
769 if (rn_error) {
770 seg->align = 1;
771 nasm_error(ERR_NONFATAL, "segment alignment should be"
772 " numeric");
774 switch (seg->align) {
775 case 1: /* BYTE */
776 case 2: /* WORD */
777 case 4: /* DWORD */
778 case 16: /* PARA */
779 case 256: /* PAGE */
780 case 8:
781 case 32:
782 case 64:
783 case 128:
784 break;
785 default:
786 nasm_error(ERR_NONFATAL, "invalid alignment value %d",
787 seg->align);
788 seg->align = 1;
789 break;
791 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
792 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
793 if (rn_error)
794 nasm_error(ERR_NONFATAL, "argument to `absolute' segment"
795 " attribute should be numeric");
799 ieee_seg_needs_update = seg;
800 if (seg->align >= SEG_ABS)
801 define_label(name, NO_SEG, seg->align - SEG_ABS, false);
802 else
803 define_label(name, seg->index + 1, 0L, false);
804 ieee_seg_needs_update = NULL;
806 if (seg->use32)
807 *bits = 32;
808 else
809 *bits = 16;
810 return seg->index;
815 * directives supported
817 static enum directive_result
818 ieee_directive(enum directive directive, char *value, int pass)
821 (void)value;
822 (void)pass;
824 switch (directive) {
825 case D_UPPERCASE:
826 ieee_uppercase = true;
827 return DIRR_OK;
829 default:
830 return DIRR_UNKNOWN;
834 static void ieee_sectalign(int32_t seg, unsigned int value)
836 struct ieeeSection *s;
838 list_for_each(s, seghead) {
839 if (s->index == seg)
840 break;
844 * 256 is maximum there, note it may happen
845 * that align is issued on "absolute" segment
846 * it's fine since SEG_ABS > 256 and we never
847 * get escape this test
849 if (!s || !is_power2(value) || value > 256)
850 return;
852 if ((unsigned int)s->align < value)
853 s->align = value;
857 * Return segment data
859 static int32_t ieee_segbase(int32_t segment)
861 struct ieeeSection *seg;
864 * Find the segment in our list.
866 for (seg = seghead; seg; seg = seg->next)
867 if (seg->index == segment - 1)
868 break;
870 if (!seg)
871 return segment; /* not one of ours - leave it alone */
873 if (seg->align >= SEG_ABS)
874 return seg->align; /* absolute segment */
876 return segment; /* no special treatment */
879 static void ieee_write_file(void)
881 const struct tm * const thetime = &official_compile_time.local;
882 struct FileName *fn;
883 struct ieeeSection *seg;
884 struct ieeePublic *pub, *loc;
885 struct ieeeExternal *ext;
886 struct ieeeObjData *data;
887 struct ieeeFixupp *fix;
888 struct Array *arr;
889 int i;
890 const bool debuginfo = (dfmt == &ladsoft_debug_form);
893 * Write the module header
895 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile), ieee_infile);
898 * Write the NASM boast comment.
900 ieee_putascii("CO0,%02X%s.\n", strlen(nasm_comment), nasm_comment);
903 * write processor-specific information
905 ieee_putascii("AD8,4,L.\n");
908 * date and time
910 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
911 1900 + thetime->tm_year, thetime->tm_mon + 1,
912 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
913 thetime->tm_sec);
915 * if debugging, dump file names
917 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
918 ieee_putascii("C0105,%02X%s.\n", strlen(fn->name), fn->name);
921 ieee_putascii("CO101,07ENDHEAD.\n");
923 * the standard doesn't specify when to put checksums,
924 * we'll just do it periodically.
926 ieee_putcs(false);
929 * Write the section headers
931 seg = seghead;
932 if (!debuginfo && !strcmp(seg->name, "??LINE"))
933 seg = seg->next;
934 while (seg) {
935 char buf[256];
936 char attrib;
937 switch (seg->combine) {
938 case CMB_PUBLIC:
939 default:
940 attrib = 'C';
941 break;
942 case CMB_PRIVATE:
943 attrib = 'S';
944 break;
945 case CMB_COMMON:
946 attrib = 'M';
947 break;
949 ieee_unqualified_name(buf, seg->name);
950 if (seg->align >= SEG_ABS) {
951 ieee_putascii("ST%X,A,%02X%s.\n", seg->ieee_index,
952 strlen(buf), buf);
953 ieee_putascii("ASL%X,%lX.\n", seg->ieee_index,
954 (seg->align - SEG_ABS) * 16);
955 } else {
956 ieee_putascii("ST%X,%c,%02X%s.\n", seg->ieee_index, attrib,
957 strlen(buf), buf);
958 ieee_putascii("SA%X,%lX.\n", seg->ieee_index, seg->align);
959 ieee_putascii("ASS%X,%X.\n", seg->ieee_index,
960 seg->currentpos);
962 seg = seg->next;
965 * write the start address if there is one
967 if (ieee_entry_seg) {
968 for (seg = seghead; seg; seg = seg->next)
969 if (seg->index == ieee_entry_seg)
970 break;
971 if (!seg)
972 nasm_panic(0, "Start address records are incorrect");
973 else
974 ieee_putascii("ASG,R%X,%lX,+.\n", seg->ieee_index,
975 ieee_entry_ofs);
978 ieee_putcs(false);
980 * Write the publics
982 i = 1;
983 for (seg = seghead; seg; seg = seg->next) {
984 for (pub = seg->pubhead; pub; pub = pub->next) {
985 char buf[256];
986 ieee_unqualified_name(buf, pub->name);
987 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
988 if (pub->segment == -1)
989 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
990 pub->offset);
991 else
992 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
993 pub->offset);
994 if (debuginfo) {
995 if (pub->type >= 0x100)
996 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
997 else
998 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1000 i++;
1003 pub = fpubhead;
1004 i = 1;
1005 while (pub) {
1006 char buf[256];
1007 ieee_unqualified_name(buf, pub->name);
1008 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1009 if (pub->segment == -1)
1010 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1011 pub->offset);
1012 else
1013 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1014 pub->offset);
1015 if (debuginfo) {
1016 if (pub->type >= 0x100)
1017 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1018 else
1019 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1021 i++;
1022 pub = pub->next;
1025 * Write the externals
1027 ext = exthead;
1028 i = 1;
1029 while (ext) {
1030 char buf[256];
1031 ieee_unqualified_name(buf, ext->name);
1032 ieee_putascii("NX%X,%02X%s.\n", i++, strlen(buf), buf);
1033 ext = ext->next;
1035 ieee_putcs(false);
1038 * IEEE doesn't have a standard pass break record
1039 * so use the ladsoft variant
1041 ieee_putascii("CO100,06ENDSYM.\n");
1044 * now put types
1046 i = ARRAY_BOT;
1047 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1048 ieee_putascii("TY%X,20,%X,%lX.\n", i++, arr->basetype,
1049 arr->size);
1052 * now put locals
1054 i = 1;
1055 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1056 for (loc = seg->lochead; loc; loc = loc->next) {
1057 char buf[256];
1058 ieee_unqualified_name(buf, loc->name);
1059 ieee_putascii("NN%X,%02X%s.\n", i, strlen(buf), buf);
1060 if (loc->segment == -1)
1061 ieee_putascii("ASN%X,R%X,%lX,+.\n", i, loc->index,
1062 loc->offset);
1063 else
1064 ieee_putascii("ASN%X,%lX,%lX,+.\n", i, loc->segment * 16,
1065 loc->offset);
1066 if (debuginfo) {
1067 if (loc->type >= 0x100)
1068 ieee_putascii("ATN%X,T%X.\n", i, loc->type - 0x100);
1069 else
1070 ieee_putascii("ATN%X,%X.\n", i, loc->type);
1072 i++;
1077 * put out section data;
1079 seg = seghead;
1080 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1081 seg = seg->next;
1082 while (seg) {
1083 if (seg->currentpos) {
1084 int32_t size, org = 0;
1085 data = seg->data;
1086 ieee_putascii("SB%X.\n", seg->ieee_index);
1087 fix = seg->fptr;
1088 while (fix) {
1089 size = HUNKSIZE - (org % HUNKSIZE);
1090 size =
1091 size + org >
1092 seg->currentpos ? seg->currentpos - org : size;
1093 size = fix->offset - org > size ? size : fix->offset - org;
1094 org = ieee_putld(org, org + size, data->data);
1095 if (org % HUNKSIZE == 0)
1096 data = data->next;
1097 if (org == fix->offset) {
1098 org += ieee_putlr(fix);
1099 fix = fix->next;
1102 while (org < seg->currentpos && data) {
1103 size =
1104 seg->currentpos - org >
1105 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1106 org = ieee_putld(org, org + size, data->data);
1107 data = data->next;
1109 ieee_putcs(false);
1112 seg = seg->next;
1115 * module end record
1117 ieee_putascii("ME.\n");
1120 static void ieee_write_byte(struct ieeeSection *seg, int data)
1122 int temp;
1123 if (!(temp = seg->currentpos++ % HUNKSIZE))
1124 ieee_data_new(seg);
1125 seg->datacurr->data[temp] = data;
1128 static void ieee_write_word(struct ieeeSection *seg, int data)
1130 ieee_write_byte(seg, data & 0xFF);
1131 ieee_write_byte(seg, (data >> 8) & 0xFF);
1134 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1136 ieee_write_byte(seg, data & 0xFF);
1137 ieee_write_byte(seg, (data >> 8) & 0xFF);
1138 ieee_write_byte(seg, (data >> 16) & 0xFF);
1139 ieee_write_byte(seg, (data >> 24) & 0xFF);
1141 static void ieee_putascii(char *format, ...)
1143 char buffer[256];
1144 int i, l;
1145 va_list ap;
1147 va_start(ap, format);
1148 vsnprintf(buffer, sizeof(buffer), format, ap);
1149 l = strlen(buffer);
1150 for (i = 0; i < l; i++)
1151 if ((uint8_t)buffer[i] > 31)
1152 checksum += buffer[i];
1153 va_end(ap);
1154 fputs(buffer, ofile);
1158 * put out a checksum record */
1159 static void ieee_putcs(int toclear)
1161 if (toclear) {
1162 ieee_putascii("CS.\n");
1163 } else {
1164 checksum += 'C';
1165 checksum += 'S';
1166 ieee_putascii("CS%02X.\n", checksum & 127);
1168 checksum = 0;
1171 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1173 int32_t val;
1174 if (start == end)
1175 return (start);
1176 val = start % HUNKSIZE;
1177 /* fill up multiple lines */
1178 while (end - start >= LDPERLINE) {
1179 int i;
1180 ieee_putascii("LD");
1181 for (i = 0; i < LDPERLINE; i++) {
1182 ieee_putascii("%02X", buf[val++]);
1183 start++;
1185 ieee_putascii(".\n");
1187 /* if no partial lines */
1188 if (start == end)
1189 return (start);
1190 /* make a partial line */
1191 ieee_putascii("LD");
1192 while (start < end) {
1193 ieee_putascii("%02X", buf[val++]);
1194 start++;
1196 ieee_putascii(".\n");
1197 return (start);
1199 static int32_t ieee_putlr(struct ieeeFixupp *p)
1202 * To deal with the vagaries of segmentation the LADsoft linker
1203 * defines two types of segments: absolute and virtual. Note that
1204 * 'absolute' in this context is a different thing from the IEEE
1205 * definition of an absolute segment type, which is also supported. If a
1206 * sement is linked in virtual mode the low limit (L-var) is
1207 * subtracted from each R,X, and P variable which appears in an
1208 * expression, so that we can have relative offsets. Meanwhile
1209 * in the ABSOLUTE mode this subtraction is not done and
1210 * so we can use absolute offsets from 0. In the LADsoft linker
1211 * this configuration is not done in the assemblker source but in
1212 * a source the linker reads. Generally this type of thing only
1213 * becomes an issue if real mode code is used. A pure 32-bit linker could
1214 * get away without defining the virtual mode...
1216 char buf[40];
1217 int32_t size = p->size;
1218 switch (p->ftype) {
1219 case FT_SEG:
1220 if (p->id1 < 0)
1221 sprintf(buf, "%"PRIX32"", -p->id1);
1222 else
1223 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1224 break;
1225 case FT_OFS:
1226 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1227 break;
1228 case FT_REL:
1229 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1230 break;
1232 case FT_WRT:
1233 if (p->id2 < 0)
1234 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1235 p->id2, -p->id1 * 16);
1236 else
1237 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1238 p->id2, p->id1);
1239 break;
1240 case FT_EXT:
1241 sprintf(buf, "X%"PRIX32"", p->id1);
1242 break;
1243 case FT_EXTREL:
1244 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1245 break;
1246 case FT_EXTSEG:
1247 /* We needed a non-ieee hack here.
1248 * We introduce the Y variable, which is the low
1249 * limit of the native segment the extern resides in
1251 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1252 break;
1253 case FT_EXTWRT:
1254 if (p->id2 < 0)
1255 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1256 -p->id1 * 16);
1257 else
1258 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1259 break;
1261 ieee_putascii("LR(%s,%"PRIX32").\n", buf, size);
1263 return (size);
1266 /* Dump all segment data (text and fixups )*/
1268 static void ieee_unqualified_name(char *dest, char *source)
1270 if (ieee_uppercase) {
1271 while (*source)
1272 *dest++ = toupper(*source++);
1273 *dest = 0;
1274 } else
1275 strcpy(dest, source);
1277 static void dbgls_init(void)
1279 int tempint;
1281 fnhead = NULL;
1282 fntail = &fnhead;
1283 arrindex = ARRAY_BOT;
1284 arrhead = NULL;
1285 arrtail = &arrhead;
1286 ieee_segment("??LINE", 2, &tempint);
1287 any_segs = false;
1289 static void dbgls_cleanup(void)
1291 struct ieeeSection *segtmp;
1292 while (fnhead) {
1293 struct FileName *fntemp = fnhead;
1294 fnhead = fnhead->next;
1295 nasm_free(fntemp->name);
1296 nasm_free(fntemp);
1298 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1299 while (segtmp->lochead) {
1300 struct ieeePublic *loctmp = segtmp->lochead;
1301 segtmp->lochead = loctmp->next;
1302 nasm_free(loctmp->name);
1303 nasm_free(loctmp);
1306 while (arrhead) {
1307 struct Array *arrtmp = arrhead;
1308 arrhead = arrhead->next;
1309 nasm_free(arrtmp);
1314 * because this routine is not bracketed in
1315 * the main program, this routine will be called even if there
1316 * is no request for debug info
1317 * so, we have to make sure the ??LINE segment is avaialbe
1318 * as the first segment when this debug format is selected
1320 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1322 struct FileName *fn;
1323 struct ieeeSection *seg;
1324 int i = 0;
1325 if (segto == NO_SEG)
1326 return;
1329 * If `any_segs' is still false, we must define a default
1330 * segment.
1332 if (!any_segs) {
1333 int tempint; /* ignored */
1334 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1335 nasm_panic(0, "strange segment conditions in OBJ driver");
1339 * Find the segment we are targetting.
1341 for (seg = seghead; seg; seg = seg->next)
1342 if (seg->index == segto)
1343 break;
1344 if (!seg)
1345 nasm_panic(0, "lineno directed to nonexistent segment?");
1347 for (fn = fnhead; fn; fn = fn->next) {
1348 if (!nasm_stricmp(lnfname, fn->name))
1349 break;
1350 i++;
1352 if (!fn) {
1353 fn = nasm_malloc(sizeof(*fn));
1354 fn->name = nasm_malloc(strlen(lnfname) + 1);
1355 fn->index = i;
1356 strcpy(fn->name, lnfname);
1357 fn->next = NULL;
1358 *fntail = fn;
1359 fntail = &fn->next;
1361 ieee_write_byte(seghead, fn->index);
1362 ieee_write_word(seghead, lineno);
1363 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1364 seg->currentpos);
1367 static void dbgls_deflabel(char *name, int32_t segment,
1368 int64_t offset, int is_global, char *special)
1370 struct ieeeSection *seg;
1372 /* Keep compiler from warning about special */
1373 (void)special;
1376 * Note: ..[^@] special symbols are filtered in labels.c
1380 * If it's a special-retry from pass two, discard it.
1382 if (is_global == 3)
1383 return;
1386 * Case (i):
1388 if (ieee_seg_needs_update)
1389 return;
1390 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
1391 return;
1393 if (segment >= SEG_ABS || segment == NO_SEG) {
1394 return;
1398 * If `any_segs' is still false, we might need to define a
1399 * default segment, if they're trying to declare a label in
1400 * `first_seg'. But the label should exist due to a prior
1401 * call to ieee_deflabel so we can skip that.
1404 for (seg = seghead; seg; seg = seg->next)
1405 if (seg->index == segment) {
1406 struct ieeePublic *loc;
1408 * Case (ii). Maybe MODPUB someday?
1410 if (!is_global) {
1411 last_defined = loc = nasm_malloc(sizeof(*loc));
1412 *seg->loctail = loc;
1413 seg->loctail = &loc->next;
1414 loc->next = NULL;
1415 loc->name = nasm_strdup(name);
1416 loc->offset = offset;
1417 loc->segment = -1;
1418 loc->index = seg->ieee_index;
1422 static void dbgls_typevalue(int32_t type)
1424 int elem = TYM_ELEMENTS(type);
1425 type = TYM_TYPE(type);
1427 if (!last_defined)
1428 return;
1430 switch (type) {
1431 case TY_BYTE:
1432 last_defined->type = 1; /* uint8_t */
1433 break;
1434 case TY_WORD:
1435 last_defined->type = 3; /* unsigned word */
1436 break;
1437 case TY_DWORD:
1438 last_defined->type = 5; /* unsigned dword */
1439 break;
1440 case TY_FLOAT:
1441 last_defined->type = 9; /* float */
1442 break;
1443 case TY_QWORD:
1444 last_defined->type = 10; /* qword */
1445 break;
1446 case TY_TBYTE:
1447 last_defined->type = 11; /* TBYTE */
1448 break;
1449 default:
1450 last_defined->type = 0x10; /* near label */
1451 break;
1454 if (elem > 1) {
1455 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
1456 int vtype = last_defined->type;
1457 arrtmp->size = elem;
1458 arrtmp->basetype = vtype;
1459 arrtmp->next = NULL;
1460 last_defined->type = arrindex++ + 0x100;
1461 *arrtail = arrtmp;
1462 arrtail = &(arrtmp->next);
1464 last_defined = NULL;
1466 static void dbgls_output(int output_type, void *param)
1468 (void)output_type;
1469 (void)param;
1471 static const struct dfmt ladsoft_debug_form = {
1472 "LADsoft Debug Records",
1473 "ladsoft",
1474 dbgls_init,
1475 dbgls_linnum,
1476 dbgls_deflabel,
1477 null_debug_directive,
1478 dbgls_typevalue,
1479 dbgls_output,
1480 dbgls_cleanup,
1481 NULL /* pragma list */
1483 static const struct dfmt * const ladsoft_debug_arr[3] = {
1484 &ladsoft_debug_form,
1485 &null_debug_form,
1486 NULL
1488 const struct ofmt of_ieee = {
1489 "IEEE-695 (LADsoft variant) object file format",
1490 "ieee",
1491 ".o",
1492 OFMT_TEXT,
1494 ladsoft_debug_arr,
1495 &ladsoft_debug_form,
1496 NULL,
1497 ieee_init,
1498 null_reset,
1499 nasm_do_legacy_output,
1500 ieee_out,
1501 ieee_deflabel,
1502 ieee_segment,
1503 NULL,
1504 ieee_sectalign,
1505 ieee_segbase,
1506 ieee_directive,
1507 ieee_cleanup,
1508 NULL /* pragma list */
1511 #endif /* OF_IEEE */