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)
212 strlcpy(ieee_infile
, inname
, sizeof(ieee_infile
));
215 fpubtail
= &fpubhead
;
221 seghead
= ieee_seg_needs_update
= NULL
;
223 ieee_entry_seg
= NO_SEG
;
224 ieee_uppercase
= false;
231 static void ieee_cleanup(void)
236 struct ieeeSection
*segtmp
= seghead
;
237 seghead
= seghead
->next
;
238 while (segtmp
->pubhead
) {
239 struct ieeePublic
*pubtmp
= segtmp
->pubhead
;
240 segtmp
->pubhead
= pubtmp
->next
;
243 while (segtmp
->fptr
) {
244 struct ieeeFixupp
*fixtmp
= segtmp
->fptr
;
245 segtmp
->fptr
= fixtmp
->next
;
248 while (segtmp
->data
) {
249 struct ieeeObjData
*dattmp
= segtmp
->data
;
250 segtmp
->data
= dattmp
->next
;
256 struct ieeePublic
*pubtmp
= fpubhead
;
257 fpubhead
= fpubhead
->next
;
261 struct ieeeExternal
*exttmp
= exthead
;
262 exthead
= exthead
->next
;
266 struct ExtBack
*ebtmp
= ebhead
;
267 ebhead
= ebhead
->next
;
273 * callback for labels
275 static void ieee_deflabel(char *name
, int32_t segment
,
276 int64_t offset
, int is_global
, char *special
)
279 * We have three cases:
281 * (i) `segment' is a segment-base. If so, set the name field
282 * for the segment structure it refers to, and then
285 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
286 * Save the label position for later output of a PUBDEF record.
289 * (iii) `segment' is not one of our segments. Save the label
290 * position for later output of an EXTDEF.
292 struct ieeeExternal
*ext
;
294 struct ieeeSection
*seg
;
298 nasm_error(ERR_NONFATAL
, "unrecognised symbol type `%s'", special
);
301 * First check for the double-period, signifying something
304 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
305 if (!strcmp(name
, "..start")) {
306 ieee_entry_seg
= segment
;
307 ieee_entry_ofs
= offset
;
315 if (ieee_seg_needs_update
) {
316 ieee_seg_needs_update
->name
= name
;
319 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
325 if (segment
>= SEG_ABS
) {
327 * SEG_ABS subcase of (ii).
330 struct ieeePublic
*pub
;
332 pub
= *fpubtail
= nasm_malloc(sizeof(*pub
));
333 fpubtail
= &pub
->next
;
336 pub
->offset
= offset
;
337 pub
->segment
= segment
& ~SEG_ABS
;
342 for (seg
= seghead
; seg
&& is_global
; seg
= seg
->next
)
343 if (seg
->index
== segment
) {
344 struct ieeePublic
*pub
;
346 last_defined
= pub
= *seg
->pubtail
= nasm_malloc(sizeof(*pub
));
347 seg
->pubtail
= &pub
->next
;
350 pub
->offset
= offset
;
351 pub
->index
= seg
->ieee_index
;
360 ext
= *exttail
= nasm_malloc(sizeof(*ext
));
362 exttail
= &ext
->next
;
365 ext
->commonsize
= offset
;
371 eb
= *ebtail
= nasm_zalloc(sizeof(*eb
));
375 while (i
> EXT_BLKSIZ
) {
379 eb
= *ebtail
= nasm_zalloc(sizeof(*eb
));
385 eb
->index
[i
] = externals
++;
393 static void ieee_out(int32_t segto
, const void *data
,
394 enum out_type type
, uint64_t size
,
395 int32_t segment
, int32_t wrt
)
397 const uint8_t *ucdata
;
399 struct ieeeSection
*seg
;
402 * If `any_segs' is still false, we must define a default
406 int tempint
; /* ignored */
407 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
408 nasm_panic("strange segment conditions in IEEE driver");
412 * Find the segment we are targetting.
414 for (seg
= seghead
; seg
; seg
= seg
->next
)
415 if (seg
->index
== segto
)
418 nasm_panic("code directed to nonexistent segment?");
420 if (type
== OUT_RAWDATA
) {
423 ieee_write_byte(seg
, *ucdata
++);
424 } else if (type
== OUT_ADDRESS
|| type
== OUT_REL2ADR
||
425 type
== OUT_REL4ADR
) {
426 if (type
== OUT_ADDRESS
)
427 size
= abs((int)size
);
428 else if (segment
== NO_SEG
)
429 nasm_error(ERR_NONFATAL
, "relative call to absolute address not"
430 " supported by IEEE format");
431 ldata
= *(int64_t *)data
;
432 if (type
== OUT_REL2ADR
)
434 if (type
== OUT_REL4ADR
)
436 ieee_write_fixup(segment
, wrt
, seg
, size
, type
, ldata
);
438 ieee_write_word(seg
, ldata
);
440 ieee_write_dword(seg
, ldata
);
441 } else if (type
== OUT_RESERVE
) {
443 ieee_write_byte(seg
, 0);
447 static void ieee_data_new(struct ieeeSection
*segto
)
451 segto
->data
= segto
->datacurr
=
452 nasm_malloc(sizeof(*(segto
->datacurr
)));
454 segto
->datacurr
= segto
->datacurr
->next
=
455 nasm_malloc(sizeof(*(segto
->datacurr
)));
456 segto
->datacurr
->next
= NULL
;
460 * this routine is unalduterated bloatware. I usually don't do this
461 * but I might as well see what it is like on a harmless program.
462 * If anyone wants to optimize this is a good canditate!
464 static void ieee_write_fixup(int32_t segment
, int32_t wrt
,
465 struct ieeeSection
*segto
, int size
,
466 uint64_t realtype
, int32_t offset
)
468 struct ieeeSection
*target
;
471 /* Don't put a fixup for things NASM can calculate */
472 if (wrt
== NO_SEG
&& segment
== NO_SEG
)
476 /* if it is a WRT offset */
481 s
.id1
= -(wrt
- SEG_ABS
);
483 if (wrt
% 2 && realtype
!= OUT_REL2ADR
484 && realtype
!= OUT_REL4ADR
) {
487 for (target
= seghead
; target
; target
= target
->next
)
488 if (target
->index
== wrt
)
491 s
.id1
= target
->ieee_index
;
492 for (target
= seghead
; target
; target
= target
->next
)
493 if (target
->index
== segment
)
497 s
.id2
= target
->ieee_index
;
500 * Now we assume the segment field is being used
501 * to hold an extern index
503 int32_t i
= segment
/ 2;
504 struct ExtBack
*eb
= ebhead
;
505 while (i
> EXT_BLKSIZ
) {
512 /* if we have an extern decide the type and make a record
517 s
.id2
= eb
->index
[i
];
519 nasm_error(ERR_NONFATAL
,
520 "Source of WRT must be an offset");
524 nasm_panic("unrecognised WRT value in ieee_write_fixup");
526 nasm_error(ERR_NONFATAL
, "target of WRT must be a section ");
529 ieee_install_fixup(segto
, &s
);
532 /* Pure segment fixup ? */
533 if (segment
!= NO_SEG
) {
536 if (segment
>= SEG_ABS
) {
537 /* absolute far segment fixup */
538 s
.id1
= -(segment
- ~SEG_ABS
);
539 } else if (segment
% 2) {
540 /* fixup to named segment */
542 for (target
= seghead
; target
; target
= target
->next
)
543 if (target
->index
== segment
- 1)
546 s
.id1
= target
->ieee_index
;
549 * Now we assume the segment field is being used
550 * to hold an extern index
552 int32_t i
= segment
/ 2;
553 struct ExtBack
*eb
= ebhead
;
554 while (i
> EXT_BLKSIZ
) {
561 /* if we have an extern decide the type and make a record
564 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
565 nasm_panic("Segment of a rel not supported in ieee_write_fixup");
567 /* If we want the segment */
570 s
.id1
= eb
->index
[i
];
574 /* If we get here the seg value doesn't make sense */
575 nasm_panic("unrecognised segment value in ieee_write_fixup");
579 /* Assume we are offsetting directly from a section
580 * So look up the target segment
582 for (target
= seghead
; target
; target
= target
->next
)
583 if (target
->index
== segment
)
586 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
587 /* PC rel to a known offset */
588 s
.id1
= target
->ieee_index
;
593 /* We were offsetting from a seg */
594 s
.id1
= target
->ieee_index
;
601 * Now we assume the segment field is being used
602 * to hold an extern index
604 int32_t i
= segment
/ 2;
605 struct ExtBack
*eb
= ebhead
;
606 while (i
> EXT_BLKSIZ
) {
613 /* if we have an extern decide the type and make a record
616 if (realtype
== OUT_REL2ADR
|| realtype
== OUT_REL4ADR
) {
619 s
.id1
= eb
->index
[i
];
621 /* else we want the external offset */
624 s
.id1
= eb
->index
[i
];
628 /* If we get here the seg value doesn't make sense */
629 nasm_panic("unrecognised segment value in ieee_write_fixup");
632 if (size
!= 2 && s
.ftype
== FT_SEG
)
633 nasm_error(ERR_NONFATAL
, "IEEE format can only handle 2-byte"
634 " segment base references");
636 ieee_install_fixup(segto
, &s
);
639 /* should never get here */
641 static void ieee_install_fixup(struct ieeeSection
*seg
,
642 struct ieeeFixupp
*fix
)
644 struct ieeeFixupp
*f
;
645 f
= nasm_malloc(sizeof(struct ieeeFixupp
));
646 memcpy(f
, fix
, sizeof(struct ieeeFixupp
));
647 f
->offset
= seg
->currentpos
;
648 seg
->currentpos
+= fix
->size
;
651 seg
->flptr
= seg
->flptr
->next
= f
;
653 seg
->fptr
= seg
->flptr
= f
;
660 static int32_t ieee_segment(char *name
, int pass
, int *bits
)
663 * We call the label manager here to define a name for the new
664 * segment, and when our _own_ label-definition stub gets
665 * called in return, it should register the new segment name
666 * using the pointer it gets passed. That way we save memory,
667 * by sponging off the label manager.
673 return seghead
->index
;
675 struct ieeeSection
*seg
;
681 * Look for segment attributes.
685 name
++; /* hack, but a documented one */
687 while (*p
&& !nasm_isspace(*p
))
691 while (*p
&& nasm_isspace(*p
))
695 while (*p
&& !nasm_isspace(*p
))
699 while (*p
&& nasm_isspace(*p
))
707 for (seg
= seghead
; seg
; seg
= seg
->next
) {
709 if (!strcmp(seg
->name
, name
)) {
710 if (attrs
> 0 && pass
== 1)
711 nasm_error(ERR_WARNING
, "segment attributes specified on"
712 " redeclaration of segment: ignoring");
721 *segtail
= seg
= nasm_malloc(sizeof(*seg
));
723 segtail
= &seg
->next
;
724 seg
->index
= seg_alloc();
725 seg
->ieee_index
= ieee_idx
;
729 seg
->align
= 1; /* default */
730 seg
->use32
= *bits
== 32; /* default to user spec */
731 seg
->combine
= CMB_PUBLIC
; /* default */
733 seg
->pubtail
= &seg
->pubhead
;
737 seg
->loctail
= &seg
->lochead
;
740 * Process the segment attributes.
749 * `p' contains a segment attribute.
751 if (!nasm_stricmp(p
, "private"))
752 seg
->combine
= CMB_PRIVATE
;
753 else if (!nasm_stricmp(p
, "public"))
754 seg
->combine
= CMB_PUBLIC
;
755 else if (!nasm_stricmp(p
, "common"))
756 seg
->combine
= CMB_COMMON
;
757 else if (!nasm_stricmp(p
, "use16"))
759 else if (!nasm_stricmp(p
, "use32"))
761 else if (!nasm_strnicmp(p
, "align=", 6)) {
762 seg
->align
= readnum(p
+ 6, &rn_error
);
767 nasm_error(ERR_NONFATAL
, "segment alignment should be"
770 switch (seg
->align
) {
782 nasm_error(ERR_NONFATAL
, "invalid alignment value %d",
787 } else if (!nasm_strnicmp(p
, "absolute=", 9)) {
788 seg
->align
= SEG_ABS
+ readnum(p
+ 9, &rn_error
);
790 nasm_error(ERR_NONFATAL
, "argument to `absolute' segment"
791 " attribute should be numeric");
795 ieee_seg_needs_update
= seg
;
796 if (seg
->align
>= SEG_ABS
)
797 define_label(name
, NO_SEG
, seg
->align
- SEG_ABS
, false);
799 define_label(name
, seg
->index
+ 1, 0L, false);
800 ieee_seg_needs_update
= NULL
;
811 * directives supported
813 static enum directive_result
814 ieee_directive(enum directive directive
, char *value
, int pass
)
822 ieee_uppercase
= true;
830 static void ieee_sectalign(int32_t seg
, unsigned int value
)
832 struct ieeeSection
*s
;
834 list_for_each(s
, seghead
) {
840 * 256 is maximum there, note it may happen
841 * that align is issued on "absolute" segment
842 * it's fine since SEG_ABS > 256 and we never
843 * get escape this test
845 if (!s
|| !is_power2(value
) || value
> 256)
848 if ((unsigned int)s
->align
< value
)
853 * Return segment data
855 static int32_t ieee_segbase(int32_t segment
)
857 struct ieeeSection
*seg
;
860 * Find the segment in our list.
862 for (seg
= seghead
; seg
; seg
= seg
->next
)
863 if (seg
->index
== segment
- 1)
867 return segment
; /* not one of ours - leave it alone */
869 if (seg
->align
>= SEG_ABS
)
870 return seg
->align
; /* absolute segment */
872 return segment
; /* no special treatment */
875 static void ieee_write_file(void)
877 const struct tm
* const thetime
= &official_compile_time
.local
;
879 struct ieeeSection
*seg
;
880 struct ieeePublic
*pub
, *loc
;
881 struct ieeeExternal
*ext
;
882 struct ieeeObjData
*data
;
883 struct ieeeFixupp
*fix
;
886 const bool debuginfo
= (dfmt
== &ladsoft_debug_form
);
889 * Write the module header
891 ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile
), ieee_infile
);
894 * Write the NASM boast comment.
896 ieee_putascii("CO0,%02X%s.\n", nasm_comment_len(), nasm_comment());
899 * write processor-specific information
901 ieee_putascii("AD8,4,L.\n");
906 ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n",
907 1900 + thetime
->tm_year
, thetime
->tm_mon
+ 1,
908 thetime
->tm_mday
, thetime
->tm_hour
, thetime
->tm_min
,
911 * if debugging, dump file names
913 for (fn
= fnhead
; fn
&& debuginfo
; fn
= fn
->next
) {
914 ieee_putascii("C0105,%02X%s.\n", strlen(fn
->name
), fn
->name
);
917 ieee_putascii("CO101,07ENDHEAD.\n");
919 * the standard doesn't specify when to put checksums,
920 * we'll just do it periodically.
925 * Write the section headers
928 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
933 switch (seg
->combine
) {
945 ieee_unqualified_name(buf
, seg
->name
);
946 if (seg
->align
>= SEG_ABS
) {
947 ieee_putascii("ST%X,A,%02X%s.\n", seg
->ieee_index
,
949 ieee_putascii("ASL%X,%lX.\n", seg
->ieee_index
,
950 (seg
->align
- SEG_ABS
) * 16);
952 ieee_putascii("ST%X,%c,%02X%s.\n", seg
->ieee_index
, attrib
,
954 ieee_putascii("SA%X,%lX.\n", seg
->ieee_index
, seg
->align
);
955 ieee_putascii("ASS%X,%X.\n", seg
->ieee_index
,
961 * write the start address if there is one
963 if (ieee_entry_seg
) {
964 for (seg
= seghead
; seg
; seg
= seg
->next
)
965 if (seg
->index
== ieee_entry_seg
)
968 nasm_panic("Start address records are incorrect");
970 ieee_putascii("ASG,R%X,%lX,+.\n", seg
->ieee_index
,
979 for (seg
= seghead
; seg
; seg
= seg
->next
) {
980 for (pub
= seg
->pubhead
; pub
; pub
= pub
->next
) {
982 ieee_unqualified_name(buf
, pub
->name
);
983 ieee_putascii("NI%X,%02X%s.\n", i
, strlen(buf
), buf
);
984 if (pub
->segment
== -1)
985 ieee_putascii("ASI%X,R%X,%lX,+.\n", i
, pub
->index
,
988 ieee_putascii("ASI%X,%lX,%lX,+.\n", i
, pub
->segment
* 16,
991 if (pub
->type
>= 0x100)
992 ieee_putascii("ATI%X,T%X.\n", i
, pub
->type
- 0x100);
994 ieee_putascii("ATI%X,%X.\n", i
, pub
->type
);
1003 ieee_unqualified_name(buf
, pub
->name
);
1004 ieee_putascii("NI%X,%02X%s.\n", i
, strlen(buf
), buf
);
1005 if (pub
->segment
== -1)
1006 ieee_putascii("ASI%X,R%X,%lX,+.\n", i
, pub
->index
,
1009 ieee_putascii("ASI%X,%lX,%lX,+.\n", i
, pub
->segment
* 16,
1012 if (pub
->type
>= 0x100)
1013 ieee_putascii("ATI%X,T%X.\n", i
, pub
->type
- 0x100);
1015 ieee_putascii("ATI%X,%X.\n", i
, pub
->type
);
1021 * Write the externals
1027 ieee_unqualified_name(buf
, ext
->name
);
1028 ieee_putascii("NX%X,%02X%s.\n", i
++, strlen(buf
), buf
);
1034 * IEEE doesn't have a standard pass break record
1035 * so use the ladsoft variant
1037 ieee_putascii("CO100,06ENDSYM.\n");
1043 for (arr
= arrhead
; arr
&& debuginfo
; arr
= arr
->next
) {
1044 ieee_putascii("TY%X,20,%X,%lX.\n", i
++, arr
->basetype
,
1051 for (seg
= seghead
; seg
&& debuginfo
; seg
= seg
->next
) {
1052 for (loc
= seg
->lochead
; loc
; loc
= loc
->next
) {
1054 ieee_unqualified_name(buf
, loc
->name
);
1055 ieee_putascii("NN%X,%02X%s.\n", i
, strlen(buf
), buf
);
1056 if (loc
->segment
== -1)
1057 ieee_putascii("ASN%X,R%X,%lX,+.\n", i
, loc
->index
,
1060 ieee_putascii("ASN%X,%lX,%lX,+.\n", i
, loc
->segment
* 16,
1063 if (loc
->type
>= 0x100)
1064 ieee_putascii("ATN%X,T%X.\n", i
, loc
->type
- 0x100);
1066 ieee_putascii("ATN%X,%X.\n", i
, loc
->type
);
1073 * put out section data;
1076 if (!debuginfo
&& !strcmp(seg
->name
, "??LINE"))
1079 if (seg
->currentpos
) {
1080 int32_t size
, org
= 0;
1082 ieee_putascii("SB%X.\n", seg
->ieee_index
);
1085 size
= HUNKSIZE
- (org
% HUNKSIZE
);
1088 seg
->currentpos
? seg
->currentpos
- org
: size
;
1089 size
= fix
->offset
- org
> size
? size
: fix
->offset
- org
;
1090 org
= ieee_putld(org
, org
+ size
, data
->data
);
1091 if (org
% HUNKSIZE
== 0)
1093 if (org
== fix
->offset
) {
1094 org
+= ieee_putlr(fix
);
1098 while (org
< seg
->currentpos
&& data
) {
1100 seg
->currentpos
- org
>
1101 HUNKSIZE
? HUNKSIZE
: seg
->currentpos
- org
;
1102 org
= ieee_putld(org
, org
+ size
, data
->data
);
1113 ieee_putascii("ME.\n");
1116 static void ieee_write_byte(struct ieeeSection
*seg
, int data
)
1119 if (!(temp
= seg
->currentpos
++ % HUNKSIZE
))
1121 seg
->datacurr
->data
[temp
] = data
;
1124 static void ieee_write_word(struct ieeeSection
*seg
, int data
)
1126 ieee_write_byte(seg
, data
& 0xFF);
1127 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1130 static void ieee_write_dword(struct ieeeSection
*seg
, int32_t data
)
1132 ieee_write_byte(seg
, data
& 0xFF);
1133 ieee_write_byte(seg
, (data
>> 8) & 0xFF);
1134 ieee_write_byte(seg
, (data
>> 16) & 0xFF);
1135 ieee_write_byte(seg
, (data
>> 24) & 0xFF);
1137 static void ieee_putascii(char *format
, ...)
1143 va_start(ap
, format
);
1144 vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1146 for (i
= 0; i
< l
; i
++)
1147 if ((uint8_t)buffer
[i
] > 31)
1148 checksum
+= buffer
[i
];
1150 fputs(buffer
, ofile
);
1154 * put out a checksum record */
1155 static void ieee_putcs(int toclear
)
1158 ieee_putascii("CS.\n");
1162 ieee_putascii("CS%02X.\n", checksum
& 127);
1167 static int32_t ieee_putld(int32_t start
, int32_t end
, uint8_t *buf
)
1172 val
= start
% HUNKSIZE
;
1173 /* fill up multiple lines */
1174 while (end
- start
>= LDPERLINE
) {
1176 ieee_putascii("LD");
1177 for (i
= 0; i
< LDPERLINE
; i
++) {
1178 ieee_putascii("%02X", buf
[val
++]);
1181 ieee_putascii(".\n");
1183 /* if no partial lines */
1186 /* make a partial line */
1187 ieee_putascii("LD");
1188 while (start
< end
) {
1189 ieee_putascii("%02X", buf
[val
++]);
1192 ieee_putascii(".\n");
1195 static int32_t ieee_putlr(struct ieeeFixupp
*p
)
1198 * To deal with the vagaries of segmentation the LADsoft linker
1199 * defines two types of segments: absolute and virtual. Note that
1200 * 'absolute' in this context is a different thing from the IEEE
1201 * definition of an absolute segment type, which is also supported. If a
1202 * sement is linked in virtual mode the low limit (L-var) is
1203 * subtracted from each R,X, and P variable which appears in an
1204 * expression, so that we can have relative offsets. Meanwhile
1205 * in the ABSOLUTE mode this subtraction is not done and
1206 * so we can use absolute offsets from 0. In the LADsoft linker
1207 * this configuration is not done in the assemblker source but in
1208 * a source the linker reads. Generally this type of thing only
1209 * becomes an issue if real mode code is used. A pure 32-bit linker could
1210 * get away without defining the virtual mode...
1213 int32_t size
= p
->size
;
1217 sprintf(buf
, "%"PRIX32
"", -p
->id1
);
1219 sprintf(buf
, "L%"PRIX32
",10,/", p
->id1
);
1222 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+", p
->id1
, p
->addend
);
1225 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,P,-,%X,-", p
->id1
, p
->addend
, p
->size
);
1230 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->addend
,
1231 p
->id2
, -p
->id1
* 16);
1233 sprintf(buf
, "R%"PRIX32
",%"PRIX32
",+,L%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->addend
,
1237 sprintf(buf
, "X%"PRIX32
"", p
->id1
);
1240 sprintf(buf
, "X%"PRIX32
",P,-,%"PRIX32
",-", p
->id1
, size
);
1243 /* We needed a non-ieee hack here.
1244 * We introduce the Y variable, which is the low
1245 * limit of the native segment the extern resides in
1247 sprintf(buf
, "Y%"PRIX32
",10,/", p
->id1
);
1251 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,%"PRIX32
",-", p
->id2
, p
->id2
,
1254 sprintf(buf
, "X%"PRIX32
",Y%"PRIX32
",+,L%"PRIX32
",-", p
->id2
, p
->id2
, p
->id1
);
1257 ieee_putascii("LR(%s,%"PRIX32
").\n", buf
, size
);
1262 /* Dump all segment data (text and fixups )*/
1264 static void ieee_unqualified_name(char *dest
, char *source
)
1266 if (ieee_uppercase
) {
1268 *dest
++ = toupper(*source
++);
1271 strcpy(dest
, source
);
1273 static void dbgls_init(void)
1279 arrindex
= ARRAY_BOT
;
1282 ieee_segment("??LINE", 2, &tempint
);
1285 static void dbgls_cleanup(void)
1287 struct ieeeSection
*segtmp
;
1289 struct FileName
*fntemp
= fnhead
;
1290 fnhead
= fnhead
->next
;
1291 nasm_free(fntemp
->name
);
1294 for (segtmp
= seghead
; segtmp
; segtmp
= segtmp
->next
) {
1295 while (segtmp
->lochead
) {
1296 struct ieeePublic
*loctmp
= segtmp
->lochead
;
1297 segtmp
->lochead
= loctmp
->next
;
1298 nasm_free(loctmp
->name
);
1303 struct Array
*arrtmp
= arrhead
;
1304 arrhead
= arrhead
->next
;
1310 * because this routine is not bracketed in
1311 * the main program, this routine will be called even if there
1312 * is no request for debug info
1313 * so, we have to make sure the ??LINE segment is avaialbe
1314 * as the first segment when this debug format is selected
1316 static void dbgls_linnum(const char *lnfname
, int32_t lineno
, int32_t segto
)
1318 struct FileName
*fn
;
1319 struct ieeeSection
*seg
;
1321 if (segto
== NO_SEG
)
1325 * If `any_segs' is still false, we must define a default
1329 int tempint
; /* ignored */
1330 if (segto
!= ieee_segment("__NASMDEFSEG", 2, &tempint
))
1331 nasm_panic("strange segment conditions in OBJ driver");
1335 * Find the segment we are targetting.
1337 for (seg
= seghead
; seg
; seg
= seg
->next
)
1338 if (seg
->index
== segto
)
1341 nasm_panic("lineno directed to nonexistent segment?");
1343 for (fn
= fnhead
; fn
; fn
= fn
->next
) {
1344 if (!nasm_stricmp(lnfname
, fn
->name
))
1349 fn
= nasm_malloc(sizeof(*fn
));
1350 fn
->name
= nasm_malloc(strlen(lnfname
) + 1);
1352 strcpy(fn
->name
, lnfname
);
1357 ieee_write_byte(seghead
, fn
->index
);
1358 ieee_write_word(seghead
, lineno
);
1359 ieee_write_fixup(segto
, NO_SEG
, seghead
, 4, OUT_ADDRESS
,
1363 static void dbgls_deflabel(char *name
, int32_t segment
,
1364 int64_t offset
, int is_global
, char *special
)
1366 struct ieeeSection
*seg
;
1368 /* Keep compiler from warning about special */
1372 * Note: ..[^@] special symbols are filtered in labels.c
1376 * If it's a special-retry from pass two, discard it.
1384 if (ieee_seg_needs_update
)
1386 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
1389 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
1394 * If `any_segs' is still false, we might need to define a
1395 * default segment, if they're trying to declare a label in
1396 * `first_seg'. But the label should exist due to a prior
1397 * call to ieee_deflabel so we can skip that.
1400 for (seg
= seghead
; seg
; seg
= seg
->next
)
1401 if (seg
->index
== segment
) {
1402 struct ieeePublic
*loc
;
1404 * Case (ii). Maybe MODPUB someday?
1407 last_defined
= loc
= nasm_malloc(sizeof(*loc
));
1408 *seg
->loctail
= loc
;
1409 seg
->loctail
= &loc
->next
;
1411 loc
->name
= nasm_strdup(name
);
1412 loc
->offset
= offset
;
1414 loc
->index
= seg
->ieee_index
;
1418 static void dbgls_typevalue(int32_t type
)
1420 int elem
= TYM_ELEMENTS(type
);
1421 type
= TYM_TYPE(type
);
1428 last_defined
->type
= 1; /* uint8_t */
1431 last_defined
->type
= 3; /* unsigned word */
1434 last_defined
->type
= 5; /* unsigned dword */
1437 last_defined
->type
= 9; /* float */
1440 last_defined
->type
= 10; /* qword */
1443 last_defined
->type
= 11; /* TBYTE */
1446 last_defined
->type
= 0x10; /* near label */
1451 struct Array
*arrtmp
= nasm_malloc(sizeof(*arrtmp
));
1452 int vtype
= last_defined
->type
;
1453 arrtmp
->size
= elem
;
1454 arrtmp
->basetype
= vtype
;
1455 arrtmp
->next
= NULL
;
1456 last_defined
->type
= arrindex
++ + 0x100;
1458 arrtail
= &(arrtmp
->next
);
1460 last_defined
= NULL
;
1462 static void dbgls_output(int output_type
, void *param
)
1467 static const struct dfmt ladsoft_debug_form
= {
1468 "LADsoft Debug Records",
1473 null_debug_directive
,
1477 NULL
/* pragma list */
1479 static const struct dfmt
* const ladsoft_debug_arr
[3] = {
1480 &ladsoft_debug_form
,
1484 const struct ofmt of_ieee
= {
1485 "IEEE-695 (LADsoft variant) object file format",
1491 &ladsoft_debug_form
,
1495 nasm_do_legacy_output
,
1504 NULL
/* pragma list */
1507 #endif /* OF_IEEE */