summaryrefslogtreecommitdiffhomepage
path: root/Kernel/Sources/PEFSharedObject.cxx
blob: 06825a3cb469bff57eb62cec6525e89c4618d412 (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
/*
 * ========================================================
 *
 * NewOS
 * Copyright SoftwareLabs, all rights reserved.
 *
 *  ========================================================
 */

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

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

 Revision History:

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

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

using namespace NewOS;

/***********************************************************************************/
/// @file SharedObjectRT.cxx
/// @brief Shared Object runtime.
/***********************************************************************************/

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

EXTERN_C SharedObjectPtr rt_library_init(void)
{
	SharedObjectPtr library = tls_new_class<SharedObject>();

	if (!library)
	{
		ProcessScheduler::The().Leak().GetCurrent().Leak().Crash();

		return nullptr;
	}

	library->Mount(tls_new_class<SharedObject::SharedObjectTrait>());

	if (!library->Get())
	{
		ProcessScheduler::The().Leak().GetCurrent().Leak().Crash();

		return nullptr;
	}

	library->Get()->fImageObject =
		ProcessScheduler::The().Leak().GetCurrent().Leak().Image;

	if (!library->Get()->fImageObject)
	{
		ProcessScheduler::The().Leak().GetCurrent().Leak().Crash();

		return nullptr;
	}

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

	return library;
}

/***********************************************************************************/
/* @brief Ends the library. */
/* @note Please check if the lib got freed! */
/* @param SharedObjectPtr the library to free. */
/***********************************************************************************/

EXTERN_C Void rt_library_free(SharedObjectPtr lib, bool* successful)
{
	MUST_PASS(successful);

	// sanity check (will also trigger a bug check if this fails)
	if (lib == nullptr)
	{
		*successful = false;
		ProcessScheduler::The().Leak().GetCurrent().Leak().Crash();
	}

	delete lib->Get();
	delete lib;

	lib = nullptr;

	*successful = true;
}

/***********************************************************************************/

/// @brief Unimplemented function (crashes by default)
/// @param
EXTERN_C void __mh_purecall(void)
{
	kcout << "newoskrnl: unimplemented symbol!\r";
}