Theodosius  v3.0
Jit linker, mapper, obfuscator, and mutator
Public Member Functions | List of all members
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

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.
38  : m_dcmp(dcmp), m_allocator(alloc), m_copier(copy), m_resolver(resolve) {}
void resolve()
when called, this function resolves all relocations in every symbol.
Definition: recomp.cpp:92

Member Function Documentation

◆ allocate()

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

when called, this function allocates space for every symbol.

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 }
recomp::symbol_table_t * syms()
gets the symbol table.
Definition: decomp.cpp:293
symbol_t is an abstraction upon the coff symbol. this allows for easier manipulation of the symbol....
Definition: symbol.hpp:59
sym_type_t type() const
returns the type of the symbol.
Definition: symbol.cpp:80
coff::section_header_t * scn() const
gets the section header of the section in which the symbol is contained.
Definition: symbol.cpp:64
std::uint32_t size() const
returns the size of the symbol.
Definition: symbol.cpp:72
std::uintptr_t allocated_at() const
returns the address where the symbol is allocated.
Definition: symbol.cpp:60
void for_each(std::function< void(decomp::symbol_t &sym)> fn)
this function is a wrapper function that allows you to get at each entry in the symbol table by refer...
Definition: symbol_table.cpp:49
@ data
Definition: symbol.hpp:46

◆ allocator()

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

setter for the allocater lambda function.

Parameters
alloclambda function which allocates memory for symbols.
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.
167  {
168  m_copier = copy;
169 }

◆ copy_syms()

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

when called, this function copies symbols into allocations.

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 }
std::vector< std::uint8_t > & data()
returns a vector by reference of bytes containing the data of the symbol.
Definition: symbol.cpp:76

◆ resolve() [1/2]

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

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

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 }
std::map< coff::section_header_t *, std::size_t > & scn_hash_tbl()
gets the section hash table section header --> hash of the section header ptr.
Definition: decomp.cpp:297
std::string name() const
gets the name of the symbol.
Definition: symbol.cpp:52
std::vector< recomp::reloc_t > & relocs()
returns a vector of relocations.
Definition: symbol.cpp:96
std::optional< decomp::symbol_t * > sym_from_hash(std::size_t hash)
returns an optional pointer to a symbol from the symbol table given the symbols hash (hash of its nam...
Definition: symbol_table.cpp:54
@ section
Definition: symbol.hpp:47
@ instruction
Definition: symbol.hpp:45
@ function
Definition: symbol.hpp:44

◆ 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.
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 }
std::size_t hash()
gets the hash of the symbol name.
Definition: symbol.cpp:88

◆ resolver()

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

setter for the resolve lambda function.

Parameters
resolvelambda function to resolve external symbols.
171  {
172  m_resolver = resolve;
173 }

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