summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/KernelKit/IPEFDylibObject.h
blob: 22360e508a4a325641cf52eed8927cb44771843b (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
/*
 *      ========================================================
 *
 *      Kernel
 *      Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved.
 *
 *      ========================================================
 */

#ifndef __KERNELKIT_SHARED_OBJECT_H__
#define __KERNELKIT_SHARED_OBJECT_H__

#include <KernelKit/PEF.h>
#include <NewKit/Defines.h>
#include <KernelKit/PEFCodeMgr.h>
#include <KernelKit/UserProcessScheduler.h>
#include <KernelKit/IDylibObject.h>

namespace Kernel
{
	/**
	 * @brief Shared Library class
	 * Load library from this class
	 */
	class IPEFDylibObject final NE_DYLIB_OBJECT
	{
	public:
		explicit IPEFDylibObject() = default;
		~IPEFDylibObject()		   = default;

	public:
		NE_COPY_DEFAULT(IPEFDylibObject);

	private:
		DLL_TRAITS* fMounted{nullptr};

	public:
		DLL_TRAITS** GetAddressOf()
		{
			return &fMounted;
		}

		DLL_TRAITS* Get()
		{
			return fMounted;
		}

	public:
		void Mount(DLL_TRAITS* to_mount)
		{
			if (!to_mount || !to_mount->ImageObject)
				return;

			fMounted = to_mount;

			if (fLoader && to_mount)
			{
				delete fLoader;
				fLoader = nullptr;
			}

			if (!fLoader)
			{
				fLoader = new PEFLoader(fMounted->ImageObject);
			}
		}

		void Unmount()
		{
			if (fMounted)
				fMounted = nullptr;
		};

		template <typename SymbolType>
		SymbolType Load(const Char* symbol_name, SizeT len, Int32 kind)
		{
			if (symbol_name == nullptr || *symbol_name == 0)
				return nullptr;
			if (len > kPathLen || len < 1)
				return nullptr;

			auto ret =
				reinterpret_cast<SymbolType>(fLoader->FindSymbol(symbol_name, kind));

			if (!ret)
			{
				if (kind == kPefCode)
					return (VoidPtr)&__zka_pure_call;

				return nullptr;
			}

			return ret;
		}

	private:
		PEFLoader* fLoader{nullptr};
	};

	typedef IPEFDylibObject* IDylibRef;

	EXTERN_C IDylibRef rtl_init_dylib(UserProcess& header);
	EXTERN_C Void	   rtl_fini_dylib(UserProcess& header, IDylibRef lib, Bool* successful);
} // namespace Kernel

#endif /* ifndef __KERNELKIT_SHARED_OBJECT_H__ */