> Date: Sat, 09 Feb 2013 22:59:59 +0000 > From: Henry Weller <hwell...@gmail.com> > To: tinycc-devel@nongnu.org > Subject: [Tinycc-devel] Patching symbols after tcc_relocate > Message-ID: <85pq09gkk0.wl%h.wel...@opencfd.co.uk> > 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. _______________________________________________ Tinycc-devel mailing list Tinycc-devel@nongnu.org https://lists.nongnu.org/mailman/listinfo/tinycc-devel