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
;
672 int ieee_idx
, attrs
, rn_error
;
676 * Look for segment attributes.
680 name
++; /* hack, but a documented one */
682 while (*p
&& !isspace(*p
))
686 while (*p
&& isspace(*p
))
690 while (*p
&& !isspace(*p
))
694 while (*p
&& isspace(*p
))
702 for (seg
= seghead
; seg
; seg
= seg
->next
) {
704 if (!strcmp(seg
->name
, name
)) {
705 if (attrs
> 0 && pass
== 1)
706 error(ERR_WARNING
, "segment attributes specified on"
707 " redeclaration of segment: ignoring");
716 *segtail
= seg
= nasm_malloc(sizeof(*seg
));
718 segtail
= &seg
->next
;
719 seg
->index
= seg_alloc();
720 seg
->ieee_index
= ieee_idx
;
724 seg
->align
= 1; /* default */
725 seg
->use32
= *bits
== 32; /* default to user spec */
726 seg
->combine
= CMB_PUBLIC
; /* default */
728 seg
->pubtail
= &seg
->pubhead
;
732 seg
->loctail
= &seg
->lochead
;
735 * Process the segment attributes.
744 * `p' contains a segment attribute.
746 if (!nasm_stricmp(p
, "private"))
747 seg
->combine
= CMB_PRIVATE
;
748 else if (!nasm_stricmp(p
, "public"))
749 seg
->combine
= CMB_PUBLIC
;
750 else if (!nasm_stricmp(p
, "common"))
751 seg
->combine
= CMB_COMMON
;
752 else if (!nasm_stricmp(p
, "use16"))
754 else if (!nasm_stricmp(p
, "use32"))
756 else if (!nasm_strnicmp(p
, "align=", 6)) {
757 seg
->align
= readnum(p
+ 6, &rn_error
);
762 error(ERR_NONFATAL
, "segment alignment should be"
765 switch ((int)seg
->align
) {
777 error(ERR_NONFATAL
, "invalid alignment value %d",
782 } else if (!nasm_strnicmp(p
, "absolute=", 9)) {
783 seg
->align
= SEG_ABS
+ readnum(p
+ 9, &rn_error
);
785 error(ERR_NONFATAL
, "argument to `absolute' segment"
786 " attribute should be numeric");
790 ieee_seg_needs_update
= seg
;
791 if (seg
->align
>= SEG_ABS
)
792 deflabel(name
, NO_SEG
, seg
->align
- SEG_ABS
,
793 NULL
, false, false, &of_ieee
, error
);
795 deflabel(name
, seg
->index
+ 1, 0L,
796 NULL
, false, false, &of_ieee
, error
);
797 ieee_seg_needs_update
= NULL
;
808 * directives supported
810 static int ieee_directive(char *directive
, char *value
, int pass
)
815 if (!strcmp(directive
, "uppercase")) {
816 ieee_uppercase
= true;
823 * Return segment data
825 static int32_t ieee_segbase(int32_t segment
)
827 struct ieeeSection
*seg
;
830 * Find the segment in our list.
832 for (seg
= seghead
; seg
; seg
= seg
->next
)
833 if (seg
->index
== segment
- 1)
837 return segment
; /* not one of ours - leave it alone */
839 if (seg
->align
>= SEG_ABS
)
840 return seg
->align
; /* absolute segment */
842 return segment
; /* no special treatment */
848 static void ieee_filename(char *inname
, char *outname
, efunc error
)
850 strcpy(ieee_infile
, inname
);
851 standard_extension(inname
, outname
, ".o", error
);
854 static void ieee_write_file(int debuginfo
)
859 struct ieeeSection
*seg
;
860 struct ieeePublic
*pub
, *loc
;
861 struct ieeeExternal
*ext
;
862 struct ieeeObjData
*data
;
863 struct ieeeFixupp
*fix
;
865 static char boast
[] = "The Netwide Assembler " NASM_VER
;
869 * Write the module header
871 ieee_putascii("MBFNASM,%02X%s.\r\n", strlen(ieee_infile
), ieee_infile
);
874 * Write the NASM boast comment.
876 ieee_putascii("CO0,%02X%s.\r\n", strlen(boast
), boast
);
879 * write processor-specific information
881 ieee_putascii("AD8,4,L.\r\n");
887 thetime
= localtime(&reltime
);
888 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\r\n",
889 1900 + thetime
->tm_year
, thetime
->tm_mon
+ 1,
890 thetime
->tm_mday
, thetime
->tm_hour
, thetime
->tm_min
,
893 * if debugging, dump file names
895 for (fn
= fnhead
; fn
&& debuginfo
; fn
= fn
->next
) {
896 ieee_putascii("C0105,%02X%s.\r\n", strlen(fn
->name
), fn
->name
);
899 ieee_putascii("CO101,07ENDHEAD.\r\n");
901 * the standard doesn't specify when to put checksums,
902 * we'll just do it periodically.
907 * Write the section headers
910 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
915 switch (seg
->combine
) {
927 ieee_unqualified_name(buf
, seg
->name
);
928 if (seg
->align
>= SEG_ABS
) {
929 ieee_putascii("ST%X,A,%02X%s.\r\n", seg
->ieee_index
,
931 ieee_putascii("ASL%X,%lX.\r\n", seg
->ieee_index
,
932 (seg
->align
- SEG_ABS
) * 16);
934 ieee_putascii("ST%X,%c,%02X%s.\r\n", seg
->ieee_index
, attrib
,
936 ieee_putascii("SA%X,%lX.\r\n", seg
->ieee_index
, seg
->align
);
937 ieee_putascii("ASS%X,%X.\r\n", seg
->ieee_index
,
943 * write the start address if there is one
945 if (ieee_entry_seg
) {
946 for (seg
= seghead
; seg
; seg
= seg
->next
)
947 if (seg
->index
== ieee_entry_seg
)
950 error(ERR_PANIC
, "Start address records are incorrect");
952 ieee_putascii("ASG,R%X,%lX,+.\r\n", seg
->ieee_index
,
961 for (seg
= seghead
; seg
; seg
= seg
->next
) {
962 for (pub
= seg
->pubhead
; pub
; pub
= pub
->next
) {
964 ieee_unqualified_name(buf
, pub
->name
);
965 ieee_putascii("NI%X,%02X%s.\r\n", i
, strlen(buf
), buf
);
966 if (pub
->segment
== -1)
967 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i
, pub
->index
,
970 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i
, pub
->segment
* 16,
973 if (pub
->type
>= 0x100)
974 ieee_putascii("ATI%X,T%X.\r\n", i
, pub
->type
- 0x100);
976 ieee_putascii("ATI%X,%X.\r\n", i
, pub
->type
);
985 ieee_unqualified_name(buf
, pub
->name
);
986 ieee_putascii("NI%X,%02X%s.\r\n", i
, strlen(buf
), buf
);
987 if (pub
->segment
== -1)
988 ieee_putascii("ASI%X,R%X,%lX,+.\r\n", i
, pub
->index
,
991 ieee_putascii("ASI%X,%lX,%lX,+.\r\n", i
, pub
->segment
* 16,
994 if (pub
->type
>= 0x100)
995 ieee_putascii("ATI%X,T%X.\r\n", i
, pub
->type
- 0x100);
997 ieee_putascii("ATI%X,%X.\r\n", i
, pub
->type
);
1003 * Write the externals
1009 ieee_unqualified_name(buf
, ext
->name
);
1010 ieee_putascii("NX%X,%02X%s.\r\n", i
++, strlen(buf
), buf
);
1016 * IEEE doesn't have a standard pass break record
1017 * so use the ladsoft variant
1019 ieee_putascii("CO100,06ENDSYM.\r\n");
1025 for (arr
= arrhead
; arr
&& debuginfo
; arr
= arr
->next
) {
1026 ieee_putascii("TY%X,20,%X,%lX.\r\n", i
++, arr
->basetype
,
1033 for (seg
= seghead
; seg
&& debuginfo
; seg
= seg
->next
) {
1034 for (loc
= seg
->lochead
; loc
; loc
= loc
->next
) {
1036 ieee_unqualified_name(buf
, loc
->name
);
1037 ieee_putascii("NN%X,%02X%s.\r\n", i
, strlen(buf
), buf
);
1038 if (loc
->segment
== -1)
1039 ieee_putascii("ASN%X,R%X,%lX,+.\r\n", i
, loc
->index
,
1042 ieee_putascii("ASN%X,%lX,%lX,+.\r\n", i
, loc
->segment
* 16,
1045 if (loc
->type
>= 0x100)
1046 ieee_putascii("ATN%X,T%X.\r\n", i
, loc
->type
- 0x100);
1048 ieee_putascii("ATN%X,%X.\r\n", i
, loc
->type
);
1055 * put out section data;
1058 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
1061 if (seg
->currentpos
) {
1062 int32_t size
, org
= 0;
1064 ieee_putascii("SB%X.\r\n", seg
->ieee_index
);
1067 size
= HUNKSIZE
- (org
% HUNKSIZE
);
1070 seg
->currentpos
? seg
->currentpos
- org
: size
;
1071 size
= fix
->offset
- org
> size
? size
: fix
->offset
- org
;
1072 org
= ieee_putld(org
, org
+ size
, data
->data
);
1073 if (org
% HUNKSIZE
== 0)
1075 if (org
== fix
->offset
) {
1076 org
+= ieee_putlr(fix
);
1080 while (org
< seg
->currentpos
&& data
) {
1082 seg
->currentpos
- org
>
1083 HUNKSIZE
? HUNKSIZE
: seg
->currentpos
- org
;
1084 org
= ieee_putld(org
, org
+ size
, data
->data
);
1095 ieee_putascii("ME.\r\n");
1098 static void ieee_write_byte(struct ieeeSection
*seg
, int data
)
1101 if (!(temp
= seg
->currentpos
++ % HUNKSIZE
))
1103 seg
->datacurr
->data
[temp
] = data
;
1106 static void ieee_write_word(struct ieeeSection
*seg
, int data
)
1108 ieee_write_byte(seg
, data
& 0xFF);
1109 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1112 static void ieee_write_dword(struct ieeeSection
*seg
, int32_t data
)
1114 ieee_write_byte(seg
, data
& 0xFF);
1115 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1116 ieee_write_byte(seg
, (data
>> 16) & 0xFF);
1117 ieee_write_byte(seg
, (data
>> 24) & 0xFF);
1119 static void ieee_putascii(char *format
, ...)
1125 va_start(ap
, format
);
1126 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1128 for (i
= 0; i
< l
; i
++)
1129 if ((buffer
[i
] & 0xff) > 31)
1130 checksum
+= buffer
[i
];
1132 fprintf(ofp
, buffer
);
1136 * put out a checksum record */
1137 static void ieee_putcs(int toclear
)
1140 ieee_putascii("CS.\r\n");
1144 ieee_putascii("CS%02X.\r\n", checksum
& 127);
1149 static int32_t ieee_putld(int32_t start
, int32_t end
, uint8_t *buf
)
1154 val
= start
% HUNKSIZE
;
1155 /* fill up multiple lines */
1156 while (end
- start
>= LDPERLINE
) {
1158 ieee_putascii("LD");
1159 for (i
= 0; i
< LDPERLINE
; i
++) {
1160 ieee_putascii("%02X", buf
[val
++]);
1163 ieee_putascii(".\r\n");
1165 /* if no partial lines */
1168 /* make a partial line */
1169 ieee_putascii("LD");
1170 while (start
< end
) {
1171 ieee_putascii("%02X", buf
[val
++]);
1174 ieee_putascii(".\r\n");
1177 static int32_t ieee_putlr(struct ieeeFixupp
*p
)
1180 * To deal with the vagaries of segmentation the LADsoft linker
1181 * defines two types of segments: absolute and virtual. Note that
1182 * 'absolute' in this context is a different thing from the IEEE
1183 * definition of an absolute segment type, which is also supported. If a
1184 * sement is linked in virtual mode the low limit (L-var) is
1185 * subtracted from each R,X, and P variable which appears in an
1186 * expression, so that we can have relative offsets. Meanwhile
1187 * in the ABSOLUTE mode this subtraction is not done and
1188 * so we can use absolute offsets from 0. In the LADsoft linker
1189 * this configuration is not done in the assemblker source but in
1190 * a source the linker reads. Generally this type of thing only
1191 * becomes an issue if real mode code is used. A pure 32-bit linker could
1192 * get away without defining the virtual mode...
1195 int32_t size
= p
->size
;
1199 sprintf(buf
, "%"PRIX32
"", -p
->id1
);
1201 sprintf(buf
, "L%"PRIX32
",10,/", p
->id1
);
1204 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+", p
->id1
, p
->addend
);
1207 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,P,-,%X,-", p
->id1
, p
->addend
, p
->size
);
1212 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->addend
,
1213 p
->id2
, -p
->id1
* 16);
1215 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->addend
,
1219 sprintf(buf
, "X%"PRIX32
"", p
->id1
);
1222 sprintf(buf
, "X%"PRIX32
",P,-,%"PRIX32
",-", p
->id1
, size
);
1225 /* We needed a non-ieee hack here.
1226 * We introduce the Y variable, which is the low
1227 * limit of the native segment the extern resides in
1229 sprintf(buf
, "Y%"PRIX32
",10,/", p
->id1
);
1233 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->id2
,
1236 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->id2
, p
->id1
);
1239 ieee_putascii("LR(%s,%"PRIX32
").\r\n", buf
, size
);
1244 /* Dump all segment data (text and fixups )*/
1246 static void ieee_unqualified_name(char *dest
, char *source
)
1248 if (ieee_uppercase
) {
1250 *dest
++ = toupper(*source
++);
1253 strcpy(dest
, source
);
1255 void dbgls_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1265 arrindex
= ARRAY_BOT
;
1268 ieee_segment("??LINE", 2, &tempint
);
1271 static void dbgls_cleanup(void)
1273 struct ieeeSection
*segtmp
;
1275 struct FileName
*fntemp
= fnhead
;
1276 fnhead
= fnhead
->next
;
1277 nasm_free(fntemp
->name
);
1280 for (segtmp
= seghead
; segtmp
; segtmp
= segtmp
->next
) {
1281 while (segtmp
->lochead
) {
1282 struct ieeePublic
*loctmp
= segtmp
->lochead
;
1283 segtmp
->lochead
= loctmp
->next
;
1284 nasm_free(loctmp
->name
);
1289 struct Array
*arrtmp
= arrhead
;
1290 arrhead
= arrhead
->next
;
1296 * because this routine is not bracketed in
1297 * the main program, this routine will be called even if there
1298 * is no request for debug info
1299 * so, we have to make sure the ??LINE segment is avaialbe
1300 * as the first segment when this debug format is selected
1302 static void dbgls_linnum(const char *lnfname
, int32_t lineno
, int32_t segto
)
1304 struct FileName
*fn
;
1305 struct ieeeSection
*seg
;
1307 if (segto
== NO_SEG
)
1311 * If `any_segs' is still false, we must define a default
1315 int tempint
; /* ignored */
1316 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
1317 error(ERR_PANIC
, "strange segment conditions in OBJ driver");
1321 * Find the segment we are targetting.
1323 for (seg
= seghead
; seg
; seg
= seg
->next
)
1324 if (seg
->index
== segto
)
1327 error(ERR_PANIC
, "lineno directed to nonexistent segment?");
1329 for (fn
= fnhead
; fn
; fn
= fn
->next
) {
1330 if (!nasm_stricmp(lnfname
, fn
->name
))
1335 fn
= nasm_malloc(sizeof(*fn
));
1336 fn
->name
= nasm_malloc(strlen(lnfname
) + 1);
1338 strcpy(fn
->name
, lnfname
);
1343 ieee_write_byte(seghead
, fn
->index
);
1344 ieee_write_word(seghead
, lineno
);
1345 ieee_write_fixup(segto
, NO_SEG
, seghead
, 4, OUT_ADDRESS
,
1349 static void dbgls_deflabel(char *name
, int32_t segment
,
1350 int32_t offset
, int is_global
, char *special
)
1352 struct ieeeSection
*seg
;
1354 /* Keep compiler from warning about special */
1358 * If it's a special-retry from pass two, discard it.
1364 * First check for the double-period, signifying something
1367 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
1374 if (ieee_seg_needs_update
)
1376 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
1379 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
1384 * If `any_segs' is still false, we might need to define a
1385 * default segment, if they're trying to declare a label in
1386 * `first_seg'. But the label should exist due to a prior
1387 * call to ieee_deflabel so we can skip that.
1390 for (seg
= seghead
; seg
; seg
= seg
->next
)
1391 if (seg
->index
== segment
) {
1392 struct ieeePublic
*loc
;
1394 * Case (ii). Maybe MODPUB someday?
1397 last_defined
= loc
= nasm_malloc(sizeof(*loc
));
1398 *seg
->loctail
= loc
;
1399 seg
->loctail
= &loc
->next
;
1401 loc
->name
= nasm_strdup(name
);
1402 loc
->offset
= offset
;
1404 loc
->index
= seg
->ieee_index
;
1408 static void dbgls_typevalue(int32_t type
)
1410 int elem
= TYM_ELEMENTS(type
);
1411 type
= TYM_TYPE(type
);
1418 last_defined
->type
= 1; /* uint8_t */
1421 last_defined
->type
= 3; /* unsigned word */
1424 last_defined
->type
= 5; /* unsigned dword */
1427 last_defined
->type
= 9; /* float */
1430 last_defined
->type
= 10; /* qword */
1433 last_defined
->type
= 11; /* TBYTE */
1436 last_defined
->type
= 0x10; /* near label */
1441 struct Array
*arrtmp
= nasm_malloc(sizeof(*arrtmp
));
1442 int vtype
= last_defined
->type
;
1443 arrtmp
->size
= elem
;
1444 arrtmp
->basetype
= vtype
;
1445 arrtmp
->next
= NULL
;
1446 last_defined
->type
= arrindex
++ + 0x100;
1448 arrtail
= &(arrtmp
->next
);
1450 last_defined
= NULL
;
1452 static void dbgls_output(int output_type
, void *param
)
1457 static struct dfmt ladsoft_debug_form
= {
1458 "LADsoft Debug Records",
1468 static struct dfmt
*ladsoft_debug_arr
[3] = {
1469 &ladsoft_debug_form
,
1473 struct ofmt of_ieee
= {
1474 "IEEE-695 (LADsoft variant) object file format",
1491 #endif /* OF_IEEE */