1*3e457fe3SDavid van Moolenbroek #ifndef MAGIC_UTIL_H 2*3e457fe3SDavid van Moolenbroek #define MAGIC_UTIL_H 3*3e457fe3SDavid van Moolenbroek 4*3e457fe3SDavid van Moolenbroek #include <magic/magic.h> 5*3e457fe3SDavid van Moolenbroek #include <magic/support/SmartType.h> 6*3e457fe3SDavid van Moolenbroek #include <cxxabi.h> 7*3e457fe3SDavid van Moolenbroek 8*3e457fe3SDavid van Moolenbroek using namespace llvm; 9*3e457fe3SDavid van Moolenbroek 10*3e457fe3SDavid van Moolenbroek namespace llvm { 11*3e457fe3SDavid van Moolenbroek 12*3e457fe3SDavid van Moolenbroek #define magicUtilLog(M) DEBUG(dbgs() << "MagicUtil: " << M << "\n") 13*3e457fe3SDavid van Moolenbroek 14*3e457fe3SDavid van Moolenbroek class MagicUtil { 15*3e457fe3SDavid van Moolenbroek public: 16*3e457fe3SDavid van Moolenbroek static StringRef getGVSourceName(Module &M, GlobalVariable *GV, DIGlobalVariable **DIGVP=NULL, const std::string &baseDir=""); 17*3e457fe3SDavid van Moolenbroek static StringRef getLVSourceName(Module &M, AllocaInst *V, DIVariable **DIVP=NULL); 18*3e457fe3SDavid van Moolenbroek static StringRef getFunctionSourceName(Module &M, Function *F, DISubprogram **DISP=NULL, const std::string &baseDir=""); 19*3e457fe3SDavid van Moolenbroek static void putStringRefCache(Module &M, const std::string &str, GlobalVariable *GV); 20*3e457fe3SDavid van Moolenbroek static Constant* getGetElementPtrConstant(Constant *constant, std::vector<Value*> &indexes); 21*3e457fe3SDavid van Moolenbroek static GetElementPtrInst* createGetElementPtrInstruction(Value *ptr, std::vector<Value*> &indexes, const Twine &NameStr="", Instruction *InsertBefore=0); 22*3e457fe3SDavid van Moolenbroek static GetElementPtrInst* createGetElementPtrInstruction(Value *ptr, std::vector<Value*> &indexes, const Twine &NameStr="", BasicBlock *InsertAtEnd=0); 23*3e457fe3SDavid van Moolenbroek static CallInst* createCallInstruction(Value *F, std::vector<Value*> &args, const Twine &NameStr="", Instruction *InsertBefore=0); 24*3e457fe3SDavid van Moolenbroek static CallInst* createCallInstruction(Value *F, std::vector<Value*> &args, const Twine &NameStr="", BasicBlock *InsertAtEnd=0); 25*3e457fe3SDavid van Moolenbroek static Function* getIntrinsicFunction(Module &M, Intrinsic::ID id, TYPECONST Type** types=NULL, unsigned size=0); 26*3e457fe3SDavid van Moolenbroek static GlobalVariable *getStringRef(Module &M, const std::string &str); 27*3e457fe3SDavid van Moolenbroek static GlobalVariable *getIntArrayRef(Module &M, unsigned arrSize, std::vector<int> *arr, bool isConstant=true); 28*3e457fe3SDavid van Moolenbroek static GlobalVariable *getStringArrayRef(Module &M, unsigned arrSize, std::vector<std::string> *arr, bool isConstant=true); 29*3e457fe3SDavid van Moolenbroek static GlobalVariable *getGenericArrayRef(Module &M, std::vector<Constant*> &arrayElems, bool isConstant=true); 30*3e457fe3SDavid van Moolenbroek static GlobalVariable *getMagicTypePtrArrayRef(Module &M, Instruction *InsertBefore, std::vector<Value*> &globalTypeIndexes, GlobalVariable *magicTypeArray); 31*3e457fe3SDavid van Moolenbroek static GlobalVariable* getExportedIntGlobalVar(Module &M, std::string name, int value, bool isConstant=true); 32*3e457fe3SDavid van Moolenbroek static GlobalVariable* getShadowRef(Module &M, GlobalVariable *GV); 33*3e457fe3SDavid van Moolenbroek static Value* getMagicStructFieldPtr(Module &M, Instruction *InsertBefore, GlobalVariable* array, Value* arrayIndex, const std::string &structFieldName, std::string *structFieldNames); 34*3e457fe3SDavid van Moolenbroek static Value* getMagicSStructFieldPtr(Module &M, Instruction *InsertBefore, GlobalVariable* magicArray, Value* magicArrayIndex, const std::string &structFieldName); 35*3e457fe3SDavid van Moolenbroek static Value* getMagicTStructFieldPtr(Module &M, Instruction *InsertBefore, GlobalVariable* magicTypeArray, Value* magicTypeArrayIndex, const std::string &structFieldName); 36*3e457fe3SDavid van Moolenbroek static Value* getMagicFStructFieldPtr(Module &M, Instruction *InsertBefore, GlobalVariable* magicFunctionArray, Value* magicFunctionArrayIndex, const std::string &structFieldName); 37*3e457fe3SDavid van Moolenbroek static Value* getMagicRStructFieldPtr(Module &M, Instruction *InsertBefore, GlobalVariable* magicVar, const std::string &structFieldName); 38*3e457fe3SDavid van Moolenbroek static Value* getMagicDStructFieldPtr(Module &M, Instruction *InsertBefore, GlobalVariable* magicDsindexArray, Value* magicDsindexArrayIndex, const std::string &structFieldName); 39*3e457fe3SDavid van Moolenbroek static Constant* getArrayPtr(Module &M, GlobalVariable* array); 40*3e457fe3SDavid van Moolenbroek static void insertMemcpyInst(Module &M, Instruction *InsertBefore, Value *Dst, Value *Src, Value *Len, unsigned Align); 41*3e457fe3SDavid van Moolenbroek static void insertCopyInst(Module &M, Instruction *InsertBefore, GlobalVariable *GV, GlobalVariable *SGV, int GVSize, bool forceMemcpy); 42*3e457fe3SDavid van Moolenbroek static Function* getCalledFunctionFromCS(const CallSite &CS); 43*3e457fe3SDavid van Moolenbroek static void replaceCallInst(Instruction *originalInst, CallInst *newInst, int argOffset=0, bool removeUnusedFunction=true); 44*3e457fe3SDavid van Moolenbroek static std::vector<Function*> getGlobalVariablesShadowFunctions(Module &M, std::vector<GlobalVariable*> globalVariables, std::vector<GlobalVariable*> shadowGlobalVariables, std::vector<int> globalVariableSizes, GlobalVariable* magicArray, int magicArraySize, bool forceShadow, bool setDirtyFlag); 45*3e457fe3SDavid van Moolenbroek static Function* getGlobalVariableShadowFunction(Module &M, GlobalVariable* GV, GlobalVariable* SGV, int GVSize, GlobalVariable* magicArray, int magicArrayIndex, bool forceShadow, bool setDirtyFlag); 46*3e457fe3SDavid van Moolenbroek static void insertGlobalVariableCleanDirtyFlag(Module &M, GlobalVariable* GV, GlobalVariable* magicArray, int magicArrayIndex, Instruction *InsertBefore); 47*3e457fe3SDavid van Moolenbroek static void insertShadowTag(Module &M, GlobalVariable *GV, Instruction *InsertBefore); 48*3e457fe3SDavid van Moolenbroek static bool isShadowTag(Instruction *inst); 49*3e457fe3SDavid van Moolenbroek static GlobalVariable* getGlobalVariableFromShadowTag(Instruction *inst, std::vector<Instruction*> &instructionsToRemove); 50*3e457fe3SDavid van Moolenbroek static void cleanupShadowTag(Module &M, std::vector<Instruction*> &instructionsToRemove); 51*3e457fe3SDavid van Moolenbroek static bool hasAddressTaken(const GlobalValue *GV, bool includeMembers=true); 52*3e457fe3SDavid van Moolenbroek static bool lookupValueSet(const GlobalVariable *GV, std::vector<int> &valueSet); 53*3e457fe3SDavid van Moolenbroek static Value* getStringOwner(GlobalVariable *GV); 54*3e457fe3SDavid van Moolenbroek static Instruction* getFirstNonAllocaInst(Function* F, bool skipAllocaPoint=true); 55*3e457fe3SDavid van Moolenbroek static void setGlobalVariableSection(GlobalVariable *GV, const std::string §ion); 56*3e457fe3SDavid van Moolenbroek static bool getCallAnnotation(Module &M, const CallSite &CS, int *annotation); 57*3e457fe3SDavid van Moolenbroek static bool getVarAnnotation(Module &M, const GlobalVariable *GV, int *annotation); 58*3e457fe3SDavid van Moolenbroek static CallSite getCallSiteFromInstruction(Instruction *I); 59*3e457fe3SDavid van Moolenbroek static AllocaInst* getAllocaInstFromArgument(Argument *argument); 60*3e457fe3SDavid van Moolenbroek static Function* getMangledFunction(Module &M, StringRef functionName); 61*3e457fe3SDavid van Moolenbroek static Function* getFunction(Module &M, StringRef functionName); 62*3e457fe3SDavid van Moolenbroek static bool isCompatibleType(const Type* type1, const Type* type2); 63*3e457fe3SDavid van Moolenbroek static void inlinePreHookForwardingCall(Function* function, Function* preHookFunction, std::vector<unsigned> argsMapping, std::vector<Value*> trailingArgs); 64*3e457fe3SDavid van Moolenbroek static void inlinePostHookForwardingCall(Function* function, Function* postHookFunction, std::vector<unsigned> mapping, std::vector<Value*> trailingArgs); 65*3e457fe3SDavid van Moolenbroek static int getPointerIndirectionLevel(const Type* type); 66*3e457fe3SDavid van Moolenbroek static Value* getFunctionParam(Function* function, unsigned index); 67*3e457fe3SDavid van Moolenbroek static bool isLocalConstant(Module &M, GlobalVariable *GV); 68*3e457fe3SDavid van Moolenbroek }; 69*3e457fe3SDavid van Moolenbroek 70*3e457fe3SDavid van Moolenbroek } 71*3e457fe3SDavid van Moolenbroek 72*3e457fe3SDavid van Moolenbroek #endif 73