summaryrefslogblamecommitdiffstats
path: root/private/mvdm/bde/bdesym.c
blob: a0b76e1322171aeb591a652237c8bdf3d0346ed6 (plain) (tree)
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






















































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                 
#include <precomp.h>
#pragma hdrstop

#define MYOF_FLAGS (OF_READ | OF_SHARE_DENY_NONE)

int read_name(
    int     ifile,
    LPSTR   pch
) {
    char    length;
    int     rc;

    rc = _lread( ifile, &length, sizeof(length) );
    if ( rc != sizeof(length) ) {
        PRINTF("Could not read name length\n");
        *pch = '\0';
        return( -1 );
    }
    rc = _lread( ifile, pch, length );
    if ( rc != length ) {
        PRINTF("Could not read name\n");
        *pch = '\0';
        return( -1 );
    }
    *(pch + length) = '\0';
    return( (int)length );
}

BOOL FindExport(
    LPSTR           filename,
    WORD            segment,
    WORD            offset,
    LPSTR           sym_text,
    int             direction,
    LONG            *dist
) {
    int     iFile;
    OFSTRUCT    ofs;
    int     rc;
    IMAGE_DOS_HEADER doshdr;
    IMAGE_OS2_HEADER winhdr;
    BYTE    Table[65536];
    BYTE    bBundles;
    BYTE    bFlags;
    BYTE    *ptr;
    WORD    wIndex = 1;
    int     i;
    int     this_dist;
    int     wIndexBest = -1;
    char    myfilename[256];
#pragma pack(1)
    typedef struct
    {
    BYTE        bFlags;
    UNALIGNED WORD  wSegOffset;
    } FENTRY, *PFENTRY;

    typedef struct
    {
    BYTE        bFlags;
    UNALIGNED WORD  wINT3F;
    BYTE        bSegNumber;
    UNALIGNED WORD  wSegOffset;
    } MENTRY, *PMENTRY;
#pragma pack()

    strcpy(myfilename, filename);
    if (-1 == (iFile=OpenFile(myfilename, &ofs, MYOF_FLAGS))) {

        PRINTF("Error reading file %s\n", filename);
        strcpy(myfilename, filename);
        strcat(myfilename, ".exe");

        if (-1 == (iFile=OpenFile(myfilename, &ofs, MYOF_FLAGS))) {

            PRINTF("Error reading file %s\n", myfilename);
            strcpy(myfilename, filename);
            strcat(myfilename, ".dll");

            if (-1 == (iFile=OpenFile(myfilename, &ofs, MYOF_FLAGS))) {
                PRINTF("Error reading file %s\n", myfilename);
                return FALSE;
            }
        }
    }

    rc = _lread(iFile, &doshdr, sizeof(doshdr));
    if (rc != sizeof(doshdr)) {
    //PRINTF("Error reading DOS header\n");
    goto Error;
    }
    if (doshdr.e_magic != IMAGE_DOS_SIGNATURE) {
    //PRINTF("Error - no DOS EXE signature");
    goto Error;
    }
    rc = _llseek(iFile, doshdr.e_lfanew, FILE_BEGIN);
    if (rc == -1) {
    //PRINTF("Error - could not seek - probably not Win3.1 exe\n");
    goto Error;
    }
    rc = _lread(iFile, &winhdr, sizeof(winhdr));
    if (rc != sizeof(winhdr)) {
    //PRINTF("Error - could not read WIN header - probably not Win3.1 exe\n");
    goto Error;
    }
    if (winhdr.ne_magic != IMAGE_OS2_SIGNATURE) {
    //PRINTF("Error - not WIN EXE signature\n");
    goto Error;
    }
    rc = _llseek(iFile, doshdr.e_lfanew+winhdr.ne_enttab, FILE_BEGIN);
    if (rc == -1) {
    //PRINTF("Error - could not seek to entry table\n");
    goto Error;
    }
    rc = _lread(iFile, Table, winhdr.ne_cbenttab);
    if (rc != winhdr.ne_cbenttab) {
    //PRINTF("Error - could not read entry table\n");
    goto Error;
    }
    ptr = Table;
    while (TRUE) {
        bBundles = *ptr++;
        if (bBundles == 0)
            break;

        bFlags = *ptr++;
        switch (bFlags) {
            case 0: // Placeholders
            wIndex += bBundles;
            break;

            case 0xff:  // movable segments
            for (i=0; i<(int)bBundles; ++i) {
                PMENTRY pe = (PMENTRY )ptr;
                if (pe->bSegNumber == segment) {
                this_dist = (direction == BEFORE) ? offset - pe->wSegOffset
                                  : pe->wSegOffset - offset;
                if ( this_dist >= 0 && (this_dist < *dist || *dist == -1) ) {
                    // mark this as the best match so far
                    *dist = this_dist;
                    wIndexBest = wIndex;
                }
                }
                ptr += sizeof(MENTRY);
                wIndex++;
            }
            break;

            default:    // fixed segments
            if ((int)bFlags != segment) {
                ptr += (int)bBundles * sizeof(FENTRY);
                wIndex += (int)bBundles;
            } else {
                for (i=0; i<(int)bBundles; ++i) {
                PFENTRY pe = (PFENTRY)ptr;
                this_dist = (direction == BEFORE) ? offset - pe->wSegOffset
                                  : pe->wSegOffset - offset;
                if ( this_dist >= 0 && (this_dist < *dist || *dist == -1) ) {
                    // mark this as the best match so far
                    *dist = this_dist;
                    wIndexBest = wIndex;
                }
                ptr += sizeof(FENTRY);
                wIndex++;
                }
            }
            break;
        }
    }
    if (wIndexBest == -1) {
    // no match found - error out
Error:
    _lclose(iFile);
    return FALSE;
    }

    // Success: match found
    // wIndexBest = ordinal of the function
    // segment:offset = address to look up
    // *dist = distance from segment:offset to the symbol
    // filename = name of .exe/.dll

    // Look for the ordinal in the resident name table
    rc = _llseek(iFile, doshdr.e_lfanew+winhdr.ne_restab, FILE_BEGIN);
    if (rc == -1) {
    //PRINTF("Error - unable to seek to residentname table\n");
    goto Error;
    }
    rc = _lread(iFile, Table, winhdr.ne_modtab-winhdr.ne_restab);
    if (rc != winhdr.ne_modtab-winhdr.ne_restab) {
    //PRINTF("Error - unable to read entire resident name table\n");
    goto Error;
    }
    ptr = Table;
    while (*ptr) {
    if ( *(UNALIGNED USHORT *)(ptr+1+*ptr) == (USHORT)wIndexBest) {
        // found the matching name
        *(ptr+1+*ptr) = '\0';   // null-terminate the function name
        wsprintf(sym_text, "%s", ptr+1);
        goto Finished;
    }
    ptr += *ptr + 3;
    }

    // Look for the ordinal in the non-resident name table
    rc = _llseek(iFile, doshdr.e_lfanew+winhdr.ne_nrestab, FILE_BEGIN);
    if (rc == -1) {
    //PRINTF("Error - unable to seek to non-residentname table\n");
    goto Error;
    }
    rc = _lread(iFile, Table, winhdr.ne_cbnrestab);
    if (rc != winhdr.ne_cbnrestab) {
    //PRINTF("Error - unable to read entire non-resident name table\n");
    goto Error;
    }
    ptr = Table;
    while (*ptr) {
    if ( *(UNALIGNED USHORT *)(ptr+1+*ptr) == (USHORT)wIndexBest) {
        // found the matching name
        *(ptr+1+*ptr) = '\0';   // null-terminate the function name
        wsprintf(sym_text, "%s", ptr+1);
        goto Finished;
    }
    ptr += *ptr + 3;
    }
    // fall into error path - no match found
    goto Error;

Finished:
    _lclose(iFile);
    return TRUE;
}


