Skip to content

Commit 0eada1f

Browse files
committed
Add mangled name maps for functions and global variables
1 parent a6fba53 commit 0eada1f

5 files changed

Lines changed: 280 additions & 77 deletions

File tree

src/ccc/mdebug_analysis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Result<void> LocalSymbolTableAnalyser::sub_source_file(const char* path, Address
106106
Result<void> LocalSymbolTableAnalyser::procedure(
107107
const char* mangled_name, Address address, const ProcedureDescriptor* procedure_descriptor, bool is_static)
108108
{
109-
if (!m_current_function || strcmp(mangled_name, m_current_function->mangled_name().c_str()) != 0) {
109+
if (!m_current_function || strcmp(mangled_name, m_current_function->raw_name().c_str()) != 0) {
110110
Result<void> result = create_function(mangled_name, address);
111111
CCC_RETURN_IF_ERROR(result);
112112
}
@@ -146,7 +146,7 @@ Result<void> LocalSymbolTableAnalyser::text_end(const char* name, s32 function_s
146146

147147
Result<void> LocalSymbolTableAnalyser::function(const char* mangled_name, const StabsType& return_type, Address address)
148148
{
149-
if (!m_current_function || strcmp(mangled_name, m_current_function->mangled_name().c_str()) != 0) {
149+
if (!m_current_function || strcmp(mangled_name, m_current_function->raw_name().c_str()) != 0) {
150150
Result<void> result = create_function(mangled_name, address);
151151
CCC_RETURN_IF_ERROR(result);
152152
} else {

src/ccc/mdebug_importer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ static void detect_duplicate_functions(SymbolDatabase& database, const SymbolGro
543543
for (FunctionHandle handle : functions_with_same_address) {
544544
ccc::Function* function = database.functions.symbol_from_handle(handle);
545545
if (!function || !group.is_in_group(*function)
546-
|| function->mangled_name() != test_function.mangled_name()) {
546+
|| function->raw_name() != test_function.raw_name()) {
547547
continue;
548548
}
549549

@@ -588,12 +588,12 @@ static void detect_fake_functions(
588588

589589
auto external_function = external_functions.find(function.address().value);
590590
if (external_function == external_functions.end()
591-
|| strcmp(function.mangled_name().c_str(), external_function->second->string) != 0) {
591+
|| strcmp(function.raw_name().c_str(), external_function->second->string) != 0) {
592592
database.functions.move_symbol(function.handle(), Address());
593593

594594
if (fake_function_count < 10) {
595595
CCC_WARN("Discarding address of function symbol '%s' as it is probably incorrect.",
596-
function.mangled_name().c_str());
596+
function.raw_name().c_str());
597597
} else if (fake_function_count == 10) {
598598
CCC_WARN("Discarding more addresses of function symbols.");
599599
}

src/ccc/symbol_database.cpp

Lines changed: 134 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -153,46 +153,22 @@ template <typename SymbolType>
153153
SymbolHandle<SymbolType> SymbolList<SymbolType>::first_handle_from_starting_address(Address address) const
154154
{
155155
auto iterator = m_address_to_handle.find(address.value);
156-
if (iterator != m_address_to_handle.end()) {
157-
return iterator->second;
158-
} else {
156+
if (iterator == m_address_to_handle.end()) {
159157
return SymbolHandle<SymbolType>();
160158
}
161-
}
162-
163-
template <typename SymbolType>
164-
std::vector<SymbolHandle<SymbolType>> SymbolList<SymbolType>::handles_from_name(const std::string& name) const
165-
{
166-
std::vector<SymbolHandle<SymbolType>> handles;
167159

168-
auto [begin, end] = m_name_to_handle.equal_range(name);
169-
for (auto iterator = begin; iterator != end; iterator++) {
170-
handles.emplace_back(iterator->second);
171-
}
172-
173-
return handles;
160+
return iterator->second;
174161
}
175162

176163
template <typename SymbolType>
177164
SymbolHandle<SymbolType> SymbolList<SymbolType>::first_handle_after_address(Address address) const
178165
{
179166
auto iterator = m_address_to_handle.upper_bound(address.value);
180-
if (iterator != m_address_to_handle.end()) {
181-
return iterator->second;
182-
} else {
167+
if (iterator == m_address_to_handle.end()) {
183168
return SymbolHandle<SymbolType>();
184169
}
185-
}
186170

187-
template <typename SymbolType>
188-
SymbolHandle<SymbolType> SymbolList<SymbolType>::first_handle_from_name(const std::string& name) const
189-
{
190-
auto iterator = m_name_to_handle.find(name);
191-
if (iterator != m_name_to_handle.end()) {
192-
return iterator->second;
193-
} else {
194-
return SymbolHandle<SymbolType>();
195-
}
171+
return iterator->second;
196172
}
197173

198174
template <typename SymbolType>
@@ -215,6 +191,55 @@ const SymbolType* SymbolList<SymbolType>::symbol_overlapping_address(Address add
215191
return const_cast<SymbolList<SymbolType>*>(this)->symbol_overlapping_address(address);
216192
}
217193

194+
template <typename SymbolType>
195+
std::vector<SymbolHandle<SymbolType>> SymbolList<SymbolType>::handles_from_name(const std::string& name) const
196+
{
197+
std::vector<SymbolHandle<SymbolType>> handles;
198+
199+
auto [begin, end] = m_name_to_handle.equal_range(name);
200+
for (auto iterator = begin; iterator != end; iterator++) {
201+
handles.emplace_back(iterator->second);
202+
}
203+
204+
return handles;
205+
}
206+
207+
template <typename SymbolType>
208+
SymbolHandle<SymbolType> SymbolList<SymbolType>::first_handle_from_name(const std::string& name) const
209+
{
210+
auto iterator = m_name_to_handle.find(name);
211+
if (iterator == m_name_to_handle.end()) {
212+
return SymbolHandle<SymbolType>();
213+
}
214+
215+
return iterator->second;
216+
}
217+
218+
template <typename SymbolType>
219+
std::vector<SymbolHandle<SymbolType>> SymbolList<SymbolType>::handles_from_mangled_name(
220+
const std::string& mangled_name) const
221+
{
222+
std::vector<SymbolHandle<SymbolType>> handles;
223+
224+
auto [begin, end] = m_mangled_name_to_handle.equal_range(mangled_name);
225+
for (auto iterator = begin; iterator != end; iterator++) {
226+
handles.emplace_back(iterator->second);
227+
}
228+
229+
return handles;
230+
}
231+
232+
template <typename SymbolType>
233+
SymbolHandle<SymbolType> SymbolList<SymbolType>::first_handle_from_mangled_name(const std::string& mangled_name) const
234+
{
235+
auto iterator = m_mangled_name_to_handle.find(mangled_name);
236+
if (iterator == m_mangled_name_to_handle.end()) {
237+
return SymbolHandle<SymbolType>();
238+
}
239+
240+
return iterator->second;
241+
}
242+
218243
template <typename SymbolType>
219244
s32 SymbolList<SymbolType>::index_from_handle(SymbolHandle<SymbolType> handle) const
220245
{
@@ -248,7 +273,6 @@ bool SymbolList<SymbolType>::empty() const
248273
return m_symbols.size() == 0;
249274
}
250275

251-
252276
template <typename SymbolType>
253277
s32 SymbolList<SymbolType>::size() const
254278
{
@@ -307,7 +331,7 @@ Result<SymbolType*> SymbolList<SymbolType>::create_symbol(std::string name,
307331
static const int DMGL_RET_POSTFIX = 1 << 5;
308332

309333
std::string demangled_name;
310-
if constexpr (SymbolType::FLAGS & NAME_NEEDS_DEMANGLING) {
334+
if constexpr (SymbolType::FLAGS & MANGLED_NAMES) {
311335
if ((importer_flags & DONT_DEMANGLE_NAMES) == 0 && demangler.cplus_demangle) {
312336
int demangler_flags = 0;
313337
if (importer_flags & DEMANGLE_PARAMETERS)
@@ -327,10 +351,11 @@ Result<SymbolType*> SymbolList<SymbolType>::create_symbol(std::string name,
327351
Result<SymbolType*> symbol = create_symbol(non_mangled_name, address, source, module_symbol);
328352
CCC_RETURN_IF_ERROR(symbol);
329353

330-
if constexpr (SymbolType::FLAGS & NAME_NEEDS_DEMANGLING) {
354+
if constexpr (SymbolType::FLAGS & MANGLED_NAMES) {
331355
if (!demangled_name.empty()) {
332-
(*symbol)->set_mangled_name(name);
356+
(*symbol)->m_mangled_name = std::move(name);
333357
}
358+
link_mangled_name_map(**symbol);
334359
}
335360

336361
return symbol;
@@ -370,19 +395,40 @@ bool SymbolList<SymbolType>::rename_symbol(SymbolHandle<SymbolType> handle, std:
370395
return true;
371396
}
372397

398+
template <typename SymbolType>
399+
bool SymbolList<SymbolType>::remangle_symbol(SymbolHandle<SymbolType> handle, std::string new_mangled_name)
400+
{
401+
if constexpr (SymbolType::FLAGS & MANGLED_NAMES) {
402+
SymbolType* symbol = symbol_from_handle(handle);
403+
if (!symbol) {
404+
return false;
405+
}
406+
407+
if (symbol->mangled_name() != new_mangled_name) {
408+
unlink_name_map(*symbol);
409+
symbol->m_mangled_name = std::move(new_mangled_name);
410+
link_name_map(*symbol);
411+
}
412+
413+
return true;
414+
}
415+
416+
return false;
417+
}
418+
373419
template <typename SymbolType>
374420
void SymbolList<SymbolType>::merge_from(
375421
SymbolList<SymbolType>& list, SymbolDatabase& src_database, SymbolDatabase& dest_database)
376422
{
377-
m_address_to_handle.clear();
378-
m_name_to_handle.clear();
379-
380423
std::vector<SymbolType> lhs = std::move(m_symbols);
381424
std::vector<SymbolType> rhs = std::move(list.m_symbols);
382425

383-
m_symbols = std::vector<SymbolType>();
426+
clear();
427+
list.clear();
428+
384429
m_symbols.reserve(lhs.size() + rhs.size());
385430

431+
// Merge the lists such that the symbols are still sorted by their handles.
386432
size_t lhs_pos = 0;
387433
size_t rhs_pos = 0;
388434
for (;;) {
@@ -406,13 +452,10 @@ void SymbolList<SymbolType>::merge_from(
406452

407453
link_address_map(*symbol);
408454
link_name_map(*symbol);
455+
link_mangled_name_map(*symbol);
409456
}
410457

411458
CCC_ASSERT(m_symbols.size() == lhs.size() + rhs.size());
412-
413-
list.m_symbols.clear();
414-
list.m_address_to_handle.clear();
415-
list.m_name_to_handle.clear();
416459
}
417460

418461
template <typename SymbolType>
@@ -468,6 +511,7 @@ void SymbolList<SymbolType>::destroy_marked_symbols()
468511
if (symbol.m_marked_for_destruction) {
469512
unlink_address_map(symbol);
470513
unlink_name_map(symbol);
514+
unlink_mangled_name_map(symbol);
471515
} else {
472516
remaining_symbols.emplace_back(std::move(symbol));
473517
}
@@ -479,9 +523,10 @@ void SymbolList<SymbolType>::destroy_marked_symbols()
479523
template <typename SymbolType>
480524
void SymbolList<SymbolType>::clear()
481525
{
482-
m_symbols.clear();
483-
m_address_to_handle.clear();
484-
m_name_to_handle.clear();
526+
m_symbols = std::vector<SymbolType>();
527+
m_address_to_handle = AddressToHandleMap();
528+
m_name_to_handle = NameToHandleMap();
529+
m_mangled_name_to_handle = NameToHandleMap();
485530
}
486531

487532
template <typename SymbolType>
@@ -534,19 +579,49 @@ template <typename SymbolType>
534579
void SymbolList<SymbolType>::link_name_map(SymbolType& symbol)
535580
{
536581
if constexpr (SymbolType::FLAGS & WITH_NAME_MAP) {
537-
m_name_to_handle.emplace(symbol.name(), symbol.handle());
582+
if (!symbol.name().empty()) {
583+
m_name_to_handle.emplace(symbol.name(), symbol.handle());
584+
}
538585
}
539586
}
540587

541588
template <typename SymbolType>
542589
void SymbolList<SymbolType>::unlink_name_map(SymbolType& symbol)
543590
{
544591
if constexpr (SymbolType::FLAGS & WITH_NAME_MAP) {
545-
auto iterators = m_name_to_handle.equal_range(symbol.name());
546-
for (auto iterator = iterators.first; iterator != iterators.second; iterator++) {
547-
if (iterator->second == symbol.handle()) {
548-
m_name_to_handle.erase(iterator);
549-
break;
592+
if (!symbol.name().empty()) {
593+
auto iterators = m_name_to_handle.equal_range(symbol.name());
594+
for (auto iterator = iterators.first; iterator != iterators.second; iterator++) {
595+
if (iterator->second == symbol.handle()) {
596+
m_name_to_handle.erase(iterator);
597+
break;
598+
}
599+
}
600+
}
601+
}
602+
}
603+
604+
template <typename SymbolType>
605+
void SymbolList<SymbolType>::link_mangled_name_map(SymbolType& symbol)
606+
{
607+
if constexpr (SymbolType::FLAGS & MANGLED_NAMES) {
608+
if (!symbol.mangled_name().empty()) {
609+
m_mangled_name_to_handle.emplace(symbol.mangled_name(), symbol.handle());
610+
}
611+
}
612+
}
613+
614+
template <typename SymbolType>
615+
void SymbolList<SymbolType>::unlink_mangled_name_map(SymbolType& symbol)
616+
{
617+
if constexpr (SymbolType::FLAGS & MANGLED_NAMES) {
618+
if (!symbol.mangled_name().empty()) {
619+
auto iterators = m_mangled_name_to_handle.equal_range(symbol.mangled_name());
620+
for (auto iterator = iterators.first; iterator != iterators.second; iterator++) {
621+
if (iterator->second == symbol.handle()) {
622+
m_mangled_name_to_handle.erase(iterator);
623+
break;
624+
}
550625
}
551626
}
552627
}
@@ -654,6 +729,11 @@ void Function::set_local_variables(
654729
}
655730

656731
const std::string& Function::mangled_name() const
732+
{
733+
return m_mangled_name;
734+
}
735+
736+
const std::string& Function::raw_name() const
657737
{
658738
if (!m_mangled_name.empty()) {
659739
return m_mangled_name;
@@ -662,11 +742,6 @@ const std::string& Function::mangled_name() const
662742
}
663743
}
664744

665-
void Function::set_mangled_name(std::string mangled)
666-
{
667-
m_mangled_name = std::move(mangled);
668-
}
669-
670745
u32 Function::original_hash() const
671746
{
672747
return m_original_hash;
@@ -709,6 +784,11 @@ void Function::on_destroy(SymbolDatabase* database)
709784
// *****************************************************************************
710785

711786
const std::string& GlobalVariable::mangled_name() const
787+
{
788+
return m_mangled_name;
789+
}
790+
791+
const std::string& GlobalVariable::raw_name() const
712792
{
713793
if (!m_mangled_name.empty()) {
714794
return m_mangled_name;
@@ -717,11 +797,6 @@ const std::string& GlobalVariable::mangled_name() const
717797
}
718798
}
719799

720-
void GlobalVariable::set_mangled_name(std::string mangled)
721-
{
722-
m_mangled_name = std::move(mangled);
723-
}
724-
725800
// *****************************************************************************
726801

727802
void Module::on_create()

0 commit comments

Comments
 (0)