Theodosius  v3.0
Jit linker, mapper, obfuscator, and mutator
decomp.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 <spdlog/spdlog.h>
33 #include <cstdint>
34 #include <linuxpe>
35 #include <optional>
36 #include <set>
37 #include <tuple>
38 #include <vector>
39 
40 #include <decomp/routine.hpp>
41 #include <recomp/symbol_table.hpp>
42 
43 #include <coff/archive.hpp>
44 #include <coff/image.hpp>
45 
49 namespace theo::decomp {
50 
55 using sym_data_t = std::tuple<coff::image_t*, coff::symbol_t*, std::uint32_t>;
56 
61 class decomp_t {
62  public:
69  explicit decomp_t(std::vector<std::uint8_t>& lib,
71 
76  std::vector<routine_t> rtns();
77 
82  std::vector<std::uint8_t> lib();
83 
88  std::vector<std::vector<std::uint8_t>> objs();
89 
95 
102  std::map<coff::section_header_t*, std::size_t>& scn_hash_tbl();
103 
111  std::optional<recomp::symbol_table_t*> decompose(std::string& entry_sym);
112 
113  private:
119  std::uint32_t ext_used_syms(const std::string&& entry_sym);
120 
126  std::optional<sym_data_t> get_symbol(const std::string_view& name);
127 
136  std::uint32_t next_sym(coff::image_t* img,
137  coff::section_header_t* hdr,
138  coff::symbol_t* s);
139 
140  const std::vector<std::uint8_t> m_lib;
141  std::vector<std::vector<std::uint8_t>> m_objs;
142  std::vector<routine_t> m_rtns;
143  std::set<sym_data_t> m_used_syms;
144  std::set<coff::image_t*> m_processed_objs;
145  std::map<coff::section_header_t*, std::size_t> m_scn_hash_tbl;
146  std::map<std::size_t, std::vector<sym_data_t>> m_lookup_tbl;
147  recomp::symbol_table_t* m_syms;
148 };
149 } // namespace theo::decomp
the main decomposition class which is responsible for breaking down lib file into coff files,...
Definition: decomp.hpp:61
decomp_t(std::vector< std::uint8_t > &lib, recomp::symbol_table_t *syms)
the explicit constructor for decomp_t
Definition: decomp.cpp:34
std::vector< routine_t > rtns()
gets all of the routine objects.
Definition: decomp.cpp:281
std::vector< std::vector< std::uint8_t > > objs()
gets all the obj files as a vector of a vector of bytes.
Definition: decomp.cpp:289
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
recomp::symbol_table_t * syms()
gets the symbol table.
Definition: decomp.cpp:293
std::vector< std::uint8_t > lib()
gets a vector of bytes consisting of the lib file.
Definition: decomp.cpp:285
std::optional< recomp::symbol_table_t * > decompose(std::string &entry_sym)
decomposes (extracts) the symbols used. this function determines all used symbols given the entry poi...
Definition: decomp.cpp:37
this class is a high level wrapper for a hashmap that contains decomp::symbol_t values....
the namespace that contains all of the decomposition related code.
Definition: decomp.hpp:49
std::tuple< coff::image_t *, coff::symbol_t *, std::uint32_t > sym_data_t
meta symbol data. consists of the coff image which contains the coff symbol, the coff symbol itself,...
Definition: decomp.hpp:55