NASM 2.15rc9
[nasm.git] / output / outieee.c
blob4cc0f0f51d4483fe35877867b916e2c878d258c1
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 <time.h>
71 #include <ctype.h> /* For toupper() */
72 #include "nctype.h"
74 #include "nasm.h"
75 #include "nasmlib.h"
76 #include "error.h"
77 #include "ver.h"
79 #include "outform.h"
80 #include "outlib.h"
82 #ifdef OF_IEEE
84 #define ARRAY_BOT 0x1
86 static char ieee_infile[FILENAME_MAX];
87 static int ieee_uppercase;
89 static bool any_segs;
90 static int arrindex;
92 #define HUNKSIZE 1024 /* Size of the data hunk */
93 #define EXT_BLKSIZ 512
94 #define LDPERLINE 32 /* bytes per line in output */
96 struct ieeeSection;
98 struct LineNumber {
99 struct LineNumber *next;
100 struct ieeeSection *segment;
101 int32_t offset;
102 int32_t lineno;
105 static struct FileName {
106 struct FileName *next;
107 char *name;
108 int32_t index;
109 } *fnhead, **fntail;
111 static struct Array {
112 struct Array *next;
113 unsigned size;
114 int basetype;
115 } *arrhead, **arrtail;
117 static struct ieeePublic {
118 struct ieeePublic *next;
119 char *name;
120 int32_t offset;
121 int32_t segment; /* only if it's far-absolute */
122 int32_t index;
123 int type; /* for debug purposes */
124 } *fpubhead, **fpubtail, *last_defined;
126 static struct ieeeExternal {
127 struct ieeeExternal *next;
128 char *name;
129 int32_t commonsize;
130 } *exthead, **exttail;
132 static int externals;
134 static struct ExtBack {
135 struct ExtBack *next;
136 int index[EXT_BLKSIZ];
137 } *ebhead, **ebtail;
139 /* NOTE: the first segment MUST be the lineno segment */
140 static struct ieeeSection {
141 struct ieeeSection *next;
142 char *name;
143 struct ieeeObjData *data, *datacurr;
144 struct ieeeFixupp *fptr, *flptr;
145 int32_t index; /* the NASM segment id */
146 int32_t ieee_index; /* the IEEE-file segment index */
147 int32_t currentpos;
148 int32_t align; /* can be SEG_ABS + absolute addr */
149 int32_t startpos;
150 int32_t use32; /* is this segment 32-bit? */
151 int64_t pass_last_seen;
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 const struct ofmt of_ieee;
188 static const 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 *);
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 strlcpy(ieee_infile, inname, sizeof(ieee_infile));
211 any_segs = false;
212 fpubhead = NULL;
213 fpubtail = &fpubhead;
214 exthead = NULL;
215 exttail = &exthead;
216 externals = 1;
217 ebhead = NULL;
218 ebtail = &ebhead;
219 seghead = ieee_seg_needs_update = NULL;
220 segtail = &seghead;
221 ieee_entry_seg = NO_SEG;
222 ieee_uppercase = false;
223 checksum = 0;
227 * Rundown
229 static void ieee_cleanup(void)
231 ieee_write_file();
232 dfmt->cleanup();
233 while (seghead) {
234 struct ieeeSection *segtmp = seghead;
235 seghead = seghead->next;
236 while (segtmp->pubhead) {
237 struct ieeePublic *pubtmp = segtmp->pubhead;
238 segtmp->pubhead = pubtmp->next;
239 nasm_free(pubtmp);
241 while (segtmp->fptr) {
242 struct ieeeFixupp *fixtmp = segtmp->fptr;
243 segtmp->fptr = fixtmp->next;
244 nasm_free(fixtmp);
246 while (segtmp->data) {
247 struct ieeeObjData *dattmp = segtmp->data;
248 segtmp->data = dattmp->next;
249 nasm_free(dattmp);
251 nasm_free(segtmp);
253 while (fpubhead) {
254 struct ieeePublic *pubtmp = fpubhead;
255 fpubhead = fpubhead->next;
256 nasm_free(pubtmp);
258 while (exthead) {
259 struct ieeeExternal *exttmp = exthead;
260 exthead = exthead->next;
261 nasm_free(exttmp);
263 while (ebhead) {
264 struct ExtBack *ebtmp = ebhead;
265 ebhead = ebhead->next;
266 nasm_free(ebtmp);
271 * callback for labels
273 static void ieee_deflabel(char *name, int32_t segment,
274 int64_t offset, int is_global, char *special)
277 * We have three cases:
279 * (i) `segment' is a segment-base. If so, set the name field
280 * for the segment structure it refers to, and then
281 * return.
283 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
284 * Save the label position for later output of a PUBDEF record.
287 * (iii) `segment' is not one of our segments. Save the label
288 * position for later output of an EXTDEF.
290 struct ieeeExternal *ext;
291 struct ExtBack *eb;
292 struct ieeeSection *seg;
293 int i;
295 if (special)
296 nasm_nonfatal("unrecognised symbol type `%s'", special);
298 * First check for the double-period, signifying something
299 * unusual.
301 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
302 if (!strcmp(name, "..start")) {
303 ieee_entry_seg = segment;
304 ieee_entry_ofs = offset;
306 return;
310 * Case (i):
312 if (ieee_seg_needs_update) {
313 ieee_seg_needs_update->name = name;
314 return;
316 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
317 return;
320 * case (ii)
322 if (segment >= SEG_ABS) {
324 * SEG_ABS subcase of (ii).
326 if (is_global) {
327 struct ieeePublic *pub;
329 pub = *fpubtail = nasm_malloc(sizeof(*pub));
330 fpubtail = &pub->next;
331 pub->next = NULL;
332 pub->name = name;
333 pub->offset = offset;
334 pub->segment = segment & ~SEG_ABS;
336 return;
339 for (seg = seghead; seg && is_global; seg = seg->next)
340 if (seg->index == segment) {
341 struct ieeePublic *pub;
343 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
344 seg->pubtail = &pub->next;
345 pub->next = NULL;
346 pub->name = name;
347 pub->offset = offset;
348 pub->index = seg->ieee_index;
349 pub->segment = -1;
350 return;
354 * Case (iii).
356 if (is_global) {
357 ext = *exttail = nasm_malloc(sizeof(*ext));
358 ext->next = NULL;
359 exttail = &ext->next;
360 ext->name = name;
361 if (is_global == 2)
362 ext->commonsize = offset;
363 else
364 ext->commonsize = 0;
365 i = segment / 2;
366 eb = ebhead;
367 if (!eb) {
368 eb = *ebtail = nasm_zalloc(sizeof(*eb));
369 eb->next = NULL;
370 ebtail = &eb->next;
372 while (i > EXT_BLKSIZ) {
373 if (eb && eb->next)
374 eb = eb->next;
375 else {
376 eb = *ebtail = nasm_zalloc(sizeof(*eb));
377 eb->next = NULL;
378 ebtail = &eb->next;
380 i -= EXT_BLKSIZ;
382 eb->index[i] = externals++;
388 * Put data out
390 static void ieee_out(int32_t segto, const void *data,
391 enum out_type type, uint64_t size,
392 int32_t segment, int32_t wrt)
394 const uint8_t *ucdata;
395 int32_t ldata;
396 struct ieeeSection *seg;
399 * If `any_segs' is still false, we must define a default
400 * segment.
402 if (!any_segs) {
403 int tempint; /* ignored */
404 if (segto != ieee_segment("__NASMDEFSEG", &tempint))
405 nasm_panic("strange segment conditions in IEEE driver");
409 * Find the segment we are targetting.
411 for (seg = seghead; seg; seg = seg->next)
412 if (seg->index == segto)
413 break;
414 if (!seg)
415 nasm_panic("code directed to nonexistent segment?");
417 if (type == OUT_RAWDATA) {
418 ucdata = data;
419 while (size--)
420 ieee_write_byte(seg, *ucdata++);
421 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
422 type == OUT_REL4ADR) {
423 if (type == OUT_ADDRESS)
424 size = abs((int)size);
425 else if (segment == NO_SEG)
426 nasm_nonfatal("relative call to absolute address not"
427 " supported by IEEE format");
428 ldata = *(int64_t *)data;
429 if (type == OUT_REL2ADR)
430 ldata += (size - 2);
431 if (type == OUT_REL4ADR)
432 ldata += (size - 4);
433 ieee_write_fixup(segment, wrt, seg, size, type, ldata);
434 if (size == 2)
435 ieee_write_word(seg, ldata);
436 else
437 ieee_write_dword(seg, ldata);
438 } else if (type == OUT_RESERVE) {
439 while (size--)
440 ieee_write_byte(seg, 0);
444 static void ieee_data_new(struct ieeeSection *segto)
447 if (!segto->data)
448 segto->data = segto->datacurr =
449 nasm_malloc(sizeof(*(segto->datacurr)));
450 else
451 segto->datacurr = segto->datacurr->next =
452 nasm_malloc(sizeof(*(segto->datacurr)));
453 segto->datacurr->next = NULL;
457 * this routine is unalduterated bloatware. I usually don't do this
458 * but I might as well see what it is like on a harmless program.
459 * If anyone wants to optimize this is a good canditate!
461 static void ieee_write_fixup(int32_t segment, int32_t wrt,
462 struct ieeeSection *segto, int size,
463 uint64_t realtype, int32_t offset)
465 struct ieeeSection *target;
466 struct ieeeFixupp s;
468 /* Don't put a fixup for things NASM can calculate */
469 if (wrt == NO_SEG && segment == NO_SEG)
470 return;
472 s.ftype = -1;
473 /* if it is a WRT offset */
474 if (wrt != NO_SEG) {
475 s.ftype = FT_WRT;
476 s.addend = offset;
477 if (wrt >= SEG_ABS)
478 s.id1 = -(wrt - SEG_ABS);
479 else {
480 if (wrt % 2 && realtype != OUT_REL2ADR
481 && realtype != OUT_REL4ADR) {
482 wrt--;
484 for (target = seghead; target; target = target->next)
485 if (target->index == wrt)
486 break;
487 if (target) {
488 s.id1 = target->ieee_index;
489 for (target = seghead; target; target = target->next)
490 if (target->index == segment)
491 break;
493 if (target)
494 s.id2 = target->ieee_index;
495 else {
497 * Now we assume the segment field is being used
498 * to hold an extern index
500 int32_t i = segment / 2;
501 struct ExtBack *eb = ebhead;
502 while (i > EXT_BLKSIZ) {
503 if (eb)
504 eb = eb->next;
505 else
506 break;
507 i -= EXT_BLKSIZ;
509 /* if we have an extern decide the type and make a record
511 if (eb) {
512 s.ftype = FT_EXTWRT;
513 s.addend = 0;
514 s.id2 = eb->index[i];
515 } else
516 nasm_nonfatal("source of WRT must be an offset");
519 } else
520 nasm_panic("unrecognised WRT value in ieee_write_fixup");
521 } else
522 nasm_nonfatal("target of WRT must be a section");
524 s.size = size;
525 ieee_install_fixup(segto, &s);
526 return;
528 /* Pure segment fixup ? */
529 if (segment != NO_SEG) {
530 s.ftype = FT_SEG;
531 s.id1 = 0;
532 if (segment >= SEG_ABS) {
533 /* absolute far segment fixup */
534 s.id1 = -(segment - ~SEG_ABS);
535 } else if (segment % 2) {
536 /* fixup to named segment */
537 /* look it up */
538 for (target = seghead; target; target = target->next)
539 if (target->index == segment - 1)
540 break;
541 if (target)
542 s.id1 = target->ieee_index;
543 else {
545 * Now we assume the segment field is being used
546 * to hold an extern index
548 int32_t i = segment / 2;
549 struct ExtBack *eb = ebhead;
550 while (i > EXT_BLKSIZ) {
551 if (eb)
552 eb = eb->next;
553 else
554 break;
555 i -= EXT_BLKSIZ;
557 /* if we have an extern decide the type and make a record
559 if (eb) {
560 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
561 nasm_panic("Segment of a rel not supported in ieee_write_fixup");
562 } else {
563 /* If we want the segment */
564 s.ftype = FT_EXTSEG;
565 s.addend = 0;
566 s.id1 = eb->index[i];
569 } else
570 /* If we get here the seg value doesn't make sense */
571 nasm_panic("unrecognised segment value in ieee_write_fixup");
574 } else {
575 /* Assume we are offsetting directly from a section
576 * So look up the target segment
578 for (target = seghead; target; target = target->next)
579 if (target->index == segment)
580 break;
581 if (target) {
582 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
583 /* PC rel to a known offset */
584 s.id1 = target->ieee_index;
585 s.ftype = FT_REL;
586 s.size = size;
587 s.addend = offset;
588 } else {
589 /* We were offsetting from a seg */
590 s.id1 = target->ieee_index;
591 s.ftype = FT_OFS;
592 s.size = size;
593 s.addend = offset;
595 } else {
597 * Now we assume the segment field is being used
598 * to hold an extern index
600 int32_t i = segment / 2;
601 struct ExtBack *eb = ebhead;
602 while (i > EXT_BLKSIZ) {
603 if (eb)
604 eb = eb->next;
605 else
606 break;
607 i -= EXT_BLKSIZ;
609 /* if we have an extern decide the type and make a record
611 if (eb) {
612 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
613 s.ftype = FT_EXTREL;
614 s.addend = 0;
615 s.id1 = eb->index[i];
616 } else {
617 /* else we want the external offset */
618 s.ftype = FT_EXT;
619 s.addend = 0;
620 s.id1 = eb->index[i];
623 } else
624 /* If we get here the seg value doesn't make sense */
625 nasm_panic("unrecognised segment value in ieee_write_fixup");
628 if (size != 2 && s.ftype == FT_SEG)
629 nasm_nonfatal("IEEE format can only handle 2-byte"
630 " segment base references");
631 s.size = size;
632 ieee_install_fixup(segto, &s);
633 return;
635 /* should never get here */
637 static void ieee_install_fixup(struct ieeeSection *seg,
638 struct ieeeFixupp *fix)
640 struct ieeeFixupp *f;
641 f = nasm_malloc(sizeof(struct ieeeFixupp));
642 memcpy(f, fix, sizeof(struct ieeeFixupp));
643 f->offset = seg->currentpos;
644 seg->currentpos += fix->size;
645 f->next = NULL;
646 if (seg->fptr)
647 seg->flptr = seg->flptr->next = f;
648 else
649 seg->fptr = seg->flptr = f;
654 * segment registry
656 static int32_t ieee_segment(char *name, int *bits)
659 * We call the label manager here to define a name for the new
660 * segment, and when our _own_ label-definition stub gets
661 * called in return, it should register the new segment name
662 * using the pointer it gets passed. That way we save memory,
663 * by sponging off the label manager.
665 if (!name) {
666 *bits = 16;
667 if (!any_segs)
668 return 0;
669 return seghead->index;
670 } else {
671 struct ieeeSection *seg;
672 int ieee_idx, attrs;
673 bool rn_error;
674 char *p;
677 * Look for segment attributes.
679 attrs = 0;
680 while (*name == '.')
681 name++; /* hack, but a documented one */
682 p = name;
683 while (*p && !nasm_isspace(*p))
684 p++;
685 if (*p) {
686 *p++ = '\0';
687 while (*p && nasm_isspace(*p))
688 *p++ = '\0';
690 while (*p) {
691 while (*p && !nasm_isspace(*p))
692 p++;
693 if (*p) {
694 *p++ = '\0';
695 while (*p && nasm_isspace(*p))
696 *p++ = '\0';
699 attrs++;
702 ieee_idx = 1;
703 for (seg = seghead; seg; seg = seg->next) {
704 ieee_idx++;
705 if (!strcmp(seg->name, name)) {
706 if (attrs > 0 && seg->pass_last_seen == pass_count())
707 nasm_warn(WARN_OTHER, "segment attributes specified on"
708 " redeclaration of segment: ignoring");
709 if (seg->use32)
710 *bits = 32;
711 else
712 *bits = 16;
714 seg->pass_last_seen = pass_count();
715 return seg->index;
719 *segtail = seg = nasm_malloc(sizeof(*seg));
720 seg->next = NULL;
721 segtail = &seg->next;
722 seg->index = seg_alloc();
723 seg->ieee_index = ieee_idx;
724 any_segs = true;
725 seg->name = NULL;
726 seg->currentpos = 0;
727 seg->align = 1; /* default */
728 seg->use32 = *bits == 32; /* default to user spec */
729 seg->combine = CMB_PUBLIC; /* default */
730 seg->pubhead = NULL;
731 seg->pubtail = &seg->pubhead;
732 seg->data = NULL;
733 seg->fptr = NULL;
734 seg->lochead = NULL;
735 seg->loctail = &seg->lochead;
738 * Process the segment attributes.
740 p = name;
741 while (attrs--) {
742 p += strlen(p);
743 while (!*p)
744 p++;
747 * `p' contains a segment attribute.
749 if (!nasm_stricmp(p, "private"))
750 seg->combine = CMB_PRIVATE;
751 else if (!nasm_stricmp(p, "public"))
752 seg->combine = CMB_PUBLIC;
753 else if (!nasm_stricmp(p, "common"))
754 seg->combine = CMB_COMMON;
755 else if (!nasm_stricmp(p, "use16"))
756 seg->use32 = false;
757 else if (!nasm_stricmp(p, "use32"))
758 seg->use32 = true;
759 else if (!nasm_strnicmp(p, "align=", 6)) {
760 seg->align = readnum(p + 6, &rn_error);
761 if (seg->align == 0)
762 seg->align = 1;
763 if (rn_error) {
764 seg->align = 1;
765 nasm_nonfatal("segment alignment should be numeric");
767 switch (seg->align) {
768 case 1: /* BYTE */
769 case 2: /* WORD */
770 case 4: /* DWORD */
771 case 16: /* PARA */
772 case 256: /* PAGE */
773 case 8:
774 case 32:
775 case 64:
776 case 128:
777 break;
778 default:
779 nasm_nonfatal("invalid alignment value %d", seg->align);
780 seg->align = 1;
781 break;
783 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
784 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
785 if (rn_error)
786 nasm_nonfatal("argument to `absolute' segment"
787 " attribute should be numeric");
791 ieee_seg_needs_update = seg;
792 if (seg->align >= SEG_ABS)
793 define_label(name, NO_SEG, seg->align - SEG_ABS, false);
794 else
795 define_label(name, seg->index + 1, 0L, false);
796 ieee_seg_needs_update = NULL;
798 if (seg->use32)
799 *bits = 32;
800 else
801 *bits = 16;
802 return seg->index;
807 * directives supported
809 static enum directive_result
810 ieee_directive(enum directive directive, char *value)
812 (void)value;
814 switch (directive) {
815 case D_UPPERCASE:
816 ieee_uppercase = true;
817 return DIRR_OK;
819 default:
820 return DIRR_UNKNOWN;
824 static void ieee_sectalign(int32_t seg, unsigned int value)
826 struct ieeeSection *s;
828 list_for_each(s, seghead) {
829 if (s->index == seg)
830 break;
834 * 256 is maximum there, note it may happen
835 * that align is issued on "absolute" segment
836 * it's fine since SEG_ABS > 256 and we never
837 * get escape this test
839 if (!s || !is_power2(value) || value > 256)
840 return;
842 if ((unsigned int)s->align < value)
843 s->align = value;
847 * Return segment data
849 static int32_t ieee_segbase(int32_t segment)
851 struct ieeeSection *seg;
854 * Find the segment in our list.
856 for (seg = seghead; seg; seg = seg->next)
857 if (seg->index == segment - 1)
858 break;
860 if (!seg)
861 return segment; /* not one of ours - leave it alone */
863 if (seg->align >= SEG_ABS)
864 return seg->align; /* absolute segment */
866 return segment; /* no special treatment */
869 static void ieee_write_file(void)
871 const struct tm * const thetime = &official_compile_time.local;
872 struct FileName *fn;
873 struct ieeeSection *seg;
874 struct ieeePublic *pub, *loc;
875 struct ieeeExternal *ext;
876 struct ieeeObjData *data;
877 struct ieeeFixupp *fix;
878 struct Array *arr;
879 int i;
880 const bool debuginfo = (dfmt == &ladsoft_debug_form);
883 * Write the module header
885 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile), ieee_infile);
888 * Write the NASM boast comment.
890 ieee_putascii("CO0,%02X%s.\n", nasm_comment_len(), nasm_comment());
893 * write processor-specific information
895 ieee_putascii("AD8,4,L.\n");
898 * date and time
900 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
901 1900 + thetime->tm_year, thetime->tm_mon + 1,
902 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
903 thetime->tm_sec);
905 * if debugging, dump file names
907 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
908 ieee_putascii("C0105,%02X%s.\n", strlen(fn->name), fn->name);
911 ieee_putascii("CO101,07ENDHEAD.\n");
913 * the standard doesn't specify when to put checksums,
914 * we'll just do it periodically.
916 ieee_putcs(false);
919 * Write the section headers
921 seg = seghead;
922 if (!debuginfo && !strcmp(seg->name, "??LINE"))
923 seg = seg->next;
924 while (seg) {
925 char buf[256];
926 char attrib;
927 switch (seg->combine) {
928 case CMB_PUBLIC:
929 default:
930 attrib = 'C';
931 break;
932 case CMB_PRIVATE:
933 attrib = 'S';
934 break;
935 case CMB_COMMON:
936 attrib = 'M';
937 break;
939 ieee_unqualified_name(buf, seg->name);
940 if (seg->align >= SEG_ABS) {
941 ieee_putascii("ST%X,A,%02X%s.\n", seg->ieee_index,
942 strlen(buf), buf);
943 ieee_putascii("ASL%X,%lX.\n", seg->ieee_index,
944 (seg->align - SEG_ABS) * 16);
945 } else {
946 ieee_putascii("ST%X,%c,%02X%s.\n", seg->ieee_index, attrib,
947 strlen(buf), buf);
948 ieee_putascii("SA%X,%lX.\n", seg->ieee_index, seg->align);
949 ieee_putascii("ASS%X,%X.\n", seg->ieee_index,
950 seg->currentpos);
952 seg = seg->next;
955 * write the start address if there is one
957 if (ieee_entry_seg) {
958 for (seg = seghead; seg; seg = seg->next)
959 if (seg->index == ieee_entry_seg)
960 break;
961 if (!seg)
962 nasm_panic("Start address records are incorrect");
963 else
964 ieee_putascii("ASG,R%X,%lX,+.\n", seg->ieee_index,
965 ieee_entry_ofs);
968 ieee_putcs(false);
970 * Write the publics
972 i = 1;
973 for (seg = seghead; seg; seg = seg->next) {
974 for (pub = seg->pubhead; pub; pub = pub->next) {
975 char buf[256];
976 ieee_unqualified_name(buf, pub->name);
977 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
978 if (pub->segment == -1)
979 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
980 pub->offset);
981 else
982 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
983 pub->offset);
984 if (debuginfo) {
985 if (pub->type >= 0x100)
986 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
987 else
988 ieee_putascii("ATI%X,%X.\n", i, pub->type);
990 i++;
993 pub = fpubhead;
994 i = 1;
995 while (pub) {
996 char buf[256];
997 ieee_unqualified_name(buf, pub->name);
998 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
999 if (pub->segment == -1)
1000 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1001 pub->offset);
1002 else
1003 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1004 pub->offset);
1005 if (debuginfo) {
1006 if (pub->type >= 0x100)
1007 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1008 else
1009 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1011 i++;
1012 pub = pub->next;
1015 * Write the externals
1017 ext = exthead;
1018 i = 1;
1019 while (ext) {
1020 char buf[256];
1021 ieee_unqualified_name(buf, ext->name);
1022 ieee_putascii("NX%X,%02X%s.\n", i++, strlen(buf), buf);
1023 ext = ext->next;
1025 ieee_putcs(false);
1028 * IEEE doesn't have a standard pass break record
1029 * so use the ladsoft variant
1031 ieee_putascii("CO100,06ENDSYM.\n");
1034 * now put types
1036 i = ARRAY_BOT;
1037 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1038 ieee_putascii("TY%X,20,%X,%lX.\n", i++, arr->basetype,
1039 arr->size);
1042 * now put locals
1044 i = 1;
1045 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1046 for (loc = seg->lochead; loc; loc = loc->next) {
1047 char buf[256];
1048 ieee_unqualified_name(buf, loc->name);
1049 ieee_putascii("NN%X,%02X%s.\n", i, strlen(buf), buf);
1050 if (loc->segment == -1)
1051 ieee_putascii("ASN%X,R%X,%lX,+.\n", i, loc->index,
1052 loc->offset);
1053 else
1054 ieee_putascii("ASN%X,%lX,%lX,+.\n", i, loc->segment * 16,
1055 loc->offset);
1056 if (debuginfo) {
1057 if (loc->type >= 0x100)
1058 ieee_putascii("ATN%X,T%X.\n", i, loc->type - 0x100);
1059 else
1060 ieee_putascii("ATN%X,%X.\n", i, loc->type);
1062 i++;
1067 * put out section data;
1069 seg = seghead;
1070 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1071 seg = seg->next;
1072 while (seg) {
1073 if (seg->currentpos) {
1074 int32_t size, org = 0;
1075 data = seg->data;
1076 ieee_putascii("SB%X.\n", seg->ieee_index);
1077 fix = seg->fptr;
1078 while (fix) {
1079 size = HUNKSIZE - (org % HUNKSIZE);
1080 size =
1081 size + org >
1082 seg->currentpos ? seg->currentpos - org : size;
1083 size = fix->offset - org > size ? size : fix->offset - org;
1084 org = ieee_putld(org, org + size, data->data);
1085 if (org % HUNKSIZE == 0)
1086 data = data->next;
1087 if (org == fix->offset) {
1088 org += ieee_putlr(fix);
1089 fix = fix->next;
1092 while (org < seg->currentpos && data) {
1093 size =
1094 seg->currentpos - org >
1095 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1096 org = ieee_putld(org, org + size, data->data);
1097 data = data->next;
1099 ieee_putcs(false);
1102 seg = seg->next;
1105 * module end record
1107 ieee_putascii("ME.\n");
1110 static void ieee_write_byte(struct ieeeSection *seg, int data)
1112 int temp;
1113 if (!(temp = seg->currentpos++ % HUNKSIZE))
1114 ieee_data_new(seg);
1115 seg->datacurr->data[temp] = data;
1118 static void ieee_write_word(struct ieeeSection *seg, int data)
1120 ieee_write_byte(seg, data & 0xFF);
1121 ieee_write_byte(seg, (data >> 8) & 0xFF);
1124 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1126 ieee_write_byte(seg, data & 0xFF);
1127 ieee_write_byte(seg, (data >> 8) & 0xFF);
1128 ieee_write_byte(seg, (data >> 16) & 0xFF);
1129 ieee_write_byte(seg, (data >> 24) & 0xFF);
1131 static void ieee_putascii(char *format, ...)
1133 char buffer[256];
1134 int i, l;
1135 va_list ap;
1137 va_start(ap, format);
1138 vsnprintf(buffer, sizeof(buffer), format, ap);
1139 l = strlen(buffer);
1140 for (i = 0; i < l; i++)
1141 if ((uint8_t)buffer[i] > 31)
1142 checksum += buffer[i];
1143 va_end(ap);
1144 fputs(buffer, ofile);
1148 * put out a checksum record */
1149 static void ieee_putcs(int toclear)
1151 if (toclear) {
1152 ieee_putascii("CS.\n");
1153 } else {
1154 checksum += 'C';
1155 checksum += 'S';
1156 ieee_putascii("CS%02X.\n", checksum & 127);
1158 checksum = 0;
1161 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1163 int32_t val;
1164 if (start == end)
1165 return (start);
1166 val = start % HUNKSIZE;
1167 /* fill up multiple lines */
1168 while (end - start >= LDPERLINE) {
1169 int i;
1170 ieee_putascii("LD");
1171 for (i = 0; i < LDPERLINE; i++) {
1172 ieee_putascii("%02X", buf[val++]);
1173 start++;
1175 ieee_putascii(".\n");
1177 /* if no partial lines */
1178 if (start == end)
1179 return (start);
1180 /* make a partial line */
1181 ieee_putascii("LD");
1182 while (start < end) {
1183 ieee_putascii("%02X", buf[val++]);
1184 start++;
1186 ieee_putascii(".\n");
1187 return (start);
1189 static int32_t ieee_putlr(struct ieeeFixupp *p)
1192 * To deal with the vagaries of segmentation the LADsoft linker
1193 * defines two types of segments: absolute and virtual. Note that
1194 * 'absolute' in this context is a different thing from the IEEE
1195 * definition of an absolute segment type, which is also supported. If a
1196 * sement is linked in virtual mode the low limit (L-var) is
1197 * subtracted from each R,X, and P variable which appears in an
1198 * expression, so that we can have relative offsets. Meanwhile
1199 * in the ABSOLUTE mode this subtraction is not done and
1200 * so we can use absolute offsets from 0. In the LADsoft linker
1201 * this configuration is not done in the assemblker source but in
1202 * a source the linker reads. Generally this type of thing only
1203 * becomes an issue if real mode code is used. A pure 32-bit linker could
1204 * get away without defining the virtual mode...
1206 char buf[40];
1207 int32_t size = p->size;
1208 switch (p->ftype) {
1209 case FT_SEG:
1210 if (p->id1 < 0)
1211 sprintf(buf, "%"PRIX32"", -p->id1);
1212 else
1213 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1214 break;
1215 case FT_OFS:
1216 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1217 break;
1218 case FT_REL:
1219 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1220 break;
1222 case FT_WRT:
1223 if (p->id2 < 0)
1224 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1225 p->id2, -p->id1 * 16);
1226 else
1227 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1228 p->id2, p->id1);
1229 break;
1230 case FT_EXT:
1231 sprintf(buf, "X%"PRIX32"", p->id1);
1232 break;
1233 case FT_EXTREL:
1234 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1235 break;
1236 case FT_EXTSEG:
1237 /* We needed a non-ieee hack here.
1238 * We introduce the Y variable, which is the low
1239 * limit of the native segment the extern resides in
1241 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1242 break;
1243 case FT_EXTWRT:
1244 if (p->id2 < 0)
1245 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1246 -p->id1 * 16);
1247 else
1248 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1249 break;
1251 ieee_putascii("LR(%s,%"PRIX32").\n", buf, size);
1253 return (size);
1256 /* Dump all segment data (text and fixups )*/
1258 static void ieee_unqualified_name(char *dest, char *source)
1260 if (ieee_uppercase) {
1261 while (*source)
1262 *dest++ = toupper(*source++);
1263 *dest = 0;
1264 } else
1265 strcpy(dest, source);
1267 static void dbgls_init(void)
1269 int tempint;
1271 fnhead = NULL;
1272 fntail = &fnhead;
1273 arrindex = ARRAY_BOT;
1274 arrhead = NULL;
1275 arrtail = &arrhead;
1276 ieee_segment("??LINE", &tempint);
1277 any_segs = false;
1279 static void dbgls_cleanup(void)
1281 struct ieeeSection *segtmp;
1282 while (fnhead) {
1283 struct FileName *fntemp = fnhead;
1284 fnhead = fnhead->next;
1285 nasm_free(fntemp->name);
1286 nasm_free(fntemp);
1288 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1289 while (segtmp->lochead) {
1290 struct ieeePublic *loctmp = segtmp->lochead;
1291 segtmp->lochead = loctmp->next;
1292 nasm_free(loctmp->name);
1293 nasm_free(loctmp);
1296 while (arrhead) {
1297 struct Array *arrtmp = arrhead;
1298 arrhead = arrhead->next;
1299 nasm_free(arrtmp);
1304 * because this routine is not bracketed in
1305 * the main program, this routine will be called even if there
1306 * is no request for debug info
1307 * so, we have to make sure the ??LINE segment is avaialbe
1308 * as the first segment when this debug format is selected
1310 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1312 struct FileName *fn;
1313 struct ieeeSection *seg;
1314 int i = 0;
1315 if (segto == NO_SEG)
1316 return;
1319 * If `any_segs' is still false, we must define a default
1320 * segment.
1322 if (!any_segs) {
1323 int tempint; /* ignored */
1324 if (segto != ieee_segment("__NASMDEFSEG", &tempint))
1325 nasm_panic("strange segment conditions in IEEE driver");
1329 * Find the segment we are targetting.
1331 for (seg = seghead; seg; seg = seg->next)
1332 if (seg->index == segto)
1333 break;
1334 if (!seg)
1335 nasm_panic("lineno directed to nonexistent segment?");
1337 for (fn = fnhead; fn; fn = fn->next) {
1338 if (!nasm_stricmp(lnfname, fn->name))
1339 break;
1340 i++;
1342 if (!fn) {
1343 fn = nasm_malloc(sizeof(*fn));
1344 fn->name = nasm_malloc(strlen(lnfname) + 1);
1345 fn->index = i;
1346 strcpy(fn->name, lnfname);
1347 fn->next = NULL;
1348 *fntail = fn;
1349 fntail = &fn->next;
1351 ieee_write_byte(seghead, fn->index);
1352 ieee_write_word(seghead, lineno);
1353 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1354 seg->currentpos);
1357 static void dbgls_deflabel(char *name, int32_t segment,
1358 int64_t offset, int is_global, char *special)
1360 struct ieeeSection *seg;
1362 /* Keep compiler from warning about special */
1363 (void)special;
1366 * Note: ..[^@] special symbols are filtered in labels.c
1370 * If it's a special-retry from pass two, discard it.
1372 if (is_global == 3)
1373 return;
1376 * Case (i):
1378 if (ieee_seg_needs_update)
1379 return;
1380 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
1381 return;
1383 if (segment >= SEG_ABS || segment == NO_SEG) {
1384 return;
1388 * If `any_segs' is still false, we might need to define a
1389 * default segment, if they're trying to declare a label in
1390 * `first_seg'. But the label should exist due to a prior
1391 * call to ieee_deflabel so we can skip that.
1394 for (seg = seghead; seg; seg = seg->next)
1395 if (seg->index == segment) {
1396 struct ieeePublic *loc;
1398 * Case (ii). Maybe MODPUB someday?
1400 if (!is_global) {
1401 last_defined = loc = nasm_malloc(sizeof(*loc));
1402 *seg->loctail = loc;
1403 seg->loctail = &loc->next;
1404 loc->next = NULL;
1405 loc->name = nasm_strdup(name);
1406 loc->offset = offset;
1407 loc->segment = -1;
1408 loc->index = seg->ieee_index;
1412 static void dbgls_typevalue(int32_t type)
1414 int elem = TYM_ELEMENTS(type);
1415 type = TYM_TYPE(type);
1417 if (!last_defined)
1418 return;
1420 switch (type) {
1421 case TY_BYTE:
1422 last_defined->type = 1; /* uint8_t */
1423 break;
1424 case TY_WORD:
1425 last_defined->type = 3; /* unsigned word */
1426 break;
1427 case TY_DWORD:
1428 last_defined->type = 5; /* unsigned dword */
1429 break;
1430 case TY_FLOAT:
1431 last_defined->type = 9; /* float */
1432 break;
1433 case TY_QWORD:
1434 last_defined->type = 10; /* qword */
1435 break;
1436 case TY_TBYTE:
1437 last_defined->type = 11; /* TBYTE */
1438 break;
1439 default:
1440 last_defined->type = 0x10; /* near label */
1441 break;
1444 if (elem > 1) {
1445 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
1446 int vtype = last_defined->type;
1447 arrtmp->size = elem;
1448 arrtmp->basetype = vtype;
1449 arrtmp->next = NULL;
1450 last_defined->type = arrindex++ + 0x100;
1451 *arrtail = arrtmp;
1452 arrtail = &(arrtmp->next);
1454 last_defined = NULL;
1456 static void dbgls_output(int output_type, void *param)
1458 (void)output_type;
1459 (void)param;
1461 static const struct dfmt ladsoft_debug_form = {
1462 "LADsoft Debug Records",
1463 "ladsoft",
1464 dbgls_init,
1465 dbgls_linnum,
1466 dbgls_deflabel,
1467 null_debug_directive,
1468 dbgls_typevalue,
1469 dbgls_output,
1470 dbgls_cleanup,
1471 NULL /* pragma list */
1473 static const struct dfmt * const ladsoft_debug_arr[3] = {
1474 &ladsoft_debug_form,
1475 &null_debug_form,
1476 NULL
1478 const struct ofmt of_ieee = {
1479 "IEEE-695 (LADsoft variant) object file format",
1480 "ieee",
1481 ".o",
1482 OFMT_TEXT,
1484 ladsoft_debug_arr,
1485 &ladsoft_debug_form,
1486 NULL,
1487 ieee_init,
1488 null_reset,
1489 nasm_do_legacy_output,
1490 ieee_out,
1491 ieee_deflabel,
1492 ieee_segment,
1493 NULL,
1494 ieee_sectalign,
1495 ieee_segbase,
1496 ieee_directive,
1497 ieee_cleanup,
1498 NULL /* pragma list */
1501 #endif /* OF_IEEE */