1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 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
60 * outobj.c is divided into two sections. The first section is low level
61 * routines for creating obj records; It has nearly zero NASM specific
62 * code. The second section is high level routines for processing calls and
63 * data structures from the rest of NASM into obj format.
65 * It should be easy (though not zero work) to lift the first section out for
66 * use as an obj file writer for some other assembler or compiler.
70 * These routines are built around the ObjRecord data struture. An ObjRecord
71 * holds an object file record that may be under construction or complete.
73 * A major function of these routines is to support continuation of an obj
74 * record into the next record when the maximum record size is exceeded. The
75 * high level code does not need to worry about where the record breaks occur.
76 * It does need to do some minor extra steps to make the automatic continuation
77 * work. Those steps may be skipped for records where the high level knows no
78 * continuation could be required.
80 * 1) An ObjRecord is allocated and cleared by obj_new, or an existing ObjRecord
81 * is cleared by obj_clear.
83 * 2) The caller should fill in .type.
85 * 3) If the record is continuable and there is processing that must be done at
86 * the start of each record then the caller should fill in .ori with the
87 * address of the record initializer routine.
89 * 4) If the record is continuable and it should be saved (rather than emitted
90 * immediately) as each record is done, the caller should set .up to be a
91 * pointer to a location in which the caller keeps the master pointer to the
92 * ObjRecord. When the record is continued, the obj_bump routine will then
93 * allocate a new ObjRecord structure and update the master pointer.
95 * 5) If the .ori field was used then the caller should fill in the .parm with
96 * any data required by the initializer.
98 * 6) The caller uses the routines: obj_byte, obj_word, obj_rword, obj_dword,
99 * obj_x, obj_index, obj_value and obj_name to fill in the various kinds of
100 * data required for this record.
102 * 7) If the record is continuable, the caller should call obj_commit at each
103 * point where breaking the record is permitted.
105 * 8) To write out the record, the caller should call obj_emit2. If the
106 * caller has called obj_commit for all data written then he can get slightly
107 * faster code by calling obj_emit instead of obj_emit2.
109 * Most of these routines return an ObjRecord pointer. This will be the input
110 * pointer most of the time and will be the new location if the ObjRecord
111 * moved as a result of the call. The caller may ignore the return value in
112 * three cases: It is a "Never Reallocates" routine; or The caller knows
113 * continuation is not possible; or The caller uses the master pointer for the
117 #define RECORD_MAX (1024-3) /* maximal size of any record except type+reclen */
118 #define OBJ_PARMS 3 /* maximum .parm used by any .ori routine */
120 #define FIX_08_LOW 0x8000 /* location type for various fixup subrecords */
121 #define FIX_16_OFFSET 0x8400
122 #define FIX_16_SELECTOR 0x8800
123 #define FIX_32_POINTER 0x8C00
124 #define FIX_08_HIGH 0x9000
125 #define FIX_32_OFFSET 0xA400
126 #define FIX_48_POINTER 0xAC00
128 enum RecordID
{ /* record ID codes */
130 THEADR
= 0x80, /* module header */
131 COMENT
= 0x88, /* comment record */
133 LINNUM
= 0x94, /* line number record */
134 LNAMES
= 0x96, /* list of names */
136 SEGDEF
= 0x98, /* segment definition */
137 GRPDEF
= 0x9A, /* group definition */
138 EXTDEF
= 0x8C, /* external definition */
139 PUBDEF
= 0x90, /* public definition */
140 COMDEF
= 0xB0, /* common definition */
142 LEDATA
= 0xA0, /* logical enumerated data */
143 FIXUPP
= 0x9C, /* fixups (relocations) */
144 FIXU32
= 0x9D, /* 32-bit fixups (relocations) */
146 MODEND
= 0x8A, /* module end */
147 MODE32
= 0x8B /* module end for 32-bit objects */
150 enum ComentID
{ /* ID codes for comment records */
151 dTRANSL
= 0x0000, /* translator comment */
152 dOMFEXT
= 0xC0A0, /* "OMF extension" */
153 dEXTENDED
= 0xC0A1, /* translator-specific extensions */
154 dLINKPASS
= 0x40A2, /* link pass 2 marker */
155 dTYPEDEF
= 0xC0E3, /* define a type */
156 dSYM
= 0xC0E6, /* symbol debug record */
157 dFILNAME
= 0xC0E8, /* file name record */
158 dDEPFILE
= 0xC0E9, /* dependency file */
159 dCOMPDEF
= 0xC0EA /* compiler type info */
162 typedef struct ObjRecord ObjRecord
;
163 typedef void ORI(ObjRecord
* orp
);
166 ORI
*ori
; /* Initialization routine */
167 int used
; /* Current data size */
168 int committed
; /* Data size at last boundary */
169 int x_size
; /* (see obj_x) */
170 unsigned int type
; /* Record type */
171 ObjRecord
*child
; /* Associated record below this one */
172 ObjRecord
**up
; /* Master pointer to this ObjRecord */
173 ObjRecord
*back
; /* Previous part of this record */
174 uint32_t parm
[OBJ_PARMS
]; /* Parameters for ori routine */
175 uint8_t buf
[RECORD_MAX
+ 3];
178 static void obj_fwrite(ObjRecord
* orp
);
179 static void ori_ledata(ObjRecord
* orp
);
180 static void ori_pubdef(ObjRecord
* orp
);
181 static void ori_null(ObjRecord
* orp
);
182 static ObjRecord
*obj_commit(ObjRecord
* orp
);
184 static bool obj_uppercase
; /* Flag: all names in uppercase */
185 static bool obj_use32
; /* Flag: at least one segment is 32-bit */
186 static bool obj_nodepend
; /* Flag: don't emit file dependencies */
189 * Clear an ObjRecord structure. (Never reallocates).
190 * To simplify reuse of ObjRecord's, .type, .ori and .parm are not cleared.
192 static ObjRecord
*obj_clear(ObjRecord
* orp
)
204 * Emit an ObjRecord structure. (Never reallocates).
205 * The record is written out preceeded (recursively) by its previous part (if
206 * any) and followed (recursively) by its child (if any).
207 * The previous part and the child are freed. The main ObjRecord is cleared,
210 static ObjRecord
*obj_emit(ObjRecord
* orp
)
214 nasm_free(orp
->back
);
221 obj_emit(orp
->child
);
222 nasm_free(orp
->child
);
225 return (obj_clear(orp
));
229 * Commit and Emit a record. (Never reallocates).
231 static ObjRecord
*obj_emit2(ObjRecord
* orp
)
234 return (obj_emit(orp
));
238 * Allocate and clear a new ObjRecord; Also sets .ori to ori_null
240 static ObjRecord
*obj_new(void)
244 orp
= obj_clear(nasm_malloc(sizeof(ObjRecord
)));
250 * Advance to the next record because the existing one is full or its x_size
252 * Any uncommited data is moved into the next record.
254 static ObjRecord
*obj_bump(ObjRecord
* orp
)
257 int used
= orp
->used
;
258 int committed
= orp
->committed
;
261 *orp
->up
= nxt
= obj_new();
263 nxt
->type
= orp
->type
;
266 memcpy(nxt
->parm
, orp
->parm
, sizeof(orp
->parm
));
274 nxt
->committed
= nxt
->used
;
275 memcpy(nxt
->buf
+ nxt
->committed
, orp
->buf
+ committed
, used
);
276 nxt
->used
= nxt
->committed
+ used
;
283 * Advance to the next record if necessary to allow the next field to fit.
285 static ObjRecord
*obj_check(ObjRecord
* orp
, int size
)
287 if (orp
->used
+ size
> RECORD_MAX
)
290 if (!orp
->committed
) {
293 orp
->committed
= orp
->used
;
300 * All data written so far is commited to the current record (won't be moved to
301 * the next record in case of continuation).
303 static ObjRecord
*obj_commit(ObjRecord
* orp
)
305 orp
->committed
= orp
->used
;
312 static ObjRecord
*obj_byte(ObjRecord
* orp
, uint8_t val
)
314 orp
= obj_check(orp
, 1);
315 orp
->buf
[orp
->used
] = val
;
323 static ObjRecord
*obj_word(ObjRecord
* orp
, unsigned int val
)
325 orp
= obj_check(orp
, 2);
326 orp
->buf
[orp
->used
] = val
;
327 orp
->buf
[orp
->used
+ 1] = val
>> 8;
333 * Write a reversed word
335 static ObjRecord
*obj_rword(ObjRecord
* orp
, unsigned int val
)
337 orp
= obj_check(orp
, 2);
338 orp
->buf
[orp
->used
] = val
>> 8;
339 orp
->buf
[orp
->used
+ 1] = val
;
347 static ObjRecord
*obj_dword(ObjRecord
* orp
, uint32_t val
)
349 orp
= obj_check(orp
, 4);
350 orp
->buf
[orp
->used
] = val
;
351 orp
->buf
[orp
->used
+ 1] = val
>> 8;
352 orp
->buf
[orp
->used
+ 2] = val
>> 16;
353 orp
->buf
[orp
->used
+ 3] = val
>> 24;
359 * All fields of "size x" in one obj record must be the same size (either 16
360 * bits or 32 bits). There is a one bit flag in each record which specifies
362 * This routine is used to force the current record to have the desired
363 * x_size. x_size is normally automatic (using obj_x), so that this
364 * routine should be used outside obj_x, only to provide compatibility with
365 * linkers that have bugs in their processing of the size bit.
368 static ObjRecord
*obj_force(ObjRecord
* orp
, int x
)
370 if (orp
->x_size
== (x
^ 48))
377 * This routine writes a field of size x. The caller does not need to worry at
378 * all about whether 16-bits or 32-bits are required.
380 static ObjRecord
*obj_x(ObjRecord
* orp
, uint32_t val
)
385 orp
= obj_force(orp
, 32);
386 if (orp
->x_size
== 32) {
387 ObjRecord
*nxt
= obj_dword(orp
, val
);
388 nxt
->x_size
= 32; /* x_size is cleared when a record overflows */
392 return (obj_word(orp
, val
));
398 static ObjRecord
*obj_index(ObjRecord
* orp
, unsigned int val
)
401 return (obj_byte(orp
, val
));
402 return (obj_word(orp
, (val
>> 8) | (val
<< 8) | 0x80));
406 * Writes a variable length value
408 static ObjRecord
*obj_value(ObjRecord
* orp
, uint32_t val
)
411 return (obj_byte(orp
, val
));
413 orp
= obj_byte(orp
, 129);
414 return (obj_word(orp
, val
));
417 return (obj_dword(orp
, (val
<< 8) + 132));
418 orp
= obj_byte(orp
, 136);
419 return (obj_dword(orp
, val
));
423 * Writes a counted string
425 static ObjRecord
*obj_name(ObjRecord
* orp
, const char *name
)
427 int len
= strlen(name
);
430 orp
= obj_check(orp
, len
+ 1);
431 ptr
= orp
->buf
+ orp
->used
;
433 orp
->used
+= len
+ 1;
436 *ptr
++ = toupper(*name
);
439 memcpy(ptr
, name
, len
);
444 * Initializer for an LEDATA record.
446 * parm[1] = segment index
447 * During the use of a LEDATA ObjRecord, parm[0] is constantly updated to
448 * represent the offset that would be required if the record were split at the
450 * parm[2] is a copy of parm[0] as it was when the current record was initted.
452 static void ori_ledata(ObjRecord
* orp
)
454 obj_index(orp
, orp
->parm
[1]);
455 orp
->parm
[2] = orp
->parm
[0];
456 obj_x(orp
, orp
->parm
[0]);
460 * Initializer for a PUBDEF record.
461 * parm[0] = group index
462 * parm[1] = segment index
463 * parm[2] = frame (only used when both indexes are zero)
465 static void ori_pubdef(ObjRecord
* orp
)
467 obj_index(orp
, orp
->parm
[0]);
468 obj_index(orp
, orp
->parm
[1]);
469 if (!(orp
->parm
[0] | orp
->parm
[1]))
470 obj_word(orp
, orp
->parm
[2]);
474 * Initializer for a LINNUM record.
475 * parm[0] = group index
476 * parm[1] = segment index
478 static void ori_linnum(ObjRecord
* orp
)
480 obj_index(orp
, orp
->parm
[0]);
481 obj_index(orp
, orp
->parm
[1]);
485 * Initializer for a local vars record.
487 static void ori_local(ObjRecord
* orp
)
489 obj_rword(orp
, dSYM
);
493 * Null initializer for records that continue without any header info
495 static void ori_null(ObjRecord
* orp
)
497 (void)orp
; /* Do nothing */
501 * This concludes the low level section of outobj.c
504 static char obj_infile
[FILENAME_MAX
];
506 static int32_t first_seg
;
507 static bool any_segs
;
511 #define GROUP_MAX 256 /* we won't _realistically_ have more
512 * than this many segs in a group */
513 #define EXT_BLKSIZ 256 /* block size for externals list */
515 struct Segment
; /* need to know these structs exist */
519 struct LineNumber
*next
;
520 struct Segment
*segment
;
525 static struct FileName
{
526 struct FileName
*next
;
528 struct LineNumber
*lnhead
, **lntail
;
532 static struct Array
{
536 } *arrhead
, **arrtail
;
538 #define ARRAYBOT 31 /* magic number for first array index */
540 static struct Public
{
544 int32_t segment
; /* only if it's far-absolute */
545 int type
; /* only for local debug syms */
546 } *fpubhead
, **fpubtail
, *last_defined
;
548 static struct External
{
549 struct External
*next
;
552 int32_t commonelem
; /* element size if FAR, else zero */
553 int index
; /* OBJ-file external index */
555 DEFWRT_NONE
, /* no unusual default-WRT */
556 DEFWRT_STRING
, /* a string we don't yet understand */
557 DEFWRT_SEGMENT
, /* a segment */
558 DEFWRT_GROUP
/* a group */
565 struct External
*next_dws
; /* next with DEFWRT_STRING */
566 } *exthead
, **exttail
, *dws
;
568 static int externals
;
570 static struct ExtBack
{
571 struct ExtBack
*next
;
572 struct External
*exts
[EXT_BLKSIZ
];
575 static struct Segment
{
576 struct Segment
*next
;
578 int32_t index
; /* the NASM segment id */
579 int32_t obj_index
; /* the OBJ-file segment index */
580 struct Group
*grp
; /* the group it beint32_ts to */
582 int32_t align
; /* can be SEG_ABS + absolute addr */
583 struct Public
*pubhead
, **pubtail
, *lochead
, **loctail
;
584 char *segclass
, *overlay
; /* `class' is a C++ keyword :-) */
592 bool use32
; /* is this segment 32-bit? */
593 } *seghead
, **segtail
, *obj_seg_needs_update
;
595 static struct Group
{
598 int32_t index
; /* NASM segment id */
599 int32_t obj_index
; /* OBJ-file group index */
600 int32_t nentries
; /* number of elements... */
601 int32_t nindices
; /* ...and number of index elts... */
605 } segs
[GROUP_MAX
]; /* ...in this */
606 } *grphead
, **grptail
, *obj_grp_needs_update
;
608 static struct ImpDef
{
612 unsigned int impindex
;
614 } *imphead
, **imptail
;
616 static struct ExpDef
{
620 unsigned int ordinal
;
622 } *exphead
, **exptail
;
624 #define EXPDEF_FLAG_ORDINAL 0x80
625 #define EXPDEF_FLAG_RESIDENT 0x40
626 #define EXPDEF_FLAG_NODATA 0x20
627 #define EXPDEF_MASK_PARMCNT 0x1F
629 static int32_t obj_entry_seg
, obj_entry_ofs
;
631 const struct ofmt of_obj
;
632 static const struct dfmt borland_debug_form
;
634 /* The current segment */
635 static struct Segment
*current_seg
;
637 static int32_t obj_segment(char *, int, int *);
638 static void obj_write_file(void);
639 static enum directive_result
obj_directive(enum directive
, char *, int);
641 static void obj_init(void)
643 strlcpy(obj_infile
, inname
, sizeof(obj_infile
));
644 first_seg
= seg_alloc();
647 fpubtail
= &fpubhead
;
658 seghead
= obj_seg_needs_update
= NULL
;
660 grphead
= obj_grp_needs_update
= NULL
;
662 obj_entry_seg
= NO_SEG
;
663 obj_uppercase
= false;
669 static void obj_cleanup(void)
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_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_set(special
);
970 tokval
.t_type
= TOKEN_INVALID
;
971 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, 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_get();
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_zalloc(sizeof(*eb
));
999 while (i
>= EXT_BLKSIZ
) {
1003 eb
= *ebtail
= nasm_zalloc(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 * If `any_segs' is still false, we must define a default
1036 int tempint
; /* ignored */
1037 if (segto
!= obj_segment("__NASMDEFSEG", 2, &tempint
))
1038 nasm_panic("strange segment conditions in OBJ driver");
1042 * Find the segment we are targetting.
1044 for (seg
= seghead
; seg
; seg
= seg
->next
)
1045 if (seg
->index
== segto
)
1048 nasm_panic("code directed to nonexistent segment?");
1051 orp
->parm
[0] = seg
->currentpos
;
1058 orp
= obj_check(seg
->orp
, 1);
1059 len
= RECORD_MAX
- orp
->used
;
1062 memcpy(orp
->buf
+ orp
->used
, ucdata
, len
);
1063 orp
->committed
= orp
->used
+= len
;
1064 orp
->parm
[0] = seg
->currentpos
+= len
;
1078 if (type
== OUT_ADDRESS
)
1079 size
= abs((int)size
);
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"
1088 ldata
= *(int64_t *)data
;
1089 if (type
!= OUT_ADDRESS
) {
1091 * For 16-bit and 32-bit x86 code, the size and realsize() always
1092 * matches as only jumps, calls and loops uses PC relative
1093 * addressing and the address isn't followed by any other opcode
1094 * bytes. In 64-bit mode there is RIP relative addressing which
1095 * means the fixup location can be followed by an immediate value,
1096 * meaning that size > realsize().
1098 * When the CPU is calculating the effective address, it takes the
1099 * RIP at the end of the instruction and adds the fixed up relative
1100 * address value to it.
1102 * The linker's point of reference is the end of the fixup location
1103 * (which is the end of the instruction for Jcc, CALL, LOOP[cc]).
1104 * It is calculating distance between the target symbol and the end
1105 * of the fixup location, and add this to the displacement value we
1106 * are calculating here and storing at the fixup location.
1108 * To get the right effect, we need to _reduce_ the displacement
1109 * value by the number of bytes following the fixup.
1112 * data at address 0x100; REL4ADR at 0x050, 4 byte immediate,
1113 * end of fixup at 0x054, end of instruction at 0x058.
1115 * => realsize() -> 4
1116 * => CPU needs a value of: 0x100 - 0x058 = 0x0a8
1117 * => linker/loader will add: 0x100 - 0x054 = 0x0ac
1118 * => We must add an addend of -4.
1119 * => realsize() - size = -4.
1121 * The code used to do size - realsize() at least since v0.90,
1122 * probably because it wasn't needed...
1125 size
= realsize(type
, size
);
1131 nasm_error(ERR_NONFATAL
, "OBJ format can only handle 16- or "
1132 "32-byte relocations");
1133 segment
= NO_SEG
; /* Don't actually generate a relocation */
1136 orp
= obj_word(orp
, ldata
);
1139 orp
= obj_dword(orp
, ldata
);
1144 if (segment
< SEG_ABS
&& (segment
!= NO_SEG
&& segment
% 2) &&
1147 * This is a 4-byte segment-base relocation such as
1148 * `MOV EAX,SEG foo'. OBJ format can't actually handle
1149 * these, but if the constant term has the 16 low bits
1150 * zero, we can just apply a 2-byte segment-base
1151 * relocation to the low word instead.
1155 nasm_error(ERR_NONFATAL
, "OBJ format cannot handle complex"
1156 " dword-size segment base references");
1158 if (segment
!= NO_SEG
)
1159 obj_write_fixup(orp
, rsize
,
1160 (type
== OUT_ADDRESS
? 0x4000 : 0),
1162 seg
->currentpos
+= size
;
1167 nasm_error(ERR_NONFATAL
,
1168 "Relocation type not supported by output format");
1173 orp
= obj_bump(orp
);
1174 seg
->currentpos
+= size
;
1180 static void obj_write_fixup(ObjRecord
* orp
, int bytes
,
1181 int segrel
, int32_t seg
, int32_t wrt
,
1182 struct Segment
*segto
)
1188 struct Segment
*s
= NULL
;
1189 struct Group
*g
= NULL
;
1190 struct External
*e
= NULL
;
1193 if (bytes
!= 2 && bytes
!= 4) {
1194 nasm_error(ERR_NONFATAL
, "`obj' output driver does not support"
1195 " %d-bit relocations", bytes
<< 3);
1201 orp
->child
= forp
= obj_new();
1202 forp
->up
= &(orp
->child
);
1203 /* We should choose between FIXUPP and FIXU32 record type */
1204 /* If we're targeting a 32-bit segment, use a FIXU32 record */
1206 forp
->type
= FIXU32
;
1208 forp
->type
= FIXUPP
;
1213 locat
= FIX_16_SELECTOR
;
1216 nasm_panic("OBJ: 4-byte segment base fixup got"
1217 " through sanity check");
1220 locat
= (bytes
== 2) ? FIX_16_OFFSET
: FIX_32_OFFSET
;
1223 * There is a bug in tlink that makes it process self relative
1224 * fixups incorrectly if the x_size doesn't match the location
1227 forp
= obj_force(forp
, bytes
<< 3);
1230 forp
= obj_rword(forp
, locat
| segrel
| (orp
->parm
[0] - orp
->parm
[2]));
1232 tidx
= fidx
= -1, method
= 0; /* placate optimisers */
1235 * See if we can find the segment ID in our segment list. If
1236 * so, we have a T4 (LSEG) target.
1238 for (s
= seghead
; s
; s
= s
->next
)
1239 if (s
->index
== seg
)
1242 method
= 4, tidx
= s
->obj_index
;
1244 for (g
= grphead
; g
; g
= g
->next
)
1245 if (g
->index
== seg
)
1248 method
= 5, tidx
= g
->obj_index
;
1250 int32_t i
= seg
/ 2;
1251 struct ExtBack
*eb
= ebhead
;
1252 while (i
>= EXT_BLKSIZ
) {
1260 method
= 6, e
= eb
->exts
[i
], tidx
= e
->index
;
1262 nasm_panic("unrecognised segment value in obj_write_fixup");
1267 * If no WRT given, assume the natural default, which is method
1270 * - we are doing an OFFSET fixup for a grouped segment, in
1271 * which case we require F1 (group).
1273 * - we are doing an OFFSET fixup for an external with a
1274 * default WRT, in which case we must honour the default WRT.
1276 if (wrt
== NO_SEG
) {
1277 if (!base
&& s
&& s
->grp
)
1278 method
|= 0x10, fidx
= s
->grp
->obj_index
;
1279 else if (!base
&& e
&& e
->defwrt_type
!= DEFWRT_NONE
) {
1280 if (e
->defwrt_type
== DEFWRT_SEGMENT
)
1281 method
|= 0x00, fidx
= e
->defwrt_ptr
.seg
->obj_index
;
1282 else if (e
->defwrt_type
== DEFWRT_GROUP
)
1283 method
|= 0x10, fidx
= e
->defwrt_ptr
.grp
->obj_index
;
1285 nasm_error(ERR_NONFATAL
, "default WRT specification for"
1286 " external `%s' unresolved", e
->name
);
1287 method
|= 0x50, fidx
= -1; /* got to do _something_ */
1290 method
|= 0x50, fidx
= -1;
1293 * See if we can find the WRT-segment ID in our segment
1294 * list. If so, we have a F0 (LSEG) frame.
1296 for (s
= seghead
; s
; s
= s
->next
)
1297 if (s
->index
== wrt
- 1)
1300 method
|= 0x00, fidx
= s
->obj_index
;
1302 for (g
= grphead
; g
; g
= g
->next
)
1303 if (g
->index
== wrt
- 1)
1306 method
|= 0x10, fidx
= g
->obj_index
;
1308 int32_t i
= wrt
/ 2;
1309 struct ExtBack
*eb
= ebhead
;
1310 while (i
>= EXT_BLKSIZ
) {
1318 method
|= 0x20, fidx
= eb
->exts
[i
]->index
;
1320 nasm_panic("unrecognised WRT value in obj_write_fixup");
1325 forp
= obj_byte(forp
, method
);
1327 forp
= obj_index(forp
, fidx
);
1328 forp
= obj_index(forp
, tidx
);
1332 static int32_t obj_segment(char *name
, int pass
, int *bits
)
1335 * We call the label manager here to define a name for the new
1336 * segment, and when our _own_ label-definition stub gets
1337 * called in return, it should register the new segment name
1338 * using the pointer it gets passed. That way we save memory,
1339 * by sponging off the label manager.
1341 #if defined(DEBUG) && DEBUG>=3
1342 nasm_error(ERR_DEBUG
, " obj_segment: < %s >, pass=%d, *bits=%d\n",
1350 struct Segment
*seg
;
1352 struct External
**extp
;
1353 int obj_idx
, i
, attrs
;
1358 * Look for segment attributes.
1361 while (*name
== '.')
1362 name
++; /* hack, but a documented one */
1364 while (*p
&& !nasm_isspace(*p
))
1368 while (*p
&& nasm_isspace(*p
))
1372 while (*p
&& !nasm_isspace(*p
))
1376 while (*p
&& nasm_isspace(*p
))
1383 for (seg
= seghead
, obj_idx
= 1; ; seg
= seg
->next
, obj_idx
++) {
1387 if (!strcmp(seg
->name
, name
)) {
1388 if (attrs
> 0 && pass
== 1)
1389 nasm_error(ERR_WARNING
, "segment attributes specified on"
1390 " redeclaration of segment: ignoring");
1400 *segtail
= seg
= nasm_malloc(sizeof(*seg
));
1402 segtail
= &seg
->next
;
1403 seg
->index
= (any_segs
? seg_alloc() : first_seg
);
1404 seg
->obj_index
= obj_idx
;
1407 seg
->name
= nasm_strdup(name
);
1408 seg
->currentpos
= 0;
1409 seg
->align
= 1; /* default */
1410 seg
->use32
= false; /* default */
1411 seg
->combine
= CMB_PUBLIC
; /* default */
1412 seg
->segclass
= seg
->overlay
= NULL
;
1413 seg
->pubhead
= NULL
;
1414 seg
->pubtail
= &seg
->pubhead
;
1415 seg
->lochead
= NULL
;
1416 seg
->loctail
= &seg
->lochead
;
1417 seg
->orp
= obj_new();
1418 seg
->orp
->up
= &(seg
->orp
);
1419 seg
->orp
->ori
= ori_ledata
;
1420 seg
->orp
->type
= LEDATA
;
1421 seg
->orp
->parm
[1] = obj_idx
;
1424 * Process the segment attributes.
1433 * `p' contains a segment attribute.
1435 if (!nasm_stricmp(p
, "private"))
1436 seg
->combine
= CMB_PRIVATE
;
1437 else if (!nasm_stricmp(p
, "public"))
1438 seg
->combine
= CMB_PUBLIC
;
1439 else if (!nasm_stricmp(p
, "common"))
1440 seg
->combine
= CMB_COMMON
;
1441 else if (!nasm_stricmp(p
, "stack"))
1442 seg
->combine
= CMB_STACK
;
1443 else if (!nasm_stricmp(p
, "use16"))
1445 else if (!nasm_stricmp(p
, "use32"))
1447 else if (!nasm_stricmp(p
, "flat")) {
1449 * This segment is an OS/2 FLAT segment. That means
1450 * that its default group is group FLAT, even if
1451 * the group FLAT does not explicitly _contain_ the
1454 * When we see this, we must create the group
1455 * `FLAT', containing no segments, if it does not
1456 * already exist; then we must set the default
1457 * group of this segment to be the FLAT group.
1460 for (grp
= grphead
; grp
; grp
= grp
->next
)
1461 if (!strcmp(grp
->name
, "FLAT"))
1464 obj_directive(D_GROUP
, "FLAT", 1);
1465 for (grp
= grphead
; grp
; grp
= grp
->next
)
1466 if (!strcmp(grp
->name
, "FLAT"))
1469 nasm_panic("failure to define FLAT?!");
1472 } else if (!nasm_strnicmp(p
, "class=", 6))
1473 seg
->segclass
= nasm_strdup(p
+ 6);
1474 else if (!nasm_strnicmp(p
, "overlay=", 8))
1475 seg
->overlay
= nasm_strdup(p
+ 8);
1476 else if (!nasm_strnicmp(p
, "align=", 6)) {
1477 seg
->align
= readnum(p
+ 6, &rn_error
);
1480 nasm_error(ERR_NONFATAL
, "segment alignment should be"
1483 switch (seg
->align
) {
1488 case 256: /* PAGE */
1489 case 4096: /* PharLap extension */
1492 nasm_error(ERR_WARNING
,
1493 "OBJ format does not support alignment"
1494 " of 8: rounding up to 16");
1500 nasm_error(ERR_WARNING
,
1501 "OBJ format does not support alignment"
1502 " of %d: rounding up to 256", seg
->align
);
1508 nasm_error(ERR_WARNING
,
1509 "OBJ format does not support alignment"
1510 " of %d: rounding up to 4096", seg
->align
);
1514 nasm_error(ERR_NONFATAL
, "invalid alignment value %d",
1519 } else if (!nasm_strnicmp(p
, "absolute=", 9)) {
1520 seg
->align
= SEG_ABS
+ readnum(p
+ 9, &rn_error
);
1522 nasm_error(ERR_NONFATAL
, "argument to `absolute' segment"
1523 " attribute should be numeric");
1527 /* We need to know whenever we have at least one 32-bit segment */
1528 obj_use32
|= seg
->use32
;
1530 obj_seg_needs_update
= seg
;
1531 if (seg
->align
>= SEG_ABS
)
1532 define_label(name
, NO_SEG
, seg
->align
- SEG_ABS
, false);
1534 define_label(name
, seg
->index
+ 1, 0L, false);
1535 obj_seg_needs_update
= NULL
;
1538 * See if this segment is defined in any groups.
1540 for (grp
= grphead
; grp
; grp
= grp
->next
) {
1541 for (i
= grp
->nindices
; i
< grp
->nentries
; i
++) {
1542 if (!strcmp(grp
->segs
[i
].name
, seg
->name
)) {
1543 nasm_free(grp
->segs
[i
].name
);
1544 grp
->segs
[i
] = grp
->segs
[grp
->nindices
];
1545 grp
->segs
[grp
->nindices
++].index
= seg
->obj_index
;
1547 nasm_error(ERR_WARNING
,
1548 "segment `%s' is already part of"
1549 " a group: first one takes precedence",
1558 * Walk through the list of externals with unresolved
1559 * default-WRT clauses, and resolve any that point at this
1564 if ((*extp
)->defwrt_type
== DEFWRT_STRING
&&
1565 !strcmp((*extp
)->defwrt_ptr
.string
, seg
->name
)) {
1566 nasm_free((*extp
)->defwrt_ptr
.string
);
1567 (*extp
)->defwrt_type
= DEFWRT_SEGMENT
;
1568 (*extp
)->defwrt_ptr
.seg
= seg
;
1569 *extp
= (*extp
)->next_dws
;
1571 extp
= &(*extp
)->next_dws
;
1583 static enum directive_result
1584 obj_directive(enum directive directive
, char *value
, int pass
)
1586 switch (directive
) {
1592 struct Segment
*seg
;
1593 struct External
**extp
;
1598 q
++; /* hack, but a documented one */
1600 while (*q
&& !nasm_isspace(*q
))
1602 if (nasm_isspace(*q
)) {
1604 while (*q
&& nasm_isspace(*q
))
1608 * Here we used to sanity-check the group directive to
1609 * ensure nobody tried to declare a group containing no
1610 * segments. However, OS/2 does this as standard
1611 * practice, so the sanity check has been removed.
1614 * nasm_error(ERR_NONFATAL,"GROUP directive contains no segments");
1615 * return DIRR_ERROR;
1620 for (grp
= grphead
; grp
; grp
= grp
->next
) {
1622 if (!strcmp(grp
->name
, v
)) {
1623 nasm_error(ERR_NONFATAL
, "group `%s' defined twice", v
);
1628 *grptail
= grp
= nasm_malloc(sizeof(*grp
));
1630 grptail
= &grp
->next
;
1631 grp
->index
= seg_alloc();
1632 grp
->obj_index
= obj_idx
;
1633 grp
->nindices
= grp
->nentries
= 0;
1636 obj_grp_needs_update
= grp
;
1637 backend_label(v
, grp
->index
+ 1, 0L);
1638 obj_grp_needs_update
= NULL
;
1642 while (*q
&& !nasm_isspace(*q
))
1644 if (nasm_isspace(*q
)) {
1646 while (*q
&& nasm_isspace(*q
))
1650 * Now p contains a segment name. Find it.
1652 for (seg
= seghead
; seg
; seg
= seg
->next
)
1653 if (!strcmp(seg
->name
, p
))
1657 * We have a segment index. Shift a name entry
1658 * to the end of the array to make room.
1660 grp
->segs
[grp
->nentries
++] = grp
->segs
[grp
->nindices
];
1661 grp
->segs
[grp
->nindices
++].index
= seg
->obj_index
;
1663 nasm_error(ERR_WARNING
,
1664 "segment `%s' is already part of"
1665 " a group: first one takes precedence",
1671 * We have an as-yet undefined segment.
1672 * Remember its name, for later.
1674 grp
->segs
[grp
->nentries
++].name
= nasm_strdup(p
);
1679 * Walk through the list of externals with unresolved
1680 * default-WRT clauses, and resolve any that point at
1685 if ((*extp
)->defwrt_type
== DEFWRT_STRING
&&
1686 !strcmp((*extp
)->defwrt_ptr
.string
, grp
->name
)) {
1687 nasm_free((*extp
)->defwrt_ptr
.string
);
1688 (*extp
)->defwrt_type
= DEFWRT_GROUP
;
1689 (*extp
)->defwrt_ptr
.grp
= grp
;
1690 *extp
= (*extp
)->next_dws
;
1692 extp
= &(*extp
)->next_dws
;
1698 obj_uppercase
= true;
1703 char *q
, *extname
, *libname
, *impname
;
1706 return 1; /* ignore in pass two */
1707 extname
= q
= value
;
1708 while (*q
&& !nasm_isspace(*q
))
1710 if (nasm_isspace(*q
)) {
1712 while (*q
&& nasm_isspace(*q
))
1717 while (*q
&& !nasm_isspace(*q
))
1719 if (nasm_isspace(*q
)) {
1721 while (*q
&& nasm_isspace(*q
))
1727 if (!*extname
|| !*libname
)
1728 nasm_error(ERR_NONFATAL
, "`import' directive requires symbol name"
1729 " and library name");
1734 imp
= *imptail
= nasm_malloc(sizeof(struct ImpDef
));
1735 imptail
= &imp
->next
;
1737 imp
->extname
= nasm_strdup(extname
);
1738 imp
->libname
= nasm_strdup(libname
);
1739 imp
->impindex
= readnum(impname
, &err
);
1740 if (!*impname
|| err
)
1741 imp
->impname
= nasm_strdup(impname
);
1743 imp
->impname
= NULL
;
1750 char *q
, *extname
, *intname
, *v
;
1751 struct ExpDef
*export
;
1753 unsigned int ordinal
= 0;
1756 return DIRR_OK
; /* ignore in pass two */
1757 intname
= q
= value
;
1758 while (*q
&& !nasm_isspace(*q
))
1760 if (nasm_isspace(*q
)) {
1762 while (*q
&& nasm_isspace(*q
))
1767 while (*q
&& !nasm_isspace(*q
))
1769 if (nasm_isspace(*q
)) {
1771 while (*q
&& nasm_isspace(*q
))
1776 nasm_error(ERR_NONFATAL
, "`export' directive requires export name");
1785 while (*q
&& !nasm_isspace(*q
))
1787 if (nasm_isspace(*q
)) {
1789 while (*q
&& nasm_isspace(*q
))
1792 if (!nasm_stricmp(v
, "resident"))
1793 flags
|= EXPDEF_FLAG_RESIDENT
;
1794 else if (!nasm_stricmp(v
, "nodata"))
1795 flags
|= EXPDEF_FLAG_NODATA
;
1796 else if (!nasm_strnicmp(v
, "parm=", 5)) {
1798 flags
|= EXPDEF_MASK_PARMCNT
& readnum(v
+ 5, &err
);
1800 nasm_error(ERR_NONFATAL
,
1801 "value `%s' for `parm' is non-numeric", v
+ 5);
1806 ordinal
= readnum(v
, &err
);
1808 nasm_error(ERR_NONFATAL
,
1809 "unrecognised export qualifier `%s'", v
);
1812 flags
|= EXPDEF_FLAG_ORDINAL
;
1816 export
= *exptail
= nasm_malloc(sizeof(struct ExpDef
));
1817 exptail
= &export
->next
;
1818 export
->next
= NULL
;
1819 export
->extname
= nasm_strdup(extname
);
1820 export
->intname
= nasm_strdup(intname
);
1821 export
->ordinal
= ordinal
;
1822 export
->flags
= flags
;
1827 return DIRR_UNKNOWN
;
1831 static void obj_sectalign(int32_t seg
, unsigned int value
)
1835 list_for_each(s
, seghead
) {
1836 if (s
->index
== seg
)
1841 * it should not be too big value
1842 * and applied on non-absolute sections
1844 if (!s
|| !is_power2(value
) ||
1845 value
> 4096 || s
->align
>= SEG_ABS
)
1849 * FIXME: No code duplication please
1850 * consider making helper for this
1851 * mapping since section handler has
1870 if (s
->align
< (int)value
)
1874 static int32_t obj_segbase(int32_t segment
)
1876 struct Segment
*seg
;
1879 * Find the segment in our list.
1881 for (seg
= seghead
; seg
; seg
= seg
->next
)
1882 if (seg
->index
== segment
- 1)
1887 * Might be an external with a default WRT.
1889 int32_t i
= segment
/ 2;
1890 struct ExtBack
*eb
= ebhead
;
1893 while (i
>= EXT_BLKSIZ
) {
1903 nasm_assert(pass0
== 0);
1904 /* Not available - can happen during optimization */
1908 switch (e
->defwrt_type
) {
1910 return segment
; /* fine */
1911 case DEFWRT_SEGMENT
:
1912 return e
->defwrt_ptr
.seg
->index
+ 1;
1914 return e
->defwrt_ptr
.grp
->index
+ 1;
1916 return NO_SEG
; /* can't tell what it is */
1920 return segment
; /* not one of ours - leave it alone */
1923 if (seg
->align
>= SEG_ABS
)
1924 return seg
->align
; /* absolute segment */
1926 return seg
->grp
->index
+ 1; /* grouped segment */
1928 return segment
; /* no special treatment */
1931 /* Get a file timestamp in MS-DOS format */
1932 static uint32_t obj_file_timestamp(const char *pathname
)
1935 const struct tm
*lt
;
1937 if (!nasm_file_time(&t
, pathname
))
1944 if (lt
->tm_year
< 80 || lt
->tm_year
> 207)
1945 return 0; /* Only years 1980-2107 representable */
1948 ((uint32_t)lt
->tm_sec
>> 1) +
1949 ((uint32_t)lt
->tm_min
<< 5) +
1950 ((uint32_t)lt
->tm_hour
<< 11) +
1951 ((uint32_t)lt
->tm_mday
<< 16) +
1952 (((uint32_t)lt
->tm_mon
+ 1) << 21) +
1953 (((uint32_t)lt
->tm_year
- 80) << 25);
1956 static void obj_write_file(void)
1958 struct Segment
*seg
, *entry_seg_ptr
= 0;
1959 struct FileName
*fn
;
1960 struct LineNumber
*ln
;
1962 struct Public
*pub
, *loc
;
1963 struct External
*ext
;
1965 struct ExpDef
*export
;
1968 const StrList
*depfile
;
1969 const bool debuginfo
= (dfmt
== &borland_debug_form
);
1972 * Write the THEADR module header.
1976 obj_name(orp
, obj_infile
);
1980 * Write the NASM boast comment.
1983 obj_rword(orp
, dTRANSL
);
1984 obj_name(orp
, nasm_comment
);
1988 * Output file dependency information
1990 if (!obj_nodepend
) {
1991 list_for_each(depfile
, depend_list
) {
1994 ts
= obj_file_timestamp(depfile
->str
);
1997 obj_rword(orp
, dDEPFILE
);
1999 obj_name(orp
, depfile
->str
);
2007 * Write the IMPDEF records, if any.
2009 for (imp
= imphead
; imp
; imp
= imp
->next
) {
2010 obj_rword(orp
, dOMFEXT
);
2011 obj_byte(orp
, 1); /* subfunction 1: IMPDEF */
2013 obj_byte(orp
, 0); /* import by name */
2015 obj_byte(orp
, 1); /* import by ordinal */
2016 obj_name(orp
, imp
->extname
);
2017 obj_name(orp
, imp
->libname
);
2019 obj_name(orp
, imp
->impname
);
2021 obj_word(orp
, imp
->impindex
);
2026 * Write the EXPDEF records, if any.
2028 for (export
= exphead
; export
; export
= export
->next
) {
2029 obj_rword(orp
, dOMFEXT
);
2030 obj_byte(orp
, 2); /* subfunction 2: EXPDEF */
2031 obj_byte(orp
, export
->flags
);
2032 obj_name(orp
, export
->extname
);
2033 obj_name(orp
, export
->intname
);
2034 if (export
->flags
& EXPDEF_FLAG_ORDINAL
)
2035 obj_word(orp
, export
->ordinal
);
2039 /* we're using extended OMF if we put in debug info */
2042 obj_rword(orp
, dEXTENDED
);
2047 * Write the first LNAMES record, containing LNAME one, which
2048 * is null. Also initialize the LNAME counter.
2054 * Write some LNAMES for the segment names
2056 for (seg
= seghead
; seg
; seg
= seg
->next
) {
2057 orp
= obj_name(orp
, seg
->name
);
2059 orp
= obj_name(orp
, seg
->segclass
);
2061 orp
= obj_name(orp
, seg
->overlay
);
2065 * Write some LNAMES for the group names
2067 for (grp
= grphead
; grp
; grp
= grp
->next
) {
2068 orp
= obj_name(orp
, grp
->name
);
2074 * Write the SEGDEF records.
2077 for (seg
= seghead
; seg
; seg
= seg
->next
) {
2079 uint32_t seglen
= seg
->currentpos
;
2081 acbp
= (seg
->combine
<< 2); /* C field */
2084 acbp
|= 0x01; /* P bit is Use32 flag */
2085 else if (seglen
== 0x10000L
) {
2086 seglen
= 0; /* This special case may be needed for old linkers */
2087 acbp
|= 0x02; /* B bit */
2091 if (seg
->align
>= SEG_ABS
)
2092 /* acbp |= 0x00 */ ;
2093 else if (seg
->align
>= 4096) {
2094 if (seg
->align
> 4096)
2095 nasm_error(ERR_NONFATAL
, "segment `%s' requires more alignment"
2096 " than OBJ format supports", seg
->name
);
2097 acbp
|= 0xC0; /* PharLap extension */
2098 } else if (seg
->align
>= 256) {
2100 } else if (seg
->align
>= 16) {
2102 } else if (seg
->align
>= 4) {
2104 } else if (seg
->align
>= 2) {
2109 obj_byte(orp
, acbp
);
2110 if (seg
->align
& SEG_ABS
) {
2111 obj_x(orp
, seg
->align
- SEG_ABS
); /* Frame */
2112 obj_byte(orp
, 0); /* Offset */
2115 obj_index(orp
, ++lname_idx
);
2116 obj_index(orp
, seg
->segclass
? ++lname_idx
: 1);
2117 obj_index(orp
, seg
->overlay
? ++lname_idx
: 1);
2122 * Write the GRPDEF records.
2125 for (grp
= grphead
; grp
; grp
= grp
->next
) {
2128 if (grp
->nindices
!= grp
->nentries
) {
2129 for (i
= grp
->nindices
; i
< grp
->nentries
; i
++) {
2130 nasm_error(ERR_NONFATAL
, "group `%s' contains undefined segment"
2131 " `%s'", grp
->name
, grp
->segs
[i
].name
);
2132 nasm_free(grp
->segs
[i
].name
);
2133 grp
->segs
[i
].name
= NULL
;
2136 obj_index(orp
, ++lname_idx
);
2137 for (i
= 0; i
< grp
->nindices
; i
++) {
2138 obj_byte(orp
, 0xFF);
2139 obj_index(orp
, grp
->segs
[i
].index
);
2145 * Write the PUBDEF records: first the ones in the segments,
2146 * then the far-absolutes.
2149 orp
->ori
= ori_pubdef
;
2150 for (seg
= seghead
; seg
; seg
= seg
->next
) {
2151 orp
->parm
[0] = seg
->grp
? seg
->grp
->obj_index
: 0;
2152 orp
->parm
[1] = seg
->obj_index
;
2153 for (pub
= seg
->pubhead
; pub
; pub
= pub
->next
) {
2154 orp
= obj_name(orp
, pub
->name
);
2155 orp
= obj_x(orp
, pub
->offset
);
2156 orp
= obj_byte(orp
, 0); /* type index */
2163 for (pub
= fpubhead
; pub
; pub
= pub
->next
) { /* pub-crawl :-) */
2164 if (orp
->parm
[2] != (uint32_t)pub
->segment
) {
2166 orp
->parm
[2] = pub
->segment
;
2168 orp
= obj_name(orp
, pub
->name
);
2169 orp
= obj_x(orp
, pub
->offset
);
2170 orp
= obj_byte(orp
, 0); /* type index */
2176 * Write the EXTDEF and COMDEF records, in order.
2178 orp
->ori
= ori_null
;
2179 for (ext
= exthead
; ext
; ext
= ext
->next
) {
2180 if (ext
->commonsize
== 0) {
2181 if (orp
->type
!= EXTDEF
) {
2185 orp
= obj_name(orp
, ext
->name
);
2186 orp
= obj_index(orp
, 0);
2188 if (orp
->type
!= COMDEF
) {
2192 orp
= obj_name(orp
, ext
->name
);
2193 orp
= obj_index(orp
, 0);
2194 if (ext
->commonelem
) {
2195 orp
= obj_byte(orp
, 0x61); /* far communal */
2196 orp
= obj_value(orp
, (ext
->commonsize
/ ext
->commonelem
));
2197 orp
= obj_value(orp
, ext
->commonelem
);
2199 orp
= obj_byte(orp
, 0x62); /* near communal */
2200 orp
= obj_value(orp
, ext
->commonsize
);
2208 * Write a COMENT record stating that the linker's first pass
2209 * may stop processing at this point. Exception is if our
2210 * MODEND record specifies a start point, in which case,
2211 * according to some variants of the documentation, this COMENT
2212 * should be omitted. So we'll omit it just in case.
2213 * But, TASM puts it in all the time so if we are using
2214 * TASM debug stuff we are putting it in
2216 if (debuginfo
|| obj_entry_seg
== NO_SEG
) {
2218 obj_rword(orp
, dLINKPASS
);
2224 * 1) put out the compiler type
2225 * 2) Put out the type info. The only type we are using is near label #19
2229 struct Array
*arrtmp
= arrhead
;
2231 obj_rword(orp
, dCOMPDEF
);
2236 obj_rword(orp
, dTYPEDEF
);
2237 obj_word(orp
, 0x18); /* type # for linking */
2238 obj_word(orp
, 6); /* size of type */
2239 obj_byte(orp
, 0x2a); /* absolute type for debugging */
2241 obj_rword(orp
, dTYPEDEF
);
2242 obj_word(orp
, 0x19); /* type # for linking */
2243 obj_word(orp
, 0); /* size of type */
2244 obj_byte(orp
, 0x24); /* absolute type for debugging */
2245 obj_byte(orp
, 0); /* near/far specifier */
2247 obj_rword(orp
, dTYPEDEF
);
2248 obj_word(orp
, 0x1A); /* type # for linking */
2249 obj_word(orp
, 0); /* size of type */
2250 obj_byte(orp
, 0x24); /* absolute type for debugging */
2251 obj_byte(orp
, 1); /* near/far specifier */
2253 obj_rword(orp
, dTYPEDEF
);
2254 obj_word(orp
, 0x1b); /* type # for linking */
2255 obj_word(orp
, 0); /* size of type */
2256 obj_byte(orp
, 0x23); /* absolute type for debugging */
2261 obj_rword(orp
, dTYPEDEF
);
2262 obj_word(orp
, 0x1c); /* type # for linking */
2263 obj_word(orp
, 0); /* size of type */
2264 obj_byte(orp
, 0x23); /* absolute type for debugging */
2269 obj_rword(orp
, dTYPEDEF
);
2270 obj_word(orp
, 0x1d); /* type # for linking */
2271 obj_word(orp
, 0); /* size of type */
2272 obj_byte(orp
, 0x23); /* absolute type for debugging */
2277 obj_rword(orp
, dTYPEDEF
);
2278 obj_word(orp
, 0x1e); /* type # for linking */
2279 obj_word(orp
, 0); /* size of type */
2280 obj_byte(orp
, 0x23); /* absolute type for debugging */
2286 /* put out the array types */
2287 for (i
= ARRAYBOT
; i
< arrindex
; i
++) {
2288 obj_rword(orp
, dTYPEDEF
);
2289 obj_word(orp
, i
); /* type # for linking */
2290 obj_word(orp
, arrtmp
->size
); /* size of type */
2291 obj_byte(orp
, 0x1A); /* absolute type for debugging (array) */
2292 obj_byte(orp
, arrtmp
->basetype
); /* base type */
2294 arrtmp
= arrtmp
->next
;
2298 * write out line number info with a LINNUM record
2299 * switch records when we switch segments, and output the
2300 * file in a pseudo-TASM fashion. The record switch is naive; that
2301 * is that one file may have many records for the same segment
2302 * if there are lots of segment switches
2304 if (fnhead
&& debuginfo
) {
2305 seg
= fnhead
->lnhead
->segment
;
2307 for (fn
= fnhead
; fn
; fn
= fn
->next
) {
2308 /* write out current file name */
2310 orp
->ori
= ori_null
;
2311 obj_rword(orp
, dFILNAME
);
2313 obj_name(orp
, fn
->name
);
2317 /* write out line numbers this file */
2320 orp
->ori
= ori_linnum
;
2321 for (ln
= fn
->lnhead
; ln
; ln
= ln
->next
) {
2322 if (seg
!= ln
->segment
) {
2323 /* if we get here have to flush the buffer and start
2324 * a new record for a new segment
2329 orp
->parm
[0] = seg
->grp
? seg
->grp
->obj_index
: 0;
2330 orp
->parm
[1] = seg
->obj_index
;
2331 orp
= obj_word(orp
, ln
->lineno
);
2332 orp
= obj_x(orp
, ln
->offset
);
2339 * we are going to locate the entry point segment now
2340 * rather than wait until the MODEND record, because,
2341 * then we can output a special symbol to tell where the
2345 if (obj_entry_seg
!= NO_SEG
) {
2346 for (seg
= seghead
; seg
; seg
= seg
->next
) {
2347 if (seg
->index
== obj_entry_seg
) {
2348 entry_seg_ptr
= seg
;
2353 nasm_error(ERR_NONFATAL
, "entry point is not in this module");
2357 * get ready to put out symbol records
2360 orp
->ori
= ori_local
;
2363 * put out a symbol for the entry point
2364 * no dots in this symbol, because, borland does
2365 * not (officially) support dots in label names
2366 * and I don't know what various versions of TLINK will do
2368 if (debuginfo
&& obj_entry_seg
!= NO_SEG
) {
2369 orp
= obj_name(orp
, "start_of_program");
2370 orp
= obj_word(orp
, 0x19); /* type: near label */
2371 orp
= obj_index(orp
, seg
->grp
? seg
->grp
->obj_index
: 0);
2372 orp
= obj_index(orp
, seg
->obj_index
);
2373 orp
= obj_x(orp
, obj_entry_ofs
);
2378 * put out the local labels
2380 for (seg
= seghead
; seg
&& debuginfo
; seg
= seg
->next
) {
2381 /* labels this seg */
2382 for (loc
= seg
->lochead
; loc
; loc
= loc
->next
) {
2383 orp
= obj_name(orp
, loc
->name
);
2384 orp
= obj_word(orp
, loc
->type
);
2385 orp
= obj_index(orp
, seg
->grp
? seg
->grp
->obj_index
: 0);
2386 orp
= obj_index(orp
, seg
->obj_index
);
2387 orp
= obj_x(orp
, loc
->offset
);
2395 * Write the LEDATA/FIXUPP pairs.
2397 for (seg
= seghead
; seg
; seg
= seg
->next
) {
2399 nasm_free(seg
->orp
);
2403 * Write the MODEND module end marker.
2405 orp
->type
= obj_use32
? MODE32
: MODEND
;
2406 orp
->ori
= ori_null
;
2407 if (entry_seg_ptr
) {
2408 orp
->type
= entry_seg_ptr
->use32
? MODE32
: MODEND
;
2409 obj_byte(orp
, 0xC1);
2410 seg
= entry_seg_ptr
;
2412 obj_byte(orp
, 0x10);
2413 obj_index(orp
, seg
->grp
->obj_index
);
2416 * the below changed to prevent TLINK crashing.
2417 * Previous more efficient version read:
2419 * obj_byte (orp, 0x50);
2421 obj_byte(orp
, 0x00);
2422 obj_index(orp
, seg
->obj_index
);
2424 obj_index(orp
, seg
->obj_index
);
2425 obj_x(orp
, obj_entry_ofs
);
2432 static void obj_fwrite(ObjRecord
* orp
)
2434 unsigned int cksum
, len
;
2438 if (orp
->x_size
== 32)
2440 fputc(cksum
, ofile
);
2441 len
= orp
->committed
+ 1;
2442 cksum
+= (len
& 0xFF) + ((len
>> 8) & 0xFF);
2443 fwriteint16_t(len
, ofile
);
2444 nasm_write(orp
->buf
, len
-1, ofile
);
2445 for (ptr
= orp
->buf
; --len
; ptr
++)
2447 fputc((-cksum
) & 0xFF, ofile
);
2450 static enum directive_result
2451 obj_pragma(const struct pragma
*pragma
)
2453 switch (pragma
->opcode
) {
2455 obj_nodepend
= true;
2465 extern macros_t obj_stdmac
[];
2467 static void dbgbi_init(void)
2471 arrindex
= ARRAYBOT
;
2475 static void dbgbi_cleanup(void)
2477 struct Segment
*segtmp
;
2479 struct FileName
*fntemp
= fnhead
;
2480 while (fnhead
->lnhead
) {
2481 struct LineNumber
*lntemp
= fnhead
->lnhead
;
2482 fnhead
->lnhead
= lntemp
->next
;
2485 fnhead
= fnhead
->next
;
2486 nasm_free(fntemp
->name
);
2489 for (segtmp
= seghead
; segtmp
; segtmp
= segtmp
->next
) {
2490 while (segtmp
->lochead
) {
2491 struct Public
*loctmp
= segtmp
->lochead
;
2492 segtmp
->lochead
= loctmp
->next
;
2493 nasm_free(loctmp
->name
);
2498 struct Array
*arrtmp
= arrhead
;
2499 arrhead
= arrhead
->next
;
2504 static void dbgbi_linnum(const char *lnfname
, int32_t lineno
, int32_t segto
)
2506 struct FileName
*fn
;
2507 struct LineNumber
*ln
;
2508 struct Segment
*seg
;
2510 if (segto
== NO_SEG
)
2514 * If `any_segs' is still false, we must define a default
2518 int tempint
; /* ignored */
2519 if (segto
!= obj_segment("__NASMDEFSEG", 2, &tempint
))
2520 nasm_panic("strange segment conditions in OBJ driver");
2524 * Find the segment we are targetting.
2526 for (seg
= seghead
; seg
; seg
= seg
->next
)
2527 if (seg
->index
== segto
)
2530 nasm_panic("lineno directed to nonexistent segment?");
2532 /* for (fn = fnhead; fn; fn = fnhead->next) */
2533 for (fn
= fnhead
; fn
; fn
= fn
->next
) /* fbk - Austin Lunnen - John Fine */
2534 if (!nasm_stricmp(lnfname
, fn
->name
))
2537 fn
= nasm_malloc(sizeof(*fn
));
2538 fn
->name
= nasm_malloc(strlen(lnfname
) + 1);
2539 strcpy(fn
->name
, lnfname
);
2541 fn
->lntail
= &fn
->lnhead
;
2546 ln
= nasm_malloc(sizeof(*ln
));
2548 ln
->offset
= seg
->currentpos
;
2549 ln
->lineno
= lineno
;
2552 fn
->lntail
= &ln
->next
;
2555 static void dbgbi_deflabel(char *name
, int32_t segment
,
2556 int64_t offset
, int is_global
, char *special
)
2558 struct Segment
*seg
;
2563 * Note: ..[^@] special symbols are filtered in labels.c
2567 * If it's a special-retry from pass two, discard it.
2575 if (obj_seg_needs_update
) {
2577 } else if (obj_grp_needs_update
) {
2580 if (segment
< SEG_ABS
&& segment
!= NO_SEG
&& segment
% 2)
2583 if (segment
>= SEG_ABS
|| segment
== NO_SEG
) {
2588 * If `any_segs' is still false, we might need to define a
2589 * default segment, if they're trying to declare a label in
2590 * `first_seg'. But the label should exist due to a prior
2591 * call to obj_deflabel so we can skip that.
2594 for (seg
= seghead
; seg
; seg
= seg
->next
)
2595 if (seg
->index
== segment
) {
2596 struct Public
*loc
= nasm_malloc(sizeof(*loc
));
2598 * Case (ii). Maybe MODPUB someday?
2600 last_defined
= *seg
->loctail
= loc
;
2601 seg
->loctail
= &loc
->next
;
2603 loc
->name
= nasm_strdup(name
);
2604 loc
->offset
= offset
;
2607 static void dbgbi_typevalue(int32_t type
)
2610 int elem
= TYM_ELEMENTS(type
);
2611 type
= TYM_TYPE(type
);
2618 last_defined
->type
= 8; /* uint8_t */
2622 last_defined
->type
= 10; /* unsigned word */
2626 last_defined
->type
= 12; /* unsigned dword */
2630 last_defined
->type
= 14; /* float */
2634 last_defined
->type
= 15; /* qword */
2638 last_defined
->type
= 16; /* TBYTE */
2642 last_defined
->type
= 0x19; /* label */
2648 struct Array
*arrtmp
= nasm_malloc(sizeof(*arrtmp
));
2649 int vtype
= last_defined
->type
;
2650 arrtmp
->size
= vsize
* elem
;
2651 arrtmp
->basetype
= vtype
;
2652 arrtmp
->next
= NULL
;
2653 last_defined
->type
= arrindex
++;
2655 arrtail
= &(arrtmp
->next
);
2657 last_defined
= NULL
;
2659 static void dbgbi_output(int output_type
, void *param
)
2664 static const struct dfmt borland_debug_form
= {
2665 "Borland Debug Records",
2670 null_debug_directive
,
2674 NULL
/* pragma list */
2677 static const struct dfmt
* const borland_debug_arr
[3] = {
2678 &borland_debug_form
,
2683 static const struct pragma_facility obj_pragma_list
[] = {
2684 { NULL
, obj_pragma
}
2687 const struct ofmt of_obj
= {
2688 "MS-DOS 16-bit/32-bit OMF object files",
2694 &borland_debug_form
,
2698 nasm_do_legacy_output
,