Don't fclose() the output in the backend
[nasm/sigaren-mirror.git] / output / outieee.c
blob003e4d06749edc9ef2cd65184c3704fd1a7a9764
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 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 efunc error;
91 static ldfunc deflabel;
92 static FILE *ofp;
93 static bool any_segs;
94 static int arrindex;
96 #define HUNKSIZE 1024 /* Size of the data hunk */
97 #define EXT_BLKSIZ 512
98 #define LDPERLINE 32 /* bytes per line in output */
100 struct ieeeSection;
102 struct LineNumber {
103 struct LineNumber *next;
104 struct ieeeSection *segment;
105 int32_t offset;
106 int32_t lineno;
109 static struct FileName {
110 struct FileName *next;
111 char *name;
112 int32_t index;
113 } *fnhead, **fntail;
115 static struct Array {
116 struct Array *next;
117 unsigned size;
118 int basetype;
119 } *arrhead, **arrtail;
121 static struct ieeePublic {
122 struct ieeePublic *next;
123 char *name;
124 int32_t offset;
125 int32_t segment; /* only if it's far-absolute */
126 int32_t index;
127 int type; /* for debug purposes */
128 } *fpubhead, **fpubtail, *last_defined;
130 static struct ieeeExternal {
131 struct ieeeExternal *next;
132 char *name;
133 int32_t commonsize;
134 } *exthead, **exttail;
136 static int externals;
138 static struct ExtBack {
139 struct ExtBack *next;
140 int index[EXT_BLKSIZ];
141 } *ebhead, **ebtail;
143 /* NOTE: the first segment MUST be the lineno segment */
144 static struct ieeeSection {
145 struct ieeeObjData *data, *datacurr;
146 struct ieeeSection *next;
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 enum {
154 CMB_PRIVATE = 0,
155 CMB_PUBLIC = 2,
156 CMB_COMMON = 6
157 } combine;
158 int32_t use32; /* is this segment 32-bit? */
159 struct ieeePublic *pubhead, **pubtail, *lochead, **loctail;
160 char *name;
161 } *seghead, **segtail, *ieee_seg_needs_update;
163 struct ieeeObjData {
164 struct ieeeObjData *next;
165 uint8_t data[HUNKSIZE];
168 struct ieeeFixupp {
169 struct ieeeFixupp *next;
170 enum {
171 FT_SEG = 0,
172 FT_REL = 1,
173 FT_OFS = 2,
174 FT_EXT = 3,
175 FT_WRT = 4,
176 FT_EXTREL = 5,
177 FT_EXTWRT = 6,
178 FT_EXTSEG = 7
179 } ftype;
180 int16_t size;
181 int32_t id1;
182 int32_t id2;
183 int32_t offset;
184 int32_t addend;
187 static int32_t ieee_entry_seg, ieee_entry_ofs;
188 static int checksum;
190 extern struct ofmt of_ieee;
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(int debuginfo);
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(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
212 (void)eval;
213 ofp = fp;
214 error = errfunc;
215 deflabel = ldef;
216 any_segs = false;
217 fpubhead = NULL;
218 fpubtail = &fpubhead;
219 exthead = NULL;
220 exttail = &exthead;
221 externals = 1;
222 ebhead = NULL;
223 ebtail = &ebhead;
224 seghead = ieee_seg_needs_update = NULL;
225 segtail = &seghead;
226 ieee_entry_seg = NO_SEG;
227 ieee_uppercase = false;
228 checksum = 0;
231 static int ieee_set_info(enum geninfo type, char **val)
233 (void)type;
234 (void)val;
236 return 0;
240 * Rundown
242 static void ieee_cleanup(int debuginfo)
244 ieee_write_file(debuginfo);
245 of_ieee.current_dfmt->cleanup();
246 while (seghead) {
247 struct ieeeSection *segtmp = seghead;
248 seghead = seghead->next;
249 while (segtmp->pubhead) {
250 struct ieeePublic *pubtmp = segtmp->pubhead;
251 segtmp->pubhead = pubtmp->next;
252 nasm_free(pubtmp);
254 while (segtmp->fptr) {
255 struct ieeeFixupp *fixtmp = segtmp->fptr;
256 segtmp->fptr = fixtmp->next;
257 nasm_free(fixtmp);
259 while (segtmp->data) {
260 struct ieeeObjData *dattmp = segtmp->data;
261 segtmp->data = dattmp->next;
262 nasm_free(dattmp);
264 nasm_free(segtmp);
266 while (fpubhead) {
267 struct ieeePublic *pubtmp = fpubhead;
268 fpubhead = fpubhead->next;
269 nasm_free(pubtmp);
271 while (exthead) {
272 struct ieeeExternal *exttmp = exthead;
273 exthead = exthead->next;
274 nasm_free(exttmp);
276 while (ebhead) {
277 struct ExtBack *ebtmp = ebhead;
278 ebhead = ebhead->next;
279 nasm_free(ebtmp);
284 * callback for labels
286 static void ieee_deflabel(char *name, int32_t segment,
287 int64_t offset, int is_global, char *special)
290 * We have three cases:
292 * (i) `segment' is a segment-base. If so, set the name field
293 * for the segment structure it refers to, and then
294 * return.
296 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
297 * Save the label position for later output of a PUBDEF record.
300 * (iii) `segment' is not one of our segments. Save the label
301 * position for later output of an EXTDEF.
303 struct ieeeExternal *ext;
304 struct ExtBack *eb;
305 struct ieeeSection *seg;
306 int i;
308 if (special) {
309 error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
312 * First check for the double-period, signifying something
313 * unusual.
315 if (name[0] == '.' && name[1] == '.') {
316 if (!strcmp(name, "..start")) {
317 ieee_entry_seg = segment;
318 ieee_entry_ofs = offset;
320 return;
324 * Case (i):
326 if (ieee_seg_needs_update) {
327 ieee_seg_needs_update->name = name;
328 return;
330 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
331 return;
334 * case (ii)
336 if (segment >= SEG_ABS) {
338 * SEG_ABS subcase of (ii).
340 if (is_global) {
341 struct ieeePublic *pub;
343 pub = *fpubtail = nasm_malloc(sizeof(*pub));
344 fpubtail = &pub->next;
345 pub->next = NULL;
346 pub->name = name;
347 pub->offset = offset;
348 pub->segment = segment & ~SEG_ABS;
350 return;
353 for (seg = seghead; seg && is_global; seg = seg->next)
354 if (seg->index == segment) {
355 struct ieeePublic *pub;
357 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
358 seg->pubtail = &pub->next;
359 pub->next = NULL;
360 pub->name = name;
361 pub->offset = offset;
362 pub->index = seg->ieee_index;
363 pub->segment = -1;
364 return;
368 * Case (iii).
370 if (is_global) {
371 ext = *exttail = nasm_malloc(sizeof(*ext));
372 ext->next = NULL;
373 exttail = &ext->next;
374 ext->name = name;
375 if (is_global == 2)
376 ext->commonsize = offset;
377 else
378 ext->commonsize = 0;
379 i = segment / 2;
380 eb = ebhead;
381 if (!eb) {
382 eb = *ebtail = nasm_malloc(sizeof(*eb));
383 eb->next = NULL;
384 ebtail = &eb->next;
386 while (i > EXT_BLKSIZ) {
387 if (eb && eb->next)
388 eb = eb->next;
389 else {
390 eb = *ebtail = nasm_malloc(sizeof(*eb));
391 eb->next = NULL;
392 ebtail = &eb->next;
394 i -= EXT_BLKSIZ;
396 eb->index[i] = externals++;
402 * Put data out
404 static void ieee_out(int32_t segto, const void *data,
405 enum out_type type, uint64_t size,
406 int32_t segment, int32_t wrt)
408 const uint8_t *ucdata;
409 int32_t ldata;
410 struct ieeeSection *seg;
413 * handle absolute-assembly (structure definitions)
415 if (segto == NO_SEG) {
416 if (type != OUT_RESERVE)
417 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
418 " space");
419 return;
423 * If `any_segs' is still false, we must define a default
424 * segment.
426 if (!any_segs) {
427 int tempint; /* ignored */
428 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
429 error(ERR_PANIC, "strange segment conditions in IEEE driver");
433 * Find the segment we are targetting.
435 for (seg = seghead; seg; seg = seg->next)
436 if (seg->index == segto)
437 break;
438 if (!seg)
439 error(ERR_PANIC, "code directed to nonexistent segment?");
441 if (type == OUT_RAWDATA) {
442 ucdata = data;
443 while (size--)
444 ieee_write_byte(seg, *ucdata++);
445 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
446 type == OUT_REL4ADR) {
447 if (segment == NO_SEG && type != OUT_ADDRESS)
448 error(ERR_NONFATAL, "relative call to absolute address not"
449 " supported by IEEE format");
450 ldata = *(int64_t *)data;
451 if (type == OUT_REL2ADR)
452 ldata += (size - 2);
453 if (type == OUT_REL4ADR)
454 ldata += (size - 4);
455 ieee_write_fixup(segment, wrt, seg, size, type, ldata);
456 if (size == 2)
457 ieee_write_word(seg, ldata);
458 else
459 ieee_write_dword(seg, ldata);
460 } else if (type == OUT_RESERVE) {
461 while (size--)
462 ieee_write_byte(seg, 0);
466 static void ieee_data_new(struct ieeeSection *segto)
469 if (!segto->data)
470 segto->data = segto->datacurr =
471 nasm_malloc(sizeof(*(segto->datacurr)));
472 else
473 segto->datacurr = segto->datacurr->next =
474 nasm_malloc(sizeof(*(segto->datacurr)));
475 segto->datacurr->next = NULL;
479 * this routine is unalduterated bloatware. I usually don't do this
480 * but I might as well see what it is like on a harmless program.
481 * If anyone wants to optimize this is a good canditate!
483 static void ieee_write_fixup(int32_t segment, int32_t wrt,
484 struct ieeeSection *segto, int size,
485 uint64_t realtype, int32_t offset)
487 struct ieeeSection *target;
488 struct ieeeFixupp s;
490 /* Don't put a fixup for things NASM can calculate */
491 if (wrt == NO_SEG && segment == NO_SEG)
492 return;
494 s.ftype = -1;
495 /* if it is a WRT offset */
496 if (wrt != NO_SEG) {
497 s.ftype = FT_WRT;
498 s.addend = offset;
499 if (wrt >= SEG_ABS)
500 s.id1 = -(wrt - SEG_ABS);
501 else {
502 if (wrt % 2 && realtype != OUT_REL2ADR
503 && realtype != OUT_REL4ADR) {
504 wrt--;
506 for (target = seghead; target; target = target->next)
507 if (target->index == wrt)
508 break;
509 if (target) {
510 s.id1 = target->ieee_index;
511 for (target = seghead; target; target = target->next)
512 if (target->index == segment)
513 break;
515 if (target)
516 s.id2 = target->ieee_index;
517 else {
519 * Now we assume the segment field is being used
520 * to hold an extern index
522 int32_t i = segment / 2;
523 struct ExtBack *eb = ebhead;
524 while (i > EXT_BLKSIZ) {
525 if (eb)
526 eb = eb->next;
527 else
528 break;
529 i -= EXT_BLKSIZ;
531 /* if we have an extern decide the type and make a record
533 if (eb) {
534 s.ftype = FT_EXTWRT;
535 s.addend = 0;
536 s.id2 = eb->index[i];
537 } else
538 error(ERR_NONFATAL,
539 "Source of WRT must be an offset");
542 } else
543 error(ERR_PANIC,
544 "unrecognised WRT value in ieee_write_fixup");
545 } else
546 error(ERR_NONFATAL, "target of WRT must be a section ");
548 s.size = size;
549 ieee_install_fixup(segto, &s);
550 return;
552 /* Pure segment fixup ? */
553 if (segment != NO_SEG) {
554 s.ftype = FT_SEG;
555 s.id1 = 0;
556 if (segment >= SEG_ABS) {
557 /* absolute far segment fixup */
558 s.id1 = -(segment - ~SEG_ABS);
559 } else if (segment % 2) {
560 /* fixup to named segment */
561 /* look it up */
562 for (target = seghead; target; target = target->next)
563 if (target->index == segment - 1)
564 break;
565 if (target)
566 s.id1 = target->ieee_index;
567 else {
569 * Now we assume the segment field is being used
570 * to hold an extern index
572 int32_t i = segment / 2;
573 struct ExtBack *eb = ebhead;
574 while (i > EXT_BLKSIZ) {
575 if (eb)
576 eb = eb->next;
577 else
578 break;
579 i -= EXT_BLKSIZ;
581 /* if we have an extern decide the type and make a record
583 if (eb) {
584 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
585 error(ERR_PANIC,
586 "Segment of a rel not supported in ieee_write_fixup");
587 } else {
588 /* If we want the segment */
589 s.ftype = FT_EXTSEG;
590 s.addend = 0;
591 s.id1 = eb->index[i];
594 } else
595 /* If we get here the seg value doesn't make sense */
596 error(ERR_PANIC,
597 "unrecognised segment value in ieee_write_fixup");
600 } else {
601 /* Assume we are offsetting directly from a section
602 * So look up the target segment
604 for (target = seghead; target; target = target->next)
605 if (target->index == segment)
606 break;
607 if (target) {
608 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
609 /* PC rel to a known offset */
610 s.id1 = target->ieee_index;
611 s.ftype = FT_REL;
612 s.size = size;
613 s.addend = offset;
614 } else {
615 /* We were offsetting from a seg */
616 s.id1 = target->ieee_index;
617 s.ftype = FT_OFS;
618 s.size = size;
619 s.addend = offset;
621 } else {
623 * Now we assume the segment field is being used
624 * to hold an extern index
626 int32_t i = segment / 2;
627 struct ExtBack *eb = ebhead;
628 while (i > EXT_BLKSIZ) {
629 if (eb)
630 eb = eb->next;
631 else
632 break;
633 i -= EXT_BLKSIZ;
635 /* if we have an extern decide the type and make a record
637 if (eb) {
638 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
639 s.ftype = FT_EXTREL;
640 s.addend = 0;
641 s.id1 = eb->index[i];
642 } else {
643 /* else we want the external offset */
644 s.ftype = FT_EXT;
645 s.addend = 0;
646 s.id1 = eb->index[i];
649 } else
650 /* If we get here the seg value doesn't make sense */
651 error(ERR_PANIC,
652 "unrecognised segment value in ieee_write_fixup");
655 if (size != 2 && s.ftype == FT_SEG)
656 error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
657 " segment base references");
658 s.size = size;
659 ieee_install_fixup(segto, &s);
660 return;
662 /* should never get here */
664 static void ieee_install_fixup(struct ieeeSection *seg,
665 struct ieeeFixupp *fix)
667 struct ieeeFixupp *f;
668 f = nasm_malloc(sizeof(struct ieeeFixupp));
669 memcpy(f, fix, sizeof(struct ieeeFixupp));
670 f->offset = seg->currentpos;
671 seg->currentpos += fix->size;
672 f->next = NULL;
673 if (seg->fptr)
674 seg->flptr = seg->flptr->next = f;
675 else
676 seg->fptr = seg->flptr = f;
681 * segment registry
683 static int32_t ieee_segment(char *name, int pass, int *bits)
686 * We call the label manager here to define a name for the new
687 * segment, and when our _own_ label-definition stub gets
688 * called in return, it should register the new segment name
689 * using the pointer it gets passed. That way we save memory,
690 * by sponging off the label manager.
692 if (!name) {
693 *bits = 16;
694 if (!any_segs)
695 return 0;
696 return seghead->index;
697 } else {
698 struct ieeeSection *seg;
699 int ieee_idx, attrs;
700 bool rn_error;
701 char *p;
704 * Look for segment attributes.
706 attrs = 0;
707 while (*name == '.')
708 name++; /* hack, but a documented one */
709 p = name;
710 while (*p && !nasm_isspace(*p))
711 p++;
712 if (*p) {
713 *p++ = '\0';
714 while (*p && nasm_isspace(*p))
715 *p++ = '\0';
717 while (*p) {
718 while (*p && !nasm_isspace(*p))
719 p++;
720 if (*p) {
721 *p++ = '\0';
722 while (*p && nasm_isspace(*p))
723 *p++ = '\0';
726 attrs++;
729 ieee_idx = 1;
730 for (seg = seghead; seg; seg = seg->next) {
731 ieee_idx++;
732 if (!strcmp(seg->name, name)) {
733 if (attrs > 0 && pass == 1)
734 error(ERR_WARNING, "segment attributes specified on"
735 " redeclaration of segment: ignoring");
736 if (seg->use32)
737 *bits = 32;
738 else
739 *bits = 16;
740 return seg->index;
744 *segtail = seg = nasm_malloc(sizeof(*seg));
745 seg->next = NULL;
746 segtail = &seg->next;
747 seg->index = seg_alloc();
748 seg->ieee_index = ieee_idx;
749 any_segs = true;
750 seg->name = NULL;
751 seg->currentpos = 0;
752 seg->align = 1; /* default */
753 seg->use32 = *bits == 32; /* default to user spec */
754 seg->combine = CMB_PUBLIC; /* default */
755 seg->pubhead = NULL;
756 seg->pubtail = &seg->pubhead;
757 seg->data = NULL;
758 seg->fptr = NULL;
759 seg->lochead = NULL;
760 seg->loctail = &seg->lochead;
763 * Process the segment attributes.
765 p = name;
766 while (attrs--) {
767 p += strlen(p);
768 while (!*p)
769 p++;
772 * `p' contains a segment attribute.
774 if (!nasm_stricmp(p, "private"))
775 seg->combine = CMB_PRIVATE;
776 else if (!nasm_stricmp(p, "public"))
777 seg->combine = CMB_PUBLIC;
778 else if (!nasm_stricmp(p, "common"))
779 seg->combine = CMB_COMMON;
780 else if (!nasm_stricmp(p, "use16"))
781 seg->use32 = false;
782 else if (!nasm_stricmp(p, "use32"))
783 seg->use32 = true;
784 else if (!nasm_strnicmp(p, "align=", 6)) {
785 seg->align = readnum(p + 6, &rn_error);
786 if (seg->align == 0)
787 seg->align = 1;
788 if (rn_error) {
789 seg->align = 1;
790 error(ERR_NONFATAL, "segment alignment should be"
791 " numeric");
793 switch ((int)seg->align) {
794 case 1: /* BYTE */
795 case 2: /* WORD */
796 case 4: /* DWORD */
797 case 16: /* PARA */
798 case 256: /* PAGE */
799 case 8:
800 case 32:
801 case 64:
802 case 128:
803 break;
804 default:
805 error(ERR_NONFATAL, "invalid alignment value %d",
806 seg->align);
807 seg->align = 1;
808 break;
810 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
811 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
812 if (rn_error)
813 error(ERR_NONFATAL, "argument to `absolute' segment"
814 " attribute should be numeric");
818 ieee_seg_needs_update = seg;
819 if (seg->align >= SEG_ABS)
820 deflabel(name, NO_SEG, seg->align - SEG_ABS,
821 NULL, false, false, &of_ieee, error);
822 else
823 deflabel(name, seg->index + 1, 0L,
824 NULL, false, false, &of_ieee, error);
825 ieee_seg_needs_update = NULL;
827 if (seg->use32)
828 *bits = 32;
829 else
830 *bits = 16;
831 return seg->index;
836 * directives supported
838 static int ieee_directive(char *directive, char *value, int pass)
841 (void)value;
842 (void)pass;
843 if (!strcmp(directive, "uppercase")) {
844 ieee_uppercase = true;
845 return 1;
847 return 0;
851 * Return segment data
853 static int32_t ieee_segbase(int32_t segment)
855 struct ieeeSection *seg;
858 * Find the segment in our list.
860 for (seg = seghead; seg; seg = seg->next)
861 if (seg->index == segment - 1)
862 break;
864 if (!seg)
865 return segment; /* not one of ours - leave it alone */
867 if (seg->align >= SEG_ABS)
868 return seg->align; /* absolute segment */
870 return segment; /* no special treatment */
874 * filename
876 static void ieee_filename(char *inname, char *outname, efunc error)
878 strcpy(ieee_infile, inname);
879 standard_extension(inname, outname, ".o", error);
882 static void ieee_write_file(int debuginfo)
884 struct tm *thetime;
885 time_t reltime;
886 struct FileName *fn;
887 struct ieeeSection *seg;
888 struct ieeePublic *pub, *loc;
889 struct ieeeExternal *ext;
890 struct ieeeObjData *data;
891 struct ieeeFixupp *fix;
892 struct Array *arr;
893 int i;
896 * Write the module header
898 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile), ieee_infile);
901 * Write the NASM boast comment.
903 ieee_putascii("CO0,%02X%s.\n", strlen(nasm_comment), nasm_comment);
906 * write processor-specific information
908 ieee_putascii("AD8,4,L.\n");
911 * date and time
913 time(&reltime);
914 thetime = localtime(&reltime);
915 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
916 1900 + thetime->tm_year, thetime->tm_mon + 1,
917 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
918 thetime->tm_sec);
920 * if debugging, dump file names
922 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
923 ieee_putascii("C0105,%02X%s.\n", strlen(fn->name), fn->name);
926 ieee_putascii("CO101,07ENDHEAD.\n");
928 * the standard doesn't specify when to put checksums,
929 * we'll just do it periodically.
931 ieee_putcs(false);
934 * Write the section headers
936 seg = seghead;
937 if (!debuginfo && !strcmp(seg->name, "??LINE"))
938 seg = seg->next;
939 while (seg) {
940 char buf[256];
941 char attrib;
942 switch (seg->combine) {
943 case CMB_PUBLIC:
944 default:
945 attrib = 'C';
946 break;
947 case CMB_PRIVATE:
948 attrib = 'S';
949 break;
950 case CMB_COMMON:
951 attrib = 'M';
952 break;
954 ieee_unqualified_name(buf, seg->name);
955 if (seg->align >= SEG_ABS) {
956 ieee_putascii("ST%X,A,%02X%s.\n", seg->ieee_index,
957 strlen(buf), buf);
958 ieee_putascii("ASL%X,%lX.\n", seg->ieee_index,
959 (seg->align - SEG_ABS) * 16);
960 } else {
961 ieee_putascii("ST%X,%c,%02X%s.\n", seg->ieee_index, attrib,
962 strlen(buf), buf);
963 ieee_putascii("SA%X,%lX.\n", seg->ieee_index, seg->align);
964 ieee_putascii("ASS%X,%X.\n", seg->ieee_index,
965 seg->currentpos);
967 seg = seg->next;
970 * write the start address if there is one
972 if (ieee_entry_seg) {
973 for (seg = seghead; seg; seg = seg->next)
974 if (seg->index == ieee_entry_seg)
975 break;
976 if (!seg)
977 error(ERR_PANIC, "Start address records are incorrect");
978 else
979 ieee_putascii("ASG,R%X,%lX,+.\n", seg->ieee_index,
980 ieee_entry_ofs);
983 ieee_putcs(false);
985 * Write the publics
987 i = 1;
988 for (seg = seghead; seg; seg = seg->next) {
989 for (pub = seg->pubhead; pub; pub = pub->next) {
990 char buf[256];
991 ieee_unqualified_name(buf, pub->name);
992 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
993 if (pub->segment == -1)
994 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
995 pub->offset);
996 else
997 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
998 pub->offset);
999 if (debuginfo) {
1000 if (pub->type >= 0x100)
1001 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1002 else
1003 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1005 i++;
1008 pub = fpubhead;
1009 i = 1;
1010 while (pub) {
1011 char buf[256];
1012 ieee_unqualified_name(buf, pub->name);
1013 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1014 if (pub->segment == -1)
1015 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1016 pub->offset);
1017 else
1018 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1019 pub->offset);
1020 if (debuginfo) {
1021 if (pub->type >= 0x100)
1022 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1023 else
1024 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1026 i++;
1027 pub = pub->next;
1030 * Write the externals
1032 ext = exthead;
1033 i = 1;
1034 while (ext) {
1035 char buf[256];
1036 ieee_unqualified_name(buf, ext->name);
1037 ieee_putascii("NX%X,%02X%s.\n", i++, strlen(buf), buf);
1038 ext = ext->next;
1040 ieee_putcs(false);
1043 * IEEE doesn't have a standard pass break record
1044 * so use the ladsoft variant
1046 ieee_putascii("CO100,06ENDSYM.\n");
1049 * now put types
1051 i = ARRAY_BOT;
1052 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1053 ieee_putascii("TY%X,20,%X,%lX.\n", i++, arr->basetype,
1054 arr->size);
1057 * now put locals
1059 i = 1;
1060 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1061 for (loc = seg->lochead; loc; loc = loc->next) {
1062 char buf[256];
1063 ieee_unqualified_name(buf, loc->name);
1064 ieee_putascii("NN%X,%02X%s.\n", i, strlen(buf), buf);
1065 if (loc->segment == -1)
1066 ieee_putascii("ASN%X,R%X,%lX,+.\n", i, loc->index,
1067 loc->offset);
1068 else
1069 ieee_putascii("ASN%X,%lX,%lX,+.\n", i, loc->segment * 16,
1070 loc->offset);
1071 if (debuginfo) {
1072 if (loc->type >= 0x100)
1073 ieee_putascii("ATN%X,T%X.\n", i, loc->type - 0x100);
1074 else
1075 ieee_putascii("ATN%X,%X.\n", i, loc->type);
1077 i++;
1082 * put out section data;
1084 seg = seghead;
1085 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1086 seg = seg->next;
1087 while (seg) {
1088 if (seg->currentpos) {
1089 int32_t size, org = 0;
1090 data = seg->data;
1091 ieee_putascii("SB%X.\n", seg->ieee_index);
1092 fix = seg->fptr;
1093 while (fix) {
1094 size = HUNKSIZE - (org % HUNKSIZE);
1095 size =
1096 size + org >
1097 seg->currentpos ? seg->currentpos - org : size;
1098 size = fix->offset - org > size ? size : fix->offset - org;
1099 org = ieee_putld(org, org + size, data->data);
1100 if (org % HUNKSIZE == 0)
1101 data = data->next;
1102 if (org == fix->offset) {
1103 org += ieee_putlr(fix);
1104 fix = fix->next;
1107 while (org < seg->currentpos && data) {
1108 size =
1109 seg->currentpos - org >
1110 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1111 org = ieee_putld(org, org + size, data->data);
1112 data = data->next;
1114 ieee_putcs(false);
1117 seg = seg->next;
1120 * module end record
1122 ieee_putascii("ME.\n");
1125 static void ieee_write_byte(struct ieeeSection *seg, int data)
1127 int temp;
1128 if (!(temp = seg->currentpos++ % HUNKSIZE))
1129 ieee_data_new(seg);
1130 seg->datacurr->data[temp] = data;
1133 static void ieee_write_word(struct ieeeSection *seg, int data)
1135 ieee_write_byte(seg, data & 0xFF);
1136 ieee_write_byte(seg, (data >> 8) & 0xFF);
1139 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1141 ieee_write_byte(seg, data & 0xFF);
1142 ieee_write_byte(seg, (data >> 8) & 0xFF);
1143 ieee_write_byte(seg, (data >> 16) & 0xFF);
1144 ieee_write_byte(seg, (data >> 24) & 0xFF);
1146 static void ieee_putascii(char *format, ...)
1148 char buffer[256];
1149 int i, l;
1150 va_list ap;
1152 va_start(ap, format);
1153 vsnprintf(buffer, sizeof(buffer), format, ap);
1154 l = strlen(buffer);
1155 for (i = 0; i < l; i++)
1156 if ((uint8_t)buffer[i] > 31)
1157 checksum += buffer[i];
1158 va_end(ap);
1159 fprintf(ofp, buffer);
1163 * put out a checksum record */
1164 static void ieee_putcs(int toclear)
1166 if (toclear) {
1167 ieee_putascii("CS.\n");
1168 } else {
1169 checksum += 'C';
1170 checksum += 'S';
1171 ieee_putascii("CS%02X.\n", checksum & 127);
1173 checksum = 0;
1176 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1178 int32_t val;
1179 if (start == end)
1180 return (start);
1181 val = start % HUNKSIZE;
1182 /* fill up multiple lines */
1183 while (end - start >= LDPERLINE) {
1184 int i;
1185 ieee_putascii("LD");
1186 for (i = 0; i < LDPERLINE; i++) {
1187 ieee_putascii("%02X", buf[val++]);
1188 start++;
1190 ieee_putascii(".\n");
1192 /* if no partial lines */
1193 if (start == end)
1194 return (start);
1195 /* make a partial line */
1196 ieee_putascii("LD");
1197 while (start < end) {
1198 ieee_putascii("%02X", buf[val++]);
1199 start++;
1201 ieee_putascii(".\n");
1202 return (start);
1204 static int32_t ieee_putlr(struct ieeeFixupp *p)
1207 * To deal with the vagaries of segmentation the LADsoft linker
1208 * defines two types of segments: absolute and virtual. Note that
1209 * 'absolute' in this context is a different thing from the IEEE
1210 * definition of an absolute segment type, which is also supported. If a
1211 * sement is linked in virtual mode the low limit (L-var) is
1212 * subtracted from each R,X, and P variable which appears in an
1213 * expression, so that we can have relative offsets. Meanwhile
1214 * in the ABSOLUTE mode this subtraction is not done and
1215 * so we can use absolute offsets from 0. In the LADsoft linker
1216 * this configuration is not done in the assemblker source but in
1217 * a source the linker reads. Generally this type of thing only
1218 * becomes an issue if real mode code is used. A pure 32-bit linker could
1219 * get away without defining the virtual mode...
1221 char buf[40];
1222 int32_t size = p->size;
1223 switch (p->ftype) {
1224 case FT_SEG:
1225 if (p->id1 < 0)
1226 sprintf(buf, "%"PRIX32"", -p->id1);
1227 else
1228 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1229 break;
1230 case FT_OFS:
1231 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1232 break;
1233 case FT_REL:
1234 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1235 break;
1237 case FT_WRT:
1238 if (p->id2 < 0)
1239 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1240 p->id2, -p->id1 * 16);
1241 else
1242 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1243 p->id2, p->id1);
1244 break;
1245 case FT_EXT:
1246 sprintf(buf, "X%"PRIX32"", p->id1);
1247 break;
1248 case FT_EXTREL:
1249 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1250 break;
1251 case FT_EXTSEG:
1252 /* We needed a non-ieee hack here.
1253 * We introduce the Y variable, which is the low
1254 * limit of the native segment the extern resides in
1256 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1257 break;
1258 case FT_EXTWRT:
1259 if (p->id2 < 0)
1260 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1261 -p->id1 * 16);
1262 else
1263 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1264 break;
1266 ieee_putascii("LR(%s,%"PRIX32").\n", buf, size);
1268 return (size);
1271 /* Dump all segment data (text and fixups )*/
1273 static void ieee_unqualified_name(char *dest, char *source)
1275 if (ieee_uppercase) {
1276 while (*source)
1277 *dest++ = toupper(*source++);
1278 *dest = 0;
1279 } else
1280 strcpy(dest, source);
1282 void dbgls_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1284 int tempint;
1285 (void)of;
1286 (void)id;
1287 (void)fp;
1288 (void)error;
1290 fnhead = NULL;
1291 fntail = &fnhead;
1292 arrindex = ARRAY_BOT;
1293 arrhead = NULL;
1294 arrtail = &arrhead;
1295 ieee_segment("??LINE", 2, &tempint);
1296 any_segs = false;
1298 static void dbgls_cleanup(void)
1300 struct ieeeSection *segtmp;
1301 while (fnhead) {
1302 struct FileName *fntemp = fnhead;
1303 fnhead = fnhead->next;
1304 nasm_free(fntemp->name);
1305 nasm_free(fntemp);
1307 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1308 while (segtmp->lochead) {
1309 struct ieeePublic *loctmp = segtmp->lochead;
1310 segtmp->lochead = loctmp->next;
1311 nasm_free(loctmp->name);
1312 nasm_free(loctmp);
1315 while (arrhead) {
1316 struct Array *arrtmp = arrhead;
1317 arrhead = arrhead->next;
1318 nasm_free(arrtmp);
1323 * because this routine is not bracketed in
1324 * the main program, this routine will be called even if there
1325 * is no request for debug info
1326 * so, we have to make sure the ??LINE segment is avaialbe
1327 * as the first segment when this debug format is selected
1329 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1331 struct FileName *fn;
1332 struct ieeeSection *seg;
1333 int i = 0;
1334 if (segto == NO_SEG)
1335 return;
1338 * If `any_segs' is still false, we must define a default
1339 * segment.
1341 if (!any_segs) {
1342 int tempint; /* ignored */
1343 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1344 error(ERR_PANIC, "strange segment conditions in OBJ driver");
1348 * Find the segment we are targetting.
1350 for (seg = seghead; seg; seg = seg->next)
1351 if (seg->index == segto)
1352 break;
1353 if (!seg)
1354 error(ERR_PANIC, "lineno directed to nonexistent segment?");
1356 for (fn = fnhead; fn; fn = fn->next) {
1357 if (!nasm_stricmp(lnfname, fn->name))
1358 break;
1359 i++;
1361 if (!fn) {
1362 fn = nasm_malloc(sizeof(*fn));
1363 fn->name = nasm_malloc(strlen(lnfname) + 1);
1364 fn->index = i;
1365 strcpy(fn->name, lnfname);
1366 fn->next = NULL;
1367 *fntail = fn;
1368 fntail = &fn->next;
1370 ieee_write_byte(seghead, fn->index);
1371 ieee_write_word(seghead, lineno);
1372 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1373 seg->currentpos);
1376 static void dbgls_deflabel(char *name, int32_t segment,
1377 int64_t offset, int is_global, char *special)
1379 struct ieeeSection *seg;
1381 /* Keep compiler from warning about special */
1382 (void)special;
1385 * If it's a special-retry from pass two, discard it.
1387 if (is_global == 3)
1388 return;
1391 * First check for the double-period, signifying something
1392 * unusual.
1394 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
1395 return;
1399 * Case (i):
1401 if (ieee_seg_needs_update)
1402 return;
1403 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
1404 return;
1406 if (segment >= SEG_ABS || segment == NO_SEG) {
1407 return;
1411 * If `any_segs' is still false, we might need to define a
1412 * default segment, if they're trying to declare a label in
1413 * `first_seg'. But the label should exist due to a prior
1414 * call to ieee_deflabel so we can skip that.
1417 for (seg = seghead; seg; seg = seg->next)
1418 if (seg->index == segment) {
1419 struct ieeePublic *loc;
1421 * Case (ii). Maybe MODPUB someday?
1423 if (!is_global) {
1424 last_defined = loc = nasm_malloc(sizeof(*loc));
1425 *seg->loctail = loc;
1426 seg->loctail = &loc->next;
1427 loc->next = NULL;
1428 loc->name = nasm_strdup(name);
1429 loc->offset = offset;
1430 loc->segment = -1;
1431 loc->index = seg->ieee_index;
1435 static void dbgls_typevalue(int32_t type)
1437 int elem = TYM_ELEMENTS(type);
1438 type = TYM_TYPE(type);
1440 if (!last_defined)
1441 return;
1443 switch (type) {
1444 case TY_BYTE:
1445 last_defined->type = 1; /* uint8_t */
1446 break;
1447 case TY_WORD:
1448 last_defined->type = 3; /* unsigned word */
1449 break;
1450 case TY_DWORD:
1451 last_defined->type = 5; /* unsigned dword */
1452 break;
1453 case TY_FLOAT:
1454 last_defined->type = 9; /* float */
1455 break;
1456 case TY_QWORD:
1457 last_defined->type = 10; /* qword */
1458 break;
1459 case TY_TBYTE:
1460 last_defined->type = 11; /* TBYTE */
1461 break;
1462 default:
1463 last_defined->type = 0x10; /* near label */
1464 break;
1467 if (elem > 1) {
1468 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
1469 int vtype = last_defined->type;
1470 arrtmp->size = elem;
1471 arrtmp->basetype = vtype;
1472 arrtmp->next = NULL;
1473 last_defined->type = arrindex++ + 0x100;
1474 *arrtail = arrtmp;
1475 arrtail = &(arrtmp->next);
1477 last_defined = NULL;
1479 static void dbgls_output(int output_type, void *param)
1481 (void)output_type;
1482 (void)param;
1484 static struct dfmt ladsoft_debug_form = {
1485 "LADsoft Debug Records",
1486 "ladsoft",
1487 dbgls_init,
1488 dbgls_linnum,
1489 dbgls_deflabel,
1490 null_debug_routine,
1491 dbgls_typevalue,
1492 dbgls_output,
1493 dbgls_cleanup,
1495 static struct dfmt *ladsoft_debug_arr[3] = {
1496 &ladsoft_debug_form,
1497 &null_debug_form,
1498 NULL
1500 struct ofmt of_ieee = {
1501 "IEEE-695 (LADsoft variant) object file format",
1502 "ieee",
1503 OFMT_TEXT,
1504 ladsoft_debug_arr,
1505 &ladsoft_debug_form,
1506 NULL,
1507 ieee_init,
1508 ieee_set_info,
1509 ieee_out,
1510 ieee_deflabel,
1511 ieee_segment,
1512 ieee_segbase,
1513 ieee_directive,
1514 ieee_filename,
1515 ieee_cleanup
1518 #endif /* OF_IEEE */