rbtree: add rb_search_exact()
[nasm.git] / output / outobj.c
blob0d4d31105eb28e67a89d5e1986d7504146bb6cf3
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 <ctype.h> /* For toupper() */
42 #include "nctype.h"
44 #include "nasm.h"
45 #include "nasmlib.h"
46 #include "error.h"
47 #include "stdscan.h"
48 #include "eval.h"
49 #include "ver.h"
51 #include "outform.h"
52 #include "outlib.h"
54 #ifdef OF_OBJ
57 * outobj.c is divided into two sections. The first section is low level
58 * routines for creating obj records; It has nearly zero NASM specific
59 * code. The second section is high level routines for processing calls and
60 * data structures from the rest of NASM into obj format.
62 * It should be easy (though not zero work) to lift the first section out for
63 * use as an obj file writer for some other assembler or compiler.
67 * These routines are built around the ObjRecord data struture. An ObjRecord
68 * holds an object file record that may be under construction or complete.
70 * A major function of these routines is to support continuation of an obj
71 * record into the next record when the maximum record size is exceeded. The
72 * high level code does not need to worry about where the record breaks occur.
73 * It does need to do some minor extra steps to make the automatic continuation
74 * work. Those steps may be skipped for records where the high level knows no
75 * continuation could be required.
77 * 1) An ObjRecord is allocated and cleared by obj_new, or an existing ObjRecord
78 * is cleared by obj_clear.
80 * 2) The caller should fill in .type.
82 * 3) If the record is continuable and there is processing that must be done at
83 * the start of each record then the caller should fill in .ori with the
84 * address of the record initializer routine.
86 * 4) If the record is continuable and it should be saved (rather than emitted
87 * immediately) as each record is done, the caller should set .up to be a
88 * pointer to a location in which the caller keeps the master pointer to the
89 * ObjRecord. When the record is continued, the obj_bump routine will then
90 * allocate a new ObjRecord structure and update the master pointer.
92 * 5) If the .ori field was used then the caller should fill in the .parm with
93 * any data required by the initializer.
95 * 6) The caller uses the routines: obj_byte, obj_word, obj_rword, obj_dword,
96 * obj_x, obj_index, obj_value and obj_name to fill in the various kinds of
97 * data required for this record.
99 * 7) If the record is continuable, the caller should call obj_commit at each
100 * point where breaking the record is permitted.
102 * 8) To write out the record, the caller should call obj_emit2. If the
103 * caller has called obj_commit for all data written then he can get slightly
104 * faster code by calling obj_emit instead of obj_emit2.
106 * Most of these routines return an ObjRecord pointer. This will be the input
107 * pointer most of the time and will be the new location if the ObjRecord
108 * moved as a result of the call. The caller may ignore the return value in
109 * three cases: It is a "Never Reallocates" routine; or The caller knows
110 * continuation is not possible; or The caller uses the master pointer for the
111 * next operation.
114 #define RECORD_MAX (1024-3) /* maximal size of any record except type+reclen */
115 #define OBJ_PARMS 3 /* maximum .parm used by any .ori routine */
117 #define FIX_08_LOW 0x8000 /* location type for various fixup subrecords */
118 #define FIX_16_OFFSET 0x8400
119 #define FIX_16_SELECTOR 0x8800
120 #define FIX_32_POINTER 0x8C00
121 #define FIX_08_HIGH 0x9000
122 #define FIX_32_OFFSET 0xA400
123 #define FIX_48_POINTER 0xAC00
125 enum RecordID { /* record ID codes */
127 THEADR = 0x80, /* module header */
128 COMENT = 0x88, /* comment record */
130 LINNUM = 0x94, /* line number record */
131 LNAMES = 0x96, /* list of names */
133 SEGDEF = 0x98, /* segment definition */
134 GRPDEF = 0x9A, /* group definition */
135 EXTDEF = 0x8C, /* external definition */
136 PUBDEF = 0x90, /* public definition */
137 COMDEF = 0xB0, /* common definition */
139 LEDATA = 0xA0, /* logical enumerated data */
140 FIXUPP = 0x9C, /* fixups (relocations) */
141 FIXU32 = 0x9D, /* 32-bit fixups (relocations) */
143 MODEND = 0x8A, /* module end */
144 MODE32 = 0x8B /* module end for 32-bit objects */
147 enum ComentID { /* ID codes for comment records */
148 dTRANSL = 0x0000, /* translator comment */
149 dOMFEXT = 0xC0A0, /* "OMF extension" */
150 dEXTENDED = 0xC0A1, /* translator-specific extensions */
151 dLINKPASS = 0x40A2, /* link pass 2 marker */
152 dTYPEDEF = 0xC0E3, /* define a type */
153 dSYM = 0xC0E6, /* symbol debug record */
154 dFILNAME = 0xC0E8, /* file name record */
155 dDEPFILE = 0xC0E9, /* dependency file */
156 dCOMPDEF = 0xC0EA /* compiler type info */
159 typedef struct ObjRecord ObjRecord;
160 typedef void ORI(ObjRecord * orp);
162 struct ObjRecord {
163 ORI *ori; /* Initialization routine */
164 int used; /* Current data size */
165 int committed; /* Data size at last boundary */
166 int x_size; /* (see obj_x) */
167 unsigned int type; /* Record type */
168 ObjRecord *child; /* Associated record below this one */
169 ObjRecord **up; /* Master pointer to this ObjRecord */
170 ObjRecord *back; /* Previous part of this record */
171 uint32_t parm[OBJ_PARMS]; /* Parameters for ori routine */
172 uint8_t buf[RECORD_MAX + 3];
175 static void obj_fwrite(ObjRecord * orp);
176 static void ori_ledata(ObjRecord * orp);
177 static void ori_pubdef(ObjRecord * orp);
178 static void ori_null(ObjRecord * orp);
179 static ObjRecord *obj_commit(ObjRecord * orp);
181 static bool obj_uppercase; /* Flag: all names in uppercase */
182 static bool obj_use32; /* Flag: at least one segment is 32-bit */
183 static bool obj_nodepend; /* Flag: don't emit file dependencies */
186 * Clear an ObjRecord structure. (Never reallocates).
187 * To simplify reuse of ObjRecord's, .type, .ori and .parm are not cleared.
189 static ObjRecord *obj_clear(ObjRecord * orp)
191 orp->used = 0;
192 orp->committed = 0;
193 orp->x_size = 0;
194 orp->child = NULL;
195 orp->up = NULL;
196 orp->back = NULL;
197 return (orp);
201 * Emit an ObjRecord structure. (Never reallocates).
202 * The record is written out preceeded (recursively) by its previous part (if
203 * any) and followed (recursively) by its child (if any).
204 * The previous part and the child are freed. The main ObjRecord is cleared,
205 * not freed.
207 static ObjRecord *obj_emit(ObjRecord * orp)
209 if (orp->back) {
210 obj_emit(orp->back);
211 nasm_free(orp->back);
214 if (orp->committed)
215 obj_fwrite(orp);
217 if (orp->child) {
218 obj_emit(orp->child);
219 nasm_free(orp->child);
222 return (obj_clear(orp));
226 * Commit and Emit a record. (Never reallocates).
228 static ObjRecord *obj_emit2(ObjRecord * orp)
230 obj_commit(orp);
231 return (obj_emit(orp));
235 * Allocate and clear a new ObjRecord; Also sets .ori to ori_null
237 static ObjRecord *obj_new(void)
239 ObjRecord *orp;
241 orp = obj_clear(nasm_malloc(sizeof(ObjRecord)));
242 orp->ori = ori_null;
243 return (orp);
247 * Advance to the next record because the existing one is full or its x_size
248 * is incompatible.
249 * Any uncommited data is moved into the next record.
251 static ObjRecord *obj_bump(ObjRecord * orp)
253 ObjRecord *nxt;
254 int used = orp->used;
255 int committed = orp->committed;
257 if (orp->up) {
258 *orp->up = nxt = obj_new();
259 nxt->ori = orp->ori;
260 nxt->type = orp->type;
261 nxt->up = orp->up;
262 nxt->back = orp;
263 memcpy(nxt->parm, orp->parm, sizeof(orp->parm));
264 } else
265 nxt = obj_emit(orp);
267 used -= committed;
268 if (used) {
269 nxt->committed = 1;
270 nxt->ori(nxt);
271 nxt->committed = nxt->used;
272 memcpy(nxt->buf + nxt->committed, orp->buf + committed, used);
273 nxt->used = nxt->committed + used;
276 return (nxt);
280 * Advance to the next record if necessary to allow the next field to fit.
282 static ObjRecord *obj_check(ObjRecord * orp, int size)
284 if (orp->used + size > RECORD_MAX)
285 orp = obj_bump(orp);
287 if (!orp->committed) {
288 orp->committed = 1;
289 orp->ori(orp);
290 orp->committed = orp->used;
293 return (orp);
297 * All data written so far is commited to the current record (won't be moved to
298 * the next record in case of continuation).
300 static ObjRecord *obj_commit(ObjRecord * orp)
302 orp->committed = orp->used;
303 return (orp);
307 * Write a byte
309 static ObjRecord *obj_byte(ObjRecord * orp, uint8_t val)
311 orp = obj_check(orp, 1);
312 orp->buf[orp->used] = val;
313 orp->used++;
314 return (orp);
318 * Write a word
320 static ObjRecord *obj_word(ObjRecord * orp, unsigned int val)
322 orp = obj_check(orp, 2);
323 orp->buf[orp->used] = val;
324 orp->buf[orp->used + 1] = val >> 8;
325 orp->used += 2;
326 return (orp);
330 * Write a reversed word
332 static ObjRecord *obj_rword(ObjRecord * orp, unsigned int val)
334 orp = obj_check(orp, 2);
335 orp->buf[orp->used] = val >> 8;
336 orp->buf[orp->used + 1] = val;
337 orp->used += 2;
338 return (orp);
342 * Write a dword
344 static ObjRecord *obj_dword(ObjRecord * orp, uint32_t val)
346 orp = obj_check(orp, 4);
347 orp->buf[orp->used] = val;
348 orp->buf[orp->used + 1] = val >> 8;
349 orp->buf[orp->used + 2] = val >> 16;
350 orp->buf[orp->used + 3] = val >> 24;
351 orp->used += 4;
352 return (orp);
356 * All fields of "size x" in one obj record must be the same size (either 16
357 * bits or 32 bits). There is a one bit flag in each record which specifies
358 * which.
359 * This routine is used to force the current record to have the desired
360 * x_size. x_size is normally automatic (using obj_x), so that this
361 * routine should be used outside obj_x, only to provide compatibility with
362 * linkers that have bugs in their processing of the size bit.
365 static ObjRecord *obj_force(ObjRecord * orp, int x)
367 if (orp->x_size == (x ^ 48))
368 orp = obj_bump(orp);
369 orp->x_size = x;
370 return (orp);
374 * This routine writes a field of size x. The caller does not need to worry at
375 * all about whether 16-bits or 32-bits are required.
377 static ObjRecord *obj_x(ObjRecord * orp, uint32_t val)
379 if (orp->type & 1)
380 orp->x_size = 32;
381 if (val > 0xFFFF)
382 orp = obj_force(orp, 32);
383 if (orp->x_size == 32) {
384 ObjRecord *nxt = obj_dword(orp, val);
385 nxt->x_size = 32; /* x_size is cleared when a record overflows */
386 return nxt;
388 orp->x_size = 16;
389 return (obj_word(orp, val));
393 * Writes an index
395 static ObjRecord *obj_index(ObjRecord * orp, unsigned int val)
397 if (val < 128)
398 return (obj_byte(orp, val));
399 return (obj_word(orp, (val >> 8) | (val << 8) | 0x80));
403 * Writes a variable length value
405 static ObjRecord *obj_value(ObjRecord * orp, uint32_t val)
407 if (val <= 128)
408 return (obj_byte(orp, val));
409 if (val <= 0xFFFF) {
410 orp = obj_byte(orp, 129);
411 return (obj_word(orp, val));
413 if (val <= 0xFFFFFF)
414 return (obj_dword(orp, (val << 8) + 132));
415 orp = obj_byte(orp, 136);
416 return (obj_dword(orp, val));
420 * Writes a counted string
422 static ObjRecord *obj_name(ObjRecord * orp, const char *name)
424 int len = strlen(name);
425 uint8_t *ptr;
427 orp = obj_check(orp, len + 1);
428 ptr = orp->buf + orp->used;
429 *ptr++ = len;
430 orp->used += len + 1;
431 if (obj_uppercase)
432 while (--len >= 0) {
433 *ptr++ = toupper(*name);
434 name++;
435 } else
436 memcpy(ptr, name, len);
437 return (orp);
441 * Initializer for an LEDATA record.
442 * parm[0] = offset
443 * parm[1] = segment index
444 * During the use of a LEDATA ObjRecord, parm[0] is constantly updated to
445 * represent the offset that would be required if the record were split at the
446 * last commit point.
447 * parm[2] is a copy of parm[0] as it was when the current record was initted.
449 static void ori_ledata(ObjRecord * orp)
451 obj_index(orp, orp->parm[1]);
452 orp->parm[2] = orp->parm[0];
453 obj_x(orp, orp->parm[0]);
457 * Initializer for a PUBDEF record.
458 * parm[0] = group index
459 * parm[1] = segment index
460 * parm[2] = frame (only used when both indexes are zero)
462 static void ori_pubdef(ObjRecord * orp)
464 obj_index(orp, orp->parm[0]);
465 obj_index(orp, orp->parm[1]);
466 if (!(orp->parm[0] | orp->parm[1]))
467 obj_word(orp, orp->parm[2]);
471 * Initializer for a LINNUM record.
472 * parm[0] = group index
473 * parm[1] = segment index
475 static void ori_linnum(ObjRecord * orp)
477 obj_index(orp, orp->parm[0]);
478 obj_index(orp, orp->parm[1]);
482 * Initializer for a local vars record.
484 static void ori_local(ObjRecord * orp)
486 obj_rword(orp, dSYM);
490 * Null initializer for records that continue without any header info
492 static void ori_null(ObjRecord * orp)
494 (void)orp; /* Do nothing */
498 * This concludes the low level section of outobj.c
501 static char obj_infile[FILENAME_MAX];
503 static int32_t first_seg;
504 static bool any_segs;
505 static int passtwo;
506 static int arrindex;
508 #define GROUP_MAX 256 /* we won't _realistically_ have more
509 * than this many segs in a group */
510 #define EXT_BLKSIZ 256 /* block size for externals list */
512 struct Segment; /* need to know these structs exist */
513 struct Group;
515 struct LineNumber {
516 struct LineNumber *next;
517 struct Segment *segment;
518 int32_t offset;
519 int32_t lineno;
522 static struct FileName {
523 struct FileName *next;
524 char *name;
525 struct LineNumber *lnhead, **lntail;
526 int index;
527 } *fnhead, **fntail;
529 static struct Array {
530 struct Array *next;
531 unsigned size;
532 int basetype;
533 } *arrhead, **arrtail;
535 #define ARRAYBOT 31 /* magic number for first array index */
537 static struct Public {
538 struct Public *next;
539 char *name;
540 int32_t offset;
541 int32_t segment; /* only if it's far-absolute */
542 int type; /* only for local debug syms */
543 } *fpubhead, **fpubtail, *last_defined;
545 static struct External {
546 struct External *next;
547 char *name;
548 int32_t commonsize;
549 int32_t commonelem; /* element size if FAR, else zero */
550 int index; /* OBJ-file external index */
551 enum {
552 DEFWRT_NONE, /* no unusual default-WRT */
553 DEFWRT_STRING, /* a string we don't yet understand */
554 DEFWRT_SEGMENT, /* a segment */
555 DEFWRT_GROUP /* a group */
556 } defwrt_type;
557 union {
558 char *string;
559 struct Segment *seg;
560 struct Group *grp;
561 } defwrt_ptr;
562 struct External *next_dws; /* next with DEFWRT_STRING */
563 } *exthead, **exttail, *dws;
565 static int externals;
567 static struct ExtBack {
568 struct ExtBack *next;
569 struct External *exts[EXT_BLKSIZ];
570 } *ebhead, **ebtail;
572 static struct Segment {
573 struct Segment *next;
574 char *name;
575 int32_t index; /* the NASM segment id */
576 int32_t obj_index; /* the OBJ-file segment index */
577 struct Group *grp; /* the group it beint32_ts to */
578 uint32_t currentpos;
579 int32_t align; /* can be SEG_ABS + absolute addr */
580 int64_t pass_last_seen;
581 struct Public *pubhead, **pubtail, *lochead, **loctail;
582 char *segclass, *overlay; /* `class' is a C++ keyword :-) */
583 ObjRecord *orp;
584 enum {
585 CMB_PRIVATE = 0,
586 CMB_PUBLIC = 2,
587 CMB_STACK = 5,
588 CMB_COMMON = 6
589 } combine;
590 bool use32; /* is this segment 32-bit? */
591 } *seghead, **segtail, *obj_seg_needs_update;
593 static struct Group {
594 struct Group *next;
595 char *name;
596 int32_t index; /* NASM segment id */
597 int32_t obj_index; /* OBJ-file group index */
598 int32_t nentries; /* number of elements... */
599 int32_t nindices; /* ...and number of index elts... */
600 union {
601 int32_t index;
602 char *name;
603 } segs[GROUP_MAX]; /* ...in this */
604 } *grphead, **grptail, *obj_grp_needs_update;
606 static struct ImpDef {
607 struct ImpDef *next;
608 char *extname;
609 char *libname;
610 unsigned int impindex;
611 char *impname;
612 } *imphead, **imptail;
614 static struct ExpDef {
615 struct ExpDef *next;
616 char *intname;
617 char *extname;
618 unsigned int ordinal;
619 int flags;
620 } *exphead, **exptail;
622 #define EXPDEF_FLAG_ORDINAL 0x80
623 #define EXPDEF_FLAG_RESIDENT 0x40
624 #define EXPDEF_FLAG_NODATA 0x20
625 #define EXPDEF_MASK_PARMCNT 0x1F
627 static int32_t obj_entry_seg, obj_entry_ofs;
629 const struct ofmt of_obj;
630 static const struct dfmt borland_debug_form;
632 /* The current segment */
633 static struct Segment *current_seg;
635 static int32_t obj_segment(char *, int *);
636 static void obj_write_file(void);
637 static enum directive_result obj_directive(enum directive, char *);
639 static void obj_init(void)
641 strlcpy(obj_infile, inname, sizeof(obj_infile));
642 first_seg = seg_alloc();
643 any_segs = false;
644 fpubhead = NULL;
645 fpubtail = &fpubhead;
646 exthead = NULL;
647 exttail = &exthead;
648 imphead = NULL;
649 imptail = &imphead;
650 exphead = NULL;
651 exptail = &exphead;
652 dws = NULL;
653 externals = 0;
654 ebhead = NULL;
655 ebtail = &ebhead;
656 seghead = obj_seg_needs_update = NULL;
657 segtail = &seghead;
658 grphead = obj_grp_needs_update = NULL;
659 grptail = &grphead;
660 obj_entry_seg = NO_SEG;
661 obj_uppercase = false;
662 obj_use32 = false;
663 passtwo = 0;
664 current_seg = NULL;
667 static void obj_cleanup(void)
669 obj_write_file();
670 dfmt->cleanup();
671 while (seghead) {
672 struct Segment *segtmp = seghead;
673 seghead = seghead->next;
674 while (segtmp->pubhead) {
675 struct Public *pubtmp = segtmp->pubhead;
676 segtmp->pubhead = pubtmp->next;
677 nasm_free(pubtmp->name);
678 nasm_free(pubtmp);
680 nasm_free(segtmp->segclass);
681 nasm_free(segtmp->overlay);
682 nasm_free(segtmp);
684 while (fpubhead) {
685 struct Public *pubtmp = fpubhead;
686 fpubhead = fpubhead->next;
687 nasm_free(pubtmp->name);
688 nasm_free(pubtmp);
690 while (exthead) {
691 struct External *exttmp = exthead;
692 exthead = exthead->next;
693 nasm_free(exttmp);
695 while (imphead) {
696 struct ImpDef *imptmp = imphead;
697 imphead = imphead->next;
698 nasm_free(imptmp->extname);
699 nasm_free(imptmp->libname);
700 nasm_free(imptmp->impname); /* nasm_free won't mind if it's NULL */
701 nasm_free(imptmp);
703 while (exphead) {
704 struct ExpDef *exptmp = exphead;
705 exphead = exphead->next;
706 nasm_free(exptmp->extname);
707 nasm_free(exptmp->intname);
708 nasm_free(exptmp);
710 while (ebhead) {
711 struct ExtBack *ebtmp = ebhead;
712 ebhead = ebhead->next;
713 nasm_free(ebtmp);
715 while (grphead) {
716 struct Group *grptmp = grphead;
717 grphead = grphead->next;
718 nasm_free(grptmp);
722 static void obj_ext_set_defwrt(struct External *ext, char *id)
724 struct Segment *seg;
725 struct Group *grp;
727 for (seg = seghead; seg; seg = seg->next)
728 if (!strcmp(seg->name, id)) {
729 ext->defwrt_type = DEFWRT_SEGMENT;
730 ext->defwrt_ptr.seg = seg;
731 nasm_free(id);
732 return;
735 for (grp = grphead; grp; grp = grp->next)
736 if (!strcmp(grp->name, id)) {
737 ext->defwrt_type = DEFWRT_GROUP;
738 ext->defwrt_ptr.grp = grp;
739 nasm_free(id);
740 return;
743 ext->defwrt_type = DEFWRT_STRING;
744 ext->defwrt_ptr.string = id;
745 ext->next_dws = dws;
746 dws = ext;
749 static void obj_deflabel(char *name, int32_t segment,
750 int64_t offset, int is_global, char *special)
753 * We have three cases:
755 * (i) `segment' is a segment-base. If so, set the name field
756 * for the segment or group structure it refers to, and then
757 * return.
759 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
760 * Save the label position for later output of a PUBDEF record.
761 * (Or a MODPUB, if we work out how.)
763 * (iii) `segment' is not one of our segments. Save the label
764 * position for later output of an EXTDEF, and also store a
765 * back-reference so that we can map later references to this
766 * segment number to the external index.
768 struct External *ext;
769 struct ExtBack *eb;
770 struct Segment *seg;
771 int i;
772 bool used_special = false; /* have we used the special text? */
774 if (debug_level(2))
775 nasm_debug(" obj_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
776 name, segment, offset, is_global, special);
779 * If it's a special-retry from pass two, discard it.
781 if (is_global == 3)
782 return;
785 * First check for the double-period, signifying something
786 * unusual.
788 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
789 if (!strcmp(name, "..start")) {
790 obj_entry_seg = segment;
791 obj_entry_ofs = offset;
792 return;
794 nasm_nonfatal("unrecognised special symbol `%s'", name);
798 * Case (i):
800 if (obj_seg_needs_update) {
801 obj_seg_needs_update->name = name;
802 return;
803 } else if (obj_grp_needs_update) {
804 obj_grp_needs_update->name = name;
805 return;
807 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
808 return;
810 if (segment >= SEG_ABS || segment == NO_SEG) {
812 * SEG_ABS subcase of (ii).
814 if (is_global) {
815 struct Public *pub;
817 pub = *fpubtail = nasm_malloc(sizeof(*pub));
818 fpubtail = &pub->next;
819 pub->next = NULL;
820 pub->name = nasm_strdup(name);
821 pub->offset = offset;
822 pub->segment = (segment == NO_SEG ? 0 : segment & ~SEG_ABS);
824 if (special)
825 nasm_nonfatal("OBJ supports no special symbol features"
826 " for this symbol type");
827 return;
831 * If `any_segs' is still false, we might need to define a
832 * default segment, if they're trying to declare a label in
833 * `first_seg'.
835 if (!any_segs && segment == first_seg) {
836 int tempint = 0;
837 if (segment != obj_segment("__NASMDEFSEG", &tempint))
838 nasm_panic("strange segment conditions in OBJ driver");
841 for (seg = seghead; seg && is_global; seg = seg->next)
842 if (seg->index == segment) {
843 struct Public *loc = nasm_malloc(sizeof(*loc));
845 * Case (ii). Maybe MODPUB someday?
847 *seg->pubtail = loc;
848 seg->pubtail = &loc->next;
849 loc->next = NULL;
850 loc->name = nasm_strdup(name);
851 loc->offset = offset;
853 if (special)
854 nasm_nonfatal("OBJ supports no special symbol features"
855 " for this symbol type");
856 return;
860 * Case (iii).
862 if (is_global) {
863 ext = *exttail = nasm_malloc(sizeof(*ext));
864 ext->next = NULL;
865 exttail = &ext->next;
866 ext->name = name;
867 /* Place by default all externs into the current segment */
868 ext->defwrt_type = DEFWRT_NONE;
870 /* 28-Apr-2002 - John Coffman
871 The following code was introduced on 12-Aug-2000, and breaks fixups
872 on code passed thru the MSC 5.1 linker (3.66) and MSC 6.00A linker
873 (5.10). It was introduced after FIXUP32 was added, and may be needed
874 for 32-bit segments. The following will get 16-bit segments working
875 again, and maybe someone can correct the 'if' condition which is
876 actually needed.
878 #if 0
879 if (current_seg) {
880 #else
881 if (current_seg && current_seg->use32) {
882 if (current_seg->grp) {
883 ext->defwrt_type = DEFWRT_GROUP;
884 ext->defwrt_ptr.grp = current_seg->grp;
885 } else {
886 ext->defwrt_type = DEFWRT_SEGMENT;
887 ext->defwrt_ptr.seg = current_seg;
890 #endif
892 if (is_global == 2) {
893 ext->commonsize = offset;
894 ext->commonelem = 1; /* default FAR */
895 } else
896 ext->commonsize = 0;
897 } else
898 return;
901 * Now process the special text, if any, to find default-WRT
902 * specifications and common-variable element-size and near/far
903 * specifications.
905 while (special && *special) {
906 used_special = true;
909 * We might have a default-WRT specification.
911 if (!nasm_strnicmp(special, "wrt", 3)) {
912 char *p;
913 int len;
914 special += 3;
915 special += strspn(special, " \t");
916 p = nasm_strndup(special, len = strcspn(special, ":"));
917 obj_ext_set_defwrt(ext, p);
918 special += len;
919 if (*special && *special != ':')
920 nasm_nonfatal("`:' expected in special symbol"
921 " text for `%s'", ext->name);
922 else if (*special == ':')
923 special++;
927 * The NEAR or FAR keywords specify nearness or
928 * farness. FAR gives default element size 1.
930 if (!nasm_strnicmp(special, "far", 3)) {
931 if (ext->commonsize)
932 ext->commonelem = 1;
933 else
934 nasm_nonfatal("`%s': `far' keyword may only be applied"
935 " to common variables\n", ext->name);
936 special += 3;
937 special += strspn(special, " \t");
938 } else if (!nasm_strnicmp(special, "near", 4)) {
939 if (ext->commonsize)
940 ext->commonelem = 0;
941 else
942 nasm_nonfatal("`%s': `far' keyword may only be applied"
943 " to common variables\n", ext->name);
944 special += 4;
945 special += strspn(special, " \t");
949 * If it's a common, and anything else remains on the line
950 * before a further colon, evaluate it as an expression and
951 * use that as the element size. Forward references aren't
952 * allowed.
954 if (*special == ':')
955 special++;
956 else if (*special) {
957 if (ext->commonsize) {
958 expr *e;
959 struct tokenval tokval;
961 stdscan_reset();
962 stdscan_set(special);
963 tokval.t_type = TOKEN_INVALID;
964 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
965 if (e) {
966 if (!is_simple(e))
967 nasm_nonfatal("cannot use relocatable"
968 " expression as common-variable element size");
969 else
970 ext->commonelem = reloc_value(e);
972 special = stdscan_get();
973 } else {
974 nasm_nonfatal("`%s': element-size specifications only"
975 " apply to common variables", ext->name);
976 while (*special && *special != ':')
977 special++;
978 if (*special == ':')
979 special++;
984 i = segment / 2;
985 eb = ebhead;
986 if (!eb) {
987 eb = *ebtail = nasm_zalloc(sizeof(*eb));
988 eb->next = NULL;
989 ebtail = &eb->next;
991 while (i >= EXT_BLKSIZ) {
992 if (eb && eb->next)
993 eb = eb->next;
994 else {
995 eb = *ebtail = nasm_zalloc(sizeof(*eb));
996 eb->next = NULL;
997 ebtail = &eb->next;
999 i -= EXT_BLKSIZ;
1001 eb->exts[i] = ext;
1002 ext->index = ++externals;
1004 if (special && !used_special)
1005 nasm_nonfatal("OBJ supports no special symbol features"
1006 " for this symbol type");
1009 /* forward declaration */
1010 static void obj_write_fixup(ObjRecord * orp, int bytes,
1011 int segrel, int32_t seg, int32_t wrt,
1012 struct Segment *segto);
1014 static void obj_out(int32_t segto, const void *data,
1015 enum out_type type, uint64_t size,
1016 int32_t segment, int32_t wrt)
1018 const uint8_t *ucdata;
1019 int32_t ldata;
1020 struct Segment *seg;
1021 ObjRecord *orp;
1024 * If `any_segs' is still false, we must define a default
1025 * segment.
1027 if (!any_segs) {
1028 int tempint = 0;
1029 if (segto != obj_segment("__NASMDEFSEG", &tempint))
1030 nasm_panic("strange segment conditions in OBJ driver");
1034 * Find the segment we are targetting.
1036 for (seg = seghead; seg; seg = seg->next)
1037 if (seg->index == segto)
1038 break;
1039 if (!seg)
1040 nasm_panic("code directed to nonexistent segment?");
1042 orp = seg->orp;
1043 orp->parm[0] = seg->currentpos;
1045 switch (type) {
1046 case OUT_RAWDATA:
1047 ucdata = data;
1048 while (size > 0) {
1049 unsigned int len;
1050 orp = obj_check(seg->orp, 1);
1051 len = RECORD_MAX - orp->used;
1052 if (len > size)
1053 len = size;
1054 memcpy(orp->buf + orp->used, ucdata, len);
1055 orp->committed = orp->used += len;
1056 orp->parm[0] = seg->currentpos += len;
1057 ucdata += len;
1058 size -= len;
1060 break;
1062 case OUT_ADDRESS:
1063 case OUT_REL1ADR:
1064 case OUT_REL2ADR:
1065 case OUT_REL4ADR:
1066 case OUT_REL8ADR:
1068 int rsize;
1070 if (type == OUT_ADDRESS)
1071 size = abs((int)size);
1073 if (segment == NO_SEG && type != OUT_ADDRESS)
1074 nasm_nonfatal("relative call to absolute address not"
1075 " supported by OBJ format");
1076 if (segment >= SEG_ABS)
1077 nasm_nonfatal("far-absolute relocations not supported"
1078 " by OBJ format");
1080 ldata = *(int64_t *)data;
1081 if (type != OUT_ADDRESS) {
1083 * For 16-bit and 32-bit x86 code, the size and realsize() always
1084 * matches as only jumps, calls and loops uses PC relative
1085 * addressing and the address isn't followed by any other opcode
1086 * bytes. In 64-bit mode there is RIP relative addressing which
1087 * means the fixup location can be followed by an immediate value,
1088 * meaning that size > realsize().
1090 * When the CPU is calculating the effective address, it takes the
1091 * RIP at the end of the instruction and adds the fixed up relative
1092 * address value to it.
1094 * The linker's point of reference is the end of the fixup location
1095 * (which is the end of the instruction for Jcc, CALL, LOOP[cc]).
1096 * It is calculating distance between the target symbol and the end
1097 * of the fixup location, and add this to the displacement value we
1098 * are calculating here and storing at the fixup location.
1100 * To get the right effect, we need to _reduce_ the displacement
1101 * value by the number of bytes following the fixup.
1103 * Example:
1104 * data at address 0x100; REL4ADR at 0x050, 4 byte immediate,
1105 * end of fixup at 0x054, end of instruction at 0x058.
1106 * => size = 8.
1107 * => realsize() -> 4
1108 * => CPU needs a value of: 0x100 - 0x058 = 0x0a8
1109 * => linker/loader will add: 0x100 - 0x054 = 0x0ac
1110 * => We must add an addend of -4.
1111 * => realsize() - size = -4.
1113 * The code used to do size - realsize() at least since v0.90,
1114 * probably because it wasn't needed...
1116 ldata -= size;
1117 size = realsize(type, size);
1118 ldata += size;
1121 switch (size) {
1122 default:
1123 nasm_nonfatal("OBJ format can only handle 16- or "
1124 "32-byte relocations");
1125 segment = NO_SEG; /* Don't actually generate a relocation */
1126 break;
1127 case 2:
1128 orp = obj_word(orp, ldata);
1129 break;
1130 case 4:
1131 orp = obj_dword(orp, ldata);
1132 break;
1135 rsize = size;
1136 if (segment < SEG_ABS && (segment != NO_SEG && segment % 2) &&
1137 size == 4) {
1139 * This is a 4-byte segment-base relocation such as
1140 * `MOV EAX,SEG foo'. OBJ format can't actually handle
1141 * these, but if the constant term has the 16 low bits
1142 * zero, we can just apply a 2-byte segment-base
1143 * relocation to the low word instead.
1145 rsize = 2;
1146 if (ldata & 0xFFFF)
1147 nasm_nonfatal("OBJ format cannot handle complex"
1148 " dword-size segment base references");
1150 if (segment != NO_SEG)
1151 obj_write_fixup(orp, rsize,
1152 (type == OUT_ADDRESS ? 0x4000 : 0),
1153 segment, wrt, seg);
1154 seg->currentpos += size;
1155 break;
1158 default:
1159 nasm_nonfatal("Relocation type not supported by output format");
1160 /* fall through */
1162 case OUT_RESERVE:
1163 if (orp->committed)
1164 orp = obj_bump(orp);
1165 seg->currentpos += size;
1166 break;
1168 obj_commit(orp);
1171 static void obj_write_fixup(ObjRecord * orp, int bytes,
1172 int segrel, int32_t seg, int32_t wrt,
1173 struct Segment *segto)
1175 unsigned locat;
1176 int method;
1177 int base;
1178 int32_t tidx, fidx;
1179 struct Segment *s = NULL;
1180 struct Group *g = NULL;
1181 struct External *e = NULL;
1182 ObjRecord *forp;
1184 if (bytes != 2 && bytes != 4) {
1185 nasm_nonfatal("`obj' output driver does not support"
1186 " %d-bit relocations", bytes << 3);
1187 return;
1190 forp = orp->child;
1191 if (forp == NULL) {
1192 orp->child = forp = obj_new();
1193 forp->up = &(orp->child);
1194 /* We should choose between FIXUPP and FIXU32 record type */
1195 /* If we're targeting a 32-bit segment, use a FIXU32 record */
1196 if (segto->use32)
1197 forp->type = FIXU32;
1198 else
1199 forp->type = FIXUPP;
1202 if (seg % 2) {
1203 base = true;
1204 locat = FIX_16_SELECTOR;
1205 seg--;
1206 if (bytes != 2)
1207 nasm_panic("OBJ: 4-byte segment base fixup got"
1208 " through sanity check");
1209 } else {
1210 base = false;
1211 locat = (bytes == 2) ? FIX_16_OFFSET : FIX_32_OFFSET;
1212 if (!segrel)
1214 * There is a bug in tlink that makes it process self relative
1215 * fixups incorrectly if the x_size doesn't match the location
1216 * size.
1218 forp = obj_force(forp, bytes << 3);
1221 forp = obj_rword(forp, locat | segrel | (orp->parm[0] - orp->parm[2]));
1223 tidx = fidx = -1, method = 0; /* placate optimisers */
1226 * See if we can find the segment ID in our segment list. If
1227 * so, we have a T4 (LSEG) target.
1229 for (s = seghead; s; s = s->next)
1230 if (s->index == seg)
1231 break;
1232 if (s)
1233 method = 4, tidx = s->obj_index;
1234 else {
1235 for (g = grphead; g; g = g->next)
1236 if (g->index == seg)
1237 break;
1238 if (g)
1239 method = 5, tidx = g->obj_index;
1240 else {
1241 int32_t i = seg / 2;
1242 struct ExtBack *eb = ebhead;
1243 while (i >= EXT_BLKSIZ) {
1244 if (eb)
1245 eb = eb->next;
1246 else
1247 break;
1248 i -= EXT_BLKSIZ;
1250 if (eb)
1251 method = 6, e = eb->exts[i], tidx = e->index;
1252 else
1253 nasm_panic("unrecognised segment value in obj_write_fixup");
1258 * If no WRT given, assume the natural default, which is method
1259 * F5 unless:
1261 * - we are doing an OFFSET fixup for a grouped segment, in
1262 * which case we require F1 (group).
1264 * - we are doing an OFFSET fixup for an external with a
1265 * default WRT, in which case we must honour the default WRT.
1267 if (wrt == NO_SEG) {
1268 if (!base && s && s->grp)
1269 method |= 0x10, fidx = s->grp->obj_index;
1270 else if (!base && e && e->defwrt_type != DEFWRT_NONE) {
1271 if (e->defwrt_type == DEFWRT_SEGMENT)
1272 method |= 0x00, fidx = e->defwrt_ptr.seg->obj_index;
1273 else if (e->defwrt_type == DEFWRT_GROUP)
1274 method |= 0x10, fidx = e->defwrt_ptr.grp->obj_index;
1275 else {
1276 nasm_nonfatal("default WRT specification for"
1277 " external `%s' unresolved", e->name);
1278 method |= 0x50, fidx = -1; /* got to do _something_ */
1280 } else
1281 method |= 0x50, fidx = -1;
1282 } else {
1284 * See if we can find the WRT-segment ID in our segment
1285 * list. If so, we have a F0 (LSEG) frame.
1287 for (s = seghead; s; s = s->next)
1288 if (s->index == wrt - 1)
1289 break;
1290 if (s)
1291 method |= 0x00, fidx = s->obj_index;
1292 else {
1293 for (g = grphead; g; g = g->next)
1294 if (g->index == wrt - 1)
1295 break;
1296 if (g)
1297 method |= 0x10, fidx = g->obj_index;
1298 else {
1299 int32_t i = wrt / 2;
1300 struct ExtBack *eb = ebhead;
1301 while (i >= EXT_BLKSIZ) {
1302 if (eb)
1303 eb = eb->next;
1304 else
1305 break;
1306 i -= EXT_BLKSIZ;
1308 if (eb)
1309 method |= 0x20, fidx = eb->exts[i]->index;
1310 else
1311 nasm_panic("unrecognised WRT value in obj_write_fixup");
1316 forp = obj_byte(forp, method);
1317 if (fidx != -1)
1318 forp = obj_index(forp, fidx);
1319 forp = obj_index(forp, tidx);
1320 obj_commit(forp);
1323 static int32_t obj_segment(char *name, int *bits)
1326 * We call the label manager here to define a name for the new
1327 * segment, and when our _own_ label-definition stub gets
1328 * called in return, it should register the new segment name
1329 * using the pointer it gets passed. That way we save memory,
1330 * by sponging off the label manager.
1332 if (debug_level(3))
1333 nasm_debug(" obj_segment: < %s >, *bits=%d\n", name, *bits);
1335 if (!name) {
1336 *bits = 16;
1337 current_seg = NULL;
1338 return first_seg;
1339 } else {
1340 struct Segment *seg;
1341 struct Group *grp;
1342 struct External **extp;
1343 int obj_idx, i, attrs;
1344 bool rn_error;
1345 char *p;
1348 * Look for segment attributes.
1350 attrs = 0;
1351 while (*name == '.')
1352 name++; /* hack, but a documented one */
1353 p = name;
1354 while (*p && !nasm_isspace(*p))
1355 p++;
1356 if (*p) {
1357 *p++ = '\0';
1358 while (*p && nasm_isspace(*p))
1359 *p++ = '\0';
1361 while (*p) {
1362 while (*p && !nasm_isspace(*p))
1363 p++;
1364 if (*p) {
1365 *p++ = '\0';
1366 while (*p && nasm_isspace(*p))
1367 *p++ = '\0';
1370 attrs++;
1373 for (seg = seghead, obj_idx = 1; ; seg = seg->next, obj_idx++) {
1374 if (!seg)
1375 break;
1377 if (!strcmp(seg->name, name)) {
1378 if (attrs > 0 && seg->pass_last_seen == pass_count())
1379 nasm_warn(WARN_OTHER, "segment attributes specified on"
1380 " redeclaration of segment: ignoring");
1381 if (seg->use32)
1382 *bits = 32;
1383 else
1384 *bits = 16;
1385 current_seg = seg;
1386 seg->pass_last_seen = pass_count();
1387 return seg->index;
1391 *segtail = seg = nasm_malloc(sizeof(*seg));
1392 seg->next = NULL;
1393 segtail = &seg->next;
1394 seg->index = (any_segs ? seg_alloc() : first_seg);
1395 seg->obj_index = obj_idx;
1396 seg->grp = NULL;
1397 any_segs = true;
1398 seg->name = nasm_strdup(name);
1399 seg->currentpos = 0;
1400 seg->align = 1; /* default */
1401 seg->use32 = false; /* default */
1402 seg->combine = CMB_PUBLIC; /* default */
1403 seg->segclass = seg->overlay = NULL;
1404 seg->pubhead = NULL;
1405 seg->pubtail = &seg->pubhead;
1406 seg->lochead = NULL;
1407 seg->loctail = &seg->lochead;
1408 seg->orp = obj_new();
1409 seg->orp->up = &(seg->orp);
1410 seg->orp->ori = ori_ledata;
1411 seg->orp->type = LEDATA;
1412 seg->orp->parm[1] = obj_idx;
1415 * Process the segment attributes.
1417 p = name;
1418 while (attrs--) {
1419 p += strlen(p);
1420 while (!*p)
1421 p++;
1424 * `p' contains a segment attribute.
1426 if (!nasm_stricmp(p, "private"))
1427 seg->combine = CMB_PRIVATE;
1428 else if (!nasm_stricmp(p, "public"))
1429 seg->combine = CMB_PUBLIC;
1430 else if (!nasm_stricmp(p, "common"))
1431 seg->combine = CMB_COMMON;
1432 else if (!nasm_stricmp(p, "stack"))
1433 seg->combine = CMB_STACK;
1434 else if (!nasm_stricmp(p, "use16"))
1435 seg->use32 = false;
1436 else if (!nasm_stricmp(p, "use32"))
1437 seg->use32 = true;
1438 else if (!nasm_stricmp(p, "flat")) {
1440 * This segment is an OS/2 FLAT segment. That means
1441 * that its default group is group FLAT, even if
1442 * the group FLAT does not explicitly _contain_ the
1443 * segment.
1445 * When we see this, we must create the group
1446 * `FLAT', containing no segments, if it does not
1447 * already exist; then we must set the default
1448 * group of this segment to be the FLAT group.
1450 struct Group *grp;
1451 for (grp = grphead; grp; grp = grp->next)
1452 if (!strcmp(grp->name, "FLAT"))
1453 break;
1454 if (!grp) {
1455 obj_directive(D_GROUP, "FLAT");
1456 for (grp = grphead; grp; grp = grp->next)
1457 if (!strcmp(grp->name, "FLAT"))
1458 break;
1459 if (!grp)
1460 nasm_panic("failure to define FLAT?!");
1462 seg->grp = grp;
1463 } else if (!nasm_strnicmp(p, "class=", 6))
1464 seg->segclass = nasm_strdup(p + 6);
1465 else if (!nasm_strnicmp(p, "overlay=", 8))
1466 seg->overlay = nasm_strdup(p + 8);
1467 else if (!nasm_strnicmp(p, "align=", 6)) {
1468 seg->align = readnum(p + 6, &rn_error);
1469 if (rn_error) {
1470 seg->align = 1;
1471 nasm_nonfatal("segment alignment should be numeric");
1473 switch (seg->align) {
1474 case 1: /* BYTE */
1475 case 2: /* WORD */
1476 case 4: /* DWORD */
1477 case 16: /* PARA */
1478 case 256: /* PAGE */
1479 case 4096: /* PharLap extension */
1480 break;
1481 case 8:
1482 nasm_warn(WARN_OTHER, "OBJ format does not support alignment"
1483 " of 8: rounding up to 16");
1484 seg->align = 16;
1485 break;
1486 case 32:
1487 case 64:
1488 case 128:
1489 nasm_warn(WARN_OTHER, "OBJ format does not support alignment"
1490 " of %d: rounding up to 256", seg->align);
1491 seg->align = 256;
1492 break;
1493 case 512:
1494 case 1024:
1495 case 2048:
1496 nasm_warn(WARN_OTHER, "OBJ format does not support alignment"
1497 " of %d: rounding up to 4096", seg->align);
1498 seg->align = 4096;
1499 break;
1500 default:
1501 nasm_nonfatal("invalid alignment value %d",
1502 seg->align);
1503 seg->align = 1;
1504 break;
1506 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
1507 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
1508 if (rn_error)
1509 nasm_nonfatal("argument to `absolute' segment"
1510 " attribute should be numeric");
1514 /* We need to know whenever we have at least one 32-bit segment */
1515 obj_use32 |= seg->use32;
1517 obj_seg_needs_update = seg;
1518 if (seg->align >= SEG_ABS)
1519 define_label(name, NO_SEG, seg->align - SEG_ABS, false);
1520 else
1521 define_label(name, seg->index + 1, 0L, false);
1522 obj_seg_needs_update = NULL;
1525 * See if this segment is defined in any groups.
1527 for (grp = grphead; grp; grp = grp->next) {
1528 for (i = grp->nindices; i < grp->nentries; i++) {
1529 if (!strcmp(grp->segs[i].name, seg->name)) {
1530 nasm_free(grp->segs[i].name);
1531 grp->segs[i] = grp->segs[grp->nindices];
1532 grp->segs[grp->nindices++].index = seg->obj_index;
1533 if (seg->grp)
1534 nasm_warn(WARN_OTHER, "segment `%s' is already part of"
1535 " a group: first one takes precedence",
1536 seg->name);
1537 else
1538 seg->grp = grp;
1544 * Walk through the list of externals with unresolved
1545 * default-WRT clauses, and resolve any that point at this
1546 * segment.
1548 extp = &dws;
1549 while (*extp) {
1550 if ((*extp)->defwrt_type == DEFWRT_STRING &&
1551 !strcmp((*extp)->defwrt_ptr.string, seg->name)) {
1552 nasm_free((*extp)->defwrt_ptr.string);
1553 (*extp)->defwrt_type = DEFWRT_SEGMENT;
1554 (*extp)->defwrt_ptr.seg = seg;
1555 *extp = (*extp)->next_dws;
1556 } else
1557 extp = &(*extp)->next_dws;
1560 if (seg->use32)
1561 *bits = 32;
1562 else
1563 *bits = 16;
1564 current_seg = seg;
1565 return seg->index;
1569 static enum directive_result
1570 obj_directive(enum directive directive, char *value)
1572 switch (directive) {
1573 case D_GROUP:
1575 char *p, *q, *v;
1576 if (pass_first()) { /* XXX */
1577 struct Group *grp;
1578 struct Segment *seg;
1579 struct External **extp;
1580 int obj_idx;
1582 q = value;
1583 while (*q == '.')
1584 q++; /* hack, but a documented one */
1585 v = q;
1586 while (*q && !nasm_isspace(*q))
1587 q++;
1588 if (nasm_isspace(*q)) {
1589 *q++ = '\0';
1590 while (*q && nasm_isspace(*q))
1591 q++;
1594 * Here we used to sanity-check the group directive to
1595 * ensure nobody tried to declare a group containing no
1596 * segments. However, OS/2 does this as standard
1597 * practice, so the sanity check has been removed.
1599 * if (!*q) {
1600 * nasm_error(ERR_NONFATAL,"GROUP directive contains no segments");
1601 * return DIRR_ERROR;
1605 obj_idx = 1;
1606 for (grp = grphead; grp; grp = grp->next) {
1607 obj_idx++;
1608 if (!strcmp(grp->name, v)) {
1609 nasm_nonfatal("group `%s' defined twice", v);
1610 return DIRR_ERROR;
1614 *grptail = grp = nasm_malloc(sizeof(*grp));
1615 grp->next = NULL;
1616 grptail = &grp->next;
1617 grp->index = seg_alloc();
1618 grp->obj_index = obj_idx;
1619 grp->nindices = grp->nentries = 0;
1620 grp->name = NULL;
1622 obj_grp_needs_update = grp;
1623 backend_label(v, grp->index + 1, 0L);
1624 obj_grp_needs_update = NULL;
1626 while (*q) {
1627 p = q;
1628 while (*q && !nasm_isspace(*q))
1629 q++;
1630 if (nasm_isspace(*q)) {
1631 *q++ = '\0';
1632 while (*q && nasm_isspace(*q))
1633 q++;
1636 * Now p contains a segment name. Find it.
1638 for (seg = seghead; seg; seg = seg->next)
1639 if (!strcmp(seg->name, p))
1640 break;
1641 if (seg) {
1643 * We have a segment index. Shift a name entry
1644 * to the end of the array to make room.
1646 grp->segs[grp->nentries++] = grp->segs[grp->nindices];
1647 grp->segs[grp->nindices++].index = seg->obj_index;
1648 if (seg->grp)
1649 nasm_warn(WARN_OTHER, "segment `%s' is already part of"
1650 " a group: first one takes precedence",
1651 seg->name);
1652 else
1653 seg->grp = grp;
1654 } else {
1656 * We have an as-yet undefined segment.
1657 * Remember its name, for later.
1659 grp->segs[grp->nentries++].name = nasm_strdup(p);
1664 * Walk through the list of externals with unresolved
1665 * default-WRT clauses, and resolve any that point at
1666 * this group.
1668 extp = &dws;
1669 while (*extp) {
1670 if ((*extp)->defwrt_type == DEFWRT_STRING &&
1671 !strcmp((*extp)->defwrt_ptr.string, grp->name)) {
1672 nasm_free((*extp)->defwrt_ptr.string);
1673 (*extp)->defwrt_type = DEFWRT_GROUP;
1674 (*extp)->defwrt_ptr.grp = grp;
1675 *extp = (*extp)->next_dws;
1676 } else
1677 extp = &(*extp)->next_dws;
1680 return DIRR_OK;
1682 case D_UPPERCASE:
1683 obj_uppercase = true;
1684 return DIRR_OK;
1686 case D_IMPORT:
1688 char *q, *extname, *libname, *impname;
1690 if (!pass_first()) /* XXX */
1691 return DIRR_OK;
1692 extname = q = value;
1693 while (*q && !nasm_isspace(*q))
1694 q++;
1695 if (nasm_isspace(*q)) {
1696 *q++ = '\0';
1697 while (*q && nasm_isspace(*q))
1698 q++;
1701 libname = q;
1702 while (*q && !nasm_isspace(*q))
1703 q++;
1704 if (nasm_isspace(*q)) {
1705 *q++ = '\0';
1706 while (*q && nasm_isspace(*q))
1707 q++;
1710 impname = q;
1712 if (!*extname || !*libname)
1713 nasm_nonfatal("`import' directive requires symbol name"
1714 " and library name");
1715 else {
1716 struct ImpDef *imp;
1717 bool err = false;
1719 imp = *imptail = nasm_malloc(sizeof(struct ImpDef));
1720 imptail = &imp->next;
1721 imp->next = NULL;
1722 imp->extname = nasm_strdup(extname);
1723 imp->libname = nasm_strdup(libname);
1724 imp->impindex = readnum(impname, &err);
1725 if (!*impname || err)
1726 imp->impname = nasm_strdup(impname);
1727 else
1728 imp->impname = NULL;
1731 return DIRR_OK;
1733 case D_EXPORT:
1735 char *q, *extname, *intname, *v;
1736 struct ExpDef *export;
1737 int flags = 0;
1738 unsigned int ordinal = 0;
1740 if (!pass_first())
1741 return DIRR_OK; /* ignore in pass two */
1742 intname = q = value;
1743 while (*q && !nasm_isspace(*q))
1744 q++;
1745 if (nasm_isspace(*q)) {
1746 *q++ = '\0';
1747 while (*q && nasm_isspace(*q))
1748 q++;
1751 extname = q;
1752 while (*q && !nasm_isspace(*q))
1753 q++;
1754 if (nasm_isspace(*q)) {
1755 *q++ = '\0';
1756 while (*q && nasm_isspace(*q))
1757 q++;
1760 if (!*intname) {
1761 nasm_nonfatal("`export' directive requires export name");
1762 return DIRR_OK;
1764 if (!*extname) {
1765 extname = intname;
1766 intname = "";
1768 while (*q) {
1769 v = q;
1770 while (*q && !nasm_isspace(*q))
1771 q++;
1772 if (nasm_isspace(*q)) {
1773 *q++ = '\0';
1774 while (*q && nasm_isspace(*q))
1775 q++;
1777 if (!nasm_stricmp(v, "resident"))
1778 flags |= EXPDEF_FLAG_RESIDENT;
1779 else if (!nasm_stricmp(v, "nodata"))
1780 flags |= EXPDEF_FLAG_NODATA;
1781 else if (!nasm_strnicmp(v, "parm=", 5)) {
1782 bool err = false;
1783 flags |= EXPDEF_MASK_PARMCNT & readnum(v + 5, &err);
1784 if (err) {
1785 nasm_nonfatal("value `%s' for `parm' is non-numeric", v + 5);
1786 return DIRR_ERROR;
1788 } else {
1789 bool err = false;
1790 ordinal = readnum(v, &err);
1791 if (err) {
1792 nasm_nonfatal("unrecognised export qualifier `%s'", v);
1793 return DIRR_ERROR;
1795 flags |= EXPDEF_FLAG_ORDINAL;
1799 export = *exptail = nasm_malloc(sizeof(struct ExpDef));
1800 exptail = &export->next;
1801 export->next = NULL;
1802 export->extname = nasm_strdup(extname);
1803 export->intname = nasm_strdup(intname);
1804 export->ordinal = ordinal;
1805 export->flags = flags;
1807 return DIRR_OK;
1809 default:
1810 return DIRR_UNKNOWN;
1814 static void obj_sectalign(int32_t seg, unsigned int value)
1816 struct Segment *s;
1818 list_for_each(s, seghead) {
1819 if (s->index == seg)
1820 break;
1824 * it should not be too big value
1825 * and applied on non-absolute sections
1827 if (!s || !is_power2(value) ||
1828 value > 4096 || s->align >= SEG_ABS)
1829 return;
1832 * FIXME: No code duplication please
1833 * consider making helper for this
1834 * mapping since section handler has
1835 * to do the same
1837 switch (value) {
1838 case 8:
1839 value = 16;
1840 break;
1841 case 32:
1842 case 64:
1843 case 128:
1844 value = 256;
1845 break;
1846 case 512:
1847 case 1024:
1848 case 2048:
1849 value = 4096;
1850 break;
1853 if (s->align < (int)value)
1854 s->align = value;
1857 static int32_t obj_segbase(int32_t segment)
1859 struct Segment *seg;
1862 * Find the segment in our list.
1864 for (seg = seghead; seg; seg = seg->next)
1865 if (seg->index == segment - 1)
1866 break;
1868 if (!seg) {
1870 * Might be an external with a default WRT.
1872 int32_t i = segment / 2;
1873 struct ExtBack *eb = ebhead;
1874 struct External *e;
1876 while (i >= EXT_BLKSIZ) {
1877 if (eb)
1878 eb = eb->next;
1879 else
1880 break;
1881 i -= EXT_BLKSIZ;
1883 if (eb) {
1884 e = eb->exts[i];
1885 if (!e) {
1886 /* Not available yet, probably a forward reference */
1887 nasm_assert(!pass_final());
1888 return NO_SEG;
1891 switch (e->defwrt_type) {
1892 case DEFWRT_NONE:
1893 return segment; /* fine */
1894 case DEFWRT_SEGMENT:
1895 return e->defwrt_ptr.seg->index + 1;
1896 case DEFWRT_GROUP:
1897 return e->defwrt_ptr.grp->index + 1;
1898 default:
1899 return NO_SEG; /* can't tell what it is */
1903 return segment; /* not one of ours - leave it alone */
1906 if (seg->align >= SEG_ABS)
1907 return seg->align; /* absolute segment */
1908 if (seg->grp)
1909 return seg->grp->index + 1; /* grouped segment */
1911 return segment; /* no special treatment */
1914 /* Get a file timestamp in MS-DOS format */
1915 static uint32_t obj_file_timestamp(const char *pathname)
1917 time_t t;
1918 const struct tm *lt;
1920 if (!nasm_file_time(&t, pathname))
1921 return 0;
1923 lt = localtime(&t);
1924 if (!lt)
1925 return 0;
1927 if (lt->tm_year < 80 || lt->tm_year > 207)
1928 return 0; /* Only years 1980-2107 representable */
1930 return
1931 ((uint32_t)lt->tm_sec >> 1) +
1932 ((uint32_t)lt->tm_min << 5) +
1933 ((uint32_t)lt->tm_hour << 11) +
1934 ((uint32_t)lt->tm_mday << 16) +
1935 (((uint32_t)lt->tm_mon + 1) << 21) +
1936 (((uint32_t)lt->tm_year - 80) << 25);
1939 static void obj_write_file(void)
1941 struct Segment *seg, *entry_seg_ptr = 0;
1942 struct FileName *fn;
1943 struct LineNumber *ln;
1944 struct Group *grp;
1945 struct Public *pub, *loc;
1946 struct External *ext;
1947 struct ImpDef *imp;
1948 struct ExpDef *export;
1949 int lname_idx;
1950 ObjRecord *orp;
1951 const struct strlist_entry *depfile;
1952 const bool debuginfo = (dfmt == &borland_debug_form);
1955 * Write the THEADR module header.
1957 orp = obj_new();
1958 orp->type = THEADR;
1959 obj_name(orp, obj_infile);
1960 obj_emit2(orp);
1963 * Write the NASM boast comment.
1965 orp->type = COMENT;
1966 obj_rword(orp, dTRANSL);
1967 obj_name(orp, nasm_comment());
1968 obj_emit2(orp);
1971 * Output file dependency information
1973 if (!obj_nodepend && depend_list) {
1974 strlist_for_each(depfile, depend_list) {
1975 uint32_t ts;
1977 ts = obj_file_timestamp(depfile->str);
1978 if (ts) {
1979 orp->type = COMENT;
1980 obj_rword(orp, dDEPFILE);
1981 obj_dword(orp, ts);
1982 obj_name(orp, depfile->str);
1983 obj_emit2(orp);
1988 orp->type = COMENT;
1990 * Write the IMPDEF records, if any.
1992 for (imp = imphead; imp; imp = imp->next) {
1993 obj_rword(orp, dOMFEXT);
1994 obj_byte(orp, 1); /* subfunction 1: IMPDEF */
1995 if (imp->impname)
1996 obj_byte(orp, 0); /* import by name */
1997 else
1998 obj_byte(orp, 1); /* import by ordinal */
1999 obj_name(orp, imp->extname);
2000 obj_name(orp, imp->libname);
2001 if (imp->impname)
2002 obj_name(orp, imp->impname);
2003 else
2004 obj_word(orp, imp->impindex);
2005 obj_emit2(orp);
2009 * Write the EXPDEF records, if any.
2011 for (export = exphead; export; export = export->next) {
2012 obj_rword(orp, dOMFEXT);
2013 obj_byte(orp, 2); /* subfunction 2: EXPDEF */
2014 obj_byte(orp, export->flags);
2015 obj_name(orp, export->extname);
2016 obj_name(orp, export->intname);
2017 if (export->flags & EXPDEF_FLAG_ORDINAL)
2018 obj_word(orp, export->ordinal);
2019 obj_emit2(orp);
2022 /* we're using extended OMF if we put in debug info */
2023 if (debuginfo) {
2024 orp->type = COMENT;
2025 obj_rword(orp, dEXTENDED);
2026 obj_emit2(orp);
2030 * Write the first LNAMES record, containing LNAME one, which
2031 * is null. Also initialize the LNAME counter.
2033 orp->type = LNAMES;
2034 obj_byte(orp, 0);
2035 lname_idx = 1;
2037 * Write some LNAMES for the segment names
2039 for (seg = seghead; seg; seg = seg->next) {
2040 orp = obj_name(orp, seg->name);
2041 if (seg->segclass)
2042 orp = obj_name(orp, seg->segclass);
2043 if (seg->overlay)
2044 orp = obj_name(orp, seg->overlay);
2045 obj_commit(orp);
2048 * Write some LNAMES for the group names
2050 for (grp = grphead; grp; grp = grp->next) {
2051 orp = obj_name(orp, grp->name);
2052 obj_commit(orp);
2054 obj_emit(orp);
2057 * Write the SEGDEF records.
2059 orp->type = SEGDEF;
2060 for (seg = seghead; seg; seg = seg->next) {
2061 int acbp;
2062 uint32_t seglen = seg->currentpos;
2064 acbp = (seg->combine << 2); /* C field */
2066 if (seg->use32)
2067 acbp |= 0x01; /* P bit is Use32 flag */
2068 else if (seglen == 0x10000L) {
2069 seglen = 0; /* This special case may be needed for old linkers */
2070 acbp |= 0x02; /* B bit */
2073 /* A field */
2074 if (seg->align >= SEG_ABS)
2075 /* acbp |= 0x00 */ ;
2076 else if (seg->align >= 4096) {
2077 if (seg->align > 4096)
2078 nasm_nonfatal("segment `%s' requires more alignment"
2079 " than OBJ format supports", seg->name);
2080 acbp |= 0xC0; /* PharLap extension */
2081 } else if (seg->align >= 256) {
2082 acbp |= 0x80;
2083 } else if (seg->align >= 16) {
2084 acbp |= 0x60;
2085 } else if (seg->align >= 4) {
2086 acbp |= 0xA0;
2087 } else if (seg->align >= 2) {
2088 acbp |= 0x40;
2089 } else
2090 acbp |= 0x20;
2092 obj_byte(orp, acbp);
2093 if (seg->align & SEG_ABS) {
2094 obj_x(orp, seg->align - SEG_ABS); /* Frame */
2095 obj_byte(orp, 0); /* Offset */
2097 obj_x(orp, seglen);
2098 obj_index(orp, ++lname_idx);
2099 obj_index(orp, seg->segclass ? ++lname_idx : 1);
2100 obj_index(orp, seg->overlay ? ++lname_idx : 1);
2101 obj_emit2(orp);
2105 * Write the GRPDEF records.
2107 orp->type = GRPDEF;
2108 for (grp = grphead; grp; grp = grp->next) {
2109 int i;
2111 if (grp->nindices != grp->nentries) {
2112 for (i = grp->nindices; i < grp->nentries; i++) {
2113 nasm_nonfatal("group `%s' contains undefined segment"
2114 " `%s'", grp->name, grp->segs[i].name);
2115 nasm_free(grp->segs[i].name);
2116 grp->segs[i].name = NULL;
2119 obj_index(orp, ++lname_idx);
2120 for (i = 0; i < grp->nindices; i++) {
2121 obj_byte(orp, 0xFF);
2122 obj_index(orp, grp->segs[i].index);
2124 obj_emit2(orp);
2128 * Write the PUBDEF records: first the ones in the segments,
2129 * then the far-absolutes.
2131 orp->type = PUBDEF;
2132 orp->ori = ori_pubdef;
2133 for (seg = seghead; seg; seg = seg->next) {
2134 orp->parm[0] = seg->grp ? seg->grp->obj_index : 0;
2135 orp->parm[1] = seg->obj_index;
2136 for (pub = seg->pubhead; pub; pub = pub->next) {
2137 orp = obj_name(orp, pub->name);
2138 orp = obj_x(orp, pub->offset);
2139 orp = obj_byte(orp, 0); /* type index */
2140 obj_commit(orp);
2142 obj_emit(orp);
2144 orp->parm[0] = 0;
2145 orp->parm[1] = 0;
2146 for (pub = fpubhead; pub; pub = pub->next) { /* pub-crawl :-) */
2147 if (orp->parm[2] != (uint32_t)pub->segment) {
2148 obj_emit(orp);
2149 orp->parm[2] = pub->segment;
2151 orp = obj_name(orp, pub->name);
2152 orp = obj_x(orp, pub->offset);
2153 orp = obj_byte(orp, 0); /* type index */
2154 obj_commit(orp);
2156 obj_emit(orp);
2159 * Write the EXTDEF and COMDEF records, in order.
2161 orp->ori = ori_null;
2162 for (ext = exthead; ext; ext = ext->next) {
2163 if (ext->commonsize == 0) {
2164 if (orp->type != EXTDEF) {
2165 obj_emit(orp);
2166 orp->type = EXTDEF;
2168 orp = obj_name(orp, ext->name);
2169 orp = obj_index(orp, 0);
2170 } else {
2171 if (orp->type != COMDEF) {
2172 obj_emit(orp);
2173 orp->type = COMDEF;
2175 orp = obj_name(orp, ext->name);
2176 orp = obj_index(orp, 0);
2177 if (ext->commonelem) {
2178 orp = obj_byte(orp, 0x61); /* far communal */
2179 orp = obj_value(orp, (ext->commonsize / ext->commonelem));
2180 orp = obj_value(orp, ext->commonelem);
2181 } else {
2182 orp = obj_byte(orp, 0x62); /* near communal */
2183 orp = obj_value(orp, ext->commonsize);
2186 obj_commit(orp);
2188 obj_emit(orp);
2191 * Write a COMENT record stating that the linker's first pass
2192 * may stop processing at this point. Exception is if our
2193 * MODEND record specifies a start point, in which case,
2194 * according to some variants of the documentation, this COMENT
2195 * should be omitted. So we'll omit it just in case.
2196 * But, TASM puts it in all the time so if we are using
2197 * TASM debug stuff we are putting it in
2199 if (debuginfo || obj_entry_seg == NO_SEG) {
2200 orp->type = COMENT;
2201 obj_rword(orp, dLINKPASS);
2202 obj_byte(orp, 1);
2203 obj_emit2(orp);
2207 * 1) put out the compiler type
2208 * 2) Put out the type info. The only type we are using is near label #19
2210 if (debuginfo) {
2211 int i;
2212 struct Array *arrtmp = arrhead;
2213 orp->type = COMENT;
2214 obj_rword(orp, dCOMPDEF);
2215 obj_byte(orp, 4);
2216 obj_byte(orp, 0);
2217 obj_emit2(orp);
2219 obj_rword(orp, dTYPEDEF);
2220 obj_word(orp, 0x18); /* type # for linking */
2221 obj_word(orp, 6); /* size of type */
2222 obj_byte(orp, 0x2a); /* absolute type for debugging */
2223 obj_emit2(orp);
2224 obj_rword(orp, dTYPEDEF);
2225 obj_word(orp, 0x19); /* type # for linking */
2226 obj_word(orp, 0); /* size of type */
2227 obj_byte(orp, 0x24); /* absolute type for debugging */
2228 obj_byte(orp, 0); /* near/far specifier */
2229 obj_emit2(orp);
2230 obj_rword(orp, dTYPEDEF);
2231 obj_word(orp, 0x1A); /* type # for linking */
2232 obj_word(orp, 0); /* size of type */
2233 obj_byte(orp, 0x24); /* absolute type for debugging */
2234 obj_byte(orp, 1); /* near/far specifier */
2235 obj_emit2(orp);
2236 obj_rword(orp, dTYPEDEF);
2237 obj_word(orp, 0x1b); /* type # for linking */
2238 obj_word(orp, 0); /* size of type */
2239 obj_byte(orp, 0x23); /* absolute type for debugging */
2240 obj_byte(orp, 0);
2241 obj_byte(orp, 0);
2242 obj_byte(orp, 0);
2243 obj_emit2(orp);
2244 obj_rword(orp, dTYPEDEF);
2245 obj_word(orp, 0x1c); /* type # for linking */
2246 obj_word(orp, 0); /* size of type */
2247 obj_byte(orp, 0x23); /* absolute type for debugging */
2248 obj_byte(orp, 0);
2249 obj_byte(orp, 4);
2250 obj_byte(orp, 0);
2251 obj_emit2(orp);
2252 obj_rword(orp, dTYPEDEF);
2253 obj_word(orp, 0x1d); /* type # for linking */
2254 obj_word(orp, 0); /* size of type */
2255 obj_byte(orp, 0x23); /* absolute type for debugging */
2256 obj_byte(orp, 0);
2257 obj_byte(orp, 1);
2258 obj_byte(orp, 0);
2259 obj_emit2(orp);
2260 obj_rword(orp, dTYPEDEF);
2261 obj_word(orp, 0x1e); /* type # for linking */
2262 obj_word(orp, 0); /* size of type */
2263 obj_byte(orp, 0x23); /* absolute type for debugging */
2264 obj_byte(orp, 0);
2265 obj_byte(orp, 5);
2266 obj_byte(orp, 0);
2267 obj_emit2(orp);
2269 /* put out the array types */
2270 for (i = ARRAYBOT; i < arrindex; i++) {
2271 obj_rword(orp, dTYPEDEF);
2272 obj_word(orp, i); /* type # for linking */
2273 obj_word(orp, arrtmp->size); /* size of type */
2274 obj_byte(orp, 0x1A); /* absolute type for debugging (array) */
2275 obj_byte(orp, arrtmp->basetype); /* base type */
2276 obj_emit2(orp);
2277 arrtmp = arrtmp->next;
2281 * write out line number info with a LINNUM record
2282 * switch records when we switch segments, and output the
2283 * file in a pseudo-TASM fashion. The record switch is naive; that
2284 * is that one file may have many records for the same segment
2285 * if there are lots of segment switches
2287 if (fnhead && debuginfo) {
2288 seg = fnhead->lnhead->segment;
2290 for (fn = fnhead; fn; fn = fn->next) {
2291 /* write out current file name */
2292 orp->type = COMENT;
2293 orp->ori = ori_null;
2294 obj_rword(orp, dFILNAME);
2295 obj_byte(orp, 0);
2296 obj_name(orp, fn->name);
2297 obj_dword(orp, 0);
2298 obj_emit2(orp);
2300 /* write out line numbers this file */
2302 orp->type = LINNUM;
2303 orp->ori = ori_linnum;
2304 for (ln = fn->lnhead; ln; ln = ln->next) {
2305 if (seg != ln->segment) {
2306 /* if we get here have to flush the buffer and start
2307 * a new record for a new segment
2309 seg = ln->segment;
2310 obj_emit(orp);
2312 orp->parm[0] = seg->grp ? seg->grp->obj_index : 0;
2313 orp->parm[1] = seg->obj_index;
2314 orp = obj_word(orp, ln->lineno);
2315 orp = obj_x(orp, ln->offset);
2316 obj_commit(orp);
2318 obj_emit(orp);
2322 * we are going to locate the entry point segment now
2323 * rather than wait until the MODEND record, because,
2324 * then we can output a special symbol to tell where the
2325 * entry point is.
2328 if (obj_entry_seg != NO_SEG) {
2329 for (seg = seghead; seg; seg = seg->next) {
2330 if (seg->index == obj_entry_seg) {
2331 entry_seg_ptr = seg;
2332 break;
2335 if (!seg)
2336 nasm_nonfatal("entry point is not in this module");
2340 * get ready to put out symbol records
2342 orp->type = COMENT;
2343 orp->ori = ori_local;
2346 * put out a symbol for the entry point
2347 * no dots in this symbol, because, borland does
2348 * not (officially) support dots in label names
2349 * and I don't know what various versions of TLINK will do
2351 if (debuginfo && obj_entry_seg != NO_SEG) {
2352 orp = obj_name(orp, "start_of_program");
2353 orp = obj_word(orp, 0x19); /* type: near label */
2354 orp = obj_index(orp, seg->grp ? seg->grp->obj_index : 0);
2355 orp = obj_index(orp, seg->obj_index);
2356 orp = obj_x(orp, obj_entry_ofs);
2357 obj_commit(orp);
2361 * put out the local labels
2363 for (seg = seghead; seg && debuginfo; seg = seg->next) {
2364 /* labels this seg */
2365 for (loc = seg->lochead; loc; loc = loc->next) {
2366 orp = obj_name(orp, loc->name);
2367 orp = obj_word(orp, loc->type);
2368 orp = obj_index(orp, seg->grp ? seg->grp->obj_index : 0);
2369 orp = obj_index(orp, seg->obj_index);
2370 orp = obj_x(orp, loc->offset);
2371 obj_commit(orp);
2374 if (orp->used)
2375 obj_emit(orp);
2378 * Write the LEDATA/FIXUPP pairs.
2380 for (seg = seghead; seg; seg = seg->next) {
2381 obj_emit(seg->orp);
2382 nasm_free(seg->orp);
2386 * Write the MODEND module end marker.
2388 orp->type = obj_use32 ? MODE32 : MODEND;
2389 orp->ori = ori_null;
2390 if (entry_seg_ptr) {
2391 orp->type = entry_seg_ptr->use32 ? MODE32 : MODEND;
2392 obj_byte(orp, 0xC1);
2393 seg = entry_seg_ptr;
2394 if (seg->grp) {
2395 obj_byte(orp, 0x10);
2396 obj_index(orp, seg->grp->obj_index);
2397 } else {
2399 * the below changed to prevent TLINK crashing.
2400 * Previous more efficient version read:
2402 * obj_byte (orp, 0x50);
2404 obj_byte(orp, 0x00);
2405 obj_index(orp, seg->obj_index);
2407 obj_index(orp, seg->obj_index);
2408 obj_x(orp, obj_entry_ofs);
2409 } else
2410 obj_byte(orp, 0);
2411 obj_emit2(orp);
2412 nasm_free(orp);
2415 static void obj_fwrite(ObjRecord * orp)
2417 unsigned int cksum, len;
2418 uint8_t *ptr;
2420 cksum = orp->type;
2421 if (orp->x_size == 32)
2422 cksum |= 1;
2423 fputc(cksum, ofile);
2424 len = orp->committed + 1;
2425 cksum += (len & 0xFF) + ((len >> 8) & 0xFF);
2426 fwriteint16_t(len, ofile);
2427 nasm_write(orp->buf, len-1, ofile);
2428 for (ptr = orp->buf; --len; ptr++)
2429 cksum += *ptr;
2430 fputc((-cksum) & 0xFF, ofile);
2433 static enum directive_result
2434 obj_pragma(const struct pragma *pragma)
2436 switch (pragma->opcode) {
2437 case D_NODEPEND:
2438 obj_nodepend = true;
2439 break;
2441 default:
2442 break;
2445 return DIRR_OK;
2448 extern macros_t obj_stdmac[];
2450 static void dbgbi_init(void)
2452 fnhead = NULL;
2453 fntail = &fnhead;
2454 arrindex = ARRAYBOT;
2455 arrhead = NULL;
2456 arrtail = &arrhead;
2458 static void dbgbi_cleanup(void)
2460 struct Segment *segtmp;
2461 while (fnhead) {
2462 struct FileName *fntemp = fnhead;
2463 while (fnhead->lnhead) {
2464 struct LineNumber *lntemp = fnhead->lnhead;
2465 fnhead->lnhead = lntemp->next;
2466 nasm_free(lntemp);
2468 fnhead = fnhead->next;
2469 nasm_free(fntemp->name);
2470 nasm_free(fntemp);
2472 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
2473 while (segtmp->lochead) {
2474 struct Public *loctmp = segtmp->lochead;
2475 segtmp->lochead = loctmp->next;
2476 nasm_free(loctmp->name);
2477 nasm_free(loctmp);
2480 while (arrhead) {
2481 struct Array *arrtmp = arrhead;
2482 arrhead = arrhead->next;
2483 nasm_free(arrtmp);
2487 static void dbgbi_linnum(const char *lnfname, int32_t lineno, int32_t segto)
2489 struct FileName *fn;
2490 struct LineNumber *ln;
2491 struct Segment *seg;
2493 if (segto == NO_SEG)
2494 return;
2497 * If `any_segs' is still false, we must define a default
2498 * segment.
2500 if (!any_segs) {
2501 int tempint = 0;
2502 if (segto != obj_segment("__NASMDEFSEG", &tempint))
2503 nasm_panic("strange segment conditions in OBJ driver");
2507 * Find the segment we are targetting.
2509 for (seg = seghead; seg; seg = seg->next)
2510 if (seg->index == segto)
2511 break;
2512 if (!seg)
2513 nasm_panic("lineno directed to nonexistent segment?");
2515 /* for (fn = fnhead; fn; fn = fnhead->next) */
2516 for (fn = fnhead; fn; fn = fn->next) /* fbk - Austin Lunnen - John Fine */
2517 if (!nasm_stricmp(lnfname, fn->name))
2518 break;
2519 if (!fn) {
2520 fn = nasm_malloc(sizeof(*fn));
2521 fn->name = nasm_malloc(strlen(lnfname) + 1);
2522 strcpy(fn->name, lnfname);
2523 fn->lnhead = NULL;
2524 fn->lntail = &fn->lnhead;
2525 fn->next = NULL;
2526 *fntail = fn;
2527 fntail = &fn->next;
2529 ln = nasm_malloc(sizeof(*ln));
2530 ln->segment = seg;
2531 ln->offset = seg->currentpos;
2532 ln->lineno = lineno;
2533 ln->next = NULL;
2534 *fn->lntail = ln;
2535 fn->lntail = &ln->next;
2538 static void dbgbi_deflabel(char *name, int32_t segment,
2539 int64_t offset, int is_global, char *special)
2541 struct Segment *seg;
2543 (void)special;
2546 * Note: ..[^@] special symbols are filtered in labels.c
2550 * If it's a special-retry from pass two, discard it.
2552 if (is_global == 3)
2553 return;
2556 * Case (i):
2558 if (obj_seg_needs_update) {
2559 return;
2560 } else if (obj_grp_needs_update) {
2561 return;
2563 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
2564 return;
2566 if (segment >= SEG_ABS || segment == NO_SEG) {
2567 return;
2571 * If `any_segs' is still false, we might need to define a
2572 * default segment, if they're trying to declare a label in
2573 * `first_seg'. But the label should exist due to a prior
2574 * call to obj_deflabel so we can skip that.
2577 for (seg = seghead; seg; seg = seg->next)
2578 if (seg->index == segment) {
2579 struct Public *loc = nasm_malloc(sizeof(*loc));
2581 * Case (ii). Maybe MODPUB someday?
2583 last_defined = *seg->loctail = loc;
2584 seg->loctail = &loc->next;
2585 loc->next = NULL;
2586 loc->name = nasm_strdup(name);
2587 loc->offset = offset;
2590 static void dbgbi_typevalue(int32_t type)
2592 int vsize;
2593 int elem = TYM_ELEMENTS(type);
2594 type = TYM_TYPE(type);
2596 if (!last_defined)
2597 return;
2599 switch (type) {
2600 case TY_BYTE:
2601 last_defined->type = 8; /* uint8_t */
2602 vsize = 1;
2603 break;
2604 case TY_WORD:
2605 last_defined->type = 10; /* unsigned word */
2606 vsize = 2;
2607 break;
2608 case TY_DWORD:
2609 last_defined->type = 12; /* unsigned dword */
2610 vsize = 4;
2611 break;
2612 case TY_FLOAT:
2613 last_defined->type = 14; /* float */
2614 vsize = 4;
2615 break;
2616 case TY_QWORD:
2617 last_defined->type = 15; /* qword */
2618 vsize = 8;
2619 break;
2620 case TY_TBYTE:
2621 last_defined->type = 16; /* TBYTE */
2622 vsize = 10;
2623 break;
2624 default:
2625 last_defined->type = 0x19; /* label */
2626 vsize = 0;
2627 break;
2630 if (elem > 1) {
2631 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
2632 int vtype = last_defined->type;
2633 arrtmp->size = vsize * elem;
2634 arrtmp->basetype = vtype;
2635 arrtmp->next = NULL;
2636 last_defined->type = arrindex++;
2637 *arrtail = arrtmp;
2638 arrtail = &(arrtmp->next);
2640 last_defined = NULL;
2642 static void dbgbi_output(int output_type, void *param)
2644 (void)output_type;
2645 (void)param;
2647 static const struct dfmt borland_debug_form = {
2648 "Borland Debug Records",
2649 "borland",
2650 dbgbi_init,
2651 dbgbi_linnum,
2652 dbgbi_deflabel,
2653 null_debug_directive,
2654 dbgbi_typevalue,
2655 dbgbi_output,
2656 dbgbi_cleanup,
2657 NULL /* pragma list */
2660 static const struct dfmt * const borland_debug_arr[3] = {
2661 &borland_debug_form,
2662 &null_debug_form,
2663 NULL
2666 static const struct pragma_facility obj_pragma_list[] = {
2667 { NULL, obj_pragma }
2670 const struct ofmt of_obj = {
2671 "Intel/Microsoft OMF (MS-DOS, OS/2, Win16)",
2672 "obj",
2673 ".obj",
2676 borland_debug_arr,
2677 &borland_debug_form,
2678 obj_stdmac,
2679 obj_init,
2680 null_reset,
2681 nasm_do_legacy_output,
2682 obj_out,
2683 obj_deflabel,
2684 obj_segment,
2685 NULL,
2686 obj_sectalign,
2687 obj_segbase,
2688 obj_directive,
2689 obj_cleanup,
2690 obj_pragma_list
2692 #endif /* OF_OBJ */