-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLINK.PAS
More file actions
4550 lines (4149 loc) · 131 KB
/
Copy pathLINK.PAS
File metadata and controls
4550 lines (4149 loc) · 131 KB
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
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2025
@website(https://www.gladir.com/msdos0)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
}
Program LINK;
{$A-}
Uses DOS;
Const
MAX_SEGMENTS=128; { Nombre maximal de segments suivis pendant l'edition de liens }
MAX_SYMBOLS=1024; { Nombre maximal de symboles (publics + externes) }
MAX_RELOCATIONS=2048; { Nombre maximal d'entrees de relocation en memoire }
MAX_GROUPS=128; { Nombre maximal de groupes OMF (GRPDEF) }
MAX_LNAMES=2048; { Nombre maximal de noms logiques LNAMES }
MAX_LINENUMS=4096; { Nombre maximal d'entrees de debug LINNUM }
MAX_COMMENTS=2048; { Nombre maximal d'enregistrements COMENT OMF }
MAX_ALIASES=1024; { Nombre maximal de definitions ALIAS }
MAX_CMD_OBJS=256; { Nombre maximal de fichiers objets en entree (phase 1) }
MAX_CMD_LIBS=256; { Nombre maximal de bibliotheques en entree (phase 1) }
MAX_LIB_TRACKED_MODULES=256; { Modules de bibliotheque charges/suivis }
Type
{ En-tete DOS MZ du fichier executable genere }
TExeHeader=Record
Signature:Word; { Signature DOS 'MZ' }
LastPageBytes:Word; { Nombre d'octets utilises dans la derniere page }
PageCount:Word; { Nombre total de pages de 512 octets dans l'EXE }
RelocCount:Word; { Nombre d'entrees de relocation }
HeaderSize:Word; { Taille de l'en-tete en paragraphes (16 octets) }
MinAlloc:Word; { Memoire minimale supplementaire demandee }
MaxAlloc:Word; { Memoire maximale supplementaire demandee }
InitSS:Word; { Valeur initiale de SS (relative au debut de chargement) }
InitSP:Word; { Valeur initiale de SP }
Checksum:Word; { Somme de controle (souvent 0 si non utilisee) }
InitIP:Word; { Valeur initiale de IP }
InitCS:Word; { Valeur initiale de CS (relative au debut de chargement) }
RelocOffset:Word; { Offset dans l'en-tete de la table de relocation }
OverlayNumber:Word; { Numero d'overlay (0 pour programme principal) }
End;
{ Description d'un segment logique dans l'image liee }
TSegment=Record
Name:String[16]; { Nom logique du segment }
Address:LongInt; { Adresse/offset de base du segment dans l'image liee }
Size:Word; { Taille du segment en octets }
Class:String[8]; { Classe du segment (CODE, DATA, etc.) }
End;
{ Description d'un symbole resolu ou externe }
TSymbol=Record
Name:String[32]; { Nom du symbole }
Segment:Word; { Index du segment de definition (0 si externe) }
Offset:Word; { Offset du symbole dans son segment }
IsExternal:Boolean; { True si symbole externe non defini dans ce module }
End;
{ Entree de relocation pour corriger une adresse a l'execution }
TRelocation=Record
Offset:Word; { Offset MZ de la valeur a relocaliser }
Segment:Word; { Segment MZ (paragraphe) de la valeur a relocaliser }
Target:Word; { Reserve pour informations de cible/fixup }
End;
PSegment=^TSegment;
PSymbol=^TSymbol;
{ Listes chainees des structures principales }
PSegmentItem=^TSegmentItem;
TSegmentItem=Record
Value:TSegment;
Next:PSegmentItem;
End;
PSymbolItem=^TSymbolItem;
TSymbolItem=Record
Value:TSymbol;
Next:PSymbolItem;
End;
PRelocationItem=^TRelocationItem;
TRelocationItem=Record
Value:TRelocation;
Next:PRelocationItem;
End;
PGroupMemberItem=^TGroupMemberItem;
TGroupMemberItem=Record
SegIndex:Word;
Next:PGroupMemberItem;
End;
PGroupItem=^TGroupItem;
TGroupItem=Record
NameIndex:Word;
Is32Bit:Boolean;
MemberHead,MemberTail:PGroupMemberItem;
MemberCount:Integer;
Next:PGroupItem;
End;
PLNameItem=^TLNameItem;
TLNameItem=Record
Value:String[64];
Next:PLNameItem;
End;
PLineInfoItem=^TLineInfoItem;
TLineInfoItem=Record
GroupIndex:Word;
SegIndex:Word;
LineNo:LongInt;
CodeOffset:LongInt;
Is32Bit:Boolean;
Next:PLineInfoItem;
End;
PCommentItem=^TCommentItem;
TCommentItem=Record
Flags:Byte;
ClassCode:Byte;
Text:String[80];
Next:PCommentItem;
End;
{ Liste chainee des corrections OFFSET }
POffsetPatchItem=^TOffsetPatchItem;
TOffsetPatchItem=Record
Pos:LongInt;
Delta:Integer;
Next:POffsetPatchItem;
End;
PBackPatchItem=^TBackPatchItem;
TBackPatchItem=Record
SegIndex:Word;
LocType:Byte;
PatchOffset:LongInt;
PatchValue:LongInt;
Is32Bit:Boolean;
Next:PBackPatchItem;
End;
PAliasItem=^TAliasItem;
TAliasItem=Record
AliasName:String[64];
SubstituteName:String[64];
Next:PAliasItem;
End;
TObjRecBuffer=Array[0..8191] of Byte;
TStr32=String[32];
TStr64=String[64];
TStr128=String[128];
TCmdObjNameArray=Array[1..MAX_CMD_OBJS] of String[128];
PCmdObjNameArray=^TCmdObjNameArray;
TCmdLibNameArray=Array[1..MAX_CMD_LIBS] of String[128];
PCmdLibNameArray=^TCmdLibNameArray;
Var
ObjFile,ExeFile,MapFile:File;
ExeHeader:TExeHeader;
SegmentHead,SegmentTail:PSegmentItem;
SymbolHead,SymbolTail:PSymbolItem;
RelocationHead,RelocationTail:PRelocationItem;
GroupHead,GroupTail:PGroupItem;
LNameHead,LNameTail:PLNameItem;
LineInfoHead,LineInfoTail:PLineInfoItem;
CommentHead,CommentTail:PCommentItem;
OffsetPatchHead,OffsetPatchTail:POffsetPatchItem;
BackPatchHead,BackPatchTail:PBackPatchItem;
AliasHead,AliasTail:PAliasItem;
SegmentCount,SymbolCount,RelocationCount:Integer;
GroupCount:Integer;
LNameCount,LineInfoCount:Integer;
CommentCount:Integer;
OffsetPatchCount:Integer;
BackPatchCount:Integer;
AliasCount:Integer;
HasEntryPoint:Boolean;
EntrySegIndex:Word;
EntryOffset:LongInt;
StackSize:Word;
ObjModuleName:String[64];
ObjFileName,ExeFileName,MapFileName:String; { Ajout des variables }
ObjInputCount,LibInputCount:Integer;
PrimaryObjInputCount:Integer;
ObjInputList:PCmdObjNameArray;
ObjSegBaseList:Array[1..MAX_CMD_OBJS] of Integer;
LibInputList:PCmdLibNameArray;
TempLibObjCount:Integer;
TempLibObjList:PCmdObjNameArray;
LoadedLibModuleCount:Integer;
LoadedLibModuleLibIndex:Array[1..MAX_LIB_TRACKED_MODULES] of Integer;
LoadedLibModuleStart:Array[1..MAX_LIB_TRACKED_MODULES] of LongInt;
LoadedLibModuleName:Array[1..MAX_LIB_TRACKED_MODULES] of String[64];
LibModulesScannedCount:LongInt;
LibModulesLoadedCount:LongInt;
LibResolutionPasses:Integer;
TraceLibRequested:Boolean;
TraceLibSymbolsRequested:Boolean;
TraceLibNameFilter:String[128];
TraceSymNameFilter:String[32];
MapRequested:Boolean;
LineNumbersRequested:Boolean;
PublicsMapRequested:Boolean;
NoDefaultLibrarySearch:Boolean;
NoGroupAssociation:Boolean;
NoIgnoreCase:Boolean;
DosSegOrdering:Boolean;
DsAllocateTop:Boolean;
ExePackRequested:Boolean;
HighLoadRequested:Boolean;
PauseRequested:Boolean;
OverlayInterruptSet:Boolean;
OverlayInterruptValue:Byte;
MaxAllocParagraphs:Word;
MaxSegmentsOption:Integer;
SpecField:Integer;
SpecTerminated:Boolean;
SpecCurrentItem:String[128];
ResponseDepth:Integer;
i:Integer;
Param:string;
Buffer:Array[0..1023] of Byte; { Ajout du tampon manquant }
Procedure InitOffsetPatchList;
Begin
OffsetPatchHead:=Nil;
OffsetPatchTail:=Nil;
OffsetPatchCount:=0;
End;
Procedure AddOffsetPatch(Const Pos:LongInt; Const Delta:Integer);
Var item:POffsetPatchItem;
Begin
If OffsetPatchCount>=MAX_RELOCATIONS Then Begin
WriteLn('Erreur : Trop de patchs OFFSET');
Halt(15);
End;
New(item);
item^.Pos:=Pos;
item^.Delta:=Delta;
item^.Next:=Nil;
If OffsetPatchHead=Nil Then OffsetPatchHead:=item
Else OffsetPatchTail^.Next:=item;
OffsetPatchTail:=item;
Inc(OffsetPatchCount);
End;
Procedure AddBackPatch(Const SegIdx:Word; Const LocTyp:Byte;
Const OfsVal,PatchVal:LongInt; Is32Rec:Boolean);
Var item:PBackPatchItem;
Begin
If BackPatchCount>=MAX_RELOCATIONS Then Begin
WriteLn('Erreur : Trop d''entrees BAKPAT');
Halt(18);
End;
New(item);
item^.SegIndex:=SegIdx;
item^.LocType:=LocTyp;
item^.PatchOffset:=OfsVal;
item^.PatchValue:=PatchVal;
item^.Is32Bit:=Is32Rec;
item^.Next:=Nil;
If BackPatchHead=Nil Then BackPatchHead:=item
Else BackPatchTail^.Next:=item;
BackPatchTail:=item;
Inc(BackPatchCount);
End;
Procedure AddAlias(Const AliasNm,SubNm:String);
Var
item,scan:PAliasItem;
Begin
scan:=AliasHead;
While scan<>Nil do Begin
If scan^.AliasName=Copy(AliasNm,1,64) Then Begin
scan^.SubstituteName:=Copy(SubNm,1,64);
Exit;
End;
scan:=scan^.Next;
End;
If AliasCount>=MAX_ALIASES Then Begin
WriteLn('Erreur : Trop de definitions ALIAS');
Halt(19);
End;
New(item);
item^.AliasName:=Copy(AliasNm,1,64);
item^.SubstituteName:=Copy(SubNm,1,64);
item^.Next:=Nil;
If AliasHead=Nil Then AliasHead:=item
Else AliasTail^.Next:=item;
AliasTail:=item;
Inc(AliasCount);
End;
Function ResolveAliasName(Const Nm:String):String;
Var
curName:String[64];
aliasItem:PAliasItem;
loopCount:Integer;
changed:Boolean;
Begin
curName:=Copy(Nm,1,64);
For loopCount:=1 to 16 do Begin
changed:=False;
aliasItem:=AliasHead;
While aliasItem<>Nil do Begin
If aliasItem^.AliasName=curName Then Begin
If aliasItem^.SubstituteName='' Then Begin
ResolveAliasName:=curName;
Exit;
End;
curName:=aliasItem^.SubstituteName;
changed:=True;
Break;
End;
aliasItem:=aliasItem^.Next;
End;
If Not changed Then Break;
End;
ResolveAliasName:=curName;
End;
Procedure FreeOffsetPatchList;
Var item,nextItem:POffsetPatchItem;
Begin
item:=OffsetPatchHead;
While item<>Nil do Begin
nextItem:=item^.Next;
Dispose(item);
item:=nextItem;
End;
OffsetPatchHead:=Nil;
OffsetPatchTail:=Nil;
OffsetPatchCount:=0;
End;
Procedure InitMainLists;
Begin
SegmentHead:=Nil;
SegmentTail:=Nil;
SymbolHead:=Nil;
SymbolTail:=Nil;
RelocationHead:=Nil;
RelocationTail:=Nil;
GroupHead:=Nil;
GroupTail:=Nil;
LNameHead:=Nil;
LNameTail:=Nil;
LineInfoHead:=Nil;
LineInfoTail:=Nil;
CommentHead:=Nil;
CommentTail:=Nil;
BackPatchHead:=Nil;
BackPatchTail:=Nil;
AliasHead:=Nil;
AliasTail:=Nil;
SegmentCount:=0;
SymbolCount:=0;
RelocationCount:=0;
GroupCount:=0;
LNameCount:=0;
LineInfoCount:=0;
CommentCount:=0;
BackPatchCount:=0;
AliasCount:=0;
End;
Procedure FreeMainLists;
Var
segItem,nextSeg:PSegmentItem;
symItem,nextSym:PSymbolItem;
relItem,nextRel:PRelocationItem;
grpItem,nextGrp:PGroupItem;
memItem,nextMem:PGroupMemberItem;
nameItem,nextName:PLNameItem;
lineItem,nextLine:PLineInfoItem;
comItem,nextCom:PCommentItem;
bakItem,nextBak:PBackPatchItem;
aliasItem,nextAlias:PAliasItem;
Begin
segItem:=SegmentHead;
While segItem<>Nil do Begin
nextSeg:=segItem^.Next;
Dispose(segItem);
segItem:=nextSeg;
End;
symItem:=SymbolHead;
While symItem<>Nil do Begin
nextSym:=symItem^.Next;
Dispose(symItem);
symItem:=nextSym;
End;
relItem:=RelocationHead;
While relItem<>Nil do Begin
nextRel:=relItem^.Next;
Dispose(relItem);
relItem:=nextRel;
End;
grpItem:=GroupHead;
While grpItem<>Nil do Begin
memItem:=grpItem^.MemberHead;
While memItem<>Nil do Begin
nextMem:=memItem^.Next;
Dispose(memItem);
memItem:=nextMem;
End;
nextGrp:=grpItem^.Next;
Dispose(grpItem);
grpItem:=nextGrp;
End;
nameItem:=LNameHead;
While nameItem<>Nil do Begin
nextName:=nameItem^.Next;
Dispose(nameItem);
nameItem:=nextName;
End;
lineItem:=LineInfoHead;
While lineItem<>Nil do Begin
nextLine:=lineItem^.Next;
Dispose(lineItem);
lineItem:=nextLine;
End;
comItem:=CommentHead;
While comItem<>Nil do Begin
nextCom:=comItem^.Next;
Dispose(comItem);
comItem:=nextCom;
End;
bakItem:=BackPatchHead;
While bakItem<>Nil do Begin
nextBak:=bakItem^.Next;
Dispose(bakItem);
bakItem:=nextBak;
End;
aliasItem:=AliasHead;
While aliasItem<>Nil do Begin
nextAlias:=aliasItem^.Next;
Dispose(aliasItem);
aliasItem:=nextAlias;
End;
InitMainLists;
End;
Procedure ApplyAliasToExternalSymbols;
Var
SymItem:PSymbolItem;
Begin
SymItem:=SymbolHead;
While SymItem<>Nil do Begin
If SymItem^.Value.IsExternal Then
SymItem^.Value.Name:=Copy(ResolveAliasName(SymItem^.Value.Name),1,32);
SymItem:=SymItem^.Next;
End;
End;
Procedure RecomputeSegmentAddresses;
Type
TSegOrderItem=Record
SegPtr:PSegmentItem;
Priority:Integer;
OrigIndex:Integer;
ClassName:String[8];
SegName:String[16];
End;
Var
segItem:PSegmentItem;
currentBase:LongInt;
segCount,a,b:Integer;
orderArr:Array[1..MAX_SEGMENTS] of TSegOrderItem;
tmp:TSegOrderItem;
Function UpStrLocal(Const S:String):String;
Var
r:String;
k:Integer;
Begin
r:=S;
For k:=1 to Length(r) do r[k]:=UpCase(r[k]);
UpStrLocal:=r;
End;
Function DosSegPriority(Const ClassName,SegName:String):Integer;
Var
cls,nm:String;
isCode,isData,isBss,isStack:Boolean;
Begin
cls:=UpStrLocal(ClassName);
nm:=UpStrLocal(SegName);
isCode:=(Pos('CODE',cls)>0)Or(Pos('CODE',nm)>0)Or(Pos('_TEXT',nm)>0);
isData:=(Pos('DATA',cls)>0)Or(Pos('DATA',nm)>0);
isBss:=(Pos('BSS',cls)>0)Or(Pos('BSS',nm)>0);
isStack:=(Pos('STACK',cls)>0)Or(Pos('STACK',nm)>0);
If isCode Then Begin
DosSegPriority:=10;
Exit;
End;
If isStack Then Begin
DosSegPriority:=90;
Exit;
End;
If DsAllocateTop Then Begin
If isData Then DosSegPriority:=70
Else If isBss Then DosSegPriority:=80
Else DosSegPriority:=50;
Exit;
End;
If isData Then DosSegPriority:=20
Else If isBss Then DosSegPriority:=30
Else DosSegPriority:=50;
End;
Begin
currentBase:=0;
If (Not DosSegOrdering)Or(SegmentCount<2) Then Begin
segItem:=SegmentHead;
While segItem<>Nil do Begin
segItem^.Value.Address:=currentBase;
Inc(currentBase,segItem^.Value.Size);
segItem:=segItem^.Next;
End;
Exit;
End;
segCount:=0;
segItem:=SegmentHead;
While segItem<>Nil do Begin
If segCount<MAX_SEGMENTS Then Begin
Inc(segCount);
orderArr[segCount].SegPtr:=segItem;
orderArr[segCount].Priority:=DosSegPriority(segItem^.Value.Class,segItem^.Value.Name);
orderArr[segCount].OrigIndex:=segCount;
orderArr[segCount].ClassName:=segItem^.Value.Class;
orderArr[segCount].SegName:=segItem^.Value.Name;
End;
segItem:=segItem^.Next;
End;
For a:=1 to segCount-1 do
For b:=a+1 to segCount do
If (orderArr[a].Priority>orderArr[b].Priority)
Or((orderArr[a].Priority=orderArr[b].Priority)And(orderArr[a].ClassName>orderArr[b].ClassName))
Or((orderArr[a].Priority=orderArr[b].Priority)And(orderArr[a].ClassName=orderArr[b].ClassName)
And(orderArr[a].SegName>orderArr[b].SegName))
Or((orderArr[a].Priority=orderArr[b].Priority)And(orderArr[a].ClassName=orderArr[b].ClassName)
And(orderArr[a].SegName=orderArr[b].SegName)
And(orderArr[a].OrigIndex>orderArr[b].OrigIndex)) Then Begin
tmp:=orderArr[a];
orderArr[a]:=orderArr[b];
orderArr[b]:=tmp;
End;
For a:=1 to segCount do Begin
orderArr[a].SegPtr^.Value.Address:=currentBase;
Inc(currentBase,orderArr[a].SegPtr^.Value.Size);
End;
End;
Function NormalizeSymbolName(Const Nm:String):String;
Var
canon:String;
k:Integer;
Begin
canon:=Copy(ResolveAliasName(Copy(Nm,1,64)),1,32);
If Not NoIgnoreCase Then
For k:=1 to Length(canon) do canon[k]:=UpCase(canon[k]);
NormalizeSymbolName:=canon;
End;
Function IsPublicSymbolDefined(Const Nm:String):Boolean;
Var
symItem:PSymbolItem;
lookupName:String[32];
Begin
IsPublicSymbolDefined:=False;
lookupName:=NormalizeSymbolName(Nm);
If lookupName='' Then Exit;
symItem:=SymbolHead;
While symItem<>Nil do Begin
If Not symItem^.Value.IsExternal Then
If NormalizeSymbolName(symItem^.Value.Name)=lookupName Then Begin
IsPublicSymbolDefined:=True;
Exit;
End;
symItem:=symItem^.Next;
End;
End;
Function IsExternalNameUnresolved(Const Nm:String):Boolean;
Var
symItem:PSymbolItem;
lookupName:String[32];
Begin
IsExternalNameUnresolved:=False;
lookupName:=NormalizeSymbolName(Nm);
If lookupName='' Then Exit;
If IsPublicSymbolDefined(lookupName) Then Exit;
symItem:=SymbolHead;
While symItem<>Nil do Begin
If symItem^.Value.IsExternal Then
If NormalizeSymbolName(symItem^.Value.Name)=lookupName Then Begin
IsExternalNameUnresolved:=True;
Exit;
End;
symItem:=symItem^.Next;
End;
End;
Function CountExternalReferences:Integer;
Var
symItem:PSymbolItem;
count:Integer;
Begin
count:=0;
symItem:=SymbolHead;
While symItem<>Nil do Begin
If symItem^.Value.IsExternal Then Inc(count);
symItem:=symItem^.Next;
End;
CountExternalReferences:=count;
End;
Function CountPublicDefinitions:Integer;
Var
symItem:PSymbolItem;
count:Integer;
Begin
count:=0;
symItem:=SymbolHead;
While symItem<>Nil do Begin
If (Not symItem^.Value.IsExternal)And(symItem^.Value.Segment>0) Then
Inc(count);
symItem:=symItem^.Next;
End;
CountPublicDefinitions:=count;
End;
Function CountUnresolvedExternalNames:Integer;
Var
symItem:PSymbolItem;
names:Array[1..MAX_SYMBOLS] of String[32];
count,k:Integer;
nm:String[32];
seen:Boolean;
Begin
count:=0;
symItem:=SymbolHead;
While symItem<>Nil do Begin
If symItem^.Value.IsExternal Then Begin
nm:=NormalizeSymbolName(symItem^.Value.Name);
If (nm<>'')And IsExternalNameUnresolved(nm) Then Begin
seen:=False;
For k:=1 to count do
If names[k]=nm Then Begin
seen:=True;
Break;
End;
If Not seen Then Begin
If count<MAX_SYMBOLS Then Begin
Inc(count);
names[count]:=nm;
End;
End;
End;
End;
symItem:=symItem^.Next;
End;
CountUnresolvedExternalNames:=count;
End;
Function TraceLibraryMatches(Const LibName:String):Boolean;
Var
u:String;
k:Integer;
Begin
u:=LibName;
For k:=1 to Length(u) do u[k]:=UpCase(u[k]);
If TraceLibNameFilter='' Then TraceLibraryMatches:=True
Else TraceLibraryMatches:=Pos(TraceLibNameFilter,u)>0;
End;
Function TraceSymbolMatches(Const SymbolName:String):Boolean;
Var
u:String;
k:Integer;
Begin
u:=SymbolName;
For k:=1 to Length(u) do u[k]:=UpCase(u[k]);
If TraceSymNameFilter='' Then TraceSymbolMatches:=True
Else TraceSymbolMatches:=Pos(TraceSymNameFilter,u)>0;
End;
Procedure TraceUnresolvedExternalNames(Const Prefix:String; Const MaxItems:Integer);
Var
symItem:PSymbolItem;
names:Array[1..MAX_SYMBOLS] of String[32];
count,k,shown,limit:Integer;
nm:String[32];
seen:Boolean;
Begin
If Not TraceLibRequested Then Exit;
count:=0;
symItem:=SymbolHead;
While symItem<>Nil do Begin
If symItem^.Value.IsExternal Then Begin
nm:=NormalizeSymbolName(symItem^.Value.Name);
If (nm<>'')And IsExternalNameUnresolved(nm)And TraceSymbolMatches(nm) Then Begin
seen:=False;
For k:=1 to count do
If names[k]=nm Then Begin
seen:=True;
Break;
End;
If (Not seen)And(count<MAX_SYMBOLS) Then Begin
Inc(count);
names[count]:=nm;
End;
End;
End;
symItem:=symItem^.Next;
End;
WriteLn('TRACE LIB: ',Prefix,' : ',count,' externe(s) non resolu(s)');
If Not TraceLibSymbolsRequested Then Exit;
limit:=MaxItems;
If limit<1 Then limit:=1;
shown:=0;
For k:=1 to count do Begin
Inc(shown);
If shown>limit Then Begin
WriteLn('TRACE LIB: ... (',count-limit,' nom(s) supplementaire(s))');
Break;
End;
WriteLn('TRACE LIB: - ',names[k]);
End;
End;
Procedure AddTempLibObjFile(Const FileName:String);
Begin
If TempLibObjCount<MAX_CMD_OBJS Then Begin
Inc(TempLibObjCount);
TempLibObjList^[TempLibObjCount]:=Copy(FileName,1,128);
End;
End;
Procedure CleanupTempLibObjFiles;
Var
f:File;
n:Integer;
Begin
For n:=1 to TempLibObjCount do Begin
If TempLibObjList^[n]<>'' Then Begin
{$I-}
Assign(f,TempLibObjList^[n]);
Erase(f);
If(IOResult<>0)Then;
{$I+}
End;
End;
TempLibObjCount:=0;
End;
Function IsLibModuleAlreadyLoaded(Const LibIdx:Integer; Const StartPos:LongInt):Boolean;
Var
n:Integer;
Begin
IsLibModuleAlreadyLoaded:=False;
For n:=1 to LoadedLibModuleCount do
If (LoadedLibModuleLibIndex[n]=LibIdx)And(LoadedLibModuleStart[n]=StartPos) Then Begin
IsLibModuleAlreadyLoaded:=True;
Exit;
End;
End;
Procedure MarkLibModuleLoaded(Const LibIdx:Integer; Const StartPos:LongInt; Const ModuleName:String);
Begin
If LoadedLibModuleCount<MAX_LIB_TRACKED_MODULES Then Begin
Inc(LoadedLibModuleCount);
LoadedLibModuleLibIndex[LoadedLibModuleCount]:=LibIdx;
LoadedLibModuleStart[LoadedLibModuleCount]:=StartPos;
LoadedLibModuleName[LoadedLibModuleCount]:=Copy(ModuleName,1,64);
End;
End;
Function AddSegment(Const Seg:TSegment):Integer;
Var item:PSegmentItem;
Begin
If SegmentCount>=MaxSegmentsOption Then Begin
WriteLn('Erreur : Trop de segments dans le fichier objet');
Halt(11);
End;
New(item);
item^.Value:=Seg;
item^.Next:=Nil;
If SegmentHead=Nil Then SegmentHead:=item
Else SegmentTail^.Next:=item;
SegmentTail:=item;
Inc(SegmentCount);
AddSegment:=SegmentCount;
End;
Procedure AddSymbol(Const Sym:TSymbol);
Var item:PSymbolItem;
Begin
New(item);
item^.Value:=Sym;
item^.Next:=Nil;
If SymbolHead=Nil Then SymbolHead:=item
Else SymbolTail^.Next:=item;
SymbolTail:=item;
Inc(SymbolCount);
End;
Function AddGroup(Const NameIdx:Word; Is32Rec:Boolean):PGroupItem;
Var item:PGroupItem;
Begin
If GroupCount>=MAX_GROUPS Then Begin
WriteLn('Erreur : Trop de groupes OMF');
Halt(16);
End;
New(item);
item^.NameIndex:=NameIdx;
item^.Is32Bit:=Is32Rec;
item^.MemberHead:=Nil;
item^.MemberTail:=Nil;
item^.MemberCount:=0;
item^.Next:=Nil;
If GroupHead=Nil Then GroupHead:=item
Else GroupTail^.Next:=item;
GroupTail:=item;
Inc(GroupCount);
AddGroup:=item;
End;
Procedure AddGroupMember(GroupItem:PGroupItem; SegIdx:Word);
Var mem:PGroupMemberItem;
Begin
If GroupItem=Nil Then Exit;
New(mem);
mem^.SegIndex:=SegIdx;
mem^.Next:=Nil;
If GroupItem^.MemberHead=Nil Then GroupItem^.MemberHead:=mem
Else GroupItem^.MemberTail^.Next:=mem;
GroupItem^.MemberTail:=mem;
Inc(GroupItem^.MemberCount);
End;
Procedure AddLName(Const Nm:String);
Var item:PLNameItem;
Begin
If LNameCount>=MAX_LNAMES Then Begin
WriteLn('Erreur : Trop de noms LNAMES');
Halt(17);
End;
New(item);
item^.Value:=Copy(Nm,1,64);
item^.Next:=Nil;
If LNameHead=Nil Then LNameHead:=item
Else LNameTail^.Next:=item;
LNameTail:=item;
Inc(LNameCount);
End;
Function GetLName(Index:Word):String;
Var
item:PLNameItem;
n:Word;
Begin
GetLName:='';
If (Index=0)Or(Index>LNameCount) Then Exit;
item:=LNameHead;
n:=1;
While(item<>Nil)And(n<Index)do Begin
item:=item^.Next;
Inc(n);
End;
If item<>Nil Then GetLName:=item^.Value;
End;
Procedure AddLineInfo(Const GrpIdx,SegIdx:Word; Const LineNum,CodeOfs:LongInt; Is32Rec:Boolean);
Var item:PLineInfoItem;
Begin
If LineInfoCount>=MAX_LINENUMS Then Exit;
New(item);
If NoGroupAssociation Then item^.GroupIndex:=0
Else item^.GroupIndex:=GrpIdx;
item^.SegIndex:=SegIdx;
item^.LineNo:=LineNum;
item^.CodeOffset:=CodeOfs;
item^.Is32Bit:=Is32Rec;
item^.Next:=Nil;
If LineInfoHead=Nil Then LineInfoHead:=item
Else LineInfoTail^.Next:=item;
LineInfoTail:=item;
Inc(LineInfoCount);
End;
Procedure AddComment(Const Flags,ClassCode:Byte; Const Txt:String);
Var item:PCommentItem;
Begin
If CommentCount>=MAX_COMMENTS Then Exit;
New(item);
item^.Flags:=Flags;
item^.ClassCode:=ClassCode;
item^.Text:=Copy(Txt,1,80);
item^.Next:=Nil;
If CommentHead=Nil Then CommentHead:=item
Else CommentTail^.Next:=item;
CommentTail:=item;
Inc(CommentCount);
End;
Function GetSegmentPtr(Index:Integer):PSegment;
Var
item:PSegmentItem;
n:Integer;
Begin
GetSegmentPtr:=Nil;
If (Index<=0)Or(Index>SegmentCount) Then Exit;
item:=SegmentHead;
n:=1;
While(item<>Nil)And(n<Index)do Begin
item:=item^.Next;
Inc(n);
End;
If item<>Nil Then GetSegmentPtr:=@item^.Value;
End;
Procedure AddRelocationEntry(Const OffsetVal,SegmentVal,TargetVal:Word);
Var
item:PRelocationItem;
rel:TRelocation;
Begin
If RelocationCount>=MAX_RELOCATIONS Then Begin
WriteLn('Erreur : Trop de relocations');
Halt(14);
End;
rel.Offset:=OffsetVal;
rel.Segment:=SegmentVal;
rel.Target:=TargetVal;
New(item);
item^.Value:=rel;
item^.Next:=Nil;
If RelocationHead=Nil Then RelocationHead:=item
Else RelocationTail^.Next:=item;
RelocationTail:=item;
Inc(RelocationCount);
End;
Function SkipIterBlock16(Var RecBuf:TObjRecBuffer; Var Pos:Integer; Limit:Integer; Var Generated:LongInt):Boolean;
Var
RepeatCount,BlockCount:Word;
DataCount:Byte;
i:Integer;
SubLen,AddLen:LongInt;
Begin
Generated:=0;
SkipIterBlock16:=False;
If Pos+3>=Limit Then Exit;
RepeatCount:=RecBuf[Pos]+Word(RecBuf[Pos+1])*256;
Inc(Pos,2);
BlockCount:=RecBuf[Pos]+Word(RecBuf[Pos+1])*256;
Inc(Pos,2);
If BlockCount=0 Then Begin
If Pos>=Limit Then Exit;
DataCount:=RecBuf[Pos];
Inc(Pos);
If Pos+DataCount>Limit Then Exit;
Inc(Pos,DataCount);
Generated:=LongInt(RepeatCount)*LongInt(DataCount);
SkipIterBlock16:=True;
Exit;
End;
For i:=1 to BlockCount do Begin
If Not SkipIterBlock16(RecBuf,Pos,Limit,SubLen) Then Exit;
AddLen:=LongInt(RepeatCount)*SubLen;