2002-07-16 18:58:08 +00: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 22:39:29 +00:00
|
|
|
#include "llvm/Pass.h"
|
2002-07-16 18:58:08 +00:00
|
|
|
|
2002-07-23 18:06:35 +00:00
|
|
|
namespace {
|
|
|
|
struct EmitFunctionTable : public Pass {
|
|
|
|
bool run(Module &M);
|
|
|
|
};
|
|
|
|
|
2002-07-26 21:12:46 +00:00
|
|
|
RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
|
2002-07-23 18:06:35 +00:00
|
|
|
}
|
2002-07-16 18:58:08 +00:00
|
|
|
|
|
|
|
// Per Module pass for inserting function table
|
|
|
|
bool EmitFunctionTable::run(Module &M){
|
2003-05-22 22:00:07 +00:00
|
|
|
std::vector<const Type*> vType;
|
|
|
|
std::vector<Constant *> vConsts;
|
|
|
|
for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
|
2002-07-16 18:58:08 +00:00
|
|
|
if (!MI->isExternal()) {
|
|
|
|
vType.push_back(MI->getType());
|
2003-05-22 22:00:07 +00:00
|
|
|
vConsts.push_back(ConstantPointerRef::get(MI));
|
2002-07-16 18:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StructType *sttype = StructType::get(vType);
|
|
|
|
ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
|
|
|
|
|
2003-04-16 20:28:45 +00:00
|
|
|
GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
|
|
|
|
GlobalValue::ExternalLinkage,
|
2002-07-16 18:58:08 +00:00
|
|
|
cstruct, "llvmFunctionTable");
|
|
|
|
M.getGlobalList().push_back(gb);
|
|
|
|
return true; // Always modifies program
|
|
|
|
}
|