Skip to content

Commit 7ff99a5

Browse files
authored
debugger: Add support for loading symbols from .map file (#1916)
Map has to have the same name as the debugger/*.xml files, but with a .map extension
1 parent ad73c1e commit 7ff99a5

10 files changed

Lines changed: 288 additions & 112 deletions

File tree

src/Cafe/GraphicPack/GraphicPack2PatchesApply.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ void PatchEntryInstruction::undoPatch()
420420
uint8* patchAddr = (uint8*)memory_base + addr;
421421
memcpy(patchAddr, m_dataBackup, m_length);
422422
PPCRecompiler_invalidateRange(addr, addr + m_length);
423-
rplSymbolStorage_removeRange(addr, m_length);
423+
rplSymbolStorage_removeRange(addr, m_length, RPL_STORED_SYMBOL_PATCH);
424424
DebugSymbolStorage::ClearRange(addr, m_length);
425425
}
426426

@@ -434,7 +434,7 @@ bool registerU32Variable(PatchContext_t& ctx, std::string& name, uint32 value, P
434434
}
435435
ctx.map_values[name] = value;
436436
// keep track of address symbols for the debugger
437-
rplSymbolStorage_store(ctx.graphicPack->GetName().data(), name.data(), value);
437+
rplSymbolStorage_store(ctx.graphicPack->GetName().data(), name.data(), value, RPL_STORED_SYMBOL_PATCH);
438438

439439
return true;
440440
}

src/Cafe/OS/RPL/rpl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,10 @@ bool RPLLoader_LoadSections(sint32 aProcId, RPLModule* rplLoaderContext)
536536
rplLoaderContext->regionSize_loaderInfo = regionLoaderinfoSize;
537537
rplLoaderContext->regionSize_text = regionTextSize;
538538

539+
// set original base addresses
540+
rplLoaderContext->regionOrigAddr_text = regionMappingTable.region[RPL_MAPPING_REGION_TEXT].baseAddress;
541+
rplLoaderContext->regionOrigAddr_data = regionMappingTable.region[RPL_MAPPING_REGION_DATA].baseAddress;
542+
539543
// load data sections
540544
for (sint32 i = 0; i < (sint32)rplLoaderContext->rplHeader.sectionTableEntryCount; i++)
541545
{

src/Cafe/OS/RPL/rpl_structs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ struct RPLModule
171171
uint32 tlsStartAddress;
172172
uint32 tlsEndAddress;
173173
uint32 regionSize_text;
174+
uint32 regionOrigAddr_text;
174175
uint32 regionSize_data;
176+
uint32 regionOrigAddr_data;
175177
uint32 regionSize_loaderInfo;
176178

177179
uint32 patchCRC; // Cemuhook style module crc for patches.txt

src/Cafe/OS/RPL/rpl_symbol_storage.cpp

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ char* rplSymbolStorage_storeLibname(const char* libName)
6363
return libEntry->libName;
6464
}
6565

66-
RPLStoredSymbol* rplSymbolStorage_store(const char* libName, const char* symbolName, MPTR address)
66+
RPLStoredSymbol* rplSymbolStorage_store(const char* libName, const char* symbolName, MPTR address, uint32 type)
6767
{
6868
std::unique_lock<std::mutex> lck(rplSymbolStorage.m_symbolStorageMutex);
6969
char* libNameStorage = rplSymbolStorage_storeLibname(libName);
@@ -72,15 +72,22 @@ RPLStoredSymbol* rplSymbolStorage_store(const char* libName, const char* symbolN
7272
storedSymbol->address = address;
7373
storedSymbol->libName = libNameStorage;
7474
storedSymbol->symbolName = symbolNameStorage;
75-
storedSymbol->flags = 0;
75+
storedSymbol->flags = type;
76+
storedSymbol->previous = nullptr;
77+
auto it = rplSymbolStorage.map_symbolByAddress.find(address);
78+
if (it != rplSymbolStorage.map_symbolByAddress.end())
79+
storedSymbol->previous = it->second;
7680
rplSymbolStorage.map_symbolByAddress[address] = storedSymbol;
7781
return storedSymbol;
7882
}
7983

8084
RPLStoredSymbol* rplSymbolStorage_getByAddress(MPTR address)
8185
{
8286
std::unique_lock<std::mutex> lck(rplSymbolStorage.m_symbolStorageMutex);
83-
return rplSymbolStorage.map_symbolByAddress[address];
87+
auto it = rplSymbolStorage.map_symbolByAddress.find(address);
88+
if (it == rplSymbolStorage.map_symbolByAddress.end())
89+
return nullptr;
90+
return it->second;
8491
}
8592

8693
RPLStoredSymbol* rplSymbolStorage_getByClosestAddress(MPTR address)
@@ -89,7 +96,8 @@ RPLStoredSymbol* rplSymbolStorage_getByClosestAddress(MPTR address)
8996
std::unique_lock<std::mutex> lck(rplSymbolStorage.m_symbolStorageMutex);
9097
for(uint32 i=0; i<4096; i++)
9198
{
92-
RPLStoredSymbol* symbol = rplSymbolStorage.map_symbolByAddress[address];
99+
auto it = rplSymbolStorage.map_symbolByAddress.find(address);
100+
RPLStoredSymbol* symbol = (it == rplSymbolStorage.map_symbolByAddress.end()) ? nullptr : it->second;
93101
if(symbol)
94102
return symbol;
95103
address -= 4;
@@ -100,18 +108,63 @@ RPLStoredSymbol* rplSymbolStorage_getByClosestAddress(MPTR address)
100108
void rplSymbolStorage_remove(RPLStoredSymbol* storedSymbol)
101109
{
102110
std::unique_lock<std::mutex> lck(rplSymbolStorage.m_symbolStorageMutex);
103-
if (rplSymbolStorage.map_symbolByAddress[storedSymbol->address] == storedSymbol)
104-
rplSymbolStorage.map_symbolByAddress[storedSymbol->address] = nullptr;
111+
auto it = rplSymbolStorage.map_symbolByAddress.find(storedSymbol->address);
112+
if (it != rplSymbolStorage.map_symbolByAddress.end())
113+
{
114+
if (it->second == storedSymbol)
115+
{
116+
if (storedSymbol->previous)
117+
it->second = storedSymbol->previous;
118+
else
119+
rplSymbolStorage.map_symbolByAddress.erase(it);
120+
}
121+
else
122+
{
123+
RPLStoredSymbol* parentSymbol = it->second;
124+
while (parentSymbol && parentSymbol->previous != storedSymbol)
125+
parentSymbol = parentSymbol->previous;
126+
if (parentSymbol)
127+
parentSymbol->previous = storedSymbol->previous;
128+
}
129+
}
105130
delete storedSymbol;
106131
}
107132

108-
void rplSymbolStorage_removeRange(MPTR address, sint32 length)
133+
134+
void rplSymbolStorage_removeRange(MPTR address, sint32 length, uint32 type)
109135
{
136+
std::unique_lock<std::mutex> lck(rplSymbolStorage.m_symbolStorageMutex);
110137
while (length > 0)
111138
{
112-
RPLStoredSymbol* symbol = rplSymbolStorage_getByAddress(address);
113-
if (symbol)
114-
rplSymbolStorage_remove(symbol);
139+
auto it = rplSymbolStorage.map_symbolByAddress.find(address);
140+
if (it != rplSymbolStorage.map_symbolByAddress.end())
141+
{
142+
RPLStoredSymbol* current = it->second;
143+
RPLStoredSymbol* newHead = current;
144+
RPLStoredSymbol* previousKept = nullptr;
145+
while (current)
146+
{
147+
RPLStoredSymbol* next = current->previous;
148+
if ((current->flags & type) == type)
149+
{
150+
if (previousKept)
151+
previousKept->previous = next;
152+
else
153+
newHead = next;
154+
delete current;
155+
}
156+
else
157+
{
158+
previousKept = current;
159+
}
160+
current = next;
161+
}
162+
163+
if (newHead)
164+
it->second = newHead;
165+
else
166+
rplSymbolStorage.map_symbolByAddress.erase(it);
167+
}
115168
address += 4;
116169
length -= 4;
117170
}
@@ -145,7 +198,15 @@ void rplSymbolStorage_unloadAll()
145198
{
146199
// free symbols
147200
for (auto& it : rplSymbolStorage.map_symbolByAddress)
148-
delete it.second;
201+
{
202+
RPLStoredSymbol* symbol = it.second;
203+
while (symbol)
204+
{
205+
RPLStoredSymbol* previous = symbol->previous;
206+
delete symbol;
207+
symbol = previous;
208+
}
209+
}
149210
rplSymbolStorage.map_symbolByAddress.clear();
150211
// free libs
151212
rplSymbolLib_t* lib = rplSymbolStorage.libs;
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1+
enum RPLStoredSymbolType : uint32
2+
{
3+
RPL_STORED_SYMBOL_NONE = 0,
4+
RPL_STORED_SYMBOL_MAP = 1 << 0,
5+
RPL_STORED_SYMBOL_PATCH = 1 << 1,
6+
};
7+
18
struct RPLStoredSymbol
29
{
310
MPTR address;
411
void* libName;
512
void* symbolName;
613
uint32 flags;
14+
RPLStoredSymbol* previous;
715
};
816

917
void rplSymbolStorage_init();
1018
void rplSymbolStorage_unloadAll();
11-
RPLStoredSymbol* rplSymbolStorage_store(const char* libName, const char* symbolName, MPTR address);
19+
RPLStoredSymbol* rplSymbolStorage_store(const char* libName, const char* symbolName, MPTR address, uint32 type = RPL_STORED_SYMBOL_NONE);
1220
void rplSymbolStorage_remove(RPLStoredSymbol* storedSymbol);
13-
void rplSymbolStorage_removeRange(MPTR address, sint32 length);
21+
void rplSymbolStorage_removeRange(MPTR address, sint32 length, uint32 type = RPL_STORED_SYMBOL_NONE);
1422
RPLStoredSymbol* rplSymbolStorage_getByAddress(MPTR address);
1523
RPLStoredSymbol* rplSymbolStorage_getByClosestAddress(MPTR address);
1624
void rplSymbolStorage_createJumpProxySymbol(MPTR jumpAddress, MPTR destAddress);
1725

1826
std::unordered_map<uint32, RPLStoredSymbol*>& rplSymbolStorage_lockSymbolMap();
19-
void rplSymbolStorage_unlockSymbolMap();
27+
void rplSymbolStorage_unlockSymbolMap();

0 commit comments

Comments
 (0)