5 // Jb Evain (jbevain@gmail.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
36 namespace Mono.Linker {
38 public class LinkContext {
41 AssemblyAction _coreAction;
43 string _outputDirectory;
44 Hashtable _parameters;
47 AssemblyResolver _resolver;
49 ReaderParameters _readerParameters;
50 ISymbolReaderProvider _symbolReaderProvider;
51 ISymbolWriterProvider _symbolWriterProvider;
53 AnnotationStore _annotations;
55 public Pipeline Pipeline {
56 get { return _pipeline; }
59 public AnnotationStore Annotations {
60 get { return _annotations; }
63 public string OutputDirectory {
64 get { return _outputDirectory; }
65 set { _outputDirectory = value; }
68 public AssemblyAction CoreAction {
69 get { return _coreAction; }
70 set { _coreAction = value; }
73 public bool LinkSymbols {
74 get { return _linkSymbols; }
75 set { _linkSymbols = value; }
78 public IDictionary Actions {
79 get { return _actions; }
82 public AssemblyResolver Resolver {
83 get { return _resolver; }
86 public ISymbolReaderProvider SymbolReaderProvider {
87 get { return _symbolReaderProvider; }
88 set { _symbolReaderProvider = value; }
91 public ISymbolWriterProvider SymbolWriterProvider {
92 get { return _symbolWriterProvider; }
93 set { _symbolWriterProvider = value; }
96 public bool LogInternalExceptions { get; set; } = false;
98 public LinkContext (Pipeline pipeline)
99 : this (pipeline, new AssemblyResolver ())
103 public LinkContext (Pipeline pipeline, AssemblyResolver resolver)
105 _pipeline = pipeline;
106 _resolver = resolver;
107 _actions = new Hashtable ();
108 _parameters = new Hashtable ();
109 _annotations = new AnnotationStore ();
110 _readerParameters = new ReaderParameters {
111 AssemblyResolver = _resolver,
115 public TypeDefinition GetType (string fullName)
117 int pos = fullName.IndexOf (",");
118 fullName = fullName.Replace ("+", "/");
120 foreach (AssemblyDefinition asm in GetAssemblies ()) {
121 var type = asm.MainModule.GetType (fullName);
129 string asmname = fullName.Substring (pos + 1);
130 fullName = fullName.Substring (0, pos);
131 AssemblyDefinition assembly = Resolve (AssemblyNameReference.Parse (asmname));
132 return assembly.MainModule.GetType (fullName);
135 public AssemblyDefinition Resolve (string name)
137 if (File.Exists (name)) {
138 AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly (name, _readerParameters);
139 _resolver.CacheAssembly (assembly);
143 return Resolve (new AssemblyNameReference (name, new Version ()));
146 public AssemblyDefinition Resolve (IMetadataScope scope)
148 AssemblyNameReference reference = GetReference (scope);
150 AssemblyDefinition assembly = _resolver.Resolve (reference, _readerParameters);
152 if (SeenFirstTime (assembly)) {
153 SafeReadSymbols (assembly);
154 SetAction (assembly);
160 throw new AssemblyResolutionException (reference);
164 bool SeenFirstTime (AssemblyDefinition assembly)
166 return !_annotations.HasAction (assembly);
169 public void SafeReadSymbols (AssemblyDefinition assembly)
174 if (assembly.MainModule.HasSymbols)
178 if (_symbolReaderProvider != null) {
179 var symbolReader = _symbolReaderProvider.GetSymbolReader (
181 assembly.MainModule.FileName);
183 _annotations.AddSymbolReader (assembly, symbolReader);
184 assembly.MainModule.ReadSymbols (symbolReader);
186 assembly.MainModule.ReadSymbols ();
190 static AssemblyNameReference GetReference (IMetadataScope scope)
192 AssemblyNameReference reference;
193 if (scope is ModuleDefinition) {
194 AssemblyDefinition asm = ((ModuleDefinition) scope).Assembly;
195 reference = asm.Name;
197 reference = (AssemblyNameReference) scope;
202 void SetAction (AssemblyDefinition assembly)
204 AssemblyAction action = AssemblyAction.Link;
206 AssemblyNameDefinition name = assembly.Name;
208 if (_actions.Contains (name.Name))
209 action = (AssemblyAction) _actions [name.Name];
210 else if (IsCore (name))
211 action = _coreAction;
213 _annotations.SetAction (assembly, action);
216 static bool IsCore (AssemblyNameReference name)
220 case "Accessibility":
221 case "Mono.Security":
223 case "PresentationFramework":
224 case "PresentationCore":
226 case "UIAutomationProvider":
227 case "UIAutomationTypes":
228 case "PresentationUI":
229 case "ReachFramework":
232 return name.Name.StartsWith ("System")
233 || name.Name.StartsWith ("Microsoft");
237 public AssemblyDefinition [] GetAssemblies ()
239 IDictionary cache = _resolver.AssemblyCache;
240 AssemblyDefinition [] asms = new AssemblyDefinition [cache.Count];
241 cache.Values.CopyTo (asms, 0);
245 public void SetParameter (string key, string value)
247 _parameters [key] = value;
250 public bool HasParameter (string key)
252 return _parameters.Contains (key);
255 public string GetParameter (string key)
257 return (string) _parameters [key];