In memory of Chuck...
[nasm.git] / output / outieee.c
blob37e24a5936bf61bb964d44435f06b8ca964ac083
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 license given in the file "LICENSE"
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 "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <time.h>
45 #include <stdarg.h> /* Note: we need the ANSI version of stdarg.h */
46 #include <ctype.h>
47 #include <inttypes.h>
49 #include "nasm.h"
50 #include "nasmlib.h"
51 #include "output/outform.h"
52 #include "output/outlib.h"
54 #ifdef OF_IEEE
56 #define ARRAY_BOT 0x1
58 static char ieee_infile[FILENAME_MAX];
59 static int ieee_uppercase;
61 static efunc error;
62 static ldfunc deflabel;
63 static FILE *ofp;
64 static bool any_segs;
65 static int arrindex;
67 #define HUNKSIZE 1024 /* Size of the data hunk */
68 #define EXT_BLKSIZ 512
69 #define LDPERLINE 32 /* bytes per line in output */
71 struct ieeeSection;
73 struct LineNumber {
74 struct LineNumber *next;
75 struct ieeeSection *segment;
76 int32_t offset;
77 int32_t lineno;
80 static struct FileName {
81 struct FileName *next;
82 char *name;
83 int32_t index;
84 } *fnhead, **fntail;
86 static struct Array {
87 struct Array *next;
88 unsigned size;
89 int basetype;
90 } *arrhead, **arrtail;
92 static struct ieeePublic {
93 struct ieeePublic *next;
94 char *name;
95 int32_t offset;
96 int32_t segment; /* only if it's far-absolute */
97 int32_t index;
98 int type; /* for debug purposes */
99 } *fpubhead, **fpubtail, *last_defined;
101 static struct ieeeExternal {
102 struct ieeeExternal *next;
103 char *name;
104 int32_t commonsize;
105 } *exthead, **exttail;
107 static int externals;
109 static struct ExtBack {
110 struct ExtBack *next;
111 int index[EXT_BLKSIZ];
112 } *ebhead, **ebtail;
114 /* NOTE: the first segment MUST be the lineno segment */
115 static struct ieeeSection {
116 struct ieeeObjData *data, *datacurr;
117 struct ieeeSection *next;
118 struct ieeeFixupp *fptr, *flptr;
119 int32_t index; /* the NASM segment id */
120 int32_t ieee_index; /* the OBJ-file segment index */
121 int32_t currentpos;
122 int32_t align; /* can be SEG_ABS + absolute addr */
123 int32_t startpos;
124 enum {
125 CMB_PRIVATE = 0,
126 CMB_PUBLIC = 2,
127 CMB_COMMON = 6
128 } combine;
129 int32_t use32; /* is this segment 32-bit? */
130 struct ieeePublic *pubhead, **pubtail, *lochead, **loctail;
131 char *name;
132 } *seghead, **segtail, *ieee_seg_needs_update;
134 struct ieeeObjData {
135 struct ieeeObjData *next;
136 uint8_t data[HUNKSIZE];
139 struct ieeeFixupp {
140 struct ieeeFixupp *next;
141 enum {
142 FT_SEG = 0,
143 FT_REL = 1,
144 FT_OFS = 2,
145 FT_EXT = 3,
146 FT_WRT = 4,
147 FT_EXTREL = 5,
148 FT_EXTWRT = 6,
149 FT_EXTSEG = 7
150 } ftype;
151 int16_t size;
152 int32_t id1;
153 int32_t id2;
154 int32_t offset;
155 int32_t addend;
158 static int32_t ieee_entry_seg, ieee_entry_ofs;
159 static int checksum;
161 extern struct ofmt of_ieee;
163 static void ieee_data_new(struct ieeeSection *);
164 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection *,
165 int, uint64_t, int32_t);
166 static void ieee_install_fixup(struct ieeeSection *, struct ieeeFixupp *);
167 static int32_t ieee_segment(char *, int, int *);
168 static void ieee_write_file(int debuginfo);
169 static void ieee_write_byte(struct ieeeSection *, int);
170 static void ieee_write_word(struct ieeeSection *, int);
171 static void ieee_write_dword(struct ieeeSection *, int32_t);
172 static void ieee_putascii(char *, ...);
173 static void ieee_putcs(int);
174 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
175 static int32_t ieee_putlr(struct ieeeFixupp *);
176 static void ieee_unqualified_name(char *, char *);
179 * pup init
181 static void ieee_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
183 (void)eval;
184 ofp = fp;
185 error = errfunc;
186 deflabel = ldef;
187 any_segs = false;
188 fpubhead = NULL;
189 fpubtail = &fpubhead;
190 exthead = NULL;
191 exttail = &exthead;
192 externals = 1;
193 ebhead = NULL;
194 ebtail = &ebhead;
195 seghead = ieee_seg_needs_update = NULL;
196 segtail = &seghead;
197 ieee_entry_seg = NO_SEG;
198 ieee_uppercase = false;
199 checksum = 0;
202 static int ieee_set_info(enum geninfo type, char **val)
204 (void)type;
205 (void)val;
207 return 0;
211 * Rundown
213 static void ieee_cleanup(int debuginfo)
215 ieee_write_file(debuginfo);
216 of_ieee.current_dfmt->cleanup();
217 fclose(ofp);
218 while (seghead) {
219 struct ieeeSection *segtmp = seghead;
220 seghead = seghead->next;
221 while (segtmp->pubhead) {
222 struct ieeePublic *pubtmp = segtmp->pubhead;
223 segtmp->pubhead = pubtmp->next;
224 nasm_free(pubtmp);
226 while (segtmp->fptr) {
227 struct ieeeFixupp *fixtmp = segtmp->fptr;
228 segtmp->fptr = fixtmp->next;
229 nasm_free(fixtmp);
231 while (segtmp->data) {
232 struct ieeeObjData *dattmp = segtmp->data;
233 segtmp->data = dattmp->next;
234 nasm_free(dattmp);
236 nasm_free(segtmp);
238 while (fpubhead) {
239 struct ieeePublic *pubtmp = fpubhead;
240 fpubhead = fpubhead->next;
241 nasm_free(pubtmp);
243 while (exthead) {
244 struct ieeeExternal *exttmp = exthead;
245 exthead = exthead->next;
246 nasm_free(exttmp);
248 while (ebhead) {
249 struct ExtBack *ebtmp = ebhead;
250 ebhead = ebhead->next;
251 nasm_free(ebtmp);
256 * callback for labels
258 static void ieee_deflabel(char *name, int32_t segment,
259 int64_t offset, int is_global, char *special)
262 * We have three cases:
264 * (i) `segment' is a segment-base. If so, set the name field
265 * for the segment structure it refers to, and then
266 * return.
268 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
269 * Save the label position for later output of a PUBDEF record.
272 * (iii) `segment' is not one of our segments. Save the label
273 * position for later output of an EXTDEF.
275 struct ieeeExternal *ext;
276 struct ExtBack *eb;
277 struct ieeeSection *seg;
278 int i;
280 if (special) {
281 error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
284 * First check for the double-period, signifying something
285 * unusual.
287 if (name[0] == '.' && name[1] == '.') {
288 if (!strcmp(name, "..start")) {
289 ieee_entry_seg = segment;
290 ieee_entry_ofs = offset;
292 return;
296 * Case (i):
298 if (ieee_seg_needs_update) {
299 ieee_seg_needs_update->name = name;
300 return;
302 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
303 return;
306 * case (ii)
308 if (segment >= SEG_ABS) {
310 * SEG_ABS subcase of (ii).
312 if (is_global) {
313 struct ieeePublic *pub;
315 pub = *fpubtail = nasm_malloc(sizeof(*pub));
316 fpubtail = &pub->next;
317 pub->next = NULL;
318 pub->name = name;
319 pub->offset = offset;
320 pub->segment = segment & ~SEG_ABS;
322 return;
325 for (seg = seghead; seg && is_global; seg = seg->next)
326 if (seg->index == segment) {
327 struct ieeePublic *pub;
329 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
330 seg->pubtail = &pub->next;
331 pub->next = NULL;
332 pub->name = name;
333 pub->offset = offset;
334 pub->index = seg->ieee_index;
335 pub->segment = -1;
336 return;
340 * Case (iii).
342 if (is_global) {
343 ext = *exttail = nasm_malloc(sizeof(*ext));
344 ext->next = NULL;
345 exttail = &ext->next;
346 ext->name = name;
347 if (is_global == 2)
348 ext->commonsize = offset;
349 else
350 ext->commonsize = 0;
351 i = segment / 2;
352 eb = ebhead;
353 if (!eb) {
354 eb = *ebtail = nasm_malloc(sizeof(*eb));
355 eb->next = NULL;
356 ebtail = &eb->next;
358 while (i > EXT_BLKSIZ) {
359 if (eb && eb->next)
360 eb = eb->next;
361 else {
362 eb = *ebtail = nasm_malloc(sizeof(*eb));
363 eb->next = NULL;
364 ebtail = &eb->next;
366 i -= EXT_BLKSIZ;
368 eb->index[i] = externals++;
374 * Put data out
376 static void ieee_out(int32_t segto, const void *data,
377 enum out_type type, uint64_t size,
378 int32_t segment, int32_t wrt)
380 const uint8_t *ucdata;
381 int32_t ldata;
382 struct ieeeSection *seg;
385 * handle absolute-assembly (structure definitions)
387 if (segto == NO_SEG) {
388 if (type != OUT_RESERVE)
389 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
390 " space");
391 return;
395 * If `any_segs' is still false, we must define a default
396 * segment.
398 if (!any_segs) {
399 int tempint; /* ignored */
400 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
401 error(ERR_PANIC, "strange segment conditions in IEEE driver");
405 * Find the segment we are targetting.
407 for (seg = seghead; seg; seg = seg->next)
408 if (seg->index == segto)
409 break;
410 if (!seg)
411 error(ERR_PANIC, "code directed to nonexistent segment?");
413 if (type == OUT_RAWDATA) {
414 ucdata = data;
415 while (size--)
416 ieee_write_byte(seg, *ucdata++);
417 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
418 type == OUT_REL4ADR) {
419 if (segment == NO_SEG && type != OUT_ADDRESS)
420 error(ERR_NONFATAL, "relative call to absolute address not"
421 " supported by IEEE format");
422 ldata = *(int64_t *)data;
423 if (type == OUT_REL2ADR)
424 ldata += (size - 2);
425 if (type == OUT_REL4ADR)
426 ldata += (size - 4);
427 ieee_write_fixup(segment, wrt, seg, size, type, ldata);
428 if (size == 2)
429 ieee_write_word(seg, ldata);
430 else
431 ieee_write_dword(seg, ldata);
432 } else if (type == OUT_RESERVE) {
433 while (size--)
434 ieee_write_byte(seg, 0);
438 static void ieee_data_new(struct ieeeSection *segto)
441 if (!segto->data)
442 segto->data = segto->datacurr =
443 nasm_malloc(sizeof(*(segto->datacurr)));
444 else
445 segto->datacurr = segto->datacurr->next =
446 nasm_malloc(sizeof(*(segto->datacurr)));
447 segto->datacurr->next = NULL;
451 * this routine is unalduterated bloatware. I usually don't do this
452 * but I might as well see what it is like on a harmless program.
453 * If anyone wants to optimize this is a good canditate!
455 static void ieee_write_fixup(int32_t segment, int32_t wrt,
456 struct ieeeSection *segto, int size,
457 uint64_t realtype, int32_t offset)
459 struct ieeeSection *target;
460 struct ieeeFixupp s;
462 /* Don't put a fixup for things NASM can calculate */
463 if (wrt == NO_SEG && segment == NO_SEG)
464 return;
466 s.ftype = -1;
467 /* if it is a WRT offset */
468 if (wrt != NO_SEG) {
469 s.ftype = FT_WRT;
470 s.addend = offset;
471 if (wrt >= SEG_ABS)
472 s.id1 = -(wrt - SEG_ABS);
473 else {
474 if (wrt % 2 && realtype != OUT_REL2ADR
475 && realtype != OUT_REL4ADR) {
476 wrt--;
478 for (target = seghead; target; target = target->next)
479 if (target->index == wrt)
480 break;
481 if (target) {
482 s.id1 = target->ieee_index;
483 for (target = seghead; target; target = target->next)
484 if (target->index == segment)
485 break;
487 if (target)
488 s.id2 = target->ieee_index;
489 else {
491 * Now we assume the segment field is being used
492 * to hold an extern index
494 int32_t i = segment / 2;
495 struct ExtBack *eb = ebhead;
496 while (i > EXT_BLKSIZ) {
497 if (eb)
498 eb = eb->next;
499 else
500 break;
501 i -= EXT_BLKSIZ;
503 /* if we have an extern decide the type and make a record
505 if (eb) {
506 s.ftype = FT_EXTWRT;
507 s.addend = 0;
508 s.id2 = eb->index[i];
509 } else
510 error(ERR_NONFATAL,
511 "Source of WRT must be an offset");
514 } else
515 error(ERR_PANIC,
516 "unrecognised WRT value in ieee_write_fixup");
517 } else
518 error(ERR_NONFATAL, "target of WRT must be a section ");
520 s.size = size;
521 ieee_install_fixup(segto, &s);
522 return;
524 /* Pure segment fixup ? */
525 if (segment != NO_SEG) {
526 s.ftype = FT_SEG;
527 s.id1 = 0;
528 if (segment >= SEG_ABS) {
529 /* absolute far segment fixup */
530 s.id1 = -(segment - ~SEG_ABS);
531 } else if (segment % 2) {
532 /* fixup to named segment */
533 /* look it up */
534 for (target = seghead; target; target = target->next)
535 if (target->index == segment - 1)
536 break;
537 if (target)
538 s.id1 = target->ieee_index;
539 else {
541 * Now we assume the segment field is being used
542 * to hold an extern index
544 int32_t i = segment / 2;
545 struct ExtBack *eb = ebhead;
546 while (i > EXT_BLKSIZ) {
547 if (eb)
548 eb = eb->next;
549 else
550 break;
551 i -= EXT_BLKSIZ;
553 /* if we have an extern decide the type and make a record
555 if (eb) {
556 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
557 error(ERR_PANIC,
558 "Segment of a rel not supported in ieee_write_fixup");
559 } else {
560 /* If we want the segment */
561 s.ftype = FT_EXTSEG;
562 s.addend = 0;
563 s.id1 = eb->index[i];
566 } else
567 /* If we get here the seg value doesn't make sense */
568 error(ERR_PANIC,
569 "unrecognised segment value in ieee_write_fixup");
572 } else {
573 /* Assume we are offsetting directly from a section
574 * So look up the target segment
576 for (target = seghead; target; target = target->next)
577 if (target->index == segment)
578 break;
579 if (target) {
580 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
581 /* PC rel to a known offset */
582 s.id1 = target->ieee_index;
583 s.ftype = FT_REL;
584 s.size = size;
585 s.addend = offset;
586 } else {
587 /* We were offsetting from a seg */
588 s.id1 = target->ieee_index;
589 s.ftype = FT_OFS;
590 s.size = size;
591 s.addend = offset;
593 } else {
595 * Now we assume the segment field is being used
596 * to hold an extern index
598 int32_t i = segment / 2;
599 struct ExtBack *eb = ebhead;
600 while (i > EXT_BLKSIZ) {
601 if (eb)
602 eb = eb->next;
603 else
604 break;
605 i -= EXT_BLKSIZ;
607 /* if we have an extern decide the type and make a record
609 if (eb) {
610 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
611 s.ftype = FT_EXTREL;
612 s.addend = 0;
613 s.id1 = eb->index[i];
614 } else {
615 /* else we want the external offset */
616 s.ftype = FT_EXT;
617 s.addend = 0;
618 s.id1 = eb->index[i];
621 } else
622 /* If we get here the seg value doesn't make sense */
623 error(ERR_PANIC,
624 "unrecognised segment value in ieee_write_fixup");
627 if (size != 2 && s.ftype == FT_SEG)
628 error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
629 " segment base references");
630 s.size = size;
631 ieee_install_fixup(segto, &s);
632 return;
634 /* should never get here */
636 static void ieee_install_fixup(struct ieeeSection *seg,
637 struct ieeeFixupp *fix)
639 struct ieeeFixupp *f;
640 f = nasm_malloc(sizeof(struct ieeeFixupp));
641 memcpy(f, fix, sizeof(struct ieeeFixupp));
642 f->offset = seg->currentpos;
643 seg->currentpos += fix->size;
644 f->next = NULL;
645 if (seg->fptr)
646 seg->flptr = seg->flptr->next = f;
647 else
648 seg->fptr = seg->flptr = f;
653 * segment registry
655 static int32_t ieee_segment(char *name, int pass, int *bits)
658 * We call the label manager here to define a name for the new
659 * segment, and when our _own_ label-definition stub gets
660 * called in return, it should register the new segment name
661 * using the pointer it gets passed. That way we save memory,
662 * by sponging off the label manager.
664 if (!name) {
665 *bits = 16;
666 if (!any_segs)
667 return 0;
668 return seghead->index;
669 } else {
670 struct ieeeSection *seg;
671 int ieee_idx, attrs;
672 bool rn_error;
673 char *p;
676 * Look for segment attributes.
678 attrs = 0;
679 while (*name == '.')
680 name++; /* hack, but a documented one */
681 p = name;
682 while (*p && !nasm_isspace(*p))
683 p++;
684 if (*p) {
685 *p++ = '\0';
686 while (*p && nasm_isspace(*p))
687 *p++ = '\0';
689 while (*p) {
690 while (*p && !nasm_isspace(*p))
691 p++;
692 if (*p) {
693 *p++ = '\0';
694 while (*p && nasm_isspace(*p))
695 *p++ = '\0';
698 attrs++;
701 ieee_idx = 1;
702 for (seg = seghead; seg; seg = seg->next) {
703 ieee_idx++;
704 if (!strcmp(seg->name, name)) {
705 if (attrs > 0 && pass == 1)
706 error(ERR_WARNING, "segment attributes specified on"
707 " redeclaration of segment: ignoring");
708 if (seg->use32)
709 *bits = 32;
710 else
711 *bits = 16;
712 return seg->index;
716 *segtail = seg = nasm_malloc(sizeof(*seg));
717 seg->next = NULL;
718 segtail = &seg->next;
719 seg->index = seg_alloc();
720 seg->ieee_index = ieee_idx;
721 any_segs = true;
722 seg->name = NULL;
723 seg->currentpos = 0;
724 seg->align = 1; /* default */
725 seg->use32 = *bits == 32; /* default to user spec */
726 seg->combine = CMB_PUBLIC; /* default */
727 seg->pubhead = NULL;
728 seg->pubtail = &seg->pubhead;
729 seg->data = NULL;
730 seg->fptr = NULL;
731 seg->lochead = NULL;
732 seg->loctail = &seg->lochead;
735 * Process the segment attributes.
737 p = name;
738 while (attrs--) {
739 p += strlen(p);
740 while (!*p)
741 p++;
744 * `p' contains a segment attribute.
746 if (!nasm_stricmp(p, "private"))
747 seg->combine = CMB_PRIVATE;
748 else if (!nasm_stricmp(p, "public"))
749 seg->combine = CMB_PUBLIC;
750 else if (!nasm_stricmp(p, "common"))
751 seg->combine = CMB_COMMON;
752 else if (!nasm_stricmp(p, "use16"))
753 seg->use32 = false;
754 else if (!nasm_stricmp(p, "use32"))
755 seg->use32 = true;
756 else if (!nasm_strnicmp(p, "align=", 6)) {
757 seg->align = readnum(p + 6, &rn_error);
758 if (seg->align == 0)
759 seg->align = 1;
760 if (rn_error) {
761 seg->align = 1;
762 error(ERR_NONFATAL, "segment alignment should be"
763 " numeric");
765 switch ((int)seg->align) {
766 case 1: /* BYTE */
767 case 2: /* WORD */
768 case 4: /* DWORD */
769 case 16: /* PARA */
770 case 256: /* PAGE */
771 case 8:
772 case 32:
773 case 64:
774 case 128:
775 break;
776 default:
777 error(ERR_NONFATAL, "invalid alignment value %d",
778 seg->align);
779 seg->align = 1;
780 break;
782 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
783 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
784 if (rn_error)
785 error(ERR_NONFATAL, "argument to `absolute' segment"
786 " attribute should be numeric");
790 ieee_seg_needs_update = seg;
791 if (seg->align >= SEG_ABS)
792 deflabel(name, NO_SEG, seg->align - SEG_ABS,
793 NULL, false, false, &of_ieee, error);
794 else
795 deflabel(name, seg->index + 1, 0L,
796 NULL, false, false, &of_ieee, error);
797 ieee_seg_needs_update = NULL;
799 if (seg->use32)
800 *bits = 32;
801 else
802 *bits = 16;
803 return seg->index;
808 * directives supported
810 static int ieee_directive(char *directive, char *value, int pass)
813 (void)value;
814 (void)pass;
815 if (!strcmp(directive, "uppercase")) {
816 ieee_uppercase = true;
817 return 1;
819 return 0;
823 * Return segment data
825 static int32_t ieee_segbase(int32_t segment)
827 struct ieeeSection *seg;
830 * Find the segment in our list.
832 for (seg = seghead; seg; seg = seg->next)
833 if (seg->index == segment - 1)
834 break;
836 if (!seg)
837 return segment; /* not one of ours - leave it alone */
839 if (seg->align >= SEG_ABS)
840 return seg->align; /* absolute segment */
842 return segment; /* no special treatment */
846 * filename
848 static void ieee_filename(char *inname, char *outname, efunc error)
850 strcpy(ieee_infile, inname);
851 standard_extension(inname, outname, ".o", error);
854 static void ieee_write_file(int debuginfo)
856 struct tm *thetime;
857 time_t reltime;
858 struct FileName *fn;
859 struct ieeeSection *seg;
860 struct ieeePublic *pub, *loc;
861 struct ieeeExternal *ext;
862 struct ieeeObjData *data;
863 struct ieeeFixupp *fix;
864 struct Array *arr;
865 int i;
868 * Write the module header
870 ieee_putascii("MBFNASM,%02X%s.\r\n", strlen(ieee_infile), ieee_infile);
873 * Write the NASM boast comment.
875 ieee_putascii("CO0,%02X%s.\r\n", strlen(nasm_comment), nasm_comment);
878 * write processor-specific information
880 ieee_putascii("AD8,4,L.\r\n");
883 * date and time
885 time(&reltime);
886 thetime = localtime(&reltime);
887 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\r\n",
888 1900 + thetime->tm_year, thetime->tm_mon + 1,
889 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
890 thetime->tm_sec);
892 * if debugging, dump file names
894 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
895 ieee_putascii("C0105,%02X%s.\r\n", strlen(fn->name), fn->name);
898 ieee_putascii("CO101,07ENDHEAD.\r\n");
900 * the standard doesn't specify when to put checksums,
901 * we'll just do it periodically.
903 ieee_putcs(false);
906 * Write the section headers
908 seg = seghead;
909 if (!debuginfo && !strcmp(seg->name, "??LINE"))
910 seg = seg->next;
911 while (seg) {
912 char buf[256];
913 char attrib;
914 switch (seg->combine) {
915 case CMB_PUBLIC:
916 default:
917 attrib = 'C';
918 break;
919 case CMB_PRIVATE:
920 attrib = 'S';
921 break;
922 case CMB_COMMON:
923 attrib = 'M';
924 break;
926 ieee_unqualified_name(buf, seg->name);
927 if (seg->align >= SEG_ABS) {
928 ieee_putascii("ST%X,A,%02X%s.\r\n", seg->ieee_index,
929 strlen(buf), buf);
930 ieee_putascii("ASL%X,%lX.\r\n", seg->ieee_index,
931 (seg->align - SEG_ABS) * 16);
932 } else {
933 ieee_putascii("ST%X,%c,%02X%s.\r\n", seg->ieee_index, attrib,
934 strlen(buf), buf);
935 ieee_putascii("SA%X,%lX.\r\n", seg->ieee_index, seg->align);
936 ieee_putascii("ASS%X,%X.\r\n", seg->ieee_index,
937 seg->currentpos);
939 seg = seg->next;
942 * write the start address if there is one
944 if (ieee_entry_seg) {
945 for (seg = seghead; seg; seg = seg->next)
946 if (seg->index == ieee_entry_seg)
947 break;
948 if (!seg)
949 error(ERR_PANIC, "Start address records are incorrect");
950 else
951 ieee_putascii("ASG,R%X,%lX,+.\r\n", seg->ieee_index,
952 ieee_entry_ofs);
955 ieee_putcs(false);
957 * Write the publics
959 i = 1;
960 for (seg = seghead; seg; seg = seg->next) {
961 for (pub = seg->pubhead; pub; pub = pub->next) {
962 char buf[256];
963 ieee_unqualified_name(buf, pub->name);
964 ieee_putascii("NI%X,%02X%s.\r\n", i, strlen(buf), buf);
965 if (pub->segment == -1)
966 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i, pub->index,
967 pub->offset);
968 else
969 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i, pub->segment * 16,
970 pub->offset);
971 if (debuginfo) {
972 if (pub->type >= 0x100)
973 ieee_putascii("ATI%X,T%X.\r\n", i, pub->type - 0x100);
974 else
975 ieee_putascii("ATI%X,%X.\r\n", i, pub->type);
977 i++;
980 pub = fpubhead;
981 i = 1;
982 while (pub) {
983 char buf[256];
984 ieee_unqualified_name(buf, pub->name);
985 ieee_putascii("NI%X,%02X%s.\r\n", i, strlen(buf), buf);
986 if (pub->segment == -1)
987 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i, pub->index,
988 pub->offset);
989 else
990 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i, pub->segment * 16,
991 pub->offset);
992 if (debuginfo) {
993 if (pub->type >= 0x100)
994 ieee_putascii("ATI%X,T%X.\r\n", i, pub->type - 0x100);
995 else
996 ieee_putascii("ATI%X,%X.\r\n", i, pub->type);
998 i++;
999 pub = pub->next;
1002 * Write the externals
1004 ext = exthead;
1005 i = 1;
1006 while (ext) {
1007 char buf[256];
1008 ieee_unqualified_name(buf, ext->name);
1009 ieee_putascii("NX%X,%02X%s.\r\n", i++, strlen(buf), buf);
1010 ext = ext->next;
1012 ieee_putcs(false);
1015 * IEEE doesn't have a standard pass break record
1016 * so use the ladsoft variant
1018 ieee_putascii("CO100,06ENDSYM.\r\n");
1021 * now put types
1023 i = ARRAY_BOT;
1024 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1025 ieee_putascii("TY%X,20,%X,%lX.\r\n", i++, arr->basetype,
1026 arr->size);
1029 * now put locals
1031 i = 1;
1032 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1033 for (loc = seg->lochead; loc; loc = loc->next) {
1034 char buf[256];
1035 ieee_unqualified_name(buf, loc->name);
1036 ieee_putascii("NN%X,%02X%s.\r\n", i, strlen(buf), buf);
1037 if (loc->segment == -1)
1038 ieee_putascii("ASN%X,R%X,%lX,+.\r\n", i, loc->index,
1039 loc->offset);
1040 else
1041 ieee_putascii("ASN%X,%lX,%lX,+.\r\n", i, loc->segment * 16,
1042 loc->offset);
1043 if (debuginfo) {
1044 if (loc->type >= 0x100)
1045 ieee_putascii("ATN%X,T%X.\r\n", i, loc->type - 0x100);
1046 else
1047 ieee_putascii("ATN%X,%X.\r\n", i, loc->type);
1049 i++;
1054 * put out section data;
1056 seg = seghead;
1057 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1058 seg = seg->next;
1059 while (seg) {
1060 if (seg->currentpos) {
1061 int32_t size, org = 0;
1062 data = seg->data;
1063 ieee_putascii("SB%X.\r\n", seg->ieee_index);
1064 fix = seg->fptr;
1065 while (fix) {
1066 size = HUNKSIZE - (org % HUNKSIZE);
1067 size =
1068 size + org >
1069 seg->currentpos ? seg->currentpos - org : size;
1070 size = fix->offset - org > size ? size : fix->offset - org;
1071 org = ieee_putld(org, org + size, data->data);
1072 if (org % HUNKSIZE == 0)
1073 data = data->next;
1074 if (org == fix->offset) {
1075 org += ieee_putlr(fix);
1076 fix = fix->next;
1079 while (org < seg->currentpos && data) {
1080 size =
1081 seg->currentpos - org >
1082 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1083 org = ieee_putld(org, org + size, data->data);
1084 data = data->next;
1086 ieee_putcs(false);
1089 seg = seg->next;
1092 * module end record
1094 ieee_putascii("ME.\r\n");
1097 static void ieee_write_byte(struct ieeeSection *seg, int data)
1099 int temp;
1100 if (!(temp = seg->currentpos++ % HUNKSIZE))
1101 ieee_data_new(seg);
1102 seg->datacurr->data[temp] = data;
1105 static void ieee_write_word(struct ieeeSection *seg, int data)
1107 ieee_write_byte(seg, data & 0xFF);
1108 ieee_write_byte(seg, (data >> 8) & 0xFF);
1111 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1113 ieee_write_byte(seg, data & 0xFF);
1114 ieee_write_byte(seg, (data >> 8) & 0xFF);
1115 ieee_write_byte(seg, (data >> 16) & 0xFF);
1116 ieee_write_byte(seg, (data >> 24) & 0xFF);
1118 static void ieee_putascii(char *format, ...)
1120 char buffer[256];
1121 int i, l;
1122 va_list ap;
1124 va_start(ap, format);
1125 vsnprintf(buffer, sizeof(buffer), format, ap);
1126 l = strlen(buffer);
1127 for (i = 0; i < l; i++)
1128 if ((buffer[i] & 0xff) > 31)
1129 checksum += buffer[i];
1130 va_end(ap);
1131 fprintf(ofp, buffer);
1135 * put out a checksum record */
1136 static void ieee_putcs(int toclear)
1138 if (toclear) {
1139 ieee_putascii("CS.\r\n");
1140 } else {
1141 checksum += 'C';
1142 checksum += 'S';
1143 ieee_putascii("CS%02X.\r\n", checksum & 127);
1145 checksum = 0;
1148 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1150 int32_t val;
1151 if (start == end)
1152 return (start);
1153 val = start % HUNKSIZE;
1154 /* fill up multiple lines */
1155 while (end - start >= LDPERLINE) {
1156 int i;
1157 ieee_putascii("LD");
1158 for (i = 0; i < LDPERLINE; i++) {
1159 ieee_putascii("%02X", buf[val++]);
1160 start++;
1162 ieee_putascii(".\r\n");
1164 /* if no partial lines */
1165 if (start == end)
1166 return (start);
1167 /* make a partial line */
1168 ieee_putascii("LD");
1169 while (start < end) {
1170 ieee_putascii("%02X", buf[val++]);
1171 start++;
1173 ieee_putascii(".\r\n");
1174 return (start);
1176 static int32_t ieee_putlr(struct ieeeFixupp *p)
1179 * To deal with the vagaries of segmentation the LADsoft linker
1180 * defines two types of segments: absolute and virtual. Note that
1181 * 'absolute' in this context is a different thing from the IEEE
1182 * definition of an absolute segment type, which is also supported. If a
1183 * sement is linked in virtual mode the low limit (L-var) is
1184 * subtracted from each R,X, and P variable which appears in an
1185 * expression, so that we can have relative offsets. Meanwhile
1186 * in the ABSOLUTE mode this subtraction is not done and
1187 * so we can use absolute offsets from 0. In the LADsoft linker
1188 * this configuration is not done in the assemblker source but in
1189 * a source the linker reads. Generally this type of thing only
1190 * becomes an issue if real mode code is used. A pure 32-bit linker could
1191 * get away without defining the virtual mode...
1193 char buf[40];
1194 int32_t size = p->size;
1195 switch (p->ftype) {
1196 case FT_SEG:
1197 if (p->id1 < 0)
1198 sprintf(buf, "%"PRIX32"", -p->id1);
1199 else
1200 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1201 break;
1202 case FT_OFS:
1203 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1204 break;
1205 case FT_REL:
1206 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1207 break;
1209 case FT_WRT:
1210 if (p->id2 < 0)
1211 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1212 p->id2, -p->id1 * 16);
1213 else
1214 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1215 p->id2, p->id1);
1216 break;
1217 case FT_EXT:
1218 sprintf(buf, "X%"PRIX32"", p->id1);
1219 break;
1220 case FT_EXTREL:
1221 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1222 break;
1223 case FT_EXTSEG:
1224 /* We needed a non-ieee hack here.
1225 * We introduce the Y variable, which is the low
1226 * limit of the native segment the extern resides in
1228 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1229 break;
1230 case FT_EXTWRT:
1231 if (p->id2 < 0)
1232 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1233 -p->id1 * 16);
1234 else
1235 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1236 break;
1238 ieee_putascii("LR(%s,%"PRIX32").\r\n", buf, size);
1240 return (size);
1243 /* Dump all segment data (text and fixups )*/
1245 static void ieee_unqualified_name(char *dest, char *source)
1247 if (ieee_uppercase) {
1248 while (*source)
1249 *dest++ = toupper(*source++);
1250 *dest = 0;
1251 } else
1252 strcpy(dest, source);
1254 void dbgls_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1256 int tempint;
1257 (void)of;
1258 (void)id;
1259 (void)fp;
1260 (void)error;
1262 fnhead = NULL;
1263 fntail = &fnhead;
1264 arrindex = ARRAY_BOT;
1265 arrhead = NULL;
1266 arrtail = &arrhead;
1267 ieee_segment("??LINE", 2, &tempint);
1268 any_segs = false;
1270 static void dbgls_cleanup(void)
1272 struct ieeeSection *segtmp;
1273 while (fnhead) {
1274 struct FileName *fntemp = fnhead;
1275 fnhead = fnhead->next;
1276 nasm_free(fntemp->name);
1277 nasm_free(fntemp);
1279 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1280 while (segtmp->lochead) {
1281 struct ieeePublic *loctmp = segtmp->lochead;
1282 segtmp->lochead = loctmp->next;
1283 nasm_free(loctmp->name);
1284 nasm_free(loctmp);
1287 while (arrhead) {
1288 struct Array *arrtmp = arrhead;
1289 arrhead = arrhead->next;
1290 nasm_free(arrtmp);
1295 * because this routine is not bracketed in
1296 * the main program, this routine will be called even if there
1297 * is no request for debug info
1298 * so, we have to make sure the ??LINE segment is avaialbe
1299 * as the first segment when this debug format is selected
1301 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1303 struct FileName *fn;
1304 struct ieeeSection *seg;
1305 int i = 0;
1306 if (segto == NO_SEG)
1307 return;
1310 * If `any_segs' is still false, we must define a default
1311 * segment.
1313 if (!any_segs) {
1314 int tempint; /* ignored */
1315 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1316 error(ERR_PANIC, "strange segment conditions in OBJ driver");
1320 * Find the segment we are targetting.
1322 for (seg = seghead; seg; seg = seg->next)
1323 if (seg->index == segto)
1324 break;
1325 if (!seg)
1326 error(ERR_PANIC, "lineno directed to nonexistent segment?");
1328 for (fn = fnhead; fn; fn = fn->next) {
1329 if (!nasm_stricmp(lnfname, fn->name))
1330 break;
1331 i++;
1333 if (!fn) {
1334 fn = nasm_malloc(sizeof(*fn));
1335 fn->name = nasm_malloc(strlen(lnfname) + 1);
1336 fn->index = i;
1337 strcpy(fn->name, lnfname);
1338 fn->next = NULL;
1339 *fntail = fn;
1340 fntail = &fn->next;
1342 ieee_write_byte(seghead, fn->index);
1343 ieee_write_word(seghead, lineno);
1344 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1345 seg->currentpos);
1348 static void dbgls_deflabel(char *name, int32_t segment,
1349 int64_t offset, int is_global, char *special)
1351 struct ieeeSection *seg;
1353 /* Keep compiler from warning about special */
1354 (void)special;
1357 * If it's a special-retry from pass two, discard it.
1359 if (is_global == 3)
1360 return;
1363 * First check for the double-period, signifying something
1364 * unusual.
1366 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
1367 return;
1371 * Case (i):
1373 if (ieee_seg_needs_update)
1374 return;
1375 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
1376 return;
1378 if (segment >= SEG_ABS || segment == NO_SEG) {
1379 return;
1383 * If `any_segs' is still false, we might need to define a
1384 * default segment, if they're trying to declare a label in
1385 * `first_seg'. But the label should exist due to a prior
1386 * call to ieee_deflabel so we can skip that.
1389 for (seg = seghead; seg; seg = seg->next)
1390 if (seg->index == segment) {
1391 struct ieeePublic *loc;
1393 * Case (ii). Maybe MODPUB someday?
1395 if (!is_global) {
1396 last_defined = loc = nasm_malloc(sizeof(*loc));
1397 *seg->loctail = loc;
1398 seg->loctail = &loc->next;
1399 loc->next = NULL;
1400 loc->name = nasm_strdup(name);
1401 loc->offset = offset;
1402 loc->segment = -1;
1403 loc->index = seg->ieee_index;
1407 static void dbgls_typevalue(int32_t type)
1409 int elem = TYM_ELEMENTS(type);
1410 type = TYM_TYPE(type);
1412 if (!last_defined)
1413 return;
1415 switch (type) {
1416 case TY_BYTE:
1417 last_defined->type = 1; /* uint8_t */
1418 break;
1419 case TY_WORD:
1420 last_defined->type = 3; /* unsigned word */
1421 break;
1422 case TY_DWORD:
1423 last_defined->type = 5; /* unsigned dword */
1424 break;
1425 case TY_FLOAT:
1426 last_defined->type = 9; /* float */
1427 break;
1428 case TY_QWORD:
1429 last_defined->type = 10; /* qword */
1430 break;
1431 case TY_TBYTE:
1432 last_defined->type = 11; /* TBYTE */
1433 break;
1434 default:
1435 last_defined->type = 0x10; /* near label */
1436 break;
1439 if (elem > 1) {
1440 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
1441 int vtype = last_defined->type;
1442 arrtmp->size = elem;
1443 arrtmp->basetype = vtype;
1444 arrtmp->next = NULL;
1445 last_defined->type = arrindex++ + 0x100;
1446 *arrtail = arrtmp;
1447 arrtail = &(arrtmp->next);
1449 last_defined = NULL;
1451 static void dbgls_output(int output_type, void *param)
1453 (void)output_type;
1454 (void)param;
1456 static struct dfmt ladsoft_debug_form = {
1457 "LADsoft Debug Records",
1458 "ladsoft",
1459 dbgls_init,
1460 dbgls_linnum,
1461 dbgls_deflabel,
1462 null_debug_routine,
1463 dbgls_typevalue,
1464 dbgls_output,
1465 dbgls_cleanup,
1467 static struct dfmt *ladsoft_debug_arr[3] = {
1468 &ladsoft_debug_form,
1469 &null_debug_form,
1470 NULL
1472 struct ofmt of_ieee = {
1473 "IEEE-695 (LADsoft variant) object file format",
1474 "ieee",
1475 NULL,
1476 ladsoft_debug_arr,
1477 &ladsoft_debug_form,
1478 NULL,
1479 ieee_init,
1480 ieee_set_info,
1481 ieee_out,
1482 ieee_deflabel,
1483 ieee_segment,
1484 ieee_segbase,
1485 ieee_directive,
1486 ieee_filename,
1487 ieee_cleanup
1490 #endif /* OF_IEEE */