1 /* outobj.c output routines for the Netwide Assembler to produce
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
22 * outobj.c is divided into two sections. The first section is low level
23 * routines for creating obj records; It has nearly zero NASM specific
24 * code. The second section is high level routines for processing calls and
25 * data structures from the rest of NASM into obj format.
27 * It should be easy (though not zero work) to lift the first section out for
28 * use as an obj file writer for some other assembler or compiler.
32 * These routines are built around the ObjRecord data struture. An ObjRecord
33 * holds an object file record that may be under construction or complete.
35 * A major function of these routines is to support continuation of an obj
36 * record into the next record when the maximum record size is exceeded. The
37 * high level code does not need to worry about where the record breaks occur.
38 * It does need to do some minor extra steps to make the automatic continuation
39 * work. Those steps may be skipped for records where the high level knows no
40 * continuation could be required.
42 * 1) An ObjRecord is allocated and cleared by obj_new, or an existing ObjRecord
43 * is cleared by obj_clear.
45 * 2) The caller should fill in .type.
47 * 3) If the record is continuable and there is processing that must be done at
48 * the start of each record then the caller should fill in .ori with the
49 * address of the record initializer routine.
51 * 4) If the record is continuable and it should be saved (rather than emitted
52 * immediately) as each record is done, the caller should set .up to be a
53 * pointer to a location in which the caller keeps the master pointer to the
54 * ObjRecord. When the record is continued, the obj_bump routine will then
55 * allocate a new ObjRecord structure and update the master pointer.
57 * 5) If the .ori field was used then the caller should fill in the .parm with
58 * any data required by the initializer.
60 * 6) The caller uses the routines: obj_byte, obj_word, obj_rword, obj_dword,
61 * obj_x, obj_index, obj_value and obj_name to fill in the various kinds of
62 * data required for this record.
64 * 7) If the record is continuable, the caller should call obj_commit at each
65 * point where breaking the record is permitted.
67 * 8) To write out the record, the caller should call obj_emit2. If the
68 * caller has called obj_commit for all data written then he can get slightly
69 * faster code by calling obj_emit instead of obj_emit2.
71 * Most of these routines return an ObjRecord pointer. This will be the input
72 * pointer most of the time and will be the new location if the ObjRecord
73 * moved as a result of the call. The caller may ignore the return value in
74 * three cases: It is a "Never Reallocates" routine; or The caller knows
75 * continuation is not possible; or The caller uses the master pointer for the
79 #define RECORD_MAX 1024 /* maximum size of _any_ record */
80 #define OBJ_PARMS 3 /* maximum .parm used by any .ori routine */
82 #define FIX_08_LOW 0x8000 /* location type for various fixup subrecords */
83 #define FIX_16_OFFSET 0x8400
84 #define FIX_16_SELECTOR 0x8800
85 #define FIX_32_POINTER 0x8C00
86 #define FIX_08_HIGH 0x9000
87 #define FIX_32_OFFSET 0xA400
88 #define FIX_48_POINTER 0xAC00
90 enum RecordID
{ /* record ID codes */
92 THEADR
= 0x80, /* module header */
93 COMENT
= 0x88, /* comment record */
95 LINNUM
= 0x94, /* line number record */
96 LNAMES
= 0x96, /* list of names */
98 SEGDEF
= 0x98, /* segment definition */
99 GRPDEF
= 0x9A, /* group definition */
100 EXTDEF
= 0x8C, /* external definition */
101 PUBDEF
= 0x90, /* public definition */
102 COMDEF
= 0xB0, /* common definition */
104 LEDATA
= 0xA0, /* logical enumerated data */
105 FIXUPP
= 0x9C, /* fixups (relocations) */
107 MODEND
= 0x8A /* module end */
110 enum ComentID
{ /* ID codes for comment records */
112 dEXTENDED
= 0xA1, /* tells that we are using translator-specific extensions */
113 dLINKPASS
= 0xA2, /* link pass 2 marker */
114 dTYPEDEF
= 0xE3, /* define a type */
115 dSYM
= 0xE6, /* symbol debug record */
116 dFILNAME
= 0xE8, /* file name record */
117 dCOMPDEF
= 0xEA /* compiler type info */
121 typedef struct ObjRecord ObjRecord
;
122 typedef void ORI(ObjRecord
*orp
);
125 ORI
*ori
; /* Initialization routine */
126 int used
; /* Current data size */
127 int committed
; /* Data size at last boundary */
128 int x_size
; /* (see obj_x) */
129 unsigned int type
; /* Record type */
130 ObjRecord
*child
; /* Associated record below this one */
131 ObjRecord
**up
; /* Master pointer to this ObjRecord */
132 ObjRecord
*back
; /* Previous part of this record */
133 unsigned long parm
[OBJ_PARMS
]; /* Parameters for ori routine */
134 unsigned char buf
[RECORD_MAX
];
137 static void obj_fwrite(ObjRecord
*orp
);
138 static void ori_ledata(ObjRecord
*orp
);
139 static void ori_pubdef(ObjRecord
*orp
);
140 static void ori_null(ObjRecord
*orp
);
141 static ObjRecord
*obj_commit(ObjRecord
*orp
);
142 static void obj_write_fixup (ObjRecord
*orp
, int bytes
,
143 int segrel
, long seg
, long wrt
);
145 static int obj_uppercase
; /* Flag: all names in uppercase */
148 * Clear an ObjRecord structure. (Never reallocates).
149 * To simplify reuse of ObjRecord's, .type, .ori and .parm are not cleared.
151 static ObjRecord
*obj_clear(ObjRecord
*orp
)
163 * Emit an ObjRecord structure. (Never reallocates).
164 * The record is written out preceeded (recursively) by its previous part (if
165 * any) and followed (recursively) by its child (if any).
166 * The previous part and the child are freed. The main ObjRecord is cleared,
169 static ObjRecord
*obj_emit(ObjRecord
*orp
)
173 nasm_free(orp
->back
);
180 obj_emit(orp
->child
);
181 nasm_free(orp
->child
);
184 return (obj_clear(orp
));
188 * Commit and Emit a record. (Never reallocates).
190 static ObjRecord
*obj_emit2(ObjRecord
*orp
)
193 return (obj_emit(orp
));
197 * Allocate and clear a new ObjRecord; Also sets .ori to ori_null
199 static ObjRecord
*obj_new(void)
203 orp
= obj_clear( nasm_malloc(sizeof(ObjRecord
)) );
209 * Advance to the next record because the existing one is full or its x_size
211 * Any uncommited data is moved into the next record.
213 static ObjRecord
*obj_bump(ObjRecord
*orp
)
216 int used
= orp
->used
;
217 int committed
= orp
->committed
;
220 *orp
->up
= nxt
= obj_new();
222 nxt
->type
= orp
->type
;
225 memcpy( nxt
->parm
, orp
->parm
, sizeof(orp
->parm
));
233 nxt
->committed
= nxt
->used
;
234 memcpy( nxt
->buf
+ nxt
->committed
, orp
->buf
+ committed
, used
);
235 nxt
->used
= nxt
->committed
+ used
;
242 * Advance to the next record if necessary to allow the next field to fit.
244 static ObjRecord
*obj_check(ObjRecord
*orp
, int size
)
246 if (orp
->used
+ size
> RECORD_MAX
)
249 if (!orp
->committed
) {
252 orp
->committed
= orp
->used
;
259 * All data written so far is commited to the current record (won't be moved to
260 * the next record in case of continuation).
262 static ObjRecord
*obj_commit(ObjRecord
*orp
)
264 orp
->committed
= orp
->used
;
271 static ObjRecord
*obj_byte(ObjRecord
*orp
, unsigned char val
)
273 orp
= obj_check(orp
, 1);
274 orp
->buf
[orp
->used
] = val
;
282 static ObjRecord
*obj_word(ObjRecord
*orp
, unsigned int val
)
284 orp
= obj_check(orp
, 2);
285 orp
->buf
[orp
->used
] = val
;
286 orp
->buf
[orp
->used
+1] = val
>> 8;
292 * Write a reversed word
294 static ObjRecord
*obj_rword(ObjRecord
*orp
, unsigned int val
)
296 orp
= obj_check(orp
, 2);
297 orp
->buf
[orp
->used
] = val
>> 8;
298 orp
->buf
[orp
->used
+1] = val
;
306 static ObjRecord
*obj_dword(ObjRecord
*orp
, unsigned long val
)
308 orp
= obj_check(orp
, 4);
309 orp
->buf
[orp
->used
] = val
;
310 orp
->buf
[orp
->used
+1] = val
>> 8;
311 orp
->buf
[orp
->used
+2] = val
>> 16;
312 orp
->buf
[orp
->used
+3] = val
>> 24;
318 * All fields of "size x" in one obj record must be the same size (either 16
319 * bits or 32 bits). There is a one bit flag in each record which specifies
321 * This routine is used to force the current record to have the desired
322 * x_size. x_size is normally automatic (using obj_x), so that this
323 * routine should be used outside obj_x, only to provide compatibility with
324 * linkers that have bugs in their processing of the size bit.
327 static ObjRecord
*obj_force(ObjRecord
*orp
, int x
)
329 if (orp
->x_size
== (x
^48))
336 * This routine writes a field of size x. The caller does not need to worry at
337 * all about whether 16-bits or 32-bits are required.
339 static ObjRecord
*obj_x(ObjRecord
*orp
, unsigned long val
)
344 orp
= obj_force(orp
, 32);
345 if (orp
->x_size
== 32)
346 return (obj_dword(orp
, val
));
348 return (obj_word(orp
, val
));
354 static ObjRecord
*obj_index(ObjRecord
*orp
, unsigned int val
)
357 return ( obj_byte(orp
, val
) );
358 return (obj_word(orp
, (val
>>8) | (val
<<8) | 0x80));
362 * Writes a variable length value
364 static ObjRecord
*obj_value(ObjRecord
*orp
, unsigned long val
)
367 return ( obj_byte(orp
, val
) );
369 orp
= obj_byte(orp
, 129);
370 return ( obj_word(orp
, val
) );
373 return ( obj_dword(orp
, (val
<<8) + 132 ) );
374 orp
= obj_byte(orp
, 136);
375 return ( obj_dword(orp
, val
) );
379 * Writes a counted string
381 static ObjRecord
*obj_name(ObjRecord
*orp
, char *name
)
383 int len
= strlen(name
);
386 orp
= obj_check(orp
, len
+1);
387 ptr
= orp
->buf
+ orp
->used
;
392 *ptr
++ = toupper(*name
);
395 memcpy(ptr
, name
, len
);
400 * Initializer for an LEDATA record.
402 * parm[1] = segment index
403 * During the use of a LEDATA ObjRecord, parm[0] is constantly updated to
404 * represent the offset that would be required if the record were split at the
406 * parm[2] is a copy of parm[0] as it was when the current record was initted.
408 static void ori_ledata(ObjRecord
*orp
)
410 obj_index (orp
, orp
->parm
[1]);
411 orp
->parm
[2] = orp
->parm
[0];
412 obj_x (orp
, orp
->parm
[0]);
416 * Initializer for a PUBDEF record.
417 * parm[0] = group index
418 * parm[1] = segment index
419 * parm[2] = frame (only used when both indexes are zero)
421 static void ori_pubdef(ObjRecord
*orp
)
423 obj_index (orp
, orp
->parm
[0]);
424 obj_index (orp
, orp
->parm
[1]);
425 if ( !(orp
->parm
[0] | orp
->parm
[1]) )
426 obj_word (orp
, orp
->parm
[2]);
430 * Initializer for a LINNUM record.
431 * parm[0] = group index
432 * parm[1] = segment index
434 static void ori_linnum(ObjRecord
*orp
)
436 obj_index (orp
, orp
->parm
[0]);
437 obj_index (orp
, orp
->parm
[1]);
440 * Initializer for a local vars record.
442 static void ori_local(ObjRecord
*orp
)
444 obj_byte (orp
, 0x40);
445 obj_byte (orp
, dSYM
);
449 * Null initializer for records that continue without any header info
451 static void ori_null(ObjRecord
*orp
)
453 (void) orp
; /* Do nothing */
457 * This concludes the low level section of outobj.c
460 static char obj_infile
[FILENAME_MAX
];
463 static evalfunc evaluate
;
464 static ldfunc deflabel
;
466 static long first_seg
;
471 #define GROUP_MAX 256 /* we won't _realistically_ have more
472 * than this many segs in a group */
473 #define EXT_BLKSIZ 256 /* block size for externals list */
475 struct Segment
; /* need to know these structs exist */
479 struct LineNumber
*next
;
480 struct Segment
*segment
;
485 static struct FileName
{
486 struct FileName
*next
;
488 struct LineNumber
*lnhead
, **lntail
;
492 static struct Array
{
496 } *arrhead
, **arrtail
;
498 #define ARRAYBOT 31 /* magic number for first array index */
501 static struct Public
{
505 long segment
; /* only if it's far-absolute */
506 int type
; /* only for local debug syms */
507 } *fpubhead
, **fpubtail
, *last_defined
;
509 static struct External
{
510 struct External
*next
;
513 long commonelem
; /* element size if FAR, else zero */
514 int index
; /* OBJ-file external index */
516 DEFWRT_NONE
, /* no unusual default-WRT */
517 DEFWRT_STRING
, /* a string we don't yet understand */
518 DEFWRT_SEGMENT
, /* a segment */
519 DEFWRT_GROUP
/* a group */
526 struct External
*next_dws
; /* next with DEFWRT_STRING */
527 } *exthead
, **exttail
, *dws
;
529 static int externals
;
531 static struct ExtBack
{
532 struct ExtBack
*next
;
533 struct External
*exts
[EXT_BLKSIZ
];
536 static struct Segment
{
537 struct Segment
*next
;
538 long index
; /* the NASM segment id */
539 long obj_index
; /* the OBJ-file segment index */
540 struct Group
*grp
; /* the group it belongs to */
541 unsigned long currentpos
;
542 long align
; /* can be SEG_ABS + absolute addr */
549 long use32
; /* is this segment 32-bit? */
550 struct Public
*pubhead
, **pubtail
, *lochead
, **loctail
;
552 char *segclass
, *overlay
; /* `class' is a C++ keyword :-) */
554 } *seghead
, **segtail
, *obj_seg_needs_update
;
556 static struct Group
{
559 long index
; /* NASM segment id */
560 long obj_index
; /* OBJ-file group index */
561 long nentries
; /* number of elements... */
562 long nindices
; /* ...and number of index elts... */
566 } segs
[GROUP_MAX
]; /* ...in this */
567 } *grphead
, **grptail
, *obj_grp_needs_update
;
569 static struct ImpDef
{
573 unsigned int impindex
;
575 } *imphead
, **imptail
;
577 static struct ExpDef
{
581 unsigned int ordinal
;
583 } *exphead
, **exptail
;
585 #define EXPDEF_FLAG_ORDINAL 0x80
586 #define EXPDEF_FLAG_RESIDENT 0x40
587 #define EXPDEF_FLAG_NODATA 0x20
588 #define EXPDEF_MASK_PARMCNT 0x1F
590 static long obj_entry_seg
, obj_entry_ofs
;
594 static long obj_segment (char *, int, int *);
595 static void obj_write_file(int debuginfo
);
596 static int obj_directive (char *, char *, int);
598 static void obj_init (FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
604 first_seg
= seg_alloc();
607 fpubtail
= &fpubhead
;
618 seghead
= obj_seg_needs_update
= NULL
;
620 grphead
= obj_grp_needs_update
= NULL
;
622 obj_entry_seg
= NO_SEG
;
623 obj_uppercase
= FALSE
;
626 of_obj
.current_dfmt
->init (&of_obj
,NULL
,fp
,errfunc
);
629 static int obj_set_info(enum geninfo type
, char **val
)
636 static void obj_cleanup (int debuginfo
)
638 obj_write_file(debuginfo
);
639 of_obj
.current_dfmt
->cleanup();
642 struct Segment
*segtmp
= seghead
;
643 seghead
= seghead
->next
;
644 while (segtmp
->pubhead
) {
645 struct Public
*pubtmp
= segtmp
->pubhead
;
646 segtmp
->pubhead
= pubtmp
->next
;
647 nasm_free (pubtmp
->name
);
650 nasm_free (segtmp
->segclass
);
651 nasm_free (segtmp
->overlay
);
655 struct Public
*pubtmp
= fpubhead
;
656 fpubhead
= fpubhead
->next
;
657 nasm_free (pubtmp
->name
);
661 struct External
*exttmp
= exthead
;
662 exthead
= exthead
->next
;
666 struct ImpDef
*imptmp
= imphead
;
667 imphead
= imphead
->next
;
668 nasm_free (imptmp
->extname
);
669 nasm_free (imptmp
->libname
);
670 nasm_free (imptmp
->impname
); /* nasm_free won't mind if it's NULL */
674 struct ExpDef
*exptmp
= exphead
;
675 exphead
= exphead
->next
;
676 nasm_free (exptmp
->extname
);
677 nasm_free (exptmp
->intname
);
681 struct ExtBack
*ebtmp
= ebhead
;
682 ebhead
= ebhead
->next
;
686 struct Group
*grptmp
= grphead
;
687 grphead
= grphead
->next
;
692 static void obj_ext_set_defwrt (struct External
*ext
, char *id
)
697 for (seg
= seghead
; seg
; seg
= seg
->next
)
698 if (!strcmp(seg
->name
, id
)) {
699 ext
->defwrt_type
= DEFWRT_SEGMENT
;
700 ext
->defwrt_ptr
.seg
= seg
;
705 for (grp
= grphead
; grp
; grp
= grp
->next
)
706 if (!strcmp(grp
->name
, id
)) {
707 ext
->defwrt_type
= DEFWRT_GROUP
;
708 ext
->defwrt_ptr
.grp
= grp
;
713 ext
->defwrt_type
= DEFWRT_STRING
;
714 ext
->defwrt_ptr
.string
= id
;
719 static void obj_deflabel (char *name
, long segment
,
720 long offset
, int is_global
, char *special
)
723 * We have three cases:
725 * (i) `segment' is a segment-base. If so, set the name field
726 * for the segment or group structure it refers to, and then
729 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
730 * Save the label position for later output of a PUBDEF record.
731 * (Or a MODPUB, if we work out how.)
733 * (iii) `segment' is not one of our segments. Save the label
734 * position for later output of an EXTDEF, and also store a
735 * back-reference so that we can map later references to this
736 * segment number to the external index.
738 struct External
*ext
;
742 int used_special
= FALSE
; /* have we used the special text? */
745 * If it's a special-retry from pass two, discard it.
751 * First check for the double-period, signifying something
754 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
755 if (!strcmp(name
, "..start")) {
756 obj_entry_seg
= segment
;
757 obj_entry_ofs
= offset
;
760 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
766 if (obj_seg_needs_update
) {
767 obj_seg_needs_update
->name
= name
;
769 } else if (obj_grp_needs_update
) {
770 obj_grp_needs_update
->name
= name
;
773 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
776 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
778 * SEG_ABS subcase of (ii).
783 pub
= *fpubtail
= nasm_malloc(sizeof(*pub
));
784 fpubtail
= &pub
->next
;
786 pub
->name
= nasm_strdup(name
);
787 pub
->offset
= offset
;
788 pub
->segment
= (segment
== NO_SEG
? 0 : segment
& ~SEG_ABS
);
791 error(ERR_NONFATAL
, "OBJ supports no special symbol features"
792 " for this symbol type");
797 * If `any_segs' is still FALSE, we might need to define a
798 * default segment, if they're trying to declare a label in
801 if (!any_segs
&& segment
== first_seg
) {
802 int tempint
; /* ignored */
803 if (segment
!= obj_segment("__NASMDEFSEG", 2, &tempint
))
804 error (ERR_PANIC
, "strange segment conditions in OBJ driver");
807 for (seg
= seghead
; seg
&& is_global
; seg
= seg
->next
)
808 if (seg
->index
== segment
) {
809 struct Public
*loc
= nasm_malloc (sizeof(*loc
));
811 * Case (ii). Maybe MODPUB someday?
814 seg
->pubtail
= &loc
->next
;
816 loc
->name
= nasm_strdup(name
);
817 loc
->offset
= offset
;
820 error(ERR_NONFATAL
, "OBJ supports no special symbol features"
821 " for this symbol type");
829 ext
= *exttail
= nasm_malloc(sizeof(*ext
));
831 exttail
= &ext
->next
;
833 ext
->defwrt_type
= DEFWRT_NONE
;
834 if (is_global
== 2) {
835 ext
->commonsize
= offset
;
836 ext
->commonelem
= 1; /* default FAR */
844 * Now process the special text, if any, to find default-WRT
845 * specifications and common-variable element-size and near/far
848 while (special
&& *special
) {
852 * We might have a default-WRT specification.
854 if (!nasm_strnicmp(special
, "wrt", 3)) {
858 special
+= strspn(special
, " \t");
859 p
= nasm_strndup(special
, len
= strcspn(special
, ":"));
860 obj_ext_set_defwrt (ext
, p
);
862 if (*special
&& *special
!= ':')
863 error(ERR_NONFATAL
, "`:' expected in special symbol"
864 " text for `%s'", ext
->name
);
865 else if (*special
== ':')
870 * The NEAR or FAR keywords specify nearness or
871 * farness. FAR gives default element size 1.
873 if (!nasm_strnicmp(special
, "far", 3)) {
877 error(ERR_NONFATAL
, "`%s': `far' keyword may only be applied"
878 " to common variables\n", ext
->name
);
880 special
+= strspn(special
, " \t");
881 } else if (!nasm_strnicmp(special
, "near", 4)) {
885 error(ERR_NONFATAL
, "`%s': `far' keyword may only be applied"
886 " to common variables\n", ext
->name
);
888 special
+= strspn(special
, " \t");
892 * If it's a common, and anything else remains on the line
893 * before a further colon, evaluate it as an expression and
894 * use that as the element size. Forward references aren't
900 if (ext
->commonsize
) {
902 struct tokenval tokval
;
905 stdscan_bufptr
= special
;
906 tokval
.t_type
= TOKEN_INVALID
;
907 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
910 error (ERR_NONFATAL
, "cannot use relocatable"
911 " expression as common-variable element size");
913 ext
->commonelem
= reloc_value(e
);
915 special
= stdscan_bufptr
;
917 error (ERR_NONFATAL
, "`%s': element-size specifications only"
918 " apply to common variables", ext
->name
);
919 while (*special
&& *special
!= ':')
930 eb
= *ebtail
= nasm_malloc(sizeof(*eb
));
934 while (i
> EXT_BLKSIZ
) {
938 eb
= *ebtail
= nasm_malloc(sizeof(*eb
));
945 ext
->index
= ++externals
;
947 if (special
&& !used_special
)
948 error(ERR_NONFATAL
, "OBJ supports no special symbol features"
949 " for this symbol type");
952 static void obj_out (long segto
, void *data
, unsigned long type
,
953 long segment
, long wrt
)
956 unsigned char *ucdata
;
962 * handle absolute-assembly (structure definitions)
964 if (segto
== NO_SEG
) {
965 if ((type
& OUT_TYPMASK
) != OUT_RESERVE
)
966 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
972 * If `any_segs' is still FALSE, we must define a default
976 int tempint
; /* ignored */
977 if (segto
!= obj_segment("__NASMDEFSEG", 2, &tempint
))
978 error (ERR_PANIC
, "strange segment conditions in OBJ driver");
982 * Find the segment we are targetting.
984 for (seg
= seghead
; seg
; seg
= seg
->next
)
985 if (seg
->index
== segto
)
988 error (ERR_PANIC
, "code directed to nonexistent segment?");
991 orp
->parm
[0] = seg
->currentpos
;
993 size
= type
& OUT_SIZMASK
;
994 realtype
= type
& OUT_TYPMASK
;
995 if (realtype
== OUT_RAWDATA
) {
999 orp
= obj_check(seg
->orp
, 1);
1000 len
= RECORD_MAX
- orp
->used
;
1003 memcpy (orp
->buf
+orp
->used
, ucdata
, len
);
1004 orp
->committed
= orp
->used
+= len
;
1005 orp
->parm
[0] = seg
->currentpos
+= len
;
1010 else if (realtype
== OUT_ADDRESS
|| realtype
== OUT_REL2ADR
||
1011 realtype
== OUT_REL4ADR
)
1015 if (segment
== NO_SEG
&& realtype
!= OUT_ADDRESS
)
1016 error(ERR_NONFATAL
, "relative call to absolute address not"
1017 " supported by OBJ format");
1018 if (segment
>= SEG_ABS
)
1019 error(ERR_NONFATAL
, "far-absolute relocations not supported"
1021 ldata
= *(long *)data
;
1022 if (realtype
== OUT_REL2ADR
) {
1026 if (realtype
== OUT_REL4ADR
) {
1031 orp
= obj_word (orp
, ldata
);
1033 orp
= obj_dword (orp
, ldata
);
1035 if (segment
< SEG_ABS
&& (segment
!= NO_SEG
&& segment
% 2) &&
1038 * This is a 4-byte segment-base relocation such as
1039 * `MOV EAX,SEG foo'. OBJ format can't actually handle
1040 * these, but if the constant term has the 16 low bits
1041 * zero, we can just apply a 2-byte segment-base
1042 * relocation to the low word instead.
1046 error(ERR_NONFATAL
, "OBJ format cannot handle complex"
1047 " dword-size segment base references");
1049 if (segment
!= NO_SEG
)
1050 obj_write_fixup (orp
, rsize
,
1051 (realtype
== OUT_ADDRESS
? 0x4000 : 0),
1053 seg
->currentpos
+= size
;
1054 } else if (realtype
== OUT_RESERVE
) {
1056 orp
= obj_bump(orp
);
1057 seg
->currentpos
+= size
;
1062 static void obj_write_fixup (ObjRecord
*orp
, int bytes
,
1063 int segrel
, long seg
, long wrt
)
1068 struct Segment
*s
= NULL
;
1069 struct Group
*g
= NULL
;
1070 struct External
*e
= NULL
;
1074 error(ERR_NONFATAL
, "`obj' output driver does not support"
1075 " one-byte relocations");
1081 orp
->child
= forp
= obj_new();
1082 forp
->up
= &(orp
->child
);
1083 forp
->type
= FIXUPP
;
1088 locat
= FIX_16_SELECTOR
;
1091 error(ERR_PANIC
, "OBJ: 4-byte segment base fixup got"
1092 " through sanity check");
1096 locat
= (bytes
== 2) ? FIX_16_OFFSET
: FIX_32_OFFSET
;
1099 * There is a bug in tlink that makes it process self relative
1100 * fixups incorrectly if the x_size doesn't match the location
1103 forp
= obj_force(forp
, bytes
<<3);
1106 forp
= obj_rword (forp
, locat
| segrel
| (orp
->parm
[0]-orp
->parm
[2]));
1108 tidx
= fidx
= -1, method
= 0; /* placate optimisers */
1111 * See if we can find the segment ID in our segment list. If
1112 * so, we have a T4 (LSEG) target.
1114 for (s
= seghead
; s
; s
= s
->next
)
1115 if (s
->index
== seg
)
1118 method
= 4, tidx
= s
->obj_index
;
1120 for (g
= grphead
; g
; g
= g
->next
)
1121 if (g
->index
== seg
)
1124 method
= 5, tidx
= g
->obj_index
;
1127 struct ExtBack
*eb
= ebhead
;
1128 while (i
> EXT_BLKSIZ
) {
1136 method
= 6, e
= eb
->exts
[i
], tidx
= e
->index
;
1139 "unrecognised segment value in obj_write_fixup");
1144 * If no WRT given, assume the natural default, which is method
1147 * - we are doing an OFFSET fixup for a grouped segment, in
1148 * which case we require F1 (group).
1150 * - we are doing an OFFSET fixup for an external with a
1151 * default WRT, in which case we must honour the default WRT.
1153 if (wrt
== NO_SEG
) {
1154 if (!base
&& s
&& s
->grp
)
1155 method
|= 0x10, fidx
= s
->grp
->obj_index
;
1156 else if (!base
&& e
&& e
->defwrt_type
!= DEFWRT_NONE
) {
1157 if (e
->defwrt_type
== DEFWRT_SEGMENT
)
1158 method
|= 0x00, fidx
= e
->defwrt_ptr
.seg
->obj_index
;
1159 else if (e
->defwrt_type
== DEFWRT_GROUP
)
1160 method
|= 0x10, fidx
= e
->defwrt_ptr
.grp
->obj_index
;
1162 error(ERR_NONFATAL
, "default WRT specification for"
1163 " external `%s' unresolved", e
->name
);
1164 method
|= 0x50, fidx
= -1; /* got to do _something_ */
1167 method
|= 0x50, fidx
= -1;
1170 * See if we can find the WRT-segment ID in our segment
1171 * list. If so, we have a F0 (LSEG) frame.
1173 for (s
= seghead
; s
; s
= s
->next
)
1174 if (s
->index
== wrt
-1)
1177 method
|= 0x00, fidx
= s
->obj_index
;
1179 for (g
= grphead
; g
; g
= g
->next
)
1180 if (g
->index
== wrt
-1)
1183 method
|= 0x10, fidx
= g
->obj_index
;
1186 struct ExtBack
*eb
= ebhead
;
1187 while (i
> EXT_BLKSIZ
) {
1195 method
|= 0x20, fidx
= eb
->exts
[i
]->index
;
1198 "unrecognised WRT value in obj_write_fixup");
1203 forp
= obj_byte (forp
, method
);
1205 forp
= obj_index (forp
, fidx
);
1206 forp
= obj_index (forp
, tidx
);
1210 static long obj_segment (char *name
, int pass
, int *bits
)
1213 * We call the label manager here to define a name for the new
1214 * segment, and when our _own_ label-definition stub gets
1215 * called in return, it should register the new segment name
1216 * using the pointer it gets passed. That way we save memory,
1217 * by sponging off the label manager.
1223 struct Segment
*seg
;
1225 struct External
**extp
;
1226 int obj_idx
, i
, attrs
, rn_error
;
1230 * Look for segment attributes.
1233 while (*name
== '.')
1234 name
++; /* hack, but a documented one */
1236 while (*p
&& !isspace(*p
))
1240 while (*p
&& isspace(*p
))
1244 while (*p
&& !isspace(*p
))
1248 while (*p
&& isspace(*p
))
1256 for (seg
= seghead
; seg
; seg
= seg
->next
) {
1258 if (!strcmp(seg
->name
, name
)) {
1259 if (attrs
> 0 && pass
== 1)
1260 error(ERR_WARNING
, "segment attributes specified on"
1261 " redeclaration of segment: ignoring");
1270 *segtail
= seg
= nasm_malloc(sizeof(*seg
));
1272 segtail
= &seg
->next
;
1273 seg
->index
= (any_segs
? seg_alloc() : first_seg
);
1274 seg
->obj_index
= obj_idx
;
1278 seg
->currentpos
= 0;
1279 seg
->align
= 1; /* default */
1280 seg
->use32
= FALSE
; /* default */
1281 seg
->combine
= CMB_PUBLIC
; /* default */
1282 seg
->segclass
= seg
->overlay
= NULL
;
1283 seg
->pubhead
= NULL
;
1284 seg
->pubtail
= &seg
->pubhead
;
1285 seg
->lochead
= NULL
;
1286 seg
->loctail
= &seg
->lochead
;
1287 seg
->orp
= obj_new();
1288 seg
->orp
->up
= &(seg
->orp
);
1289 seg
->orp
->ori
= ori_ledata
;
1290 seg
->orp
->type
= LEDATA
;
1291 seg
->orp
->parm
[1] = obj_idx
;
1294 * Process the segment attributes.
1302 * `p' contains a segment attribute.
1304 if (!nasm_stricmp(p
, "private"))
1305 seg
->combine
= CMB_PRIVATE
;
1306 else if (!nasm_stricmp(p
, "public"))
1307 seg
->combine
= CMB_PUBLIC
;
1308 else if (!nasm_stricmp(p
, "common"))
1309 seg
->combine
= CMB_COMMON
;
1310 else if (!nasm_stricmp(p
, "stack"))
1311 seg
->combine
= CMB_STACK
;
1312 else if (!nasm_stricmp(p
, "use16"))
1314 else if (!nasm_stricmp(p
, "use32"))
1316 else if (!nasm_stricmp(p
, "flat")) {
1318 * This segment is an OS/2 FLAT segment. That means
1319 * that its default group is group FLAT, even if
1320 * the group FLAT does not explicitly _contain_ the
1323 * When we see this, we must create the group
1324 * `FLAT', containing no segments, if it does not
1325 * already exist; then we must set the default
1326 * group of this segment to be the FLAT group.
1329 for (grp
= grphead
; grp
; grp
= grp
->next
)
1330 if (!strcmp(grp
->name
, "FLAT"))
1333 obj_directive ("group", "FLAT", 1);
1334 for (grp
= grphead
; grp
; grp
= grp
->next
)
1335 if (!strcmp(grp
->name
, "FLAT"))
1338 error (ERR_PANIC
, "failure to define FLAT?!");
1341 } else if (!nasm_strnicmp(p
, "class=", 6))
1342 seg
->segclass
= nasm_strdup(p
+6);
1343 else if (!nasm_strnicmp(p
, "overlay=", 8))
1344 seg
->overlay
= nasm_strdup(p
+8);
1345 else if (!nasm_strnicmp(p
, "align=", 6)) {
1346 seg
->align
= readnum(p
+6, &rn_error
);
1349 error (ERR_NONFATAL
, "segment alignment should be"
1352 switch ((int) seg
->align
) {
1357 case 256: /* PAGE */
1358 case 4096: /* PharLap extension */
1361 error(ERR_WARNING
, "OBJ format does not support alignment"
1362 " of 8: rounding up to 16");
1368 error(ERR_WARNING
, "OBJ format does not support alignment"
1369 " of %d: rounding up to 256", seg
->align
);
1375 error(ERR_WARNING
, "OBJ format does not support alignment"
1376 " of %d: rounding up to 4096", seg
->align
);
1380 error(ERR_NONFATAL
, "invalid alignment value %d",
1385 } else if (!nasm_strnicmp(p
, "absolute=", 9)) {
1386 seg
->align
= SEG_ABS
+ readnum(p
+9, &rn_error
);
1388 error (ERR_NONFATAL
, "argument to `absolute' segment"
1389 " attribute should be numeric");
1393 obj_seg_needs_update
= seg
;
1394 if (seg
->align
>= SEG_ABS
)
1395 deflabel (name
, NO_SEG
, seg
->align
- SEG_ABS
,
1396 NULL
, FALSE
, FALSE
, &of_obj
, error
);
1398 deflabel (name
, seg
->index
+1, 0L,
1399 NULL
, FALSE
, FALSE
, &of_obj
, error
);
1400 obj_seg_needs_update
= NULL
;
1403 * See if this segment is defined in any groups.
1405 for (grp
= grphead
; grp
; grp
= grp
->next
) {
1406 for (i
= grp
->nindices
; i
< grp
->nentries
; i
++) {
1407 if (!strcmp(grp
->segs
[i
].name
, seg
->name
)) {
1408 nasm_free (grp
->segs
[i
].name
);
1409 grp
->segs
[i
] = grp
->segs
[grp
->nindices
];
1410 grp
->segs
[grp
->nindices
++].index
= seg
->obj_index
;
1412 error(ERR_WARNING
, "segment `%s' is already part of"
1413 " a group: first one takes precedence",
1422 * Walk through the list of externals with unresolved
1423 * default-WRT clauses, and resolve any that point at this
1428 if ((*extp
)->defwrt_type
== DEFWRT_STRING
&&
1429 !strcmp((*extp
)->defwrt_ptr
.string
, seg
->name
)) {
1430 nasm_free((*extp
)->defwrt_ptr
.string
);
1431 (*extp
)->defwrt_type
= DEFWRT_SEGMENT
;
1432 (*extp
)->defwrt_ptr
.seg
= seg
;
1433 *extp
= (*extp
)->next_dws
;
1435 extp
= &(*extp
)->next_dws
;
1446 static int obj_directive (char *directive
, char *value
, int pass
)
1448 if (!strcmp(directive
, "group")) {
1452 struct Segment
*seg
;
1453 struct External
**extp
;
1458 q
++; /* hack, but a documented one */
1460 while (*q
&& !isspace(*q
))
1464 while (*q
&& isspace(*q
))
1468 * Here we used to sanity-check the group directive to
1469 * ensure nobody tried to declare a group containing no
1470 * segments. However, OS/2 does this as standard
1471 * practice, so the sanity check has been removed.
1474 * error(ERR_NONFATAL,"GROUP directive contains no segments");
1480 for (grp
= grphead
; grp
; grp
= grp
->next
) {
1482 if (!strcmp(grp
->name
, v
)) {
1483 error(ERR_NONFATAL
, "group `%s' defined twice", v
);
1488 *grptail
= grp
= nasm_malloc(sizeof(*grp
));
1490 grptail
= &grp
->next
;
1491 grp
->index
= seg_alloc();
1492 grp
->obj_index
= obj_idx
;
1493 grp
->nindices
= grp
->nentries
= 0;
1496 obj_grp_needs_update
= grp
;
1497 deflabel (v
, grp
->index
+1, 0L,
1498 NULL
, FALSE
, FALSE
, &of_obj
, error
);
1499 obj_grp_needs_update
= NULL
;
1503 while (*q
&& !isspace(*q
))
1507 while (*q
&& isspace(*q
))
1511 * Now p contains a segment name. Find it.
1513 for (seg
= seghead
; seg
; seg
= seg
->next
)
1514 if (!strcmp(seg
->name
, p
))
1518 * We have a segment index. Shift a name entry
1519 * to the end of the array to make room.
1521 grp
->segs
[grp
->nentries
++] = grp
->segs
[grp
->nindices
];
1522 grp
->segs
[grp
->nindices
++].index
= seg
->obj_index
;
1524 error(ERR_WARNING
, "segment `%s' is already part of"
1525 " a group: first one takes precedence",
1531 * We have an as-yet undefined segment.
1532 * Remember its name, for later.
1534 grp
->segs
[grp
->nentries
++].name
= nasm_strdup(p
);
1539 * Walk through the list of externals with unresolved
1540 * default-WRT clauses, and resolve any that point at
1545 if ((*extp
)->defwrt_type
== DEFWRT_STRING
&&
1546 !strcmp((*extp
)->defwrt_ptr
.string
, grp
->name
)) {
1547 nasm_free((*extp
)->defwrt_ptr
.string
);
1548 (*extp
)->defwrt_type
= DEFWRT_GROUP
;
1549 (*extp
)->defwrt_ptr
.grp
= grp
;
1550 *extp
= (*extp
)->next_dws
;
1552 extp
= &(*extp
)->next_dws
;
1557 if (!strcmp(directive
, "uppercase")) {
1558 obj_uppercase
= TRUE
;
1561 if (!strcmp(directive
, "import")) {
1562 char *q
, *extname
, *libname
, *impname
;
1565 return 1; /* ignore in pass two */
1566 extname
= q
= value
;
1567 while (*q
&& !isspace(*q
))
1571 while (*q
&& isspace(*q
))
1576 while (*q
&& !isspace(*q
))
1580 while (*q
&& isspace(*q
))
1586 if (!*extname
|| !*libname
)
1587 error(ERR_NONFATAL
, "`import' directive requires symbol name"
1588 " and library name");
1593 imp
= *imptail
= nasm_malloc(sizeof(struct ImpDef
));
1594 imptail
= &imp
->next
;
1596 imp
->extname
= nasm_strdup(extname
);
1597 imp
->libname
= nasm_strdup(libname
);
1598 imp
->impindex
= readnum(impname
, &err
);
1599 if (!*impname
|| err
)
1600 imp
->impname
= nasm_strdup(impname
);
1602 imp
->impname
= NULL
;
1607 if (!strcmp(directive
, "export")) {
1608 char *q
, *extname
, *intname
, *v
;
1609 struct ExpDef
*export
;
1611 unsigned int ordinal
= 0;
1614 return 1; /* ignore in pass two */
1615 intname
= q
= value
;
1616 while (*q
&& !isspace(*q
))
1620 while (*q
&& isspace(*q
))
1625 while (*q
&& !isspace(*q
))
1629 while (*q
&& isspace(*q
))
1634 error(ERR_NONFATAL
, "`export' directive requires export name");
1643 while (*q
&& !isspace(*q
))
1647 while (*q
&& isspace(*q
))
1650 if (!nasm_stricmp(v
, "resident"))
1651 flags
|= EXPDEF_FLAG_RESIDENT
;
1652 else if (!nasm_stricmp(v
, "nodata"))
1653 flags
|= EXPDEF_FLAG_NODATA
;
1654 else if (!nasm_strnicmp(v
, "parm=", 5)) {
1656 flags
|= EXPDEF_MASK_PARMCNT
& readnum(v
+5, &err
);
1659 "value `%s' for `parm' is non-numeric", v
+5);
1664 ordinal
= readnum(v
, &err
);
1666 error(ERR_NONFATAL
, "unrecognised export qualifier `%s'",
1670 flags
|= EXPDEF_FLAG_ORDINAL
;
1674 export
= *exptail
= nasm_malloc(sizeof(struct ExpDef
));
1675 exptail
= &export
->next
;
1676 export
->next
= NULL
;
1677 export
->extname
= nasm_strdup(extname
);
1678 export
->intname
= nasm_strdup(intname
);
1679 export
->ordinal
= ordinal
;
1680 export
->flags
= flags
;
1687 static long obj_segbase (long segment
)
1689 struct Segment
*seg
;
1692 * Find the segment in our list.
1694 for (seg
= seghead
; seg
; seg
= seg
->next
)
1695 if (seg
->index
== segment
-1)
1700 * Might be an external with a default WRT.
1703 struct ExtBack
*eb
= ebhead
;
1706 while (i
> EXT_BLKSIZ
) {
1715 if (e
->defwrt_type
== DEFWRT_NONE
)
1716 return segment
; /* fine */
1717 else if (e
->defwrt_type
== DEFWRT_SEGMENT
)
1718 return e
->defwrt_ptr
.seg
->index
+1;
1719 else if (e
->defwrt_type
== DEFWRT_GROUP
)
1720 return e
->defwrt_ptr
.grp
->index
+1;
1722 return NO_SEG
; /* can't tell what it is */
1725 return segment
; /* not one of ours - leave it alone */
1728 if (seg
->align
>= SEG_ABS
)
1729 return seg
->align
; /* absolute segment */
1731 return seg
->grp
->index
+1; /* grouped segment */
1733 return segment
; /* no special treatment */
1736 static void obj_filename (char *inname
, char *outname
, efunc error
)
1738 strcpy(obj_infile
, inname
);
1739 standard_extension (inname
, outname
, ".obj", error
);
1742 static void obj_write_file (int debuginfo
)
1744 struct Segment
*seg
, *entry_seg_ptr
= 0;
1745 struct FileName
*fn
;
1746 struct LineNumber
*ln
;
1748 struct Public
*pub
, *loc
;
1749 struct External
*ext
;
1751 struct ExpDef
*export
;
1752 static char boast
[] = "The Netwide Assembler " NASM_VER
;
1757 * Write the THEADR module header.
1761 obj_name (orp
, obj_infile
);
1765 * Write the NASM boast comment.
1768 obj_rword (orp
, 0); /* comment type zero */
1769 obj_name (orp
, boast
);
1774 * Write the IMPDEF records, if any.
1776 for (imp
= imphead
; imp
; imp
= imp
->next
) {
1777 obj_rword (orp
, 0xA0); /* comment class A0 */
1778 obj_byte (orp
, 1); /* subfunction 1: IMPDEF */
1780 obj_byte (orp
, 0); /* import by name */
1782 obj_byte (orp
, 1); /* import by ordinal */
1783 obj_name (orp
, imp
->extname
);
1784 obj_name (orp
, imp
->libname
);
1786 obj_name (orp
, imp
->impname
);
1788 obj_word (orp
, imp
->impindex
);
1793 * Write the EXPDEF records, if any.
1795 for (export
= exphead
; export
; export
= export
->next
) {
1796 obj_rword (orp
, 0xA0); /* comment class A0 */
1797 obj_byte (orp
, 2); /* subfunction 2: EXPDEF */
1798 obj_byte (orp
, export
->flags
);
1799 obj_name (orp
, export
->extname
);
1800 obj_name (orp
, export
->intname
);
1801 if (export
->flags
& EXPDEF_FLAG_ORDINAL
)
1802 obj_word (orp
, export
->ordinal
);
1806 /* we're using extended OMF if we put in debug info*/
1809 obj_byte (orp
, 0x40);
1810 obj_byte (orp
, dEXTENDED
);
1815 * Write the first LNAMES record, containing LNAME one, which
1816 * is null. Also initialise the LNAME counter.
1822 * Write some LNAMES for the segment names
1824 for (seg
= seghead
; seg
; seg
= seg
->next
) {
1825 orp
= obj_name (orp
, seg
->name
);
1827 orp
= obj_name (orp
, seg
->segclass
);
1829 orp
= obj_name (orp
, seg
->overlay
);
1833 * Write some LNAMES for the group names
1835 for (grp
= grphead
; grp
; grp
= grp
->next
) {
1836 orp
= obj_name (orp
, grp
->name
);
1843 * Write the SEGDEF records.
1846 for (seg
= seghead
; seg
; seg
= seg
->next
) {
1848 unsigned long seglen
= seg
->currentpos
;
1850 acbp
= (seg
->combine
<< 2); /* C field */
1853 acbp
|= 0x01; /* P bit is Use32 flag */
1854 else if (seglen
== 0x10000L
) {
1855 seglen
= 0; /* This special case may be needed for old linkers */
1856 acbp
|= 0x02; /* B bit */
1861 if (seg
->align
>= SEG_ABS
)
1863 else if (seg
->align
>= 4096) {
1864 if (seg
->align
> 4096)
1865 error(ERR_NONFATAL
, "segment `%s' requires more alignment"
1866 " than OBJ format supports", seg
->name
);
1867 acbp
|= 0xC0; /* PharLap extension */
1868 } else if (seg
->align
>= 256) {
1870 } else if (seg
->align
>= 16) {
1872 } else if (seg
->align
>= 4) {
1874 } else if (seg
->align
>= 2) {
1879 obj_byte (orp
, acbp
);
1880 if (seg
->align
& SEG_ABS
) {
1881 obj_x (orp
, seg
->align
- SEG_ABS
); /* Frame */
1882 obj_byte (orp
, 0); /* Offset */
1884 obj_x (orp
, seglen
);
1885 obj_index (orp
, ++lname_idx
);
1886 obj_index (orp
, seg
->segclass
? ++lname_idx
: 1);
1887 obj_index (orp
, seg
->overlay
? ++lname_idx
: 1);
1892 * Write the GRPDEF records.
1895 for (grp
= grphead
; grp
; grp
= grp
->next
) {
1898 if (grp
->nindices
!= grp
->nentries
) {
1899 for (i
= grp
->nindices
; i
< grp
->nentries
; i
++) {
1900 error(ERR_NONFATAL
, "group `%s' contains undefined segment"
1901 " `%s'", grp
->name
, grp
->segs
[i
].name
);
1902 nasm_free (grp
->segs
[i
].name
);
1903 grp
->segs
[i
].name
= NULL
;
1906 obj_index (orp
, ++lname_idx
);
1907 for (i
= 0; i
< grp
->nindices
; i
++) {
1908 obj_byte (orp
, 0xFF);
1909 obj_index (orp
, grp
->segs
[i
].index
);
1915 * Write the PUBDEF records: first the ones in the segments,
1916 * then the far-absolutes.
1919 orp
->ori
= ori_pubdef
;
1920 for (seg
= seghead
; seg
; seg
= seg
->next
) {
1921 orp
->parm
[0] = seg
->grp
? seg
->grp
->obj_index
: 0;
1922 orp
->parm
[1] = seg
->obj_index
;
1923 for (pub
= seg
->pubhead
; pub
; pub
= pub
->next
) {
1924 orp
= obj_name (orp
, pub
->name
);
1925 orp
= obj_x (orp
, pub
->offset
);
1926 orp
= obj_byte (orp
, 0); /* type index */
1933 for (pub
= fpubhead
; pub
; pub
= pub
->next
) { /* pub-crawl :-) */
1934 if (orp
->parm
[2] != pub
->segment
) {
1936 orp
->parm
[2] = pub
->segment
;
1938 orp
= obj_name (orp
, pub
->name
);
1939 orp
= obj_x (orp
, pub
->offset
);
1940 orp
= obj_byte (orp
, 0); /* type index */
1946 * Write the EXTDEF and COMDEF records, in order.
1948 orp
->ori
= ori_null
;
1949 for (ext
= exthead
; ext
; ext
= ext
->next
) {
1950 if (ext
->commonsize
== 0) {
1951 if (orp
->type
!= EXTDEF
) {
1955 orp
= obj_name (orp
, ext
->name
);
1956 orp
= obj_index (orp
, 0);
1958 if (orp
->type
!= COMDEF
) {
1962 orp
= obj_name (orp
, ext
->name
);
1963 orp
= obj_index (orp
, 0);
1964 if (ext
->commonelem
) {
1965 orp
= obj_byte (orp
, 0x61);/* far communal */
1966 orp
= obj_value (orp
, (ext
->commonsize
/ ext
->commonelem
));
1967 orp
= obj_value (orp
, ext
->commonelem
);
1969 orp
= obj_byte (orp
, 0x62);/* near communal */
1970 orp
= obj_value (orp
, ext
->commonsize
);
1978 * Write a COMENT record stating that the linker's first pass
1979 * may stop processing at this point. Exception is if our
1980 * MODEND record specifies a start point, in which case,
1981 * according to some variants of the documentation, this COMENT
1982 * should be omitted. So we'll omit it just in case.
1983 * But, TASM puts it in all the time so if we are using
1984 * TASM debug stuff we are putting it in
1986 if (debuginfo
|| obj_entry_seg
== NO_SEG
) {
1988 obj_byte (orp
, 0x40);
1989 obj_byte (orp
, dLINKPASS
);
1995 * 1) put out the compiler type
1996 * 2) Put out the type info. The only type we are using is near label #19
2000 struct Array
*arrtmp
= arrhead
;
2002 obj_byte (orp
, 0x40);
2003 obj_byte (orp
, dCOMPDEF
);
2008 obj_byte (orp
, 0x40);
2009 obj_byte (orp
, dTYPEDEF
);
2010 obj_word (orp
, 0x18); /* type # for linking */
2011 obj_word (orp
, 6); /* size of type */
2012 obj_byte (orp
, 0x2a); /* absolute type for debugging */
2014 obj_byte (orp
, 0x40);
2015 obj_byte (orp
, dTYPEDEF
);
2016 obj_word (orp
, 0x19); /* type # for linking */
2017 obj_word (orp
, 0); /* size of type */
2018 obj_byte (orp
, 0x24); /* absolute type for debugging */
2019 obj_byte (orp
, 0); /* near/far specifier */
2021 obj_byte (orp
, 0x40);
2022 obj_byte (orp
, dTYPEDEF
);
2023 obj_word (orp
, 0x1A); /* type # for linking */
2024 obj_word (orp
, 0); /* size of type */
2025 obj_byte (orp
, 0x24); /* absolute type for debugging */
2026 obj_byte (orp
, 1); /* near/far specifier */
2028 obj_byte (orp
, 0x40);
2029 obj_byte (orp
, dTYPEDEF
);
2030 obj_word (orp
, 0x1b); /* type # for linking */
2031 obj_word (orp
, 0); /* size of type */
2032 obj_byte (orp
, 0x23); /* absolute type for debugging */
2037 obj_byte (orp
, 0x40);
2038 obj_byte (orp
, dTYPEDEF
);
2039 obj_word (orp
, 0x1c); /* type # for linking */
2040 obj_word (orp
, 0); /* size of type */
2041 obj_byte (orp
, 0x23); /* absolute type for debugging */
2046 obj_byte (orp
, 0x40);
2047 obj_byte (orp
, dTYPEDEF
);
2048 obj_word (orp
, 0x1d); /* type # for linking */
2049 obj_word (orp
, 0); /* size of type */
2050 obj_byte (orp
, 0x23); /* absolute type for debugging */
2055 obj_byte (orp
, 0x40);
2056 obj_byte (orp
, dTYPEDEF
);
2057 obj_word (orp
, 0x1e); /* type # for linking */
2058 obj_word (orp
, 0); /* size of type */
2059 obj_byte (orp
, 0x23); /* absolute type for debugging */
2065 /* put out the array types */
2066 for (i
= ARRAYBOT
; i
< arrindex
; i
++) {
2067 obj_byte (orp
, 0x40);
2068 obj_byte (orp
, dTYPEDEF
);
2069 obj_word (orp
, i
); /* type # for linking */
2070 obj_word (orp
, arrtmp
->size
); /* size of type */
2071 obj_byte (orp
, 0x1A); /* absolute type for debugging (array)*/
2072 obj_byte (orp
, arrtmp
->basetype
); /* base type */
2074 arrtmp
= arrtmp
->next
;
2078 * write out line number info with a LINNUM record
2079 * switch records when we switch segments, and output the
2080 * file in a pseudo-TASM fashion. The record switch is naive; that
2081 * is that one file may have many records for the same segment
2082 * if there are lots of segment switches
2084 if (fnhead
&& debuginfo
) {
2085 seg
= fnhead
->lnhead
->segment
;
2087 for (fn
= fnhead
; fn
; fn
= fn
->next
) {
2088 /* write out current file name */
2090 orp
->ori
= ori_null
;
2091 obj_byte (orp
, 0x40);
2092 obj_byte (orp
, dFILNAME
);
2094 obj_name( orp
,fn
->name
);
2098 /* write out line numbers this file */
2101 orp
->ori
= ori_linnum
;
2102 for (ln
= fn
->lnhead
; ln
; ln
= ln
->next
) {
2103 if (seg
!= ln
->segment
) {
2104 /* if we get here have to flush the buffer and start
2105 * a new record for a new segment
2110 orp
->parm
[0] = seg
->grp
? seg
->grp
->obj_index
: 0;
2111 orp
->parm
[1] = seg
->obj_index
;
2112 orp
= obj_word(orp
, ln
->lineno
);
2113 orp
= obj_x(orp
, ln
->offset
);
2120 * we are going to locate the entry point segment now
2121 * rather than wait until the MODEND record, because,
2122 * then we can output a special symbol to tell where the
2126 if (obj_entry_seg
!= NO_SEG
) {
2127 for (seg
= seghead
; seg
; seg
= seg
->next
) {
2128 if (seg
->index
== obj_entry_seg
) {
2129 entry_seg_ptr
= seg
;
2134 error(ERR_NONFATAL
, "entry point is not in this module");
2138 * get ready to put out symbol records
2141 orp
->ori
= ori_local
;
2144 * put out a symbol for the entry point
2145 * no dots in this symbol, because, borland does
2146 * not (officially) support dots in label names
2147 * and I don't know what various versions of TLINK will do
2149 if (debuginfo
&& obj_entry_seg
!= NO_SEG
) {
2150 orp
= obj_name (orp
,"start_of_program");
2151 orp
= obj_word (orp
,0x19); /* type: near label */
2152 orp
= obj_index (orp
, seg
->grp
? seg
->grp
->obj_index
: 0);
2153 orp
= obj_index (orp
, seg
->obj_index
);
2154 orp
= obj_x (orp
, obj_entry_ofs
);
2159 * put out the local labels
2161 for (seg
= seghead
; seg
&& debuginfo
; seg
= seg
->next
) {
2162 /* labels this seg */
2163 for (loc
= seg
->lochead
; loc
; loc
= loc
->next
) {
2164 orp
= obj_name (orp
,loc
->name
);
2165 orp
= obj_word (orp
, loc
->type
);
2166 orp
= obj_index (orp
, seg
->grp
? seg
->grp
->obj_index
: 0);
2167 orp
= obj_index (orp
, seg
->obj_index
);
2168 orp
= obj_x (orp
,loc
->offset
);
2176 * Write the LEDATA/FIXUPP pairs.
2178 for (seg
= seghead
; seg
; seg
= seg
->next
) {
2179 obj_emit (seg
->orp
);
2180 nasm_free (seg
->orp
);
2184 * Write the MODEND module end marker.
2187 orp
->ori
= ori_null
;
2188 if (entry_seg_ptr
) {
2189 obj_byte (orp
, 0xC1);
2190 seg
= entry_seg_ptr
;
2192 obj_byte (orp
, 0x10);
2193 obj_index (orp
, seg
->grp
->obj_index
);
2196 * the below changed to prevent TLINK crashing.
2197 * Previous more efficient version read:
2199 * obj_byte (orp, 0x50);
2201 obj_byte (orp
, 0x00);
2202 obj_index (orp
, seg
->obj_index
);
2204 obj_index (orp
, seg
->obj_index
);
2205 obj_x (orp
, obj_entry_ofs
);
2212 void obj_fwrite(ObjRecord
*orp
)
2214 unsigned int cksum
, len
;
2218 if (orp
->x_size
== 32)
2221 len
= orp
->committed
+1;
2222 cksum
+= (len
& 0xFF) + ((len
>>8) & 0xFF);
2223 fwriteshort (len
, ofp
);
2224 fwrite (orp
->buf
, 1, len
-1, ofp
);
2225 for (ptr
=orp
->buf
; --len
; ptr
++)
2227 fputc ( (-cksum
) & 0xFF, ofp
);
2230 static char *obj_stdmac
[] = {
2231 "%define __SECT__ [section .text]",
2232 "%imacro group 1+.nolist",
2235 "%imacro uppercase 0+.nolist",
2238 "%imacro export 1+.nolist",
2241 "%imacro import 1+.nolist",
2244 "%macro __NASM_CDecl__ 1",
2249 void dbgbi_init(struct ofmt
* of
, void * id
, FILE * fp
, efunc error
)
2258 arrindex
= ARRAYBOT
;
2262 static void dbgbi_cleanup(void)
2264 struct Segment
*segtmp
;
2266 struct FileName
*fntemp
= fnhead
;
2267 while (fnhead
->lnhead
) {
2268 struct LineNumber
*lntemp
= fnhead
->lnhead
;
2269 fnhead
->lnhead
= lntemp
->next
;
2272 fnhead
= fnhead
->next
;
2273 nasm_free (fntemp
->name
);
2276 for (segtmp
=seghead
; segtmp
; segtmp
=segtmp
->next
) {
2277 while (segtmp
->lochead
) {
2278 struct Public
*loctmp
= segtmp
->lochead
;
2279 segtmp
->lochead
= loctmp
->next
;
2280 nasm_free (loctmp
->name
);
2285 struct Array
*arrtmp
= arrhead
;
2286 arrhead
= arrhead
->next
;
2291 static void dbgbi_linnum (const char *lnfname
, long lineno
, long segto
)
2293 struct FileName
*fn
;
2294 struct LineNumber
*ln
;
2295 struct Segment
*seg
;
2297 if (segto
== NO_SEG
)
2301 * If `any_segs' is still FALSE, we must define a default
2305 int tempint
; /* ignored */
2306 if (segto
!= obj_segment("__NASMDEFSEG", 2, &tempint
))
2307 error (ERR_PANIC
, "strange segment conditions in OBJ driver");
2311 * Find the segment we are targetting.
2313 for (seg
= seghead
; seg
; seg
= seg
->next
)
2314 if (seg
->index
== segto
)
2317 error (ERR_PANIC
, "lineno directed to nonexistent segment?");
2319 for (fn
= fnhead
; fn
; fn
= fnhead
->next
)
2320 if (!nasm_stricmp(lnfname
,fn
->name
))
2323 fn
= nasm_malloc ( sizeof( *fn
));
2324 fn
->name
= nasm_malloc ( strlen(lnfname
) + 1) ;
2325 strcpy (fn
->name
,lnfname
);
2327 fn
->lntail
= & fn
->lnhead
;
2332 ln
= nasm_malloc ( sizeof( *ln
));
2334 ln
->offset
= seg
->currentpos
;
2335 ln
->lineno
= lineno
;
2338 fn
->lntail
= &ln
->next
;
2341 static void dbgbi_deflabel (char *name
, long segment
,
2342 long offset
, int is_global
, char *special
)
2344 struct Segment
*seg
;
2349 * If it's a special-retry from pass two, discard it.
2355 * First check for the double-period, signifying something
2358 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
2365 if (obj_seg_needs_update
) {
2367 } else if (obj_grp_needs_update
) {
2370 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
2373 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
2378 * If `any_segs' is still FALSE, we might need to define a
2379 * default segment, if they're trying to declare a label in
2380 * `first_seg'. But the label should exist due to a prior
2381 * call to obj_deflabel so we can skip that.
2384 for (seg
= seghead
; seg
; seg
= seg
->next
)
2385 if (seg
->index
== segment
) {
2386 struct Public
*loc
= nasm_malloc (sizeof(*loc
));
2388 * Case (ii). Maybe MODPUB someday?
2390 last_defined
= *seg
->loctail
= loc
;
2391 seg
->loctail
= &loc
->next
;
2393 loc
->name
= nasm_strdup(name
);
2394 loc
->offset
= offset
;
2397 static void dbgbi_typevalue (long type
)
2400 int elem
= TYM_ELEMENTS(type
);
2401 type
= TYM_TYPE(type
);
2408 last_defined
->type
= 8; /* unsigned char */
2412 last_defined
->type
= 10; /* unsigned word */
2416 last_defined
->type
= 12; /* unsigned dword */
2420 last_defined
->type
= 14; /* float */
2424 last_defined
->type
= 15; /* qword */
2428 last_defined
->type
= 16; /* TBYTE */
2432 last_defined
->type
= 0x19; /*label */
2438 struct Array
*arrtmp
= nasm_malloc (sizeof(*arrtmp
));
2439 int vtype
= last_defined
->type
;
2440 arrtmp
->size
= vsize
* elem
;
2441 arrtmp
->basetype
= vtype
;
2442 arrtmp
->next
= NULL
;
2443 last_defined
->type
= arrindex
++;
2445 arrtail
= & (arrtmp
->next
);
2447 last_defined
= NULL
;
2449 static void dbgbi_output (int output_type
, void *param
)
2454 static struct dfmt borland_debug_form
= {
2455 "Borland Debug Records",
2466 static struct dfmt
*borland_debug_arr
[3] = {
2467 &borland_debug_form
,
2472 struct ofmt of_obj
= {
2473 "MS-DOS 16-bit/32-bit OMF object files",