forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.cpp
More file actions
1718 lines (1414 loc) · 43.5 KB
/
module.cpp
File metadata and controls
1718 lines (1414 loc) · 43.5 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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
/*++
Module Name:
module.c
Abstract:
Implementation of module related functions in the Win32 API
--*/
#include "pal/thread.hpp"
#include "pal/malloc.hpp"
#include "pal/file.hpp"
#include "pal/palinternal.h"
#include "pal/dbgmsg.h"
#include "pal/module.h"
#include "pal/cs.hpp"
#include "pal/process.h"
#include "pal/file.h"
#include "pal/utils.h"
#include "pal/init.h"
#include "pal/modulename.h"
#include "pal/misc.h"
#include "pal/virtual.h"
#include "pal/map.hpp"
#include "pal/stackstring.hpp"
#include <sys/param.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#if NEED_DLCOMPAT
#include "dlcompat.h"
#else // NEED_DLCOMPAT
#include <dlfcn.h>
#endif // NEED_DLCOMPAT
#if HAVE_ALLOCA_H
#include <alloca.h>
#endif // HAVE_ALLOCA_H
#ifdef __APPLE__
#include <mach-o/dyld.h>
#include <mach-o/loader.h>
#endif // __APPLE__
#include <sys/types.h>
#include <sys/mman.h>
#if defined(__LINUX__) && !defined(__ANDROID__)
#include <gnu/lib-names.h>
#endif
using namespace CorUnix;
SET_DEFAULT_DEBUG_CHANNEL(LOADER);
// In safemath.h, Template SafeInt uses macro _ASSERTE, which need to use variable
// defdbgchan defined by SET_DEFAULT_DEBUG_CHANNEL. Therefore, the include statement
// should be placed after the SET_DEFAULT_DEBUG_CHANNEL(LOADER)
#include <safemath.h>
/* macro definitions **********************************************************/
/* get the full name of a module if available, and the short name otherwise*/
#define MODNAME(x) ((x)->lib_name)
/* Which path should FindLibrary search? */
#if defined(__APPLE__)
#define LIBSEARCHPATH "DYLD_LIBRARY_PATH"
#else
#define LIBSEARCHPATH "LD_LIBRARY_PATH"
#endif
#define LIBC_NAME_WITHOUT_EXTENSION "libc"
/* static variables ***********************************************************/
/* critical section that regulates access to the module list */
CRITICAL_SECTION module_critsec;
/* always the first, in the in-load-order list */
MODSTRUCT exe_module;
MODSTRUCT *pal_module = nullptr;
char * g_szCoreCLRPath = nullptr;
size_t g_cbszCoreCLRPath = MAX_LONGPATH * sizeof(char);
int MaxWCharToAcpLength = 3;
/* static function declarations ***********************************************/
template<class TChar> static bool LOADVerifyLibraryPath(const TChar *libraryPath);
static bool LOADConvertLibraryPathWideStringToMultibyteString(
LPCWSTR wideLibraryPath,
LPSTR multibyteLibraryPath,
INT *multibyteLibraryPathLengthRef);
static BOOL LOADValidateModule(MODSTRUCT *module);
static LPWSTR LOADGetModuleFileName(MODSTRUCT *module);
static MODSTRUCT *LOADAddModule(void *dl_handle, LPCSTR libraryNameOrPath);
static void *LOADLoadLibraryDirect(LPCSTR libraryNameOrPath);
static BOOL LOADFreeLibrary(MODSTRUCT *module, BOOL fCallDllMain);
static HMODULE LOADRegisterLibraryDirect(void *dl_handle, LPCSTR libraryNameOrPath, BOOL fDynamic);
static HMODULE LOADLoadLibrary(LPCSTR shortAsciiName, BOOL fDynamic);
static BOOL LOADCallDllMainSafe(MODSTRUCT *module, DWORD dwReason, LPVOID lpReserved);
/* API function definitions ***************************************************/
/*++
Function:
LoadLibraryA
See MSDN doc.
--*/
HMODULE
PALAPI
LoadLibraryA(
IN LPCSTR lpLibFileName)
{
return LoadLibraryExA(lpLibFileName, nullptr, 0);
}
/*++
Function:
LoadLibraryW
See MSDN doc.
--*/
HMODULE
PALAPI
LoadLibraryW(
IN LPCWSTR lpLibFileName)
{
return LoadLibraryExW(lpLibFileName, nullptr, 0);
}
/*++
Function:
LoadLibraryExA
See MSDN doc.
--*/
HMODULE
PALAPI
LoadLibraryExA(
IN LPCSTR lpLibFileName,
IN /*Reserved*/ HANDLE hFile,
IN DWORD dwFlags)
{
if (dwFlags != 0)
{
// UNIXTODO: Implement this!
ASSERT("Needs Implementation!!!");
return nullptr;
}
LPSTR lpstr = nullptr;
HMODULE hModule = nullptr;
PERF_ENTRY(LoadLibraryA);
ENTRY("LoadLibraryExA (lpLibFileName=%p (%s)) \n",
(lpLibFileName) ? lpLibFileName : "NULL",
(lpLibFileName) ? lpLibFileName : "NULL");
if (!LOADVerifyLibraryPath(lpLibFileName))
{
goto Done;
}
/* do the Dos/Unix conversion on our own copy of the name */
lpstr = InternalStrdup(lpLibFileName);
if (!lpstr)
{
ERROR("InternalStrdup failure!\n");
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
goto Done;
}
FILEDosToUnixPathA(lpstr);
hModule = LOADLoadLibrary(lpstr, TRUE);
/* let LOADLoadLibrary call SetLastError */
Done:
if (lpstr != nullptr)
{
InternalFree(lpstr);
}
LOGEXIT("LoadLibraryExA returns HMODULE %p\n", hModule);
PERF_EXIT(LoadLibraryExA);
return hModule;
}
/*++
Function:
LoadLibraryExW
See MSDN doc.
--*/
HMODULE
PALAPI
LoadLibraryExW(
IN LPCWSTR lpLibFileName,
IN /*Reserved*/ HANDLE hFile,
IN DWORD dwFlags)
{
if (dwFlags != 0)
{
// UNIXTODO: Implement this!
ASSERT("Needs Implementation!!!");
return nullptr;
}
CHAR * lpstr;
INT name_length;
PathCharString pathstr;
HMODULE hModule = nullptr;
PERF_ENTRY(LoadLibraryExW);
ENTRY("LoadLibraryExW (lpLibFileName=%p (%S)) \n",
lpLibFileName ? lpLibFileName : W16_NULLSTRING,
lpLibFileName ? lpLibFileName : W16_NULLSTRING);
if (!LOADVerifyLibraryPath(lpLibFileName))
{
goto done;
}
lpstr = pathstr.OpenStringBuffer((PAL_wcslen(lpLibFileName)+1) * MaxWCharToAcpLength);
if (nullptr == lpstr)
{
goto done;
}
if (!LOADConvertLibraryPathWideStringToMultibyteString(lpLibFileName, lpstr, &name_length))
{
goto done;
}
/* do the Dos/Unix conversion on our own copy of the name */
FILEDosToUnixPathA(lpstr);
pathstr.CloseBuffer(name_length);
/* let LOADLoadLibrary call SetLastError in case of failure */
hModule = LOADLoadLibrary(lpstr, TRUE);
done:
LOGEXIT("LoadLibraryExW returns HMODULE %p\n", hModule);
PERF_EXIT(LoadLibraryExW);
return hModule;
}
/*++
Function:
GetProcAddress
See MSDN doc.
--*/
FARPROC
PALAPI
GetProcAddress(
IN HMODULE hModule,
IN LPCSTR lpProcName)
{
MODSTRUCT *module;
FARPROC ProcAddress = nullptr;
LPCSTR symbolName = lpProcName;
PERF_ENTRY(GetProcAddress);
ENTRY("GetProcAddress (hModule=%p, lpProcName=%p (%s))\n",
hModule, lpProcName ? lpProcName : "NULL", lpProcName ? lpProcName : "NULL");
LockModuleList();
module = (MODSTRUCT *) hModule;
/* parameter validation */
if ((lpProcName == nullptr) || (*lpProcName == '\0'))
{
TRACE("No function name given\n");
SetLastError(ERROR_INVALID_PARAMETER);
goto done;
}
if (!LOADValidateModule(module))
{
TRACE("Invalid module handle %p\n", hModule);
SetLastError(ERROR_INVALID_HANDLE);
goto done;
}
/* try to assert on attempt to locate symbol by ordinal */
/* this can't be an exact test for HIWORD((DWORD)lpProcName) == 0
because of the address range reserved for ordinals contain can
be a valid string address on non-Windows systems
*/
if ((DWORD_PTR)lpProcName < VIRTUAL_PAGE_SIZE)
{
ASSERT("Attempt to locate symbol by ordinal?!\n");
}
// Get the symbol's address.
// If we're looking for a symbol inside the PAL, we try the PAL_ variant
// first because otherwise we run the risk of having the non-PAL_
// variant preferred over the PAL's implementation.
if (pal_module && module->dl_handle == pal_module->dl_handle)
{
int iLen = 4 + strlen(lpProcName) + 1;
LPSTR lpPALProcName = (LPSTR) alloca(iLen);
if (strcpy_s(lpPALProcName, iLen, "PAL_") != SAFECRT_SUCCESS)
{
ERROR("strcpy_s failed!\n");
SetLastError(ERROR_INSUFFICIENT_BUFFER);
goto done;
}
if (strcat_s(lpPALProcName, iLen, lpProcName) != SAFECRT_SUCCESS)
{
ERROR("strcat_s failed!\n");
SetLastError(ERROR_INSUFFICIENT_BUFFER);
goto done;
}
ProcAddress = (FARPROC) dlsym(module->dl_handle, lpPALProcName);
symbolName = lpPALProcName;
}
// If we aren't looking inside the PAL or we didn't find a PAL_ variant
// inside the PAL, fall back to a normal search.
if (ProcAddress == nullptr)
{
ProcAddress = (FARPROC) dlsym(module->dl_handle, lpProcName);
}
if (ProcAddress)
{
TRACE("Symbol %s found at address %p in module %p (named %S)\n",
lpProcName, ProcAddress, module, MODNAME(module));
/* if we don't know the module's full name yet, this is our chance to obtain it */
if (!module->lib_name && module->dl_handle)
{
const char* libName = PAL_dladdr((LPVOID)ProcAddress);
if (libName)
{
module->lib_name = UTIL_MBToWC_Alloc(libName, -1);
if (nullptr == module->lib_name)
{
ERROR("MBToWC failure; can't save module's full name\n");
}
else
{
TRACE("Saving full path of module %p as %s\n",
module, libName);
}
}
}
}
else
{
TRACE("Symbol %s not found in module %p (named %S), dlerror message is \"%s\"\n",
lpProcName, module, MODNAME(module), dlerror());
SetLastError(ERROR_PROC_NOT_FOUND);
}
done:
UnlockModuleList();
LOGEXIT("GetProcAddress returns FARPROC %p\n", ProcAddress);
PERF_EXIT(GetProcAddress);
return ProcAddress;
}
/*++
Function:
FreeLibrary
See MSDN doc.
--*/
BOOL
PALAPI
FreeLibrary(
IN OUT HMODULE hLibModule)
{
BOOL retval = FALSE;
PERF_ENTRY(FreeLibrary);
ENTRY("FreeLibrary (hLibModule=%p)\n", hLibModule);
retval = LOADFreeLibrary((MODSTRUCT *)hLibModule, TRUE /* fCallDllMain */);
LOGEXIT("FreeLibrary returns BOOL %d\n", retval);
PERF_EXIT(FreeLibrary);
return retval;
}
/*++
Function:
FreeLibraryAndExitThread
See MSDN doc.
--*/
PALIMPORT
VOID
PALAPI
FreeLibraryAndExitThread(
IN HMODULE hLibModule,
IN DWORD dwExitCode)
{
PERF_ENTRY(FreeLibraryAndExitThread);
ENTRY("FreeLibraryAndExitThread()\n");
FreeLibrary(hLibModule);
ExitThread(dwExitCode);
LOGEXIT("FreeLibraryAndExitThread\n");
PERF_EXIT(FreeLibraryAndExitThread);
}
/*++
Function:
GetModuleFileNameA
See MSDN doc.
Notes :
because of limitations in the dlopen() mechanism, this will only return the
full path name if a relative or absolute path was given to LoadLibrary, or
if the module was used in a GetProcAddress call. otherwise, this will return
the short name as given to LoadLibrary. The exception is if hModule is
NULL : in this case, the full path of the executable is always returned.
--*/
DWORD
PALAPI
GetModuleFileNameA(
IN HMODULE hModule,
OUT LPSTR lpFileName,
IN DWORD nSize)
{
INT name_length;
DWORD retval = 0;
LPWSTR wide_name = nullptr;
PERF_ENTRY(GetModuleFileNameA);
ENTRY("GetModuleFileNameA (hModule=%p, lpFileName=%p, nSize=%u)\n",
hModule, lpFileName, nSize);
LockModuleList();
if (hModule && !LOADValidateModule((MODSTRUCT *)hModule))
{
TRACE("Can't find name for invalid module handle %p\n", hModule);
SetLastError(ERROR_INVALID_HANDLE);
goto done;
}
wide_name = LOADGetModuleFileName((MODSTRUCT *)hModule);
if (!wide_name)
{
ASSERT("Can't find name for valid module handle %p\n", hModule);
SetLastError(ERROR_INTERNAL_ERROR);
goto done;
}
/* Convert module name to Ascii, place it in the supplied buffer */
name_length = WideCharToMultiByte(CP_ACP, 0, wide_name, -1, lpFileName,
nSize, nullptr, nullptr);
if (name_length == 0)
{
TRACE("Buffer too small to copy module's file name.\n");
SetLastError(ERROR_INSUFFICIENT_BUFFER);
goto done;
}
TRACE("File name of module %p is %s\n", hModule, lpFileName);
retval = name_length;
done:
UnlockModuleList();
LOGEXIT("GetModuleFileNameA returns DWORD %d\n", retval);
PERF_EXIT(GetModuleFileNameA);
return retval;
}
/*++
Function:
GetModuleFileNameW
See MSDN doc.
Notes :
because of limitations in the dlopen() mechanism, this will only return the
full path name if a relative or absolute path was given to LoadLibrary, or
if the module was used in a GetProcAddress call. otherwise, this will return
the short name as given to LoadLibrary. The exception is if hModule is
NULL : in this case, the full path of the executable is always returned.
--*/
DWORD
PALAPI
GetModuleFileNameW(
IN HMODULE hModule,
OUT LPWSTR lpFileName,
IN DWORD nSize)
{
INT name_length;
DWORD retval = 0;
LPWSTR wide_name = nullptr;
PERF_ENTRY(GetModuleFileNameW);
ENTRY("GetModuleFileNameW (hModule=%p, lpFileName=%p, nSize=%u)\n",
hModule, lpFileName, nSize);
LockModuleList();
wcscpy_s(lpFileName, nSize, W(""));
if (hModule && !LOADValidateModule((MODSTRUCT *)hModule))
{
TRACE("Can't find name for invalid module handle %p\n", hModule);
SetLastError(ERROR_INVALID_HANDLE);
goto done;
}
wide_name = LOADGetModuleFileName((MODSTRUCT *)hModule);
if (!wide_name)
{
TRACE("Can't find name for valid module handle %p\n", hModule);
SetLastError(ERROR_INTERNAL_ERROR);
goto done;
}
/* Copy module name into supplied buffer */
name_length = lstrlenW(wide_name);
if (name_length >= (INT)nSize)
{
TRACE("Buffer too small (%u) to copy module's file name (%u).\n", nSize, name_length);
SetLastError(ERROR_INSUFFICIENT_BUFFER);
goto done;
}
wcscpy_s(lpFileName, nSize, wide_name);
TRACE("file name of module %p is %S\n", hModule, lpFileName);
retval = name_length;
done:
UnlockModuleList();
LOGEXIT("GetModuleFileNameW returns DWORD %u\n", retval);
PERF_EXIT(GetModuleFileNameW);
return retval;
}
HMODULE
PALAPI
GetModuleHandleW(
IN OPTIONAL LPCWSTR lpModuleName)
{
if (lpModuleName)
{
// UNIXTODO: Implement this!
ASSERT("Needs Implementation!!!");
return nullptr;
}
return (HMODULE)&exe_module;
}
BOOL
PALAPI
GetModuleHandleExW(
IN DWORD dwFlags,
IN OPTIONAL LPCWSTR lpModuleName,
OUT HMODULE *phModule)
{
*phModule = NULL;
return FALSE;
}
/*
Function:
PAL_LoadLibraryDirect
Loads a library using a system call, without registering the library with the module list.
Returns the system handle to the loaded library, or nullptr upon failure (error is set via SetLastError()).
*/
void *
PALAPI
PAL_LoadLibraryDirect(
IN LPCWSTR lpLibFileName)
{
PathCharString pathstr;
CHAR * lpstr = nullptr;
INT name_length;
void *dl_handle = nullptr;
PERF_ENTRY(LoadLibraryDirect);
ENTRY("LoadLibraryDirect (lpLibFileName=%p (%S)) \n",
lpLibFileName ? lpLibFileName : W16_NULLSTRING,
lpLibFileName ? lpLibFileName : W16_NULLSTRING);
if (!LOADVerifyLibraryPath(lpLibFileName))
{
goto done;
}
lpstr = pathstr.OpenStringBuffer((PAL_wcslen(lpLibFileName)+1) * MaxWCharToAcpLength);
if (nullptr == lpstr)
{
goto done;
}
if (!LOADConvertLibraryPathWideStringToMultibyteString(lpLibFileName, lpstr, &name_length))
{
goto done;
}
/* do the Dos/Unix conversion on our own copy of the name */
FILEDosToUnixPathA(lpstr);
pathstr.CloseBuffer(name_length);
dl_handle = LOADLoadLibraryDirect(lpstr);
done:
LOGEXIT("LoadLibraryDirect returns HMODULE %p\n", dl_handle);
PERF_EXIT(LoadLibraryDirect);
return dl_handle;
}
/*
Function:
PAL_RegisterLibraryDirect
Registers a system handle to a loaded library with the module list.
Returns a PAL handle to the loaded library, or nullptr upon failure (error is set via SetLastError()).
*/
HMODULE
PALAPI
PAL_RegisterLibraryDirect(
IN void *dl_handle,
IN LPCWSTR lpLibFileName)
{
PathCharString pathstr;
CHAR * lpstr = nullptr;
INT name_length;
HMODULE hModule = nullptr;
PERF_ENTRY(RegisterLibraryDirect);
ENTRY("RegisterLibraryDirect (lpLibFileName=%p (%S)) \n",
lpLibFileName ? lpLibFileName : W16_NULLSTRING,
lpLibFileName ? lpLibFileName : W16_NULLSTRING);
if (!LOADVerifyLibraryPath(lpLibFileName))
{
goto done;
}
lpstr = pathstr.OpenStringBuffer((PAL_wcslen(lpLibFileName)+1) * MaxWCharToAcpLength);
if (nullptr == lpstr)
{
goto done;
}
if (!LOADConvertLibraryPathWideStringToMultibyteString(lpLibFileName, lpstr, &name_length))
{
goto done;
}
/* do the Dos/Unix conversion on our own copy of the name */
FILEDosToUnixPathA(lpstr);
pathstr.CloseBuffer(name_length);
/* let LOADRegisterLibraryDirect call SetLastError in case of failure */
LockModuleList();
hModule = LOADRegisterLibraryDirect((void *)dl_handle, lpstr, true /* fDynamic */);
UnlockModuleList();
done:
LOGEXIT("RegisterLibraryDirect returns HMODULE %p\n", hModule);
PERF_EXIT(RegisterLibraryDirect);
return hModule;
}
/*++
Function:
PAL_UnregisterModule
Used to cleanup the module HINSTANCE from PAL_RegisterModule.
--*/
VOID
PALAPI
PAL_UnregisterModule(
IN HINSTANCE hInstance)
{
PERF_ENTRY(PAL_UnregisterModule);
ENTRY("PAL_UnregisterModule(hInstance=%p)\n", hInstance);
LOADFreeLibrary((MODSTRUCT *)hInstance, FALSE /* fCallDllMain */);
LOGEXIT("PAL_UnregisterModule returns\n");
PERF_EXIT(PAL_UnregisterModule);
}
/*++
PAL_LOADLoadPEFile
Map a PE format file into memory like Windows LoadLibrary() would do.
Doesn't apply base relocations if the function is relocated.
Parameters:
IN hFile - file to map
Return value:
non-NULL - the base address of the mapped image
NULL - error, with last error set.
--*/
void *
PALAPI
PAL_LOADLoadPEFile(HANDLE hFile)
{
ENTRY("PAL_LOADLoadPEFile (hFile=%p)\n", hFile);
void * loadedBase = MAPMapPEFile(hFile);
#ifdef _DEBUG
if (loadedBase != nullptr)
{
char* envVar = getenv("PAL_ForcePEMapFailure");
if (envVar && strlen(envVar) > 0)
{
TRACE("Forcing failure of PE file map, and retry\n");
PAL_LOADUnloadPEFile(loadedBase); // unload it
loadedBase = MAPMapPEFile(hFile); // load it again
}
}
#endif // _DEBUG
LOGEXIT("PAL_LOADLoadPEFile returns %p\n", loadedBase);
return loadedBase;
}
/*++
PAL_LOADUnloadPEFile
Unload a PE file that was loaded by PAL_LOADLoadPEFile().
Parameters:
IN ptr - the file pointer returned by PAL_LOADLoadPEFile()
Return value:
TRUE - success
FALSE - failure (incorrect ptr, etc.)
--*/
BOOL
PALAPI
PAL_LOADUnloadPEFile(void * ptr)
{
BOOL retval = FALSE;
ENTRY("PAL_LOADUnloadPEFile (ptr=%p)\n", ptr);
if (nullptr == ptr)
{
ERROR( "Invalid pointer value\n" );
}
else
{
retval = MAPUnmapPEFile(ptr);
}
LOGEXIT("PAL_LOADUnloadPEFile returns %d\n", retval);
return retval;
}
/*++
PAL_GetSymbolModuleBase
Get base address of the module containing a given symbol
Parameters:
void *symbol - address of symbol
Return value:
module base address
--*/
LPCVOID
PALAPI
PAL_GetSymbolModuleBase(void *symbol)
{
LPCVOID retval = nullptr;
PERF_ENTRY(PAL_GetPalModuleBase);
ENTRY("PAL_GetPalModuleBase\n");
if (symbol == nullptr)
{
TRACE("Can't get base address. Argument symbol == nullptr\n");
SetLastError(ERROR_INVALID_DATA);
}
else
{
Dl_info info;
if (dladdr(symbol, &info) != 0)
{
retval = info.dli_fbase;
}
else
{
TRACE("Can't get base address of the current module\n");
SetLastError(ERROR_INVALID_DATA);
}
}
LOGEXIT("PAL_GetPalModuleBase returns %p\n", retval);
PERF_EXIT(PAL_GetPalModuleBase);
return retval;
}
/* Internal PAL functions *****************************************************/
/*++
Function :
LOADInitializeModules
Initialize the process-wide list of modules
Parameters :
None
Return value :
TRUE if initialization succeedded
FALSE otherwise
--*/
extern "C"
BOOL LOADInitializeModules()
{
_ASSERTE(exe_module.prev == nullptr);
InternalInitializeCriticalSection(&module_critsec);
// Initialize module for main executable
TRACE("Initializing module for main executable\n");
exe_module.self = (HMODULE)&exe_module;
exe_module.dl_handle = dlopen(nullptr, RTLD_LAZY);
if (exe_module.dl_handle == nullptr)
{
ERROR("Executable module will be broken : dlopen(nullptr) failed dlerror message is \"%s\" \n", dlerror());
return FALSE;
}
exe_module.lib_name = nullptr;
exe_module.refcount = -1;
exe_module.next = &exe_module;
exe_module.prev = &exe_module;
exe_module.pDllMain = nullptr;
exe_module.hinstance = nullptr;
exe_module.threadLibCalls = TRUE;
return TRUE;
}
/*++
Function :
LOADSetExeName
Set the exe name path
Parameters :
LPWSTR man exe path and name
Return value :
TRUE if initialization succeedded
FALSE otherwise
--*/
extern "C"
BOOL LOADSetExeName(LPWSTR name)
{
#if RETURNS_NEW_HANDLES_ON_REPEAT_DLOPEN
LPSTR pszExeName = nullptr;
#endif
BOOL result = FALSE;
LockModuleList();
// Save the exe path in the exe module struct
InternalFree(exe_module.lib_name);
exe_module.lib_name = name;
// For platforms where we can't trust the handle to be constant, we need to
// store the inode/device pairs for the modules we just initialized.
#if RETURNS_NEW_HANDLES_ON_REPEAT_DLOPEN
{
struct stat stat_buf;
pszExeName = UTIL_WCToMB_Alloc(name, -1);
if (nullptr == pszExeName)
{
ERROR("WCToMB failure, unable to get full name of exe\n");
goto exit;
}
if (-1 == stat(pszExeName, &stat_buf))
{
SetLastError(ERROR_MOD_NOT_FOUND);
goto exit;
}
TRACE("Executable has inode %d and device %d\n", stat_buf.st_ino, stat_buf.st_dev);
exe_module.inode = stat_buf.st_ino;
exe_module.device = stat_buf.st_dev;
}
#endif
result = TRUE;
#if RETURNS_NEW_HANDLES_ON_REPEAT_DLOPEN
exit:
if (pszExeName)
{
InternalFree(pszExeName);
}
#endif
UnlockModuleList();
return result;
}
/*++
Function :
LOADCallDllMain
Call DllMain for all modules (that have one) with the given "fwReason"
Parameters :
DWORD dwReason : parameter to pass down to DllMain, one of DLL_PROCESS_ATTACH, DLL_PROCESS_DETACH,
DLL_THREAD_ATTACH, DLL_THREAD_DETACH
LPVOID lpReserved : parameter to pass down to DllMain
If dwReason is DLL_PROCESS_ATTACH, lpvReserved is NULL for dynamic loads and non-NULL for static loads.
If dwReason is DLL_PROCESS_DETACH, lpvReserved is NULL if DllMain has been called by using FreeLibrary
and non-NULL if DllMain has been called during process termination.
(no return value)
Notes :
This is used to send DLL_THREAD_*TACH messages to modules
--*/
extern "C"
void LOADCallDllMain(DWORD dwReason, LPVOID lpReserved)
{
MODSTRUCT *module = nullptr;
BOOL InLoadOrder = TRUE; /* true if in load order, false for reverse */
CPalThread *pThread;
pThread = InternalGetCurrentThread();
if (UserCreatedThread != pThread->GetThreadType())
{
return;
}
/* Validate dwReason */
switch(dwReason)
{
case DLL_PROCESS_ATTACH:
ASSERT("got called with DLL_PROCESS_ATTACH parameter! Why?\n");
break;
case DLL_PROCESS_DETACH:
ASSERT("got called with DLL_PROCESS_DETACH parameter! Why?\n");
InLoadOrder = FALSE;
break;
case DLL_THREAD_ATTACH:
TRACE("Calling DllMain(DLL_THREAD_ATTACH) on all known modules.\n");
break;
case DLL_THREAD_DETACH:
TRACE("Calling DllMain(DLL_THREAD_DETACH) on all known modules.\n");
InLoadOrder = FALSE;
break;
default:
ASSERT("LOADCallDllMain called with unknown parameter %d!\n", dwReason);
return;
}
LockModuleList();
module = &exe_module;
do
{
if (!InLoadOrder)
module = module->prev;
if (module->threadLibCalls)
{
if (module->pDllMain)
{
LOADCallDllMainSafe(module, dwReason, lpReserved);