-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment.c
1497 lines (1340 loc) · 49.1 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 "myassert.h"
extern ret_code EndstructDirective( int, struct asm_tok tokenarray[] );
struct asym symPC = { NULL,"$", 0 }; /* the '$' symbol */
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 */
#if COMDATSUPP
#define INIT_COMBINE_COMDAT (0xC0 | INIT_COMBINE) /* COMDAT */
#endif
#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 uint grpdefidx; /* Number of group definitions */
static uint LnamesIdx; /* Number of LNAMES definitions */
static struct dsym *SegStack[MAX_SEG_NESTING]; /* stack of open segments */
static int stkindex; /* current top of stack */
#if FASTPASS
/* saved state */
static struct dsym *saved_CurrSeg;
static struct dsym **saved_SegStack;
static int saved_stkindex;
#endif
/* generic byte buffer, used for OMF LEDATA records only */
static uint_8 codebuf[ 1024 ];
static uint_32 buffer_size; /* total size of code buffer */
/* 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 */
}
void SetCurPC( struct asym *sym )
/*******************************/
{
if( CurrStruct ) {
//symPC.segment = NULL;
//symPC.mem_type = MT_ABS;
symPC.mem_type = MT_EMPTY;
symPC.segment = NULL; /* v2.07: needed again */
symPC.offset = CurrStruct->sym.offset + (CurrStruct->next ? CurrStruct->next->sym.offset : 0);
} else {
symPC.mem_type = MT_NEAR;
symPC.segment = (struct asym *)CurrSeg;
symPC.offset = GetCurrOffset();
}
DebugMsg1(("SetCurPC: curr value=%" FX32 "h\n", symPC.offset ));
}
#if 0
/* value of text macros can't be set by internal function call yet! */
void SetCurSeg( struct asym *sym )
/********************************/
{
symCurSeg->string_ptr = CurrSeg ? CurrSeg->sym.name : "";
DebugMsg1(("SetCurSeg: curr value=>%s<\n", symCurSeg->string_ptr ));
}
#endif
/* find a class name.
* those names aren't in the symbol table!
*/
char *GetLname( direct_idx idx )
/******************************/
{
struct qnode *node;
struct asym *sym;
for( node = ModuleInfo.g.LnameQueue.head; node != NULL; node = node->next ) {
sym = (struct asym *)node->elmt;
if( sym->state == SYM_CLASS_LNAME && sym->class_lname_idx == idx ) {
return( sym->name );
}
}
return( NULL );
}
/* find a class index.
* the classes aren't in the symbol table!
*/
static direct_idx FindLnameIdx( const char *name )
/************************************************/
{
struct qnode *node;
struct asym *sym;
for( node = ModuleInfo.g.LnameQueue.head; node != NULL; node = node->next ) {
sym = (struct asym *)node->elmt;
if( sym->state != SYM_CLASS_LNAME )
continue;
if( _stricmp( sym->name, name ) == 0 ) {
return( sym->class_lname_idx );
}
}
return( LNAME_NULL );
}
static void AddLnameData( struct asym *sym )
/******************************************/
{
QAddItem( &ModuleInfo.g.LnameQueue, sym );
}
/* what's inserted into the LNAMES queue:
* SYM_SEG: segment names
* SYM_GRP: group names
* SYM_CLASS_LNAME : class names
*/
void FreeLnameQueue( void )
/*************************/
{
struct asym *sym;
struct qnode *curr;
struct qnode *next;
DebugMsg(("FreeLnameQueue enter\n" ));
for( curr = ModuleInfo.g.LnameQueue.head; curr; curr = next ) {
next = curr->next;
sym = (struct asym *)curr->elmt;
/* the class name symbols are not part of the
* symbol table and hence must be freed now.
*/
if( sym->state == SYM_CLASS_LNAME ) {
SymFree( sym );
}
LclFree( curr );
}
}
/* set CS assume entry whenever current segment is changed.
* Also updates values of text macro @CurSeg.
*/
static void UpdateCurrSegVars( void )
/***********************************/
{
struct assume_info *info;
DebugMsg1(("UpdateCurrSegVars(%s)\n", CurrSeg ? CurrSeg->sym.name : "NULL" ));
info = &(SegAssumeTable[ ASSUME_CS ]);
if( CurrSeg == NULL ) {
info->symbol = NULL;
info->flat = FALSE;
info->error = TRUE;
symCurSeg->string_ptr = "";
//symPC.segment = NULL; /* v2.05: removed */
} else {
info->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->flat = TRUE;
} else {
info->symbol = &CurrSeg->sym;
}
symCurSeg->string_ptr = CurrSeg->sym.name;
//symPC.segment = &CurrSeg->sym; /* v2.05: removed */
}
return;
}
static void push_seg( struct dsym *seg )
/**************************************/
/* Push a segment into the current segment stack */
{
//pushitem( &CurrSeg, seg ); /* changed in v1.96 */
if ( stkindex >= MAX_SEG_NESTING ) {
EmitError( NESTING_LEVEL_TOO_DEEP );
return;
}
SegStack[stkindex] = CurrSeg;
stkindex++;
CurrSeg = seg;
UpdateCurrSegVars();
return;
}
static 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;
}
/* add a class name to the queue of names */
static direct_idx InsertClassLname( const char *name )
/****************************************************/
{
struct asym *sym;
if( strlen( name ) > MAX_LNAME ) {
EmitError( CLASS_NAME_TOO_LONG );
return( LNAME_NULL );
}
/* 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 = ++LnamesIdx;
/* put it into the lname table */
AddLnameData( sym );
return( LnamesIdx );
}
uint_32 GetCurrOffset( void )
/***************************/
{
return( CurrSeg ? CurrSeg->e.seginfo->current_loc : 0 );
}
#if 0
struct dsym *GetCurrSeg( void )
/*****************************/
{
return( CurrSeg );
}
#endif
#if 0
int GetCurrClass( void )
/***********************/
{
if( CurrSeg == NULL )
return( 0 );
return( CurrSeg->e.seginfo->segrec->d.segdef.class_name_idx );
}
#endif
uint_32 GetCurrSegAlign( void )
/*****************************/
{
if( CurrSeg == NULL )
return( 0 );
if ( CurrSeg->e.seginfo->alignment == MAX_SEGALIGNMENT ) /* ABS? */
return( 0x10 ); /* assume PARA alignment for AT segments */
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->grp_idx = 0;
//grp->e.grpinfo->lname_idx = 0;
grp->e.grpinfo->numseg = 0;
sym_add_table( &SymTables[TAB_GRP], grp );
grp->sym.list = TRUE;
grp->e.grpinfo->grp_idx = ++grpdefidx;
grp->e.grpinfo->lname_idx = ++LnamesIdx;
AddLnameData( &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->e.seginfo->class_name_idx = 1;
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 )
/**********************************/
{
struct seg_item *curr;
struct seg_item *next;
for( curr = dir->e.grpinfo->seglist; curr; curr = next ) {
next = curr->next;
DebugMsg(("DeleteGroup(%s): free seg_item=%p\n", dir->sym.name, curr ));
LclFree( curr );
}
DebugMsg(("DeleteGroup(%s): extension %p will be freed\n", dir->sym.name, dir->e.grpinfo ));
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 ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
#if COFF_SUPPORT || ELF_SUPPORT
/* GROUP valid for OMF + BIN only */
if ( Options.output_format == OFORMAT_COFF
#if ELF_SUPPORT
|| Options.output_format == OFORMAT_ELF
#endif
) {
EmitError( GROUP_DIRECTIVE_INVALID_FOR_COFF );
return( ERROR );
}
#endif
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 ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
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 ) {
EmitErr( SEGMENT_EXPECTED, name );
return( ERROR );
} else if( seg->e.seginfo->group != NULL &&
seg->e.seginfo->group != &grp->sym ) {
/* segment is in another group */
DebugMsg(("GrpDir: segment >%s< is in group >%s< already\n", name, seg->e.seginfo->group->name));
EmitErr( SEGMENT_IN_ANOTHER_GROUP, name );
return( ERROR );
}
/* 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 ) {
EmitErr( GROUP_SEGMENT_SIZE_CONFLICT, grp->sym.name, seg->sym.name );
return( ERROR );
}
} else {
/* v2.04: don't check the "defined" flag. 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 ) {
EmitErr( SEG_NOT_DEFINED, name );
return( ERROR );
}
}
/* 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 ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].tokpos );
return( ERROR );
}
i++;
}
} while ( i < Token_Count );
return( NOT_ERROR );
}
ret_code SetOfssize( void )
/*************************/
{
if( CurrSeg == NULL ) {
ModuleInfo.Ofssize = ModuleInfo.defOfssize;
} else {
ModuleInfo.Ofssize = CurrSeg->e.seginfo->Ofssize;
if( ModuleInfo.Ofssize > USE16 && ( ( ModuleInfo.curr_cpu & P_CPU_MASK ) < P_386 ) ) {
DebugMsg(("SetOfssize, error: CurrSeg=%s, ModuleInfo.Ofssize=%u, curr_cpu=%X, defOfssize=%u\n",
CurrSeg->sym.name, ModuleInfo.Ofssize, ModuleInfo.curr_cpu, ModuleInfo.defOfssize ));
EmitError( INCOMPATIBLE_CPU_MODE_FOR_32BIT_SEGMENT );
return( ERROR );
}
}
WordSize.value = (2 << ModuleInfo.Ofssize);
#if AMD64_SUPPORT
Set64Bit( ModuleInfo.Ofssize == USE64 );
#endif
return( NOT_ERROR );
}
/* close segment */
static ret_code CloseSeg( const char *name )
/******************************************/
{
//struct asym *sym;
DebugMsg1(("CloseSeg(%s) enter\n", name));
if( CurrSeg == NULL || ( SymCmpFunc( CurrSeg->sym.name, name, CurrSeg->sym.name_size ) != 0 ) ) {
DebugMsg(("CloseSeg(%s): nesting error, CurrSeg=%s\n", name, CurrSeg ? CurrSeg->sym.name : "(null)" ));
EmitErr( BLOCK_NESTING_ERROR, name );
return( ERROR );
}
DebugMsg1(("CloseSeg(%s): current ofs=%" FX32 "\n", name, CurrSeg->e.seginfo->current_loc));
if ( write_to_file && ( Options.output_format == OFORMAT_OMF ) ) {
//if ( !omf_FlushCurrSeg() ) /* v2: error check is obsolete */
// EmitErr( INTERNAL_ERROR, "CloseSeg", 1 ); /* coding error! */
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.flatgrp_idx = ModuleInfo.flat_grp->e.grpinfo->grp_idx;
}
}
uint 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;
/* v2.07: MT_ABS has been removed */
//if ( sym->mem_type == MT_ABS )
// return( USE16 );
curr = GetSegm( sym );
if( curr == NULL ) {
/* v2.04: SYM_STACK added */
//if( sym->state == SYM_EXTERNAL || ( sym->state == SYM_INTERNAL && sym->isproc ) || sym->state == SYM_GRP )
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 );
/* v2.07: added */
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();
}
static enum seg_type TypeFromClassName( const struct dsym *dir, const char *name )
/********************************************************************************/
{
int slen;
char uname[MAX_ID_LEN+1];
if ( dir->e.seginfo->alignment == MAX_SEGALIGNMENT )
return( SEGTYPE_ABS );
/* v2.03: added */
if ( dir->e.seginfo->combine == COMB_STACK )
return( SEGTYPE_STACK );
if( name == NULL )
return( SEGTYPE_UNDEF );
if( _stricmp( name, GetCodeClass() ) == 0 )
return( SEGTYPE_CODE );
slen = strlen( name );
strcpy( uname, name );
_strupr( uname );
switch( slen ) {
default:
case 5:
if( memcmp( uname, "CONST", 6 ) == 0 )
return( SEGTYPE_DATA );
//if( memcmp( uname, "STACK", 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:
/* v2.03: changed */
//if( memcmp( uname , "CODE", 5 ) == 0 )
// return( SEGTYPE_CODE );
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 );
}
}
#if 0 /* v2.03: obsolete */
/* get the type of the segment by checking it's name.
* this is called only if the class gives no hint
*/
static enum seg_type TypeFromSegmentName( const char *name )
/**********************************************************/
{
int slen;
char uname[MAX_ID_LEN+1];
slen = strlen( name );
strcpy( uname, name );
_strupr( uname );
switch( slen ) {
default:
case 5:
if ( Options.output_format != OFORMAT_COFF ) {
/* '..._TEXT'? */
if( memcmp( uname + slen - 5, SegmNamesDef[SIM_CODE], 5 ) == 0 )
return( SEGTYPE_CODE );
}
/* '..._DATA' */
if( memcmp( uname + slen - 5, SegmNamesDef[SIM_DATA], 5 ) == 0 )
return( SEGTYPE_DATA );
/* 'CONST' */
if( memcmp( uname + slen - 5, SegmNamesDef[SIM_CONST], 5 ) == 0 )
return( SEGTYPE_DATA );
case 4:
if ( Options.output_format != OFORMAT_COFF ) {
/* '..._BSS' */
if( memcmp( uname + slen - 4, "_BSS", 4 ) == 0 )
return( SEGTYPE_BSS );
}
case 3:
case 2:
case 1:
case 0:
return( SEGTYPE_UNDEF );
}
}
#endif
/* set the segment's class. report an error if the class has been set
* already and the new value differs. */
static direct_idx SetSegmentClass( struct asym *seg, const char *classname )
/**************************************************************************/
{
direct_idx classidx;
classidx = FindLnameIdx( classname );
if( classidx == LNAME_NULL ) {
classidx = InsertClassLname( classname );
if( classidx == LNAME_NULL ) {
return( ERROR );
}
}
/* default class name index is 1, which is the NULL class name */
if ( ((struct dsym *)seg)->e.seginfo->class_name_idx == 1 )
((struct dsym *)seg)->e.seginfo->class_name_idx = classidx;
else if ( ((struct dsym *)seg)->e.seginfo->class_name_idx != classidx ) {
EmitErr( SEGDEF_CHANGED, seg->name, MsgGetEx( TXT_CLASS ) );
return( ERROR );
}
return( classidx );
}
/* 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->e.seginfo->lname_idx == 0 ) {
seg->e.seginfo->seg_idx = ++ModuleInfo.g.num_segs;
seg->e.seginfo->lname_idx = ++LnamesIdx;
AddLnameData( &seg->sym );
}
seg->sym.segment = &seg->sym;
seg->e.seginfo->alignment = alignment;
seg->e.seginfo->Ofssize = Ofssize;
SetSegmentClass( (struct asym *)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 ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
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 );
DebugMsg1(("SetCurrSeg(%s) sym=%p\n", tokenarray[0].string_ptr, sym));
if ( sym == NULL || sym->state != SYM_SEG ) {
EmitErr( SEG_NOT_DEFINED, tokenarray[0].string_ptr );
return( ERROR );
}
/* v2.04: added */
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;
for ( curr = SymTables[TAB_SEG].head; curr; curr = curr->next )
if ( curr == dir ) {
SymTables[TAB_SEG].head = dir->next;
break;
} else if ( curr->next == dir ) {
curr->next = dir->next;
break;
}
/* if segment is last, set a new tail */
if ( dir->next == NULL ) {
for ( curr = SymTables[TAB_SEG].head; curr && curr->next; curr = curr->next );
SymTables[TAB_SEG].tail = curr;
}
return;
}
/* SEGMENT directive */
ret_code SegmentDir( int i, struct asm_tok tokenarray[] )
/*******************************************************/
{
char is_old;
char *token;
int typeidx;
const struct typeinfo *type; /* type of option */
int temp;
int temp2;
uint initstate = 0; /* flags for attribute initialization */
unsigned char oldreadonly; /* readonly value of a defined segment */
//unsigned char oldsegtype;
unsigned char oldOfssize;
char oldalign;
char oldcombine;
uint oldclassidx;
uint_8 oldcharacteristics;
struct dsym *dir;
char *name;
struct asym *sym;
struct expr opndx;
if ( Parse_Pass != PASS_1 )
return( SetCurrSeg( i, tokenarray ) );
if( i != 1 ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
name = tokenarray[0].string_ptr;
DebugMsg1(("SegmentDir(%s) enter: ModuleInfo.Ofssize=%u, num_seg=%u\n", name, ModuleInfo.Ofssize, ModuleInfo.g.num_segs ));
/* See if the segment is already defined */
sym = SymSearch( name );
if( sym == NULL || sym->state == SYM_UNDEFINED ) {
/* segment is not defined (yet) */
sym = (struct asym *)CreateSegment( (struct dsym *)sym, name, TRUE );
sym->list = TRUE; /* always list segments */
dir = (struct dsym *)sym;
dir->e.seginfo->seg_idx = ++ModuleInfo.g.num_segs;
is_old = FALSE;
/*
* initialize segment with values from the one without suffix
*/
#if COFF_SUPPORT || ELF_SUPPORT
if (Options.output_format == OFORMAT_COFF
#if ELF_SUPPORT
|| Options.output_format == OFORMAT_ELF
#endif
) {
char *p;
if ( p = strchr(sym->name, '$') ) {
char buffer[MAX_ID_LEN+1];
struct dsym *dir2;
memcpy(buffer, sym->name, p - sym->name);
buffer[p - sym->name] = NULLC;
if ((dir2 = (struct dsym *)SymSearch(buffer)) && dir2->sym.state == SYM_SEG) {
dir->e.seginfo->readonly = dir2->e.seginfo->readonly;
dir->e.seginfo->segtype = dir2->e.seginfo->segtype;
dir->e.seginfo->Ofssize = dir2->e.seginfo->Ofssize;
dir->e.seginfo->alignment= dir2->e.seginfo->alignment;
dir->e.seginfo->characteristics = dir2->e.seginfo->characteristics;
dir->e.seginfo->combine = dir2->e.seginfo->combine;
dir->e.seginfo->class_name_idx = dir2->e.seginfo->class_name_idx;
}
}
}
#endif
} else if ( sym->state == SYM_SEG ) {
/* segment already defined */
dir = (struct dsym *)sym;
is_old = TRUE;
oldreadonly = dir->e.seginfo->readonly;
//oldsegtype = dir->e.seginfo->segtype;
oldOfssize = dir->e.seginfo->Ofssize;
oldalign = dir->e.seginfo->alignment;
oldcharacteristics = dir->e.seginfo->characteristics;
oldcombine = dir->e.seginfo->combine;
oldclassidx = dir->e.seginfo->class_name_idx;
if( dir->e.seginfo->lname_idx == 0 ) {
/* segment was mentioned in a group statement, but not really set up */
is_old = FALSE;
/* the segment list is to be sorted.
* So unlink the segment and add it at the end.
*/
UnlinkSeg( dir );
dir->e.seginfo->seg_idx = ++ModuleInfo.g.num_segs;
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 {
/* symbol is different kind, error */
DebugMsg(("SegmentDir(%s): symbol redefinition\n", name ));
EmitErr( SYMBOL_REDEFINITION, name );
return( ERROR );
}
i++; /* go past SEGMENT */
for( ; i < Token_Count; i++ ) {
token = tokenarray[i].string_ptr;
DebugMsg1(("SegmentDir(%s): i=%u, string=%s token=%X\n", name, i, token, tokenarray[i].token ));
if( tokenarray[i].token == T_STRING ) {