private void EmitNativeMain(LLVMContextRef context) { LLVMValueRef shadowStackTop = Module.GetNamedGlobal("t_pShadowStackTop"); LLVMBuilderRef builder = context.CreateBuilder(); var mainSignature = LLVMTypeRef.CreateFunction(LLVMTypeRef.Int32, new LLVMTypeRef[] { LLVMTypeRef.Int32, LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0) }, false); var mainFunc = Module.AddFunction("__managed__Main", mainSignature); var mainEntryBlock = mainFunc.AppendBasicBlock("entry"); builder.PositionAtEnd(mainEntryBlock); LLVMValueRef managedMain = Module.GetNamedFunction("StartupCodeMain"); if (managedMain.Handle == IntPtr.Zero) { throw new Exception("Main not found"); } LLVMTypeRef reversePInvokeFrameType = LLVMTypeRef.CreateStruct(new LLVMTypeRef[] { LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0), LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0) }, false); LLVMValueRef reversePinvokeFrame = builder.BuildAlloca(reversePInvokeFrameType, "ReversePInvokeFrame"); LLVMValueRef RhpReversePInvoke2 = Module.GetNamedFunction("RhpReversePInvoke2"); if (RhpReversePInvoke2.Handle == IntPtr.Zero) { RhpReversePInvoke2 = Module.AddFunction("RhpReversePInvoke2", LLVMTypeRef.CreateFunction(LLVMTypeRef.Void, new LLVMTypeRef[] { LLVMTypeRef.CreatePointer(reversePInvokeFrameType, 0) }, false)); } builder.BuildCall(RhpReversePInvoke2, new LLVMValueRef[] { reversePinvokeFrame }, ""); var shadowStack = builder.BuildMalloc(LLVMTypeRef.CreateArray(LLVMTypeRef.Int8, 1000000), String.Empty); var castShadowStack = builder.BuildPointerCast(shadowStack, LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0), String.Empty); builder.BuildStore(castShadowStack, shadowStackTop); var shadowStackBottom = Module.AddGlobal(LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0), "t_pShadowStackBottom"); shadowStackBottom.Linkage = LLVMLinkage.LLVMExternalLinkage; shadowStackBottom.Initializer = LLVMValueRef.CreateConstPointerNull(LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0)); shadowStackBottom.ThreadLocalMode = LLVMThreadLocalMode.LLVMLocalDynamicTLSModel; builder.BuildStore(castShadowStack, shadowStackBottom); // Pass on main arguments LLVMValueRef argc = mainFunc.GetParam(0); LLVMValueRef argv = mainFunc.GetParam(1); LLVMValueRef mainReturn = builder.BuildCall(managedMain, new LLVMValueRef[] { castShadowStack, argc, argv, }, "returnValue"); builder.BuildRet(mainReturn); mainFunc.Linkage = LLVMLinkage.LLVMExternalLinkage; }
private void EmitReadyToRunHeaderCallback(LLVMContextRef context) { LLVMTypeRef intPtr = LLVMTypeRef.CreatePointer(LLVMTypeRef.Int32, 0); LLVMTypeRef intPtrPtr = LLVMTypeRef.CreatePointer(intPtr, 0); var callback = Module.AddFunction("RtRHeaderWrapper", LLVMTypeRef.CreateFunction(intPtrPtr, new LLVMTypeRef[0], false)); var builder = context.CreateBuilder(); var block = callback.AppendBasicBlock("Block"); builder.PositionAtEnd(block); LLVMValueRef rtrHeaderPtr = GetSymbolValuePointer(Module, _nodeFactory.ReadyToRunHeader, _nodeFactory.NameMangler, false); LLVMValueRef castRtrHeaderPtr = builder.BuildPointerCast(rtrHeaderPtr, intPtrPtr, "castRtrHeaderPtr"); builder.BuildRet(castRtrHeaderPtr); }