starting to work on decomposition functions

3.0
_xeroxz 3 years ago
parent dd78f5309a
commit 3cc9c0e05e

@ -55,11 +55,17 @@ set(Theodosius_SOURCES "")
list(APPEND Theodosius_SOURCES
"include/comp/comp.hpp"
"include/comp/reloc.hpp"
"include/comp/symbol_table.hpp"
"include/decomp/decomp.hpp"
"include/decomp/routine.hpp"
"include/decomp/symbol.hpp"
"include/theo.hpp"
"src/theo/comp/comp.cpp"
"src/theo/comp/symbol_table.cpp"
"src/theo/decomp/decomp.cpp"
"src/theo/decomp/routine.cpp"
"src/theo/decomp/symbol.cpp"
"src/theo/theo.cpp"
)

@ -17,6 +17,7 @@ class comp_t {
copier_t copy,
resolver_t resolve);
std::optional<std::uint32_t> compose();
void allocator(allocator_t alloc);
void copier(copier_t copy);
void resolver(resolver_t resolve);

@ -0,0 +1,18 @@
#pragma once
#include <cstddef>
#include <cstdint>
namespace theo::comp {
class reloc_t {
public:
explicit reloc_t(std::uint16_t offset, std::size_t hash)
: m_offset(offset), m_hash(hash) {}
std::size_t hash() { return m_hash; }
std::uint16_t offset() { return m_offset; }
private:
std::size_t m_hash;
std::uint16_t m_offset;
};
} // namespace theo::comp

@ -1,4 +1,5 @@
#pragma once
#include <algorithm>
#include <functional>
#include <map>
#include <vector>
@ -8,19 +9,22 @@
namespace theo::comp {
class symbol_table_t {
public:
symbol_table_t();
symbol_table_t() {}
symbol_table_t(const std::vector<decomp::symbol_t>&& syms);
void add_symbol(decomp::symbol_t& sym);
void add_symbols(std::vector<decomp::symbol_t>& syms);
void update(std::string& name, decomp::symbol_t& sym);
void update(std::string& name, std::uintptr_t location);
decomp::symbol_t sym_from_hash(std::size_t hash);
decomp::symbol_t sym_from_alloc(std::uintptr_t allocated_at);
void update(std::size_t hash, decomp::symbol_t& sym);
void update(std::size_t hash, std::uintptr_t allocated_at);
void for_each(std::function<bool(decomp::symbol_t& sym)> fn);
std::uint32_t size();
private:
std::map<std::string, decomp::symbol_t> m_table;
std::map<std::size_t, decomp::symbol_t> m_table;
};
} // namespace theo::comp

