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