After tested under windows.
[dyninst.git] / dyninstAPI / src / Relocation / DynCFGMaker.C
1
2 #include "DynObject.h"
3 #include "dyninstAPI/src/mapped_module.h"
4 #include "dyninstAPI/src/mapped_object.h"
5 #include "dyninstAPI/src/parse-cfg.h"
6 #include "dyninstAPI/src/function.h"
7
8 using Dyninst::PatchAPI::DynCFGMaker;
9 using Dyninst::PatchAPI::PatchObject;
10 using Dyninst::ParseAPI::Function;
11 using Dyninst::PatchAPI::PatchFunction;
12 using Dyninst::ParseAPI::Block;
13 using Dyninst::PatchAPI::PatchBlock;
14 using Dyninst::ParseAPI::Edge;
15 using Dyninst::PatchAPI::PatchEdge;
16
17 PatchFunction* DynCFGMaker::makeFunction(Function* f,
18                                          PatchObject* obj) {
19   Address code_base = obj->codeBase();
20   mapped_object* mo = SCAST_MO(obj);
21   parse_func* img_func = SCAST_PF(f);
22   if (!img_func) return NULL;
23   assert(img_func->getSymtabFunction());
24   mapped_module* mod = mo->findModule(img_func->pdmod());
25   if (!mod) {
26     fprintf(stderr, "%s[%d]: ERROR: cannot find module %p\n", FILE__, __LINE__,
27             img_func->pdmod());
28     fprintf(stderr, "%s[%d]:  ERROR:  Cannot find module %s\n", FILE__, __LINE__,
29             img_func->pdmod()->fileName().c_str());
30   }
31   func_instance* fi = new func_instance(img_func, code_base, mod);
32   mo->addFunction(fi);
33   return fi;
34 }
35
36 PatchFunction* DynCFGMaker::copyFunction(PatchFunction* f, PatchObject* o) {
37   func_instance *parFunc = SCAST_FI(f);
38   mapped_object *mo = SCAST_MO(o);
39   assert(parFunc->mod());
40   mapped_module *mod = mo->getOrCreateForkedModule(parFunc->mod());
41   func_instance *newFunc = new func_instance(parFunc, mod);
42   mo->addFunction(newFunc);
43   return newFunc;
44 }
45
46 PatchBlock* DynCFGMaker::makeBlock(ParseAPI::Block* b, PatchObject* obj) {
47   block_instance *inst = new block_instance(b, SCAST_MO(obj));
48   obj->addBlock(inst);
49   return inst;
50 }
51
52 PatchBlock* DynCFGMaker::copyBlock(PatchBlock* b, PatchObject* o) {
53   block_instance *newBlock = new block_instance(SCAST_BI(b), SCAST_MO(o));
54   // o->addBlock(newBlock);
55   return  newBlock;
56 }
57
58 PatchEdge* DynCFGMaker::makeEdge(ParseAPI::Edge* e,
59                                  PatchBlock* s,
60                                  PatchBlock* t,
61                                  PatchObject* o) {
62   mapped_object* mo = SCAST_MO(o);
63   edge_instance *inst = new edge_instance(e,
64                          s ? SCAST_BI(s) : mo->findBlock(e->src()),
65                          t ? SCAST_BI(t) : mo->findBlock(e->trg()));
66   return inst;
67 }
68
69 PatchEdge* DynCFGMaker::copyEdge(PatchEdge* e, PatchObject* o) {
70   edge_instance *new_edge = new edge_instance(SCAST_EI(e), SCAST_MO(o));
71   return new_edge;
72 }