insns: add OBSOLETE flag for instructions removed from architecture
[nasm.git] / output / outobj.c
blobc33339f7c9743c48707d34d02b92b3df5105d735
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2016 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 <inttypes.h>
46 #include <limits.h>
48 #include "nasm.h"
49 #include "nasmlib.h"
50 #include "stdscan.h"
51 #include "eval.h"
52 #include "output/outform.h"
53 #include "output/outlib.h"
55 #ifdef OF_OBJ
58 * outobj.c is divided into two sections. The first section is low level
59 * routines for creating obj records; It has nearly zero NASM specific
60 * code. The second section is high level routines for processing calls and
61 * data structures from the rest of NASM into obj format.
63 * It should be easy (though not zero work) to lift the first section out for
64 * use as an obj file writer for some other assembler or compiler.
68 * These routines are built around the ObjRecord data struture. An ObjRecord
69 * holds an object file record that may be under construction or complete.
71 * A major function of these routines is to support continuation of an obj
72 * record into the next record when the maximum record size is exceeded. The
73 * high level code does not need to worry about where the record breaks occur.
74 * It does need to do some minor extra steps to make the automatic continuation
75 * work. Those steps may be skipped for records where the high level knows no
76 * continuation could be required.
78 * 1) An ObjRecord is allocated and cleared by obj_new, or an existing ObjRecord
79 * is cleared by obj_clear.
81 * 2) The caller should fill in .type.
83 * 3) If the record is continuable and there is processing that must be done at
84 * the start of each record then the caller should fill in .ori with the
85 * address of the record initializer routine.
87 * 4) If the record is continuable and it should be saved (rather than emitted
88 * immediately) as each record is done, the caller should set .up to be a
89 * pointer to a location in which the caller keeps the master pointer to the
90 * ObjRecord. When the record is continued, the obj_bump routine will then
91 * allocate a new ObjRecord structure and update the master pointer.
93 * 5) If the .ori field was used then the caller should fill in the .parm with
94 * any data required by the initializer.
96 * 6) The caller uses the routines: obj_byte, obj_word, obj_rword, obj_dword,
97 * obj_x, obj_index, obj_value and obj_name to fill in the various kinds of
98 * data required for this record.
100 * 7) If the record is continuable, the caller should call obj_commit at each
101 * point where breaking the record is permitted.
103 * 8) To write out the record, the caller should call obj_emit2. If the
104 * caller has called obj_commit for all data written then he can get slightly
105 * faster code by calling obj_emit instead of obj_emit2.
107 * Most of these routines return an ObjRecord pointer. This will be the input
108 * pointer most of the time and will be the new location if the ObjRecord
109 * moved as a result of the call. The caller may ignore the return value in
110 * three cases: It is a "Never Reallocates" routine; or The caller knows
111 * continuation is not possible; or The caller uses the master pointer for the
112 * next operation.
115 #define RECORD_MAX (1024-3) /* maximal size of any record except type+reclen */
116 #define OBJ_PARMS 3 /* maximum .parm used by any .ori routine */
118 #define FIX_08_LOW 0x8000 /* location type for various fixup subrecords */
119 #define FIX_16_OFFSET 0x8400
120 #define FIX_16_SELECTOR 0x8800
121 #define FIX_32_POINTER 0x8C00
122 #define FIX_08_HIGH 0x9000
123 #define FIX_32_OFFSET 0xA400
124 #define FIX_48_POINTER 0xAC00
126 enum RecordID { /* record ID codes */
128 THEADR = 0x80, /* module header */
129 COMENT = 0x88, /* comment record */
131 LINNUM = 0x94, /* line number record */
132 LNAMES = 0x96, /* list of names */
134 SEGDEF = 0x98, /* segment definition */
135 GRPDEF = 0x9A, /* group definition */
136 EXTDEF = 0x8C, /* external definition */
137 PUBDEF = 0x90, /* public definition */
138 COMDEF = 0xB0, /* common definition */
140 LEDATA = 0xA0, /* logical enumerated data */
141 FIXUPP = 0x9C, /* fixups (relocations) */
142 FIXU32 = 0x9D, /* 32-bit fixups (relocations) */
144 MODEND = 0x8A, /* module end */
145 MODE32 = 0x8B /* module end for 32-bit objects */
148 enum ComentID { /* ID codes for comment records */
150 dEXTENDED = 0xA1, /* tells that we are using translator-specific extensions */
151 dLINKPASS = 0xA2, /* link pass 2 marker */
152 dTYPEDEF = 0xE3, /* define a type */
153 dSYM = 0xE6, /* symbol debug record */
154 dFILNAME = 0xE8, /* file name record */
155 dCOMPDEF = 0xEA /* compiler type info */
158 typedef struct ObjRecord ObjRecord;
159 typedef void ORI(ObjRecord * orp);
161 struct ObjRecord {
162 ORI *ori; /* Initialization routine */
163 int used; /* Current data size */
164 int committed; /* Data size at last boundary */
165 int x_size; /* (see obj_x) */
166 unsigned int type; /* Record type */
167 ObjRecord *child; /* Associated record below this one */
168 ObjRecord **up; /* Master pointer to this ObjRecord */
169 ObjRecord *back; /* Previous part of this record */
170 uint32_t parm[OBJ_PARMS]; /* Parameters for ori routine */
171 uint8_t buf[RECORD_MAX + 3];
174 static void obj_fwrite(ObjRecord * orp);
175 static void ori_ledata(ObjRecord * orp);
176 static void ori_pubdef(ObjRecord * orp);
177 static void ori_null(ObjRecord * orp);
178 static ObjRecord *obj_commit(ObjRecord * orp);
180 static bool obj_uppercase; /* Flag: all names in uppercase */
181 static bool obj_use32; /* Flag: at least one segment is 32-bit */
184 * Clear an ObjRecord structure. (Never reallocates).
185 * To simplify reuse of ObjRecord's, .type, .ori and .parm are not cleared.
187 static ObjRecord *obj_clear(ObjRecord * orp)
189 orp->used = 0;
190 orp->committed = 0;
191 orp->x_size = 0;
192 orp->child = NULL;
193 orp->up = NULL;
194 orp->back = NULL;
195 return (orp);
199 * Emit an ObjRecord structure. (Never reallocates).
200 * The record is written out preceeded (recursively) by its previous part (if
201 * any) and followed (recursively) by its child (if any).
202 * The previous part and the child are freed. The main ObjRecord is cleared,
203 * not freed.
205 static ObjRecord *obj_emit(ObjRecord * orp)
207 if (orp->back) {
208 obj_emit(orp->back);
209 nasm_free(orp->back);
212 if (orp->committed)
213 obj_fwrite(orp);
215 if (orp->child) {
216 obj_emit(orp->child);
217 nasm_free(orp->child);
220 return (obj_clear(orp));
224 * Commit and Emit a record. (Never reallocates).
226 static ObjRecord *obj_emit2(ObjRecord * orp)
228 obj_commit(orp);
229 return (obj_emit(orp));
233 * Allocate and clear a new ObjRecord; Also sets .ori to ori_null
235 static ObjRecord *obj_new(void)
237 ObjRecord *orp;
239 orp = obj_clear(nasm_malloc(sizeof(ObjRecord)));
240 orp->ori = ori_null;
241 return (orp);
245 * Advance to the next record because the existing one is full or its x_size
246 * is incompatible.
247 * Any uncommited data is moved into the next record.
249 static ObjRecord *obj_bump(ObjRecord * orp)
251 ObjRecord *nxt;
252 int used = orp->used;
253 int committed = orp->committed;
255 if (orp->up) {
256 *orp->up = nxt = obj_new();
257 nxt->ori = orp->ori;
258 nxt->type = orp->type;
259 nxt->up = orp->up;
260 nxt->back = orp;
261 memcpy(nxt->parm, orp->parm, sizeof(orp->parm));
262 } else
263 nxt = obj_emit(orp);
265 used -= committed;
266 if (used) {
267 nxt->committed = 1;
268 nxt->ori(nxt);
269 nxt->committed = nxt->used;
270 memcpy(nxt->buf + nxt->committed, orp->buf + committed, used);
271 nxt->used = nxt->committed + used;
274 return (nxt);
278 * Advance to the next record if necessary to allow the next field to fit.
280 static ObjRecord *obj_check(ObjRecord * orp, int size)
282 if (orp->used + size > RECORD_MAX)
283 orp = obj_bump(orp);
285 if (!orp->committed) {
286 orp->committed = 1;
287 orp->ori(orp);
288 orp->committed = orp->used;
291 return (orp);
295 * All data written so far is commited to the current record (won't be moved to
296 * the next record in case of continuation).
298 static ObjRecord *obj_commit(ObjRecord * orp)
300 orp->committed = orp->used;
301 return (orp);
305 * Write a byte
307 static ObjRecord *obj_byte(ObjRecord * orp, uint8_t val)
309 orp = obj_check(orp, 1);
310 orp->buf[orp->used] = val;
311 orp->used++;
312 return (orp);
316 * Write a word
318 static ObjRecord *obj_word(ObjRecord * orp, unsigned int val)
320 orp = obj_check(orp, 2);
321 orp->buf[orp->used] = val;
322 orp->buf[orp->used + 1] = val >> 8;
323 orp->used += 2;
324 return (orp);
328 * Write a reversed word
330 static ObjRecord *obj_rword(ObjRecord * orp, unsigned int val)
332 orp = obj_check(orp, 2);
333 orp->buf[orp->used] = val >> 8;
334 orp->buf[orp->used + 1] = val;
335 orp->used += 2;
336 return (orp);
340 * Write a dword
342 static ObjRecord *obj_dword(ObjRecord * orp, uint32_t val)
344 orp = obj_check(orp, 4);
345 orp->buf[orp->used] = val;
346 orp->buf[orp->used + 1] = val >> 8;
347 orp->buf[orp->used + 2] = val >> 16;
348 orp->buf[orp->used + 3] = val >> 24;
349 orp->used += 4;
350 return (orp);
354 * All fields of "size x" in one obj record must be the same size (either 16
355 * bits or 32 bits). There is a one bit flag in each record which specifies
356 * which.
357 * This routine is used to force the current record to have the desired
358 * x_size. x_size is normally automatic (using obj_x), so that this
359 * routine should be used outside obj_x, only to provide compatibility with
360 * linkers that have bugs in their processing of the size bit.
363 static ObjRecord *obj_force(ObjRecord * orp, int x)
365 if (orp->x_size == (x ^ 48))
366 orp = obj_bump(orp);
367 orp->x_size = x;
368 return (orp);
372 * This routine writes a field of size x. The caller does not need to worry at
373 * all about whether 16-bits or 32-bits are required.
375 static ObjRecord *obj_x(ObjRecord * orp, uint32_t val)
377 if (orp->type & 1)
378 orp->x_size = 32;
379 if (val > 0xFFFF)
380 orp = obj_force(orp, 32);
381 if (orp->x_size == 32) {
382 ObjRecord *nxt = obj_dword(orp, val);
383 nxt->x_size = 32; /* x_size is cleared when a record overflows */
384 return nxt;
386 orp->x_size = 16;
387 return (obj_word(orp, val));
391 * Writes an index
393 static ObjRecord *obj_index(ObjRecord * orp, unsigned int val)
395 if (val < 128)
396 return (obj_byte(orp, val));
397 return (obj_word(orp, (val >> 8) | (val << 8) | 0x80));
401 * Writes a variable length value
403 static ObjRecord *obj_value(ObjRecord * orp, uint32_t val)
405 if (val <= 128)
406 return (obj_byte(orp, val));
407 if (val <= 0xFFFF) {
408 orp = obj_byte(orp, 129);
409 return (obj_word(orp, val));
411 if (val <= 0xFFFFFF)
412 return (obj_dword(orp, (val << 8) + 132));
413 orp = obj_byte(orp, 136);
414 return (obj_dword(orp, val));
418 * Writes a counted string
420 static ObjRecord *obj_name(ObjRecord * orp, const char *name)
422 int len = strlen(name);
423 uint8_t *ptr;
425 orp = obj_check(orp, len + 1);
426 ptr = orp->buf + orp->used;
427 *ptr++ = len;
428 orp->used += len + 1;
429 if (obj_uppercase)
430 while (--len >= 0) {
431 *ptr++ = toupper(*name);
432 name++;
433 } else
434 memcpy(ptr, name, len);
435 return (orp);
439 * Initializer for an LEDATA record.
440 * parm[0] = offset
441 * parm[1] = segment index
442 * During the use of a LEDATA ObjRecord, parm[0] is constantly updated to
443 * represent the offset that would be required if the record were split at the
444 * last commit point.
445 * parm[2] is a copy of parm[0] as it was when the current record was initted.
447 static void ori_ledata(ObjRecord * orp)
449 obj_index(orp, orp->parm[1]);
450 orp->parm[2] = orp->parm[0];
451 obj_x(orp, orp->parm[0]);
455 * Initializer for a PUBDEF record.
456 * parm[0] = group index
457 * parm[1] = segment index
458 * parm[2] = frame (only used when both indexes are zero)
460 static void ori_pubdef(ObjRecord * orp)
462 obj_index(orp, orp->parm[0]);
463 obj_index(orp, orp->parm[1]);
464 if (!(orp->parm[0] | orp->parm[1]))
465 obj_word(orp, orp->parm[2]);
469 * Initializer for a LINNUM record.
470 * parm[0] = group index
471 * parm[1] = segment index
473 static void ori_linnum(ObjRecord * orp)
475 obj_index(orp, orp->parm[0]);
476 obj_index(orp, orp->parm[1]);
480 * Initializer for a local vars record.
482 static void ori_local(ObjRecord * orp)
484 obj_byte(orp, 0x40);
485 obj_byte(orp, dSYM);
489 * Null initializer for records that continue without any header info
491 static void ori_null(ObjRecord * orp)
493 (void)orp; /* Do nothing */
497 * This concludes the low level section of outobj.c
500 static char obj_infile[FILENAME_MAX];
502 static int32_t first_seg;
503 static bool any_segs;
504 static int passtwo;
505 static int arrindex;
507 #define GROUP_MAX 256 /* we won't _realistically_ have more
508 * than this many segs in a group */
509 #define EXT_BLKSIZ 256 /* block size for externals list */
511 struct Segment; /* need to know these structs exist */
512 struct Group;
514 struct LineNumber {
515 struct LineNumber *next;
516 struct Segment *segment;
517 int32_t offset;
518 int32_t lineno;
521 static struct FileName {
522 struct FileName *next;
523 char *name;
524 struct LineNumber *lnhead, **lntail;
525 int index;
526 } *fnhead, **fntail;
528 static struct Array {
529 struct Array *next;
530 unsigned size;
531 int basetype;
532 } *arrhead, **arrtail;
534 #define ARRAYBOT 31 /* magic number for first array index */
536 static struct Public {
537 struct Public *next;
538 char *name;
539 int32_t offset;
540 int32_t segment; /* only if it's far-absolute */
541 int type; /* only for local debug syms */
542 } *fpubhead, **fpubtail, *last_defined;
544 static struct External {
545 struct External *next;
546 char *name;
547 int32_t commonsize;
548 int32_t commonelem; /* element size if FAR, else zero */
549 int index; /* OBJ-file external index */
550 enum {
551 DEFWRT_NONE, /* no unusual default-WRT */
552 DEFWRT_STRING, /* a string we don't yet understand */
553 DEFWRT_SEGMENT, /* a segment */
554 DEFWRT_GROUP /* a group */
555 } defwrt_type;
556 union {
557 char *string;
558 struct Segment *seg;
559 struct Group *grp;
560 } defwrt_ptr;
561 struct External *next_dws; /* next with DEFWRT_STRING */
562 } *exthead, **exttail, *dws;
564 static int externals;
566 static struct ExtBack {
567 struct ExtBack *next;
568 struct External *exts[EXT_BLKSIZ];
569 } *ebhead, **ebtail;
571 static struct Segment {
572 struct Segment *next;
573 char *name;
574 int32_t index; /* the NASM segment id */
575 int32_t obj_index; /* the OBJ-file segment index */
576 struct Group *grp; /* the group it beint32_ts to */
577 uint32_t currentpos;
578 int32_t align; /* can be SEG_ABS + absolute addr */
579 struct Public *pubhead, **pubtail, *lochead, **loctail;
580 char *segclass, *overlay; /* `class' is a C++ keyword :-) */
581 ObjRecord *orp;
582 enum {
583 CMB_PRIVATE = 0,
584 CMB_PUBLIC = 2,
585 CMB_STACK = 5,
586 CMB_COMMON = 6
587 } combine;
588 bool use32; /* is this segment 32-bit? */
589 } *seghead, **segtail, *obj_seg_needs_update;
591 static struct Group {
592 struct Group *next;
593 char *name;
594 int32_t index; /* NASM segment id */
595 int32_t obj_index; /* OBJ-file group index */
596 int32_t nentries; /* number of elements... */
597 int32_t nindices; /* ...and number of index elts... */
598 union {
599 int32_t index;
600 char *name;
601 } segs[GROUP_MAX]; /* ...in this */
602 } *grphead, **grptail, *obj_grp_needs_update;
604 static struct ImpDef {
605 struct ImpDef *next;
606 char *extname;
607 char *libname;
608 unsigned int impindex;
609 char *impname;
610 } *imphead, **imptail;
612 static struct ExpDef {
613 struct ExpDef *next;
614 char *intname;
615 char *extname;
616 unsigned int ordinal;
617 int flags;
618 } *exphead, **exptail;
620 #define EXPDEF_FLAG_ORDINAL 0x80
621 #define EXPDEF_FLAG_RESIDENT 0x40
622 #define EXPDEF_FLAG_NODATA 0x20
623 #define EXPDEF_MASK_PARMCNT 0x1F
625 static int32_t obj_entry_seg, obj_entry_ofs;
627 struct ofmt of_obj;
628 static struct dfmt borland_debug_form;
630 /* The current segment */
631 static struct Segment *current_seg;
633 static int32_t obj_segment(char *, int, int *);
634 static void obj_write_file(void);
635 static int obj_directive(enum directives, char *, int);
637 static void obj_init(void)
639 first_seg = seg_alloc();
640 any_segs = false;
641 fpubhead = NULL;
642 fpubtail = &fpubhead;
643 exthead = NULL;
644 exttail = &exthead;
645 imphead = NULL;
646 imptail = &imphead;
647 exphead = NULL;
648 exptail = &exphead;
649 dws = NULL;
650 externals = 0;
651 ebhead = NULL;
652 ebtail = &ebhead;
653 seghead = obj_seg_needs_update = NULL;
654 segtail = &seghead;
655 grphead = obj_grp_needs_update = NULL;
656 grptail = &grphead;
657 obj_entry_seg = NO_SEG;
658 obj_uppercase = false;
659 obj_use32 = false;
660 passtwo = 0;
661 current_seg = NULL;
664 static int obj_set_info(enum geninfo type, char **val)
666 (void)type;
667 (void)val;
669 return 0;
672 static void obj_cleanup(void)
674 obj_write_file();
675 dfmt->cleanup();
676 while (seghead) {
677 struct Segment *segtmp = seghead;
678 seghead = seghead->next;
679 while (segtmp->pubhead) {
680 struct Public *pubtmp = segtmp->pubhead;
681 segtmp->pubhead = pubtmp->next;
682 nasm_free(pubtmp->name);
683 nasm_free(pubtmp);
685 nasm_free(segtmp->segclass);
686 nasm_free(segtmp->overlay);
687 nasm_free(segtmp);
689 while (fpubhead) {
690 struct Public *pubtmp = fpubhead;
691 fpubhead = fpubhead->next;
692 nasm_free(pubtmp->name);
693 nasm_free(pubtmp);
695 while (exthead) {
696 struct External *exttmp = exthead;
697 exthead = exthead->next;
698 nasm_free(exttmp);
700 while (imphead) {
701 struct ImpDef *imptmp = imphead;
702 imphead = imphead->next;
703 nasm_free(imptmp->extname);
704 nasm_free(imptmp->libname);
705 nasm_free(imptmp->impname); /* nasm_free won't mind if it's NULL */
706 nasm_free(imptmp);
708 while (exphead) {
709 struct ExpDef *exptmp = exphead;
710 exphead = exphead->next;
711 nasm_free(exptmp->extname);
712 nasm_free(exptmp->intname);
713 nasm_free(exptmp);
715 while (ebhead) {
716 struct ExtBack *ebtmp = ebhead;
717 ebhead = ebhead->next;
718 nasm_free(ebtmp);
720 while (grphead) {
721 struct Group *grptmp = grphead;
722 grphead = grphead->next;
723 nasm_free(grptmp);
727 static void obj_ext_set_defwrt(struct External *ext, char *id)
729 struct Segment *seg;
730 struct Group *grp;
732 for (seg = seghead; seg; seg = seg->next)
733 if (!strcmp(seg->name, id)) {
734 ext->defwrt_type = DEFWRT_SEGMENT;
735 ext->defwrt_ptr.seg = seg;
736 nasm_free(id);
737 return;
740 for (grp = grphead; grp; grp = grp->next)
741 if (!strcmp(grp->name, id)) {
742 ext->defwrt_type = DEFWRT_GROUP;
743 ext->defwrt_ptr.grp = grp;
744 nasm_free(id);
745 return;
748 ext->defwrt_type = DEFWRT_STRING;
749 ext->defwrt_ptr.string = id;
750 ext->next_dws = dws;
751 dws = ext;
754 static void obj_deflabel(char *name, int32_t segment,
755 int64_t offset, int is_global, char *special)
758 * We have three cases:
760 * (i) `segment' is a segment-base. If so, set the name field
761 * for the segment or group structure it refers to, and then
762 * return.
764 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
765 * Save the label position for later output of a PUBDEF record.
766 * (Or a MODPUB, if we work out how.)
768 * (iii) `segment' is not one of our segments. Save the label
769 * position for later output of an EXTDEF, and also store a
770 * back-reference so that we can map later references to this
771 * segment number to the external index.
773 struct External *ext;
774 struct ExtBack *eb;
775 struct Segment *seg;
776 int i;
777 bool used_special = false; /* have we used the special text? */
779 #if defined(DEBUG) && DEBUG>2
780 nasm_error(ERR_DEBUG,
781 " obj_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
782 name, segment, offset, is_global, special);
783 #endif
786 * If it's a special-retry from pass two, discard it.
788 if (is_global == 3)
789 return;
792 * First check for the double-period, signifying something
793 * unusual.
795 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
796 if (!strcmp(name, "..start")) {
797 obj_entry_seg = segment;
798 obj_entry_ofs = offset;
799 return;
801 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
805 * Case (i):
807 if (obj_seg_needs_update) {
808 obj_seg_needs_update->name = name;
809 return;
810 } else if (obj_grp_needs_update) {
811 obj_grp_needs_update->name = name;
812 return;
814 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
815 return;
817 if (segment >= SEG_ABS || segment == NO_SEG) {
819 * SEG_ABS subcase of (ii).
821 if (is_global) {
822 struct Public *pub;
824 pub = *fpubtail = nasm_malloc(sizeof(*pub));
825 fpubtail = &pub->next;
826 pub->next = NULL;
827 pub->name = nasm_strdup(name);
828 pub->offset = offset;
829 pub->segment = (segment == NO_SEG ? 0 : segment & ~SEG_ABS);
831 if (special)
832 nasm_error(ERR_NONFATAL, "OBJ supports no special symbol features"
833 " for this symbol type");
834 return;
838 * If `any_segs' is still false, we might need to define a
839 * default segment, if they're trying to declare a label in
840 * `first_seg'.
842 if (!any_segs && segment == first_seg) {
843 int tempint; /* ignored */
844 if (segment != obj_segment("__NASMDEFSEG", 2, &tempint))
845 nasm_panic(0, "strange segment conditions in OBJ driver");
848 for (seg = seghead; seg && is_global; seg = seg->next)
849 if (seg->index == segment) {
850 struct Public *loc = nasm_malloc(sizeof(*loc));
852 * Case (ii). Maybe MODPUB someday?
854 *seg->pubtail = loc;
855 seg->pubtail = &loc->next;
856 loc->next = NULL;
857 loc->name = nasm_strdup(name);
858 loc->offset = offset;
860 if (special)
861 nasm_error(ERR_NONFATAL,
862 "OBJ supports no special symbol features"
863 " for this symbol type");
864 return;
868 * Case (iii).
870 if (is_global) {
871 ext = *exttail = nasm_malloc(sizeof(*ext));
872 ext->next = NULL;
873 exttail = &ext->next;
874 ext->name = name;
875 /* Place by default all externs into the current segment */
876 ext->defwrt_type = DEFWRT_NONE;
878 /* 28-Apr-2002 - John Coffman
879 The following code was introduced on 12-Aug-2000, and breaks fixups
880 on code passed thru the MSC 5.1 linker (3.66) and MSC 6.00A linker
881 (5.10). It was introduced after FIXUP32 was added, and may be needed
882 for 32-bit segments. The following will get 16-bit segments working
883 again, and maybe someone can correct the 'if' condition which is
884 actually needed.
886 #if 0
887 if (current_seg) {
888 #else
889 if (current_seg && current_seg->use32) {
890 if (current_seg->grp) {
891 ext->defwrt_type = DEFWRT_GROUP;
892 ext->defwrt_ptr.grp = current_seg->grp;
893 } else {
894 ext->defwrt_type = DEFWRT_SEGMENT;
895 ext->defwrt_ptr.seg = current_seg;
898 #endif
900 if (is_global == 2) {
901 ext->commonsize = offset;
902 ext->commonelem = 1; /* default FAR */
903 } else
904 ext->commonsize = 0;
905 } else
906 return;
909 * Now process the special text, if any, to find default-WRT
910 * specifications and common-variable element-size and near/far
911 * specifications.
913 while (special && *special) {
914 used_special = true;
917 * We might have a default-WRT specification.
919 if (!nasm_strnicmp(special, "wrt", 3)) {
920 char *p;
921 int len;
922 special += 3;
923 special += strspn(special, " \t");
924 p = nasm_strndup(special, len = strcspn(special, ":"));
925 obj_ext_set_defwrt(ext, p);
926 special += len;
927 if (*special && *special != ':')
928 nasm_error(ERR_NONFATAL, "`:' expected in special symbol"
929 " text for `%s'", ext->name);
930 else if (*special == ':')
931 special++;
935 * The NEAR or FAR keywords specify nearness or
936 * farness. FAR gives default element size 1.
938 if (!nasm_strnicmp(special, "far", 3)) {
939 if (ext->commonsize)
940 ext->commonelem = 1;
941 else
942 nasm_error(ERR_NONFATAL,
943 "`%s': `far' keyword may only be applied"
944 " to common variables\n", ext->name);
945 special += 3;
946 special += strspn(special, " \t");
947 } else if (!nasm_strnicmp(special, "near", 4)) {
948 if (ext->commonsize)
949 ext->commonelem = 0;
950 else
951 nasm_error(ERR_NONFATAL,
952 "`%s': `far' keyword may only be applied"
953 " to common variables\n", ext->name);
954 special += 4;
955 special += strspn(special, " \t");
959 * If it's a common, and anything else remains on the line
960 * before a further colon, evaluate it as an expression and
961 * use that as the element size. Forward references aren't
962 * allowed.
964 if (*special == ':')
965 special++;
966 else if (*special) {
967 if (ext->commonsize) {
968 expr *e;
969 struct tokenval tokval;
971 stdscan_reset();
972 stdscan_set(special);
973 tokval.t_type = TOKEN_INVALID;
974 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
975 if (e) {
976 if (!is_simple(e))
977 nasm_error(ERR_NONFATAL, "cannot use relocatable"
978 " expression as common-variable element size");
979 else
980 ext->commonelem = reloc_value(e);
982 special = stdscan_get();
983 } else {
984 nasm_error(ERR_NONFATAL,
985 "`%s': element-size specifications only"
986 " apply to common variables", ext->name);
987 while (*special && *special != ':')
988 special++;
989 if (*special == ':')
990 special++;
995 i = segment / 2;
996 eb = ebhead;
997 if (!eb) {
998 eb = *ebtail = nasm_zalloc(sizeof(*eb));
999 eb->next = NULL;
1000 ebtail = &eb->next;
1002 while (i >= EXT_BLKSIZ) {
1003 if (eb && eb->next)
1004 eb = eb->next;
1005 else {
1006 eb = *ebtail = nasm_zalloc(sizeof(*eb));
1007 eb->next = NULL;
1008 ebtail = &eb->next;
1010 i -= EXT_BLKSIZ;
1012 eb->exts[i] = ext;
1013 ext->index = ++externals;
1015 if (special && !used_special)
1016 nasm_error(ERR_NONFATAL, "OBJ supports no special symbol features"
1017 " for this symbol type");
1020 /* forward declaration */
1021 static void obj_write_fixup(ObjRecord * orp, int bytes,
1022 int segrel, int32_t seg, int32_t wrt,
1023 struct Segment *segto);
1025 static void obj_out(int32_t segto, const void *data,
1026 enum out_type type, uint64_t size,
1027 int32_t segment, int32_t wrt)
1029 const uint8_t *ucdata;
1030 int32_t ldata;
1031 struct Segment *seg;
1032 ObjRecord *orp;
1035 * handle absolute-assembly (structure definitions)
1037 if (segto == NO_SEG) {
1038 if (type != OUT_RESERVE)
1039 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
1040 " space");
1041 return;
1045 * If `any_segs' is still false, we must define a default
1046 * segment.
1048 if (!any_segs) {
1049 int tempint; /* ignored */
1050 if (segto != obj_segment("__NASMDEFSEG", 2, &tempint))
1051 nasm_panic(0, "strange segment conditions in OBJ driver");
1055 * Find the segment we are targetting.
1057 for (seg = seghead; seg; seg = seg->next)
1058 if (seg->index == segto)
1059 break;
1060 if (!seg)
1061 nasm_panic(0, "code directed to nonexistent segment?");
1063 orp = seg->orp;
1064 orp->parm[0] = seg->currentpos;
1066 switch (type) {
1067 case OUT_RAWDATA:
1068 ucdata = data;
1069 while (size > 0) {
1070 unsigned int len;
1071 orp = obj_check(seg->orp, 1);
1072 len = RECORD_MAX - orp->used;
1073 if (len > size)
1074 len = size;
1075 memcpy(orp->buf + orp->used, ucdata, len);
1076 orp->committed = orp->used += len;
1077 orp->parm[0] = seg->currentpos += len;
1078 ucdata += len;
1079 size -= len;
1081 break;
1083 case OUT_ADDRESS:
1084 case OUT_REL1ADR:
1085 case OUT_REL2ADR:
1086 case OUT_REL4ADR:
1087 case OUT_REL8ADR:
1089 int rsize;
1091 if (type == OUT_ADDRESS)
1092 size = abs((int)size);
1094 if (segment == NO_SEG && type != OUT_ADDRESS)
1095 nasm_error(ERR_NONFATAL, "relative call to absolute address not"
1096 " supported by OBJ format");
1097 if (segment >= SEG_ABS)
1098 nasm_error(ERR_NONFATAL, "far-absolute relocations not supported"
1099 " by OBJ format");
1101 ldata = *(int64_t *)data;
1102 if (type != OUT_ADDRESS) {
1104 * For 16-bit and 32-bit x86 code, the size and realsize() always
1105 * matches as only jumps, calls and loops uses PC relative
1106 * addressing and the address isn't followed by any other opcode
1107 * bytes. In 64-bit mode there is RIP relative addressing which
1108 * means the fixup location can be followed by an immediate value,
1109 * meaning that size > realsize().
1111 * When the CPU is calculating the effective address, it takes the
1112 * RIP at the end of the instruction and adds the fixed up relative
1113 * address value to it.
1115 * The linker's point of reference is the end of the fixup location
1116 * (which is the end of the instruction for Jcc, CALL, LOOP[cc]).
1117 * It is calculating distance between the target symbol and the end
1118 * of the fixup location, and add this to the displacement value we
1119 * are calculating here and storing at the fixup location.
1121 * To get the right effect, we need to _reduce_ the displacement
1122 * value by the number of bytes following the fixup.
1124 * Example:
1125 * data at address 0x100; REL4ADR at 0x050, 4 byte immediate,
1126 * end of fixup at 0x054, end of instruction at 0x058.
1127 * => size = 8.
1128 * => realsize() -> 4
1129 * => CPU needs a value of: 0x100 - 0x058 = 0x0a8
1130 * => linker/loader will add: 0x100 - 0x054 = 0x0ac
1131 * => We must add an addend of -4.
1132 * => realsize() - size = -4.
1134 * The code used to do size - realsize() at least since v0.90,
1135 * probably because it wasn't needed...
1137 ldata -= size;
1138 size = realsize(type, size);
1139 ldata += size;
1142 if (size > UINT_MAX)
1143 size = 0;
1145 switch ((unsigned int)size) {
1146 default:
1147 nasm_error(ERR_NONFATAL, "OBJ format can only handle 16- or "
1148 "32-byte relocations");
1149 segment = NO_SEG; /* Don't actually generate a relocation */
1150 break;
1151 case 2:
1152 orp = obj_word(orp, ldata);
1153 break;
1154 case 4:
1155 orp = obj_dword(orp, ldata);
1156 break;
1159 rsize = size;
1160 if (segment < SEG_ABS && (segment != NO_SEG && segment % 2) &&
1161 size == 4) {
1163 * This is a 4-byte segment-base relocation such as
1164 * `MOV EAX,SEG foo'. OBJ format can't actually handle
1165 * these, but if the constant term has the 16 low bits
1166 * zero, we can just apply a 2-byte segment-base
1167 * relocation to the low word instead.
1169 rsize = 2;
1170 if (ldata & 0xFFFF)
1171 nasm_error(ERR_NONFATAL, "OBJ format cannot handle complex"
1172 " dword-size segment base references");
1174 if (segment != NO_SEG)
1175 obj_write_fixup(orp, rsize,
1176 (type == OUT_ADDRESS ? 0x4000 : 0),
1177 segment, wrt, seg);
1178 seg->currentpos += size;
1179 break;
1182 default:
1183 nasm_error(ERR_NONFATAL,
1184 "Relocation type not supported by output format");
1185 /* fall through */
1187 case OUT_RESERVE:
1188 if (orp->committed)
1189 orp = obj_bump(orp);
1190 seg->currentpos += size;
1191 break;
1193 obj_commit(orp);
1196 static void obj_write_fixup(ObjRecord * orp, int bytes,
1197 int segrel, int32_t seg, int32_t wrt,
1198 struct Segment *segto)
1200 unsigned locat;
1201 int method;
1202 int base;
1203 int32_t tidx, fidx;
1204 struct Segment *s = NULL;
1205 struct Group *g = NULL;
1206 struct External *e = NULL;
1207 ObjRecord *forp;
1209 if (bytes != 2 && bytes != 4) {
1210 nasm_error(ERR_NONFATAL, "`obj' output driver does not support"
1211 " %d-bit relocations", bytes << 3);
1212 return;
1215 forp = orp->child;
1216 if (forp == NULL) {
1217 orp->child = forp = obj_new();
1218 forp->up = &(orp->child);
1219 /* We should choose between FIXUPP and FIXU32 record type */
1220 /* If we're targeting a 32-bit segment, use a FIXU32 record */
1221 if (segto->use32)
1222 forp->type = FIXU32;
1223 else
1224 forp->type = FIXUPP;
1227 if (seg % 2) {
1228 base = true;
1229 locat = FIX_16_SELECTOR;
1230 seg--;
1231 if (bytes != 2)
1232 nasm_panic(0, "OBJ: 4-byte segment base fixup got"
1233 " through sanity check");
1234 } else {
1235 base = false;
1236 locat = (bytes == 2) ? FIX_16_OFFSET : FIX_32_OFFSET;
1237 if (!segrel)
1239 * There is a bug in tlink that makes it process self relative
1240 * fixups incorrectly if the x_size doesn't match the location
1241 * size.
1243 forp = obj_force(forp, bytes << 3);
1246 forp = obj_rword(forp, locat | segrel | (orp->parm[0] - orp->parm[2]));
1248 tidx = fidx = -1, method = 0; /* placate optimisers */
1251 * See if we can find the segment ID in our segment list. If
1252 * so, we have a T4 (LSEG) target.
1254 for (s = seghead; s; s = s->next)
1255 if (s->index == seg)
1256 break;
1257 if (s)
1258 method = 4, tidx = s->obj_index;
1259 else {
1260 for (g = grphead; g; g = g->next)
1261 if (g->index == seg)
1262 break;
1263 if (g)
1264 method = 5, tidx = g->obj_index;
1265 else {
1266 int32_t i = seg / 2;
1267 struct ExtBack *eb = ebhead;
1268 while (i >= EXT_BLKSIZ) {
1269 if (eb)
1270 eb = eb->next;
1271 else
1272 break;
1273 i -= EXT_BLKSIZ;
1275 if (eb)
1276 method = 6, e = eb->exts[i], tidx = e->index;
1277 else
1278 nasm_panic(0,
1279 "unrecognised segment value in obj_write_fixup");
1284 * If no WRT given, assume the natural default, which is method
1285 * F5 unless:
1287 * - we are doing an OFFSET fixup for a grouped segment, in
1288 * which case we require F1 (group).
1290 * - we are doing an OFFSET fixup for an external with a
1291 * default WRT, in which case we must honour the default WRT.
1293 if (wrt == NO_SEG) {
1294 if (!base && s && s->grp)
1295 method |= 0x10, fidx = s->grp->obj_index;
1296 else if (!base && e && e->defwrt_type != DEFWRT_NONE) {
1297 if (e->defwrt_type == DEFWRT_SEGMENT)
1298 method |= 0x00, fidx = e->defwrt_ptr.seg->obj_index;
1299 else if (e->defwrt_type == DEFWRT_GROUP)
1300 method |= 0x10, fidx = e->defwrt_ptr.grp->obj_index;
1301 else {
1302 nasm_error(ERR_NONFATAL, "default WRT specification for"
1303 " external `%s' unresolved", e->name);
1304 method |= 0x50, fidx = -1; /* got to do _something_ */
1306 } else
1307 method |= 0x50, fidx = -1;
1308 } else {
1310 * See if we can find the WRT-segment ID in our segment
1311 * list. If so, we have a F0 (LSEG) frame.
1313 for (s = seghead; s; s = s->next)
1314 if (s->index == wrt - 1)
1315 break;
1316 if (s)
1317 method |= 0x00, fidx = s->obj_index;
1318 else {
1319 for (g = grphead; g; g = g->next)
1320 if (g->index == wrt - 1)
1321 break;
1322 if (g)
1323 method |= 0x10, fidx = g->obj_index;
1324 else {
1325 int32_t i = wrt / 2;
1326 struct ExtBack *eb = ebhead;
1327 while (i >= EXT_BLKSIZ) {
1328 if (eb)
1329 eb = eb->next;
1330 else
1331 break;
1332 i -= EXT_BLKSIZ;
1334 if (eb)
1335 method |= 0x20, fidx = eb->exts[i]->index;
1336 else
1337 nasm_panic(0,
1338 "unrecognised WRT value in obj_write_fixup");
1343 forp = obj_byte(forp, method);
1344 if (fidx != -1)
1345 forp = obj_index(forp, fidx);
1346 forp = obj_index(forp, tidx);
1347 obj_commit(forp);
1350 static int32_t obj_segment(char *name, int pass, int *bits)
1353 * We call the label manager here to define a name for the new
1354 * segment, and when our _own_ label-definition stub gets
1355 * called in return, it should register the new segment name
1356 * using the pointer it gets passed. That way we save memory,
1357 * by sponging off the label manager.
1359 #if defined(DEBUG) && DEBUG>=3
1360 nasm_error(ERR_DEBUG, " obj_segment: < %s >, pass=%d, *bits=%d\n",
1361 name, pass, *bits);
1362 #endif
1363 if (!name) {
1364 *bits = 16;
1365 current_seg = NULL;
1366 return first_seg;
1367 } else {
1368 struct Segment *seg;
1369 struct Group *grp;
1370 struct External **extp;
1371 int obj_idx, i, attrs;
1372 bool rn_error;
1373 char *p;
1376 * Look for segment attributes.
1378 attrs = 0;
1379 while (*name == '.')
1380 name++; /* hack, but a documented one */
1381 p = name;
1382 while (*p && !nasm_isspace(*p))
1383 p++;
1384 if (*p) {
1385 *p++ = '\0';
1386 while (*p && nasm_isspace(*p))
1387 *p++ = '\0';
1389 while (*p) {
1390 while (*p && !nasm_isspace(*p))
1391 p++;
1392 if (*p) {
1393 *p++ = '\0';
1394 while (*p && nasm_isspace(*p))
1395 *p++ = '\0';
1398 attrs++;
1401 obj_idx = 1;
1402 for (seg = seghead; seg; seg = seg->next) {
1403 obj_idx++;
1404 if (!strcmp(seg->name, name)) {
1405 if (attrs > 0 && pass == 1)
1406 nasm_error(ERR_WARNING, "segment attributes specified on"
1407 " redeclaration of segment: ignoring");
1408 if (seg->use32)
1409 *bits = 32;
1410 else
1411 *bits = 16;
1412 current_seg = seg;
1413 return seg->index;
1417 *segtail = seg = nasm_malloc(sizeof(*seg));
1418 seg->next = NULL;
1419 segtail = &seg->next;
1420 seg->index = (any_segs ? seg_alloc() : first_seg);
1421 seg->obj_index = obj_idx;
1422 seg->grp = NULL;
1423 any_segs = true;
1424 seg->name = NULL;
1425 seg->currentpos = 0;
1426 seg->align = 1; /* default */
1427 seg->use32 = false; /* default */
1428 seg->combine = CMB_PUBLIC; /* default */
1429 seg->segclass = seg->overlay = NULL;
1430 seg->pubhead = NULL;
1431 seg->pubtail = &seg->pubhead;
1432 seg->lochead = NULL;
1433 seg->loctail = &seg->lochead;
1434 seg->orp = obj_new();
1435 seg->orp->up = &(seg->orp);
1436 seg->orp->ori = ori_ledata;
1437 seg->orp->type = LEDATA;
1438 seg->orp->parm[1] = obj_idx;
1441 * Process the segment attributes.
1443 p = name;
1444 while (attrs--) {
1445 p += strlen(p);
1446 while (!*p)
1447 p++;
1450 * `p' contains a segment attribute.
1452 if (!nasm_stricmp(p, "private"))
1453 seg->combine = CMB_PRIVATE;
1454 else if (!nasm_stricmp(p, "public"))
1455 seg->combine = CMB_PUBLIC;
1456 else if (!nasm_stricmp(p, "common"))
1457 seg->combine = CMB_COMMON;
1458 else if (!nasm_stricmp(p, "stack"))
1459 seg->combine = CMB_STACK;
1460 else if (!nasm_stricmp(p, "use16"))
1461 seg->use32 = false;
1462 else if (!nasm_stricmp(p, "use32"))
1463 seg->use32 = true;
1464 else if (!nasm_stricmp(p, "flat")) {
1466 * This segment is an OS/2 FLAT segment. That means
1467 * that its default group is group FLAT, even if
1468 * the group FLAT does not explicitly _contain_ the
1469 * segment.
1471 * When we see this, we must create the group
1472 * `FLAT', containing no segments, if it does not
1473 * already exist; then we must set the default
1474 * group of this segment to be the FLAT group.
1476 struct Group *grp;
1477 for (grp = grphead; grp; grp = grp->next)
1478 if (!strcmp(grp->name, "FLAT"))
1479 break;
1480 if (!grp) {
1481 obj_directive(D_GROUP, "FLAT", 1);
1482 for (grp = grphead; grp; grp = grp->next)
1483 if (!strcmp(grp->name, "FLAT"))
1484 break;
1485 if (!grp)
1486 nasm_panic(0, "failure to define FLAT?!");
1488 seg->grp = grp;
1489 } else if (!nasm_strnicmp(p, "class=", 6))
1490 seg->segclass = nasm_strdup(p + 6);
1491 else if (!nasm_strnicmp(p, "overlay=", 8))
1492 seg->overlay = nasm_strdup(p + 8);
1493 else if (!nasm_strnicmp(p, "align=", 6)) {
1494 seg->align = readnum(p + 6, &rn_error);
1495 if (rn_error) {
1496 seg->align = 1;
1497 nasm_error(ERR_NONFATAL, "segment alignment should be"
1498 " numeric");
1500 switch ((int)seg->align) {
1501 case 1: /* BYTE */
1502 case 2: /* WORD */
1503 case 4: /* DWORD */
1504 case 16: /* PARA */
1505 case 256: /* PAGE */
1506 case 4096: /* PharLap extension */
1507 break;
1508 case 8:
1509 nasm_error(ERR_WARNING,
1510 "OBJ format does not support alignment"
1511 " of 8: rounding up to 16");
1512 seg->align = 16;
1513 break;
1514 case 32:
1515 case 64:
1516 case 128:
1517 nasm_error(ERR_WARNING,
1518 "OBJ format does not support alignment"
1519 " of %d: rounding up to 256", seg->align);
1520 seg->align = 256;
1521 break;
1522 case 512:
1523 case 1024:
1524 case 2048:
1525 nasm_error(ERR_WARNING,
1526 "OBJ format does not support alignment"
1527 " of %d: rounding up to 4096", seg->align);
1528 seg->align = 4096;
1529 break;
1530 default:
1531 nasm_error(ERR_NONFATAL, "invalid alignment value %d",
1532 seg->align);
1533 seg->align = 1;
1534 break;
1536 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
1537 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
1538 if (rn_error)
1539 nasm_error(ERR_NONFATAL, "argument to `absolute' segment"
1540 " attribute should be numeric");
1544 /* We need to know whenever we have at least one 32-bit segment */
1545 obj_use32 |= seg->use32;
1547 obj_seg_needs_update = seg;
1548 if (seg->align >= SEG_ABS)
1549 define_label(name, NO_SEG, seg->align - SEG_ABS,
1550 NULL, false, false);
1551 else
1552 define_label(name, seg->index + 1, 0L,
1553 NULL, false, false);
1554 obj_seg_needs_update = NULL;
1557 * See if this segment is defined in any groups.
1559 for (grp = grphead; grp; grp = grp->next) {
1560 for (i = grp->nindices; i < grp->nentries; i++) {
1561 if (!strcmp(grp->segs[i].name, seg->name)) {
1562 nasm_free(grp->segs[i].name);
1563 grp->segs[i] = grp->segs[grp->nindices];
1564 grp->segs[grp->nindices++].index = seg->obj_index;
1565 if (seg->grp)
1566 nasm_error(ERR_WARNING,
1567 "segment `%s' is already part of"
1568 " a group: first one takes precedence",
1569 seg->name);
1570 else
1571 seg->grp = grp;
1577 * Walk through the list of externals with unresolved
1578 * default-WRT clauses, and resolve any that point at this
1579 * segment.
1581 extp = &dws;
1582 while (*extp) {
1583 if ((*extp)->defwrt_type == DEFWRT_STRING &&
1584 !strcmp((*extp)->defwrt_ptr.string, seg->name)) {
1585 nasm_free((*extp)->defwrt_ptr.string);
1586 (*extp)->defwrt_type = DEFWRT_SEGMENT;
1587 (*extp)->defwrt_ptr.seg = seg;
1588 *extp = (*extp)->next_dws;
1589 } else
1590 extp = &(*extp)->next_dws;
1593 if (seg->use32)
1594 *bits = 32;
1595 else
1596 *bits = 16;
1597 current_seg = seg;
1598 return seg->index;
1602 static int obj_directive(enum directives directive, char *value, int pass)
1604 switch (directive) {
1605 case D_GROUP:
1607 char *p, *q, *v;
1608 if (pass == 1) {
1609 struct Group *grp;
1610 struct Segment *seg;
1611 struct External **extp;
1612 int obj_idx;
1614 q = value;
1615 while (*q == '.')
1616 q++; /* hack, but a documented one */
1617 v = q;
1618 while (*q && !nasm_isspace(*q))
1619 q++;
1620 if (nasm_isspace(*q)) {
1621 *q++ = '\0';
1622 while (*q && nasm_isspace(*q))
1623 q++;
1626 * Here we used to sanity-check the group directive to
1627 * ensure nobody tried to declare a group containing no
1628 * segments. However, OS/2 does this as standard
1629 * practice, so the sanity check has been removed.
1631 * if (!*q) {
1632 * nasm_error(ERR_NONFATAL,"GROUP directive contains no segments");
1633 * return 1;
1637 obj_idx = 1;
1638 for (grp = grphead; grp; grp = grp->next) {
1639 obj_idx++;
1640 if (!strcmp(grp->name, v)) {
1641 nasm_error(ERR_NONFATAL, "group `%s' defined twice", v);
1642 return 1;
1646 *grptail = grp = nasm_malloc(sizeof(*grp));
1647 grp->next = NULL;
1648 grptail = &grp->next;
1649 grp->index = seg_alloc();
1650 grp->obj_index = obj_idx;
1651 grp->nindices = grp->nentries = 0;
1652 grp->name = NULL;
1654 obj_grp_needs_update = grp;
1655 define_label(v, grp->index + 1, 0L, NULL, false, false);
1656 obj_grp_needs_update = NULL;
1658 while (*q) {
1659 p = q;
1660 while (*q && !nasm_isspace(*q))
1661 q++;
1662 if (nasm_isspace(*q)) {
1663 *q++ = '\0';
1664 while (*q && nasm_isspace(*q))
1665 q++;
1668 * Now p contains a segment name. Find it.
1670 for (seg = seghead; seg; seg = seg->next)
1671 if (!strcmp(seg->name, p))
1672 break;
1673 if (seg) {
1675 * We have a segment index. Shift a name entry
1676 * to the end of the array to make room.
1678 grp->segs[grp->nentries++] = grp->segs[grp->nindices];
1679 grp->segs[grp->nindices++].index = seg->obj_index;
1680 if (seg->grp)
1681 nasm_error(ERR_WARNING,
1682 "segment `%s' is already part of"
1683 " a group: first one takes precedence",
1684 seg->name);
1685 else
1686 seg->grp = grp;
1687 } else {
1689 * We have an as-yet undefined segment.
1690 * Remember its name, for later.
1692 grp->segs[grp->nentries++].name = nasm_strdup(p);
1697 * Walk through the list of externals with unresolved
1698 * default-WRT clauses, and resolve any that point at
1699 * this group.
1701 extp = &dws;
1702 while (*extp) {
1703 if ((*extp)->defwrt_type == DEFWRT_STRING &&
1704 !strcmp((*extp)->defwrt_ptr.string, grp->name)) {
1705 nasm_free((*extp)->defwrt_ptr.string);
1706 (*extp)->defwrt_type = DEFWRT_GROUP;
1707 (*extp)->defwrt_ptr.grp = grp;
1708 *extp = (*extp)->next_dws;
1709 } else
1710 extp = &(*extp)->next_dws;
1713 return 1;
1715 case D_UPPERCASE:
1716 obj_uppercase = true;
1717 return 1;
1719 case D_IMPORT:
1721 char *q, *extname, *libname, *impname;
1723 if (pass == 2)
1724 return 1; /* ignore in pass two */
1725 extname = q = value;
1726 while (*q && !nasm_isspace(*q))
1727 q++;
1728 if (nasm_isspace(*q)) {
1729 *q++ = '\0';
1730 while (*q && nasm_isspace(*q))
1731 q++;
1734 libname = q;
1735 while (*q && !nasm_isspace(*q))
1736 q++;
1737 if (nasm_isspace(*q)) {
1738 *q++ = '\0';
1739 while (*q && nasm_isspace(*q))
1740 q++;
1743 impname = q;
1745 if (!*extname || !*libname)
1746 nasm_error(ERR_NONFATAL, "`import' directive requires symbol name"
1747 " and library name");
1748 else {
1749 struct ImpDef *imp;
1750 bool err = false;
1752 imp = *imptail = nasm_malloc(sizeof(struct ImpDef));
1753 imptail = &imp->next;
1754 imp->next = NULL;
1755 imp->extname = nasm_strdup(extname);
1756 imp->libname = nasm_strdup(libname);
1757 imp->impindex = readnum(impname, &err);
1758 if (!*impname || err)
1759 imp->impname = nasm_strdup(impname);
1760 else
1761 imp->impname = NULL;
1764 return 1;
1766 case D_EXPORT:
1768 char *q, *extname, *intname, *v;
1769 struct ExpDef *export;
1770 int flags = 0;
1771 unsigned int ordinal = 0;
1773 if (pass == 2)
1774 return 1; /* ignore in pass two */
1775 intname = q = value;
1776 while (*q && !nasm_isspace(*q))
1777 q++;
1778 if (nasm_isspace(*q)) {
1779 *q++ = '\0';
1780 while (*q && nasm_isspace(*q))
1781 q++;
1784 extname = q;
1785 while (*q && !nasm_isspace(*q))
1786 q++;
1787 if (nasm_isspace(*q)) {
1788 *q++ = '\0';
1789 while (*q && nasm_isspace(*q))
1790 q++;
1793 if (!*intname) {
1794 nasm_error(ERR_NONFATAL, "`export' directive requires export name");
1795 return 1;
1797 if (!*extname) {
1798 extname = intname;
1799 intname = "";
1801 while (*q) {
1802 v = q;
1803 while (*q && !nasm_isspace(*q))
1804 q++;
1805 if (nasm_isspace(*q)) {
1806 *q++ = '\0';
1807 while (*q && nasm_isspace(*q))
1808 q++;
1810 if (!nasm_stricmp(v, "resident"))
1811 flags |= EXPDEF_FLAG_RESIDENT;
1812 else if (!nasm_stricmp(v, "nodata"))
1813 flags |= EXPDEF_FLAG_NODATA;
1814 else if (!nasm_strnicmp(v, "parm=", 5)) {
1815 bool err = false;
1816 flags |= EXPDEF_MASK_PARMCNT & readnum(v + 5, &err);
1817 if (err) {
1818 nasm_error(ERR_NONFATAL,
1819 "value `%s' for `parm' is non-numeric", v + 5);
1820 return 1;
1822 } else {
1823 bool err = false;
1824 ordinal = readnum(v, &err);
1825 if (err) {
1826 nasm_error(ERR_NONFATAL,
1827 "unrecognised export qualifier `%s'", v);
1828 return 1;
1830 flags |= EXPDEF_FLAG_ORDINAL;
1834 export = *exptail = nasm_malloc(sizeof(struct ExpDef));
1835 exptail = &export->next;
1836 export->next = NULL;
1837 export->extname = nasm_strdup(extname);
1838 export->intname = nasm_strdup(intname);
1839 export->ordinal = ordinal;
1840 export->flags = flags;
1842 return 1;
1844 default:
1845 return 0;
1849 static void obj_sectalign(int32_t seg, unsigned int value)
1851 struct Segment *s;
1853 list_for_each(s, seghead) {
1854 if (s->index == seg)
1855 break;
1859 * it should not be too big value
1860 * and applied on non-absolute sections
1862 if (!s || !is_power2(value) ||
1863 value > 4096 || s->align >= SEG_ABS)
1864 return;
1867 * FIXME: No code duplication please
1868 * consider making helper for this
1869 * mapping since section handler has
1870 * to do the same
1872 switch (value) {
1873 case 8:
1874 value = 16;
1875 break;
1876 case 32:
1877 case 64:
1878 case 128:
1879 value = 256;
1880 break;
1881 case 512:
1882 case 1024:
1883 case 2048:
1884 value = 4096;
1885 break;
1888 if (s->align < (int)value)
1889 s->align = value;
1892 static int32_t obj_segbase(int32_t segment)
1894 struct Segment *seg;
1897 * Find the segment in our list.
1899 for (seg = seghead; seg; seg = seg->next)
1900 if (seg->index == segment - 1)
1901 break;
1903 if (!seg) {
1905 * Might be an external with a default WRT.
1907 int32_t i = segment / 2;
1908 struct ExtBack *eb = ebhead;
1909 struct External *e;
1911 while (i >= EXT_BLKSIZ) {
1912 if (eb)
1913 eb = eb->next;
1914 else
1915 break;
1916 i -= EXT_BLKSIZ;
1918 if (eb) {
1919 e = eb->exts[i];
1920 if (!e) {
1921 nasm_assert(pass0 == 0);
1922 /* Not available - can happen during optimization */
1923 return NO_SEG;
1926 switch (e->defwrt_type) {
1927 case DEFWRT_NONE:
1928 return segment; /* fine */
1929 case DEFWRT_SEGMENT:
1930 return e->defwrt_ptr.seg->index + 1;
1931 case DEFWRT_GROUP:
1932 return e->defwrt_ptr.grp->index + 1;
1933 default:
1934 return NO_SEG; /* can't tell what it is */
1938 return segment; /* not one of ours - leave it alone */
1941 if (seg->align >= SEG_ABS)
1942 return seg->align; /* absolute segment */
1943 if (seg->grp)
1944 return seg->grp->index + 1; /* grouped segment */
1946 return segment; /* no special treatment */
1949 static void obj_filename(char *inname, char *outname)
1951 strcpy(obj_infile, inname);
1952 standard_extension(inname, outname, ".obj");
1955 static void obj_write_file(void)
1957 struct Segment *seg, *entry_seg_ptr = 0;
1958 struct FileName *fn;
1959 struct LineNumber *ln;
1960 struct Group *grp;
1961 struct Public *pub, *loc;
1962 struct External *ext;
1963 struct ImpDef *imp;
1964 struct ExpDef *export;
1965 int lname_idx;
1966 ObjRecord *orp;
1967 const bool debuginfo = (dfmt == &borland_debug_form);
1970 * Write the THEADR module header.
1972 orp = obj_new();
1973 orp->type = THEADR;
1974 obj_name(orp, obj_infile);
1975 obj_emit2(orp);
1978 * Write the NASM boast comment.
1980 orp->type = COMENT;
1981 obj_rword(orp, 0); /* comment type zero */
1982 obj_name(orp, nasm_comment);
1983 obj_emit2(orp);
1985 orp->type = COMENT;
1987 * Write the IMPDEF records, if any.
1989 for (imp = imphead; imp; imp = imp->next) {
1990 obj_rword(orp, 0xA0); /* comment class A0 */
1991 obj_byte(orp, 1); /* subfunction 1: IMPDEF */
1992 if (imp->impname)
1993 obj_byte(orp, 0); /* import by name */
1994 else
1995 obj_byte(orp, 1); /* import by ordinal */
1996 obj_name(orp, imp->extname);
1997 obj_name(orp, imp->libname);
1998 if (imp->impname)
1999 obj_name(orp, imp->impname);
2000 else
2001 obj_word(orp, imp->impindex);
2002 obj_emit2(orp);
2006 * Write the EXPDEF records, if any.
2008 for (export = exphead; export; export = export->next) {
2009 obj_rword(orp, 0xA0); /* comment class A0 */
2010 obj_byte(orp, 2); /* subfunction 2: EXPDEF */
2011 obj_byte(orp, export->flags);
2012 obj_name(orp, export->extname);
2013 obj_name(orp, export->intname);
2014 if (export->flags & EXPDEF_FLAG_ORDINAL)
2015 obj_word(orp, export->ordinal);
2016 obj_emit2(orp);
2019 /* we're using extended OMF if we put in debug info */
2020 if (debuginfo) {
2021 orp->type = COMENT;
2022 obj_byte(orp, 0x40);
2023 obj_byte(orp, dEXTENDED);
2024 obj_emit2(orp);
2028 * Write the first LNAMES record, containing LNAME one, which
2029 * is null. Also initialize the LNAME counter.
2031 orp->type = LNAMES;
2032 obj_byte(orp, 0);
2033 lname_idx = 1;
2035 * Write some LNAMES for the segment names
2037 for (seg = seghead; seg; seg = seg->next) {
2038 orp = obj_name(orp, seg->name);
2039 if (seg->segclass)
2040 orp = obj_name(orp, seg->segclass);
2041 if (seg->overlay)
2042 orp = obj_name(orp, seg->overlay);
2043 obj_commit(orp);
2046 * Write some LNAMES for the group names
2048 for (grp = grphead; grp; grp = grp->next) {
2049 orp = obj_name(orp, grp->name);
2050 obj_commit(orp);
2052 obj_emit(orp);
2055 * Write the SEGDEF records.
2057 orp->type = SEGDEF;
2058 for (seg = seghead; seg; seg = seg->next) {
2059 int acbp;
2060 uint32_t seglen = seg->currentpos;
2062 acbp = (seg->combine << 2); /* C field */
2064 if (seg->use32)
2065 acbp |= 0x01; /* P bit is Use32 flag */
2066 else if (seglen == 0x10000L) {
2067 seglen = 0; /* This special case may be needed for old linkers */
2068 acbp |= 0x02; /* B bit */
2071 /* A field */
2072 if (seg->align >= SEG_ABS)
2073 /* acbp |= 0x00 */ ;
2074 else if (seg->align >= 4096) {
2075 if (seg->align > 4096)
2076 nasm_error(ERR_NONFATAL, "segment `%s' requires more alignment"
2077 " than OBJ format supports", seg->name);
2078 acbp |= 0xC0; /* PharLap extension */
2079 } else if (seg->align >= 256) {
2080 acbp |= 0x80;
2081 } else if (seg->align >= 16) {
2082 acbp |= 0x60;
2083 } else if (seg->align >= 4) {
2084 acbp |= 0xA0;
2085 } else if (seg->align >= 2) {
2086 acbp |= 0x40;
2087 } else
2088 acbp |= 0x20;
2090 obj_byte(orp, acbp);
2091 if (seg->align & SEG_ABS) {
2092 obj_x(orp, seg->align - SEG_ABS); /* Frame */
2093 obj_byte(orp, 0); /* Offset */
2095 obj_x(orp, seglen);
2096 obj_index(orp, ++lname_idx);
2097 obj_index(orp, seg->segclass ? ++lname_idx : 1);
2098 obj_index(orp, seg->overlay ? ++lname_idx : 1);
2099 obj_emit2(orp);
2103 * Write the GRPDEF records.
2105 orp->type = GRPDEF;
2106 for (grp = grphead; grp; grp = grp->next) {
2107 int i;
2109 if (grp->nindices != grp->nentries) {
2110 for (i = grp->nindices; i < grp->nentries; i++) {
2111 nasm_error(ERR_NONFATAL, "group `%s' contains undefined segment"
2112 " `%s'", grp->name, grp->segs[i].name);
2113 nasm_free(grp->segs[i].name);
2114 grp->segs[i].name = NULL;
2117 obj_index(orp, ++lname_idx);
2118 for (i = 0; i < grp->nindices; i++) {
2119 obj_byte(orp, 0xFF);
2120 obj_index(orp, grp->segs[i].index);
2122 obj_emit2(orp);
2126 * Write the PUBDEF records: first the ones in the segments,
2127 * then the far-absolutes.
2129 orp->type = PUBDEF;
2130 orp->ori = ori_pubdef;
2131 for (seg = seghead; seg; seg = seg->next) {
2132 orp->parm[0] = seg->grp ? seg->grp->obj_index : 0;
2133 orp->parm[1] = seg->obj_index;
2134 for (pub = seg->pubhead; pub; pub = pub->next) {
2135 orp = obj_name(orp, pub->name);
2136 orp = obj_x(orp, pub->offset);
2137 orp = obj_byte(orp, 0); /* type index */
2138 obj_commit(orp);
2140 obj_emit(orp);
2142 orp->parm[0] = 0;
2143 orp->parm[1] = 0;
2144 for (pub = fpubhead; pub; pub = pub->next) { /* pub-crawl :-) */
2145 if (orp->parm[2] != (uint32_t)pub->segment) {
2146 obj_emit(orp);
2147 orp->parm[2] = pub->segment;
2149 orp = obj_name(orp, pub->name);
2150 orp = obj_x(orp, pub->offset);
2151 orp = obj_byte(orp, 0); /* type index */
2152 obj_commit(orp);
2154 obj_emit(orp);
2157 * Write the EXTDEF and COMDEF records, in order.
2159 orp->ori = ori_null;
2160 for (ext = exthead; ext; ext = ext->next) {
2161 if (ext->commonsize == 0) {
2162 if (orp->type != EXTDEF) {
2163 obj_emit(orp);
2164 orp->type = EXTDEF;
2166 orp = obj_name(orp, ext->name);
2167 orp = obj_index(orp, 0);
2168 } else {
2169 if (orp->type != COMDEF) {
2170 obj_emit(orp);
2171 orp->type = COMDEF;
2173 orp = obj_name(orp, ext->name);
2174 orp = obj_index(orp, 0);
2175 if (ext->commonelem) {
2176 orp = obj_byte(orp, 0x61); /* far communal */
2177 orp = obj_value(orp, (ext->commonsize / ext->commonelem));
2178 orp = obj_value(orp, ext->commonelem);
2179 } else {
2180 orp = obj_byte(orp, 0x62); /* near communal */
2181 orp = obj_value(orp, ext->commonsize);
2184 obj_commit(orp);
2186 obj_emit(orp);
2189 * Write a COMENT record stating that the linker's first pass
2190 * may stop processing at this point. Exception is if our
2191 * MODEND record specifies a start point, in which case,
2192 * according to some variants of the documentation, this COMENT
2193 * should be omitted. So we'll omit it just in case.
2194 * But, TASM puts it in all the time so if we are using
2195 * TASM debug stuff we are putting it in
2197 if (debuginfo || obj_entry_seg == NO_SEG) {
2198 orp->type = COMENT;
2199 obj_byte(orp, 0x40);
2200 obj_byte(orp, dLINKPASS);
2201 obj_byte(orp, 1);
2202 obj_emit2(orp);
2206 * 1) put out the compiler type
2207 * 2) Put out the type info. The only type we are using is near label #19
2209 if (debuginfo) {
2210 int i;
2211 struct Array *arrtmp = arrhead;
2212 orp->type = COMENT;
2213 obj_byte(orp, 0x40);
2214 obj_byte(orp, dCOMPDEF);
2215 obj_byte(orp, 4);
2216 obj_byte(orp, 0);
2217 obj_emit2(orp);
2219 obj_byte(orp, 0x40);
2220 obj_byte(orp, dTYPEDEF);
2221 obj_word(orp, 0x18); /* type # for linking */
2222 obj_word(orp, 6); /* size of type */
2223 obj_byte(orp, 0x2a); /* absolute type for debugging */
2224 obj_emit2(orp);
2225 obj_byte(orp, 0x40);
2226 obj_byte(orp, dTYPEDEF);
2227 obj_word(orp, 0x19); /* type # for linking */
2228 obj_word(orp, 0); /* size of type */
2229 obj_byte(orp, 0x24); /* absolute type for debugging */
2230 obj_byte(orp, 0); /* near/far specifier */
2231 obj_emit2(orp);
2232 obj_byte(orp, 0x40);
2233 obj_byte(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_byte(orp, 0x40);
2240 obj_byte(orp, dTYPEDEF);
2241 obj_word(orp, 0x1b); /* type # for linking */
2242 obj_word(orp, 0); /* size of type */
2243 obj_byte(orp, 0x23); /* absolute type for debugging */
2244 obj_byte(orp, 0);
2245 obj_byte(orp, 0);
2246 obj_byte(orp, 0);
2247 obj_emit2(orp);
2248 obj_byte(orp, 0x40);
2249 obj_byte(orp, dTYPEDEF);
2250 obj_word(orp, 0x1c); /* type # for linking */
2251 obj_word(orp, 0); /* size of type */
2252 obj_byte(orp, 0x23); /* absolute type for debugging */
2253 obj_byte(orp, 0);
2254 obj_byte(orp, 4);
2255 obj_byte(orp, 0);
2256 obj_emit2(orp);
2257 obj_byte(orp, 0x40);
2258 obj_byte(orp, dTYPEDEF);
2259 obj_word(orp, 0x1d); /* type # for linking */
2260 obj_word(orp, 0); /* size of type */
2261 obj_byte(orp, 0x23); /* absolute type for debugging */
2262 obj_byte(orp, 0);
2263 obj_byte(orp, 1);
2264 obj_byte(orp, 0);
2265 obj_emit2(orp);
2266 obj_byte(orp, 0x40);
2267 obj_byte(orp, dTYPEDEF);
2268 obj_word(orp, 0x1e); /* type # for linking */
2269 obj_word(orp, 0); /* size of type */
2270 obj_byte(orp, 0x23); /* absolute type for debugging */
2271 obj_byte(orp, 0);
2272 obj_byte(orp, 5);
2273 obj_byte(orp, 0);
2274 obj_emit2(orp);
2276 /* put out the array types */
2277 for (i = ARRAYBOT; i < arrindex; i++) {
2278 obj_byte(orp, 0x40);
2279 obj_byte(orp, dTYPEDEF);
2280 obj_word(orp, i); /* type # for linking */
2281 obj_word(orp, arrtmp->size); /* size of type */
2282 obj_byte(orp, 0x1A); /* absolute type for debugging (array) */
2283 obj_byte(orp, arrtmp->basetype); /* base type */
2284 obj_emit2(orp);
2285 arrtmp = arrtmp->next;
2289 * write out line number info with a LINNUM record
2290 * switch records when we switch segments, and output the
2291 * file in a pseudo-TASM fashion. The record switch is naive; that
2292 * is that one file may have many records for the same segment
2293 * if there are lots of segment switches
2295 if (fnhead && debuginfo) {
2296 seg = fnhead->lnhead->segment;
2298 for (fn = fnhead; fn; fn = fn->next) {
2299 /* write out current file name */
2300 orp->type = COMENT;
2301 orp->ori = ori_null;
2302 obj_byte(orp, 0x40);
2303 obj_byte(orp, dFILNAME);
2304 obj_byte(orp, 0);
2305 obj_name(orp, fn->name);
2306 obj_dword(orp, 0);
2307 obj_emit2(orp);
2309 /* write out line numbers this file */
2311 orp->type = LINNUM;
2312 orp->ori = ori_linnum;
2313 for (ln = fn->lnhead; ln; ln = ln->next) {
2314 if (seg != ln->segment) {
2315 /* if we get here have to flush the buffer and start
2316 * a new record for a new segment
2318 seg = ln->segment;
2319 obj_emit(orp);
2321 orp->parm[0] = seg->grp ? seg->grp->obj_index : 0;
2322 orp->parm[1] = seg->obj_index;
2323 orp = obj_word(orp, ln->lineno);
2324 orp = obj_x(orp, ln->offset);
2325 obj_commit(orp);
2327 obj_emit(orp);
2331 * we are going to locate the entry point segment now
2332 * rather than wait until the MODEND record, because,
2333 * then we can output a special symbol to tell where the
2334 * entry point is.
2337 if (obj_entry_seg != NO_SEG) {
2338 for (seg = seghead; seg; seg = seg->next) {
2339 if (seg->index == obj_entry_seg) {
2340 entry_seg_ptr = seg;
2341 break;
2344 if (!seg)
2345 nasm_error(ERR_NONFATAL, "entry point is not in this module");
2349 * get ready to put out symbol records
2351 orp->type = COMENT;
2352 orp->ori = ori_local;
2355 * put out a symbol for the entry point
2356 * no dots in this symbol, because, borland does
2357 * not (officially) support dots in label names
2358 * and I don't know what various versions of TLINK will do
2360 if (debuginfo && obj_entry_seg != NO_SEG) {
2361 orp = obj_name(orp, "start_of_program");
2362 orp = obj_word(orp, 0x19); /* type: near label */
2363 orp = obj_index(orp, seg->grp ? seg->grp->obj_index : 0);
2364 orp = obj_index(orp, seg->obj_index);
2365 orp = obj_x(orp, obj_entry_ofs);
2366 obj_commit(orp);
2370 * put out the local labels
2372 for (seg = seghead; seg && debuginfo; seg = seg->next) {
2373 /* labels this seg */
2374 for (loc = seg->lochead; loc; loc = loc->next) {
2375 orp = obj_name(orp, loc->name);
2376 orp = obj_word(orp, loc->type);
2377 orp = obj_index(orp, seg->grp ? seg->grp->obj_index : 0);
2378 orp = obj_index(orp, seg->obj_index);
2379 orp = obj_x(orp, loc->offset);
2380 obj_commit(orp);
2383 if (orp->used)
2384 obj_emit(orp);
2387 * Write the LEDATA/FIXUPP pairs.
2389 for (seg = seghead; seg; seg = seg->next) {
2390 obj_emit(seg->orp);
2391 nasm_free(seg->orp);
2395 * Write the MODEND module end marker.
2397 orp->type = obj_use32 ? MODE32 : MODEND;
2398 orp->ori = ori_null;
2399 if (entry_seg_ptr) {
2400 orp->type = entry_seg_ptr->use32 ? MODE32 : MODEND;
2401 obj_byte(orp, 0xC1);
2402 seg = entry_seg_ptr;
2403 if (seg->grp) {
2404 obj_byte(orp, 0x10);
2405 obj_index(orp, seg->grp->obj_index);
2406 } else {
2408 * the below changed to prevent TLINK crashing.
2409 * Previous more efficient version read:
2411 * obj_byte (orp, 0x50);
2413 obj_byte(orp, 0x00);
2414 obj_index(orp, seg->obj_index);
2416 obj_index(orp, seg->obj_index);
2417 obj_x(orp, obj_entry_ofs);
2418 } else
2419 obj_byte(orp, 0);
2420 obj_emit2(orp);
2421 nasm_free(orp);
2424 static void obj_fwrite(ObjRecord * orp)
2426 unsigned int cksum, len;
2427 uint8_t *ptr;
2429 cksum = orp->type;
2430 if (orp->x_size == 32)
2431 cksum |= 1;
2432 fputc(cksum, ofile);
2433 len = orp->committed + 1;
2434 cksum += (len & 0xFF) + ((len >> 8) & 0xFF);
2435 fwriteint16_t(len, ofile);
2436 nasm_write(orp->buf, len-1, ofile);
2437 for (ptr = orp->buf; --len; ptr++)
2438 cksum += *ptr;
2439 fputc((-cksum) & 0xFF, ofile);
2442 extern macros_t obj_stdmac[];
2444 static void dbgbi_init(void)
2446 fnhead = NULL;
2447 fntail = &fnhead;
2448 arrindex = ARRAYBOT;
2449 arrhead = NULL;
2450 arrtail = &arrhead;
2452 static void dbgbi_cleanup(void)
2454 struct Segment *segtmp;
2455 while (fnhead) {
2456 struct FileName *fntemp = fnhead;
2457 while (fnhead->lnhead) {
2458 struct LineNumber *lntemp = fnhead->lnhead;
2459 fnhead->lnhead = lntemp->next;
2460 nasm_free(lntemp);
2462 fnhead = fnhead->next;
2463 nasm_free(fntemp->name);
2464 nasm_free(fntemp);
2466 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
2467 while (segtmp->lochead) {
2468 struct Public *loctmp = segtmp->lochead;
2469 segtmp->lochead = loctmp->next;
2470 nasm_free(loctmp->name);
2471 nasm_free(loctmp);
2474 while (arrhead) {
2475 struct Array *arrtmp = arrhead;
2476 arrhead = arrhead->next;
2477 nasm_free(arrtmp);
2481 static void dbgbi_linnum(const char *lnfname, int32_t lineno, int32_t segto)
2483 struct FileName *fn;
2484 struct LineNumber *ln;
2485 struct Segment *seg;
2487 if (segto == NO_SEG)
2488 return;
2491 * If `any_segs' is still false, we must define a default
2492 * segment.
2494 if (!any_segs) {
2495 int tempint; /* ignored */
2496 if (segto != obj_segment("__NASMDEFSEG", 2, &tempint))
2497 nasm_panic(0, "strange segment conditions in OBJ driver");
2501 * Find the segment we are targetting.
2503 for (seg = seghead; seg; seg = seg->next)
2504 if (seg->index == segto)
2505 break;
2506 if (!seg)
2507 nasm_panic(0, "lineno directed to nonexistent segment?");
2509 /* for (fn = fnhead; fn; fn = fnhead->next) */
2510 for (fn = fnhead; fn; fn = fn->next) /* fbk - Austin Lunnen - John Fine */
2511 if (!nasm_stricmp(lnfname, fn->name))
2512 break;
2513 if (!fn) {
2514 fn = nasm_malloc(sizeof(*fn));
2515 fn->name = nasm_malloc(strlen(lnfname) + 1);
2516 strcpy(fn->name, lnfname);
2517 fn->lnhead = NULL;
2518 fn->lntail = &fn->lnhead;
2519 fn->next = NULL;
2520 *fntail = fn;
2521 fntail = &fn->next;
2523 ln = nasm_malloc(sizeof(*ln));
2524 ln->segment = seg;
2525 ln->offset = seg->currentpos;
2526 ln->lineno = lineno;
2527 ln->next = NULL;
2528 *fn->lntail = ln;
2529 fn->lntail = &ln->next;
2532 static void dbgbi_deflabel(char *name, int32_t segment,
2533 int64_t offset, int is_global, char *special)
2535 struct Segment *seg;
2537 (void)special;
2540 * Note: ..[^@] special symbols are filtered in labels.c
2544 * If it's a special-retry from pass two, discard it.
2546 if (is_global == 3)
2547 return;
2550 * Case (i):
2552 if (obj_seg_needs_update) {
2553 return;
2554 } else if (obj_grp_needs_update) {
2555 return;
2557 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
2558 return;
2560 if (segment >= SEG_ABS || segment == NO_SEG) {
2561 return;
2565 * If `any_segs' is still false, we might need to define a
2566 * default segment, if they're trying to declare a label in
2567 * `first_seg'. But the label should exist due to a prior
2568 * call to obj_deflabel so we can skip that.
2571 for (seg = seghead; seg; seg = seg->next)
2572 if (seg->index == segment) {
2573 struct Public *loc = nasm_malloc(sizeof(*loc));
2575 * Case (ii). Maybe MODPUB someday?
2577 last_defined = *seg->loctail = loc;
2578 seg->loctail = &loc->next;
2579 loc->next = NULL;
2580 loc->name = nasm_strdup(name);
2581 loc->offset = offset;
2584 static void dbgbi_typevalue(int32_t type)
2586 int vsize;
2587 int elem = TYM_ELEMENTS(type);
2588 type = TYM_TYPE(type);
2590 if (!last_defined)
2591 return;
2593 switch (type) {
2594 case TY_BYTE:
2595 last_defined->type = 8; /* uint8_t */
2596 vsize = 1;
2597 break;
2598 case TY_WORD:
2599 last_defined->type = 10; /* unsigned word */
2600 vsize = 2;
2601 break;
2602 case TY_DWORD:
2603 last_defined->type = 12; /* unsigned dword */
2604 vsize = 4;
2605 break;
2606 case TY_FLOAT:
2607 last_defined->type = 14; /* float */
2608 vsize = 4;
2609 break;
2610 case TY_QWORD:
2611 last_defined->type = 15; /* qword */
2612 vsize = 8;
2613 break;
2614 case TY_TBYTE:
2615 last_defined->type = 16; /* TBYTE */
2616 vsize = 10;
2617 break;
2618 default:
2619 last_defined->type = 0x19; /*label */
2620 vsize = 0;
2621 break;
2624 if (elem > 1) {
2625 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
2626 int vtype = last_defined->type;
2627 arrtmp->size = vsize * elem;
2628 arrtmp->basetype = vtype;
2629 arrtmp->next = NULL;
2630 last_defined->type = arrindex++;
2631 *arrtail = arrtmp;
2632 arrtail = &(arrtmp->next);
2634 last_defined = NULL;
2636 static void dbgbi_output(int output_type, void *param)
2638 (void)output_type;
2639 (void)param;
2641 static struct dfmt borland_debug_form = {
2642 "Borland Debug Records",
2643 "borland",
2644 dbgbi_init,
2645 dbgbi_linnum,
2646 dbgbi_deflabel,
2647 null_debug_directive,
2648 dbgbi_typevalue,
2649 dbgbi_output,
2650 dbgbi_cleanup,
2653 static struct dfmt *borland_debug_arr[3] = {
2654 &borland_debug_form,
2655 &null_debug_form,
2656 NULL
2659 struct ofmt of_obj = {
2660 "MS-DOS 16-bit/32-bit OMF object files",
2661 "obj",
2664 borland_debug_arr,
2665 &borland_debug_form,
2666 obj_stdmac,
2667 obj_init,
2668 obj_set_info,
2669 obj_out,
2670 obj_deflabel,
2671 obj_segment,
2672 obj_sectalign,
2673 obj_segbase,
2674 obj_directive,
2675 obj_filename,
2676 obj_cleanup
2678 #endif /* OF_OBJ */