From fb6373ec630c20e347e57d3f272deaa7d5e66005 Mon Sep 17 00:00:00 2001 From: Maschell Date: Tue, 10 May 2022 22:06:58 +0200 Subject: [PATCH] Fix elfLinkOne to support a tramp in both directions --- source/utils/ElfUtils.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/source/utils/ElfUtils.cpp b/source/utils/ElfUtils.cpp index 307cc2e..342353e 100644 --- a/source/utils/ElfUtils.cpp +++ b/source/utils/ElfUtils.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "ElfUtils.h" @@ -99,9 +100,16 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des DEBUG_FUNCTION_LINE_ERR("***value %08X - target %08X = distance %08X", value, target, target - (uint32_t) & (freeSlot->trampoline[0])); return false; } - if (target - (uint32_t) & (freeSlot->trampoline[0]) > 0x1FFFFFC) { + auto symbolValue = (uint32_t) & (freeSlot->trampoline[0]); + auto newValue = symbolValue + addend; + auto newDistance = static_cast(newValue) - static_cast(target); + if (newDistance > 0x1FFFFFC || newDistance < -0x1FFFFFC) { DEBUG_FUNCTION_LINE_ERR("**Cannot link 24-bit jump (too far to tramp buffer)."); - DEBUG_FUNCTION_LINE_ERR("***value %08X - target %08X = distance %08X", value, target, (target - (uint32_t) & (freeSlot->trampoline[0]))); + if (newDistance < 0) { + DEBUG_FUNCTION_LINE_ERR("***value %08X - target %08X = distance -%08X", newValue, target, abs(newDistance)); + } else { + DEBUG_FUNCTION_LINE_ERR("***value %08X - target %08X = distance %08X", newValue, target, newDistance); + } return false; } @@ -122,9 +130,7 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des // Relocations for the imports may be overridden freeSlot->status = RELOC_TRAMP_IMPORT_DONE; } - auto symbolValue = (uint32_t) & (freeSlot->trampoline[0]); - value = symbolValue + addend; - distance = static_cast(value) - static_cast(target); + distance = newDistance; } } @@ -150,5 +156,7 @@ bool ElfUtils::elfLinkOne(char type, size_t offset, int32_t addend, uint32_t des DEBUG_FUNCTION_LINE_ERR("***ERROR: Unsupported Relocation_Add Type (%08X):", type); return false; } + ICInvalidateRange(reinterpret_cast(target), 4); + DCFlushRange(reinterpret_cast(target), 4); return true; }