In the first half of this year, we were working on a carrier project. The device specification was written by large manufacturers such as, which had requirements for hot Patching. The operator has high requirements on the running time of the device, so it does not need to restart the program to change a small problem. So there is a hot Patching requirement: requires that the program can change the behavior of the program during the running process.
There is not much information about the implementation of hot Patching in Linux, I only find one: http://www.cnblogs.com/WuCountry/archive/2010/02/22/1671537.html
There is a general implementation here for reference, but some problems need to be solved. I will write several articles, record the development process, and make a summary.
First, we need:
1. You must be able to change the behavior of the program while the program is running.
When the program is running, it is loaded into the memory. To change the program behavior, you must change the memory of the process. Here I use the ptrace function.
2. How to dynamically add new program behaviors to the process memory.
The new program is loaded to the memory in the form of a dynamic link library. There are two loading methods: one is to open up a piece of memory in the process, and then load the Dynamic Linked Library into the memory; instead, use the system function dlopen to load the Dynamic Linked Library into the program process.
3. How to replace the behavior of a function with a new behavior.
Use the BFD library to find the function address to be replaced and the new function address, and then change the original function start memory to the new function address of the JMP command.
4. Can I change the static function in this way?
5. How do new functions call other functions in the original process? How to access the original global variables? How do I access static functions and static global variables?
These details will be described later.