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;
227 static int ieee_set_info(enum geninfo type
, char **val
)
238 static void ieee_cleanup(void)
243 struct ieeeSection
*segtmp
= seghead
;
244 seghead
= seghead
->next
;
245 while (segtmp
->pubhead
) {
246 struct ieeePublic
*pubtmp
= segtmp
->pubhead
;
247 segtmp
->pubhead
= pubtmp
->next
;
250 while (segtmp
->fptr
) {
251 struct ieeeFixupp
*fixtmp
= segtmp
->fptr
;
252 segtmp
->fptr
= fixtmp
->next
;
255 while (segtmp
->data
) {
256 struct ieeeObjData
*dattmp
= segtmp
->data
;
257 segtmp
->data
= dattmp
->next
;
263 struct ieeePublic
*pubtmp
= fpubhead
;
264 fpubhead
= fpubhead
->next
;
268 struct ieeeExternal
*exttmp
= exthead
;
269 exthead
= exthead
->next
;
273 struct ExtBack
*ebtmp
= ebhead
;
274 ebhead
= ebhead
->next
;
280 * callback for labels
282 static void ieee_deflabel(char *name
, int32_t segment
,
283 int64_t offset
, int is_global
, char *special
)
286 * We have three cases:
288 * (i) `segment' is a segment-base. If so, set the name field
289 * for the segment structure it refers to, and then
292 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
293 * Save the label position for later output of a PUBDEF record.
296 * (iii) `segment' is not one of our segments. Save the label
297 * position for later output of an EXTDEF.
299 struct ieeeExternal
*ext
;
301 struct ieeeSection
*seg
;
305 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%s'", special
);
308 * First check for the double-period, signifying something
311 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
312 if (!strcmp(name
, "..start")) {
313 ieee_entry_seg
= segment
;
314 ieee_entry_ofs
= offset
;
322 if (ieee_seg_needs_update
) {
323 ieee_seg_needs_update
->name
= name
;
326 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
332 if (segment
>= SEG_ABS
) {
334 * SEG_ABS subcase of (ii).
337 struct ieeePublic
*pub
;
339 pub
= *fpubtail
= nasm_malloc(sizeof(*pub
));
340 fpubtail
= &pub
->next
;
343 pub
->offset
= offset
;
344 pub
->segment
= segment
& ~SEG_ABS
;
349 for (seg
= seghead
; seg
&& is_global
; seg
= seg
->next
)
350 if (seg
->index
== segment
) {
351 struct ieeePublic
*pub
;
353 last_defined
= pub
= *seg
->pubtail
= nasm_malloc(sizeof(*pub
));
354 seg
->pubtail
= &pub
->next
;
357 pub
->offset
= offset
;
358 pub
->index
= seg
->ieee_index
;
367 ext
= *exttail
= nasm_malloc(sizeof(*ext
));
369 exttail
= &ext
->next
;
372 ext
->commonsize
= offset
;
378 eb
= *ebtail
= nasm_zalloc(sizeof(*eb
));
382 while (i
> EXT_BLKSIZ
) {
386 eb
= *ebtail
= nasm_zalloc(sizeof(*eb
));
392 eb
->index
[i
] = externals
++;
400 static void ieee_out(int32_t segto
, const void *data
,
401 enum out_type type
, uint64_t size
,
402 int32_t segment
, int32_t wrt
)
404 const uint8_t *ucdata
;
406 struct ieeeSection
*seg
;
409 * handle absolute-assembly (structure definitions)
411 if (segto
== NO_SEG
) {
412 if (type
!= OUT_RESERVE
)
413 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
419 * If `any_segs' is still false, we must define a default
423 int tempint
; /* ignored */
424 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
425 nasm_panic(0, "strange segment conditions in IEEE driver");
429 * Find the segment we are targetting.
431 for (seg
= seghead
; seg
; seg
= seg
->next
)
432 if (seg
->index
== segto
)
435 nasm_panic(0, "code directed to nonexistent segment?");
437 if (type
== OUT_RAWDATA
) {
440 ieee_write_byte(seg
, *ucdata
++);
441 } else if (type
== OUT_ADDRESS
|| type
== OUT_REL2ADR
||
442 type
== OUT_REL4ADR
) {
443 if (type
== OUT_ADDRESS
)
444 size
= abs((int)size
);
445 else if (segment
== NO_SEG
)
446 nasm_error(ERR_NONFATAL
, "relative call to absolute address not"
447 " supported by IEEE format");
448 ldata
= *(int64_t *)data
;
449 if (type
== OUT_REL2ADR
)
451 if (type
== OUT_REL4ADR
)
453 ieee_write_fixup(segment
, wrt
, seg
, size
, type
, ldata
);
455 ieee_write_word(seg
, ldata
);
457 ieee_write_dword(seg
, ldata
);
458 } else if (type
== OUT_RESERVE
) {
460 ieee_write_byte(seg
, 0);
464 static void ieee_data_new(struct ieeeSection
*segto
)
468 segto
->data
= segto
->datacurr
=
469 nasm_malloc(sizeof(*(segto
->datacurr
)));
471 segto
->datacurr
= segto
->datacurr
->next
=
472 nasm_malloc(sizeof(*(segto
->datacurr
)));
473 segto
->datacurr
->next
= NULL
;
477 * this routine is unalduterated bloatware. I usually don't do this
478 * but I might as well see what it is like on a harmless program.
479 * If anyone wants to optimize this is a good canditate!
481 static void ieee_write_fixup(int32_t segment
, int32_t wrt
,
482 struct ieeeSection
*segto
, int size
,
483 uint64_t realtype
, int32_t offset
)
485 struct ieeeSection
*target
;
488 /* Don't put a fixup for things NASM can calculate */
489 if (wrt
== NO_SEG
&& segment
== NO_SEG
)
493 /* if it is a WRT offset */
498 s
.id1
= -(wrt
- SEG_ABS
);
500 if (wrt
% 2 && realtype
!= OUT_REL2ADR
501 && realtype
!= OUT_REL4ADR
) {
504 for (target
= seghead
; target
; target
= target
->next
)
505 if (target
->index
== wrt
)
508 s
.id1
= target
->ieee_index
;
509 for (target
= seghead
; target
; target
= target
->next
)
510 if (target
->index
== segment
)
514 s
.id2
= target
->ieee_index
;
517 * Now we assume the segment field is being used
518 * to hold an extern index
520 int32_t i
= segment
/ 2;
521 struct ExtBack
*eb
= ebhead
;
522 while (i
> EXT_BLKSIZ
) {
529 /* if we have an extern decide the type and make a record
534 s
.id2
= eb
->index
[i
];
536 nasm_error(ERR_NONFATAL
,
537 "Source of WRT must be an offset");
542 "unrecognised WRT value in ieee_write_fixup");
544 nasm_error(ERR_NONFATAL
, "target of WRT must be a section ");
547 ieee_install_fixup(segto
, &s
);
550 /* Pure segment fixup ? */
551 if (segment
!= NO_SEG
) {
554 if (segment
>= SEG_ABS
) {
555 /* absolute far segment fixup */
556 s
.id1
= -(segment
- ~SEG_ABS
);
557 } else if (segment
% 2) {
558 /* fixup to named segment */
560 for (target
= seghead
; target
; target
= target
->next
)
561 if (target
->index
== segment
- 1)
564 s
.id1
= target
->ieee_index
;
567 * Now we assume the segment field is being used
568 * to hold an extern index
570 int32_t i
= segment
/ 2;
571 struct ExtBack
*eb
= ebhead
;
572 while (i
> EXT_BLKSIZ
) {
579 /* if we have an extern decide the type and make a record
582 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
584 "Segment of a rel not supported in ieee_write_fixup");
586 /* If we want the segment */
589 s
.id1
= eb
->index
[i
];
593 /* If we get here the seg value doesn't make sense */
595 "unrecognised segment value in ieee_write_fixup");
599 /* Assume we are offsetting directly from a section
600 * So look up the target segment
602 for (target
= seghead
; target
; target
= target
->next
)
603 if (target
->index
== segment
)
606 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
607 /* PC rel to a known offset */
608 s
.id1
= target
->ieee_index
;
613 /* We were offsetting from a seg */
614 s
.id1
= target
->ieee_index
;
621 * Now we assume the segment field is being used
622 * to hold an extern index
624 int32_t i
= segment
/ 2;
625 struct ExtBack
*eb
= ebhead
;
626 while (i
> EXT_BLKSIZ
) {
633 /* if we have an extern decide the type and make a record
636 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
639 s
.id1
= eb
->index
[i
];
641 /* else we want the external offset */
644 s
.id1
= eb
->index
[i
];
648 /* If we get here the seg value doesn't make sense */
650 "unrecognised segment value in ieee_write_fixup");
653 if (size
!= 2 && s
.ftype
== FT_SEG
)
654 nasm_error(ERR_NONFATAL
, "IEEE format can only handle 2-byte"
655 " segment base references");
657 ieee_install_fixup(segto
, &s
);
660 /* should never get here */
662 static void ieee_install_fixup(struct ieeeSection
*seg
,
663 struct ieeeFixupp
*fix
)
665 struct ieeeFixupp
*f
;
666 f
= nasm_malloc(sizeof(struct ieeeFixupp
));
667 memcpy(f
, fix
, sizeof(struct ieeeFixupp
));
668 f
->offset
= seg
->currentpos
;
669 seg
->currentpos
+= fix
->size
;
672 seg
->flptr
= seg
->flptr
->next
= f
;
674 seg
->fptr
= seg
->flptr
= f
;
681 static int32_t ieee_segment(char *name
, int pass
, int *bits
)
684 * We call the label manager here to define a name for the new
685 * segment, and when our _own_ label-definition stub gets
686 * called in return, it should register the new segment name
687 * using the pointer it gets passed. That way we save memory,
688 * by sponging off the label manager.
694 return seghead
->index
;
696 struct ieeeSection
*seg
;
702 * Look for segment attributes.
706 name
++; /* hack, but a documented one */
708 while (*p
&& !nasm_isspace(*p
))
712 while (*p
&& nasm_isspace(*p
))
716 while (*p
&& !nasm_isspace(*p
))
720 while (*p
&& nasm_isspace(*p
))
728 for (seg
= seghead
; seg
; seg
= seg
->next
) {
730 if (!strcmp(seg
->name
, name
)) {
731 if (attrs
> 0 && pass
== 1)
732 nasm_error(ERR_WARNING
, "segment attributes specified on"
733 " redeclaration of segment: ignoring");
742 *segtail
= seg
= nasm_malloc(sizeof(*seg
));
744 segtail
= &seg
->next
;
745 seg
->index
= seg_alloc();
746 seg
->ieee_index
= ieee_idx
;
750 seg
->align
= 1; /* default */
751 seg
->use32
= *bits
== 32; /* default to user spec */
752 seg
->combine
= CMB_PUBLIC
; /* default */
754 seg
->pubtail
= &seg
->pubhead
;
758 seg
->loctail
= &seg
->lochead
;
761 * Process the segment attributes.
770 * `p' contains a segment attribute.
772 if (!nasm_stricmp(p
, "private"))
773 seg
->combine
= CMB_PRIVATE
;
774 else if (!nasm_stricmp(p
, "public"))
775 seg
->combine
= CMB_PUBLIC
;
776 else if (!nasm_stricmp(p
, "common"))
777 seg
->combine
= CMB_COMMON
;
778 else if (!nasm_stricmp(p
, "use16"))
780 else if (!nasm_stricmp(p
, "use32"))
782 else if (!nasm_strnicmp(p
, "align=", 6)) {
783 seg
->align
= readnum(p
+ 6, &rn_error
);
788 nasm_error(ERR_NONFATAL
, "segment alignment should be"
791 switch (seg
->align
) {
803 nasm_error(ERR_NONFATAL
, "invalid alignment value %d",
808 } else if (!nasm_strnicmp(p
, "absolute=", 9)) {
809 seg
->align
= SEG_ABS
+ readnum(p
+ 9, &rn_error
);
811 nasm_error(ERR_NONFATAL
, "argument to `absolute' segment"
812 " attribute should be numeric");
816 ieee_seg_needs_update
= seg
;
817 if (seg
->align
>= SEG_ABS
)
818 define_label(name
, NO_SEG
, seg
->align
- SEG_ABS
,
821 define_label(name
, seg
->index
+ 1, 0L, NULL
, false, false);
822 ieee_seg_needs_update
= NULL
;
833 * directives supported
835 static int ieee_directive(enum directives directive
, char *value
, int pass
)
843 ieee_uppercase
= true;
851 static void ieee_sectalign(int32_t seg
, unsigned int value
)
853 struct ieeeSection
*s
;
855 list_for_each(s
, seghead
) {
861 * 256 is maximum there, note it may happen
862 * that align is issued on "absolute" segment
863 * it's fine since SEG_ABS > 256 and we never
864 * get escape this test
866 if (!s
|| !is_power2(value
) || value
> 256)
869 if ((unsigned int)s
->align
< value
)
874 * Return segment data
876 static int32_t ieee_segbase(int32_t segment
)
878 struct ieeeSection
*seg
;
881 * Find the segment in our list.
883 for (seg
= seghead
; seg
; seg
= seg
->next
)
884 if (seg
->index
== segment
- 1)
888 return segment
; /* not one of ours - leave it alone */
890 if (seg
->align
>= SEG_ABS
)
891 return seg
->align
; /* absolute segment */
893 return segment
; /* no special treatment */
899 static void ieee_filename(char *inname
, char *outname
)
901 strcpy(ieee_infile
, inname
);
902 standard_extension(inname
, outname
, ".o");
905 static void ieee_write_file(void)
910 struct ieeeSection
*seg
;
911 struct ieeePublic
*pub
, *loc
;
912 struct ieeeExternal
*ext
;
913 struct ieeeObjData
*data
;
914 struct ieeeFixupp
*fix
;
917 const bool debuginfo
= (dfmt
== &ladsoft_debug_form
);
920 * Write the module header
922 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile
), ieee_infile
);
925 * Write the NASM boast comment.
927 ieee_putascii("CO0,%02X%s.\n", strlen(nasm_comment
), nasm_comment
);
930 * write processor-specific information
932 ieee_putascii("AD8,4,L.\n");
938 thetime
= localtime(&reltime
);
939 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
940 1900 + thetime
->tm_year
, thetime
->tm_mon
+ 1,
941 thetime
->tm_mday
, thetime
->tm_hour
, thetime
->tm_min
,
944 * if debugging, dump file names
946 for (fn
= fnhead
; fn
&& debuginfo
; fn
= fn
->next
) {
947 ieee_putascii("C0105,%02X%s.\n", strlen(fn
->name
), fn
->name
);
950 ieee_putascii("CO101,07ENDHEAD.\n");
952 * the standard doesn't specify when to put checksums,
953 * we'll just do it periodically.
958 * Write the section headers
961 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
966 switch (seg
->combine
) {
978 ieee_unqualified_name(buf
, seg
->name
);
979 if (seg
->align
>= SEG_ABS
) {
980 ieee_putascii("ST%X,A,%02X%s.\n", seg
->ieee_index
,
982 ieee_putascii("ASL%X,%lX.\n", seg
->ieee_index
,
983 (seg
->align
- SEG_ABS
) * 16);
985 ieee_putascii("ST%X,%c,%02X%s.\n", seg
->ieee_index
, attrib
,
987 ieee_putascii("SA%X,%lX.\n", seg
->ieee_index
, seg
->align
);
988 ieee_putascii("ASS%X,%X.\n", seg
->ieee_index
,
994 * write the start address if there is one
996 if (ieee_entry_seg
) {
997 for (seg
= seghead
; seg
; seg
= seg
->next
)
998 if (seg
->index
== ieee_entry_seg
)
1001 nasm_panic(0, "Start address records are incorrect");
1003 ieee_putascii("ASG,R%X,%lX,+.\n", seg
->ieee_index
,
1012 for (seg
= seghead
; seg
; seg
= seg
->next
) {
1013 for (pub
= seg
->pubhead
; pub
; pub
= pub
->next
) {
1015 ieee_unqualified_name(buf
, pub
->name
);
1016 ieee_putascii("NI%X,%02X%s.\n", i
, strlen(buf
), buf
);
1017 if (pub
->segment
== -1)
1018 ieee_putascii("ASI%X,R%X,%lX,+.\n", i
, pub
->index
,
1021 ieee_putascii("ASI%X,%lX,%lX,+.\n", i
, pub
->segment
* 16,
1024 if (pub
->type
>= 0x100)
1025 ieee_putascii("ATI%X,T%X.\n", i
, pub
->type
- 0x100);
1027 ieee_putascii("ATI%X,%X.\n", i
, pub
->type
);
1036 ieee_unqualified_name(buf
, pub
->name
);
1037 ieee_putascii("NI%X,%02X%s.\n", i
, strlen(buf
), buf
);
1038 if (pub
->segment
== -1)
1039 ieee_putascii("ASI%X,R%X,%lX,+.\n", i
, pub
->index
,
1042 ieee_putascii("ASI%X,%lX,%lX,+.\n", i
, pub
->segment
* 16,
1045 if (pub
->type
>= 0x100)
1046 ieee_putascii("ATI%X,T%X.\n", i
, pub
->type
- 0x100);
1048 ieee_putascii("ATI%X,%X.\n", i
, pub
->type
);
1054 * Write the externals
1060 ieee_unqualified_name(buf
, ext
->name
);
1061 ieee_putascii("NX%X,%02X%s.\n", i
++, strlen(buf
), buf
);
1067 * IEEE doesn't have a standard pass break record
1068 * so use the ladsoft variant
1070 ieee_putascii("CO100,06ENDSYM.\n");
1076 for (arr
= arrhead
; arr
&& debuginfo
; arr
= arr
->next
) {
1077 ieee_putascii("TY%X,20,%X,%lX.\n", i
++, arr
->basetype
,
1084 for (seg
= seghead
; seg
&& debuginfo
; seg
= seg
->next
) {
1085 for (loc
= seg
->lochead
; loc
; loc
= loc
->next
) {
1087 ieee_unqualified_name(buf
, loc
->name
);
1088 ieee_putascii("NN%X,%02X%s.\n", i
, strlen(buf
), buf
);
1089 if (loc
->segment
== -1)
1090 ieee_putascii("ASN%X,R%X,%lX,+.\n", i
, loc
->index
,
1093 ieee_putascii("ASN%X,%lX,%lX,+.\n", i
, loc
->segment
* 16,
1096 if (loc
->type
>= 0x100)
1097 ieee_putascii("ATN%X,T%X.\n", i
, loc
->type
- 0x100);
1099 ieee_putascii("ATN%X,%X.\n", i
, loc
->type
);
1106 * put out section data;
1109 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
1112 if (seg
->currentpos
) {
1113 int32_t size
, org
= 0;
1115 ieee_putascii("SB%X.\n", seg
->ieee_index
);
1118 size
= HUNKSIZE
- (org
% HUNKSIZE
);
1121 seg
->currentpos
? seg
->currentpos
- org
: size
;
1122 size
= fix
->offset
- org
> size
? size
: fix
->offset
- org
;
1123 org
= ieee_putld(org
, org
+ size
, data
->data
);
1124 if (org
% HUNKSIZE
== 0)
1126 if (org
== fix
->offset
) {
1127 org
+= ieee_putlr(fix
);
1131 while (org
< seg
->currentpos
&& data
) {
1133 seg
->currentpos
- org
>
1134 HUNKSIZE
? HUNKSIZE
: seg
->currentpos
- org
;
1135 org
= ieee_putld(org
, org
+ size
, data
->data
);
1146 ieee_putascii("ME.\n");
1149 static void ieee_write_byte(struct ieeeSection
*seg
, int data
)
1152 if (!(temp
= seg
->currentpos
++ % HUNKSIZE
))
1154 seg
->datacurr
->data
[temp
] = data
;
1157 static void ieee_write_word(struct ieeeSection
*seg
, int data
)
1159 ieee_write_byte(seg
, data
& 0xFF);
1160 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1163 static void ieee_write_dword(struct ieeeSection
*seg
, int32_t data
)
1165 ieee_write_byte(seg
, data
& 0xFF);
1166 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1167 ieee_write_byte(seg
, (data
>> 16) & 0xFF);
1168 ieee_write_byte(seg
, (data
>> 24) & 0xFF);
1170 static void ieee_putascii(char *format
, ...)
1176 va_start(ap
, format
);
1177 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1179 for (i
= 0; i
< l
; i
++)
1180 if ((uint8_t)buffer
[i
] > 31)
1181 checksum
+= buffer
[i
];
1183 fputs(buffer
, ofile
);
1187 * put out a checksum record */
1188 static void ieee_putcs(int toclear
)
1191 ieee_putascii("CS.\n");
1195 ieee_putascii("CS%02X.\n", checksum
& 127);
1200 static int32_t ieee_putld(int32_t start
, int32_t end
, uint8_t *buf
)
1205 val
= start
% HUNKSIZE
;
1206 /* fill up multiple lines */
1207 while (end
- start
>= LDPERLINE
) {
1209 ieee_putascii("LD");
1210 for (i
= 0; i
< LDPERLINE
; i
++) {
1211 ieee_putascii("%02X", buf
[val
++]);
1214 ieee_putascii(".\n");
1216 /* if no partial lines */
1219 /* make a partial line */
1220 ieee_putascii("LD");
1221 while (start
< end
) {
1222 ieee_putascii("%02X", buf
[val
++]);
1225 ieee_putascii(".\n");
1228 static int32_t ieee_putlr(struct ieeeFixupp
*p
)
1231 * To deal with the vagaries of segmentation the LADsoft linker
1232 * defines two types of segments: absolute and virtual. Note that
1233 * 'absolute' in this context is a different thing from the IEEE
1234 * definition of an absolute segment type, which is also supported. If a
1235 * sement is linked in virtual mode the low limit (L-var) is
1236 * subtracted from each R,X, and P variable which appears in an
1237 * expression, so that we can have relative offsets. Meanwhile
1238 * in the ABSOLUTE mode this subtraction is not done and
1239 * so we can use absolute offsets from 0. In the LADsoft linker
1240 * this configuration is not done in the assemblker source but in
1241 * a source the linker reads. Generally this type of thing only
1242 * becomes an issue if real mode code is used. A pure 32-bit linker could
1243 * get away without defining the virtual mode...
1246 int32_t size
= p
->size
;
1250 sprintf(buf
, "%"PRIX32
"", -p
->id1
);
1252 sprintf(buf
, "L%"PRIX32
",10,/", p
->id1
);
1255 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+", p
->id1
, p
->addend
);
1258 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,P,-,%X,-", p
->id1
, p
->addend
, p
->size
);
1263 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->addend
,
1264 p
->id2
, -p
->id1
* 16);
1266 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->addend
,
1270 sprintf(buf
, "X%"PRIX32
"", p
->id1
);
1273 sprintf(buf
, "X%"PRIX32
",P,-,%"PRIX32
",-", p
->id1
, size
);
1276 /* We needed a non-ieee hack here.
1277 * We introduce the Y variable, which is the low
1278 * limit of the native segment the extern resides in
1280 sprintf(buf
, "Y%"PRIX32
",10,/", p
->id1
);
1284 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->id2
,
1287 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->id2
, p
->id1
);
1290 ieee_putascii("LR(%s,%"PRIX32
").\n", buf
, size
);
1295 /* Dump all segment data (text and fixups )*/
1297 static void ieee_unqualified_name(char *dest
, char *source
)
1299 if (ieee_uppercase
) {
1301 *dest
++ = toupper(*source
++);
1304 strcpy(dest
, source
);
1306 static void dbgls_init(void)
1312 arrindex
= ARRAY_BOT
;
1315 ieee_segment("??LINE", 2, &tempint
);
1318 static void dbgls_cleanup(void)
1320 struct ieeeSection
*segtmp
;
1322 struct FileName
*fntemp
= fnhead
;
1323 fnhead
= fnhead
->next
;
1324 nasm_free(fntemp
->name
);
1327 for (segtmp
= seghead
; segtmp
; segtmp
= segtmp
->next
) {
1328 while (segtmp
->lochead
) {
1329 struct ieeePublic
*loctmp
= segtmp
->lochead
;
1330 segtmp
->lochead
= loctmp
->next
;
1331 nasm_free(loctmp
->name
);
1336 struct Array
*arrtmp
= arrhead
;
1337 arrhead
= arrhead
->next
;
1343 * because this routine is not bracketed in
1344 * the main program, this routine will be called even if there
1345 * is no request for debug info
1346 * so, we have to make sure the ??LINE segment is avaialbe
1347 * as the first segment when this debug format is selected
1349 static void dbgls_linnum(const char *lnfname
, int32_t lineno
, int32_t segto
)
1351 struct FileName
*fn
;
1352 struct ieeeSection
*seg
;
1354 if (segto
== NO_SEG
)
1358 * If `any_segs' is still false, we must define a default
1362 int tempint
; /* ignored */
1363 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
1364 nasm_panic(0, "strange segment conditions in OBJ driver");
1368 * Find the segment we are targetting.
1370 for (seg
= seghead
; seg
; seg
= seg
->next
)
1371 if (seg
->index
== segto
)
1374 nasm_panic(0, "lineno directed to nonexistent segment?");
1376 for (fn
= fnhead
; fn
; fn
= fn
->next
) {
1377 if (!nasm_stricmp(lnfname
, fn
->name
))
1382 fn
= nasm_malloc(sizeof(*fn
));
1383 fn
->name
= nasm_malloc(strlen(lnfname
) + 1);
1385 strcpy(fn
->name
, lnfname
);
1390 ieee_write_byte(seghead
, fn
->index
);
1391 ieee_write_word(seghead
, lineno
);
1392 ieee_write_fixup(segto
, NO_SEG
, seghead
, 4, OUT_ADDRESS
,
1396 static void dbgls_deflabel(char *name
, int32_t segment
,
1397 int64_t offset
, int is_global
, char *special
)
1399 struct ieeeSection
*seg
;
1401 /* Keep compiler from warning about special */
1405 * Note: ..[^@] special symbols are filtered in labels.c
1409 * If it's a special-retry from pass two, discard it.
1417 if (ieee_seg_needs_update
)
1419 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
1422 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
1427 * If `any_segs' is still false, we might need to define a
1428 * default segment, if they're trying to declare a label in
1429 * `first_seg'. But the label should exist due to a prior
1430 * call to ieee_deflabel so we can skip that.
1433 for (seg
= seghead
; seg
; seg
= seg
->next
)
1434 if (seg
->index
== segment
) {
1435 struct ieeePublic
*loc
;
1437 * Case (ii). Maybe MODPUB someday?
1440 last_defined
= loc
= nasm_malloc(sizeof(*loc
));
1441 *seg
->loctail
= loc
;
1442 seg
->loctail
= &loc
->next
;
1444 loc
->name
= nasm_strdup(name
);
1445 loc
->offset
= offset
;
1447 loc
->index
= seg
->ieee_index
;
1451 static void dbgls_typevalue(int32_t type
)
1453 int elem
= TYM_ELEMENTS(type
);
1454 type
= TYM_TYPE(type
);
1461 last_defined
->type
= 1; /* uint8_t */
1464 last_defined
->type
= 3; /* unsigned word */
1467 last_defined
->type
= 5; /* unsigned dword */
1470 last_defined
->type
= 9; /* float */
1473 last_defined
->type
= 10; /* qword */
1476 last_defined
->type
= 11; /* TBYTE */
1479 last_defined
->type
= 0x10; /* near label */
1484 struct Array
*arrtmp
= nasm_malloc(sizeof(*arrtmp
));
1485 int vtype
= last_defined
->type
;
1486 arrtmp
->size
= elem
;
1487 arrtmp
->basetype
= vtype
;
1488 arrtmp
->next
= NULL
;
1489 last_defined
->type
= arrindex
++ + 0x100;
1491 arrtail
= &(arrtmp
->next
);
1493 last_defined
= NULL
;
1495 static void dbgls_output(int output_type
, void *param
)
1500 static const struct dfmt ladsoft_debug_form
= {
1501 "LADsoft Debug Records",
1506 null_debug_directive
,
1510 NULL
/* pragma list */
1512 static const struct dfmt
* const ladsoft_debug_arr
[3] = {
1513 &ladsoft_debug_form
,
1517 const struct ofmt of_ieee
= {
1518 "IEEE-695 (LADsoft variant) object file format",
1523 &ladsoft_debug_form
,
1527 nasm_do_legacy_output
,
1536 NULL
/* pragma list */
1539 #endif /* OF_IEEE */