int read_symbol(
    int     ifile,
    LPSTR   pch,
    LONG    *offset
) {
    int     rc;
    WORD    word;

    rc = _lread( ifile, (LPSTR)&word, sizeof(WORD) );
    if ( rc != sizeof(WORD) ) {
        PRINTF("Could not read symbol offset\n");
        *pch = '\0';
        *offset = 0L;
        return(-1);
    }
    *offset = (LONG)word;
    rc = read_name( ifile, pch );
    return( rc );
}

BOOL FindSymbol(
    WORD        selector,
    LONG        offset,
    LPSTR       sym_text,
    LONG        *dist,
    int         direction,
    int         mode
) {
    BOOL        result;
    int         length;
    int         iFile;
    char        filename[256];
    OFSTRUCT    ofs;
    LONG        filesize;
    LONG        start_position;
    LONG        position;
    WORD        w1;
    WORD        num_syms;
    WORD        w3;
    WORD        w4;
    WORD        next_off;
#ifdef NEED_INDICES
    WORD        index;
    char        c2;
    int         nSubEtry;
#endif
    char        c1;
    int         rc;
    int         cnt;
    LONG        this_offset;
    WORD        r2;
    WORD        seg_num;
    char        b[12];
    char        name_buff[128];
    LONG        this_dist;
    HEAPENTRY   he = {0};

    *dist = -1;
    strcpy( sym_text, "[Unknown]" );
    result = FALSE;

    if ( mode == V86_MODE ) {
        return FALSE;
//        v86_addr = ((ULONG)selector << 4) + (ULONG)offset;
    }

    /*
    ** Search for selector in kernel heap
    */
    he.Selector = selector;
    while (FindHeapEntry(&he, FALSE)) {
        {
            strcpy(filename, he.FileName);
            strcat(filename,".sym");

            iFile = OpenFile( filename, &ofs, MYOF_FLAGS );

            if ( iFile == -1 ) {
                PRINTF("Could not open symbol file \"%s\"\n", filename );

                // Open the .EXE/.DLL file and see if the address corresponds
                // to an exported function.
                result = FindExport(he.FileName,(WORD)(he.SegmentNumber+1),(WORD)offset,sym_text,direction,dist);
                if (!result) {
                    // not found in any .dll/.exe's export table, so just print file name,
                    // segment number, and offset
                    wsprintf(sym_text,"%s(%X):%04X",he.OwnerName, he.SegmentNumber,offset);
                    *dist = 0;
                    result = TRUE;
                }
                continue;
            }

            rc = _lread( iFile, (LPSTR)&filesize, sizeof(filesize) );
            if ( rc != sizeof(filesize) ) {
                PRINTF("Could not read file size\n");
                _lclose( iFile );
                return( FALSE );
            }
            filesize <<= 4;

            rc = _lread( iFile, (LPSTR)&w1, sizeof(w1) );
            if ( rc != sizeof(w1) ) {
                PRINTF("Could not read w1\n");
                _lclose( iFile );
                return( FALSE );
            }

            rc = _lread( iFile, (LPSTR)&num_syms, sizeof(num_syms) );
            if ( rc != sizeof(num_syms) ) {
                PRINTF("Could not read num_syms\n");
                _lclose( iFile );
                return( FALSE );
            }
            rc = _lread( iFile, (LPSTR)&w3, sizeof(w3) );
            if ( rc != sizeof(w3) ) {
                PRINTF("Could not read w3\n");
                _lclose( iFile );
                return( FALSE );
            }
            rc = _lread( iFile, (LPSTR)&w4, sizeof(w4) );
            if ( rc != sizeof(w4) ) {
                PRINTF("Could not read w4\n");
                _lclose( iFile );
                return( FALSE );
            }
            rc = _lread( iFile, (LPSTR)&next_off, sizeof(next_off) );
            if ( rc != sizeof(next_off) ) {
                PRINTF("Could not read next_off\n");
                _lclose( iFile );
                return( FALSE );
            }
            start_position = ((LONG)next_off) << 4;

            rc = _lread( iFile, (LPSTR)&c1, sizeof(c1) );
            if ( rc != sizeof(c1) ) {
                PRINTF("Could not read c1\n");
                _lclose( iFile );
                return( FALSE );
            }

            read_name( iFile, name_buff );

#ifdef NEED_ABSOLUTES
#ifdef NEED_SYM4
            rc = _lread( iFile, (LPSTR)&c2, sizeof(c2) );
            if ( rc != sizeof(c2) ) {
                PRINTF("Could not read c2\n");
                _lclose( iFile );
                return( FALSE );
            }
#endif

            cnt = num_syms;
            while ( cnt ) {
                length = read_symbol( lpOutputRoutine, iFile, name_buff, &
                                      this_offset );
                if ( length == 0 ) {
                    PRINTF("Error access symbols in file %s\n", filename );
                    break;
                }
                --cnt;
            }
#ifdef NEED_INDICES
            cnt = num_syms;
            while ( cnt ) {
                rc = _lread( iFile, (LPSTR)&index, sizeof(index) );
                if ( rc != sizeof(index) ) {
                    PRINTF("Could not read index table entry\n");
                    _lclose( iFile );
                    return( FALSE );
                }
                PRINTF("Index: %04X\n", index );
                --cnt;
            }
#endif
#endif

            position = start_position;
            do {
                rc = _llseek( iFile, position, FILE_BEGIN );
                if ( rc == -1 ) {
                    PRINTF("Failed to seek to next record\n");
                    _lclose( iFile );
                    return( FALSE );
                }
                rc = _lread( iFile, (LPSTR)&next_off, sizeof(next_off) );
                if ( rc != sizeof(next_off) ) {
                    PRINTF("Could not read next_off\n");
                    _lclose( iFile );
                    return( FALSE );
                }
                position = ((LONG)next_off) << 4;

                rc = _lread( iFile, (LPSTR)&num_syms, sizeof(num_syms) );
                if ( rc != sizeof(num_syms) ) {
                    PRINTF("Could not read num_syms\n");
                    _lclose( iFile );
                    return( FALSE );
                }

                rc = _lread( iFile, (LPSTR)&r2, sizeof(r2) );
                if ( rc != sizeof(r2) ) {
                    PRINTF("Could not read r2\n");
                    _lclose( iFile );
                    return( FALSE );
                }

                rc = _lread( iFile, (LPSTR)&seg_num, sizeof(seg_num) );
                if ( rc != sizeof(seg_num) ) {
                    PRINTF("Could not read seg_num\n");
                    _lclose( iFile );
                    return( FALSE );
                }

                if ( mode == PROT_MODE && seg_num != (WORD)(he.SegmentNumber+1) ) {
                    /*
                    ** Skip reading of symbols for segments with the wrong seg_num
                    */
                    continue;
                }

                cnt = 0;
                while ( cnt < 12 ) {
                    rc = _lread( iFile, (LPSTR)&b[cnt], sizeof(b[0]) );
                    if ( rc != sizeof(b[0]) ) {
                        PRINTF("Could not read 12 byte b array\n");
                        _lclose( iFile );
                        return( FALSE );
                    }
                    cnt++;
                }
                read_name( iFile, name_buff );

                cnt = num_syms;
                while ( cnt ) {
                    length = read_symbol( iFile, name_buff,
                                          &this_offset );
                    if ( length == 0 ) {
                        PRINTF("Error access symbols in file %s\n", filename );
                        break;
                    }
                    switch( mode ) {
                        case PROT_MODE:
                            switch( direction ) {
                                case BEFORE:
                                    this_dist = offset - this_offset;
                                    break;
                                case AFTER:
                                    this_dist = this_offset - offset;
                                    break;
                            }
                            break;
                        case V86_MODE:
#if 0
                            mod_addr = mod_offset + (ULONG)(seg_num << 4) + this_offset;
                            switch( direction ) {
                                case BEFORE:
                                    this_dist = v86_addr - mod_addr;
                                    break;
                                case AFTER:
                                    this_dist = mod_addr - v86_addr;
                                    break;
                            }
#endif
                            break;
                    }
                    if ( this_dist >= 0 && (this_dist < *dist || *dist == -1) ) {
                        *dist = this_dist;
                        strcpy( sym_text, name_buff );
                        result = TRUE;
                    }
                    --cnt;
                }
#ifdef NEED_INDICES
                cnt = num_syms;
                while ( cnt ) {
                    rc = _lread( iFile, (LPSTR)&index, sizeof(index) );
                    if ( rc != sizeof(index) ) {
                        PRINTF("Could not read index table entry\n");
                        _lclose( iFile );
                        return( FALSE );
                    }
                    --cnt;
                }
#endif
            } while ( position != start_position && position != 0 );

            _lclose( iFile );
        }
    }
    return( result );
}

