1 //===-- EmitFunctions.cpp - interface to insert instrumentation --*- C++ -*--=//
3 // This inserts a global constant table with function pointers all along
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Constants.h"
8 #include "llvm/DerivedTypes.h"
9 #include "llvm/Module.h"
10 #include "llvm/Pass.h"
13 struct EmitFunctionTable : public Pass {
17 RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
20 // Per Module pass for inserting function table
21 bool EmitFunctionTable::run(Module &M){
22 std::vector<const Type*> vType;
23 std::vector<Constant *> vConsts;
24 unsigned char counter = 0;
25 for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
26 if (!MI->isExternal()) {
27 vType.push_back(MI->getType());
28 vConsts.push_back(ConstantPointerRef::get(MI));
32 StructType *sttype = StructType::get(vType);
33 ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
35 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
36 GlobalValue::ExternalLinkage,
37 cstruct, "llvmFunctionTable");
38 M.getGlobalList().push_back(gb);
40 ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter);
41 GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true,
42 GlobalValue::ExternalLinkage,
43 cnst, "llvmFunctionCount");
44 M.getGlobalList().push_back(fnCount);
45 return true; // Always modifies program