Skip to content

Commit

Permalink
Support pre-RTTI debug symbol information
Browse files Browse the repository at this point in the history
Allow to use the API on old .smx binaries too.
  • Loading branch information
peace-maker committed Jan 5, 2025
1 parent 6bbb94c commit 5185822
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
68 changes: 66 additions & 2 deletions vm/smx-v1-image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,12 +1060,71 @@ SmxV1Image::GetFileName(size_t index) const
return debug_names_ + debug_files_[index].name;
}

template <typename SymbolType, typename DimType>
uint32_t
SmxV1Image::getFunctionCount(const SymbolType* syms) const {
const uint8_t* cursor = reinterpret_cast<const uint8_t*>(syms);
const uint8_t* cursor_end = cursor + debug_symbols_section_->size;
uint32_t func_count = 0;
for (uint32_t i = 0; i < debug_info_->num_syms; i++) {
if (cursor + sizeof(SymbolType) > cursor_end)
break;

const SymbolType* sym = reinterpret_cast<const SymbolType*>(cursor);
if (sym->ident == sp::IDENT_FUNCTION)
func_count++;

if (sym->dimcount > 0)
cursor += sizeof(DimType) * sym->dimcount;
cursor += sizeof(SymbolType);
}
return func_count;
}

size_t
SmxV1Image::NumFunctions() const {
if (rtti_methods_) {
return rtti_methods_->row_count;
}
return 0;

// Count function symbols once.
static uint32_t num_debug_functions = 0;
if (num_debug_functions == 0) {
if (debug_syms_)
num_debug_functions = getFunctionCount<sp_fdbg_symbol_t, sp_fdbg_arraydim_t>(debug_syms_);
else
num_debug_functions = getFunctionCount<sp_u_fdbg_symbol_t, sp_u_fdbg_arraydim_t>(debug_syms_unpacked_);
}
return num_debug_functions;
}

template <typename SymbolType, typename DimType>
const char*
SmxV1Image::getFunctionName(const SymbolType* syms, const char** filename, uint32_t index) const {
const uint8_t* cursor = reinterpret_cast<const uint8_t*>(syms);
const uint8_t* cursor_end = cursor + debug_symbols_section_->size;
uint32_t func_count = 0;
for (uint32_t i = 0; i < debug_info_->num_syms; i++) {
if (cursor + sizeof(SymbolType) > cursor_end)
break;

const SymbolType* sym = reinterpret_cast<const SymbolType*>(cursor);
if (sym->ident == sp::IDENT_FUNCTION) {
if (func_count == index) {
if (filename)
*filename = LookupFile(sym->addr);
if (sym->name < debug_names_section_->size)
return debug_names_ + sym->name;
return nullptr;
}
func_count++;
}

if (sym->dimcount > 0)
cursor += sizeof(DimType) * sym->dimcount;
cursor += sizeof(SymbolType);
}
return nullptr;
}

const char*
Expand All @@ -1079,7 +1138,12 @@ SmxV1Image::GetFunctionName(size_t index, const char** filename) const {
*filename = LookupFile(method->pcode_start);
return names_ + method->name;
}
return nullptr;

if (debug_syms_) {
return getFunctionName<sp_fdbg_symbol_t, sp_fdbg_arraydim_t>(debug_syms_, filename, index);
} else {
return getFunctionName<sp_u_fdbg_symbol_t, sp_u_fdbg_arraydim_t>(debug_syms_unpacked_, filename, index);
}
}

template <typename SymbolType, typename DimType>
Expand Down
4 changes: 4 additions & 0 deletions vm/smx-v1-image.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ class SmxV1Image
template <typename SymbolType, typename DimType>
const char* lookupFunction(const SymbolType* syms, uint32_t addr) const;
template <typename SymbolType, typename DimType>
uint32_t getFunctionCount(const SymbolType* syms) const;
template <typename SymbolType, typename DimType>
const char* getFunctionName(const SymbolType* syms, const char** filename, uint32_t index) const;
template <typename SymbolType, typename DimType>
bool getFunctionAddress(const SymbolType* syms, const char* function, ucell_t* funcaddr, uint32_t& index) const;

const smx_rtti_table_header* findRttiSection(const char* name) const {
Expand Down

0 comments on commit 5185822

Please sign in to comment.