BOOL FindAddress(
    WORD        *selector,
    LONG        *offset,
    LPSTR       sym_text,
    int         mode
) {
    int         length;
    int         iFile;
    char        filename[256];
    OFSTRUCT    ofs;
    LONG        filesize;
    LONG        start_position;
    LONG        position;
    WORD        w1;
    WORD        num_syms;
    WORD        w3;
    WORD        w4;
    WORD        next_off;
#ifdef NEED_INDICES
    WORD        index;
#endif
    char        c1;
    char        c2;
    int         rc;
    int         cnt;
    LONG        this_offset;
    WORD        r2;
    WORD        seg_num;
    char        b[12];
    char        name_buff[128];
    char        name1[200];
    HEAPENTRY   he = {0};

    strcpy(name1,sym_text);
    _strupr(name1);

    if (mode == V86_MODE) { //BUGBUG
        return FALSE;
    }

    /*
    ** Search for selector in kernel heap
    */
    while (FindHeapEntry(&he, FALSE)) {
        {
            strcpy(filename, he.FileName);
            strcat(filename,".sym");

            iFile = OpenFile( filename, &ofs, MYOF_FLAGS );

            if ( iFile == -1 ) {
                // PRINTF("Could not open symbol file \"%s\"\n", filename );
                continue;
            }

            rc = _lread( iFile, (LPSTR)&filesize, sizeof(filesize) );
            if ( rc != sizeof(filesize) ) {
                PRINTF("Could not read file size\n");
                _lclose( iFile );
                return( FALSE );
            }
            filesize <<= 4;

            rc = _lread( iFile, (LPSTR)&w1, sizeof(w1) );
            if ( rc != sizeof(w1) ) {
                PRINTF("Could not read w1\n");
                _lclose( iFile );
                return( FALSE );
            }

            rc = _lread( iFile, (LPSTR)&num_syms, sizeof(num_syms) );
            if ( rc != sizeof(num_syms) ) {
                PRINTF("Could not read num_syms\n");
                _lclose( iFile );
                return( FALSE );
            }
            rc = _lread( iFile, (LPSTR)&w3, sizeof(w3) );
            if ( rc != sizeof(w3) ) {
                PRINTF("Could not read w3\n");
                _lclose( iFile );
                return( FALSE );
            }
            rc = _lread( iFile, (LPSTR)&w4, sizeof(w4) );
            if ( rc != sizeof(w4) ) {
                PRINTF("Could not read w4\n");
                _lclose( iFile );
                return( FALSE );
            }
            rc = _lread( iFile, (LPSTR)&next_off, sizeof(next_off) );
            if ( rc != sizeof(next_off) ) {
                PRINTF("Could not read next_off\n");
                _lclose( iFile );
                return( FALSE );
            }
            start_position = ((LONG)next_off) << 4;

            rc = _lread( iFile, (LPSTR)&c1, sizeof(c1) );
            if ( rc != sizeof(c1) ) {
                PRINTF("Could not read c1\n");
                _lclose( iFile );
                return( FALSE );
            }

            read_name( iFile, name_buff );

            rc = _lread( iFile, (LPSTR)&c2, sizeof(c2) );
            if ( rc != sizeof(c2) ) {
                PRINTF("Could not read c2\n");
                _lclose( iFile );
                return( FALSE );
            }

            cnt = num_syms;
            while ( cnt ) {
                length = read_symbol( iFile, name_buff,
                                      &this_offset );
                if ( length == 0 ) {
                    PRINTF("Error access symbols in file %s\n", filename );
                    break;
                }

                --cnt;
            }
#ifdef NEED_INDICES
            cnt = num_syms;
            while ( cnt ) {
                rc = _lread( iFile, (LPSTR)&index, sizeof(index) );
                if ( rc != sizeof(index) ) {
                    PRINTF("Could not read index table entry\n");
                    _lclose( iFile );
                    return( FALSE );
                }
                PRINTF("Index: %04X\n", index );
                --cnt;
            }
#endif

            position = start_position;
            do {
                rc = _llseek( iFile, position, FILE_BEGIN );
                if ( rc == -1 ) {
                    PRINTF("Failed to seek to next record\n");
                    _lclose( iFile );
                    return( FALSE );
                }
                rc = _lread( iFile, (LPSTR)&next_off, sizeof(next_off) );
                if ( rc != sizeof(next_off) ) {
                    PRINTF("Could not read next_off\n");
                    _lclose( iFile );
                    return( FALSE );
                }
                position = ((LONG)next_off) << 4;

                rc = _lread( iFile, (LPSTR)&num_syms, sizeof(num_syms) );
                if ( rc != sizeof(num_syms) ) {
                    PRINTF("Could not read num_syms\n");
                    _lclose( iFile );
                    return( FALSE );
                }

                rc = _lread( iFile, (LPSTR)&r2, sizeof(r2) );
                if ( rc != sizeof(r2) ) {
                    PRINTF("Could not read r2\n");
                    _lclose( iFile );
                    return( FALSE );
                }

                rc = _lread( iFile, (LPSTR)&seg_num, sizeof(seg_num) );
                if ( rc != sizeof(seg_num) ) {
                    PRINTF("Could not read seg_num\n");
                    _lclose( iFile );
                    return( FALSE );
                }

                if ( mode == PROT_MODE &&
                        seg_num != (WORD)(he.SegmentNumber+1) ) {
                    /*
                    ** Skip reading of symbols for segments with the wrong seg_num
                    */
                    continue;
                }

                cnt = 0;
                while ( cnt < 12 ) {
                    rc = _lread( iFile, (LPSTR)&b[cnt], sizeof(b[0]) );
                    if ( rc != sizeof(b[0]) ) {
                        PRINTF("Could not read 12 byte b array\n");
                        _lclose( iFile );
                        return( FALSE );
                    }
                    cnt++;
                }
                read_name( iFile, name_buff );

                cnt = num_syms;
                while ( cnt ) {
                    length = read_symbol( iFile, name_buff, &this_offset );
                    if ( length == 0 ) {
                        PRINTF("Error access symbols in file %s\n", filename );
                        break;
                    }
                    if ( _stricmp(name_buff,sym_text) == 0 ) {
                        switch( mode ) {
                            case PROT_MODE:
                                *selector = (WORD)(he.gnode.pga_handle | 1);
                                *offset   = this_offset;
                                _lclose( iFile );
                                return( TRUE );
#if 0
                            case V86_MODE:
                                *selector = se[nEntry].selector + seg_num;
                                *offset   = this_offset;
                                _lclose( iFile );
                                return( TRUE );
#endif
                        }
                    }
                    {
                        char name2[200];

                        strcpy(name2,name_buff);
                        _strupr(name2);

                        if ( strstr(name2,name1) != 0 ) {
                            switch( mode ) {
                                case PROT_MODE:
                                    *selector = (WORD)(he.gnode.pga_handle | 1);
                                    *offset   = this_offset;
                                    PRINTF("%04X:%04X = %s\n", *selector, *offset, name_buff );
                                    break;
#if 0
                                case V86_MODE:
                                    *selector = se[nEntry].selector + seg_num;
                                    *offset   = this_offset;
                                    PRINTF("%04X:%04X = %s\n", *selector, *offset, name_buff );
                                    break;
#endif
                            }
                        }
                    }

                    --cnt;
                }
            } while ( position != start_position && position != 0 );

            _lclose( iFile );
        }
    }
    return( FALSE );
}

