Skip to content

Commit

Permalink
Merge pull request #2 from skiff/develop
Browse files Browse the repository at this point in the history
Fix OPD generation for symbols
  • Loading branch information
skiff authored Jul 1, 2021
2 parents a79ee31 + 566d011 commit 92d4a2e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
1 change: 1 addition & 0 deletions libpsutil/libpsutil.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<ClCompile Include="network\httpclient.cpp" />
<ClCompile Include="network\socket.cpp" />
<ClCompile Include="string.cpp" />
<ClCompile Include="symbol.cpp" />
<ClCompile Include="system\filesystem.cpp" />
<ClCompile Include="system\imports.cpp" />
<ClCompile Include="system\memory.cpp" />
Expand Down
1 change: 1 addition & 0 deletions libpsutil/libpsutil.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<Filter>network</Filter>
</ClCompile>
<ClCompile Include="string.cpp" />
<ClCompile Include="symbol.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="math.hpp" />
Expand Down
31 changes: 31 additions & 0 deletions libpsutil/symbol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "symbol.hpp"

namespace libpsutil
{
namespace symbol_helper
{
struct opd_s
{
unsigned int address;
unsigned int toc;
};

std::vector<opd_s> symbol_table;
void* get_symbol(unsigned int address)
{
for (auto i = 0; i < symbol_table.size(); i++)
{
opd_s* opd = &symbol_table[i];
if (opd->address == address)
return opd;
}

symbol_table.push_back(opd_s());
opd_s* opd = &symbol_table[symbol_table.size() - 1];
opd->address = address;
opd->toc = memory::get_game_toc();

return opd;
}
}
}
19 changes: 6 additions & 13 deletions libpsutil/symbol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@

namespace libpsutil
{
template<typename T> class symbol
namespace symbol_helper
{
private:
static int opd_table[10][2];
static int opd_table_id;
void* get_symbol(unsigned int address);
}

template<typename T> class symbol
{
public:
operator T* ()
{
if (opd_table_id > 9) { opd_table_id = 0; }

opd_table[opd_table_id][0] = this->mp_object_;
opd_table[opd_table_id][1] = memory::get_game_toc();

T* type = *reinterpret_cast<T*>(this->mp_object_);
return (decltype(type))opd_table[opd_table_id++];
return (decltype(type))symbol_helper::get_symbol(this->mp_object_);
}

T* get()
Expand All @@ -28,7 +24,4 @@ namespace libpsutil

int mp_object_;
};

template <typename T> int symbol<T>::opd_table[10][2];
template <typename T> int symbol<T>::opd_table_id;
}
4 changes: 2 additions & 2 deletions libpsutil/system/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace libpsutil
detour::force_stub_addr = address;
}

void detour::setup_detour(uint32_t address, void *destination)
void detour::setup_detour(uint32_t address, void *destination, uint32_t toc_override)
{
if (address == NULL) { return; }

Expand Down Expand Up @@ -131,7 +131,7 @@ namespace libpsutil
memory::jump(address, *reinterpret_cast<uint32_t*>(destination), false);

this->stub_opd[0] = reinterpret_cast<uint32_t>(stub_address);
this->stub_opd[1] = memory::get_game_toc();
this->stub_opd[1] = toc_override != 0 ? toc_override : memory::get_game_toc();
}

detour::~detour()
Expand Down
6 changes: 3 additions & 3 deletions libpsutil/system/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ namespace libpsutil

uint32_t allocate_stub();
uint32_t resolve_branch(uint32_t instruction, uint32_t branch_address);
void setup_detour(uint32_t address, void* destination);
void setup_detour(uint32_t address, void* destination, uint32_t toc_override);

public:
template<class T> detour(uint32_t address, T(*destination))
template<class T> detour(uint32_t address, T(*destination), uint32_t toc_override = 0)
{
this->setup_detour(address, reinterpret_cast<void*>(destination));
this->setup_detour(address, reinterpret_cast<void*>(destination), toc_override);
}

~detour();
Expand Down

0 comments on commit 92d4a2e

Please sign in to comment.