Esempio n. 1
0
 private ModuleReference Fix(ModuleReference moduleRef)
 {
     ModuleReference nmr = repack.TargetAssemblyMainModule.ModuleReferences.First(x => x.Name == moduleRef.Name);
     if (nmr == null)
         throw new NullReferenceException("referenced module not found: \"" + moduleRef.Name + "\".");
     return nmr;
 }
		public MethodDefinition GetExternalMethod (MethodReference mr) {
			string library = GetSymbolLibrary (mr.Name);

			if (library != null) {
				List<MethodDefinition> methods = null;
				if (externalMethods.ContainsKey (mr.Name)) {
					methods = externalMethods [mr.Name];
					foreach (MethodDefinition method in methods) {
						if (CompareMethodSignatures (mr, method)) {
							return method;
						}
					}
				} else {
					methods = new List<MethodDefinition> ();
					externalMethods [mr.Name] = methods;
				}
				if (! referredLibraries.ContainsKey (library)) {
					referredLibraries [library] = new ModuleReference (library);
				}
				MethodDefinition md = CreateExternalMethod (mr, library, referredLibraries [library]);
				methods.Add (md);
				return md;
			} else {
				return null;
			}
		}
Esempio n. 3
0
        public static void EnsureWellName(ModuleReference r)
        {
            if (r.mWellName != null) return;

            r.mWellName = r.Name;
            if (!IGNORE_WELLNAMING)
            {
                TryFormatName("MDL_", ref r.mWellName, "");
            }
        }
		MethodDefinition CreateExternalMethod (MethodReference mr, string library, ModuleReference referredLibrary) {
			MethodDefinition md = new MethodDefinition(mr.Name,
					MethodAttributes.PInvokeImpl |  MethodAttributes.HideBySig | MethodAttributes.Private |
					MethodAttributes.Static, mr.ReturnType.ReturnType);
			foreach (ParameterDefinition pd in mr.Parameters) {
				md.Parameters.Add (pd);
			}
			md.PInvokeInfo = new PInvokeInfo(md,
					PInvokeAttributes.CharSetAnsi | PInvokeAttributes.CallConvCdecl, md.Name, referredLibrary);
			return md;

		}
        public virtual TypeDefinition Resolve(TypeReference type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            type = type.GetElementType();
            IMetadataScope scope = type.Scope;

            if (scope == null)
            {
                return(null);
            }
            switch (scope.MetadataScopeType)
            {
            case MetadataScopeType.AssemblyNameReference:
            {
                AssemblyDefinition assemblyDefinition = this.assembly_resolver.Resolve((AssemblyNameReference)scope);
                if (assemblyDefinition == null)
                {
                    return(null);
                }
                return(MetadataResolver.GetType(assemblyDefinition.MainModule, type));
            }

            case MetadataScopeType.ModuleReference:
            {
                Collection <ModuleDefinition> modules = type.Module.Assembly.Modules;
                ModuleReference moduleReference       = (ModuleReference)scope;
                for (int i = 0; i < modules.Count; i++)
                {
                    ModuleDefinition item = modules[i];
                    if (item.Name == moduleReference.Name)
                    {
                        return(MetadataResolver.GetType(item, type));
                    }
                }
                break;
            }

            case MetadataScopeType.ModuleDefinition:
            {
                return(MetadataResolver.GetType((ModuleDefinition)scope, type));
            }
            }
            throw new NotSupportedException();
        }
Esempio n. 6
0
        public override void VisitModuleReferenceCollection(ModuleReferenceCollection modules)
        {
            if (!m_tHeap.HasTable(ModuleRefTable.RId))
            {
                return;
            }

            ModuleRefTable mrTable = m_tableReader.GetModuleRefTable();

            for (int i = 0; i < mrTable.Rows.Count; i++)
            {
                ModuleRefRow    mrRow = mrTable [i];
                ModuleReference mod   = new ModuleReference(ReadString(mrRow.Name));
                mod.MetadataToken = MetadataToken.FromMetadataRow(TokenType.ModuleRef, i);
                modules.Add(mod);
            }
        }
