blob: 3506cb90566c447e434a5ee18af4b36794102fc8 [file] [log] [blame]
Anand Shuklae0b51422002-07-16 18:58:08 +00001//===-- EmitFunctions.cpp - interface to insert instrumentation --*- C++ -*--=//
2//
3// This inserts a global constant table with function pointers all along
4//
5//===----------------------------------------------------------------------===//
6
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"
12
13using std::vector;
14
Chris Lattnerf6293092002-07-23 18:06:35 +000015namespace {
16 struct EmitFunctionTable : public Pass {
17 bool run(Module &M);
18 };
19
20 RegisterPass<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
21}
Anand Shuklae0b51422002-07-16 18:58:08 +000022
23// Create a new pass to add function table
24//
25Pass *createEmitFunctionTablePass() {
26 return new EmitFunctionTable();
27}
28
29// Per Module pass for inserting function table
30bool 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);
38 }
39
40 StructType *sttype = StructType::get(vType);
41 ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
42
43 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, false,
44 cstruct, "llvmFunctionTable");
45 M.getGlobalList().push_back(gb);
46 return true; // Always modifies program
47}