Theodosius v3.0
Jit linker, symbol mapper, and obfuscator
recomp.hpp
Go to the documentation of this file.
1// Copyright (c) 2022, _xeroxz
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9//
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13//
14// 3. Neither the name of the copyright holder nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28// POSSIBILITY OF SUCH DAMAGE.
29//
30
31#pragma once
32#include <decomp/decomp.hpp>
33#include <obf/engine.hpp>
35
36/// <summary>
37/// this namespace encompasses all recomposition related code.
38/// </summary>
39namespace theo::recomp {
40
41/// <summary>
42/// a function which is called by recomp_t to resolve external symbols
43/// </summary>
44using resolver_t = std::function<std::uintptr_t(std::string)>;
45
46/// <summary>
47/// a function which is called by recomp_t to copy symbols into memory.
48/// </summary>
49using copier_t = std::function<void(std::uintptr_t, void*, std::uint32_t)>;
50
51/// <summary>
52/// a function which is called to allocate space for a symbol.
53///
54/// the first param is the size of the symbol, the second param is the
55/// characteristics of the section which the symbol is allocated in.
56/// </summary>
58 std::function<std::uintptr_t(std::uint32_t,
59 coff::section_characteristics_t)>;
60
61/// <summary>
62/// the main class responsible for recomposition
63/// </summary>
64class recomp_t {
65 public:
66 /// <summary>
67 /// the explicit constructor for the recomp_t class.
68 /// </summary>
69 /// <param name="dcmp">pointer to a decomp_t class.</param>
70 /// <param name="alloc">lambda function which is used to allocate memory for
71 /// symbols.</param> <param name="copy">lambda function used to copy bytes
72 /// into allocations.</param> <param name="resolve">lambda function used to
73 /// resolve external symbols.</param>
74 explicit recomp_t(decomp::decomp_t* dcmp,
75 allocator_t alloc,
76 copier_t copy,
78
79 /// <summary>
80 /// when called, this function allocates space for every symbol.
81 /// </summary>
82 void allocate();
83
84 /// <summary>
85 /// when called, this function resolves all relocations in every symbol.
86 /// </summary>
87 void resolve();
88
89 /// <summary>
90 /// when called, this function copies symbols into allocations.
91 /// </summary>
92 void copy_syms();
93
94 /// <summary>
95 /// setter for the allocater lambda function.
96 /// </summary>
97 /// <param name="alloc">lambda function which allocates memory for
98 /// symbols.</param>
99 void allocator(allocator_t alloc);
100
101 /// <summary>
102 /// setter for the copier lambda function.
103 /// </summary>
104 /// <param name="copy">copier lambda function used to copy bytes into
105 /// allocations made by the allocator.</param>
106 void copier(copier_t copy);
107
108 /// <summary>
109 /// setter for the resolve lambda function.
110 /// </summary>
111 /// <param name="resolve">lambda function to resolve external symbols.</param>
113
114 /// <summary>
115 /// resolves the address of a function given its name.
116 /// </summary>
117 /// <param name="sym">the name of the symbol to resolve the location
118 /// of.</param> <returns>the address of the symbol.</returns>
119 std::uintptr_t resolve(const std::string&& sym);
120
121 private:
122 decomp::decomp_t* m_dcmp;
123 resolver_t m_resolver;
124 copier_t m_copier;
125 allocator_t m_allocator;
126};
127} // namespace theo::recomp