From dedfad81a52e0b87e90150a13ec5b7261262b8d6 Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Sat, 9 Apr 2022 20:56:08 -0700 Subject: [PATCH] fixed an issue with number of symbols... code works good now, its finished --- include/decomp/decomp.hpp | 9 ++++---- include/decomp/symbol.hpp | 2 +- src/tests/demo/main.cpp | 2 +- src/tests/demolib/main.cpp | 10 +++++---- src/theo/decomp/decomp.cpp | 43 +++++++++++++++++++++----------------- src/theo/decomp/symbol.cpp | 4 ++-- src/theo/recomp/recomp.cpp | 4 ++-- src/theo/theo.cpp | 2 +- 8 files changed, 42 insertions(+), 34 deletions(-) diff --git a/include/decomp/decomp.hpp b/include/decomp/decomp.hpp index 018472e..027f1f7 100644 --- a/include/decomp/decomp.hpp +++ b/include/decomp/decomp.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -21,11 +22,11 @@ class decomp_t { std::vector rtns(); std::vector lib(); - std::vector objs(); + std::vector> objs(); + recomp::symbol_table_t* syms(); std::map& scn_hash_tbl(); - std::optional decompose( - const std::string&& entry_sym); + std::optional decompose(std::string& entry_sym); private: std::uint32_t ext_used_syms(const std::string&& entry_sym); @@ -35,7 +36,7 @@ class decomp_t { coff::symbol_t* s); const std::vector m_lib; - std::vector m_objs; + std::vector> m_objs; std::vector m_rtns; std::set m_used_syms; std::set m_processed_objs; diff --git a/include/decomp/symbol.hpp b/include/decomp/symbol.hpp index 40659aa..069e65c 100644 --- a/include/decomp/symbol.hpp +++ b/include/decomp/symbol.hpp @@ -33,7 +33,7 @@ class symbol_t { std::size_t hash(); static std::size_t hash(const std::string& sym); - static std::string name(coff::image_t* img, coff::symbol_t* sym); + static std::string name(const coff::image_t* img, coff::symbol_t* sym); private: std::string m_name; diff --git a/src/tests/demo/main.cpp b/src/tests/demo/main.cpp index 66f13a8..43c47ab 100644 --- a/src/tests/demo/main.cpp +++ b/src/tests/demo/main.cpp @@ -56,7 +56,7 @@ int main(int argc, char* argv[]) { return result; }; - theo::theo_t t(fdata, {allocator, copier, resolver}, "main"); + theo::theo_t t(fdata, {allocator, copier, resolver}, "EntryPoint"); auto res = t.decompose(); if (!res.has_value()) { diff --git a/src/tests/demolib/main.cpp b/src/tests/demolib/main.cpp index 6003bb3..cf42f3a 100644 --- a/src/tests/demolib/main.cpp +++ b/src/tests/demolib/main.cpp @@ -6,18 +6,20 @@ extern "C" int MessageBoxA(void* hWnd, void* uType); struct test_t { + char* c; + char* c2; char buff[0x2000]; }; -test_t t = {}; +test_t t = {"Hello", "World", {}}; OBF extern "C" void EntryPoint() { t.buff[0] = 1; - t.buff[1] = 2; + t.buff[1] = 1; if (t.buff[0]) - MessageBoxA(nullptr, "Hello World", "Hello World", nullptr); + MessageBoxA(nullptr, t.c, t.c2, nullptr); if (t.buff[1]) - MessageBoxA(nullptr, "Hello World 1", "Hello World 1", nullptr); + MessageBoxA(nullptr, t.c, t.c2, nullptr); } \ No newline at end of file diff --git a/src/theo/decomp/decomp.cpp b/src/theo/decomp/decomp.cpp index 3bf0361..5bb401c 100644 --- a/src/theo/decomp/decomp.cpp +++ b/src/theo/decomp/decomp.cpp @@ -5,7 +5,7 @@ decomp_t::decomp_t(std::vector& lib, recomp::symbol_table_t* syms) : m_lib(lib), m_syms(syms) {} std::optional decomp_t::decompose( - const std::string&& entry_sym) { + std::string& entry_sym) { // extract obj files from the archive file... // ar::view lib(m_lib.data(), m_lib.size()); @@ -17,25 +17,29 @@ std::optional decomp_t::decompose( // 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 data(itr.second.begin(), itr.second.end()); + m_objs.push_back(data); } }); - std::for_each(m_objs.begin(), m_objs.end(), [&](coff::image_t* img) { - for (auto idx = 0u; idx < img->file_header.num_symbols; ++idx) { - auto sym = img->get_symbol(idx); - auto sym_name = symbol_t::name(img, sym); - if (sym_name.length()) { - auto sym_hash = symbol_t::hash(sym_name.data()); - auto sym_size = - sym->has_section() - ? next_sym(img, img->get_section(sym->section_index - 1), sym) - : 0u; - - m_lookup_tbl[sym_hash].push_back({img, sym, sym_size}); - } - } - }); + std::for_each( + m_objs.begin(), m_objs.end(), [&](std::vector& img_data) { + auto img = reinterpret_cast(img_data.data()); + for (auto idx = 0u; idx < img->file_header.num_symbols - 1; ++idx) { + auto sym = img->get_symbol(idx); + auto sym_name = symbol_t::name(img, sym); + if (sym_name.length()) { + auto sym_hash = symbol_t::hash(sym_name.data()); + auto sym_size = + sym->has_section() + ? next_sym(img, img->get_section(sym->section_index - 1), + sym) + : 0u; + + m_lookup_tbl[sym_hash].emplace_back(img, sym, sym_size); + } + } + }); // extract used symbols from objs and create a nice little set of them so that // we can easily decompose them... no need deal with every single symbol... @@ -47,6 +51,7 @@ std::optional decomp_t::decompose( // std::for_each(m_used_syms.begin(), m_used_syms.end(), [&](sym_data_t data) { auto [img, sym, size] = data; + // populate section hash table with sections for the img of this // symbol... only populate the hash table if its not been populated for // this obj before... @@ -173,7 +178,7 @@ std::uint32_t decomp_t::next_sym(coff::image_t* img, for (auto idx = 0u; idx < img->file_header.num_symbols; ++idx) { auto q = img->get_symbol(idx); if (q->derived_type == coff::derived_type_id::function && - q->section_index == s->section_index) + q->section_index == s->section_index && q != s) if (q->value > s->value && q->value < res) res = q->value; } @@ -244,7 +249,7 @@ std::vector decomp_t::lib() { return m_lib; } -std::vector decomp_t::objs() { +std::vector> decomp_t::objs() { return m_objs; } diff --git a/src/theo/decomp/symbol.cpp b/src/theo/decomp/symbol.cpp index d07f974..34e4860 100644 --- a/src/theo/decomp/symbol.cpp +++ b/src/theo/decomp/symbol.cpp @@ -71,12 +71,12 @@ std::size_t symbol_t::hash(const std::string& sym) { return std::hash{}(sym); } -std::string symbol_t::name(coff::image_t* img, coff::symbol_t* sym) { +std::string symbol_t::name(const coff::image_t* img, coff::symbol_t* sym) { if (sym->has_section() && sym->storage_class == coff::storage_class_id::private_symbol && sym->derived_type == coff::derived_type_id::none) { auto scn = img->get_section(sym->section_index - 1); - auto res = std::string(scn->name.to_string(img->get_strings())) + auto res = std::string(scn->name.to_string(img->get_strings()).data()) .append("#") .append(std::to_string(sym->section_index)) .append("!") diff --git a/src/theo/recomp/recomp.cpp b/src/theo/recomp/recomp.cpp index d3175ca..a1881be 100644 --- a/src/theo/recomp/recomp.cpp +++ b/src/theo/recomp/recomp.cpp @@ -95,8 +95,8 @@ void recomp_t::resolve() { auto scn_sym = m_dcmp->syms()->sym_from_hash(m_dcmp->scn_hash_tbl()[sym.scn()]); - m_copier(scn_sym.value()->allocated_at() + reloc.offset(), - &allocated_at, sizeof(allocated_at)); + *reinterpret_cast(scn_sym.value()->data().data() + + reloc.offset()) = allocated_at; break; } case decomp::sym_type_t::function: { diff --git a/src/theo/theo.cpp b/src/theo/theo.cpp index e5fc55d..5b237b4 100644 --- a/src/theo/theo.cpp +++ b/src/theo/theo.cpp @@ -25,7 +25,7 @@ theo_t::theo_t(std::vector& lib, } std::optional theo_t::decompose() { - auto res = m_dcmp.decompose(m_entry_sym.data()); + auto res = m_dcmp.decompose(m_entry_sym); if (!res.has_value()) { spdlog::error("failed to decompose...\n"); return {};