Esempio n. 7
0
        public TypeDefinition Resolve(TypeReference type)
        {
            type = type.GetOriginalType();

            if (type is TypeDefinition)
            {
                return((TypeDefinition)type);
            }

            AssemblyNameReference reference = type.Scope as AssemblyNameReference;

            if (reference != null)
            {
                AssemblyDefinition assembly = AssemblyResolver.Resolve(reference);
                if (assembly == null)
                {
                    return(null);
                }

                return(Resolve(assembly.MainModule, type));
            }

            ModuleDefinition module = type.Scope as ModuleDefinition;

            if (module != null)
            {
                return(Resolve(module, type));
            }

            ModuleReference mod_reference = type.Scope as ModuleReference;

            if (mod_reference != null)
            {
                foreach (ModuleDefinition netmodule in type.Module.Assembly.Modules)
                {
                    if (netmodule.Name == mod_reference.Name)
                    {
                        return(Resolve(netmodule, type));
                    }
                }
            }

            throw new NotImplementedException();
        }
Esempio n. 8
0
 private static void AddGetProcAddress(TypeDefinition type)
 {
     var module = type.Module;
       MethodDefinition getProcAddressDef = new MethodDefinition("GetProcAddress", MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig, module.TypeSystem.IntPtr);
       getProcAddressDef.IsPInvokeImpl = true;
       var kernel32ref = module.ModuleReferences.FirstOrDefault(mr => mr.Name.Equals("kernel32.dll", StringComparison.OrdinalIgnoreCase));
       if (kernel32ref == null)
       {
     kernel32ref = new ModuleReference("kernel32.dll");
     module.ModuleReferences.Add(kernel32ref);
       }
       getProcAddressDef.IsPreserveSig = true;
       getProcAddressDef.PInvokeInfo = new PInvokeInfo(PInvokeAttributes.CallConvWinapi | PInvokeAttributes.NoMangle | PInvokeAttributes.CharSetAnsi |
     PInvokeAttributes.SupportsLastError, "GetProcAddress", kernel32ref);
       getProcAddressDef.IsIL = false;
       getProcAddressDef.Parameters.Add(new ParameterDefinition("hModule", ParameterAttributes.None, module.TypeSystem.IntPtr));
       getProcAddressDef.Parameters.Add(new ParameterDefinition("lpProcName", ParameterAttributes.None, module.TypeSystem.String));
       type.Methods.Add(getProcAddressDef);
 }
Esempio n. 9
0
        public virtual TypeDefinition Resolve(TypeReference type)
        {
            Mixin.CheckType(type);
            type = type.GetElementType();
            IMetadataScope scope = type.Scope;

            if (scope == null)
            {
                return(null);
            }
            switch (scope.MetadataScopeType)
            {
            case MetadataScopeType.AssemblyNameReference:
            {
                AssemblyDefinition assemblyDefinition = assembly_resolver.Resolve((AssemblyNameReference)scope);
                if (assemblyDefinition == null)
                {
                    return(null);
                }
                return(GetType(assemblyDefinition.MainModule, type));
            }

            case MetadataScopeType.ModuleDefinition:
                return(GetType((ModuleDefinition)scope, type));

            case MetadataScopeType.ModuleReference:
            {
                Collection <ModuleDefinition> modules = type.Module.Assembly.Modules;
                ModuleReference moduleReference       = (ModuleReference)scope;
                for (int i = 0; i < modules.Count; i++)
                {
                    ModuleDefinition moduleDefinition = modules[i];
                    if (moduleDefinition.Name == moduleReference.Name)
                    {
                        return(GetType(moduleDefinition, type));
                    }
                }
                break;
            }
            }
            throw new NotSupportedException();
        }
 public void Remove(ModuleReference value)
 {
     List.Remove(value);
 }
Esempio n. 11
0
 public DependencyGraph(
         ModuleReference module,
         IEqualityComparer<ModuleReference> moduleComparer)
     : base(module, moduleComparer)
 {
 }
Esempio n. 12
0
		public override void VisitModuleReference (ModuleReference module)
		{
			ModuleRefTable mrTable = m_tableWriter.GetModuleRefTable ();
			ModuleRefRow mrRow = m_rowWriter.CreateModuleRefRow (
				m_mdWriter.AddString (module.Name));

			mrTable.Rows.Add (mrRow);
		}
