macho: Improve macho_calculate_sizes
[nasm.git] / output / outieee.c
blobff242ac6713bc28cd54695d7c96fb0b759c3889f
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 "ver.h"
81 #include "outform.h"
82 #include "outlib.h"
84 #ifdef OF_IEEE
86 #define ARRAY_BOT 0x1
88 static char ieee_infile[FILENAME_MAX];
89 static int ieee_uppercase;
91 static bool any_segs;
92 static int arrindex;
94 #define HUNKSIZE 1024 /* Size of the data hunk */
95 #define EXT_BLKSIZ 512
96 #define LDPERLINE 32 /* bytes per line in output */
98 struct ieeeSection;
100 struct LineNumber {
101 struct LineNumber *next;
102 struct ieeeSection *segment;
103 int32_t offset;
104 int32_t lineno;
107 static struct FileName {
108 struct FileName *next;
109 char *name;
110 int32_t index;
111 } *fnhead, **fntail;
113 static struct Array {
114 struct Array *next;
115 unsigned size;
116 int basetype;
117 } *arrhead, **arrtail;
119 static struct ieeePublic {
120 struct ieeePublic *next;
121 char *name;
122 int32_t offset;
123 int32_t segment; /* only if it's far-absolute */
124 int32_t index;
125 int type; /* for debug purposes */
126 } *fpubhead, **fpubtail, *last_defined;
128 static struct ieeeExternal {
129 struct ieeeExternal *next;
130 char *name;
131 int32_t commonsize;
132 } *exthead, **exttail;
134 static int externals;
136 static struct ExtBack {
137 struct ExtBack *next;
138 int index[EXT_BLKSIZ];
139 } *ebhead, **ebtail;
141 /* NOTE: the first segment MUST be the lineno segment */
142 static struct ieeeSection {
143 struct ieeeSection *next;
144 char *name;
145 struct ieeeObjData *data, *datacurr;
146 struct ieeeFixupp *fptr, *flptr;
147 int32_t index; /* the NASM segment id */
148 int32_t ieee_index; /* the OBJ-file segment index */
149 int32_t currentpos;
150 int32_t align; /* can be SEG_ABS + absolute addr */
151 int32_t startpos;
152 int32_t use32; /* is this segment 32-bit? */
153 struct ieeePublic *pubhead, **pubtail, *lochead, **loctail;
154 enum {
155 CMB_PRIVATE = 0,
156 CMB_PUBLIC = 2,
157 CMB_COMMON = 6
158 } combine;
159 } *seghead, **segtail, *ieee_seg_needs_update;
161 struct ieeeObjData {
162 struct ieeeObjData *next;
163 uint8_t data[HUNKSIZE];
166 struct ieeeFixupp {
167 struct ieeeFixupp *next;
168 enum {
169 FT_SEG = 0,
170 FT_REL = 1,
171 FT_OFS = 2,
172 FT_EXT = 3,
173 FT_WRT = 4,
174 FT_EXTREL = 5,
175 FT_EXTWRT = 6,
176 FT_EXTSEG = 7
177 } ftype;
178 int16_t size;
179 int32_t id1;
180 int32_t id2;
181 int32_t offset;
182 int32_t addend;
185 static int32_t ieee_entry_seg, ieee_entry_ofs;
186 static int checksum;
188 extern const struct ofmt of_ieee;
189 static const struct dfmt ladsoft_debug_form;
191 static void ieee_data_new(struct ieeeSection *);
192 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection *,
193 int, uint64_t, int32_t);
194 static void ieee_install_fixup(struct ieeeSection *, struct ieeeFixupp *);
195 static int32_t ieee_segment(char *, int, int *);
196 static void ieee_write_file(void);
197 static void ieee_write_byte(struct ieeeSection *, int);
198 static void ieee_write_word(struct ieeeSection *, int);
199 static void ieee_write_dword(struct ieeeSection *, int32_t);
200 static void ieee_putascii(char *, ...);
201 static void ieee_putcs(int);
202 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
203 static int32_t ieee_putlr(struct ieeeFixupp *);
204 static void ieee_unqualified_name(char *, char *);
207 * pup init
209 static void ieee_init(void)
211 any_segs = false;
212 fpubhead = NULL;
213 fpubtail = &fpubhead;
214 exthead = NULL;
215 exttail = &exthead;
216 externals = 1;
217 ebhead = NULL;
218 ebtail = &ebhead;
219 seghead = ieee_seg_needs_update = NULL;
220 segtail = &seghead;
221 ieee_entry_seg = NO_SEG;
222 ieee_uppercase = false;
223 checksum = 0;
226 static int ieee_set_info(enum geninfo type, char **val)
228 (void)type;
229 (void)val;
231 return 0;
235 * Rundown
237 static void ieee_cleanup(void)
239 ieee_write_file();
240 dfmt->cleanup();
241 while (seghead) {
242 struct ieeeSection *segtmp = seghead;
243 seghead = seghead->next;
244 while (segtmp->pubhead) {
245 struct ieeePublic *pubtmp = segtmp->pubhead;
246 segtmp->pubhead = pubtmp->next;
247 nasm_free(pubtmp);
249 while (segtmp->fptr) {
250 struct ieeeFixupp *fixtmp = segtmp->fptr;
251 segtmp->fptr = fixtmp->next;
252 nasm_free(fixtmp);
254 while (segtmp->data) {
255 struct ieeeObjData *dattmp = segtmp->data;
256 segtmp->data = dattmp->next;
257 nasm_free(dattmp);
259 nasm_free(segtmp);
261 while (fpubhead) {
262 struct ieeePublic *pubtmp = fpubhead;
263 fpubhead = fpubhead->next;
264 nasm_free(pubtmp);
266 while (exthead) {
267 struct ieeeExternal *exttmp = exthead;
268 exthead = exthead->next;
269 nasm_free(exttmp);
271 while (ebhead) {
272 struct ExtBack *ebtmp = ebhead;
273 ebhead = ebhead->next;
274 nasm_free(ebtmp);
279 * callback for labels
281 static void ieee_deflabel(char *name, int32_t segment,
282 int64_t offset, int is_global, char *special)
285 * We have three cases:
287 * (i) `segment' is a segment-base. If so, set the name field
288 * for the segment structure it refers to, and then
289 * return.
291 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
292 * Save the label position for later output of a PUBDEF record.
295 * (iii) `segment' is not one of our segments. Save the label
296 * position for later output of an EXTDEF.
298 struct ieeeExternal *ext;
299 struct ExtBack *eb;
300 struct ieeeSection *seg;
301 int i;
303 if (special) {
304 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
307 * First check for the double-period, signifying something
308 * unusual.
310 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
311 if (!strcmp(name, "..start")) {
312 ieee_entry_seg = segment;
313 ieee_entry_ofs = offset;
315 return;
319 * Case (i):
321 if (ieee_seg_needs_update) {
322 ieee_seg_needs_update->name = name;
323 return;
325 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
326 return;
329 * case (ii)
331 if (segment >= SEG_ABS) {
333 * SEG_ABS subcase of (ii).
335 if (is_global) {
336 struct ieeePublic *pub;
338 pub = *fpubtail = nasm_malloc(sizeof(*pub));
339 fpubtail = &pub->next;
340 pub->next = NULL;
341 pub->name = name;
342 pub->offset = offset;
343 pub->segment = segment & ~SEG_ABS;
345 return;
348 for (seg = seghead; seg && is_global; seg = seg->next)
349 if (seg->index == segment) {
350 struct ieeePublic *pub;
352 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
353 seg->pubtail = &pub->next;
354 pub->next = NULL;
355 pub->name = name;
356 pub->offset = offset;
357 pub->index = seg->ieee_index;
358 pub->segment = -1;
359 return;
363 * Case (iii).
365 if (is_global) {
366 ext = *exttail = nasm_malloc(sizeof(*ext));
367 ext->next = NULL;
368 exttail = &ext->next;
369 ext->name = name;
370 if (is_global == 2)
371 ext->commonsize = offset;
372 else
373 ext->commonsize = 0;
374 i = segment / 2;
375 eb = ebhead;
376 if (!eb) {
377 eb = *ebtail = nasm_zalloc(sizeof(*eb));
378 eb->next = NULL;
379 ebtail = &eb->next;
381 while (i > EXT_BLKSIZ) {
382 if (eb && eb->next)
383 eb = eb->next;
384 else {
385 eb = *ebtail = nasm_zalloc(sizeof(*eb));
386 eb->next = NULL;
387 ebtail = &eb->next;
389 i -= EXT_BLKSIZ;
391 eb->index[i] = externals++;
397 * Put data out
399 static void ieee_out(int32_t segto, const void *data,
400 enum out_type type, uint64_t size,
401 int32_t segment, int32_t wrt)
403 const uint8_t *ucdata;
404 int32_t ldata;
405 struct ieeeSection *seg;
408 * handle absolute-assembly (structure definitions)
410 if (segto == NO_SEG) {
411 if (type != OUT_RESERVE)
412 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
413 " space");
414 return;
418 * If `any_segs' is still false, we must define a default
419 * segment.
421 if (!any_segs) {
422 int tempint; /* ignored */
423 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
424 nasm_panic(0, "strange segment conditions in IEEE driver");
428 * Find the segment we are targetting.
430 for (seg = seghead; seg; seg = seg->next)
431 if (seg->index == segto)
432 break;
433 if (!seg)
434 nasm_panic(0, "code directed to nonexistent segment?");
436 if (type == OUT_RAWDATA) {
437 ucdata = data;
438 while (size--)
439 ieee_write_byte(seg, *ucdata++);
440 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
441 type == OUT_REL4ADR) {
442 if (type == OUT_ADDRESS)
443 size = abs((int)size);
444 else if (segment == NO_SEG)
445 nasm_error(ERR_NONFATAL, "relative call to absolute address not"
446 " supported by IEEE format");
447 ldata = *(int64_t *)data;
448 if (type == OUT_REL2ADR)
449 ldata += (size - 2);
450 if (type == OUT_REL4ADR)
451 ldata += (size - 4);
452 ieee_write_fixup(segment, wrt, seg, size, type, ldata);
453 if (size == 2)
454 ieee_write_word(seg, ldata);
455 else
456 ieee_write_dword(seg, ldata);
457 } else if (type == OUT_RESERVE) {
458 while (size--)
459 ieee_write_byte(seg, 0);
463 static void ieee_data_new(struct ieeeSection *segto)
466 if (!segto->data)
467 segto->data = segto->datacurr =
468 nasm_malloc(sizeof(*(segto->datacurr)));
469 else
470 segto->datacurr = segto->datacurr->next =
471 nasm_malloc(sizeof(*(segto->datacurr)));
472 segto->datacurr->next = NULL;
476 * this routine is unalduterated bloatware. I usually don't do this
477 * but I might as well see what it is like on a harmless program.
478 * If anyone wants to optimize this is a good canditate!
480 static void ieee_write_fixup(int32_t segment, int32_t wrt,
481 struct ieeeSection *segto, int size,
482 uint64_t realtype, int32_t offset)
484 struct ieeeSection *target;
485 struct ieeeFixupp s;
487 /* Don't put a fixup for things NASM can calculate */
488 if (wrt == NO_SEG && segment == NO_SEG)
489 return;
491 s.ftype = -1;
492 /* if it is a WRT offset */
493 if (wrt != NO_SEG) {
494 s.ftype = FT_WRT;
495 s.addend = offset;
496 if (wrt >= SEG_ABS)
497 s.id1 = -(wrt - SEG_ABS);
498 else {
499 if (wrt % 2 && realtype != OUT_REL2ADR
500 && realtype != OUT_REL4ADR) {
501 wrt--;
503 for (target = seghead; target; target = target->next)
504 if (target->index == wrt)
505 break;
506 if (target) {
507 s.id1 = target->ieee_index;
508 for (target = seghead; target; target = target->next)
509 if (target->index == segment)
510 break;
512 if (target)
513 s.id2 = target->ieee_index;
514 else {
516 * Now we assume the segment field is being used
517 * to hold an extern index
519 int32_t i = segment / 2;
520 struct ExtBack *eb = ebhead;
521 while (i > EXT_BLKSIZ) {
522 if (eb)
523 eb = eb->next;
524 else
525 break;
526 i -= EXT_BLKSIZ;
528 /* if we have an extern decide the type and make a record
530 if (eb) {
531 s.ftype = FT_EXTWRT;
532 s.addend = 0;
533 s.id2 = eb->index[i];
534 } else
535 nasm_error(ERR_NONFATAL,
536 "Source of WRT must be an offset");
539 } else
540 nasm_panic(0,
541 "unrecognised WRT value in ieee_write_fixup");
542 } else
543 nasm_error(ERR_NONFATAL, "target of WRT must be a section ");
545 s.size = size;
546 ieee_install_fixup(segto, &s);
547 return;
549 /* Pure segment fixup ? */
550 if (segment != NO_SEG) {
551 s.ftype = FT_SEG;
552 s.id1 = 0;
553 if (segment >= SEG_ABS) {
554 /* absolute far segment fixup */
555 s.id1 = -(segment - ~SEG_ABS);
556 } else if (segment % 2) {
557 /* fixup to named segment */
558 /* look it up */
559 for (target = seghead; target; target = target->next)
560 if (target->index == segment - 1)
561 break;
562 if (target)
563 s.id1 = target->ieee_index;
564 else {
566 * Now we assume the segment field is being used
567 * to hold an extern index
569 int32_t i = segment / 2;
570 struct ExtBack *eb = ebhead;
571 while (i > EXT_BLKSIZ) {
572 if (eb)
573 eb = eb->next;
574 else
575 break;
576 i -= EXT_BLKSIZ;
578 /* if we have an extern decide the type and make a record
580 if (eb) {
581 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
582 nasm_panic(0,
583 "Segment of a rel not supported in ieee_write_fixup");
584 } else {
585 /* If we want the segment */
586 s.ftype = FT_EXTSEG;
587 s.addend = 0;
588 s.id1 = eb->index[i];
591 } else
592 /* If we get here the seg value doesn't make sense */
593 nasm_panic(0,
594 "unrecognised segment value in ieee_write_fixup");
597 } else {
598 /* Assume we are offsetting directly from a section
599 * So look up the target segment
601 for (target = seghead; target; target = target->next)
602 if (target->index == segment)
603 break;
604 if (target) {
605 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
606 /* PC rel to a known offset */
607 s.id1 = target->ieee_index;
608 s.ftype = FT_REL;
609 s.size = size;
610 s.addend = offset;
611 } else {
612 /* We were offsetting from a seg */
613 s.id1 = target->ieee_index;
614 s.ftype = FT_OFS;
615 s.size = size;
616 s.addend = offset;
618 } else {
620 * Now we assume the segment field is being used
621 * to hold an extern index
623 int32_t i = segment / 2;
624 struct ExtBack *eb = ebhead;
625 while (i > EXT_BLKSIZ) {
626 if (eb)
627 eb = eb->next;
628 else
629 break;
630 i -= EXT_BLKSIZ;
632 /* if we have an extern decide the type and make a record
634 if (eb) {
635 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
636 s.ftype = FT_EXTREL;
637 s.addend = 0;
638 s.id1 = eb->index[i];
639 } else {
640 /* else we want the external offset */
641 s.ftype = FT_EXT;
642 s.addend = 0;
643 s.id1 = eb->index[i];
646 } else
647 /* If we get here the seg value doesn't make sense */
648 nasm_panic(0,
649 "unrecognised segment value in ieee_write_fixup");
652 if (size != 2 && s.ftype == FT_SEG)
653 nasm_error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
654 " segment base references");
655 s.size = size;
656 ieee_install_fixup(segto, &s);
657 return;
659 /* should never get here */
661 static void ieee_install_fixup(struct ieeeSection *seg,
662 struct ieeeFixupp *fix)
664 struct ieeeFixupp *f;
665 f = nasm_malloc(sizeof(struct ieeeFixupp));
666 memcpy(f, fix, sizeof(struct ieeeFixupp));
667 f->offset = seg->currentpos;
668 seg->currentpos += fix->size;
669 f->next = NULL;
670 if (seg->fptr)
671 seg->flptr = seg->flptr->next = f;
672 else
673 seg->fptr = seg->flptr = f;
678 * segment registry
680 static int32_t ieee_segment(char *name, int pass, int *bits)
683 * We call the label manager here to define a name for the new
684 * segment, and when our _own_ label-definition stub gets
685 * called in return, it should register the new segment name
686 * using the pointer it gets passed. That way we save memory,
687 * by sponging off the label manager.
689 if (!name) {
690 *bits = 16;
691 if (!any_segs)
692 return 0;
693 return seghead->index;
694 } else {
695 struct ieeeSection *seg;
696 int ieee_idx, attrs;
697 bool rn_error;
698 char *p;
701 * Look for segment attributes.
703 attrs = 0;
704 while (*name == '.')
705 name++; /* hack, but a documented one */
706 p = name;
707 while (*p && !nasm_isspace(*p))
708 p++;
709 if (*p) {
710 *p++ = '\0';
711 while (*p && nasm_isspace(*p))
712 *p++ = '\0';
714 while (*p) {
715 while (*p && !nasm_isspace(*p))
716 p++;
717 if (*p) {
718 *p++ = '\0';
719 while (*p && nasm_isspace(*p))
720 *p++ = '\0';
723 attrs++;
726 ieee_idx = 1;
727 for (seg = seghead; seg; seg = seg->next) {
728 ieee_idx++;
729 if (!strcmp(seg->name, name)) {
730 if (attrs > 0 && pass == 1)
731 nasm_error(ERR_WARNING, "segment attributes specified on"
732 " redeclaration of segment: ignoring");
733 if (seg->use32)
734 *bits = 32;
735 else
736 *bits = 16;
737 return seg->index;
741 *segtail = seg = nasm_malloc(sizeof(*seg));
742 seg->next = NULL;
743 segtail = &seg->next;
744 seg->index = seg_alloc();
745 seg->ieee_index = ieee_idx;
746 any_segs = true;
747 seg->name = NULL;
748 seg->currentpos = 0;
749 seg->align = 1; /* default */
750 seg->use32 = *bits == 32; /* default to user spec */
751 seg->combine = CMB_PUBLIC; /* default */
752 seg->pubhead = NULL;
753 seg->pubtail = &seg->pubhead;
754 seg->data = NULL;
755 seg->fptr = NULL;
756 seg->lochead = NULL;
757 seg->loctail = &seg->lochead;
760 * Process the segment attributes.
762 p = name;
763 while (attrs--) {
764 p += strlen(p);
765 while (!*p)
766 p++;
769 * `p' contains a segment attribute.
771 if (!nasm_stricmp(p, "private"))
772 seg->combine = CMB_PRIVATE;
773 else if (!nasm_stricmp(p, "public"))
774 seg->combine = CMB_PUBLIC;
775 else if (!nasm_stricmp(p, "common"))
776 seg->combine = CMB_COMMON;
777 else if (!nasm_stricmp(p, "use16"))
778 seg->use32 = false;
779 else if (!nasm_stricmp(p, "use32"))
780 seg->use32 = true;
781 else if (!nasm_strnicmp(p, "align=", 6)) {
782 seg->align = readnum(p + 6, &rn_error);
783 if (seg->align == 0)
784 seg->align = 1;
785 if (rn_error) {
786 seg->align = 1;
787 nasm_error(ERR_NONFATAL, "segment alignment should be"
788 " numeric");
790 switch ((int)seg->align) {
791 case 1: /* BYTE */
792 case 2: /* WORD */
793 case 4: /* DWORD */
794 case 16: /* PARA */
795 case 256: /* PAGE */
796 case 8:
797 case 32:
798 case 64:
799 case 128:
800 break;
801 default:
802 nasm_error(ERR_NONFATAL, "invalid alignment value %d",
803 seg->align);
804 seg->align = 1;
805 break;
807 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
808 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
809 if (rn_error)
810 nasm_error(ERR_NONFATAL, "argument to `absolute' segment"
811 " attribute should be numeric");
815 ieee_seg_needs_update = seg;
816 if (seg->align >= SEG_ABS)
817 define_label(name, NO_SEG, seg->align - SEG_ABS,
818 NULL, false, false);
819 else
820 define_label(name, seg->index + 1, 0L, NULL, false, false);
821 ieee_seg_needs_update = NULL;
823 if (seg->use32)
824 *bits = 32;
825 else
826 *bits = 16;
827 return seg->index;
832 * directives supported
834 static int ieee_directive(enum directives directive, char *value, int pass)
837 (void)value;
838 (void)pass;
840 switch (directive) {
841 case D_UPPERCASE:
842 ieee_uppercase = true;
843 return 1;
845 default:
846 return 0;
850 static void ieee_sectalign(int32_t seg, unsigned int value)
852 struct ieeeSection *s;
854 list_for_each(s, seghead) {
855 if (s->index == seg)
856 break;
860 * 256 is maximum there, note it may happen
861 * that align is issued on "absolute" segment
862 * it's fine since SEG_ABS > 256 and we never
863 * get escape this test
865 if (!s || !is_power2(value) || value > 256)
866 return;
868 if ((unsigned int)s->align < value)
869 s->align = value;
873 * Return segment data
875 static int32_t ieee_segbase(int32_t segment)
877 struct ieeeSection *seg;
880 * Find the segment in our list.
882 for (seg = seghead; seg; seg = seg->next)
883 if (seg->index == segment - 1)
884 break;
886 if (!seg)
887 return segment; /* not one of ours - leave it alone */
889 if (seg->align >= SEG_ABS)
890 return seg->align; /* absolute segment */
892 return segment; /* no special treatment */
896 * filename
898 static void ieee_filename(char *inname, char *outname)
900 strcpy(ieee_infile, inname);
901 standard_extension(inname, outname, ".o");
904 static void ieee_write_file(void)
906 struct tm *thetime;
907 time_t reltime;
908 struct FileName *fn;
909 struct ieeeSection *seg;
910 struct ieeePublic *pub, *loc;
911 struct ieeeExternal *ext;
912 struct ieeeObjData *data;
913 struct ieeeFixupp *fix;
914 struct Array *arr;
915 int i;
916 const bool debuginfo = (dfmt == &ladsoft_debug_form);
919 * Write the module header
921 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile), ieee_infile);
924 * Write the NASM boast comment.
926 ieee_putascii("CO0,%02X%s.\n", strlen(nasm_comment), nasm_comment);
929 * write processor-specific information
931 ieee_putascii("AD8,4,L.\n");
934 * date and time
936 time(&reltime);
937 thetime = localtime(&reltime);
938 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
939 1900 + thetime->tm_year, thetime->tm_mon + 1,
940 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
941 thetime->tm_sec);
943 * if debugging, dump file names
945 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
946 ieee_putascii("C0105,%02X%s.\n", strlen(fn->name), fn->name);
949 ieee_putascii("CO101,07ENDHEAD.\n");
951 * the standard doesn't specify when to put checksums,
952 * we'll just do it periodically.
954 ieee_putcs(false);
957 * Write the section headers
959 seg = seghead;
960 if (!debuginfo && !strcmp(seg->name, "??LINE"))
961 seg = seg->next;
962 while (seg) {
963 char buf[256];
964 char attrib;
965 switch (seg->combine) {
966 case CMB_PUBLIC:
967 default:
968 attrib = 'C';
969 break;
970 case CMB_PRIVATE:
971 attrib = 'S';
972 break;
973 case CMB_COMMON:
974 attrib = 'M';
975 break;
977 ieee_unqualified_name(buf, seg->name);
978 if (seg->align >= SEG_ABS) {
979 ieee_putascii("ST%X,A,%02X%s.\n", seg->ieee_index,
980 strlen(buf), buf);
981 ieee_putascii("ASL%X,%lX.\n", seg->ieee_index,
982 (seg->align - SEG_ABS) * 16);
983 } else {
984 ieee_putascii("ST%X,%c,%02X%s.\n", seg->ieee_index, attrib,
985 strlen(buf), buf);
986 ieee_putascii("SA%X,%lX.\n", seg->ieee_index, seg->align);
987 ieee_putascii("ASS%X,%X.\n", seg->ieee_index,
988 seg->currentpos);
990 seg = seg->next;
993 * write the start address if there is one
995 if (ieee_entry_seg) {
996 for (seg = seghead; seg; seg = seg->next)
997 if (seg->index == ieee_entry_seg)
998 break;
999 if (!seg)
1000 nasm_panic(0, "Start address records are incorrect");
1001 else
1002 ieee_putascii("ASG,R%X,%lX,+.\n", seg->ieee_index,
1003 ieee_entry_ofs);
1006 ieee_putcs(false);
1008 * Write the publics
1010 i = 1;
1011 for (seg = seghead; seg; seg = seg->next) {
1012 for (pub = seg->pubhead; pub; pub = pub->next) {
1013 char buf[256];
1014 ieee_unqualified_name(buf, pub->name);
1015 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1016 if (pub->segment == -1)
1017 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1018 pub->offset);
1019 else
1020 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1021 pub->offset);
1022 if (debuginfo) {
1023 if (pub->type >= 0x100)
1024 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1025 else
1026 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1028 i++;
1031 pub = fpubhead;
1032 i = 1;
1033 while (pub) {
1034 char buf[256];
1035 ieee_unqualified_name(buf, pub->name);
1036 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1037 if (pub->segment == -1)
1038 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1039 pub->offset);
1040 else
1041 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1042 pub->offset);
1043 if (debuginfo) {
1044 if (pub->type >= 0x100)
1045 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1046 else
1047 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1049 i++;
1050 pub = pub->next;
1053 * Write the externals
1055 ext = exthead;
1056 i = 1;
1057 while (ext) {
1058 char buf[256];
1059 ieee_unqualified_name(buf, ext->name);
1060 ieee_putascii("NX%X,%02X%s.\n", i++, strlen(buf), buf);
1061 ext = ext->next;
1063 ieee_putcs(false);
1066 * IEEE doesn't have a standard pass break record
1067 * so use the ladsoft variant
1069 ieee_putascii("CO100,06ENDSYM.\n");
1072 * now put types
1074 i = ARRAY_BOT;
1075 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1076 ieee_putascii("TY%X,20,%X,%lX.\n", i++, arr->basetype,
1077 arr->size);
1080 * now put locals
1082 i = 1;
1083 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1084 for (loc = seg->lochead; loc; loc = loc->next) {
1085 char buf[256];
1086 ieee_unqualified_name(buf, loc->name);
1087 ieee_putascii("NN%X,%02X%s.\n", i, strlen(buf), buf);
1088 if (loc->segment == -1)
1089 ieee_putascii("ASN%X,R%X,%lX,+.\n", i, loc->index,
1090 loc->offset);
1091 else
1092 ieee_putascii("ASN%X,%lX,%lX,+.\n", i, loc->segment * 16,
1093 loc->offset);
1094 if (debuginfo) {
1095 if (loc->type >= 0x100)
1096 ieee_putascii("ATN%X,T%X.\n", i, loc->type - 0x100);
1097 else
1098 ieee_putascii("ATN%X,%X.\n", i, loc->type);
1100 i++;
1105 * put out section data;
1107 seg = seghead;
1108 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1109 seg = seg->next;
1110 while (seg) {
1111 if (seg->currentpos) {
1112 int32_t size, org = 0;
1113 data = seg->data;
1114 ieee_putascii("SB%X.\n", seg->ieee_index);
1115 fix = seg->fptr;
1116 while (fix) {
1117 size = HUNKSIZE - (org % HUNKSIZE);
1118 size =
1119 size + org >
1120 seg->currentpos ? seg->currentpos - org : size;
1121 size = fix->offset - org > size ? size : fix->offset - org;
1122 org = ieee_putld(org, org + size, data->data);
1123 if (org % HUNKSIZE == 0)
1124 data = data->next;
1125 if (org == fix->offset) {
1126 org += ieee_putlr(fix);
1127 fix = fix->next;
1130 while (org < seg->currentpos && data) {
1131 size =
1132 seg->currentpos - org >
1133 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1134 org = ieee_putld(org, org + size, data->data);
1135 data = data->next;
1137 ieee_putcs(false);
1140 seg = seg->next;
1143 * module end record
1145 ieee_putascii("ME.\n");
1148 static void ieee_write_byte(struct ieeeSection *seg, int data)
1150 int temp;
1151 if (!(temp = seg->currentpos++ % HUNKSIZE))
1152 ieee_data_new(seg);
1153 seg->datacurr->data[temp] = data;
1156 static void ieee_write_word(struct ieeeSection *seg, int data)
1158 ieee_write_byte(seg, data & 0xFF);
1159 ieee_write_byte(seg, (data >> 8) & 0xFF);
1162 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1164 ieee_write_byte(seg, data & 0xFF);
1165 ieee_write_byte(seg, (data >> 8) & 0xFF);
1166 ieee_write_byte(seg, (data >> 16) & 0xFF);
1167 ieee_write_byte(seg, (data >> 24) & 0xFF);
1169 static void ieee_putascii(char *format, ...)
1171 char buffer[256];
1172 int i, l;
1173 va_list ap;
1175 va_start(ap, format);
1176 vsnprintf(buffer, sizeof(buffer), format, ap);
1177 l = strlen(buffer);
1178 for (i = 0; i < l; i++)
1179 if ((uint8_t)buffer[i] > 31)
1180 checksum += buffer[i];
1181 va_end(ap);
1182 fputs(buffer, ofile);
1186 * put out a checksum record */
1187 static void ieee_putcs(int toclear)
1189 if (toclear) {
1190 ieee_putascii("CS.\n");
1191 } else {
1192 checksum += 'C';
1193 checksum += 'S';
1194 ieee_putascii("CS%02X.\n", checksum & 127);
1196 checksum = 0;
1199 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1201 int32_t val;
1202 if (start == end)
1203 return (start);
1204 val = start % HUNKSIZE;
1205 /* fill up multiple lines */
1206 while (end - start >= LDPERLINE) {
1207 int i;
1208 ieee_putascii("LD");
1209 for (i = 0; i < LDPERLINE; i++) {
1210 ieee_putascii("%02X", buf[val++]);
1211 start++;
1213 ieee_putascii(".\n");
1215 /* if no partial lines */
1216 if (start == end)
1217 return (start);
1218 /* make a partial line */
1219 ieee_putascii("LD");
1220 while (start < end) {
1221 ieee_putascii("%02X", buf[val++]);
1222 start++;
1224 ieee_putascii(".\n");
1225 return (start);
1227 static int32_t ieee_putlr(struct ieeeFixupp *p)
1230 * To deal with the vagaries of segmentation the LADsoft linker
1231 * defines two types of segments: absolute and virtual. Note that
1232 * 'absolute' in this context is a different thing from the IEEE
1233 * definition of an absolute segment type, which is also supported. If a
1234 * sement is linked in virtual mode the low limit (L-var) is
1235 * subtracted from each R,X, and P variable which appears in an
1236 * expression, so that we can have relative offsets. Meanwhile
1237 * in the ABSOLUTE mode this subtraction is not done and
1238 * so we can use absolute offsets from 0. In the LADsoft linker
1239 * this configuration is not done in the assemblker source but in
1240 * a source the linker reads. Generally this type of thing only
1241 * becomes an issue if real mode code is used. A pure 32-bit linker could
1242 * get away without defining the virtual mode...
1244 char buf[40];
1245 int32_t size = p->size;
1246 switch (p->ftype) {
1247 case FT_SEG:
1248 if (p->id1 < 0)
1249 sprintf(buf, "%"PRIX32"", -p->id1);
1250 else
1251 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1252 break;
1253 case FT_OFS:
1254 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1255 break;
1256 case FT_REL:
1257 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1258 break;
1260 case FT_WRT:
1261 if (p->id2 < 0)
1262 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1263 p->id2, -p->id1 * 16);
1264 else
1265 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1266 p->id2, p->id1);
1267 break;
1268 case FT_EXT:
1269 sprintf(buf, "X%"PRIX32"", p->id1);
1270 break;
1271 case FT_EXTREL:
1272 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1273 break;
1274 case FT_EXTSEG:
1275 /* We needed a non-ieee hack here.
1276 * We introduce the Y variable, which is the low
1277 * limit of the native segment the extern resides in
1279 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1280 break;
1281 case FT_EXTWRT:
1282 if (p->id2 < 0)
1283 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1284 -p->id1 * 16);
1285 else
1286 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1287 break;
1289 ieee_putascii("LR(%s,%"PRIX32").\n", buf, size);
1291 return (size);
1294 /* Dump all segment data (text and fixups )*/
1296 static void ieee_unqualified_name(char *dest, char *source)
1298 if (ieee_uppercase) {
1299 while (*source)
1300 *dest++ = toupper(*source++);
1301 *dest = 0;
1302 } else
1303 strcpy(dest, source);
1305 static void dbgls_init(void)
1307 int tempint;
1309 fnhead = NULL;
1310 fntail = &fnhead;
1311 arrindex = ARRAY_BOT;
1312 arrhead = NULL;
1313 arrtail = &arrhead;
1314 ieee_segment("??LINE", 2, &tempint);
1315 any_segs = false;
1317 static void dbgls_cleanup(void)
1319 struct ieeeSection *segtmp;
1320 while (fnhead) {
1321 struct FileName *fntemp = fnhead;
1322 fnhead = fnhead->next;
1323 nasm_free(fntemp->name);
1324 nasm_free(fntemp);
1326 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1327 while (segtmp->lochead) {
1328 struct ieeePublic *loctmp = segtmp->lochead;
1329 segtmp->lochead = loctmp->next;
1330 nasm_free(loctmp->name);
1331 nasm_free(loctmp);
1334 while (arrhead) {
1335 struct Array *arrtmp = arrhead;
1336 arrhead = arrhead->next;
1337 nasm_free(arrtmp);
1342 * because this routine is not bracketed in
1343 * the main program, this routine will be called even if there
1344 * is no request for debug info
1345 * so, we have to make sure the ??LINE segment is avaialbe
1346 * as the first segment when this debug format is selected
1348 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1350 struct FileName *fn;
1351 struct ieeeSection *seg;
1352 int i = 0;
1353 if (segto == NO_SEG)
1354 return;
1357 * If `any_segs' is still false, we must define a default
1358 * segment.
1360 if (!any_segs) {
1361 int tempint; /* ignored */
1362 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1363 nasm_panic(0, "strange segment conditions in OBJ driver");
1367 * Find the segment we are targetting.
1369 for (seg = seghead; seg; seg = seg->next)
1370 if (seg->index == segto)
1371 break;
1372 if (!seg)
1373 nasm_panic(0, "lineno directed to nonexistent segment?");
1375 for (fn = fnhead; fn; fn = fn->next) {
1376 if (!nasm_stricmp(lnfname, fn->name))
1377 break;
1378 i++;
1380 if (!fn) {
1381 fn = nasm_malloc(sizeof(*fn));
1382 fn->name = nasm_malloc(strlen(lnfname) + 1);
1383 fn->index = i;
1384 strcpy(fn->name, lnfname);
1385 fn->next = NULL;
1386 *fntail = fn;
1387 fntail = &fn->next;
1389 ieee_write_byte(seghead, fn->index);
1390 ieee_write_word(seghead, lineno);
1391 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1392 seg->currentpos);
1395 static void dbgls_deflabel(char *name, int32_t segment,
1396 int64_t offset, int is_global, char *special)
1398 struct ieeeSection *seg;
1400 /* Keep compiler from warning about special */
1401 (void)special;
1404 * Note: ..[^@] special symbols are filtered in labels.c
1408 * If it's a special-retry from pass two, discard it.
1410 if (is_global == 3)
1411 return;
1414 * Case (i):
1416 if (ieee_seg_needs_update)
1417 return;
1418 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
1419 return;
1421 if (segment >= SEG_ABS || segment == NO_SEG) {
1422 return;
1426 * If `any_segs' is still false, we might need to define a
1427 * default segment, if they're trying to declare a label in
1428 * `first_seg'. But the label should exist due to a prior
1429 * call to ieee_deflabel so we can skip that.
1432 for (seg = seghead; seg; seg = seg->next)
1433 if (seg->index == segment) {
1434 struct ieeePublic *loc;
1436 * Case (ii). Maybe MODPUB someday?
1438 if (!is_global) {
1439 last_defined = loc = nasm_malloc(sizeof(*loc));
1440 *seg->loctail = loc;
1441 seg->loctail = &loc->next;
1442 loc->next = NULL;
1443 loc->name = nasm_strdup(name);
1444 loc->offset = offset;
1445 loc->segment = -1;
1446 loc->index = seg->ieee_index;
1450 static void dbgls_typevalue(int32_t type)
1452 int elem = TYM_ELEMENTS(type);
1453 type = TYM_TYPE(type);
1455 if (!last_defined)
1456 return;
1458 switch (type) {
1459 case TY_BYTE:
1460 last_defined->type = 1; /* uint8_t */
1461 break;
1462 case TY_WORD:
1463 last_defined->type = 3; /* unsigned word */
1464 break;
1465 case TY_DWORD:
1466 last_defined->type = 5; /* unsigned dword */
1467 break;
1468 case TY_FLOAT:
1469 last_defined->type = 9; /* float */
1470 break;
1471 case TY_QWORD:
1472 last_defined->type = 10; /* qword */
1473 break;
1474 case TY_TBYTE:
1475 last_defined->type = 11; /* TBYTE */
1476 break;
1477 default:
1478 last_defined->type = 0x10; /* near label */
1479 break;
1482 if (elem > 1) {
1483 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
1484 int vtype = last_defined->type;
1485 arrtmp->size = elem;
1486 arrtmp->basetype = vtype;
1487 arrtmp->next = NULL;
1488 last_defined->type = arrindex++ + 0x100;
1489 *arrtail = arrtmp;
1490 arrtail = &(arrtmp->next);
1492 last_defined = NULL;
1494 static void dbgls_output(int output_type, void *param)
1496 (void)output_type;
1497 (void)param;
1499 static const struct dfmt ladsoft_debug_form = {
1500 "LADsoft Debug Records",
1501 "ladsoft",
1502 dbgls_init,
1503 dbgls_linnum,
1504 dbgls_deflabel,
1505 null_debug_directive,
1506 dbgls_typevalue,
1507 dbgls_output,
1508 dbgls_cleanup,
1510 static const struct dfmt * const ladsoft_debug_arr[3] = {
1511 &ladsoft_debug_form,
1512 &null_debug_form,
1513 NULL
1515 const struct ofmt of_ieee = {
1516 "IEEE-695 (LADsoft variant) object file format",
1517 "ieee",
1518 OFMT_TEXT,
1520 ladsoft_debug_arr,
1521 &ladsoft_debug_form,
1522 NULL,
1523 ieee_init,
1524 ieee_set_info,
1525 nasm_do_legacy_output,
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 */