1 /* outieee.c output routines for the Netwide Assembler to produce
2 * IEEE-std object files
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
10 /* notes: I have tried to make this correspond to the IEEE version
11 * of the standard, specifically the primary ASCII version. It should
12 * be trivial to create the binary version given this source (which is
13 * one of MANY things that have to be done to make this correspond to
14 * the hp-microtek version of the standard).
16 * 16-bit support is assumed to use 24-bit addresses
17 * The linker can sort out segmentation-specific stuff
18 * if it keeps track of externals
19 * in terms of being relative to section bases
21 * A non-standard variable type, the 'Yn' variable, has been introduced.
22 * Basically it is a reference to extern 'n'- denoting the low limit
23 * (L-variable) of the section that extern 'n' is defined in. Like the
24 * x variable, there may be no explicit assignment to it, it is derived
25 * from the public definition corresponding to the extern name. This
26 * is required because the one thing the mufom guys forgot to do well was
27 * take into account segmented architectures.
29 * I use comment classes for various things and these are undefined by
32 * Debug info should be considered totally non-standard (local labels are
33 * standard but linenum records are not covered by the standard.
34 * Type defs have the standard format but absolute meanings for ordinal
35 * types are not covered by the standard.)
37 * David Lindauer, LADsoft
45 #include <stdarg.h> /* Note: we need the ANSI version of stdarg.h */
57 static char ieee_infile[FILENAME_MAX];
58 static int ieee_uppercase;
61 static ldfunc deflabel;
66 #define HUNKSIZE 1024 /* Size of the data hunk */
67 #define EXT_BLKSIZ 512
68 #define LDPERLINE 32 /* bytes per line in output */
73 struct LineNumber *next;
74 struct ieeeSection *segment;
79 static struct FileName {
80 struct FileName *next;
89 } *arrhead, **arrtail;
91 static struct ieeePublic {
92 struct ieeePublic *next;
95 int32_t segment; /* only if it's far-absolute */
97 int type; /* for debug purposes */
98 } *fpubhead, **fpubtail, *last_defined;
100 static struct ieeeExternal {
101 struct ieeeExternal *next;
104 } *exthead, **exttail;
106 static int externals;
108 static struct ExtBack {
109 struct ExtBack *next;
110 int index[EXT_BLKSIZ];
113 /* NOTE: the first segment MUST be the lineno segment */
114 static struct ieeeSection {
115 struct ieeeObjData *data, *datacurr;
116 struct ieeeSection *next;
117 struct ieeeFixupp *fptr, *flptr;
118 int32_t index; /* the NASM segment id */
119 int32_t ieee_index; /* the OBJ-file segment index */
121 int32_t align; /* can be SEG_ABS + absolute addr */
128 int32_t use32; /* is this segment 32-bit? */
129 struct ieeePublic *pubhead, **pubtail, *lochead, **loctail;
131 } *seghead, **segtail, *ieee_seg_needs_update;
134 struct ieeeObjData *next;
135 uint8_t data[HUNKSIZE];
139 struct ieeeFixupp *next;
157 static int32_t ieee_entry_seg, ieee_entry_ofs;
160 extern struct ofmt of_ieee;
162 static void ieee_data_new(struct ieeeSection *);
163 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection *,
164 int, uint32_t, int32_t);
165 static void ieee_install_fixup(struct ieeeSection *, struct ieeeFixupp *);
166 static int32_t ieee_segment(char *, int, int *);
167 static void ieee_write_file(int debuginfo);
168 static void ieee_write_byte(struct ieeeSection *, int);
169 static void ieee_write_word(struct ieeeSection *, int);
170 static void ieee_write_dword(struct ieeeSection *, int32_t);
171 static void ieee_putascii(char *, ...);
172 static void ieee_putcs(int);
173 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
174 static int32_t ieee_putlr(struct ieeeFixupp *);
175 static void ieee_unqualified_name(char *, char *);
180 static void ieee_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
188 fpubtail = &fpubhead;
194 seghead = ieee_seg_needs_update = NULL;
196 ieee_entry_seg = NO_SEG;
197 ieee_uppercase = false;
199 of_ieee.current_dfmt->init(&of_ieee, NULL, fp, errfunc);
201 static int ieee_set_info(enum geninfo type, char **val)
212 static void ieee_cleanup(int debuginfo)
214 ieee_write_file(debuginfo);
215 of_ieee.current_dfmt->cleanup();
218 struct ieeeSection *segtmp = seghead;
219 seghead = seghead->next;
220 while (segtmp->pubhead) {
221 struct ieeePublic *pubtmp = segtmp->pubhead;
222 segtmp->pubhead = pubtmp->next;
225 while (segtmp->fptr) {
226 struct ieeeFixupp *fixtmp = segtmp->fptr;
227 segtmp->fptr = fixtmp->next;
230 while (segtmp->data) {
231 struct ieeeObjData *dattmp = segtmp->data;
232 segtmp->data = dattmp->next;
238 struct ieeePublic *pubtmp = fpubhead;
239 fpubhead = fpubhead->next;
243 struct ieeeExternal *exttmp = exthead;
244 exthead = exthead->next;
248 struct ExtBack *ebtmp = ebhead;
249 ebhead = ebhead->next;
255 * callback for labels
257 static void ieee_deflabel(char *name, int32_t segment,
258 int32_t offset, int is_global, char *special)
261 * We have three cases:
263 * (i) `segment' is a segment-base. If so, set the name field
264 * for the segment structure it refers to, and then
267 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
268 * Save the label position for later output of a PUBDEF record.
271 * (iii) `segment' is not one of our segments. Save the label
272 * position for later output of an EXTDEF.
274 struct ieeeExternal *ext;
276 struct ieeeSection *seg;
280 error(ERR_NONFATAL, "unrecognised symbol type `%s'", special);
283 * First check for the double-period, signifying something
286 if (name[0] == '.' && name[1] == '.') {
287 if (!strcmp(name, "..start")) {
288 ieee_entry_seg = segment;
289 ieee_entry_ofs = offset;
297 if (ieee_seg_needs_update) {
298 ieee_seg_needs_update->name = name;
301 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
307 if (segment >= SEG_ABS) {
309 * SEG_ABS subcase of (ii).
312 struct ieeePublic *pub;
314 pub = *fpubtail = nasm_malloc(sizeof(*pub));
315 fpubtail = &pub->next;
318 pub->offset = offset;
319 pub->segment = segment & ~SEG_ABS;
324 for (seg = seghead; seg && is_global; seg = seg->next)
325 if (seg->index == segment) {
326 struct ieeePublic *pub;
328 last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub));
329 seg->pubtail = &pub->next;
332 pub->offset = offset;
333 pub->index = seg->ieee_index;
342 ext = *exttail = nasm_malloc(sizeof(*ext));
344 exttail = &ext->next;
347 ext->commonsize = offset;
353 eb = *ebtail = nasm_malloc(sizeof(*eb));
357 while (i > EXT_BLKSIZ) {
361 eb = *ebtail = nasm_malloc(sizeof(*eb));
367 eb->index[i] = externals++;
375 static void ieee_out(int32_t segto, const void *data, uint32_t type,
376 int32_t segment, int32_t wrt)
378 uint32_t size, realtype;
379 const uint8_t *ucdata;
381 struct ieeeSection *seg;
384 * handle absolute-assembly (structure definitions)
386 if (segto == NO_SEG) {
387 if ((type & OUT_TYPMASK) != OUT_RESERVE)
388 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
394 * If `any_segs' is still false, we must define a default
398 int tempint; /* ignored */
399 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
400 error(ERR_PANIC, "strange segment conditions in IEEE driver");
404 * Find the segment we are targetting.
406 for (seg = seghead; seg; seg = seg->next)
407 if (seg->index == segto)
410 error(ERR_PANIC, "code directed to nonexistent segment?");
412 size = type & OUT_SIZMASK;
413 realtype = type & OUT_TYPMASK;
414 if (realtype == OUT_RAWDATA) {
417 ieee_write_byte(seg, *ucdata++);
418 } else if (realtype == OUT_ADDRESS || realtype == OUT_REL2ADR ||
419 realtype == OUT_REL4ADR) {
420 if (segment == NO_SEG && realtype != OUT_ADDRESS)
421 error(ERR_NONFATAL, "relative call to absolute address not"
422 " supported by IEEE format");
423 ldata = *(int32_t *)data;
424 if (realtype == OUT_REL2ADR)
426 if (realtype == OUT_REL4ADR)
428 ieee_write_fixup(segment, wrt, seg, size, realtype, ldata);
430 ieee_write_word(seg, ldata);
432 ieee_write_dword(seg, ldata);
433 } else if (realtype == OUT_RESERVE) {
435 ieee_write_byte(seg, 0);
439 static void ieee_data_new(struct ieeeSection *segto)
443 segto->data = segto->datacurr =
444 nasm_malloc(sizeof(*(segto->datacurr)));
446 segto->datacurr = segto->datacurr->next =
447 nasm_malloc(sizeof(*(segto->datacurr)));
448 segto->datacurr->next = NULL;
452 * this routine is unalduterated bloatware. I usually don't do this
453 * but I might as well see what it is like on a harmless program.
454 * If anyone wants to optimize this is a good canditate!
456 static void ieee_write_fixup(int32_t segment, int32_t wrt,
457 struct ieeeSection *segto, int size,
458 uint32_t realtype, int32_t offset)
460 struct ieeeSection *target;
463 /* Don't put a fixup for things NASM can calculate */
464 if (wrt == NO_SEG && segment == NO_SEG)
468 /* if it is a WRT offset */
473 s.id1 = -(wrt - SEG_ABS);
475 if (wrt % 2 && realtype != OUT_REL2ADR
476 && realtype != OUT_REL4ADR) {
479 for (target = seghead; target; target = target->next)
480 if (target->index == wrt)
483 s.id1 = target->ieee_index;
484 for (target = seghead; target; target = target->next)
485 if (target->index == segment)
489 s.id2 = target->ieee_index;
492 * Now we assume the segment field is being used
493 * to hold an extern index
495 int32_t i = segment / 2;
496 struct ExtBack *eb = ebhead;
497 while (i > EXT_BLKSIZ) {
504 /* if we have an extern decide the type and make a record
509 s.id2 = eb->index[i];
512 "Source of WRT must be an offset");
517 "unrecognised WRT value in ieee_write_fixup");
519 error(ERR_NONFATAL, "target of WRT must be a section ");
522 ieee_install_fixup(segto, &s);
525 /* Pure segment fixup ? */
526 if (segment != NO_SEG) {
529 if (segment >= SEG_ABS) {
530 /* absolute far segment fixup */
531 s.id1 = -(segment - ~SEG_ABS);
532 } else if (segment % 2) {
533 /* fixup to named segment */
535 for (target = seghead; target; target = target->next)
536 if (target->index == segment - 1)
539 s.id1 = target->ieee_index;
542 * Now we assume the segment field is being used
543 * to hold an extern index
545 int32_t i = segment / 2;
546 struct ExtBack *eb = ebhead;
547 while (i > EXT_BLKSIZ) {
554 /* if we have an extern decide the type and make a record
557 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
559 "Segment of a rel not supported in ieee_write_fixup");
561 /* If we want the segment */
564 s.id1 = eb->index[i];
568 /* If we get here the seg value doesn't make sense */
570 "unrecognised segment value in ieee_write_fixup");
574 /* Assume we are offsetting directly from a section
575 * So look up the target segment
577 for (target = seghead; target; target = target->next)
578 if (target->index == segment)
581 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
582 /* PC rel to a known offset */
583 s.id1 = target->ieee_index;
588 /* We were offsetting from a seg */
589 s.id1 = target->ieee_index;
596 * Now we assume the segment field is being used
597 * to hold an extern index
599 int32_t i = segment / 2;
600 struct ExtBack *eb = ebhead;
601 while (i > EXT_BLKSIZ) {
608 /* if we have an extern decide the type and make a record
611 if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) {
614 s.id1 = eb->index[i];
616 /* else we want the external offset */
619 s.id1 = eb->index[i];
623 /* If we get here the seg value doesn't make sense */
625 "unrecognised segment value in ieee_write_fixup");
628 if (size != 2 && s.ftype == FT_SEG)
629 error(ERR_NONFATAL, "IEEE format can only handle 2-byte"
630 " segment base references");
632 ieee_install_fixup(segto, &s);
635 /* should never get here */
637 static void ieee_install_fixup(struct ieeeSection *seg,
638 struct ieeeFixupp *fix)
640 struct ieeeFixupp *f;
641 f = nasm_malloc(sizeof(struct ieeeFixupp));
642 memcpy(f, fix, sizeof(struct ieeeFixupp));
643 f->offset = seg->currentpos;
644 seg->currentpos += fix->size;
647 seg->flptr = seg->flptr->next = f;
649 seg->fptr = seg->flptr = f;
656 static int32_t ieee_segment(char *name, int pass, int *bits)
659 * We call the label manager here to define a name for the new
660 * segment, and when our _own_ label-definition stub gets
661 * called in return, it should register the new segment name
662 * using the pointer it gets passed. That way we save memory,
663 * by sponging off the label manager.
669 return seghead->index;
671 struct ieeeSection *seg;
677 * Look for segment attributes.
681 name++; /* hack, but a documented one */
683 while (*p && !isspace(*p))
687 while (*p && isspace(*p))
691 while (*p && !isspace(*p))
695 while (*p && isspace(*p))
703 for (seg = seghead; seg; seg = seg->next) {
705 if (!strcmp(seg->name, name)) {
706 if (attrs > 0 && pass == 1)
707 error(ERR_WARNING, "segment attributes specified on"
708 " redeclaration of segment: ignoring");
717 *segtail = seg = nasm_malloc(sizeof(*seg));
719 segtail = &seg->next;
720 seg->index = seg_alloc();
721 seg->ieee_index = ieee_idx;
725 seg->align = 1; /* default */
726 seg->use32 = *bits == 32; /* default to user spec */
727 seg->combine = CMB_PUBLIC; /* default */
729 seg->pubtail = &seg->pubhead;
733 seg->loctail = &seg->lochead;
736 * Process the segment attributes.
745 * `p' contains a segment attribute.
747 if (!nasm_stricmp(p, "private"))
748 seg->combine = CMB_PRIVATE;
749 else if (!nasm_stricmp(p, "public"))
750 seg->combine = CMB_PUBLIC;
751 else if (!nasm_stricmp(p, "common"))
752 seg->combine = CMB_COMMON;
753 else if (!nasm_stricmp(p, "use16"))
755 else if (!nasm_stricmp(p, "use32"))
757 else if (!nasm_strnicmp(p, "align=", 6)) {
758 seg->align = readnum(p + 6, &rn_error);
763 error(ERR_NONFATAL, "segment alignment should be"
766 switch ((int)seg->align) {
778 error(ERR_NONFATAL, "invalid alignment value %d",
783 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
784 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
786 error(ERR_NONFATAL, "argument to `absolute' segment"
787 " attribute should be numeric");
791 ieee_seg_needs_update = seg;
792 if (seg->align >= SEG_ABS)
793 deflabel(name, NO_SEG, seg->align - SEG_ABS,
794 NULL, false, false, &of_ieee, error);
796 deflabel(name, seg->index + 1, 0L,
797 NULL, false, false, &of_ieee, error);
798 ieee_seg_needs_update = NULL;
809 * directives supported
811 static int ieee_directive(char *directive, char *value, int pass)
816 if (!strcmp(directive, "uppercase")) {
817 ieee_uppercase = true;
824 * Return segment data
826 static int32_t ieee_segbase(int32_t segment)
828 struct ieeeSection *seg;
831 * Find the segment in our list.
833 for (seg = seghead; seg; seg = seg->next)
834 if (seg->index == segment - 1)
838 return segment; /* not one of ours - leave it alone */
840 if (seg->align >= SEG_ABS)
841 return seg->align; /* absolute segment */
843 return segment; /* no special treatment */
849 static void ieee_filename(char *inname, char *outname, efunc error)
851 strcpy(ieee_infile, inname);
852 standard_extension(inname, outname, ".o", error);
855 static void ieee_write_file(int debuginfo)
860 struct ieeeSection *seg;
861 struct ieeePublic *pub, *loc;
862 struct ieeeExternal *ext;
863 struct ieeeObjData *data;
864 struct ieeeFixupp *fix;
866 static char boast[] = "The Netwide Assembler " NASM_VER;
870 * Write the module header
872 ieee_putascii("MBFNASM,%02X%s.\r\n", strlen(ieee_infile), ieee_infile);
875 * Write the NASM boast comment.
877 ieee_putascii("CO0,%02X%s.\r\n", strlen(boast), boast);
880 * write processor-specific information
882 ieee_putascii("AD8,4,L.\r\n");
888 thetime = localtime(&reltime);
889 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\r\n",
890 1900 + thetime->tm_year, thetime->tm_mon + 1,
891 thetime->tm_mday, thetime->tm_hour, thetime->tm_min,
894 * if debugging, dump file names
896 for (fn = fnhead; fn && debuginfo; fn = fn->next) {
897 ieee_putascii("C0105,%02X%s.\r\n", strlen(fn->name), fn->name);
900 ieee_putascii("CO101,07ENDHEAD.\r\n");
902 * the standard doesn't specify when to put checksums,
903 * we'll just do it periodically.
908 * Write the section headers
911 if (!debuginfo && !strcmp(seg->name, "??LINE"))
916 switch (seg->combine) {
928 ieee_unqualified_name(buf, seg->name);
929 if (seg->align >= SEG_ABS) {
930 ieee_putascii("ST%X,A,%02X%s.\r\n", seg->ieee_index,
932 ieee_putascii("ASL%X,%lX.\r\n", seg->ieee_index,
933 (seg->align - SEG_ABS) * 16);
935 ieee_putascii("ST%X,%c,%02X%s.\r\n", seg->ieee_index, attrib,
937 ieee_putascii("SA%X,%lX.\r\n", seg->ieee_index, seg->align);
938 ieee_putascii("ASS%X,%X.\r\n", seg->ieee_index,
944 * write the start address if there is one
946 if (ieee_entry_seg) {
947 for (seg = seghead; seg; seg = seg->next)
948 if (seg->index == ieee_entry_seg)
951 error(ERR_PANIC, "Start address records are incorrect");
953 ieee_putascii("ASG,R%X,%lX,+.\r\n", seg->ieee_index,
962 for (seg = seghead; seg; seg = seg->next) {
963 for (pub = seg->pubhead; pub; pub = pub->next) {
965 ieee_unqualified_name(buf, pub->name);
966 ieee_putascii("NI%X,%02X%s.\r\n", i, strlen(buf), buf);
967 if (pub->segment == -1)
968 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i, pub->index,
971 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i, pub->segment * 16,
974 if (pub->type >= 0x100)
975 ieee_putascii("ATI%X,T%X.\r\n", i, pub->type - 0x100);
977 ieee_putascii("ATI%X,%X.\r\n", i, pub->type);
986 ieee_unqualified_name(buf, pub->name);
987 ieee_putascii("NI%X,%02X%s.\r\n", i, strlen(buf), buf);
988 if (pub->segment == -1)
989 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i, pub->index,
992 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i, pub->segment * 16,
995 if (pub->type >= 0x100)
996 ieee_putascii("ATI%X,T%X.\r\n", i, pub->type - 0x100);
998 ieee_putascii("ATI%X,%X.\r\n", i, pub->type);
1004 * Write the externals
1010 ieee_unqualified_name(buf, ext->name);
1011 ieee_putascii("NX%X,%02X%s.\r\n", i++, strlen(buf), buf);
1017 * IEEE doesn't have a standard pass break record
1018 * so use the ladsoft variant
1020 ieee_putascii("CO100,06ENDSYM.\r\n");
1026 for (arr = arrhead; arr && debuginfo; arr = arr->next) {
1027 ieee_putascii("TY%X,20,%X,%lX.\r\n", i++, arr->basetype,
1034 for (seg = seghead; seg && debuginfo; seg = seg->next) {
1035 for (loc = seg->lochead; loc; loc = loc->next) {
1037 ieee_unqualified_name(buf, loc->name);
1038 ieee_putascii("NN%X,%02X%s.\r\n", i, strlen(buf), buf);
1039 if (loc->segment == -1)
1040 ieee_putascii("ASN%X,R%X,%lX,+.\r\n", i, loc->index,
1043 ieee_putascii("ASN%X,%lX,%lX,+.\r\n", i, loc->segment * 16,
1046 if (loc->type >= 0x100)
1047 ieee_putascii("ATN%X,T%X.\r\n", i, loc->type - 0x100);
1049 ieee_putascii("ATN%X,%X.\r\n", i, loc->type);
1056 * put out section data;
1059 if (!debuginfo && !strcmp(seg->name, "??LINE"))
1062 if (seg->currentpos) {
1063 int32_t size, org = 0;
1065 ieee_putascii("SB%X.\r\n", seg->ieee_index);
1068 size = HUNKSIZE - (org % HUNKSIZE);
1071 seg->currentpos ? seg->currentpos - org : size;
1072 size = fix->offset - org > size ? size : fix->offset - org;
1073 org = ieee_putld(org, org + size, data->data);
1074 if (org % HUNKSIZE == 0)
1076 if (org == fix->offset) {
1077 org += ieee_putlr(fix);
1081 while (org < seg->currentpos && data) {
1083 seg->currentpos - org >
1084 HUNKSIZE ? HUNKSIZE : seg->currentpos - org;
1085 org = ieee_putld(org, org + size, data->data);
1096 ieee_putascii("ME.\r\n");
1099 static void ieee_write_byte(struct ieeeSection *seg, int data)
1102 if (!(temp = seg->currentpos++ % HUNKSIZE))
1104 seg->datacurr->data[temp] = data;
1107 static void ieee_write_word(struct ieeeSection *seg, int data)
1109 ieee_write_byte(seg, data & 0xFF);
1110 ieee_write_byte(seg, (data >> 8) & 0xFF);
1113 static void ieee_write_dword(struct ieeeSection *seg, int32_t data)
1115 ieee_write_byte(seg, data & 0xFF);
1116 ieee_write_byte(seg, (data >> 8) & 0xFF);
1117 ieee_write_byte(seg, (data >> 16) & 0xFF);
1118 ieee_write_byte(seg, (data >> 24) & 0xFF);
1120 static void ieee_putascii(char *format, ...)
1126 va_start(ap, format);
1127 vsnprintf(buffer, sizeof(buffer), format, ap);
1129 for (i = 0; i < l; i++)
1130 if ((buffer[i] & 0xff) > 31)
1131 checksum += buffer[i];
1133 fprintf(ofp, buffer);
1137 * put out a checksum record */
1138 static void ieee_putcs(int toclear)
1141 ieee_putascii("CS.\r\n");
1145 ieee_putascii("CS%02X.\r\n", checksum & 127);
1150 static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf)
1155 val = start % HUNKSIZE;
1156 /* fill up multiple lines */
1157 while (end - start >= LDPERLINE) {
1159 ieee_putascii("LD");
1160 for (i = 0; i < LDPERLINE; i++) {
1161 ieee_putascii("%02X", buf[val++]);
1164 ieee_putascii(".\r\n");
1166 /* if no partial lines */
1169 /* make a partial line */
1170 ieee_putascii("LD");
1171 while (start < end) {
1172 ieee_putascii("%02X", buf[val++]);
1175 ieee_putascii(".\r\n");
1178 static int32_t ieee_putlr(struct ieeeFixupp *p)
1181 * To deal with the vagaries of segmentation the LADsoft linker
1182 * defines two types of segments: absolute and virtual. Note that
1183 * 'absolute' in this context is a different thing from the IEEE
1184 * definition of an absolute segment type, which is also supported. If a
1185 * sement is linked in virtual mode the low limit (L-var) is
1186 * subtracted from each R,X, and P variable which appears in an
1187 * expression, so that we can have relative offsets. Meanwhile
1188 * in the ABSOLUTE mode this subtraction is not done and
1189 * so we can use absolute offsets from 0. In the LADsoft linker
1190 * this configuration is not done in the assemblker source but in
1191 * a source the linker reads. Generally this type of thing only
1192 * becomes an issue if real mode code is used. A pure 32-bit linker could
1193 * get away without defining the virtual mode...
1196 int32_t size = p->size;
1200 sprintf(buf, "%"PRIX32"", -p->id1);
1202 sprintf(buf, "L%"PRIX32",10,/", p->id1);
1205 sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend);
1208 sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size);
1213 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend,
1214 p->id2, -p->id1 * 16);
1216 sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend,
1220 sprintf(buf, "X%"PRIX32"", p->id1);
1223 sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size);
1226 /* We needed a non-ieee hack here.
1227 * We introduce the Y variable, which is the low
1228 * limit of the native segment the extern resides in
1230 sprintf(buf, "Y%"PRIX32",10,/", p->id1);
1234 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2,
1237 sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1);
1240 ieee_putascii("LR(%s,%"PRIX32").\r\n", buf, size);
1245 /* Dump all segment data (text and fixups )*/
1247 static void ieee_unqualified_name(char *dest, char *source)
1249 if (ieee_uppercase) {
1251 *dest++ = toupper(*source++);
1254 strcpy(dest, source);
1256 void dbgls_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1266 arrindex = ARRAY_BOT;
1269 ieee_segment("??LINE", 2, &tempint);
1272 static void dbgls_cleanup(void)
1274 struct ieeeSection *segtmp;
1276 struct FileName *fntemp = fnhead;
1277 fnhead = fnhead->next;
1278 nasm_free(fntemp->name);
1281 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
1282 while (segtmp->lochead) {
1283 struct ieeePublic *loctmp = segtmp->lochead;
1284 segtmp->lochead = loctmp->next;
1285 nasm_free(loctmp->name);
1290 struct Array *arrtmp = arrhead;
1291 arrhead = arrhead->next;
1297 * because this routine is not bracketed in
1298 * the main program, this routine will be called even if there
1299 * is no request for debug info
1300 * so, we have to make sure the ??LINE segment is avaialbe
1301 * as the first segment when this debug format is selected
1303 static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto)
1305 struct FileName *fn;
1306 struct ieeeSection *seg;
1308 if (segto == NO_SEG)
1312 * If `any_segs' is still false, we must define a default
1316 int tempint; /* ignored */
1317 if (segto != ieee_segment("__NASMDEFSEG", 2, &tempint))
1318 error(ERR_PANIC, "strange segment conditions in OBJ driver");
1322 * Find the segment we are targetting.
1324 for (seg = seghead; seg; seg = seg->next)
1325 if (seg->index == segto)
1328 error(ERR_PANIC, "lineno directed to nonexistent segment?");
1330 for (fn = fnhead; fn; fn = fn->next) {
1331 if (!nasm_stricmp(lnfname, fn->name))
1336 fn = nasm_malloc(sizeof(*fn));
1337 fn->name = nasm_malloc(strlen(lnfname) + 1);
1339 strcpy(fn->name, lnfname);
1344 ieee_write_byte(seghead, fn->index);
1345 ieee_write_word(seghead, lineno);
1346 ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS,
1350 static void dbgls_deflabel(char *name, int32_t segment,
1351 int32_t offset, int is_global, char *special)
1353 struct ieeeSection *seg;
1355 /* Keep compiler from warning about special */
1359 * If it's a special-retry from pass two, discard it.
1365 * First check for the double-period, signifying something
1368 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
1375 if (ieee_seg_needs_update)
1377 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
1380 if (segment >= SEG_ABS || segment == NO_SEG) {
1385 * If `any_segs' is still false, we might need to define a
1386 * default segment, if they're trying to declare a label in
1387 * `first_seg'. But the label should exist due to a prior
1388 * call to ieee_deflabel so we can skip that.
1391 for (seg = seghead; seg; seg = seg->next)
1392 if (seg->index == segment) {
1393 struct ieeePublic *loc;
1395 * Case (ii). Maybe MODPUB someday?
1398 last_defined = loc = nasm_malloc(sizeof(*loc));
1399 *seg->loctail = loc;
1400 seg->loctail = &loc->next;
1402 loc->name = nasm_strdup(name);
1403 loc->offset = offset;
1405 loc->index = seg->ieee_index;
1409 static void dbgls_typevalue(int32_t type)
1411 int elem = TYM_ELEMENTS(type);
1412 type = TYM_TYPE(type);
1419 last_defined->type = 1; /* uint8_t */
1422 last_defined->type = 3; /* unsigned word */
1425 last_defined->type = 5; /* unsigned dword */
1428 last_defined->type = 9; /* float */
1431 last_defined->type = 10; /* qword */
1434 last_defined->type = 11; /* TBYTE */
1437 last_defined->type = 0x10; /* near label */
1442 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
1443 int vtype = last_defined->type;
1444 arrtmp->size = elem;
1445 arrtmp->basetype = vtype;
1446 arrtmp->next = NULL;
1447 last_defined->type = arrindex++ + 0x100;
1449 arrtail = &(arrtmp->next);
1451 last_defined = NULL;
1453 static void dbgls_output(int output_type, void *param)
1458 static struct dfmt ladsoft_debug_form = {
1459 "LADsoft Debug Records",
1469 static struct dfmt *ladsoft_debug_arr[3] = {
1470 &ladsoft_debug_form,
1474 struct ofmt of_ieee = {
1475 "IEEE-695 (LADsoft variant) object file format",
1492 #endif /* OF_IEEE */