@@ -26,7 +26,7 @@ PG_MODULE_MAGIC;
2626void _PG_init (void );
2727void _PG_fini (void );
2828
29- #define DEBUG_GEN 0
29+ #define DEBUG_GEN 1
3030#define SHOW_TIME 1
3131
3232static const char * opcodeNames [] = {
@@ -329,7 +329,7 @@ static void apply_arm64_x26 (CodeGen *codeGen, size_t u32offset, intptr_t target
329329 // Now we can code a 26bits delta using the offset between codeGen->code+u32offset and trampoline_address
330330 intptr_t current_address = & (codeGen -> code .as_u32 [u32offset ]);
331331 int32_t delta = current_address - trampoline_address ;
332- if (delta < (1 << 26 ) && delta > - (1 << 26 ))
332+ if (delta > (1 << 26 ) || delta < - (1 << 26 ))
333333 elog (WARNING , "Computed delta, %p, from %p to %p, is far too big" , delta , current_address , trampoline_address );
334334
335335 // Force instruction target bits to 0, for safety
@@ -459,8 +459,12 @@ static void apply_jump(CodeGen *codeGen, size_t offset, intptr_t target, const s
459459
460460static void apply_patch_with_target (CodeGen * codeGen , size_t offset , intptr_t target , const struct Patch * patch )
461461{
462- size_t u32offset = offset / 4 ;
462+ #if defined(__aarch64__ ) || defined(_M_ARM64 )
463+ size_t u32offset = (offset + patch -> offset ) / 4 ;
463464 uint32_t value ;
465+ #endif
466+ if (DEBUG_GEN )
467+ elog (WARNING , "Applying a patch at offset %i+%i, target %p, kind %i" , offset , patch -> offset , target , patch -> relkind );
464468 switch (patch -> relkind ) {
465469#if defined(__x86_64__ )
466470 case RELKIND_R_X86_64_64 :
@@ -473,19 +477,35 @@ static void apply_patch_with_target (CodeGen *codeGen, size_t offset, intptr_t t
473477#if defined(__aarch64__ ) || defined(_M_ARM64 )
474478 case RELKIND_R_AARCH64_MOVW_UABS_G0_NC :
475479 value = target & 0xFFFF ;
480+ if (DEBUG_GEN )
481+ elog (WARNING , "Patching 0x%08x with value %p (moved to %p)" , codeGen -> code .as_u32 [u32offset ], value , value << 5 );
476482 codeGen -> code .as_u32 [u32offset ] = (codeGen -> code .as_u32 [u32offset ] | (value << 5 ));
483+ if (DEBUG_GEN )
484+ elog (WARNING , "Patched 0x%08x with value %p (moved to %p)" , codeGen -> code .as_u32 [u32offset ], value , value << 5 );
477485 break ;
478486 case RELKIND_R_AARCH64_MOVW_UABS_G1_NC :
479487 value = (target & 0xFFFF0000 ) >> 16 ;
488+ if (DEBUG_GEN )
489+ elog (WARNING , "Patching 0x%08x with value %p (moved to %p)" , codeGen -> code .as_u32 [u32offset ], value , value << 5 );
480490 codeGen -> code .as_u32 [u32offset ] = (codeGen -> code .as_u32 [u32offset ] | (value << 5 ));
491+ if (DEBUG_GEN )
492+ elog (WARNING , "Patched 0x%08x with value %p (moved to %p)" , codeGen -> code .as_u32 [u32offset ], value , value << 5 );
481493 break ;
482494 case RELKIND_R_AARCH64_MOVW_UABS_G2_NC :
483495 value = (target & 0xFFFF00000000 ) >> 32 ;
496+ if (DEBUG_GEN )
497+ elog (WARNING , "Patching 0x%08x with value %p (moved to %p)" , codeGen -> code .as_u32 [u32offset ], value , value << 5 );
484498 codeGen -> code .as_u32 [u32offset ] = (codeGen -> code .as_u32 [u32offset ] | (value << 5 ));
499+ if (DEBUG_GEN )
500+ elog (WARNING , "Patched 0x%08x with value %p (moved to %p)" , codeGen -> code .as_u32 [u32offset ], value , value << 5 );
485501 break ;
486502 case RELKIND_R_AARCH64_MOVW_UABS_G3 :
487503 value = (target & 0xFFFF000000000000 ) >> 48 ;
504+ if (DEBUG_GEN )
505+ elog (WARNING , "Patching 0x%08x with value %p (moved to %p)" , codeGen -> code .as_u32 [u32offset ], value , value << 5 );
488506 codeGen -> code .as_u32 [u32offset ] = (codeGen -> code .as_u32 [u32offset ] | (value << 5 ));
507+ if (DEBUG_GEN )
508+ elog (WARNING , "Patched 0x%08x with value %p (moved to %p)" , codeGen -> code .as_u32 [u32offset ], value , value << 5 );
489509 break ;
490510 // These two require trampolines
491511 case RELKIND_R_AARCH64_JUMP26 :
@@ -646,8 +666,10 @@ copyjit_compile_expr(ExprState *state)
646666 if (DEBUG_GEN )
647667 elog (WARNING , "Result of mprotect is %i" , mprotect_res );
648668 state -> evalfunc_private = codeGen .code .as_void ;
649- state -> evalfunc = (ExprStateEvalFunc ) codeGen .code .as_void ; // When this one starts being usefull, we can bring it back. ExecRunCompiledExpr;
669+ // state->evalfunc = (ExprStateEvalFunc) codeGen.code.as_void; // We jump through ExecRunCompiledExpr so we can breakpoint, if needed...
650670 state -> evalfunc = ExecRunCompiledExpr ;
671+ if (DEBUG_GEN )
672+ elog (WARNING , "Code generated is located at %p for %i bytes (with %i trampolines)" , codeGen .code .as_void , codeGen .code_size , required_trampolines );
651673 }
652674 free (codeGen .offsets );
653675 if (codeGen .trampoline_targets )
0 commit comments