tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] Patching symbols after tcc_relocate


From: Jared Maddox
Subject: Re: [Tinycc-devel] Patching symbols after tcc_relocate
Date: Sat, 9 Feb 2013 21:54:29 -0600

> Date: Sat, 09 Feb 2013 22:59:59 +0000
> From: Henry Weller <address@hidden>
> To: address@hidden
> Subject: [Tinycc-devel] Patching symbols after tcc_relocate
> Message-ID: <address@hidden>
> Content-Type: text/plain; charset=US-ASCII
>
> I am interested in using libtcc to implement a REPL for a statically typed
> language I am working.  Ideally I would like to be able to patch a symbol
> after
> initial relocation following the re-definition of a function for example
> e.g.
> .
> .
> .
>     TCCState *s = compile(my_program);
>
>     tcc_add_symbol(s, "add", add1Ptr);
>
>     if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
>         return 1;
>
>     tcc_add_symbol(s, "add", add2Ptr);
> .
> .
> .
>
> The first problem I hit was that tcc_add_symbol does not allow re-definition
> so
> I hacked add_elf_sym to allow it but the above still does not work although
>
>     TCCState *s = compile(my_program);
>
>     tcc_add_symbol(s, "add", add1Ptr);
>     tcc_add_symbol(s, "add", add2Ptr);
>
>     if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
>         return 1;
>
> does but isn't very useful.
>
> Another option is to use ELF-Hook:
>
> http://www.codeproject.com/Articles/70302/Redirecting-functions-in-shared-ELF-libraries
> https://github.com/shoumikhin/ELF-Hook
>
> which is possible if I use tcc to write out DLs, dlopen them and then patch
> them
> with ELF-Hook but it would be much cleaner to be able to manipulate the
> symbols
> directly after tcc_relocate.  Does anyone know if this is this already
> possible
> with libtcc or how difficult it would be to add?
>

I can think of one way that you can do it with TCC right now, but it
somewhat changes your goal. What you do is that the initial definition
of "add" is for a function that calls a function pointer. You then
change the target of the function pointer.

Frankly, I would just write the code to access the pointer directly
instead. It does the same thing, but without all of the potential
breakage. The issue is that you're trying to change a value that is
directly embedded in the instructions. This can be done on some
platforms, but any platform that has protection for executable memory
is going to make it more complicated, and the possibility that part
(but not necessarily all!) of the value will be stored in the
processor's instruction cache means that any change that you CAN
propagate to the instruction memory might not make it to the
processor, or (even worse) might PARTIALLY make it to the processor,
sending your program haring off to some unpredictable piece of memory.

In short, write yourself some dispatcher & redirection code that uses
atomic reads/writes to access function pointers, instead of trying to
redirect at the TCC level. Doing this at the C-level is far wiser than
the path you're currently considering.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]