Add copyright header to nasmdoc.src
[nasm/sigaren-mirror.git] / output / outobj.c
blobef4d06319579a358a59d4b625b933ba6f91ebe7e
1 /* outobj.c output routines for the Netwide Assembler to produce
2 * .OBJ object files
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "nasmlib.h"
20 #include "stdscan.h"
21 #include "output/outform.h"
22 #include "output/outlib.h"
24 #ifdef OF_OBJ
27 * outobj.c is divided into two sections. The first section is low level
28 * routines for creating obj records; It has nearly zero NASM specific
29 * code. The second section is high level routines for processing calls and
30 * data structures from the rest of NASM into obj format.
32 * It should be easy (though not zero work) to lift the first section out for
33 * use as an obj file writer for some other assembler or compiler.
37 * These routines are built around the ObjRecord data struture. An ObjRecord
38 * holds an object file record that may be under construction or complete.
40 * A major function of these routines is to support continuation of an obj
41 * record into the next record when the maximum record size is exceeded. The
42 * high level code does not need to worry about where the record breaks occur.
43 * It does need to do some minor extra steps to make the automatic continuation
44 * work. Those steps may be skipped for records where the high level knows no
45 * continuation could be required.
47 * 1) An ObjRecord is allocated and cleared by obj_new, or an existing ObjRecord
48 * is cleared by obj_clear.
50 * 2) The caller should fill in .type.
52 * 3) If the record is continuable and there is processing that must be done at
53 * the start of each record then the caller should fill in .ori with the
54 * address of the record initializer routine.
56 * 4) If the record is continuable and it should be saved (rather than emitted
57 * immediately) as each record is done, the caller should set .up to be a
58 * pointer to a location in which the caller keeps the master pointer to the
59 * ObjRecord. When the record is continued, the obj_bump routine will then
60 * allocate a new ObjRecord structure and update the master pointer.
62 * 5) If the .ori field was used then the caller should fill in the .parm with
63 * any data required by the initializer.
65 * 6) The caller uses the routines: obj_byte, obj_word, obj_rword, obj_dword,
66 * obj_x, obj_index, obj_value and obj_name to fill in the various kinds of
67 * data required for this record.
69 * 7) If the record is continuable, the caller should call obj_commit at each
70 * point where breaking the record is permitted.
72 * 8) To write out the record, the caller should call obj_emit2. If the
73 * caller has called obj_commit for all data written then he can get slightly
74 * faster code by calling obj_emit instead of obj_emit2.
76 * Most of these routines return an ObjRecord pointer. This will be the input
77 * pointer most of the time and will be the new location if the ObjRecord
78 * moved as a result of the call. The caller may ignore the return value in
79 * three cases: It is a "Never Reallocates" routine; or The caller knows
80 * continuation is not possible; or The caller uses the master pointer for the
81 * next operation.
84 #define RECORD_MAX (1024-3) /* maximal size of any record except type+reclen */
85 #define OBJ_PARMS 3 /* maximum .parm used by any .ori routine */
87 #define FIX_08_LOW 0x8000 /* location type for various fixup subrecords */
88 #define FIX_16_OFFSET 0x8400
89 #define FIX_16_SELECTOR 0x8800
90 #define FIX_32_POINTER 0x8C00
91 #define FIX_08_HIGH 0x9000
92 #define FIX_32_OFFSET 0xA400
93 #define FIX_48_POINTER 0xAC00
95 enum RecordID { /* record ID codes */
97 THEADR = 0x80, /* module header */
98 COMENT = 0x88, /* comment record */
100 LINNUM = 0x94, /* line number record */
101 LNAMES = 0x96, /* list of names */
103 SEGDEF = 0x98, /* segment definition */
104 GRPDEF = 0x9A, /* group definition */
105 EXTDEF = 0x8C, /* external definition */
106 PUBDEF = 0x90, /* public definition */
107 COMDEF = 0xB0, /* common definition */
109 LEDATA = 0xA0, /* logical enumerated data */
110 FIXUPP = 0x9C, /* fixups (relocations) */
111 FIXU32 = 0x9D, /* 32-bit fixups (relocations) */
113 MODEND = 0x8A, /* module end */
114 MODE32 = 0x8B /* module end for 32-bit objects */
117 enum ComentID { /* ID codes for comment records */
119 dEXTENDED = 0xA1, /* tells that we are using translator-specific extensions */
120 dLINKPASS = 0xA2, /* link pass 2 marker */
121 dTYPEDEF = 0xE3, /* define a type */
122 dSYM = 0xE6, /* symbol debug record */
123 dFILNAME = 0xE8, /* file name record */
124 dCOMPDEF = 0xEA /* compiler type info */
127 typedef struct ObjRecord ObjRecord;
128 typedef void ORI(ObjRecord * orp);
130 struct ObjRecord {
131 ORI *ori; /* Initialization routine */
132 int used; /* Current data size */
133 int committed; /* Data size at last boundary */
134 int x_size; /* (see obj_x) */
135 unsigned int type; /* Record type */
136 ObjRecord *child; /* Associated record below this one */
137 ObjRecord **up; /* Master pointer to this ObjRecord */
138 ObjRecord *back; /* Previous part of this record */
139 uint32_t parm[OBJ_PARMS]; /* Parameters for ori routine */
140 uint8_t buf[RECORD_MAX + 3];
143 static void obj_fwrite(ObjRecord * orp);
144 static void ori_ledata(ObjRecord * orp);
145 static void ori_pubdef(ObjRecord * orp);
146 static void ori_null(ObjRecord * orp);
147 static ObjRecord *obj_commit(ObjRecord * orp);
149 static bool obj_uppercase; /* Flag: all names in uppercase */
150 static bool obj_use32; /* Flag: at least one segment is 32-bit */
153 * Clear an ObjRecord structure. (Never reallocates).
154 * To simplify reuse of ObjRecord's, .type, .ori and .parm are not cleared.
156 static ObjRecord *obj_clear(ObjRecord * orp)
158 orp->used = 0;
159 orp->committed = 0;
160 orp->x_size = 0;
161 orp->child = NULL;
162 orp->up = NULL;
163 orp->back = NULL;
164 return (orp);
168 * Emit an ObjRecord structure. (Never reallocates).
169 * The record is written out preceeded (recursively) by its previous part (if
170 * any) and followed (recursively) by its child (if any).
171 * The previous part and the child are freed. The main ObjRecord is cleared,
172 * not freed.
174 static ObjRecord *obj_emit(ObjRecord * orp)
176 if (orp->back) {
177 obj_emit(orp->back);
178 nasm_free(orp->back);
181 if (orp->committed)
182 obj_fwrite(orp);
184 if (orp->child) {
185 obj_emit(orp->child);
186 nasm_free(orp->child);
189 return (obj_clear(orp));
193 * Commit and Emit a record. (Never reallocates).
195 static ObjRecord *obj_emit2(ObjRecord * orp)
197 obj_commit(orp);
198 return (obj_emit(orp));
202 * Allocate and clear a new ObjRecord; Also sets .ori to ori_null
204 static ObjRecord *obj_new(void)
206 ObjRecord *orp;
208 orp = obj_clear(nasm_malloc(sizeof(ObjRecord)));
209 orp->ori = ori_null;
210 return (orp);
214 * Advance to the next record because the existing one is full or its x_size
215 * is incompatible.
216 * Any uncommited data is moved into the next record.
218 static ObjRecord *obj_bump(ObjRecord * orp)
220 ObjRecord *nxt;
221 int used = orp->used;
222 int committed = orp->committed;
224 if (orp->up) {
225 *orp->up = nxt = obj_new();
226 nxt->ori = orp->ori;
227 nxt->type = orp->type;
228 nxt->up = orp->up;
229 nxt->back = orp;
230 memcpy(nxt->parm, orp->parm, sizeof(orp->parm));
231 } else
232 nxt = obj_emit(orp);
234 used -= committed;
235 if (used) {
236 nxt->committed = 1;
237 nxt->ori(nxt);
238 nxt->committed = nxt->used;
239 memcpy(nxt->buf + nxt->committed, orp->buf + committed, used);
240 nxt->used = nxt->committed + used;
243 return (nxt);
247 * Advance to the next record if necessary to allow the next field to fit.
249 static ObjRecord *obj_check(ObjRecord * orp, int size)
251 if (orp->used + size > RECORD_MAX)
252 orp = obj_bump(orp);
254 if (!orp->committed) {
255 orp->committed = 1;
256 orp->ori(orp);
257 orp->committed = orp->used;
260 return (orp);
264 * All data written so far is commited to the current record (won't be moved to
265 * the next record in case of continuation).
267 static ObjRecord *obj_commit(ObjRecord * orp)
269 orp->committed = orp->used;
270 return (orp);
274 * Write a byte
276 static ObjRecord *obj_byte(ObjRecord * orp, uint8_t val)
278 orp = obj_check(orp, 1);
279 orp->buf[orp->used] = val;
280 orp->used++;
281 return (orp);
285 * Write a word
287 static ObjRecord *obj_word(ObjRecord * orp, unsigned int val)
289 orp = obj_check(orp, 2);
290 orp->buf[orp->used] = val;
291 orp->buf[orp->used + 1] = val >> 8;
292 orp->used += 2;
293 return (orp);
297 * Write a reversed word
299 static ObjRecord *obj_rword(ObjRecord * orp, unsigned int val)
301 orp = obj_check(orp, 2);
302 orp->buf[orp->used] = val >> 8;
303 orp->buf[orp->used + 1] = val;
304 orp->used += 2;
305 return (orp);
309 * Write a dword
311 static ObjRecord *obj_dword(ObjRecord * orp, uint32_t val)
313 orp = obj_check(orp, 4);
314 orp->buf[orp->used] = val;
315 orp->buf[orp->used + 1] = val >> 8;
316 orp->buf[orp->used + 2] = val >> 16;
317 orp->buf[orp->used + 3] = val >> 24;
318 orp->used += 4;
319 return (orp);
323 * All fields of "size x" in one obj record must be the same size (either 16
324 * bits or 32 bits). There is a one bit flag in each record which specifies
325 * which.
326 * This routine is used to force the current record to have the desired
327 * x_size. x_size is normally automatic (using obj_x), so that this
328 * routine should be used outside obj_x, only to provide compatibility with
329 * linkers that have bugs in their processing of the size bit.
332 static ObjRecord *obj_force(ObjRecord * orp, int x)
334 if (orp->x_size == (x ^ 48))
335 orp = obj_bump(orp);
336 orp->x_size = x;
337 return (orp);
341 * This routine writes a field of size x. The caller does not need to worry at
342 * all about whether 16-bits or 32-bits are required.
344 static ObjRecord *obj_x(ObjRecord * orp, uint32_t val)
346 if (orp->type & 1)
347 orp->x_size = 32;
348 if (val > 0xFFFF)
349 orp = obj_force(orp, 32);
350 if (orp->x_size == 32) {
351 ObjRecord *nxt = obj_dword(orp, val);
352 nxt->x_size = 32; /* x_size is cleared when a record overflows */
353 return nxt;
355 orp->x_size = 16;
356 return (obj_word(orp, val));
360 * Writes an index
362 static ObjRecord *obj_index(ObjRecord * orp, unsigned int val)
364 if (val < 128)
365 return (obj_byte(orp, val));
366 return (obj_word(orp, (val >> 8) | (val << 8) | 0x80));
370 * Writes a variable length value
372 static ObjRecord *obj_value(ObjRecord * orp, uint32_t val)
374 if (val <= 128)
375 return (obj_byte(orp, val));
376 if (val <= 0xFFFF) {
377 orp = obj_byte(orp, 129);
378 return (obj_word(orp, val));
380 if (val <= 0xFFFFFF)
381 return (obj_dword(orp, (val << 8) + 132));
382 orp = obj_byte(orp, 136);
383 return (obj_dword(orp, val));
387 * Writes a counted string
389 static ObjRecord *obj_name(ObjRecord * orp, const char *name)
391 int len = strlen(name);
392 uint8_t *ptr;
394 orp = obj_check(orp, len + 1);
395 ptr = orp->buf + orp->used;
396 *ptr++ = len;
397 orp->used += len + 1;
398 if (obj_uppercase)
399 while (--len >= 0) {
400 *ptr++ = toupper(*name);
401 name++;
402 } else
403 memcpy(ptr, name, len);
404 return (orp);
408 * Initializer for an LEDATA record.
409 * parm[0] = offset
410 * parm[1] = segment index
411 * During the use of a LEDATA ObjRecord, parm[0] is constantly updated to
412 * represent the offset that would be required if the record were split at the
413 * last commit point.
414 * parm[2] is a copy of parm[0] as it was when the current record was initted.
416 static void ori_ledata(ObjRecord * orp)
418 obj_index(orp, orp->parm[1]);
419 orp->parm[2] = orp->parm[0];
420 obj_x(orp, orp->parm[0]);
424 * Initializer for a PUBDEF record.
425 * parm[0] = group index
426 * parm[1] = segment index
427 * parm[2] = frame (only used when both indexes are zero)
429 static void ori_pubdef(ObjRecord * orp)
431 obj_index(orp, orp->parm[0]);
432 obj_index(orp, orp->parm[1]);
433 if (!(orp->parm[0] | orp->parm[1]))
434 obj_word(orp, orp->parm[2]);
438 * Initializer for a LINNUM record.
439 * parm[0] = group index
440 * parm[1] = segment index
442 static void ori_linnum(ObjRecord * orp)
444 obj_index(orp, orp->parm[0]);
445 obj_index(orp, orp->parm[1]);
449 * Initializer for a local vars record.
451 static void ori_local(ObjRecord * orp)
453 obj_byte(orp, 0x40);
454 obj_byte(orp, dSYM);
458 * Null initializer for records that continue without any header info
460 static void ori_null(ObjRecord * orp)
462 (void)orp; /* Do nothing */
466 * This concludes the low level section of outobj.c
469 static char obj_infile[FILENAME_MAX];
471 static efunc error;
472 static evalfunc evaluate;
473 static ldfunc deflabel;
474 static FILE *ofp;
475 static int32_t first_seg;
476 static bool any_segs;
477 static int passtwo;
478 static int arrindex;
480 #define GROUP_MAX 256 /* we won't _realistically_ have more
481 * than this many segs in a group */
482 #define EXT_BLKSIZ 256 /* block size for externals list */
484 struct Segment; /* need to know these structs exist */
485 struct Group;
487 struct LineNumber {
488 struct LineNumber *next;
489 struct Segment *segment;
490 int32_t offset;
491 int32_t lineno;
494 static struct FileName {
495 struct FileName *next;
496 char *name;
497 struct LineNumber *lnhead, **lntail;
498 int index;
499 } *fnhead, **fntail;
501 static struct Array {
502 struct Array *next;
503 unsigned size;
504 int basetype;
505 } *arrhead, **arrtail;
507 #define ARRAYBOT 31 /* magic number for first array index */
509 static struct Public {
510 struct Public *next;
511 char *name;
512 int32_t offset;
513 int32_t segment; /* only if it's far-absolute */
514 int type; /* only for local debug syms */
515 } *fpubhead, **fpubtail, *last_defined;
517 static struct External {
518 struct External *next;
519 char *name;
520 int32_t commonsize;
521 int32_t commonelem; /* element size if FAR, else zero */
522 int index; /* OBJ-file external index */
523 enum {
524 DEFWRT_NONE, /* no unusual default-WRT */
525 DEFWRT_STRING, /* a string we don't yet understand */
526 DEFWRT_SEGMENT, /* a segment */
527 DEFWRT_GROUP /* a group */
528 } defwrt_type;
529 union {
530 char *string;
531 struct Segment *seg;
532 struct Group *grp;
533 } defwrt_ptr;
534 struct External *next_dws; /* next with DEFWRT_STRING */
535 } *exthead, **exttail, *dws;
537 static int externals;
539 static struct ExtBack {
540 struct ExtBack *next;
541 struct External *exts[EXT_BLKSIZ];
542 } *ebhead, **ebtail;
544 static struct Segment {
545 struct Segment *next;
546 int32_t index; /* the NASM segment id */
547 int32_t obj_index; /* the OBJ-file segment index */
548 struct Group *grp; /* the group it beint32_ts to */
549 uint32_t currentpos;
550 int32_t align; /* can be SEG_ABS + absolute addr */
551 enum {
552 CMB_PRIVATE = 0,
553 CMB_PUBLIC = 2,
554 CMB_STACK = 5,
555 CMB_COMMON = 6
556 } combine;
557 bool use32; /* is this segment 32-bit? */
558 struct Public *pubhead, **pubtail, *lochead, **loctail;
559 char *name;
560 char *segclass, *overlay; /* `class' is a C++ keyword :-) */
561 ObjRecord *orp;
562 } *seghead, **segtail, *obj_seg_needs_update;
564 static struct Group {
565 struct Group *next;
566 char *name;
567 int32_t index; /* NASM segment id */
568 int32_t obj_index; /* OBJ-file group index */
569 int32_t nentries; /* number of elements... */
570 int32_t nindices; /* ...and number of index elts... */
571 union {
572 int32_t index;
573 char *name;
574 } segs[GROUP_MAX]; /* ...in this */
575 } *grphead, **grptail, *obj_grp_needs_update;
577 static struct ImpDef {
578 struct ImpDef *next;
579 char *extname;
580 char *libname;
581 unsigned int impindex;
582 char *impname;
583 } *imphead, **imptail;
585 static struct ExpDef {
586 struct ExpDef *next;
587 char *intname;
588 char *extname;
589 unsigned int ordinal;
590 int flags;
591 } *exphead, **exptail;
593 #define EXPDEF_FLAG_ORDINAL 0x80
594 #define EXPDEF_FLAG_RESIDENT 0x40
595 #define EXPDEF_FLAG_NODATA 0x20
596 #define EXPDEF_MASK_PARMCNT 0x1F
598 static int32_t obj_entry_seg, obj_entry_ofs;
600 struct ofmt of_obj;
602 /* The current segment */
603 static struct Segment *current_seg;
605 static int32_t obj_segment(char *, int, int *);
606 static void obj_write_file(int debuginfo);
607 static int obj_directive(char *, char *, int);
609 static void obj_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
611 ofp = fp;
612 error = errfunc;
613 evaluate = eval;
614 deflabel = ldef;
615 first_seg = seg_alloc();
616 any_segs = false;
617 fpubhead = NULL;
618 fpubtail = &fpubhead;
619 exthead = NULL;
620 exttail = &exthead;
621 imphead = NULL;
622 imptail = &imphead;
623 exphead = NULL;
624 exptail = &exphead;
625 dws = NULL;
626 externals = 0;
627 ebhead = NULL;
628 ebtail = &ebhead;
629 seghead = obj_seg_needs_update = NULL;
630 segtail = &seghead;
631 grphead = obj_grp_needs_update = NULL;
632 grptail = &grphead;
633 obj_entry_seg = NO_SEG;
634 obj_uppercase = false;
635 obj_use32 = false;
636 passtwo = 0;
637 current_seg = NULL;
640 static int obj_set_info(enum geninfo type, char **val)
642 (void)type;
643 (void)val;
645 return 0;
647 static void obj_cleanup(int debuginfo)
649 obj_write_file(debuginfo);
650 of_obj.current_dfmt->cleanup();
651 fclose(ofp);
652 while (seghead) {
653 struct Segment *segtmp = seghead;
654 seghead = seghead->next;
655 while (segtmp->pubhead) {
656 struct Public *pubtmp = segtmp->pubhead;
657 segtmp->pubhead = pubtmp->next;
658 nasm_free(pubtmp->name);
659 nasm_free(pubtmp);
661 nasm_free(segtmp->segclass);
662 nasm_free(segtmp->overlay);
663 nasm_free(segtmp);
665 while (fpubhead) {
666 struct Public *pubtmp = fpubhead;
667 fpubhead = fpubhead->next;
668 nasm_free(pubtmp->name);
669 nasm_free(pubtmp);
671 while (exthead) {
672 struct External *exttmp = exthead;
673 exthead = exthead->next;
674 nasm_free(exttmp);
676 while (imphead) {
677 struct ImpDef *imptmp = imphead;
678 imphead = imphead->next;
679 nasm_free(imptmp->extname);
680 nasm_free(imptmp->libname);
681 nasm_free(imptmp->impname); /* nasm_free won't mind if it's NULL */
682 nasm_free(imptmp);
684 while (exphead) {
685 struct ExpDef *exptmp = exphead;
686 exphead = exphead->next;
687 nasm_free(exptmp->extname);
688 nasm_free(exptmp->intname);
689 nasm_free(exptmp);
691 while (ebhead) {
692 struct ExtBack *ebtmp = ebhead;
693 ebhead = ebhead->next;
694 nasm_free(ebtmp);
696 while (grphead) {
697 struct Group *grptmp = grphead;
698 grphead = grphead->next;
699 nasm_free(grptmp);
703 static void obj_ext_set_defwrt(struct External *ext, char *id)
705 struct Segment *seg;
706 struct Group *grp;
708 for (seg = seghead; seg; seg = seg->next)
709 if (!strcmp(seg->name, id)) {
710 ext->defwrt_type = DEFWRT_SEGMENT;
711 ext->defwrt_ptr.seg = seg;
712 nasm_free(id);
713 return;
716 for (grp = grphead; grp; grp = grp->next)
717 if (!strcmp(grp->name, id)) {
718 ext->defwrt_type = DEFWRT_GROUP;
719 ext->defwrt_ptr.grp = grp;
720 nasm_free(id);
721 return;
724 ext->defwrt_type = DEFWRT_STRING;
725 ext->defwrt_ptr.string = id;
726 ext->next_dws = dws;
727 dws = ext;
730 static void obj_deflabel(char *name, int32_t segment,
731 int64_t offset, int is_global, char *special)
734 * We have three cases:
736 * (i) `segment' is a segment-base. If so, set the name field
737 * for the segment or group structure it refers to, and then
738 * return.
740 * (ii) `segment' is one of our segments, or a SEG_ABS segment.
741 * Save the label position for later output of a PUBDEF record.
742 * (Or a MODPUB, if we work out how.)
744 * (iii) `segment' is not one of our segments. Save the label
745 * position for later output of an EXTDEF, and also store a
746 * back-reference so that we can map later references to this
747 * segment number to the external index.
749 struct External *ext;
750 struct ExtBack *eb;
751 struct Segment *seg;
752 int i;
753 bool used_special = false; /* have we used the special text? */
755 #if defined(DEBUG) && DEBUG>2
756 fprintf(stderr,
757 " obj_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
758 name, segment, offset, is_global, special);
759 #endif
762 * If it's a special-retry from pass two, discard it.
764 if (is_global == 3)
765 return;
768 * First check for the double-period, signifying something
769 * unusual.
771 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
772 if (!strcmp(name, "..start")) {
773 obj_entry_seg = segment;
774 obj_entry_ofs = offset;
775 return;
777 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
781 * Case (i):
783 if (obj_seg_needs_update) {
784 obj_seg_needs_update->name = name;
785 return;
786 } else if (obj_grp_needs_update) {
787 obj_grp_needs_update->name = name;
788 return;
790 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
791 return;
793 if (segment >= SEG_ABS || segment == NO_SEG) {
795 * SEG_ABS subcase of (ii).
797 if (is_global) {
798 struct Public *pub;
800 pub = *fpubtail = nasm_malloc(sizeof(*pub));
801 fpubtail = &pub->next;
802 pub->next = NULL;
803 pub->name = nasm_strdup(name);
804 pub->offset = offset;
805 pub->segment = (segment == NO_SEG ? 0 : segment & ~SEG_ABS);
807 if (special)
808 error(ERR_NONFATAL, "OBJ supports no special symbol features"
809 " for this symbol type");
810 return;
814 * If `any_segs' is still false, we might need to define a
815 * default segment, if they're trying to declare a label in
816 * `first_seg'.
818 if (!any_segs && segment == first_seg) {
819 int tempint; /* ignored */
820 if (segment != obj_segment("__NASMDEFSEG", 2, &tempint))
821 error(ERR_PANIC, "strange segment conditions in OBJ driver");
824 for (seg = seghead; seg && is_global; seg = seg->next)
825 if (seg->index == segment) {
826 struct Public *loc = nasm_malloc(sizeof(*loc));
828 * Case (ii). Maybe MODPUB someday?
830 *seg->pubtail = loc;
831 seg->pubtail = &loc->next;
832 loc->next = NULL;
833 loc->name = nasm_strdup(name);
834 loc->offset = offset;
836 if (special)
837 error(ERR_NONFATAL,
838 "OBJ supports no special symbol features"
839 " for this symbol type");
840 return;
844 * Case (iii).
846 if (is_global) {
847 ext = *exttail = nasm_malloc(sizeof(*ext));
848 ext->next = NULL;
849 exttail = &ext->next;
850 ext->name = name;
851 /* Place by default all externs into the current segment */
852 ext->defwrt_type = DEFWRT_NONE;
854 /* 28-Apr-2002 - John Coffman
855 The following code was introduced on 12-Aug-2000, and breaks fixups
856 on code passed thru the MSC 5.1 linker (3.66) and MSC 6.00A linker
857 (5.10). It was introduced after FIXUP32 was added, and may be needed
858 for 32-bit segments. The following will get 16-bit segments working
859 again, and maybe someone can correct the 'if' condition which is
860 actually needed.
862 #if 0
863 if (current_seg) {
864 #else
865 if (current_seg && current_seg->use32) {
866 if (current_seg->grp) {
867 ext->defwrt_type = DEFWRT_GROUP;
868 ext->defwrt_ptr.grp = current_seg->grp;
869 } else {
870 ext->defwrt_type = DEFWRT_SEGMENT;
871 ext->defwrt_ptr.seg = current_seg;
874 #endif
876 if (is_global == 2) {
877 ext->commonsize = offset;
878 ext->commonelem = 1; /* default FAR */
879 } else
880 ext->commonsize = 0;
881 } else
882 return;
885 * Now process the special text, if any, to find default-WRT
886 * specifications and common-variable element-size and near/far
887 * specifications.
889 while (special && *special) {
890 used_special = true;
893 * We might have a default-WRT specification.
895 if (!nasm_strnicmp(special, "wrt", 3)) {
896 char *p;
897 int len;
898 special += 3;
899 special += strspn(special, " \t");
900 p = nasm_strndup(special, len = strcspn(special, ":"));
901 obj_ext_set_defwrt(ext, p);
902 special += len;
903 if (*special && *special != ':')
904 error(ERR_NONFATAL, "`:' expected in special symbol"
905 " text for `%s'", ext->name);
906 else if (*special == ':')
907 special++;
911 * The NEAR or FAR keywords specify nearness or
912 * farness. FAR gives default element size 1.
914 if (!nasm_strnicmp(special, "far", 3)) {
915 if (ext->commonsize)
916 ext->commonelem = 1;
917 else
918 error(ERR_NONFATAL,
919 "`%s': `far' keyword may only be applied"
920 " to common variables\n", ext->name);
921 special += 3;
922 special += strspn(special, " \t");
923 } else if (!nasm_strnicmp(special, "near", 4)) {
924 if (ext->commonsize)
925 ext->commonelem = 0;
926 else
927 error(ERR_NONFATAL,
928 "`%s': `far' keyword may only be applied"
929 " to common variables\n", ext->name);
930 special += 4;
931 special += strspn(special, " \t");
935 * If it's a common, and anything else remains on the line
936 * before a further colon, evaluate it as an expression and
937 * use that as the element size. Forward references aren't
938 * allowed.
940 if (*special == ':')
941 special++;
942 else if (*special) {
943 if (ext->commonsize) {
944 expr *e;
945 struct tokenval tokval;
947 stdscan_reset();
948 stdscan_bufptr = special;
949 tokval.t_type = TOKEN_INVALID;
950 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
951 if (e) {
952 if (!is_simple(e))
953 error(ERR_NONFATAL, "cannot use relocatable"
954 " expression as common-variable element size");
955 else
956 ext->commonelem = reloc_value(e);
958 special = stdscan_bufptr;
959 } else {
960 error(ERR_NONFATAL,
961 "`%s': element-size specifications only"
962 " apply to common variables", ext->name);
963 while (*special && *special != ':')
964 special++;
965 if (*special == ':')
966 special++;
971 i = segment / 2;
972 eb = ebhead;
973 if (!eb) {
974 eb = *ebtail = nasm_malloc(sizeof(*eb));
975 eb->next = NULL;
976 ebtail = &eb->next;
978 while (i >= EXT_BLKSIZ) {
979 if (eb && eb->next)
980 eb = eb->next;
981 else {
982 eb = *ebtail = nasm_malloc(sizeof(*eb));
983 eb->next = NULL;
984 ebtail = &eb->next;
986 i -= EXT_BLKSIZ;
988 eb->exts[i] = ext;
989 ext->index = ++externals;
991 if (special && !used_special)
992 error(ERR_NONFATAL, "OBJ supports no special symbol features"
993 " for this symbol type");
996 /* forward declaration */
997 static void obj_write_fixup(ObjRecord * orp, int bytes,
998 int segrel, int32_t seg, int32_t wrt,
999 struct Segment *segto);
1001 static void obj_out(int32_t segto, const void *data,
1002 enum out_type type, uint64_t size,
1003 int32_t segment, int32_t wrt)
1005 const uint8_t *ucdata;
1006 int32_t ldata;
1007 struct Segment *seg;
1008 ObjRecord *orp;
1011 * handle absolute-assembly (structure definitions)
1013 if (segto == NO_SEG) {
1014 if (type != OUT_RESERVE)
1015 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
1016 " space");
1017 return;
1021 * If `any_segs' is still false, we must define a default
1022 * segment.
1024 if (!any_segs) {
1025 int tempint; /* ignored */
1026 if (segto != obj_segment("__NASMDEFSEG", 2, &tempint))
1027 error(ERR_PANIC, "strange segment conditions in OBJ driver");
1031 * Find the segment we are targetting.
1033 for (seg = seghead; seg; seg = seg->next)
1034 if (seg->index == segto)
1035 break;
1036 if (!seg)
1037 error(ERR_PANIC, "code directed to nonexistent segment?");
1039 orp = seg->orp;
1040 orp->parm[0] = seg->currentpos;
1042 if (type == OUT_RAWDATA) {
1043 ucdata = data;
1044 while (size > 0) {
1045 unsigned int len;
1046 orp = obj_check(seg->orp, 1);
1047 len = RECORD_MAX - orp->used;
1048 if (len > size)
1049 len = size;
1050 memcpy(orp->buf + orp->used, ucdata, len);
1051 orp->committed = orp->used += len;
1052 orp->parm[0] = seg->currentpos += len;
1053 ucdata += len;
1054 size -= len;
1056 } else if (type == OUT_ADDRESS || type == OUT_REL2ADR ||
1057 type == OUT_REL4ADR) {
1058 int rsize;
1060 if (segment == NO_SEG && type != OUT_ADDRESS)
1061 error(ERR_NONFATAL, "relative call to absolute address not"
1062 " supported by OBJ format");
1063 if (segment >= SEG_ABS)
1064 error(ERR_NONFATAL, "far-absolute relocations not supported"
1065 " by OBJ format");
1066 ldata = *(int64_t *)data;
1067 if (type == OUT_REL2ADR) {
1068 ldata += (size - 2);
1069 size = 2;
1070 } else if (type == OUT_REL4ADR) {
1071 ldata += (size - 4);
1072 size = 4;
1074 if (size == 2)
1075 orp = obj_word(orp, ldata);
1076 else
1077 orp = obj_dword(orp, ldata);
1078 rsize = size;
1079 if (segment < SEG_ABS && (segment != NO_SEG && segment % 2) &&
1080 size == 4) {
1082 * This is a 4-byte segment-base relocation such as
1083 * `MOV EAX,SEG foo'. OBJ format can't actually handle
1084 * these, but if the constant term has the 16 low bits
1085 * zero, we can just apply a 2-byte segment-base
1086 * relocation to the low word instead.
1088 rsize = 2;
1089 if (ldata & 0xFFFF)
1090 error(ERR_NONFATAL, "OBJ format cannot handle complex"
1091 " dword-size segment base references");
1093 if (segment != NO_SEG)
1094 obj_write_fixup(orp, rsize,
1095 (type == OUT_ADDRESS ? 0x4000 : 0),
1096 segment, wrt, seg);
1097 seg->currentpos += size;
1098 } else if (type == OUT_RESERVE) {
1099 if (orp->committed)
1100 orp = obj_bump(orp);
1101 seg->currentpos += size;
1103 obj_commit(orp);
1106 static void obj_write_fixup(ObjRecord * orp, int bytes,
1107 int segrel, int32_t seg, int32_t wrt,
1108 struct Segment *segto)
1110 unsigned locat;
1111 int method;
1112 int base;
1113 int32_t tidx, fidx;
1114 struct Segment *s = NULL;
1115 struct Group *g = NULL;
1116 struct External *e = NULL;
1117 ObjRecord *forp;
1119 if (bytes == 1) {
1120 error(ERR_NONFATAL, "`obj' output driver does not support"
1121 " one-byte relocations");
1122 return;
1125 forp = orp->child;
1126 if (forp == NULL) {
1127 orp->child = forp = obj_new();
1128 forp->up = &(orp->child);
1129 /* We should choose between FIXUPP and FIXU32 record type */
1130 /* If we're targeting a 32-bit segment, use a FIXU32 record */
1131 if (segto->use32)
1132 forp->type = FIXU32;
1133 else
1134 forp->type = FIXUPP;
1137 if (seg % 2) {
1138 base = true;
1139 locat = FIX_16_SELECTOR;
1140 seg--;
1141 if (bytes != 2)
1142 error(ERR_PANIC, "OBJ: 4-byte segment base fixup got"
1143 " through sanity check");
1144 } else {
1145 base = false;
1146 locat = (bytes == 2) ? FIX_16_OFFSET : FIX_32_OFFSET;
1147 if (!segrel)
1149 * There is a bug in tlink that makes it process self relative
1150 * fixups incorrectly if the x_size doesn't match the location
1151 * size.
1153 forp = obj_force(forp, bytes << 3);
1156 forp = obj_rword(forp, locat | segrel | (orp->parm[0] - orp->parm[2]));
1158 tidx = fidx = -1, method = 0; /* placate optimisers */
1161 * See if we can find the segment ID in our segment list. If
1162 * so, we have a T4 (LSEG) target.
1164 for (s = seghead; s; s = s->next)
1165 if (s->index == seg)
1166 break;
1167 if (s)
1168 method = 4, tidx = s->obj_index;
1169 else {
1170 for (g = grphead; g; g = g->next)
1171 if (g->index == seg)
1172 break;
1173 if (g)
1174 method = 5, tidx = g->obj_index;
1175 else {
1176 int32_t i = seg / 2;
1177 struct ExtBack *eb = ebhead;
1178 while (i >= EXT_BLKSIZ) {
1179 if (eb)
1180 eb = eb->next;
1181 else
1182 break;
1183 i -= EXT_BLKSIZ;
1185 if (eb)
1186 method = 6, e = eb->exts[i], tidx = e->index;
1187 else
1188 error(ERR_PANIC,
1189 "unrecognised segment value in obj_write_fixup");
1194 * If no WRT given, assume the natural default, which is method
1195 * F5 unless:
1197 * - we are doing an OFFSET fixup for a grouped segment, in
1198 * which case we require F1 (group).
1200 * - we are doing an OFFSET fixup for an external with a
1201 * default WRT, in which case we must honour the default WRT.
1203 if (wrt == NO_SEG) {
1204 if (!base && s && s->grp)
1205 method |= 0x10, fidx = s->grp->obj_index;
1206 else if (!base && e && e->defwrt_type != DEFWRT_NONE) {
1207 if (e->defwrt_type == DEFWRT_SEGMENT)
1208 method |= 0x00, fidx = e->defwrt_ptr.seg->obj_index;
1209 else if (e->defwrt_type == DEFWRT_GROUP)
1210 method |= 0x10, fidx = e->defwrt_ptr.grp->obj_index;
1211 else {
1212 error(ERR_NONFATAL, "default WRT specification for"
1213 " external `%s' unresolved", e->name);
1214 method |= 0x50, fidx = -1; /* got to do _something_ */
1216 } else
1217 method |= 0x50, fidx = -1;
1218 } else {
1220 * See if we can find the WRT-segment ID in our segment
1221 * list. If so, we have a F0 (LSEG) frame.
1223 for (s = seghead; s; s = s->next)
1224 if (s->index == wrt - 1)
1225 break;
1226 if (s)
1227 method |= 0x00, fidx = s->obj_index;
1228 else {
1229 for (g = grphead; g; g = g->next)
1230 if (g->index == wrt - 1)
1231 break;
1232 if (g)
1233 method |= 0x10, fidx = g->obj_index;
1234 else {
1235 int32_t i = wrt / 2;
1236 struct ExtBack *eb = ebhead;
1237 while (i >= EXT_BLKSIZ) {
1238 if (eb)
1239 eb = eb->next;
1240 else
1241 break;
1242 i -= EXT_BLKSIZ;
1244 if (eb)
1245 method |= 0x20, fidx = eb->exts[i]->index;
1246 else
1247 error(ERR_PANIC,
1248 "unrecognised WRT value in obj_write_fixup");
1253 forp = obj_byte(forp, method);
1254 if (fidx != -1)
1255 forp = obj_index(forp, fidx);
1256 forp = obj_index(forp, tidx);
1257 obj_commit(forp);
1260 static int32_t obj_segment(char *name, int pass, int *bits)
1263 * We call the label manager here to define a name for the new
1264 * segment, and when our _own_ label-definition stub gets
1265 * called in return, it should register the new segment name
1266 * using the pointer it gets passed. That way we save memory,
1267 * by sponging off the label manager.
1269 #if defined(DEBUG) && DEBUG>=3
1270 fprintf(stderr, " obj_segment: < %s >, pass=%d, *bits=%d\n",
1271 name, pass, *bits);
1272 #endif
1273 if (!name) {
1274 *bits = 16;
1275 current_seg = NULL;
1276 return first_seg;
1277 } else {
1278 struct Segment *seg;
1279 struct Group *grp;
1280 struct External **extp;
1281 int obj_idx, i, attrs;
1282 bool rn_error;
1283 char *p;
1286 * Look for segment attributes.
1288 attrs = 0;
1289 while (*name == '.')
1290 name++; /* hack, but a documented one */
1291 p = name;
1292 while (*p && !nasm_isspace(*p))
1293 p++;
1294 if (*p) {
1295 *p++ = '\0';
1296 while (*p && nasm_isspace(*p))
1297 *p++ = '\0';
1299 while (*p) {
1300 while (*p && !nasm_isspace(*p))
1301 p++;
1302 if (*p) {
1303 *p++ = '\0';
1304 while (*p && nasm_isspace(*p))
1305 *p++ = '\0';
1308 attrs++;
1311 obj_idx = 1;
1312 for (seg = seghead; seg; seg = seg->next) {
1313 obj_idx++;
1314 if (!strcmp(seg->name, name)) {
1315 if (attrs > 0 && pass == 1)
1316 error(ERR_WARNING, "segment attributes specified on"
1317 " redeclaration of segment: ignoring");
1318 if (seg->use32)
1319 *bits = 32;
1320 else
1321 *bits = 16;
1322 current_seg = seg;
1323 return seg->index;
1327 *segtail = seg = nasm_malloc(sizeof(*seg));
1328 seg->next = NULL;
1329 segtail = &seg->next;
1330 seg->index = (any_segs ? seg_alloc() : first_seg);
1331 seg->obj_index = obj_idx;
1332 seg->grp = NULL;
1333 any_segs = true;
1334 seg->name = NULL;
1335 seg->currentpos = 0;
1336 seg->align = 1; /* default */
1337 seg->use32 = false; /* default */
1338 seg->combine = CMB_PUBLIC; /* default */
1339 seg->segclass = seg->overlay = NULL;
1340 seg->pubhead = NULL;
1341 seg->pubtail = &seg->pubhead;
1342 seg->lochead = NULL;
1343 seg->loctail = &seg->lochead;
1344 seg->orp = obj_new();
1345 seg->orp->up = &(seg->orp);
1346 seg->orp->ori = ori_ledata;
1347 seg->orp->type = LEDATA;
1348 seg->orp->parm[1] = obj_idx;
1351 * Process the segment attributes.
1353 p = name;
1354 while (attrs--) {
1355 p += strlen(p);
1356 while (!*p)
1357 p++;
1360 * `p' contains a segment attribute.
1362 if (!nasm_stricmp(p, "private"))
1363 seg->combine = CMB_PRIVATE;
1364 else if (!nasm_stricmp(p, "public"))
1365 seg->combine = CMB_PUBLIC;
1366 else if (!nasm_stricmp(p, "common"))
1367 seg->combine = CMB_COMMON;
1368 else if (!nasm_stricmp(p, "stack"))
1369 seg->combine = CMB_STACK;
1370 else if (!nasm_stricmp(p, "use16"))
1371 seg->use32 = false;
1372 else if (!nasm_stricmp(p, "use32"))
1373 seg->use32 = true;
1374 else if (!nasm_stricmp(p, "flat")) {
1376 * This segment is an OS/2 FLAT segment. That means
1377 * that its default group is group FLAT, even if
1378 * the group FLAT does not explicitly _contain_ the
1379 * segment.
1381 * When we see this, we must create the group
1382 * `FLAT', containing no segments, if it does not
1383 * already exist; then we must set the default
1384 * group of this segment to be the FLAT group.
1386 struct Group *grp;
1387 for (grp = grphead; grp; grp = grp->next)
1388 if (!strcmp(grp->name, "FLAT"))
1389 break;
1390 if (!grp) {
1391 obj_directive("group", "FLAT", 1);
1392 for (grp = grphead; grp; grp = grp->next)
1393 if (!strcmp(grp->name, "FLAT"))
1394 break;
1395 if (!grp)
1396 error(ERR_PANIC, "failure to define FLAT?!");
1398 seg->grp = grp;
1399 } else if (!nasm_strnicmp(p, "class=", 6))
1400 seg->segclass = nasm_strdup(p + 6);
1401 else if (!nasm_strnicmp(p, "overlay=", 8))
1402 seg->overlay = nasm_strdup(p + 8);
1403 else if (!nasm_strnicmp(p, "align=", 6)) {
1404 seg->align = readnum(p + 6, &rn_error);
1405 if (rn_error) {
1406 seg->align = 1;
1407 error(ERR_NONFATAL, "segment alignment should be"
1408 " numeric");
1410 switch ((int)seg->align) {
1411 case 1: /* BYTE */
1412 case 2: /* WORD */
1413 case 4: /* DWORD */
1414 case 16: /* PARA */
1415 case 256: /* PAGE */
1416 case 4096: /* PharLap extension */
1417 break;
1418 case 8:
1419 error(ERR_WARNING,
1420 "OBJ format does not support alignment"
1421 " of 8: rounding up to 16");
1422 seg->align = 16;
1423 break;
1424 case 32:
1425 case 64:
1426 case 128:
1427 error(ERR_WARNING,
1428 "OBJ format does not support alignment"
1429 " of %d: rounding up to 256", seg->align);
1430 seg->align = 256;
1431 break;
1432 case 512:
1433 case 1024:
1434 case 2048:
1435 error(ERR_WARNING,
1436 "OBJ format does not support alignment"
1437 " of %d: rounding up to 4096", seg->align);
1438 seg->align = 4096;
1439 break;
1440 default:
1441 error(ERR_NONFATAL, "invalid alignment value %d",
1442 seg->align);
1443 seg->align = 1;
1444 break;
1446 } else if (!nasm_strnicmp(p, "absolute=", 9)) {
1447 seg->align = SEG_ABS + readnum(p + 9, &rn_error);
1448 if (rn_error)
1449 error(ERR_NONFATAL, "argument to `absolute' segment"
1450 " attribute should be numeric");
1454 /* We need to know whenever we have at least one 32-bit segment */
1455 obj_use32 |= seg->use32;
1457 obj_seg_needs_update = seg;
1458 if (seg->align >= SEG_ABS)
1459 deflabel(name, NO_SEG, seg->align - SEG_ABS,
1460 NULL, false, false, &of_obj, error);
1461 else
1462 deflabel(name, seg->index + 1, 0L,
1463 NULL, false, false, &of_obj, error);
1464 obj_seg_needs_update = NULL;
1467 * See if this segment is defined in any groups.
1469 for (grp = grphead; grp; grp = grp->next) {
1470 for (i = grp->nindices; i < grp->nentries; i++) {
1471 if (!strcmp(grp->segs[i].name, seg->name)) {
1472 nasm_free(grp->segs[i].name);
1473 grp->segs[i] = grp->segs[grp->nindices];
1474 grp->segs[grp->nindices++].index = seg->obj_index;
1475 if (seg->grp)
1476 error(ERR_WARNING,
1477 "segment `%s' is already part of"
1478 " a group: first one takes precedence",
1479 seg->name);
1480 else
1481 seg->grp = grp;
1487 * Walk through the list of externals with unresolved
1488 * default-WRT clauses, and resolve any that point at this
1489 * segment.
1491 extp = &dws;
1492 while (*extp) {
1493 if ((*extp)->defwrt_type == DEFWRT_STRING &&
1494 !strcmp((*extp)->defwrt_ptr.string, seg->name)) {
1495 nasm_free((*extp)->defwrt_ptr.string);
1496 (*extp)->defwrt_type = DEFWRT_SEGMENT;
1497 (*extp)->defwrt_ptr.seg = seg;
1498 *extp = (*extp)->next_dws;
1499 } else
1500 extp = &(*extp)->next_dws;
1503 if (seg->use32)
1504 *bits = 32;
1505 else
1506 *bits = 16;
1507 current_seg = seg;
1508 return seg->index;
1512 static int obj_directive(char *directive, char *value, int pass)
1514 if (!strcmp(directive, "group")) {
1515 char *p, *q, *v;
1516 if (pass == 1) {
1517 struct Group *grp;
1518 struct Segment *seg;
1519 struct External **extp;
1520 int obj_idx;
1522 q = value;
1523 while (*q == '.')
1524 q++; /* hack, but a documented one */
1525 v = q;
1526 while (*q && !nasm_isspace(*q))
1527 q++;
1528 if (nasm_isspace(*q)) {
1529 *q++ = '\0';
1530 while (*q && nasm_isspace(*q))
1531 q++;
1534 * Here we used to sanity-check the group directive to
1535 * ensure nobody tried to declare a group containing no
1536 * segments. However, OS/2 does this as standard
1537 * practice, so the sanity check has been removed.
1539 * if (!*q) {
1540 * error(ERR_NONFATAL,"GROUP directive contains no segments");
1541 * return 1;
1545 obj_idx = 1;
1546 for (grp = grphead; grp; grp = grp->next) {
1547 obj_idx++;
1548 if (!strcmp(grp->name, v)) {
1549 error(ERR_NONFATAL, "group `%s' defined twice", v);
1550 return 1;
1554 *grptail = grp = nasm_malloc(sizeof(*grp));
1555 grp->next = NULL;
1556 grptail = &grp->next;
1557 grp->index = seg_alloc();
1558 grp->obj_index = obj_idx;
1559 grp->nindices = grp->nentries = 0;
1560 grp->name = NULL;
1562 obj_grp_needs_update = grp;
1563 deflabel(v, grp->index + 1, 0L,
1564 NULL, false, false, &of_obj, error);
1565 obj_grp_needs_update = NULL;
1567 while (*q) {
1568 p = q;
1569 while (*q && !nasm_isspace(*q))
1570 q++;
1571 if (nasm_isspace(*q)) {
1572 *q++ = '\0';
1573 while (*q && nasm_isspace(*q))
1574 q++;
1577 * Now p contains a segment name. Find it.
1579 for (seg = seghead; seg; seg = seg->next)
1580 if (!strcmp(seg->name, p))
1581 break;
1582 if (seg) {
1584 * We have a segment index. Shift a name entry
1585 * to the end of the array to make room.
1587 grp->segs[grp->nentries++] = grp->segs[grp->nindices];
1588 grp->segs[grp->nindices++].index = seg->obj_index;
1589 if (seg->grp)
1590 error(ERR_WARNING,
1591 "segment `%s' is already part of"
1592 " a group: first one takes precedence",
1593 seg->name);
1594 else
1595 seg->grp = grp;
1596 } else {
1598 * We have an as-yet undefined segment.
1599 * Remember its name, for later.
1601 grp->segs[grp->nentries++].name = nasm_strdup(p);
1606 * Walk through the list of externals with unresolved
1607 * default-WRT clauses, and resolve any that point at
1608 * this group.
1610 extp = &dws;
1611 while (*extp) {
1612 if ((*extp)->defwrt_type == DEFWRT_STRING &&
1613 !strcmp((*extp)->defwrt_ptr.string, grp->name)) {
1614 nasm_free((*extp)->defwrt_ptr.string);
1615 (*extp)->defwrt_type = DEFWRT_GROUP;
1616 (*extp)->defwrt_ptr.grp = grp;
1617 *extp = (*extp)->next_dws;
1618 } else
1619 extp = &(*extp)->next_dws;
1622 return 1;
1624 if (!strcmp(directive, "uppercase")) {
1625 obj_uppercase = true;
1626 return 1;
1628 if (!strcmp(directive, "import")) {
1629 char *q, *extname, *libname, *impname;
1631 if (pass == 2)
1632 return 1; /* ignore in pass two */
1633 extname = q = value;
1634 while (*q && !nasm_isspace(*q))
1635 q++;
1636 if (nasm_isspace(*q)) {
1637 *q++ = '\0';
1638 while (*q && nasm_isspace(*q))
1639 q++;
1642 libname = q;
1643 while (*q && !nasm_isspace(*q))
1644 q++;
1645 if (nasm_isspace(*q)) {
1646 *q++ = '\0';
1647 while (*q && nasm_isspace(*q))
1648 q++;
1651 impname = q;
1653 if (!*extname || !*libname)
1654 error(ERR_NONFATAL, "`import' directive requires symbol name"
1655 " and library name");
1656 else {
1657 struct ImpDef *imp;
1658 bool err = false;
1660 imp = *imptail = nasm_malloc(sizeof(struct ImpDef));
1661 imptail = &imp->next;
1662 imp->next = NULL;
1663 imp->extname = nasm_strdup(extname);
1664 imp->libname = nasm_strdup(libname);
1665 imp->impindex = readnum(impname, &err);
1666 if (!*impname || err)
1667 imp->impname = nasm_strdup(impname);
1668 else
1669 imp->impname = NULL;
1672 return 1;
1674 if (!strcmp(directive, "export")) {
1675 char *q, *extname, *intname, *v;
1676 struct ExpDef *export;
1677 int flags = 0;
1678 unsigned int ordinal = 0;
1680 if (pass == 2)
1681 return 1; /* ignore in pass two */
1682 intname = q = value;
1683 while (*q && !nasm_isspace(*q))
1684 q++;
1685 if (nasm_isspace(*q)) {
1686 *q++ = '\0';
1687 while (*q && nasm_isspace(*q))
1688 q++;
1691 extname = q;
1692 while (*q && !nasm_isspace(*q))
1693 q++;
1694 if (nasm_isspace(*q)) {
1695 *q++ = '\0';
1696 while (*q && nasm_isspace(*q))
1697 q++;
1700 if (!*intname) {
1701 error(ERR_NONFATAL, "`export' directive requires export name");
1702 return 1;
1704 if (!*extname) {
1705 extname = intname;
1706 intname = "";
1708 while (*q) {
1709 v = q;
1710 while (*q && !nasm_isspace(*q))
1711 q++;
1712 if (nasm_isspace(*q)) {
1713 *q++ = '\0';
1714 while (*q && nasm_isspace(*q))
1715 q++;
1717 if (!nasm_stricmp(v, "resident"))
1718 flags |= EXPDEF_FLAG_RESIDENT;
1719 else if (!nasm_stricmp(v, "nodata"))
1720 flags |= EXPDEF_FLAG_NODATA;
1721 else if (!nasm_strnicmp(v, "parm=", 5)) {
1722 bool err = false;
1723 flags |= EXPDEF_MASK_PARMCNT & readnum(v + 5, &err);
1724 if (err) {
1725 error(ERR_NONFATAL,
1726 "value `%s' for `parm' is non-numeric", v + 5);
1727 return 1;
1729 } else {
1730 bool err = false;
1731 ordinal = readnum(v, &err);
1732 if (err) {
1733 error(ERR_NONFATAL,
1734 "unrecognised export qualifier `%s'", v);
1735 return 1;
1737 flags |= EXPDEF_FLAG_ORDINAL;
1741 export = *exptail = nasm_malloc(sizeof(struct ExpDef));
1742 exptail = &export->next;
1743 export->next = NULL;
1744 export->extname = nasm_strdup(extname);
1745 export->intname = nasm_strdup(intname);
1746 export->ordinal = ordinal;
1747 export->flags = flags;
1749 return 1;
1751 return 0;
1754 static int32_t obj_segbase(int32_t segment)
1756 struct Segment *seg;
1759 * Find the segment in our list.
1761 for (seg = seghead; seg; seg = seg->next)
1762 if (seg->index == segment - 1)
1763 break;
1765 if (!seg) {
1767 * Might be an external with a default WRT.
1769 int32_t i = segment / 2;
1770 struct ExtBack *eb = ebhead;
1771 struct External *e;
1773 while (i >= EXT_BLKSIZ) {
1774 if (eb)
1775 eb = eb->next;
1776 else
1777 break;
1778 i -= EXT_BLKSIZ;
1780 if (eb) {
1781 e = eb->exts[i];
1782 if (e->defwrt_type == DEFWRT_NONE)
1783 return segment; /* fine */
1784 else if (e->defwrt_type == DEFWRT_SEGMENT)
1785 return e->defwrt_ptr.seg->index + 1;
1786 else if (e->defwrt_type == DEFWRT_GROUP)
1787 return e->defwrt_ptr.grp->index + 1;
1788 else
1789 return NO_SEG; /* can't tell what it is */
1792 return segment; /* not one of ours - leave it alone */
1795 if (seg->align >= SEG_ABS)
1796 return seg->align; /* absolute segment */
1797 if (seg->grp)
1798 return seg->grp->index + 1; /* grouped segment */
1800 return segment; /* no special treatment */
1803 static void obj_filename(char *inname, char *outname, efunc lerror)
1805 strcpy(obj_infile, inname);
1806 standard_extension(inname, outname, ".obj", lerror);
1809 static void obj_write_file(int debuginfo)
1811 struct Segment *seg, *entry_seg_ptr = 0;
1812 struct FileName *fn;
1813 struct LineNumber *ln;
1814 struct Group *grp;
1815 struct Public *pub, *loc;
1816 struct External *ext;
1817 struct ImpDef *imp;
1818 struct ExpDef *export;
1819 int lname_idx;
1820 ObjRecord *orp;
1823 * Write the THEADR module header.
1825 orp = obj_new();
1826 orp->type = THEADR;
1827 obj_name(orp, obj_infile);
1828 obj_emit2(orp);
1831 * Write the NASM boast comment.
1833 orp->type = COMENT;
1834 obj_rword(orp, 0); /* comment type zero */
1835 obj_name(orp, nasm_comment);
1836 obj_emit2(orp);
1838 orp->type = COMENT;
1840 * Write the IMPDEF records, if any.
1842 for (imp = imphead; imp; imp = imp->next) {
1843 obj_rword(orp, 0xA0); /* comment class A0 */
1844 obj_byte(orp, 1); /* subfunction 1: IMPDEF */
1845 if (imp->impname)
1846 obj_byte(orp, 0); /* import by name */
1847 else
1848 obj_byte(orp, 1); /* import by ordinal */
1849 obj_name(orp, imp->extname);
1850 obj_name(orp, imp->libname);
1851 if (imp->impname)
1852 obj_name(orp, imp->impname);
1853 else
1854 obj_word(orp, imp->impindex);
1855 obj_emit2(orp);
1859 * Write the EXPDEF records, if any.
1861 for (export = exphead; export; export = export->next) {
1862 obj_rword(orp, 0xA0); /* comment class A0 */
1863 obj_byte(orp, 2); /* subfunction 2: EXPDEF */
1864 obj_byte(orp, export->flags);
1865 obj_name(orp, export->extname);
1866 obj_name(orp, export->intname);
1867 if (export->flags & EXPDEF_FLAG_ORDINAL)
1868 obj_word(orp, export->ordinal);
1869 obj_emit2(orp);
1872 /* we're using extended OMF if we put in debug info */
1873 if (debuginfo) {
1874 orp->type = COMENT;
1875 obj_byte(orp, 0x40);
1876 obj_byte(orp, dEXTENDED);
1877 obj_emit2(orp);
1881 * Write the first LNAMES record, containing LNAME one, which
1882 * is null. Also initialize the LNAME counter.
1884 orp->type = LNAMES;
1885 obj_byte(orp, 0);
1886 lname_idx = 1;
1888 * Write some LNAMES for the segment names
1890 for (seg = seghead; seg; seg = seg->next) {
1891 orp = obj_name(orp, seg->name);
1892 if (seg->segclass)
1893 orp = obj_name(orp, seg->segclass);
1894 if (seg->overlay)
1895 orp = obj_name(orp, seg->overlay);
1896 obj_commit(orp);
1899 * Write some LNAMES for the group names
1901 for (grp = grphead; grp; grp = grp->next) {
1902 orp = obj_name(orp, grp->name);
1903 obj_commit(orp);
1905 obj_emit(orp);
1908 * Write the SEGDEF records.
1910 orp->type = SEGDEF;
1911 for (seg = seghead; seg; seg = seg->next) {
1912 int acbp;
1913 uint32_t seglen = seg->currentpos;
1915 acbp = (seg->combine << 2); /* C field */
1917 if (seg->use32)
1918 acbp |= 0x01; /* P bit is Use32 flag */
1919 else if (seglen == 0x10000L) {
1920 seglen = 0; /* This special case may be needed for old linkers */
1921 acbp |= 0x02; /* B bit */
1924 /* A field */
1925 if (seg->align >= SEG_ABS)
1926 /* acbp |= 0x00 */ ;
1927 else if (seg->align >= 4096) {
1928 if (seg->align > 4096)
1929 error(ERR_NONFATAL, "segment `%s' requires more alignment"
1930 " than OBJ format supports", seg->name);
1931 acbp |= 0xC0; /* PharLap extension */
1932 } else if (seg->align >= 256) {
1933 acbp |= 0x80;
1934 } else if (seg->align >= 16) {
1935 acbp |= 0x60;
1936 } else if (seg->align >= 4) {
1937 acbp |= 0xA0;
1938 } else if (seg->align >= 2) {
1939 acbp |= 0x40;
1940 } else
1941 acbp |= 0x20;
1943 obj_byte(orp, acbp);
1944 if (seg->align & SEG_ABS) {
1945 obj_x(orp, seg->align - SEG_ABS); /* Frame */
1946 obj_byte(orp, 0); /* Offset */
1948 obj_x(orp, seglen);
1949 obj_index(orp, ++lname_idx);
1950 obj_index(orp, seg->segclass ? ++lname_idx : 1);
1951 obj_index(orp, seg->overlay ? ++lname_idx : 1);
1952 obj_emit2(orp);
1956 * Write the GRPDEF records.
1958 orp->type = GRPDEF;
1959 for (grp = grphead; grp; grp = grp->next) {
1960 int i;
1962 if (grp->nindices != grp->nentries) {
1963 for (i = grp->nindices; i < grp->nentries; i++) {
1964 error(ERR_NONFATAL, "group `%s' contains undefined segment"
1965 " `%s'", grp->name, grp->segs[i].name);
1966 nasm_free(grp->segs[i].name);
1967 grp->segs[i].name = NULL;
1970 obj_index(orp, ++lname_idx);
1971 for (i = 0; i < grp->nindices; i++) {
1972 obj_byte(orp, 0xFF);
1973 obj_index(orp, grp->segs[i].index);
1975 obj_emit2(orp);
1979 * Write the PUBDEF records: first the ones in the segments,
1980 * then the far-absolutes.
1982 orp->type = PUBDEF;
1983 orp->ori = ori_pubdef;
1984 for (seg = seghead; seg; seg = seg->next) {
1985 orp->parm[0] = seg->grp ? seg->grp->obj_index : 0;
1986 orp->parm[1] = seg->obj_index;
1987 for (pub = seg->pubhead; pub; pub = pub->next) {
1988 orp = obj_name(orp, pub->name);
1989 orp = obj_x(orp, pub->offset);
1990 orp = obj_byte(orp, 0); /* type index */
1991 obj_commit(orp);
1993 obj_emit(orp);
1995 orp->parm[0] = 0;
1996 orp->parm[1] = 0;
1997 for (pub = fpubhead; pub; pub = pub->next) { /* pub-crawl :-) */
1998 if (orp->parm[2] != (uint32_t)pub->segment) {
1999 obj_emit(orp);
2000 orp->parm[2] = pub->segment;
2002 orp = obj_name(orp, pub->name);
2003 orp = obj_x(orp, pub->offset);
2004 orp = obj_byte(orp, 0); /* type index */
2005 obj_commit(orp);
2007 obj_emit(orp);
2010 * Write the EXTDEF and COMDEF records, in order.
2012 orp->ori = ori_null;
2013 for (ext = exthead; ext; ext = ext->next) {
2014 if (ext->commonsize == 0) {
2015 if (orp->type != EXTDEF) {
2016 obj_emit(orp);
2017 orp->type = EXTDEF;
2019 orp = obj_name(orp, ext->name);
2020 orp = obj_index(orp, 0);
2021 } else {
2022 if (orp->type != COMDEF) {
2023 obj_emit(orp);
2024 orp->type = COMDEF;
2026 orp = obj_name(orp, ext->name);
2027 orp = obj_index(orp, 0);
2028 if (ext->commonelem) {
2029 orp = obj_byte(orp, 0x61); /* far communal */
2030 orp = obj_value(orp, (ext->commonsize / ext->commonelem));
2031 orp = obj_value(orp, ext->commonelem);
2032 } else {
2033 orp = obj_byte(orp, 0x62); /* near communal */
2034 orp = obj_value(orp, ext->commonsize);
2037 obj_commit(orp);
2039 obj_emit(orp);
2042 * Write a COMENT record stating that the linker's first pass
2043 * may stop processing at this point. Exception is if our
2044 * MODEND record specifies a start point, in which case,
2045 * according to some variants of the documentation, this COMENT
2046 * should be omitted. So we'll omit it just in case.
2047 * But, TASM puts it in all the time so if we are using
2048 * TASM debug stuff we are putting it in
2050 if (debuginfo || obj_entry_seg == NO_SEG) {
2051 orp->type = COMENT;
2052 obj_byte(orp, 0x40);
2053 obj_byte(orp, dLINKPASS);
2054 obj_byte(orp, 1);
2055 obj_emit2(orp);
2059 * 1) put out the compiler type
2060 * 2) Put out the type info. The only type we are using is near label #19
2062 if (debuginfo) {
2063 int i;
2064 struct Array *arrtmp = arrhead;
2065 orp->type = COMENT;
2066 obj_byte(orp, 0x40);
2067 obj_byte(orp, dCOMPDEF);
2068 obj_byte(orp, 4);
2069 obj_byte(orp, 0);
2070 obj_emit2(orp);
2072 obj_byte(orp, 0x40);
2073 obj_byte(orp, dTYPEDEF);
2074 obj_word(orp, 0x18); /* type # for linking */
2075 obj_word(orp, 6); /* size of type */
2076 obj_byte(orp, 0x2a); /* absolute type for debugging */
2077 obj_emit2(orp);
2078 obj_byte(orp, 0x40);
2079 obj_byte(orp, dTYPEDEF);
2080 obj_word(orp, 0x19); /* type # for linking */
2081 obj_word(orp, 0); /* size of type */
2082 obj_byte(orp, 0x24); /* absolute type for debugging */
2083 obj_byte(orp, 0); /* near/far specifier */
2084 obj_emit2(orp);
2085 obj_byte(orp, 0x40);
2086 obj_byte(orp, dTYPEDEF);
2087 obj_word(orp, 0x1A); /* type # for linking */
2088 obj_word(orp, 0); /* size of type */
2089 obj_byte(orp, 0x24); /* absolute type for debugging */
2090 obj_byte(orp, 1); /* near/far specifier */
2091 obj_emit2(orp);
2092 obj_byte(orp, 0x40);
2093 obj_byte(orp, dTYPEDEF);
2094 obj_word(orp, 0x1b); /* type # for linking */
2095 obj_word(orp, 0); /* size of type */
2096 obj_byte(orp, 0x23); /* absolute type for debugging */
2097 obj_byte(orp, 0);
2098 obj_byte(orp, 0);
2099 obj_byte(orp, 0);
2100 obj_emit2(orp);
2101 obj_byte(orp, 0x40);
2102 obj_byte(orp, dTYPEDEF);
2103 obj_word(orp, 0x1c); /* type # for linking */
2104 obj_word(orp, 0); /* size of type */
2105 obj_byte(orp, 0x23); /* absolute type for debugging */
2106 obj_byte(orp, 0);
2107 obj_byte(orp, 4);
2108 obj_byte(orp, 0);
2109 obj_emit2(orp);
2110 obj_byte(orp, 0x40);
2111 obj_byte(orp, dTYPEDEF);
2112 obj_word(orp, 0x1d); /* type # for linking */
2113 obj_word(orp, 0); /* size of type */
2114 obj_byte(orp, 0x23); /* absolute type for debugging */
2115 obj_byte(orp, 0);
2116 obj_byte(orp, 1);
2117 obj_byte(orp, 0);
2118 obj_emit2(orp);
2119 obj_byte(orp, 0x40);
2120 obj_byte(orp, dTYPEDEF);
2121 obj_word(orp, 0x1e); /* type # for linking */
2122 obj_word(orp, 0); /* size of type */
2123 obj_byte(orp, 0x23); /* absolute type for debugging */
2124 obj_byte(orp, 0);
2125 obj_byte(orp, 5);
2126 obj_byte(orp, 0);
2127 obj_emit2(orp);
2129 /* put out the array types */
2130 for (i = ARRAYBOT; i < arrindex; i++) {
2131 obj_byte(orp, 0x40);
2132 obj_byte(orp, dTYPEDEF);
2133 obj_word(orp, i); /* type # for linking */
2134 obj_word(orp, arrtmp->size); /* size of type */
2135 obj_byte(orp, 0x1A); /* absolute type for debugging (array) */
2136 obj_byte(orp, arrtmp->basetype); /* base type */
2137 obj_emit2(orp);
2138 arrtmp = arrtmp->next;
2142 * write out line number info with a LINNUM record
2143 * switch records when we switch segments, and output the
2144 * file in a pseudo-TASM fashion. The record switch is naive; that
2145 * is that one file may have many records for the same segment
2146 * if there are lots of segment switches
2148 if (fnhead && debuginfo) {
2149 seg = fnhead->lnhead->segment;
2151 for (fn = fnhead; fn; fn = fn->next) {
2152 /* write out current file name */
2153 orp->type = COMENT;
2154 orp->ori = ori_null;
2155 obj_byte(orp, 0x40);
2156 obj_byte(orp, dFILNAME);
2157 obj_byte(orp, 0);
2158 obj_name(orp, fn->name);
2159 obj_dword(orp, 0);
2160 obj_emit2(orp);
2162 /* write out line numbers this file */
2164 orp->type = LINNUM;
2165 orp->ori = ori_linnum;
2166 for (ln = fn->lnhead; ln; ln = ln->next) {
2167 if (seg != ln->segment) {
2168 /* if we get here have to flush the buffer and start
2169 * a new record for a new segment
2171 seg = ln->segment;
2172 obj_emit(orp);
2174 orp->parm[0] = seg->grp ? seg->grp->obj_index : 0;
2175 orp->parm[1] = seg->obj_index;
2176 orp = obj_word(orp, ln->lineno);
2177 orp = obj_x(orp, ln->offset);
2178 obj_commit(orp);
2180 obj_emit(orp);
2184 * we are going to locate the entry point segment now
2185 * rather than wait until the MODEND record, because,
2186 * then we can output a special symbol to tell where the
2187 * entry point is.
2190 if (obj_entry_seg != NO_SEG) {
2191 for (seg = seghead; seg; seg = seg->next) {
2192 if (seg->index == obj_entry_seg) {
2193 entry_seg_ptr = seg;
2194 break;
2197 if (!seg)
2198 error(ERR_NONFATAL, "entry point is not in this module");
2202 * get ready to put out symbol records
2204 orp->type = COMENT;
2205 orp->ori = ori_local;
2208 * put out a symbol for the entry point
2209 * no dots in this symbol, because, borland does
2210 * not (officially) support dots in label names
2211 * and I don't know what various versions of TLINK will do
2213 if (debuginfo && obj_entry_seg != NO_SEG) {
2214 orp = obj_name(orp, "start_of_program");
2215 orp = obj_word(orp, 0x19); /* type: near label */
2216 orp = obj_index(orp, seg->grp ? seg->grp->obj_index : 0);
2217 orp = obj_index(orp, seg->obj_index);
2218 orp = obj_x(orp, obj_entry_ofs);
2219 obj_commit(orp);
2223 * put out the local labels
2225 for (seg = seghead; seg && debuginfo; seg = seg->next) {
2226 /* labels this seg */
2227 for (loc = seg->lochead; loc; loc = loc->next) {
2228 orp = obj_name(orp, loc->name);
2229 orp = obj_word(orp, loc->type);
2230 orp = obj_index(orp, seg->grp ? seg->grp->obj_index : 0);
2231 orp = obj_index(orp, seg->obj_index);
2232 orp = obj_x(orp, loc->offset);
2233 obj_commit(orp);
2236 if (orp->used)
2237 obj_emit(orp);
2240 * Write the LEDATA/FIXUPP pairs.
2242 for (seg = seghead; seg; seg = seg->next) {
2243 obj_emit(seg->orp);
2244 nasm_free(seg->orp);
2248 * Write the MODEND module end marker.
2250 orp->type = obj_use32 ? MODE32 : MODEND;
2251 orp->ori = ori_null;
2252 if (entry_seg_ptr) {
2253 orp->type = entry_seg_ptr->use32 ? MODE32 : MODEND;
2254 obj_byte(orp, 0xC1);
2255 seg = entry_seg_ptr;
2256 if (seg->grp) {
2257 obj_byte(orp, 0x10);
2258 obj_index(orp, seg->grp->obj_index);
2259 } else {
2261 * the below changed to prevent TLINK crashing.
2262 * Previous more efficient version read:
2264 * obj_byte (orp, 0x50);
2266 obj_byte(orp, 0x00);
2267 obj_index(orp, seg->obj_index);
2269 obj_index(orp, seg->obj_index);
2270 obj_x(orp, obj_entry_ofs);
2271 } else
2272 obj_byte(orp, 0);
2273 obj_emit2(orp);
2274 nasm_free(orp);
2277 static void obj_fwrite(ObjRecord * orp)
2279 unsigned int cksum, len;
2280 uint8_t *ptr;
2282 cksum = orp->type;
2283 if (orp->x_size == 32)
2284 cksum |= 1;
2285 fputc(cksum, ofp);
2286 len = orp->committed + 1;
2287 cksum += (len & 0xFF) + ((len >> 8) & 0xFF);
2288 fwriteint16_t(len, ofp);
2289 fwrite(orp->buf, 1, len - 1, ofp);
2290 for (ptr = orp->buf; --len; ptr++)
2291 cksum += *ptr;
2292 fputc((-cksum) & 0xFF, ofp);
2295 extern macros_t obj_stdmac[];
2297 void dbgbi_init(struct ofmt *of, void *id, FILE * fp, efunc error)
2299 (void)of;
2300 (void)id;
2301 (void)fp;
2302 (void)error;
2304 fnhead = NULL;
2305 fntail = &fnhead;
2306 arrindex = ARRAYBOT;
2307 arrhead = NULL;
2308 arrtail = &arrhead;
2310 static void dbgbi_cleanup(void)
2312 struct Segment *segtmp;
2313 while (fnhead) {
2314 struct FileName *fntemp = fnhead;
2315 while (fnhead->lnhead) {
2316 struct LineNumber *lntemp = fnhead->lnhead;
2317 fnhead->lnhead = lntemp->next;
2318 nasm_free(lntemp);
2320 fnhead = fnhead->next;
2321 nasm_free(fntemp->name);
2322 nasm_free(fntemp);
2324 for (segtmp = seghead; segtmp; segtmp = segtmp->next) {
2325 while (segtmp->lochead) {
2326 struct Public *loctmp = segtmp->lochead;
2327 segtmp->lochead = loctmp->next;
2328 nasm_free(loctmp->name);
2329 nasm_free(loctmp);
2332 while (arrhead) {
2333 struct Array *arrtmp = arrhead;
2334 arrhead = arrhead->next;
2335 nasm_free(arrtmp);
2339 static void dbgbi_linnum(const char *lnfname, int32_t lineno, int32_t segto)
2341 struct FileName *fn;
2342 struct LineNumber *ln;
2343 struct Segment *seg;
2345 if (segto == NO_SEG)
2346 return;
2349 * If `any_segs' is still false, we must define a default
2350 * segment.
2352 if (!any_segs) {
2353 int tempint; /* ignored */
2354 if (segto != obj_segment("__NASMDEFSEG", 2, &tempint))
2355 error(ERR_PANIC, "strange segment conditions in OBJ driver");
2359 * Find the segment we are targetting.
2361 for (seg = seghead; seg; seg = seg->next)
2362 if (seg->index == segto)
2363 break;
2364 if (!seg)
2365 error(ERR_PANIC, "lineno directed to nonexistent segment?");
2367 /* for (fn = fnhead; fn; fn = fnhead->next) */
2368 for (fn = fnhead; fn; fn = fn->next) /* fbk - Austin Lunnen - John Fine */
2369 if (!nasm_stricmp(lnfname, fn->name))
2370 break;
2371 if (!fn) {
2372 fn = nasm_malloc(sizeof(*fn));
2373 fn->name = nasm_malloc(strlen(lnfname) + 1);
2374 strcpy(fn->name, lnfname);
2375 fn->lnhead = NULL;
2376 fn->lntail = &fn->lnhead;
2377 fn->next = NULL;
2378 *fntail = fn;
2379 fntail = &fn->next;
2381 ln = nasm_malloc(sizeof(*ln));
2382 ln->segment = seg;
2383 ln->offset = seg->currentpos;
2384 ln->lineno = lineno;
2385 ln->next = NULL;
2386 *fn->lntail = ln;
2387 fn->lntail = &ln->next;
2390 static void dbgbi_deflabel(char *name, int32_t segment,
2391 int64_t offset, int is_global, char *special)
2393 struct Segment *seg;
2395 (void)special;
2398 * If it's a special-retry from pass two, discard it.
2400 if (is_global == 3)
2401 return;
2404 * First check for the double-period, signifying something
2405 * unusual.
2407 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
2408 return;
2412 * Case (i):
2414 if (obj_seg_needs_update) {
2415 return;
2416 } else if (obj_grp_needs_update) {
2417 return;
2419 if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
2420 return;
2422 if (segment >= SEG_ABS || segment == NO_SEG) {
2423 return;
2427 * If `any_segs' is still false, we might need to define a
2428 * default segment, if they're trying to declare a label in
2429 * `first_seg'. But the label should exist due to a prior
2430 * call to obj_deflabel so we can skip that.
2433 for (seg = seghead; seg; seg = seg->next)
2434 if (seg->index == segment) {
2435 struct Public *loc = nasm_malloc(sizeof(*loc));
2437 * Case (ii). Maybe MODPUB someday?
2439 last_defined = *seg->loctail = loc;
2440 seg->loctail = &loc->next;
2441 loc->next = NULL;
2442 loc->name = nasm_strdup(name);
2443 loc->offset = offset;
2446 static void dbgbi_typevalue(int32_t type)
2448 int vsize;
2449 int elem = TYM_ELEMENTS(type);
2450 type = TYM_TYPE(type);
2452 if (!last_defined)
2453 return;
2455 switch (type) {
2456 case TY_BYTE:
2457 last_defined->type = 8; /* uint8_t */
2458 vsize = 1;
2459 break;
2460 case TY_WORD:
2461 last_defined->type = 10; /* unsigned word */
2462 vsize = 2;
2463 break;
2464 case TY_DWORD:
2465 last_defined->type = 12; /* unsigned dword */
2466 vsize = 4;
2467 break;
2468 case TY_FLOAT:
2469 last_defined->type = 14; /* float */
2470 vsize = 4;
2471 break;
2472 case TY_QWORD:
2473 last_defined->type = 15; /* qword */
2474 vsize = 8;
2475 break;
2476 case TY_TBYTE:
2477 last_defined->type = 16; /* TBYTE */
2478 vsize = 10;
2479 break;
2480 default:
2481 last_defined->type = 0x19; /*label */
2482 vsize = 0;
2483 break;
2486 if (elem > 1) {
2487 struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp));
2488 int vtype = last_defined->type;
2489 arrtmp->size = vsize * elem;
2490 arrtmp->basetype = vtype;
2491 arrtmp->next = NULL;
2492 last_defined->type = arrindex++;
2493 *arrtail = arrtmp;
2494 arrtail = &(arrtmp->next);
2496 last_defined = NULL;
2498 static void dbgbi_output(int output_type, void *param)
2500 (void)output_type;
2501 (void)param;
2503 static struct dfmt borland_debug_form = {
2504 "Borland Debug Records",
2505 "borland",
2506 dbgbi_init,
2507 dbgbi_linnum,
2508 dbgbi_deflabel,
2509 null_debug_routine,
2510 dbgbi_typevalue,
2511 dbgbi_output,
2512 dbgbi_cleanup,
2515 static struct dfmt *borland_debug_arr[3] = {
2516 &borland_debug_form,
2517 &null_debug_form,
2518 NULL
2521 struct ofmt of_obj = {
2522 "MS-DOS 16-bit/32-bit OMF object files",
2523 "obj",
2524 NULL,
2525 borland_debug_arr,
2526 &borland_debug_form,
2527 obj_stdmac,
2528 obj_init,
2529 obj_set_info,
2530 obj_out,
2531 obj_deflabel,
2532 obj_segment,
2533 obj_segbase,
2534 obj_directive,
2535 obj_filename,
2536 obj_cleanup
2538 #endif /* OF_OBJ */