nasmlib: Fix space/tabs mess
[nasm.git] / output / outobj.c
blob9fd4ddcab7f2633c2ad91632a388fe73956d0be4
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
9 * conditions are met:
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
36 * .OBJ object files
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <limits.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "error.h"
50 #include "stdscan.h"
51 #include "eval.h"
52 #include "ver.h"
54 #include "outform.h"
55 #include "outlib.h"
57 #ifdef OF_OBJ
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
114 * next operation.
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);
165 struct ObjRecord {
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)
194 orp->used = 0;
195 orp->committed = 0;
196 orp->x_size = 0;
197 orp->child = NULL;
198 orp->up = NULL;
199 orp->back = NULL;
200 return (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,
208 * not freed.
210 static ObjRecord *obj_emit(ObjRecord * orp)
212 if (orp->back) {
213 obj_emit(orp->back);
214 nasm_free(orp->back);
217 if (orp->committed)
218 obj_fwrite(orp);
220 if (orp->child) {
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)
233 obj_commit(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)
242 ObjRecord *orp;
244 orp = obj_clear(nasm_malloc(sizeof(ObjRecord)));
245 orp->ori = ori_null;
246 return (orp);
250 * Advance to the next record because the existing one is full or its x_size
251 * is incompatible.
252 * Any uncommited data is moved into the next record.
254 static ObjRecord *obj_bump(ObjRecord * orp)
256 ObjRecord *nxt;
257 int used = orp->used;
258 int committed = orp->committed;
260 if (orp->up) {
261 *orp->up = nxt = obj_new();
262 nxt->ori = orp->ori;
263 nxt->type = orp->type;
264 nxt->up = orp->up;
265 nxt->back = orp;
266 memcpy(nxt->parm, orp->parm, sizeof(orp->parm));
267 } else
268 nxt = obj_emit(orp);
270 used -= committed;
271 if (used) {
272 nxt->committed = 1;
273 nxt->ori(nxt);
274 nxt->committed = nxt->used;
275 memcpy(nxt->buf + nxt->committed, orp->buf + committed, used);
276 nxt->used = nxt->committed + used;
279 return (nxt);
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)
288 orp = obj_bump(orp);
290 if (!orp->committed) {
291 orp->committed = 1;
292 orp->ori(orp);
293 orp->committed = orp->used;
296 return (orp);
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;
306 return (orp);
310 * Write a byte
312 static ObjRecord *obj_byte(ObjRecord * orp, uint8_t val)
314 orp = obj_check(orp, 1);
315 orp->buf[orp->used] = val;
316 orp->used++;
317 return (orp);
321 * Write a word
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;
328 orp->used += 2;
329 return (orp);
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;
340 orp->used += 2;
341 return (orp);
345 * Write a dword
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;
354 orp->used += 4;
355 return (orp);
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
361 * which.
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))
371 orp = obj_bump(orp);
372 orp->x_size = x;
373 return (orp);
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)
382 if (orp->type & 1)
383 orp->x_size = 32;
384 if (val > 0xFFFF)
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 */
389 return nxt;
391 orp->x_size = 16;
392 return (obj_word(orp, val));
396 * Writes an index
398 static ObjRecord *obj_index(ObjRecord * orp, unsigned int val)
400 if (val < 128)
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)
410 if (val <= 128)
411 return (obj_byte(orp, val));
412 if (val <= 0xFFFF) {
413 orp = obj_byte(orp, 129);
414 return (obj_word(orp, val));
416 if (val <= 0xFFFFFF)
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);
428 uint8_t *ptr;
430 orp = obj_check(orp, len + 1);
431 ptr = orp->buf + orp->used;
432 *ptr++ = len;
433 orp->used += len + 1;
434 if (obj_uppercase)
435 while (--len >= 0) {
436 *ptr++ = toupper(*name);
437 name++;
438 } else
439 memcpy(ptr, name, len);
440 return (orp);
444 * Initializer for an LEDATA record.
445 * parm[0] = offset
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
449 * last commit point.
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;
508 static int passtwo;
509 static int arrindex;
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 */
516 struct Group;
518 struct LineNumber {
519 struct LineNumber *next;
520 struct Segment *segment;
521 int32_t offset;
522 int32_t lineno;
525 static struct FileName {
526 struct FileName *next;
527 char *name;
528 struct LineNumber *lnhead, **lntail;
529 int index;
530 } *fnhead, **fntail;
532 static struct Array {
533 struct Array *next;
534 unsigned size;
535 int basetype;
536 } *arrhead, **arrtail;
538 #define ARRAYBOT 31 /* magic number for first array index */
540 static struct Public {
541 struct Public *next;
542 char *name;
543 int32_t offset;
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;
550 char *name;
551 int32_t commonsize;
552 int32_t commonelem; /* element size if FAR, else zero */
553 int index; /* OBJ-file external index */
554 enum {
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 */
559 } defwrt_type;
560 union {
561 char *string;
562 struct Segment *seg;
563 struct Group *grp;
564 } defwrt_ptr;
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];
573 } *ebhead, **ebtail;
575 static struct Segment {
576 struct Segment *next;
577 char *name;
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 */
581 uint32_t currentpos;
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 :-) */
585 ObjRecord *orp;
586 enum {
587 CMB_PRIVATE = 0,
588 CMB_PUBLIC = 2,
589 CMB_STACK = 5,
590 CMB_COMMON = 6
591 } combine;
592 bool use32; /* is this segment 32-bit? */
593 } *seghead, **segtail, *obj_seg_needs_update;
595 static struct Group {
596 struct Group *next;
597 char *name;
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... */
602 union {
603 int32_t index;
604 char *name;
605 } segs[GROUP_MAX]; /* ...in this */
606 } *grphead, **grptail, *obj_grp_needs_update;
608 static struct ImpDef {
609 struct ImpDef *next;
610 char *extname;
611 char *libname;
612 unsigned int impindex;
613 char *impname;
614 } *imphead, **imptail;
616 static struct ExpDef {
617 struct ExpDef *next;
618 char *intname;
619 char *extname;
620 unsigned int ordinal;
621 int flags;
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();
645 any_segs = false;
646 fpubhead = NULL;
647 fpubtail = &fpubhead;
648 exthead = NULL;
649 exttail = &exthead;
650 imphead = NULL;
651 imptail = &imphead;
652 exphead = NULL;
653 exptail = &exphead;
654 dws = NULL;
655 externals = 0;
656 ebhead = NULL;
657 ebtail = &ebhead;
658 seghead = obj_seg_needs_update = NULL;
659 segtail = &seghead;
660 grphead = obj_grp_needs_update = NULL;
661 grptail = &grphead;
662 obj_entry_seg = NO_SEG;
663 obj_uppercase = false;
664 obj_use32 = false;
665 passtwo = 0;
666 current_seg = NULL;
669 static void obj_cleanup(void)
671 obj_write_file();
672 dfmt->cleanup();
673 while (seghead) {
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);
680 nasm_free(pubtmp);
682 nasm_free(segtmp->segclass);
683 nasm_free(segtmp->overlay);
684 nasm_free(segtmp);
686 while (fpubhead) {
687 struct Public *pubtmp = fpubhead;
688 fpubhead = fpubhead->next;
689 nasm_free(pubtmp->name);
690 nasm_free(pubtmp);
692 while (exthead) {
693 struct External *exttmp = exthead;
694 exthead = exthead->next;
695 nasm_free(exttmp);
697 while (imphead) {
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 */
703 nasm_free(imptmp);
705 while (exphead) {
706 struct ExpDef *exptmp = exphead;
707 exphead = exphead->next;
708 nasm_free(exptmp->extname);
709 nasm_free(exptmp->intname);
710 nasm_free(exptmp);
712 while (ebhead) {
713 struct ExtBack *ebtmp = ebhead;
714 ebhead = ebhead->next;
715 nasm_free(ebtmp);
717 while (grphead) {
718 struct Group *grptmp = grphead;
719 grphead = grphead->next;
720 nasm_free(grptmp);
724 static void obj_ext_set_defwrt(struct External *ext, char *id)
726 struct Segment *seg;
727 struct Group *grp;
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;
733 nasm_free(id);
734 return;
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;
741 nasm_free(id);
742 return;
745 ext->defwrt_type = DEFWRT_STRING;
746 ext->defwrt_ptr.string = id;
747 ext->next_dws = dws;
748 dws = ext;
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
759 * return.
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;
771 struct ExtBack *eb;
772 struct Segment *seg;
773 int i;
774 bool used_special = false; /* have we used the special text? */
776 #if defined(DEBUG) && DEBUG>2
777 nasm_debug(" obj_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
778 name, segment, offset, is_global, special);
779 #endif
782 * If it's a special-retry from pass two, discard it.
784 if (is_global == 3)
785 return;
788 * First check for the double-period, signifying something
789 * unusual.
791 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
792 if (!strcmp(name, "..start")) {
793 obj_entry_seg = segment;
794 obj_entry_ofs = offset;
795 return;
797 nasm_nonfatal("unrecognised special symbol `%s'", name);
801 * Case (i):
803 if (obj_seg_needs_update) {
804 obj_seg_needs_update->name = name;
805 return;
806 } else if (obj_grp_needs_update) {
807 obj_grp_needs_update->name = name;
808 return;
810 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
811 return;
813 if (segment >= SEG_ABS || segment == NO_SEG) {
815 * SEG_ABS subcase of (ii).
817 if (is_global) {
818 struct Public *pub;
820 pub = *fpubtail = nasm_malloc(sizeof(*pub));
821 fpubtail = &pub->next;
822 pub->next = NULL;
823 pub->name = nasm_strdup(name);
824 pub->offset = offset;
825 pub->segment = (segment == NO_SEG ? 0 : segment & ~SEG_ABS);
827 if (special)
828 nasm_nonfatal("OBJ supports no special symbol features"
829 " for this symbol type");
830 return;
834 * If `any_segs' is still false, we might need to define a
835 * default segment, if they're trying to declare a label in
836 * `first_seg'.
838 if (!any_segs && segment == first_seg) {
839 int tempint; /* ignored */
840 if (segment != obj_segment("__NASMDEFSEG", 2, &tempint))
841 nasm_panic("strange segment conditions in OBJ driver");
844 for (seg = seghead; seg && is_global; seg = seg->next)
845 if (seg->index == segment) {
846 struct Public *loc = nasm_malloc(sizeof(*loc));
848 * Case (ii). Maybe MODPUB someday?
850 *seg->pubtail = loc;
851 seg->pubtail = &loc->next;
852 loc->next = NULL;
853 loc->name = nasm_strdup(name);
854 loc->offset = offset;
856 if (special)
857 nasm_nonfatal("OBJ supports no special symbol features"
858 " for this symbol type");
859 return;
863 * Case (iii).
865 if (is_global) {
866 ext = *exttail = nasm_malloc(sizeof(*ext));
867 ext->next = NULL;
868 exttail = &ext->next;
869 ext->name = name;
870 /* Place by default all externs into the current segment */
871 ext->defwrt_type = DEFWRT_NONE;
873 /* 28-Apr-2002 - John Coffman
874 The following code was introduced on 12-Aug-2000, and breaks fixups
875 on code passed thru the MSC 5.1 linker (3.66) and MSC 6.00A linker
876 (5.10). It was introduced after FIXUP32 was added, and may be needed
877 for 32-bit segments. The following will get 16-bit segments working
878 again, and maybe someone can correct the 'if' condition which is
879 actually needed.
881 #if 0
882 if (current_seg) {
883 #else
884 if (current_seg && current_seg->use32) {
885 if (current_seg->grp) {
886 ext->defwrt_type = DEFWRT_GROUP;
887 ext->defwrt_ptr.grp = current_seg->grp;
888 } else {
889 ext->defwrt_type = DEFWRT_SEGMENT;
890 ext->defwrt_ptr.seg = current_seg;
893 #endif
895 if (is_global == 2) {
896 ext->commonsize = offset;
897 ext->commonelem = 1; /* default FAR */
898 } else
899 ext->commonsize = 0;
900 } else
901 return;
904 * Now process the special text, if any, to find default-WRT
905 * specifications and common-variable element-size and near/far
906 * specifications.
908 while (special && *special) {
909 used_special = true;
912 * We might have a default-WRT specification.
914 if (!nasm_strnicmp(special, "wrt", 3)) {
915 char *p;
916 int len;
917 special += 3;
918 special += strspn(special, " \t");
919 p = nasm_strndup(special, len = strcspn(special, ":"));
920 obj_ext_set_defwrt(ext, p);
921 special += len;
922 if (*special && *special != ':')
923 nasm_nonfatal("`:' expected in special symbol"
924 " text for `%s'", ext->name);
925 else if (*special == ':')
926 special++;
930 * The NEAR or FAR keywords specify nearness or
931 * farness. FAR gives default element size 1.
933 if (!nasm_strnicmp(special, "far", 3)) {
934 if (ext->commonsize)
935 ext->commonelem = 1;
936 else
937 nasm_nonfatal("`%s': `far' keyword may only be applied"
938 " to common variables\n", ext->name);
939 special += 3;
940 special += strspn(special, " \t");
941 } else if (!nasm_strnicmp(special, "near", 4)) {
942 if (ext->commonsize)
943 ext->commonelem = 0;
944 else
945 nasm_nonfatal("`%s': `far' keyword may only be applied"
946 " to common variables\n", ext->name);
947 special += 4;
948 special += strspn(special, " \t");
952 * If it's a common, and anything else remains on the line
953 * before a further colon, evaluate it as an expression and
954 * use that as the element size. Forward references aren't
955 * allowed.
957 if (*special == ':')
958 special++;
959 else if (*special) {
960 if (ext->commonsize) {
961 expr *e;
962 struct tokenval tokval;
964 stdscan_reset();
965 stdscan_set(special);
966 tokval.t_type = TOKEN_INVALID;
967 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
968 if (e) {
969 if (!is_simple(e))
970 nasm_nonfatal("cannot use relocatable"
971 " expression as common-variable element size");
972 else
973 ext->commonelem = reloc_value(e);
975 special = stdscan_get();
976 } else {
977 nasm_nonfatal("`%s': element-size specifications only"
978 " apply to common variables", ext->name);
979 while (*special && *special != ':')
980 special++;
981 if (*special == ':')
982 special++;
987 i = segment / 2;
988 eb = ebhead;
989 if (!eb) {
990 eb = *ebtail = nasm_zalloc(sizeof(*eb));
991 eb->next = NULL;
992 ebtail = &eb->next;
994 while (i >= EXT_BLKSIZ) {
995 if (eb && eb->next)
996 eb = eb->next;
997 else {
998 eb = *ebtail = nasm_zalloc(sizeof(*eb));
999 eb->next = NULL;
1000 ebtail = &eb->next;
1002 i -= EXT_BLKSIZ;
1004 eb->exts[i] = ext;
1005 ext->index = ++externals;
1007 if (special && !used_special)
1008 nasm_nonfatal("OBJ supports no special symbol features"
1009 " for this symbol type");
1012 /* forward declaration */
1013 static void obj_write_fixup(ObjRecord * orp, int bytes,
1014 int segrel, int32_t seg, int32_t wrt,
1015 struct Segment *segto);
1017 static void obj_out(int32_t segto, const void *data,
1018 enum out_type type, uint64_t size,
1019 int32_t segment, int32_t wrt)
1021 const uint8_t *ucdata;
1022 int32_t ldata;
1023 struct Segment *seg;
1024 ObjRecord *orp;
1027 * If `any_segs' is still false, we must define a default
1028 * segment.
1030 if (!any_segs) {
1031 int tempint; /* ignored */
1032 if (segto != obj_segment("__NASMDEFSEG", 2, &tempint))
1033 nasm_panic("strange segment conditions in OBJ driver");
1037 * Find the segment we are targetting.
1039 for (seg = seghead; seg; seg = seg->next)
1040 if (seg->index == segto)
1041 break;
1042 if (!seg)
1043 nasm_panic("code directed to nonexistent segment?");
1045 orp = seg->orp;
1046 orp->parm[0] = seg->currentpos;
1048 switch (type) {
1049 case OUT_RAWDATA:
1050 ucdata = data;
1051 while (size > 0) {
1052 unsigned int len;
1053 orp = obj_check(seg->orp, 1);
1054 len = RECORD_MAX - orp->used;
1055 if (len > size)
1056 len = size;
1057 memcpy(orp->buf + orp->used, ucdata, len);
1058 orp->committed = orp->used += len;
1059 orp->parm[0] = seg->currentpos += len;
1060 ucdata += len;
1061 size -= len;
1063 break;
1065 case OUT_ADDRESS:
1066 case OUT_REL1ADR:
1067 case OUT_REL2ADR:
1068 case OUT_REL4ADR:
1069 case OUT_REL8ADR:
1071 int rsize;
1073 if (type == OUT_ADDRESS)
1074 size = abs((int)size);
1076 if (segment == NO_SEG && type != OUT_ADDRESS)
1077 nasm_nonfatal("relative call to absolute address not"
1078 " supported by OBJ format");
1079 if (segment >= SEG_ABS)
1080 nasm_nonfatal("far-absolute relocations not supported"
1081 " by OBJ format");
1083 ldata = *(int64_t *)data;
1084 if (type != OUT_ADDRESS) {
1086 * For 16-bit and 32-bit x86 code, the size and realsize() always
1087 * matches as only jumps, calls and loops uses PC relative
1088 * addressing and the address isn't followed by any other opcode
1089 * bytes. In 64-bit mode there is RIP relative addressing which
1090 * means the fixup location can be followed by an immediate value,
1091 * meaning that size > realsize().
1093 * When the CPU is calculating the effective address, it takes the
1094 * RIP at the end of the instruction and adds the fixed up relative
1095 * address value to it.
1097 * The linker's point of reference is the end of the fixup location
1098 * (which is the end of the instruction for Jcc, CALL, LOOP[cc]).
1099 * It is calculating distance between the target symbol and the end
1100 * of the fixup location, and add this to the displacement value we
1101 * are calculating here and storing at the fixup location.
1103 * To get the right effect, we need to _reduce_ the displacement
1104 * value by the number of bytes following the fixup.
1106 * Example:
1107 * data at address 0x100; REL4ADR at 0x050, 4 byte immediate,
1108 * end of fixup at 0x054, end of instruction at 0x058.
1109 * => size = 8.
1110 * => realsize() -> 4
1111 * => CPU needs a value of: 0x100 - 0x058 = 0x0a8
1112 * => linker/loader will add: 0x100 - 0x054 = 0x0ac
1113 * => We must add an addend of -4.
1114 * => realsize() - size = -4.
1116 * The code used to do size - realsize() at least since v0.90,
1117 * probably because it wasn't needed...
1119 ldata -= size;
1120 size = realsize(type, size);
1121 ldata += size;
1124 switch (size) {
1125 default:
1126 nasm_nonfatal("OBJ format can only handle 16- or "
1127 "32-byte relocations");
1128 segment = NO_SEG; /* Don't actually generate a relocation */
1129 break;
1130 case 2:
1131 orp = obj_word(orp, ldata);
1132 break;
1133 case 4:
1134 orp = obj_dword(orp, ldata);
1135 break;
1138 rsize = size;
1139 if (segment < SEG_ABS && (segment != NO_SEG && segment % 2) &&
1140 size == 4) {
1142 * This is a 4-byte segment-base relocation such as
1143 * `MOV EAX,SEG foo'. OBJ format can't actually handle
1144 * these, but if the constant term has the 16 low bits
1145 * zero, we can just apply a 2-byte segment-base
1146 * relocation to the low word instead.
1148 rsize = 2;
1149 if (ldata & 0xFFFF)
1150 nasm_nonfatal("OBJ format cannot handle complex"
1151 " dword-size segment base references");
1153 if (segment != NO_SEG)
1154 obj_write_fixup(orp, rsize,
1155 (type == OUT_ADDRESS ? 0x4000 : 0),
1156 segment, wrt, seg);
1157 seg->currentpos += size;
1158 break;
1161 default:
1162 nasm_nonfatal("Relocation type not supported by output format");
1163 /* fall through */
1165 case OUT_RESERVE:
1166 if (orp->committed)
1167 orp = obj_bump(orp);
1168 seg->currentpos += size;
1169 break;
1171 obj_commit(orp);
1174 static void obj_write_fixup(ObjRecord * orp, int bytes,
1175 int segrel, int32_t seg, int32_t wrt,
1176 struct Segment *segto)
1178 unsigned locat;
1179 int method;
1180 int base;
1181 int32_t tidx, fidx;
1182 struct Segment *s = NULL;
1183 struct Group *g = NULL;
1184 struct External *e = NULL;
1185 ObjRecord *forp;
1187 if (bytes != 2 && bytes != 4) {
1188 nasm_nonfatal("`obj' output driver does not support"
1189 " %d-bit relocations", bytes << 3);
1190 return;
1193 forp = orp->child;
1194 if (forp == NULL) {
1195 orp->child = forp = obj_new();
1196 forp->up = &(orp->child);
1197 /* We should choose between FIXUPP and FIXU32 record type */
1198 /* If we're targeting a 32-bit segment, use a FIXU32 record */
1199 if (segto->use32)
1200 forp->type = FIXU32;
1201 else
1202 forp->type = FIXUPP;
1205 if (seg % 2) {
1206 base = true;
1207 locat = FIX_16_SELECTOR;
1208 seg--;
1209 if (bytes != 2)
1210 nasm_panic("OBJ: 4-byte segment base fixup got"
1211 " through sanity check");
1212 } else {
1213 base = false;
1214 locat = (bytes == 2) ? FIX_16_OFFSET : FIX_32_OFFSET;
1215 if (!segrel)
1217 * There is a bug in tlink that makes it process self relative
1218 * fixups incorrectly if the x_size doesn't match the location
1219 * size.
1221 forp = obj_force(forp, bytes << 3);
1224 forp = obj_rword(forp, locat | segrel | (orp->parm[0] - orp->parm[2]));
1226 tidx = fidx = -1, method = 0; /* placate optimisers */
1229 * See if we can find the segment ID in our segment list. If
1230 * so, we have a T4 (LSEG) target.
1232 for (s = seghead; s; s = s->next)
1233 if (s->index == seg)
1234 break;
1235 if (s)
1236 method = 4, tidx = s->obj_index;
1237 else {
1238 for (g = grphead; g; g = g->next)
1239 if (g->index == seg)
1240 break;
1241 if (g)
1242 method = 5, tidx = g->obj_index;
1243 else {
1244 int32_t i = seg / 2;
1245 struct ExtBack *eb = ebhead;
1246 while (i >= EXT_BLKSIZ) {
1247 if (eb)
1248 eb = eb->next;
1249 else
1250 break;
1251 i -= EXT_BLKSIZ;
1253 if (eb)
1254 method = 6, e = eb->exts[i], tidx = e->index;
1255 else
1256 nasm_panic("unrecognised segment value in obj_write_fixup");
1261 * If no WRT given, assume the natural default, which is method
1262 * F5 unless:
1264 * - we are doing an OFFSET fixup for a grouped segment, in
1265 * which case we require F1 (group).
1267 * - we are doing an OFFSET fixup for an external with a
1268 * default WRT, in which case we must honour the default WRT.
1270 if (wrt == NO_SEG) {
1271 if (!base && s && s->grp)
1272 method |= 0x10, fidx = s->grp->obj_index;
1273 else if (!base && e && e->defwrt_type != DEFWRT_NONE) {
1274 if (e->defwrt_type == DEFWRT_SEGMENT)
1275 method |= 0x00, fidx = e->defwrt_ptr.seg->obj_index;
1276 else if (e->defwrt_type == DEFWRT_GROUP)
1277 method |= 0x10, fidx = e->defwrt_ptr.grp->obj_index;
1278 else {
1279 nasm_nonfatal("default WRT specification for"
1280 " external `%s' unresolved", e->name);
1281 method |= 0x50, fidx = -1; /* got to do _something_ */
1283 } else
1284 method |= 0x50, fidx = -1;
1285 } else {
1287 * See if we can find the WRT-segment ID in our segment
1288 * list. If so, we have a F0 (LSEG) frame.
1290 for (s = seghead; s; s = s->next)
1291 if (s->index == wrt - 1)
1292 break;
1293 if (s)
1294 method |= 0x00, fidx = s->obj_index;
1295 else {
1296 for (g = grphead; g; g = g->next)
1297 if (g->index == wrt - 1)
1298 break;
1299 if (g)
1300 method |= 0x10, fidx = g->obj_index;
1301 else {
1302 int32_t i = wrt / 2;
1303 struct ExtBack *eb = ebhead;
1304 while (i >= EXT_BLKSIZ) {
1305 if (eb)
1306 eb = eb->next;
1307 else
1308 break;
1309 i -= EXT_BLKSIZ;
1311 if (eb)
1312 method |= 0x20, fidx = eb->exts[i]->index;
1313 else
1314 nasm_panic("unrecognised WRT value in obj_write_fixup");
1319 forp = obj_byte(forp, method);
1320 if (fidx != -1)
1321 forp = obj_index(forp, fidx);
1322 forp = obj_index(forp, tidx);
1323 obj_commit(forp);
1326 static int32_t obj_segment(char *name, int pass, int *bits)
1329 * We call the label manager here to define a name for the new
1330 * segment, and when our _own_ label-definition stub gets
1331 * called in return, it should register the new segment name
1332 * using the pointer it gets passed. That way we save memory,
1333 * by sponging off the label manager.
1335 #if defined(DEBUG) && DEBUG>=3
1336 nasm_debug(" obj_segment: < %s >, pass=%d, *bits=%d\n",
1337 name, pass, *bits);
1338 #endif
1339 if (!name) {
1340 *bits = 16;
1341 current_seg = NULL;
1342 return first_seg;
1343 } else {
1344 struct Segment *seg;
1345 struct Group *grp;
1346 struct External **extp;
1347 int obj_idx, i, attrs;
1348 bool rn_error;
1349 char *p;
1352 * Look for segment attributes.
1354 attrs = 0;
1355 while (*name == '.')
1356 name++; /* hack, but a documented one */
1357 p = name;
1358 while (*p && !nasm_isspace(*p))
1359 p++;
1360 if (*p) {
1361 *p++ = '\0';
1362 while (*p && nasm_isspace(*p))
1363 *p++ = '\0';
1365 while (*p) {
1366 while (*p && !nasm_isspace(*p))
1367 p++;
1368 if (*p) {
1369 *p++ = '\0';
1370 while (*p && nasm_isspace(*p))
1371 *p++ = '\0';
1374 attrs++;
1377 for (seg = seghead, obj_idx = 1; ; seg = seg->next, obj_idx++) {
1378 if (!seg)
1379 break;
1381 if (!strcmp(seg->name, name)) {
1382 if (attrs > 0 && pass == 1)
1383 nasm_warn(WARN_OTHER, "segment attributes specified on"
1384 " redeclaration of segment: ignoring");
1385 if (seg->use32)
1386 *bits = 32;
1387 else
1388 *bits = 16;
1389 current_seg = seg;
1390 return seg->index;
1394 *segtail = seg = nasm_malloc(sizeof(*seg));
1395 seg->next = NULL;
1396 segtail = &seg->next;
1397 seg->index = (any_segs ? seg_alloc() : first_seg);
1398 seg->obj_index = obj_idx;
1399 seg->grp = NULL;
1400 any_segs = true;
1401 seg->name = nasm_strdup(name);
1402 seg->currentpos = 0;
1403 seg->align = 1; /* default */
1404 seg->use32 = false; /* default */
1405 seg->combine = CMB_PUBLIC; /* default */
1406 seg->segclass = seg->overlay = NULL;
1407 seg->pubhead = NULL;
1408 seg->pubtail = &seg->pubhead;
1409 seg->lochead = NULL;
1410 seg->loctail = &seg->lochead;
1411 seg->orp = obj_new();
1412 seg->orp->up = &(seg->orp);
1413 seg->orp->ori = ori_ledata;
1414 seg->orp->type = LEDATA;
1415 seg->orp->parm[1] = obj_idx;
1418 * Process the segment attributes.
1420 p = name;
1421 while (attrs--) {
1422 p += strlen(p);
1423 while (!*p)
1424 p++;
1427 * `p' contains a segment attribute.
1429 if (!nasm_stricmp(p, "private"))
1430 seg->combine = CMB_PRIVATE;
1431 else if (!nasm_stricmp(p, "public"))
1432 seg->combine = CMB_PUBLIC;
1433 else if (!nasm_stricmp(p, "common"))
1434 seg->combine = CMB_COMMON;
1435 else if (!nasm_stricmp(p, "stack"))
1436 seg->combine = CMB_STACK;
1437 else if (!nasm_stricmp(p, "use16"))
1438 seg->use32 = false;
1439 else if (!nasm_stricmp(p, "use32"))
1440 seg->use32 = true;
1441 else if (!nasm_stricmp(p, "flat")) {
1443 * This segment is an OS/2 FLAT segment. That means
1444 * that its default group is group FLAT, even if
1445 * the group FLAT does not explicitly _contain_ the
1446 * segment.
1448 * When we see this, we must create the group
1449 * `FLAT', containing no segments, if it does not
1450 * already exist; then we must set the default
1451 * group of this segment to be the FLAT group.
1453 struct Group *grp;
1454 for (grp = grphead; grp; grp = grp->next)
1455 if (!strcmp(grp->name, "FLAT"))
1456 break;
1457 if (!grp) {
1458 obj_directive(D_GROUP, "FLAT", 1);
1459 for (grp = grphead; grp; grp = grp->next)
1460 if (!strcmp(grp->name, "FLAT"))
1461 break;
1462 if (!grp)
1463 nasm_panic("failure to define FLAT?!");
1465 seg->grp = grp;
1466 } else if (!nasm_strnicmp(p, "class=", 6))
1467 seg->segclass = nasm_strdup(p + 6);
1468 else if (!nasm_strnicmp(p, "overlay=", 8))
1469 seg->overlay = nasm_strdup(p + 8);
1470 else if (!nasm_strnicmp(p, "align=", 6)) {
1471 seg->align = readnum(p + 6, &rn_error);
1472 if (rn_error) {
1473 seg->align = 1;
1474 nasm_nonfatal("segment alignment should be numeric");
1476 switch (seg->align) {
1477 case 1: /* BYTE */
1478 case 2: /* WORD */
1479 case 4: /* DWORD */
1480 case 16: /* PARA */
1481 case 256: /* PAGE */
1482 case 4096: /* PharLap extension */
1483 break;
1484 case 8:
1485 nasm_warn(WARN_OTHER, "OBJ format does not support alignment"
1486 " of 8: rounding up to 16");
1487 seg->align = 16;
1488 break;
1489 case 32:
1490 case 64:
1491 case 128:
1492 nasm_warn(WARN_OTHER, "OBJ format does not support alignment"
1493 " of %d: rounding up to 256", seg->align);
1494 seg->align = 256;
1495 break;
1496 case 512:
1497 case 1024:
1498 case 2048:
1499 nasm_warn(WARN_OTHER, "OBJ format does not support alignment"
1500 " of %d: rounding up to 4096", seg->align);
1501 seg->align = 4096;
1502 break;
1503 default:
1504 nasm_nonfatal("invalid alignment value %d",
1505 seg->align);
1506 seg->align = 1;
1507 break;
1509 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
1510 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
1511 if (rn_error)
1512 nasm_nonfatal("argument to `absolute' segment"
1513 " attribute should be numeric");
1517 /* We need to know whenever we have at least one 32-bit segment */
1518 obj_use32 |= seg->use32;
1520 obj_seg_needs_update = seg;
1521 if (seg->align >= SEG_ABS)
1522 define_label(name, NO_SEG, seg->align - SEG_ABS, false);
1523 else
1524 define_label(name, seg->index + 1, 0L, false);
1525 obj_seg_needs_update = NULL;
1528 * See if this segment is defined in any groups.
1530 for (grp = grphead; grp; grp = grp->next) {
1531 for (i = grp->nindices; i < grp->nentries; i++) {
1532 if (!strcmp(grp->segs[i].name, seg->name)) {
1533 nasm_free(grp->segs[i].name);
1534 grp->segs[i] = grp->segs[grp->nindices];
1535 grp->segs[grp->nindices++].index = seg->obj_index;
1536 if (seg->grp)
1537 nasm_warn(WARN_OTHER, "segment `%s' is already part of"
1538 " a group: first one takes precedence",
1539 seg->name);
1540 else
1541 seg->grp = grp;
1547 * Walk through the list of externals with unresolved
1548 * default-WRT clauses, and resolve any that point at this
1549 * segment.
1551 extp = &dws;
1552 while (*extp) {
1553 if ((*extp)->defwrt_type == DEFWRT_STRING &&
1554 !strcmp((*extp)->defwrt_ptr.string, seg->name)) {
1555 nasm_free((*extp)->defwrt_ptr.string);
1556 (*extp)->defwrt_type = DEFWRT_SEGMENT;
1557 (*extp)->defwrt_ptr.seg = seg;
1558 *extp = (*extp)->next_dws;
1559 } else
1560 extp = &(*extp)->next_dws;
1563 if (seg->use32)
1564 *bits = 32;
1565 else
1566 *bits = 16;
1567 current_seg = seg;
1568 return seg->index;
1572 static enum directive_result
1573 obj_directive(enum directive directive, char *value, int pass)
1575 switch (directive) {
1576 case D_GROUP:
1578 char *p, *q, *v;
1579 if (pass == 1) {
1580 struct Group *grp;
1581 struct Segment *seg;
1582 struct External **extp;
1583 int obj_idx;
1585 q = value;
1586 while (*q == '.')
1587 q++; /* hack, but a documented one */
1588 v = q;
1589 while (*q && !nasm_isspace(*q))
1590 q++;
1591 if (nasm_isspace(*q)) {
1592 *q++ = '\0';
1593 while (*q && nasm_isspace(*q))
1594 q++;
1597 * Here we used to sanity-check the group directive to
1598 * ensure nobody tried to declare a group containing no
1599 * segments. However, OS/2 does this as standard
1600 * practice, so the sanity check has been removed.
1602 * if (!*q) {
1603 * nasm_error(ERR_NONFATAL,"GROUP directive contains no segments");
1604 * return DIRR_ERROR;
1608 obj_idx = 1;
1609 for (grp = grphead; grp; grp = grp->next) {
1610 obj_idx++;
1611 if (!strcmp(grp->name, v)) {
1612 nasm_nonfatal("group `%s' defined twice", v);
1613 return DIRR_ERROR;
1617 *grptail = grp = nasm_malloc(sizeof(*grp));
1618 grp->next = NULL;
1619 grptail = &grp->next;
1620 grp->index = seg_alloc();
1621 grp->obj_index = obj_idx;
1622 grp->nindices = grp->nentries = 0;
1623 grp->name = NULL;
1625 obj_grp_needs_update = grp;
1626 backend_label(v, grp->index + 1, 0L);
1627 obj_grp_needs_update = NULL;
1629 while (*q) {
1630 p = q;
1631 while (*q && !nasm_isspace(*q))
1632 q++;
1633 if (nasm_isspace(*q)) {
1634 *q++ = '\0';
1635 while (*q && nasm_isspace(*q))
1636 q++;
1639 * Now p contains a segment name. Find it.
1641 for (seg = seghead; seg; seg = seg->next)
1642 if (!strcmp(seg->name, p))
1643 break;
1644 if (seg) {
1646 * We have a segment index. Shift a name entry
1647 * to the end of the array to make room.
1649 grp->segs[grp->nentries++] = grp->segs[grp->nindices];
1650 grp->segs[grp->nindices++].index = seg->obj_index;
1651 if (seg->grp)
1652 nasm_warn(WARN_OTHER, "segment `%s' is already part of"
1653 " a group: first one takes precedence",
1654 seg->name);
1655 else
1656 seg->grp = grp;
1657 } else {
1659 * We have an as-yet undefined segment.
1660 * Remember its name, for later.
1662 grp->segs[grp->nentries++].name = nasm_strdup(p);
1667 * Walk through the list of externals with unresolved
1668 * default-WRT clauses, and resolve any that point at
1669 * this group.
1671 extp = &dws;
1672 while (*extp) {
1673 if ((*extp)->defwrt_type == DEFWRT_STRING &&
1674 !strcmp((*extp)->defwrt_ptr.string, grp->name)) {
1675 nasm_free((*extp)->defwrt_ptr.string);
1676 (*extp)->defwrt_type = DEFWRT_GROUP;
1677 (*extp)->defwrt_ptr.grp = grp;
1678 *extp = (*extp)->next_dws;
1679 } else
1680 extp = &(*extp)->next_dws;
1683 return DIRR_OK;
1685 case D_UPPERCASE:
1686 obj_uppercase = true;
1687 return DIRR_OK;
1689 case D_IMPORT:
1691 char *q, *extname, *libname, *impname;
1693 if (pass == 2)
1694 return 1; /* ignore in pass two */
1695 extname = q = value;
1696 while (*q && !nasm_isspace(*q))
1697 q++;
1698 if (nasm_isspace(*q)) {
1699 *q++ = '\0';
1700 while (*q && nasm_isspace(*q))
1701 q++;
1704 libname = q;
1705 while (*q && !nasm_isspace(*q))
1706 q++;
1707 if (nasm_isspace(*q)) {
1708 *q++ = '\0';
1709 while (*q && nasm_isspace(*q))
1710 q++;
1713 impname = q;
1715 if (!*extname || !*libname)
1716 nasm_nonfatal("`import' directive requires symbol name"
1717 " and library name");
1718 else {
1719 struct ImpDef *imp;
1720 bool err = false;
1722 imp = *imptail = nasm_malloc(sizeof(struct ImpDef));
1723 imptail = &imp->next;
1724 imp->next = NULL;
1725 imp->extname = nasm_strdup(extname);
1726 imp->libname = nasm_strdup(libname);
1727 imp->impindex = readnum(impname, &err);
1728 if (!*impname || err)
1729 imp->impname = nasm_strdup(impname);
1730 else
1731 imp->impname = NULL;
1734 return DIRR_OK;
1736 case D_EXPORT:
1738 char *q, *extname, *intname, *v;
1739 struct ExpDef *export;
1740 int flags = 0;
1741 unsigned int ordinal = 0;
1743 if (pass == 2)
1744 return DIRR_OK; /* ignore in pass two */
1745 intname = q = value;
1746 while (*q && !nasm_isspace(*q))
1747 q++;
1748 if (nasm_isspace(*q)) {
1749 *q++ = '\0';
1750 while (*q && nasm_isspace(*q))
1751 q++;
1754 extname = q;
1755 while (*q && !nasm_isspace(*q))
1756 q++;
1757 if (nasm_isspace(*q)) {
1758 *q++ = '\0';
1759 while (*q && nasm_isspace(*q))
1760 q++;
1763 if (!*intname) {
1764 nasm_nonfatal("`export' directive requires export name");
1765 return DIRR_OK;
1767 if (!*extname) {
1768 extname = intname;
1769 intname = "";
1771 while (*q) {
1772 v = q;
1773 while (*q && !nasm_isspace(*q))
1774 q++;
1775 if (nasm_isspace(*q)) {
1776 *q++ = '\0';
1777 while (*q && nasm_isspace(*q))
1778 q++;
1780 if (!nasm_stricmp(v, "resident"))
1781 flags |= EXPDEF_FLAG_RESIDENT;
1782 else if (!nasm_stricmp(v, "nodata"))
1783 flags |= EXPDEF_FLAG_NODATA;
1784 else if (!nasm_strnicmp(v, "parm=", 5)) {
1785 bool err = false;
1786 flags |= EXPDEF_MASK_PARMCNT & readnum(v + 5, &err);
1787 if (err) {
1788 nasm_nonfatal("value `%s' for `parm' is non-numeric", v + 5);
1789 return DIRR_ERROR;
1791 } else {
1792 bool err = false;
1793 ordinal = readnum(v, &err);
1794 if (err) {
1795 nasm_nonfatal("unrecognised export qualifier `%s'", v);
1796 return DIRR_ERROR;
1798 flags |= EXPDEF_FLAG_ORDINAL;
1802 export = *exptail = nasm_malloc(sizeof(struct ExpDef));
1803 exptail = &export->next;
1804 export->next = NULL;
1805 export->extname = nasm_strdup(extname);
1806 export->intname = nasm_strdup(intname);
1807 export->ordinal = ordinal;
1808 export->flags = flags;
1810 return DIRR_OK;
1812 default:
1813 return DIRR_UNKNOWN;
1817 static void obj_sectalign(int32_t seg, unsigned int value)
1819 struct Segment *s;
1821 list_for_each(s, seghead) {
1822 if (s->index == seg)
1823 break;
1827 * it should not be too big value
1828 * and applied on non-absolute sections
1830 if (!s || !is_power2(value) ||
1831 value > 4096 || s->align >= SEG_ABS)
1832 return;
1835 * FIXME: No code duplication please
1836 * consider making helper for this
1837 * mapping since section handler has
1838 * to do the same
1840 switch (value) {
1841 case 8:
1842 value = 16;
1843 break;
1844 case 32:
1845 case 64:
1846 case 128:
1847 value = 256;
1848 break;
1849 case 512:
1850 case 1024:
1851 case 2048:
1852 value = 4096;
1853 break;
1856 if (s->align < (int)value)
1857 s->align = value;
1860 static int32_t obj_segbase(int32_t segment)
1862 struct Segment *seg;
1865 * Find the segment in our list.
1867 for (seg = seghead; seg; seg = seg->next)
1868 if (seg->index == segment - 1)
1869 break;
1871 if (!seg) {
1873 * Might be an external with a default WRT.
1875 int32_t i = segment / 2;
1876 struct ExtBack *eb = ebhead;
1877 struct External *e;
1879 while (i >= EXT_BLKSIZ) {
1880 if (eb)
1881 eb = eb->next;
1882 else
1883 break;
1884 i -= EXT_BLKSIZ;
1886 if (eb) {
1887 e = eb->exts[i];
1888 if (!e) {
1889 /* Not available yet, probably a forward reference */
1890 nasm_assert(pass0 < 2); /* Convergence failure */
1891 return NO_SEG;
1894 switch (e->defwrt_type) {
1895 case DEFWRT_NONE:
1896 return segment; /* fine */
1897 case DEFWRT_SEGMENT:
1898 return e->defwrt_ptr.seg->index + 1;
1899 case DEFWRT_GROUP:
1900 return e->defwrt_ptr.grp->index + 1;
1901 default:
1902 return NO_SEG; /* can't tell what it is */
1906 return segment; /* not one of ours - leave it alone */
1909 if (seg->align >= SEG_ABS)
1910 return seg->align; /* absolute segment */
1911 if (seg->grp)
1912 return seg->grp->index + 1; /* grouped segment */
1914 return segment; /* no special treatment */
1917 /* Get a file timestamp in MS-DOS format */
1918 static uint32_t obj_file_timestamp(const char *pathname)
1920 time_t t;
1921 const struct tm *lt;
1923 if (!nasm_file_time(&t, pathname))
1924 return 0;
1926 lt = localtime(&t);
1927 if (!lt)
1928 return 0;
1930 if (lt->tm_year < 80 || lt->tm_year > 207)
1931 return 0; /* Only years 1980-2107 representable */
1933 return
1934 ((uint32_t)lt->tm_sec >> 1) +
1935 ((uint32_t)lt->tm_min << 5) +
1936 ((uint32_t)lt->tm_hour << 11) +
1937 ((uint32_t)lt->tm_mday << 16) +
1938 (((uint32_t)lt->tm_mon + 1) << 21) +
1939 (((uint32_t)lt->tm_year - 80) << 25);
1942 static void obj_write_file(void)
1944 struct Segment *seg, *entry_seg_ptr = 0;
1945 struct FileName *fn;
1946 struct LineNumber *ln;
1947 struct Group *grp;
1948 struct Public *pub, *loc;
1949 struct External *ext;
1950 struct ImpDef *imp;
1951 struct ExpDef *export;
1952 int lname_idx;
1953 ObjRecord *orp;
1954 const struct strlist_entry *depfile;
1955 const bool debuginfo = (dfmt == &borland_debug_form);
1958 * Write the THEADR module header.
1960 orp = obj_new();
1961 orp->type = THEADR;
1962 obj_name(orp, obj_infile);
1963 obj_emit2(orp);
1966 * Write the NASM boast comment.
1968 orp->type = COMENT;
1969 obj_rword(orp, dTRANSL);
1970 obj_name(orp, nasm_comment());
1971 obj_emit2(orp);
1974 * Output file dependency information
1976 if (!obj_nodepend && depend_list) {
1977 strlist_for_each(depfile, depend_list) {
1978 uint32_t ts;
1980 ts = obj_file_timestamp(depfile->str);
1981 if (ts) {
1982 orp->type = COMENT;
1983 obj_rword(orp, dDEPFILE);
1984 obj_dword(orp, ts);
1985 obj_name(orp, depfile->str);
1986 obj_emit2(orp);
1991 orp->type = COMENT;
1993 * Write the IMPDEF records, if any.
1995 for (imp = imphead; imp; imp = imp->next) {
1996 obj_rword(orp, dOMFEXT);
1997 obj_byte(orp, 1); /* subfunction 1: IMPDEF */
1998 if (imp->impname)
1999 obj_byte(orp, 0); /* import by name */
2000 else
2001 obj_byte(orp, 1); /* import by ordinal */
2002 obj_name(orp, imp->extname);
2003 obj_name(orp, imp->libname);
2004 if (imp->impname)
2005 obj_name(orp, imp->impname);
2006 else
2007 obj_word(orp, imp->impindex);
2008 obj_emit2(orp);
2012 * Write the EXPDEF records, if any.
2014 for (export = exphead; export; export = export->next) {
2015 obj_rword(orp, dOMFEXT);
2016 obj_byte(orp, 2); /* subfunction 2: EXPDEF */
2017 obj_byte(orp, export->flags);
2018 obj_name(orp, export->extname);
2019 obj_name(orp, export->intname);
2020 if (export->flags & EXPDEF_FLAG_ORDINAL)
2021 obj_word(orp, export->ordinal);
2022 obj_emit2(orp);
2025 /* we're using extended OMF if we put in debug info */
2026 if (debuginfo) {
2027 orp->type = COMENT;
2028 obj_rword(orp, dEXTENDED);
2029 obj_emit2(orp);
2033 * Write the first LNAMES record, containing LNAME one, which
2034 * is null. Also initialize the LNAME counter.
2036 orp->type = LNAMES;
2037 obj_byte(orp, 0);
2038 lname_idx = 1;
2040 * Write some LNAMES for the segment names
2042 for (seg = seghead; seg; seg = seg->next) {
2043 orp = obj_name(orp, seg->name);
2044 if (seg->segclass)
2045 orp = obj_name(orp, seg->segclass);
2046 if (seg->overlay)
2047 orp = obj_name(orp, seg->overlay);
2048 obj_commit(orp);
2051 * Write some LNAMES for the group names
2053 for (grp = grphead; grp; grp = grp->next) {
2054 orp = obj_name(orp, grp->name);
2055 obj_commit(orp);
2057 obj_emit(orp);
2060 * Write the SEGDEF records.
2062 orp->type = SEGDEF;
2063 for (seg = seghead; seg; seg = seg->next) {
2064 int acbp;
2065 uint32_t seglen = seg->currentpos;
2067 acbp = (seg->combine << 2); /* C field */
2069 if (seg->use32)
2070 acbp |= 0x01; /* P bit is Use32 flag */
2071 else if (seglen == 0x10000L) {
2072 seglen = 0; /* This special case may be needed for old linkers */
2073 acbp |= 0x02; /* B bit */
2076 /* A field */
2077 if (seg->align >= SEG_ABS)
2078 /* acbp |= 0x00 */ ;
2079 else if (seg->align >= 4096) {
2080 if (seg->align > 4096)
2081 nasm_nonfatal("segment `%s' requires more alignment"
2082 " than OBJ format supports", seg->name);
2083 acbp |= 0xC0; /* PharLap extension */
2084 } else if (seg->align >= 256) {
2085 acbp |= 0x80;
2086 } else if (seg->align >= 16) {
2087 acbp |= 0x60;
2088 } else if (seg->align >= 4) {
2089 acbp |= 0xA0;
2090 } else if (seg->align >= 2) {
2091 acbp |= 0x40;
2092 } else
2093 acbp |= 0x20;
2095 obj_byte(orp, acbp);
2096 if (seg->align & SEG_ABS) {
2097 obj_x(orp, seg->align - SEG_ABS); /* Frame */
2098 obj_byte(orp, 0); /* Offset */
2100 obj_x(orp, seglen);
2101 obj_index(orp, ++lname_idx);
2102 obj_index(orp, seg->segclass ? ++lname_idx : 1);
2103 obj_index(orp, seg->overlay ? ++lname_idx : 1);
2104 obj_emit2(orp);
2108 * Write the GRPDEF records.
2110 orp->type = GRPDEF;
2111 for (grp = grphead; grp; grp = grp->next) {
2112 int i;
2114 if (grp->nindices != grp->nentries) {
2115 for (i = grp->nindices; i < grp->nentries; i++) {
2116 nasm_nonfatal("group `%s' contains undefined segment"
2117 " `%s'", grp->name, grp->segs[i].name);
2118 nasm_free(grp->segs[i].name);
2119 grp->segs[i].name = NULL;
2122 obj_index(orp, ++lname_idx);
2123 for (i = 0; i < grp->nindices; i++) {
2124 obj_byte(orp, 0xFF);
2125 obj_index(orp, grp->segs[i].index);
2127 obj_emit2(orp);
2131 * Write the PUBDEF records: first the ones in the segments,
2132 * then the far-absolutes.
2134 orp->type = PUBDEF;
2135 orp->ori = ori_pubdef;
2136 for (seg = seghead; seg; seg = seg->next) {
2137 orp->parm[0] = seg->grp ? seg->grp->obj_index : 0;
2138 orp->parm[1] = seg->obj_index;
2139 for (pub = seg->pubhead; pub; pub = pub->next) {
2140 orp = obj_name(orp, pub->name);
2141 orp = obj_x(orp, pub->offset);
2142 orp = obj_byte(orp, 0); /* type index */
2143 obj_commit(orp);
2145 obj_emit(orp);
2147 orp->parm[0] = 0;
2148 orp->parm[1] = 0;
2149 for (pub = fpubhead; pub; pub = pub->next) { /* pub-crawl :-) */
2150 if (orp->parm[2] != (uint32_t)pub->segment) {
2151 obj_emit(orp);
2152 orp->parm[2] = pub->segment;
2154 orp = obj_name(orp, pub->name);
2155 orp = obj_x(orp, pub->offset);
2156 orp = obj_byte(orp, 0); /* type index */
2157 obj_commit(orp);
2159 obj_emit(orp);
2162 * Write the EXTDEF and COMDEF records, in order.
2164 orp->ori = ori_null;
2165 for (ext = exthead; ext; ext = ext->next) {
2166 if (ext->commonsize == 0) {
2167 if (orp->type != EXTDEF) {
2168 obj_emit(orp);
2169 orp->type = EXTDEF;
2171 orp = obj_name(orp, ext->name);
2172 orp = obj_index(orp, 0);
2173 } else {
2174 if (orp->type != COMDEF) {
2175 obj_emit(orp);
2176 orp->type = COMDEF;
2178 orp = obj_name(orp, ext->name);
2179 orp = obj_index(orp, 0);
2180 if (ext->commonelem) {
2181 orp = obj_byte(orp, 0x61); /* far communal */
2182 orp = obj_value(orp, (ext->commonsize / ext->commonelem));
2183 orp = obj_value(orp, ext->commonelem);
2184 } else {
2185 orp = obj_byte(orp, 0x62); /* near communal */
2186 orp = obj_value(orp, ext->commonsize);
2189 obj_commit(orp);
2191 obj_emit(orp);
2194 * Write a COMENT record stating that the linker's first pass
2195 * may stop processing at this point. Exception is if our
2196 * MODEND record specifies a start point, in which case,
2197 * according to some variants of the documentation, this COMENT
2198 * should be omitted. So we'll omit it just in case.
2199 * But, TASM puts it in all the time so if we are using
2200 * TASM debug stuff we are putting it in
2202 if (debuginfo || obj_entry_seg == NO_SEG) {
2203 orp->type = COMENT;
2204 obj_rword(orp, dLINKPASS);
2205 obj_byte(orp, 1);
2206 obj_emit2(orp);
2210 * 1) put out the compiler type
2211 * 2) Put out the type info. The only type we are using is near label #19
2213 if (debuginfo) {
2214 int i;
2215 struct Array *arrtmp = arrhead;
2216 orp->type = COMENT;
2217 obj_rword(orp, dCOMPDEF);
2218 obj_byte(orp, 4);
2219 obj_byte(orp, 0);
2220 obj_emit2(orp);
2222 obj_rword(orp, dTYPEDEF);
2223 obj_word(orp, 0x18); /* type # for linking */
2224 obj_word(orp, 6); /* size of type */
2225 obj_byte(orp, 0x2a); /* absolute type for debugging */
2226 obj_emit2(orp);
2227 obj_rword(orp, dTYPEDEF);
2228 obj_word(orp, 0x19); /* type # for linking */
2229 obj_word(orp, 0); /* size of type */
2230 obj_byte(orp, 0x24); /* absolute type for debugging */
2231 obj_byte(orp, 0); /* near/far specifier */
2232 obj_emit2(orp);
2233 obj_rword(orp, dTYPEDEF);
2234 obj_word(orp, 0x1A); /* type # for linking */
2235 obj_word(orp, 0); /* size of type */
2236 obj_byte(orp, 0x24); /* absolute type for debugging */
2237 obj_byte(orp, 1); /* near/far specifier */
2238 obj_emit2(orp);
2239 obj_rword(orp, dTYPEDEF);
2240 obj_word(orp, 0x1b); /* type # for linking */
2241 obj_word(orp, 0); /* size of type */
2242 obj_byte(orp, 0x23); /* absolute type for debugging */
2243 obj_byte(orp, 0);
2244 obj_byte(orp, 0);
2245 obj_byte(orp, 0);
2246 obj_emit2(orp);
2247 obj_rword(orp, dTYPEDEF);
2248 obj_word(orp, 0x1c); /* type # for linking */
2249 obj_word(orp, 0); /* size of type */
2250 obj_byte(orp, 0x23); /* absolute type for debugging */
2251 obj_byte(orp, 0);
2252 obj_byte(orp, 4);
2253 obj_byte(orp, 0);
2254 obj_emit2(orp);
2255 obj_rword(orp, dTYPEDEF);
2256 obj_word(orp, 0x1d); /* type # for linking */
2257 obj_word(orp, 0); /* size of type */
2258 obj_byte(orp, 0x23); /* absolute type for debugging */
2259 obj_byte(orp, 0);
2260 obj_byte(orp, 1);
2261 obj_byte(orp, 0);
2262 obj_emit2(orp);
2263 obj_rword(orp, dTYPEDEF);
2264 obj_word(orp, 0x1e); /* type # for linking */
2265 obj_word(orp, 0); /* size of type */
2266 obj_byte(orp, 0x23); /* absolute type for debugging */
2267 obj_byte(orp, 0);
2268 obj_byte(orp, 5);
2269 obj_byte(orp, 0);
2270 obj_emit2(orp);
2272 /* put out the array types */
2273 for (i = ARRAYBOT; i < arrindex; i++) {
2274 obj_rword(orp, dTYPEDEF);
2275 obj_word(orp, i); /* type # for linking */
2276 obj_word(orp, arrtmp->size); /* size of type */
2277 obj_byte(orp, 0x1A); /* absolute type for debugging (array) */
2278 obj_byte(orp, arrtmp->basetype); /* base type */
2279 obj_emit2(orp);
2280 arrtmp = arrtmp->next;
2284 * write out line number info with a LINNUM record
2285 * switch records when we switch segments, and output the
2286 * file in a pseudo-TASM fashion. The record switch is naive; that
2287 * is that one file may have many records for the same segment
2288 * if there are lots of segment switches
2290 if (fnhead && debuginfo) {
2291 seg = fnhead->lnhead->segment;
2293 for (fn = fnhead; fn; fn = fn->next) {
2294 /* write out current file name */
2295 orp->type = COMENT;
2296 orp->ori = ori_null;
2297 obj_rword(orp, dFILNAME);
2298 obj_byte(orp, 0);
2299 obj_name(orp, fn->name);
2300 obj_dword(orp, 0);
2301 obj_emit2(orp);
2303 /* write out line numbers this file */
2305 orp->type = LINNUM;
2306 orp->ori = ori_linnum;
2307 for (ln = fn->lnhead; ln; ln = ln->next) {
2308 if (seg != ln->segment) {
2309 /* if we get here have to flush the buffer and start
2310 * a new record for a new segment
2312 seg = ln->segment;
2313 obj_emit(orp);
2315 orp->parm[0] = seg->grp ? seg->grp->obj_index : 0;
2316 orp->parm[1] = seg->obj_index;
2317 orp = obj_word(orp, ln->lineno);
2318 orp = obj_x(orp, ln->offset);
2319 obj_commit(orp);
2321 obj_emit(orp);
2325 * we are going to locate the entry point segment now
2326 * rather than wait until the MODEND record, because,
2327 * then we can output a special symbol to tell where the
2328 * entry point is.
2331 if (obj_entry_seg != NO_SEG) {
2332 for (seg = seghead; seg; seg = seg->next) {
2333 if (seg->index == obj_entry_seg) {
2334 entry_seg_ptr = seg;
2335 break;
2338 if (!seg)
2339 nasm_nonfatal("entry point is not in this module");
2343 * get ready to put out symbol records
2345 orp->type = COMENT;
2346 orp->ori = ori_local;
2349 * put out a symbol for the entry point
2350 * no dots in this symbol, because, borland does
2351 * not (officially) support dots in label names
2352 * and I don't know what various versions of TLINK will do
2354 if (debuginfo && obj_entry_seg != NO_SEG) {
2355 orp = obj_name(orp, "start_of_program");
2356 orp = obj_word(orp, 0x19); /* type: near label */
2357 orp = obj_index(orp, seg->grp ? seg->grp->obj_index : 0);
2358 orp = obj_index(orp, seg->obj_index);
2359 orp = obj_x(orp, obj_entry_ofs);
2360 obj_commit(orp);
2364 * put out the local labels
2366 for (seg = seghead; seg && debuginfo; seg = seg->next) {
2367 /* labels this seg */
2368 for (loc = seg->lochead; loc; loc = loc->next) {
2369 orp = obj_name(orp, loc->name);
2370 orp = obj_word(orp, loc->type);
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, loc->offset);
2374 obj_commit(orp);
2377 if (orp->used)
2378 obj_emit(orp);
2381 * Write the LEDATA/FIXUPP pairs.
2383 for (seg = seghead; seg; seg = seg->next) {
2384 obj_emit(seg->orp);
2385 nasm_free(seg->orp);
2389 * Write the MODEND module end marker.
2391 orp->type = obj_use32 ? MODE32 : MODEND;
2392 orp->ori = ori_null;
2393 if (entry_seg_ptr) {
2394 orp->type = entry_seg_ptr->use32 ? MODE32 : MODEND;
2395 obj_byte(orp, 0xC1);
2396 seg = entry_seg_ptr;
2397 if (seg->grp) {
2398 obj_byte(orp, 0x10);
2399 obj_index(orp, seg->grp->obj_index);
2400 } else {
2402 * the below changed to prevent TLINK crashing.
2403 * Previous more efficient version read:
2405 * obj_byte (orp, 0x50);
2407 obj_byte(orp, 0x00);
2408 obj_index(orp, seg->obj_index);
2410 obj_index(orp, seg->obj_index);
2411 obj_x(orp, obj_entry_ofs);
2412 } else
2413 obj_byte(orp, 0);
2414 obj_emit2(orp);
2415 nasm_free(orp);
2418 static void obj_fwrite(ObjRecord * orp)
2420 unsigned int cksum, len;
2421 uint8_t *ptr;
2423 cksum = orp->type;
2424 if (orp->x_size == 32)
2425 cksum |= 1;
2426 fputc(cksum, ofile);
2427 len = orp->committed + 1;
2428 cksum += (len & 0xFF) + ((len >> 8) & 0xFF);
2429 fwriteint16_t(len, ofile);
2430 nasm_write(orp->buf, len-1, ofile);
2431 for (ptr = orp->buf; --len; ptr++)
2432 cksum += *ptr;
2433 fputc((-cksum) & 0xFF, ofile);
2436 static enum directive_result
2437 obj_pragma(const struct pragma *pragma)
2439 switch (pragma->opcode) {
2440 case D_NODEPEND:
2441 obj_nodepend = true;
2442 break;
2444 default:
2445 break;
2448 return DIRR_OK;
2451 extern macros_t obj_stdmac[];
2453 static void dbgbi_init(void)
2455 fnhead = NULL;
2456 fntail = &fnhead;
2457 arrindex = ARRAYBOT;
2458 arrhead = NULL;
2459 arrtail = &arrhead;
2461 static void dbgbi_cleanup(void)
2463 struct Segment *segtmp;
2464 while (fnhead) {
2465 struct FileName *fntemp = fnhead;
2466 while (fnhead->lnhead) {
2467 struct LineNumber *lntemp = fnhead->lnhead;
2468 fnhead->lnhead = lntemp->next;
2469 nasm_free(lntemp);
2471 fnhead = fnhead->next;
2472 nasm_free(fntemp->name);
2473 nasm_free(fntemp);
2475 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
2476 while (segtmp->lochead) {
2477 struct Public *loctmp = segtmp->lochead;
2478 segtmp->lochead = loctmp->next;
2479 nasm_free(loctmp->name);
2480 nasm_free(loctmp);
2483 while (arrhead) {
2484 struct Array *arrtmp = arrhead;
2485 arrhead = arrhead->next;
2486 nasm_free(arrtmp);
2490 static void dbgbi_linnum(const char *lnfname, int32_t lineno, int32_t segto)
2492 struct FileName *fn;
2493 struct LineNumber *ln;
2494 struct Segment *seg;
2496 if (segto == NO_SEG)
2497 return;
2500 * If `any_segs' is still false, we must define a default
2501 * segment.
2503 if (!any_segs) {
2504 int tempint; /* ignored */
2505 if (segto != obj_segment("__NASMDEFSEG", 2, &tempint))
2506 nasm_panic("strange segment conditions in OBJ driver");
2510 * Find the segment we are targetting.
2512 for (seg = seghead; seg; seg = seg->next)
2513 if (seg->index == segto)
2514 break;
2515 if (!seg)
2516 nasm_panic("lineno directed to nonexistent segment?");
2518 /* for (fn = fnhead; fn; fn = fnhead->next) */
2519 for (fn = fnhead; fn; fn = fn->next) /* fbk - Austin Lunnen - John Fine */
2520 if (!nasm_stricmp(lnfname, fn->name))
2521 break;
2522 if (!fn) {
2523 fn = nasm_malloc(sizeof(*fn));
2524 fn->name = nasm_malloc(strlen(lnfname) + 1);
2525 strcpy(fn->name, lnfname);
2526 fn->lnhead = NULL;
2527 fn->lntail = &fn->lnhead;
2528 fn->next = NULL;
2529 *fntail = fn;
2530 fntail = &fn->next;
2532 ln = nasm_malloc(sizeof(*ln));
2533 ln->segment = seg;
2534 ln->offset = seg->currentpos;
2535 ln->lineno = lineno;
2536 ln->next = NULL;
2537 *fn->lntail = ln;
2538 fn->lntail = &ln->next;
2541 static void dbgbi_deflabel(char *name, int32_t segment,
2542 int64_t offset, int is_global, char *special)
2544 struct Segment *seg;
2546 (void)special;
2549 * Note: ..[^@] special symbols are filtered in labels.c
2553 * If it's a special-retry from pass two, discard it.
2555 if (is_global == 3)
2556 return;
2559 * Case (i):
2561 if (obj_seg_needs_update) {
2562 return;
2563 } else if (obj_grp_needs_update) {
2564 return;
2566 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
2567 return;
2569 if (segment >= SEG_ABS || segment == NO_SEG) {
2570 return;
2574 * If `any_segs' is still false, we might need to define a
2575 * default segment, if they're trying to declare a label in
2576 * `first_seg'. But the label should exist due to a prior
2577 * call to obj_deflabel so we can skip that.
2580 for (seg = seghead; seg; seg = seg->next)
2581 if (seg->index == segment) {
2582 struct Public *loc = nasm_malloc(sizeof(*loc));
2584 * Case (ii). Maybe MODPUB someday?
2586 last_defined = *seg->loctail = loc;
2587 seg->loctail = &loc->next;
2588 loc->next = NULL;
2589 loc->name = nasm_strdup(name);
2590 loc->offset = offset;
2593 static void dbgbi_typevalue(int32_t type)
2595 int vsize;
2596 int elem = TYM_ELEMENTS(type);
2597 type = TYM_TYPE(type);
2599 if (!last_defined)
2600 return;
2602 switch (type) {
2603 case TY_BYTE:
2604 last_defined->type = 8; /* uint8_t */
2605 vsize = 1;
2606 break;
2607 case TY_WORD:
2608 last_defined->type = 10; /* unsigned word */
2609 vsize = 2;
2610 break;
2611 case TY_DWORD:
2612 last_defined->type = 12; /* unsigned dword */
2613 vsize = 4;
2614 break;
2615 case TY_FLOAT:
2616 last_defined->type = 14; /* float */
2617 vsize = 4;
2618 break;
2619 case TY_QWORD:
2620 last_defined->type = 15; /* qword */
2621 vsize = 8;
2622 break;
2623 case TY_TBYTE:
2624 last_defined->type = 16; /* TBYTE */
2625 vsize = 10;
2626 break;
2627 default:
2628 last_defined->type = 0x19; /* label */
2629 vsize = 0;
2630 break;
2633 if (elem > 1) {
2634 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
2635 int vtype = last_defined->type;
2636 arrtmp->size = vsize * elem;
2637 arrtmp->basetype = vtype;
2638 arrtmp->next = NULL;
2639 last_defined->type = arrindex++;
2640 *arrtail = arrtmp;
2641 arrtail = &(arrtmp->next);
2643 last_defined = NULL;
2645 static void dbgbi_output(int output_type, void *param)
2647 (void)output_type;
2648 (void)param;
2650 static const struct dfmt borland_debug_form = {
2651 "Borland Debug Records",
2652 "borland",
2653 dbgbi_init,
2654 dbgbi_linnum,
2655 dbgbi_deflabel,
2656 null_debug_directive,
2657 dbgbi_typevalue,
2658 dbgbi_output,
2659 dbgbi_cleanup,
2660 NULL /* pragma list */
2663 static const struct dfmt * const borland_debug_arr[3] = {
2664 &borland_debug_form,
2665 &null_debug_form,
2666 NULL
2669 static const struct pragma_facility obj_pragma_list[] = {
2670 { NULL, obj_pragma }
2673 const struct ofmt of_obj = {
2674 "MS-DOS 16-bit/32-bit OMF object files",
2675 "obj",
2676 ".obj",
2679 borland_debug_arr,
2680 &borland_debug_form,
2681 obj_stdmac,
2682 obj_init,
2683 null_reset,
2684 nasm_do_legacy_output,
2685 obj_out,
2686 obj_deflabel,
2687 obj_segment,
2688 NULL,
2689 obj_sectalign,
2690 obj_segbase,
2691 obj_directive,
2692 obj_cleanup,
2693 obj_pragma_list
2695 #endif /* OF_OBJ */