summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/src/IPEFDylibObject.cc
blob: d1d4eeca6208d1fd8d6634bf8cf96a5252acbb71 (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
/*
 * ========================================================
 *
 * NeKernel
 * Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
 *
 *  ========================================================
 */

#include <KernelKit/DebugOutput.h>
#include <KernelKit/IPEFDylibObject.h>
#include <KernelKit/PEF.h>
#include <KernelKit/ProcessScheduler.h>
#include <KernelKit/ThreadLocalStorage.h>
#include <NeKit/Defines.h>

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

 Revision History:

  01/02/24: Reworked dll ABI, expect a rtl_init_dylib_pef and
 rtl_fini_dylib_pef (amlel)

  15/02/24: Breaking changes, changed the name of the
 routines. (amlel)

  07/28/24: Replace rt_library_free with rtl_fini_dylib_pef

  10/8/24: FIX: Fix log comment.

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

using namespace Kernel;

/***********************************************************************************/
/// @file IPEFDylibObject.cc
/// @brief PEF's Dylib runtime.
/***********************************************************************************/

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

EXTERN_C IDylibRef rtl_init_dylib_pef(USER_PROCESS& process) {
  IDylibRef dll_obj = tls_new_class<IPEFDylibObject>();

  if (!dll_obj) {
    process.Crash();
    return nullptr;
  }

  dll_obj->Mount(new IPEFDylibObject::DylibTraits());

  if (!dll_obj->Get()) {
    tls_delete_class(dll_obj);
    dll_obj = nullptr;

    process.Crash();

    return nullptr;
  }

  dll_obj->Get()->ImageObject = process.Image.LeakBlob().Leak().Leak();

  if (!dll_obj->Get()->ImageObject) {
    delete dll_obj->Get();

    tls_delete_class(dll_obj);
    dll_obj = nullptr;

    process.Crash();

    return nullptr;
  }

  dll_obj->Get()->ImageEntrypointOffset =
      dll_obj->Load<VoidPtr>(kPefStart, rt_string_len(kPefStart, 0), kPefCode);

  return dll_obj;
}

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

EXTERN_C Void rtl_fini_dylib_pef(USER_PROCESS& process, IDylibRef dll_obj, BOOL* successful) {
  MUST_PASS(successful);

  // sanity check (will also trigger a bug check if this fails)
  if (dll_obj == nullptr) {
    *successful = false;
    process.Crash();
  }

  delete dll_obj->Get();
  delete dll_obj;

  dll_obj = nullptr;

  *successful = true;
}