views:

52

answers:

3

Let's say you have a function foo() compiled into a program that is running on Unix.

While the program is running, can one "replace" the function foo by dynamically loading an object file containining a modified version of foo()?

On an embedded system I worked on in the past, we could unprotect the text segment and then essentially "patch" the address of foo() to point to the newly modified foo().

It was used for debugging on occasion and with lots of special constraints, on customer sites.

Is this possible on Unix?

+1  A: 

It depends on the environment, I suppose. I know that hot-swapping production code is trivial in Erlang modules and not too difficult in Ruby. C might be a different animal.

Eli
+1 for mentioning Erlang's built-in hot swap
Pestilence
A: 

The short of it is yes, of course it's possible. The question should really be, "how difficult?"

You can load & unload shared libraries (.so & .DLL) all you want on Linux & Windows. Specific variants of UNIX, I'm not sure about. This would be the easiest way to achieve your goal.

If you don't mind getting your hands dirty, you can always patch up the code segment to jump to someplace else out on the heap. I don't recommend it.

Pestilence
+1  A: 

Yes. That's how debuggers like gdb work, after all.

Ross Patterson
specifically via the ptrace() syscall, AFAIK.
asveikau