VOID
ListNear(
) {
    VDMCONTEXT              ThreadContext;
    WORD                    selector;
    LONG                    offset;
    CHAR                    sym_text[1000];
    DWORD                   dist;
    BOOL                    b;
    int                     mode;

    mode = GetContext( &ThreadContext );

    if (!GetNextToken()) {
        selector = (WORD) ThreadContext.SegCs;
        offset   = ThreadContext.Eip;
    } else if (!ParseIntelAddress(&mode, &selector, &offset)) {
        return;
    }


    if ( mode == PROT_MODE ) {
        PRINTF( "#%04X:%04lX", selector, offset );
    }
    if ( mode == V86_MODE ) {
        PRINTF( "&%04X:%04lX", selector, offset );
    }


    b = FindSymbol( selector, offset, sym_text, &dist, BEFORE, mode );
    if ( !b ) {
        PRINTF(" = Could not find symbol before");
    } else {
        if ( dist == 0 ) {
            PRINTF(" = %s", sym_text );
        } else {
            PRINTF(" = %s + 0x%lx", sym_text, dist );
        }
    }
    b = FindSymbol( selector, offset, sym_text, &dist, AFTER, mode );
    if ( !b ) {
        PRINTF(" = Could not find symbol after");
    } else {
        if ( dist == 0 ) {
            PRINTF(" = %s", sym_text );
        } else {
            PRINTF(" = %s - 0x%lx", sym_text, dist );
        }
    }
    PRINTF("\n");
}

