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 */
88 static char ieee_infile
[FILENAME_MAX
];
89 static int ieee_uppercase
;
94 #define HUNKSIZE 1024 /* Size of the data hunk */
95 #define EXT_BLKSIZ 512
96 #define LDPERLINE 32 /* bytes per line in output */
101 struct LineNumber
*next
;
102 struct ieeeSection
*segment
;
107 static struct FileName
{
108 struct FileName
*next
;
113 static struct Array
{
117 } *arrhead
, **arrtail
;
119 static struct ieeePublic
{
120 struct ieeePublic
*next
;
123 int32_t segment
; /* only if it's far-absolute */
125 int type
; /* for debug purposes */
126 } *fpubhead
, **fpubtail
, *last_defined
;
128 static struct ieeeExternal
{
129 struct ieeeExternal
*next
;
132 } *exthead
, **exttail
;
134 static int externals
;
136 static struct ExtBack
{
137 struct ExtBack
*next
;
138 int index
[EXT_BLKSIZ
];
141 /* NOTE: the first segment MUST be the lineno segment */
142 static struct ieeeSection
{
143 struct ieeeSection
*next
;
145 struct ieeeObjData
*data
, *datacurr
;
146 struct ieeeFixupp
*fptr
, *flptr
;
147 int32_t index
; /* the NASM segment id */
148 int32_t ieee_index
; /* the OBJ-file segment index */
150 int32_t align
; /* can be SEG_ABS + absolute addr */
152 int32_t use32
; /* is this segment 32-bit? */
153 struct ieeePublic
*pubhead
, **pubtail
, *lochead
, **loctail
;
159 } *seghead
, **segtail
, *ieee_seg_needs_update
;
162 struct ieeeObjData
*next
;
163 uint8_t data
[HUNKSIZE
];
167 struct ieeeFixupp
*next
;
185 static int32_t ieee_entry_seg
, ieee_entry_ofs
;
188 extern const struct ofmt of_ieee
;
189 static const struct dfmt ladsoft_debug_form
;
191 static void ieee_data_new(struct ieeeSection
*);
192 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection
*,
193 int, uint64_t, int32_t);
194 static void ieee_install_fixup(struct ieeeSection
*, struct ieeeFixupp
*);
195 static int32_t ieee_segment(char *, int, int *);
196 static void ieee_write_file(void);
197 static void ieee_write_byte(struct ieeeSection
*, int);
198 static void ieee_write_word(struct ieeeSection
*, int);
199 static void ieee_write_dword(struct ieeeSection
*, int32_t);
200 static void ieee_putascii(char *, ...);
201 static void ieee_putcs(int);
202 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
203 static int32_t ieee_putlr(struct ieeeFixupp
*);
204 static void ieee_unqualified_name(char *, char *);
209 static void ieee_init(void)
213 fpubtail
= &fpubhead
;
219 seghead
= ieee_seg_needs_update
= NULL
;
221 ieee_entry_seg
= NO_SEG
;
222 ieee_uppercase
= false;
226 static int ieee_set_info(enum geninfo type
, char **val
)
237 static void ieee_cleanup(void)
242 struct ieeeSection
*segtmp
= seghead
;
243 seghead
= seghead
->next
;
244 while (segtmp
->pubhead
) {
245 struct ieeePublic
*pubtmp
= segtmp
->pubhead
;
246 segtmp
->pubhead
= pubtmp
->next
;
249 while (segtmp
->fptr
) {
250 struct ieeeFixupp
*fixtmp
= segtmp
->fptr
;
251 segtmp
->fptr
= fixtmp
->next
;
254 while (segtmp
->data
) {
255 struct ieeeObjData
*dattmp
= segtmp
->data
;
256 segtmp
->data
= dattmp
->next
;
262 struct ieeePublic
*pubtmp
= fpubhead
;
263 fpubhead
= fpubhead
->next
;
267 struct ieeeExternal
*exttmp
= exthead
;
268 exthead
= exthead
->next
;
272 struct ExtBack
*ebtmp
= ebhead
;
273 ebhead
= ebhead
->next
;
279 * callback for labels
281 static void ieee_deflabel(char *name
, int32_t segment
,
282 int64_t offset
, int is_global
, char *special
)
285 * We have three cases:
287 * (i) `segment' is a segment-base. If so, set the name field
288 * for the segment structure it refers to, and then
291 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
292 * Save the label position for later output of a PUBDEF record.
295 * (iii) `segment' is not one of our segments. Save the label
296 * position for later output of an EXTDEF.
298 struct ieeeExternal
*ext
;
300 struct ieeeSection
*seg
;
304 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%s'", special
);
307 * First check for the double-period, signifying something
310 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
311 if (!strcmp(name
, "..start")) {
312 ieee_entry_seg
= segment
;
313 ieee_entry_ofs
= offset
;
321 if (ieee_seg_needs_update
) {
322 ieee_seg_needs_update
->name
= name
;
325 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
331 if (segment
>= SEG_ABS
) {
333 * SEG_ABS subcase of (ii).
336 struct ieeePublic
*pub
;
338 pub
= *fpubtail
= nasm_malloc(sizeof(*pub
));
339 fpubtail
= &pub
->next
;
342 pub
->offset
= offset
;
343 pub
->segment
= segment
& ~SEG_ABS
;
348 for (seg
= seghead
; seg
&& is_global
; seg
= seg
->next
)
349 if (seg
->index
== segment
) {
350 struct ieeePublic
*pub
;
352 last_defined
= pub
= *seg
->pubtail
= nasm_malloc(sizeof(*pub
));
353 seg
->pubtail
= &pub
->next
;
356 pub
->offset
= offset
;
357 pub
->index
= seg
->ieee_index
;
366 ext
= *exttail
= nasm_malloc(sizeof(*ext
));
368 exttail
= &ext
->next
;
371 ext
->commonsize
= offset
;
377 eb
= *ebtail
= nasm_zalloc(sizeof(*eb
));
381 while (i
> EXT_BLKSIZ
) {
385 eb
= *ebtail
= nasm_zalloc(sizeof(*eb
));
391 eb
->index
[i
] = externals
++;
399 static void ieee_out(int32_t segto
, const void *data
,
400 enum out_type type
, uint64_t size
,
401 int32_t segment
, int32_t wrt
)
403 const uint8_t *ucdata
;
405 struct ieeeSection
*seg
;
408 * handle absolute-assembly (structure definitions)
410 if (segto
== NO_SEG
) {
411 if (type
!= OUT_RESERVE
)
412 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
418 * If `any_segs' is still false, we must define a default
422 int tempint
; /* ignored */
423 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
424 nasm_panic(0, "strange segment conditions in IEEE driver");
428 * Find the segment we are targetting.
430 for (seg
= seghead
; seg
; seg
= seg
->next
)
431 if (seg
->index
== segto
)
434 nasm_panic(0, "code directed to nonexistent segment?");
436 if (type
== OUT_RAWDATA
) {
439 ieee_write_byte(seg
, *ucdata
++);
440 } else if (type
== OUT_ADDRESS
|| type
== OUT_REL2ADR
||
441 type
== OUT_REL4ADR
) {
442 if (type
== OUT_ADDRESS
)
443 size
= abs((int)size
);
444 else if (segment
== NO_SEG
)
445 nasm_error(ERR_NONFATAL
, "relative call to absolute address not"
446 " supported by IEEE format");
447 ldata
= *(int64_t *)data
;
448 if (type
== OUT_REL2ADR
)
450 if (type
== OUT_REL4ADR
)
452 ieee_write_fixup(segment
, wrt
, seg
, size
, type
, ldata
);
454 ieee_write_word(seg
, ldata
);
456 ieee_write_dword(seg
, ldata
);
457 } else if (type
== OUT_RESERVE
) {
459 ieee_write_byte(seg
, 0);
463 static void ieee_data_new(struct ieeeSection
*segto
)
467 segto
->data
= segto
->datacurr
=
468 nasm_malloc(sizeof(*(segto
->datacurr
)));
470 segto
->datacurr
= segto
->datacurr
->next
=
471 nasm_malloc(sizeof(*(segto
->datacurr
)));
472 segto
->datacurr
->next
= NULL
;
476 * this routine is unalduterated bloatware. I usually don't do this
477 * but I might as well see what it is like on a harmless program.
478 * If anyone wants to optimize this is a good canditate!
480 static void ieee_write_fixup(int32_t segment
, int32_t wrt
,
481 struct ieeeSection
*segto
, int size
,
482 uint64_t realtype
, int32_t offset
)
484 struct ieeeSection
*target
;
487 /* Don't put a fixup for things NASM can calculate */
488 if (wrt
== NO_SEG
&& segment
== NO_SEG
)
492 /* if it is a WRT offset */
497 s
.id1
= -(wrt
- SEG_ABS
);
499 if (wrt
% 2 && realtype
!= OUT_REL2ADR
500 && realtype
!= OUT_REL4ADR
) {
503 for (target
= seghead
; target
; target
= target
->next
)
504 if (target
->index
== wrt
)
507 s
.id1
= target
->ieee_index
;
508 for (target
= seghead
; target
; target
= target
->next
)
509 if (target
->index
== segment
)
513 s
.id2
= target
->ieee_index
;
516 * Now we assume the segment field is being used
517 * to hold an extern index
519 int32_t i
= segment
/ 2;
520 struct ExtBack
*eb
= ebhead
;
521 while (i
> EXT_BLKSIZ
) {
528 /* if we have an extern decide the type and make a record
533 s
.id2
= eb
->index
[i
];
535 nasm_error(ERR_NONFATAL
,
536 "Source of WRT must be an offset");
541 "unrecognised WRT value in ieee_write_fixup");
543 nasm_error(ERR_NONFATAL
, "target of WRT must be a section ");
546 ieee_install_fixup(segto
, &s
);
549 /* Pure segment fixup ? */
550 if (segment
!= NO_SEG
) {
553 if (segment
>= SEG_ABS
) {
554 /* absolute far segment fixup */
555 s
.id1
= -(segment
- ~SEG_ABS
);
556 } else if (segment
% 2) {
557 /* fixup to named segment */
559 for (target
= seghead
; target
; target
= target
->next
)
560 if (target
->index
== segment
- 1)
563 s
.id1
= target
->ieee_index
;
566 * Now we assume the segment field is being used
567 * to hold an extern index
569 int32_t i
= segment
/ 2;
570 struct ExtBack
*eb
= ebhead
;
571 while (i
> EXT_BLKSIZ
) {
578 /* if we have an extern decide the type and make a record
581 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
583 "Segment of a rel not supported in ieee_write_fixup");
585 /* If we want the segment */
588 s
.id1
= eb
->index
[i
];
592 /* If we get here the seg value doesn't make sense */
594 "unrecognised segment value in ieee_write_fixup");
598 /* Assume we are offsetting directly from a section
599 * So look up the target segment
601 for (target
= seghead
; target
; target
= target
->next
)
602 if (target
->index
== segment
)
605 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
606 /* PC rel to a known offset */
607 s
.id1
= target
->ieee_index
;
612 /* We were offsetting from a seg */
613 s
.id1
= target
->ieee_index
;
620 * Now we assume the segment field is being used
621 * to hold an extern index
623 int32_t i
= segment
/ 2;
624 struct ExtBack
*eb
= ebhead
;
625 while (i
> EXT_BLKSIZ
) {
632 /* if we have an extern decide the type and make a record
635 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
638 s
.id1
= eb
->index
[i
];
640 /* else we want the external offset */
643 s
.id1
= eb
->index
[i
];
647 /* If we get here the seg value doesn't make sense */
649 "unrecognised segment value in ieee_write_fixup");
652 if (size
!= 2 && s
.ftype
== FT_SEG
)
653 nasm_error(ERR_NONFATAL
, "IEEE format can only handle 2-byte"
654 " segment base references");
656 ieee_install_fixup(segto
, &s
);
659 /* should never get here */
661 static void ieee_install_fixup(struct ieeeSection
*seg
,
662 struct ieeeFixupp
*fix
)
664 struct ieeeFixupp
*f
;
665 f
= nasm_malloc(sizeof(struct ieeeFixupp
));
666 memcpy(f
, fix
, sizeof(struct ieeeFixupp
));
667 f
->offset
= seg
->currentpos
;
668 seg
->currentpos
+= fix
->size
;
671 seg
->flptr
= seg
->flptr
->next
= f
;
673 seg
->fptr
= seg
->flptr
= f
;
680 static int32_t ieee_segment(char *name
, int pass
, int *bits
)
683 * We call the label manager here to define a name for the new
684 * segment, and when our _own_ label-definition stub gets
685 * called in return, it should register the new segment name
686 * using the pointer it gets passed. That way we save memory,
687 * by sponging off the label manager.
693 return seghead
->index
;
695 struct ieeeSection
*seg
;
701 * Look for segment attributes.
705 name
++; /* hack, but a documented one */
707 while (*p
&& !nasm_isspace(*p
))
711 while (*p
&& nasm_isspace(*p
))
715 while (*p
&& !nasm_isspace(*p
))
719 while (*p
&& nasm_isspace(*p
))
727 for (seg
= seghead
; seg
; seg
= seg
->next
) {
729 if (!strcmp(seg
->name
, name
)) {
730 if (attrs
> 0 && pass
== 1)
731 nasm_error(ERR_WARNING
, "segment attributes specified on"
732 " redeclaration of segment: ignoring");
741 *segtail
= seg
= nasm_malloc(sizeof(*seg
));
743 segtail
= &seg
->next
;
744 seg
->index
= seg_alloc();
745 seg
->ieee_index
= ieee_idx
;
749 seg
->align
= 1; /* default */
750 seg
->use32
= *bits
== 32; /* default to user spec */
751 seg
->combine
= CMB_PUBLIC
; /* default */
753 seg
->pubtail
= &seg
->pubhead
;
757 seg
->loctail
= &seg
->lochead
;
760 * Process the segment attributes.
769 * `p' contains a segment attribute.
771 if (!nasm_stricmp(p
, "private"))
772 seg
->combine
= CMB_PRIVATE
;
773 else if (!nasm_stricmp(p
, "public"))
774 seg
->combine
= CMB_PUBLIC
;
775 else if (!nasm_stricmp(p
, "common"))
776 seg
->combine
= CMB_COMMON
;
777 else if (!nasm_stricmp(p
, "use16"))
779 else if (!nasm_stricmp(p
, "use32"))
781 else if (!nasm_strnicmp(p
, "align=", 6)) {
782 seg
->align
= readnum(p
+ 6, &rn_error
);
787 nasm_error(ERR_NONFATAL
, "segment alignment should be"
790 switch ((int)seg
->align
) {
802 nasm_error(ERR_NONFATAL
, "invalid alignment value %d",
807 } else if (!nasm_strnicmp(p
, "absolute=", 9)) {
808 seg
->align
= SEG_ABS
+ readnum(p
+ 9, &rn_error
);
810 nasm_error(ERR_NONFATAL
, "argument to `absolute' segment"
811 " attribute should be numeric");
815 ieee_seg_needs_update
= seg
;
816 if (seg
->align
>= SEG_ABS
)
817 define_label(name
, NO_SEG
, seg
->align
- SEG_ABS
,
820 define_label(name
, seg
->index
+ 1, 0L, NULL
, false, false);
821 ieee_seg_needs_update
= NULL
;
832 * directives supported
834 static int ieee_directive(enum directives directive
, char *value
, int pass
)
842 ieee_uppercase
= true;
850 static void ieee_sectalign(int32_t seg
, unsigned int value
)
852 struct ieeeSection
*s
;
854 list_for_each(s
, seghead
) {
860 * 256 is maximum there, note it may happen
861 * that align is issued on "absolute" segment
862 * it's fine since SEG_ABS > 256 and we never
863 * get escape this test
865 if (!s
|| !is_power2(value
) || value
> 256)
868 if ((unsigned int)s
->align
< value
)
873 * Return segment data
875 static int32_t ieee_segbase(int32_t segment
)
877 struct ieeeSection
*seg
;
880 * Find the segment in our list.
882 for (seg
= seghead
; seg
; seg
= seg
->next
)
883 if (seg
->index
== segment
- 1)
887 return segment
; /* not one of ours - leave it alone */
889 if (seg
->align
>= SEG_ABS
)
890 return seg
->align
; /* absolute segment */
892 return segment
; /* no special treatment */
898 static void ieee_filename(char *inname
, char *outname
)
900 strcpy(ieee_infile
, inname
);
901 standard_extension(inname
, outname
, ".o");
904 static void ieee_write_file(void)
909 struct ieeeSection
*seg
;
910 struct ieeePublic
*pub
, *loc
;
911 struct ieeeExternal
*ext
;
912 struct ieeeObjData
*data
;
913 struct ieeeFixupp
*fix
;
916 const bool debuginfo
= (dfmt
== &ladsoft_debug_form
);
919 * Write the module header
921 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile
), ieee_infile
);
924 * Write the NASM boast comment.
926 ieee_putascii("CO0,%02X%s.\n", strlen(nasm_comment
), nasm_comment
);
929 * write processor-specific information
931 ieee_putascii("AD8,4,L.\n");
937 thetime
= localtime(&reltime
);
938 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
939 1900 + thetime
->tm_year
, thetime
->tm_mon
+ 1,
940 thetime
->tm_mday
, thetime
->tm_hour
, thetime
->tm_min
,
943 * if debugging, dump file names
945 for (fn
= fnhead
; fn
&& debuginfo
; fn
= fn
->next
) {
946 ieee_putascii("C0105,%02X%s.\n", strlen(fn
->name
), fn
->name
);
949 ieee_putascii("CO101,07ENDHEAD.\n");
951 * the standard doesn't specify when to put checksums,
952 * we'll just do it periodically.
957 * Write the section headers
960 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
965 switch (seg
->combine
) {
977 ieee_unqualified_name(buf
, seg
->name
);
978 if (seg
->align
>= SEG_ABS
) {
979 ieee_putascii("ST%X,A,%02X%s.\n", seg
->ieee_index
,
981 ieee_putascii("ASL%X,%lX.\n", seg
->ieee_index
,
982 (seg
->align
- SEG_ABS
) * 16);
984 ieee_putascii("ST%X,%c,%02X%s.\n", seg
->ieee_index
, attrib
,
986 ieee_putascii("SA%X,%lX.\n", seg
->ieee_index
, seg
->align
);
987 ieee_putascii("ASS%X,%X.\n", seg
->ieee_index
,
993 * write the start address if there is one
995 if (ieee_entry_seg
) {
996 for (seg
= seghead
; seg
; seg
= seg
->next
)
997 if (seg
->index
== ieee_entry_seg
)
1000 nasm_panic(0, "Start address records are incorrect");
1002 ieee_putascii("ASG,R%X,%lX,+.\n", seg
->ieee_index
,
1011 for (seg
= seghead
; seg
; seg
= seg
->next
) {
1012 for (pub
= seg
->pubhead
; pub
; pub
= pub
->next
) {
1014 ieee_unqualified_name(buf
, pub
->name
);
1015 ieee_putascii("NI%X,%02X%s.\n", i
, strlen(buf
), buf
);
1016 if (pub
->segment
== -1)
1017 ieee_putascii("ASI%X,R%X,%lX,+.\n", i
, pub
->index
,
1020 ieee_putascii("ASI%X,%lX,%lX,+.\n", i
, pub
->segment
* 16,
1023 if (pub
->type
>= 0x100)
1024 ieee_putascii("ATI%X,T%X.\n", i
, pub
->type
- 0x100);
1026 ieee_putascii("ATI%X,%X.\n", i
, pub
->type
);
1035 ieee_unqualified_name(buf
, pub
->name
);
1036 ieee_putascii("NI%X,%02X%s.\n", i
, strlen(buf
), buf
);
1037 if (pub
->segment
== -1)
1038 ieee_putascii("ASI%X,R%X,%lX,+.\n", i
, pub
->index
,
1041 ieee_putascii("ASI%X,%lX,%lX,+.\n", i
, pub
->segment
* 16,
1044 if (pub
->type
>= 0x100)
1045 ieee_putascii("ATI%X,T%X.\n", i
, pub
->type
- 0x100);
1047 ieee_putascii("ATI%X,%X.\n", i
, pub
->type
);
1053 * Write the externals
1059 ieee_unqualified_name(buf
, ext
->name
);
1060 ieee_putascii("NX%X,%02X%s.\n", i
++, strlen(buf
), buf
);
1066 * IEEE doesn't have a standard pass break record
1067 * so use the ladsoft variant
1069 ieee_putascii("CO100,06ENDSYM.\n");
1075 for (arr
= arrhead
; arr
&& debuginfo
; arr
= arr
->next
) {
1076 ieee_putascii("TY%X,20,%X,%lX.\n", i
++, arr
->basetype
,
1083 for (seg
= seghead
; seg
&& debuginfo
; seg
= seg
->next
) {
1084 for (loc
= seg
->lochead
; loc
; loc
= loc
->next
) {
1086 ieee_unqualified_name(buf
, loc
->name
);
1087 ieee_putascii("NN%X,%02X%s.\n", i
, strlen(buf
), buf
);
1088 if (loc
->segment
== -1)
1089 ieee_putascii("ASN%X,R%X,%lX,+.\n", i
, loc
->index
,
1092 ieee_putascii("ASN%X,%lX,%lX,+.\n", i
, loc
->segment
* 16,
1095 if (loc
->type
>= 0x100)
1096 ieee_putascii("ATN%X,T%X.\n", i
, loc
->type
- 0x100);
1098 ieee_putascii("ATN%X,%X.\n", i
, loc
->type
);
1105 * put out section data;
1108 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
1111 if (seg
->currentpos
) {
1112 int32_t size
, org
= 0;
1114 ieee_putascii("SB%X.\n", seg
->ieee_index
);
1117 size
= HUNKSIZE
- (org
% HUNKSIZE
);
1120 seg
->currentpos
? seg
->currentpos
- org
: size
;
1121 size
= fix
->offset
- org
> size
? size
: fix
->offset
- org
;
1122 org
= ieee_putld(org
, org
+ size
, data
->data
);
1123 if (org
% HUNKSIZE
== 0)
1125 if (org
== fix
->offset
) {
1126 org
+= ieee_putlr(fix
);
1130 while (org
< seg
->currentpos
&& data
) {
1132 seg
->currentpos
- org
>
1133 HUNKSIZE
? HUNKSIZE
: seg
->currentpos
- org
;
1134 org
= ieee_putld(org
, org
+ size
, data
->data
);
1145 ieee_putascii("ME.\n");
1148 static void ieee_write_byte(struct ieeeSection
*seg
, int data
)
1151 if (!(temp
= seg
->currentpos
++ % HUNKSIZE
))
1153 seg
->datacurr
->data
[temp
] = data
;
1156 static void ieee_write_word(struct ieeeSection
*seg
, int data
)
1158 ieee_write_byte(seg
, data
& 0xFF);
1159 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1162 static void ieee_write_dword(struct ieeeSection
*seg
, int32_t data
)
1164 ieee_write_byte(seg
, data
& 0xFF);
1165 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1166 ieee_write_byte(seg
, (data
>> 16) & 0xFF);
1167 ieee_write_byte(seg
, (data
>> 24) & 0xFF);
1169 static void ieee_putascii(char *format
, ...)
1175 va_start(ap
, format
);
1176 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1178 for (i
= 0; i
< l
; i
++)
1179 if ((uint8_t)buffer
[i
] > 31)
1180 checksum
+= buffer
[i
];
1182 fputs(buffer
, ofile
);
1186 * put out a checksum record */
1187 static void ieee_putcs(int toclear
)
1190 ieee_putascii("CS.\n");
1194 ieee_putascii("CS%02X.\n", checksum
& 127);
1199 static int32_t ieee_putld(int32_t start
, int32_t end
, uint8_t *buf
)
1204 val
= start
% HUNKSIZE
;
1205 /* fill up multiple lines */
1206 while (end
- start
>= LDPERLINE
) {
1208 ieee_putascii("LD");
1209 for (i
= 0; i
< LDPERLINE
; i
++) {
1210 ieee_putascii("%02X", buf
[val
++]);
1213 ieee_putascii(".\n");
1215 /* if no partial lines */
1218 /* make a partial line */
1219 ieee_putascii("LD");
1220 while (start
< end
) {
1221 ieee_putascii("%02X", buf
[val
++]);
1224 ieee_putascii(".\n");
1227 static int32_t ieee_putlr(struct ieeeFixupp
*p
)
1230 * To deal with the vagaries of segmentation the LADsoft linker
1231 * defines two types of segments: absolute and virtual. Note that
1232 * 'absolute' in this context is a different thing from the IEEE
1233 * definition of an absolute segment type, which is also supported. If a
1234 * sement is linked in virtual mode the low limit (L-var) is
1235 * subtracted from each R,X, and P variable which appears in an
1236 * expression, so that we can have relative offsets. Meanwhile
1237 * in the ABSOLUTE mode this subtraction is not done and
1238 * so we can use absolute offsets from 0. In the LADsoft linker
1239 * this configuration is not done in the assemblker source but in
1240 * a source the linker reads. Generally this type of thing only
1241 * becomes an issue if real mode code is used. A pure 32-bit linker could
1242 * get away without defining the virtual mode...
1245 int32_t size
= p
->size
;
1249 sprintf(buf
, "%"PRIX32
"", -p
->id1
);
1251 sprintf(buf
, "L%"PRIX32
",10,/", p
->id1
);
1254 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+", p
->id1
, p
->addend
);
1257 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,P,-,%X,-", p
->id1
, p
->addend
, p
->size
);
1262 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->addend
,
1263 p
->id2
, -p
->id1
* 16);
1265 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->addend
,
1269 sprintf(buf
, "X%"PRIX32
"", p
->id1
);
1272 sprintf(buf
, "X%"PRIX32
",P,-,%"PRIX32
",-", p
->id1
, size
);
1275 /* We needed a non-ieee hack here.
1276 * We introduce the Y variable, which is the low
1277 * limit of the native segment the extern resides in
1279 sprintf(buf
, "Y%"PRIX32
",10,/", p
->id1
);
1283 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->id2
,
1286 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->id2
, p
->id1
);
1289 ieee_putascii("LR(%s,%"PRIX32
").\n", buf
, size
);
1294 /* Dump all segment data (text and fixups )*/
1296 static void ieee_unqualified_name(char *dest
, char *source
)
1298 if (ieee_uppercase
) {
1300 *dest
++ = toupper(*source
++);
1303 strcpy(dest
, source
);
1305 static void dbgls_init(void)
1311 arrindex
= ARRAY_BOT
;
1314 ieee_segment("??LINE", 2, &tempint
);
1317 static void dbgls_cleanup(void)
1319 struct ieeeSection
*segtmp
;
1321 struct FileName
*fntemp
= fnhead
;
1322 fnhead
= fnhead
->next
;
1323 nasm_free(fntemp
->name
);
1326 for (segtmp
= seghead
; segtmp
; segtmp
= segtmp
->next
) {
1327 while (segtmp
->lochead
) {
1328 struct ieeePublic
*loctmp
= segtmp
->lochead
;
1329 segtmp
->lochead
= loctmp
->next
;
1330 nasm_free(loctmp
->name
);
1335 struct Array
*arrtmp
= arrhead
;
1336 arrhead
= arrhead
->next
;
1342 * because this routine is not bracketed in
1343 * the main program, this routine will be called even if there
1344 * is no request for debug info
1345 * so, we have to make sure the ??LINE segment is avaialbe
1346 * as the first segment when this debug format is selected
1348 static void dbgls_linnum(const char *lnfname
, int32_t lineno
, int32_t segto
)
1350 struct FileName
*fn
;
1351 struct ieeeSection
*seg
;
1353 if (segto
== NO_SEG
)
1357 * If `any_segs' is still false, we must define a default
1361 int tempint
; /* ignored */
1362 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
1363 nasm_panic(0, "strange segment conditions in OBJ driver");
1367 * Find the segment we are targetting.
1369 for (seg
= seghead
; seg
; seg
= seg
->next
)
1370 if (seg
->index
== segto
)
1373 nasm_panic(0, "lineno directed to nonexistent segment?");
1375 for (fn
= fnhead
; fn
; fn
= fn
->next
) {
1376 if (!nasm_stricmp(lnfname
, fn
->name
))
1381 fn
= nasm_malloc(sizeof(*fn
));
1382 fn
->name
= nasm_malloc(strlen(lnfname
) + 1);
1384 strcpy(fn
->name
, lnfname
);
1389 ieee_write_byte(seghead
, fn
->index
);
1390 ieee_write_word(seghead
, lineno
);
1391 ieee_write_fixup(segto
, NO_SEG
, seghead
, 4, OUT_ADDRESS
,
1395 static void dbgls_deflabel(char *name
, int32_t segment
,
1396 int64_t offset
, int is_global
, char *special
)
1398 struct ieeeSection
*seg
;
1400 /* Keep compiler from warning about special */
1404 * Note: ..[^@] special symbols are filtered in labels.c
1408 * If it's a special-retry from pass two, discard it.
1416 if (ieee_seg_needs_update
)
1418 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
1421 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
1426 * If `any_segs' is still false, we might need to define a
1427 * default segment, if they're trying to declare a label in
1428 * `first_seg'. But the label should exist due to a prior
1429 * call to ieee_deflabel so we can skip that.
1432 for (seg
= seghead
; seg
; seg
= seg
->next
)
1433 if (seg
->index
== segment
) {
1434 struct ieeePublic
*loc
;
1436 * Case (ii). Maybe MODPUB someday?
1439 last_defined
= loc
= nasm_malloc(sizeof(*loc
));
1440 *seg
->loctail
= loc
;
1441 seg
->loctail
= &loc
->next
;
1443 loc
->name
= nasm_strdup(name
);
1444 loc
->offset
= offset
;
1446 loc
->index
= seg
->ieee_index
;
1450 static void dbgls_typevalue(int32_t type
)
1452 int elem
= TYM_ELEMENTS(type
);
1453 type
= TYM_TYPE(type
);
1460 last_defined
->type
= 1; /* uint8_t */
1463 last_defined
->type
= 3; /* unsigned word */
1466 last_defined
->type
= 5; /* unsigned dword */
1469 last_defined
->type
= 9; /* float */
1472 last_defined
->type
= 10; /* qword */
1475 last_defined
->type
= 11; /* TBYTE */
1478 last_defined
->type
= 0x10; /* near label */
1483 struct Array
*arrtmp
= nasm_malloc(sizeof(*arrtmp
));
1484 int vtype
= last_defined
->type
;
1485 arrtmp
->size
= elem
;
1486 arrtmp
->basetype
= vtype
;
1487 arrtmp
->next
= NULL
;
1488 last_defined
->type
= arrindex
++ + 0x100;
1490 arrtail
= &(arrtmp
->next
);
1492 last_defined
= NULL
;
1494 static void dbgls_output(int output_type
, void *param
)
1499 static const struct dfmt ladsoft_debug_form
= {
1500 "LADsoft Debug Records",
1505 null_debug_directive
,
1510 static const struct dfmt
* const ladsoft_debug_arr
[3] = {
1511 &ladsoft_debug_form
,
1515 const struct ofmt of_ieee
= {
1516 "IEEE-695 (LADsoft variant) object file format",
1521 &ladsoft_debug_form
,
1525 nasm_do_legacy_output
,
1536 #endif /* OF_IEEE */