[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Tinycc-devel] Patching symbols after tcc_relocate
From: |
grischka |
Subject: |
Re: [Tinycc-devel] Patching symbols after tcc_relocate |
Date: |
Sun, 10 Feb 2013 14:01:49 +0100 |
User-agent: |
Thunderbird 2.0.0.23 (Windows/20090812) |
Henry Weller wrote:
This requires significant change to the operation of the executable for all
functions whether they are over-ridden or not which is an overhead I am not
prepared to accept. What I want is to be able to have a REPL for an executable
which is as efficient as possible. If this is not going to be possible within
libtcc then I will live with ELF-Hook but given that it is possible for DLs I
see no reason why it is not possible from libtcc after relocation.
Everything is possible. This is software after all.
Say your original function is:
int add(int a, int b) { return a + b; }
Your replacement (same prototype) is:
int sub(int a, int b) { return a - b; }
Have a tool to make it happen:
void replace_function(TCCState *s, const char *name, void *new)
{
char *old = tcc_get_symbol(s, name);
set_pages_executable(old, 5); //from tccrun.c
#ifdef ___i386__
*old = 0xe9;
*(unsigned*)(old + 1) = (char *)new - old - 5;
#endif
}
Put all that into libtcc_test.c:
/* run the code */
func(32);
+ printf("-- Replacing -- \n");
+ replace_function(s, "add", sub);
+ func(32);
/* delete the state */
tcc_delete(s);
Run:
Hello World!
fib(32) = 2178309
add(32, 64) = 96
-- Replacing --
Hello World!
fib(32) = 2178309
add(32, 64) = -32 !!! voilĂ
--- grischka