This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
coreclrloader.cpp
81 lines (76 loc) · 2.27 KB
/
coreclrloader.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include <stdio.h>
#include <assert.h>
#include "dlfcn.h"
#include "coreclrloader.h"
#include "coreruncommon.h"
using namespace std;
void *CoreCLRLoader::LoadFunction(const char *funcName)
{
void *func = nullptr;
if (coreclrLib == nullptr)
{
fprintf(stderr, "Error: coreclr should be loaded before loading a function: %s\n", funcName);
}
else
{
func = dlsym(coreclrLib, funcName);
if (func == nullptr)
{
fprintf(stderr, "Error: cannot find %s in coreclr\n", funcName);
}
}
return func;
}
CoreCLRLoader* CoreCLRLoader::Create(const char *exePath)
{
string absolutePath, coreClrPath;
bool success = GetAbsolutePath(exePath, absolutePath);
if (!success)
{
success = GetEntrypointExecutableAbsolutePath(absolutePath);
assert(success);
}
GetDirectory(absolutePath.c_str(), coreClrPath);
coreClrPath.append("/");
coreClrPath.append(coreClrDll);
CoreCLRLoader *loader = new CoreCLRLoader();
loader->coreclrLib = dlopen(coreClrPath.c_str(), RTLD_NOW | RTLD_LOCAL);
if (loader->coreclrLib == nullptr)
{
fprintf(stderr, "Error: Fail to load %s\n", coreClrPath.c_str());
delete loader;
return nullptr;
}
else
{
loader->initializeCoreCLR = (InitializeCoreCLRFunction)loader->LoadFunction("coreclr_initialize");
loader->shutdownCoreCLR = (ShutdownCoreCLRFunction)loader->LoadFunction("coreclr_shutdown");
int ret = loader->initializeCoreCLR(
exePath,
"coreclrloader",
0,
0,
0,
&loader->hostHandle,
&loader->domainId);
if (ret != 0)
{
fprintf(stderr, "Error: Fail to initialize CoreCLR\n");
delete loader;
return nullptr;
}
}
return loader;
}
int CoreCLRLoader::Finish()
{
if (hostHandle != 0)
{
shutdownCoreCLR(hostHandle, domainId);
delete this;
}
return 0;
}