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 */
54 static char ieee_infile
[FILENAME_MAX
];
55 static int ieee_uppercase
;
58 static ldfunc deflabel
;
63 #define HUNKSIZE 1024 /* Size of the data hunk */
64 #define EXT_BLKSIZ 512
65 #define LDPERLINE 32 /* bytes per line in output */
70 struct LineNumber
*next
;
71 struct ieeeSection
*segment
;
76 static struct FileName
{
77 struct FileName
*next
;
86 } *arrhead
, **arrtail
;
88 static struct ieeePublic
{
89 struct ieeePublic
*next
;
92 long segment
; /* only if it's far-absolute */
94 int type
; /* for debug purposes */
95 } *fpubhead
, **fpubtail
, *last_defined
;
97 static struct ieeeExternal
{
98 struct ieeeExternal
*next
;
101 } *exthead
, **exttail
;
103 static int externals
;
105 static struct ExtBack
{
106 struct ExtBack
*next
;
107 int index
[EXT_BLKSIZ
];
110 /* NOTE: the first segment MUST be the lineno segment */
111 static struct ieeeSection
{
112 struct ieeeObjData
*data
,*datacurr
;
113 struct ieeeSection
*next
;
114 struct ieeeFixupp
*fptr
, * flptr
;
115 long index
; /* the NASM segment id */
116 long ieee_index
; /* the OBJ-file segment index */
118 long align
; /* can be SEG_ABS + absolute addr */
125 long use32
; /* is this segment 32-bit? */
126 struct ieeePublic
*pubhead
, **pubtail
, *lochead
, **loctail
;
128 } *seghead
, **segtail
, *ieee_seg_needs_update
;
130 static struct ieeeObjData
{
131 struct ieeeObjData
*next
;
132 unsigned char data
[HUNKSIZE
];
136 struct ieeeFixupp
*next
;
154 static long ieee_entry_seg
, ieee_entry_ofs
;
157 extern struct ofmt of_ieee
;
159 static void ieee_data_new(struct ieeeSection
*);
160 static void ieee_write_fixup (long, long, struct ieeeSection
*,
162 static void ieee_install_fixup(struct ieeeSection
*, struct ieeeFixupp
*);
163 static long ieee_segment (char *, int, int *);
164 static void ieee_write_file(int debuginfo
);
165 static void ieee_write_byte(struct ieeeSection
*, int);
166 static void ieee_write_word(struct ieeeSection
*, int);
167 static void ieee_write_dword(struct ieeeSection
*, long);
168 static void ieee_putascii(char *, ...);
169 static void ieee_putcs(int);
170 static long ieee_putld(long, long, unsigned char *);
171 static long ieee_putlr(struct ieeeFixupp
*);
172 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
)
209 static void ieee_cleanup (int debuginfo
)
211 ieee_write_file(debuginfo
);
212 of_ieee
.current_dfmt
->cleanup ();
215 struct ieeeSection
*segtmp
= seghead
;
216 seghead
= seghead
->next
;
217 while (segtmp
->pubhead
) {
218 struct ieeePublic
*pubtmp
= segtmp
->pubhead
;
219 segtmp
->pubhead
= pubtmp
->next
;
222 while (segtmp
->fptr
) {
223 struct ieeeFixupp
*fixtmp
= segtmp
->fptr
;
224 segtmp
->fptr
= fixtmp
->next
;
227 while (segtmp
->data
) {
228 struct ieeeObjData
*dattmp
= segtmp
->data
;
229 segtmp
->data
= dattmp
->next
;
235 struct ieeePublic
*pubtmp
= fpubhead
;
236 fpubhead
= fpubhead
->next
;
240 struct ieeeExternal
*exttmp
= exthead
;
241 exthead
= exthead
->next
;
245 struct ExtBack
*ebtmp
= ebhead
;
246 ebhead
= ebhead
->next
;
251 * callback for labels
253 static void ieee_deflabel (char *name
, long segment
,
254 long offset
, int is_global
, char *special
) {
256 * We have three cases:
258 * (i) `segment' is a segment-base. If so, set the name field
259 * for the segment structure it refers to, and then
262 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
263 * Save the label position for later output of a PUBDEF record.
266 * (iii) `segment' is not one of our segments. Save the label
267 * position for later output of an EXTDEF.
269 struct ieeeExternal
*ext
;
271 struct ieeeSection
*seg
;
275 error(ERR_NONFATAL
, "unrecognised symbol type `%s'", special
);
278 * First check for the double-period, signifying something
281 if (name
[0] == '.' && name
[1] == '.') {
282 if (!strcmp(name
, "..start")) {
283 ieee_entry_seg
= segment
;
284 ieee_entry_ofs
= offset
;
292 if (ieee_seg_needs_update
) {
293 ieee_seg_needs_update
->name
= name
;
296 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
302 if (segment
>= SEG_ABS
) {
304 * SEG_ABS subcase of (ii).
307 struct ieeePublic
*pub
;
309 pub
= *fpubtail
= nasm_malloc(sizeof(*pub
));
310 fpubtail
= &pub
->next
;
313 pub
->offset
= offset
;
314 pub
->segment
= segment
& ~SEG_ABS
;
319 for (seg
= seghead
; seg
&& is_global
; seg
= seg
->next
)
320 if (seg
->index
== segment
) {
321 struct ieeePublic
*pub
;
323 last_defined
= pub
= *seg
->pubtail
= nasm_malloc(sizeof(*pub
));
324 seg
->pubtail
= &pub
->next
;
327 pub
->offset
= offset
;
328 pub
->index
= seg
->ieee_index
;
337 ext
= *exttail
= nasm_malloc(sizeof(*ext
));
339 exttail
= &ext
->next
;
342 ext
->commonsize
= offset
;
348 eb
= *ebtail
= nasm_malloc(sizeof(*eb
));
352 while (i
> EXT_BLKSIZ
) {
356 eb
= *ebtail
= nasm_malloc(sizeof(*eb
));
362 eb
->index
[i
] = externals
++;
370 static void ieee_out (long segto
, void *data
, unsigned long type
,
371 long segment
, long wrt
) {
373 unsigned char *ucdata
;
375 struct ieeeSection
*seg
;
378 * handle absolute-assembly (structure definitions)
380 if (segto
== NO_SEG
) {
381 if ((type
& OUT_TYPMASK
) != OUT_RESERVE
)
382 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
388 * If `any_segs' is still FALSE, we must define a default
392 int tempint
; /* ignored */
393 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
394 error (ERR_PANIC
, "strange segment conditions in IEEE driver");
398 * Find the segment we are targetting.
400 for (seg
= seghead
; seg
; seg
= seg
->next
)
401 if (seg
->index
== segto
)
404 error (ERR_PANIC
, "code directed to nonexistent segment?");
406 size
= type
& OUT_SIZMASK
;
407 realtype
= type
& OUT_TYPMASK
;
408 if (realtype
== OUT_RAWDATA
) {
411 ieee_write_byte(seg
,*ucdata
++);
412 } else if (realtype
== OUT_ADDRESS
|| realtype
== OUT_REL2ADR
||
413 realtype
== OUT_REL4ADR
) {
414 if (segment
== NO_SEG
&& realtype
!= OUT_ADDRESS
)
415 error(ERR_NONFATAL
, "relative call to absolute address not"
416 " supported by IEEE format");
417 ldata
= *(long *)data
;
418 if (realtype
== OUT_REL2ADR
)
420 if (realtype
== OUT_REL4ADR
)
422 ieee_write_fixup (segment
, wrt
, seg
, size
, realtype
,ldata
);
424 ieee_write_word (seg
, ldata
);
426 ieee_write_dword (seg
, ldata
);
428 else if (realtype
== OUT_RESERVE
) {
430 ieee_write_byte(seg
,0);
434 static void ieee_data_new(struct ieeeSection
*segto
) {
437 segto
->data
= segto
->datacurr
= nasm_malloc(sizeof(*(segto
->datacurr
)));
439 segto
->datacurr
= segto
->datacurr
->next
= nasm_malloc(sizeof(*(segto
->datacurr
)));
440 segto
->datacurr
->next
= NULL
;
445 * this routine is unalduterated bloatware. I usually don't do this
446 * but I might as well see what it is like on a harmless program.
447 * If anyone wants to optimize this is a good canditate!
449 static void ieee_write_fixup (long segment
, long wrt
, struct ieeeSection
* segto
,
450 int size
, long realtype
, long offset
) {
451 struct ieeeSection
*target
;
454 /* Don't put a fixup for things NASM can calculate */
455 if (wrt
== NO_SEG
&& segment
== NO_SEG
)
459 /* if it is a WRT offset */
464 s
.id1
= -(wrt
-SEG_ABS
);
466 if (wrt
%2 && realtype
!= OUT_REL2ADR
&& realtype
!= OUT_REL4ADR
) {
469 for (target
= seghead
; target
; target
= target
->next
)
470 if (target
->index
== wrt
)
473 s
.id1
= target
->ieee_index
;
474 for (target
= seghead
; target
; target
= target
->next
)
475 if (target
->index
== segment
)
479 s
.id2
= target
->ieee_index
;
482 * Now we assume the segment field is being used
483 * to hold an extern index
486 struct ExtBack
*eb
= ebhead
;
487 while (i
> EXT_BLKSIZ
) {
494 /* if we have an extern decide the type and make a record
499 s
.id2
= eb
->index
[i
];
503 "Source of WRT must be an offset");
509 "unrecognised WRT value in ieee_write_fixup");
512 error(ERR_NONFATAL
,"target of WRT must be a section ");
515 ieee_install_fixup(segto
,&s
);
518 /* Pure segment fixup ? */
519 if (segment
!= NO_SEG
) {
522 if (segment
>= SEG_ABS
) {
523 /* absolute far segment fixup */
524 s
.id1
= -(segment
-~SEG_ABS
);
526 else if (segment
% 2) {
527 /* fixup to named segment */
529 for (target
= seghead
; target
; target
= target
->next
)
530 if (target
->index
== segment
-1)
533 s
.id1
= target
->ieee_index
;
536 * Now we assume the segment field is being used
537 * to hold an extern index
540 struct ExtBack
*eb
= ebhead
;
541 while (i
> EXT_BLKSIZ
) {
548 /* if we have an extern decide the type and make a record
551 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
552 error(ERR_PANIC
,"Segment of a rel not supported in ieee_write_fixup");
555 /* If we want the segment */
558 s
.id1
= eb
->index
[i
];
563 /* If we get here the seg value doesn't make sense */
565 "unrecognised segment value in ieee_write_fixup");
569 /* Assume we are offsetting directly from a section
570 * So look up the target segment
572 for (target
= seghead
; target
; target
= target
->next
)
573 if (target
->index
== segment
)
576 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
577 /* PC rel to a known offset */
578 s
.id1
= target
->ieee_index
;
584 /* We were offsetting from a seg */
585 s
.id1
= target
->ieee_index
;
593 * Now we assume the segment field is being used
594 * to hold an extern index
597 struct ExtBack
*eb
= ebhead
;
598 while (i
> EXT_BLKSIZ
) {
605 /* if we have an extern decide the type and make a record
608 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
611 s
.id1
= eb
->index
[i
];
614 /* else we want the external offset */
617 s
.id1
= eb
->index
[i
];
622 /* If we get here the seg value doesn't make sense */
624 "unrecognised segment value in ieee_write_fixup");
627 if (size
!= 2 && s
.ftype
== FT_SEG
)
628 error(ERR_NONFATAL
, "IEEE format can only handle 2-byte"
629 " segment base references");
631 ieee_install_fixup(segto
,&s
);
634 /* should never get here */
636 static void ieee_install_fixup(struct ieeeSection
*seg
, 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 long ieee_segment (char *name
, int pass
, int *bits
) {
656 * We call the label manager here to define a name for the new
657 * segment, and when our _own_ label-definition stub gets
658 * called in return, it should register the new segment name
659 * using the pointer it gets passed. That way we save memory,
660 * by sponging off the label manager.
666 return seghead
->index
;
668 struct ieeeSection
*seg
;
669 int ieee_idx
, attrs
, rn_error
;
673 * Look for segment attributes.
677 name
++; /* hack, but a documented one */
679 while (*p
&& !isspace(*p
))
683 while (*p
&& isspace(*p
))
687 while (*p
&& !isspace(*p
))
691 while (*p
&& isspace(*p
))
699 for (seg
= seghead
; seg
; seg
= seg
->next
) {
701 if (!strcmp(seg
->name
, name
)) {
702 if (attrs
> 0 && pass
== 1)
703 error(ERR_WARNING
, "segment attributes specified on"
704 " redeclaration of segment: ignoring");
713 *segtail
= seg
= nasm_malloc(sizeof(*seg
));
715 segtail
= &seg
->next
;
716 seg
->index
= seg_alloc();
717 seg
->ieee_index
= ieee_idx
;
721 seg
->align
= 1; /* default */
722 seg
->use32
= *bits
== 32; /* default to user spec */
723 seg
->combine
= CMB_PUBLIC
; /* default */
725 seg
->pubtail
= &seg
->pubhead
;
729 seg
->loctail
= &seg
->lochead
;
732 * Process the segment attributes.
740 * `p' contains a segment attribute.
742 if (!nasm_stricmp(p
, "private"))
743 seg
->combine
= CMB_PRIVATE
;
744 else if (!nasm_stricmp(p
, "public"))
745 seg
->combine
= CMB_PUBLIC
;
746 else if (!nasm_stricmp(p
, "common"))
747 seg
->combine
= CMB_COMMON
;
748 else if (!nasm_stricmp(p
, "use16"))
750 else if (!nasm_stricmp(p
, "use32"))
752 else if (!nasm_strnicmp(p
, "align=", 6)) {
753 seg
->align
= readnum(p
+6, &rn_error
);
758 error (ERR_NONFATAL
, "segment alignment should be"
761 switch ((int) seg
->align
) {
773 error(ERR_NONFATAL
, "invalid alignment value %d",
778 } else if (!nasm_strnicmp(p
, "absolute=", 9)) {
779 seg
->align
= SEG_ABS
+ readnum(p
+9, &rn_error
);
781 error (ERR_NONFATAL
, "argument to `absolute' segment"
782 " attribute should be numeric");
786 ieee_seg_needs_update
= seg
;
787 if (seg
->align
>= SEG_ABS
)
788 deflabel (name
, NO_SEG
, seg
->align
- SEG_ABS
,
789 NULL
, FALSE
, FALSE
, &of_ieee
, error
);
791 deflabel (name
, seg
->index
+1, 0L,
792 NULL
, FALSE
, FALSE
, &of_ieee
, error
);
793 ieee_seg_needs_update
= NULL
;
803 * directives supported
805 static int ieee_directive (char *directive
, char *value
, int pass
)
810 if (!strcmp(directive
, "uppercase")) {
811 ieee_uppercase
= TRUE
;
817 * Return segment data
819 static long ieee_segbase (long segment
)
821 struct ieeeSection
*seg
;
824 * Find the segment in our list.
826 for (seg
= seghead
; seg
; seg
= seg
->next
)
827 if (seg
->index
== segment
-1)
831 return segment
; /* not one of ours - leave it alone */
833 if (seg
->align
>= SEG_ABS
)
834 return seg
->align
; /* absolute segment */
836 return segment
; /* no special treatment */
841 static void ieee_filename (char *inname
, char *outname
, efunc error
) {
842 strcpy(ieee_infile
, inname
);
843 standard_extension (inname
, outname
, ".o", error
);
846 static void ieee_write_file (int debuginfo
) {
850 struct ieeeSection
*seg
;
851 struct ieeePublic
*pub
, *loc
;
852 struct ieeeExternal
*ext
;
853 struct ieeeObjData
*data
;
854 struct ieeeFixupp
*fix
;
856 static char boast
[] = "The Netwide Assembler " NASM_VER
;
860 * Write the module header
862 ieee_putascii("MBFNASM,%02X%s.\r\n",strlen(ieee_infile
),ieee_infile
);
865 * Write the NASM boast comment.
867 ieee_putascii("CO0,%02X%s.\r\n",strlen(boast
),boast
);
870 * write processor-specific information
872 ieee_putascii("AD8,4,L.\r\n");
878 thetime
= localtime(&reltime
);
879 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\r\n",
880 1900+thetime
->tm_year
,thetime
->tm_mon
+1,thetime
->tm_mday
,
881 thetime
->tm_hour
, thetime
->tm_min
, thetime
->tm_sec
);
883 * if debugging, dump file names
885 for (fn
= fnhead
; fn
&& debuginfo
; fn
= fn
->next
) {
886 ieee_putascii("C0105,%02X%s.\r\n",strlen(fn
->name
),fn
->name
);
889 ieee_putascii("CO101,07ENDHEAD.\r\n");
891 * the standard doesn't specify when to put checksums,
892 * we'll just do it periodically.
897 * Write the section headers
900 if (!debuginfo
&& !strcmp(seg
->name
,"??LINE"))
905 switch(seg
->combine
) {
917 ieee_unqualified_name(buf
,seg
->name
);
918 if (seg
->align
>= SEG_ABS
) {
919 ieee_putascii("ST%X,A,%02X%s.\r\n",seg
->ieee_index
,strlen(buf
), buf
);
920 ieee_putascii("ASL%X,%lX.\r\n",seg
->ieee_index
, (seg
->align
- SEG_ABS
)*16);
923 ieee_putascii("ST%X,%c,%02X%s.\r\n",seg
->ieee_index
,attrib
,strlen(buf
), buf
);
924 ieee_putascii("SA%X,%lX.\r\n",seg
->ieee_index
,seg
->align
);
925 ieee_putascii("ASS%X,%X.\r\n",seg
->ieee_index
, seg
->currentpos
);
930 * write the start address if there is one
932 if (ieee_entry_seg
) {
933 for (seg
= seghead
; seg
; seg
= seg
->next
)
934 if (seg
->index
== ieee_entry_seg
)
937 error(ERR_PANIC
,"Start address records are incorrect");
939 ieee_putascii("ASG,R%X,%lX,+.\r\n",seg
->ieee_index
, ieee_entry_ofs
);
948 for (seg
= seghead
; seg
; seg
= seg
->next
) {
949 for (pub
= seg
->pubhead
; pub
; pub
= pub
->next
) {
951 ieee_unqualified_name(buf
,pub
->name
);
952 ieee_putascii("NI%X,%02X%s.\r\n",i
, strlen(buf
), buf
);
953 if (pub
->segment
== -1)
954 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i
, pub
->index
,pub
->offset
);
956 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i
, pub
->segment
*16,pub
->offset
);
958 if (pub
->type
>= 0x100)
959 ieee_putascii("ATI%X,T%X.\r\n", i
, pub
->type
- 0x100);
961 ieee_putascii("ATI%X,%X.\r\n", i
, pub
->type
);
969 ieee_unqualified_name(buf
,pub
->name
);
970 ieee_putascii("NI%X,%02X%s.\r\n",i
, strlen(buf
), buf
);
971 if (pub
->segment
== -1)
972 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i
, pub
->index
,pub
->offset
);
974 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i
, pub
->segment
*16,pub
->offset
);
976 if (pub
->type
>= 0x100)
977 ieee_putascii("ATI%X,T%X.\r\n", i
, pub
->type
- 0x100);
979 ieee_putascii("ATI%X,%X.\r\n", i
, pub
->type
);
984 * Write the externals
990 ieee_unqualified_name(buf
,ext
->name
);
991 ieee_putascii("NX%X,%02X%s.\r\n",i
++, strlen(buf
), buf
);
997 * IEEE doesn't have a standard pass break record
998 * so use the ladsoft variant
1000 ieee_putascii("CO100,06ENDSYM.\r\n");
1006 for (arr
= arrhead
; arr
&& debuginfo
; arr
= arr
->next
) {
1007 ieee_putascii("TY%X,20,%X,%lX.\r\n",i
++,arr
->basetype
,arr
->size
);
1013 for (seg
= seghead
; seg
&& debuginfo
; seg
= seg
->next
) {
1014 for (loc
= seg
->lochead
; loc
; loc
= loc
->next
) {
1016 ieee_unqualified_name(buf
,loc
->name
);
1017 ieee_putascii("NN%X,%02X%s.\r\n",i
, strlen(buf
), buf
);
1018 if (loc
->segment
== -1)
1019 ieee_putascii("ASN%X,R%X,%lX,+.\r\n", i
, loc
->index
,loc
->offset
);
1021 ieee_putascii("ASN%X,%lX,%lX,+.\r\n", i
, loc
->segment
*16,loc
->offset
);
1023 if (loc
->type
>= 0x100)
1024 ieee_putascii("ATN%X,T%X.\r\n", i
, loc
->type
- 0x100);
1026 ieee_putascii("ATN%X,%X.\r\n", i
, loc
->type
);
1032 * put out section data;
1035 if (!debuginfo
&& !strcmp(seg
->name
,"??LINE"))
1038 if (seg
->currentpos
) {
1041 ieee_putascii("SB%X.\r\n",seg
->ieee_index
);
1044 size
= HUNKSIZE
- (org
%HUNKSIZE
);
1045 size
= size
+org
> seg
->currentpos
? seg
->currentpos
-org
: size
;
1046 size
= fix
->offset
- org
> size
? size
: fix
->offset
-org
;
1047 org
= ieee_putld(org
,org
+size
,data
->data
);
1048 if (org
% HUNKSIZE
== 0)
1050 if (org
== fix
->offset
) {
1051 org
+= ieee_putlr(fix
);
1055 while (org
< seg
->currentpos
&& data
) {
1056 size
= seg
->currentpos
-org
> HUNKSIZE
? HUNKSIZE
: seg
->currentpos
- org
;
1057 org
= ieee_putld(org
,org
+size
,data
->data
);
1068 ieee_putascii("ME.\r\n");
1071 static void ieee_write_byte(struct ieeeSection
*seg
, int data
) {
1073 if (!(temp
= seg
->currentpos
++ % HUNKSIZE
))
1075 seg
->datacurr
->data
[temp
] = data
;
1078 static void ieee_write_word(struct ieeeSection
*seg
, int data
) {
1079 ieee_write_byte(seg
, data
& 0xFF);
1080 ieee_write_byte(seg
,(data
>> 8) & 0xFF);
1083 static void ieee_write_dword(struct ieeeSection
*seg
, long data
) {
1084 ieee_write_byte(seg
, data
& 0xFF);
1085 ieee_write_byte(seg
,(data
>> 8) & 0xFF);
1086 ieee_write_byte(seg
,(data
>> 16) & 0xFF);
1087 ieee_write_byte(seg
,(data
>> 24) & 0xFF);
1089 static void ieee_putascii(char *format
, ...)
1095 va_start(ap
, format
);
1096 vsprintf(buffer
, format
, ap
);
1098 for (i
=0; i
< l
; i
++)
1099 if ((buffer
[i
] & 0xff) > 31)
1100 checksum
+=buffer
[i
];
1102 fprintf(ofp
,buffer
);
1106 * put out a checksum record */
1107 static void ieee_putcs(int toclear
)
1110 ieee_putascii("CS.\r\n");
1115 ieee_putascii("CS%02X.\r\n",checksum
& 127);
1120 static long ieee_putld(long start
, long end
, unsigned char *buf
)
1125 val
= start
% HUNKSIZE
;
1126 /* fill up multiple lines */
1127 while (end
- start
>= LDPERLINE
) {
1129 ieee_putascii("LD");
1130 for (i
=0; i
< LDPERLINE
; i
++) {
1131 ieee_putascii("%02X",buf
[val
++]);
1134 ieee_putascii(".\r\n");
1136 /* if no partial lines */
1139 /* make a partial line */
1140 ieee_putascii("LD");
1141 while (start
< end
) {
1142 ieee_putascii("%02X",buf
[val
++]);
1145 ieee_putascii(".\r\n");
1148 static long ieee_putlr(struct ieeeFixupp
*p
)
1151 * To deal with the vagaries of segmentation the LADsoft linker
1152 * defines two types of segments: absolute and virtual. Note that
1153 * 'absolute' in this context is a different thing from the IEEE
1154 * definition of an absolute segment type, which is also supported. If a
1155 * sement is linked in virtual mode the low limit (L-var) is
1156 * subtracted from each R,X, and P variable which appears in an
1157 * expression, so that we can have relative offsets. Meanwhile
1158 * in the ABSOLUTE mode this subtraction is not done and
1159 * so we can use absolute offsets from 0. In the LADsoft linker
1160 * this configuration is not done in the assemblker source but in
1161 * a source the linker reads. Generally this type of thing only
1162 * becomes an issue if real mode code is used. A pure 32-bit linker could
1163 * get away without defining the virtual mode...
1170 sprintf(buf
,"%lX",-p
->id1
);
1172 sprintf(buf
,"L%X,10,/",p
->id1
);
1175 sprintf(buf
,"R%X,%lX,+",p
->id1
,p
->addend
);
1178 sprintf(buf
,"R%X,%lX,+,P,-,%X,-",p
->id1
,p
->addend
,p
->size
);
1183 sprintf(buf
,"R%X,%lX,+,L%X,+,%lX,-",p
->id2
,p
->addend
,p
->id2
,-p
->id1
*16);
1185 sprintf(buf
,"R%X,%lX,+,L%X,+,L%X,-",p
->id2
,p
->addend
,p
->id2
,p
->id1
);
1188 sprintf(buf
,"X%X",p
->id1
,p
->id1
);
1191 sprintf(buf
,"X%X,P,-,%X,-",p
->id1
,size
);
1194 /* We needed a non-ieee hack here.
1195 * We introduce the Y variable, which is the low
1196 * limit of the native segment the extern resides in
1198 sprintf(buf
,"Y%X,10,/",p
->id1
);
1202 sprintf(buf
,"X%X,Y%X,+,%lX,-",p
->id2
,p
->id2
,-p
->id1
*16);
1204 sprintf(buf
,"X%X,Y%X,+,L%X,-",p
->id2
,p
->id2
,p
->id1
);
1207 ieee_putascii("LR(%s,%lX).\r\n", buf
, size
);
1211 /* Dump all segment data (text and fixups )*/
1213 static void ieee_unqualified_name(char *dest
, char *source
)
1215 if (ieee_uppercase
) {
1217 *dest
++ = toupper(*source
++);
1220 else strcpy(dest
,source
);
1222 void dbgls_init(struct ofmt
* of
, void * id
, FILE * fp
, efunc error
)
1232 arrindex
= ARRAY_BOT
;
1235 ieee_segment("??LINE", 2, &tempint
);
1238 static void dbgls_cleanup(void)
1240 struct ieeeSection
*segtmp
;
1242 struct FileName
*fntemp
= fnhead
;
1243 fnhead
= fnhead
->next
;
1244 nasm_free (fntemp
->name
);
1247 for (segtmp
= seghead
; segtmp
; segtmp
= segtmp
->next
) {
1248 while (segtmp
->lochead
) {
1249 struct ieeePublic
*loctmp
= segtmp
->lochead
;
1250 segtmp
->lochead
= loctmp
->next
;
1251 nasm_free (loctmp
->name
);
1256 struct Array
*arrtmp
= arrhead
;
1257 arrhead
= arrhead
->next
;
1263 * because this routine is not bracketed in
1264 * the main program, this routine will be called even if there
1265 * is no request for debug info
1266 * so, we have to make sure the ??LINE segment is avaialbe
1267 * as the first segment when this debug format is selected
1269 static void dbgls_linnum (const char *lnfname
, long lineno
, long segto
)
1271 struct FileName
*fn
;
1272 struct ieeeSection
*seg
;
1274 if (segto
== NO_SEG
)
1278 * If `any_segs' is still FALSE, we must define a default
1282 int tempint
; /* ignored */
1283 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
1284 error (ERR_PANIC
, "strange segment conditions in OBJ driver");
1288 * Find the segment we are targetting.
1290 for (seg
= seghead
; seg
; seg
= seg
->next
)
1291 if (seg
->index
== segto
)
1294 error (ERR_PANIC
, "lineno directed to nonexistent segment?");
1296 for (fn
= fnhead
; fn
; fn
= fnhead
->next
) {
1297 if (!nasm_stricmp(lnfname
,fn
->name
))
1302 fn
= nasm_malloc ( sizeof( *fn
));
1303 fn
->name
= nasm_malloc ( strlen(lnfname
) + 1) ;
1305 strcpy (fn
->name
,lnfname
);
1310 ieee_write_byte(seghead
, fn
->index
);
1311 ieee_write_word(seghead
, lineno
);
1312 ieee_write_fixup (segto
, NO_SEG
, seghead
, 4,OUT_ADDRESS
,seg
->currentpos
);
1315 static void dbgls_deflabel (char *name
, long segment
,
1316 long offset
, int is_global
, char *special
)
1318 struct ieeeSection
*seg
;
1319 int used_special
= FALSE
; /* have we used the special text? */
1324 * If it's a special-retry from pass two, discard it.
1330 * First check for the double-period, signifying something
1333 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
1340 if (ieee_seg_needs_update
)
1342 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
1345 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
1350 * If `any_segs' is still FALSE, we might need to define a
1351 * default segment, if they're trying to declare a label in
1352 * `first_seg'. But the label should exist due to a prior
1353 * call to ieee_deflabel so we can skip that.
1356 for (seg
= seghead
; seg
; seg
= seg
->next
)
1357 if (seg
->index
== segment
) {
1358 struct ieeePublic
*loc
;
1360 * Case (ii). Maybe MODPUB someday?
1363 last_defined
= loc
= nasm_malloc (sizeof(*loc
));
1364 *seg
->loctail
= loc
;
1365 seg
->loctail
= &loc
->next
;
1367 loc
->name
= nasm_strdup(name
);
1368 loc
->offset
= offset
;
1370 loc
->index
= seg
->ieee_index
;
1374 static void dbgls_typevalue (long type
)
1376 int elem
= TYM_ELEMENTS(type
);
1377 type
= TYM_TYPE(type
);
1384 last_defined
->type
= 1; /* unsigned char */
1387 last_defined
->type
= 3; /* unsigned word */
1390 last_defined
->type
= 5; /* unsigned dword */
1393 last_defined
->type
= 9; /* float */
1396 last_defined
->type
= 10; /* qword */
1399 last_defined
->type
= 11; /* TBYTE */
1402 last_defined
->type
= 0x10; /* near label */
1407 struct Array
*arrtmp
= nasm_malloc (sizeof(*arrtmp
));
1408 int vtype
= last_defined
->type
;
1409 arrtmp
->size
= elem
;
1410 arrtmp
->basetype
= vtype
;
1411 arrtmp
->next
= NULL
;
1412 last_defined
->type
= arrindex
++ + 0x100;
1414 arrtail
= & (arrtmp
->next
);
1416 last_defined
= NULL
;
1418 static void dbgls_output (int output_type
, void *param
)
1423 static struct dfmt ladsoft_debug_form
= {
1424 "LADsoft Debug Records",
1434 static struct dfmt
*ladsoft_debug_arr
[3] = {
1435 &ladsoft_debug_form
,
1439 struct ofmt of_ieee
= {
1440 "IEEE-695 (LADsoft variant) object file format",
1457 #endif /* OF_IEEE */