Esempio n. 13
0
        /// <summary>
        /// Checks if the operand calls a native library, via PInvoke.
        /// </summary>
        /// <param name="operand">The operand in question.</param>
        /// <returns>True if the operand is a PInvoke, otherwise false.</returns>
        static bool IsPInvoke(object operand, out ModuleReference nativeLib)
        {
            nativeLib = null;
            // try to cast operand to method definition and check for PInvoke
            var mdef = operand as MethodDefinition;
            if (mdef != null)
            {
                if (mdef.IsPInvokeImpl)
                {
                    logger.Debug("Is PInvoke? {0}", true);
                    if (mdef.PInvokeInfo != null)
                    {
                      nativeLib = mdef.PInvokeInfo.Module;
                      logger.Debug("Native library: {0}", nativeLib.Name);
                    }
                    return true;
                }
            }

            return false;
        }
Esempio n. 14
0
 public uint GetRidFor(ModuleReference modRef)
 {
     return (uint) m_mod.ModuleReferences.IndexOf (modRef) + 1;
 }
Esempio n. 15
0
		public void Remove (ModuleReference value)
		{
			List.Remove (value);
		}
Esempio n. 16
0
		public int IndexOf (ModuleReference value)
		{
			return List.IndexOf (value);
		}
Esempio n. 17
0
 public PInvokeInfo(PInvokeAttributes attributes, string entryPoint, ModuleReference module)
 {
     this.attributes  = (ushort)attributes;
     this.entry_point = entryPoint;
     this.module      = module;
 }
        public static Reflector.CodeModel.IModuleReference Cecil2Reflector(ModuleReference mr, ModuleDefinition md)
        {
            Reflector.CodeModel.IModule m = Cecil2Reflector(md);
            if (m == null) return null;

            string mrName = mr.Name;
            foreach (Reflector.CodeModel.IModuleReference mrTmp in m.ModuleReferences)
            {
                if (mrTmp.Name == mrName)
                    return mrTmp;
            }
            return null;
        }
Esempio n. 19
0
		public ModuleReferenceTreeNode(ModuleReference r)
		{
			if (r == null)
				throw new ArgumentNullException("r");
			this.r = r;
		}
 private static bool IsAny(ModuleReference module, params ModuleDefinition[] others)
 {
     bool result = module != null && others.Any(other => other.Name == module.Name);
     return result;
 }
 public virtual void VisitModuleReference(ModuleReference module)
 {
 }
        private MethodReference MakeCtorAndReference(ModuleDefinition targetModule, ModuleDefinition moduleWithAttributeType)
        {
            // optimization - trygettypereference does not work in case of in memory reference...
              //TypeReference existingAttributeReference;
              //if (targetModule.TryGetTypeReference (_attributeNamespace + "." + _attributeName, out existingAttributeReference))
              //  return (MethodReference) targetModule.GetMemberReferences ().Single (mr => mr.DeclaringType == existingAttributeReference);

              var attributeCtorDefinition = GetCustomAttributeCtor (moduleWithAttributeType);
              if (targetModule == moduleWithAttributeType)
            return attributeCtorDefinition;

              var moduleReference = new ModuleReference (moduleWithAttributeType.Name);

              if (!targetModule.ModuleReferences.Contains (moduleReference))
            targetModule.ModuleReferences.Add (moduleReference);

              var ctorReference = targetModule.Import (attributeCtorDefinition);

              return ctorReference;
        }
Esempio n. 23
0
        private ModuleReference ModuleReferenceFor(string name)
        {
            foreach (var reference in _module_definition.ModuleReferences)
                if (reference.Name == name)
                    return reference;

            var module = new ModuleReference(name);
            _module_definition.ModuleReferences.Add(module);
            return module;
        }
 public int IndexOf(ModuleReference value)
 {
     return(m_items.IndexOf(value));
 }
Esempio n. 25
0
		public bool Contains (ModuleReference value)
		{
			return List.Contains (value);
		}
 internal Module ResolveModule(ModuleReference reference)
 {
     return GetModule(reference.Name);
 }
Esempio n. 27
0
		public void Insert (int index, ModuleReference value)
		{
			List.Insert (index, value);
		}
