forked from IDontCode/Theodosius
parent
dd78f5309a
commit
3cc9c0e05e
@ -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,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
|
@ -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…
Reference in new issue