Example #1
0
        public LinkContext(Pipeline pipeline, AssemblyResolver resolver, ReaderParameters readerParameters, UnintializedContextFactory factory)
        {
            _pipeline         = pipeline;
            _resolver         = resolver;
            _resolver.Context = this;
            _actions          = new Dictionary <string, AssemblyAction> ();
            _parameters       = new Dictionary <string, string> ();
            _readerParameters = readerParameters;

            SymbolReaderProvider = new DefaultSymbolReaderProvider(false);

            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            _annotations              = factory.CreateAnnotationStore(this);
            MarkingHelpers            = factory.CreateMarkingHelpers(this);
            Tracer                    = factory.CreateTracer(this);
            ReflectionPatternRecorder = new LoggingReflectionPatternRecorder(this);
            MarkedKnownMembers        = new KnownMembers();
            StripResources            = true;

            // See https://github.com/mono/linker/issues/612
            DisabledOptimizations |= CodeOptimizations.UnreachableBodies;
            DisabledOptimizations |= CodeOptimizations.ClearInitLocals;
            DisabledOptimizations |= CodeOptimizations.IPConstantPropagation;
        }
Example #2
0
        public LinkContext(Pipeline pipeline, AssemblyResolver resolver, ReaderParameters readerParameters, UnintializedContextFactory factory)
        {
            _pipeline         = pipeline;
            _resolver         = resolver;
            _resolver.Context = this;
            _actions          = new Dictionary <string, AssemblyAction> ();
            _parameters       = new Dictionary <string, string> (StringComparer.Ordinal);
            _readerParameters = readerParameters;

            SymbolReaderProvider = new DefaultSymbolReaderProvider(false);

            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            _annotations              = factory.CreateAnnotationStore(this);
            MarkingHelpers            = factory.CreateMarkingHelpers(this);
            Tracer                    = factory.CreateTracer(this);
            ReflectionPatternRecorder = new LoggingReflectionPatternRecorder(this);
            MarkedKnownMembers        = new KnownMembers();
            StripResources            = true;
            PInvokes                  = new List <PInvokeInfo> ();

            // See https://github.com/mono/linker/issues/612
            const CodeOptimizations defaultOptimizations =
                CodeOptimizations.BeforeFieldInit |
                CodeOptimizations.OverrideRemoval |
                CodeOptimizations.UnusedInterfaces |
                CodeOptimizations.IPConstantPropagation;

            Optimizations = new CodeOptimizationsSettings(defaultOptimizations);
        }
Example #3
0
        public static void SaveAssembly(AssemblyDefinition assembly, string destination)
        {
            var  main    = assembly.MainModule;
            bool symbols = main.HasSymbols;

            if (symbols)
            {
                var provider = new DefaultSymbolReaderProvider();
                main.ReadSymbols(provider.GetSymbolReader(main, main.FileName));
            }

            var wp = new WriterParameters()
            {
                WriteSymbols = symbols
            };

            // re-write symbols, if available, so the new tokens will match
            assembly.Write(destination, wp);

            if (!symbols)
            {
                // if we're not saving the symbols then we must not leave stale/old files to be used by other tools
                string dest_mdb = destination + ".mdb";
                if (File.Exists(dest_mdb))
                {
                    File.Delete(dest_mdb);
                }
                string dest_pdb = Path.ChangeExtension(destination, ".pdb");
                if (File.Exists(dest_pdb))
                {
                    File.Delete(dest_pdb);
                }
            }
        }
 private void LoadSourceAssembly()
 {
     try
     {
         try
         {
             _sourceAssembly = AssemblyDefinition.ReadAssembly(ModulePath);
             if (_sourceAssembly != null)
             {
                 var symbolReader = new DefaultSymbolReaderProvider(true)
                                    .GetSymbolReader(_sourceAssembly.MainModule, _sourceAssembly.MainModule.FileName);
                 _sourceAssembly.MainModule.ReadSymbols(symbolReader);
             }
         }
         catch (FileNotFoundException)
         {
             _sourceAssembly = null;
             SearchForSymbolsAndLoad();
         }
     }
     catch (Exception)
     {
         // failure to here is quite normal for DLL's with no, or incompatible, PDBs => no instrumentation
         _sourceAssembly = null;
     }
 }
Example #5
0
		public void ReadSymbols ()
		{
			if (string.IsNullOrEmpty (file_name))
				throw new InvalidOperationException ();

			var provider = new DefaultSymbolReaderProvider (throwIfNoSymbol: true);
			ReadSymbols (provider.GetSymbolReader (this, file_name), throwIfSymbolsAreNotMaching: true);
		}