VOID
EvaluateSymbol(
) {
    VDMCONTEXT              ThreadContext;
    BOOL                    result;
    WORD                    selector;
    LONG                    offset;
    int                     mode;

    try {

        mode = GetContext( &ThreadContext );

        result = FindAddress( &selector, &offset, lpArgumentString, mode );

        if ( result ) {
            if ( mode == PROT_MODE ) {
                PRINTF("Symbol %s = #%04X:%04X PROT_MODE\n", lpArgumentString, selector, offset );
            }
            if ( mode == V86_MODE ) {
                PRINTF("Symbol %s = &%04X:%04X V86_MODE\n", lpArgumentString, selector, offset );
            }
            return;
        }

        if ( mode == PROT_MODE ) {
            mode = V86_MODE;
        } else {
            if ( mode == V86_MODE ) {
                mode = PROT_MODE;
            }
        }

        result = FindAddress( &selector, &offset, lpArgumentString, mode );
        if ( result ) {
            if ( mode == PROT_MODE ) {
                PRINTF("Symbol %s = #%04X:%04X PROT_MODE\n", lpArgumentString, selector, offset );
            }
            if ( mode == V86_MODE ) {
                PRINTF("Symbol %s = &%04X:%04X V86_MODE\n", lpArgumentString, selector, offset );
            }
            return;
        }

        PRINTF("Could not find symbol %s\n", lpArgumentString );

    } except (1) {

        PRINTF("Exception 0x%08x in !bde.es, don't forget to thank Dave Hart for saving\n"
               "your debugging bacon by adding a try/except block!\n"
               "And while you're at it remind him to take out this caffeine-induced spew.\n",
               GetExceptionCode());

    }
}