1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outieee.c output routines for the Netwide Assembler to produce
36 * IEEE-std object files
39 /* notes: I have tried to make this correspond to the IEEE version
40 * of the standard, specifically the primary ASCII version. It should
41 * be trivial to create the binary version given this source (which is
42 * one of MANY things that have to be done to make this correspond to
43 * the hp-microtek version of the standard).
45 * 16-bit support is assumed to use 24-bit addresses
46 * The linker can sort out segmentation-specific stuff
47 * if it keeps track of externals
48 * in terms of being relative to section bases
50 * A non-standard variable type, the 'Yn' variable, has been introduced.
51 * Basically it is a reference to extern 'n'- denoting the low limit
52 * (L-variable) of the section that extern 'n' is defined in. Like the
53 * x variable, there may be no explicit assignment to it, it is derived
54 * from the public definition corresponding to the extern name. This
55 * is required because the one thing the mufom guys forgot to do well was
56 * take into account segmented architectures.
58 * I use comment classes for various things and these are undefined by
61 * Debug info should be considered totally non-standard (local labels are
62 * standard but linenum records are not covered by the standard.
63 * Type defs have the standard format but absolute meanings for ordinal
64 * types are not covered by the standard.)
66 * David Lindauer, LADsoft
74 #include <stdarg.h> /* Note: we need the ANSI version of stdarg.h */
80 #include "output/outform.h"
81 #include "output/outlib.h"
87 static char ieee_infile
[FILENAME_MAX
];
88 static int ieee_uppercase
;
93 #define HUNKSIZE 1024 /* Size of the data hunk */
94 #define EXT_BLKSIZ 512
95 #define LDPERLINE 32 /* bytes per line in output */
100 struct LineNumber
*next
;
101 struct ieeeSection
*segment
;
106 static struct FileName
{
107 struct FileName
*next
;
112 static struct Array
{
116 } *arrhead
, **arrtail
;
118 static struct ieeePublic
{
119 struct ieeePublic
*next
;
122 int32_t segment
; /* only if it's far-absolute */
124 int type
; /* for debug purposes */
125 } *fpubhead
, **fpubtail
, *last_defined
;
127 static struct ieeeExternal
{
128 struct ieeeExternal
*next
;
131 } *exthead
, **exttail
;
133 static int externals
;
135 static struct ExtBack
{
136 struct ExtBack
*next
;
137 int index
[EXT_BLKSIZ
];
140 /* NOTE: the first segment MUST be the lineno segment */
141 static struct ieeeSection
{
142 struct ieeeObjData
*data
, *datacurr
;
143 struct ieeeSection
*next
;
144 struct ieeeFixupp
*fptr
, *flptr
;
145 int32_t index
; /* the NASM segment id */
146 int32_t ieee_index
; /* the OBJ-file segment index */
148 int32_t align
; /* can be SEG_ABS + absolute addr */
155 int32_t use32
; /* is this segment 32-bit? */
156 struct ieeePublic
*pubhead
, **pubtail
, *lochead
, **loctail
;
158 } *seghead
, **segtail
, *ieee_seg_needs_update
;
161 struct ieeeObjData
*next
;
162 uint8_t data
[HUNKSIZE
];
166 struct ieeeFixupp
*next
;
184 static int32_t ieee_entry_seg
, ieee_entry_ofs
;
187 extern struct ofmt of_ieee
;
189 static void ieee_data_new(struct ieeeSection
*);
190 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection
*,
191 int, uint64_t, int32_t);
192 static void ieee_install_fixup(struct ieeeSection
*, struct ieeeFixupp
*);
193 static int32_t ieee_segment(char *, int, int *);
194 static void ieee_write_file(int debuginfo
);
195 static void ieee_write_byte(struct ieeeSection
*, int);
196 static void ieee_write_word(struct ieeeSection
*, int);
197 static void ieee_write_dword(struct ieeeSection
*, int32_t);
198 static void ieee_putascii(char *, ...);
199 static void ieee_putcs(int);
200 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
201 static int32_t ieee_putlr(struct ieeeFixupp
*);
202 static void ieee_unqualified_name(char *, char *);
207 static void ieee_init(void)
211 fpubtail
= &fpubhead
;
217 seghead
= ieee_seg_needs_update
= NULL
;
219 ieee_entry_seg
= NO_SEG
;
220 ieee_uppercase
= false;
224 static int ieee_set_info(enum geninfo type
, char **val
)
235 static void ieee_cleanup(int debuginfo
)
237 ieee_write_file(debuginfo
);
238 of_ieee
.current_dfmt
->cleanup();
240 struct ieeeSection
*segtmp
= seghead
;
241 seghead
= seghead
->next
;
242 while (segtmp
->pubhead
) {
243 struct ieeePublic
*pubtmp
= segtmp
->pubhead
;
244 segtmp
->pubhead
= pubtmp
->next
;
247 while (segtmp
->fptr
) {
248 struct ieeeFixupp
*fixtmp
= segtmp
->fptr
;
249 segtmp
->fptr
= fixtmp
->next
;
252 while (segtmp
->data
) {
253 struct ieeeObjData
*dattmp
= segtmp
->data
;
254 segtmp
->data
= dattmp
->next
;
260 struct ieeePublic
*pubtmp
= fpubhead
;
261 fpubhead
= fpubhead
->next
;
265 struct ieeeExternal
*exttmp
= exthead
;
266 exthead
= exthead
->next
;
270 struct ExtBack
*ebtmp
= ebhead
;
271 ebhead
= ebhead
->next
;
277 * callback for labels
279 static void ieee_deflabel(char *name
, int32_t segment
,
280 int64_t offset
, int is_global
, char *special
)
283 * We have three cases:
285 * (i) `segment' is a segment-base. If so, set the name field
286 * for the segment structure it refers to, and then
289 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
290 * Save the label position for later output of a PUBDEF record.
293 * (iii) `segment' is not one of our segments. Save the label
294 * position for later output of an EXTDEF.
296 struct ieeeExternal
*ext
;
298 struct ieeeSection
*seg
;
302 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%s'", special
);
305 * First check for the double-period, signifying something
308 if (name
[0] == '.' && name
[1] == '.') {
309 if (!strcmp(name
, "..start")) {
310 ieee_entry_seg
= segment
;
311 ieee_entry_ofs
= offset
;
319 if (ieee_seg_needs_update
) {
320 ieee_seg_needs_update
->name
= name
;
323 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
329 if (segment
>= SEG_ABS
) {
331 * SEG_ABS subcase of (ii).
334 struct ieeePublic
*pub
;
336 pub
= *fpubtail
= nasm_malloc(sizeof(*pub
));
337 fpubtail
= &pub
->next
;
340 pub
->offset
= offset
;
341 pub
->segment
= segment
& ~SEG_ABS
;
346 for (seg
= seghead
; seg
&& is_global
; seg
= seg
->next
)
347 if (seg
->index
== segment
) {
348 struct ieeePublic
*pub
;
350 last_defined
= pub
= *seg
->pubtail
= nasm_malloc(sizeof(*pub
));
351 seg
->pubtail
= &pub
->next
;
354 pub
->offset
= offset
;
355 pub
->index
= seg
->ieee_index
;
364 ext
= *exttail
= nasm_malloc(sizeof(*ext
));
366 exttail
= &ext
->next
;
369 ext
->commonsize
= offset
;
375 eb
= *ebtail
= nasm_malloc(sizeof(*eb
));
379 while (i
> EXT_BLKSIZ
) {
383 eb
= *ebtail
= nasm_malloc(sizeof(*eb
));
389 eb
->index
[i
] = externals
++;
397 static void ieee_out(int32_t segto
, const void *data
,
398 enum out_type type
, uint64_t size
,
399 int32_t segment
, int32_t wrt
)
401 const uint8_t *ucdata
;
403 struct ieeeSection
*seg
;
406 * handle absolute-assembly (structure definitions)
408 if (segto
== NO_SEG
) {
409 if (type
!= OUT_RESERVE
)
410 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
416 * If `any_segs' is still false, we must define a default
420 int tempint
; /* ignored */
421 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
422 nasm_error(ERR_PANIC
, "strange segment conditions in IEEE driver");
426 * Find the segment we are targetting.
428 for (seg
= seghead
; seg
; seg
= seg
->next
)
429 if (seg
->index
== segto
)
432 nasm_error(ERR_PANIC
, "code directed to nonexistent segment?");
434 if (type
== OUT_RAWDATA
) {
437 ieee_write_byte(seg
, *ucdata
++);
438 } else if (type
== OUT_ADDRESS
|| type
== OUT_REL2ADR
||
439 type
== OUT_REL4ADR
) {
440 if (segment
== NO_SEG
&& type
!= OUT_ADDRESS
)
441 nasm_error(ERR_NONFATAL
, "relative call to absolute address not"
442 " supported by IEEE format");
443 ldata
= *(int64_t *)data
;
444 if (type
== OUT_REL2ADR
)
446 if (type
== OUT_REL4ADR
)
448 ieee_write_fixup(segment
, wrt
, seg
, size
, type
, ldata
);
450 ieee_write_word(seg
, ldata
);
452 ieee_write_dword(seg
, ldata
);
453 } else if (type
== OUT_RESERVE
) {
455 ieee_write_byte(seg
, 0);
459 static void ieee_data_new(struct ieeeSection
*segto
)
463 segto
->data
= segto
->datacurr
=
464 nasm_malloc(sizeof(*(segto
->datacurr
)));
466 segto
->datacurr
= segto
->datacurr
->next
=
467 nasm_malloc(sizeof(*(segto
->datacurr
)));
468 segto
->datacurr
->next
= NULL
;
472 * this routine is unalduterated bloatware. I usually don't do this
473 * but I might as well see what it is like on a harmless program.
474 * If anyone wants to optimize this is a good canditate!
476 static void ieee_write_fixup(int32_t segment
, int32_t wrt
,
477 struct ieeeSection
*segto
, int size
,
478 uint64_t realtype
, int32_t offset
)
480 struct ieeeSection
*target
;
483 /* Don't put a fixup for things NASM can calculate */
484 if (wrt
== NO_SEG
&& segment
== NO_SEG
)
488 /* if it is a WRT offset */
493 s
.id1
= -(wrt
- SEG_ABS
);
495 if (wrt
% 2 && realtype
!= OUT_REL2ADR
496 && realtype
!= OUT_REL4ADR
) {
499 for (target
= seghead
; target
; target
= target
->next
)
500 if (target
->index
== wrt
)
503 s
.id1
= target
->ieee_index
;
504 for (target
= seghead
; target
; target
= target
->next
)
505 if (target
->index
== segment
)
509 s
.id2
= target
->ieee_index
;
512 * Now we assume the segment field is being used
513 * to hold an extern index
515 int32_t i
= segment
/ 2;
516 struct ExtBack
*eb
= ebhead
;
517 while (i
> EXT_BLKSIZ
) {
524 /* if we have an extern decide the type and make a record
529 s
.id2
= eb
->index
[i
];
531 nasm_error(ERR_NONFATAL
,
532 "Source of WRT must be an offset");
536 nasm_error(ERR_PANIC
,
537 "unrecognised WRT value in ieee_write_fixup");
539 nasm_error(ERR_NONFATAL
, "target of WRT must be a section ");
542 ieee_install_fixup(segto
, &s
);
545 /* Pure segment fixup ? */
546 if (segment
!= NO_SEG
) {
549 if (segment
>= SEG_ABS
) {
550 /* absolute far segment fixup */
551 s
.id1
= -(segment
- ~SEG_ABS
);
552 } else if (segment
% 2) {
553 /* fixup to named segment */
555 for (target
= seghead
; target
; target
= target
->next
)
556 if (target
->index
== segment
- 1)
559 s
.id1
= target
->ieee_index
;
562 * Now we assume the segment field is being used
563 * to hold an extern index
565 int32_t i
= segment
/ 2;
566 struct ExtBack
*eb
= ebhead
;
567 while (i
> EXT_BLKSIZ
) {
574 /* if we have an extern decide the type and make a record
577 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
578 nasm_error(ERR_PANIC
,
579 "Segment of a rel not supported in ieee_write_fixup");
581 /* If we want the segment */
584 s
.id1
= eb
->index
[i
];
588 /* If we get here the seg value doesn't make sense */
589 nasm_error(ERR_PANIC
,
590 "unrecognised segment value in ieee_write_fixup");
594 /* Assume we are offsetting directly from a section
595 * So look up the target segment
597 for (target
= seghead
; target
; target
= target
->next
)
598 if (target
->index
== segment
)
601 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
602 /* PC rel to a known offset */
603 s
.id1
= target
->ieee_index
;
608 /* We were offsetting from a seg */
609 s
.id1
= target
->ieee_index
;
616 * Now we assume the segment field is being used
617 * to hold an extern index
619 int32_t i
= segment
/ 2;
620 struct ExtBack
*eb
= ebhead
;
621 while (i
> EXT_BLKSIZ
) {
628 /* if we have an extern decide the type and make a record
631 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
634 s
.id1
= eb
->index
[i
];
636 /* else we want the external offset */
639 s
.id1
= eb
->index
[i
];
643 /* If we get here the seg value doesn't make sense */
644 nasm_error(ERR_PANIC
,
645 "unrecognised segment value in ieee_write_fixup");
648 if (size
!= 2 && s
.ftype
== FT_SEG
)
649 nasm_error(ERR_NONFATAL
, "IEEE format can only handle 2-byte"
650 " segment base references");
652 ieee_install_fixup(segto
, &s
);
655 /* should never get here */
657 static void ieee_install_fixup(struct ieeeSection
*seg
,
658 struct ieeeFixupp
*fix
)
660 struct ieeeFixupp
*f
;
661 f
= nasm_malloc(sizeof(struct ieeeFixupp
));
662 memcpy(f
, fix
, sizeof(struct ieeeFixupp
));
663 f
->offset
= seg
->currentpos
;
664 seg
->currentpos
+= fix
->size
;
667 seg
->flptr
= seg
->flptr
->next
= f
;
669 seg
->fptr
= seg
->flptr
= f
;
676 static int32_t ieee_segment(char *name
, int pass
, int *bits
)
679 * We call the label manager here to define a name for the new
680 * segment, and when our _own_ label-definition stub gets
681 * called in return, it should register the new segment name
682 * using the pointer it gets passed. That way we save memory,
683 * by sponging off the label manager.
689 return seghead
->index
;
691 struct ieeeSection
*seg
;
697 * Look for segment attributes.
701 name
++; /* hack, but a documented one */
703 while (*p
&& !nasm_isspace(*p
))
707 while (*p
&& nasm_isspace(*p
))
711 while (*p
&& !nasm_isspace(*p
))
715 while (*p
&& nasm_isspace(*p
))
723 for (seg
= seghead
; seg
; seg
= seg
->next
) {
725 if (!strcmp(seg
->name
, name
)) {
726 if (attrs
> 0 && pass
== 1)
727 nasm_error(ERR_WARNING
, "segment attributes specified on"
728 " redeclaration of segment: ignoring");
737 *segtail
= seg
= nasm_malloc(sizeof(*seg
));
739 segtail
= &seg
->next
;
740 seg
->index
= seg_alloc();
741 seg
->ieee_index
= ieee_idx
;
745 seg
->align
= 1; /* default */
746 seg
->use32
= *bits
== 32; /* default to user spec */
747 seg
->combine
= CMB_PUBLIC
; /* default */
749 seg
->pubtail
= &seg
->pubhead
;
753 seg
->loctail
= &seg
->lochead
;
756 * Process the segment attributes.
765 * `p' contains a segment attribute.
767 if (!nasm_stricmp(p
, "private"))
768 seg
->combine
= CMB_PRIVATE
;
769 else if (!nasm_stricmp(p
, "public"))
770 seg
->combine
= CMB_PUBLIC
;
771 else if (!nasm_stricmp(p
, "common"))
772 seg
->combine
= CMB_COMMON
;
773 else if (!nasm_stricmp(p
, "use16"))
775 else if (!nasm_stricmp(p
, "use32"))
777 else if (!nasm_strnicmp(p
, "align=", 6)) {
778 seg
->align
= readnum(p
+ 6, &rn_error
);
783 nasm_error(ERR_NONFATAL
, "segment alignment should be"
786 switch ((int)seg
->align
) {
798 nasm_error(ERR_NONFATAL
, "invalid alignment value %d",
803 } else if (!nasm_strnicmp(p
, "absolute=", 9)) {
804 seg
->align
= SEG_ABS
+ readnum(p
+ 9, &rn_error
);
806 nasm_error(ERR_NONFATAL
, "argument to `absolute' segment"
807 " attribute should be numeric");
811 ieee_seg_needs_update
= seg
;
812 if (seg
->align
>= SEG_ABS
)
813 define_label(name
, NO_SEG
, seg
->align
- SEG_ABS
,
816 define_label(name
, seg
->index
+ 1, 0L, NULL
, false, false);
817 ieee_seg_needs_update
= NULL
;
828 * directives supported
830 static int ieee_directive(enum directives directive
, char *value
, int pass
)
838 ieee_uppercase
= true;
847 * Return segment data
849 static int32_t ieee_segbase(int32_t segment
)
851 struct ieeeSection
*seg
;
854 * Find the segment in our list.
856 for (seg
= seghead
; seg
; seg
= seg
->next
)
857 if (seg
->index
== segment
- 1)
861 return segment
; /* not one of ours - leave it alone */
863 if (seg
->align
>= SEG_ABS
)
864 return seg
->align
; /* absolute segment */
866 return segment
; /* no special treatment */
872 static void ieee_filename(char *inname
, char *outname
)
874 strcpy(ieee_infile
, inname
);
875 standard_extension(inname
, outname
, ".o");
878 static void ieee_write_file(int debuginfo
)
883 struct ieeeSection
*seg
;
884 struct ieeePublic
*pub
, *loc
;
885 struct ieeeExternal
*ext
;
886 struct ieeeObjData
*data
;
887 struct ieeeFixupp
*fix
;
892 * Write the module header
894 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile
), ieee_infile
);
897 * Write the NASM boast comment.
899 ieee_putascii("CO0,%02X%s.\n", strlen(nasm_comment
), nasm_comment
);
902 * write processor-specific information
904 ieee_putascii("AD8,4,L.\n");
910 thetime
= localtime(&reltime
);
911 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
912 1900 + thetime
->tm_year
, thetime
->tm_mon
+ 1,
913 thetime
->tm_mday
, thetime
->tm_hour
, thetime
->tm_min
,
916 * if debugging, dump file names
918 for (fn
= fnhead
; fn
&& debuginfo
; fn
= fn
->next
) {
919 ieee_putascii("C0105,%02X%s.\n", strlen(fn
->name
), fn
->name
);
922 ieee_putascii("CO101,07ENDHEAD.\n");
924 * the standard doesn't specify when to put checksums,
925 * we'll just do it periodically.
930 * Write the section headers
933 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
938 switch (seg
->combine
) {
950 ieee_unqualified_name(buf
, seg
->name
);
951 if (seg
->align
>= SEG_ABS
) {
952 ieee_putascii("ST%X,A,%02X%s.\n", seg
->ieee_index
,
954 ieee_putascii("ASL%X,%lX.\n", seg
->ieee_index
,
955 (seg
->align
- SEG_ABS
) * 16);
957 ieee_putascii("ST%X,%c,%02X%s.\n", seg
->ieee_index
, attrib
,
959 ieee_putascii("SA%X,%lX.\n", seg
->ieee_index
, seg
->align
);
960 ieee_putascii("ASS%X,%X.\n", seg
->ieee_index
,
966 * write the start address if there is one
968 if (ieee_entry_seg
) {
969 for (seg
= seghead
; seg
; seg
= seg
->next
)
970 if (seg
->index
== ieee_entry_seg
)
973 nasm_error(ERR_PANIC
, "Start address records are incorrect");
975 ieee_putascii("ASG,R%X,%lX,+.\n", seg
->ieee_index
,
984 for (seg
= seghead
; seg
; seg
= seg
->next
) {
985 for (pub
= seg
->pubhead
; pub
; pub
= pub
->next
) {
987 ieee_unqualified_name(buf
, pub
->name
);
988 ieee_putascii("NI%X,%02X%s.\n", i
, strlen(buf
), buf
);
989 if (pub
->segment
== -1)
990 ieee_putascii("ASI%X,R%X,%lX,+.\n", i
, pub
->index
,
993 ieee_putascii("ASI%X,%lX,%lX,+.\n", i
, pub
->segment
* 16,
996 if (pub
->type
>= 0x100)
997 ieee_putascii("ATI%X,T%X.\n", i
, pub
->type
- 0x100);
999 ieee_putascii("ATI%X,%X.\n", i
, pub
->type
);
1008 ieee_unqualified_name(buf
, pub
->name
);
1009 ieee_putascii("NI%X,%02X%s.\n", i
, strlen(buf
), buf
);
1010 if (pub
->segment
== -1)
1011 ieee_putascii("ASI%X,R%X,%lX,+.\n", i
, pub
->index
,
1014 ieee_putascii("ASI%X,%lX,%lX,+.\n", i
, pub
->segment
* 16,
1017 if (pub
->type
>= 0x100)
1018 ieee_putascii("ATI%X,T%X.\n", i
, pub
->type
- 0x100);
1020 ieee_putascii("ATI%X,%X.\n", i
, pub
->type
);
1026 * Write the externals
1032 ieee_unqualified_name(buf
, ext
->name
);
1033 ieee_putascii("NX%X,%02X%s.\n", i
++, strlen(buf
), buf
);
1039 * IEEE doesn't have a standard pass break record
1040 * so use the ladsoft variant
1042 ieee_putascii("CO100,06ENDSYM.\n");
1048 for (arr
= arrhead
; arr
&& debuginfo
; arr
= arr
->next
) {
1049 ieee_putascii("TY%X,20,%X,%lX.\n", i
++, arr
->basetype
,
1056 for (seg
= seghead
; seg
&& debuginfo
; seg
= seg
->next
) {
1057 for (loc
= seg
->lochead
; loc
; loc
= loc
->next
) {
1059 ieee_unqualified_name(buf
, loc
->name
);
1060 ieee_putascii("NN%X,%02X%s.\n", i
, strlen(buf
), buf
);
1061 if (loc
->segment
== -1)
1062 ieee_putascii("ASN%X,R%X,%lX,+.\n", i
, loc
->index
,
1065 ieee_putascii("ASN%X,%lX,%lX,+.\n", i
, loc
->segment
* 16,
1068 if (loc
->type
>= 0x100)
1069 ieee_putascii("ATN%X,T%X.\n", i
, loc
->type
- 0x100);
1071 ieee_putascii("ATN%X,%X.\n", i
, loc
->type
);
1078 * put out section data;
1081 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
1084 if (seg
->currentpos
) {
1085 int32_t size
, org
= 0;
1087 ieee_putascii("SB%X.\n", seg
->ieee_index
);
1090 size
= HUNKSIZE
- (org
% HUNKSIZE
);
1093 seg
->currentpos
? seg
->currentpos
- org
: size
;
1094 size
= fix
->offset
- org
> size
? size
: fix
->offset
- org
;
1095 org
= ieee_putld(org
, org
+ size
, data
->data
);
1096 if (org
% HUNKSIZE
== 0)
1098 if (org
== fix
->offset
) {
1099 org
+= ieee_putlr(fix
);
1103 while (org
< seg
->currentpos
&& data
) {
1105 seg
->currentpos
- org
>
1106 HUNKSIZE
? HUNKSIZE
: seg
->currentpos
- org
;
1107 org
= ieee_putld(org
, org
+ size
, data
->data
);
1118 ieee_putascii("ME.\n");
1121 static void ieee_write_byte(struct ieeeSection
*seg
, int data
)
1124 if (!(temp
= seg
->currentpos
++ % HUNKSIZE
))
1126 seg
->datacurr
->data
[temp
] = data
;
1129 static void ieee_write_word(struct ieeeSection
*seg
, int data
)
1131 ieee_write_byte(seg
, data
& 0xFF);
1132 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1135 static void ieee_write_dword(struct ieeeSection
*seg
, int32_t data
)
1137 ieee_write_byte(seg
, data
& 0xFF);
1138 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1139 ieee_write_byte(seg
, (data
>> 16) & 0xFF);
1140 ieee_write_byte(seg
, (data
>> 24) & 0xFF);
1142 static void ieee_putascii(char *format
, ...)
1148 va_start(ap
, format
);
1149 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1151 for (i
= 0; i
< l
; i
++)
1152 if ((uint8_t)buffer
[i
] > 31)
1153 checksum
+= buffer
[i
];
1155 fprintf(ofile
, buffer
);
1159 * put out a checksum record */
1160 static void ieee_putcs(int toclear
)
1163 ieee_putascii("CS.\n");
1167 ieee_putascii("CS%02X.\n", checksum
& 127);
1172 static int32_t ieee_putld(int32_t start
, int32_t end
, uint8_t *buf
)
1177 val
= start
% HUNKSIZE
;
1178 /* fill up multiple lines */
1179 while (end
- start
>= LDPERLINE
) {
1181 ieee_putascii("LD");
1182 for (i
= 0; i
< LDPERLINE
; i
++) {
1183 ieee_putascii("%02X", buf
[val
++]);
1186 ieee_putascii(".\n");
1188 /* if no partial lines */
1191 /* make a partial line */
1192 ieee_putascii("LD");
1193 while (start
< end
) {
1194 ieee_putascii("%02X", buf
[val
++]);
1197 ieee_putascii(".\n");
1200 static int32_t ieee_putlr(struct ieeeFixupp
*p
)
1203 * To deal with the vagaries of segmentation the LADsoft linker
1204 * defines two types of segments: absolute and virtual. Note that
1205 * 'absolute' in this context is a different thing from the IEEE
1206 * definition of an absolute segment type, which is also supported. If a
1207 * sement is linked in virtual mode the low limit (L-var) is
1208 * subtracted from each R,X, and P variable which appears in an
1209 * expression, so that we can have relative offsets. Meanwhile
1210 * in the ABSOLUTE mode this subtraction is not done and
1211 * so we can use absolute offsets from 0. In the LADsoft linker
1212 * this configuration is not done in the assemblker source but in
1213 * a source the linker reads. Generally this type of thing only
1214 * becomes an issue if real mode code is used. A pure 32-bit linker could
1215 * get away without defining the virtual mode...
1218 int32_t size
= p
->size
;
1222 sprintf(buf
, "%"PRIX32
"", -p
->id1
);
1224 sprintf(buf
, "L%"PRIX32
",10,/", p
->id1
);
1227 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+", p
->id1
, p
->addend
);
1230 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,P,-,%X,-", p
->id1
, p
->addend
, p
->size
);
1235 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->addend
,
1236 p
->id2
, -p
->id1
* 16);
1238 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->addend
,
1242 sprintf(buf
, "X%"PRIX32
"", p
->id1
);
1245 sprintf(buf
, "X%"PRIX32
",P,-,%"PRIX32
",-", p
->id1
, size
);
1248 /* We needed a non-ieee hack here.
1249 * We introduce the Y variable, which is the low
1250 * limit of the native segment the extern resides in
1252 sprintf(buf
, "Y%"PRIX32
",10,/", p
->id1
);
1256 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->id2
,
1259 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->id2
, p
->id1
);
1262 ieee_putascii("LR(%s,%"PRIX32
").\n", buf
, size
);
1267 /* Dump all segment data (text and fixups )*/
1269 static void ieee_unqualified_name(char *dest
, char *source
)
1271 if (ieee_uppercase
) {
1273 *dest
++ = toupper(*source
++);
1276 strcpy(dest
, source
);
1278 void dbgls_init(void)
1284 arrindex
= ARRAY_BOT
;
1287 ieee_segment("??LINE", 2, &tempint
);
1290 static void dbgls_cleanup(void)
1292 struct ieeeSection
*segtmp
;
1294 struct FileName
*fntemp
= fnhead
;
1295 fnhead
= fnhead
->next
;
1296 nasm_free(fntemp
->name
);
1299 for (segtmp
= seghead
; segtmp
; segtmp
= segtmp
->next
) {
1300 while (segtmp
->lochead
) {
1301 struct ieeePublic
*loctmp
= segtmp
->lochead
;
1302 segtmp
->lochead
= loctmp
->next
;
1303 nasm_free(loctmp
->name
);
1308 struct Array
*arrtmp
= arrhead
;
1309 arrhead
= arrhead
->next
;
1315 * because this routine is not bracketed in
1316 * the main program, this routine will be called even if there
1317 * is no request for debug info
1318 * so, we have to make sure the ??LINE segment is avaialbe
1319 * as the first segment when this debug format is selected
1321 static void dbgls_linnum(const char *lnfname
, int32_t lineno
, int32_t segto
)
1323 struct FileName
*fn
;
1324 struct ieeeSection
*seg
;
1326 if (segto
== NO_SEG
)
1330 * If `any_segs' is still false, we must define a default
1334 int tempint
; /* ignored */
1335 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
1336 nasm_error(ERR_PANIC
, "strange segment conditions in OBJ driver");
1340 * Find the segment we are targetting.
1342 for (seg
= seghead
; seg
; seg
= seg
->next
)
1343 if (seg
->index
== segto
)
1346 nasm_error(ERR_PANIC
, "lineno directed to nonexistent segment?");
1348 for (fn
= fnhead
; fn
; fn
= fn
->next
) {
1349 if (!nasm_stricmp(lnfname
, fn
->name
))
1354 fn
= nasm_malloc(sizeof(*fn
));
1355 fn
->name
= nasm_malloc(strlen(lnfname
) + 1);
1357 strcpy(fn
->name
, lnfname
);
1362 ieee_write_byte(seghead
, fn
->index
);
1363 ieee_write_word(seghead
, lineno
);
1364 ieee_write_fixup(segto
, NO_SEG
, seghead
, 4, OUT_ADDRESS
,
1368 static void dbgls_deflabel(char *name
, int32_t segment
,
1369 int64_t offset
, int is_global
, char *special
)
1371 struct ieeeSection
*seg
;
1373 /* Keep compiler from warning about special */
1377 * If it's a special-retry from pass two, discard it.
1383 * First check for the double-period, signifying something
1386 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
1393 if (ieee_seg_needs_update
)
1395 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
1398 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
1403 * If `any_segs' is still false, we might need to define a
1404 * default segment, if they're trying to declare a label in
1405 * `first_seg'. But the label should exist due to a prior
1406 * call to ieee_deflabel so we can skip that.
1409 for (seg
= seghead
; seg
; seg
= seg
->next
)
1410 if (seg
->index
== segment
) {
1411 struct ieeePublic
*loc
;
1413 * Case (ii). Maybe MODPUB someday?
1416 last_defined
= loc
= nasm_malloc(sizeof(*loc
));
1417 *seg
->loctail
= loc
;
1418 seg
->loctail
= &loc
->next
;
1420 loc
->name
= nasm_strdup(name
);
1421 loc
->offset
= offset
;
1423 loc
->index
= seg
->ieee_index
;
1427 static void dbgls_typevalue(int32_t type
)
1429 int elem
= TYM_ELEMENTS(type
);
1430 type
= TYM_TYPE(type
);
1437 last_defined
->type
= 1; /* uint8_t */
1440 last_defined
->type
= 3; /* unsigned word */
1443 last_defined
->type
= 5; /* unsigned dword */
1446 last_defined
->type
= 9; /* float */
1449 last_defined
->type
= 10; /* qword */
1452 last_defined
->type
= 11; /* TBYTE */
1455 last_defined
->type
= 0x10; /* near label */
1460 struct Array
*arrtmp
= nasm_malloc(sizeof(*arrtmp
));
1461 int vtype
= last_defined
->type
;
1462 arrtmp
->size
= elem
;
1463 arrtmp
->basetype
= vtype
;
1464 arrtmp
->next
= NULL
;
1465 last_defined
->type
= arrindex
++ + 0x100;
1467 arrtail
= &(arrtmp
->next
);
1469 last_defined
= NULL
;
1471 static void dbgls_output(int output_type
, void *param
)
1476 static struct dfmt ladsoft_debug_form
= {
1477 "LADsoft Debug Records",
1482 null_debug_directive
,
1487 static struct dfmt
*ladsoft_debug_arr
[3] = {
1488 &ladsoft_debug_form
,
1492 struct ofmt of_ieee
= {
1493 "IEEE-695 (LADsoft variant) object file format",
1497 &ladsoft_debug_form
,
1510 #endif /* OF_IEEE */