summaryrefslogtreecommitdiff
path: root/src/ildasm/unixcoreclrloader/coreclrloader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ildasm/unixcoreclrloader/coreclrloader.cpp')
-rw-r--r--src/ildasm/unixcoreclrloader/coreclrloader.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/ildasm/unixcoreclrloader/coreclrloader.cpp b/src/ildasm/unixcoreclrloader/coreclrloader.cpp
new file mode 100644
index 0000000000..11c5bd4d10
--- /dev/null
+++ b/src/ildasm/unixcoreclrloader/coreclrloader.cpp
@@ -0,0 +1,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;
+}