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
43 #include <stdarg.h> /* Note: we need the ANSI version of stdarg.h */
55 static char ieee_infile
[FILENAME_MAX
];
56 static int ieee_uppercase
;
59 static ldfunc deflabel
;
64 #define HUNKSIZE 1024 /* Size of the data hunk */
65 #define EXT_BLKSIZ 512
66 #define LDPERLINE 32 /* bytes per line in output */
71 struct LineNumber
*next
;
72 struct ieeeSection
*segment
;
77 static struct FileName
{
78 struct FileName
*next
;
87 } *arrhead
, **arrtail
;
89 static struct ieeePublic
{
90 struct ieeePublic
*next
;
93 int32_t segment
; /* only if it's far-absolute */
95 int type
; /* for debug purposes */
96 } *fpubhead
, **fpubtail
, *last_defined
;
98 static struct ieeeExternal
{
99 struct ieeeExternal
*next
;
102 } *exthead
, **exttail
;
104 static int externals
;
106 static struct ExtBack
{
107 struct ExtBack
*next
;
108 int index
[EXT_BLKSIZ
];
111 /* NOTE: the first segment MUST be the lineno segment */
112 static struct ieeeSection
{
113 struct ieeeObjData
*data
, *datacurr
;
114 struct ieeeSection
*next
;
115 struct ieeeFixupp
*fptr
, *flptr
;
116 int32_t index
; /* the NASM segment id */
117 int32_t ieee_index
; /* the OBJ-file segment index */
119 int32_t align
; /* can be SEG_ABS + absolute addr */
126 int32_t use32
; /* is this segment 32-bit? */
127 struct ieeePublic
*pubhead
, **pubtail
, *lochead
, **loctail
;
129 } *seghead
, **segtail
, *ieee_seg_needs_update
;
132 struct ieeeObjData
*next
;
133 uint8_t data
[HUNKSIZE
];
137 struct ieeeFixupp
*next
;
155 static int32_t ieee_entry_seg
, ieee_entry_ofs
;
158 extern struct ofmt of_ieee
;
160 static void ieee_data_new(struct ieeeSection
*);
161 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection
*,
162 int, uint32_t, int32_t);
163 static void ieee_install_fixup(struct ieeeSection
*, struct ieeeFixupp
*);
164 static int32_t ieee_segment(char *, int, int *);
165 static void ieee_write_file(int debuginfo
);
166 static void ieee_write_byte(struct ieeeSection
*, int);
167 static void ieee_write_word(struct ieeeSection
*, int);
168 static void ieee_write_dword(struct ieeeSection
*, int32_t);
169 static void ieee_putascii(char *, ...);
170 static void ieee_putcs(int);
171 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
172 static int32_t ieee_putlr(struct ieeeFixupp
*);
173 static void ieee_unqualified_name(char *, char *);
178 static void ieee_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
186 fpubtail
= &fpubhead
;
192 seghead
= ieee_seg_needs_update
= NULL
;
194 ieee_entry_seg
= NO_SEG
;
195 ieee_uppercase
= FALSE
;
197 of_ieee
.current_dfmt
->init(&of_ieee
, NULL
, fp
, errfunc
);
199 static int ieee_set_info(enum geninfo type
, char **val
)
210 static void ieee_cleanup(int debuginfo
)
212 ieee_write_file(debuginfo
);
213 of_ieee
.current_dfmt
->cleanup();
216 struct ieeeSection
*segtmp
= seghead
;
217 seghead
= seghead
->next
;
218 while (segtmp
->pubhead
) {
219 struct ieeePublic
*pubtmp
= segtmp
->pubhead
;
220 segtmp
->pubhead
= pubtmp
->next
;
223 while (segtmp
->fptr
) {
224 struct ieeeFixupp
*fixtmp
= segtmp
->fptr
;
225 segtmp
->fptr
= fixtmp
->next
;
228 while (segtmp
->data
) {
229 struct ieeeObjData
*dattmp
= segtmp
->data
;
230 segtmp
->data
= dattmp
->next
;
236 struct ieeePublic
*pubtmp
= fpubhead
;
237 fpubhead
= fpubhead
->next
;
241 struct ieeeExternal
*exttmp
= exthead
;
242 exthead
= exthead
->next
;
246 struct ExtBack
*ebtmp
= ebhead
;
247 ebhead
= ebhead
->next
;
253 * callback for labels
255 static void ieee_deflabel(char *name
, int32_t segment
,
256 int32_t offset
, int is_global
, char *special
)
259 * We have three cases:
261 * (i) `segment' is a segment-base. If so, set the name field
262 * for the segment structure it refers to, and then
265 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
266 * Save the label position for later output of a PUBDEF record.
269 * (iii) `segment' is not one of our segments. Save the label
270 * position for later output of an EXTDEF.
272 struct ieeeExternal
*ext
;
274 struct ieeeSection
*seg
;
278 error(ERR_NONFATAL
, "unrecognised symbol type `%s'", special
);
281 * First check for the double-period, signifying something
284 if (name
[0] == '.' && name
[1] == '.') {
285 if (!strcmp(name
, "..start")) {
286 ieee_entry_seg
= segment
;
287 ieee_entry_ofs
= offset
;
295 if (ieee_seg_needs_update
) {
296 ieee_seg_needs_update
->name
= name
;
299 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
305 if (segment
>= SEG_ABS
) {
307 * SEG_ABS subcase of (ii).
310 struct ieeePublic
*pub
;
312 pub
= *fpubtail
= nasm_malloc(sizeof(*pub
));
313 fpubtail
= &pub
->next
;
316 pub
->offset
= offset
;
317 pub
->segment
= segment
& ~SEG_ABS
;
322 for (seg
= seghead
; seg
&& is_global
; seg
= seg
->next
)
323 if (seg
->index
== segment
) {
324 struct ieeePublic
*pub
;
326 last_defined
= pub
= *seg
->pubtail
= nasm_malloc(sizeof(*pub
));
327 seg
->pubtail
= &pub
->next
;
330 pub
->offset
= offset
;
331 pub
->index
= seg
->ieee_index
;
340 ext
= *exttail
= nasm_malloc(sizeof(*ext
));
342 exttail
= &ext
->next
;
345 ext
->commonsize
= offset
;
351 eb
= *ebtail
= nasm_malloc(sizeof(*eb
));
355 while (i
> EXT_BLKSIZ
) {
359 eb
= *ebtail
= nasm_malloc(sizeof(*eb
));
365 eb
->index
[i
] = externals
++;
373 static void ieee_out(int32_t segto
, const void *data
, uint32_t type
,
374 int32_t segment
, int32_t wrt
)
376 uint32_t size
, realtype
;
377 const uint8_t *ucdata
;
379 struct ieeeSection
*seg
;
382 * handle absolute-assembly (structure definitions)
384 if (segto
== NO_SEG
) {
385 if ((type
& OUT_TYPMASK
) != OUT_RESERVE
)
386 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
392 * If `any_segs' is still FALSE, we must define a default
396 int tempint
; /* ignored */
397 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
398 error(ERR_PANIC
, "strange segment conditions in IEEE driver");
402 * Find the segment we are targetting.
404 for (seg
= seghead
; seg
; seg
= seg
->next
)
405 if (seg
->index
== segto
)
408 error(ERR_PANIC
, "code directed to nonexistent segment?");
410 size
= type
& OUT_SIZMASK
;
411 realtype
= type
& OUT_TYPMASK
;
412 if (realtype
== OUT_RAWDATA
) {
415 ieee_write_byte(seg
, *ucdata
++);
416 } else if (realtype
== OUT_ADDRESS
|| realtype
== OUT_REL2ADR
||
417 realtype
== OUT_REL4ADR
) {
418 if (segment
== NO_SEG
&& realtype
!= OUT_ADDRESS
)
419 error(ERR_NONFATAL
, "relative call to absolute address not"
420 " supported by IEEE format");
421 ldata
= *(int32_t *)data
;
422 if (realtype
== OUT_REL2ADR
)
424 if (realtype
== OUT_REL4ADR
)
426 ieee_write_fixup(segment
, wrt
, seg
, size
, realtype
, ldata
);
428 ieee_write_word(seg
, ldata
);
430 ieee_write_dword(seg
, ldata
);
431 } else if (realtype
== OUT_RESERVE
) {
433 ieee_write_byte(seg
, 0);
437 static void ieee_data_new(struct ieeeSection
*segto
)
441 segto
->data
= segto
->datacurr
=
442 nasm_malloc(sizeof(*(segto
->datacurr
)));
444 segto
->datacurr
= segto
->datacurr
->next
=
445 nasm_malloc(sizeof(*(segto
->datacurr
)));
446 segto
->datacurr
->next
= NULL
;
450 * this routine is unalduterated bloatware. I usually don't do this
451 * but I might as well see what it is like on a harmless program.
452 * If anyone wants to optimize this is a good canditate!
454 static void ieee_write_fixup(int32_t segment
, int32_t wrt
,
455 struct ieeeSection
*segto
, int size
,
456 uint32_t realtype
, int32_t offset
)
458 struct ieeeSection
*target
;
461 /* Don't put a fixup for things NASM can calculate */
462 if (wrt
== NO_SEG
&& segment
== NO_SEG
)
466 /* if it is a WRT offset */
471 s
.id1
= -(wrt
- SEG_ABS
);
473 if (wrt
% 2 && realtype
!= OUT_REL2ADR
474 && realtype
!= OUT_REL4ADR
) {
477 for (target
= seghead
; target
; target
= target
->next
)
478 if (target
->index
== wrt
)
481 s
.id1
= target
->ieee_index
;
482 for (target
= seghead
; target
; target
= target
->next
)
483 if (target
->index
== segment
)
487 s
.id2
= target
->ieee_index
;
490 * Now we assume the segment field is being used
491 * to hold an extern index
493 int32_t i
= segment
/ 2;
494 struct ExtBack
*eb
= ebhead
;
495 while (i
> EXT_BLKSIZ
) {
502 /* if we have an extern decide the type and make a record
507 s
.id2
= eb
->index
[i
];
510 "Source of WRT must be an offset");
515 "unrecognised WRT value in ieee_write_fixup");
517 error(ERR_NONFATAL
, "target of WRT must be a section ");
520 ieee_install_fixup(segto
, &s
);
523 /* Pure segment fixup ? */
524 if (segment
!= NO_SEG
) {
527 if (segment
>= SEG_ABS
) {
528 /* absolute far segment fixup */
529 s
.id1
= -(segment
- ~SEG_ABS
);
530 } else if (segment
% 2) {
531 /* fixup to named segment */
533 for (target
= seghead
; target
; target
= target
->next
)
534 if (target
->index
== segment
- 1)
537 s
.id1
= target
->ieee_index
;
540 * Now we assume the segment field is being used
541 * to hold an extern index
543 int32_t i
= segment
/ 2;
544 struct ExtBack
*eb
= ebhead
;
545 while (i
> EXT_BLKSIZ
) {
552 /* if we have an extern decide the type and make a record
555 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
557 "Segment of a rel not supported in ieee_write_fixup");
559 /* If we want the segment */
562 s
.id1
= eb
->index
[i
];
566 /* If we get here the seg value doesn't make sense */
568 "unrecognised segment value in ieee_write_fixup");
572 /* Assume we are offsetting directly from a section
573 * So look up the target segment
575 for (target
= seghead
; target
; target
= target
->next
)
576 if (target
->index
== segment
)
579 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
580 /* PC rel to a known offset */
581 s
.id1
= target
->ieee_index
;
586 /* We were offsetting from a seg */
587 s
.id1
= target
->ieee_index
;
594 * Now we assume the segment field is being used
595 * to hold an extern index
597 int32_t i
= segment
/ 2;
598 struct ExtBack
*eb
= ebhead
;
599 while (i
> EXT_BLKSIZ
) {
606 /* if we have an extern decide the type and make a record
609 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
612 s
.id1
= eb
->index
[i
];
614 /* else we want the external offset */
617 s
.id1
= eb
->index
[i
];
621 /* If we get here the seg value doesn't make sense */
623 "unrecognised segment value in ieee_write_fixup");
626 if (size
!= 2 && s
.ftype
== FT_SEG
)
627 error(ERR_NONFATAL
, "IEEE format can only handle 2-byte"
628 " segment base references");
630 ieee_install_fixup(segto
, &s
);
633 /* should never get here */
635 static void ieee_install_fixup(struct ieeeSection
*seg
,
636 struct ieeeFixupp
*fix
)
638 struct ieeeFixupp
*f
;
639 f
= nasm_malloc(sizeof(struct ieeeFixupp
));
640 memcpy(f
, fix
, sizeof(struct ieeeFixupp
));
641 f
->offset
= seg
->currentpos
;
642 seg
->currentpos
+= fix
->size
;
645 seg
->flptr
= seg
->flptr
->next
= f
;
647 seg
->fptr
= seg
->flptr
= f
;
654 static int32_t ieee_segment(char *name
, int pass
, int *bits
)
657 * We call the label manager here to define a name for the new
658 * segment, and when our _own_ label-definition stub gets
659 * called in return, it should register the new segment name
660 * using the pointer it gets passed. That way we save memory,
661 * by sponging off the label manager.
667 return seghead
->index
;
669 struct ieeeSection
*seg
;
670 int ieee_idx
, attrs
, rn_error
;
674 * Look for segment attributes.
678 name
++; /* hack, but a documented one */
680 while (*p
&& !isspace(*p
))
684 while (*p
&& isspace(*p
))
688 while (*p
&& !isspace(*p
))
692 while (*p
&& isspace(*p
))
700 for (seg
= seghead
; seg
; seg
= seg
->next
) {
702 if (!strcmp(seg
->name
, name
)) {
703 if (attrs
> 0 && pass
== 1)
704 error(ERR_WARNING
, "segment attributes specified on"
705 " redeclaration of segment: ignoring");
714 *segtail
= seg
= nasm_malloc(sizeof(*seg
));
716 segtail
= &seg
->next
;
717 seg
->index
= seg_alloc();
718 seg
->ieee_index
= ieee_idx
;
722 seg
->align
= 1; /* default */
723 seg
->use32
= *bits
== 32; /* default to user spec */
724 seg
->combine
= CMB_PUBLIC
; /* default */
726 seg
->pubtail
= &seg
->pubhead
;
730 seg
->loctail
= &seg
->lochead
;
733 * Process the segment attributes.
742 * `p' contains a segment attribute.
744 if (!nasm_stricmp(p
, "private"))
745 seg
->combine
= CMB_PRIVATE
;
746 else if (!nasm_stricmp(p
, "public"))
747 seg
->combine
= CMB_PUBLIC
;
748 else if (!nasm_stricmp(p
, "common"))
749 seg
->combine
= CMB_COMMON
;
750 else if (!nasm_stricmp(p
, "use16"))
752 else if (!nasm_stricmp(p
, "use32"))
754 else if (!nasm_strnicmp(p
, "align=", 6)) {
755 seg
->align
= readnum(p
+ 6, &rn_error
);
760 error(ERR_NONFATAL
, "segment alignment should be"
763 switch ((int)seg
->align
) {
775 error(ERR_NONFATAL
, "invalid alignment value %d",
780 } else if (!nasm_strnicmp(p
, "absolute=", 9)) {
781 seg
->align
= SEG_ABS
+ readnum(p
+ 9, &rn_error
);
783 error(ERR_NONFATAL
, "argument to `absolute' segment"
784 " attribute should be numeric");
788 ieee_seg_needs_update
= seg
;
789 if (seg
->align
>= SEG_ABS
)
790 deflabel(name
, NO_SEG
, seg
->align
- SEG_ABS
,
791 NULL
, FALSE
, FALSE
, &of_ieee
, error
);
793 deflabel(name
, seg
->index
+ 1, 0L,
794 NULL
, FALSE
, FALSE
, &of_ieee
, error
);
795 ieee_seg_needs_update
= NULL
;
806 * directives supported
808 static int ieee_directive(char *directive
, char *value
, int pass
)
813 if (!strcmp(directive
, "uppercase")) {
814 ieee_uppercase
= TRUE
;
821 * Return segment data
823 static int32_t ieee_segbase(int32_t segment
)
825 struct ieeeSection
*seg
;
828 * Find the segment in our list.
830 for (seg
= seghead
; seg
; seg
= seg
->next
)
831 if (seg
->index
== segment
- 1)
835 return segment
; /* not one of ours - leave it alone */
837 if (seg
->align
>= SEG_ABS
)
838 return seg
->align
; /* absolute segment */
840 return segment
; /* no special treatment */
846 static void ieee_filename(char *inname
, char *outname
, efunc error
)
848 strcpy(ieee_infile
, inname
);
849 standard_extension(inname
, outname
, ".o", error
);
852 static void ieee_write_file(int debuginfo
)
857 struct ieeeSection
*seg
;
858 struct ieeePublic
*pub
, *loc
;
859 struct ieeeExternal
*ext
;
860 struct ieeeObjData
*data
;
861 struct ieeeFixupp
*fix
;
863 static char boast
[] = "The Netwide Assembler " NASM_VER
;
867 * Write the module header
869 ieee_putascii("MBFNASM,%02X%s.\r\n", strlen(ieee_infile
), ieee_infile
);
872 * Write the NASM boast comment.
874 ieee_putascii("CO0,%02X%s.\r\n", strlen(boast
), boast
);
877 * write processor-specific information
879 ieee_putascii("AD8,4,L.\r\n");
885 thetime
= localtime(&reltime
);
886 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\r\n",
887 1900 + thetime
->tm_year
, thetime
->tm_mon
+ 1,
888 thetime
->tm_mday
, thetime
->tm_hour
, thetime
->tm_min
,
891 * if debugging, dump file names
893 for (fn
= fnhead
; fn
&& debuginfo
; fn
= fn
->next
) {
894 ieee_putascii("C0105,%02X%s.\r\n", strlen(fn
->name
), fn
->name
);
897 ieee_putascii("CO101,07ENDHEAD.\r\n");
899 * the standard doesn't specify when to put checksums,
900 * we'll just do it periodically.
905 * Write the section headers
908 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
913 switch (seg
->combine
) {
925 ieee_unqualified_name(buf
, seg
->name
);
926 if (seg
->align
>= SEG_ABS
) {
927 ieee_putascii("ST%X,A,%02X%s.\r\n", seg
->ieee_index
,
929 ieee_putascii("ASL%X,%lX.\r\n", seg
->ieee_index
,
930 (seg
->align
- SEG_ABS
) * 16);
932 ieee_putascii("ST%X,%c,%02X%s.\r\n", seg
->ieee_index
, attrib
,
934 ieee_putascii("SA%X,%lX.\r\n", seg
->ieee_index
, seg
->align
);
935 ieee_putascii("ASS%X,%X.\r\n", seg
->ieee_index
,
941 * write the start address if there is one
943 if (ieee_entry_seg
) {
944 for (seg
= seghead
; seg
; seg
= seg
->next
)
945 if (seg
->index
== ieee_entry_seg
)
948 error(ERR_PANIC
, "Start address records are incorrect");
950 ieee_putascii("ASG,R%X,%lX,+.\r\n", seg
->ieee_index
,
959 for (seg
= seghead
; seg
; seg
= seg
->next
) {
960 for (pub
= seg
->pubhead
; pub
; pub
= pub
->next
) {
962 ieee_unqualified_name(buf
, pub
->name
);
963 ieee_putascii("NI%X,%02X%s.\r\n", i
, strlen(buf
), buf
);
964 if (pub
->segment
== -1)
965 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i
, pub
->index
,
968 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i
, pub
->segment
* 16,
971 if (pub
->type
>= 0x100)
972 ieee_putascii("ATI%X,T%X.\r\n", i
, pub
->type
- 0x100);
974 ieee_putascii("ATI%X,%X.\r\n", i
, pub
->type
);
983 ieee_unqualified_name(buf
, pub
->name
);
984 ieee_putascii("NI%X,%02X%s.\r\n", i
, strlen(buf
), buf
);
985 if (pub
->segment
== -1)
986 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i
, pub
->index
,
989 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i
, pub
->segment
* 16,
992 if (pub
->type
>= 0x100)
993 ieee_putascii("ATI%X,T%X.\r\n", i
, pub
->type
- 0x100);
995 ieee_putascii("ATI%X,%X.\r\n", i
, pub
->type
);
1001 * Write the externals
1007 ieee_unqualified_name(buf
, ext
->name
);
1008 ieee_putascii("NX%X,%02X%s.\r\n", i
++, strlen(buf
), buf
);
1014 * IEEE doesn't have a standard pass break record
1015 * so use the ladsoft variant
1017 ieee_putascii("CO100,06ENDSYM.\r\n");
1023 for (arr
= arrhead
; arr
&& debuginfo
; arr
= arr
->next
) {
1024 ieee_putascii("TY%X,20,%X,%lX.\r\n", i
++, arr
->basetype
,
1031 for (seg
= seghead
; seg
&& debuginfo
; seg
= seg
->next
) {
1032 for (loc
= seg
->lochead
; loc
; loc
= loc
->next
) {
1034 ieee_unqualified_name(buf
, loc
->name
);
1035 ieee_putascii("NN%X,%02X%s.\r\n", i
, strlen(buf
), buf
);
1036 if (loc
->segment
== -1)
1037 ieee_putascii("ASN%X,R%X,%lX,+.\r\n", i
, loc
->index
,
1040 ieee_putascii("ASN%X,%lX,%lX,+.\r\n", i
, loc
->segment
* 16,
1043 if (loc
->type
>= 0x100)
1044 ieee_putascii("ATN%X,T%X.\r\n", i
, loc
->type
- 0x100);
1046 ieee_putascii("ATN%X,%X.\r\n", i
, loc
->type
);
1053 * put out section data;
1056 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
1059 if (seg
->currentpos
) {
1060 int32_t size
, org
= 0;
1062 ieee_putascii("SB%X.\r\n", seg
->ieee_index
);
1065 size
= HUNKSIZE
- (org
% HUNKSIZE
);
1068 seg
->currentpos
? seg
->currentpos
- org
: size
;
1069 size
= fix
->offset
- org
> size
? size
: fix
->offset
- org
;
1070 org
= ieee_putld(org
, org
+ size
, data
->data
);
1071 if (org
% HUNKSIZE
== 0)
1073 if (org
== fix
->offset
) {
1074 org
+= ieee_putlr(fix
);
1078 while (org
< seg
->currentpos
&& data
) {
1080 seg
->currentpos
- org
>
1081 HUNKSIZE
? HUNKSIZE
: seg
->currentpos
- org
;
1082 org
= ieee_putld(org
, org
+ size
, data
->data
);
1093 ieee_putascii("ME.\r\n");
1096 static void ieee_write_byte(struct ieeeSection
*seg
, int data
)
1099 if (!(temp
= seg
->currentpos
++ % HUNKSIZE
))
1101 seg
->datacurr
->data
[temp
] = data
;
1104 static void ieee_write_word(struct ieeeSection
*seg
, int data
)
1106 ieee_write_byte(seg
, data
& 0xFF);
1107 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1110 static void ieee_write_dword(struct ieeeSection
*seg
, int32_t data
)
1112 ieee_write_byte(seg
, data
& 0xFF);
1113 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1114 ieee_write_byte(seg
, (data
>> 16) & 0xFF);
1115 ieee_write_byte(seg
, (data
>> 24) & 0xFF);
1117 static void ieee_putascii(char *format
, ...)
1123 va_start(ap
, format
);
1124 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1126 for (i
= 0; i
< l
; i
++)
1127 if ((buffer
[i
] & 0xff) > 31)
1128 checksum
+= buffer
[i
];
1130 fprintf(ofp
, buffer
);
1134 * put out a checksum record */
1135 static void ieee_putcs(int toclear
)
1138 ieee_putascii("CS.\r\n");
1142 ieee_putascii("CS%02X.\r\n", checksum
& 127);
1147 static int32_t ieee_putld(int32_t start
, int32_t end
, uint8_t *buf
)
1152 val
= start
% HUNKSIZE
;
1153 /* fill up multiple lines */
1154 while (end
- start
>= LDPERLINE
) {
1156 ieee_putascii("LD");
1157 for (i
= 0; i
< LDPERLINE
; i
++) {
1158 ieee_putascii("%02X", buf
[val
++]);
1161 ieee_putascii(".\r\n");
1163 /* if no partial lines */
1166 /* make a partial line */
1167 ieee_putascii("LD");
1168 while (start
< end
) {
1169 ieee_putascii("%02X", buf
[val
++]);
1172 ieee_putascii(".\r\n");
1175 static int32_t ieee_putlr(struct ieeeFixupp
*p
)
1178 * To deal with the vagaries of segmentation the LADsoft linker
1179 * defines two types of segments: absolute and virtual. Note that
1180 * 'absolute' in this context is a different thing from the IEEE
1181 * definition of an absolute segment type, which is also supported. If a
1182 * sement is linked in virtual mode the low limit (L-var) is
1183 * subtracted from each R,X, and P variable which appears in an
1184 * expression, so that we can have relative offsets. Meanwhile
1185 * in the ABSOLUTE mode this subtraction is not done and
1186 * so we can use absolute offsets from 0. In the LADsoft linker
1187 * this configuration is not done in the assemblker source but in
1188 * a source the linker reads. Generally this type of thing only
1189 * becomes an issue if real mode code is used. A pure 32-bit linker could
1190 * get away without defining the virtual mode...
1193 int32_t size
= p
->size
;
1197 sprintf(buf
, "%"PRIX32
"", -p
->id1
);
1199 sprintf(buf
, "L%"PRIX32
",10,/", p
->id1
);
1202 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+", p
->id1
, p
->addend
);
1205 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,P,-,%X,-", p
->id1
, p
->addend
, p
->size
);
1210 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->addend
,
1211 p
->id2
, -p
->id1
* 16);
1213 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->addend
,
1217 sprintf(buf
, "X%"PRIX32
"", p
->id1
);
1220 sprintf(buf
, "X%"PRIX32
",P,-,%"PRIX32
",-", p
->id1
, size
);
1223 /* We needed a non-ieee hack here.
1224 * We introduce the Y variable, which is the low
1225 * limit of the native segment the extern resides in
1227 sprintf(buf
, "Y%"PRIX32
",10,/", p
->id1
);
1231 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->id2
,
1234 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->id2
, p
->id1
);
1237 ieee_putascii("LR(%s,%"PRIX32
").\r\n", buf
, size
);
1242 /* Dump all segment data (text and fixups )*/
1244 static void ieee_unqualified_name(char *dest
, char *source
)
1246 if (ieee_uppercase
) {
1248 *dest
++ = toupper(*source
++);
1251 strcpy(dest
, source
);
1253 void dbgls_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1263 arrindex
= ARRAY_BOT
;
1266 ieee_segment("??LINE", 2, &tempint
);
1269 static void dbgls_cleanup(void)
1271 struct ieeeSection
*segtmp
;
1273 struct FileName
*fntemp
= fnhead
;
1274 fnhead
= fnhead
->next
;
1275 nasm_free(fntemp
->name
);
1278 for (segtmp
= seghead
; segtmp
; segtmp
= segtmp
->next
) {
1279 while (segtmp
->lochead
) {
1280 struct ieeePublic
*loctmp
= segtmp
->lochead
;
1281 segtmp
->lochead
= loctmp
->next
;
1282 nasm_free(loctmp
->name
);
1287 struct Array
*arrtmp
= arrhead
;
1288 arrhead
= arrhead
->next
;
1294 * because this routine is not bracketed in
1295 * the main program, this routine will be called even if there
1296 * is no request for debug info
1297 * so, we have to make sure the ??LINE segment is avaialbe
1298 * as the first segment when this debug format is selected
1300 static void dbgls_linnum(const char *lnfname
, int32_t lineno
, int32_t segto
)
1302 struct FileName
*fn
;
1303 struct ieeeSection
*seg
;
1305 if (segto
== NO_SEG
)
1309 * If `any_segs' is still FALSE, we must define a default
1313 int tempint
; /* ignored */
1314 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
1315 error(ERR_PANIC
, "strange segment conditions in OBJ driver");
1319 * Find the segment we are targetting.
1321 for (seg
= seghead
; seg
; seg
= seg
->next
)
1322 if (seg
->index
== segto
)
1325 error(ERR_PANIC
, "lineno directed to nonexistent segment?");
1327 for (fn
= fnhead
; fn
; fn
= fn
->next
) {
1328 if (!nasm_stricmp(lnfname
, fn
->name
))
1333 fn
= nasm_malloc(sizeof(*fn
));
1334 fn
->name
= nasm_malloc(strlen(lnfname
) + 1);
1336 strcpy(fn
->name
, lnfname
);
1341 ieee_write_byte(seghead
, fn
->index
);
1342 ieee_write_word(seghead
, lineno
);
1343 ieee_write_fixup(segto
, NO_SEG
, seghead
, 4, OUT_ADDRESS
,
1347 static void dbgls_deflabel(char *name
, int32_t segment
,
1348 int32_t offset
, int is_global
, char *special
)
1350 struct ieeeSection
*seg
;
1351 int used_special
; /* have we used the special text? */
1353 /* Keep compiler from warning about special and used_special */
1354 used_special
= special
? FALSE
: FALSE
;
1357 * If it's a special-retry from pass two, discard it.
1363 * First check for the double-period, signifying something
1366 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
1373 if (ieee_seg_needs_update
)
1375 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
1378 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
1383 * If `any_segs' is still FALSE, we might need to define a
1384 * default segment, if they're trying to declare a label in
1385 * `first_seg'. But the label should exist due to a prior
1386 * call to ieee_deflabel so we can skip that.
1389 for (seg
= seghead
; seg
; seg
= seg
->next
)
1390 if (seg
->index
== segment
) {
1391 struct ieeePublic
*loc
;
1393 * Case (ii). Maybe MODPUB someday?
1396 last_defined
= loc
= nasm_malloc(sizeof(*loc
));
1397 *seg
->loctail
= loc
;
1398 seg
->loctail
= &loc
->next
;
1400 loc
->name
= nasm_strdup(name
);
1401 loc
->offset
= offset
;
1403 loc
->index
= seg
->ieee_index
;
1407 static void dbgls_typevalue(int32_t type
)
1409 int elem
= TYM_ELEMENTS(type
);
1410 type
= TYM_TYPE(type
);
1417 last_defined
->type
= 1; /* uint8_t */
1420 last_defined
->type
= 3; /* unsigned word */
1423 last_defined
->type
= 5; /* unsigned dword */
1426 last_defined
->type
= 9; /* float */
1429 last_defined
->type
= 10; /* qword */
1432 last_defined
->type
= 11; /* TBYTE */
1435 last_defined
->type
= 0x10; /* near label */
1440 struct Array
*arrtmp
= nasm_malloc(sizeof(*arrtmp
));
1441 int vtype
= last_defined
->type
;
1442 arrtmp
->size
= elem
;
1443 arrtmp
->basetype
= vtype
;
1444 arrtmp
->next
= NULL
;
1445 last_defined
->type
= arrindex
++ + 0x100;
1447 arrtail
= &(arrtmp
->next
);
1449 last_defined
= NULL
;
1451 static void dbgls_output(int output_type
, void *param
)
1456 static struct dfmt ladsoft_debug_form
= {
1457 "LADsoft Debug Records",
1467 static struct dfmt
*ladsoft_debug_arr
[3] = {
1468 &ladsoft_debug_form
,
1472 struct ofmt of_ieee
= {
1473 "IEEE-695 (LADsoft variant) object file format",
1490 #endif /* OF_IEEE */