summaryrefslogtreecommitdiffhomepage
path: root/dev/ZKA/Sources/PEFDLLInterface.cxx
blob: 0ca6b85ef1e9a5b24778cdd35c63fc1a96173868 (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
/*
 * ========================================================
 *
 * newoskrnl
 * Copyright ZKA Technologies., all rights reserved.
 *
 *  ========================================================
 */

#include <KernelKit/DebugOutput.hxx>
#include <KernelKit/PEF.hxx>
#include <KernelKit/PEFDLLInterface.hxx>
#include <KernelKit/ProcessScheduler.hxx>
#include <KernelKit/ThreadLocalStorage.hxx>
#include <NewKit/Defines.hxx>

/* -------------------------------------------

 Revision History:

	 01/02/24: Rework shared sharedObj ABI, except a rtl_init_shared_object and
 rtl_fini_shared_object (amlel) 15/02/24: Breaking changes, changed the name of the
 routines. (amlel)

	07/28/24: Replace rt_library_free with rtl_fini_shared_object

 ------------------------------------------- */

using namespace Kernel;

/***********************************************************************************/
/// @file PEFSharedObjectRT.cxx
/// @brief PEF's shared object runtime.
/***********************************************************************************/

/***********************************************************************************/
/** @brief Library initializer. */
/***********************************************************************************/

EXTERN_C DLLInterfacePtr rtl_init_shared_object(PROCESS_HEADER_BLOCK* header)
{
	DLLInterfacePtr sharedObj = tls_new_class<PEFDLLInterface>();

	if (!sharedObj)
	{
		header->Crash();

		return nullptr;
	}

	sharedObj->Mount(tls_new_class<PEFDLLInterface::DLL_TRAITS>());

	if (!sharedObj->Get())
	{
		header->Crash();

		return nullptr;
	}

	sharedObj->Get()->fImageObject =
		header->Image;

	if (!sharedObj->Get()->fImageObject)
	{
		header->Crash();

		return nullptr;
	}

	sharedObj->Get()->fImageEntrypointOffset =
		sharedObj->Load<VoidPtr>(kPefStart, rt_string_len(kPefStart, 0), kPefCode);

	return sharedObj;
}

/***********************************************************************************/
/** @brief Frees the sharedObj. */
/** @note Please check if the lib got freed! */
/** @param lib The sharedObj to free. */
/** @param successful Reports if successful or not. */
/***********************************************************************************/

EXTERN_C Void rtl_fini_shared_object(PROCESS_HEADER_BLOCK* header, DLLInterfacePtr lib, Bool* successful)
{
	MUST_PASS(successful);

	// sanity check (will also trigger a bug check if this fails)
	if (lib == nullptr)
	{
		*successful = false;
		header->Crash();
	}

	delete lib->Get();
	delete lib;

	lib = nullptr;

	*successful = true;
}