Esempio n. 28
0
        public static ModuleReference addModuleReference(ModuleDefinition module, ModuleReference modRef)
        {
            foreach (var modModRef in module.ModuleReferences) {
                if (modModRef.Name == modRef.Name)
                    return modModRef;
            }

            var newModRef = new ModuleReference(modRef.Name);
            module.ModuleReferences.Add(newModRef);
            return newModRef;
        }
Esempio n. 29
0
        public ModuleReference convert(ModuleReference modRef)
        {
            foreach (var modModRef in module.ModuleReferences) {
                if (modModRef.Name == modRef.Name)
                    return modModRef;
            }

            var newModRef = new ModuleReference(modRef.Name);
            module.ModuleReferences.Add(newModRef);
            return newModRef;
        }
		public static string GetNetmoduleName(ModuleReference module)
		{
			if (module.Name.EndsWith(".netmodule"))
			{
				return module.Name.Substring(0, module.Name.Length - ".netmodule".Length);
			}
			else
			{
				return module.Name;
			}
		}
		public override void VisitModuleReferenceCollection (ModuleReferenceCollection modules)
		{
			if (!m_tHeap.HasTable (ModuleRefTable.RId))
				return;

			ModuleRefTable mrTable = m_tableReader.GetModuleRefTable ();
			for (int i = 0; i < mrTable.Rows.Count; i++) {
				ModuleRefRow mrRow = mrTable [i];
				ModuleReference mod = new ModuleReference (ReadString (mrRow.Name));
				mod.MetadataToken = MetadataToken.FromMetadataRow (TokenType.ModuleRef, i);
				modules.Add (mod);
			}
		}
Esempio n. 32
0
 protected virtual void ProcessModuleReference(ModuleReference moduleRef)
 {
 }
		public override void VisitModuleReference (ModuleReference module)
		{
			string name = module.Name;
			name = name.ToLower ();
			if (!BaseAssemblyResolver.OnMono ()) {
				if (!name.EndsWith (".dll"))
					name += ".dll";
			}

			if (!ModuleReferencesContains (target.MainModule.ModuleReferences, name)) {
				module.Name = name;
				target.MainModule.ModuleReferences.Add (module);
			}
		}
 public void Add(ModuleReference value)
 {
     List.Add(value);
 }
 public int IndexOf(ModuleReference value)
 {
     return(List.IndexOf(value));
 }
 public bool Contains(ModuleReference value)
 {
     return(m_items.Contains(value));
 }
 public bool Contains(ModuleReference value)
 {
     return(List.Contains(value));
 }
Esempio n. 38
0
        public static bool isReferenceToModule(ModuleReference moduleReference, IMetadataScope scope)
        {
            switch (scope.MetadataScopeType) {
            case MetadataScopeType.AssemblyNameReference:
                var asmRef = (AssemblyNameReference)scope;
                var module = moduleReference as ModuleDefinition;
                return module != null && module.Assembly != null && module.Assembly.Name.FullName == asmRef.FullName;

            case MetadataScopeType.ModuleDefinition:
                return moduleReference == scope;

            case MetadataScopeType.ModuleReference:
                return moduleReference.Name == ((ModuleReference)scope).Name;

            default:
                throw new ApplicationException("Unknown MetadataScopeType");
            }
        }
 public void Insert(int index, ModuleReference value)
 {
     List.Insert(index, value);
 }
Esempio n. 40
0
		public void Add (ModuleReference value)
		{
			List.Add (value);
		}
		ModuleReference GetModuleReference (ModuleReferenceCollection members, ModuleReference module)
		{
			string name = module.Name;
			name = name.ToLower ();
			if (!BaseAssemblyResolver.OnMono ()) {
				if (!name.EndsWith (".dll"))
					name += ".dll";
			}

			foreach (ModuleReference mr in members) {
				if (mr.Name == name)
					return mr;
			}
			return null;
		}
Esempio n. 42
0
 public ModuleReferenceEventArgs(ModuleReference item)
 {
     m_item = item;
 }
		public virtual void VisitModuleReference (ModuleReference module)
		{
		}
 public void Remove(ModuleReference value)
 {
     if (OnModuleReferenceRemoved != null && this.Contains (value))
         OnModuleReferenceRemoved (this, new ModuleReferenceEventArgs (value));
     m_items.Remove (value);
 }