1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 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 * outobj.c output routines for the Netwide Assembler to produce
51 #include "output/outform.h"
52 #include "output/outlib.h"
57 * outobj.c is divided into two sections. The first section is low level
58 * routines for creating obj records; It has nearly zero NASM specific
59 * code. The second section is high level routines for processing calls and
60 * data structures from the rest of NASM into obj format.
62 * It should be easy (though not zero work) to lift the first section out for
63 * use as an obj file writer for some other assembler or compiler.
67 * These routines are built around the ObjRecord data struture. An ObjRecord
68 * holds an object file record that may be under construction or complete.
70 * A major function of these routines is to support continuation of an obj
71 * record into the next record when the maximum record size is exceeded. The
72 * high level code does not need to worry about where the record breaks occur.
73 * It does need to do some minor extra steps to make the automatic continuation
74 * work. Those steps may be skipped for records where the high level knows no
75 * continuation could be required.
77 * 1) An ObjRecord is allocated and cleared by obj_new, or an existing ObjRecord
78 * is cleared by obj_clear.
80 * 2) The caller should fill in .type.
82 * 3) If the record is continuable and there is processing that must be done at
83 * the start of each record then the caller should fill in .ori with the
84 * address of the record initializer routine.
86 * 4) If the record is continuable and it should be saved (rather than emitted
87 * immediately) as each record is done, the caller should set .up to be a
88 * pointer to a location in which the caller keeps the master pointer to the
89 * ObjRecord. When the record is continued, the obj_bump routine will then
90 * allocate a new ObjRecord structure and update the master pointer.
92 * 5) If the .ori field was used then the caller should fill in the .parm with
93 * any data required by the initializer.
95 * 6) The caller uses the routines: obj_byte, obj_word, obj_rword, obj_dword,
96 * obj_x, obj_index, obj_value and obj_name to fill in the various kinds of
97 * data required for this record.
99 * 7) If the record is continuable, the caller should call obj_commit at each
100 * point where breaking the record is permitted.
102 * 8) To write out the record, the caller should call obj_emit2. If the
103 * caller has called obj_commit for all data written then he can get slightly
104 * faster code by calling obj_emit instead of obj_emit2.
106 * Most of these routines return an ObjRecord pointer. This will be the input
107 * pointer most of the time and will be the new location if the ObjRecord
108 * moved as a result of the call. The caller may ignore the return value in
109 * three cases: It is a "Never Reallocates" routine; or The caller knows
110 * continuation is not possible; or The caller uses the master pointer for the
114 #define RECORD_MAX (1024-3) /* maximal size of any record except type+reclen */
115 #define OBJ_PARMS 3 /* maximum .parm used by any .ori routine */
117 #define FIX_08_LOW 0x8000 /* location type for various fixup subrecords */
118 #define FIX_16_OFFSET 0x8400
119 #define FIX_16_SELECTOR 0x8800
120 #define FIX_32_POINTER 0x8C00
121 #define FIX_08_HIGH 0x9000
122 #define FIX_32_OFFSET 0xA400
123 #define FIX_48_POINTER 0xAC00
125 enum RecordID
{ /* record ID codes */
127 THEADR
= 0x80, /* module header */
128 COMENT
= 0x88, /* comment record */
130 LINNUM
= 0x94, /* line number record */
131 LNAMES
= 0x96, /* list of names */
133 SEGDEF
= 0x98, /* segment definition */
134 GRPDEF
= 0x9A, /* group definition */
135 EXTDEF
= 0x8C, /* external definition */
136 PUBDEF
= 0x90, /* public definition */
137 COMDEF
= 0xB0, /* common definition */
139 LEDATA
= 0xA0, /* logical enumerated data */
140 FIXUPP
= 0x9C, /* fixups (relocations) */
141 FIXU32
= 0x9D, /* 32-bit fixups (relocations) */
143 MODEND
= 0x8A, /* module end */
144 MODE32
= 0x8B /* module end for 32-bit objects */
147 enum ComentID
{ /* ID codes for comment records */
149 dEXTENDED
= 0xA1, /* tells that we are using translator-specific extensions */
150 dLINKPASS
= 0xA2, /* link pass 2 marker */
151 dTYPEDEF
= 0xE3, /* define a type */
152 dSYM
= 0xE6, /* symbol debug record */
153 dFILNAME
= 0xE8, /* file name record */
154 dCOMPDEF
= 0xEA /* compiler type info */
157 typedef struct ObjRecord ObjRecord
;
158 typedef void ORI(ObjRecord
* orp
);
161 ORI
*ori
; /* Initialization routine */
162 int used
; /* Current data size */
163 int committed
; /* Data size at last boundary */
164 int x_size
; /* (see obj_x) */
165 unsigned int type
; /* Record type */
166 ObjRecord
*child
; /* Associated record below this one */
167 ObjRecord
**up
; /* Master pointer to this ObjRecord */
168 ObjRecord
*back
; /* Previous part of this record */
169 uint32_t parm
[OBJ_PARMS
]; /* Parameters for ori routine */
170 uint8_t buf
[RECORD_MAX
+ 3];
173 static void obj_fwrite(ObjRecord
* orp
);
174 static void ori_ledata(ObjRecord
* orp
);
175 static void ori_pubdef(ObjRecord
* orp
);
176 static void ori_null(ObjRecord
* orp
);
177 static ObjRecord
*obj_commit(ObjRecord
* orp
);
179 static bool obj_uppercase
; /* Flag: all names in uppercase */
180 static bool obj_use32
; /* Flag: at least one segment is 32-bit */
183 * Clear an ObjRecord structure. (Never reallocates).
184 * To simplify reuse of ObjRecord's, .type, .ori and .parm are not cleared.
186 static ObjRecord
*obj_clear(ObjRecord
* orp
)
198 * Emit an ObjRecord structure. (Never reallocates).
199 * The record is written out preceeded (recursively) by its previous part (if
200 * any) and followed (recursively) by its child (if any).
201 * The previous part and the child are freed. The main ObjRecord is cleared,
204 static ObjRecord
*obj_emit(ObjRecord
* orp
)
208 nasm_free(orp
->back
);
215 obj_emit(orp
->child
);
216 nasm_free(orp
->child
);
219 return (obj_clear(orp
));
223 * Commit and Emit a record. (Never reallocates).
225 static ObjRecord
*obj_emit2(ObjRecord
* orp
)
228 return (obj_emit(orp
));
232 * Allocate and clear a new ObjRecord; Also sets .ori to ori_null
234 static ObjRecord
*obj_new(void)
238 orp
= obj_clear(nasm_malloc(sizeof(ObjRecord
)));
244 * Advance to the next record because the existing one is full or its x_size
246 * Any uncommited data is moved into the next record.
248 static ObjRecord
*obj_bump(ObjRecord
* orp
)
251 int used
= orp
->used
;
252 int committed
= orp
->committed
;
255 *orp
->up
= nxt
= obj_new();
257 nxt
->type
= orp
->type
;
260 memcpy(nxt
->parm
, orp
->parm
, sizeof(orp
->parm
));
268 nxt
->committed
= nxt
->used
;
269 memcpy(nxt
->buf
+ nxt
->committed
, orp
->buf
+ committed
, used
);
270 nxt
->used
= nxt
->committed
+ used
;
277 * Advance to the next record if necessary to allow the next field to fit.
279 static ObjRecord
*obj_check(ObjRecord
* orp
, int size
)
281 if (orp
->used
+ size
> RECORD_MAX
)
284 if (!orp
->committed
) {
287 orp
->committed
= orp
->used
;
294 * All data written so far is commited to the current record (won't be moved to
295 * the next record in case of continuation).
297 static ObjRecord
*obj_commit(ObjRecord
* orp
)
299 orp
->committed
= orp
->used
;
306 static ObjRecord
*obj_byte(ObjRecord
* orp
, uint8_t val
)
308 orp
= obj_check(orp
, 1);
309 orp
->buf
[orp
->used
] = val
;
317 static ObjRecord
*obj_word(ObjRecord
* orp
, unsigned int val
)
319 orp
= obj_check(orp
, 2);
320 orp
->buf
[orp
->used
] = val
;
321 orp
->buf
[orp
->used
+ 1] = val
>> 8;
327 * Write a reversed word
329 static ObjRecord
*obj_rword(ObjRecord
* orp
, unsigned int val
)
331 orp
= obj_check(orp
, 2);
332 orp
->buf
[orp
->used
] = val
>> 8;
333 orp
->buf
[orp
->used
+ 1] = val
;
341 static ObjRecord
*obj_dword(ObjRecord
* orp
, uint32_t val
)
343 orp
= obj_check(orp
, 4);
344 orp
->buf
[orp
->used
] = val
;
345 orp
->buf
[orp
->used
+ 1] = val
>> 8;
346 orp
->buf
[orp
->used
+ 2] = val
>> 16;
347 orp
->buf
[orp
->used
+ 3] = val
>> 24;
353 * All fields of "size x" in one obj record must be the same size (either 16
354 * bits or 32 bits). There is a one bit flag in each record which specifies
356 * This routine is used to force the current record to have the desired
357 * x_size. x_size is normally automatic (using obj_x), so that this
358 * routine should be used outside obj_x, only to provide compatibility with
359 * linkers that have bugs in their processing of the size bit.
362 static ObjRecord
*obj_force(ObjRecord
* orp
, int x
)
364 if (orp
->x_size
== (x
^ 48))
371 * This routine writes a field of size x. The caller does not need to worry at
372 * all about whether 16-bits or 32-bits are required.
374 static ObjRecord
*obj_x(ObjRecord
* orp
, uint32_t val
)
379 orp
= obj_force(orp
, 32);
380 if (orp
->x_size
== 32) {
381 ObjRecord
*nxt
= obj_dword(orp
, val
);
382 nxt
->x_size
= 32; /* x_size is cleared when a record overflows */
386 return (obj_word(orp
, val
));
392 static ObjRecord
*obj_index(ObjRecord
* orp
, unsigned int val
)
395 return (obj_byte(orp
, val
));
396 return (obj_word(orp
, (val
>> 8) | (val
<< 8) | 0x80));
400 * Writes a variable length value
402 static ObjRecord
*obj_value(ObjRecord
* orp
, uint32_t val
)
405 return (obj_byte(orp
, val
));
407 orp
= obj_byte(orp
, 129);
408 return (obj_word(orp
, val
));
411 return (obj_dword(orp
, (val
<< 8) + 132));
412 orp
= obj_byte(orp
, 136);
413 return (obj_dword(orp
, val
));
417 * Writes a counted string
419 static ObjRecord
*obj_name(ObjRecord
* orp
, const char *name
)
421 int len
= strlen(name
);
424 orp
= obj_check(orp
, len
+ 1);
425 ptr
= orp
->buf
+ orp
->used
;
427 orp
->used
+= len
+ 1;
430 *ptr
++ = toupper(*name
);
433 memcpy(ptr
, name
, len
);
438 * Initializer for an LEDATA record.
440 * parm[1] = segment index
441 * During the use of a LEDATA ObjRecord, parm[0] is constantly updated to
442 * represent the offset that would be required if the record were split at the
444 * parm[2] is a copy of parm[0] as it was when the current record was initted.
446 static void ori_ledata(ObjRecord
* orp
)
448 obj_index(orp
, orp
->parm
[1]);
449 orp
->parm
[2] = orp
->parm
[0];
450 obj_x(orp
, orp
->parm
[0]);
454 * Initializer for a PUBDEF record.
455 * parm[0] = group index
456 * parm[1] = segment index
457 * parm[2] = frame (only used when both indexes are zero)
459 static void ori_pubdef(ObjRecord
* orp
)
461 obj_index(orp
, orp
->parm
[0]);
462 obj_index(orp
, orp
->parm
[1]);
463 if (!(orp
->parm
[0] | orp
->parm
[1]))
464 obj_word(orp
, orp
->parm
[2]);
468 * Initializer for a LINNUM record.
469 * parm[0] = group index
470 * parm[1] = segment index
472 static void ori_linnum(ObjRecord
* orp
)
474 obj_index(orp
, orp
->parm
[0]);
475 obj_index(orp
, orp
->parm
[1]);
479 * Initializer for a local vars record.
481 static void ori_local(ObjRecord
* orp
)
488 * Null initializer for records that continue without any header info
490 static void ori_null(ObjRecord
* orp
)
492 (void)orp
; /* Do nothing */
496 * This concludes the low level section of outobj.c
499 static char obj_infile
[FILENAME_MAX
];
501 static int32_t first_seg
;
502 static bool any_segs
;
506 #define GROUP_MAX 256 /* we won't _realistically_ have more
507 * than this many segs in a group */
508 #define EXT_BLKSIZ 256 /* block size for externals list */
510 struct Segment
; /* need to know these structs exist */
514 struct LineNumber
*next
;
515 struct Segment
*segment
;
520 static struct FileName
{
521 struct FileName
*next
;
523 struct LineNumber
*lnhead
, **lntail
;
527 static struct Array
{
531 } *arrhead
, **arrtail
;
533 #define ARRAYBOT 31 /* magic number for first array index */
535 static struct Public
{
539 int32_t segment
; /* only if it's far-absolute */
540 int type
; /* only for local debug syms */
541 } *fpubhead
, **fpubtail
, *last_defined
;
543 static struct External
{
544 struct External
*next
;
547 int32_t commonelem
; /* element size if FAR, else zero */
548 int index
; /* OBJ-file external index */
550 DEFWRT_NONE
, /* no unusual default-WRT */
551 DEFWRT_STRING
, /* a string we don't yet understand */
552 DEFWRT_SEGMENT
, /* a segment */
553 DEFWRT_GROUP
/* a group */
560 struct External
*next_dws
; /* next with DEFWRT_STRING */
561 } *exthead
, **exttail
, *dws
;
563 static int externals
;
565 static struct ExtBack
{
566 struct ExtBack
*next
;
567 struct External
*exts
[EXT_BLKSIZ
];
570 static struct Segment
{
571 struct Segment
*next
;
572 int32_t index
; /* the NASM segment id */
573 int32_t obj_index
; /* the OBJ-file segment index */
574 struct Group
*grp
; /* the group it beint32_ts to */
576 int32_t align
; /* can be SEG_ABS + absolute addr */
583 bool use32
; /* is this segment 32-bit? */
584 struct Public
*pubhead
, **pubtail
, *lochead
, **loctail
;
586 char *segclass
, *overlay
; /* `class' is a C++ keyword :-) */
588 } *seghead
, **segtail
, *obj_seg_needs_update
;
590 static struct Group
{
593 int32_t index
; /* NASM segment id */
594 int32_t obj_index
; /* OBJ-file group index */
595 int32_t nentries
; /* number of elements... */
596 int32_t nindices
; /* ...and number of index elts... */
600 } segs
[GROUP_MAX
]; /* ...in this */
601 } *grphead
, **grptail
, *obj_grp_needs_update
;
603 static struct ImpDef
{
607 unsigned int impindex
;
609 } *imphead
, **imptail
;
611 static struct ExpDef
{
615 unsigned int ordinal
;
617 } *exphead
, **exptail
;
619 #define EXPDEF_FLAG_ORDINAL 0x80
620 #define EXPDEF_FLAG_RESIDENT 0x40
621 #define EXPDEF_FLAG_NODATA 0x20
622 #define EXPDEF_MASK_PARMCNT 0x1F
624 static int32_t obj_entry_seg
, obj_entry_ofs
;
628 /* The current segment */
629 static struct Segment
*current_seg
;
631 static int32_t obj_segment(char *, int, int *);
632 static void obj_write_file(int debuginfo
);
633 static int obj_directive(enum directives
, char *, int);
635 static void obj_init(void)
637 first_seg
= seg_alloc();
640 fpubtail
= &fpubhead
;
651 seghead
= obj_seg_needs_update
= NULL
;
653 grphead
= obj_grp_needs_update
= NULL
;
655 obj_entry_seg
= NO_SEG
;
656 obj_uppercase
= false;
662 static int obj_set_info(enum geninfo type
, char **val
)
669 static void obj_cleanup(int debuginfo
)
671 obj_write_file(debuginfo
);
672 of_obj
.current_dfmt
->cleanup();
674 struct Segment
*segtmp
= seghead
;
675 seghead
= seghead
->next
;
676 while (segtmp
->pubhead
) {
677 struct Public
*pubtmp
= segtmp
->pubhead
;
678 segtmp
->pubhead
= pubtmp
->next
;
679 nasm_free(pubtmp
->name
);
682 nasm_free(segtmp
->segclass
);
683 nasm_free(segtmp
->overlay
);
687 struct Public
*pubtmp
= fpubhead
;
688 fpubhead
= fpubhead
->next
;
689 nasm_free(pubtmp
->name
);
693 struct External
*exttmp
= exthead
;
694 exthead
= exthead
->next
;
698 struct ImpDef
*imptmp
= imphead
;
699 imphead
= imphead
->next
;
700 nasm_free(imptmp
->extname
);
701 nasm_free(imptmp
->libname
);
702 nasm_free(imptmp
->impname
); /* nasm_free won't mind if it's NULL */
706 struct ExpDef
*exptmp
= exphead
;
707 exphead
= exphead
->next
;
708 nasm_free(exptmp
->extname
);
709 nasm_free(exptmp
->intname
);
713 struct ExtBack
*ebtmp
= ebhead
;
714 ebhead
= ebhead
->next
;
718 struct Group
*grptmp
= grphead
;
719 grphead
= grphead
->next
;
724 static void obj_ext_set_defwrt(struct External
*ext
, char *id
)
729 for (seg
= seghead
; seg
; seg
= seg
->next
)
730 if (!strcmp(seg
->name
, id
)) {
731 ext
->defwrt_type
= DEFWRT_SEGMENT
;
732 ext
->defwrt_ptr
.seg
= seg
;
737 for (grp
= grphead
; grp
; grp
= grp
->next
)
738 if (!strcmp(grp
->name
, id
)) {
739 ext
->defwrt_type
= DEFWRT_GROUP
;
740 ext
->defwrt_ptr
.grp
= grp
;
745 ext
->defwrt_type
= DEFWRT_STRING
;
746 ext
->defwrt_ptr
.string
= id
;
751 static void obj_deflabel(char *name
, int32_t segment
,
752 int64_t offset
, int is_global
, char *special
)
755 * We have three cases:
757 * (i) `segment' is a segment-base. If so, set the name field
758 * for the segment or group structure it refers to, and then
761 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
762 * Save the label position for later output of a PUBDEF record.
763 * (Or a MODPUB, if we work out how.)
765 * (iii) `segment' is not one of our segments. Save the label
766 * position for later output of an EXTDEF, and also store a
767 * back-reference so that we can map later references to this
768 * segment number to the external index.
770 struct External
*ext
;
774 bool used_special
= false; /* have we used the special text? */
776 #if defined(DEBUG) && DEBUG>2
777 nasm_error(ERR_DEBUG
,
778 " obj_deflabel: %s, seg=%"PRIx32
", off=%"PRIx64
", is_global=%d, %s\n",
779 name
, segment
, offset
, is_global
, special
);
783 * If it's a special-retry from pass two, discard it.
789 * First check for the double-period, signifying something
792 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
793 if (!strcmp(name
, "..start")) {
794 obj_entry_seg
= segment
;
795 obj_entry_ofs
= offset
;
798 nasm_error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
804 if (obj_seg_needs_update
) {
805 obj_seg_needs_update
->name
= name
;
807 } else if (obj_grp_needs_update
) {
808 obj_grp_needs_update
->name
= name
;
811 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
814 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
816 * SEG_ABS subcase of (ii).
821 pub
= *fpubtail
= nasm_malloc(sizeof(*pub
));
822 fpubtail
= &pub
->next
;
824 pub
->name
= nasm_strdup(name
);
825 pub
->offset
= offset
;
826 pub
->segment
= (segment
== NO_SEG
? 0 : segment
& ~SEG_ABS
);
829 nasm_error(ERR_NONFATAL
, "OBJ supports no special symbol features"
830 " for this symbol type");
835 * If `any_segs' is still false, we might need to define a
836 * default segment, if they're trying to declare a label in
839 if (!any_segs
&& segment
== first_seg
) {
840 int tempint
; /* ignored */
841 if (segment
!= obj_segment("__NASMDEFSEG", 2, &tempint
))
842 nasm_error(ERR_PANIC
, "strange segment conditions in OBJ driver");
845 for (seg
= seghead
; seg
&& is_global
; seg
= seg
->next
)
846 if (seg
->index
== segment
) {
847 struct Public
*loc
= nasm_malloc(sizeof(*loc
));
849 * Case (ii). Maybe MODPUB someday?
852 seg
->pubtail
= &loc
->next
;
854 loc
->name
= nasm_strdup(name
);
855 loc
->offset
= offset
;
858 nasm_error(ERR_NONFATAL
,
859 "OBJ supports no special symbol features"
860 " for this symbol type");
868 ext
= *exttail
= nasm_malloc(sizeof(*ext
));
870 exttail
= &ext
->next
;
872 /* Place by default all externs into the current segment */
873 ext
->defwrt_type
= DEFWRT_NONE
;
875 /* 28-Apr-2002 - John Coffman
876 The following code was introduced on 12-Aug-2000, and breaks fixups
877 on code passed thru the MSC 5.1 linker (3.66) and MSC 6.00A linker
878 (5.10). It was introduced after FIXUP32 was added, and may be needed
879 for 32-bit segments. The following will get 16-bit segments working
880 again, and maybe someone can correct the 'if' condition which is
886 if (current_seg
&& current_seg
->use32
) {
887 if (current_seg
->grp
) {
888 ext
->defwrt_type
= DEFWRT_GROUP
;
889 ext
->defwrt_ptr
.grp
= current_seg
->grp
;
891 ext
->defwrt_type
= DEFWRT_SEGMENT
;
892 ext
->defwrt_ptr
.seg
= current_seg
;
897 if (is_global
== 2) {
898 ext
->commonsize
= offset
;
899 ext
->commonelem
= 1; /* default FAR */
906 * Now process the special text, if any, to find default-WRT
907 * specifications and common-variable element-size and near/far
910 while (special
&& *special
) {
914 * We might have a default-WRT specification.
916 if (!nasm_strnicmp(special
, "wrt", 3)) {
920 special
+= strspn(special
, " \t");
921 p
= nasm_strndup(special
, len
= strcspn(special
, ":"));
922 obj_ext_set_defwrt(ext
, p
);
924 if (*special
&& *special
!= ':')
925 nasm_error(ERR_NONFATAL
, "`:' expected in special symbol"
926 " text for `%s'", ext
->name
);
927 else if (*special
== ':')
932 * The NEAR or FAR keywords specify nearness or
933 * farness. FAR gives default element size 1.
935 if (!nasm_strnicmp(special
, "far", 3)) {
939 nasm_error(ERR_NONFATAL
,
940 "`%s': `far' keyword may only be applied"
941 " to common variables\n", ext
->name
);
943 special
+= strspn(special
, " \t");
944 } else if (!nasm_strnicmp(special
, "near", 4)) {
948 nasm_error(ERR_NONFATAL
,
949 "`%s': `far' keyword may only be applied"
950 " to common variables\n", ext
->name
);
952 special
+= strspn(special
, " \t");
956 * If it's a common, and anything else remains on the line
957 * before a further colon, evaluate it as an expression and
958 * use that as the element size. Forward references aren't
964 if (ext
->commonsize
) {
966 struct tokenval tokval
;
969 stdscan_bufptr
= special
;
970 tokval
.t_type
= TOKEN_INVALID
;
971 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, nasm_error
, NULL
);
974 nasm_error(ERR_NONFATAL
, "cannot use relocatable"
975 " expression as common-variable element size");
977 ext
->commonelem
= reloc_value(e
);
979 special
= stdscan_bufptr
;
981 nasm_error(ERR_NONFATAL
,
982 "`%s': element-size specifications only"
983 " apply to common variables", ext
->name
);
984 while (*special
&& *special
!= ':')
995 eb
= *ebtail
= nasm_malloc(sizeof(*eb
));
999 while (i
>= EXT_BLKSIZ
) {
1003 eb
= *ebtail
= nasm_malloc(sizeof(*eb
));
1010 ext
->index
= ++externals
;
1012 if (special
&& !used_special
)
1013 nasm_error(ERR_NONFATAL
, "OBJ supports no special symbol features"
1014 " for this symbol type");
1017 /* forward declaration */
1018 static void obj_write_fixup(ObjRecord
* orp
, int bytes
,
1019 int segrel
, int32_t seg
, int32_t wrt
,
1020 struct Segment
*segto
);
1022 static void obj_out(int32_t segto
, const void *data
,
1023 enum out_type type
, uint64_t size
,
1024 int32_t segment
, int32_t wrt
)
1026 const uint8_t *ucdata
;
1028 struct Segment
*seg
;
1032 * handle absolute-assembly (structure definitions)
1034 if (segto
== NO_SEG
) {
1035 if (type
!= OUT_RESERVE
)
1036 nasm_error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
1042 * If `any_segs' is still false, we must define a default
1046 int tempint
; /* ignored */
1047 if (segto
!= obj_segment("__NASMDEFSEG", 2, &tempint
))
1048 nasm_error(ERR_PANIC
, "strange segment conditions in OBJ driver");
1052 * Find the segment we are targetting.
1054 for (seg
= seghead
; seg
; seg
= seg
->next
)
1055 if (seg
->index
== segto
)
1058 nasm_error(ERR_PANIC
, "code directed to nonexistent segment?");
1061 orp
->parm
[0] = seg
->currentpos
;
1063 if (type
== OUT_RAWDATA
) {
1067 orp
= obj_check(seg
->orp
, 1);
1068 len
= RECORD_MAX
- orp
->used
;
1071 memcpy(orp
->buf
+ orp
->used
, ucdata
, len
);
1072 orp
->committed
= orp
->used
+= len
;
1073 orp
->parm
[0] = seg
->currentpos
+= len
;
1077 } else if (type
== OUT_ADDRESS
|| type
== OUT_REL2ADR
||
1078 type
== OUT_REL4ADR
) {
1081 if (segment
== NO_SEG
&& type
!= OUT_ADDRESS
)
1082 nasm_error(ERR_NONFATAL
, "relative call to absolute address not"
1083 " supported by OBJ format");
1084 if (segment
>= SEG_ABS
)
1085 nasm_error(ERR_NONFATAL
, "far-absolute relocations not supported"
1087 ldata
= *(int64_t *)data
;
1088 if (type
== OUT_REL2ADR
) {
1089 ldata
+= (size
- 2);
1091 } else if (type
== OUT_REL4ADR
) {
1092 ldata
+= (size
- 4);
1096 orp
= obj_word(orp
, ldata
);
1098 orp
= obj_dword(orp
, ldata
);
1100 if (segment
< SEG_ABS
&& (segment
!= NO_SEG
&& segment
% 2) &&
1103 * This is a 4-byte segment-base relocation such as
1104 * `MOV EAX,SEG foo'. OBJ format can't actually handle
1105 * these, but if the constant term has the 16 low bits
1106 * zero, we can just apply a 2-byte segment-base
1107 * relocation to the low word instead.
1111 nasm_error(ERR_NONFATAL
, "OBJ format cannot handle complex"
1112 " dword-size segment base references");
1114 if (segment
!= NO_SEG
)
1115 obj_write_fixup(orp
, rsize
,
1116 (type
== OUT_ADDRESS
? 0x4000 : 0),
1118 seg
->currentpos
+= size
;
1119 } else if (type
== OUT_RESERVE
) {
1121 orp
= obj_bump(orp
);
1122 seg
->currentpos
+= size
;
1127 static void obj_write_fixup(ObjRecord
* orp
, int bytes
,
1128 int segrel
, int32_t seg
, int32_t wrt
,
1129 struct Segment
*segto
)
1135 struct Segment
*s
= NULL
;
1136 struct Group
*g
= NULL
;
1137 struct External
*e
= NULL
;
1141 nasm_error(ERR_NONFATAL
, "`obj' output driver does not support"
1142 " one-byte relocations");
1148 orp
->child
= forp
= obj_new();
1149 forp
->up
= &(orp
->child
);
1150 /* We should choose between FIXUPP and FIXU32 record type */
1151 /* If we're targeting a 32-bit segment, use a FIXU32 record */
1153 forp
->type
= FIXU32
;
1155 forp
->type
= FIXUPP
;
1160 locat
= FIX_16_SELECTOR
;
1163 nasm_error(ERR_PANIC
, "OBJ: 4-byte segment base fixup got"
1164 " through sanity check");
1167 locat
= (bytes
== 2) ? FIX_16_OFFSET
: FIX_32_OFFSET
;
1170 * There is a bug in tlink that makes it process self relative
1171 * fixups incorrectly if the x_size doesn't match the location
1174 forp
= obj_force(forp
, bytes
<< 3);
1177 forp
= obj_rword(forp
, locat
| segrel
| (orp
->parm
[0] - orp
->parm
[2]));
1179 tidx
= fidx
= -1, method
= 0; /* placate optimisers */
1182 * See if we can find the segment ID in our segment list. If
1183 * so, we have a T4 (LSEG) target.
1185 for (s
= seghead
; s
; s
= s
->next
)
1186 if (s
->index
== seg
)
1189 method
= 4, tidx
= s
->obj_index
;
1191 for (g
= grphead
; g
; g
= g
->next
)
1192 if (g
->index
== seg
)
1195 method
= 5, tidx
= g
->obj_index
;
1197 int32_t i
= seg
/ 2;
1198 struct ExtBack
*eb
= ebhead
;
1199 while (i
>= EXT_BLKSIZ
) {
1207 method
= 6, e
= eb
->exts
[i
], tidx
= e
->index
;
1209 nasm_error(ERR_PANIC
,
1210 "unrecognised segment value in obj_write_fixup");
1215 * If no WRT given, assume the natural default, which is method
1218 * - we are doing an OFFSET fixup for a grouped segment, in
1219 * which case we require F1 (group).
1221 * - we are doing an OFFSET fixup for an external with a
1222 * default WRT, in which case we must honour the default WRT.
1224 if (wrt
== NO_SEG
) {
1225 if (!base
&& s
&& s
->grp
)
1226 method
|= 0x10, fidx
= s
->grp
->obj_index
;
1227 else if (!base
&& e
&& e
->defwrt_type
!= DEFWRT_NONE
) {
1228 if (e
->defwrt_type
== DEFWRT_SEGMENT
)
1229 method
|= 0x00, fidx
= e
->defwrt_ptr
.seg
->obj_index
;
1230 else if (e
->defwrt_type
== DEFWRT_GROUP
)
1231 method
|= 0x10, fidx
= e
->defwrt_ptr
.grp
->obj_index
;
1233 nasm_error(ERR_NONFATAL
, "default WRT specification for"
1234 " external `%s' unresolved", e
->name
);
1235 method
|= 0x50, fidx
= -1; /* got to do _something_ */
1238 method
|= 0x50, fidx
= -1;
1241 * See if we can find the WRT-segment ID in our segment
1242 * list. If so, we have a F0 (LSEG) frame.
1244 for (s
= seghead
; s
; s
= s
->next
)
1245 if (s
->index
== wrt
- 1)
1248 method
|= 0x00, fidx
= s
->obj_index
;
1250 for (g
= grphead
; g
; g
= g
->next
)
1251 if (g
->index
== wrt
- 1)
1254 method
|= 0x10, fidx
= g
->obj_index
;
1256 int32_t i
= wrt
/ 2;
1257 struct ExtBack
*eb
= ebhead
;
1258 while (i
>= EXT_BLKSIZ
) {
1266 method
|= 0x20, fidx
= eb
->exts
[i
]->index
;
1268 nasm_error(ERR_PANIC
,
1269 "unrecognised WRT value in obj_write_fixup");
1274 forp
= obj_byte(forp
, method
);
1276 forp
= obj_index(forp
, fidx
);
1277 forp
= obj_index(forp
, tidx
);
1281 static int32_t obj_segment(char *name
, int pass
, int *bits
)
1284 * We call the label manager here to define a name for the new
1285 * segment, and when our _own_ label-definition stub gets
1286 * called in return, it should register the new segment name
1287 * using the pointer it gets passed. That way we save memory,
1288 * by sponging off the label manager.
1290 #if defined(DEBUG) && DEBUG>=3
1291 nasm_error(ERR_DEBUG
, " obj_segment: < %s >, pass=%d, *bits=%d\n",
1299 struct Segment
*seg
;
1301 struct External
**extp
;
1302 int obj_idx
, i
, attrs
;
1307 * Look for segment attributes.
1310 while (*name
== '.')
1311 name
++; /* hack, but a documented one */
1313 while (*p
&& !nasm_isspace(*p
))
1317 while (*p
&& nasm_isspace(*p
))
1321 while (*p
&& !nasm_isspace(*p
))
1325 while (*p
&& nasm_isspace(*p
))
1333 for (seg
= seghead
; seg
; seg
= seg
->next
) {
1335 if (!strcmp(seg
->name
, name
)) {
1336 if (attrs
> 0 && pass
== 1)
1337 nasm_error(ERR_WARNING
, "segment attributes specified on"
1338 " redeclaration of segment: ignoring");
1348 *segtail
= seg
= nasm_malloc(sizeof(*seg
));
1350 segtail
= &seg
->next
;
1351 seg
->index
= (any_segs
? seg_alloc() : first_seg
);
1352 seg
->obj_index
= obj_idx
;
1356 seg
->currentpos
= 0;
1357 seg
->align
= 1; /* default */
1358 seg
->use32
= false; /* default */
1359 seg
->combine
= CMB_PUBLIC
; /* default */
1360 seg
->segclass
= seg
->overlay
= NULL
;
1361 seg
->pubhead
= NULL
;
1362 seg
->pubtail
= &seg
->pubhead
;
1363 seg
->lochead
= NULL
;
1364 seg
->loctail
= &seg
->lochead
;
1365 seg
->orp
= obj_new();
1366 seg
->orp
->up
= &(seg
->orp
);
1367 seg
->orp
->ori
= ori_ledata
;
1368 seg
->orp
->type
= LEDATA
;
1369 seg
->orp
->parm
[1] = obj_idx
;
1372 * Process the segment attributes.
1381 * `p' contains a segment attribute.
1383 if (!nasm_stricmp(p
, "private"))
1384 seg
->combine
= CMB_PRIVATE
;
1385 else if (!nasm_stricmp(p
, "public"))
1386 seg
->combine
= CMB_PUBLIC
;
1387 else if (!nasm_stricmp(p
, "common"))
1388 seg
->combine
= CMB_COMMON
;
1389 else if (!nasm_stricmp(p
, "stack"))
1390 seg
->combine
= CMB_STACK
;
1391 else if (!nasm_stricmp(p
, "use16"))
1393 else if (!nasm_stricmp(p
, "use32"))
1395 else if (!nasm_stricmp(p
, "flat")) {
1397 * This segment is an OS/2 FLAT segment. That means
1398 * that its default group is group FLAT, even if
1399 * the group FLAT does not explicitly _contain_ the
1402 * When we see this, we must create the group
1403 * `FLAT', containing no segments, if it does not
1404 * already exist; then we must set the default
1405 * group of this segment to be the FLAT group.
1408 for (grp
= grphead
; grp
; grp
= grp
->next
)
1409 if (!strcmp(grp
->name
, "FLAT"))
1412 obj_directive(D_GROUP
, "FLAT", 1);
1413 for (grp
= grphead
; grp
; grp
= grp
->next
)
1414 if (!strcmp(grp
->name
, "FLAT"))
1417 nasm_error(ERR_PANIC
, "failure to define FLAT?!");
1420 } else if (!nasm_strnicmp(p
, "class=", 6))
1421 seg
->segclass
= nasm_strdup(p
+ 6);
1422 else if (!nasm_strnicmp(p
, "overlay=", 8))
1423 seg
->overlay
= nasm_strdup(p
+ 8);
1424 else if (!nasm_strnicmp(p
, "align=", 6)) {
1425 seg
->align
= readnum(p
+ 6, &rn_error
);
1428 nasm_error(ERR_NONFATAL
, "segment alignment should be"
1431 switch ((int)seg
->align
) {
1436 case 256: /* PAGE */
1437 case 4096: /* PharLap extension */
1440 nasm_error(ERR_WARNING
,
1441 "OBJ format does not support alignment"
1442 " of 8: rounding up to 16");
1448 nasm_error(ERR_WARNING
,
1449 "OBJ format does not support alignment"
1450 " of %d: rounding up to 256", seg
->align
);
1456 nasm_error(ERR_WARNING
,
1457 "OBJ format does not support alignment"
1458 " of %d: rounding up to 4096", seg
->align
);
1462 nasm_error(ERR_NONFATAL
, "invalid alignment value %d",
1467 } else if (!nasm_strnicmp(p
, "absolute=", 9)) {
1468 seg
->align
= SEG_ABS
+ readnum(p
+ 9, &rn_error
);
1470 nasm_error(ERR_NONFATAL
, "argument to `absolute' segment"
1471 " attribute should be numeric");
1475 /* We need to know whenever we have at least one 32-bit segment */
1476 obj_use32
|= seg
->use32
;
1478 obj_seg_needs_update
= seg
;
1479 if (seg
->align
>= SEG_ABS
)
1480 define_label(name
, NO_SEG
, seg
->align
- SEG_ABS
,
1481 NULL
, false, false);
1483 define_label(name
, seg
->index
+ 1, 0L,
1484 NULL
, false, false);
1485 obj_seg_needs_update
= NULL
;
1488 * See if this segment is defined in any groups.
1490 for (grp
= grphead
; grp
; grp
= grp
->next
) {
1491 for (i
= grp
->nindices
; i
< grp
->nentries
; i
++) {
1492 if (!strcmp(grp
->segs
[i
].name
, seg
->name
)) {
1493 nasm_free(grp
->segs
[i
].name
);
1494 grp
->segs
[i
] = grp
->segs
[grp
->nindices
];
1495 grp
->segs
[grp
->nindices
++].index
= seg
->obj_index
;
1497 nasm_error(ERR_WARNING
,
1498 "segment `%s' is already part of"
1499 " a group: first one takes precedence",
1508 * Walk through the list of externals with unresolved
1509 * default-WRT clauses, and resolve any that point at this
1514 if ((*extp
)->defwrt_type
== DEFWRT_STRING
&&
1515 !strcmp((*extp
)->defwrt_ptr
.string
, seg
->name
)) {
1516 nasm_free((*extp
)->defwrt_ptr
.string
);
1517 (*extp
)->defwrt_type
= DEFWRT_SEGMENT
;
1518 (*extp
)->defwrt_ptr
.seg
= seg
;
1519 *extp
= (*extp
)->next_dws
;
1521 extp
= &(*extp
)->next_dws
;
1533 static int obj_directive(enum directives directive
, char *value
, int pass
)
1535 switch (directive
) {
1541 struct Segment
*seg
;
1542 struct External
**extp
;
1547 q
++; /* hack, but a documented one */
1549 while (*q
&& !nasm_isspace(*q
))
1551 if (nasm_isspace(*q
)) {
1553 while (*q
&& nasm_isspace(*q
))
1557 * Here we used to sanity-check the group directive to
1558 * ensure nobody tried to declare a group containing no
1559 * segments. However, OS/2 does this as standard
1560 * practice, so the sanity check has been removed.
1563 * nasm_error(ERR_NONFATAL,"GROUP directive contains no segments");
1569 for (grp
= grphead
; grp
; grp
= grp
->next
) {
1571 if (!strcmp(grp
->name
, v
)) {
1572 nasm_error(ERR_NONFATAL
, "group `%s' defined twice", v
);
1577 *grptail
= grp
= nasm_malloc(sizeof(*grp
));
1579 grptail
= &grp
->next
;
1580 grp
->index
= seg_alloc();
1581 grp
->obj_index
= obj_idx
;
1582 grp
->nindices
= grp
->nentries
= 0;
1585 obj_grp_needs_update
= grp
;
1586 define_label(v
, grp
->index
+ 1, 0L, NULL
, false, false);
1587 obj_grp_needs_update
= NULL
;
1591 while (*q
&& !nasm_isspace(*q
))
1593 if (nasm_isspace(*q
)) {
1595 while (*q
&& nasm_isspace(*q
))
1599 * Now p contains a segment name. Find it.
1601 for (seg
= seghead
; seg
; seg
= seg
->next
)
1602 if (!strcmp(seg
->name
, p
))
1606 * We have a segment index. Shift a name entry
1607 * to the end of the array to make room.
1609 grp
->segs
[grp
->nentries
++] = grp
->segs
[grp
->nindices
];
1610 grp
->segs
[grp
->nindices
++].index
= seg
->obj_index
;
1612 nasm_error(ERR_WARNING
,
1613 "segment `%s' is already part of"
1614 " a group: first one takes precedence",
1620 * We have an as-yet undefined segment.
1621 * Remember its name, for later.
1623 grp
->segs
[grp
->nentries
++].name
= nasm_strdup(p
);
1628 * Walk through the list of externals with unresolved
1629 * default-WRT clauses, and resolve any that point at
1634 if ((*extp
)->defwrt_type
== DEFWRT_STRING
&&
1635 !strcmp((*extp
)->defwrt_ptr
.string
, grp
->name
)) {
1636 nasm_free((*extp
)->defwrt_ptr
.string
);
1637 (*extp
)->defwrt_type
= DEFWRT_GROUP
;
1638 (*extp
)->defwrt_ptr
.grp
= grp
;
1639 *extp
= (*extp
)->next_dws
;
1641 extp
= &(*extp
)->next_dws
;
1647 obj_uppercase
= true;
1652 char *q
, *extname
, *libname
, *impname
;
1655 return 1; /* ignore in pass two */
1656 extname
= q
= value
;
1657 while (*q
&& !nasm_isspace(*q
))
1659 if (nasm_isspace(*q
)) {
1661 while (*q
&& nasm_isspace(*q
))
1666 while (*q
&& !nasm_isspace(*q
))
1668 if (nasm_isspace(*q
)) {
1670 while (*q
&& nasm_isspace(*q
))
1676 if (!*extname
|| !*libname
)
1677 nasm_error(ERR_NONFATAL
, "`import' directive requires symbol name"
1678 " and library name");
1683 imp
= *imptail
= nasm_malloc(sizeof(struct ImpDef
));
1684 imptail
= &imp
->next
;
1686 imp
->extname
= nasm_strdup(extname
);
1687 imp
->libname
= nasm_strdup(libname
);
1688 imp
->impindex
= readnum(impname
, &err
);
1689 if (!*impname
|| err
)
1690 imp
->impname
= nasm_strdup(impname
);
1692 imp
->impname
= NULL
;
1699 char *q
, *extname
, *intname
, *v
;
1700 struct ExpDef
*export
;
1702 unsigned int ordinal
= 0;
1705 return 1; /* ignore in pass two */
1706 intname
= q
= value
;
1707 while (*q
&& !nasm_isspace(*q
))
1709 if (nasm_isspace(*q
)) {
1711 while (*q
&& nasm_isspace(*q
))
1716 while (*q
&& !nasm_isspace(*q
))
1718 if (nasm_isspace(*q
)) {
1720 while (*q
&& nasm_isspace(*q
))
1725 nasm_error(ERR_NONFATAL
, "`export' directive requires export name");
1734 while (*q
&& !nasm_isspace(*q
))
1736 if (nasm_isspace(*q
)) {
1738 while (*q
&& nasm_isspace(*q
))
1741 if (!nasm_stricmp(v
, "resident"))
1742 flags
|= EXPDEF_FLAG_RESIDENT
;
1743 else if (!nasm_stricmp(v
, "nodata"))
1744 flags
|= EXPDEF_FLAG_NODATA
;
1745 else if (!nasm_strnicmp(v
, "parm=", 5)) {
1747 flags
|= EXPDEF_MASK_PARMCNT
& readnum(v
+ 5, &err
);
1749 nasm_error(ERR_NONFATAL
,
1750 "value `%s' for `parm' is non-numeric", v
+ 5);
1755 ordinal
= readnum(v
, &err
);
1757 nasm_error(ERR_NONFATAL
,
1758 "unrecognised export qualifier `%s'", v
);
1761 flags
|= EXPDEF_FLAG_ORDINAL
;
1765 export
= *exptail
= nasm_malloc(sizeof(struct ExpDef
));
1766 exptail
= &export
->next
;
1767 export
->next
= NULL
;
1768 export
->extname
= nasm_strdup(extname
);
1769 export
->intname
= nasm_strdup(intname
);
1770 export
->ordinal
= ordinal
;
1771 export
->flags
= flags
;
1780 static int32_t obj_segbase(int32_t segment
)
1782 struct Segment
*seg
;
1785 * Find the segment in our list.
1787 for (seg
= seghead
; seg
; seg
= seg
->next
)
1788 if (seg
->index
== segment
- 1)
1793 * Might be an external with a default WRT.
1795 int32_t i
= segment
/ 2;
1796 struct ExtBack
*eb
= ebhead
;
1799 while (i
>= EXT_BLKSIZ
) {
1809 nasm_assert(pass0
== 0);
1810 /* Not available - can happen during optimization */
1814 switch (e
->defwrt_type
) {
1816 return segment
; /* fine */
1817 case DEFWRT_SEGMENT
:
1818 return e
->defwrt_ptr
.seg
->index
+ 1;
1820 return e
->defwrt_ptr
.grp
->index
+ 1;
1822 return NO_SEG
; /* can't tell what it is */
1826 return segment
; /* not one of ours - leave it alone */
1829 if (seg
->align
>= SEG_ABS
)
1830 return seg
->align
; /* absolute segment */
1832 return seg
->grp
->index
+ 1; /* grouped segment */
1834 return segment
; /* no special treatment */
1837 static void obj_filename(char *inname
, char *outname
)
1839 strcpy(obj_infile
, inname
);
1840 standard_extension(inname
, outname
, ".obj");
1843 static void obj_write_file(int debuginfo
)
1845 struct Segment
*seg
, *entry_seg_ptr
= 0;
1846 struct FileName
*fn
;
1847 struct LineNumber
*ln
;
1849 struct Public
*pub
, *loc
;
1850 struct External
*ext
;
1852 struct ExpDef
*export
;
1857 * Write the THEADR module header.
1861 obj_name(orp
, obj_infile
);
1865 * Write the NASM boast comment.
1868 obj_rword(orp
, 0); /* comment type zero */
1869 obj_name(orp
, nasm_comment
);
1874 * Write the IMPDEF records, if any.
1876 for (imp
= imphead
; imp
; imp
= imp
->next
) {
1877 obj_rword(orp
, 0xA0); /* comment class A0 */
1878 obj_byte(orp
, 1); /* subfunction 1: IMPDEF */
1880 obj_byte(orp
, 0); /* import by name */
1882 obj_byte(orp
, 1); /* import by ordinal */
1883 obj_name(orp
, imp
->extname
);
1884 obj_name(orp
, imp
->libname
);
1886 obj_name(orp
, imp
->impname
);
1888 obj_word(orp
, imp
->impindex
);
1893 * Write the EXPDEF records, if any.
1895 for (export
= exphead
; export
; export
= export
->next
) {
1896 obj_rword(orp
, 0xA0); /* comment class A0 */
1897 obj_byte(orp
, 2); /* subfunction 2: EXPDEF */
1898 obj_byte(orp
, export
->flags
);
1899 obj_name(orp
, export
->extname
);
1900 obj_name(orp
, export
->intname
);
1901 if (export
->flags
& EXPDEF_FLAG_ORDINAL
)
1902 obj_word(orp
, export
->ordinal
);
1906 /* we're using extended OMF if we put in debug info */
1909 obj_byte(orp
, 0x40);
1910 obj_byte(orp
, dEXTENDED
);
1915 * Write the first LNAMES record, containing LNAME one, which
1916 * is null. Also initialize the LNAME counter.
1922 * Write some LNAMES for the segment names
1924 for (seg
= seghead
; seg
; seg
= seg
->next
) {
1925 orp
= obj_name(orp
, seg
->name
);
1927 orp
= obj_name(orp
, seg
->segclass
);
1929 orp
= obj_name(orp
, seg
->overlay
);
1933 * Write some LNAMES for the group names
1935 for (grp
= grphead
; grp
; grp
= grp
->next
) {
1936 orp
= obj_name(orp
, grp
->name
);
1942 * Write the SEGDEF records.
1945 for (seg
= seghead
; seg
; seg
= seg
->next
) {
1947 uint32_t seglen
= seg
->currentpos
;
1949 acbp
= (seg
->combine
<< 2); /* C field */
1952 acbp
|= 0x01; /* P bit is Use32 flag */
1953 else if (seglen
== 0x10000L
) {
1954 seglen
= 0; /* This special case may be needed for old linkers */
1955 acbp
|= 0x02; /* B bit */
1959 if (seg
->align
>= SEG_ABS
)
1960 /* acbp |= 0x00 */ ;
1961 else if (seg
->align
>= 4096) {
1962 if (seg
->align
> 4096)
1963 nasm_error(ERR_NONFATAL
, "segment `%s' requires more alignment"
1964 " than OBJ format supports", seg
->name
);
1965 acbp
|= 0xC0; /* PharLap extension */
1966 } else if (seg
->align
>= 256) {
1968 } else if (seg
->align
>= 16) {
1970 } else if (seg
->align
>= 4) {
1972 } else if (seg
->align
>= 2) {
1977 obj_byte(orp
, acbp
);
1978 if (seg
->align
& SEG_ABS
) {
1979 obj_x(orp
, seg
->align
- SEG_ABS
); /* Frame */
1980 obj_byte(orp
, 0); /* Offset */
1983 obj_index(orp
, ++lname_idx
);
1984 obj_index(orp
, seg
->segclass
? ++lname_idx
: 1);
1985 obj_index(orp
, seg
->overlay
? ++lname_idx
: 1);
1990 * Write the GRPDEF records.
1993 for (grp
= grphead
; grp
; grp
= grp
->next
) {
1996 if (grp
->nindices
!= grp
->nentries
) {
1997 for (i
= grp
->nindices
; i
< grp
->nentries
; i
++) {
1998 nasm_error(ERR_NONFATAL
, "group `%s' contains undefined segment"
1999 " `%s'", grp
->name
, grp
->segs
[i
].name
);
2000 nasm_free(grp
->segs
[i
].name
);
2001 grp
->segs
[i
].name
= NULL
;
2004 obj_index(orp
, ++lname_idx
);
2005 for (i
= 0; i
< grp
->nindices
; i
++) {
2006 obj_byte(orp
, 0xFF);
2007 obj_index(orp
, grp
->segs
[i
].index
);
2013 * Write the PUBDEF records: first the ones in the segments,
2014 * then the far-absolutes.
2017 orp
->ori
= ori_pubdef
;
2018 for (seg
= seghead
; seg
; seg
= seg
->next
) {
2019 orp
->parm
[0] = seg
->grp
? seg
->grp
->obj_index
: 0;
2020 orp
->parm
[1] = seg
->obj_index
;
2021 for (pub
= seg
->pubhead
; pub
; pub
= pub
->next
) {
2022 orp
= obj_name(orp
, pub
->name
);
2023 orp
= obj_x(orp
, pub
->offset
);
2024 orp
= obj_byte(orp
, 0); /* type index */
2031 for (pub
= fpubhead
; pub
; pub
= pub
->next
) { /* pub-crawl :-) */
2032 if (orp
->parm
[2] != (uint32_t)pub
->segment
) {
2034 orp
->parm
[2] = pub
->segment
;
2036 orp
= obj_name(orp
, pub
->name
);
2037 orp
= obj_x(orp
, pub
->offset
);
2038 orp
= obj_byte(orp
, 0); /* type index */
2044 * Write the EXTDEF and COMDEF records, in order.
2046 orp
->ori
= ori_null
;
2047 for (ext
= exthead
; ext
; ext
= ext
->next
) {
2048 if (ext
->commonsize
== 0) {
2049 if (orp
->type
!= EXTDEF
) {
2053 orp
= obj_name(orp
, ext
->name
);
2054 orp
= obj_index(orp
, 0);
2056 if (orp
->type
!= COMDEF
) {
2060 orp
= obj_name(orp
, ext
->name
);
2061 orp
= obj_index(orp
, 0);
2062 if (ext
->commonelem
) {
2063 orp
= obj_byte(orp
, 0x61); /* far communal */
2064 orp
= obj_value(orp
, (ext
->commonsize
/ ext
->commonelem
));
2065 orp
= obj_value(orp
, ext
->commonelem
);
2067 orp
= obj_byte(orp
, 0x62); /* near communal */
2068 orp
= obj_value(orp
, ext
->commonsize
);
2076 * Write a COMENT record stating that the linker's first pass
2077 * may stop processing at this point. Exception is if our
2078 * MODEND record specifies a start point, in which case,
2079 * according to some variants of the documentation, this COMENT
2080 * should be omitted. So we'll omit it just in case.
2081 * But, TASM puts it in all the time so if we are using
2082 * TASM debug stuff we are putting it in
2084 if (debuginfo
|| obj_entry_seg
== NO_SEG
) {
2086 obj_byte(orp
, 0x40);
2087 obj_byte(orp
, dLINKPASS
);
2093 * 1) put out the compiler type
2094 * 2) Put out the type info. The only type we are using is near label #19
2098 struct Array
*arrtmp
= arrhead
;
2100 obj_byte(orp
, 0x40);
2101 obj_byte(orp
, dCOMPDEF
);
2106 obj_byte(orp
, 0x40);
2107 obj_byte(orp
, dTYPEDEF
);
2108 obj_word(orp
, 0x18); /* type # for linking */
2109 obj_word(orp
, 6); /* size of type */
2110 obj_byte(orp
, 0x2a); /* absolute type for debugging */
2112 obj_byte(orp
, 0x40);
2113 obj_byte(orp
, dTYPEDEF
);
2114 obj_word(orp
, 0x19); /* type # for linking */
2115 obj_word(orp
, 0); /* size of type */
2116 obj_byte(orp
, 0x24); /* absolute type for debugging */
2117 obj_byte(orp
, 0); /* near/far specifier */
2119 obj_byte(orp
, 0x40);
2120 obj_byte(orp
, dTYPEDEF
);
2121 obj_word(orp
, 0x1A); /* type # for linking */
2122 obj_word(orp
, 0); /* size of type */
2123 obj_byte(orp
, 0x24); /* absolute type for debugging */
2124 obj_byte(orp
, 1); /* near/far specifier */
2126 obj_byte(orp
, 0x40);
2127 obj_byte(orp
, dTYPEDEF
);
2128 obj_word(orp
, 0x1b); /* type # for linking */
2129 obj_word(orp
, 0); /* size of type */
2130 obj_byte(orp
, 0x23); /* absolute type for debugging */
2135 obj_byte(orp
, 0x40);
2136 obj_byte(orp
, dTYPEDEF
);
2137 obj_word(orp
, 0x1c); /* type # for linking */
2138 obj_word(orp
, 0); /* size of type */
2139 obj_byte(orp
, 0x23); /* absolute type for debugging */
2144 obj_byte(orp
, 0x40);
2145 obj_byte(orp
, dTYPEDEF
);
2146 obj_word(orp
, 0x1d); /* type # for linking */
2147 obj_word(orp
, 0); /* size of type */
2148 obj_byte(orp
, 0x23); /* absolute type for debugging */
2153 obj_byte(orp
, 0x40);
2154 obj_byte(orp
, dTYPEDEF
);
2155 obj_word(orp
, 0x1e); /* type # for linking */
2156 obj_word(orp
, 0); /* size of type */
2157 obj_byte(orp
, 0x23); /* absolute type for debugging */
2163 /* put out the array types */
2164 for (i
= ARRAYBOT
; i
< arrindex
; i
++) {
2165 obj_byte(orp
, 0x40);
2166 obj_byte(orp
, dTYPEDEF
);
2167 obj_word(orp
, i
); /* type # for linking */
2168 obj_word(orp
, arrtmp
->size
); /* size of type */
2169 obj_byte(orp
, 0x1A); /* absolute type for debugging (array) */
2170 obj_byte(orp
, arrtmp
->basetype
); /* base type */
2172 arrtmp
= arrtmp
->next
;
2176 * write out line number info with a LINNUM record
2177 * switch records when we switch segments, and output the
2178 * file in a pseudo-TASM fashion. The record switch is naive; that
2179 * is that one file may have many records for the same segment
2180 * if there are lots of segment switches
2182 if (fnhead
&& debuginfo
) {
2183 seg
= fnhead
->lnhead
->segment
;
2185 for (fn
= fnhead
; fn
; fn
= fn
->next
) {
2186 /* write out current file name */
2188 orp
->ori
= ori_null
;
2189 obj_byte(orp
, 0x40);
2190 obj_byte(orp
, dFILNAME
);
2192 obj_name(orp
, fn
->name
);
2196 /* write out line numbers this file */
2199 orp
->ori
= ori_linnum
;
2200 for (ln
= fn
->lnhead
; ln
; ln
= ln
->next
) {
2201 if (seg
!= ln
->segment
) {
2202 /* if we get here have to flush the buffer and start
2203 * a new record for a new segment
2208 orp
->parm
[0] = seg
->grp
? seg
->grp
->obj_index
: 0;
2209 orp
->parm
[1] = seg
->obj_index
;
2210 orp
= obj_word(orp
, ln
->lineno
);
2211 orp
= obj_x(orp
, ln
->offset
);
2218 * we are going to locate the entry point segment now
2219 * rather than wait until the MODEND record, because,
2220 * then we can output a special symbol to tell where the
2224 if (obj_entry_seg
!= NO_SEG
) {
2225 for (seg
= seghead
; seg
; seg
= seg
->next
) {
2226 if (seg
->index
== obj_entry_seg
) {
2227 entry_seg_ptr
= seg
;
2232 nasm_error(ERR_NONFATAL
, "entry point is not in this module");
2236 * get ready to put out symbol records
2239 orp
->ori
= ori_local
;
2242 * put out a symbol for the entry point
2243 * no dots in this symbol, because, borland does
2244 * not (officially) support dots in label names
2245 * and I don't know what various versions of TLINK will do
2247 if (debuginfo
&& obj_entry_seg
!= NO_SEG
) {
2248 orp
= obj_name(orp
, "start_of_program");
2249 orp
= obj_word(orp
, 0x19); /* type: near label */
2250 orp
= obj_index(orp
, seg
->grp
? seg
->grp
->obj_index
: 0);
2251 orp
= obj_index(orp
, seg
->obj_index
);
2252 orp
= obj_x(orp
, obj_entry_ofs
);
2257 * put out the local labels
2259 for (seg
= seghead
; seg
&& debuginfo
; seg
= seg
->next
) {
2260 /* labels this seg */
2261 for (loc
= seg
->lochead
; loc
; loc
= loc
->next
) {
2262 orp
= obj_name(orp
, loc
->name
);
2263 orp
= obj_word(orp
, loc
->type
);
2264 orp
= obj_index(orp
, seg
->grp
? seg
->grp
->obj_index
: 0);
2265 orp
= obj_index(orp
, seg
->obj_index
);
2266 orp
= obj_x(orp
, loc
->offset
);
2274 * Write the LEDATA/FIXUPP pairs.
2276 for (seg
= seghead
; seg
; seg
= seg
->next
) {
2278 nasm_free(seg
->orp
);
2282 * Write the MODEND module end marker.
2284 orp
->type
= obj_use32
? MODE32
: MODEND
;
2285 orp
->ori
= ori_null
;
2286 if (entry_seg_ptr
) {
2287 orp
->type
= entry_seg_ptr
->use32
? MODE32
: MODEND
;
2288 obj_byte(orp
, 0xC1);
2289 seg
= entry_seg_ptr
;
2291 obj_byte(orp
, 0x10);
2292 obj_index(orp
, seg
->grp
->obj_index
);
2295 * the below changed to prevent TLINK crashing.
2296 * Previous more efficient version read:
2298 * obj_byte (orp, 0x50);
2300 obj_byte(orp
, 0x00);
2301 obj_index(orp
, seg
->obj_index
);
2303 obj_index(orp
, seg
->obj_index
);
2304 obj_x(orp
, obj_entry_ofs
);
2311 static void obj_fwrite(ObjRecord
* orp
)
2313 unsigned int cksum
, len
;
2317 if (orp
->x_size
== 32)
2319 fputc(cksum
, ofile
);
2320 len
= orp
->committed
+ 1;
2321 cksum
+= (len
& 0xFF) + ((len
>> 8) & 0xFF);
2322 fwriteint16_t(len
, ofile
);
2323 fwrite(orp
->buf
, 1, len
- 1, ofile
);
2324 for (ptr
= orp
->buf
; --len
; ptr
++)
2326 fputc((-cksum
) & 0xFF, ofile
);
2329 extern macros_t obj_stdmac
[];
2331 void dbgbi_init(void)
2335 arrindex
= ARRAYBOT
;
2339 static void dbgbi_cleanup(void)
2341 struct Segment
*segtmp
;
2343 struct FileName
*fntemp
= fnhead
;
2344 while (fnhead
->lnhead
) {
2345 struct LineNumber
*lntemp
= fnhead
->lnhead
;
2346 fnhead
->lnhead
= lntemp
->next
;
2349 fnhead
= fnhead
->next
;
2350 nasm_free(fntemp
->name
);
2353 for (segtmp
= seghead
; segtmp
; segtmp
= segtmp
->next
) {
2354 while (segtmp
->lochead
) {
2355 struct Public
*loctmp
= segtmp
->lochead
;
2356 segtmp
->lochead
= loctmp
->next
;
2357 nasm_free(loctmp
->name
);
2362 struct Array
*arrtmp
= arrhead
;
2363 arrhead
= arrhead
->next
;
2368 static void dbgbi_linnum(const char *lnfname
, int32_t lineno
, int32_t segto
)
2370 struct FileName
*fn
;
2371 struct LineNumber
*ln
;
2372 struct Segment
*seg
;
2374 if (segto
== NO_SEG
)
2378 * If `any_segs' is still false, we must define a default
2382 int tempint
; /* ignored */
2383 if (segto
!= obj_segment("__NASMDEFSEG", 2, &tempint
))
2384 nasm_error(ERR_PANIC
, "strange segment conditions in OBJ driver");
2388 * Find the segment we are targetting.
2390 for (seg
= seghead
; seg
; seg
= seg
->next
)
2391 if (seg
->index
== segto
)
2394 nasm_error(ERR_PANIC
, "lineno directed to nonexistent segment?");
2396 /* for (fn = fnhead; fn; fn = fnhead->next) */
2397 for (fn
= fnhead
; fn
; fn
= fn
->next
) /* fbk - Austin Lunnen - John Fine */
2398 if (!nasm_stricmp(lnfname
, fn
->name
))
2401 fn
= nasm_malloc(sizeof(*fn
));
2402 fn
->name
= nasm_malloc(strlen(lnfname
) + 1);
2403 strcpy(fn
->name
, lnfname
);
2405 fn
->lntail
= &fn
->lnhead
;
2410 ln
= nasm_malloc(sizeof(*ln
));
2412 ln
->offset
= seg
->currentpos
;
2413 ln
->lineno
= lineno
;
2416 fn
->lntail
= &ln
->next
;
2419 static void dbgbi_deflabel(char *name
, int32_t segment
,
2420 int64_t offset
, int is_global
, char *special
)
2422 struct Segment
*seg
;
2427 * If it's a special-retry from pass two, discard it.
2433 * First check for the double-period, signifying something
2436 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
2443 if (obj_seg_needs_update
) {
2445 } else if (obj_grp_needs_update
) {
2448 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
2451 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
2456 * If `any_segs' is still false, we might need to define a
2457 * default segment, if they're trying to declare a label in
2458 * `first_seg'. But the label should exist due to a prior
2459 * call to obj_deflabel so we can skip that.
2462 for (seg
= seghead
; seg
; seg
= seg
->next
)
2463 if (seg
->index
== segment
) {
2464 struct Public
*loc
= nasm_malloc(sizeof(*loc
));
2466 * Case (ii). Maybe MODPUB someday?
2468 last_defined
= *seg
->loctail
= loc
;
2469 seg
->loctail
= &loc
->next
;
2471 loc
->name
= nasm_strdup(name
);
2472 loc
->offset
= offset
;
2475 static void dbgbi_typevalue(int32_t type
)
2478 int elem
= TYM_ELEMENTS(type
);
2479 type
= TYM_TYPE(type
);
2486 last_defined
->type
= 8; /* uint8_t */
2490 last_defined
->type
= 10; /* unsigned word */
2494 last_defined
->type
= 12; /* unsigned dword */
2498 last_defined
->type
= 14; /* float */
2502 last_defined
->type
= 15; /* qword */
2506 last_defined
->type
= 16; /* TBYTE */
2510 last_defined
->type
= 0x19; /*label */
2516 struct Array
*arrtmp
= nasm_malloc(sizeof(*arrtmp
));
2517 int vtype
= last_defined
->type
;
2518 arrtmp
->size
= vsize
* elem
;
2519 arrtmp
->basetype
= vtype
;
2520 arrtmp
->next
= NULL
;
2521 last_defined
->type
= arrindex
++;
2523 arrtail
= &(arrtmp
->next
);
2525 last_defined
= NULL
;
2527 static void dbgbi_output(int output_type
, void *param
)
2532 static struct dfmt borland_debug_form
= {
2533 "Borland Debug Records",
2538 null_debug_directive
,
2544 static struct dfmt
*borland_debug_arr
[3] = {
2545 &borland_debug_form
,
2550 struct ofmt of_obj
= {
2551 "MS-DOS 16-bit/32-bit OMF object files",
2555 &borland_debug_form
,