outobj: handle the case of SEG <undefined> in pass 0
[nasm/avx512.git] / output / outieee.c
blob56dc71e7d9b3fa2c408d7dfe0eb733718c1cebe7
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 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 efunc error;
91 static ldfunc deflabel;
92 static FILE *ofp;
93 static bool any_segs;
94 static int arrindex;
96 #define HUNKSIZE 1024 /* Size of the data hunk */
97 #define EXT_BLKSIZ 512
98 #define LDPERLINE 32 /* bytes per line in output */
100 struct ieeeSection;
102 struct LineNumber {
103 struct LineNumber *next;
104 struct ieeeSection *segment;
105 int32_t offset;
106 int32_t lineno;
109 static struct FileName {
110 struct FileName *next;
111 char *name;
112 int32_t index;
113 } *fnhead, **fntail;
115 static struct Array {
116 struct Array *next;
117 unsigned size;
118 int basetype;
119 } *arrhead, **arrtail;
121 static struct ieeePublic {
122 struct ieeePublic *next;
123 char *name;
124 int32_t offset;
125 int32_t segment; /* only if it's far-absolute */
126 int32_t index;
127 int type; /* for debug purposes */
128 } *fpubhead, **fpubtail, *last_defined;
130 static struct ieeeExternal {
131 struct ieeeExternal *next;
132 char *name;
133 int32_t commonsize;
134 } *exthead, **exttail;
136 static int externals;
138 static struct ExtBack {
139 struct ExtBack *next;
140 int index[EXT_BLKSIZ];
141 } *ebhead, **ebtail;
143 /* NOTE: the first segment MUST be the lineno segment */
144 static struct ieeeSection {
145 struct ieeeObjData *data, *datacurr;
146 struct ieeeSection *next;
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 enum {
154 CMB_PRIVATE = 0,
155 CMB_PUBLIC = 2,
156 CMB_COMMON = 6
157 } combine;
158 int32_t use32; /* is this segment 32-bit? */
159 struct ieeePublic *pubhead, **pubtail, *lochead, **loctail;
160 char *name;
161 } *seghead, **segtail, *ieee_seg_needs_update;
163 struct ieeeObjData {
164 struct ieeeObjData *next;
165 uint8_t data[HUNKSIZE];
168 struct ieeeFixupp {
169 struct ieeeFixupp *next;
170 enum {
171 FT_SEG = 0,
172 FT_REL = 1,
173 FT_OFS = 2,
174 FT_EXT = 3,
175 FT_WRT = 4,
176 FT_EXTREL = 5,
177 FT_EXTWRT = 6,
178 FT_EXTSEG = 7
179 } ftype;
180 int16_t size;
181 int32_t id1;
182 int32_t id2;
183 int32_t offset;
184 int32_t addend;
187 static int32_t ieee_entry_seg, ieee_entry_ofs;
188 static int checksum;
190 extern struct ofmt of_ieee;
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(int debuginfo);
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(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
212 (void)eval;
213 ofp = fp;
214 error = errfunc;
215 deflabel = ldef;
216 any_segs = false;
217 fpubhead = NULL;
218 fpubtail = &fpubhead;
219 exthead = NULL;
220 exttail = &exthead;
221 externals = 1;
222 ebhead = NULL;
223 ebtail = &ebhead;
224 seghead = ieee_seg_needs_update = NULL;
225 segtail = &seghead;
226 ieee_entry_seg = NO_SEG;
227 ieee_uppercase = false;
228 checksum = 0;
231 static int ieee_set_info(enum geninfo type, char **val)
233 (void)type;
234 (void)val;
236 return 0;
240 * Rundown
242 static void ieee_cleanup(int debuginfo)
244 ieee_write_file(debuginfo);
245 of_ieee.current_dfmt->cleanup();
246 fclose(ofp);
247 while (seghead) {
248 struct ieeeSection *segtmp = seghead;
249 seghead = seghead->next;
250 while (segtmp->pubhead) {
251 struct ieeePublic *pubtmp = segtmp->pubhead;
252 segtmp->pubhead = pubtmp->next;
253 nasm_free(pubtmp);
255 while (segtmp->fptr) {
256 struct ieeeFixupp *fixtmp = segtmp->fptr;
257 segtmp->fptr = fixtmp->next;
258 nasm_free(fixtmp);
260 while (segtmp->data) {
261 struct ieeeObjData *dattmp = segtmp->data;
262 segtmp->data = dattmp->next;
263 nasm_free(dattmp);
265 nasm_free(segtmp);
267 while (fpubhead) {
268 struct ieeePublic *pubtmp = fpubhead;
269 fpubhead = fpubhead->next;
270 nasm_free(pubtmp);
272 while (exthead) {
273 struct ieeeExternal *exttmp = exthead;
274 exthead = exthead->next;
275 nasm_free(exttmp);
277 while (ebhead) {
278 struct ExtBack *ebtmp = ebhead;
279 ebhead = ebhead->next;
280 nasm_free(ebtmp);
285 * callback for labels
287 static void ieee_deflabel(char *name, int32_t segment,
288 int64_t offset, int is_global, char *special)
291 * We have three cases:
293 * (i) `segment' is a segment-base. If so, set the name field
294 * for the segment structure it refers to, and then
295 * return.
297 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
298 * Save the label position for later output of a PUBDEF record.
301 * (iii) `segment' is not one of our segments. Save the label
302 * position for later output of an EXTDEF.
304 struct ieeeExternal *ext;
305 struct ExtBack *eb;
306 struct ieeeSection *seg;
307 int i;
309 if (special) {
310 error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
313 * First check for the double-period, signifying something
314 * unusual.
316 if (name[0] == '.' && name[1] == '.') {
317 if (!strcmp(name, "..start")) {
318 ieee_entry_seg = segment;
319 ieee_entry_ofs = offset;
321 return;
325 * Case (i):
327 if (ieee_seg_needs_update) {
328 ieee_seg_needs_update->name = name;
329 return;
331 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
332 return;
335 * case (ii)
337 if (segment >= SEG_ABS) {
339 * SEG_ABS subcase of (ii).
341 if (is_global) {
342 struct ieeePublic *pub;
344 pub = *fpubtail = nasm_malloc(sizeof(*pub));
345 fpubtail = &pub->next;
346 pub->next = NULL;
347 pub->name = name;
348 pub->offset = offset;
349 pub->segment = segment & ~SEG_ABS;
351 return;
354 for (seg = seghead; seg && is_global; seg = seg->next)
355 if (seg->index == segment) {
356 struct ieeePublic *pub;
358 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
359 seg->pubtail = &pub->next;
360 pub->next = NULL;
361 pub->name = name;
362 pub->offset = offset;
363 pub->index = seg->ieee_index;
364 pub->segment = -1;
365 return;
369 * Case (iii).
371 if (is_global) {
372 ext = *exttail = nasm_malloc(sizeof(*ext));
373 ext->next = NULL;
374 exttail = &ext->next;
375 ext->name = name;
376 if (is_global == 2)
377 ext->commonsize = offset;
378 else
379 ext->commonsize = 0;
380 i = segment / 2;
381 eb = ebhead;
382 if (!eb) {
383 eb = *ebtail = nasm_malloc(sizeof(*eb));
384 eb->next = NULL;
385 ebtail = &eb->next;
387 while (i > EXT_BLKSIZ) {
388 if (eb && eb->next)
389 eb = eb->next;
390 else {
391 eb = *ebtail = nasm_malloc(sizeof(*eb));
392 eb->next = NULL;
393 ebtail = &eb->next;
395 i -= EXT_BLKSIZ;
397 eb->index[i] = externals++;
403 * Put data out
405 static void ieee_out(int32_t segto, const void *data,
406 enum out_type type, uint64_t size,
407 int32_t segment, int32_t wrt)
409 const uint8_t *ucdata;
410 int32_t ldata;
411 struct ieeeSection *seg;
414 * handle absolute-assembly (structure definitions)
416 if (segto == NO_SEG) {
417 if (type != OUT_RESERVE)
418 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
419 " space");
420 return;
424 * If `any_segs' is still false, we must define a default
425 * segment.
427 if (!any_segs) {
428 int tempint; /* ignored */
429 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
430 error(ERR_PANIC, "strange segment conditions in IEEE driver");
434 * Find the segment we are targetting.
436 for (seg = seghead; seg; seg = seg->next)
437 if (seg->index == segto)
438 break;
439 if (!seg)
440 error(ERR_PANIC, "code directed to nonexistent segment?");
442 if (type == OUT_RAWDATA) {
443 ucdata = data;
444 while (size--)
445 ieee_write_byte(seg, *ucdata++);
446 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
447 type == OUT_REL4ADR) {
448 if (segment == NO_SEG && type != OUT_ADDRESS)
449 error(ERR_NONFATAL, "relative call to absolute address not"
450 " supported by IEEE format");
451 ldata = *(int64_t *)data;
452 if (type == OUT_REL2ADR)
453 ldata += (size - 2);
454 if (type == OUT_REL4ADR)
455 ldata += (size - 4);
456 ieee_write_fixup(segment, wrt, seg, size, type, ldata);
457 if (size == 2)
458 ieee_write_word(seg, ldata);
459 else
460 ieee_write_dword(seg, ldata);
461 } else if (type == OUT_RESERVE) {
462 while (size--)
463 ieee_write_byte(seg, 0);
467 static void ieee_data_new(struct ieeeSection *segto)
470 if (!segto->data)
471 segto->data = segto->datacurr =
472 nasm_malloc(sizeof(*(segto->datacurr)));
473 else
474 segto->datacurr = segto->datacurr->next =
475 nasm_malloc(sizeof(*(segto->datacurr)));
476 segto->datacurr->next = NULL;
480 * this routine is unalduterated bloatware. I usually don't do this
481 * but I might as well see what it is like on a harmless program.
482 * If anyone wants to optimize this is a good canditate!
484 static void ieee_write_fixup(int32_t segment, int32_t wrt,
485 struct ieeeSection *segto, int size,
486 uint64_t realtype, int32_t offset)
488 struct ieeeSection *target;
489 struct ieeeFixupp s;
491 /* Don't put a fixup for things NASM can calculate */
492 if (wrt == NO_SEG && segment == NO_SEG)
493 return;
495 s.ftype = -1;
496 /* if it is a WRT offset */
497 if (wrt != NO_SEG) {
498 s.ftype = FT_WRT;
499 s.addend = offset;
500 if (wrt >= SEG_ABS)
501 s.id1 = -(wrt - SEG_ABS);
502 else {
503 if (wrt % 2 && realtype != OUT_REL2ADR
504 && realtype != OUT_REL4ADR) {
505 wrt--;
507 for (target = seghead; target; target = target->next)
508 if (target->index == wrt)
509 break;
510 if (target) {
511 s.id1 = target->ieee_index;
512 for (target = seghead; target; target = target->next)
513 if (target->index == segment)
514 break;
516 if (target)
517 s.id2 = target->ieee_index;
518 else {
520 * Now we assume the segment field is being used
521 * to hold an extern index
523 int32_t i = segment / 2;
524 struct ExtBack *eb = ebhead;
525 while (i > EXT_BLKSIZ) {
526 if (eb)
527 eb = eb->next;
528 else
529 break;
530 i -= EXT_BLKSIZ;
532 /* if we have an extern decide the type and make a record
534 if (eb) {
535 s.ftype = FT_EXTWRT;
536 s.addend = 0;
537 s.id2 = eb->index[i];
538 } else
539 error(ERR_NONFATAL,
540 "Source of WRT must be an offset");
543 } else
544 error(ERR_PANIC,
545 "unrecognised WRT value in ieee_write_fixup");
546 } else
547 error(ERR_NONFATAL, "target of WRT must be a section ");
549 s.size = size;
550 ieee_install_fixup(segto, &s);
551 return;
553 /* Pure segment fixup ? */
554 if (segment != NO_SEG) {
555 s.ftype = FT_SEG;
556 s.id1 = 0;
557 if (segment >= SEG_ABS) {
558 /* absolute far segment fixup */
559 s.id1 = -(segment - ~SEG_ABS);
560 } else if (segment % 2) {
561 /* fixup to named segment */
562 /* look it up */
563 for (target = seghead; target; target = target->next)
564 if (target->index == segment - 1)
565 break;
566 if (target)
567 s.id1 = target->ieee_index;
568 else {
570 * Now we assume the segment field is being used
571 * to hold an extern index
573 int32_t i = segment / 2;
574 struct ExtBack *eb = ebhead;
575 while (i > EXT_BLKSIZ) {
576 if (eb)
577 eb = eb->next;
578 else
579 break;
580 i -= EXT_BLKSIZ;
582 /* if we have an extern decide the type and make a record
584 if (eb) {
585 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
586 error(ERR_PANIC,
587 "Segment of a rel not supported in ieee_write_fixup");
588 } else {
589 /* If we want the segment */
590 s.ftype = FT_EXTSEG;
591 s.addend = 0;
592 s.id1 = eb->index[i];
595 } else
596 /* If we get here the seg value doesn't make sense */
597 error(ERR_PANIC,
598 "unrecognised segment value in ieee_write_fixup");
601 } else {
602 /* Assume we are offsetting directly from a section
603 * So look up the target segment
605 for (target = seghead; target; target = target->next)
606 if (target->index == segment)
607 break;
608 if (target) {
609 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
610 /* PC rel to a known offset */
611 s.id1 = target->ieee_index;
612 s.ftype = FT_REL;
613 s.size = size;
614 s.addend = offset;
615 } else {
616 /* We were offsetting from a seg */
617 s.id1 = target->ieee_index;
618 s.ftype = FT_OFS;
619 s.size = size;
620 s.addend = offset;
622 } else {
624 * Now we assume the segment field is being used
625 * to hold an extern index
627 int32_t i = segment / 2;
628 struct ExtBack *eb = ebhead;
629 while (i > EXT_BLKSIZ) {
630 if (eb)
631 eb = eb->next;
632 else
633 break;
634 i -= EXT_BLKSIZ;
636 /* if we have an extern decide the type and make a record
638 if (eb) {
639 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
640 s.ftype = FT_EXTREL;
641 s.addend = 0;
642 s.id1 = eb->index[i];
643 } else {
644 /* else we want the external offset */
645 s.ftype = FT_EXT;
646 s.addend = 0;
647 s.id1 = eb->index[i];
650 } else
651 /* If we get here the seg value doesn't make sense */
652 error(ERR_PANIC,
653 "unrecognised segment value in ieee_write_fixup");
656 if (size != 2 && s.ftype == FT_SEG)
657 error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
658 " segment base references");
659 s.size = size;
660 ieee_install_fixup(segto, &s);
661 return;
663 /* should never get here */
665 static void ieee_install_fixup(struct ieeeSection *seg,
666 struct ieeeFixupp *fix)
668 struct ieeeFixupp *f;
669 f = nasm_malloc(sizeof(struct ieeeFixupp));
670 memcpy(f, fix, sizeof(struct ieeeFixupp));
671 f->offset = seg->currentpos;
672 seg->currentpos += fix->size;
673 f->next = NULL;
674 if (seg->fptr)
675 seg->flptr = seg->flptr->next = f;
676 else
677 seg->fptr = seg->flptr = f;
682 * segment registry
684 static int32_t ieee_segment(char *name, int pass, int *bits)
687 * We call the label manager here to define a name for the new
688 * segment, and when our _own_ label-definition stub gets
689 * called in return, it should register the new segment name
690 * using the pointer it gets passed. That way we save memory,
691 * by sponging off the label manager.
693 if (!name) {
694 *bits = 16;
695 if (!any_segs)
696 return 0;
697 return seghead->index;
698 } else {
699 struct ieeeSection *seg;
700 int ieee_idx, attrs;
701 bool rn_error;
702 char *p;
705 * Look for segment attributes.
707 attrs = 0;
708 while (*name == '.')
709 name++; /* hack, but a documented one */
710 p = name;
711 while (*p && !nasm_isspace(*p))
712 p++;
713 if (*p) {
714 *p++ = '\0';
715 while (*p && nasm_isspace(*p))
716 *p++ = '\0';
718 while (*p) {
719 while (*p && !nasm_isspace(*p))
720 p++;
721 if (*p) {
722 *p++ = '\0';
723 while (*p && nasm_isspace(*p))
724 *p++ = '\0';
727 attrs++;
730 ieee_idx = 1;
731 for (seg = seghead; seg; seg = seg->next) {
732 ieee_idx++;
733 if (!strcmp(seg->name, name)) {
734 if (attrs > 0 && pass == 1)
735 error(ERR_WARNING, "segment attributes specified on"
736 " redeclaration of segment: ignoring");
737 if (seg->use32)
738 *bits = 32;
739 else
740 *bits = 16;
741 return seg->index;
745 *segtail = seg = nasm_malloc(sizeof(*seg));
746 seg->next = NULL;
747 segtail = &seg->next;
748 seg->index = seg_alloc();
749 seg->ieee_index = ieee_idx;
750 any_segs = true;
751 seg->name = NULL;
752 seg->currentpos = 0;
753 seg->align = 1; /* default */
754 seg->use32 = *bits == 32; /* default to user spec */
755 seg->combine = CMB_PUBLIC; /* default */
756 seg->pubhead = NULL;
757 seg->pubtail = &seg->pubhead;
758 seg->data = NULL;
759 seg->fptr = NULL;
760 seg->lochead = NULL;
761 seg->loctail = &seg->lochead;
764 * Process the segment attributes.
766 p = name;
767 while (attrs--) {
768 p += strlen(p);
769 while (!*p)
770 p++;
773 * `p' contains a segment attribute.
775 if (!nasm_stricmp(p, "private"))
776 seg->combine = CMB_PRIVATE;
777 else if (!nasm_stricmp(p, "public"))
778 seg->combine = CMB_PUBLIC;
779 else if (!nasm_stricmp(p, "common"))
780 seg->combine = CMB_COMMON;
781 else if (!nasm_stricmp(p, "use16"))
782 seg->use32 = false;
783 else if (!nasm_stricmp(p, "use32"))
784 seg->use32 = true;
785 else if (!nasm_strnicmp(p, "align=", 6)) {
786 seg->align = readnum(p + 6, &rn_error);
787 if (seg->align == 0)
788 seg->align = 1;
789 if (rn_error) {
790 seg->align = 1;
791 error(ERR_NONFATAL, "segment alignment should be"
792 " numeric");
794 switch ((int)seg->align) {
795 case 1: /* BYTE */
796 case 2: /* WORD */
797 case 4: /* DWORD */
798 case 16: /* PARA */
799 case 256: /* PAGE */
800 case 8:
801 case 32:
802 case 64:
803 case 128:
804 break;
805 default:
806 error(ERR_NONFATAL, "invalid alignment value %d",
807 seg->align);
808 seg->align = 1;
809 break;
811 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
812 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
813 if (rn_error)
814 error(ERR_NONFATAL, "argument to `absolute' segment"
815 " attribute should be numeric");
819 ieee_seg_needs_update = seg;
820 if (seg->align >= SEG_ABS)
821 deflabel(name, NO_SEG, seg->align - SEG_ABS,
822 NULL, false, false, &of_ieee, error);
823 else
824 deflabel(name, seg->index + 1, 0L,
825 NULL, false, false, &of_ieee, error);
826 ieee_seg_needs_update = NULL;
828 if (seg->use32)
829 *bits = 32;
830 else
831 *bits = 16;
832 return seg->index;
837 * directives supported
839 static int ieee_directive(char *directive, char *value, int pass)
842 (void)value;
843 (void)pass;
844 if (!strcmp(directive, "uppercase")) {
845 ieee_uppercase = true;
846 return 1;
848 return 0;
852 * Return segment data
854 static int32_t ieee_segbase(int32_t segment)
856 struct ieeeSection *seg;
859 * Find the segment in our list.
861 for (seg = seghead; seg; seg = seg->next)
862 if (seg->index == segment - 1)
863 break;
865 if (!seg)
866 return segment; /* not one of ours - leave it alone */
868 if (seg->align >= SEG_ABS)
869 return seg->align; /* absolute segment */
871 return segment; /* no special treatment */
875 * filename
877 static void ieee_filename(char *inname, char *outname, efunc error)
879 strcpy(ieee_infile, inname);
880 standard_extension(inname, outname, ".o", error);
883 static void ieee_write_file(int debuginfo)
885 struct tm *thetime;
886 time_t reltime;
887 struct FileName *fn;
888 struct ieeeSection *seg;
889 struct ieeePublic *pub, *loc;
890 struct ieeeExternal *ext;
891 struct ieeeObjData *data;
892 struct ieeeFixupp *fix;
893 struct Array *arr;
894 int i;
897 * Write the module header
899 ieee_putascii("MBFNASM,%02X%s.\r\n", strlen(ieee_infile), ieee_infile);
902 * Write the NASM boast comment.
904 ieee_putascii("CO0,%02X%s.\r\n", strlen(nasm_comment), nasm_comment);
907 * write processor-specific information
909 ieee_putascii("AD8,4,L.\r\n");
912 * date and time
914 time(&reltime);
915 thetime = localtime(&reltime);
916 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\r\n",
917 1900 + thetime->tm_year, thetime->tm_mon + 1,
918 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
919 thetime->tm_sec);
921 * if debugging, dump file names
923 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
924 ieee_putascii("C0105,%02X%s.\r\n", strlen(fn->name), fn->name);
927 ieee_putascii("CO101,07ENDHEAD.\r\n");
929 * the standard doesn't specify when to put checksums,
930 * we'll just do it periodically.
932 ieee_putcs(false);
935 * Write the section headers
937 seg = seghead;
938 if (!debuginfo && !strcmp(seg->name, "??LINE"))
939 seg = seg->next;
940 while (seg) {
941 char buf[256];
942 char attrib;
943 switch (seg->combine) {
944 case CMB_PUBLIC:
945 default:
946 attrib = 'C';
947 break;
948 case CMB_PRIVATE:
949 attrib = 'S';
950 break;
951 case CMB_COMMON:
952 attrib = 'M';
953 break;
955 ieee_unqualified_name(buf, seg->name);
956 if (seg->align >= SEG_ABS) {
957 ieee_putascii("ST%X,A,%02X%s.\r\n", seg->ieee_index,
958 strlen(buf), buf);
959 ieee_putascii("ASL%X,%lX.\r\n", seg->ieee_index,
960 (seg->align - SEG_ABS) * 16);
961 } else {
962 ieee_putascii("ST%X,%c,%02X%s.\r\n", seg->ieee_index, attrib,
963 strlen(buf), buf);
964 ieee_putascii("SA%X,%lX.\r\n", seg->ieee_index, seg->align);
965 ieee_putascii("ASS%X,%X.\r\n", seg->ieee_index,
966 seg->currentpos);
968 seg = seg->next;
971 * write the start address if there is one
973 if (ieee_entry_seg) {
974 for (seg = seghead; seg; seg = seg->next)
975 if (seg->index == ieee_entry_seg)
976 break;
977 if (!seg)
978 error(ERR_PANIC, "Start address records are incorrect");
979 else
980 ieee_putascii("ASG,R%X,%lX,+.\r\n", seg->ieee_index,
981 ieee_entry_ofs);
984 ieee_putcs(false);
986 * Write the publics
988 i = 1;
989 for (seg = seghead; seg; seg = seg->next) {
990 for (pub = seg->pubhead; pub; pub = pub->next) {
991 char buf[256];
992 ieee_unqualified_name(buf, pub->name);
993 ieee_putascii("NI%X,%02X%s.\r\n", i, strlen(buf), buf);
994 if (pub->segment == -1)
995 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i, pub->index,
996 pub->offset);
997 else
998 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i, pub->segment * 16,
999 pub->offset);
1000 if (debuginfo) {
1001 if (pub->type >= 0x100)
1002 ieee_putascii("ATI%X,T%X.\r\n", i, pub->type - 0x100);
1003 else
1004 ieee_putascii("ATI%X,%X.\r\n", i, pub->type);
1006 i++;
1009 pub = fpubhead;
1010 i = 1;
1011 while (pub) {
1012 char buf[256];
1013 ieee_unqualified_name(buf, pub->name);
1014 ieee_putascii("NI%X,%02X%s.\r\n", i, strlen(buf), buf);
1015 if (pub->segment == -1)
1016 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i, pub->index,
1017 pub->offset);
1018 else
1019 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i, pub->segment * 16,
1020 pub->offset);
1021 if (debuginfo) {
1022 if (pub->type >= 0x100)
1023 ieee_putascii("ATI%X,T%X.\r\n", i, pub->type - 0x100);
1024 else
1025 ieee_putascii("ATI%X,%X.\r\n", i, pub->type);
1027 i++;
1028 pub = pub->next;
1031 * Write the externals
1033 ext = exthead;
1034 i = 1;
1035 while (ext) {
1036 char buf[256];
1037 ieee_unqualified_name(buf, ext->name);
1038 ieee_putascii("NX%X,%02X%s.\r\n", i++, strlen(buf), buf);
1039 ext = ext->next;
1041 ieee_putcs(false);
1044 * IEEE doesn't have a standard pass break record
1045 * so use the ladsoft variant
1047 ieee_putascii("CO100,06ENDSYM.\r\n");
1050 * now put types
1052 i = ARRAY_BOT;
1053 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1054 ieee_putascii("TY%X,20,%X,%lX.\r\n", i++, arr->basetype,
1055 arr->size);
1058 * now put locals
1060 i = 1;
1061 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1062 for (loc = seg->lochead; loc; loc = loc->next) {
1063 char buf[256];
1064 ieee_unqualified_name(buf, loc->name);
1065 ieee_putascii("NN%X,%02X%s.\r\n", i, strlen(buf), buf);
1066 if (loc->segment == -1)
1067 ieee_putascii("ASN%X,R%X,%lX,+.\r\n", i, loc->index,
1068 loc->offset);
1069 else
1070 ieee_putascii("ASN%X,%lX,%lX,+.\r\n", i, loc->segment * 16,
1071 loc->offset);
1072 if (debuginfo) {
1073 if (loc->type >= 0x100)
1074 ieee_putascii("ATN%X,T%X.\r\n", i, loc->type - 0x100);
1075 else
1076 ieee_putascii("ATN%X,%X.\r\n", i, loc->type);
1078 i++;
1083 * put out section data;
1085 seg = seghead;
1086 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1087 seg = seg->next;
1088 while (seg) {
1089 if (seg->currentpos) {
1090 int32_t size, org = 0;
1091 data = seg->data;
1092 ieee_putascii("SB%X.\r\n", seg->ieee_index);
1093 fix = seg->fptr;
1094 while (fix) {
1095 size = HUNKSIZE - (org % HUNKSIZE);
1096 size =
1097 size + org >
1098 seg->currentpos ? seg->currentpos - org : size;
1099 size = fix->offset - org > size ? size : fix->offset - org;
1100 org = ieee_putld(org, org + size, data->data);
1101 if (org % HUNKSIZE == 0)
1102 data = data->next;
1103 if (org == fix->offset) {
1104 org += ieee_putlr(fix);
1105 fix = fix->next;
1108 while (org < seg->currentpos && data) {
1109 size =
1110 seg->currentpos - org >
1111 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1112 org = ieee_putld(org, org + size, data->data);
1113 data = data->next;
1115 ieee_putcs(false);
1118 seg = seg->next;
1121 * module end record
1123 ieee_putascii("ME.\r\n");
1126 static void ieee_write_byte(struct ieeeSection *seg, int data)
1128 int temp;
1129 if (!(temp = seg->currentpos++ % HUNKSIZE))
1130 ieee_data_new(seg);
1131 seg->datacurr->data[temp] = data;
1134 static void ieee_write_word(struct ieeeSection *seg, int data)
1136 ieee_write_byte(seg, data & 0xFF);
1137 ieee_write_byte(seg, (data >> 8) & 0xFF);
1140 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1142 ieee_write_byte(seg, data & 0xFF);
1143 ieee_write_byte(seg, (data >> 8) & 0xFF);
1144 ieee_write_byte(seg, (data >> 16) & 0xFF);
1145 ieee_write_byte(seg, (data >> 24) & 0xFF);
1147 static void ieee_putascii(char *format, ...)
1149 char buffer[256];
1150 int i, l;
1151 va_list ap;
1153 va_start(ap, format);
1154 vsnprintf(buffer, sizeof(buffer), format, ap);
1155 l = strlen(buffer);
1156 for (i = 0; i < l; i++)
1157 if ((buffer[i] & 0xff) > 31)
1158 checksum += buffer[i];
1159 va_end(ap);
1160 fprintf(ofp, buffer);
1164 * put out a checksum record */
1165 static void ieee_putcs(int toclear)
1167 if (toclear) {
1168 ieee_putascii("CS.\r\n");
1169 } else {
1170 checksum += 'C';
1171 checksum += 'S';
1172 ieee_putascii("CS%02X.\r\n", checksum & 127);
1174 checksum = 0;
1177 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1179 int32_t val;
1180 if (start == end)
1181 return (start);
1182 val = start % HUNKSIZE;
1183 /* fill up multiple lines */
1184 while (end - start >= LDPERLINE) {
1185 int i;
1186 ieee_putascii("LD");
1187 for (i = 0; i < LDPERLINE; i++) {
1188 ieee_putascii("%02X", buf[val++]);
1189 start++;
1191 ieee_putascii(".\r\n");
1193 /* if no partial lines */
1194 if (start == end)
1195 return (start);
1196 /* make a partial line */
1197 ieee_putascii("LD");
1198 while (start < end) {
1199 ieee_putascii("%02X", buf[val++]);
1200 start++;
1202 ieee_putascii(".\r\n");
1203 return (start);
1205 static int32_t ieee_putlr(struct ieeeFixupp *p)
1208 * To deal with the vagaries of segmentation the LADsoft linker
1209 * defines two types of segments: absolute and virtual. Note that
1210 * 'absolute' in this context is a different thing from the IEEE
1211 * definition of an absolute segment type, which is also supported. If a
1212 * sement is linked in virtual mode the low limit (L-var) is
1213 * subtracted from each R,X, and P variable which appears in an
1214 * expression, so that we can have relative offsets. Meanwhile
1215 * in the ABSOLUTE mode this subtraction is not done and
1216 * so we can use absolute offsets from 0. In the LADsoft linker
1217 * this configuration is not done in the assemblker source but in
1218 * a source the linker reads. Generally this type of thing only
1219 * becomes an issue if real mode code is used. A pure 32-bit linker could
1220 * get away without defining the virtual mode...
1222 char buf[40];
1223 int32_t size = p->size;
1224 switch (p->ftype) {
1225 case FT_SEG:
1226 if (p->id1 < 0)
1227 sprintf(buf, "%"PRIX32"", -p->id1);
1228 else
1229 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1230 break;
1231 case FT_OFS:
1232 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1233 break;
1234 case FT_REL:
1235 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1236 break;
1238 case FT_WRT:
1239 if (p->id2 < 0)
1240 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1241 p->id2, -p->id1 * 16);
1242 else
1243 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1244 p->id2, p->id1);
1245 break;
1246 case FT_EXT:
1247 sprintf(buf, "X%"PRIX32"", p->id1);
1248 break;
1249 case FT_EXTREL:
1250 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1251 break;
1252 case FT_EXTSEG:
1253 /* We needed a non-ieee hack here.
1254 * We introduce the Y variable, which is the low
1255 * limit of the native segment the extern resides in
1257 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1258 break;
1259 case FT_EXTWRT:
1260 if (p->id2 < 0)
1261 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1262 -p->id1 * 16);
1263 else
1264 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1265 break;
1267 ieee_putascii("LR(%s,%"PRIX32").\r\n", buf, size);
1269 return (size);
1272 /* Dump all segment data (text and fixups )*/
1274 static void ieee_unqualified_name(char *dest, char *source)
1276 if (ieee_uppercase) {
1277 while (*source)
1278 *dest++ = toupper(*source++);
1279 *dest = 0;
1280 } else
1281 strcpy(dest, source);
1283 void dbgls_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1285 int tempint;
1286 (void)of;
1287 (void)id;
1288 (void)fp;
1289 (void)error;
1291 fnhead = NULL;
1292 fntail = &fnhead;
1293 arrindex = ARRAY_BOT;
1294 arrhead = NULL;
1295 arrtail = &arrhead;
1296 ieee_segment("??LINE", 2, &tempint);
1297 any_segs = false;
1299 static void dbgls_cleanup(void)
1301 struct ieeeSection *segtmp;
1302 while (fnhead) {
1303 struct FileName *fntemp = fnhead;
1304 fnhead = fnhead->next;
1305 nasm_free(fntemp->name);
1306 nasm_free(fntemp);
1308 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1309 while (segtmp->lochead) {
1310 struct ieeePublic *loctmp = segtmp->lochead;
1311 segtmp->lochead = loctmp->next;
1312 nasm_free(loctmp->name);
1313 nasm_free(loctmp);
1316 while (arrhead) {
1317 struct Array *arrtmp = arrhead;
1318 arrhead = arrhead->next;
1319 nasm_free(arrtmp);
1324 * because this routine is not bracketed in
1325 * the main program, this routine will be called even if there
1326 * is no request for debug info
1327 * so, we have to make sure the ??LINE segment is avaialbe
1328 * as the first segment when this debug format is selected
1330 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1332 struct FileName *fn;
1333 struct ieeeSection *seg;
1334 int i = 0;
1335 if (segto == NO_SEG)
1336 return;
1339 * If `any_segs' is still false, we must define a default
1340 * segment.
1342 if (!any_segs) {
1343 int tempint; /* ignored */
1344 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1345 error(ERR_PANIC, "strange segment conditions in OBJ driver");
1349 * Find the segment we are targetting.
1351 for (seg = seghead; seg; seg = seg->next)
1352 if (seg->index == segto)
1353 break;
1354 if (!seg)
1355 error(ERR_PANIC, "lineno directed to nonexistent segment?");
1357 for (fn = fnhead; fn; fn = fn->next) {
1358 if (!nasm_stricmp(lnfname, fn->name))
1359 break;
1360 i++;
1362 if (!fn) {
1363 fn = nasm_malloc(sizeof(*fn));
1364 fn->name = nasm_malloc(strlen(lnfname) + 1);
1365 fn->index = i;
1366 strcpy(fn->name, lnfname);
1367 fn->next = NULL;
1368 *fntail = fn;
1369 fntail = &fn->next;
1371 ieee_write_byte(seghead, fn->index);
1372 ieee_write_word(seghead, lineno);
1373 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1374 seg->currentpos);
1377 static void dbgls_deflabel(char *name, int32_t segment,
1378 int64_t offset, int is_global, char *special)
1380 struct ieeeSection *seg;
1382 /* Keep compiler from warning about special */
1383 (void)special;
1386 * If it's a special-retry from pass two, discard it.
1388 if (is_global == 3)
1389 return;
1392 * First check for the double-period, signifying something
1393 * unusual.
1395 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
1396 return;
1400 * Case (i):
1402 if (ieee_seg_needs_update)
1403 return;
1404 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
1405 return;
1407 if (segment >= SEG_ABS || segment == NO_SEG) {
1408 return;
1412 * If `any_segs' is still false, we might need to define a
1413 * default segment, if they're trying to declare a label in
1414 * `first_seg'. But the label should exist due to a prior
1415 * call to ieee_deflabel so we can skip that.
1418 for (seg = seghead; seg; seg = seg->next)
1419 if (seg->index == segment) {
1420 struct ieeePublic *loc;
1422 * Case (ii). Maybe MODPUB someday?
1424 if (!is_global) {
1425 last_defined = loc = nasm_malloc(sizeof(*loc));
1426 *seg->loctail = loc;
1427 seg->loctail = &loc->next;
1428 loc->next = NULL;
1429 loc->name = nasm_strdup(name);
1430 loc->offset = offset;
1431 loc->segment = -1;
1432 loc->index = seg->ieee_index;
1436 static void dbgls_typevalue(int32_t type)
1438 int elem = TYM_ELEMENTS(type);
1439 type = TYM_TYPE(type);
1441 if (!last_defined)
1442 return;
1444 switch (type) {
1445 case TY_BYTE:
1446 last_defined->type = 1; /* uint8_t */
1447 break;
1448 case TY_WORD:
1449 last_defined->type = 3; /* unsigned word */
1450 break;
1451 case TY_DWORD:
1452 last_defined->type = 5; /* unsigned dword */
1453 break;
1454 case TY_FLOAT:
1455 last_defined->type = 9; /* float */
1456 break;
1457 case TY_QWORD:
1458 last_defined->type = 10; /* qword */
1459 break;
1460 case TY_TBYTE:
1461 last_defined->type = 11; /* TBYTE */
1462 break;
1463 default:
1464 last_defined->type = 0x10; /* near label */
1465 break;
1468 if (elem > 1) {
1469 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
1470 int vtype = last_defined->type;
1471 arrtmp->size = elem;
1472 arrtmp->basetype = vtype;
1473 arrtmp->next = NULL;
1474 last_defined->type = arrindex++ + 0x100;
1475 *arrtail = arrtmp;
1476 arrtail = &(arrtmp->next);
1478 last_defined = NULL;
1480 static void dbgls_output(int output_type, void *param)
1482 (void)output_type;
1483 (void)param;
1485 static struct dfmt ladsoft_debug_form = {
1486 "LADsoft Debug Records",
1487 "ladsoft",
1488 dbgls_init,
1489 dbgls_linnum,
1490 dbgls_deflabel,
1491 null_debug_routine,
1492 dbgls_typevalue,
1493 dbgls_output,
1494 dbgls_cleanup,
1496 static struct dfmt *ladsoft_debug_arr[3] = {
1497 &ladsoft_debug_form,
1498 &null_debug_form,
1499 NULL
1501 struct ofmt of_ieee = {
1502 "IEEE-695 (LADsoft variant) object file format",
1503 "ieee",
1504 NULL,
1505 ladsoft_debug_arr,
1506 &ladsoft_debug_form,
1507 NULL,
1508 ieee_init,
1509 ieee_set_info,
1510 ieee_out,
1511 ieee_deflabel,
1512 ieee_segment,
1513 ieee_segbase,
1514 ieee_directive,
1515 ieee_filename,
1516 ieee_cleanup
1519 #endif /* OF_IEEE */