Theodosius v3.0
Jit linker, symbol mapper, and obfuscator
Public Member Functions
theo::recomp::recomp_t Class Reference

the main class responsible for recomposition More...

#include "recomp.hpp"

Public Member Functions

 recomp_t (decomp::decomp_t *dcmp, allocator_t alloc, copier_t copy, resolver_t resolve)
 the explicit constructor for the recomp_t class. More...
 
void allocate ()
 when called, this function allocates space for every symbol. More...
 
void resolve ()
 when called, this function resolves all relocations in every symbol. More...
 
void copy_syms ()
 when called, this function copies symbols into allocations. More...
 
void allocator (allocator_t alloc)
 setter for the allocater lambda function. More...
 
void copier (copier_t copy)
 setter for the copier lambda function. More...
 
void resolver (resolver_t resolve)
 setter for the resolve lambda function. More...
 
std::uintptr_t resolve (const std::string &&sym)
 resolves the address of a function given its name. More...
 

Detailed Description

the main class responsible for recomposition

Definition at line 64 of file recomp.hpp.

Constructor & Destructor Documentation

◆ recomp_t()

theo::recomp::recomp_t::recomp_t ( decomp::decomp_t dcmp,
allocator_t  alloc,
copier_t  copy,
resolver_t  resolve 
)
explicit

the explicit constructor for the recomp_t class.

Parameters
dcmppointer to a decomp_t class.
alloclambda function which is used to allocate memory for symbols.
copylambda function used to copy bytes into allocations.
resolvelambda function used to resolve external symbols.

Definition at line 34 of file recomp.cpp.

38 : m_dcmp(dcmp), m_allocator(alloc), m_copier(copy), m_resolver(resolve) {}

Member Function Documentation

◆ allocate()

void theo::recomp::recomp_t::allocate ( )

when called, this function allocates space for every symbol.

Definition at line 40 of file recomp.cpp.

40 {
41 // map code & data/rdata/bss sections first...
42 //
43 m_dcmp->syms()->for_each([&](theo::decomp::symbol_t& sym) {
44 switch (sym.type()) {
45 case decomp::sym_type_t::section:
46 case decomp::sym_type_t::function:
47 case decomp::sym_type_t::instruction: {
48 sym.allocated_at(m_allocator(sym.size(), sym.scn()->characteristics));
49 break;
50 }
51 default:
52 break;
53 }
54 });
55
56 // then map data/rdata/bss symbols to the allocated sections...
57 //
58 m_dcmp->syms()->for_each([&](theo::decomp::symbol_t& sym) {
59 if (sym.type() == decomp::sym_type_t::data) {
60 // if the symbol has a section then we will refer to the allocation made
61 // for that section...
62 //
63 if (sym.scn()) {
64 auto scn_sym =
65 m_dcmp->syms()->sym_from_hash(m_dcmp->scn_hash_tbl()[sym.scn()]);
66
67 if (!scn_sym.has_value()) {
68 spdlog::error("failed to locate section: {} for symbol: {}",
69 sym.scn()->name.to_string(), sym.name());
70
71 assert(scn_sym.has_value());
72 }
73
74 sym.allocated_at(scn_sym.value()->allocated_at() + sym.offset());
75 } else { // else if there is no section then we allocate based upon the
76 // size of the symbol... this is only done for symbols that are
77 // bss...
78 //
79
80 // bss is read write...
81 //
82 coff::section_characteristics_t prot = {};
83 prot.mem_read = true;
84 prot.mem_write = true;
85
86 sym.allocated_at(m_allocator(sym.size(), sym.scn()->characteristics));
87 }
88 }
89 });
90}

References theo::recomp::symbol_table_t::for_each(), theo::decomp::decomp_t::syms(), and theo::decomp::symbol_t::type().

Referenced by theo::theo_t::compose().

◆ allocator()

void theo::recomp::recomp_t::allocator ( allocator_t  alloc)

setter for the allocater lambda function.

Parameters
alloclambda function which allocates memory for symbols.

Definition at line 163 of file recomp.cpp.

163 {
164 m_allocator = alloc;
165}

◆ copier()

void theo::recomp::recomp_t::copier ( copier_t  copy)

setter for the copier lambda function.

Parameters
copycopier lambda function used to copy bytes into allocations made by the allocator.

Definition at line 167 of file recomp.cpp.

