From e244d3eed10b52ecbe6df977125dcade169391ef Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Sat, 9 Apr 2022 17:16:01 -0700 Subject: [PATCH] need to continue debugging and figure out why relocations in data sections arent happening... --- include/decomp/symbol.hpp | 1 + src/tests/demo/main.cpp | 2 -- src/theo/decomp/decomp.cpp | 33 +++++++++++++++++++++++---------- src/theo/decomp/routine.cpp | 13 ++++++------- src/theo/decomp/symbol.cpp | 18 ++++++++++++++++++ src/theo/recomp/recomp.cpp | 8 ++++++++ 6 files changed, 56 insertions(+), 19 deletions(-) diff --git a/include/decomp/symbol.hpp b/include/decomp/symbol.hpp index e43401c..40659aa 100644 --- a/include/decomp/symbol.hpp +++ b/include/decomp/symbol.hpp @@ -33,6 +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); private: std::string m_name; diff --git a/src/tests/demo/main.cpp b/src/tests/demo/main.cpp index 579a091..66f13a8 100644 --- a/src/tests/demo/main.cpp +++ b/src/tests/demo/main.cpp @@ -66,9 +66,7 @@ int main(int argc, char* argv[]) { spdlog::info("decomposed {} symbols...", res.value()); auto entry_pnt = t.compose(); - auto winproc = t.resolve("?GetIO@ImGui@@YAAEAUImGuiIO@@XZ"); spdlog::info("entry point address: {:X}", entry_pnt); - spdlog::info("?GetIO@ImGui@@YAAEAUImGuiIO@@XZ: {:X}", winproc); std::getchar(); reinterpret_cast(entry_pnt)(); } \ No newline at end of file diff --git a/src/theo/decomp/decomp.cpp b/src/theo/decomp/decomp.cpp index a2cc29d..3bf0361 100644 --- a/src/theo/decomp/decomp.cpp +++ b/src/theo/decomp/decomp.cpp @@ -24,7 +24,7 @@ std::optional decomp_t::decompose( 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 = sym->name.to_string(img->get_strings()); + auto sym_name = symbol_t::name(img, sym); if (sym_name.length()) { auto sym_hash = symbol_t::hash(sym_name.data()); auto sym_size = @@ -104,7 +104,7 @@ std::optional decomp_t::decompose( .append("!") .append(std::to_string(img->file_header.timedate_stamp)); - std::vector scn_data; + std::vector scn_data(scn->size_raw_data); if (scn->characteristics.cnt_uninit_data) { scn_data.insert(scn_data.begin(), scn->size_raw_data, 0); } else { @@ -115,17 +115,31 @@ std::optional decomp_t::decompose( scn->size_raw_data); } + std::vector relocs; + auto scn_relocs = reinterpret_cast( + scn->ptr_relocs + reinterpret_cast(img)); + + for (auto idx = 0u; idx < scn->num_relocs; ++idx) { + auto scn_reloc = &scn_relocs[idx]; + auto sym_reloc = img->get_symbol(scn_relocs[idx].symbol_index); + auto sym_name = symbol_t::name(img, sym_reloc); + auto sym_hash = decomp::symbol_t::hash(sym_name.data()); + relocs.push_back( + recomp::reloc_t(scn_reloc->virtual_address - sym->value, + sym_hash, sym_name.data())); + } + decomp::symbol_t new_scn_sym(img, scn_sym_name, 0, scn_data, scn, {}, - {}, sym_type_t::section); + relocs, sym_type_t::section); m_syms->add_symbol(new_scn_sym); } // create a symbol for the data... // - decomp::symbol_t new_sym( - img, sym->name.to_string(img->get_strings()).data(), sym->value, {}, - scn, sym, {}, sym_type_t::data); + decomp::symbol_t new_sym(img, symbol_t::name(img, sym).data(), + sym->value, {}, scn, sym, {}, + sym_type_t::data); m_syms->add_symbol(new_sym); } @@ -137,9 +151,8 @@ std::optional decomp_t::decompose( // space for them... std::vector data(sym->value, 0); - decomp::symbol_t bss_sym(img, - sym->name.to_string(img->get_strings()).data(), - {}, data, {}, sym, {}, sym_type_t::data); + decomp::symbol_t bss_sym(img, symbol_t::name(img, sym).data(), {}, data, + {}, sym, {}, sym_type_t::data); m_syms->add_symbol(bss_sym); } @@ -188,7 +201,7 @@ std::uint32_t decomp_t::ext_used_syms(const std::string&& entry_sym) { if (reloc->virtual_address >= sym->value && reloc->virtual_address < sym->value + size) { auto reloc_sym = img->get_symbol(reloc->symbol_index); - auto sym_name = reloc_sym->name.to_string(img->get_strings()); + auto sym_name = symbol_t::name(img, reloc_sym); entry = get_symbol(sym_name); if (m_used_syms.emplace(entry.value()).second) return true; diff --git a/src/theo/decomp/routine.cpp b/src/theo/decomp/routine.cpp index 7bedb4e..665a9e6 100644 --- a/src/theo/decomp/routine.cpp +++ b/src/theo/decomp/routine.cpp @@ -23,7 +23,7 @@ std::vector routine_t::decompose() { if (scn_reloc->virtual_address >= m_sym->value && scn_reloc->virtual_address < m_sym->value + m_data.size()) { auto sym_reloc = m_img->get_symbol(scn_relocs[idx].symbol_index); - auto sym_name = sym_reloc->name.to_string(m_img->get_strings()); + auto sym_name = symbol_t::name(m_img, sym_reloc); auto sym_hash = decomp::symbol_t::hash(sym_name.data()); relocs.push_back( recomp::reloc_t(scn_reloc->virtual_address - m_sym->value, @@ -32,8 +32,8 @@ std::vector routine_t::decompose() { } result.push_back(decomp::symbol_t( - m_img, m_sym->name.to_string(m_img->get_strings()).data(), - m_sym->value, m_data, m_scn, m_sym, relocs, sym_type_t::function)); + m_img, symbol_t::name(m_img, m_sym).data(), m_sym->value, m_data, + m_scn, m_sym, relocs, sym_type_t::function)); break; } case instruction: { @@ -50,8 +50,7 @@ std::vector routine_t::decompose() { m_data.size() - offset)) == XED_ERROR_NONE) { // symbol name is of the format: symbol@instroffset, I.E: main@11... // - auto new_sym_name = - std::string(m_sym->name.to_string(m_img->get_strings())); + auto new_sym_name = symbol_t::name(m_img, m_sym); // first instruction doesnt need the @offset... // @@ -78,7 +77,7 @@ std::vector routine_t::decompose() { // if (reloc != scn_relocs + m_scn->num_relocs) { auto sym_reloc = m_img->get_symbol(reloc->symbol_index); - auto sym_name = sym_reloc->name.to_string(m_img->get_strings()); + auto sym_name = symbol_t::name(m_img, sym_reloc); auto sym_hash = decomp::symbol_t::hash(sym_name.data()); auto reloc_offset = reloc->virtual_address - m_sym->value - offset; @@ -91,7 +90,7 @@ std::vector routine_t::decompose() { // relocs with offset ZERO means the next instructions... // auto next_inst_sym = - std::string(m_sym->name.to_string(m_img->get_strings())) + symbol_t::name(m_img, m_sym) .append("@") .append(std::to_string(offset + xed_decoded_inst_get_length(&instr))); diff --git a/src/theo/decomp/symbol.cpp b/src/theo/decomp/symbol.cpp index 34aaf81..d07f974 100644 --- a/src/theo/decomp/symbol.cpp +++ b/src/theo/decomp/symbol.cpp @@ -70,4 +70,22 @@ std::vector& symbol_t::relocs() { 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) { + 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())) + .append("#") + .append(std::to_string(sym->section_index)) + .append("!") + .append(std::to_string(img->file_header.timedate_stamp)) + .append("+") + .append(std::to_string(sym->value)); + + return res; + } + return std::string(sym->name.to_string(img->get_strings())); +} } // namespace theo::decomp \ No newline at end of file diff --git a/src/theo/recomp/recomp.cpp b/src/theo/recomp/recomp.cpp index 58a5648..d3175ca 100644 --- a/src/theo/recomp/recomp.cpp +++ b/src/theo/recomp/recomp.cpp @@ -91,6 +91,14 @@ void recomp_t::resolve() { } switch (sym.type()) { + case decomp::sym_type_t::section: { + 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)); + break; + } case decomp::sym_type_t::function: { *reinterpret_cast(sym.data().data() + reloc.offset()) = allocated_at;