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