forked from netwide-assembler/nasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutobj.c
2489 lines (2285 loc) · 63.2 KB
/
outobj.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* outobj.c output routines for the Netwide Assembler to produce
* .OBJ object files
*
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
* Julian Hall. All rights reserved. The software is
* redistributable under the licence given in the file "Licence"
* distributed in the NASM archive.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "nasm.h"
#include "nasmlib.h"
#include "outform.h"
#ifdef OF_OBJ
/*
* outobj.c is divided into two sections. The first section is low level
* routines for creating obj records; It has nearly zero NASM specific
* code. The second section is high level routines for processing calls and
* data structures from the rest of NASM into obj format.
*
* It should be easy (though not zero work) to lift the first section out for
* use as an obj file writer for some other assembler or compiler.
*/
/*
* These routines are built around the ObjRecord data struture. An ObjRecord
* holds an object file record that may be under construction or complete.
*
* A major function of these routines is to support continuation of an obj
* record into the next record when the maximum record size is exceeded. The
* high level code does not need to worry about where the record breaks occur.
* It does need to do some minor extra steps to make the automatic continuation
* work. Those steps may be skipped for records where the high level knows no
* continuation could be required.
*
* 1) An ObjRecord is allocated and cleared by obj_new, or an existing ObjRecord
* is cleared by obj_clear.
*
* 2) The caller should fill in .type.
*
* 3) If the record is continuable and there is processing that must be done at
* the start of each record then the caller should fill in .ori with the
* address of the record initializer routine.
*
* 4) If the record is continuable and it should be saved (rather than emitted
* immediately) as each record is done, the caller should set .up to be a
* pointer to a location in which the caller keeps the master pointer to the
* ObjRecord. When the record is continued, the obj_bump routine will then
* allocate a new ObjRecord structure and update the master pointer.
*
* 5) If the .ori field was used then the caller should fill in the .parm with
* any data required by the initializer.
*
* 6) The caller uses the routines: obj_byte, obj_word, obj_rword, obj_dword,
* obj_x, obj_index, obj_value and obj_name to fill in the various kinds of
* data required for this record.
*
* 7) If the record is continuable, the caller should call obj_commit at each
* point where breaking the record is permitted.
*
* 8) To write out the record, the caller should call obj_emit2. If the
* caller has called obj_commit for all data written then he can get slightly
* faster code by calling obj_emit instead of obj_emit2.
*
* Most of these routines return an ObjRecord pointer. This will be the input
* pointer most of the time and will be the new location if the ObjRecord
* moved as a result of the call. The caller may ignore the return value in
* three cases: It is a "Never Reallocates" routine; or The caller knows
* continuation is not possible; or The caller uses the master pointer for the
* next operation.
*/
#define RECORD_MAX 1024 /* maximum size of _any_ record */
#define OBJ_PARMS 3 /* maximum .parm used by any .ori routine */
#define FIX_08_LOW 0x8000 /* location type for various fixup subrecords */
#define FIX_16_OFFSET 0x8400
#define FIX_16_SELECTOR 0x8800
#define FIX_32_POINTER 0x8C00
#define FIX_08_HIGH 0x9000
#define FIX_32_OFFSET 0xA400
#define FIX_48_POINTER 0xAC00
enum RecordID { /* record ID codes */
THEADR = 0x80, /* module header */
COMENT = 0x88, /* comment record */
LINNUM = 0x94, /* line number record */
LNAMES = 0x96, /* list of names */
SEGDEF = 0x98, /* segment definition */
GRPDEF = 0x9A, /* group definition */
EXTDEF = 0x8C, /* external definition */
PUBDEF = 0x90, /* public definition */
COMDEF = 0xB0, /* common definition */
LEDATA = 0xA0, /* logical enumerated data */
FIXUPP = 0x9C, /* fixups (relocations) */
MODEND = 0x8A /* module end */
};
enum ComentID { /* ID codes for comment records */
dEXTENDED = 0xA1, /* tells that we are using translator-specific extensions */
dLINKPASS = 0xA2, /* link pass 2 marker */
dTYPEDEF = 0xE3, /* define a type */
dSYM = 0xE6, /* symbol debug record */
dFILNAME = 0xE8, /* file name record */
dCOMPDEF = 0xEA /* compiler type info */
};
typedef struct ObjRecord ObjRecord;
typedef void ORI(ObjRecord *orp);
struct ObjRecord {
ORI *ori; /* Initialization routine */
int used; /* Current data size */
int committed; /* Data size at last boundary */
int x_size; /* (see obj_x) */
unsigned int type; /* Record type */
ObjRecord *child; /* Associated record below this one */
ObjRecord **up; /* Master pointer to this ObjRecord */
ObjRecord *back; /* Previous part of this record */
unsigned long parm[OBJ_PARMS]; /* Parameters for ori routine */
unsigned char buf[RECORD_MAX];
};
static void obj_fwrite(ObjRecord *orp);
static void ori_ledata(ObjRecord *orp);
static void ori_pubdef(ObjRecord *orp);
static void ori_null(ObjRecord *orp);
static ObjRecord *obj_commit(ObjRecord *orp);
static void obj_write_fixup (ObjRecord *orp, int bytes,
int segrel, long seg, long wrt);
static int obj_uppercase; /* Flag: all names in uppercase */
/*
* Clear an ObjRecord structure. (Never reallocates).
* To simplify reuse of ObjRecord's, .type, .ori and .parm are not cleared.
*/
static ObjRecord *obj_clear(ObjRecord *orp)
{
orp->used = 0;
orp->committed = 0;
orp->x_size = 0;
orp->child = NULL;
orp->up = NULL;
orp->back = NULL;
return (orp);
}
/*
* Emit an ObjRecord structure. (Never reallocates).
* The record is written out preceeded (recursively) by its previous part (if
* any) and followed (recursively) by its child (if any).
* The previous part and the child are freed. The main ObjRecord is cleared,
* not freed.
*/
static ObjRecord *obj_emit(ObjRecord *orp)
{
if (orp->back) {
obj_emit(orp->back);
nasm_free(orp->back);
}
if (orp->committed)
obj_fwrite(orp);
if (orp->child) {
obj_emit(orp->child);
nasm_free(orp->child);
}
return (obj_clear(orp));
}
/*
* Commit and Emit a record. (Never reallocates).
*/
static ObjRecord *obj_emit2(ObjRecord *orp)
{
obj_commit(orp);
return (obj_emit(orp));
}
/*
* Allocate and clear a new ObjRecord; Also sets .ori to ori_null
*/
static ObjRecord *obj_new(void)
{
ObjRecord *orp;
orp = obj_clear( nasm_malloc(sizeof(ObjRecord)) );
orp->ori = ori_null;
return (orp);
}
/*
* Advance to the next record because the existing one is full or its x_size
* is incompatible.
* Any uncommited data is moved into the next record.
*/
static ObjRecord *obj_bump(ObjRecord *orp)
{
ObjRecord *nxt;
int used = orp->used;
int committed = orp->committed;
if (orp->up) {
*orp->up = nxt = obj_new();
nxt->ori = orp->ori;
nxt->type = orp->type;
nxt->up = orp->up;
nxt->back = orp;
memcpy( nxt->parm, orp->parm, sizeof(orp->parm));
} else
nxt = obj_emit(orp);
used -= committed;
if (used) {
nxt->committed = 1;
nxt->ori (nxt);
nxt->committed = nxt->used;
memcpy( nxt->buf + nxt->committed, orp->buf + committed, used);
nxt->used = nxt->committed + used;
}
return (nxt);
}
/*
* Advance to the next record if necessary to allow the next field to fit.
*/
static ObjRecord *obj_check(ObjRecord *orp, int size)
{
if (orp->used + size > RECORD_MAX)
orp = obj_bump(orp);
if (!orp->committed) {
orp->committed = 1;
orp->ori (orp);
orp->committed = orp->used;
}
return (orp);
}
/*
* All data written so far is commited to the current record (won't be moved to
* the next record in case of continuation).
*/
static ObjRecord *obj_commit(ObjRecord *orp)
{
orp->committed = orp->used;
return (orp);
}
/*
* Write a byte
*/
static ObjRecord *obj_byte(ObjRecord *orp, unsigned char val)
{
orp = obj_check(orp, 1);
orp->buf[orp->used] = val;
orp->used++;
return (orp);
}
/*
* Write a word
*/
static ObjRecord *obj_word(ObjRecord *orp, unsigned int val)
{
orp = obj_check(orp, 2);
orp->buf[orp->used] = val;
orp->buf[orp->used+1] = val >> 8;
orp->used += 2;
return (orp);
}
/*
* Write a reversed word
*/
static ObjRecord *obj_rword(ObjRecord *orp, unsigned int val)
{
orp = obj_check(orp, 2);
orp->buf[orp->used] = val >> 8;
orp->buf[orp->used+1] = val;
orp->used += 2;
return (orp);
}
/*
* Write a dword
*/
static ObjRecord *obj_dword(ObjRecord *orp, unsigned long val)
{
orp = obj_check(orp, 4);
orp->buf[orp->used] = val;
orp->buf[orp->used+1] = val >> 8;
orp->buf[orp->used+2] = val >> 16;
orp->buf[orp->used+3] = val >> 24;
orp->used += 4;
return (orp);
}
/*
* All fields of "size x" in one obj record must be the same size (either 16
* bits or 32 bits). There is a one bit flag in each record which specifies
* which.
* This routine is used to force the current record to have the desired
* x_size. x_size is normally automatic (using obj_x), so that this
* routine should be used outside obj_x, only to provide compatibility with
* linkers that have bugs in their processing of the size bit.
*/
static ObjRecord *obj_force(ObjRecord *orp, int x)
{
if (orp->x_size == (x^48))
orp = obj_bump(orp);
orp->x_size = x;
return (orp);
}
/*
* This routine writes a field of size x. The caller does not need to worry at
* all about whether 16-bits or 32-bits are required.
*/
static ObjRecord *obj_x(ObjRecord *orp, unsigned long val)
{
if (orp->type & 1)
orp->x_size = 32;
if (val > 0xFFFF)
orp = obj_force(orp, 32);
if (orp->x_size == 32)
return (obj_dword(orp, val));
orp->x_size = 16;
return (obj_word(orp, val));
}
/*
* Writes an index
*/
static ObjRecord *obj_index(ObjRecord *orp, unsigned int val)
{
if (val < 128)
return ( obj_byte(orp, val) );
return (obj_word(orp, (val>>8) | (val<<8) | 0x80));
}
/*
* Writes a variable length value
*/
static ObjRecord *obj_value(ObjRecord *orp, unsigned long val)
{
if (val <= 128)
return ( obj_byte(orp, val) );
if (val <= 0xFFFF) {
orp = obj_byte(orp, 129);
return ( obj_word(orp, val) );
}
if (val <= 0xFFFFFF)
return ( obj_dword(orp, (val<<8) + 132 ) );
orp = obj_byte(orp, 136);
return ( obj_dword(orp, val) );
}
/*
* Writes a counted string
*/
static ObjRecord *obj_name(ObjRecord *orp, char *name)
{
int len = strlen(name);
unsigned char *ptr;
orp = obj_check(orp, len+1);
ptr = orp->buf + orp->used;
*ptr++ = len;
orp->used += len+1;
if (obj_uppercase)
while (--len >= 0) {
*ptr++ = toupper(*name);
name++;
} else
memcpy(ptr, name, len);
return (orp);
}
/*
* Initializer for an LEDATA record.
* parm[0] = offset
* parm[1] = segment index
* During the use of a LEDATA ObjRecord, parm[0] is constantly updated to
* represent the offset that would be required if the record were split at the
* last commit point.
* parm[2] is a copy of parm[0] as it was when the current record was initted.
*/
static void ori_ledata(ObjRecord *orp)
{
obj_index (orp, orp->parm[1]);
orp->parm[2] = orp->parm[0];
obj_x (orp, orp->parm[0]);
}
/*
* Initializer for a PUBDEF record.
* parm[0] = group index
* parm[1] = segment index
* parm[2] = frame (only used when both indexes are zero)
*/
static void ori_pubdef(ObjRecord *orp)
{
obj_index (orp, orp->parm[0]);
obj_index (orp, orp->parm[1]);
if ( !(orp->parm[0] | orp->parm[1]) )
obj_word (orp, orp->parm[2]);
}
/*
* Initializer for a LINNUM record.
* parm[0] = group index
* parm[1] = segment index
*/
static void ori_linnum(ObjRecord *orp)
{
obj_index (orp, orp->parm[0]);
obj_index (orp, orp->parm[1]);
}
/*
* Initializer for a local vars record.
*/
static void ori_local(ObjRecord *orp)
{
obj_byte (orp, 0x40);
obj_byte (orp, dSYM);
}
/*
* Null initializer for records that continue without any header info
*/
static void ori_null(ObjRecord *orp)
{
(void) orp; /* Do nothing */
}
/*
* This concludes the low level section of outobj.c
*/
static char obj_infile[FILENAME_MAX];
static efunc error;
static evalfunc evaluate;
static ldfunc deflabel;
static FILE *ofp;
static long first_seg;
static int any_segs;
static int passtwo;
static int arrindex;
#define GROUP_MAX 256 /* we won't _realistically_ have more
* than this many segs in a group */
#define EXT_BLKSIZ 256 /* block size for externals list */
struct Segment; /* need to know these structs exist */
struct Group;
struct LineNumber {
struct LineNumber *next;
struct Segment *segment;
long offset;
long lineno;
};
static struct FileName {
struct FileName *next;
char *name;
struct LineNumber *lnhead, **lntail;
int index;
} *fnhead, **fntail;
static struct Array {
struct Array *next;
unsigned size;
int basetype;
} *arrhead, **arrtail;
#define ARRAYBOT 31 /* magic number for first array index */
static struct Public {
struct Public *next;
char *name;
long offset;
long segment; /* only if it's far-absolute */
int type; /* only for local debug syms */
} *fpubhead, **fpubtail, *last_defined;
static struct External {
struct External *next;
char *name;
long commonsize;
long commonelem; /* element size if FAR, else zero */
int index; /* OBJ-file external index */
enum {
DEFWRT_NONE, /* no unusual default-WRT */
DEFWRT_STRING, /* a string we don't yet understand */
DEFWRT_SEGMENT, /* a segment */
DEFWRT_GROUP /* a group */
} defwrt_type;
union {
char *string;
struct Segment *seg;
struct Group *grp;
} defwrt_ptr;
struct External *next_dws; /* next with DEFWRT_STRING */
} *exthead, **exttail, *dws;
static int externals;
static struct ExtBack {
struct ExtBack *next;
struct External *exts[EXT_BLKSIZ];
} *ebhead, **ebtail;
static struct Segment {
struct Segment *next;
long index; /* the NASM segment id */
long obj_index; /* the OBJ-file segment index */
struct Group *grp; /* the group it belongs to */
unsigned long currentpos;
long align; /* can be SEG_ABS + absolute addr */
enum {
CMB_PRIVATE = 0,
CMB_PUBLIC = 2,
CMB_STACK = 5,
CMB_COMMON = 6
} combine;
long use32; /* is this segment 32-bit? */
struct Public *pubhead, **pubtail, *lochead, **loctail;
char *name;
char *segclass, *overlay; /* `class' is a C++ keyword :-) */
ObjRecord *orp;
} *seghead, **segtail, *obj_seg_needs_update;
static struct Group {
struct Group *next;
char *name;
long index; /* NASM segment id */
long obj_index; /* OBJ-file group index */
long nentries; /* number of elements... */
long nindices; /* ...and number of index elts... */
union {
long index;
char *name;
} segs[GROUP_MAX]; /* ...in this */
} *grphead, **grptail, *obj_grp_needs_update;
static struct ImpDef {
struct ImpDef *next;
char *extname;
char *libname;
unsigned int impindex;
char *impname;
} *imphead, **imptail;
static struct ExpDef {
struct ExpDef *next;
char *intname;
char *extname;
unsigned int ordinal;
int flags;
} *exphead, **exptail;
#define EXPDEF_FLAG_ORDINAL 0x80
#define EXPDEF_FLAG_RESIDENT 0x40
#define EXPDEF_FLAG_NODATA 0x20
#define EXPDEF_MASK_PARMCNT 0x1F
static long obj_entry_seg, obj_entry_ofs;
struct ofmt of_obj;
static long obj_segment (char *, int, int *);
static void obj_write_file(int debuginfo);
static int obj_directive (char *, char *, int);
static void obj_init (FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
{
ofp = fp;
error = errfunc;
evaluate = eval;
deflabel = ldef;
first_seg = seg_alloc();
any_segs = FALSE;
fpubhead = NULL;
fpubtail = &fpubhead;
exthead = NULL;
exttail = &exthead;
imphead = NULL;
imptail = &imphead;
exphead = NULL;
exptail = &exphead;
dws = NULL;
externals = 0;
ebhead = NULL;
ebtail = &ebhead;
seghead = obj_seg_needs_update = NULL;
segtail = &seghead;
grphead = obj_grp_needs_update = NULL;
grptail = &grphead;
obj_entry_seg = NO_SEG;
obj_uppercase = FALSE;
passtwo = 0;
of_obj.current_dfmt->init (&of_obj,NULL,fp,errfunc);
}
static int obj_set_info(enum geninfo type, char **val)
{
(void) type;
(void) val;
return 0;
}
static void obj_cleanup (int debuginfo)
{
obj_write_file(debuginfo);
of_obj.current_dfmt->cleanup();
fclose (ofp);
while (seghead) {
struct Segment *segtmp = seghead;
seghead = seghead->next;
while (segtmp->pubhead) {
struct Public *pubtmp = segtmp->pubhead;
segtmp->pubhead = pubtmp->next;
nasm_free (pubtmp->name);
nasm_free (pubtmp);
}
nasm_free (segtmp->segclass);
nasm_free (segtmp->overlay);
nasm_free (segtmp);
}
while (fpubhead) {
struct Public *pubtmp = fpubhead;
fpubhead = fpubhead->next;
nasm_free (pubtmp->name);
nasm_free (pubtmp);
}
while (exthead) {
struct External *exttmp = exthead;
exthead = exthead->next;
nasm_free (exttmp);
}
while (imphead) {
struct ImpDef *imptmp = imphead;
imphead = imphead->next;
nasm_free (imptmp->extname);
nasm_free (imptmp->libname);
nasm_free (imptmp->impname); /* nasm_free won't mind if it's NULL */
nasm_free (imptmp);
}
while (exphead) {
struct ExpDef *exptmp = exphead;
exphead = exphead->next;
nasm_free (exptmp->extname);
nasm_free (exptmp->intname);
nasm_free (exptmp);
}
while (ebhead) {
struct ExtBack *ebtmp = ebhead;
ebhead = ebhead->next;
nasm_free (ebtmp);
}
while (grphead) {
struct Group *grptmp = grphead;
grphead = grphead->next;
nasm_free (grptmp);
}
}
static void obj_ext_set_defwrt (struct External *ext, char *id)
{
struct Segment *seg;
struct Group *grp;
for (seg = seghead; seg; seg = seg->next)
if (!strcmp(seg->name, id)) {
ext->defwrt_type = DEFWRT_SEGMENT;
ext->defwrt_ptr.seg = seg;
nasm_free (id);
return;
}
for (grp = grphead; grp; grp = grp->next)
if (!strcmp(grp->name, id)) {
ext->defwrt_type = DEFWRT_GROUP;
ext->defwrt_ptr.grp = grp;
nasm_free (id);
return;
}
ext->defwrt_type = DEFWRT_STRING;
ext->defwrt_ptr.string = id;
ext->next_dws = dws;
dws = ext;
}
static void obj_deflabel (char *name, long segment,
long offset, int is_global, char *special)
{
/*
* We have three cases:
*
* (i) `segment' is a segment-base. If so, set the name field
* for the segment or group structure it refers to, and then
* return.
*
* (ii) `segment' is one of our segments, or a SEG_ABS segment.
* Save the label position for later output of a PUBDEF record.
* (Or a MODPUB, if we work out how.)
*
* (iii) `segment' is not one of our segments. Save the label
* position for later output of an EXTDEF, and also store a
* back-reference so that we can map later references to this
* segment number to the external index.
*/
struct External *ext;
struct ExtBack *eb;
struct Segment *seg;
int i;
int used_special = FALSE; /* have we used the special text? */
/*
* If it's a special-retry from pass two, discard it.
*/
if (is_global == 3)
return;
/*
* First check for the double-period, signifying something
* unusual.
*/
if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
if (!strcmp(name, "..start")) {
obj_entry_seg = segment;
obj_entry_ofs = offset;
return;
}
error (ERR_NONFATAL, "unrecognised special symbol `%s'", name);
}
/*
* Case (i):
*/
if (obj_seg_needs_update) {
obj_seg_needs_update->name = name;
return;
} else if (obj_grp_needs_update) {
obj_grp_needs_update->name = name;
return;
}
if (segment < SEG_ABS && segment != NO_SEG && segment % 2)
return;
if (segment >= SEG_ABS || segment == NO_SEG) {
/*
* SEG_ABS subcase of (ii).
*/
if (is_global) {
struct Public *pub;
pub = *fpubtail = nasm_malloc(sizeof(*pub));
fpubtail = &pub->next;
pub->next = NULL;
pub->name = nasm_strdup(name);
pub->offset = offset;
pub->segment = (segment == NO_SEG ? 0 : segment & ~SEG_ABS);
}
if (special)
error(ERR_NONFATAL, "OBJ supports no special symbol features"
" for this symbol type");
return;
}
/*
* If `any_segs' is still FALSE, we might need to define a
* default segment, if they're trying to declare a label in
* `first_seg'.
*/
if (!any_segs && segment == first_seg) {
int tempint; /* ignored */
if (segment != obj_segment("__NASMDEFSEG", 2, &tempint))
error (ERR_PANIC, "strange segment conditions in OBJ driver");
}
for (seg = seghead; seg && is_global; seg = seg->next)
if (seg->index == segment) {
struct Public *loc = nasm_malloc (sizeof(*loc));
/*
* Case (ii). Maybe MODPUB someday?
*/
*seg->pubtail = loc;
seg->pubtail = &loc->next;
loc->next = NULL;
loc->name = nasm_strdup(name);
loc->offset = offset;
if (special)
error(ERR_NONFATAL, "OBJ supports no special symbol features"
" for this symbol type");
return;
}
/*
* Case (iii).
*/
if (is_global) {
ext = *exttail = nasm_malloc(sizeof(*ext));
ext->next = NULL;
exttail = &ext->next;
ext->name = name;
ext->defwrt_type = DEFWRT_NONE;
if (is_global == 2) {
ext->commonsize = offset;
ext->commonelem = 1; /* default FAR */
} else
ext->commonsize = 0;
}
else
return;
/*
* Now process the special text, if any, to find default-WRT
* specifications and common-variable element-size and near/far
* specifications.
*/
while (special && *special) {
used_special = TRUE;
/*
* We might have a default-WRT specification.
*/
if (!nasm_strnicmp(special, "wrt", 3)) {
char *p;
int len;
special += 3;
special += strspn(special, " \t");
p = nasm_strndup(special, len = strcspn(special, ":"));
obj_ext_set_defwrt (ext, p);
special += len;
if (*special && *special != ':')
error(ERR_NONFATAL, "`:' expected in special symbol"
" text for `%s'", ext->name);
else if (*special == ':')
special++;
}
/*
* The NEAR or FAR keywords specify nearness or
* farness. FAR gives default element size 1.
*/
if (!nasm_strnicmp(special, "far", 3)) {
if (ext->commonsize)
ext->commonelem = 1;
else
error(ERR_NONFATAL, "`%s': `far' keyword may only be applied"
" to common variables\n", ext->name);
special += 3;
special += strspn(special, " \t");
} else if (!nasm_strnicmp(special, "near", 4)) {
if (ext->commonsize)
ext->commonelem = 0;
else
error(ERR_NONFATAL, "`%s': `far' keyword may only be applied"
" to common variables\n", ext->name);
special += 4;
special += strspn(special, " \t");
}
/*
* If it's a common, and anything else remains on the line
* before a further colon, evaluate it as an expression and
* use that as the element size. Forward references aren't
* allowed.
*/
if (*special == ':')
special++;
else if (*special) {
if (ext->commonsize) {
expr *e;
struct tokenval tokval;
stdscan_reset();
stdscan_bufptr = special;
tokval.t_type = TOKEN_INVALID;
e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
if (e) {
if (!is_simple(e))
error (ERR_NONFATAL, "cannot use relocatable"
" expression as common-variable element size");
else
ext->commonelem = reloc_value(e);
}
special = stdscan_bufptr;
} else {
error (ERR_NONFATAL, "`%s': element-size specifications only"
" apply to common variables", ext->name);
while (*special && *special != ':')
special++;
if (*special == ':')
special++;
}
}
}
i = segment/2;
eb = ebhead;
if (!eb) {
eb = *ebtail = nasm_malloc(sizeof(*eb));
eb->next = NULL;
ebtail = &eb->next;
}
while (i > EXT_BLKSIZ) {
if (eb && eb->next)
eb = eb->next;
else {
eb = *ebtail = nasm_malloc(sizeof(*eb));
eb->next = NULL;
ebtail = &eb->next;
}
i -= EXT_BLKSIZ;
}
eb->exts[i] = ext;
ext->index = ++externals;
if (special && !used_special)
error(ERR_NONFATAL, "OBJ supports no special symbol features"
" for this symbol type");
}
static void obj_out (long segto, void *data, unsigned long type,
long segment, long wrt)
{
long size, realtype;
unsigned char *ucdata;
long ldata;
struct Segment *seg;
ObjRecord *orp;
/*
* handle absolute-assembly (structure definitions)
*/
if (segto == NO_SEG) {
if ((type & OUT_TYPMASK) != OUT_RESERVE)
error (ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
" space");
return;
}
/*
* If `any_segs' is still FALSE, we must define a default
* segment.
*/
if (!any_segs) {
int tempint; /* ignored */
if (segto != obj_segment("__NASMDEFSEG", 2, &tempint))
error (ERR_PANIC, "strange segment conditions in OBJ driver");
}
/*
* Find the segment we are targetting.
*/
for (seg = seghead; seg; seg = seg->next)
if (seg->index == segto)
break;
if (!seg)
error (ERR_PANIC, "code directed to nonexistent segment?");
orp = seg->orp;
orp->parm[0] = seg->currentpos;
size = type & OUT_SIZMASK;
realtype = type & OUT_TYPMASK;
if (realtype == OUT_RAWDATA) {
ucdata = data;
while (size > 0) {
unsigned int len;
orp = obj_check(seg->orp, 1);
len = RECORD_MAX - orp->used;