2002-07-16 20:58:08 +02:00
|
|
|
//===-- EmitFunctions.cpp - interface to insert instrumentation --*- C++ -*--=//
|
|
|
|
//
|
|
|
|
// This inserts a global constant table with function pointers all along
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
2003-01-14 23:39:29 +01:00
|
|
|
#include "llvm/Pass.h"
|
2002-07-16 20:58:08 +02:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
2002-07-23 20:06:35 +02:00
|
|
|
namespace {
|
|
|
|
struct EmitFunctionTable : public Pass {
|
|
|
|
bool run(Module &M);
|
|
|
|
};
|
|
|
|
|
2002-07-26 23:12:46 +02:00
|
|
|
RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
|
2002-07-23 20:06:35 +02:00
|
|
|
}
|
2002-07-16 20:58:08 +02:00
|
|
|
|
|
|
|
// Per Module pass for inserting function table
|
|
|
|
bool EmitFunctionTable::run(Module &M){
|
|
|
|
vector<const Type*> vType;
|
|
|
|
vector<Constant *> vConsts;
|
|
|
|
for(Module::iterator MI = M.begin(), ME = M.end(); MI!=ME; ++MI)
|
|
|
|
if (!MI->isExternal()) {
|
|
|
|
ConstantPointerRef *CP = ConstantPointerRef::get(MI);
|
|
|
|
vType.push_back(MI->getType());
|
|
|
|
vConsts.push_back(CP);
|
|
|
|
}
|
|
|
|
|
|
|
|
StructType *sttype = StructType::get(vType);
|
|
|
|
ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
|
|
|
|
|
|
|
|
GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, false,
|
|
|
|
cstruct, "llvmFunctionTable");
|
|
|
|
M.getGlobalList().push_back(gb);
|
|
|
|
return true; // Always modifies program
|
|
|
|
}
|