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
|
|
|
|
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){
|
2003-05-23 00:00:07 +02:00
|
|
|
std::vector<const Type*> vType;
|
|
|
|
std::vector<Constant *> vConsts;
|
2003-06-01 04:40:49 +02:00
|
|
|
unsigned char counter = 0;
|
2003-05-23 00:00:07 +02:00
|
|
|
for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
|
2002-07-16 20:58:08 +02:00
|
|
|
if (!MI->isExternal()) {
|
|
|
|
vType.push_back(MI->getType());
|
2003-05-23 00:00:07 +02:00
|
|
|
vConsts.push_back(ConstantPointerRef::get(MI));
|
2003-06-01 04:40:49 +02:00
|
|
|
counter++;
|
2002-07-16 20:58:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StructType *sttype = StructType::get(vType);
|
|
|
|
ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
|
|
|
|
|
2003-04-16 22:28:45 +02:00
|
|
|
GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
|
|
|
|
GlobalValue::ExternalLinkage,
|
2002-07-16 20:58:08 +02:00
|
|
|
cstruct, "llvmFunctionTable");
|
|
|
|
M.getGlobalList().push_back(gb);
|
2003-06-01 04:40:49 +02:00
|
|
|
|
2003-06-04 22:08:47 +02:00
|
|
|
ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter);
|
2003-06-01 04:40:49 +02:00
|
|
|
GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true,
|
|
|
|
GlobalValue::ExternalLinkage,
|
|
|
|
cnst, "llvmFunctionCount");
|
|
|
|
M.getGlobalList().push_back(fnCount);
|
2002-07-16 20:58:08 +02:00
|
|
|
return true; // Always modifies program
|
|
|
|
}
|