Add a generic pragma-handling infrastructure
[nasm.git] / output / outieee.c
blob7607e67395834857c117c0e518c8e6ac6c8940bb
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outieee.c output routines for the Netwide Assembler to produce
36 * IEEE-std object files
39 /* notes: I have tried to make this correspond to the IEEE version
40 * of the standard, specifically the primary ASCII version. It should
41 * be trivial to create the binary version given this source (which is
42 * one of MANY things that have to be done to make this correspond to
43 * the hp-microtek version of the standard).
45 * 16-bit support is assumed to use 24-bit addresses
46 * The linker can sort out segmentation-specific stuff
47 * if it keeps track of externals
48 * in terms of being relative to section bases
50 * A non-standard variable type, the 'Yn' variable, has been introduced.
51 * Basically it is a reference to extern 'n'- denoting the low limit
52 * (L-variable) of the section that extern 'n' is defined in. Like the
53 * x variable, there may be no explicit assignment to it, it is derived
54 * from the public definition corresponding to the extern name. This
55 * is required because the one thing the mufom guys forgot to do well was
56 * take into account segmented architectures.
58 * I use comment classes for various things and these are undefined by
59 * the standard.
61 * Debug info should be considered totally non-standard (local labels are
62 * standard but linenum records are not covered by the standard.
63 * Type defs have the standard format but absolute meanings for ordinal
64 * types are not covered by the standard.)
66 * David Lindauer, LADsoft
68 #include "compiler.h"
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <time.h>
74 #include <stdarg.h> /* Note: we need the ANSI version of stdarg.h */
75 #include <ctype.h>
77 #include "nasm.h"
78 #include "nasmlib.h"
79 #include "error.h"
80 #include "ver.h"
82 #include "outform.h"
83 #include "outlib.h"
85 #ifdef OF_IEEE
87 #define ARRAY_BOT 0x1
89 static char ieee_infile[FILENAME_MAX];
90 static int ieee_uppercase;
92 static bool any_segs;
93 static int arrindex;
95 #define HUNKSIZE 1024 /* Size of the data hunk */
96 #define EXT_BLKSIZ 512
97 #define LDPERLINE 32 /* bytes per line in output */
99 struct ieeeSection;
101 struct LineNumber {
102 struct LineNumber *next;
103 struct ieeeSection *segment;
104 int32_t offset;
105 int32_t lineno;
108 static struct FileName {
109 struct FileName *next;
110 char *name;
111 int32_t index;
112 } *fnhead, **fntail;
114 static struct Array {
115 struct Array *next;
116 unsigned size;
117 int basetype;
118 } *arrhead, **arrtail;
120 static struct ieeePublic {
121 struct ieeePublic *next;
122 char *name;
123 int32_t offset;
124 int32_t segment; /* only if it's far-absolute */
125 int32_t index;
126 int type; /* for debug purposes */
127 } *fpubhead, **fpubtail, *last_defined;
129 static struct ieeeExternal {
130 struct ieeeExternal *next;
131 char *name;
132 int32_t commonsize;
133 } *exthead, **exttail;
135 static int externals;
137 static struct ExtBack {
138 struct ExtBack *next;
139 int index[EXT_BLKSIZ];
140 } *ebhead, **ebtail;
142 /* NOTE: the first segment MUST be the lineno segment */
143 static struct ieeeSection {
144 struct ieeeSection *next;
145 char *name;
146 struct ieeeObjData *data, *datacurr;
147 struct ieeeFixupp *fptr, *flptr;
148 int32_t index; /* the NASM segment id */
149 int32_t ieee_index; /* the OBJ-file segment index */
150 int32_t currentpos;
151 int32_t align; /* can be SEG_ABS + absolute addr */
152 int32_t startpos;
153 int32_t use32; /* is this segment 32-bit? */
154 struct ieeePublic *pubhead, **pubtail, *lochead, **loctail;
155 enum {
156 CMB_PRIVATE = 0,
157 CMB_PUBLIC = 2,
158 CMB_COMMON = 6
159 } combine;
160 } *seghead, **segtail, *ieee_seg_needs_update;
162 struct ieeeObjData {
163 struct ieeeObjData *next;
164 uint8_t data[HUNKSIZE];
167 struct ieeeFixupp {
168 struct ieeeFixupp *next;
169 enum {
170 FT_SEG = 0,
171 FT_REL = 1,
172 FT_OFS = 2,
173 FT_EXT = 3,
174 FT_WRT = 4,
175 FT_EXTREL = 5,
176 FT_EXTWRT = 6,
177 FT_EXTSEG = 7
178 } ftype;
179 int16_t size;
180 int32_t id1;
181 int32_t id2;
182 int32_t offset;
183 int32_t addend;
186 static int32_t ieee_entry_seg, ieee_entry_ofs;
187 static int checksum;
189 extern const struct ofmt of_ieee;
190 static const struct dfmt ladsoft_debug_form;
192 static void ieee_data_new(struct ieeeSection *);
193 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection *,
194 int, uint64_t, int32_t);
195 static void ieee_install_fixup(struct ieeeSection *, struct ieeeFixupp *);
196 static int32_t ieee_segment(char *, int, int *);
197 static void ieee_write_file(void);
198 static void ieee_write_byte(struct ieeeSection *, int);
199 static void ieee_write_word(struct ieeeSection *, int);
200 static void ieee_write_dword(struct ieeeSection *, int32_t);
201 static void ieee_putascii(char *, ...);
202 static void ieee_putcs(int);
203 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
204 static int32_t ieee_putlr(struct ieeeFixupp *);
205 static void ieee_unqualified_name(char *, char *);
208 * pup init
210 static void ieee_init(void)
212 any_segs = false;
213 fpubhead = NULL;
214 fpubtail = &fpubhead;
215 exthead = NULL;
216 exttail = &exthead;
217 externals = 1;
218 ebhead = NULL;
219 ebtail = &ebhead;
220 seghead = ieee_seg_needs_update = NULL;
221 segtail = &seghead;
222 ieee_entry_seg = NO_SEG;
223 ieee_uppercase = false;
224 checksum = 0;
227 static int ieee_set_info(enum geninfo type, char **val)
229 (void)type;
230 (void)val;
232 return 0;
236 * Rundown
238 static void ieee_cleanup(void)
240 ieee_write_file();
241 dfmt->cleanup();
242 while (seghead) {
243 struct ieeeSection *segtmp = seghead;
244 seghead = seghead->next;
245 while (segtmp->pubhead) {
246 struct ieeePublic *pubtmp = segtmp->pubhead;
247 segtmp->pubhead = pubtmp->next;
248 nasm_free(pubtmp);
250 while (segtmp->fptr) {
251 struct ieeeFixupp *fixtmp = segtmp->fptr;
252 segtmp->fptr = fixtmp->next;
253 nasm_free(fixtmp);
255 while (segtmp->data) {
256 struct ieeeObjData *dattmp = segtmp->data;
257 segtmp->data = dattmp->next;
258 nasm_free(dattmp);
260 nasm_free(segtmp);
262 while (fpubhead) {
263 struct ieeePublic *pubtmp = fpubhead;
264 fpubhead = fpubhead->next;
265 nasm_free(pubtmp);
267 while (exthead) {
268 struct ieeeExternal *exttmp = exthead;
269 exthead = exthead->next;
270 nasm_free(exttmp);
272 while (ebhead) {
273 struct ExtBack *ebtmp = ebhead;
274 ebhead = ebhead->next;
275 nasm_free(ebtmp);
280 * callback for labels
282 static void ieee_deflabel(char *name, int32_t segment,
283 int64_t offset, int is_global, char *special)
286 * We have three cases:
288 * (i) `segment' is a segment-base. If so, set the name field
289 * for the segment structure it refers to, and then
290 * return.
292 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
293 * Save the label position for later output of a PUBDEF record.
296 * (iii) `segment' is not one of our segments. Save the label
297 * position for later output of an EXTDEF.
299 struct ieeeExternal *ext;
300 struct ExtBack *eb;
301 struct ieeeSection *seg;
302 int i;
304 if (special) {
305 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
308 * First check for the double-period, signifying something
309 * unusual.
311 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
312 if (!strcmp(name, "..start")) {
313 ieee_entry_seg = segment;
314 ieee_entry_ofs = offset;
316 return;
320 * Case (i):
322 if (ieee_seg_needs_update) {
323 ieee_seg_needs_update->name = name;
324 return;
326 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
327 return;
330 * case (ii)
332 if (segment >= SEG_ABS) {
334 * SEG_ABS subcase of (ii).
336 if (is_global) {
337 struct ieeePublic *pub;
339 pub = *fpubtail = nasm_malloc(sizeof(*pub));
340 fpubtail = &pub->next;
341 pub->next = NULL;
342 pub->name = name;
343 pub->offset = offset;
344 pub->segment = segment & ~SEG_ABS;
346 return;
349 for (seg = seghead; seg && is_global; seg = seg->next)
350 if (seg->index == segment) {
351 struct ieeePublic *pub;
353 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
354 seg->pubtail = &pub->next;
355 pub->next = NULL;
356 pub->name = name;
357 pub->offset = offset;
358 pub->index = seg->ieee_index;
359 pub->segment = -1;
360 return;
364 * Case (iii).
366 if (is_global) {
367 ext = *exttail = nasm_malloc(sizeof(*ext));
368 ext->next = NULL;
369 exttail = &ext->next;
370 ext->name = name;
371 if (is_global == 2)
372 ext->commonsize = offset;
373 else
374 ext->commonsize = 0;
375 i = segment / 2;
376 eb = ebhead;
377 if (!eb) {
378 eb = *ebtail = nasm_zalloc(sizeof(*eb));
379 eb->next = NULL;
380 ebtail = &eb->next;
382 while (i > EXT_BLKSIZ) {
383 if (eb && eb->next)
384 eb = eb->next;
385 else {
386 eb = *ebtail = nasm_zalloc(sizeof(*eb));
387 eb->next = NULL;
388 ebtail = &eb->next;
390 i -= EXT_BLKSIZ;
392 eb->index[i] = externals++;
398 * Put data out
400 static void ieee_out(int32_t segto, const void *data,
401 enum out_type type, uint64_t size,
402 int32_t segment, int32_t wrt)
404 const uint8_t *ucdata;
405 int32_t ldata;
406 struct ieeeSection *seg;
409 * handle absolute-assembly (structure definitions)
411 if (segto == NO_SEG) {
412 if (type != OUT_RESERVE)
413 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
414 " space");
415 return;
419 * If `any_segs' is still false, we must define a default
420 * segment.
422 if (!any_segs) {
423 int tempint; /* ignored */
424 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
425 nasm_panic(0, "strange segment conditions in IEEE driver");
429 * Find the segment we are targetting.
431 for (seg = seghead; seg; seg = seg->next)
432 if (seg->index == segto)
433 break;
434 if (!seg)
435 nasm_panic(0, "code directed to nonexistent segment?");
437 if (type == OUT_RAWDATA) {
438 ucdata = data;
439 while (size--)
440 ieee_write_byte(seg, *ucdata++);
441 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
442 type == OUT_REL4ADR) {
443 if (type == OUT_ADDRESS)
444 size = abs((int)size);
445 else if (segment == NO_SEG)
446 nasm_error(ERR_NONFATAL, "relative call to absolute address not"
447 " supported by IEEE format");
448 ldata = *(int64_t *)data;
449 if (type == OUT_REL2ADR)
450 ldata += (size - 2);
451 if (type == OUT_REL4ADR)
452 ldata += (size - 4);
453 ieee_write_fixup(segment, wrt, seg, size, type, ldata);
454 if (size == 2)
455 ieee_write_word(seg, ldata);
456 else
457 ieee_write_dword(seg, ldata);
458 } else if (type == OUT_RESERVE) {
459 while (size--)
460 ieee_write_byte(seg, 0);
464 static void ieee_data_new(struct ieeeSection *segto)
467 if (!segto->data)
468 segto->data = segto->datacurr =
469 nasm_malloc(sizeof(*(segto->datacurr)));
470 else
471 segto->datacurr = segto->datacurr->next =
472 nasm_malloc(sizeof(*(segto->datacurr)));
473 segto->datacurr->next = NULL;
477 * this routine is unalduterated bloatware. I usually don't do this
478 * but I might as well see what it is like on a harmless program.
479 * If anyone wants to optimize this is a good canditate!
481 static void ieee_write_fixup(int32_t segment, int32_t wrt,
482 struct ieeeSection *segto, int size,
483 uint64_t realtype, int32_t offset)
485 struct ieeeSection *target;
486 struct ieeeFixupp s;
488 /* Don't put a fixup for things NASM can calculate */
489 if (wrt == NO_SEG && segment == NO_SEG)
490 return;
492 s.ftype = -1;
493 /* if it is a WRT offset */
494 if (wrt != NO_SEG) {
495 s.ftype = FT_WRT;
496 s.addend = offset;
497 if (wrt >= SEG_ABS)
498 s.id1 = -(wrt - SEG_ABS);
499 else {
500 if (wrt % 2 && realtype != OUT_REL2ADR
501 && realtype != OUT_REL4ADR) {
502 wrt--;
504 for (target = seghead; target; target = target->next)
505 if (target->index == wrt)
506 break;
507 if (target) {
508 s.id1 = target->ieee_index;
509 for (target = seghead; target; target = target->next)
510 if (target->index == segment)
511 break;
513 if (target)
514 s.id2 = target->ieee_index;
515 else {
517 * Now we assume the segment field is being used
518 * to hold an extern index
520 int32_t i = segment / 2;
521 struct ExtBack *eb = ebhead;
522 while (i > EXT_BLKSIZ) {
523 if (eb)
524 eb = eb->next;
525 else
526 break;
527 i -= EXT_BLKSIZ;
529 /* if we have an extern decide the type and make a record
531 if (eb) {
532 s.ftype = FT_EXTWRT;
533 s.addend = 0;
534 s.id2 = eb->index[i];
535 } else
536 nasm_error(ERR_NONFATAL,
537 "Source of WRT must be an offset");
540 } else
541 nasm_panic(0,
542 "unrecognised WRT value in ieee_write_fixup");
543 } else
544 nasm_error(ERR_NONFATAL, "target of WRT must be a section ");
546 s.size = size;
547 ieee_install_fixup(segto, &s);
548 return;
550 /* Pure segment fixup ? */
551 if (segment != NO_SEG) {
552 s.ftype = FT_SEG;
553 s.id1 = 0;
554 if (segment >= SEG_ABS) {
555 /* absolute far segment fixup */
556 s.id1 = -(segment - ~SEG_ABS);
557 } else if (segment % 2) {
558 /* fixup to named segment */
559 /* look it up */
560 for (target = seghead; target; target = target->next)
561 if (target->index == segment - 1)
562 break;
563 if (target)
564 s.id1 = target->ieee_index;
565 else {
567 * Now we assume the segment field is being used
568 * to hold an extern index
570 int32_t i = segment / 2;
571 struct ExtBack *eb = ebhead;
572 while (i > EXT_BLKSIZ) {
573 if (eb)
574 eb = eb->next;
575 else
576 break;
577 i -= EXT_BLKSIZ;
579 /* if we have an extern decide the type and make a record
581 if (eb) {
582 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
583 nasm_panic(0,
584 "Segment of a rel not supported in ieee_write_fixup");
585 } else {
586 /* If we want the segment */
587 s.ftype = FT_EXTSEG;
588 s.addend = 0;
589 s.id1 = eb->index[i];
592 } else
593 /* If we get here the seg value doesn't make sense */
594 nasm_panic(0,
595 "unrecognised segment value in ieee_write_fixup");
598 } else {
599 /* Assume we are offsetting directly from a section
600 * So look up the target segment
602 for (target = seghead; target; target = target->next)
603 if (target->index == segment)
604 break;
605 if (target) {
606 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
607 /* PC rel to a known offset */
608 s.id1 = target->ieee_index;
609 s.ftype = FT_REL;
610 s.size = size;
611 s.addend = offset;
612 } else {
613 /* We were offsetting from a seg */
614 s.id1 = target->ieee_index;
615 s.ftype = FT_OFS;
616 s.size = size;
617 s.addend = offset;
619 } else {
621 * Now we assume the segment field is being used
622 * to hold an extern index
624 int32_t i = segment / 2;
625 struct ExtBack *eb = ebhead;
626 while (i > EXT_BLKSIZ) {
627 if (eb)
628 eb = eb->next;
629 else
630 break;
631 i -= EXT_BLKSIZ;
633 /* if we have an extern decide the type and make a record
635 if (eb) {
636 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
637 s.ftype = FT_EXTREL;
638 s.addend = 0;
639 s.id1 = eb->index[i];
640 } else {
641 /* else we want the external offset */
642 s.ftype = FT_EXT;
643 s.addend = 0;
644 s.id1 = eb->index[i];
647 } else
648 /* If we get here the seg value doesn't make sense */
649 nasm_panic(0,
650 "unrecognised segment value in ieee_write_fixup");
653 if (size != 2 && s.ftype == FT_SEG)
654 nasm_error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
655 " segment base references");
656 s.size = size;
657 ieee_install_fixup(segto, &s);
658 return;
660 /* should never get here */
662 static void ieee_install_fixup(struct ieeeSection *seg,
663 struct ieeeFixupp *fix)
665 struct ieeeFixupp *f;
666 f = nasm_malloc(sizeof(struct ieeeFixupp));
667 memcpy(f, fix, sizeof(struct ieeeFixupp));
668 f->offset = seg->currentpos;
669 seg->currentpos += fix->size;
670 f->next = NULL;
671 if (seg->fptr)
672 seg->flptr = seg->flptr->next = f;
673 else
674 seg->fptr = seg->flptr = f;
679 * segment registry
681 static int32_t ieee_segment(char *name, int pass, int *bits)
684 * We call the label manager here to define a name for the new
685 * segment, and when our _own_ label-definition stub gets
686 * called in return, it should register the new segment name
687 * using the pointer it gets passed. That way we save memory,
688 * by sponging off the label manager.
690 if (!name) {
691 *bits = 16;
692 if (!any_segs)
693 return 0;
694 return seghead->index;
695 } else {
696 struct ieeeSection *seg;
697 int ieee_idx, attrs;
698 bool rn_error;
699 char *p;
702 * Look for segment attributes.
704 attrs = 0;
705 while (*name == '.')
706 name++; /* hack, but a documented one */
707 p = name;
708 while (*p && !nasm_isspace(*p))
709 p++;
710 if (*p) {
711 *p++ = '\0';
712 while (*p && nasm_isspace(*p))
713 *p++ = '\0';
715 while (*p) {
716 while (*p && !nasm_isspace(*p))
717 p++;
718 if (*p) {
719 *p++ = '\0';
720 while (*p && nasm_isspace(*p))
721 *p++ = '\0';
724 attrs++;
727 ieee_idx = 1;
728 for (seg = seghead; seg; seg = seg->next) {
729 ieee_idx++;
730 if (!strcmp(seg->name, name)) {
731 if (attrs > 0 && pass == 1)
732 nasm_error(ERR_WARNING, "segment attributes specified on"
733 " redeclaration of segment: ignoring");
734 if (seg->use32)
735 *bits = 32;
736 else
737 *bits = 16;
738 return seg->index;
742 *segtail = seg = nasm_malloc(sizeof(*seg));
743 seg->next = NULL;
744 segtail = &seg->next;
745 seg->index = seg_alloc();
746 seg->ieee_index = ieee_idx;
747 any_segs = true;
748 seg->name = NULL;
749 seg->currentpos = 0;
750 seg->align = 1; /* default */
751 seg->use32 = *bits == 32; /* default to user spec */
752 seg->combine = CMB_PUBLIC; /* default */
753 seg->pubhead = NULL;
754 seg->pubtail = &seg->pubhead;
755 seg->data = NULL;
756 seg->fptr = NULL;
757 seg->lochead = NULL;
758 seg->loctail = &seg->lochead;
761 * Process the segment attributes.
763 p = name;
764 while (attrs--) {
765 p += strlen(p);
766 while (!*p)
767 p++;
770 * `p' contains a segment attribute.
772 if (!nasm_stricmp(p, "private"))
773 seg->combine = CMB_PRIVATE;
774 else if (!nasm_stricmp(p, "public"))
775 seg->combine = CMB_PUBLIC;
776 else if (!nasm_stricmp(p, "common"))
777 seg->combine = CMB_COMMON;
778 else if (!nasm_stricmp(p, "use16"))
779 seg->use32 = false;
780 else if (!nasm_stricmp(p, "use32"))
781 seg->use32 = true;
782 else if (!nasm_strnicmp(p, "align=", 6)) {
783 seg->align = readnum(p + 6, &rn_error);
784 if (seg->align == 0)
785 seg->align = 1;
786 if (rn_error) {
787 seg->align = 1;
788 nasm_error(ERR_NONFATAL, "segment alignment should be"
789 " numeric");
791 switch (seg->align) {
792 case 1: /* BYTE */
793 case 2: /* WORD */
794 case 4: /* DWORD */
795 case 16: /* PARA */
796 case 256: /* PAGE */
797 case 8:
798 case 32:
799 case 64:
800 case 128:
801 break;
802 default:
803 nasm_error(ERR_NONFATAL, "invalid alignment value %d",
804 seg->align);
805 seg->align = 1;
806 break;
808 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
809 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
810 if (rn_error)
811 nasm_error(ERR_NONFATAL, "argument to `absolute' segment"
812 " attribute should be numeric");
816 ieee_seg_needs_update = seg;
817 if (seg->align >= SEG_ABS)
818 define_label(name, NO_SEG, seg->align - SEG_ABS,
819 NULL, false, false);
820 else
821 define_label(name, seg->index + 1, 0L, NULL, false, false);
822 ieee_seg_needs_update = NULL;
824 if (seg->use32)
825 *bits = 32;
826 else
827 *bits = 16;
828 return seg->index;
833 * directives supported
835 static int ieee_directive(enum directives directive, char *value, int pass)
838 (void)value;
839 (void)pass;
841 switch (directive) {
842 case D_UPPERCASE:
843 ieee_uppercase = true;
844 return 1;
846 default:
847 return 0;
851 static void ieee_sectalign(int32_t seg, unsigned int value)
853 struct ieeeSection *s;
855 list_for_each(s, seghead) {
856 if (s->index == seg)
857 break;
861 * 256 is maximum there, note it may happen
862 * that align is issued on "absolute" segment
863 * it's fine since SEG_ABS > 256 and we never
864 * get escape this test
866 if (!s || !is_power2(value) || value > 256)
867 return;
869 if ((unsigned int)s->align < value)
870 s->align = value;
874 * Return segment data
876 static int32_t ieee_segbase(int32_t segment)
878 struct ieeeSection *seg;
881 * Find the segment in our list.
883 for (seg = seghead; seg; seg = seg->next)
884 if (seg->index == segment - 1)
885 break;
887 if (!seg)
888 return segment; /* not one of ours - leave it alone */
890 if (seg->align >= SEG_ABS)
891 return seg->align; /* absolute segment */
893 return segment; /* no special treatment */
897 * filename
899 static void ieee_filename(char *inname, char *outname)
901 strcpy(ieee_infile, inname);
902 standard_extension(inname, outname, ".o");
905 static void ieee_write_file(void)
907 struct tm *thetime;
908 time_t reltime;
909 struct FileName *fn;
910 struct ieeeSection *seg;
911 struct ieeePublic *pub, *loc;
912 struct ieeeExternal *ext;
913 struct ieeeObjData *data;
914 struct ieeeFixupp *fix;
915 struct Array *arr;
916 int i;
917 const bool debuginfo = (dfmt == &ladsoft_debug_form);
920 * Write the module header
922 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile), ieee_infile);
925 * Write the NASM boast comment.
927 ieee_putascii("CO0,%02X%s.\n", strlen(nasm_comment), nasm_comment);
930 * write processor-specific information
932 ieee_putascii("AD8,4,L.\n");
935 * date and time
937 time(&reltime);
938 thetime = localtime(&reltime);
939 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
940 1900 + thetime->tm_year, thetime->tm_mon + 1,
941 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
942 thetime->tm_sec);
944 * if debugging, dump file names
946 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
947 ieee_putascii("C0105,%02X%s.\n", strlen(fn->name), fn->name);
950 ieee_putascii("CO101,07ENDHEAD.\n");
952 * the standard doesn't specify when to put checksums,
953 * we'll just do it periodically.
955 ieee_putcs(false);
958 * Write the section headers
960 seg = seghead;
961 if (!debuginfo && !strcmp(seg->name, "??LINE"))
962 seg = seg->next;
963 while (seg) {
964 char buf[256];
965 char attrib;
966 switch (seg->combine) {
967 case CMB_PUBLIC:
968 default:
969 attrib = 'C';
970 break;
971 case CMB_PRIVATE:
972 attrib = 'S';
973 break;
974 case CMB_COMMON:
975 attrib = 'M';
976 break;
978 ieee_unqualified_name(buf, seg->name);
979 if (seg->align >= SEG_ABS) {
980 ieee_putascii("ST%X,A,%02X%s.\n", seg->ieee_index,
981 strlen(buf), buf);
982 ieee_putascii("ASL%X,%lX.\n", seg->ieee_index,
983 (seg->align - SEG_ABS) * 16);
984 } else {
985 ieee_putascii("ST%X,%c,%02X%s.\n", seg->ieee_index, attrib,
986 strlen(buf), buf);
987 ieee_putascii("SA%X,%lX.\n", seg->ieee_index, seg->align);
988 ieee_putascii("ASS%X,%X.\n", seg->ieee_index,
989 seg->currentpos);
991 seg = seg->next;
994 * write the start address if there is one
996 if (ieee_entry_seg) {
997 for (seg = seghead; seg; seg = seg->next)
998 if (seg->index == ieee_entry_seg)
999 break;
1000 if (!seg)
1001 nasm_panic(0, "Start address records are incorrect");
1002 else
1003 ieee_putascii("ASG,R%X,%lX,+.\n", seg->ieee_index,
1004 ieee_entry_ofs);
1007 ieee_putcs(false);
1009 * Write the publics
1011 i = 1;
1012 for (seg = seghead; seg; seg = seg->next) {
1013 for (pub = seg->pubhead; pub; pub = pub->next) {
1014 char buf[256];
1015 ieee_unqualified_name(buf, pub->name);
1016 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1017 if (pub->segment == -1)
1018 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1019 pub->offset);
1020 else
1021 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1022 pub->offset);
1023 if (debuginfo) {
1024 if (pub->type >= 0x100)
1025 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1026 else
1027 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1029 i++;
1032 pub = fpubhead;
1033 i = 1;
1034 while (pub) {
1035 char buf[256];
1036 ieee_unqualified_name(buf, pub->name);
1037 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1038 if (pub->segment == -1)
1039 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1040 pub->offset);
1041 else
1042 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1043 pub->offset);
1044 if (debuginfo) {
1045 if (pub->type >= 0x100)
1046 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1047 else
1048 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1050 i++;
1051 pub = pub->next;
1054 * Write the externals
1056 ext = exthead;
1057 i = 1;
1058 while (ext) {
1059 char buf[256];
1060 ieee_unqualified_name(buf, ext->name);
1061 ieee_putascii("NX%X,%02X%s.\n", i++, strlen(buf), buf);
1062 ext = ext->next;
1064 ieee_putcs(false);
1067 * IEEE doesn't have a standard pass break record
1068 * so use the ladsoft variant
1070 ieee_putascii("CO100,06ENDSYM.\n");
1073 * now put types
1075 i = ARRAY_BOT;
1076 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1077 ieee_putascii("TY%X,20,%X,%lX.\n", i++, arr->basetype,
1078 arr->size);
1081 * now put locals
1083 i = 1;
1084 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1085 for (loc = seg->lochead; loc; loc = loc->next) {
1086 char buf[256];
1087 ieee_unqualified_name(buf, loc->name);
1088 ieee_putascii("NN%X,%02X%s.\n", i, strlen(buf), buf);
1089 if (loc->segment == -1)
1090 ieee_putascii("ASN%X,R%X,%lX,+.\n", i, loc->index,
1091 loc->offset);
1092 else
1093 ieee_putascii("ASN%X,%lX,%lX,+.\n", i, loc->segment * 16,
1094 loc->offset);
1095 if (debuginfo) {
1096 if (loc->type >= 0x100)
1097 ieee_putascii("ATN%X,T%X.\n", i, loc->type - 0x100);
1098 else
1099 ieee_putascii("ATN%X,%X.\n", i, loc->type);
1101 i++;
1106 * put out section data;
1108 seg = seghead;
1109 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1110 seg = seg->next;
1111 while (seg) {
1112 if (seg->currentpos) {
1113 int32_t size, org = 0;
1114 data = seg->data;
1115 ieee_putascii("SB%X.\n", seg->ieee_index);
1116 fix = seg->fptr;
1117 while (fix) {
1118 size = HUNKSIZE - (org % HUNKSIZE);
1119 size =
1120 size + org >
1121 seg->currentpos ? seg->currentpos - org : size;
1122 size = fix->offset - org > size ? size : fix->offset - org;
1123 org = ieee_putld(org, org + size, data->data);
1124 if (org % HUNKSIZE == 0)
1125 data = data->next;
1126 if (org == fix->offset) {
1127 org += ieee_putlr(fix);
1128 fix = fix->next;
1131 while (org < seg->currentpos && data) {
1132 size =
1133 seg->currentpos - org >
1134 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1135 org = ieee_putld(org, org + size, data->data);
1136 data = data->next;
1138 ieee_putcs(false);
1141 seg = seg->next;
1144 * module end record
1146 ieee_putascii("ME.\n");
1149 static void ieee_write_byte(struct ieeeSection *seg, int data)
1151 int temp;
1152 if (!(temp = seg->currentpos++ % HUNKSIZE))
1153 ieee_data_new(seg);
1154 seg->datacurr->data[temp] = data;
1157 static void ieee_write_word(struct ieeeSection *seg, int data)
1159 ieee_write_byte(seg, data & 0xFF);
1160 ieee_write_byte(seg, (data >> 8) & 0xFF);
1163 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1165 ieee_write_byte(seg, data & 0xFF);
1166 ieee_write_byte(seg, (data >> 8) & 0xFF);
1167 ieee_write_byte(seg, (data >> 16) & 0xFF);
1168 ieee_write_byte(seg, (data >> 24) & 0xFF);
1170 static void ieee_putascii(char *format, ...)
1172 char buffer[256];
1173 int i, l;
1174 va_list ap;
1176 va_start(ap, format);
1177 vsnprintf(buffer, sizeof(buffer), format, ap);
1178 l = strlen(buffer);
1179 for (i = 0; i < l; i++)
1180 if ((uint8_t)buffer[i] > 31)
1181 checksum += buffer[i];
1182 va_end(ap);
1183 fputs(buffer, ofile);
1187 * put out a checksum record */
1188 static void ieee_putcs(int toclear)
1190 if (toclear) {
1191 ieee_putascii("CS.\n");
1192 } else {
1193 checksum += 'C';
1194 checksum += 'S';
1195 ieee_putascii("CS%02X.\n", checksum & 127);
1197 checksum = 0;
1200 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1202 int32_t val;
1203 if (start == end)
1204 return (start);
1205 val = start % HUNKSIZE;
1206 /* fill up multiple lines */
1207 while (end - start >= LDPERLINE) {
1208 int i;
1209 ieee_putascii("LD");
1210 for (i = 0; i < LDPERLINE; i++) {
1211 ieee_putascii("%02X", buf[val++]);
1212 start++;
1214 ieee_putascii(".\n");
1216 /* if no partial lines */
1217 if (start == end)
1218 return (start);
1219 /* make a partial line */
1220 ieee_putascii("LD");
1221 while (start < end) {
1222 ieee_putascii("%02X", buf[val++]);
1223 start++;
1225 ieee_putascii(".\n");
1226 return (start);
1228 static int32_t ieee_putlr(struct ieeeFixupp *p)
1231 * To deal with the vagaries of segmentation the LADsoft linker
1232 * defines two types of segments: absolute and virtual. Note that
1233 * 'absolute' in this context is a different thing from the IEEE
1234 * definition of an absolute segment type, which is also supported. If a
1235 * sement is linked in virtual mode the low limit (L-var) is
1236 * subtracted from each R,X, and P variable which appears in an
1237 * expression, so that we can have relative offsets. Meanwhile
1238 * in the ABSOLUTE mode this subtraction is not done and
1239 * so we can use absolute offsets from 0. In the LADsoft linker
1240 * this configuration is not done in the assemblker source but in
1241 * a source the linker reads. Generally this type of thing only
1242 * becomes an issue if real mode code is used. A pure 32-bit linker could
1243 * get away without defining the virtual mode...
1245 char buf[40];
1246 int32_t size = p->size;
1247 switch (p->ftype) {
1248 case FT_SEG:
1249 if (p->id1 < 0)
1250 sprintf(buf, "%"PRIX32"", -p->id1);
1251 else
1252 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1253 break;
1254 case FT_OFS:
1255 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1256 break;
1257 case FT_REL:
1258 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1259 break;
1261 case FT_WRT:
1262 if (p->id2 < 0)
1263 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1264 p->id2, -p->id1 * 16);
1265 else
1266 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1267 p->id2, p->id1);
1268 break;
1269 case FT_EXT:
1270 sprintf(buf, "X%"PRIX32"", p->id1);
1271 break;
1272 case FT_EXTREL:
1273 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1274 break;
1275 case FT_EXTSEG:
1276 /* We needed a non-ieee hack here.
1277 * We introduce the Y variable, which is the low
1278 * limit of the native segment the extern resides in
1280 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1281 break;
1282 case FT_EXTWRT:
1283 if (p->id2 < 0)
1284 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1285 -p->id1 * 16);
1286 else
1287 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1288 break;
1290 ieee_putascii("LR(%s,%"PRIX32").\n", buf, size);
1292 return (size);
1295 /* Dump all segment data (text and fixups )*/
1297 static void ieee_unqualified_name(char *dest, char *source)
1299 if (ieee_uppercase) {
1300 while (*source)
1301 *dest++ = toupper(*source++);
1302 *dest = 0;
1303 } else
1304 strcpy(dest, source);
1306 static void dbgls_init(void)
1308 int tempint;
1310 fnhead = NULL;
1311 fntail = &fnhead;
1312 arrindex = ARRAY_BOT;
1313 arrhead = NULL;
1314 arrtail = &arrhead;
1315 ieee_segment("??LINE", 2, &tempint);
1316 any_segs = false;
1318 static void dbgls_cleanup(void)
1320 struct ieeeSection *segtmp;
1321 while (fnhead) {
1322 struct FileName *fntemp = fnhead;
1323 fnhead = fnhead->next;
1324 nasm_free(fntemp->name);
1325 nasm_free(fntemp);
1327 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1328 while (segtmp->lochead) {
1329 struct ieeePublic *loctmp = segtmp->lochead;
1330 segtmp->lochead = loctmp->next;
1331 nasm_free(loctmp->name);
1332 nasm_free(loctmp);
1335 while (arrhead) {
1336 struct Array *arrtmp = arrhead;
1337 arrhead = arrhead->next;
1338 nasm_free(arrtmp);
1343 * because this routine is not bracketed in
1344 * the main program, this routine will be called even if there
1345 * is no request for debug info
1346 * so, we have to make sure the ??LINE segment is avaialbe
1347 * as the first segment when this debug format is selected
1349 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1351 struct FileName *fn;
1352 struct ieeeSection *seg;
1353 int i = 0;
1354 if (segto == NO_SEG)
1355 return;
1358 * If `any_segs' is still false, we must define a default
1359 * segment.
1361 if (!any_segs) {
1362 int tempint; /* ignored */
1363 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1364 nasm_panic(0, "strange segment conditions in OBJ driver");
1368 * Find the segment we are targetting.
1370 for (seg = seghead; seg; seg = seg->next)
1371 if (seg->index == segto)
1372 break;
1373 if (!seg)
1374 nasm_panic(0, "lineno directed to nonexistent segment?");
1376 for (fn = fnhead; fn; fn = fn->next) {
1377 if (!nasm_stricmp(lnfname, fn->name))
1378 break;
1379 i++;
1381 if (!fn) {
1382 fn = nasm_malloc(sizeof(*fn));
1383 fn->name = nasm_malloc(strlen(lnfname) + 1);
1384 fn->index = i;
1385 strcpy(fn->name, lnfname);
1386 fn->next = NULL;
1387 *fntail = fn;
1388 fntail = &fn->next;
1390 ieee_write_byte(seghead, fn->index);
1391 ieee_write_word(seghead, lineno);
1392 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1393 seg->currentpos);
1396 static void dbgls_deflabel(char *name, int32_t segment,
1397 int64_t offset, int is_global, char *special)
1399 struct ieeeSection *seg;
1401 /* Keep compiler from warning about special */
1402 (void)special;
1405 * Note: ..[^@] special symbols are filtered in labels.c
1409 * If it's a special-retry from pass two, discard it.
1411 if (is_global == 3)
1412 return;
1415 * Case (i):
1417 if (ieee_seg_needs_update)
1418 return;
1419 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
1420 return;
1422 if (segment >= SEG_ABS || segment == NO_SEG) {
1423 return;
1427 * If `any_segs' is still false, we might need to define a
1428 * default segment, if they're trying to declare a label in
1429 * `first_seg'. But the label should exist due to a prior
1430 * call to ieee_deflabel so we can skip that.
1433 for (seg = seghead; seg; seg = seg->next)
1434 if (seg->index == segment) {
1435 struct ieeePublic *loc;
1437 * Case (ii). Maybe MODPUB someday?
1439 if (!is_global) {
1440 last_defined = loc = nasm_malloc(sizeof(*loc));
1441 *seg->loctail = loc;
1442 seg->loctail = &loc->next;
1443 loc->next = NULL;
1444 loc->name = nasm_strdup(name);
1445 loc->offset = offset;
1446 loc->segment = -1;
1447 loc->index = seg->ieee_index;
1451 static void dbgls_typevalue(int32_t type)
1453 int elem = TYM_ELEMENTS(type);
1454 type = TYM_TYPE(type);
1456 if (!last_defined)
1457 return;
1459 switch (type) {
1460 case TY_BYTE:
1461 last_defined->type = 1; /* uint8_t */
1462 break;
1463 case TY_WORD:
1464 last_defined->type = 3; /* unsigned word */
1465 break;
1466 case TY_DWORD:
1467 last_defined->type = 5; /* unsigned dword */
1468 break;
1469 case TY_FLOAT:
1470 last_defined->type = 9; /* float */
1471 break;
1472 case TY_QWORD:
1473 last_defined->type = 10; /* qword */
1474 break;
1475 case TY_TBYTE:
1476 last_defined->type = 11; /* TBYTE */
1477 break;
1478 default:
1479 last_defined->type = 0x10; /* near label */
1480 break;
1483 if (elem > 1) {
1484 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
1485 int vtype = last_defined->type;
1486 arrtmp->size = elem;
1487 arrtmp->basetype = vtype;
1488 arrtmp->next = NULL;
1489 last_defined->type = arrindex++ + 0x100;
1490 *arrtail = arrtmp;
1491 arrtail = &(arrtmp->next);
1493 last_defined = NULL;
1495 static void dbgls_output(int output_type, void *param)
1497 (void)output_type;
1498 (void)param;
1500 static const struct dfmt ladsoft_debug_form = {
1501 "LADsoft Debug Records",
1502 "ladsoft",
1503 dbgls_init,
1504 dbgls_linnum,
1505 dbgls_deflabel,
1506 null_debug_directive,
1507 dbgls_typevalue,
1508 dbgls_output,
1509 dbgls_cleanup,
1510 NULL /* pragma list */
1512 static const struct dfmt * const ladsoft_debug_arr[3] = {
1513 &ladsoft_debug_form,
1514 &null_debug_form,
1515 NULL
1517 const struct ofmt of_ieee = {
1518 "IEEE-695 (LADsoft variant) object file format",
1519 "ieee",
1520 OFMT_TEXT,
1522 ladsoft_debug_arr,
1523 &ladsoft_debug_form,
1524 NULL,
1525 ieee_init,
1526 ieee_set_info,
1527 nasm_do_legacy_output,
1528 ieee_out,
1529 ieee_deflabel,
1530 ieee_segment,
1531 ieee_sectalign,
1532 ieee_segbase,
1533 ieee_directive,
1534 ieee_filename,
1535 ieee_cleanup,
1536 NULL /* pragma list */
1539 #endif /* OF_IEEE */