summaryrefslogtreecommitdiff
path: root/src/pal/src/include/pal/module.h
blob: 803ca36dc7adb09c80cf2de64e6b1eda0e431371 (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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. 
//

/*++



Module Name:

    include/pal/module.h

Abstract:
    Header file for modle management utilities.



--*/

#ifndef _PAL_MODULE_H_
#define _PAL_MODULE_H_

#if defined(CORECLR) && defined(__APPLE__)

#include <CoreFoundation/CFBundle.h>

// Name of the CoreCLR bundle executable
#define CORECLR_BUNDLE_NAME "coreclr"

// Name of the CoreCLR bundle root directory.
#define CORECLR_BUNDLE_DIR "CoreCLR.bundle"

// Directory components between the bundle root and the executable.
#define CORECLR_BUNDLE_PATH "Contents/MacOS/"

// Abstract the API used to load and query for functions in the CoreCLR binary to make it easier to change the
// underlying implementation.
typedef CFBundleRef CORECLRHANDLE;
#endif // CORECLR && __APPLE__

#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus

#define PAL_SHLIB_PREFIX "lib"

#if __APPLE__
#define PAL_SHLIB_SUFFIX ".dylib"
#elif _AIX
#define PAL_SHLIB_SUFFIX ".a"
#elif _HPUX_
#define PAL_SHLIB_SUFFIX ".sl"
#else
#define PAL_SHLIB_SUFFIX ".so"
#endif

typedef BOOL (__stdcall *PDLLMAIN)(HINSTANCE,DWORD,LPVOID); /* entry point of module */

typedef struct _MODSTRUCT
{
    HMODULE self;         /* circular reference to this module */
    void *dl_handle;      /* handle returned by dlopen() */
#if defined(CORECLR) && defined(__APPLE__)
    CORECLRHANDLE sys_module; /* System modules can be loaded via mechanisms other than dlopen() under
                               * CoreCLR/Mac */
#endif // CORECLR && __APPLE__
    LPWSTR lib_name;      /* full path of module */
    INT refcount;         /* reference count */
                          /* -1 means infinite reference count - module is never released */
    BOOL ThreadLibCalls;  /* TRUE for DLL_THREAD_ATTACH/DETACH notifications 
                              enabled, FALSE if they are disabled */

#if RETURNS_NEW_HANDLES_ON_REPEAT_DLOPEN
    ino_t inode;
    dev_t device;
#endif

    PDLLMAIN pDllMain; /* entry point of module */

    /* reference to next and previous modules in list (in load order) */
    struct _MODSTRUCT *next;
    struct _MODSTRUCT *prev;
} MODSTRUCT;

extern MODSTRUCT pal_module;


/*++
Function :
    LoadInitializeModules

    Initialize the process-wide list of modules (2 initial modules : 1 for
    the executable and 1 for the PAL)

Parameters :
    LPWSTR exe_name : full path to executable

Return value :
    TRUE on success, FALSE on failure

Notes :
    the module manager takes ownership of the string
--*/
BOOL LOADInitializeModules(LPWSTR exe_name);

/*++
Function :
    LOADFreeModules

    Release all resources held by the module manager (including dlopen handles)

Parameters:
    BOOL bTerminateUnconditionally: If TRUE, this will avoid calling any DllMains

    (no return value)
--*/
void LOADFreeModules(BOOL bTerminateUnconditionally);

/*++
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
--*/
void LOADCallDllMain(DWORD dwReason, LPVOID lpReserved);

/*++
Function:
  LockModuleList

Abstract
  Enter the critical section associated to the module list

Parameter
  void

Return
  void
--*/
void LockModuleList();

/*++
Function:
  UnlockModuleList

Abstract
  Leave the critical section associated to the module list

Parameter
  void

Return
  void
--*/
void UnlockModuleList();

/*++
Function:
  PAL_LOADLoadPEFile

Abstract
  Loads a PE file into memory.  Properly maps all of the sections in the PE file.  Returns a pointer to the
  loaded base.

Parameters:
    IN hFile    - The file to load

Return value:
    A valid base address if successful.
    0 if failure
--*/
void * PAL_LOADLoadPEFile(HANDLE hFile);

/*++
    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 PAL_LOADUnloadPEFile(void * ptr);


#if !defined(CORECLR) || !defined(__APPLE__)
/*++
    LOADGetLibRotorPalSoFileName

    Retrieve the full path of the librotor_pal.so being used.

Parameters:
    OUT pwzBuf - WCHAR buffer of MAX_PATH length to receive file name

Return value:
    0 if successful
    -1 if failure, with last error set.
--*/
int LOADGetLibRotorPalSoFileName(LPSTR pszBuf);
#endif // !CORECLR || !__APPLE__

/*++
    LOADInitCoreCLRModules

    Run the initialization methods for CoreCLR modules that used to be standalone dynamic libraries (PALRT and
    mscorwks).

Parameters:
    Core CLR path

Return value:
    TRUE if successful
    FALSE if failure
--*/
BOOL LOADInitCoreCLRModules(const char *szCoreCLRPath);

#if defined(CORECLR) && defined(__APPLE__)
// Abstract the API used to load and query for functions in the CoreCLR binary to make it easier to change the
// underlying implementation.

// Load the CoreCLR module into memory given the directory in which it resides. Returns NULL on failure.
CORECLRHANDLE LoadCoreCLR(const char *szPath);

// Lookup the named function in the given CoreCLR image. Returns NULL on failure.
void *LookupFunctionInCoreCLR(CORECLRHANDLE hCoreCLR, const char *szFunction);

// Locate the CoreCLR module handle associated with the code currently executing. Returns NULL on failure.
CORECLRHANDLE FindCoreCLRHandle();

#endif // CORECLR && __APPLE__

#ifdef __cplusplus
}
#endif // __cplusplus

#endif /* _PAL_MODULE_H_ */