1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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 */
89 static char ieee_infile
[FILENAME_MAX
];
90 static int ieee_uppercase
;
95 #define HUNKSIZE 1024 /* Size of the data hunk */
96 #define EXT_BLKSIZ 512
97 #define LDPERLINE 32 /* bytes per line in output */
102 struct LineNumber
*next
;
103 struct ieeeSection
*segment
;
108 static struct FileName
{
109 struct FileName
*next
;
114 static struct Array
{
118 } *arrhead
, **arrtail
;
120 static struct ieeePublic
{
121 struct ieeePublic
*next
;
124 int32_t segment
; /* only if it's far-absolute */
126 int type
; /* for debug purposes */
127 } *fpubhead
, **fpubtail
, *last_defined
;
129 static struct ieeeExternal
{
130 struct ieeeExternal
*next
;
133 } *exthead
, **exttail
;
135 static int externals
;
137 static struct ExtBack
{
138 struct ExtBack
*next
;
139 int index
[EXT_BLKSIZ
];
142 /* NOTE: the first segment MUST be the lineno segment */
143 static struct ieeeSection
{
144 struct ieeeSection
*next
;
146 struct ieeeObjData
*data
, *datacurr
;
147 struct ieeeFixupp
*fptr
, *flptr
;
148 int32_t index
; /* the NASM segment id */
149 int32_t ieee_index
; /* the OBJ-file segment index */
151 int32_t align
; /* can be SEG_ABS + absolute addr */
153 int32_t use32
; /* is this segment 32-bit? */
154 struct ieeePublic
*pubhead
, **pubtail
, *lochead
, **loctail
;
160 } *seghead
, **segtail
, *ieee_seg_needs_update
;
163 struct ieeeObjData
*next
;
164 uint8_t data
[HUNKSIZE
];
168 struct ieeeFixupp
*next
;
186 static int32_t ieee_entry_seg
, ieee_entry_ofs
;
189 extern const struct ofmt of_ieee
;
190 static const struct dfmt ladsoft_debug_form
;
192 static void ieee_data_new(struct ieeeSection
*);
193 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection
*,
194 int, uint64_t, int32_t);
195 static void ieee_install_fixup(struct ieeeSection
*, struct ieeeFixupp
*);
196 static int32_t ieee_segment(char *, int, int *);
197 static void ieee_write_file(void);
198 static void ieee_write_byte(struct ieeeSection
*, int);
199 static void ieee_write_word(struct ieeeSection
*, int);
200 static void ieee_write_dword(struct ieeeSection
*, int32_t);
201 static void ieee_putascii(char *, ...);
202 static void ieee_putcs(int);
203 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
204 static int32_t ieee_putlr(struct ieeeFixupp
*);
205 static void ieee_unqualified_name(char *, char *);
210 static void ieee_init(void)
214 fpubtail
= &fpubhead
;
220 seghead
= ieee_seg_needs_update
= NULL
;
222 ieee_entry_seg
= NO_SEG
;
223 ieee_uppercase
= false;
230 static void ieee_cleanup(void)
235 struct ieeeSection
*segtmp
= seghead
;
236 seghead
= seghead
->next
;
237 while (segtmp
->pubhead
) {
238 struct ieeePublic
*pubtmp
= segtmp
->pubhead
;
239 segtmp
->pubhead
= pubtmp
->next
;
242 while (segtmp
->fptr
) {
243 struct ieeeFixupp
*fixtmp
= segtmp
->fptr
;
244 segtmp
->fptr
= fixtmp
->next
;
247 while (segtmp
->data
) {
248 struct ieeeObjData
*dattmp
= segtmp
->data
;
249 segtmp
->data
= dattmp
->next
;
255 struct ieeePublic
*pubtmp
= fpubhead
;
256 fpubhead
= fpubhead
->next
;
260 struct ieeeExternal
*exttmp
= exthead
;
261 exthead
= exthead
->next
;
265 struct ExtBack
*ebtmp
= ebhead
;
266 ebhead
= ebhead
->next
;
272 * callback for labels
274 static void ieee_deflabel(char *name
, int32_t segment
,
275 int64_t offset
, int is_global
, char *special
)
278 * We have three cases:
280 * (i) `segment' is a segment-base. If so, set the name field
281 * for the segment structure it refers to, and then
284 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
285 * Save the label position for later output of a PUBDEF record.
288 * (iii) `segment' is not one of our segments. Save the label
289 * position for later output of an EXTDEF.
291 struct ieeeExternal
*ext
;
293 struct ieeeSection
*seg
;
297 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%s'", special
);
300 * First check for the double-period, signifying something
303 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
304 if (!strcmp(name
, "..start")) {
305 ieee_entry_seg
= segment
;
306 ieee_entry_ofs
= offset
;
314 if (ieee_seg_needs_update
) {
315 ieee_seg_needs_update
->name
= name
;
318 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
324 if (segment
>= SEG_ABS
) {
326 * SEG_ABS subcase of (ii).
329 struct ieeePublic
*pub
;
331 pub
= *fpubtail
= nasm_malloc(sizeof(*pub
));
332 fpubtail
= &pub
->next
;
335 pub
->offset
= offset
;
336 pub
->segment
= segment
& ~SEG_ABS
;
341 for (seg
= seghead
; seg
&& is_global
; seg
= seg
->next
)
342 if (seg
->index
== segment
) {
343 struct ieeePublic
*pub
;
345 last_defined
= pub
= *seg
->pubtail
= nasm_malloc(sizeof(*pub
));
346 seg
->pubtail
= &pub
->next
;
349 pub
->offset
= offset
;
350 pub
->index
= seg
->ieee_index
;
359 ext
= *exttail
= nasm_malloc(sizeof(*ext
));
361 exttail
= &ext
->next
;
364 ext
->commonsize
= offset
;
370 eb
= *ebtail
= nasm_zalloc(sizeof(*eb
));
374 while (i
> EXT_BLKSIZ
) {
378 eb
= *ebtail
= nasm_zalloc(sizeof(*eb
));
384 eb
->index
[i
] = externals
++;
392 static void ieee_out(int32_t segto
, const void *data
,
393 enum out_type type
, uint64_t size
,
394 int32_t segment
, int32_t wrt
)
396 const uint8_t *ucdata
;
398 struct ieeeSection
*seg
;
401 * handle absolute-assembly (structure definitions)
403 if (segto
== NO_SEG
) {
404 if (type
!= OUT_RESERVE
)
405 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
411 * If `any_segs' is still false, we must define a default
415 int tempint
; /* ignored */
416 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
417 nasm_panic(0, "strange segment conditions in IEEE driver");
421 * Find the segment we are targetting.
423 for (seg
= seghead
; seg
; seg
= seg
->next
)
424 if (seg
->index
== segto
)
427 nasm_panic(0, "code directed to nonexistent segment?");
429 if (type
== OUT_RAWDATA
) {
432 ieee_write_byte(seg
, *ucdata
++);
433 } else if (type
== OUT_ADDRESS
|| type
== OUT_REL2ADR
||
434 type
== OUT_REL4ADR
) {
435 if (type
== OUT_ADDRESS
)
436 size
= abs((int)size
);
437 else if (segment
== NO_SEG
)
438 nasm_error(ERR_NONFATAL
, "relative call to absolute address not"
439 " supported by IEEE format");
440 ldata
= *(int64_t *)data
;
441 if (type
== OUT_REL2ADR
)
443 if (type
== OUT_REL4ADR
)
445 ieee_write_fixup(segment
, wrt
, seg
, size
, type
, ldata
);
447 ieee_write_word(seg
, ldata
);
449 ieee_write_dword(seg
, ldata
);
450 } else if (type
== OUT_RESERVE
) {
452 ieee_write_byte(seg
, 0);
456 static void ieee_data_new(struct ieeeSection
*segto
)
460 segto
->data
= segto
->datacurr
=
461 nasm_malloc(sizeof(*(segto
->datacurr
)));
463 segto
->datacurr
= segto
->datacurr
->next
=
464 nasm_malloc(sizeof(*(segto
->datacurr
)));
465 segto
->datacurr
->next
= NULL
;
469 * this routine is unalduterated bloatware. I usually don't do this
470 * but I might as well see what it is like on a harmless program.
471 * If anyone wants to optimize this is a good canditate!
473 static void ieee_write_fixup(int32_t segment
, int32_t wrt
,
474 struct ieeeSection
*segto
, int size
,
475 uint64_t realtype
, int32_t offset
)
477 struct ieeeSection
*target
;
480 /* Don't put a fixup for things NASM can calculate */
481 if (wrt
== NO_SEG
&& segment
== NO_SEG
)
485 /* if it is a WRT offset */
490 s
.id1
= -(wrt
- SEG_ABS
);
492 if (wrt
% 2 && realtype
!= OUT_REL2ADR
493 && realtype
!= OUT_REL4ADR
) {
496 for (target
= seghead
; target
; target
= target
->next
)
497 if (target
->index
== wrt
)
500 s
.id1
= target
->ieee_index
;
501 for (target
= seghead
; target
; target
= target
->next
)
502 if (target
->index
== segment
)
506 s
.id2
= target
->ieee_index
;
509 * Now we assume the segment field is being used
510 * to hold an extern index
512 int32_t i
= segment
/ 2;
513 struct ExtBack
*eb
= ebhead
;
514 while (i
> EXT_BLKSIZ
) {
521 /* if we have an extern decide the type and make a record
526 s
.id2
= eb
->index
[i
];
528 nasm_error(ERR_NONFATAL
,
529 "Source of WRT must be an offset");
534 "unrecognised WRT value in ieee_write_fixup");
536 nasm_error(ERR_NONFATAL
, "target of WRT must be a section ");
539 ieee_install_fixup(segto
, &s
);
542 /* Pure segment fixup ? */
543 if (segment
!= NO_SEG
) {
546 if (segment
>= SEG_ABS
) {
547 /* absolute far segment fixup */
548 s
.id1
= -(segment
- ~SEG_ABS
);
549 } else if (segment
% 2) {
550 /* fixup to named segment */
552 for (target
= seghead
; target
; target
= target
->next
)
553 if (target
->index
== segment
- 1)
556 s
.id1
= target
->ieee_index
;
559 * Now we assume the segment field is being used
560 * to hold an extern index
562 int32_t i
= segment
/ 2;
563 struct ExtBack
*eb
= ebhead
;
564 while (i
> EXT_BLKSIZ
) {
571 /* if we have an extern decide the type and make a record
574 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
576 "Segment of a rel not supported in ieee_write_fixup");
578 /* If we want the segment */
581 s
.id1
= eb
->index
[i
];
585 /* If we get here the seg value doesn't make sense */
587 "unrecognised segment value in ieee_write_fixup");
591 /* Assume we are offsetting directly from a section
592 * So look up the target segment
594 for (target
= seghead
; target
; target
= target
->next
)
595 if (target
->index
== segment
)
598 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
599 /* PC rel to a known offset */
600 s
.id1
= target
->ieee_index
;
605 /* We were offsetting from a seg */
606 s
.id1
= target
->ieee_index
;
613 * Now we assume the segment field is being used
614 * to hold an extern index
616 int32_t i
= segment
/ 2;
617 struct ExtBack
*eb
= ebhead
;
618 while (i
> EXT_BLKSIZ
) {
625 /* if we have an extern decide the type and make a record
628 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
631 s
.id1
= eb
->index
[i
];
633 /* else we want the external offset */
636 s
.id1
= eb
->index
[i
];
640 /* If we get here the seg value doesn't make sense */
642 "unrecognised segment value in ieee_write_fixup");
645 if (size
!= 2 && s
.ftype
== FT_SEG
)
646 nasm_error(ERR_NONFATAL
, "IEEE format can only handle 2-byte"
647 " segment base references");
649 ieee_install_fixup(segto
, &s
);
652 /* should never get here */
654 static void ieee_install_fixup(struct ieeeSection
*seg
,
655 struct ieeeFixupp
*fix
)
657 struct ieeeFixupp
*f
;
658 f
= nasm_malloc(sizeof(struct ieeeFixupp
));
659 memcpy(f
, fix
, sizeof(struct ieeeFixupp
));
660 f
->offset
= seg
->currentpos
;
661 seg
->currentpos
+= fix
->size
;
664 seg
->flptr
= seg
->flptr
->next
= f
;
666 seg
->fptr
= seg
->flptr
= f
;
673 static int32_t ieee_segment(char *name
, int pass
, int *bits
)
676 * We call the label manager here to define a name for the new
677 * segment, and when our _own_ label-definition stub gets
678 * called in return, it should register the new segment name
679 * using the pointer it gets passed. That way we save memory,
680 * by sponging off the label manager.
686 return seghead
->index
;
688 struct ieeeSection
*seg
;
694 * Look for segment attributes.
698 name
++; /* hack, but a documented one */
700 while (*p
&& !nasm_isspace(*p
))
704 while (*p
&& nasm_isspace(*p
))
708 while (*p
&& !nasm_isspace(*p
))
712 while (*p
&& nasm_isspace(*p
))
720 for (seg
= seghead
; seg
; seg
= seg
->next
) {
722 if (!strcmp(seg
->name
, name
)) {
723 if (attrs
> 0 && pass
== 1)
724 nasm_error(ERR_WARNING
, "segment attributes specified on"
725 " redeclaration of segment: ignoring");
734 *segtail
= seg
= nasm_malloc(sizeof(*seg
));
736 segtail
= &seg
->next
;
737 seg
->index
= seg_alloc();
738 seg
->ieee_index
= ieee_idx
;
742 seg
->align
= 1; /* default */
743 seg
->use32
= *bits
== 32; /* default to user spec */
744 seg
->combine
= CMB_PUBLIC
; /* default */
746 seg
->pubtail
= &seg
->pubhead
;
750 seg
->loctail
= &seg
->lochead
;
753 * Process the segment attributes.
762 * `p' contains a segment attribute.
764 if (!nasm_stricmp(p
, "private"))
765 seg
->combine
= CMB_PRIVATE
;
766 else if (!nasm_stricmp(p
, "public"))
767 seg
->combine
= CMB_PUBLIC
;
768 else if (!nasm_stricmp(p
, "common"))
769 seg
->combine
= CMB_COMMON
;
770 else if (!nasm_stricmp(p
, "use16"))
772 else if (!nasm_stricmp(p
, "use32"))
774 else if (!nasm_strnicmp(p
, "align=", 6)) {
775 seg
->align
= readnum(p
+ 6, &rn_error
);
780 nasm_error(ERR_NONFATAL
, "segment alignment should be"
783 switch (seg
->align
) {
795 nasm_error(ERR_NONFATAL
, "invalid alignment value %d",
800 } else if (!nasm_strnicmp(p
, "absolute=", 9)) {
801 seg
->align
= SEG_ABS
+ readnum(p
+ 9, &rn_error
);
803 nasm_error(ERR_NONFATAL
, "argument to `absolute' segment"
804 " attribute should be numeric");
808 ieee_seg_needs_update
= seg
;
809 if (seg
->align
>= SEG_ABS
)
810 define_label(name
, NO_SEG
, seg
->align
- SEG_ABS
,
813 define_label(name
, seg
->index
+ 1, 0L, NULL
, false, false);
814 ieee_seg_needs_update
= NULL
;
825 * directives supported
827 static enum directive_result
828 ieee_directive(enum directive directive
, char *value
, int pass
)
836 ieee_uppercase
= true;
844 static void ieee_sectalign(int32_t seg
, unsigned int value
)
846 struct ieeeSection
*s
;
848 list_for_each(s
, seghead
) {
854 * 256 is maximum there, note it may happen
855 * that align is issued on "absolute" segment
856 * it's fine since SEG_ABS > 256 and we never
857 * get escape this test
859 if (!s
|| !is_power2(value
) || value
> 256)
862 if ((unsigned int)s
->align
< value
)
867 * Return segment data
869 static int32_t ieee_segbase(int32_t segment
)
871 struct ieeeSection
*seg
;
874 * Find the segment in our list.
876 for (seg
= seghead
; seg
; seg
= seg
->next
)
877 if (seg
->index
== segment
- 1)
881 return segment
; /* not one of ours - leave it alone */
883 if (seg
->align
>= SEG_ABS
)
884 return seg
->align
; /* absolute segment */
886 return segment
; /* no special treatment */
892 static void ieee_filename(char *inname
, char *outname
)
894 strcpy(ieee_infile
, inname
);
895 standard_extension(inname
, outname
, ".o");
898 static void ieee_write_file(void)
900 const struct tm
* const thetime
= &official_compile_time
.local
;
902 struct ieeeSection
*seg
;
903 struct ieeePublic
*pub
, *loc
;
904 struct ieeeExternal
*ext
;
905 struct ieeeObjData
*data
;
906 struct ieeeFixupp
*fix
;
909 const bool debuginfo
= (dfmt
== &ladsoft_debug_form
);
912 * Write the module header
914 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile
), ieee_infile
);
917 * Write the NASM boast comment.
919 ieee_putascii("CO0,%02X%s.\n", strlen(nasm_comment
), nasm_comment
);
922 * write processor-specific information
924 ieee_putascii("AD8,4,L.\n");
929 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
930 1900 + thetime
->tm_year
, thetime
->tm_mon
+ 1,
931 thetime
->tm_mday
, thetime
->tm_hour
, thetime
->tm_min
,
934 * if debugging, dump file names
936 for (fn
= fnhead
; fn
&& debuginfo
; fn
= fn
->next
) {
937 ieee_putascii("C0105,%02X%s.\n", strlen(fn
->name
), fn
->name
);
940 ieee_putascii("CO101,07ENDHEAD.\n");
942 * the standard doesn't specify when to put checksums,
943 * we'll just do it periodically.
948 * Write the section headers
951 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
956 switch (seg
->combine
) {
968 ieee_unqualified_name(buf
, seg
->name
);
969 if (seg
->align
>= SEG_ABS
) {
970 ieee_putascii("ST%X,A,%02X%s.\n", seg
->ieee_index
,
972 ieee_putascii("ASL%X,%lX.\n", seg
->ieee_index
,
973 (seg
->align
- SEG_ABS
) * 16);
975 ieee_putascii("ST%X,%c,%02X%s.\n", seg
->ieee_index
, attrib
,
977 ieee_putascii("SA%X,%lX.\n", seg
->ieee_index
, seg
->align
);
978 ieee_putascii("ASS%X,%X.\n", seg
->ieee_index
,
984 * write the start address if there is one
986 if (ieee_entry_seg
) {
987 for (seg
= seghead
; seg
; seg
= seg
->next
)
988 if (seg
->index
== ieee_entry_seg
)
991 nasm_panic(0, "Start address records are incorrect");
993 ieee_putascii("ASG,R%X,%lX,+.\n", seg
->ieee_index
,
1002 for (seg
= seghead
; seg
; seg
= seg
->next
) {
1003 for (pub
= seg
->pubhead
; pub
; pub
= pub
->next
) {
1005 ieee_unqualified_name(buf
, pub
->name
);
1006 ieee_putascii("NI%X,%02X%s.\n", i
, strlen(buf
), buf
);
1007 if (pub
->segment
== -1)
1008 ieee_putascii("ASI%X,R%X,%lX,+.\n", i
, pub
->index
,
1011 ieee_putascii("ASI%X,%lX,%lX,+.\n", i
, pub
->segment
* 16,
1014 if (pub
->type
>= 0x100)
1015 ieee_putascii("ATI%X,T%X.\n", i
, pub
->type
- 0x100);
1017 ieee_putascii("ATI%X,%X.\n", i
, pub
->type
);
1026 ieee_unqualified_name(buf
, pub
->name
);
1027 ieee_putascii("NI%X,%02X%s.\n", i
, strlen(buf
), buf
);
1028 if (pub
->segment
== -1)
1029 ieee_putascii("ASI%X,R%X,%lX,+.\n", i
, pub
->index
,
1032 ieee_putascii("ASI%X,%lX,%lX,+.\n", i
, pub
->segment
* 16,
1035 if (pub
->type
>= 0x100)
1036 ieee_putascii("ATI%X,T%X.\n", i
, pub
->type
- 0x100);
1038 ieee_putascii("ATI%X,%X.\n", i
, pub
->type
);
1044 * Write the externals
1050 ieee_unqualified_name(buf
, ext
->name
);
1051 ieee_putascii("NX%X,%02X%s.\n", i
++, strlen(buf
), buf
);
1057 * IEEE doesn't have a standard pass break record
1058 * so use the ladsoft variant
1060 ieee_putascii("CO100,06ENDSYM.\n");
1066 for (arr
= arrhead
; arr
&& debuginfo
; arr
= arr
->next
) {
1067 ieee_putascii("TY%X,20,%X,%lX.\n", i
++, arr
->basetype
,
1074 for (seg
= seghead
; seg
&& debuginfo
; seg
= seg
->next
) {
1075 for (loc
= seg
->lochead
; loc
; loc
= loc
->next
) {
1077 ieee_unqualified_name(buf
, loc
->name
);
1078 ieee_putascii("NN%X,%02X%s.\n", i
, strlen(buf
), buf
);
1079 if (loc
->segment
== -1)
1080 ieee_putascii("ASN%X,R%X,%lX,+.\n", i
, loc
->index
,
1083 ieee_putascii("ASN%X,%lX,%lX,+.\n", i
, loc
->segment
* 16,
1086 if (loc
->type
>= 0x100)
1087 ieee_putascii("ATN%X,T%X.\n", i
, loc
->type
- 0x100);
1089 ieee_putascii("ATN%X,%X.\n", i
, loc
->type
);
1096 * put out section data;
1099 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
1102 if (seg
->currentpos
) {
1103 int32_t size
, org
= 0;
1105 ieee_putascii("SB%X.\n", seg
->ieee_index
);
1108 size
= HUNKSIZE
- (org
% HUNKSIZE
);
1111 seg
->currentpos
? seg
->currentpos
- org
: size
;
1112 size
= fix
->offset
- org
> size
? size
: fix
->offset
- org
;
1113 org
= ieee_putld(org
, org
+ size
, data
->data
);
1114 if (org
% HUNKSIZE
== 0)
1116 if (org
== fix
->offset
) {
1117 org
+= ieee_putlr(fix
);
1121 while (org
< seg
->currentpos
&& data
) {
1123 seg
->currentpos
- org
>
1124 HUNKSIZE
? HUNKSIZE
: seg
->currentpos
- org
;
1125 org
= ieee_putld(org
, org
+ size
, data
->data
);
1136 ieee_putascii("ME.\n");
1139 static void ieee_write_byte(struct ieeeSection
*seg
, int data
)
1142 if (!(temp
= seg
->currentpos
++ % HUNKSIZE
))
1144 seg
->datacurr
->data
[temp
] = data
;
1147 static void ieee_write_word(struct ieeeSection
*seg
, int data
)
1149 ieee_write_byte(seg
, data
& 0xFF);
1150 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1153 static void ieee_write_dword(struct ieeeSection
*seg
, int32_t data
)
1155 ieee_write_byte(seg
, data
& 0xFF);
1156 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1157 ieee_write_byte(seg
, (data
>> 16) & 0xFF);
1158 ieee_write_byte(seg
, (data
>> 24) & 0xFF);
1160 static void ieee_putascii(char *format
, ...)
1166 va_start(ap
, format
);
1167 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1169 for (i
= 0; i
< l
; i
++)
1170 if ((uint8_t)buffer
[i
] > 31)
1171 checksum
+= buffer
[i
];
1173 fputs(buffer
, ofile
);
1177 * put out a checksum record */
1178 static void ieee_putcs(int toclear
)
1181 ieee_putascii("CS.\n");
1185 ieee_putascii("CS%02X.\n", checksum
& 127);
1190 static int32_t ieee_putld(int32_t start
, int32_t end
, uint8_t *buf
)
1195 val
= start
% HUNKSIZE
;
1196 /* fill up multiple lines */
1197 while (end
- start
>= LDPERLINE
) {
1199 ieee_putascii("LD");
1200 for (i
= 0; i
< LDPERLINE
; i
++) {
1201 ieee_putascii("%02X", buf
[val
++]);
1204 ieee_putascii(".\n");
1206 /* if no partial lines */
1209 /* make a partial line */
1210 ieee_putascii("LD");
1211 while (start
< end
) {
1212 ieee_putascii("%02X", buf
[val
++]);
1215 ieee_putascii(".\n");
1218 static int32_t ieee_putlr(struct ieeeFixupp
*p
)
1221 * To deal with the vagaries of segmentation the LADsoft linker
1222 * defines two types of segments: absolute and virtual. Note that
1223 * 'absolute' in this context is a different thing from the IEEE
1224 * definition of an absolute segment type, which is also supported. If a
1225 * sement is linked in virtual mode the low limit (L-var) is
1226 * subtracted from each R,X, and P variable which appears in an
1227 * expression, so that we can have relative offsets. Meanwhile
1228 * in the ABSOLUTE mode this subtraction is not done and
1229 * so we can use absolute offsets from 0. In the LADsoft linker
1230 * this configuration is not done in the assemblker source but in
1231 * a source the linker reads. Generally this type of thing only
1232 * becomes an issue if real mode code is used. A pure 32-bit linker could
1233 * get away without defining the virtual mode...
1236 int32_t size
= p
->size
;
1240 sprintf(buf
, "%"PRIX32
"", -p
->id1
);
1242 sprintf(buf
, "L%"PRIX32
",10,/", p
->id1
);
1245 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+", p
->id1
, p
->addend
);
1248 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,P,-,%X,-", p
->id1
, p
->addend
, p
->size
);
1253 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->addend
,
1254 p
->id2
, -p
->id1
* 16);
1256 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->addend
,
1260 sprintf(buf
, "X%"PRIX32
"", p
->id1
);
1263 sprintf(buf
, "X%"PRIX32
",P,-,%"PRIX32
",-", p
->id1
, size
);
1266 /* We needed a non-ieee hack here.
1267 * We introduce the Y variable, which is the low
1268 * limit of the native segment the extern resides in
1270 sprintf(buf
, "Y%"PRIX32
",10,/", p
->id1
);
1274 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->id2
,
1277 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->id2
, p
->id1
);
1280 ieee_putascii("LR(%s,%"PRIX32
").\n", buf
, size
);
1285 /* Dump all segment data (text and fixups )*/
1287 static void ieee_unqualified_name(char *dest
, char *source
)
1289 if (ieee_uppercase
) {
1291 *dest
++ = toupper(*source
++);
1294 strcpy(dest
, source
);
1296 static void dbgls_init(void)
1302 arrindex
= ARRAY_BOT
;
1305 ieee_segment("??LINE", 2, &tempint
);
1308 static void dbgls_cleanup(void)
1310 struct ieeeSection
*segtmp
;
1312 struct FileName
*fntemp
= fnhead
;
1313 fnhead
= fnhead
->next
;
1314 nasm_free(fntemp
->name
);
1317 for (segtmp
= seghead
; segtmp
; segtmp
= segtmp
->next
) {
1318 while (segtmp
->lochead
) {
1319 struct ieeePublic
*loctmp
= segtmp
->lochead
;
1320 segtmp
->lochead
= loctmp
->next
;
1321 nasm_free(loctmp
->name
);
1326 struct Array
*arrtmp
= arrhead
;
1327 arrhead
= arrhead
->next
;
1333 * because this routine is not bracketed in
1334 * the main program, this routine will be called even if there
1335 * is no request for debug info
1336 * so, we have to make sure the ??LINE segment is avaialbe
1337 * as the first segment when this debug format is selected
1339 static void dbgls_linnum(const char *lnfname
, int32_t lineno
, int32_t segto
)
1341 struct FileName
*fn
;
1342 struct ieeeSection
*seg
;
1344 if (segto
== NO_SEG
)
1348 * If `any_segs' is still false, we must define a default
1352 int tempint
; /* ignored */
1353 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
1354 nasm_panic(0, "strange segment conditions in OBJ driver");
1358 * Find the segment we are targetting.
1360 for (seg
= seghead
; seg
; seg
= seg
->next
)
1361 if (seg
->index
== segto
)
1364 nasm_panic(0, "lineno directed to nonexistent segment?");
1366 for (fn
= fnhead
; fn
; fn
= fn
->next
) {
1367 if (!nasm_stricmp(lnfname
, fn
->name
))
1372 fn
= nasm_malloc(sizeof(*fn
));
1373 fn
->name
= nasm_malloc(strlen(lnfname
) + 1);
1375 strcpy(fn
->name
, lnfname
);
1380 ieee_write_byte(seghead
, fn
->index
);
1381 ieee_write_word(seghead
, lineno
);
1382 ieee_write_fixup(segto
, NO_SEG
, seghead
, 4, OUT_ADDRESS
,
1386 static void dbgls_deflabel(char *name
, int32_t segment
,
1387 int64_t offset
, int is_global
, char *special
)
1389 struct ieeeSection
*seg
;
1391 /* Keep compiler from warning about special */
1395 * Note: ..[^@] special symbols are filtered in labels.c
1399 * If it's a special-retry from pass two, discard it.
1407 if (ieee_seg_needs_update
)
1409 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
1412 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
1417 * If `any_segs' is still false, we might need to define a
1418 * default segment, if they're trying to declare a label in
1419 * `first_seg'. But the label should exist due to a prior
1420 * call to ieee_deflabel so we can skip that.
1423 for (seg
= seghead
; seg
; seg
= seg
->next
)
1424 if (seg
->index
== segment
) {
1425 struct ieeePublic
*loc
;
1427 * Case (ii). Maybe MODPUB someday?
1430 last_defined
= loc
= nasm_malloc(sizeof(*loc
));
1431 *seg
->loctail
= loc
;
1432 seg
->loctail
= &loc
->next
;
1434 loc
->name
= nasm_strdup(name
);
1435 loc
->offset
= offset
;
1437 loc
->index
= seg
->ieee_index
;
1441 static void dbgls_typevalue(int32_t type
)
1443 int elem
= TYM_ELEMENTS(type
);
1444 type
= TYM_TYPE(type
);
1451 last_defined
->type
= 1; /* uint8_t */
1454 last_defined
->type
= 3; /* unsigned word */
1457 last_defined
->type
= 5; /* unsigned dword */
1460 last_defined
->type
= 9; /* float */
1463 last_defined
->type
= 10; /* qword */
1466 last_defined
->type
= 11; /* TBYTE */
1469 last_defined
->type
= 0x10; /* near label */
1474 struct Array
*arrtmp
= nasm_malloc(sizeof(*arrtmp
));
1475 int vtype
= last_defined
->type
;
1476 arrtmp
->size
= elem
;
1477 arrtmp
->basetype
= vtype
;
1478 arrtmp
->next
= NULL
;
1479 last_defined
->type
= arrindex
++ + 0x100;
1481 arrtail
= &(arrtmp
->next
);
1483 last_defined
= NULL
;
1485 static void dbgls_output(int output_type
, void *param
)
1490 static const struct dfmt ladsoft_debug_form
= {
1491 "LADsoft Debug Records",
1496 null_debug_directive
,
1500 NULL
/* pragma list */
1502 static const struct dfmt
* const ladsoft_debug_arr
[3] = {
1503 &ladsoft_debug_form
,
1507 const struct ofmt of_ieee
= {
1508 "IEEE-695 (LADsoft variant) object file format",
1513 &ladsoft_debug_form
,
1516 nasm_do_legacy_output
,
1525 NULL
/* pragma list */
1528 #endif /* OF_IEEE */