-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathsegment.c
1256 lines (1123 loc) · 41.6 KB
/
segment.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
/****************************************************************************
*
* Open Watcom Project
*
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
* ========================================================================
*
* This file contains Original Code and/or Modifications of Original
* Code as defined in and that are subject to the Sybase Open Watcom
* Public License version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. BY USING THIS FILE YOU AGREE TO
* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
* provided with the Original Code and Modifications, and is also
* available at www.sybase.com/developer/opensource.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
* NON-INFRINGEMENT. Please see the License for the specific language
* governing rights and limitations under the License.
*
* ========================================================================
*
* Description: Processing of segment and group related directives:
* - SEGMENT, ENDS, GROUP
****************************************************************************/
#include <ctype.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "reswords.h"
#include "segment.h"
#include "expreval.h"
#include "omf.h"
#include "omfspec.h"
#include "fastpass.h"
#include "coffspec.h"
#include "assume.h"
#include "listing.h"
#include "msgtext.h"
#include "types.h"
#include "fixup.h"
#include "label.h"
#include "myassert.h"
extern ret_code EndstructDirective( int, struct asm_tok tokenarray[] );
struct asym *symCurSeg; /* @CurSeg symbol */
#define INIT_ATTR 0x01 /* READONLY attribute */
#define INIT_ALIGN 0x02 /* BYTE, WORD, PARA, DWORD, ... */
#define INIT_ALIGN_PARAM (0x80 | INIT_ALIGN) /* ALIGN(x) */
#define INIT_COMBINE 0x04 /* PRIVATE, PUBLIC, STACK, COMMON */
#define INIT_COMBINE_AT (0x80 | INIT_COMBINE) /* AT */
#define INIT_COMBINE_COMDAT (0xC0 | INIT_COMBINE) /* COMDAT */
#define INIT_OFSSIZE 0x08 /* USE16, USE32, ... */
#define INIT_OFSSIZE_FLAT (0x80 | INIT_OFSSIZE) /* FLAT */
#define INIT_ALIAS 0x10 /* ALIAS(x) */
#define INIT_CHAR 0x20 /* DISCARD, SHARED, EXECUTE, ... */
#define INIT_CHAR_INFO (0x80 | INIT_CHAR) /* INFO */
#define INIT_EXCL_MASK 0x1F /* exclusive bits */
struct typeinfo {
uint_8 value; /* value assigned to the token */
uint_8 init; /* kind of token */
};
static const char * const SegAttrToken[] = {
#define sitem( text, value, init ) text,
#include "segattr.h"
#undef sitem
};
static const struct typeinfo SegAttrValue[] = {
#define sitem( text, value, init ) { value, init },
#include "segattr.h"
#undef sitem
};
static unsigned grpdefidx; /* Number of group definitions */
static struct dsym *SegStack[MAX_SEG_NESTING]; /* stack of open segments */
static int stkindex; /* current top of stack */
/* saved state */
static struct dsym *saved_CurrSeg;
static struct dsym **saved_SegStack;
static int saved_stkindex;
/* generic byte buffer, used for OMF LEDATA records only */
static uint_8 codebuf[ 1024 ];
static uint_32 buffer_size; /* total size of code buffer */
/* min cpu for USE16, USE32 and USE64 */
static const uint_16 min_cpu[] = { P_86, P_386, P_64 };
/* find token in a string table */
static int FindToken( const char *token, const char * const *table, int size )
/****************************************************************************/
{
int i;
for( i = 0; i < size; i++, table++ ) {
if( _stricmp( *table, token ) == 0 ) {
return( i );
}
}
return( -1 ); /* Not found */
}
/* set value of $ symbol */
void UpdateCurPC( struct asym *sym, void *p )
/*******************************************/
{
if( CurrStruct ) {
sym->mem_type = MT_EMPTY;
sym->segment = NULL;
sym->offset = CurrStruct->sym.offset + (CurrStruct->next ? CurrStruct->next->sym.offset : 0);
} else if ( CurrSeg ) {
sym->mem_type = MT_NEAR;
sym->segment = (struct asym *)CurrSeg;
sym->offset = GetCurrOffset();
} else
EmitErr( MUST_BE_IN_SEGMENT_BLOCK );
}
static void AddLnameItem( struct asym *sym )
/******************************************/
{
QAddItem( &ModuleInfo.g.LnameQueue, sym );
return;
}
/* what's inserted into the LNAMES queue:
* SYM_SEG: segment names
* SYM_GRP: group names
* SYM_CLASS_LNAME : class names
*/
static void FreeLnameQueue( void )
/********************************/
{
struct qnode *curr;
struct qnode *next;
DebugMsg(("FreeLnameQueue enter\n" ));
for( curr = ModuleInfo.g.LnameQueue.head; curr; curr = next ) {
next = curr->next;
/* the class name symbols are not part of the
* symbol table and hence must be freed now.
*/
if( curr->sym->state == SYM_CLASS_LNAME ) {
SymFree( curr->sym );
}
LclFree( curr );
}
}
/* set CS assume entry whenever current segment is changed.
* Also updates values of text macro @CurSeg.
*/
void UpdateCurrSegVars( void )
/***********************************/
{
struct assume_info *info;
info = &(SegAssumeTable[ ASSUME_CS ]);
if( CurrSeg == NULL ) {
info->symbol = NULL;
info->is_flat = FALSE;
info->error = TRUE;
symCurSeg->string_ptr = "";
} else {
info->is_flat = FALSE;
info->error = FALSE;
/* fixme: OPTION OFFSET:SEGMENT? */
if( CurrSeg->e.seginfo->group != NULL ) {
info->symbol = CurrSeg->e.seginfo->group;
if ( info->symbol == &ModuleInfo.flat_grp->sym )
info->is_flat = TRUE;
} else {
info->symbol = &CurrSeg->sym;
}
symCurSeg->string_ptr = CurrSeg->sym.name;
}
return;
}
void push_seg( struct dsym *seg )
/**************************************/
/* Push a segment into the current segment stack */
{
if ( stkindex >= MAX_SEG_NESTING ) {
EmitError( NESTING_LEVEL_TOO_DEEP );
return;
}
SegStack[stkindex] = CurrSeg;
stkindex++;
CurrSeg = seg;
UpdateCurrSegVars();
return;
}
void pop_seg( void )
/*************************/
/* Pop a segment out of the current segment stack */
{
//seg = popitem( &CurrSeg ); /* changed in v1.96 */
/* it's already checked that CurrSeg is != NULL, so
* stkindex must be > 0, but anyway ...
*/
if ( stkindex ) {
stkindex--;
CurrSeg = SegStack[stkindex];
UpdateCurrSegVars();
}
return;
}
uint_32 GetCurrOffset( void )
/***************************/
{
return( CurrSeg ? CurrSeg->e.seginfo->current_loc : 0 );
}
uint_32 GetCurrSegAlign( void )
/*****************************/
{
if( CurrSeg == NULL )
return( 0 );
if ( CurrSeg->e.seginfo->alignment == MAX_SEGALIGNMENT ) /* ABS? */
return( 0x40 );
return( 1 << CurrSeg->e.seginfo->alignment );
}
static struct dsym *CreateGroup( const char *name )
/*************************************************/
{
struct dsym *grp;
grp = (struct dsym *)SymSearch( name );
if( grp == NULL || grp->sym.state == SYM_UNDEFINED ) {
if ( grp == NULL )
grp = (struct dsym *)SymCreate( name );
else
sym_remove_table( &SymTables[TAB_UNDEF], grp );
grp->sym.state = SYM_GRP;
grp->e.grpinfo = LclAlloc( sizeof( struct grp_info ) );
grp->e.grpinfo->seglist = NULL;
grp->e.grpinfo->numseg = 0;
sym_add_table( &SymTables[TAB_GRP], grp );
grp->sym.list = TRUE;
grp->e.grpinfo->grp_idx = ++grpdefidx;
AddLnameItem( &grp->sym );
} else if( grp->sym.state != SYM_GRP ) {
EmitErr( SYMBOL_REDEFINITION, name );
return( NULL );
}
grp->sym.isdefined = TRUE;
return( grp );
}
static struct dsym *CreateSegment( struct dsym *seg, const char *name, bool add_global )
/**************************************************************************************/
{
if ( seg == NULL )
seg = ( add_global ? (struct dsym *)SymCreate( name ) : (struct dsym *)SymAlloc( name ) );
else if ( seg->sym.state == SYM_UNDEFINED )
sym_remove_table( &SymTables[TAB_UNDEF], seg );
if ( seg ) {
seg->sym.state = SYM_SEG;
seg->e.seginfo = LclAlloc( sizeof( struct seg_info ) );
memset( seg->e.seginfo, 0, sizeof( struct seg_info ) );
seg->e.seginfo->Ofssize = ModuleInfo.defOfssize;
seg->e.seginfo->alignment = 4; /* this is PARA (2^4) */
seg->e.seginfo->combine = COMB_INVALID;
/* null class name, in case none is mentioned */
seg->next = NULL;
/* don't use sym_add_table(). Thus the "prev" member
* becomes free for another use.
*/
if ( SymTables[TAB_SEG].head == NULL )
SymTables[TAB_SEG].head = SymTables[TAB_SEG].tail = seg;
else {
SymTables[TAB_SEG].tail->next = seg;
SymTables[TAB_SEG].tail = seg;
}
}
return( seg );
}
void DeleteGroup( struct dsym *dir )
/**********************************/
{
#if FASTMEM==0 || defined(DEBUG_OUT)
struct seg_item *curr;
struct seg_item *next;
for( curr = dir->e.grpinfo->seglist; curr; curr = next ) {
next = curr->next;
LclFree( curr );
}
#endif
LclFree( dir->e.grpinfo );
return;
}
/* handle GROUP directive */
ret_code GrpDir( int i, struct asm_tok tokenarray[] )
/***************************************************/
{
char *name;
struct dsym *grp;
struct dsym *seg;
/* GROUP directive must be at pos 1, needs a name at pos 0 */
if( i != 1 ) {
return( EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr ) );
}
/* GROUP isn't valid for COFF/ELF/BIN-PE */
if ( Options.output_format == OFORMAT_COFF || Options.output_format == OFORMAT_ELF
|| ( Options.output_format == OFORMAT_BIN && ModuleInfo.sub_format == SFORMAT_PE )) {
return( EmitErr( NOT_SUPPORTED_WITH_CURR_FORMAT, _strupr( tokenarray[i].string_ptr ) ) );
}
grp = CreateGroup( tokenarray[0].string_ptr );
if( grp == NULL )
return( ERROR );
i++; /* go past GROUP */
do {
/* get segment name */
if ( tokenarray[i].token != T_ID ) {
return( EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr ) );
}
name = tokenarray[i].string_ptr;
i++;
seg = (struct dsym *)SymSearch( name );
if ( Parse_Pass == PASS_1 ) {
if( seg == NULL || seg->sym.state == SYM_UNDEFINED ) {
seg = CreateSegment( seg, name, TRUE );
/* inherit the offset magnitude from the group */
if ( grp->e.grpinfo->seglist )
seg->e.seginfo->Ofssize = grp->sym.Ofssize;
} else if( seg->sym.state != SYM_SEG ) {
return( EmitErr( SEGMENT_EXPECTED, name ) );
} else if( seg->e.seginfo->group != NULL &&
/* v2.09: allow segments in FLAT magic group be moved to a "real" group */
seg->e.seginfo->group != &ModuleInfo.flat_grp->sym &&
seg->e.seginfo->group != &grp->sym ) {
/* segment is in another group */
return( EmitErr( SEGMENT_IN_ANOTHER_GROUP, name ) );
}
/* the first segment will define the group's word size */
if( grp->e.grpinfo->seglist == NULL ) {
grp->sym.Ofssize = seg->e.seginfo->Ofssize;
} else if ( grp->sym.Ofssize != seg->e.seginfo->Ofssize ) {
return( EmitErr( GROUP_SEGMENT_SIZE_CONFLICT, grp->sym.name, seg->sym.name ) );
}
} else {
/* v2.04: don't check the "defined" flag in passes > 1. It's for IFDEF only! */
//if( seg == NULL || seg->sym.state != SYM_SEG || seg->sym.defined == FALSE ) {
/* v2.07: check the "segment" field instead of "defined" flag! */
//if( seg == NULL || seg->sym.state != SYM_SEG ) {
if( seg == NULL || seg->sym.state != SYM_SEG || seg->sym.segment == NULL ) {
return( EmitErr( SEGMENT_NOT_DEFINED, name ) );
}
}
/* insert segment in group if it's not there already */
if ( seg->e.seginfo->group == NULL ) {
struct seg_item *si;
/* set the segment's grp */
seg->e.seginfo->group = &grp->sym;
si = LclAlloc( sizeof( struct seg_item ) );
si->seg = seg;
si->next = NULL;
grp->e.grpinfo->numseg++;
/* insert the segment at the end of linked list */
if( grp->e.grpinfo->seglist == NULL ) {
grp->e.grpinfo->seglist = si;
} else {
struct seg_item *curr;
curr = grp->e.grpinfo->seglist;
while( curr->next != NULL ) {
curr = curr->next;
}
curr->next = si;
}
}
if ( i < Token_Count ) {
if ( tokenarray[i].token != T_COMMA || tokenarray[i+1].token == T_FINAL ) {
return( EmitErr( SYNTAX_ERROR_EX, tokenarray[i].tokpos ) );
}
i++;
}
} while ( i < Token_Count );
return( NOT_ERROR );
}
/* get/set value of predefined symbol @WordSize */
void UpdateWordSize( struct asym *sym, void *p )
/**********************************************/
{
sym->value = CurrWordSize;
return;
}
/* called by SEGMENT and ENDS directives */
ret_code SetOfssize( void )
/*************************/
{
if( CurrSeg == NULL ) {
ModuleInfo.Ofssize = ModuleInfo.defOfssize;
} else {
ModuleInfo.Ofssize = CurrSeg->e.seginfo->Ofssize;
if( (uint_8)ModuleInfo.curr_cpu < min_cpu[ModuleInfo.Ofssize] ) {
return( EmitErr( INCOMPATIBLE_CPU_MODE_FOR_XXBIT_SEGMENT, 16 << ModuleInfo.Ofssize ) );
}
}
CurrWordSize = (2 << ModuleInfo.Ofssize);
Set64Bit( ModuleInfo.Ofssize == USE64 );
return( NOT_ERROR );
}
/* close segment */
static ret_code CloseSeg( const char *name )
/******************************************/
{
if( CurrSeg == NULL || ( SymCmpFunc( CurrSeg->sym.name, name, CurrSeg->sym.name_size ) != 0 ) ) {
return( EmitErr( BLOCK_NESTING_ERROR, name ) );
}
if ( write_to_file && ( Options.output_format == OFORMAT_OMF ) ) {
omf_FlushCurrSeg();
if ( Options.no_comment_data_in_code_records == FALSE )
omf_OutSelect( FALSE );
}
pop_seg();
return( NOT_ERROR );
}
void DefineFlatGroup( void )
/**************************/
{
if( ModuleInfo.flat_grp == NULL ) {
/* can't fail because <FLAT> is a reserved word */
ModuleInfo.flat_grp = CreateGroup( "FLAT" );
ModuleInfo.flat_grp->sym.Ofssize = ModuleInfo.defOfssize;
}
ModuleInfo.flat_grp->sym.isdefined = TRUE; /* v2.09 */
}
unsigned GetSegIdx( const struct asym *sym )
/******************************************/
/* get idx to sym's segment */
{
if( sym )
return( ((struct dsym *)sym)->e.seginfo->seg_idx );
return( 0 );
}
struct asym *GetGroup( const struct asym *sym )
/*********************************************/
/* get a symbol's group */
{
struct dsym *curr;
curr = GetSegm( sym );
if( curr != NULL )
return( curr->e.seginfo->group );
return( NULL );
}
int GetSymOfssize( const struct asym *sym )
/*****************************************/
/* get sym's offset size (64=2, 32=1, 16=0) */
{
struct dsym *curr;
curr = GetSegm( sym );
if( curr == NULL ) {
if( sym->state == SYM_EXTERNAL )
return( sym->seg_ofssize );
if( sym->state == SYM_STACK || sym->state == SYM_GRP )
return( sym->Ofssize );
if( sym->state == SYM_SEG )
return( ((struct dsym *)sym)->e.seginfo->Ofssize );
if ( sym->mem_type == MT_EMPTY )
return( USE16 );
} else {
return( curr->e.seginfo->Ofssize );
}
return( ModuleInfo.Ofssize );
}
void SetSymSegOfs( struct asym *sym )
/***********************************/
{
sym->segment = &CurrSeg->sym;
sym->offset = GetCurrOffset();
}
/* get segment type from alignment, combine type or class name */
enum seg_type TypeFromClassName( const struct dsym *seg, const struct asym *clname )
/**********************************************************************************/
{
int slen;
char uname[MAX_ID_LEN+1];
if ( seg->e.seginfo->alignment == MAX_SEGALIGNMENT )
return( SEGTYPE_ABS );
if ( seg->e.seginfo->combine == COMB_STACK )
return( SEGTYPE_STACK );
if( clname == NULL )
return( SEGTYPE_UNDEF );
if( _stricmp( clname->name, GetCodeClass() ) == 0 )
return( SEGTYPE_CODE );
slen = clname->name_size;
memcpy( uname, clname->name, clname->name_size + 1 );
_strupr( uname );
switch( slen ) {
default:
case 5:
if( memcmp( uname, "CONST", 6 ) == 0 )
return( SEGTYPE_DATA );
if( memcmp( uname, "DBTYP", 6 ) == 0 )
return( SEGTYPE_DATA );
if( memcmp( uname, "DBSYM", 6 ) == 0 )
return( SEGTYPE_DATA );
case 4:
if( memcmp( uname + slen - 4, "CODE", 4 ) == 0 )
return( SEGTYPE_CODE );
if( memcmp( uname + slen - 4, "DATA", 4 ) == 0 )
return( SEGTYPE_DATA );
case 3:
if( memcmp( uname + slen - 3, "BSS", 3 ) == 0 )
return( SEGTYPE_BSS );
case 2:
case 1:
case 0:
return( SEGTYPE_UNDEF );
}
}
/* search a class item by name.
* the classes aren't stored in the symbol table!
*/
static struct asym *FindClass( const char *name, int len )
/********************************************************/
{
struct qnode *node;
for( node = ModuleInfo.g.LnameQueue.head; node; node = node->next ) {
struct asym *sym = node->sym;
if( sym->state == SYM_CLASS_LNAME && ( SymCmpFunc( sym->name, name, len ) == 0 ) )
return( sym );
}
return( NULL );
}
/* add a class name to the lname queue
* if it doesn't exist yet.
*/
static struct asym *CreateClassLname( const char *name )
/******************************************************/
{
struct asym *sym;
int len = strlen( name );
/* max lname is 255 - this is an OMF restriction */
if( len > MAX_LNAME ) {
EmitError( CLASS_NAME_TOO_LONG );
return( NULL );
}
if ( !( sym = FindClass( name, len ) ) ) {
/* the classes aren't inserted into the symbol table but they are in a queue */
sym = SymAlloc( name );
sym->state = SYM_CLASS_LNAME;
/* sym->class_lname_idx = */ AddLnameItem( sym ); /* index needed by OMF only */
}
return( sym );
}
/* set the segment's class. report an error if the class has been set
* already and the new value differs. */
static ret_code SetSegmentClass( struct dsym *seg, const char *name )
/*******************************************************************/
{
struct asym *clsym;
clsym = CreateClassLname( name );
if( clsym == NULL ) {
return( ERROR );
}
seg->e.seginfo->clsym = clsym;
return( NOT_ERROR );
}
/* CreateIntSegment(), used for internally defined segments:
* codeview debugging segments, COFF .drectve, COFF .sxdata
*/
struct asym *CreateIntSegment( const char *name, const char *classname, uint_8 alignment, uint_8 Ofssize, bool add_global )
/*************************************************************************************************************************/
{
struct dsym *seg;
if ( add_global ) {
seg = (struct dsym *)SymSearch( name );
if ( seg == NULL || seg->sym.state == SYM_UNDEFINED )
seg = CreateSegment( seg, name, add_global );
else if ( seg->sym.state != SYM_SEG ) {
EmitErr( SYMBOL_REDEFINITION, name );
return( NULL );
}
} else
seg = CreateSegment( NULL, name, FALSE );
if ( seg ) {
if( seg->sym.isdefined == FALSE ) {
seg->e.seginfo->seg_idx = ++ModuleInfo.g.num_segs;
AddLnameItem( &seg->sym );
seg->sym.isdefined = TRUE;
}
seg->e.seginfo->internal = TRUE; /* segment has private buffer */
seg->sym.segment = &seg->sym;
seg->e.seginfo->alignment = alignment;
seg->e.seginfo->Ofssize = Ofssize;
SetSegmentClass( seg, classname );
return( &seg->sym );
}
return( NULL );
}
/* ENDS directive */
ret_code EndsDir( int i, struct asm_tok tokenarray[] )
/****************************************************/
{
if( CurrStruct != NULL ) {
return( EndstructDirective( i, tokenarray ) );
}
/* a label must precede ENDS */
if( i != 1 ) {
return( EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr ) );
}
if ( Parse_Pass != PASS_1 ) {
if ( ModuleInfo.list )
LstWrite( LSTTYPE_LABEL, 0, NULL );
}
if ( CloseSeg( tokenarray[0].string_ptr ) == ERROR )
return( ERROR );
i++;
if ( tokenarray[i].token != T_FINAL ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
}
return( SetOfssize() );
}
/* SEGMENT directive if pass is > 1 */
static ret_code SetCurrSeg( int i, struct asm_tok tokenarray[] )
/**************************************************************/
{
struct asym *sym;
sym = SymSearch( tokenarray[0].string_ptr );
if ( sym == NULL || sym->state != SYM_SEG ) {
return( EmitErr( SEGMENT_NOT_DEFINED, tokenarray[0].string_ptr ) );
}
sym->isdefined = TRUE;
if ( CurrSeg && Options.output_format == OFORMAT_OMF ) {
omf_FlushCurrSeg();
if ( Options.no_comment_data_in_code_records == FALSE )
omf_OutSelect( FALSE );
}
push_seg( (struct dsym *)sym );
if ( ModuleInfo.list )
LstWrite( LSTTYPE_LABEL, 0, NULL );
return( SetOfssize() );
}
static void UnlinkSeg( struct dsym *dir )
/***************************************/
{
struct dsym *curr;
struct dsym *prev;
for ( curr = SymTables[TAB_SEG].head, prev = NULL; curr; prev = curr, curr = curr->next )
if ( curr == dir ) {
/* if segment is first, set a new head */
if ( prev == NULL )
SymTables[TAB_SEG].head = curr->next;
else
prev->next = curr->next;
/* if segment is last, set a new tail */
if ( curr->next == NULL )
SymTables[TAB_SEG].tail = prev;
break;
}
return;
}
/* SEGMENT directive */
/* Start label for Code View 8 */
struct asym* CV8Label = NULL;
ret_code SegmentDir( int i, struct asm_tok tokenarray[] )
/*******************************************************/
{
char is_old = FALSE;
char *token;
int typeidx;
const struct typeinfo *type; /* type of option */
int newseg = 0;
int temp;
int temp2;
unsigned initstate = 0; /* flags for attribute initialization */
unsigned char oldOfssize;
char oldalign;
char oldcombine;
uint_8 newcharacteristics = 0;
struct dsym *dir;
char *name;
struct asym *sym;
struct expr opndx;
if ( Parse_Pass != PASS_1 )
return( SetCurrSeg( i, tokenarray ) );
if( i != 1 ) {
return( EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr ) );
}
name = tokenarray[0].string_ptr;
/* See if the segment is already defined */
sym = SymSearch( name );
if( sym == NULL || sym->state == SYM_UNDEFINED ) {
newseg = 1;
/* segment is not defined (yet) */
sym = (struct asym *)CreateSegment( (struct dsym *)sym, name, TRUE );
sym->list = TRUE; /* always list segments */
dir = (struct dsym *)sym;
/* v2.12: seg_idx member now set AFTER parsing is done */
} else if ( sym->state == SYM_SEG ) { /* segment already defined? */
dir = (struct dsym *)sym;
/* v2.12: check 'isdefined' instead of lname_idx */
if( sym->isdefined == FALSE ) {
/* segment was forward referenced (in a GROUP directive), but not really set up */
/* the segment list is to be sorted.
* So unlink the segment and add it at the end.
*/
UnlinkSeg( dir );
dir->next = NULL;
if ( SymTables[TAB_SEG].head == NULL )
SymTables[TAB_SEG].head = SymTables[TAB_SEG].tail = dir;
else {
SymTables[TAB_SEG].tail->next = dir;
SymTables[TAB_SEG].tail = dir;
}
} else {
is_old = TRUE;
oldOfssize = dir->e.seginfo->Ofssize;
oldalign = dir->e.seginfo->alignment;
oldcombine = dir->e.seginfo->combine;
}
} else {
/* symbol is different kind, error */
return( EmitErr( SYMBOL_REDEFINITION, name ) );
}
i++; /* go past SEGMENT */
for( ; i < Token_Count; i++ ) {
token = tokenarray[i].string_ptr;
if( tokenarray[i].token == T_STRING ) {
/* the class name - the only token which is of type STRING */
/* string must be delimited by [double]quotes */
if ( tokenarray[i].string_delim != '"' &&
tokenarray[i].string_delim != '\'' ) {
EmitErr( SYNTAX_ERROR_EX, token );
continue;
}
/* remove the quote delimiters */
token++;
*(token+tokenarray[i].stringlen) = NULLC;
SetSegmentClass( dir, token );
continue;
}
/* check the rest of segment attributes.
*/
typeidx = FindToken( token, SegAttrToken, sizeof( SegAttrToken )/sizeof( SegAttrToken[0] ) );
if( typeidx < 0 ) {
EmitErr( UNKNOWN_SEGMENT_ATTRIBUTE, token );
continue;
}
type = &SegAttrValue[typeidx];
/* initstate is used to check if any field is already
* initialized
*/
if( initstate & INIT_EXCL_MASK & type->init ) {
EmitErr( SEGMENT_ATTRIBUTE_DEFINED_ALREADY, token );
continue;
} else {
initstate |= type->init; /* mark it initialized */
}
switch ( type->init ) {
case INIT_ATTR:
dir->e.seginfo->readonly = TRUE;
break;
case INIT_ALIGN:
dir->e.seginfo->alignment = type->value;
break;
case INIT_ALIGN_PARAM:
if ( Options.output_format == OFORMAT_OMF ) {
EmitErr( NOT_SUPPORTED_WITH_OMF_FORMAT, tokenarray[i].string_ptr );
i = Token_Count; /* stop further parsing of this line */
break;
}
i++;
if ( tokenarray[i].token != T_OP_BRACKET ) {
EmitErr( EXPECTED, "(" );
break;
}
i++;
if ( EvalOperand( &i, tokenarray, Token_Count, &opndx, 0 ) == ERROR )
break;
if ( tokenarray[i].token != T_CL_BRACKET ) {
EmitErr( EXPECTED, ")" );
break;
}
if ( opndx.kind != EXPR_CONST ) {
EmitError( CONSTANT_EXPECTED );
break;
}
/*
COFF allows alignment values
1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192
*/
for( temp = 1, temp2 = 0; temp < opndx.value && temp < 8192 ; temp <<= 1, temp2++ );
if( temp != opndx.value ) {
EmitErr( POWER_OF_2, opndx.value );
}
dir->e.seginfo->alignment = temp2;
break;
case INIT_COMBINE:
dir->e.seginfo->combine = type->value;
break;
case INIT_COMBINE_AT:
dir->e.seginfo->combine = type->value;
dir->e.seginfo->alignment = MAX_SEGALIGNMENT;
i++;
if ( EvalOperand( &i, tokenarray, Token_Count, &opndx, 0 ) != ERROR ) {
if ( opndx.kind == EXPR_CONST ) {
dir->e.seginfo->abs_frame = opndx.value;
dir->e.seginfo->abs_offset = 0;
} else {
EmitError( CONSTANT_EXPECTED );
}
}
break;
case INIT_COMBINE_COMDAT:
if ( Options.output_format != OFORMAT_COFF && Options.output_format != OFORMAT_OMF ) {
EmitErr( NOT_SUPPORTED_WITH_CURR_FORMAT, tokenarray[i].string_ptr );
i = Token_Count; /* stop further parsing of this line */
break;
}
i++;
if ( tokenarray[i].token != T_OP_BRACKET ) {
EmitErr( EXPECTED, "(" );
break;
}
i++;
if ( EvalOperand( &i, tokenarray, Token_Count, &opndx, 0 ) == ERROR )
break;
if ( opndx.kind != EXPR_CONST ) {
EmitError( CONSTANT_EXPECTED );
i = Token_Count; /* stop further parsing of this line */
break;
}
if ( opndx.value < 1 || opndx.value > 6 ) {
EmitErr( VALUE_NOT_WITHIN_ALLOWED_RANGE, "1-6" );
} else {
/* if value is IMAGE_COMDAT_SELECT_ASSOCIATIVE,
* get the associated segment name argument.
*/
if ( opndx.value == 5 ) {
struct asym *sym2;
if ( tokenarray[i].token != T_COMMA ) {
EmitErr( EXPECTING_COMMA, tokenarray[i].tokpos );
i = Token_Count; /* stop further parsing of this line */
break;
}
i++;
if ( tokenarray[i].token != T_ID ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
i = Token_Count; /* stop further parsing of this line */
break;
}
/* associated segment must be COMDAT, but not associative */
sym2 = SymSearch( tokenarray[i].string_ptr );
if ( sym2 == NULL ||
sym2->state != SYM_SEG ||
((struct dsym *)sym2)->e.seginfo->comdat_selection == 0 ||
((struct dsym *)sym2)->e.seginfo->comdat_selection == 5 )
EmitErr( INVALID_ASSOCIATED_SEGMENT, tokenarray[i].string_ptr );
else
dir->e.seginfo->comdat_number = ((struct dsym *)sym2)->e.seginfo->seg_idx;
i++;
}
}
if ( tokenarray[i].token != T_CL_BRACKET ) {
EmitErr( EXPECTED, ")" );
break;
}
dir->e.seginfo->comdat_selection = opndx.value;
dir->e.seginfo->combine = type->value;
break;
case INIT_OFSSIZE:
case INIT_OFSSIZE_FLAT:
if ( type->init == INIT_OFSSIZE_FLAT ) {
DefineFlatGroup();
dir->e.seginfo->Ofssize = ( ModuleInfo.defOfssize > USE16 ? ModuleInfo.defOfssize : USE32 );
/* put the segment into the FLAT group.
* this is not quite Masm-compatible, because trying to put
* the segment into another group will cause an error.
*/
dir->e.seginfo->group = &ModuleInfo.flat_grp->sym;
} else
dir->e.seginfo->Ofssize = type->value;
break;
case INIT_CHAR_INFO:
dir->e.seginfo->info = TRUE; /* fixme: check that this flag isn't changed */
break;
case INIT_CHAR:
/* characteristics are restricted to COFF/ELF/BIN-PE */
if ( Options.output_format == OFORMAT_OMF ||
( ModuleInfo.flat == FALSE && Options.output_format == OFORMAT_BIN && ModuleInfo.sub_format != SFORMAT_PE )
) {
EmitErr( NOT_SUPPORTED_WITH_CURR_FORMAT, tokenarray[i].string_ptr );
} else
newcharacteristics |= type->value;
break;
case INIT_ALIAS:
/* alias() is restricted to COFF/ELF/BIN-PE */
if ( Options.output_format == OFORMAT_OMF
|| ( Options.output_format == OFORMAT_BIN && ModuleInfo.sub_format != SFORMAT_PE )
) {
EmitErr( NOT_SUPPORTED_WITH_CURR_FORMAT, tokenarray[i].string_ptr );
i = Token_Count; /* stop further parsing of this line */
break;
}
i++;
if ( tokenarray[i].token != T_OP_BRACKET ) {
EmitErr( EXPECTED, "(" );
break;
}
i++;