need to continue debugging and figure out why relocations in data

sections arent happening...
3.0
_xeroxz 3 years ago
parent 367a62d8de
commit e244d3eed1

@ -33,6 +33,7 @@ class symbol_t {
std::size_t hash(); std::size_t hash();
static std::size_t hash(const std::string& sym); static std::size_t hash(const std::string& sym);
static std::string name(coff::image_t* img, coff::symbol_t* sym);
private: private:
std::string m_name; std::string m_name;

@ -66,9 +66,7 @@ int main(int argc, char* argv[]) {
spdlog::info("decomposed {} symbols...", res.value()); spdlog::info("decomposed {} symbols...", res.value());
auto entry_pnt = t.compose(); auto entry_pnt = t.compose();
auto winproc = t.resolve("?GetIO@ImGui@@YAAEAUImGuiIO@@XZ");
spdlog::info("entry point address: {:X}", entry_pnt); spdlog::info("entry point address: {:X}", entry_pnt);
spdlog::info("?GetIO@ImGui@@YAAEAUImGuiIO@@XZ: {:X}", winproc);
std::getchar(); std::getchar();
reinterpret_cast<void (*)()>(entry_pnt)(); reinterpret_cast<void (*)()>(entry_pnt)();
} }

@ -24,7 +24,7 @@ std::optional<recomp::symbol_table_t*> decomp_t::decompose(
std::for_each(m_objs.begin(), m_objs.end(), [&](coff::image_t* img) { std::for_each(m_objs.begin(), m_objs.end(), [&](coff::image_t* img) {
for (auto idx = 0u; idx < img->file_header.num_symbols; ++idx) { for (auto idx = 0u; idx < img->file_header.num_symbols; ++idx) {
auto sym = img->get_symbol(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()) { if (sym_name.length()) {
auto sym_hash = symbol_t::hash(sym_name.data()); auto sym_hash = symbol_t::hash(sym_name.data());
auto sym_size = auto sym_size =
@ -104,7 +104,7 @@ std::optional<recomp::symbol_table_t*> decomp_t::decompose(
.append("!") .append("!")
.append(std::to_string(img->file_header.timedate_stamp)); .append(std::to_string(img->file_header.timedate_stamp));
std::vector<std::uint8_t> scn_data; std::vector<std::uint8_t> scn_data(scn->size_raw_data);
if (scn->characteristics.cnt_uninit_data) { if (scn->characteristics.cnt_uninit_data) {
scn_data.insert(scn_data.begin(), scn->size_raw_data, 0); scn_data.insert(scn_data.begin(), scn->size_raw_data, 0);
} else { } else {
@ -115,17 +115,31 @@ std::optional<recomp::symbol_table_t*> decomp_t::decompose(
scn->size_raw_data); scn->size_raw_data);
} }
std::vector<recomp::reloc_t> relocs;
auto scn_relocs = reinterpret_cast<coff::reloc_t*>(
scn->ptr_relocs + reinterpret_cast<std::uint8_t*>(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, {}, 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); m_syms->add_symbol(new_scn_sym);
} }
// create a symbol for the data... // create a symbol for the data...
// //
decomp::symbol_t new_sym( decomp::symbol_t new_sym(img, symbol_t::name(img, sym).data(),
img, sym->name.to_string(img->get_strings()).data(), sym->value, {}, sym->value, {}, scn, sym, {},
scn, sym, {}, sym_type_t::data); sym_type_t::data);
m_syms->add_symbol(new_sym); m_syms->add_symbol(new_sym);
} }
@ -137,9 +151,8 @@ std::optional<recomp::symbol_table_t*> decomp_t::decompose(
// space for them... // space for them...
std::vector<std::uint8_t> data(sym->value, 0); std::vector<std::uint8_t> data(sym->value, 0);
decomp::symbol_t bss_sym(img, decomp::symbol_t bss_sym(img, symbol_t::name(img, sym).data(), {}, data,
sym->name.to_string(img->get_strings()).data(), {}, sym, {}, sym_type_t::data);
{}, data, {}, sym, {}, sym_type_t::data);
m_syms->add_symbol(bss_sym); 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 && if (reloc->virtual_address >= sym->value &&
reloc->virtual_address < sym->value + size) { reloc->virtual_address < sym->value + size) {
auto reloc_sym = img->get_symbol(reloc->symbol_index); 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); entry = get_symbol(sym_name);
if (m_used_syms.emplace(entry.value()).second) if (m_used_syms.emplace(entry.value()).second)
return true; return true;

@ -23,7 +23,7 @@ std::vector<decomp::symbol_t> routine_t::decompose() {
if (scn_reloc->virtual_address >= m_sym->value && if (scn_reloc->virtual_address >= m_sym->value &&
scn_reloc->virtual_address < m_sym->value + m_data.size()) { scn_reloc->virtual_address < m_sym->value + m_data.size()) {
auto sym_reloc = m_img->get_symbol(scn_relocs[idx].symbol_index); 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()); auto sym_hash = decomp::symbol_t::hash(sym_name.data());
relocs.push_back( relocs.push_back(
recomp::reloc_t(scn_reloc->virtual_address - m_sym->value, recomp::reloc_t(scn_reloc->virtual_address - m_sym->value,
@ -32,8 +32,8 @@ std::vector<decomp::symbol_t> routine_t::decompose() {
} }
result.push_back(decomp::symbol_t( result.push_back(decomp::symbol_t(
m_img, m_sym->name.to_string(m_img->get_strings()).data(), m_img, symbol_t::name(m_img, m_sym).data(), m_sym->value, m_data,
m_sym->value, m_data, m_scn, m_sym, relocs, sym_type_t::function)); m_scn, m_sym, relocs, sym_type_t::function));
break; break;
} }
case instruction: { case instruction: {
@ -50,8 +50,7 @@ std::vector<decomp::symbol_t> routine_t::decompose() {
m_data.size() - offset)) == XED_ERROR_NONE) { m_data.size() - offset)) == XED_ERROR_NONE) {
// symbol name is of the format: symbol@instroffset, I.E: main@11... // symbol name is of the format: symbol@instroffset, I.E: main@11...
// //
auto new_sym_name = auto new_sym_name = symbol_t::name(m_img, m_sym);
std::string(m_sym->name.to_string(m_img->get_strings()));
// first instruction doesnt need the @offset... // first instruction doesnt need the @offset...
// //
@ -78,7 +77,7 @@ std::vector<decomp::symbol_t> routine_t::decompose() {
// //
if (reloc != scn_relocs + m_scn->num_relocs) { if (reloc != scn_relocs + m_scn->num_relocs) {
auto sym_reloc = m_img->get_symbol(reloc->symbol_index); 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 sym_hash = decomp::symbol_t::hash(sym_name.data());
auto reloc_offset = reloc->virtual_address - m_sym->value - offset; auto reloc_offset = reloc->virtual_address - m_sym->value - offset;
@ -91,7 +90,7 @@ std::vector<decomp::symbol_t> routine_t::decompose() {
// relocs with offset ZERO means the next instructions... // relocs with offset ZERO means the next instructions...
// //
auto next_inst_sym = auto next_inst_sym =
std::string(m_sym->name.to_string(m_img->get_strings())) symbol_t::name(m_img, m_sym)
.append("@") .append("@")
.append(std::to_string(offset + .append(std::to_string(offset +
xed_decoded_inst_get_length(&instr))); xed_decoded_inst_get_length(&instr)));

@ -70,4 +70,22 @@ std::vector<recomp::reloc_t>& symbol_t::relocs() {
std::size_t symbol_t::hash(const std::string& sym) { std::size_t symbol_t::hash(const std::string& sym) {
return std::hash<std::string>{}(sym); return std::hash<std::string>{}(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 } // namespace theo::decomp

@ -91,6 +91,14 @@ void recomp_t::resolve() {
} }
switch (sym.type()) { 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: { case decomp::sym_type_t::function: {
*reinterpret_cast<std::uintptr_t*>(sym.data().data() + *reinterpret_cast<std::uintptr_t*>(sym.data().data() +
reloc.offset()) = allocated_at; reloc.offset()) = allocated_at;

Loading…
Cancel
Save