outcoff: BR 2685756: fix SAFESEH with an internal symbol
[nasm/perl-rewrite.git] / output / outieee.c
blob74c1fc85e575d0f432008256b3a20aa140ca9349
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 "outform.h"
53 #ifdef OF_IEEE
55 #define ARRAY_BOT 0x1
57 static char ieee_infile[FILENAME_MAX];
58 static int ieee_uppercase;
60 static efunc error;
61 static ldfunc deflabel;
62 static FILE *ofp;
63 static bool any_segs;
64 static int arrindex;
66 #define HUNKSIZE 1024 /* Size of the data hunk */
67 #define EXT_BLKSIZ 512
68 #define LDPERLINE 32 /* bytes per line in output */
70 struct ieeeSection;
72 struct LineNumber {
73 struct LineNumber *next;
74 struct ieeeSection *segment;
75 int32_t offset;
76 int32_t lineno;
79 static struct FileName {
80 struct FileName *next;
81 char *name;
82 int32_t index;
83 } *fnhead, **fntail;
85 static struct Array {
86 struct Array *next;
87 unsigned size;
88 int basetype;
89 } *arrhead, **arrtail;
91 static struct ieeePublic {
92 struct ieeePublic *next;
93 char *name;
94 int32_t offset;
95 int32_t segment; /* only if it's far-absolute */
96 int32_t index;
97 int type; /* for debug purposes */
98 } *fpubhead, **fpubtail, *last_defined;
100 static struct ieeeExternal {
101 struct ieeeExternal *next;
102 char *name;
103 int32_t commonsize;
104 } *exthead, **exttail;
106 static int externals;
108 static struct ExtBack {
109 struct ExtBack *next;
110 int index[EXT_BLKSIZ];
111 } *ebhead, **ebtail;
113 /* NOTE: the first segment MUST be the lineno segment */
114 static struct ieeeSection {
115 struct ieeeObjData *data, *datacurr;
116 struct ieeeSection *next;
117 struct ieeeFixupp *fptr, *flptr;
118 int32_t index; /* the NASM segment id */
119 int32_t ieee_index; /* the OBJ-file segment index */
120 int32_t currentpos;
121 int32_t align; /* can be SEG_ABS + absolute addr */
122 int32_t startpos;
123 enum {
124 CMB_PRIVATE = 0,
125 CMB_PUBLIC = 2,
126 CMB_COMMON = 6
127 } combine;
128 int32_t use32; /* is this segment 32-bit? */
129 struct ieeePublic *pubhead, **pubtail, *lochead, **loctail;
130 char *name;
131 } *seghead, **segtail, *ieee_seg_needs_update;
133 struct ieeeObjData {
134 struct ieeeObjData *next;
135 uint8_t data[HUNKSIZE];
138 struct ieeeFixupp {
139 struct ieeeFixupp *next;
140 enum {
141 FT_SEG = 0,
142 FT_REL = 1,
143 FT_OFS = 2,
144 FT_EXT = 3,
145 FT_WRT = 4,
146 FT_EXTREL = 5,
147 FT_EXTWRT = 6,
148 FT_EXTSEG = 7
149 } ftype;
150 int16_t size;
151 int32_t id1;
152 int32_t id2;
153 int32_t offset;
154 int32_t addend;
157 static int32_t ieee_entry_seg, ieee_entry_ofs;
158 static int checksum;
160 extern struct ofmt of_ieee;
162 static void ieee_data_new(struct ieeeSection *);
163 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection *,
164 int, uint64_t, int32_t);
165 static void ieee_install_fixup(struct ieeeSection *, struct ieeeFixupp *);
166 static int32_t ieee_segment(char *, int, int *);
167 static void ieee_write_file(int debuginfo);
168 static void ieee_write_byte(struct ieeeSection *, int);
169 static void ieee_write_word(struct ieeeSection *, int);
170 static void ieee_write_dword(struct ieeeSection *, int32_t);
171 static void ieee_putascii(char *, ...);
172 static void ieee_putcs(int);
173 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
174 static int32_t ieee_putlr(struct ieeeFixupp *);
175 static void ieee_unqualified_name(char *, char *);
178 * pup init
180 static void ieee_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
182 (void)eval;
183 ofp = fp;
184 error = errfunc;
185 deflabel = ldef;
186 any_segs = false;
187 fpubhead = NULL;
188 fpubtail = &fpubhead;
189 exthead = NULL;
190 exttail = &exthead;
191 externals = 1;
192 ebhead = NULL;
193 ebtail = &ebhead;
194 seghead = ieee_seg_needs_update = NULL;
195 segtail = &seghead;
196 ieee_entry_seg = NO_SEG;
197 ieee_uppercase = false;
198 checksum = 0;
199 of_ieee.current_dfmt->init(&of_ieee, NULL, fp, errfunc);
201 static int ieee_set_info(enum geninfo type, char **val)
203 (void)type;
204 (void)val;
206 return 0;
210 * Rundown
212 static void ieee_cleanup(int debuginfo)
214 ieee_write_file(debuginfo);
215 of_ieee.current_dfmt->cleanup();
216 fclose(ofp);
217 while (seghead) {
218 struct ieeeSection *segtmp = seghead;
219 seghead = seghead->next;
220 while (segtmp->pubhead) {
221 struct ieeePublic *pubtmp = segtmp->pubhead;
222 segtmp->pubhead = pubtmp->next;
223 nasm_free(pubtmp);
225 while (segtmp->fptr) {
226 struct ieeeFixupp *fixtmp = segtmp->fptr;
227 segtmp->fptr = fixtmp->next;
228 nasm_free(fixtmp);
230 while (segtmp->data) {
231 struct ieeeObjData *dattmp = segtmp->data;
232 segtmp->data = dattmp->next;
233 nasm_free(dattmp);
235 nasm_free(segtmp);
237 while (fpubhead) {
238 struct ieeePublic *pubtmp = fpubhead;
239 fpubhead = fpubhead->next;
240 nasm_free(pubtmp);
242 while (exthead) {
243 struct ieeeExternal *exttmp = exthead;
244 exthead = exthead->next;
245 nasm_free(exttmp);
247 while (ebhead) {
248 struct ExtBack *ebtmp = ebhead;
249 ebhead = ebhead->next;
250 nasm_free(ebtmp);
255 * callback for labels
257 static void ieee_deflabel(char *name, int32_t segment,
258 int64_t offset, int is_global, char *special)
261 * We have three cases:
263 * (i) `segment' is a segment-base. If so, set the name field
264 * for the segment structure it refers to, and then
265 * return.
267 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
268 * Save the label position for later output of a PUBDEF record.
271 * (iii) `segment' is not one of our segments. Save the label
272 * position for later output of an EXTDEF.
274 struct ieeeExternal *ext;
275 struct ExtBack *eb;
276 struct ieeeSection *seg;
277 int i;
279 if (special) {
280 error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
283 * First check for the double-period, signifying something
284 * unusual.
286 if (name[0] == '.' && name[1] == '.') {
287 if (!strcmp(name, "..start")) {
288 ieee_entry_seg = segment;
289 ieee_entry_ofs = offset;
291 return;
295 * Case (i):
297 if (ieee_seg_needs_update) {
298 ieee_seg_needs_update->name = name;
299 return;
301 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
302 return;
305 * case (ii)
307 if (segment >= SEG_ABS) {
309 * SEG_ABS subcase of (ii).
311 if (is_global) {
312 struct ieeePublic *pub;
314 pub = *fpubtail = nasm_malloc(sizeof(*pub));
315 fpubtail = &pub->next;
316 pub->next = NULL;
317 pub->name = name;
318 pub->offset = offset;
319 pub->segment = segment & ~SEG_ABS;
321 return;
324 for (seg = seghead; seg && is_global; seg = seg->next)
325 if (seg->index == segment) {
326 struct ieeePublic *pub;
328 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
329 seg->pubtail = &pub->next;
330 pub->next = NULL;
331 pub->name = name;
332 pub->offset = offset;
333 pub->index = seg->ieee_index;
334 pub->segment = -1;
335 return;
339 * Case (iii).
341 if (is_global) {
342 ext = *exttail = nasm_malloc(sizeof(*ext));
343 ext->next = NULL;
344 exttail = &ext->next;
345 ext->name = name;
346 if (is_global == 2)
347 ext->commonsize = offset;
348 else
349 ext->commonsize = 0;
350 i = segment / 2;
351 eb = ebhead;
352 if (!eb) {
353 eb = *ebtail = nasm_malloc(sizeof(*eb));
354 eb->next = NULL;
355 ebtail = &eb->next;
357 while (i > EXT_BLKSIZ) {
358 if (eb && eb->next)
359 eb = eb->next;
360 else {
361 eb = *ebtail = nasm_malloc(sizeof(*eb));
362 eb->next = NULL;
363 ebtail = &eb->next;
365 i -= EXT_BLKSIZ;
367 eb->index[i] = externals++;
373 * Put data out
375 static void ieee_out(int32_t segto, const void *data,
376 enum out_type type, uint64_t size,
377 int32_t segment, int32_t wrt)
379 const uint8_t *ucdata;
380 int32_t ldata;
381 struct ieeeSection *seg;
384 * handle absolute-assembly (structure definitions)
386 if (segto == NO_SEG) {
387 if (type != OUT_RESERVE)
388 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
389 " space");
390 return;
394 * If `any_segs' is still false, we must define a default
395 * segment.
397 if (!any_segs) {
398 int tempint; /* ignored */
399 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
400 error(ERR_PANIC, "strange segment conditions in IEEE driver");
404 * Find the segment we are targetting.
406 for (seg = seghead; seg; seg = seg->next)
407 if (seg->index == segto)
408 break;
409 if (!seg)
410 error(ERR_PANIC, "code directed to nonexistent segment?");
412 if (type == OUT_RAWDATA) {
413 ucdata = data;
414 while (size--)
415 ieee_write_byte(seg, *ucdata++);
416 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
417 type == OUT_REL4ADR) {
418 if (segment == NO_SEG && type != OUT_ADDRESS)
419 error(ERR_NONFATAL, "relative call to absolute address not"
420 " supported by IEEE format");
421 ldata = *(int64_t *)data;
422 if (type == OUT_REL2ADR)
423 ldata += (size - 2);
424 if (type == OUT_REL4ADR)
425 ldata += (size - 4);
426 ieee_write_fixup(segment, wrt, seg, size, type, ldata);
427 if (size == 2)
428 ieee_write_word(seg, ldata);
429 else
430 ieee_write_dword(seg, ldata);
431 } else if (type == OUT_RESERVE) {
432 while (size--)
433 ieee_write_byte(seg, 0);
437 static void ieee_data_new(struct ieeeSection *segto)
440 if (!segto->data)
441 segto->data = segto->datacurr =
442 nasm_malloc(sizeof(*(segto->datacurr)));
443 else
444 segto->datacurr = segto->datacurr->next =
445 nasm_malloc(sizeof(*(segto->datacurr)));
446 segto->datacurr->next = NULL;
450 * this routine is unalduterated bloatware. I usually don't do this
451 * but I might as well see what it is like on a harmless program.
452 * If anyone wants to optimize this is a good canditate!
454 static void ieee_write_fixup(int32_t segment, int32_t wrt,
455 struct ieeeSection *segto, int size,
456 uint64_t realtype, int32_t offset)
458 struct ieeeSection *target;
459 struct ieeeFixupp s;
461 /* Don't put a fixup for things NASM can calculate */
462 if (wrt == NO_SEG && segment == NO_SEG)
463 return;
465 s.ftype = -1;
466 /* if it is a WRT offset */
467 if (wrt != NO_SEG) {
468 s.ftype = FT_WRT;
469 s.addend = offset;
470 if (wrt >= SEG_ABS)
471 s.id1 = -(wrt - SEG_ABS);
472 else {
473 if (wrt % 2 && realtype != OUT_REL2ADR
474 && realtype != OUT_REL4ADR) {
475 wrt--;
477 for (target = seghead; target; target = target->next)
478 if (target->index == wrt)
479 break;
480 if (target) {
481 s.id1 = target->ieee_index;
482 for (target = seghead; target; target = target->next)
483 if (target->index == segment)
484 break;
486 if (target)
487 s.id2 = target->ieee_index;
488 else {
490 * Now we assume the segment field is being used
491 * to hold an extern index
493 int32_t i = segment / 2;
494 struct ExtBack *eb = ebhead;
495 while (i > EXT_BLKSIZ) {
496 if (eb)
497 eb = eb->next;
498 else
499 break;
500 i -= EXT_BLKSIZ;
502 /* if we have an extern decide the type and make a record
504 if (eb) {
505 s.ftype = FT_EXTWRT;
506 s.addend = 0;
507 s.id2 = eb->index[i];
508 } else
509 error(ERR_NONFATAL,
510 "Source of WRT must be an offset");
513 } else
514 error(ERR_PANIC,
515 "unrecognised WRT value in ieee_write_fixup");
516 } else
517 error(ERR_NONFATAL, "target of WRT must be a section ");
519 s.size = size;
520 ieee_install_fixup(segto, &s);
521 return;
523 /* Pure segment fixup ? */
524 if (segment != NO_SEG) {
525 s.ftype = FT_SEG;
526 s.id1 = 0;
527 if (segment >= SEG_ABS) {
528 /* absolute far segment fixup */
529 s.id1 = -(segment - ~SEG_ABS);
530 } else if (segment % 2) {
531 /* fixup to named segment */
532 /* look it up */
533 for (target = seghead; target; target = target->next)
534 if (target->index == segment - 1)
535 break;
536 if (target)
537 s.id1 = target->ieee_index;
538 else {
540 * Now we assume the segment field is being used
541 * to hold an extern index
543 int32_t i = segment / 2;
544 struct ExtBack *eb = ebhead;
545 while (i > EXT_BLKSIZ) {
546 if (eb)
547 eb = eb->next;
548 else
549 break;
550 i -= EXT_BLKSIZ;
552 /* if we have an extern decide the type and make a record
554 if (eb) {
555 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
556 error(ERR_PANIC,
557 "Segment of a rel not supported in ieee_write_fixup");
558 } else {
559 /* If we want the segment */
560 s.ftype = FT_EXTSEG;
561 s.addend = 0;
562 s.id1 = eb->index[i];
565 } else
566 /* If we get here the seg value doesn't make sense */
567 error(ERR_PANIC,
568 "unrecognised segment value in ieee_write_fixup");
571 } else {
572 /* Assume we are offsetting directly from a section
573 * So look up the target segment
575 for (target = seghead; target; target = target->next)
576 if (target->index == segment)
577 break;
578 if (target) {
579 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
580 /* PC rel to a known offset */
581 s.id1 = target->ieee_index;
582 s.ftype = FT_REL;
583 s.size = size;
584 s.addend = offset;
585 } else {
586 /* We were offsetting from a seg */
587 s.id1 = target->ieee_index;
588 s.ftype = FT_OFS;
589 s.size = size;
590 s.addend = offset;
592 } else {
594 * Now we assume the segment field is being used
595 * to hold an extern index
597 int32_t i = segment / 2;
598 struct ExtBack *eb = ebhead;
599 while (i > EXT_BLKSIZ) {
600 if (eb)
601 eb = eb->next;
602 else
603 break;
604 i -= EXT_BLKSIZ;
606 /* if we have an extern decide the type and make a record
608 if (eb) {
609 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
610 s.ftype = FT_EXTREL;
611 s.addend = 0;
612 s.id1 = eb->index[i];
613 } else {
614 /* else we want the external offset */
615 s.ftype = FT_EXT;
616 s.addend = 0;
617 s.id1 = eb->index[i];
620 } else
621 /* If we get here the seg value doesn't make sense */
622 error(ERR_PANIC,
623 "unrecognised segment value in ieee_write_fixup");
626 if (size != 2 && s.ftype == FT_SEG)
627 error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
628 " segment base references");
629 s.size = size;
630 ieee_install_fixup(segto, &s);
631 return;
633 /* should never get here */
635 static void ieee_install_fixup(struct ieeeSection *seg,
636 struct ieeeFixupp *fix)
638 struct ieeeFixupp *f;
639 f = nasm_malloc(sizeof(struct ieeeFixupp));
640 memcpy(f, fix, sizeof(struct ieeeFixupp));
641 f->offset = seg->currentpos;
642 seg->currentpos += fix->size;
643 f->next = NULL;
644 if (seg->fptr)
645 seg->flptr = seg->flptr->next = f;
646 else
647 seg->fptr = seg->flptr = f;
652 * segment registry
654 static int32_t ieee_segment(char *name, int pass, int *bits)
657 * We call the label manager here to define a name for the new
658 * segment, and when our _own_ label-definition stub gets
659 * called in return, it should register the new segment name
660 * using the pointer it gets passed. That way we save memory,
661 * by sponging off the label manager.
663 if (!name) {
664 *bits = 16;
665 if (!any_segs)
666 return 0;
667 return seghead->index;
668 } else {
669 struct ieeeSection *seg;
670 int ieee_idx, attrs;
671 bool rn_error;
672 char *p;
675 * Look for segment attributes.
677 attrs = 0;
678 while (*name == '.')
679 name++; /* hack, but a documented one */
680 p = name;
681 while (*p && !nasm_isspace(*p))
682 p++;
683 if (*p) {
684 *p++ = '\0';
685 while (*p && nasm_isspace(*p))
686 *p++ = '\0';
688 while (*p) {
689 while (*p && !nasm_isspace(*p))
690 p++;
691 if (*p) {
692 *p++ = '\0';
693 while (*p && nasm_isspace(*p))
694 *p++ = '\0';
697 attrs++;
700 ieee_idx = 1;
701 for (seg = seghead; seg; seg = seg->next) {
702 ieee_idx++;
703 if (!strcmp(seg->name, name)) {
704 if (attrs > 0 && pass == 1)
705 error(ERR_WARNING, "segment attributes specified on"
706 " redeclaration of segment: ignoring");
707 if (seg->use32)
708 *bits = 32;
709 else
710 *bits = 16;
711 return seg->index;
715 *segtail = seg = nasm_malloc(sizeof(*seg));
716 seg->next = NULL;
717 segtail = &seg->next;
718 seg->index = seg_alloc();
719 seg->ieee_index = ieee_idx;
720 any_segs = true;
721 seg->name = NULL;
722 seg->currentpos = 0;
723 seg->align = 1; /* default */
724 seg->use32 = *bits == 32; /* default to user spec */
725 seg->combine = CMB_PUBLIC; /* default */
726 seg->pubhead = NULL;
727 seg->pubtail = &seg->pubhead;
728 seg->data = NULL;
729 seg->fptr = NULL;
730 seg->lochead = NULL;
731 seg->loctail = &seg->lochead;
734 * Process the segment attributes.
736 p = name;
737 while (attrs--) {
738 p += strlen(p);
739 while (!*p)
740 p++;
743 * `p' contains a segment attribute.
745 if (!nasm_stricmp(p, "private"))
746 seg->combine = CMB_PRIVATE;
747 else if (!nasm_stricmp(p, "public"))
748 seg->combine = CMB_PUBLIC;
749 else if (!nasm_stricmp(p, "common"))
750 seg->combine = CMB_COMMON;
751 else if (!nasm_stricmp(p, "use16"))
752 seg->use32 = false;
753 else if (!nasm_stricmp(p, "use32"))
754 seg->use32 = true;
755 else if (!nasm_strnicmp(p, "align=", 6)) {
756 seg->align = readnum(p + 6, &rn_error);
757 if (seg->align == 0)
758 seg->align = 1;
759 if (rn_error) {
760 seg->align = 1;
761 error(ERR_NONFATAL, "segment alignment should be"
762 " numeric");
764 switch ((int)seg->align) {
765 case 1: /* BYTE */
766 case 2: /* WORD */
767 case 4: /* DWORD */
768 case 16: /* PARA */
769 case 256: /* PAGE */
770 case 8:
771 case 32:
772 case 64:
773 case 128:
774 break;
775 default:
776 error(ERR_NONFATAL, "invalid alignment value %d",
777 seg->align);
778 seg->align = 1;
779 break;
781 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
782 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
783 if (rn_error)
784 error(ERR_NONFATAL, "argument to `absolute' segment"
785 " attribute should be numeric");
789 ieee_seg_needs_update = seg;
790 if (seg->align >= SEG_ABS)
791 deflabel(name, NO_SEG, seg->align - SEG_ABS,
792 NULL, false, false, &of_ieee, error);
793 else
794 deflabel(name, seg->index + 1, 0L,
795 NULL, false, false, &of_ieee, error);
796 ieee_seg_needs_update = NULL;
798 if (seg->use32)
799 *bits = 32;
800 else
801 *bits = 16;
802 return seg->index;
807 * directives supported
809 static int ieee_directive(char *directive, char *value, int pass)
812 (void)value;
813 (void)pass;
814 if (!strcmp(directive, "uppercase")) {
815 ieee_uppercase = true;
816 return 1;
818 return 0;
822 * Return segment data
824 static int32_t ieee_segbase(int32_t segment)
826 struct ieeeSection *seg;
829 * Find the segment in our list.
831 for (seg = seghead; seg; seg = seg->next)
832 if (seg->index == segment - 1)
833 break;
835 if (!seg)
836 return segment; /* not one of ours - leave it alone */
838 if (seg->align >= SEG_ABS)
839 return seg->align; /* absolute segment */
841 return segment; /* no special treatment */
845 * filename
847 static void ieee_filename(char *inname, char *outname, efunc error)
849 strcpy(ieee_infile, inname);
850 standard_extension(inname, outname, ".o", error);
853 static void ieee_write_file(int debuginfo)
855 struct tm *thetime;
856 time_t reltime;
857 struct FileName *fn;
858 struct ieeeSection *seg;
859 struct ieeePublic *pub, *loc;
860 struct ieeeExternal *ext;
861 struct ieeeObjData *data;
862 struct ieeeFixupp *fix;
863 struct Array *arr;
864 int i;
867 * Write the module header
869 ieee_putascii("MBFNASM,%02X%s.\r\n", strlen(ieee_infile), ieee_infile);
872 * Write the NASM boast comment.
874 ieee_putascii("CO0,%02X%s.\r\n", strlen(nasm_comment), nasm_comment);
877 * write processor-specific information
879 ieee_putascii("AD8,4,L.\r\n");
882 * date and time
884 time(&reltime);
885 thetime = localtime(&reltime);
886 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\r\n",
887 1900 + thetime->tm_year, thetime->tm_mon + 1,
888 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
889 thetime->tm_sec);
891 * if debugging, dump file names
893 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
894 ieee_putascii("C0105,%02X%s.\r\n", strlen(fn->name), fn->name);
897 ieee_putascii("CO101,07ENDHEAD.\r\n");
899 * the standard doesn't specify when to put checksums,
900 * we'll just do it periodically.
902 ieee_putcs(false);
905 * Write the section headers
907 seg = seghead;
908 if (!debuginfo && !strcmp(seg->name, "??LINE"))
909 seg = seg->next;
910 while (seg) {
911 char buf[256];
912 char attrib;
913 switch (seg->combine) {
914 case CMB_PUBLIC:
915 default:
916 attrib = 'C';
917 break;
918 case CMB_PRIVATE:
919 attrib = 'S';
920 break;
921 case CMB_COMMON:
922 attrib = 'M';
923 break;
925 ieee_unqualified_name(buf, seg->name);
926 if (seg->align >= SEG_ABS) {
927 ieee_putascii("ST%X,A,%02X%s.\r\n", seg->ieee_index,
928 strlen(buf), buf);
929 ieee_putascii("ASL%X,%lX.\r\n", seg->ieee_index,
930 (seg->align - SEG_ABS) * 16);
931 } else {
932 ieee_putascii("ST%X,%c,%02X%s.\r\n", seg->ieee_index, attrib,
933 strlen(buf), buf);
934 ieee_putascii("SA%X,%lX.\r\n", seg->ieee_index, seg->align);
935 ieee_putascii("ASS%X,%X.\r\n", seg->ieee_index,
936 seg->currentpos);
938 seg = seg->next;
941 * write the start address if there is one
943 if (ieee_entry_seg) {
944 for (seg = seghead; seg; seg = seg->next)
945 if (seg->index == ieee_entry_seg)
946 break;
947 if (!seg)
948 error(ERR_PANIC, "Start address records are incorrect");
949 else
950 ieee_putascii("ASG,R%X,%lX,+.\r\n", seg->ieee_index,
951 ieee_entry_ofs);
954 ieee_putcs(false);
956 * Write the publics
958 i = 1;
959 for (seg = seghead; seg; seg = seg->next) {
960 for (pub = seg->pubhead; pub; pub = pub->next) {
961 char buf[256];
962 ieee_unqualified_name(buf, pub->name);
963 ieee_putascii("NI%X,%02X%s.\r\n", i, strlen(buf), buf);
964 if (pub->segment == -1)
965 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i, pub->index,
966 pub->offset);
967 else
968 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i, pub->segment * 16,
969 pub->offset);
970 if (debuginfo) {
971 if (pub->type >= 0x100)
972 ieee_putascii("ATI%X,T%X.\r\n", i, pub->type - 0x100);
973 else
974 ieee_putascii("ATI%X,%X.\r\n", i, pub->type);
976 i++;
979 pub = fpubhead;
980 i = 1;
981 while (pub) {
982 char buf[256];
983 ieee_unqualified_name(buf, pub->name);
984 ieee_putascii("NI%X,%02X%s.\r\n", i, strlen(buf), buf);
985 if (pub->segment == -1)
986 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i, pub->index,
987 pub->offset);
988 else
989 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i, pub->segment * 16,
990 pub->offset);
991 if (debuginfo) {
992 if (pub->type >= 0x100)
993 ieee_putascii("ATI%X,T%X.\r\n", i, pub->type - 0x100);
994 else
995 ieee_putascii("ATI%X,%X.\r\n", i, pub->type);
997 i++;
998 pub = pub->next;
1001 * Write the externals
1003 ext = exthead;
1004 i = 1;
1005 while (ext) {
1006 char buf[256];
1007 ieee_unqualified_name(buf, ext->name);
1008 ieee_putascii("NX%X,%02X%s.\r\n", i++, strlen(buf), buf);
1009 ext = ext->next;
1011 ieee_putcs(false);
1014 * IEEE doesn't have a standard pass break record
1015 * so use the ladsoft variant
1017 ieee_putascii("CO100,06ENDSYM.\r\n");
1020 * now put types
1022 i = ARRAY_BOT;
1023 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1024 ieee_putascii("TY%X,20,%X,%lX.\r\n", i++, arr->basetype,
1025 arr->size);
1028 * now put locals
1030 i = 1;
1031 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1032 for (loc = seg->lochead; loc; loc = loc->next) {
1033 char buf[256];
1034 ieee_unqualified_name(buf, loc->name);
1035 ieee_putascii("NN%X,%02X%s.\r\n", i, strlen(buf), buf);
1036 if (loc->segment == -1)
1037 ieee_putascii("ASN%X,R%X,%lX,+.\r\n", i, loc->index,
1038 loc->offset);
1039 else
1040 ieee_putascii("ASN%X,%lX,%lX,+.\r\n", i, loc->segment * 16,
1041 loc->offset);
1042 if (debuginfo) {
1043 if (loc->type >= 0x100)
1044 ieee_putascii("ATN%X,T%X.\r\n", i, loc->type - 0x100);
1045 else
1046 ieee_putascii("ATN%X,%X.\r\n", i, loc->type);
1048 i++;
1053 * put out section data;
1055 seg = seghead;
1056 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1057 seg = seg->next;
1058 while (seg) {
1059 if (seg->currentpos) {
1060 int32_t size, org = 0;
1061 data = seg->data;
1062 ieee_putascii("SB%X.\r\n", seg->ieee_index);
1063 fix = seg->fptr;
1064 while (fix) {
1065 size = HUNKSIZE - (org % HUNKSIZE);
1066 size =
1067 size + org >
1068 seg->currentpos ? seg->currentpos - org : size;
1069 size = fix->offset - org > size ? size : fix->offset - org;
1070 org = ieee_putld(org, org + size, data->data);
1071 if (org % HUNKSIZE == 0)
1072 data = data->next;
1073 if (org == fix->offset) {
1074 org += ieee_putlr(fix);
1075 fix = fix->next;
1078 while (org < seg->currentpos && data) {
1079 size =
1080 seg->currentpos - org >
1081 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1082 org = ieee_putld(org, org + size, data->data);
1083 data = data->next;
1085 ieee_putcs(false);
1088 seg = seg->next;
1091 * module end record
1093 ieee_putascii("ME.\r\n");
1096 static void ieee_write_byte(struct ieeeSection *seg, int data)
1098 int temp;
1099 if (!(temp = seg->currentpos++ % HUNKSIZE))
1100 ieee_data_new(seg);
1101 seg->datacurr->data[temp] = data;
1104 static void ieee_write_word(struct ieeeSection *seg, int data)
1106 ieee_write_byte(seg, data & 0xFF);
1107 ieee_write_byte(seg, (data >> 8) & 0xFF);
1110 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1112 ieee_write_byte(seg, data & 0xFF);
1113 ieee_write_byte(seg, (data >> 8) & 0xFF);
1114 ieee_write_byte(seg, (data >> 16) & 0xFF);
1115 ieee_write_byte(seg, (data >> 24) & 0xFF);
1117 static void ieee_putascii(char *format, ...)
1119 char buffer[256];
1120 int i, l;
1121 va_list ap;
1123 va_start(ap, format);
1124 vsnprintf(buffer, sizeof(buffer), format, ap);
1125 l = strlen(buffer);
1126 for (i = 0; i < l; i++)
1127 if ((buffer[i] & 0xff) > 31)
1128 checksum += buffer[i];
1129 va_end(ap);
1130 fprintf(ofp, buffer);
1134 * put out a checksum record */
1135 static void ieee_putcs(int toclear)
1137 if (toclear) {
1138 ieee_putascii("CS.\r\n");
1139 } else {
1140 checksum += 'C';
1141 checksum += 'S';
1142 ieee_putascii("CS%02X.\r\n", checksum & 127);
1144 checksum = 0;
1147 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1149 int32_t val;
1150 if (start == end)
1151 return (start);
1152 val = start % HUNKSIZE;
1153 /* fill up multiple lines */
1154 while (end - start >= LDPERLINE) {
1155 int i;
1156 ieee_putascii("LD");
1157 for (i = 0; i < LDPERLINE; i++) {
1158 ieee_putascii("%02X", buf[val++]);
1159 start++;
1161 ieee_putascii(".\r\n");
1163 /* if no partial lines */
1164 if (start == end)
1165 return (start);
1166 /* make a partial line */
1167 ieee_putascii("LD");
1168 while (start < end) {
1169 ieee_putascii("%02X", buf[val++]);
1170 start++;
1172 ieee_putascii(".\r\n");
1173 return (start);
1175 static int32_t ieee_putlr(struct ieeeFixupp *p)
1178 * To deal with the vagaries of segmentation the LADsoft linker
1179 * defines two types of segments: absolute and virtual. Note that
1180 * 'absolute' in this context is a different thing from the IEEE
1181 * definition of an absolute segment type, which is also supported. If a
1182 * sement is linked in virtual mode the low limit (L-var) is
1183 * subtracted from each R,X, and P variable which appears in an
1184 * expression, so that we can have relative offsets. Meanwhile
1185 * in the ABSOLUTE mode this subtraction is not done and
1186 * so we can use absolute offsets from 0. In the LADsoft linker
1187 * this configuration is not done in the assemblker source but in
1188 * a source the linker reads. Generally this type of thing only
1189 * becomes an issue if real mode code is used. A pure 32-bit linker could
1190 * get away without defining the virtual mode...
1192 char buf[40];
1193 int32_t size = p->size;
1194 switch (p->ftype) {
1195 case FT_SEG:
1196 if (p->id1 < 0)
1197 sprintf(buf, "%"PRIX32"", -p->id1);
1198 else
1199 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1200 break;
1201 case FT_OFS:
1202 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1203 break;
1204 case FT_REL:
1205 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1206 break;
1208 case FT_WRT:
1209 if (p->id2 < 0)
1210 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1211 p->id2, -p->id1 * 16);
1212 else
1213 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1214 p->id2, p->id1);
1215 break;
1216 case FT_EXT:
1217 sprintf(buf, "X%"PRIX32"", p->id1);
1218 break;
1219 case FT_EXTREL:
1220 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1221 break;
1222 case FT_EXTSEG:
1223 /* We needed a non-ieee hack here.
1224 * We introduce the Y variable, which is the low
1225 * limit of the native segment the extern resides in
1227 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1228 break;
1229 case FT_EXTWRT:
1230 if (p->id2 < 0)
1231 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1232 -p->id1 * 16);
1233 else
1234 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1235 break;
1237 ieee_putascii("LR(%s,%"PRIX32").\r\n", buf, size);
1239 return (size);
1242 /* Dump all segment data (text and fixups )*/
1244 static void ieee_unqualified_name(char *dest, char *source)
1246 if (ieee_uppercase) {
1247 while (*source)
1248 *dest++ = toupper(*source++);
1249 *dest = 0;
1250 } else
1251 strcpy(dest, source);
1253 void dbgls_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1255 int tempint;
1256 (void)of;
1257 (void)id;
1258 (void)fp;
1259 (void)error;
1261 fnhead = NULL;
1262 fntail = &fnhead;
1263 arrindex = ARRAY_BOT;
1264 arrhead = NULL;
1265 arrtail = &arrhead;
1266 ieee_segment("??LINE", 2, &tempint);
1267 any_segs = false;
1269 static void dbgls_cleanup(void)
1271 struct ieeeSection *segtmp;
1272 while (fnhead) {
1273 struct FileName *fntemp = fnhead;
1274 fnhead = fnhead->next;
1275 nasm_free(fntemp->name);
1276 nasm_free(fntemp);
1278 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1279 while (segtmp->lochead) {
1280 struct ieeePublic *loctmp = segtmp->lochead;
1281 segtmp->lochead = loctmp->next;
1282 nasm_free(loctmp->name);
1283 nasm_free(loctmp);
1286 while (arrhead) {
1287 struct Array *arrtmp = arrhead;
1288 arrhead = arrhead->next;
1289 nasm_free(arrtmp);
1294 * because this routine is not bracketed in
1295 * the main program, this routine will be called even if there
1296 * is no request for debug info
1297 * so, we have to make sure the ??LINE segment is avaialbe
1298 * as the first segment when this debug format is selected
1300 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1302 struct FileName *fn;
1303 struct ieeeSection *seg;
1304 int i = 0;
1305 if (segto == NO_SEG)
1306 return;
1309 * If `any_segs' is still false, we must define a default
1310 * segment.
1312 if (!any_segs) {
1313 int tempint; /* ignored */
1314 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1315 error(ERR_PANIC, "strange segment conditions in OBJ driver");
1319 * Find the segment we are targetting.
1321 for (seg = seghead; seg; seg = seg->next)
1322 if (seg->index == segto)
1323 break;
1324 if (!seg)
1325 error(ERR_PANIC, "lineno directed to nonexistent segment?");
1327 for (fn = fnhead; fn; fn = fn->next) {
1328 if (!nasm_stricmp(lnfname, fn->name))
1329 break;
1330 i++;
1332 if (!fn) {
1333 fn = nasm_malloc(sizeof(*fn));
1334 fn->name = nasm_malloc(strlen(lnfname) + 1);
1335 fn->index = i;
1336 strcpy(fn->name, lnfname);
1337 fn->next = NULL;
1338 *fntail = fn;
1339 fntail = &fn->next;
1341 ieee_write_byte(seghead, fn->index);
1342 ieee_write_word(seghead, lineno);
1343 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1344 seg->currentpos);
1347 static void dbgls_deflabel(char *name, int32_t segment,
1348 int64_t offset, int is_global, char *special)
1350 struct ieeeSection *seg;
1352 /* Keep compiler from warning about special */
1353 (void)special;
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(int32_t 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; /* uint8_t */
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 &ladsoft_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 */