How to Patch Live Running Unix Code

193 views Asked by At

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?

3

There are 3 answers

0
Eli On

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.

0
pestilence669 On

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.

1
Ross Patterson On

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