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
71 #include <ctype.h> /* For toupper() */
86 static char ieee_infile
[FILENAME_MAX
];
87 static int ieee_uppercase
;
92 #define HUNKSIZE 1024 /* Size of the data hunk */
93 #define EXT_BLKSIZ 512
94 #define LDPERLINE 32 /* bytes per line in output */
99 struct LineNumber
*next
;
100 struct ieeeSection
*segment
;
105 static struct FileName
{
106 struct FileName
*next
;
111 static struct Array
{
115 } *arrhead
, **arrtail
;
117 static struct ieeePublic
{
118 struct ieeePublic
*next
;
121 int32_t segment
; /* only if it's far-absolute */
123 int type
; /* for debug purposes */
124 } *fpubhead
, **fpubtail
, *last_defined
;
126 static struct ieeeExternal
{
127 struct ieeeExternal
*next
;
130 } *exthead
, **exttail
;
132 static int externals
;
134 static struct ExtBack
{
135 struct ExtBack
*next
;
136 int index
[EXT_BLKSIZ
];
139 /* NOTE: the first segment MUST be the lineno segment */
140 static struct ieeeSection
{
141 struct ieeeSection
*next
;
143 struct ieeeObjData
*data
, *datacurr
;
144 struct ieeeFixupp
*fptr
, *flptr
;
145 int32_t index
; /* the NASM segment id */
146 int32_t ieee_index
; /* the IEEE-file segment index */
148 int32_t align
; /* can be SEG_ABS + absolute addr */
150 int32_t use32
; /* is this segment 32-bit? */
151 int64_t pass_last_seen
;
152 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 const struct ofmt of_ieee
;
188 static const struct dfmt ladsoft_debug_form
;
190 static void ieee_data_new(struct ieeeSection
*);
191 static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection
*,
192 int, uint64_t, int32_t);
193 static void ieee_install_fixup(struct ieeeSection
*, struct ieeeFixupp
*);
194 static int32_t ieee_segment(char *, int *);
195 static void ieee_write_file(void);
196 static void ieee_write_byte(struct ieeeSection
*, int);
197 static void ieee_write_word(struct ieeeSection
*, int);
198 static void ieee_write_dword(struct ieeeSection
*, int32_t);
199 static void ieee_putascii(char *, ...);
200 static void ieee_putcs(int);
201 static int32_t ieee_putld(int32_t, int32_t, uint8_t *);
202 static int32_t ieee_putlr(struct ieeeFixupp
*);
203 static void ieee_unqualified_name(char *, char *);
208 static void ieee_init(void)
210 strlcpy(ieee_infile
, inname
, sizeof(ieee_infile
));
213 fpubtail
= &fpubhead
;
219 seghead
= ieee_seg_needs_update
= NULL
;
221 ieee_entry_seg
= NO_SEG
;
222 ieee_uppercase
= false;
229 static void ieee_cleanup(void)
234 struct ieeeSection
*segtmp
= seghead
;
235 seghead
= seghead
->next
;
236 while (segtmp
->pubhead
) {
237 struct ieeePublic
*pubtmp
= segtmp
->pubhead
;
238 segtmp
->pubhead
= pubtmp
->next
;
241 while (segtmp
->fptr
) {
242 struct ieeeFixupp
*fixtmp
= segtmp
->fptr
;
243 segtmp
->fptr
= fixtmp
->next
;
246 while (segtmp
->data
) {
247 struct ieeeObjData
*dattmp
= segtmp
->data
;
248 segtmp
->data
= dattmp
->next
;
254 struct ieeePublic
*pubtmp
= fpubhead
;
255 fpubhead
= fpubhead
->next
;
259 struct ieeeExternal
*exttmp
= exthead
;
260 exthead
= exthead
->next
;
264 struct ExtBack
*ebtmp
= ebhead
;
265 ebhead
= ebhead
->next
;
271 * callback for labels
273 static void ieee_deflabel(char *name
, int32_t segment
,
274 int64_t offset
, int is_global
, char *special
)
277 * We have three cases:
279 * (i) `segment' is a segment-base. If so, set the name field
280 * for the segment structure it refers to, and then
283 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
284 * Save the label position for later output of a PUBDEF record.
287 * (iii) `segment' is not one of our segments. Save the label
288 * position for later output of an EXTDEF.
290 struct ieeeExternal
*ext
;
292 struct ieeeSection
*seg
;
296 nasm_nonfatal("unrecognised symbol type `%s'", special
);
298 * First check for the double-period, signifying something
301 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
302 if (!strcmp(name
, "..start")) {
303 ieee_entry_seg
= segment
;
304 ieee_entry_ofs
= offset
;
312 if (ieee_seg_needs_update
) {
313 ieee_seg_needs_update
->name
= name
;
316 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
322 if (segment
>= SEG_ABS
) {
324 * SEG_ABS subcase of (ii).
327 struct ieeePublic
*pub
;
329 pub
= *fpubtail
= nasm_malloc(sizeof(*pub
));
330 fpubtail
= &pub
->next
;
333 pub
->offset
= offset
;
334 pub
->segment
= segment
& ~SEG_ABS
;
339 for (seg
= seghead
; seg
&& is_global
; seg
= seg
->next
)
340 if (seg
->index
== segment
) {
341 struct ieeePublic
*pub
;
343 last_defined
= pub
= *seg
->pubtail
= nasm_malloc(sizeof(*pub
));
344 seg
->pubtail
= &pub
->next
;
347 pub
->offset
= offset
;
348 pub
->index
= seg
->ieee_index
;
357 ext
= *exttail
= nasm_malloc(sizeof(*ext
));
359 exttail
= &ext
->next
;
362 ext
->commonsize
= offset
;
368 eb
= *ebtail
= nasm_zalloc(sizeof(*eb
));
372 while (i
> EXT_BLKSIZ
) {
376 eb
= *ebtail
= nasm_zalloc(sizeof(*eb
));
382 eb
->index
[i
] = externals
++;
390 static void ieee_out(int32_t segto
, const void *data
,
391 enum out_type type
, uint64_t size
,
392 int32_t segment
, int32_t wrt
)
394 const uint8_t *ucdata
;
396 struct ieeeSection
*seg
;
399 * If `any_segs' is still false, we must define a default
403 int tempint
; /* ignored */
404 if (segto
!= ieee_segment("__NASMDEFSEG", &tempint
))
405 nasm_panic("strange segment conditions in IEEE driver");
409 * Find the segment we are targetting.
411 for (seg
= seghead
; seg
; seg
= seg
->next
)
412 if (seg
->index
== segto
)
415 nasm_panic("code directed to nonexistent segment?");
417 if (type
== OUT_RAWDATA
) {
420 ieee_write_byte(seg
, *ucdata
++);
421 } else if (type
== OUT_ADDRESS
|| type
== OUT_REL2ADR
||
422 type
== OUT_REL4ADR
) {
423 if (type
== OUT_ADDRESS
)
424 size
= abs((int)size
);
425 else if (segment
== NO_SEG
)
426 nasm_nonfatal("relative call to absolute address not"
427 " supported by IEEE format");
428 ldata
= *(int64_t *)data
;
429 if (type
== OUT_REL2ADR
)
431 if (type
== OUT_REL4ADR
)
433 ieee_write_fixup(segment
, wrt
, seg
, size
, type
, ldata
);
435 ieee_write_word(seg
, ldata
);
437 ieee_write_dword(seg
, ldata
);
438 } else if (type
== OUT_RESERVE
) {
440 ieee_write_byte(seg
, 0);
444 static void ieee_data_new(struct ieeeSection
*segto
)
448 segto
->data
= segto
->datacurr
=
449 nasm_malloc(sizeof(*(segto
->datacurr
)));
451 segto
->datacurr
= segto
->datacurr
->next
=
452 nasm_malloc(sizeof(*(segto
->datacurr
)));
453 segto
->datacurr
->next
= NULL
;
457 * this routine is unalduterated bloatware. I usually don't do this
458 * but I might as well see what it is like on a harmless program.
459 * If anyone wants to optimize this is a good canditate!
461 static void ieee_write_fixup(int32_t segment
, int32_t wrt
,
462 struct ieeeSection
*segto
, int size
,
463 uint64_t realtype
, int32_t offset
)
465 struct ieeeSection
*target
;
468 /* Don't put a fixup for things NASM can calculate */
469 if (wrt
== NO_SEG
&& segment
== NO_SEG
)
473 /* if it is a WRT offset */
478 s
.id1
= -(wrt
- SEG_ABS
);
480 if (wrt
% 2 && realtype
!= OUT_REL2ADR
481 && realtype
!= OUT_REL4ADR
) {
484 for (target
= seghead
; target
; target
= target
->next
)
485 if (target
->index
== wrt
)
488 s
.id1
= target
->ieee_index
;
489 for (target
= seghead
; target
; target
= target
->next
)
490 if (target
->index
== segment
)
494 s
.id2
= target
->ieee_index
;
497 * Now we assume the segment field is being used
498 * to hold an extern index
500 int32_t i
= segment
/ 2;
501 struct ExtBack
*eb
= ebhead
;
502 while (i
> EXT_BLKSIZ
) {
509 /* if we have an extern decide the type and make a record
514 s
.id2
= eb
->index
[i
];
516 nasm_nonfatal("source of WRT must be an offset");
520 nasm_panic("unrecognised WRT value in ieee_write_fixup");
522 nasm_nonfatal("target of WRT must be a section");
525 ieee_install_fixup(segto
, &s
);
528 /* Pure segment fixup ? */
529 if (segment
!= NO_SEG
) {
532 if (segment
>= SEG_ABS
) {
533 /* absolute far segment fixup */
534 s
.id1
= -(segment
- ~SEG_ABS
);
535 } else if (segment
% 2) {
536 /* fixup to named segment */
538 for (target
= seghead
; target
; target
= target
->next
)
539 if (target
->index
== segment
- 1)
542 s
.id1
= target
->ieee_index
;
545 * Now we assume the segment field is being used
546 * to hold an extern index
548 int32_t i
= segment
/ 2;
549 struct ExtBack
*eb
= ebhead
;
550 while (i
> EXT_BLKSIZ
) {
557 /* if we have an extern decide the type and make a record
560 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
561 nasm_panic("Segment of a rel not supported in ieee_write_fixup");
563 /* If we want the segment */
566 s
.id1
= eb
->index
[i
];
570 /* If we get here the seg value doesn't make sense */
571 nasm_panic("unrecognised segment value in ieee_write_fixup");
575 /* Assume we are offsetting directly from a section
576 * So look up the target segment
578 for (target
= seghead
; target
; target
= target
->next
)
579 if (target
->index
== segment
)
582 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
583 /* PC rel to a known offset */
584 s
.id1
= target
->ieee_index
;
589 /* We were offsetting from a seg */
590 s
.id1
= target
->ieee_index
;
597 * Now we assume the segment field is being used
598 * to hold an extern index
600 int32_t i
= segment
/ 2;
601 struct ExtBack
*eb
= ebhead
;
602 while (i
> EXT_BLKSIZ
) {
609 /* if we have an extern decide the type and make a record
612 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
615 s
.id1
= eb
->index
[i
];
617 /* else we want the external offset */
620 s
.id1
= eb
->index
[i
];
624 /* If we get here the seg value doesn't make sense */
625 nasm_panic("unrecognised segment value in ieee_write_fixup");
628 if (size
!= 2 && s
.ftype
== FT_SEG
)
629 nasm_nonfatal("IEEE format can only handle 2-byte"
630 " segment base references");
632 ieee_install_fixup(segto
, &s
);
635 /* should never get here */
637 static void ieee_install_fixup(struct ieeeSection
*seg
,
638 struct ieeeFixupp
*fix
)
640 struct ieeeFixupp
*f
;
641 f
= nasm_malloc(sizeof(struct ieeeFixupp
));
642 memcpy(f
, fix
, sizeof(struct ieeeFixupp
));
643 f
->offset
= seg
->currentpos
;
644 seg
->currentpos
+= fix
->size
;
647 seg
->flptr
= seg
->flptr
->next
= f
;
649 seg
->fptr
= seg
->flptr
= f
;
656 static int32_t ieee_segment(char *name
, int *bits
)
659 * We call the label manager here to define a name for the new
660 * segment, and when our _own_ label-definition stub gets
661 * called in return, it should register the new segment name
662 * using the pointer it gets passed. That way we save memory,
663 * by sponging off the label manager.
669 return seghead
->index
;
671 struct ieeeSection
*seg
;
677 * Look for segment attributes.
681 name
++; /* hack, but a documented one */
683 while (*p
&& !nasm_isspace(*p
))
687 while (*p
&& nasm_isspace(*p
))
691 while (*p
&& !nasm_isspace(*p
))
695 while (*p
&& nasm_isspace(*p
))
703 for (seg
= seghead
; seg
; seg
= seg
->next
) {
705 if (!strcmp(seg
->name
, name
)) {
706 if (attrs
> 0 && seg
->pass_last_seen
== pass_count())
707 nasm_warn(WARN_OTHER
, "segment attributes specified on"
708 " redeclaration of segment: ignoring");
714 seg
->pass_last_seen
= pass_count();
719 *segtail
= seg
= nasm_malloc(sizeof(*seg
));
721 segtail
= &seg
->next
;
722 seg
->index
= seg_alloc();
723 seg
->ieee_index
= ieee_idx
;
727 seg
->align
= 1; /* default */
728 seg
->use32
= *bits
== 32; /* default to user spec */
729 seg
->combine
= CMB_PUBLIC
; /* default */
731 seg
->pubtail
= &seg
->pubhead
;
735 seg
->loctail
= &seg
->lochead
;
738 * Process the segment attributes.
747 * `p' contains a segment attribute.
749 if (!nasm_stricmp(p
, "private"))
750 seg
->combine
= CMB_PRIVATE
;
751 else if (!nasm_stricmp(p
, "public"))
752 seg
->combine
= CMB_PUBLIC
;
753 else if (!nasm_stricmp(p
, "common"))
754 seg
->combine
= CMB_COMMON
;
755 else if (!nasm_stricmp(p
, "use16"))
757 else if (!nasm_stricmp(p
, "use32"))
759 else if (!nasm_strnicmp(p
, "align=", 6)) {
760 seg
->align
= readnum(p
+ 6, &rn_error
);
765 nasm_nonfatal("segment alignment should be numeric");
767 switch (seg
->align
) {
779 nasm_nonfatal("invalid alignment value %d", seg
->align
);
783 } else if (!nasm_strnicmp(p
, "absolute=", 9)) {
784 seg
->align
= SEG_ABS
+ readnum(p
+ 9, &rn_error
);
786 nasm_nonfatal("argument to `absolute' segment"
787 " attribute should be numeric");
791 ieee_seg_needs_update
= seg
;
792 if (seg
->align
>= SEG_ABS
)
793 define_label(name
, NO_SEG
, seg
->align
- SEG_ABS
, false);
795 define_label(name
, seg
->index
+ 1, 0L, false);
796 ieee_seg_needs_update
= NULL
;
807 * directives supported
809 static enum directive_result
810 ieee_directive(enum directive directive
, char *value
)
816 ieee_uppercase
= true;
824 static void ieee_sectalign(int32_t seg
, unsigned int value
)
826 struct ieeeSection
*s
;
828 list_for_each(s
, seghead
) {
834 * 256 is maximum there, note it may happen
835 * that align is issued on "absolute" segment
836 * it's fine since SEG_ABS > 256 and we never
837 * get escape this test
839 if (!s
|| !is_power2(value
) || value
> 256)
842 if ((unsigned int)s
->align
< value
)
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 */
869 static void ieee_write_file(void)
871 const struct tm
* const thetime
= &official_compile_time
.local
;
873 struct ieeeSection
*seg
;
874 struct ieeePublic
*pub
, *loc
;
875 struct ieeeExternal
*ext
;
876 struct ieeeObjData
*data
;
877 struct ieeeFixupp
*fix
;
880 const bool debuginfo
= (dfmt
== &ladsoft_debug_form
);
883 * Write the module header
885 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile
), ieee_infile
);
888 * Write the NASM boast comment.
890 ieee_putascii("CO0,%02X%s.\n", nasm_comment_len(), nasm_comment());
893 * write processor-specific information
895 ieee_putascii("AD8,4,L.\n");
900 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
901 1900 + thetime
->tm_year
, thetime
->tm_mon
+ 1,
902 thetime
->tm_mday
, thetime
->tm_hour
, thetime
->tm_min
,
905 * if debugging, dump file names
907 for (fn
= fnhead
; fn
&& debuginfo
; fn
= fn
->next
) {
908 ieee_putascii("C0105,%02X%s.\n", strlen(fn
->name
), fn
->name
);
911 ieee_putascii("CO101,07ENDHEAD.\n");
913 * the standard doesn't specify when to put checksums,
914 * we'll just do it periodically.
919 * Write the section headers
922 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
927 switch (seg
->combine
) {
939 ieee_unqualified_name(buf
, seg
->name
);
940 if (seg
->align
>= SEG_ABS
) {
941 ieee_putascii("ST%X,A,%02X%s.\n", seg
->ieee_index
,
943 ieee_putascii("ASL%X,%lX.\n", seg
->ieee_index
,
944 (seg
->align
- SEG_ABS
) * 16);
946 ieee_putascii("ST%X,%c,%02X%s.\n", seg
->ieee_index
, attrib
,
948 ieee_putascii("SA%X,%lX.\n", seg
->ieee_index
, seg
->align
);
949 ieee_putascii("ASS%X,%X.\n", seg
->ieee_index
,
955 * write the start address if there is one
957 if (ieee_entry_seg
) {
958 for (seg
= seghead
; seg
; seg
= seg
->next
)
959 if (seg
->index
== ieee_entry_seg
)
962 nasm_panic("Start address records are incorrect");
964 ieee_putascii("ASG,R%X,%lX,+.\n", seg
->ieee_index
,
973 for (seg
= seghead
; seg
; seg
= seg
->next
) {
974 for (pub
= seg
->pubhead
; pub
; pub
= pub
->next
) {
976 ieee_unqualified_name(buf
, pub
->name
);
977 ieee_putascii("NI%X,%02X%s.\n", i
, strlen(buf
), buf
);
978 if (pub
->segment
== -1)
979 ieee_putascii("ASI%X,R%X,%lX,+.\n", i
, pub
->index
,
982 ieee_putascii("ASI%X,%lX,%lX,+.\n", i
, pub
->segment
* 16,
985 if (pub
->type
>= 0x100)
986 ieee_putascii("ATI%X,T%X.\n", i
, pub
->type
- 0x100);
988 ieee_putascii("ATI%X,%X.\n", i
, pub
->type
);
997 ieee_unqualified_name(buf
, pub
->name
);
998 ieee_putascii("NI%X,%02X%s.\n", i
, strlen(buf
), buf
);
999 if (pub
->segment
== -1)
1000 ieee_putascii("ASI%X,R%X,%lX,+.\n", i
, pub
->index
,
1003 ieee_putascii("ASI%X,%lX,%lX,+.\n", i
, pub
->segment
* 16,
1006 if (pub
->type
>= 0x100)
1007 ieee_putascii("ATI%X,T%X.\n", i
, pub
->type
- 0x100);
1009 ieee_putascii("ATI%X,%X.\n", i
, pub
->type
);
1015 * Write the externals
1021 ieee_unqualified_name(buf
, ext
->name
);
1022 ieee_putascii("NX%X,%02X%s.\n", i
++, strlen(buf
), buf
);
1028 * IEEE doesn't have a standard pass break record
1029 * so use the ladsoft variant
1031 ieee_putascii("CO100,06ENDSYM.\n");
1037 for (arr
= arrhead
; arr
&& debuginfo
; arr
= arr
->next
) {
1038 ieee_putascii("TY%X,20,%X,%lX.\n", i
++, arr
->basetype
,
1045 for (seg
= seghead
; seg
&& debuginfo
; seg
= seg
->next
) {
1046 for (loc
= seg
->lochead
; loc
; loc
= loc
->next
) {
1048 ieee_unqualified_name(buf
, loc
->name
);
1049 ieee_putascii("NN%X,%02X%s.\n", i
, strlen(buf
), buf
);
1050 if (loc
->segment
== -1)
1051 ieee_putascii("ASN%X,R%X,%lX,+.\n", i
, loc
->index
,
1054 ieee_putascii("ASN%X,%lX,%lX,+.\n", i
, loc
->segment
* 16,
1057 if (loc
->type
>= 0x100)
1058 ieee_putascii("ATN%X,T%X.\n", i
, loc
->type
- 0x100);
1060 ieee_putascii("ATN%X,%X.\n", i
, loc
->type
);
1067 * put out section data;
1070 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
1073 if (seg
->currentpos
) {
1074 int32_t size
, org
= 0;
1076 ieee_putascii("SB%X.\n", seg
->ieee_index
);
1079 size
= HUNKSIZE
- (org
% HUNKSIZE
);
1082 seg
->currentpos
? seg
->currentpos
- org
: size
;
1083 size
= fix
->offset
- org
> size
? size
: fix
->offset
- org
;
1084 org
= ieee_putld(org
, org
+ size
, data
->data
);
1085 if (org
% HUNKSIZE
== 0)
1087 if (org
== fix
->offset
) {
1088 org
+= ieee_putlr(fix
);
1092 while (org
< seg
->currentpos
&& data
) {
1094 seg
->currentpos
- org
>
1095 HUNKSIZE
? HUNKSIZE
: seg
->currentpos
- org
;
1096 org
= ieee_putld(org
, org
+ size
, data
->data
);
1107 ieee_putascii("ME.\n");
1110 static void ieee_write_byte(struct ieeeSection
*seg
, int data
)
1113 if (!(temp
= seg
->currentpos
++ % HUNKSIZE
))
1115 seg
->datacurr
->data
[temp
] = data
;
1118 static void ieee_write_word(struct ieeeSection
*seg
, int data
)
1120 ieee_write_byte(seg
, data
& 0xFF);
1121 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1124 static void ieee_write_dword(struct ieeeSection
*seg
, int32_t data
)
1126 ieee_write_byte(seg
, data
& 0xFF);
1127 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1128 ieee_write_byte(seg
, (data
>> 16) & 0xFF);
1129 ieee_write_byte(seg
, (data
>> 24) & 0xFF);
1131 static void ieee_putascii(char *format
, ...)
1137 va_start(ap
, format
);
1138 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1140 for (i
= 0; i
< l
; i
++)
1141 if ((uint8_t)buffer
[i
] > 31)
1142 checksum
+= buffer
[i
];
1144 fputs(buffer
, ofile
);
1148 * put out a checksum record */
1149 static void ieee_putcs(int toclear
)
1152 ieee_putascii("CS.\n");
1156 ieee_putascii("CS%02X.\n", checksum
& 127);
1161 static int32_t ieee_putld(int32_t start
, int32_t end
, uint8_t *buf
)
1166 val
= start
% HUNKSIZE
;
1167 /* fill up multiple lines */
1168 while (end
- start
>= LDPERLINE
) {
1170 ieee_putascii("LD");
1171 for (i
= 0; i
< LDPERLINE
; i
++) {
1172 ieee_putascii("%02X", buf
[val
++]);
1175 ieee_putascii(".\n");
1177 /* if no partial lines */
1180 /* make a partial line */
1181 ieee_putascii("LD");
1182 while (start
< end
) {
1183 ieee_putascii("%02X", buf
[val
++]);
1186 ieee_putascii(".\n");
1189 static int32_t ieee_putlr(struct ieeeFixupp
*p
)
1192 * To deal with the vagaries of segmentation the LADsoft linker
1193 * defines two types of segments: absolute and virtual. Note that
1194 * 'absolute' in this context is a different thing from the IEEE
1195 * definition of an absolute segment type, which is also supported. If a
1196 * sement is linked in virtual mode the low limit (L-var) is
1197 * subtracted from each R,X, and P variable which appears in an
1198 * expression, so that we can have relative offsets. Meanwhile
1199 * in the ABSOLUTE mode this subtraction is not done and
1200 * so we can use absolute offsets from 0. In the LADsoft linker
1201 * this configuration is not done in the assemblker source but in
1202 * a source the linker reads. Generally this type of thing only
1203 * becomes an issue if real mode code is used. A pure 32-bit linker could
1204 * get away without defining the virtual mode...
1207 int32_t size
= p
->size
;
1211 sprintf(buf
, "%"PRIX32
"", -p
->id1
);
1213 sprintf(buf
, "L%"PRIX32
",10,/", p
->id1
);
1216 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+", p
->id1
, p
->addend
);
1219 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,P,-,%X,-", p
->id1
, p
->addend
, p
->size
);
1224 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->addend
,
1225 p
->id2
, -p
->id1
* 16);
1227 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->addend
,
1231 sprintf(buf
, "X%"PRIX32
"", p
->id1
);
1234 sprintf(buf
, "X%"PRIX32
",P,-,%"PRIX32
",-", p
->id1
, size
);
1237 /* We needed a non-ieee hack here.
1238 * We introduce the Y variable, which is the low
1239 * limit of the native segment the extern resides in
1241 sprintf(buf
, "Y%"PRIX32
",10,/", p
->id1
);
1245 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->id2
,
1248 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->id2
, p
->id1
);
1251 ieee_putascii("LR(%s,%"PRIX32
").\n", buf
, size
);
1256 /* Dump all segment data (text and fixups )*/
1258 static void ieee_unqualified_name(char *dest
, char *source
)
1260 if (ieee_uppercase
) {
1262 *dest
++ = toupper(*source
++);
1265 strcpy(dest
, source
);
1267 static void dbgls_init(void)
1273 arrindex
= ARRAY_BOT
;
1276 ieee_segment("??LINE", &tempint
);
1279 static void dbgls_cleanup(void)
1281 struct ieeeSection
*segtmp
;
1283 struct FileName
*fntemp
= fnhead
;
1284 fnhead
= fnhead
->next
;
1285 nasm_free(fntemp
->name
);
1288 for (segtmp
= seghead
; segtmp
; segtmp
= segtmp
->next
) {
1289 while (segtmp
->lochead
) {
1290 struct ieeePublic
*loctmp
= segtmp
->lochead
;
1291 segtmp
->lochead
= loctmp
->next
;
1292 nasm_free(loctmp
->name
);
1297 struct Array
*arrtmp
= arrhead
;
1298 arrhead
= arrhead
->next
;
1304 * because this routine is not bracketed in
1305 * the main program, this routine will be called even if there
1306 * is no request for debug info
1307 * so, we have to make sure the ??LINE segment is avaialbe
1308 * as the first segment when this debug format is selected
1310 static void dbgls_linnum(const char *lnfname
, int32_t lineno
, int32_t segto
)
1312 struct FileName
*fn
;
1313 struct ieeeSection
*seg
;
1315 if (segto
== NO_SEG
)
1319 * If `any_segs' is still false, we must define a default
1323 int tempint
; /* ignored */
1324 if (segto
!= ieee_segment("__NASMDEFSEG", &tempint
))
1325 nasm_panic("strange segment conditions in IEEE driver");
1329 * Find the segment we are targetting.
1331 for (seg
= seghead
; seg
; seg
= seg
->next
)
1332 if (seg
->index
== segto
)
1335 nasm_panic("lineno directed to nonexistent segment?");
1337 for (fn
= fnhead
; fn
; fn
= fn
->next
) {
1338 if (!nasm_stricmp(lnfname
, fn
->name
))
1343 fn
= nasm_malloc(sizeof(*fn
));
1344 fn
->name
= nasm_malloc(strlen(lnfname
) + 1);
1346 strcpy(fn
->name
, lnfname
);
1351 ieee_write_byte(seghead
, fn
->index
);
1352 ieee_write_word(seghead
, lineno
);
1353 ieee_write_fixup(segto
, NO_SEG
, seghead
, 4, OUT_ADDRESS
,
1357 static void dbgls_deflabel(char *name
, int32_t segment
,
1358 int64_t offset
, int is_global
, char *special
)
1360 struct ieeeSection
*seg
;
1362 /* Keep compiler from warning about special */
1366 * Note: ..[^@] special symbols are filtered in labels.c
1370 * If it's a special-retry from pass two, discard it.
1378 if (ieee_seg_needs_update
)
1380 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
1383 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
1388 * If `any_segs' is still false, we might need to define a
1389 * default segment, if they're trying to declare a label in
1390 * `first_seg'. But the label should exist due to a prior
1391 * call to ieee_deflabel so we can skip that.
1394 for (seg
= seghead
; seg
; seg
= seg
->next
)
1395 if (seg
->index
== segment
) {
1396 struct ieeePublic
*loc
;
1398 * Case (ii). Maybe MODPUB someday?
1401 last_defined
= loc
= nasm_malloc(sizeof(*loc
));
1402 *seg
->loctail
= loc
;
1403 seg
->loctail
= &loc
->next
;
1405 loc
->name
= nasm_strdup(name
);
1406 loc
->offset
= offset
;
1408 loc
->index
= seg
->ieee_index
;
1412 static void dbgls_typevalue(int32_t type
)
1414 int elem
= TYM_ELEMENTS(type
);
1415 type
= TYM_TYPE(type
);
1422 last_defined
->type
= 1; /* uint8_t */
1425 last_defined
->type
= 3; /* unsigned word */
1428 last_defined
->type
= 5; /* unsigned dword */
1431 last_defined
->type
= 9; /* float */
1434 last_defined
->type
= 10; /* qword */
1437 last_defined
->type
= 11; /* TBYTE */
1440 last_defined
->type
= 0x10; /* near label */
1445 struct Array
*arrtmp
= nasm_malloc(sizeof(*arrtmp
));
1446 int vtype
= last_defined
->type
;
1447 arrtmp
->size
= elem
;
1448 arrtmp
->basetype
= vtype
;
1449 arrtmp
->next
= NULL
;
1450 last_defined
->type
= arrindex
++ + 0x100;
1452 arrtail
= &(arrtmp
->next
);
1454 last_defined
= NULL
;
1456 static void dbgls_output(int output_type
, void *param
)
1461 static const struct dfmt ladsoft_debug_form
= {
1462 "LADsoft Debug Records",
1467 null_debug_directive
,
1471 NULL
/* pragma list */
1473 static const struct dfmt
* const ladsoft_debug_arr
[3] = {
1474 &ladsoft_debug_form
,
1478 const struct ofmt of_ieee
= {
1479 "IEEE-695 (LADsoft variant) object file format",
1485 &ladsoft_debug_form
,
1489 nasm_do_legacy_output
,
1498 NULL
/* pragma list */
1501 #endif /* OF_IEEE */