Bladeren bron

patch previous method in-place to ensure redirection to hot loaded one

Nicolas Cannasse 6 jaren geleden
bovenliggende
commit
a9be75999b
3 gewijzigde bestanden met toevoegingen van 29 en 0 verwijderingen
  1. 1 0
      src/hlmodule.h
  2. 27 0
      src/jit.c
  3. 1 0
      src/module.c

+ 1 - 0
src/hlmodule.h

@@ -128,3 +128,4 @@ void hl_jit_reset( jit_ctx *ctx, hl_module *m );
 void hl_jit_init( jit_ctx *ctx, hl_module *m );
 int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f );
 void *hl_jit_code( jit_ctx *ctx, hl_module *m, int *codesize, hl_debug_infos **debug, hl_module *previous );
+void hl_jit_patch_method( void *old_fun, void **new_fun_table );

+ 27 - 0
src/jit.c

@@ -4102,6 +4102,33 @@ static void *get_wrapper( hl_type *t ) {
 	return call_jit_hl2c;
 }
 
+void hl_jit_patch_method( void *old_fun, void **new_fun_table ) {
+	// mov eax, addr
+	// jmp [eax]
+	unsigned char *b = (unsigned char*)old_fun;
+	unsigned long long addr = (unsigned long long)(int_val)new_fun_table;
+#	ifdef HL_64
+	*b++ = 0x48;
+	*b++ = 0xB8;
+	*b++ = (unsigned char)addr;
+	*b++ = (unsigned char)(addr>>8);
+	*b++ = (unsigned char)(addr>>16);
+	*b++ = (unsigned char)(addr>>24);
+	*b++ = (unsigned char)(addr>>32);
+	*b++ = (unsigned char)(addr>>40);
+	*b++ = (unsigned char)(addr>>48);
+	*b++ = (unsigned char)(addr>>56);
+#	else
+	*b++ = 0xB8;
+	*b++ = (unsigned char)addr;
+	*b++ = (unsigned char)(addr>>8);
+	*b++ = (unsigned char)(addr>>16);
+	*b++ = (unsigned char)(addr>>24);
+#	endif
+	*b++ = 0xFF;
+	*b++ = 0x20;
+}
+
 void *hl_jit_code( jit_ctx *ctx, hl_module *m, int *codesize, hl_debug_infos **debug, hl_module *previous ) {
 	jlist *c;
 	int size = BUF_POS();

+ 1 - 0
src/module.c

@@ -568,6 +568,7 @@ bool hl_module_patch( hl_module *m1, hl_code *c ) {
 		m2->functions_ptrs[f2->findex] = ptr;
 		// update real function ptr
 		hl_function *f1 = m1->code->functions + m2->functions_hashes[i];
+		hl_jit_patch_method(m1->functions_ptrs[f1->findex], m1->functions_ptrs + f1->findex);
 		m1->functions_ptrs[f1->findex] = ptr;
 	}
 	for(i=0;i<m1->code->ntypes;i++) {