Using Dyninst API, we can perform both static and dynamic binary instrumentation.
In static instrumentation, we can rewrite a binary file and inject an extra code (snippets) at a specific points.
I have tested the following code in order to patch a binary code. However, this code inject the snippets at the entery of the interested function, i.e. foo.
How can we inject a snippets at a specific address?
this is the code (
link to original code)
#include <stdio.h>
#include "BPatch.h"
#include "BPatch_addressSpace.h"
#include "BPatch_function.h"
#include "BPatch_binaryEdit.h"
#include "BPatch_point.h"
int main(int argc, const char *argv[]) {
BPatch bpatch;
BPatch_addressSpace *app = bpatch.openBinary("hello", true);
bool flag = false;
flag = app->loadLibrary("liblib.so");
BPatch_image* image = app->getImage();
std::vector<BPatch_function*> func;
image->findFunction("code_to_inject", func);
std::cout<<"\nFunction is: "<<func[0]->getName()<<std::endl;
std::vector<BPatch_snippet*> openArgs;
BPatch_funcCallExpr enter_call(*(func[0]), openArgs);
func.clear();
std::vector<BPatch_point *> *points;
image->findFunction("foo1", func);
points = func[0]->findPoint(BPatch_entry);
app->insertSnippet(enter_call, *points);
BPatch_binaryEdit *appBin = dynamic_cast<BPatch_binaryEdit *>(app);
appBin->writeFile("newbinary");
return 0;
}
What I have tried:
I think we should use BPatch_function and pass a specific address, but i field to comiple the modified code. Maybe we should use Dyninst::Address.
Thanks for helps