public static byte[] BuildSignatureForMethodDefinedInModule(MethodDesc method, NodeFactory factory) { EcmaMethod typicalMethod = (EcmaMethod)method.GetTypicalMethodDefinition(); ModuleToken moduleToken; if (factory.CompilationModuleGroup.VersionsWithMethodBody(typicalMethod)) { moduleToken = new ModuleToken(typicalMethod.Module, typicalMethod.Handle); } else { MutableModule manifestMetadata = factory.ManifestMetadataTable._mutableModule; var handle = manifestMetadata.TryGetExistingEntityHandle(method.GetTypicalMethodDefinition()); Debug.Assert(handle.HasValue); moduleToken = new ModuleToken(factory.ManifestMetadataTable._mutableModule, handle.Value); } ArraySignatureBuilder signatureBuilder = new ArraySignatureBuilder(); signatureBuilder.EmitMethodSignature( new MethodWithToken(method, moduleToken, constrainedType: null, unboxing: false, context: null), enforceDefEncoding: true, enforceOwningType: moduleToken.Module is EcmaModule ? factory.CompilationModuleGroup.EnforceOwningType((EcmaModule)moduleToken.Module) : true, factory.SignatureContext, isInstantiatingStub: false); return(signatureBuilder.ToArray()); }
public void InitManifestMutableModule(MutableModule mutableModule) { _manifestMutableModule = mutableModule; }
public ManifestMetadataTableNode(NodeFactory nodeFactory) : base(nodeFactory.Target) { _assemblyRefToModuleIdMap = new Dictionary <string, int>(); _moduleIdToAssemblyNameMap = new Dictionary <int, AssemblyName>(); _manifestAssemblyMvids = new List <Guid>(); _signatureEmitters = new List <ISignatureEmitter>(); _nodeFactory = nodeFactory; _nextModuleId = 2; AssemblyHashAlgorithm hashAlgorithm = AssemblyHashAlgorithm.None; byte[] publicKeyBlob = null; AssemblyFlags manifestAssemblyFlags = default(AssemblyFlags); Version manifestAssemblyVersion = new Version(0, 0, 0, 0); if ((nodeFactory.CompositeImageSettings != null) && nodeFactory.CompilationModuleGroup.IsCompositeBuildMode) { if (nodeFactory.CompositeImageSettings.PublicKey != null) { hashAlgorithm = AssemblyHashAlgorithm.Sha1; publicKeyBlob = nodeFactory.CompositeImageSettings.PublicKey.ToArray(); manifestAssemblyFlags |= AssemblyFlags.PublicKey; } if (nodeFactory.CompositeImageSettings.AssemblyVersion != null) { manifestAssemblyVersion = nodeFactory.CompositeImageSettings.AssemblyVersion; } } _mutableModule = new MutableModule(nodeFactory.TypeSystemContext, "ManifestMetadata", manifestAssemblyFlags, publicKeyBlob, manifestAssemblyVersion, hashAlgorithm, ModuleToIndexSingleThreadedAndSorted, nodeFactory.CompilationModuleGroup); if (!_nodeFactory.CompilationModuleGroup.IsCompositeBuildMode) { MetadataReader mdReader = _nodeFactory.CompilationModuleGroup.CompilationModuleSet.Single().MetadataReader; _assemblyRefCount = mdReader.GetTableRowCount(TableIndex.AssemblyRef); if (!_nodeFactory.CompilationModuleGroup.IsInputBubble) { for (int assemblyRefIndex = 1; assemblyRefIndex < _assemblyRefCount; assemblyRefIndex++) { AssemblyReferenceHandle assemblyRefHandle = MetadataTokens.AssemblyReferenceHandle(assemblyRefIndex); AssemblyReference assemblyRef = mdReader.GetAssemblyReference(assemblyRefHandle); string assemblyName = mdReader.GetString(assemblyRef.Name); _assemblyRefToModuleIdMap[assemblyName] = assemblyRefIndex; } } // AssemblyRefCount + 1 corresponds to rid 0 in the manifest metadata which indicates to use the manifest metadata itself // AssemblyRefCount + 2 corresponds to ROWID 1 in the manifest metadata _nextModuleId += _assemblyRefCount; } if (_nodeFactory.CompilationModuleGroup.IsCompositeBuildMode) { // Fill in entries for all input modules right away to make sure they have parallel indices int nextExpectedId = 2; foreach (EcmaModule inputModule in _nodeFactory.CompilationModuleGroup.CompilationModuleSet) { int acquiredId = ModuleToIndexInternal(inputModule); if (acquiredId != nextExpectedId) { throw new InternalCompilerErrorException($"Manifest metadata consistency error - acquired ID {acquiredId}, expected {nextExpectedId}"); } nextExpectedId++; } } }
public bool Initialize(MutableModule mutableModule, EcmaMethodIL wrappedMethod) { bool failedToReplaceToken = false; try { Debug.Assert(mutableModule.ModuleThatIsCurrentlyTheSourceOfNewReferences == null); mutableModule.ModuleThatIsCurrentlyTheSourceOfNewReferences = ((EcmaMethod)wrappedMethod.OwningMethod).Module; var owningMethodHandle = mutableModule.TryGetEntityHandle(wrappedMethod.OwningMethod); if (!owningMethodHandle.HasValue) { return(false); } _mutableModule = mutableModule; _maxStack = wrappedMethod.MaxStack; _isInitLocals = wrappedMethod.IsInitLocals; _owningMethod = wrappedMethod.OwningMethod; _exceptionRegions = (ILExceptionRegion[])wrappedMethod.GetExceptionRegions().Clone(); _ilBytes = (byte[])wrappedMethod.GetILBytes().Clone(); _locals = (LocalVariableDefinition[])wrappedMethod.GetLocals(); for (int i = 0; i < _exceptionRegions.Length; i++) { var region = _exceptionRegions[i]; if (region.Kind == ILExceptionRegionKind.Catch) { var newHandle = _mutableModule.TryGetHandle((TypeSystemEntity)wrappedMethod.GetObject(region.ClassToken)); if (!newHandle.HasValue) { return(false); } _exceptionRegions[i] = new ILExceptionRegion(region.Kind, region.TryOffset, region.TryLength, region.HandlerOffset, region.HandlerLength, newHandle.Value, newHandle.Value); } } ILTokenReplacer.Replace(_ilBytes, GetMutableModuleToken); #if DEBUG Debug.Assert(ReadyToRunStandaloneMethodMetadata.Compute((EcmaMethod)_owningMethod) != null); #endif // DEBUG } finally { mutableModule.ModuleThatIsCurrentlyTheSourceOfNewReferences = null; } return(!failedToReplaceToken); int GetMutableModuleToken(int token) { object result = wrappedMethod.GetObject(token); int? newToken; if (result is string str) { newToken = mutableModule.TryGetStringHandle(str); } else { newToken = mutableModule.TryGetHandle((TypeSystemEntity)result); } if (!newToken.HasValue) { // Toekn replacement has failed. Do not attempt to use this IL. failedToReplaceToken = true; return(1); } return(newToken.Value); } }