@ -1,28 +1,30 @@
#pragma once
#include <coff/image.hpp>
#include <cstdint>
#include <linuxpe>
#include <vector>
#include <optional>
#include <vector>
#include <spdlog/spdlog.h>
#include <comp/symbol_table.hpp>
#include <decomp/routine.hpp>
#include <coff/archive.hpp>
#include <coff/image.hpp>
namespace theo::decomp {
class decomp_t {
public:
explicit decomp_t(std::vector<std::uint8_t>& lib_data,
comp::symbol_table_t* syms);
explicit decomp_t(std::vector<std::uint8_t>& lib, comp::symbol_table_t* syms);
std::vector<routine_t> rtns();
std::vector<std::uint8_t> lib();
std::vector<coff::image_t> objs();
std::vector<coff::image_t*> objs();
comp::symbol_table_t* syms();
std::optional<comp::symbol_table_t*> decompose();
private:
std::vector<std::uint8_t> m_lib_data;
std::vector<coff::image_t> m_obj_imgs;
const std::vector<std::uint8_t> m_lib;
std::vector<coff::image_t*> m_objs;
std::vector<routine_t> m_rtns;
comp::symbol_table_t* m_syms;
};

@ -1,29 +1,35 @@
#pragma once
#include <coff/image.hpp>
#include <comp/reloc.hpp>
#include <cstdint>
#include <string>
#include <vector>
namespace theo::decomp {
class symbol_t {
public:
explicit symbol_t(const std::string&& name,
std::uintptr_t location,
const std::vector<std::uint8_t>&& data,
coff::section_header_t scn_hdr);
explicit symbol_t(std::string name,
std::uintptr_t offset,
std::vector<std::uint8_t> data,
coff::section_header_t scn_hdr,
std::vector<comp::reloc_t> relocs);
std::string name() const;
std::uintptr_t location() const;
std::uintptr_t offset() const;
std::uintptr_t allocated_at() const;
std::uint32_t size() const;
std::vector<std::uint8_t> data() const;
void name(const std::string&& name);
void location(std::uintptr_t location);
void data(const std::vector<std::uint8_t>&& data);
void allocated_at(std::uintptr_t allocated_at);
std::size_t hash();
static std::size_t hash(const std::string& sym);
private:
std::string m_name;
std::uintptr_t m_location;
std::uintptr_t m_offset, m_allocated_at;
std::vector<std::uint8_t> m_data;
const coff::section_header_t m_scn_hdr;
coff::section_header_t m_scn_hdr;
std::vector<comp::reloc_t> m_relocs;
};
} // namespace theo::decomp

@ -8,9 +8,6 @@
namespace fs = std::filesystem;
int main(int argc, char* argv[]) {
spdlog::info("this is info...");
spdlog::error("this is an error...");
if (argc < 2)
return -1;

@ -0,0 +1,28 @@
#include <comp/comp.hpp>
namespace theo::comp {
comp_t::comp_t(decomp::decomp_t* dcmp) : m_dcmp(dcmp) {}
comp_t::comp_t(decomp::decomp_t* dcmp,
allocator_t alloc,
copier_t copy,
resolver_t resolve)
: m_dcmp(dcmp), m_allocator(alloc), m_copier(copy), m_resolver(resolve) {}
void comp_t::allocator(allocator_t alloc) {
m_allocator = alloc;
}
void comp_t::copier(copier_t copy) {
m_copier = copy;
}
void comp_t::resolver(resolver_t resolve) {
m_resolver = resolve;
}
std::uintptr_t comp_t::resolve(std::string&& sym) {
return m_dcmp->syms()
->sym_from_hash(decomp::symbol_t::hash(sym))
.allocated_at();
}
} // namespace theo::comp

@ -0,0 +1,48 @@
#include <comp/symbol_table.hpp>
namespace theo::comp {
symbol_table_t::symbol_table_t(const std::vector<decomp::symbol_t>&& syms) {
std::for_each(syms.begin(), syms.end(), [&](decomp::symbol_t sym) {
m_table.insert({sym.hash(), sym});
});
}
void symbol_table_t::add_symbol(decomp::symbol_t& sym) {
m_table.insert({sym.hash(), sym});
}
void symbol_table_t::add_symbols(std::vector<decomp::symbol_t>& syms) {
std::for_each(syms.begin(), syms.end(),
[&](decomp::symbol_t sym) { add_symbol(sym); });
}
void symbol_table_t::update(std::size_t hash, decomp::symbol_t& sym) {
m_table.insert({hash, sym});
}
void symbol_table_t::update(std::size_t hash, std::uintptr_t allocated_at) {
auto v = m_table.at(hash);
v.allocated_at(allocated_at);
m_table.insert({hash, v});
}
void symbol_table_t::for_each(std::function<bool(decomp::symbol_t& sym)> fn) {
std::for_each(m_table.begin(), m_table.end(), [&](auto v) { fn(v.second); });
}
decomp::symbol_t symbol_table_t::sym_from_hash(std::size_t hash) {
return m_table.at(hash);
}
decomp::symbol_t symbol_table_t::sym_from_alloc(std::uintptr_t allocated_at) {
return std::find_if(m_table.begin(), m_table.end(),
[&](std::pair<const std::size_t, decomp::symbol_t> itr) {
return itr.second.allocated_at() == allocated_at;
})
->second;
}
std::uint32_t symbol_table_t::size() {
return m_table.size();
}
} // namespace theo::comp

@ -0,0 +1,37 @@
#include <decomp/decomp.hpp>
namespace theo::decomp {
decomp_t::decomp_t(std::vector<std::uint8_t>& lib, comp::symbol_table_t* syms)
: m_lib(lib), m_syms(syms) {}
std::optional<comp::symbol_table_t*> decomp_t::decompose() {
ar::view<false> lib(m_lib.data(), m_lib.size());
std::for_each(
lib.begin(), lib.end(),
[&](std::pair<std::string_view, ar::entry_t&> itr) {
// if the entry isnt the symbol table or the string table
// then we know its an obj file...
//
if (!itr.second.is_symbol_table() && !itr.second.is_string_table()) {
spdlog::info("extracted obj from archive: {}", itr.first);
m_objs.push_back(reinterpret_cast<coff::image_t*>(itr.second.data()));
}
});
}
std::vector<routine_t> decomp_t::rtns() {
return m_rtns;
}
std::vector<std::uint8_t> decomp_t::lib() {
return m_lib;
}
std::vector<coff::image_t*> decomp_t::objs() {
return m_objs;
}
comp::symbol_table_t* decomp_t::syms() {
return m_syms;
}
} // namespace theo::decomp

@ -0,0 +1,48 @@
#include <decomp/symbol.hpp>
namespace theo::decomp {
symbol_t::symbol_t(std::string name,
std::uintptr_t offset,
std::vector<std::uint8_t> data,
coff::section_header_t scn_hdr,
std::vector<comp::reloc_t> relocs)
: m_name(name),
m_offset(offset),
m_data(data),
m_scn_hdr(scn_hdr),
m_relocs(relocs),
m_allocated_at(0) {}
std::string symbol_t::name() const {
return m_name;
}
std::uintptr_t symbol_t::offset() const {
return m_offset;
}
std::uintptr_t symbol_t::allocated_at() const {
return m_allocated_at;
}
std::uint32_t symbol_t::size() const {
return m_data.size();
}
std::vector<std::uint8_t> symbol_t::data() const {
return m_data;
}
void symbol_t::allocated_at(std::uintptr_t allocated_at) {
m_allocated_at = allocated_at;
}
std::size_t symbol_t::hash() {
return hash(m_name);
}
std::size_t symbol_t::hash(const std::string& sym)
{
return std::hash<std::string>{}(sym);
}
} // namespace theo::decomp
Loading…
Cancel
Save