|
|
@ -5,6 +5,8 @@ decomp_t::decomp_t(std::vector<std::uint8_t>& lib, comp::symbol_table_t* syms)
|
|
|
|
: m_lib(lib), m_syms(syms) {}
|
|
|
|
: m_lib(lib), m_syms(syms) {}
|
|
|
|
|
|
|
|
|
|
|
|
std::optional<comp::symbol_table_t*> decomp_t::decompose() {
|
|
|
|
std::optional<comp::symbol_table_t*> decomp_t::decompose() {
|
|
|
|
|
|
|
|
// extract obj files from the archive file...
|
|
|
|
|
|
|
|
//
|
|
|
|
ar::view<false> lib(m_lib.data(), m_lib.size());
|
|
|
|
ar::view<false> lib(m_lib.data(), m_lib.size());
|
|
|
|
std::for_each(
|
|
|
|
std::for_each(
|
|
|
|
lib.begin(), lib.end(),
|
|
|
|
lib.begin(), lib.end(),
|
|
|
@ -18,7 +20,27 @@ std::optional<comp::symbol_table_t*> decomp_t::decompose() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// generate symbols, populate section hash table, for each object file
|
|
|
|
|
|
|
|
// extracted from the archive file...
|
|
|
|
|
|
|
|
//
|
|
|
|
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) {
|
|
|
|
|
|
|
|
// populate section hash table...
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
for (auto idx = 0u; idx < img->file_header.num_sections; ++idx) {
|
|
|
|
|
|
|
|
auto scn = img->get_section(idx);
|
|
|
|
|
|
|
|
auto scn_sym_name =
|
|
|
|
|
|
|
|
std::string(scn->name.to_string(img->get_strings()))
|
|
|
|
|
|
|
|
.append("#")
|
|
|
|
|
|
|
|
.append(std::to_string(idx))
|
|
|
|
|
|
|
|
.append("!")
|
|
|
|
|
|
|
|
.append(std::to_string(img->file_header.timedate_stamp));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// hash the name of the section + the index + thhe timestamp of the obj
|
|
|
|
|
|
|
|
// file it is in...
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
m_scn_hash_tbl.insert({scn, decomp::symbol_t::hash(scn_sym_name)});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
auto syms_cnt = img->file_header.num_symbols;
|
|
|
|
auto syms_cnt = img->file_header.num_symbols;
|
|
|
|
for (auto idx = 0u; idx < syms_cnt; ++idx) {
|
|
|
|
for (auto idx = 0u; idx < syms_cnt; ++idx) {
|
|
|
|
auto sym = img->get_symbol(idx);
|
|
|
|
auto sym = img->get_symbol(idx);
|
|
|
@ -31,8 +53,8 @@ std::optional<comp::symbol_table_t*> decomp_t::decompose() {
|
|
|
|
auto scn = img->get_section(sym->section_index - 1);
|
|
|
|
auto scn = img->get_section(sym->section_index - 1);
|
|
|
|
auto dcmp_type =
|
|
|
|
auto dcmp_type =
|
|
|
|
scn->name.to_string(img->get_strings()) == INSTR_SPLIT_SECTION_NAME
|
|
|
|
scn->name.to_string(img->get_strings()) == INSTR_SPLIT_SECTION_NAME
|
|
|
|
? decomp::decomp_type_t::instr_split
|
|
|
|
? decomp::sym_type_t::inst_split
|
|
|
|
: decomp::decomp_type_t::none;
|
|
|
|
: decomp::sym_type_t::function;
|
|
|
|
auto fn_size = scn->size_raw_data;
|
|
|
|
auto fn_size = scn->size_raw_data;
|
|
|
|
auto fn_bgn = scn->ptr_raw_data + reinterpret_cast<std::uint8_t*>(img);
|
|
|
|
auto fn_bgn = scn->ptr_raw_data + reinterpret_cast<std::uint8_t*>(img);
|
|
|
|
|
|
|
|
|
|
|
@ -48,14 +70,54 @@ std::optional<comp::symbol_table_t*> decomp_t::decompose() {
|
|
|
|
} else if (sym->has_section() &&
|
|
|
|
} else if (sym->has_section() &&
|
|
|
|
sym->storage_class == coff::storage_class_id::public_symbol) {
|
|
|
|
sym->storage_class == coff::storage_class_id::public_symbol) {
|
|
|
|
auto scn = img->get_section(sym->section_index - 1);
|
|
|
|
auto scn = img->get_section(sym->section_index - 1);
|
|
|
|
spdlog::info("{} allocated in section: {} with size: {}",
|
|
|
|
auto scn_sym = m_syms->sym_from_hash(m_scn_hash_tbl[scn]);
|
|
|
|
sym->name.to_string(img->get_strings()),
|
|
|
|
|
|
|
|
scn->name.to_string(img->get_strings()),
|
|
|
|
// if the section doesnt have a symbol then make one and put it into the
|
|
|
|
|
|
|
|
// symbol table...
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
if (!scn_sym.has_value()) {
|
|
|
|
|
|
|
|
auto scn_sym_name =
|
|
|
|
|
|
|
|
std::string(scn->name.to_string(img->get_strings()))
|
|
|
|
|
|
|
|
.append("#")
|
|
|
|
|
|
|
|
.append(std::to_string(sym->section_index - 1))
|
|
|
|
|
|
|
|
.append("!")
|
|
|
|
|
|
|
|
.append(std::to_string(img->file_header.timedate_stamp));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::uint8_t> scn_data(
|
|
|
|
|
|
|
|
reinterpret_cast<std::uint8_t*>(img) + scn->ptr_raw_data,
|
|
|
|
|
|
|
|
reinterpret_cast<std::uint8_t*>(img) + scn->ptr_raw_data +
|
|
|
|
scn->size_raw_data);
|
|
|
|
scn->size_raw_data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
decomp::symbol_t new_scn_sym(scn_sym_name, 0, scn_data, scn, {}, {},
|
|
|
|
|
|
|
|
sym_type_t::section);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
spdlog::info(
|
|
|
|
|
|
|
|
"generating symbol for section: {} sym name: {} hash: {:X}",
|
|
|
|
|
|
|
|
scn->name.to_string(img->get_strings()), new_scn_sym.name(),
|
|
|
|
|
|
|
|
new_scn_sym.hash());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_syms->add_symbol(new_scn_sym);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// create a symbol for the data...
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
decomp::symbol_t new_sym(sym->name.to_string(img->get_strings()).data(),
|
|
|
|
|
|
|
|
sym->value, {}, scn, sym, {},
|
|
|
|
|
|
|
|
sym_type_t::data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
spdlog::info("adding data symbol: {} located inside of section: {}",
|
|
|
|
|
|
|
|
new_sym.name(),
|
|
|
|
|
|
|
|
m_syms->sym_from_hash(m_scn_hash_tbl[new_sym.scn()])
|
|
|
|
|
|
|
|
.value()
|
|
|
|
|
|
|
|
.name());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_syms->add_symbol(new_sym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return the extract symbols to the caller...
|
|
|
|
|
|
|
|
//
|
|
|
|
return m_syms;
|
|
|
|
return m_syms;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -74,4 +136,8 @@ std::vector<coff::image_t*> decomp_t::objs() {
|
|
|
|
comp::symbol_table_t* decomp_t::syms() {
|
|
|
|
comp::symbol_table_t* decomp_t::syms() {
|
|
|
|
return m_syms;
|
|
|
|
return m_syms;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::map<coff::section_header_t*, std::size_t>& decomp_t::scn_hash_tbl() {
|
|
|
|
|
|
|
|
return m_scn_hash_tbl;
|
|
|
|
|
|
|
|
}
|
|
|
|
} // namespace theo::decomp
|
|
|
|
} // namespace theo::decomp
|