Example #6
0
        public void ReadSymbols()
        {
            if (string.IsNullOrEmpty(file_name))
            {
                throw new InvalidOperationException();
            }
            DefaultSymbolReaderProvider defaultSymbolReaderProvider = new DefaultSymbolReaderProvider(true);

            ReadSymbols(defaultSymbolReaderProvider.GetSymbolReader(this, file_name), true);
        }
        private AssemblyDefinition SearchForSymbolsAndLoad()
        {
            AssemblyDefinition sourceAssembly = null;
            var provider = new DefaultSymbolReaderProvider(true);

            try
            {
                sourceAssembly = AssemblyDefinition.ReadAssembly(ModulePath);
                if (sourceAssembly != null)
                {
                    var symbolReader = provider
                                       .GetSymbolReader(sourceAssembly.MainModule, sourceAssembly.MainModule.FileName);
                    if (symbolReader != null)
                    {
                        sourceAssembly.MainModule.ReadSymbols(symbolReader);
                        if (sourceAssembly.MainModule.HasSymbols)
                        {
                            return(sourceAssembly);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }

            foreach (var symbolFile in _symbolFileHelper.GetSymbolFileLocations(ModulePath, _commandLine))
            {
                try
                {
                    using (var stream = System.IO.File.Open(symbolFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        var parameters = new ReaderParameters
                        {
                            SymbolReaderProvider         = provider,
                            ReadingMode                  = ReadingMode.Immediate,
                            ReadSymbols                  = true,
                            SymbolStream                 = stream,
                            ThrowIfSymbolsAreNotMatching = true
                        };
                        sourceAssembly = AssemblyDefinition.ReadAssembly(ModulePath, parameters);
                        if (sourceAssembly.MainModule.HasSymbols)
                        {
                            return(sourceAssembly);
                        }
                    }
                }
                catch (Exception)
                {
                }
            }

            return(null);
        }
Example #8
0
        public LinkContext(Pipeline pipeline, ILogger logger)
        {
            _pipeline = pipeline;
            _logger   = logger ?? throw new ArgumentNullException(nameof(logger));

            _resolver = new AssemblyResolver()
            {
                Context = this
            };
            _typeNameResolver = new TypeNameResolver(this);
            _actions          = new Dictionary <string, AssemblyAction> ();
            _parameters       = new Dictionary <string, string> (StringComparer.Ordinal);
            _readerParameters = new ReaderParameters {
                AssemblyResolver = _resolver
            };
            _customAttributes = new CustomAttributeSource(this);
            _cachedWarningMessageContainers = new List <MessageContainer> ();

            SymbolReaderProvider = new DefaultSymbolReaderProvider(false);

            var factory = new UnintializedContextFactory();

            _annotations              = factory.CreateAnnotationStore(this);
            MarkingHelpers            = factory.CreateMarkingHelpers(this);
            Tracer                    = factory.CreateTracer(this);
            ReflectionPatternRecorder = new LoggingReflectionPatternRecorder(this);
            MarkedKnownMembers        = new KnownMembers();
            PInvokes                  = new List <PInvokeInfo> ();
            Suppressions              = new UnconditionalSuppressMessageAttributeState(this);
            NoWarn                    = new HashSet <int> ();
            GeneralWarnAsError        = false;
            WarnAsError               = new Dictionary <int, bool> ();
            WarnVersion               = WarnVersion.Latest;

            const CodeOptimizations defaultOptimizations =
                CodeOptimizations.BeforeFieldInit |
                CodeOptimizations.OverrideRemoval |
                CodeOptimizations.UnusedInterfaces |
                CodeOptimizations.UnusedTypeChecks |
                CodeOptimizations.IPConstantPropagation |
                CodeOptimizations.UnreachableBodies |
                CodeOptimizations.RemoveDescriptors |
                CodeOptimizations.RemoveLinkAttributes |
                CodeOptimizations.RemoveSubstitutions |
                CodeOptimizations.RemoveDynamicDependencyAttribute;

            Optimizations = new CodeOptimizationsSettings(defaultOptimizations);
        }
Example #9
0
        public LinkContext(Pipeline pipeline, AssemblyResolver resolver, ReaderParameters readerParameters, UnintializedContextFactory factory)
        {
            _pipeline         = pipeline;
            _resolver         = resolver;
            _resolver.Context = this;
            _typeNameResolver = new TypeNameResolver(this);
            _actions          = new Dictionary <string, AssemblyAction> ();
            _parameters       = new Dictionary <string, string> (StringComparer.Ordinal);
            _readerParameters = readerParameters;
            _customAttributes = new CustomAttributeSource();
            _cachedWarningMessageContainers = new List <MessageContainer> ();

            SymbolReaderProvider = new DefaultSymbolReaderProvider(false);
            Logger = new ConsoleLogger();

            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            _annotations              = factory.CreateAnnotationStore(this);
            MarkingHelpers            = factory.CreateMarkingHelpers(this);
            Tracer                    = factory.CreateTracer(this);
            ReflectionPatternRecorder = new LoggingReflectionPatternRecorder(this);
            MarkedKnownMembers        = new KnownMembers();
            StripDescriptors          = true;
            StripSubstitutions        = true;
            StripLinkAttributes       = true;
            PInvokes                  = new List <PInvokeInfo> ();
            Suppressions              = new UnconditionalSuppressMessageAttributeState(this);
            NoWarn                    = new HashSet <int> ();
            GeneralWarnAsError        = false;
            WarnAsError               = new Dictionary <int, bool> ();
            WarnVersion               = WarnVersion.Latest;

            // See https://github.com/mono/linker/issues/612
            const CodeOptimizations defaultOptimizations =
                CodeOptimizations.BeforeFieldInit |
                CodeOptimizations.OverrideRemoval |
                CodeOptimizations.UnusedInterfaces |
                CodeOptimizations.UnusedTypeChecks |
                CodeOptimizations.IPConstantPropagation;

            Optimizations = new CodeOptimizationsSettings(defaultOptimizations);
        }
Example #10
0
        public LinkContext(Pipeline pipeline, AssemblyResolver resolver, ReaderParameters readerParameters, UnintializedContextFactory factory)
        {
            _pipeline         = pipeline;
            _resolver         = resolver;
            _resolver.Context = this;
            _actions          = new Dictionary <string, AssemblyAction> ();
            _parameters       = new Dictionary <string, string> ();
            _readerParameters = readerParameters;

            SymbolReaderProvider = new DefaultSymbolReaderProvider(false);

            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            _annotations   = factory.CreateAnnotationStore(this);
            MarkingHelpers = factory.CreateMarkingHelpers(this);
            Tracer         = factory.CreateTracer(this);
        }
Example #11
0
        private static void ReadSymbols(ModuleDefinition module, ReaderParameters parameters)
        {
            ISymbolReaderProvider symbolReaderProvider = parameters.SymbolReaderProvider;

            if (symbolReaderProvider == null && parameters.ReadSymbols)
            {
                symbolReaderProvider = new DefaultSymbolReaderProvider();
            }
            if (symbolReaderProvider != null)
            {
                module.SymbolReaderProvider = symbolReaderProvider;
                ISymbolReader symbolReader = (parameters.SymbolStream != null) ? symbolReaderProvider.GetSymbolReader(module, parameters.SymbolStream) : symbolReaderProvider.GetSymbolReader(module, module.FileName);
                if (symbolReader != null)
                {
                    module.ReadSymbols(symbolReader, parameters.ThrowIfSymbolsAreNotMatching);
                }
            }
            if (module.Image.HasDebugTables())
            {
                module.ReadSymbols(new PortablePdbReader(module.Image, module));
            }
        }
Example #12
0
        // Token: 0x0600003E RID: 62 RVA: 0x00002D90 File Offset: 0x00000F90
        private bool IsMyCode(string path)
        {
            string text = Path.ChangeExtension(path, "pdb");

            if (File.Exists(text))
            {
                if (File.Exists(path + ".mdb"))
                {
                    MonoSymbolFile monoSymbolFile = MonoSymbolFile.ReadSymbolFile(path + ".mdb");
                    return(File.Exists(monoSymbolFile.Sources.First <SourceFileEntry>().FileName));
                }
                if (StartInfo.IsPortablePdb(text))
                {
                    ModuleDefinition            moduleDefinition            = ModuleDefinition.ReadModule(path);
                    DefaultSymbolReaderProvider defaultSymbolReaderProvider = new DefaultSymbolReaderProvider(false);
                    moduleDefinition.ReadSymbols(defaultSymbolReaderProvider.GetSymbolReader(moduleDefinition, text));
                    return(File.Exists((from t in moduleDefinition.Types
                                        where t.HasMethods
                                        select t).First <TypeDefinition>().Methods.First <MethodDefinition>().DebugInformation.SequencePoints.First <SequencePoint>().Document.Url));
                }
            }
            return(false);
        }
Example #13
0
 public SymbolReaderProvider()
 {
     inner = new DefaultSymbolReaderProvider(false);
 }