summaryrefslogtreecommitdiffstats
path: root/src/lib/kStuff/kLdr/kLdrDyldFind.c
blob: 9d6562eb50fb71bcb2db825b67955d5ab1943a3c (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
/* $Id: kLdrDyldFind.c 29 2009-07-01 20:30:29Z bird $ */
/** @file
 * kLdr - The Dynamic Loader, File Searching Methods.
 */

/*
 * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

/*******************************************************************************
*   Header Files                                                               *
*******************************************************************************/
#include <k/kLdr.h>
#include "kLdrInternal.h"

#if K_OS == K_OS_LINUX
# include <k/kHlpSys.h>

#elif K_OS == K_OS_OS2
# define INCL_BASE
# define INCL_ERRORS
# include <os2.h>
# ifndef LIBPATHSTRICT
#  define LIBPATHSTRICT 3
# endif
  extern APIRET APIENTRY DosQueryHeaderInfo(HMODULE hmod, ULONG ulIndex, PVOID pvBuffer, ULONG cbBuffer, ULONG ulSubFunction);
# define QHINF_EXEINFO       1 /* NE exeinfo. */
# define QHINF_READRSRCTBL   2 /* Reads from the resource table. */
# define QHINF_READFILE      3 /* Reads from the executable file. */
# define QHINF_LIBPATHLENGTH 4 /* Gets the libpath length. */
# define QHINF_LIBPATH       5 /* Gets the entire libpath. */
# define QHINF_FIXENTRY      6 /* NE only */
# define QHINF_STE           7 /* NE only */
# define QHINF_MAPSEL        8 /* NE only */

#elif K_OS == K_OS_WINDOWS
# undef IMAGE_DOS_SIGNATURE
# undef IMAGE_NT_SIGNATURE
# include <Windows.h>

#endif


/*******************************************************************************
*   Defined Constants And Macros                                               *
*******************************************************************************/
/** @def KLDRDYLDFIND_STRICT
 * Define KLDRDYLDFIND_STRICT to enabled strict checks in kLdrDyldFind. */
#define KLDRDYLDFIND_STRICT 1

/** @def KLDRDYLDFIND_ASSERT
 * Assert that an expression is true when KLDRDYLDFIND_STRICT is defined.
 */
#ifdef KLDRDYLDFIND_STRICT
# define KLDRDYLDFIND_ASSERT(expr)  kHlpAssert(expr)
#else
# define KLDRDYLDFIND_ASSERT(expr)  do {} while (0)
#endif


/*******************************************************************************
*   Structures and Typedefs                                                    *
*******************************************************************************/
/**
 * Search arguments.
 * This avoids a bunch of unnecessary string lengths and calculations.
 */
typedef struct KLDRDYLDFINDARGS
{
    const char *pszName;
    KSIZE       cchName;

    const char *pszPrefix;
    KSIZE       cchPrefix;

    const char *pszSuffix;
    KSIZE       cchSuffix;

    KSIZE       cchMaxLength;

    KLDRDYLDSEARCH  enmSearch;
    KU32            fFlags;
    PPKRDR          ppRdr;
} KLDRDYLDFINDARGS, *PKLDRDYLDFINDARGS;

typedef const KLDRDYLDFINDARGS *PCKLDRDYLDFINDARGS;


/*******************************************************************************
*   Global Variables                                                           *
*******************************************************************************/
/** @name The kLdr search method parameters.
 * @{ */
/** The kLdr EXE search path.
 * During EXE searching the it's initialized with the values of the KLDR_PATH and
 * the PATH env.vars. Both ';' and ':' can be used as separators.
 */
char            kLdrDyldExePath[8192];
/** The kLdr DLL search path.
 * During initialization the KLDR_LIBRARY_PATH env.var. and the path in the
 * executable stub is appended. Both ';' and ':' can be used as separators.
 */
char            kLdrDyldLibraryPath[8192];
/** The kLdr application directory.
 * This is initialized when the executable is 'loaded' or by a kLdr user.
 */
char            kLdrDyldAppDir[260];
/** The default kLdr DLL prefix.
 * This is initialized with the KLDR_DEF_PREFIX env.var. + the prefix in the executable stub.
 */
char            kLdrDyldDefPrefix[16];
/** The default kLdr DLL suffix.
 * This is initialized with the KLDR_DEF_SUFFIX env.var. + the prefix in the executable stub.
 */
char            kLdrDyldDefSuffix[16];
/** @} */


/** @name The OS/2 search method parameters.
 * @{
 */
/** The OS/2 LIBPATH.
 * This is queried from the os2krnl on OS/2, while on other systems initialized using
 * the KLDR_OS2_LIBPATH env.var.
 */
char            kLdrDyldOS2Libpath[2048];
/** The OS/2 LIBPATHSTRICT ("T" or '\0').
 * This is queried from the os2krnl on OS/2, while on other systems initialized using
 * the KLDR_OS2_LIBPATHSTRICT env.var.
 */
char            kLdrDyldOS2LibpathStrict[8];
/** The OS/2 BEGINLIBPATH.
 * This is queried from the os2krnl on OS/2, while on other systems initialized using
 * the KLDR_OS2_BEGINLIBPATH env.var.
 */
char            kLdrDyldOS2BeginLibpath[2048];
/** The OS/2 ENDLIBPATH.
 * This is queried from the os2krnl on OS/2, while on other systems initialized using
 * the KLDR_OS2_ENDLIBPATH env.var.
 */
char            kLdrDyldOS2EndLibpath[2048];
/** @} */


/** @name The Windows search method parameters.
 * @{ */
/** The Windows application directory.
 * This is initialized when the executable is 'loaded' or by a kLdr user.
 */
char            kLdrDyldWindowsAppDir[260];
/** The Windows system directory.
 * This is queried from the Win32/64 subsystem on Windows, while on other systems
 * initialized using the KLDR_WINDOWS_SYSTEM_DIR env.var.
 */
char            kLdrDyldWindowsSystemDir[260];
/** The Windows directory.
 * This is queried from the Win32/64 subsystem on Windows, while on other systems
 * initialized using the KLDR_WINDOWS_DIR env.var.
 */
char            kLdrDyldWindowsDir[260];
/** The Windows path.
 * This is queried from the PATH env.var. on Windows, while on other systems
 * initialized using the KLDR_WINDOWS_PATH env.var. and falling back on
 * the PATH env.var. if it wasn't found.
 */
char            kLdrDyldWindowsPath[8192];
/** @} */


/** @name The Common Unix search method parameters.
 * @{
 */
/** The Common Unix library path.
 * Initialized from the env.var. KLDR_UNIX_LIBRARY_PATH or LD_LIBRARY_PATH or the
 * former wasn't found.
 */
char            kLdrDyldUnixLibraryPath[8192];
/** The Common Unix system library path. */
char            kLdrDyldUnixSystemLibraryPath[1024] = "/lib;/usr/lib";
/** @} */

/** @todo Deal with DT_RUNPATH and DT_RPATH. */
/** @todo ld.so.cache? */


/*******************************************************************************
*   Internal Functions                                                         *
*******************************************************************************/
static int kldrDyldFindDoDllSearch(const char *pszName, const char *pszPrefix, const char *pszSuffix,
                                KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKRDR ppRdr);
static int kldrDyldFindDoExeSearch(const char *pszName, const char *pszPrefix, const char *pszSuffix,
                                   KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKRDR ppRdr);
static int kldrDyldFindTryOpen(const char *pszFilename, PPKRDR ppRdr);
static int kldrDyldFindTryOpenPath(const char *pchPath, KSIZE cchPath, PCKLDRDYLDFINDARGS pArgs);
static int kldrDyldFindEnumeratePath(const char *pszSearchPath, PCKLDRDYLDFINDARGS pArgs);
static int kldrDyldFindGetDefaults(KLDRDYLDSEARCH *penmSearch, const char **pszPrefix,
                                   const char **pszSuffix, const char *pszName, KU32 fFlags);


/**
 * Initializes the find paths.
 *
 * @returns 0 on success, non-zero on failure.
 */
int kldrDyldFindInit(void)
{
    KSIZE   cch;
    int     rc;
    char    szTmp[sizeof(kLdrDyldDefSuffix)];

    /*
     * The kLdr search parameters.
     */
    rc = kHlpGetEnv("KLDR_LIBRARY_PATH", kLdrDyldLibraryPath, sizeof(kLdrDyldLibraryPath));
    rc = kHlpGetEnv("KLDR_DEF_PREFIX", szTmp, sizeof(szTmp));
    if (!rc)
        kHlpMemCopy(kLdrDyldDefPrefix, szTmp, sizeof(szTmp));
    rc = kHlpGetEnv("KLDR_DEF_SUFFIX", szTmp, sizeof(szTmp));
    if (!rc)
        kHlpMemCopy(kLdrDyldDefSuffix, szTmp, sizeof(szTmp));

    /*
     * The OS/2 search parameters.
     */
#if K_OS == K_OS_OS2
    rc = DosQueryHeaderInfo(NULLHANDLE, 0, kLdrDyldOS2Libpath, sizeof(kLdrDyldOS2Libpath), QHINF_LIBPATH);
    if (rc)
        return rc;
    rc = DosQueryExtLIBPATH((PSZ)kLdrDyldOS2LibpathStrict, LIBPATHSTRICT);
    if (rc)
        kLdrDyldOS2LibpathStrict[0] = '\0';
    rc = DosQueryExtLIBPATH((PSZ)kLdrDyldOS2BeginLibpath, BEGIN_LIBPATH);
    if (rc)
        kLdrDyldOS2BeginLibpath[0] = '\0';
    rc = DosQueryExtLIBPATH((PSZ)kLdrDyldOS2EndLibpath, END_LIBPATH);
    if (rc)
        kLdrDyldOS2EndLibpath[0] = '\0';

#else
    kHlpGetEnv("KLDR_OS2_LIBPATH", kLdrDyldOS2Libpath, sizeof(kLdrDyldOS2Libpath));
    kHlpGetEnv("KLDR_OS2_LIBPATHSTRICT", kLdrDyldOS2LibpathStrict, sizeof(kLdrDyldOS2LibpathStrict));
    if (    kLdrDyldOS2LibpathStrict[0] == 'T'
        ||  kLdrDyldOS2LibpathStrict[0] == 't')
        kLdrDyldOS2LibpathStrict[0] = 'T';
    else
        kLdrDyldOS2LibpathStrict[0] = '\0';
    kLdrDyldOS2LibpathStrict[1] = '\0';
    kHlpGetEnv("KLDR_OS2_BEGINLIBPATH", kLdrDyldOS2BeginLibpath, sizeof(kLdrDyldOS2BeginLibpath));
    kHlpGetEnv("KLDR_OS2_ENDLIBPATH", kLdrDyldOS2EndLibpath, sizeof(kLdrDyldOS2EndLibpath));
#endif

    /*
     * The windows search parameters.
     */
#if K_OS == K_OS_WINDOWS
    cch = GetSystemDirectory(kLdrDyldWindowsSystemDir, sizeof(kLdrDyldWindowsSystemDir));
    if (cch >= sizeof(kLdrDyldWindowsSystemDir))
        return (rc = GetLastError()) ? rc : -1;
    cch = GetWindowsDirectory(kLdrDyldWindowsDir, sizeof(kLdrDyldWindowsDir));
    if (cch >= sizeof(kLdrDyldWindowsDir))
        return (rc = GetLastError()) ? rc : -1;
    kHlpGetEnv("PATH", kLdrDyldWindowsPath, sizeof(kLdrDyldWindowsPath));
#else
    kHlpGetEnv("KLDR_WINDOWS_SYSTEM_DIR", kLdrDyldWindowsSystemDir, sizeof(kLdrDyldWindowsSystemDir));
    kHlpGetEnv("KLDR_WINDOWS_DIR", kLdrDyldWindowsDir, sizeof(kLdrDyldWindowsDir));
    rc = kHlpGetEnv("KLDR_WINDOWS_PATH", kLdrDyldWindowsPath, sizeof(kLdrDyldWindowsPath));
    if (rc)
        kHlpGetEnv("PATH", kLdrDyldWindowsPath, sizeof(kLdrDyldWindowsPath));
#endif

    /*
     * The Unix search parameters.
     */
    rc = kHlpGetEnv("KLDR_UNIX_LIBRARY_PATH", kLdrDyldUnixLibraryPath, sizeof(kLdrDyldUnixLibraryPath));
    if (rc)
        kHlpGetEnv("LD_LIBRARY_PATH", kLdrDyldUnixLibraryPath, sizeof(kLdrDyldUnixLibraryPath));

    (void)cch;
    return 0;
}


/**
 * Lazily initialize the two application directory paths.
 */
static void kldrDyldFindLazyInitAppDir(void)
{
    if (!kLdrDyldAppDir[0])
    {
#if K_OS == K_OS_DARWIN
        /** @todo implement this! */
        kLdrDyldWindowsAppDir[0] = kLdrDyldAppDir[0] = '.';
        kLdrDyldWindowsAppDir[1] = kLdrDyldAppDir[1] = '\0';

#elif K_OS == K_OS_LINUX
        KSSIZE cch = kHlpSys_readlink("/proc/self/exe", kLdrDyldAppDir, sizeof(kLdrDyldAppDir) - 1);
        if (cch > 0)
        {
            kLdrDyldAppDir[cch] = '\0';
            *kHlpGetFilename(kLdrDyldAppDir) = '\0';
            kHlpMemCopy(kLdrDyldWindowsAppDir, kLdrDyldAppDir, sizeof(kLdrDyldAppDir));
        }
        else
        {
            kLdrDyldWindowsAppDir[0] = kLdrDyldAppDir[0] = '.';
            kLdrDyldWindowsAppDir[1] = kLdrDyldAppDir[1] = '\0';
        }

#elif K_OS == K_OS_OS2
        PPIB pPib;
        PTIB pTib;
        APIRET rc;

        DosGetInfoBlocks(&pTib, &pPib);
        rc = DosQueryModuleName(pPib->pib_hmte, sizeof(kLdrDyldAppDir), kLdrDyldAppDir);
        if (!rc)
        {
            *kHlpGetFilename(kLdrDyldAppDir) = '\0';
             kHlpMemCopy(kLdrDyldWindowsAppDir, kLdrDyldAppDir, sizeof(kLdrDyldAppDir));
        }
        else
        {
            kLdrDyldWindowsAppDir[0] = kLdrDyldAppDir[0] = '.';
            kLdrDyldWindowsAppDir[1] = kLdrDyldAppDir[1] = '\0';
        }

#elif K_OS == K_OS_WINDOWS
        DWORD dwSize = GetModuleFileName(NULL /* the executable */, kLdrDyldAppDir, sizeof(kLdrDyldAppDir));
        if (dwSize > 0)
        {
            *kHlpGetFilename(kLdrDyldAppDir) = '\0';
            kHlpMemCopy(kLdrDyldWindowsAppDir, kLdrDyldAppDir, sizeof(kLdrDyldAppDir));
        }
        else
        {
            kLdrDyldWindowsAppDir[0] = kLdrDyldAppDir[0] = '.';
            kLdrDyldWindowsAppDir[1] = kLdrDyldAppDir[1] = '\0';
        }

#else
# error "Port me"
#endif
    }
}


/**
 * Locates and opens a module using the specified search method.
 *
 * @returns 0 and *ppMod on success, non-zero OS specific error on failure.
 *
 * @param   pszName         Partial or complete name, it's specific to the search method to determin which.
 * @param   pszPrefix       Prefix than can be used when searching.
 * @param   pszSuffix       Suffix than can be used when searching.
 * @param   enmSearch       The file search method to apply.
 * @param   fFlags          Search flags.
 * @param   ppMod           Where to store the file provider instance on success.
 */
int kldrDyldFindNewModule(const char *pszName, const char *pszPrefix, const char *pszSuffix,
                          KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKLDRDYLDMOD ppMod)
{
    int rc;
    PKRDR pRdr = NULL;

    *ppMod = NULL;

    /*
     * If this isn't just a filename, we the caller has specified a file
     * that should be opened directly and not a module name to be searched for.
     */
    if (!kHlpIsFilenameOnly(pszName))
        rc = kldrDyldFindTryOpen(pszName, &pRdr);
    else if (!(fFlags & KLDRDYLD_LOAD_FLAGS_EXECUTABLE))
        rc = kldrDyldFindDoDllSearch(pszName, pszPrefix, pszSuffix, enmSearch, fFlags, &pRdr);
    else
        rc = kldrDyldFindDoExeSearch(pszName, pszPrefix, pszSuffix, enmSearch, fFlags, &pRdr);
    if (!rc)
    {
#ifdef KLDRDYLDFIND_STRICT
        /* Sanity check of kldrDyldFindExistingModule. */
        if (fFlags & KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE)
        {
            const char     *pszFilename = kRdrName(pRdr);
            const KSIZE     cchFilename = kHlpStrLen(pszFilename);
            PKLDRDYLDMOD    pCur;
            for (pCur = kLdrDyldHead; pCur; pCur = pCur->Load.pNext)
                KLDRDYLDFIND_ASSERT(    pCur->pMod->cchFilename != cchFilename
                                    ||  kHlpMemComp(pCur->pMod->pszFilename, pszFilename, cchFilename));
        }
#endif

        /*
         * Check for matching non-global modules that should be promoted.
         */
        if (!(fFlags & KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE))
        {
            const char     *pszFilename = kRdrName(pRdr);
            const KSIZE     cchFilename = kHlpStrLen(pszFilename);
            PKLDRDYLDMOD    pCur;
            for (pCur = kLdrDyldHead; pCur; pCur = pCur->Load.pNext)
            {
                if (    !pCur->fGlobalOrSpecific
                    &&  pCur->pMod->cchFilename == cchFilename
                    &&  !kHlpMemComp(pCur->pMod->pszFilename, pszFilename, cchFilename))
                {
                    kRdrClose(pRdr);
                    kldrDyldModMarkGlobal(pCur);
                    *ppMod = pCur;
                    return 0;
                }
                KLDRDYLDFIND_ASSERT(    pCur->pMod->cchFilename != cchFilename
                                    ||  kHlpMemComp(pCur->pMod->pszFilename, pszFilename, cchFilename));
            }
        }

        /*
         * Create a new module.
         */
        rc = kldrDyldModCreate(pRdr, fFlags, ppMod);
        if (rc)
            kRdrClose(pRdr);
    }
    return rc;
}


/**
 * Searches for a DLL file using the specified method.
 *
 * @returns 0 on success and *ppMod pointing to the new module.
 * @returns KLDR_ERR_MODULE_NOT_FOUND if the specified file couldn't be opened.
 * @returns non-zero kLdr or OS specific status code on other failures.
 * @param   pszName     The name.
 * @param   pszPrefix   The prefix, optional.
 * @param   pszSuffix   The suffix, optional.
 * @param   enmSearch   The search method.
 * @param   fFlags      The load/search flags.
 * @param   ppRdr       Where to store the pointer to the file provider instance on success.
 */
static int kldrDyldFindDoDllSearch(const char *pszName, const char *pszPrefix, const char *pszSuffix,
                                   KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKRDR ppRdr)
{
    int rc;
    KLDRDYLDFINDARGS Args;

    /*
     * Initialize the argument structure and resolve defaults.
     */
    Args.enmSearch = enmSearch;
    Args.pszPrefix = pszPrefix;
    Args.pszSuffix = pszSuffix;
    rc = kldrDyldFindGetDefaults(&Args.enmSearch, &Args.pszPrefix, &Args.pszSuffix, pszName, fFlags);
    if (rc)
        return rc;
    Args.pszName = pszName;
    Args.cchName = kHlpStrLen(pszName);
    Args.cchPrefix = Args.pszPrefix ? kHlpStrLen(Args.pszPrefix) : 0;
    Args.cchSuffix = Args.pszSuffix ? kHlpStrLen(Args.pszSuffix) : 0;
    Args.cchMaxLength = Args.cchName + Args.cchSuffix + Args.cchPrefix;
    Args.fFlags = fFlags;
    Args.ppRdr = ppRdr;

    /*
     * Apply the specified search method.
     */
/** @todo get rid of the strlen() on the various paths here! */
    switch (Args.enmSearch)
    {
        case KLDRDYLD_SEARCH_KLDR:
        {
            kldrDyldFindLazyInitAppDir();
            if (kLdrDyldAppDir[0] != '\0')
            {
                rc = kldrDyldFindTryOpenPath(kLdrDyldAppDir, kHlpStrLen(kLdrDyldAppDir), &Args);
                if (rc != KLDR_ERR_MODULE_NOT_FOUND)
                    break;
            }
            rc = kldrDyldFindTryOpenPath(".", 1, &Args);
            if (rc != KLDR_ERR_MODULE_NOT_FOUND)
                break;
            rc = kldrDyldFindEnumeratePath(kLdrDyldLibraryPath, &Args);
            break;
        }

        case KLDRDYLD_SEARCH_OS2:
        {
            rc = kldrDyldFindEnumeratePath(kLdrDyldOS2BeginLibpath, &Args);
            if (rc != KLDR_ERR_MODULE_NOT_FOUND)
                break;
            rc = kldrDyldFindEnumeratePath(kLdrDyldOS2Libpath, &Args);
            if (rc != KLDR_ERR_MODULE_NOT_FOUND)
                break;
            rc = kldrDyldFindEnumeratePath(kLdrDyldOS2EndLibpath, &Args);
            break;
        }

        case KLDRDYLD_SEARCH_WINDOWS:
        case KLDRDYLD_SEARCH_WINDOWS_ALTERED:
        {
            kldrDyldFindLazyInitAppDir();
            rc = kldrDyldFindTryOpenPath(kLdrDyldWindowsAppDir, kHlpStrLen(kLdrDyldWindowsAppDir), &Args);
            if (rc != KLDR_ERR_MODULE_NOT_FOUND)
                break;
            if (Args.enmSearch == KLDRDYLD_SEARCH_WINDOWS_ALTERED)
            {
                rc = kldrDyldFindTryOpenPath(".", 1, &Args);
                if (rc != KLDR_ERR_MODULE_NOT_FOUND)
                    break;
            }
            rc = kldrDyldFindTryOpenPath(kLdrDyldWindowsSystemDir, kHlpStrLen(kLdrDyldWindowsSystemDir), &Args);
            if (rc != KLDR_ERR_MODULE_NOT_FOUND)
                break;
            rc = kldrDyldFindTryOpenPath(kLdrDyldWindowsDir, kHlpStrLen(kLdrDyldWindowsDir), &Args);
            if (rc != KLDR_ERR_MODULE_NOT_FOUND)
                break;
            if (Args.enmSearch == KLDRDYLD_SEARCH_WINDOWS)
            {
                rc = kldrDyldFindTryOpenPath(".", 1, &Args);
                if (rc != KLDR_ERR_MODULE_NOT_FOUND)
                    break;
            }
            rc = kldrDyldFindEnumeratePath(kLdrDyldWindowsPath, &Args);
            break;
        }

        case KLDRDYLD_SEARCH_UNIX_COMMON:
        {
            rc = kldrDyldFindEnumeratePath(kLdrDyldUnixLibraryPath, &Args);
            if (rc == KLDR_ERR_MODULE_NOT_FOUND)
                break;
            rc = kldrDyldFindEnumeratePath(kLdrDyldUnixSystemLibraryPath, &Args);
            break;
        }

        default: kHlpAssert(!"internal error"); return -1;
    }
    return rc;
}


/**
 * Searches for an EXE file using the specified method.
 *
 * @returns 0 on success and *ppMod pointing to the new module.
 * @returns KLDR_ERR_MODULE_NOT_FOUND if the specified file couldn't be opened.
 * @returns non-zero kLdr or OS specific status code on other failures.
 * @param   pszName     The name.
 * @param   pszPrefix   The prefix, optional.
 * @param   pszSuffix   The suffix, optional.
 * @param   enmSearch   The search method.
 * @param   fFlags      The load/search flags.
 * @param   ppRdr       Where to store the pointer to the file provider instance on success.
 */
static int kldrDyldFindDoExeSearch(const char *pszName, const char *pszPrefix, const char *pszSuffix,
                                   KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKRDR ppRdr)
{
    int rc;
    KLDRDYLDFINDARGS Args;

    /*
     * Initialize the argument structure and resolve defaults.
     */
    Args.enmSearch = enmSearch;
    Args.pszPrefix = pszPrefix;
    Args.pszSuffix = pszSuffix;
    rc = kldrDyldFindGetDefaults(&Args.enmSearch, &Args.pszPrefix, &Args.pszSuffix, pszName, fFlags);
    if (rc)
        return rc;
    Args.pszName = pszName;
    Args.cchName = kHlpStrLen(pszName);
    Args.cchPrefix = Args.pszPrefix ? kHlpStrLen(Args.pszPrefix) : 0;
    Args.cchSuffix = Args.pszSuffix ? kHlpStrLen(Args.pszSuffix) : 0;
    Args.cchMaxLength = Args.cchName + Args.cchSuffix + Args.cchPrefix;
    Args.fFlags = fFlags;
    Args.ppRdr = ppRdr;

    /*
     * If we're bootstrapping a process, we'll start by looking in the
     * application directory and the check out the path.
     */
    if (g_fBootstrapping)
    {
        kldrDyldFindLazyInitAppDir();
        if (kLdrDyldAppDir[0] != '\0')
        {
            rc = kldrDyldFindTryOpenPath(kLdrDyldAppDir, kHlpStrLen(kLdrDyldAppDir), &Args);
            if (rc != KLDR_ERR_MODULE_NOT_FOUND)
                return rc;
        }
    }

    /*
     * Search the EXE search path. Initialize it the first time around.
     */
    if (!kLdrDyldExePath[0])
    {
        KSIZE cch;
        kHlpGetEnv("KLDR_EXE_PATH", kLdrDyldExePath, sizeof(kLdrDyldExePath) - 10);
        cch = kHlpStrLen(kLdrDyldExePath);
        kLdrDyldExePath[cch++] = ';';
        kHlpGetEnv("PATH", &kLdrDyldExePath[cch], sizeof(kLdrDyldExePath) - cch);
    }
    return kldrDyldFindEnumeratePath(kLdrDyldExePath, &Args);
}


/**
 * Try open the specfied file.
 *
 * @returns 0 on success and *ppMod pointing to the new module.
 * @returns KLDR_ERR_MODULE_NOT_FOUND if the specified file couldn't be opened.
 * @returns non-zero kLdr or OS specific status code on other failures.
 * @param   pszFilename     The filename.
 * @param   ppRdr           Where to store the pointer to the new module.
 */
static int kldrDyldFindTryOpen(const char *pszFilename, PPKRDR ppRdr)
{
    int rc;

    /*
     * Try open the file.
     */
    rc = kRdrOpen(ppRdr, pszFilename);
    if (!rc)
        return 0;
    /** @todo deal with return codes properly. */
    if (rc >= KERR_BASE && rc <= KERR_END)
        return rc;

    return KLDR_ERR_MODULE_NOT_FOUND;
}


/**
 * Composes a filename from the specified directory path,
 * prefix (optional), name and suffix (optional, will try with and without).
 *
 * @param   pchPath     The directory path - this doesn't have to be null terminated.
 * @param   cchPath     The length of the path.
 * @param   pArgs       The search argument structure.
 *
 * @returns See kldrDyldFindTryOpen
 */
static int kldrDyldFindTryOpenPath(const char *pchPath, KSIZE cchPath, PCKLDRDYLDFINDARGS pArgs)
{
    static char s_szFilename[1024];
    char *psz;
    int rc;

    /*
     * Ignore any attempts at opening empty paths.
     * This can happen when a *Dir globals is empty.
     */
    if (!cchPath)
        return KLDR_ERR_MODULE_NOT_FOUND; /* ignore */

    /*
     * Limit check first.
     */
    if (cchPath + 1 + pArgs->cchMaxLength >= sizeof(s_szFilename))
    {
        KLDRDYLDFIND_ASSERT(!"too long");
        return KLDR_ERR_MODULE_NOT_FOUND; /* ignore */
    }

    /*
     * The directory path.
     */
    kHlpMemCopy(s_szFilename, pchPath, cchPath);
    psz = &s_szFilename[cchPath];
    if (psz[-1] != '/'
#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
        && psz[-1] != '\\'
        && psz[-1] != ':'
#endif
        )
        *psz++ = '/';

    /*
     * The name.
     */
    if (pArgs->cchPrefix)
    {
        kHlpMemCopy(psz, pArgs->pszPrefix, pArgs->cchPrefix);
        psz += pArgs->cchPrefix;
    }
    kHlpMemCopy(psz, pArgs->pszName, pArgs->cchName);
    psz += pArgs->cchName;
    if (pArgs->cchSuffix)
    {
        kHlpMemCopy(psz, pArgs->pszSuffix, pArgs->cchSuffix);
        psz += pArgs->cchSuffix;
    }
    *psz = '\0';


    /*
     * Try open it.
     */
    rc = kldrDyldFindTryOpen(s_szFilename, pArgs->ppRdr);
    /* If we're opening an executable, try again without the suffix.*/
    if (    rc
        &&  pArgs->cchSuffix
        &&  (pArgs->fFlags & KLDRDYLD_LOAD_FLAGS_EXECUTABLE))
    {
        psz -= pArgs->cchSuffix;
        *psz = '\0';
        rc = kldrDyldFindTryOpen(s_szFilename, pArgs->ppRdr);
    }
    return rc;
}


/**
 * Enumerates the specfied path.
 *
 * @returns Any return code from the kldrDyldFindTryOpenPath() which isn't KLDR_ERR_MODULE_NOT_FOUND.
 * @returns KLDR_ERR_MODULE_NOT_FOUND if the end of the search path was reached.
 * @param   pszSearchPath   The search path to enumeare.
 * @param   pArgs       The search argument structure.
 */
static int kldrDyldFindEnumeratePath(const char *pszSearchPath, PCKLDRDYLDFINDARGS pArgs)
{
    const char *psz = pszSearchPath;
    for (;;)
    {
        const char *pszEnd;
        KSIZE       cchPath;

        /*
         * Trim.
         */
        while (*psz == ';' || *psz == ':')
            psz++;
        if (*psz == '\0')
            return KLDR_ERR_MODULE_NOT_FOUND;

        /*
         * Find the end.
         */
        pszEnd = psz + 1;
        while (     *pszEnd != '\0'
               &&   *pszEnd != ';'
#if K_OS == K_OS_OS2 || K_OS == K_OS_WINDOWS
               && (     *pszEnd != ':'
                   ||   (   pszEnd - psz == 1
                         && (   (*psz >= 'A' && *psz <= 'Z')
                             || (*psz >= 'a' && *psz <= 'z')
                            )
                        )
                  )
#else
               && *pszEnd != ':'
#endif
              )
            pszEnd++;

        /*
         * If not empty path, try open the module using it.
         */
        cchPath = pszEnd - psz;
        if (cchPath > 0)
        {
            int rc;
            rc = kldrDyldFindTryOpenPath(psz, cchPath, pArgs);
            if (rc != KLDR_ERR_MODULE_NOT_FOUND)
                return rc;
        }

        /* next */
        psz = pszEnd;
    }
}


/**
 * Resolve default search method, prefix and suffix.
 *
 * @returns 0 on success, KERR_INVALID_PARAMETER on failure.
 * @param   penmSearch  The search method. In/Out.
 * @param   ppszPrefix  The prefix. In/Out.
 * @param   ppszSuffix  The suffix. In/Out.
 * @param   pszName     The name. In.
 * @param   fFlags      The load/search flags.
 */
static int kldrDyldFindGetDefaults(KLDRDYLDSEARCH *penmSearch, const char **ppszPrefix, const char **ppszSuffix,
                                   const char *pszName, KU32 fFlags)
{
    unsigned fCaseSensitive;

    /*
     * Fixup search method alias.
     */
    if (*penmSearch == KLDRDYLD_SEARCH_HOST)
#if K_OS == K_OS_DARWIN
        /** @todo *penmSearch = KLDRDYLD_SEARCH_DARWIN; */
        *penmSearch = KLDRDYLD_SEARCH_UNIX_COMMON;
#elif K_OS == K_OS_FREEBSD \
   || K_OS == K_OS_LINUX \
   || K_OS == K_OS_NETBSD \
   || K_OS == K_OS_OPENBSD \
   || K_OS == K_OS_SOLARIS
        *penmSearch = KLDRDYLD_SEARCH_UNIX_COMMON;
#elif K_OS == K_OS_OS2
        *penmSearch = KLDRDYLD_SEARCH_OS2;
#elif K_OS == K_OS_WINDOWS
        *penmSearch = KLDRDYLD_SEARCH_WINDOWS;
#else
# error "Port me"
#endif

    /*
     * Apply search method specific prefix/suffix.
     */
    switch (*penmSearch)
    {
        case KLDRDYLD_SEARCH_KLDR:
            if (!*ppszPrefix && kLdrDyldDefPrefix[0])
                *ppszPrefix = kLdrDyldDefPrefix;
            if (!*ppszSuffix && kLdrDyldDefSuffix[0])
                *ppszSuffix = kLdrDyldDefSuffix;
            fCaseSensitive = 1;
            break;

        case KLDRDYLD_SEARCH_OS2:
            if (!*ppszSuffix)
                *ppszSuffix = !(fFlags & KLDRDYLD_LOAD_FLAGS_EXECUTABLE) ? ".dll" : ".exe";
            fCaseSensitive = 0;
            break;

        case KLDRDYLD_SEARCH_WINDOWS:
        case KLDRDYLD_SEARCH_WINDOWS_ALTERED:
            if (!*ppszSuffix)
                *ppszSuffix = !(fFlags & KLDRDYLD_LOAD_FLAGS_EXECUTABLE) ? ".dll" : ".exe";
            fCaseSensitive = 0;
            break;

        case KLDRDYLD_SEARCH_UNIX_COMMON:
            fCaseSensitive = 1;
            break;

        default:
            KLDRDYLDFIND_ASSERT(!"invalid search method");
            return KERR_INVALID_PARAMETER;
    }

    /*
     * Drop the suffix if it's already included in the name.
     */
    if (*ppszSuffix)
    {
        const KSIZE cchName = kHlpStrLen(pszName);
        const KSIZE cchSuffix = kHlpStrLen(*ppszSuffix);
        if (    cchName > cchSuffix
            &&  (   fCaseSensitive
                 ?  !kHlpMemComp(pszName + cchName - cchSuffix, *ppszSuffix, cchSuffix)
                 :  !kHlpMemICompAscii(pszName + cchName - cchSuffix, *ppszSuffix, cchSuffix))
           )
            *ppszSuffix = NULL;
    }

    return 0;
}


/**
 * Locates an already open module using the specified search method.
 *
 * @returns 0 and *ppMod on success, non-zero OS specific error on failure.
 *
 * @param   pszName         Partial or complete name, it's specific to the search method to determin which.
 * @param   pszPrefix       Prefix than can be used when searching.
 * @param   pszSuffix       Suffix than can be used when searching.
 * @param   enmSearch       The file search method to apply.
 * @param   fFlags          Search flags.
 * @param   ppMod           Where to store the file provider instance on success.
 */
int kldrDyldFindExistingModule(const char *pszName, const char *pszPrefix, const char *pszSuffix,
                               KLDRDYLDSEARCH enmSearch, unsigned fFlags, PPKLDRDYLDMOD ppMod)
{

    int rc;
    unsigned fOS2LibpathStrict;
    *ppMod = NULL;

    /*
     * Don't bother if no modules are loaded yet.
     */
    if (!kLdrDyldHead)
        return KLDR_ERR_MODULE_NOT_FOUND;

    /*
     * Defaults.
     */
    rc = kldrDyldFindGetDefaults(&enmSearch, &pszPrefix, &pszSuffix, pszName, fFlags);
    if (rc)
        return rc;

    /*
     * If this isn't just a filename, the caller has specified a file
     * that should be opened directly and not a module name to be searched for.
     *
     * In order to do the right thing we'll have to open the file and get the
     * correct filename for it.
     *
     * The OS/2 libpath strict method require us to find the correct DLL first.
     */
    fOS2LibpathStrict = 0;
    if (    !kHlpIsFilenameOnly(pszName)
        ||  (fOS2LibpathStrict = (   enmSearch == KLDRDYLD_SEARCH_OS2
                                  && kLdrDyldOS2LibpathStrict[0] == 'T')
            )
       )
    {
        PKRDR pRdr;
        if (fOS2LibpathStrict)
            rc = kldrDyldFindDoDllSearch(pszName, pszPrefix, pszSuffix, enmSearch, fFlags, &pRdr);
        else
            rc = kldrDyldFindTryOpen(pszName, &pRdr);
        if (!rc)
        {
            /* do a filename based search. */
            const char     *pszFilename = kRdrName(pRdr);
            const KSIZE     cchFilename = kHlpStrLen(pszFilename);
            PKLDRDYLDMOD    pCur;
            rc = KLDR_ERR_MODULE_NOT_FOUND;
            for (pCur = kLdrDyldHead; pCur; pCur = pCur->Load.pNext)
            {
                if (    pCur->pMod->cchFilename == cchFilename
                    &&  !kHlpMemComp(pCur->pMod->pszFilename, pszFilename, cchFilename))
                {
                    *ppMod = pCur;
                    rc = 0;
                    break;
                }
            }
            kRdrClose(pRdr);
        }
    }
    else
    {
        const KSIZE     cchName = kHlpStrLen(pszName);
        const KSIZE     cchPrefix = pszPrefix ? kHlpStrLen(pszPrefix) : 0;
        const KSIZE     cchSuffix = pszSuffix ? kHlpStrLen(pszSuffix) : 0;
        const char     *pszNameSuffix = kHlpGetSuff(pszName);
        PKLDRDYLDMOD    pCur = kLdrDyldHead;

        /*
         * Some of the methods are case insensitive (ASCII), others are case sensitive.
         * To avoid having todo indirect calls to the compare functions here, we split
         * ways even if it means a lot of duplicate code.
         */
        if (   enmSearch == KLDRDYLD_SEARCH_OS2
            || enmSearch == KLDRDYLD_SEARCH_WINDOWS
            || enmSearch == KLDRDYLD_SEARCH_WINDOWS_ALTERED)
        {
            const unsigned fNameHasSuffix = pszNameSuffix
                                         && kHlpStrLen(pszNameSuffix) == cchSuffix
                                         && !kHlpMemICompAscii(pszNameSuffix, pszName + cchName - cchSuffix, cchSuffix);
            for (; pCur; pCur = pCur->Load.pNext)
            {
                /* match global / specific */
                if (    !pCur->fGlobalOrSpecific
                    &&  !(fFlags & KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE))
                    continue;

                /* match name */
                if (    pCur->pMod->cchName == cchName
                    &&  !kHlpMemICompAscii(pCur->pMod->pszName, pszName, cchName))
                    break;
                if (cchPrefix)
                {
                    if (    pCur->pMod->cchName == cchName + cchPrefix
                        &&  !kHlpMemICompAscii(pCur->pMod->pszName, pszPrefix, cchPrefix)
                        &&  !kHlpMemICompAscii(pCur->pMod->pszName + cchPrefix, pszName, cchName))
                    break;
                }
                if (cchSuffix)
                {
                    if (    pCur->pMod->cchName == cchName + cchSuffix
                        &&  !kHlpMemICompAscii(pCur->pMod->pszName + cchName, pszSuffix, cchSuffix)
                        &&  !kHlpMemICompAscii(pCur->pMod->pszName, pszName, cchName))
                    break;
                    if (    fNameHasSuffix
                        &&  pCur->pMod->cchName == cchName - cchSuffix
                        &&  !kHlpMemICompAscii(pCur->pMod->pszName, pszName, cchName - cchSuffix))
                    break;
                    if (cchPrefix)
                    {
                        if (    pCur->pMod->cchName == cchName + cchPrefix + cchSuffix
                            &&  !kHlpMemICompAscii(pCur->pMod->pszName, pszPrefix, cchPrefix)
                            &&  !kHlpMemICompAscii(pCur->pMod->pszName + cchPrefix, pszName, cchName)
                            &&  !kHlpMemICompAscii(pCur->pMod->pszName + cchPrefix + cchName, pszSuffix, cchSuffix))
                        break;
                        if (    fNameHasSuffix
                            &&  pCur->pMod->cchName == cchName + cchPrefix - cchSuffix
                            &&  !kHlpMemICompAscii(pCur->pMod->pszName, pszPrefix, cchPrefix)
                            &&  !kHlpMemICompAscii(pCur->pMod->pszName + cchPrefix, pszName, cchName - cchSuffix))
                        break;
                    }
                }
            }
        }
        else
        {
            const unsigned fNameHasSuffix = pszNameSuffix
                                         && kHlpStrLen(pszNameSuffix) == cchSuffix
                                         && kHlpMemComp(pszNameSuffix, pszName + cchName - cchSuffix, cchSuffix);
            for (; pCur; pCur = pCur->Load.pNext)
            {
                /* match global / specific */
                if (    !pCur->fGlobalOrSpecific
                    &&  !(fFlags & KLDRYDLD_LOAD_FLAGS_SPECIFIC_MODULE))
                    continue;

                /* match name */
                if (    pCur->pMod->cchName == cchName
                    &&  !kHlpMemComp(pCur->pMod->pszName, pszName, cchName))
                    break;
                if (cchPrefix)
                {
                    if (    pCur->pMod->cchName == cchName + cchPrefix
                        &&  !kHlpMemComp(pCur->pMod->pszName, pszPrefix, cchPrefix)
                        &&  !kHlpMemComp(pCur->pMod->pszName + cchPrefix, pszName, cchName))
                    break;
                }
                if (cchSuffix)
                {
                    if (    pCur->pMod->cchName == cchName + cchSuffix
                        &&  !kHlpMemComp(pCur->pMod->pszName + cchName, pszSuffix, cchSuffix)
                        &&  !kHlpMemComp(pCur->pMod->pszName, pszName, cchName))
                    break;
                    if (    fNameHasSuffix
                        &&  pCur->pMod->cchName == cchName - cchSuffix
                        &&  !kHlpMemComp(pCur->pMod->pszName, pszName, cchName - cchSuffix))
                    break;
                    if (cchPrefix)
                    {
                        if (    pCur->pMod->cchName == cchName + cchPrefix + cchSuffix
                            &&  !kHlpMemComp(pCur->pMod->pszName, pszPrefix, cchPrefix)
                            &&  !kHlpMemComp(pCur->pMod->pszName + cchPrefix, pszName, cchName)
                            &&  !kHlpMemComp(pCur->pMod->pszName + cchPrefix + cchName, pszSuffix, cchSuffix))
                        break;
                        if (    pCur->pMod->cchName == cchName + cchPrefix - cchSuffix
                            &&  !kHlpMemComp(pCur->pMod->pszName, pszPrefix, cchPrefix)
                            &&  !kHlpMemComp(pCur->pMod->pszName + cchPrefix, pszName, cchName - cchSuffix))
                        break;
                    }
                }
            }
        }

        /* search result. */
        if (pCur)
        {
            *ppMod = pCur;
            rc = 0;
        }
        else
            rc = KLDR_ERR_MODULE_NOT_FOUND;
    }

    return rc;
}