2002-07-17 02:58:08 +08: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-15 06:39:29 +08:00
|
|
|
#include "llvm/Pass.h"
|
2002-07-17 02:58:08 +08:00
|
|
|
|
2002-07-24 02:06:35 +08:00
|
|
|
namespace {
|
|
|
|
struct EmitFunctionTable : public Pass {
|
|
|
|
bool run(Module &M);
|
|
|
|
};
|
|
|
|
|
2002-07-27 05:12:46 +08:00
|
|
|
RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
|
2002-07-24 02:06:35 +08:00
|
|
|
}
|
2002-07-17 02:58:08 +08:00
|
|
|
|
|
|
|
// Per Module pass for inserting function table
|
|
|
|
bool EmitFunctionTable::run(Module &M){
|
2003-05-23 06:00:07 +08:00
|
|
|
std::vector<const Type*> vType;
|
|
|
|
std::vector<Constant *> vConsts;
|
2003-06-01 10:40:49 +08:00
|
|
|
unsigned char counter = 0;
|
2003-05-23 06:00:07 +08:00
|
|
|
for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
|
2002-07-17 02:58:08 +08:00
|
|
|
if (!MI->isExternal()) {
|
|
|
|
vType.push_back(MI->getType());
|
2003-05-23 06:00:07 +08:00
|
|
|
vConsts.push_back(ConstantPointerRef::get(MI));
|
2003-06-01 10:40:49 +08:00
|
|
|
counter++;
|
2002-07-17 02:58:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
StructType *sttype = StructType::get(vType);
|
|
|
|
ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
|
|
|
|
|
2003-04-17 04:28:45 +08:00
|
|
|
GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
|
|
|
|
GlobalValue::ExternalLinkage,
|
2002-07-17 02:58:08 +08:00
|
|
|
cstruct, "llvmFunctionTable");
|
|
|
|
M.getGlobalList().push_back(gb);
|
2003-06-01 10:40:49 +08:00
|
|
|
|
2003-06-05 04:08:47 +08:00
|
|
|
ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter);
|
2003-06-01 10:40:49 +08:00
|
|
|
GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true,
|
|
|
|
GlobalValue::ExternalLinkage,
|
|
|
|
cnst, "llvmFunctionCount");
|
|
|
|
M.getGlobalList().push_back(fnCount);
|
2002-07-17 02:58:08 +08:00
|
|
|
return true; // Always modifies program
|
|
|
|
}
|