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"
16 struct EmitFunctionTable : public Pass {
20 RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
23 // Create a new pass to add function table
25 Pass *createEmitFunctionTablePass() {
26 return new EmitFunctionTable();
29 // Per Module pass for inserting function table
30 bool EmitFunctionTable::run(Module &M){
31 vector<const Type*> vType;
32 vector<Constant *> vConsts;
33 for(Module::iterator MI = M.begin(), ME = M.end(); MI!=ME; ++MI)
34 if (!MI->isExternal()) {
35 ConstantPointerRef *CP = ConstantPointerRef::get(MI);
36 vType.push_back(MI->getType());
37 vConsts.push_back(CP);
40 StructType *sttype = StructType::get(vType);
41 ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
43 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, false,
44 cstruct, "llvmFunctionTable");
45 M.getGlobalList().push_back(gb);
46 return true; // Always modifies program