From 3cc9c0e05e5de1ce99c6a411891e5a67ff72c48d Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Sun, 3 Apr 2022 18:34:59 -0700 Subject: [PATCH] starting to work on decomposition functions --- CMakeLists.txt | 6 +++ include/comp/comp.hpp | 1 + include/comp/reloc.hpp | 18 +++++++ include/comp/symbol_table.hpp | 12 +++-- include/decomp/decomp.hpp | 16 ++++--- include/decomp/symbol.hpp | 26 ++++++---- src/tests/demo/main.cpp | 3 -- src/theo/comp/comp.cpp | 28 +++++++++++ src/theo/comp/symbol_table.cpp | 48 +++++++++++++++++++ src/theo/comp/symbol_table.hpp | 0 src/theo/decomp/decomp.cpp | 37 ++++++++++++++ src/theo/decomp/decomp.hpp | 0 .../{comp/comp.hpp => decomp/routine.cpp} | 0 src/theo/decomp/routine.hpp | 0 src/theo/decomp/symbol.cpp | 48 +++++++++++++++++++ src/theo/decomp/symbol.hpp | 0 16 files changed, 219 insertions(+), 24 deletions(-) create mode 100644 include/comp/reloc.hpp create mode 100644 src/theo/comp/comp.cpp create mode 100644 src/theo/comp/symbol_table.cpp delete mode 100644 src/theo/comp/symbol_table.hpp create mode 100644 src/theo/decomp/decomp.cpp delete mode 100644 src/theo/decomp/decomp.hpp rename src/theo/{comp/comp.hpp => decomp/routine.cpp} (100%) delete mode 100644 src/theo/decomp/routine.hpp create mode 100644 src/theo/decomp/symbol.cpp delete mode 100644 src/theo/decomp/symbol.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a3c36f..6c470c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" ) diff --git a/include/comp/comp.hpp b/include/comp/comp.hpp index b4de1af..0319dfc 100644 --- a/include/comp/comp.hpp +++ b/include/comp/comp.hpp @@ -17,6 +17,7 @@ class comp_t { copier_t copy, resolver_t resolve); + std::optional compose(); void allocator(allocator_t alloc); void copier(copier_t copy); void resolver(resolver_t resolve); diff --git a/include/comp/reloc.hpp b/include/comp/reloc.hpp new file mode 100644 index 0000000..fb90cec --- /dev/null +++ b/include/comp/reloc.hpp @@ -0,0 +1,18 @@ +#pragma once +#include +#include + +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 \ No newline at end of file diff --git a/include/comp/symbol_table.hpp b/include/comp/symbol_table.hpp index d67d210..4ae1a46 100644 --- a/include/comp/symbol_table.hpp +++ b/include/comp/symbol_table.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -8,19 +9,22 @@ namespace theo::comp { class symbol_table_t { public: - symbol_table_t(); + symbol_table_t() {} symbol_table_t(const std::vector&& syms); void add_symbol(decomp::symbol_t& sym); void add_symbols(std::vector& 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 fn); std::uint32_t size(); private: - std::map m_table; + std::map m_table; }; } // namespace theo::comp \ No newline at end of file diff --git a/include/decomp/decomp.hpp b/include/decomp/decomp.hpp index c1da28a..72c83ee 100644 --- a/include/decomp/decomp.hpp +++ b/include/decomp/decomp.hpp @@ -1,28 +1,30 @@ #pragma once -#include #include #include -#include #include +#include +#include #include #include +#include +#include + namespace theo::decomp { class decomp_t { public: - explicit decomp_t(std::vector& lib_data, - comp::symbol_table_t* syms); + explicit decomp_t(std::vector& lib, comp::symbol_table_t* syms); std::vector rtns(); std::vector lib(); - std::vector objs(); + std::vector objs(); comp::symbol_table_t* syms(); std::optional decompose(); private: - std::vector m_lib_data; - std::vector m_obj_imgs; + const std::vector m_lib; + std::vector m_objs; std::vector m_rtns; comp::symbol_table_t* m_syms; }; diff --git a/include/decomp/symbol.hpp b/include/decomp/symbol.hpp index 02faf51..47a1a76 100644 --- a/include/decomp/symbol.hpp +++ b/include/decomp/symbol.hpp @@ -1,29 +1,35 @@ #pragma once #include +#include #include #include +#include namespace theo::decomp { class symbol_t { public: - explicit symbol_t(const std::string&& name, - std::uintptr_t location, - const std::vector&& data, - coff::section_header_t scn_hdr); + explicit symbol_t(std::string name, + std::uintptr_t offset, + std::vector data, + coff::section_header_t scn_hdr, + std::vector 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 data() const; - void name(const std::string&& name); - void location(std::uintptr_t location); - void data(const std::vector&& 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 m_data; - const coff::section_header_t m_scn_hdr; + coff::section_header_t m_scn_hdr; + std::vector m_relocs; }; } // namespace theo::decomp \ No newline at end of file diff --git a/src/tests/demo/main.cpp b/src/tests/demo/main.cpp index 590cbcf..a561406 100644 --- a/src/tests/demo/main.cpp +++ b/src/tests/demo/main.cpp @@ -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; diff --git a/src/theo/comp/comp.cpp b/src/theo/comp/comp.cpp new file mode 100644 index 0000000..4ca4537 --- /dev/null +++ b/src/theo/comp/comp.cpp @@ -0,0 +1,28 @@ +#include + +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 \ No newline at end of file diff --git a/src/theo/comp/symbol_table.cpp b/src/theo/comp/symbol_table.cpp new file mode 100644 index 0000000..f9d02f3 --- /dev/null +++ b/src/theo/comp/symbol_table.cpp @@ -0,0 +1,48 @@ +#include + +namespace theo::comp { +symbol_table_t::symbol_table_t(const std::vector&& 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& 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 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 itr) { + return itr.second.allocated_at() == allocated_at; + }) + ->second; +} + +std::uint32_t symbol_table_t::size() { + return m_table.size(); +} +} // namespace theo::comp \ No newline at end of file diff --git a/src/theo/comp/symbol_table.hpp b/src/theo/comp/symbol_table.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/theo/decomp/decomp.cpp b/src/theo/decomp/decomp.cpp new file mode 100644 index 0000000..b55196c --- /dev/null +++ b/src/theo/decomp/decomp.cpp @@ -0,0 +1,37 @@ +#include + +namespace theo::decomp { +decomp_t::decomp_t(std::vector& lib, comp::symbol_table_t* syms) + : m_lib(lib), m_syms(syms) {} + +std::optional decomp_t::decompose() { + ar::view lib(m_lib.data(), m_lib.size()); + std::for_each( + lib.begin(), lib.end(), + [&](std::pair 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(itr.second.data())); + } + }); +} + +std::vector decomp_t::rtns() { + return m_rtns; +} + +std::vector decomp_t::lib() { + return m_lib; +} + +std::vector decomp_t::objs() { + return m_objs; +} + +comp::symbol_table_t* decomp_t::syms() { + return m_syms; +} +} // namespace theo::decomp \ No newline at end of file diff --git a/src/theo/decomp/decomp.hpp b/src/theo/decomp/decomp.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/theo/comp/comp.hpp b/src/theo/decomp/routine.cpp similarity index 100% rename from src/theo/comp/comp.hpp rename to src/theo/decomp/routine.cpp diff --git a/src/theo/decomp/routine.hpp b/src/theo/decomp/routine.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/theo/decomp/symbol.cpp b/src/theo/decomp/symbol.cpp new file mode 100644 index 0000000..f00cf71 --- /dev/null +++ b/src/theo/decomp/symbol.cpp @@ -0,0 +1,48 @@ +#include + +namespace theo::decomp { +symbol_t::symbol_t(std::string name, + std::uintptr_t offset, + std::vector data, + coff::section_header_t scn_hdr, + std::vector 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 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{}(sym); +} +} // namespace theo::decomp \ No newline at end of file diff --git a/src/theo/decomp/symbol.hpp b/src/theo/decomp/symbol.hpp deleted file mode 100644 index e69de29..0000000