Apply Nindent to all .c and .h files
[nasm/avx512.git] / output / outieee.c
blob7d723900fd051cb3bfcd0170024b33164d5a1cad
1 /* outieee.c output routines for the Netwide Assembler to produce
2 * IEEE-std object files
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
8 */
10 /* notes: I have tried to make this correspond to the IEEE version
11 * of the standard, specifically the primary ASCII version. It should
12 * be trivial to create the binary version given this source (which is
13 * one of MANY things that have to be done to make this correspond to
14 * the hp-microtek version of the standard).
16 * 16-bit support is assumed to use 24-bit addresses
17 * The linker can sort out segmentation-specific stuff
18 * if it keeps track of externals
19 * in terms of being relative to section bases
21 * A non-standard variable type, the 'Yn' variable, has been introduced.
22 * Basically it is a reference to extern 'n'- denoting the low limit
23 * (L-variable) of the section that extern 'n' is defined in. Like the
24 * x variable, there may be no explicit assignment to it, it is derived
25 * from the public definition corresponding to the extern name. This
26 * is required because the one thing the mufom guys forgot to do well was
27 * take into account segmented architectures.
29 * I use comment classes for various things and these are undefined by
30 * the standard.
32 * Debug info should be considered totally non-standard (local labels are
33 * standard but linenum records are not covered by the standard.
34 * Type defs have the standard format but absolute meanings for ordinal
35 * types are not covered by the standard.)
37 * David Lindauer, LADsoft
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43 #include <stdarg.h> /* Note: we need the ANSI version of stdarg.h */
44 #include <ctype.h>
46 #include "nasm.h"
47 #include "nasmlib.h"
48 #include "outform.h"
50 #ifdef OF_IEEE
52 #define ARRAY_BOT 0x1
54 static char ieee_infile[FILENAME_MAX];
55 static int ieee_uppercase;
57 static efunc error;
58 static ldfunc deflabel;
59 static FILE *ofp;
60 static int any_segs;
61 static int arrindex;
63 #define HUNKSIZE 1024 /* Size of the data hunk */
64 #define EXT_BLKSIZ 512
65 #define LDPERLINE 32 /* bytes per line in output */
67 struct ieeeSection;
69 struct LineNumber {
70 struct LineNumber *next;
71 struct ieeeSection *segment;
72 long offset;
73 long lineno;
76 static struct FileName {
77 struct FileName *next;
78 char *name;
79 long index;
80 } *fnhead, **fntail;
82 static struct Array {
83 struct Array *next;
84 unsigned size;
85 int basetype;
86 } *arrhead, **arrtail;
88 static struct ieeePublic {
89 struct ieeePublic *next;
90 char *name;
91 long offset;
92 long segment; /* only if it's far-absolute */
93 long index;
94 int type; /* for debug purposes */
95 } *fpubhead, **fpubtail, *last_defined;
97 static struct ieeeExternal {
98 struct ieeeExternal *next;
99 char *name;
100 long commonsize;
101 } *exthead, **exttail;
103 static int externals;
105 static struct ExtBack {
106 struct ExtBack *next;
107 int index[EXT_BLKSIZ];
108 } *ebhead, **ebtail;
110 /* NOTE: the first segment MUST be the lineno segment */
111 static struct ieeeSection {
112 struct ieeeObjData *data, *datacurr;
113 struct ieeeSection *next;
114 struct ieeeFixupp *fptr, *flptr;
115 long index; /* the NASM segment id */
116 long ieee_index; /* the OBJ-file segment index */
117 long currentpos;
118 long align; /* can be SEG_ABS + absolute addr */
119 long startpos;
120 enum {
121 CMB_PRIVATE = 0,
122 CMB_PUBLIC = 2,
123 CMB_COMMON = 6
124 } combine;
125 long use32; /* is this segment 32-bit? */
126 struct ieeePublic *pubhead, **pubtail, *lochead, **loctail;
127 char *name;
128 } *seghead, **segtail, *ieee_seg_needs_update;
130 struct ieeeObjData {
131 struct ieeeObjData *next;
132 unsigned char data[HUNKSIZE];
135 struct ieeeFixupp {
136 struct ieeeFixupp *next;
137 enum {
138 FT_SEG = 0,
139 FT_REL = 1,
140 FT_OFS = 2,
141 FT_EXT = 3,
142 FT_WRT = 4,
143 FT_EXTREL = 5,
144 FT_EXTWRT = 6,
145 FT_EXTSEG = 7
146 } ftype;
147 short size;
148 long id1;
149 long id2;
150 long offset;
151 long addend;
154 static long ieee_entry_seg, ieee_entry_ofs;
155 static int checksum;
157 extern struct ofmt of_ieee;
159 static void ieee_data_new(struct ieeeSection *);
160 static void ieee_write_fixup(long, long, struct ieeeSection *,
161 int, unsigned long, long);
162 static void ieee_install_fixup(struct ieeeSection *, struct ieeeFixupp *);
163 static long ieee_segment(char *, int, int *);
164 static void ieee_write_file(int debuginfo);
165 static void ieee_write_byte(struct ieeeSection *, int);
166 static void ieee_write_word(struct ieeeSection *, int);
167 static void ieee_write_dword(struct ieeeSection *, long);
168 static void ieee_putascii(char *, ...);
169 static void ieee_putcs(int);
170 static long ieee_putld(long, long, unsigned char *);
171 static long ieee_putlr(struct ieeeFixupp *);
172 static void ieee_unqualified_name(char *, char *);
175 * pup init
177 static void ieee_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
179 (void)eval;
180 ofp = fp;
181 error = errfunc;
182 deflabel = ldef;
183 any_segs = FALSE;
184 fpubhead = NULL;
185 fpubtail = &fpubhead;
186 exthead = NULL;
187 exttail = &exthead;
188 externals = 1;
189 ebhead = NULL;
190 ebtail = &ebhead;
191 seghead = ieee_seg_needs_update = NULL;
192 segtail = &seghead;
193 ieee_entry_seg = NO_SEG;
194 ieee_uppercase = FALSE;
195 checksum = 0;
196 of_ieee.current_dfmt->init(&of_ieee, NULL, fp, errfunc);
198 static int ieee_set_info(enum geninfo type, char **val)
200 (void)type;
201 (void)val;
203 return 0;
207 * Rundown
209 static void ieee_cleanup(int debuginfo)
211 ieee_write_file(debuginfo);
212 of_ieee.current_dfmt->cleanup();
213 fclose(ofp);
214 while (seghead) {
215 struct ieeeSection *segtmp = seghead;
216 seghead = seghead->next;
217 while (segtmp->pubhead) {
218 struct ieeePublic *pubtmp = segtmp->pubhead;
219 segtmp->pubhead = pubtmp->next;
220 nasm_free(pubtmp);
222 while (segtmp->fptr) {
223 struct ieeeFixupp *fixtmp = segtmp->fptr;
224 segtmp->fptr = fixtmp->next;
225 nasm_free(fixtmp);
227 while (segtmp->data) {
228 struct ieeeObjData *dattmp = segtmp->data;
229 segtmp->data = dattmp->next;
230 nasm_free(dattmp);
232 nasm_free(segtmp);
234 while (fpubhead) {
235 struct ieeePublic *pubtmp = fpubhead;
236 fpubhead = fpubhead->next;
237 nasm_free(pubtmp);
239 while (exthead) {
240 struct ieeeExternal *exttmp = exthead;
241 exthead = exthead->next;
242 nasm_free(exttmp);
244 while (ebhead) {
245 struct ExtBack *ebtmp = ebhead;
246 ebhead = ebhead->next;
247 nasm_free(ebtmp);
252 * callback for labels
254 static void ieee_deflabel(char *name, long segment,
255 long offset, int is_global, char *special)
258 * We have three cases:
260 * (i) `segment' is a segment-base. If so, set the name field
261 * for the segment structure it refers to, and then
262 * return.
264 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
265 * Save the label position for later output of a PUBDEF record.
268 * (iii) `segment' is not one of our segments. Save the label
269 * position for later output of an EXTDEF.
271 struct ieeeExternal *ext;
272 struct ExtBack *eb;
273 struct ieeeSection *seg;
274 int i;
276 if (special) {
277 error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
280 * First check for the double-period, signifying something
281 * unusual.
283 if (name[0] == '.' && name[1] == '.') {
284 if (!strcmp(name, "..start")) {
285 ieee_entry_seg = segment;
286 ieee_entry_ofs = offset;
288 return;
292 * Case (i):
294 if (ieee_seg_needs_update) {
295 ieee_seg_needs_update->name = name;
296 return;
298 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
299 return;
302 * case (ii)
304 if (segment >= SEG_ABS) {
306 * SEG_ABS subcase of (ii).
308 if (is_global) {
309 struct ieeePublic *pub;
311 pub = *fpubtail = nasm_malloc(sizeof(*pub));
312 fpubtail = &pub->next;
313 pub->next = NULL;
314 pub->name = name;
315 pub->offset = offset;
316 pub->segment = segment & ~SEG_ABS;
318 return;
321 for (seg = seghead; seg && is_global; seg = seg->next)
322 if (seg->index == segment) {
323 struct ieeePublic *pub;
325 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
326 seg->pubtail = &pub->next;
327 pub->next = NULL;
328 pub->name = name;
329 pub->offset = offset;
330 pub->index = seg->ieee_index;
331 pub->segment = -1;
332 return;
336 * Case (iii).
338 if (is_global) {
339 ext = *exttail = nasm_malloc(sizeof(*ext));
340 ext->next = NULL;
341 exttail = &ext->next;
342 ext->name = name;
343 if (is_global == 2)
344 ext->commonsize = offset;
345 else
346 ext->commonsize = 0;
347 i = segment / 2;
348 eb = ebhead;
349 if (!eb) {
350 eb = *ebtail = nasm_malloc(sizeof(*eb));
351 eb->next = NULL;
352 ebtail = &eb->next;
354 while (i > EXT_BLKSIZ) {
355 if (eb && eb->next)
356 eb = eb->next;
357 else {
358 eb = *ebtail = nasm_malloc(sizeof(*eb));
359 eb->next = NULL;
360 ebtail = &eb->next;
362 i -= EXT_BLKSIZ;
364 eb->index[i] = externals++;
370 * Put data out
372 static void ieee_out(long segto, const void *data, unsigned long type,
373 long segment, long wrt)
375 unsigned long size, realtype;
376 const unsigned char *ucdata;
377 long ldata;
378 struct ieeeSection *seg;
381 * handle absolute-assembly (structure definitions)
383 if (segto == NO_SEG) {
384 if ((type & OUT_TYPMASK) != OUT_RESERVE)
385 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
386 " space");
387 return;
391 * If `any_segs' is still FALSE, we must define a default
392 * segment.
394 if (!any_segs) {
395 int tempint; /* ignored */
396 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
397 error(ERR_PANIC, "strange segment conditions in IEEE driver");
401 * Find the segment we are targetting.
403 for (seg = seghead; seg; seg = seg->next)
404 if (seg->index == segto)
405 break;
406 if (!seg)
407 error(ERR_PANIC, "code directed to nonexistent segment?");
409 size = type & OUT_SIZMASK;
410 realtype = type & OUT_TYPMASK;
411 if (realtype == OUT_RAWDATA) {
412 ucdata = data;
413 while (size--)
414 ieee_write_byte(seg, *ucdata++);
415 } else if (realtype == OUT_ADDRESS || realtype == OUT_REL2ADR ||
416 realtype == OUT_REL4ADR) {
417 if (segment == NO_SEG && realtype != OUT_ADDRESS)
418 error(ERR_NONFATAL, "relative call to absolute address not"
419 " supported by IEEE format");
420 ldata = *(long *)data;
421 if (realtype == OUT_REL2ADR)
422 ldata += (size - 2);
423 if (realtype == OUT_REL4ADR)
424 ldata += (size - 4);
425 ieee_write_fixup(segment, wrt, seg, size, realtype, ldata);
426 if (size == 2)
427 ieee_write_word(seg, ldata);
428 else
429 ieee_write_dword(seg, ldata);
430 } else if (realtype == OUT_RESERVE) {
431 while (size--)
432 ieee_write_byte(seg, 0);
436 static void ieee_data_new(struct ieeeSection *segto)
439 if (!segto->data)
440 segto->data = segto->datacurr =
441 nasm_malloc(sizeof(*(segto->datacurr)));
442 else
443 segto->datacurr = segto->datacurr->next =
444 nasm_malloc(sizeof(*(segto->datacurr)));
445 segto->datacurr->next = NULL;
449 * this routine is unalduterated bloatware. I usually don't do this
450 * but I might as well see what it is like on a harmless program.
451 * If anyone wants to optimize this is a good canditate!
453 static void ieee_write_fixup(long segment, long wrt,
454 struct ieeeSection *segto, int size,
455 unsigned long realtype, long offset)
457 struct ieeeSection *target;
458 struct ieeeFixupp s;
460 /* Don't put a fixup for things NASM can calculate */
461 if (wrt == NO_SEG && segment == NO_SEG)
462 return;
464 s.ftype = -1;
465 /* if it is a WRT offset */
466 if (wrt != NO_SEG) {
467 s.ftype = FT_WRT;
468 s.addend = offset;
469 if (wrt >= SEG_ABS)
470 s.id1 = -(wrt - SEG_ABS);
471 else {
472 if (wrt % 2 && realtype != OUT_REL2ADR
473 && realtype != OUT_REL4ADR) {
474 wrt--;
476 for (target = seghead; target; target = target->next)
477 if (target->index == wrt)
478 break;
479 if (target) {
480 s.id1 = target->ieee_index;
481 for (target = seghead; target; target = target->next)
482 if (target->index == segment)
483 break;
485 if (target)
486 s.id2 = target->ieee_index;
487 else {
489 * Now we assume the segment field is being used
490 * to hold an extern index
492 long i = segment / 2;
493 struct ExtBack *eb = ebhead;
494 while (i > EXT_BLKSIZ) {
495 if (eb)
496 eb = eb->next;
497 else
498 break;
499 i -= EXT_BLKSIZ;
501 /* if we have an extern decide the type and make a record
503 if (eb) {
504 s.ftype = FT_EXTWRT;
505 s.addend = 0;
506 s.id2 = eb->index[i];
507 } else
508 error(ERR_NONFATAL,
509 "Source of WRT must be an offset");
512 } else
513 error(ERR_PANIC,
514 "unrecognised WRT value in ieee_write_fixup");
515 } else
516 error(ERR_NONFATAL, "target of WRT must be a section ");
518 s.size = size;
519 ieee_install_fixup(segto, &s);
520 return;
522 /* Pure segment fixup ? */
523 if (segment != NO_SEG) {
524 s.ftype = FT_SEG;
525 s.id1 = 0;
526 if (segment >= SEG_ABS) {
527 /* absolute far segment fixup */
528 s.id1 = -(segment - ~SEG_ABS);
529 } else if (segment % 2) {
530 /* fixup to named segment */
531 /* look it up */
532 for (target = seghead; target; target = target->next)
533 if (target->index == segment - 1)
534 break;
535 if (target)
536 s.id1 = target->ieee_index;
537 else {
539 * Now we assume the segment field is being used
540 * to hold an extern index
542 long i = segment / 2;
543 struct ExtBack *eb = ebhead;
544 while (i > EXT_BLKSIZ) {
545 if (eb)
546 eb = eb->next;
547 else
548 break;
549 i -= EXT_BLKSIZ;
551 /* if we have an extern decide the type and make a record
553 if (eb) {
554 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
555 error(ERR_PANIC,
556 "Segment of a rel not supported in ieee_write_fixup");
557 } else {
558 /* If we want the segment */
559 s.ftype = FT_EXTSEG;
560 s.addend = 0;
561 s.id1 = eb->index[i];
564 } else
565 /* If we get here the seg value doesn't make sense */
566 error(ERR_PANIC,
567 "unrecognised segment value in ieee_write_fixup");
570 } else {
571 /* Assume we are offsetting directly from a section
572 * So look up the target segment
574 for (target = seghead; target; target = target->next)
575 if (target->index == segment)
576 break;
577 if (target) {
578 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
579 /* PC rel to a known offset */
580 s.id1 = target->ieee_index;
581 s.ftype = FT_REL;
582 s.size = size;
583 s.addend = offset;
584 } else {
585 /* We were offsetting from a seg */
586 s.id1 = target->ieee_index;
587 s.ftype = FT_OFS;
588 s.size = size;
589 s.addend = offset;
591 } else {
593 * Now we assume the segment field is being used
594 * to hold an extern index
596 long i = segment / 2;
597 struct ExtBack *eb = ebhead;
598 while (i > EXT_BLKSIZ) {
599 if (eb)
600 eb = eb->next;
601 else
602 break;
603 i -= EXT_BLKSIZ;
605 /* if we have an extern decide the type and make a record
607 if (eb) {
608 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
609 s.ftype = FT_EXTREL;
610 s.addend = 0;
611 s.id1 = eb->index[i];
612 } else {
613 /* else we want the external offset */
614 s.ftype = FT_EXT;
615 s.addend = 0;
616 s.id1 = eb->index[i];
619 } else
620 /* If we get here the seg value doesn't make sense */
621 error(ERR_PANIC,
622 "unrecognised segment value in ieee_write_fixup");
625 if (size != 2 && s.ftype == FT_SEG)
626 error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
627 " segment base references");
628 s.size = size;
629 ieee_install_fixup(segto, &s);
630 return;
632 /* should never get here */
634 static void ieee_install_fixup(struct ieeeSection *seg,
635 struct ieeeFixupp *fix)
637 struct ieeeFixupp *f;
638 f = nasm_malloc(sizeof(struct ieeeFixupp));
639 memcpy(f, fix, sizeof(struct ieeeFixupp));
640 f->offset = seg->currentpos;
641 seg->currentpos += fix->size;
642 f->next = NULL;
643 if (seg->fptr)
644 seg->flptr = seg->flptr->next = f;
645 else
646 seg->fptr = seg->flptr = f;
651 * segment registry
653 static long ieee_segment(char *name, int pass, int *bits)
656 * We call the label manager here to define a name for the new
657 * segment, and when our _own_ label-definition stub gets
658 * called in return, it should register the new segment name
659 * using the pointer it gets passed. That way we save memory,
660 * by sponging off the label manager.
662 if (!name) {
663 *bits = 16;
664 if (!any_segs)
665 return 0;
666 return seghead->index;
667 } else {
668 struct ieeeSection *seg;
669 int ieee_idx, attrs, rn_error;
670 char *p;
673 * Look for segment attributes.
675 attrs = 0;
676 while (*name == '.')
677 name++; /* hack, but a documented one */
678 p = name;
679 while (*p && !isspace(*p))
680 p++;
681 if (*p) {
682 *p++ = '\0';
683 while (*p && isspace(*p))
684 *p++ = '\0';
686 while (*p) {
687 while (*p && !isspace(*p))
688 p++;
689 if (*p) {
690 *p++ = '\0';
691 while (*p && isspace(*p))
692 *p++ = '\0';
695 attrs++;
698 ieee_idx = 1;
699 for (seg = seghead; seg; seg = seg->next) {
700 ieee_idx++;
701 if (!strcmp(seg->name, name)) {
702 if (attrs > 0 && pass == 1)
703 error(ERR_WARNING, "segment attributes specified on"
704 " redeclaration of segment: ignoring");
705 if (seg->use32)
706 *bits = 32;
707 else
708 *bits = 16;
709 return seg->index;
713 *segtail = seg = nasm_malloc(sizeof(*seg));
714 seg->next = NULL;
715 segtail = &seg->next;
716 seg->index = seg_alloc();
717 seg->ieee_index = ieee_idx;
718 any_segs = TRUE;
719 seg->name = NULL;
720 seg->currentpos = 0;
721 seg->align = 1; /* default */
722 seg->use32 = *bits == 32; /* default to user spec */
723 seg->combine = CMB_PUBLIC; /* default */
724 seg->pubhead = NULL;
725 seg->pubtail = &seg->pubhead;
726 seg->data = NULL;
727 seg->fptr = NULL;
728 seg->lochead = NULL;
729 seg->loctail = &seg->lochead;
732 * Process the segment attributes.
734 p = name;
735 while (attrs--) {
736 p += strlen(p);
737 while (!*p)
738 p++;
741 * `p' contains a segment attribute.
743 if (!nasm_stricmp(p, "private"))
744 seg->combine = CMB_PRIVATE;
745 else if (!nasm_stricmp(p, "public"))
746 seg->combine = CMB_PUBLIC;
747 else if (!nasm_stricmp(p, "common"))
748 seg->combine = CMB_COMMON;
749 else if (!nasm_stricmp(p, "use16"))
750 seg->use32 = FALSE;
751 else if (!nasm_stricmp(p, "use32"))
752 seg->use32 = TRUE;
753 else if (!nasm_strnicmp(p, "align=", 6)) {
754 seg->align = readnum(p + 6, &rn_error);
755 if (seg->align == 0)
756 seg->align = 1;
757 if (rn_error) {
758 seg->align = 1;
759 error(ERR_NONFATAL, "segment alignment should be"
760 " numeric");
762 switch ((int)seg->align) {
763 case 1: /* BYTE */
764 case 2: /* WORD */
765 case 4: /* DWORD */
766 case 16: /* PARA */
767 case 256: /* PAGE */
768 case 8:
769 case 32:
770 case 64:
771 case 128:
772 break;
773 default:
774 error(ERR_NONFATAL, "invalid alignment value %d",
775 seg->align);
776 seg->align = 1;
777 break;
779 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
780 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
781 if (rn_error)
782 error(ERR_NONFATAL, "argument to `absolute' segment"
783 " attribute should be numeric");
787 ieee_seg_needs_update = seg;
788 if (seg->align >= SEG_ABS)
789 deflabel(name, NO_SEG, seg->align - SEG_ABS,
790 NULL, FALSE, FALSE, &of_ieee, error);
791 else
792 deflabel(name, seg->index + 1, 0L,
793 NULL, FALSE, FALSE, &of_ieee, error);
794 ieee_seg_needs_update = NULL;
796 if (seg->use32)
797 *bits = 32;
798 else
799 *bits = 16;
800 return seg->index;
805 * directives supported
807 static int ieee_directive(char *directive, char *value, int pass)
810 (void)value;
811 (void)pass;
812 if (!strcmp(directive, "uppercase")) {
813 ieee_uppercase = TRUE;
814 return 1;
816 return 0;
820 * Return segment data
822 static long ieee_segbase(long segment)
824 struct ieeeSection *seg;
827 * Find the segment in our list.
829 for (seg = seghead; seg; seg = seg->next)
830 if (seg->index == segment - 1)
831 break;
833 if (!seg)
834 return segment; /* not one of ours - leave it alone */
836 if (seg->align >= SEG_ABS)
837 return seg->align; /* absolute segment */
839 return segment; /* no special treatment */
843 * filename
845 static void ieee_filename(char *inname, char *outname, efunc error)
847 strcpy(ieee_infile, inname);
848 standard_extension(inname, outname, ".o", error);
851 static void ieee_write_file(int debuginfo)
853 struct tm *thetime;
854 time_t reltime;
855 struct FileName *fn;
856 struct ieeeSection *seg;
857 struct ieeePublic *pub, *loc;
858 struct ieeeExternal *ext;
859 struct ieeeObjData *data;
860 struct ieeeFixupp *fix;
861 struct Array *arr;
862 static char boast[] = "The Netwide Assembler " NASM_VER;
863 int i;
866 * Write the module header
868 ieee_putascii("MBFNASM,%02X%s.\r\n", strlen(ieee_infile), ieee_infile);
871 * Write the NASM boast comment.
873 ieee_putascii("CO0,%02X%s.\r\n", strlen(boast), boast);
876 * write processor-specific information
878 ieee_putascii("AD8,4,L.\r\n");
881 * date and time
883 time(&reltime);
884 thetime = localtime(&reltime);
885 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\r\n",
886 1900 + thetime->tm_year, thetime->tm_mon + 1,
887 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
888 thetime->tm_sec);
890 * if debugging, dump file names
892 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
893 ieee_putascii("C0105,%02X%s.\r\n", strlen(fn->name), fn->name);
896 ieee_putascii("CO101,07ENDHEAD.\r\n");
898 * the standard doesn't specify when to put checksums,
899 * we'll just do it periodically.
901 ieee_putcs(FALSE);
904 * Write the section headers
906 seg = seghead;
907 if (!debuginfo && !strcmp(seg->name, "??LINE"))
908 seg = seg->next;
909 while (seg) {
910 char buf[256];
911 char attrib;
912 switch (seg->combine) {
913 case CMB_PUBLIC:
914 default:
915 attrib = 'C';
916 break;
917 case CMB_PRIVATE:
918 attrib = 'S';
919 break;
920 case CMB_COMMON:
921 attrib = 'M';
922 break;
924 ieee_unqualified_name(buf, seg->name);
925 if (seg->align >= SEG_ABS) {
926 ieee_putascii("ST%X,A,%02X%s.\r\n", seg->ieee_index,
927 strlen(buf), buf);
928 ieee_putascii("ASL%X,%lX.\r\n", seg->ieee_index,
929 (seg->align - SEG_ABS) * 16);
930 } else {
931 ieee_putascii("ST%X,%c,%02X%s.\r\n", seg->ieee_index, attrib,
932 strlen(buf), buf);
933 ieee_putascii("SA%X,%lX.\r\n", seg->ieee_index, seg->align);
934 ieee_putascii("ASS%X,%X.\r\n", seg->ieee_index,
935 seg->currentpos);
937 seg = seg->next;
940 * write the start address if there is one
942 if (ieee_entry_seg) {
943 for (seg = seghead; seg; seg = seg->next)
944 if (seg->index == ieee_entry_seg)
945 break;
946 if (!seg)
947 error(ERR_PANIC, "Start address records are incorrect");
948 else
949 ieee_putascii("ASG,R%X,%lX,+.\r\n", seg->ieee_index,
950 ieee_entry_ofs);
953 ieee_putcs(FALSE);
955 * Write the publics
957 i = 1;
958 for (seg = seghead; seg; seg = seg->next) {
959 for (pub = seg->pubhead; pub; pub = pub->next) {
960 char buf[256];
961 ieee_unqualified_name(buf, pub->name);
962 ieee_putascii("NI%X,%02X%s.\r\n", i, strlen(buf), buf);
963 if (pub->segment == -1)
964 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i, pub->index,
965 pub->offset);
966 else
967 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i, pub->segment * 16,
968 pub->offset);
969 if (debuginfo) {
970 if (pub->type >= 0x100)
971 ieee_putascii("ATI%X,T%X.\r\n", i, pub->type - 0x100);
972 else
973 ieee_putascii("ATI%X,%X.\r\n", i, pub->type);
975 i++;
978 pub = fpubhead;
979 i = 1;
980 while (pub) {
981 char buf[256];
982 ieee_unqualified_name(buf, pub->name);
983 ieee_putascii("NI%X,%02X%s.\r\n", i, strlen(buf), buf);
984 if (pub->segment == -1)
985 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i, pub->index,
986 pub->offset);
987 else
988 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i, pub->segment * 16,
989 pub->offset);
990 if (debuginfo) {
991 if (pub->type >= 0x100)
992 ieee_putascii("ATI%X,T%X.\r\n", i, pub->type - 0x100);
993 else
994 ieee_putascii("ATI%X,%X.\r\n", i, pub->type);
996 i++;
997 pub = pub->next;
1000 * Write the externals
1002 ext = exthead;
1003 i = 1;
1004 while (ext) {
1005 char buf[256];
1006 ieee_unqualified_name(buf, ext->name);
1007 ieee_putascii("NX%X,%02X%s.\r\n", i++, strlen(buf), buf);
1008 ext = ext->next;
1010 ieee_putcs(FALSE);
1013 * IEEE doesn't have a standard pass break record
1014 * so use the ladsoft variant
1016 ieee_putascii("CO100,06ENDSYM.\r\n");
1019 * now put types
1021 i = ARRAY_BOT;
1022 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1023 ieee_putascii("TY%X,20,%X,%lX.\r\n", i++, arr->basetype,
1024 arr->size);
1027 * now put locals
1029 i = 1;
1030 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1031 for (loc = seg->lochead; loc; loc = loc->next) {
1032 char buf[256];
1033 ieee_unqualified_name(buf, loc->name);
1034 ieee_putascii("NN%X,%02X%s.\r\n", i, strlen(buf), buf);
1035 if (loc->segment == -1)
1036 ieee_putascii("ASN%X,R%X,%lX,+.\r\n", i, loc->index,
1037 loc->offset);
1038 else
1039 ieee_putascii("ASN%X,%lX,%lX,+.\r\n", i, loc->segment * 16,
1040 loc->offset);
1041 if (debuginfo) {
1042 if (loc->type >= 0x100)
1043 ieee_putascii("ATN%X,T%X.\r\n", i, loc->type - 0x100);
1044 else
1045 ieee_putascii("ATN%X,%X.\r\n", i, loc->type);
1047 i++;
1052 * put out section data;
1054 seg = seghead;
1055 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1056 seg = seg->next;
1057 while (seg) {
1058 if (seg->currentpos) {
1059 long size, org = 0;
1060 data = seg->data;
1061 ieee_putascii("SB%X.\r\n", seg->ieee_index);
1062 fix = seg->fptr;
1063 while (fix) {
1064 size = HUNKSIZE - (org % HUNKSIZE);
1065 size =
1066 size + org >
1067 seg->currentpos ? seg->currentpos - org : size;
1068 size = fix->offset - org > size ? size : fix->offset - org;
1069 org = ieee_putld(org, org + size, data->data);
1070 if (org % HUNKSIZE == 0)
1071 data = data->next;
1072 if (org == fix->offset) {
1073 org += ieee_putlr(fix);
1074 fix = fix->next;
1077 while (org < seg->currentpos && data) {
1078 size =
1079 seg->currentpos - org >
1080 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1081 org = ieee_putld(org, org + size, data->data);
1082 data = data->next;
1084 ieee_putcs(FALSE);
1087 seg = seg->next;
1090 * module end record
1092 ieee_putascii("ME.\r\n");
1095 static void ieee_write_byte(struct ieeeSection *seg, int data)
1097 int temp;
1098 if (!(temp = seg->currentpos++ % HUNKSIZE))
1099 ieee_data_new(seg);
1100 seg->datacurr->data[temp] = data;
1103 static void ieee_write_word(struct ieeeSection *seg, int data)
1105 ieee_write_byte(seg, data & 0xFF);
1106 ieee_write_byte(seg, (data >> 8) & 0xFF);
1109 static void ieee_write_dword(struct ieeeSection *seg, long data)
1111 ieee_write_byte(seg, data & 0xFF);
1112 ieee_write_byte(seg, (data >> 8) & 0xFF);
1113 ieee_write_byte(seg, (data >> 16) & 0xFF);
1114 ieee_write_byte(seg, (data >> 24) & 0xFF);
1116 static void ieee_putascii(char *format, ...)
1118 char buffer[256];
1119 int i, l;
1120 va_list ap;
1122 va_start(ap, format);
1123 vsprintf(buffer, format, ap);
1124 l = strlen(buffer);
1125 for (i = 0; i < l; i++)
1126 if ((buffer[i] & 0xff) > 31)
1127 checksum += buffer[i];
1128 va_end(ap);
1129 fprintf(ofp, buffer);
1133 * put out a checksum record */
1134 static void ieee_putcs(int toclear)
1136 if (toclear) {
1137 ieee_putascii("CS.\r\n");
1138 } else {
1139 checksum += 'C';
1140 checksum += 'S';
1141 ieee_putascii("CS%02X.\r\n", checksum & 127);
1143 checksum = 0;
1146 static long ieee_putld(long start, long end, unsigned char *buf)
1148 long val;
1149 if (start == end)
1150 return (start);
1151 val = start % HUNKSIZE;
1152 /* fill up multiple lines */
1153 while (end - start >= LDPERLINE) {
1154 int i;
1155 ieee_putascii("LD");
1156 for (i = 0; i < LDPERLINE; i++) {
1157 ieee_putascii("%02X", buf[val++]);
1158 start++;
1160 ieee_putascii(".\r\n");
1162 /* if no partial lines */
1163 if (start == end)
1164 return (start);
1165 /* make a partial line */
1166 ieee_putascii("LD");
1167 while (start < end) {
1168 ieee_putascii("%02X", buf[val++]);
1169 start++;
1171 ieee_putascii(".\r\n");
1172 return (start);
1174 static long ieee_putlr(struct ieeeFixupp *p)
1177 * To deal with the vagaries of segmentation the LADsoft linker
1178 * defines two types of segments: absolute and virtual. Note that
1179 * 'absolute' in this context is a different thing from the IEEE
1180 * definition of an absolute segment type, which is also supported. If a
1181 * sement is linked in virtual mode the low limit (L-var) is
1182 * subtracted from each R,X, and P variable which appears in an
1183 * expression, so that we can have relative offsets. Meanwhile
1184 * in the ABSOLUTE mode this subtraction is not done and
1185 * so we can use absolute offsets from 0. In the LADsoft linker
1186 * this configuration is not done in the assemblker source but in
1187 * a source the linker reads. Generally this type of thing only
1188 * becomes an issue if real mode code is used. A pure 32-bit linker could
1189 * get away without defining the virtual mode...
1191 char buf[40];
1192 long size = p->size;
1193 switch (p->ftype) {
1194 case FT_SEG:
1195 if (p->id1 < 0)
1196 sprintf(buf, "%lX", -p->id1);
1197 else
1198 sprintf(buf, "L%lX,10,/", p->id1);
1199 break;
1200 case FT_OFS:
1201 sprintf(buf, "R%lX,%lX,+", p->id1, p->addend);
1202 break;
1203 case FT_REL:
1204 sprintf(buf, "R%lX,%lX,+,P,-,%X,-", p->id1, p->addend, p->size);
1205 break;
1207 case FT_WRT:
1208 if (p->id2 < 0)
1209 sprintf(buf, "R%lX,%lX,+,L%lX,+,%lX,-", p->id2, p->addend,
1210 p->id2, -p->id1 * 16);
1211 else
1212 sprintf(buf, "R%lX,%lX,+,L%lX,+,L%lX,-", p->id2, p->addend,
1213 p->id2, p->id1);
1214 break;
1215 case FT_EXT:
1216 sprintf(buf, "X%lX", p->id1);
1217 break;
1218 case FT_EXTREL:
1219 sprintf(buf, "X%lX,P,-,%lX,-", p->id1, size);
1220 break;
1221 case FT_EXTSEG:
1222 /* We needed a non-ieee hack here.
1223 * We introduce the Y variable, which is the low
1224 * limit of the native segment the extern resides in
1226 sprintf(buf, "Y%lX,10,/", p->id1);
1227 break;
1228 case FT_EXTWRT:
1229 if (p->id2 < 0)
1230 sprintf(buf, "X%lX,Y%lX,+,%lX,-", p->id2, p->id2,
1231 -p->id1 * 16);
1232 else
1233 sprintf(buf, "X%lX,Y%lX,+,L%lX,-", p->id2, p->id2, p->id1);
1234 break;
1236 ieee_putascii("LR(%s,%lX).\r\n", buf, size);
1238 return (size);
1241 /* Dump all segment data (text and fixups )*/
1243 static void ieee_unqualified_name(char *dest, char *source)
1245 if (ieee_uppercase) {
1246 while (*source)
1247 *dest++ = toupper(*source++);
1248 *dest = 0;
1249 } else
1250 strcpy(dest, source);
1252 void dbgls_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1254 int tempint;
1255 (void)of;
1256 (void)id;
1257 (void)fp;
1258 (void)error;
1260 fnhead = NULL;
1261 fntail = &fnhead;
1262 arrindex = ARRAY_BOT;
1263 arrhead = NULL;
1264 arrtail = &arrhead;
1265 ieee_segment("??LINE", 2, &tempint);
1266 any_segs = FALSE;
1268 static void dbgls_cleanup(void)
1270 struct ieeeSection *segtmp;
1271 while (fnhead) {
1272 struct FileName *fntemp = fnhead;
1273 fnhead = fnhead->next;
1274 nasm_free(fntemp->name);
1275 nasm_free(fntemp);
1277 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1278 while (segtmp->lochead) {
1279 struct ieeePublic *loctmp = segtmp->lochead;
1280 segtmp->lochead = loctmp->next;
1281 nasm_free(loctmp->name);
1282 nasm_free(loctmp);
1285 while (arrhead) {
1286 struct Array *arrtmp = arrhead;
1287 arrhead = arrhead->next;
1288 nasm_free(arrtmp);
1293 * because this routine is not bracketed in
1294 * the main program, this routine will be called even if there
1295 * is no request for debug info
1296 * so, we have to make sure the ??LINE segment is avaialbe
1297 * as the first segment when this debug format is selected
1299 static void dbgls_linnum(const char *lnfname, long lineno, long segto)
1301 struct FileName *fn;
1302 struct ieeeSection *seg;
1303 int i = 0;
1304 if (segto == NO_SEG)
1305 return;
1308 * If `any_segs' is still FALSE, we must define a default
1309 * segment.
1311 if (!any_segs) {
1312 int tempint; /* ignored */
1313 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1314 error(ERR_PANIC, "strange segment conditions in OBJ driver");
1318 * Find the segment we are targetting.
1320 for (seg = seghead; seg; seg = seg->next)
1321 if (seg->index == segto)
1322 break;
1323 if (!seg)
1324 error(ERR_PANIC, "lineno directed to nonexistent segment?");
1326 for (fn = fnhead; fn; fn = fn->next) {
1327 if (!nasm_stricmp(lnfname, fn->name))
1328 break;
1329 i++;
1331 if (!fn) {
1332 fn = nasm_malloc(sizeof(*fn));
1333 fn->name = nasm_malloc(strlen(lnfname) + 1);
1334 fn->index = i;
1335 strcpy(fn->name, lnfname);
1336 fn->next = NULL;
1337 *fntail = fn;
1338 fntail = &fn->next;
1340 ieee_write_byte(seghead, fn->index);
1341 ieee_write_word(seghead, lineno);
1342 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1343 seg->currentpos);
1346 static void dbgls_deflabel(char *name, long segment,
1347 long offset, int is_global, char *special)
1349 struct ieeeSection *seg;
1350 int used_special; /* have we used the special text? */
1352 /* Keep compiler from warning about special and used_special */
1353 used_special = special ? FALSE : FALSE;
1356 * If it's a special-retry from pass two, discard it.
1358 if (is_global == 3)
1359 return;
1362 * First check for the double-period, signifying something
1363 * unusual.
1365 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
1366 return;
1370 * Case (i):
1372 if (ieee_seg_needs_update)
1373 return;
1374 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
1375 return;
1377 if (segment >= SEG_ABS || segment == NO_SEG) {
1378 return;
1382 * If `any_segs' is still FALSE, we might need to define a
1383 * default segment, if they're trying to declare a label in
1384 * `first_seg'. But the label should exist due to a prior
1385 * call to ieee_deflabel so we can skip that.
1388 for (seg = seghead; seg; seg = seg->next)
1389 if (seg->index == segment) {
1390 struct ieeePublic *loc;
1392 * Case (ii). Maybe MODPUB someday?
1394 if (!is_global) {
1395 last_defined = loc = nasm_malloc(sizeof(*loc));
1396 *seg->loctail = loc;
1397 seg->loctail = &loc->next;
1398 loc->next = NULL;
1399 loc->name = nasm_strdup(name);
1400 loc->offset = offset;
1401 loc->segment = -1;
1402 loc->index = seg->ieee_index;
1406 static void dbgls_typevalue(long type)
1408 int elem = TYM_ELEMENTS(type);
1409 type = TYM_TYPE(type);
1411 if (!last_defined)
1412 return;
1414 switch (type) {
1415 case TY_BYTE:
1416 last_defined->type = 1; /* unsigned char */
1417 break;
1418 case TY_WORD:
1419 last_defined->type = 3; /* unsigned word */
1420 break;
1421 case TY_DWORD:
1422 last_defined->type = 5; /* unsigned dword */
1423 break;
1424 case TY_FLOAT:
1425 last_defined->type = 9; /* float */
1426 break;
1427 case TY_QWORD:
1428 last_defined->type = 10; /* qword */
1429 break;
1430 case TY_TBYTE:
1431 last_defined->type = 11; /* TBYTE */
1432 break;
1433 default:
1434 last_defined->type = 0x10; /* near label */
1435 break;
1438 if (elem > 1) {
1439 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
1440 int vtype = last_defined->type;
1441 arrtmp->size = elem;
1442 arrtmp->basetype = vtype;
1443 arrtmp->next = NULL;
1444 last_defined->type = arrindex++ + 0x100;
1445 *arrtail = arrtmp;
1446 arrtail = &(arrtmp->next);
1448 last_defined = NULL;
1450 static void dbgls_output(int output_type, void *param)
1452 (void)output_type;
1453 (void)param;
1455 static struct dfmt ladsoft_debug_form = {
1456 "LADsoft Debug Records",
1457 "ladsoft",
1458 dbgls_init,
1459 dbgls_linnum,
1460 dbgls_deflabel,
1461 null_debug_routine,
1462 dbgls_typevalue,
1463 dbgls_output,
1464 dbgls_cleanup,
1466 static struct dfmt *ladsoft_debug_arr[3] = {
1467 &ladsoft_debug_form,
1468 &null_debug_form,
1469 NULL
1471 struct ofmt of_ieee = {
1472 "IEEE-695 (LADsoft variant) object file format",
1473 "ieee",
1474 NULL,
1475 ladsoft_debug_arr,
1476 &null_debug_form,
1477 NULL,
1478 ieee_init,
1479 ieee_set_info,
1480 ieee_out,
1481 ieee_deflabel,
1482 ieee_segment,
1483 ieee_segbase,
1484 ieee_directive,
1485 ieee_filename,
1486 ieee_cleanup
1489 #endif /* OF_IEEE */