configure.in: add --enable-werror option
[nasm.git] / output / outieee.c
blob42f0fb6664b6b5b2ebf4b6f3df603a0711f16340
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outieee.c output routines for the Netwide Assembler to produce
36 * IEEE-std object files
39 /* notes: I have tried to make this correspond to the IEEE version
40 * of the standard, specifically the primary ASCII version. It should
41 * be trivial to create the binary version given this source (which is
42 * one of MANY things that have to be done to make this correspond to
43 * the hp-microtek version of the standard).
45 * 16-bit support is assumed to use 24-bit addresses
46 * The linker can sort out segmentation-specific stuff
47 * if it keeps track of externals
48 * in terms of being relative to section bases
50 * A non-standard variable type, the 'Yn' variable, has been introduced.
51 * Basically it is a reference to extern 'n'- denoting the low limit
52 * (L-variable) of the section that extern 'n' is defined in. Like the
53 * x variable, there may be no explicit assignment to it, it is derived
54 * from the public definition corresponding to the extern name. This
55 * is required because the one thing the mufom guys forgot to do well was
56 * take into account segmented architectures.
58 * I use comment classes for various things and these are undefined by
59 * the standard.
61 * Debug info should be considered totally non-standard (local labels are
62 * standard but linenum records are not covered by the standard.
63 * Type defs have the standard format but absolute meanings for ordinal
64 * types are not covered by the standard.)
66 * David Lindauer, LADsoft
68 #include "compiler.h"
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <time.h>
74 #include <stdarg.h> /* Note: we need the ANSI version of stdarg.h */
75 #include <ctype.h>
76 #include <inttypes.h>
78 #include "nasm.h"
79 #include "nasmlib.h"
80 #include "output/outform.h"
81 #include "output/outlib.h"
83 #ifdef OF_IEEE
85 #define ARRAY_BOT 0x1
87 static char ieee_infile[FILENAME_MAX];
88 static int ieee_uppercase;
90 static bool any_segs;
91 static int arrindex;
93 #define HUNKSIZE 1024 /* Size of the data hunk */
94 #define EXT_BLKSIZ 512
95 #define LDPERLINE 32 /* bytes per line in output */
97 struct ieeeSection;
99 struct LineNumber {
100 struct LineNumber *next;
101 struct ieeeSection *segment;
102 int32_t offset;
103 int32_t lineno;
106 static struct FileName {
107 struct FileName *next;
108 char *name;
109 int32_t index;
110 } *fnhead, **fntail;
112 static struct Array {
113 struct Array *next;
114 unsigned size;
115 int basetype;
116 } *arrhead, **arrtail;
118 static struct ieeePublic {
119 struct ieeePublic *next;
120 char *name;
121 int32_t offset;
122 int32_t segment; /* only if it's far-absolute */
123 int32_t index;
124 int type; /* for debug purposes */
125 } *fpubhead, **fpubtail, *last_defined;
127 static struct ieeeExternal {
128 struct ieeeExternal *next;
129 char *name;
130 int32_t commonsize;
131 } *exthead, **exttail;
133 static int externals;
135 static struct ExtBack {
136 struct ExtBack *next;
137 int index[EXT_BLKSIZ];
138 } *ebhead, **ebtail;
140 /* NOTE: the first segment MUST be the lineno segment */
141 static struct ieeeSection {
142 struct ieeeSection *next;
143 char *name;
144 struct ieeeObjData *data, *datacurr;
145 struct ieeeFixupp *fptr, *flptr;
146 int32_t index; /* the NASM segment id */
147 int32_t ieee_index; /* the OBJ-file segment index */
148 int32_t currentpos;
149 int32_t align; /* can be SEG_ABS + absolute addr */
150 int32_t startpos;
151 int32_t use32; /* is this segment 32-bit? */
152 struct ieeePublic *pubhead, **pubtail, *lochead, **loctail;
153 enum {
154 CMB_PRIVATE = 0,
155 CMB_PUBLIC = 2,
156 CMB_COMMON = 6
157 } combine;
158 } *seghead, **segtail, *ieee_seg_needs_update;
160 struct ieeeObjData {
161 struct ieeeObjData *next;
162 uint8_t data[HUNKSIZE];
165 struct ieeeFixupp {
166 struct ieeeFixupp *next;
167 enum {
168 FT_SEG = 0,
169 FT_REL = 1,
170 FT_OFS = 2,
171 FT_EXT = 3,
172 FT_WRT = 4,
173 FT_EXTREL = 5,
174 FT_EXTWRT = 6,
175 FT_EXTSEG = 7
176 } ftype;
177 int16_t size;
178 int32_t id1;
179 int32_t id2;
180 int32_t offset;
181 int32_t addend;
184 static int32_t ieee_entry_seg, ieee_entry_ofs;
185 static int checksum;
187 extern struct ofmt of_ieee;
189 static void ieee_data_new(struct ieeeSection *);
190 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection *,
191 int, uint64_t, int32_t);
192 static void ieee_install_fixup(struct ieeeSection *, struct ieeeFixupp *);
193 static int32_t ieee_segment(char *, int, int *);
194 static void ieee_write_file(int debuginfo);
195 static void ieee_write_byte(struct ieeeSection *, int);
196 static void ieee_write_word(struct ieeeSection *, int);
197 static void ieee_write_dword(struct ieeeSection *, int32_t);
198 static void ieee_putascii(char *, ...);
199 static void ieee_putcs(int);
200 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
201 static int32_t ieee_putlr(struct ieeeFixupp *);
202 static void ieee_unqualified_name(char *, char *);
205 * pup init
207 static void ieee_init(void)
209 any_segs = false;
210 fpubhead = NULL;
211 fpubtail = &fpubhead;
212 exthead = NULL;
213 exttail = &exthead;
214 externals = 1;
215 ebhead = NULL;
216 ebtail = &ebhead;
217 seghead = ieee_seg_needs_update = NULL;
218 segtail = &seghead;
219 ieee_entry_seg = NO_SEG;
220 ieee_uppercase = false;
221 checksum = 0;
224 static int ieee_set_info(enum geninfo type, char **val)
226 (void)type;
227 (void)val;
229 return 0;
233 * Rundown
235 static void ieee_cleanup(int debuginfo)
237 ieee_write_file(debuginfo);
238 of_ieee.current_dfmt->cleanup();
239 while (seghead) {
240 struct ieeeSection *segtmp = seghead;
241 seghead = seghead->next;
242 while (segtmp->pubhead) {
243 struct ieeePublic *pubtmp = segtmp->pubhead;
244 segtmp->pubhead = pubtmp->next;
245 nasm_free(pubtmp);
247 while (segtmp->fptr) {
248 struct ieeeFixupp *fixtmp = segtmp->fptr;
249 segtmp->fptr = fixtmp->next;
250 nasm_free(fixtmp);
252 while (segtmp->data) {
253 struct ieeeObjData *dattmp = segtmp->data;
254 segtmp->data = dattmp->next;
255 nasm_free(dattmp);
257 nasm_free(segtmp);
259 while (fpubhead) {
260 struct ieeePublic *pubtmp = fpubhead;
261 fpubhead = fpubhead->next;
262 nasm_free(pubtmp);
264 while (exthead) {
265 struct ieeeExternal *exttmp = exthead;
266 exthead = exthead->next;
267 nasm_free(exttmp);
269 while (ebhead) {
270 struct ExtBack *ebtmp = ebhead;
271 ebhead = ebhead->next;
272 nasm_free(ebtmp);
277 * callback for labels
279 static void ieee_deflabel(char *name, int32_t segment,
280 int64_t offset, int is_global, char *special)
283 * We have three cases:
285 * (i) `segment' is a segment-base. If so, set the name field
286 * for the segment structure it refers to, and then
287 * return.
289 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
290 * Save the label position for later output of a PUBDEF record.
293 * (iii) `segment' is not one of our segments. Save the label
294 * position for later output of an EXTDEF.
296 struct ieeeExternal *ext;
297 struct ExtBack *eb;
298 struct ieeeSection *seg;
299 int i;
301 if (special) {
302 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
305 * First check for the double-period, signifying something
306 * unusual.
308 if (name[0] == '.' && name[1] == '.') {
309 if (!strcmp(name, "..start")) {
310 ieee_entry_seg = segment;
311 ieee_entry_ofs = offset;
313 return;
317 * Case (i):
319 if (ieee_seg_needs_update) {
320 ieee_seg_needs_update->name = name;
321 return;
323 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
324 return;
327 * case (ii)
329 if (segment >= SEG_ABS) {
331 * SEG_ABS subcase of (ii).
333 if (is_global) {
334 struct ieeePublic *pub;
336 pub = *fpubtail = nasm_malloc(sizeof(*pub));
337 fpubtail = &pub->next;
338 pub->next = NULL;
339 pub->name = name;
340 pub->offset = offset;
341 pub->segment = segment & ~SEG_ABS;
343 return;
346 for (seg = seghead; seg && is_global; seg = seg->next)
347 if (seg->index == segment) {
348 struct ieeePublic *pub;
350 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
351 seg->pubtail = &pub->next;
352 pub->next = NULL;
353 pub->name = name;
354 pub->offset = offset;
355 pub->index = seg->ieee_index;
356 pub->segment = -1;
357 return;
361 * Case (iii).
363 if (is_global) {
364 ext = *exttail = nasm_malloc(sizeof(*ext));
365 ext->next = NULL;
366 exttail = &ext->next;
367 ext->name = name;
368 if (is_global == 2)
369 ext->commonsize = offset;
370 else
371 ext->commonsize = 0;
372 i = segment / 2;
373 eb = ebhead;
374 if (!eb) {
375 eb = *ebtail = nasm_zalloc(sizeof(*eb));
376 eb->next = NULL;
377 ebtail = &eb->next;
379 while (i > EXT_BLKSIZ) {
380 if (eb && eb->next)
381 eb = eb->next;
382 else {
383 eb = *ebtail = nasm_zalloc(sizeof(*eb));
384 eb->next = NULL;
385 ebtail = &eb->next;
387 i -= EXT_BLKSIZ;
389 eb->index[i] = externals++;
395 * Put data out
397 static void ieee_out(int32_t segto, const void *data,
398 enum out_type type, uint64_t size,
399 int32_t segment, int32_t wrt)
401 const uint8_t *ucdata;
402 int32_t ldata;
403 struct ieeeSection *seg;
406 * handle absolute-assembly (structure definitions)
408 if (segto == NO_SEG) {
409 if (type != OUT_RESERVE)
410 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
411 " space");
412 return;
416 * If `any_segs' is still false, we must define a default
417 * segment.
419 if (!any_segs) {
420 int tempint; /* ignored */
421 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
422 nasm_error(ERR_PANIC, "strange segment conditions in IEEE driver");
426 * Find the segment we are targetting.
428 for (seg = seghead; seg; seg = seg->next)
429 if (seg->index == segto)
430 break;
431 if (!seg)
432 nasm_error(ERR_PANIC, "code directed to nonexistent segment?");
434 if (type == OUT_RAWDATA) {
435 ucdata = data;
436 while (size--)
437 ieee_write_byte(seg, *ucdata++);
438 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
439 type == OUT_REL4ADR) {
440 if (type == OUT_ADDRESS)
441 size = abs((int)size);
442 else if (segment == NO_SEG)
443 nasm_error(ERR_NONFATAL, "relative call to absolute address not"
444 " supported by IEEE format");
445 ldata = *(int64_t *)data;
446 if (type == OUT_REL2ADR)
447 ldata += (size - 2);
448 if (type == OUT_REL4ADR)
449 ldata += (size - 4);
450 ieee_write_fixup(segment, wrt, seg, size, type, ldata);
451 if (size == 2)
452 ieee_write_word(seg, ldata);
453 else
454 ieee_write_dword(seg, ldata);
455 } else if (type == OUT_RESERVE) {
456 while (size--)
457 ieee_write_byte(seg, 0);
461 static void ieee_data_new(struct ieeeSection *segto)
464 if (!segto->data)
465 segto->data = segto->datacurr =
466 nasm_malloc(sizeof(*(segto->datacurr)));
467 else
468 segto->datacurr = segto->datacurr->next =
469 nasm_malloc(sizeof(*(segto->datacurr)));
470 segto->datacurr->next = NULL;
474 * this routine is unalduterated bloatware. I usually don't do this
475 * but I might as well see what it is like on a harmless program.
476 * If anyone wants to optimize this is a good canditate!
478 static void ieee_write_fixup(int32_t segment, int32_t wrt,
479 struct ieeeSection *segto, int size,
480 uint64_t realtype, int32_t offset)
482 struct ieeeSection *target;
483 struct ieeeFixupp s;
485 /* Don't put a fixup for things NASM can calculate */
486 if (wrt == NO_SEG && segment == NO_SEG)
487 return;
489 s.ftype = -1;
490 /* if it is a WRT offset */
491 if (wrt != NO_SEG) {
492 s.ftype = FT_WRT;
493 s.addend = offset;
494 if (wrt >= SEG_ABS)
495 s.id1 = -(wrt - SEG_ABS);
496 else {
497 if (wrt % 2 && realtype != OUT_REL2ADR
498 && realtype != OUT_REL4ADR) {
499 wrt--;
501 for (target = seghead; target; target = target->next)
502 if (target->index == wrt)
503 break;
504 if (target) {
505 s.id1 = target->ieee_index;
506 for (target = seghead; target; target = target->next)
507 if (target->index == segment)
508 break;
510 if (target)
511 s.id2 = target->ieee_index;
512 else {
514 * Now we assume the segment field is being used
515 * to hold an extern index
517 int32_t i = segment / 2;
518 struct ExtBack *eb = ebhead;
519 while (i > EXT_BLKSIZ) {
520 if (eb)
521 eb = eb->next;
522 else
523 break;
524 i -= EXT_BLKSIZ;
526 /* if we have an extern decide the type and make a record
528 if (eb) {
529 s.ftype = FT_EXTWRT;
530 s.addend = 0;
531 s.id2 = eb->index[i];
532 } else
533 nasm_error(ERR_NONFATAL,
534 "Source of WRT must be an offset");
537 } else
538 nasm_error(ERR_PANIC,
539 "unrecognised WRT value in ieee_write_fixup");
540 } else
541 nasm_error(ERR_NONFATAL, "target of WRT must be a section ");
543 s.size = size;
544 ieee_install_fixup(segto, &s);
545 return;
547 /* Pure segment fixup ? */
548 if (segment != NO_SEG) {
549 s.ftype = FT_SEG;
550 s.id1 = 0;
551 if (segment >= SEG_ABS) {
552 /* absolute far segment fixup */
553 s.id1 = -(segment - ~SEG_ABS);
554 } else if (segment % 2) {
555 /* fixup to named segment */
556 /* look it up */
557 for (target = seghead; target; target = target->next)
558 if (target->index == segment - 1)
559 break;
560 if (target)
561 s.id1 = target->ieee_index;
562 else {
564 * Now we assume the segment field is being used
565 * to hold an extern index
567 int32_t i = segment / 2;
568 struct ExtBack *eb = ebhead;
569 while (i > EXT_BLKSIZ) {
570 if (eb)
571 eb = eb->next;
572 else
573 break;
574 i -= EXT_BLKSIZ;
576 /* if we have an extern decide the type and make a record
578 if (eb) {
579 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
580 nasm_error(ERR_PANIC,
581 "Segment of a rel not supported in ieee_write_fixup");
582 } else {
583 /* If we want the segment */
584 s.ftype = FT_EXTSEG;
585 s.addend = 0;
586 s.id1 = eb->index[i];
589 } else
590 /* If we get here the seg value doesn't make sense */
591 nasm_error(ERR_PANIC,
592 "unrecognised segment value in ieee_write_fixup");
595 } else {
596 /* Assume we are offsetting directly from a section
597 * So look up the target segment
599 for (target = seghead; target; target = target->next)
600 if (target->index == segment)
601 break;
602 if (target) {
603 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
604 /* PC rel to a known offset */
605 s.id1 = target->ieee_index;
606 s.ftype = FT_REL;
607 s.size = size;
608 s.addend = offset;
609 } else {
610 /* We were offsetting from a seg */
611 s.id1 = target->ieee_index;
612 s.ftype = FT_OFS;
613 s.size = size;
614 s.addend = offset;
616 } else {
618 * Now we assume the segment field is being used
619 * to hold an extern index
621 int32_t i = segment / 2;
622 struct ExtBack *eb = ebhead;
623 while (i > EXT_BLKSIZ) {
624 if (eb)
625 eb = eb->next;
626 else
627 break;
628 i -= EXT_BLKSIZ;
630 /* if we have an extern decide the type and make a record
632 if (eb) {
633 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
634 s.ftype = FT_EXTREL;
635 s.addend = 0;
636 s.id1 = eb->index[i];
637 } else {
638 /* else we want the external offset */
639 s.ftype = FT_EXT;
640 s.addend = 0;
641 s.id1 = eb->index[i];
644 } else
645 /* If we get here the seg value doesn't make sense */
646 nasm_error(ERR_PANIC,
647 "unrecognised segment value in ieee_write_fixup");
650 if (size != 2 && s.ftype == FT_SEG)
651 nasm_error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
652 " segment base references");
653 s.size = size;
654 ieee_install_fixup(segto, &s);
655 return;
657 /* should never get here */
659 static void ieee_install_fixup(struct ieeeSection *seg,
660 struct ieeeFixupp *fix)
662 struct ieeeFixupp *f;
663 f = nasm_malloc(sizeof(struct ieeeFixupp));
664 memcpy(f, fix, sizeof(struct ieeeFixupp));
665 f->offset = seg->currentpos;
666 seg->currentpos += fix->size;
667 f->next = NULL;
668 if (seg->fptr)
669 seg->flptr = seg->flptr->next = f;
670 else
671 seg->fptr = seg->flptr = f;
676 * segment registry
678 static int32_t ieee_segment(char *name, int pass, int *bits)
681 * We call the label manager here to define a name for the new
682 * segment, and when our _own_ label-definition stub gets
683 * called in return, it should register the new segment name
684 * using the pointer it gets passed. That way we save memory,
685 * by sponging off the label manager.
687 if (!name) {
688 *bits = 16;
689 if (!any_segs)
690 return 0;
691 return seghead->index;
692 } else {
693 struct ieeeSection *seg;
694 int ieee_idx, attrs;
695 bool rn_error;
696 char *p;
699 * Look for segment attributes.
701 attrs = 0;
702 while (*name == '.')
703 name++; /* hack, but a documented one */
704 p = name;
705 while (*p && !nasm_isspace(*p))
706 p++;
707 if (*p) {
708 *p++ = '\0';
709 while (*p && nasm_isspace(*p))
710 *p++ = '\0';
712 while (*p) {
713 while (*p && !nasm_isspace(*p))
714 p++;
715 if (*p) {
716 *p++ = '\0';
717 while (*p && nasm_isspace(*p))
718 *p++ = '\0';
721 attrs++;
724 ieee_idx = 1;
725 for (seg = seghead; seg; seg = seg->next) {
726 ieee_idx++;
727 if (!strcmp(seg->name, name)) {
728 if (attrs > 0 && pass == 1)
729 nasm_error(ERR_WARNING, "segment attributes specified on"
730 " redeclaration of segment: ignoring");
731 if (seg->use32)
732 *bits = 32;
733 else
734 *bits = 16;
735 return seg->index;
739 *segtail = seg = nasm_malloc(sizeof(*seg));
740 seg->next = NULL;
741 segtail = &seg->next;
742 seg->index = seg_alloc();
743 seg->ieee_index = ieee_idx;
744 any_segs = true;
745 seg->name = NULL;
746 seg->currentpos = 0;
747 seg->align = 1; /* default */
748 seg->use32 = *bits == 32; /* default to user spec */
749 seg->combine = CMB_PUBLIC; /* default */
750 seg->pubhead = NULL;
751 seg->pubtail = &seg->pubhead;
752 seg->data = NULL;
753 seg->fptr = NULL;
754 seg->lochead = NULL;
755 seg->loctail = &seg->lochead;
758 * Process the segment attributes.
760 p = name;
761 while (attrs--) {
762 p += strlen(p);
763 while (!*p)
764 p++;
767 * `p' contains a segment attribute.
769 if (!nasm_stricmp(p, "private"))
770 seg->combine = CMB_PRIVATE;
771 else if (!nasm_stricmp(p, "public"))
772 seg->combine = CMB_PUBLIC;
773 else if (!nasm_stricmp(p, "common"))
774 seg->combine = CMB_COMMON;
775 else if (!nasm_stricmp(p, "use16"))
776 seg->use32 = false;
777 else if (!nasm_stricmp(p, "use32"))
778 seg->use32 = true;
779 else if (!nasm_strnicmp(p, "align=", 6)) {
780 seg->align = readnum(p + 6, &rn_error);
781 if (seg->align == 0)
782 seg->align = 1;
783 if (rn_error) {
784 seg->align = 1;
785 nasm_error(ERR_NONFATAL, "segment alignment should be"
786 " numeric");
788 switch ((int)seg->align) {
789 case 1: /* BYTE */
790 case 2: /* WORD */
791 case 4: /* DWORD */
792 case 16: /* PARA */
793 case 256: /* PAGE */
794 case 8:
795 case 32:
796 case 64:
797 case 128:
798 break;
799 default:
800 nasm_error(ERR_NONFATAL, "invalid alignment value %d",
801 seg->align);
802 seg->align = 1;
803 break;
805 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
806 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
807 if (rn_error)
808 nasm_error(ERR_NONFATAL, "argument to `absolute' segment"
809 " attribute should be numeric");
813 ieee_seg_needs_update = seg;
814 if (seg->align >= SEG_ABS)
815 define_label(name, NO_SEG, seg->align - SEG_ABS,
816 NULL, false, false);
817 else
818 define_label(name, seg->index + 1, 0L, NULL, false, false);
819 ieee_seg_needs_update = NULL;
821 if (seg->use32)
822 *bits = 32;
823 else
824 *bits = 16;
825 return seg->index;
830 * directives supported
832 static int ieee_directive(enum directives directive, char *value, int pass)
835 (void)value;
836 (void)pass;
838 switch (directive) {
839 case D_UPPERCASE:
840 ieee_uppercase = true;
841 return 1;
843 default:
844 return 0;
848 static void ieee_sectalign(int32_t seg, unsigned int value)
850 struct ieeeSection *s;
852 list_for_each(s, seghead) {
853 if (s->index == seg)
854 break;
858 * 256 is maximum there, note it may happen
859 * that align is issued on "absolute" segment
860 * it's fine since SEG_ABS > 256 and we never
861 * get escape this test
863 if (!s || !is_power2(value) || value > 256)
864 return;
866 if ((unsigned int)s->align < value)
867 s->align = value;
871 * Return segment data
873 static int32_t ieee_segbase(int32_t segment)
875 struct ieeeSection *seg;
878 * Find the segment in our list.
880 for (seg = seghead; seg; seg = seg->next)
881 if (seg->index == segment - 1)
882 break;
884 if (!seg)
885 return segment; /* not one of ours - leave it alone */
887 if (seg->align >= SEG_ABS)
888 return seg->align; /* absolute segment */
890 return segment; /* no special treatment */
894 * filename
896 static void ieee_filename(char *inname, char *outname)
898 strcpy(ieee_infile, inname);
899 standard_extension(inname, outname, ".o");
902 static void ieee_write_file(int debuginfo)
904 struct tm *thetime;
905 time_t reltime;
906 struct FileName *fn;
907 struct ieeeSection *seg;
908 struct ieeePublic *pub, *loc;
909 struct ieeeExternal *ext;
910 struct ieeeObjData *data;
911 struct ieeeFixupp *fix;
912 struct Array *arr;
913 int i;
916 * Write the module header
918 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile), ieee_infile);
921 * Write the NASM boast comment.
923 ieee_putascii("CO0,%02X%s.\n", strlen(nasm_comment), nasm_comment);
926 * write processor-specific information
928 ieee_putascii("AD8,4,L.\n");
931 * date and time
933 time(&reltime);
934 thetime = localtime(&reltime);
935 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
936 1900 + thetime->tm_year, thetime->tm_mon + 1,
937 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
938 thetime->tm_sec);
940 * if debugging, dump file names
942 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
943 ieee_putascii("C0105,%02X%s.\n", strlen(fn->name), fn->name);
946 ieee_putascii("CO101,07ENDHEAD.\n");
948 * the standard doesn't specify when to put checksums,
949 * we'll just do it periodically.
951 ieee_putcs(false);
954 * Write the section headers
956 seg = seghead;
957 if (!debuginfo && !strcmp(seg->name, "??LINE"))
958 seg = seg->next;
959 while (seg) {
960 char buf[256];
961 char attrib;
962 switch (seg->combine) {
963 case CMB_PUBLIC:
964 default:
965 attrib = 'C';
966 break;
967 case CMB_PRIVATE:
968 attrib = 'S';
969 break;
970 case CMB_COMMON:
971 attrib = 'M';
972 break;
974 ieee_unqualified_name(buf, seg->name);
975 if (seg->align >= SEG_ABS) {
976 ieee_putascii("ST%X,A,%02X%s.\n", seg->ieee_index,
977 strlen(buf), buf);
978 ieee_putascii("ASL%X,%lX.\n", seg->ieee_index,
979 (seg->align - SEG_ABS) * 16);
980 } else {
981 ieee_putascii("ST%X,%c,%02X%s.\n", seg->ieee_index, attrib,
982 strlen(buf), buf);
983 ieee_putascii("SA%X,%lX.\n", seg->ieee_index, seg->align);
984 ieee_putascii("ASS%X,%X.\n", seg->ieee_index,
985 seg->currentpos);
987 seg = seg->next;
990 * write the start address if there is one
992 if (ieee_entry_seg) {
993 for (seg = seghead; seg; seg = seg->next)
994 if (seg->index == ieee_entry_seg)
995 break;
996 if (!seg)
997 nasm_error(ERR_PANIC, "Start address records are incorrect");
998 else
999 ieee_putascii("ASG,R%X,%lX,+.\n", seg->ieee_index,
1000 ieee_entry_ofs);
1003 ieee_putcs(false);
1005 * Write the publics
1007 i = 1;
1008 for (seg = seghead; seg; seg = seg->next) {
1009 for (pub = seg->pubhead; pub; pub = pub->next) {
1010 char buf[256];
1011 ieee_unqualified_name(buf, pub->name);
1012 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1013 if (pub->segment == -1)
1014 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1015 pub->offset);
1016 else
1017 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1018 pub->offset);
1019 if (debuginfo) {
1020 if (pub->type >= 0x100)
1021 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1022 else
1023 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1025 i++;
1028 pub = fpubhead;
1029 i = 1;
1030 while (pub) {
1031 char buf[256];
1032 ieee_unqualified_name(buf, pub->name);
1033 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1034 if (pub->segment == -1)
1035 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1036 pub->offset);
1037 else
1038 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1039 pub->offset);
1040 if (debuginfo) {
1041 if (pub->type >= 0x100)
1042 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1043 else
1044 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1046 i++;
1047 pub = pub->next;
1050 * Write the externals
1052 ext = exthead;
1053 i = 1;
1054 while (ext) {
1055 char buf[256];
1056 ieee_unqualified_name(buf, ext->name);
1057 ieee_putascii("NX%X,%02X%s.\n", i++, strlen(buf), buf);
1058 ext = ext->next;
1060 ieee_putcs(false);
1063 * IEEE doesn't have a standard pass break record
1064 * so use the ladsoft variant
1066 ieee_putascii("CO100,06ENDSYM.\n");
1069 * now put types
1071 i = ARRAY_BOT;
1072 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1073 ieee_putascii("TY%X,20,%X,%lX.\n", i++, arr->basetype,
1074 arr->size);
1077 * now put locals
1079 i = 1;
1080 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1081 for (loc = seg->lochead; loc; loc = loc->next) {
1082 char buf[256];
1083 ieee_unqualified_name(buf, loc->name);
1084 ieee_putascii("NN%X,%02X%s.\n", i, strlen(buf), buf);
1085 if (loc->segment == -1)
1086 ieee_putascii("ASN%X,R%X,%lX,+.\n", i, loc->index,
1087 loc->offset);
1088 else
1089 ieee_putascii("ASN%X,%lX,%lX,+.\n", i, loc->segment * 16,
1090 loc->offset);
1091 if (debuginfo) {
1092 if (loc->type >= 0x100)
1093 ieee_putascii("ATN%X,T%X.\n", i, loc->type - 0x100);
1094 else
1095 ieee_putascii("ATN%X,%X.\n", i, loc->type);
1097 i++;
1102 * put out section data;
1104 seg = seghead;
1105 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1106 seg = seg->next;
1107 while (seg) {
1108 if (seg->currentpos) {
1109 int32_t size, org = 0;
1110 data = seg->data;
1111 ieee_putascii("SB%X.\n", seg->ieee_index);
1112 fix = seg->fptr;
1113 while (fix) {
1114 size = HUNKSIZE - (org % HUNKSIZE);
1115 size =
1116 size + org >
1117 seg->currentpos ? seg->currentpos - org : size;
1118 size = fix->offset - org > size ? size : fix->offset - org;
1119 org = ieee_putld(org, org + size, data->data);
1120 if (org % HUNKSIZE == 0)
1121 data = data->next;
1122 if (org == fix->offset) {
1123 org += ieee_putlr(fix);
1124 fix = fix->next;
1127 while (org < seg->currentpos && data) {
1128 size =
1129 seg->currentpos - org >
1130 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1131 org = ieee_putld(org, org + size, data->data);
1132 data = data->next;
1134 ieee_putcs(false);
1137 seg = seg->next;
1140 * module end record
1142 ieee_putascii("ME.\n");
1145 static void ieee_write_byte(struct ieeeSection *seg, int data)
1147 int temp;
1148 if (!(temp = seg->currentpos++ % HUNKSIZE))
1149 ieee_data_new(seg);
1150 seg->datacurr->data[temp] = data;
1153 static void ieee_write_word(struct ieeeSection *seg, int data)
1155 ieee_write_byte(seg, data & 0xFF);
1156 ieee_write_byte(seg, (data >> 8) & 0xFF);
1159 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1161 ieee_write_byte(seg, data & 0xFF);
1162 ieee_write_byte(seg, (data >> 8) & 0xFF);
1163 ieee_write_byte(seg, (data >> 16) & 0xFF);
1164 ieee_write_byte(seg, (data >> 24) & 0xFF);
1166 static void ieee_putascii(char *format, ...)
1168 char buffer[256];
1169 int i, l;
1170 va_list ap;
1172 va_start(ap, format);
1173 vsnprintf(buffer, sizeof(buffer), format, ap);
1174 l = strlen(buffer);
1175 for (i = 0; i < l; i++)
1176 if ((uint8_t)buffer[i] > 31)
1177 checksum += buffer[i];
1178 va_end(ap);
1179 fputs(buffer, ofile);
1183 * put out a checksum record */
1184 static void ieee_putcs(int toclear)
1186 if (toclear) {
1187 ieee_putascii("CS.\n");
1188 } else {
1189 checksum += 'C';
1190 checksum += 'S';
1191 ieee_putascii("CS%02X.\n", checksum & 127);
1193 checksum = 0;
1196 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1198 int32_t val;
1199 if (start == end)
1200 return (start);
1201 val = start % HUNKSIZE;
1202 /* fill up multiple lines */
1203 while (end - start >= LDPERLINE) {
1204 int i;
1205 ieee_putascii("LD");
1206 for (i = 0; i < LDPERLINE; i++) {
1207 ieee_putascii("%02X", buf[val++]);
1208 start++;
1210 ieee_putascii(".\n");
1212 /* if no partial lines */
1213 if (start == end)
1214 return (start);
1215 /* make a partial line */
1216 ieee_putascii("LD");
1217 while (start < end) {
1218 ieee_putascii("%02X", buf[val++]);
1219 start++;
1221 ieee_putascii(".\n");
1222 return (start);
1224 static int32_t ieee_putlr(struct ieeeFixupp *p)
1227 * To deal with the vagaries of segmentation the LADsoft linker
1228 * defines two types of segments: absolute and virtual. Note that
1229 * 'absolute' in this context is a different thing from the IEEE
1230 * definition of an absolute segment type, which is also supported. If a
1231 * sement is linked in virtual mode the low limit (L-var) is
1232 * subtracted from each R,X, and P variable which appears in an
1233 * expression, so that we can have relative offsets. Meanwhile
1234 * in the ABSOLUTE mode this subtraction is not done and
1235 * so we can use absolute offsets from 0. In the LADsoft linker
1236 * this configuration is not done in the assemblker source but in
1237 * a source the linker reads. Generally this type of thing only
1238 * becomes an issue if real mode code is used. A pure 32-bit linker could
1239 * get away without defining the virtual mode...
1241 char buf[40];
1242 int32_t size = p->size;
1243 switch (p->ftype) {
1244 case FT_SEG:
1245 if (p->id1 < 0)
1246 sprintf(buf, "%"PRIX32"", -p->id1);
1247 else
1248 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1249 break;
1250 case FT_OFS:
1251 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1252 break;
1253 case FT_REL:
1254 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1255 break;
1257 case FT_WRT:
1258 if (p->id2 < 0)
1259 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1260 p->id2, -p->id1 * 16);
1261 else
1262 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1263 p->id2, p->id1);
1264 break;
1265 case FT_EXT:
1266 sprintf(buf, "X%"PRIX32"", p->id1);
1267 break;
1268 case FT_EXTREL:
1269 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1270 break;
1271 case FT_EXTSEG:
1272 /* We needed a non-ieee hack here.
1273 * We introduce the Y variable, which is the low
1274 * limit of the native segment the extern resides in
1276 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1277 break;
1278 case FT_EXTWRT:
1279 if (p->id2 < 0)
1280 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1281 -p->id1 * 16);
1282 else
1283 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1284 break;
1286 ieee_putascii("LR(%s,%"PRIX32").\n", buf, size);
1288 return (size);
1291 /* Dump all segment data (text and fixups )*/
1293 static void ieee_unqualified_name(char *dest, char *source)
1295 if (ieee_uppercase) {
1296 while (*source)
1297 *dest++ = toupper(*source++);
1298 *dest = 0;
1299 } else
1300 strcpy(dest, source);
1302 static void dbgls_init(void)
1304 int tempint;
1306 fnhead = NULL;
1307 fntail = &fnhead;
1308 arrindex = ARRAY_BOT;
1309 arrhead = NULL;
1310 arrtail = &arrhead;
1311 ieee_segment("??LINE", 2, &tempint);
1312 any_segs = false;
1314 static void dbgls_cleanup(void)
1316 struct ieeeSection *segtmp;
1317 while (fnhead) {
1318 struct FileName *fntemp = fnhead;
1319 fnhead = fnhead->next;
1320 nasm_free(fntemp->name);
1321 nasm_free(fntemp);
1323 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1324 while (segtmp->lochead) {
1325 struct ieeePublic *loctmp = segtmp->lochead;
1326 segtmp->lochead = loctmp->next;
1327 nasm_free(loctmp->name);
1328 nasm_free(loctmp);
1331 while (arrhead) {
1332 struct Array *arrtmp = arrhead;
1333 arrhead = arrhead->next;
1334 nasm_free(arrtmp);
1339 * because this routine is not bracketed in
1340 * the main program, this routine will be called even if there
1341 * is no request for debug info
1342 * so, we have to make sure the ??LINE segment is avaialbe
1343 * as the first segment when this debug format is selected
1345 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1347 struct FileName *fn;
1348 struct ieeeSection *seg;
1349 int i = 0;
1350 if (segto == NO_SEG)
1351 return;
1354 * If `any_segs' is still false, we must define a default
1355 * segment.
1357 if (!any_segs) {
1358 int tempint; /* ignored */
1359 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1360 nasm_error(ERR_PANIC, "strange segment conditions in OBJ driver");
1364 * Find the segment we are targetting.
1366 for (seg = seghead; seg; seg = seg->next)
1367 if (seg->index == segto)
1368 break;
1369 if (!seg)
1370 nasm_error(ERR_PANIC, "lineno directed to nonexistent segment?");
1372 for (fn = fnhead; fn; fn = fn->next) {
1373 if (!nasm_stricmp(lnfname, fn->name))
1374 break;
1375 i++;
1377 if (!fn) {
1378 fn = nasm_malloc(sizeof(*fn));
1379 fn->name = nasm_malloc(strlen(lnfname) + 1);
1380 fn->index = i;
1381 strcpy(fn->name, lnfname);
1382 fn->next = NULL;
1383 *fntail = fn;
1384 fntail = &fn->next;
1386 ieee_write_byte(seghead, fn->index);
1387 ieee_write_word(seghead, lineno);
1388 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1389 seg->currentpos);
1392 static void dbgls_deflabel(char *name, int32_t segment,
1393 int64_t offset, int is_global, char *special)
1395 struct ieeeSection *seg;
1397 /* Keep compiler from warning about special */
1398 (void)special;
1401 * If it's a special-retry from pass two, discard it.
1403 if (is_global == 3)
1404 return;
1407 * First check for the double-period, signifying something
1408 * unusual.
1410 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
1411 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 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,
1511 static struct dfmt *ladsoft_debug_arr[3] = {
1512 &ladsoft_debug_form,
1513 &null_debug_form,
1514 NULL
1516 struct ofmt of_ieee = {
1517 "IEEE-695 (LADsoft variant) object file format",
1518 "ieee",
1519 OFMT_TEXT,
1521 ladsoft_debug_arr,
1522 &ladsoft_debug_form,
1523 NULL,
1524 ieee_init,
1525 ieee_set_info,
1526 ieee_out,
1527 ieee_deflabel,
1528 ieee_segment,
1529 ieee_sectalign,
1530 ieee_segbase,
1531 ieee_directive,
1532 ieee_filename,
1533 ieee_cleanup
1536 #endif /* OF_IEEE */