1 /* ----------------------------------------------------------------------- *
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
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
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
74 #include <stdarg.h> /* Note: we need the ANSI version of stdarg.h */
80 #include "output/outform.h"
81 #include "output/outlib.h"
87 static char ieee_infile[FILENAME_MAX];
88 static int ieee_uppercase;
91 static ldfunc deflabel;
96 #define HUNKSIZE 1024 /* Size of the data hunk */
97 #define EXT_BLKSIZ 512
98 #define LDPERLINE 32 /* bytes per line in output */
103 struct LineNumber *next;
104 struct ieeeSection *segment;
109 static struct FileName {
110 struct FileName *next;
115 static struct Array {
119 } *arrhead, **arrtail;
121 static struct ieeePublic {
122 struct ieeePublic *next;
125 int32_t segment; /* only if it's far-absolute */
127 int type; /* for debug purposes */
128 } *fpubhead, **fpubtail, *last_defined;
130 static struct ieeeExternal {
131 struct ieeeExternal *next;
134 } *exthead, **exttail;
136 static int externals;
138 static struct ExtBack {
139 struct ExtBack *next;
140 int index[EXT_BLKSIZ];
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 */
151 int32_t align; /* can be SEG_ABS + absolute addr */
158 int32_t use32; /* is this segment 32-bit? */
159 struct ieeePublic *pubhead, **pubtail, *lochead, **loctail;
161 } *seghead, **segtail, *ieee_seg_needs_update;
164 struct ieeeObjData *next;
165 uint8_t data[HUNKSIZE];
169 struct ieeeFixupp *next;
187 static int32_t ieee_entry_seg, ieee_entry_ofs;
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 *);
210 static void ieee_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
218 fpubtail = &fpubhead;
224 seghead = ieee_seg_needs_update = NULL;
226 ieee_entry_seg = NO_SEG;
227 ieee_uppercase = false;
231 static int ieee_set_info(enum geninfo type, char **val)
242 static void ieee_cleanup(int debuginfo)
244 ieee_write_file(debuginfo);
245 of_ieee.current_dfmt->cleanup();
247 struct ieeeSection *segtmp = seghead;
248 seghead = seghead->next;
249 while (segtmp->pubhead) {
250 struct ieeePublic *pubtmp = segtmp->pubhead;
251 segtmp->pubhead = pubtmp->next;
254 while (segtmp->fptr) {
255 struct ieeeFixupp *fixtmp = segtmp->fptr;
256 segtmp->fptr = fixtmp->next;
259 while (segtmp->data) {
260 struct ieeeObjData *dattmp = segtmp->data;
261 segtmp->data = dattmp->next;
267 struct ieeePublic *pubtmp = fpubhead;
268 fpubhead = fpubhead->next;
272 struct ieeeExternal *exttmp = exthead;
273 exthead = exthead->next;
277 struct ExtBack *ebtmp = ebhead;
278 ebhead = ebhead->next;
284 * callback for labels
286 static void ieee_deflabel(char *name, int32_t segment,
287 int64_t offset, int is_global, char *special)
290 * We have three cases:
292 * (i) `segment' is a segment-base. If so, set the name field
293 * for the segment structure it refers to, and then
296 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
297 * Save the label position for later output of a PUBDEF record.
300 * (iii) `segment' is not one of our segments. Save the label
301 * position for later output of an EXTDEF.
303 struct ieeeExternal *ext;
305 struct ieeeSection *seg;
309 error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
312 * First check for the double-period, signifying something
315 if (name[0] == '.' && name[1] == '.') {
316 if (!strcmp(name, "..start")) {
317 ieee_entry_seg = segment;
318 ieee_entry_ofs = offset;
326 if (ieee_seg_needs_update) {
327 ieee_seg_needs_update->name = name;
330 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
336 if (segment >= SEG_ABS) {
338 * SEG_ABS subcase of (ii).
341 struct ieeePublic *pub;
343 pub = *fpubtail = nasm_malloc(sizeof(*pub));
344 fpubtail = &pub->next;
347 pub->offset = offset;
348 pub->segment = segment & ~SEG_ABS;
353 for (seg = seghead; seg && is_global; seg = seg->next)
354 if (seg->index == segment) {
355 struct ieeePublic *pub;
357 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
358 seg->pubtail = &pub->next;
361 pub->offset = offset;
362 pub->index = seg->ieee_index;
371 ext = *exttail = nasm_malloc(sizeof(*ext));
373 exttail = &ext->next;
376 ext->commonsize = offset;
382 eb = *ebtail = nasm_malloc(sizeof(*eb));
386 while (i > EXT_BLKSIZ) {
390 eb = *ebtail = nasm_malloc(sizeof(*eb));
396 eb->index[i] = externals++;
404 static void ieee_out(int32_t segto, const void *data,
405 enum out_type type, uint64_t size,
406 int32_t segment, int32_t wrt)
408 const uint8_t *ucdata;
410 struct ieeeSection *seg;
413 * handle absolute-assembly (structure definitions)
415 if (segto == NO_SEG) {
416 if (type != OUT_RESERVE)
417 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
423 * If `any_segs' is still false, we must define a default
427 int tempint; /* ignored */
428 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
429 error(ERR_PANIC, "strange segment conditions in IEEE driver");
433 * Find the segment we are targetting.
435 for (seg = seghead; seg; seg = seg->next)
436 if (seg->index == segto)
439 error(ERR_PANIC, "code directed to nonexistent segment?");
441 if (type == OUT_RAWDATA) {
444 ieee_write_byte(seg, *ucdata++);
445 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
446 type == OUT_REL4ADR) {
447 if (segment == NO_SEG && type != OUT_ADDRESS)
448 error(ERR_NONFATAL, "relative call to absolute address not"
449 " supported by IEEE format");
450 ldata = *(int64_t *)data;
451 if (type == OUT_REL2ADR)
453 if (type == OUT_REL4ADR)
455 ieee_write_fixup(segment, wrt, seg, size, type, ldata);
457 ieee_write_word(seg, ldata);
459 ieee_write_dword(seg, ldata);
460 } else if (type == OUT_RESERVE) {
462 ieee_write_byte(seg, 0);
466 static void ieee_data_new(struct ieeeSection *segto)
470 segto->data = segto->datacurr =
471 nasm_malloc(sizeof(*(segto->datacurr)));
473 segto->datacurr = segto->datacurr->next =
474 nasm_malloc(sizeof(*(segto->datacurr)));
475 segto->datacurr->next = NULL;
479 * this routine is unalduterated bloatware. I usually don't do this
480 * but I might as well see what it is like on a harmless program.
481 * If anyone wants to optimize this is a good canditate!
483 static void ieee_write_fixup(int32_t segment, int32_t wrt,
484 struct ieeeSection *segto, int size,
485 uint64_t realtype, int32_t offset)
487 struct ieeeSection *target;
490 /* Don't put a fixup for things NASM can calculate */
491 if (wrt == NO_SEG && segment == NO_SEG)
495 /* if it is a WRT offset */
500 s.id1 = -(wrt - SEG_ABS);
502 if (wrt % 2 && realtype != OUT_REL2ADR
503 && realtype != OUT_REL4ADR) {
506 for (target = seghead; target; target = target->next)
507 if (target->index == wrt)
510 s.id1 = target->ieee_index;
511 for (target = seghead; target; target = target->next)
512 if (target->index == segment)
516 s.id2 = target->ieee_index;
519 * Now we assume the segment field is being used
520 * to hold an extern index
522 int32_t i = segment / 2;
523 struct ExtBack *eb = ebhead;
524 while (i > EXT_BLKSIZ) {
531 /* if we have an extern decide the type and make a record
536 s.id2 = eb->index[i];
539 "Source of WRT must be an offset");
544 "unrecognised WRT value in ieee_write_fixup");
546 error(ERR_NONFATAL, "target of WRT must be a section ");
549 ieee_install_fixup(segto, &s);
552 /* Pure segment fixup ? */
553 if (segment != NO_SEG) {
556 if (segment >= SEG_ABS) {
557 /* absolute far segment fixup */
558 s.id1 = -(segment - ~SEG_ABS);
559 } else if (segment % 2) {
560 /* fixup to named segment */
562 for (target = seghead; target; target = target->next)
563 if (target->index == segment - 1)
566 s.id1 = target->ieee_index;
569 * Now we assume the segment field is being used
570 * to hold an extern index
572 int32_t i = segment / 2;
573 struct ExtBack *eb = ebhead;
574 while (i > EXT_BLKSIZ) {
581 /* if we have an extern decide the type and make a record
584 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
586 "Segment of a rel not supported in ieee_write_fixup");
588 /* If we want the segment */
591 s.id1 = eb->index[i];
595 /* If we get here the seg value doesn't make sense */
597 "unrecognised segment value in ieee_write_fixup");
601 /* Assume we are offsetting directly from a section
602 * So look up the target segment
604 for (target = seghead; target; target = target->next)
605 if (target->index == segment)
608 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
609 /* PC rel to a known offset */
610 s.id1 = target->ieee_index;
615 /* We were offsetting from a seg */
616 s.id1 = target->ieee_index;
623 * Now we assume the segment field is being used
624 * to hold an extern index
626 int32_t i = segment / 2;
627 struct ExtBack *eb = ebhead;
628 while (i > EXT_BLKSIZ) {
635 /* if we have an extern decide the type and make a record
638 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
641 s.id1 = eb->index[i];
643 /* else we want the external offset */
646 s.id1 = eb->index[i];
650 /* If we get here the seg value doesn't make sense */
652 "unrecognised segment value in ieee_write_fixup");
655 if (size != 2 && s.ftype == FT_SEG)
656 error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
657 " segment base references");
659 ieee_install_fixup(segto, &s);
662 /* should never get here */
664 static void ieee_install_fixup(struct ieeeSection *seg,
665 struct ieeeFixupp *fix)
667 struct ieeeFixupp *f;
668 f = nasm_malloc(sizeof(struct ieeeFixupp));
669 memcpy(f, fix, sizeof(struct ieeeFixupp));
670 f->offset = seg->currentpos;
671 seg->currentpos += fix->size;
674 seg->flptr = seg->flptr->next = f;
676 seg->fptr = seg->flptr = f;
683 static int32_t ieee_segment(char *name, int pass, int *bits)
686 * We call the label manager here to define a name for the new
687 * segment, and when our _own_ label-definition stub gets
688 * called in return, it should register the new segment name
689 * using the pointer it gets passed. That way we save memory,
690 * by sponging off the label manager.
696 return seghead->index;
698 struct ieeeSection *seg;
704 * Look for segment attributes.
708 name++; /* hack, but a documented one */
710 while (*p && !nasm_isspace(*p))
714 while (*p && nasm_isspace(*p))
718 while (*p && !nasm_isspace(*p))
722 while (*p && nasm_isspace(*p))
730 for (seg = seghead; seg; seg = seg->next) {
732 if (!strcmp(seg->name, name)) {
733 if (attrs > 0 && pass == 1)
734 error(ERR_WARNING, "segment attributes specified on"
735 " redeclaration of segment: ignoring");
744 *segtail = seg = nasm_malloc(sizeof(*seg));
746 segtail = &seg->next;
747 seg->index = seg_alloc();
748 seg->ieee_index = ieee_idx;
752 seg->align = 1; /* default */
753 seg->use32 = *bits == 32; /* default to user spec */
754 seg->combine = CMB_PUBLIC; /* default */
756 seg->pubtail = &seg->pubhead;
760 seg->loctail = &seg->lochead;
763 * Process the segment attributes.
772 * `p' contains a segment attribute.
774 if (!nasm_stricmp(p, "private"))
775 seg->combine = CMB_PRIVATE;
776 else if (!nasm_stricmp(p, "public"))
777 seg->combine = CMB_PUBLIC;
778 else if (!nasm_stricmp(p, "common"))
779 seg->combine = CMB_COMMON;
780 else if (!nasm_stricmp(p, "use16"))
782 else if (!nasm_stricmp(p, "use32"))
784 else if (!nasm_strnicmp(p, "align=", 6)) {
785 seg->align = readnum(p + 6, &rn_error);
790 error(ERR_NONFATAL, "segment alignment should be"
793 switch ((int)seg->align) {
805 error(ERR_NONFATAL, "invalid alignment value %d",
810 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
811 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
813 error(ERR_NONFATAL, "argument to `absolute' segment"
814 " attribute should be numeric");
818 ieee_seg_needs_update = seg;
819 if (seg->align >= SEG_ABS)
820 deflabel(name, NO_SEG, seg->align - SEG_ABS,
821 NULL, false, false, &of_ieee, error);
823 deflabel(name, seg->index + 1, 0L,
824 NULL, false, false, &of_ieee, error);
825 ieee_seg_needs_update = NULL;
836 * directives supported
838 static int ieee_directive(enum directives directive, char *value, int pass)
846 ieee_uppercase = true;
855 * Return segment data
857 static int32_t ieee_segbase(int32_t segment)
859 struct ieeeSection *seg;
862 * Find the segment in our list.
864 for (seg = seghead; seg; seg = seg->next)
865 if (seg->index == segment - 1)
869 return segment; /* not one of ours - leave it alone */
871 if (seg->align >= SEG_ABS)
872 return seg->align; /* absolute segment */
874 return segment; /* no special treatment */
880 static void ieee_filename(char *inname, char *outname, efunc error)
882 strcpy(ieee_infile, inname);
883 standard_extension(inname, outname, ".o", error);
886 static void ieee_write_file(int debuginfo)
891 struct ieeeSection *seg;
892 struct ieeePublic *pub, *loc;
893 struct ieeeExternal *ext;
894 struct ieeeObjData *data;
895 struct ieeeFixupp *fix;
900 * Write the module header
902 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile), ieee_infile);
905 * Write the NASM boast comment.
907 ieee_putascii("CO0,%02X%s.\n", strlen(nasm_comment), nasm_comment);
910 * write processor-specific information
912 ieee_putascii("AD8,4,L.\n");
918 thetime = localtime(&reltime);
919 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
920 1900 + thetime->tm_year, thetime->tm_mon + 1,
921 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
924 * if debugging, dump file names
926 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
927 ieee_putascii("C0105,%02X%s.\n", strlen(fn->name), fn->name);
930 ieee_putascii("CO101,07ENDHEAD.\n");
932 * the standard doesn't specify when to put checksums,
933 * we'll just do it periodically.
938 * Write the section headers
941 if (!debuginfo && !strcmp(seg->name, "??LINE"))
946 switch (seg->combine) {
958 ieee_unqualified_name(buf, seg->name);
959 if (seg->align >= SEG_ABS) {
960 ieee_putascii("ST%X,A,%02X%s.\n", seg->ieee_index,
962 ieee_putascii("ASL%X,%lX.\n", seg->ieee_index,
963 (seg->align - SEG_ABS) * 16);
965 ieee_putascii("ST%X,%c,%02X%s.\n", seg->ieee_index, attrib,
967 ieee_putascii("SA%X,%lX.\n", seg->ieee_index, seg->align);
968 ieee_putascii("ASS%X,%X.\n", seg->ieee_index,
974 * write the start address if there is one
976 if (ieee_entry_seg) {
977 for (seg = seghead; seg; seg = seg->next)
978 if (seg->index == ieee_entry_seg)
981 error(ERR_PANIC, "Start address records are incorrect");
983 ieee_putascii("ASG,R%X,%lX,+.\n", seg->ieee_index,
992 for (seg = seghead; seg; seg = seg->next) {
993 for (pub = seg->pubhead; pub; pub = pub->next) {
995 ieee_unqualified_name(buf, pub->name);
996 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
997 if (pub->segment == -1)
998 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1001 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1004 if (pub->type >= 0x100)
1005 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1007 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1016 ieee_unqualified_name(buf, pub->name);
1017 ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf);
1018 if (pub->segment == -1)
1019 ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index,
1022 ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16,
1025 if (pub->type >= 0x100)
1026 ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100);
1028 ieee_putascii("ATI%X,%X.\n", i, pub->type);
1034 * Write the externals
1040 ieee_unqualified_name(buf, ext->name);
1041 ieee_putascii("NX%X,%02X%s.\n", i++, strlen(buf), buf);
1047 * IEEE doesn't have a standard pass break record
1048 * so use the ladsoft variant
1050 ieee_putascii("CO100,06ENDSYM.\n");
1056 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1057 ieee_putascii("TY%X,20,%X,%lX.\n", i++, arr->basetype,
1064 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1065 for (loc = seg->lochead; loc; loc = loc->next) {
1067 ieee_unqualified_name(buf, loc->name);
1068 ieee_putascii("NN%X,%02X%s.\n", i, strlen(buf), buf);
1069 if (loc->segment == -1)
1070 ieee_putascii("ASN%X,R%X,%lX,+.\n", i, loc->index,
1073 ieee_putascii("ASN%X,%lX,%lX,+.\n", i, loc->segment * 16,
1076 if (loc->type >= 0x100)
1077 ieee_putascii("ATN%X,T%X.\n", i, loc->type - 0x100);
1079 ieee_putascii("ATN%X,%X.\n", i, loc->type);
1086 * put out section data;
1089 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1092 if (seg->currentpos) {
1093 int32_t size, org = 0;
1095 ieee_putascii("SB%X.\n", seg->ieee_index);
1098 size = HUNKSIZE - (org % HUNKSIZE);
1101 seg->currentpos ? seg->currentpos - org : size;
1102 size = fix->offset - org > size ? size : fix->offset - org;
1103 org = ieee_putld(org, org + size, data->data);
1104 if (org % HUNKSIZE == 0)
1106 if (org == fix->offset) {
1107 org += ieee_putlr(fix);
1111 while (org < seg->currentpos && data) {
1113 seg->currentpos - org >
1114 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1115 org = ieee_putld(org, org + size, data->data);
1126 ieee_putascii("ME.\n");
1129 static void ieee_write_byte(struct ieeeSection *seg, int data)
1132 if (!(temp = seg->currentpos++ % HUNKSIZE))
1134 seg->datacurr->data[temp] = data;
1137 static void ieee_write_word(struct ieeeSection *seg, int data)
1139 ieee_write_byte(seg, data & 0xFF);
1140 ieee_write_byte(seg, (data >> 8) & 0xFF);
1143 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1145 ieee_write_byte(seg, data & 0xFF);
1146 ieee_write_byte(seg, (data >> 8) & 0xFF);
1147 ieee_write_byte(seg, (data >> 16) & 0xFF);
1148 ieee_write_byte(seg, (data >> 24) & 0xFF);
1150 static void ieee_putascii(char *format, ...)
1156 va_start(ap, format);
1157 vsnprintf(buffer, sizeof(buffer), format, ap);
1159 for (i = 0; i < l; i++)
1160 if ((uint8_t)buffer[i] > 31)
1161 checksum += buffer[i];
1163 fprintf(ofp, buffer);
1167 * put out a checksum record */
1168 static void ieee_putcs(int toclear)
1171 ieee_putascii("CS.\n");
1175 ieee_putascii("CS%02X.\n", checksum & 127);
1180 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1185 val = start % HUNKSIZE;
1186 /* fill up multiple lines */
1187 while (end - start >= LDPERLINE) {
1189 ieee_putascii("LD");
1190 for (i = 0; i < LDPERLINE; i++) {
1191 ieee_putascii("%02X", buf[val++]);
1194 ieee_putascii(".\n");
1196 /* if no partial lines */
1199 /* make a partial line */
1200 ieee_putascii("LD");
1201 while (start < end) {
1202 ieee_putascii("%02X", buf[val++]);
1205 ieee_putascii(".\n");
1208 static int32_t ieee_putlr(struct ieeeFixupp *p)
1211 * To deal with the vagaries of segmentation the LADsoft linker
1212 * defines two types of segments: absolute and virtual. Note that
1213 * 'absolute' in this context is a different thing from the IEEE
1214 * definition of an absolute segment type, which is also supported. If a
1215 * sement is linked in virtual mode the low limit (L-var) is
1216 * subtracted from each R,X, and P variable which appears in an
1217 * expression, so that we can have relative offsets. Meanwhile
1218 * in the ABSOLUTE mode this subtraction is not done and
1219 * so we can use absolute offsets from 0. In the LADsoft linker
1220 * this configuration is not done in the assemblker source but in
1221 * a source the linker reads. Generally this type of thing only
1222 * becomes an issue if real mode code is used. A pure 32-bit linker could
1223 * get away without defining the virtual mode...
1226 int32_t size = p->size;
1230 sprintf(buf, "%"PRIX32"", -p->id1);
1232 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1235 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1238 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1243 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1244 p->id2, -p->id1 * 16);
1246 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1250 sprintf(buf, "X%"PRIX32"", p->id1);
1253 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1256 /* We needed a non-ieee hack here.
1257 * We introduce the Y variable, which is the low
1258 * limit of the native segment the extern resides in
1260 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1264 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1267 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1270 ieee_putascii("LR(%s,%"PRIX32").\n", buf, size);
1275 /* Dump all segment data (text and fixups )*/
1277 static void ieee_unqualified_name(char *dest, char *source)
1279 if (ieee_uppercase) {
1281 *dest++ = toupper(*source++);
1284 strcpy(dest, source);
1286 void dbgls_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1296 arrindex = ARRAY_BOT;
1299 ieee_segment("??LINE", 2, &tempint);
1302 static void dbgls_cleanup(void)
1304 struct ieeeSection *segtmp;
1306 struct FileName *fntemp = fnhead;
1307 fnhead = fnhead->next;
1308 nasm_free(fntemp->name);
1311 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1312 while (segtmp->lochead) {
1313 struct ieeePublic *loctmp = segtmp->lochead;
1314 segtmp->lochead = loctmp->next;
1315 nasm_free(loctmp->name);
1320 struct Array *arrtmp = arrhead;
1321 arrhead = arrhead->next;
1327 * because this routine is not bracketed in
1328 * the main program, this routine will be called even if there
1329 * is no request for debug info
1330 * so, we have to make sure the ??LINE segment is avaialbe
1331 * as the first segment when this debug format is selected
1333 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1335 struct FileName *fn;
1336 struct ieeeSection *seg;
1338 if (segto == NO_SEG)
1342 * If `any_segs' is still false, we must define a default
1346 int tempint; /* ignored */
1347 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1348 error(ERR_PANIC, "strange segment conditions in OBJ driver");
1352 * Find the segment we are targetting.
1354 for (seg = seghead; seg; seg = seg->next)
1355 if (seg->index == segto)
1358 error(ERR_PANIC, "lineno directed to nonexistent segment?");
1360 for (fn = fnhead; fn; fn = fn->next) {
1361 if (!nasm_stricmp(lnfname, fn->name))
1366 fn = nasm_malloc(sizeof(*fn));
1367 fn->name = nasm_malloc(strlen(lnfname) + 1);
1369 strcpy(fn->name, lnfname);
1374 ieee_write_byte(seghead, fn->index);
1375 ieee_write_word(seghead, lineno);
1376 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1380 static void dbgls_deflabel(char *name, int32_t segment,
1381 int64_t offset, int is_global, char *special)
1383 struct ieeeSection *seg;
1385 /* Keep compiler from warning about special */
1389 * If it's a special-retry from pass two, discard it.
1395 * First check for the double-period, signifying something
1398 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
1405 if (ieee_seg_needs_update)
1407 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
1410 if (segment >= SEG_ABS || segment == NO_SEG) {
1415 * If `any_segs' is still false, we might need to define a
1416 * default segment, if they're trying to declare a label in
1417 * `first_seg'. But the label should exist due to a prior
1418 * call to ieee_deflabel so we can skip that.
1421 for (seg = seghead; seg; seg = seg->next)
1422 if (seg->index == segment) {
1423 struct ieeePublic *loc;
1425 * Case (ii). Maybe MODPUB someday?
1428 last_defined = loc = nasm_malloc(sizeof(*loc));
1429 *seg->loctail = loc;
1430 seg->loctail = &loc->next;
1432 loc->name = nasm_strdup(name);
1433 loc->offset = offset;
1435 loc->index = seg->ieee_index;
1439 static void dbgls_typevalue(int32_t type)
1441 int elem = TYM_ELEMENTS(type);
1442 type = TYM_TYPE(type);
1449 last_defined->type = 1; /* uint8_t */
1452 last_defined->type = 3; /* unsigned word */
1455 last_defined->type = 5; /* unsigned dword */
1458 last_defined->type = 9; /* float */
1461 last_defined->type = 10; /* qword */
1464 last_defined->type = 11; /* TBYTE */
1467 last_defined->type = 0x10; /* near label */
1472 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
1473 int vtype = last_defined->type;
1474 arrtmp->size = elem;
1475 arrtmp->basetype = vtype;
1476 arrtmp->next = NULL;
1477 last_defined->type = arrindex++ + 0x100;
1479 arrtail = &(arrtmp->next);
1481 last_defined = NULL;
1483 static void dbgls_output(int output_type, void *param)
1488 static struct dfmt ladsoft_debug_form = {
1489 "LADsoft Debug Records",
1499 static struct dfmt *ladsoft_debug_arr[3] = {
1500 &ladsoft_debug_form,
1504 struct ofmt of_ieee = {
1505 "IEEE-695 (LADsoft variant) object file format",
1509 &ladsoft_debug_form,
1522 #endif /* OF_IEEE */