1 //===-- EmitFunctions.cpp - interface to insert instrumentation --*- C++ -*--=//
3 // This inserts a global constant table with function pointers all along
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Transforms/Instrumentation/EmitFunctions.h"
8 #include "llvm/Constants.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/Constants.h"
11 #include "llvm/Module.h"
15 struct EmitFunctionTable : public Pass {
16 const char *getPassName() const { return "EmitFunctionTablePass"; }
21 // Create a new pass to add function table
23 Pass *createEmitFunctionTablePass() {
24 return new EmitFunctionTable();
27 // Per Module pass for inserting function table
28 bool EmitFunctionTable::run(Module &M){
29 vector<const Type*> vType;
30 vector<Constant *> vConsts;
31 for(Module::iterator MI = M.begin(), ME = M.end(); MI!=ME; ++MI)
32 if (!MI->isExternal()) {
33 ConstantPointerRef *CP = ConstantPointerRef::get(MI);
34 vType.push_back(MI->getType());
35 vConsts.push_back(CP);
38 StructType *sttype = StructType::get(vType);
39 ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
41 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, false,
42 cstruct, "llvmFunctionTable");
43 M.getGlobalList().push_back(gb);
44 return true; // Always modifies program