167 {
168 m_copier = copy;
169}

◆ copy_syms()

void theo::recomp::recomp_t::copy_syms ( )

when called, this function copies symbols into allocations.

Definition at line 155 of file recomp.cpp.

155 {
156 // copy symbols into memory using the copier supplied...
157 //
158 m_dcmp->syms()->for_each([&](theo::decomp::symbol_t& sym) {
159 m_copier(sym.allocated_at(), sym.data().data(), sym.data().size());
160 });
161}

References theo::decomp::symbol_t::allocated_at(), and theo::decomp::symbol_t::data().

Referenced by theo::theo_t::compose().

◆ resolve() [1/2]

void theo::recomp::recomp_t::resolve ( )

when called, this function resolves all relocations in every symbol.

Definition at line 92 of file recomp.cpp.

92 {
93 // resolve relocations in all symbols...
94 //
95 m_dcmp->syms()->for_each([&](theo::decomp::symbol_t& sym) {
96 auto& relocs = sym.relocs();
97 std::for_each(relocs.begin(), relocs.end(), [&](reloc_t& reloc) {
98 if (reloc.offset() > sym.data().size()) {
99 spdlog::error(
100 "invalid relocation... writing outside of symbol length... offset: "
101 "{} sym size: {}",
102 sym.offset(), sym.data().size());
103
104 assert(reloc.offset() > sym.data().size());
105 }
106
107 // try and resolve the symbol by refering to the internal symbol table
108 // first... if there is no symbol then refer to the resolver...
109 //
110 auto reloc_sym = m_dcmp->syms()->sym_from_hash(reloc.hash());
111 auto allocated_at = reloc_sym.has_value()
112 ? reloc_sym.value()->allocated_at()
113 : m_resolver(reloc.name());
114
115 if (!allocated_at) {
116 spdlog::error("failed to resolve reloc from symbol: {} to symbol: {}",
117 sym.name(), reloc.name());
118
119 assert(allocated_at);
120 }
121
122 switch (sym.type()) {
124 auto scn_sym =
125 m_dcmp->syms()->sym_from_hash(m_dcmp->scn_hash_tbl()[sym.scn()]);
126
127 *reinterpret_cast<std::uintptr_t*>(scn_sym.value()->data().data() +
128 reloc.offset()) = allocated_at;
129 break;
130 }
132 *reinterpret_cast<std::uintptr_t*>(sym.data().data() +
133 reloc.offset()) = allocated_at;
134 break;
135 }
137 auto& transforms = reloc.get_transforms();
138 std::for_each(
139 transforms.begin(), transforms.end(),
140 [&](std::pair<obf::transform::transform_t*, std::uint32_t>& t) {
141 allocated_at = (*t.first)(allocated_at, t.second);
142 });
143
144 *reinterpret_cast<std::uintptr_t*>(sym.data().data() +
145 reloc.offset()) = allocated_at;
146 break;
147 }
148 default:
149 break;
150 }
151 });
152 });
153}

References theo::decomp::symbol_t::data(), theo::decomp::function, theo::recomp::reloc_t::get_transforms(), theo::recomp::reloc_t::hash(), theo::decomp::instruction, theo::recomp::reloc_t::name(), theo::decomp::symbol_t::name(), theo::recomp::reloc_t::offset(), theo::decomp::symbol_t::relocs(), theo::decomp::symbol_t::scn(), theo::decomp::section, and theo::decomp::symbol_t::type().

Referenced by theo::theo_t::compose().

◆ resolve() [2/2]

std::uintptr_t theo::recomp::recomp_t::resolve ( const std::string &&  sym)

resolves the address of a function given its name.

Parameters
symthe name of the symbol to resolve the location of.
Returns
the address of the symbol.

Definition at line 175 of file recomp.cpp.

175 {
176 auto res = m_dcmp->syms()->sym_from_hash(decomp::symbol_t::hash(sym));
177 return res.has_value() ? res.value()->allocated_at() : 0;
178}

References theo::decomp::symbol_t::hash().

◆ resolver()

void theo::recomp::recomp_t::resolver ( resolver_t  resolve)

setter for the resolve lambda function.

Parameters
resolvelambda function to resolve external symbols.

Definition at line 171 of file recomp.cpp.

171 {
172 m_resolver = resolve;
173}

The documentation for this class was generated from the following files: