fixed an issue with number of symbols... code works good now, its

finished
3.0
_xeroxz 2 years ago
parent e244d3eed1
commit dedfad81a5

@ -4,6 +4,7 @@
#include <linuxpe>
#include <optional>
#include <set>
#include <tuple>
#include <vector>
#include <decomp/routine.hpp>
@ -21,11 +22,11 @@ class decomp_t {
std::vector<routine_t> rtns();
std::vector<std::uint8_t> lib();
std::vector<coff::image_t*> objs();
std::vector<std::vector<std::uint8_t>> objs();
recomp::symbol_table_t* syms();
std::map<coff::section_header_t*, std::size_t>& scn_hash_tbl();
std::optional<recomp::symbol_table_t*> decompose(
const std::string&& entry_sym);
std::optional<recomp::symbol_table_t*> 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<std::uint8_t> m_lib;
std::vector<coff::image_t*> m_objs;
std::vector<std::vector<std::uint8_t>> m_objs;
std::vector<routine_t> m_rtns;
std::set<sym_data_t> m_used_syms;
std::set<coff::image_t*> m_processed_objs;

@ -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;

@ -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()) {

@ -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);
}

@ -5,7 +5,7 @@ decomp_t::decomp_t(std::vector<std::uint8_t>& lib, recomp::symbol_table_t* syms)
: m_lib(lib), m_syms(syms) {}
std::optional<recomp::symbol_table_t*> decomp_t::decompose(
const std::string&& entry_sym) {
std::string& entry_sym) {
// extract obj files from the archive file...
//
ar::view<false> lib(m_lib.data(), m_lib.size());
@ -17,25 +17,29 @@ std::optional<recomp::symbol_table_t*> 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<coff::image_t*>(itr.second.data()));
std::vector<std::uint8_t> 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<std::uint8_t>& img_data) {
auto img = reinterpret_cast<coff::image_t*>(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<recomp::symbol_table_t*> 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<std::uint8_t> decomp_t::lib() {
return m_lib;
}
std::vector<coff::image_t*> decomp_t::objs() {
std::vector<std::vector<std::uint8_t>> decomp_t::objs() {
return m_objs;
}

@ -71,12 +71,12 @@ std::size_t symbol_t::hash(const std::string& sym) {
return std::hash<std::string>{}(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("!")

@ -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<std::uintptr_t*>(scn_sym.value()->data().data() +
reloc.offset()) = allocated_at;
break;
}
case decomp::sym_type_t::function: {

@ -25,7 +25,7 @@ theo_t::theo_t(std::vector<std::uint8_t>& lib,
}
std::optional<std::uint32_t> 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 {};

Loading…
Cancel
Save