fix the build
[mcs.git] / tools / linker / README
blob34371de220f4733319b92298ecf032d69d5e575b
1 monolinker
2 ====
4 monolinker is the Mono CIL Linker.
6 The linker is a tool one can use to only ship the minimal possible set of
7 functions that a set of programs might require to run as opposed to the full
8 libraries.
10 * How does the linker work?
12 The linker analyses the intermediate code (CIL) produced by every compiler
13 targeting the Mono platform like mcs, gmcs, vbnc, booc or others. It will walk
14 through all the code that it is given to it, and basically, perform a mark and
15 sweep operations on all the code that it is referenced, to only keep what is
16 necessary for the source program to run.
18 * Usage
20 1) Linking from a source assembly
22 The command:
24 monolinker -a Program.exe
26 will use the assembly Program.exe as a source. That means that the linker will
27 walk through all the methods of Program.exe to generate only what is necessary
28 for this assembly to run.
30 2) Linking from an xml descriptor
32 The command:
34 monolinker -x desc.xml
36 will use the XML descriptor as a source. That means that the linker will
37 use this file to decide what to link in a set of assemblies. The format of the
38 descriptors is described further on in this document.
40 3) Linking from an api info file
42 The command:
44 monolinker -i assembly.info
46 will use a file produced by mono-api-info as a source. The linker will use
47 this file to link only what is necessary to match the public API defined in
48 the info file.
50 4) Actions on the assemblies
52 You can specify what the linker should do exactly per assembly.
54 The linker can do 3 things:
56         - skip them, and do nothing with them,
57         - copy them to the output directory,
58         - link them, to reduce their size.
60 You can specify an action per assembly like this:
62 monolinker -p link Foo
66 monolinker -p skip System.Windows.Forms
68 Or you can specify what to do for the core assemblies.
70 Core assemblies are the assemblies that belongs to the base class library,
71 like mscorlib.dll, System.dll or System.Windows.Forms.dll.
73 You can specify what action to do on the core assemblies with the option:
75 -c skip|copy|link
77 5) The output directory
79 By default, the linker will create an `output' directory in the current
80 directory where it will emit the linked files, to avoid erasing source
81 assemblies. You can specify the output directory with the option:
83 -o output_directory
85 If you specify the directory `.', please ensure that you won't write over
86 important assemblies of yours.
88 * Syntax of a xml descriptor
90 Here is an example that shows all the possibilities of this format:
92 ---
93 <linker>
94         <assembly fullname="Library">
95                 <type fullname="Foo" />
96                 <type fullname="Bar" preserve="nothing" required="false" />
97                 <type fullname="Baz" preserve="fields" required="false" />
98                 <type fullname="Gazonk">
99                         <method signature="System.Void .ctor(System.String)" />
100                         <field signature="System.String _blah" />
101                 </type>
102         </assembly>
103 </linker>
106 In this example, the linker will link the types Foo, Bar, Baz and Gazonk.
108 The preserve attribute ensures that all the fields of the type Baz will be
109 always be linked, not matter if they are used or not, but that neither the
110 fields or the methods of Bar will be linked if they are not used. Not
111 specifying a preserve attribute implies that we are preserving everything in
112 the specified type.
114 The required attribute specifies that if the type is not marked, during the
115 mark operation, it will not be linked.
117 The type Gazonk will be linked, as well as its constructor taking a string as a
118 parameter, and it's _blah field.
120 You can have multiple assembly nodes.
122 6) The i18n Assemblies
124 Mono have a few assemblies which contains everything region specific:
126     I18N.CJK.dll
127     I18N.MidEast.dll
128     I18N.Other.dll
129     I18N.Rare.dll
130     I18N.West.dll
132 By default, they will all be copied to the output directory. But you can
133 specify which one you want using the command:
135 monolinker -l choice
137 Where choice can either be: none, all, cjk, mideast, other, rare or west. You can
138 combine the values with a comma.
140 Example:
142 monolinker -a assembly -l mideast,cjk
144 7) Specifying directories where the linker should look for assemblies
146 By default, the linker will first look for assemblies in the directories `.'
147 and `bin'. You can specify
149 Example:
151 monoliner -d ../../libs -a program.exe
153 8) Adding custom steps to the linker.
155 You can write custom steps for the linker and tell the linker to use them.
156 Let's take a simple example:
158 using System;
160 using Mono.Linker;
161 using Mono.Linker.Steps;
163 namespace Foo {
165         public class FooStep : IStep {
167                 public void Process (LinkContext context)
168                 {
169                         foreach (IStep step in context.Pipeline.GetSteps ()) {
170                                 Console.WriteLine (step.GetType ().Name);
171                         }
172                 }
173         }
177 That is compiled against the linker to a Foo.dll assembly.
179 You can ask the linker to add it at the end of the pipeline:
181 monolinker -s Foo.FooStep,Foo -a program.exe
183 Or you can ask the linker to add it after a specific step:
185 monolinker -s MarkStep:Foo.FooStep,Foo -a program.exe
187 Or before a specific step:
189 monolinker -s Foo.FooStep,Foo:MarkStep
191 * Inside the linker
193 The linker is a quite small piece of code, and it pretty simple to address.
194 Its only dependency is Mono.Cecil, that is used to read, modify and write back
195 the assemblies.
197 Everything is located in the namespace Mono.Linker, or in sub namespaces.
198 Being a command line utility, its entry point function is in the class Driver.
200 This class is in charge of analyzing the command line, and to instantiate two
201 important objects, a LinkContext, and a Pipeline.
203 The LinkContext contains all the informations that will be used during the
204 linking process, such as the assemblies involved, the output directory and
205 probably other useful stuff.
207 The Pipeline is simply a queue of actions (steps), to be applied to the current
208 context. The whole process of linking is split into those differents steps
209 that are all located in the Mono.Linker.Steps namespace.
211 Here are the current steps that are implemented, in the order they are used:
213 1) ResolveFromAssembly or ResolveFromXml
215 Those steps are used to initialize the context, and pre-mark the root code
216 that will be used as a source for the linker.
218 Resolving from an assembly or resolving from a xml descriptor is a decision
219 taken in the command line parsing.
221 2) LoadReferences
223 This step will load all the references of all the assemblies involved in the
224 current context.
226 3) Blacklist
228 This step is used if and only if you have specified that the core should be
229 linked. It will load a bunch of resources from the assemblies, that are
230 actually a few XML descriptors, that will ensure that some types and methods
231 that are used from inside the runtime are properly linked and not removed.
233 It is doing so by inserting a ResolveFromXml step per blacklist in the
234 pipeline.
236 4) Mark
238 This is the most complex step. The linker will get from the context the list
239 of types, fields and methods that have been pre-marked in the resolve steps,
240 and walk through all of them. For every method, it will analyse the CIL stream,
241 to find references to other fields, types, or methods.
243 When it encounters such a reference, it will resolve the original definition of
244 this reference, and add this to the queue of items to be processed. For
245 instance, if have in a source assembly a call to Console.WriteLine, the linker
246 will resolve the appropriate method WriteLine in the Console type from the
247 mscorlib assembly, and add it to the queue. When this WriteLine method will be
248 dequeued, and processed, the linker will go through everything that is used in
249 it, and add it to the queue, if they have not been processed already.
251 To know if something have been marked to be linked, or processed, the linker
252 is using a functionality of Cecil called annotations. Almost everything in
253 Cecil can be annotated. Concretely, it means that almost everything own an
254 Hashtable in which you can add what you want, using the keys and the values you
255 want.
257 So the linker will annotate assemblies, types, methods and fields to know
258 what should be linked or not, and what have been processed, and how it should
259 process them.
261 This is really useful as we don't have to recreate a full hierarchy of classes
262 to encapsulate the different Cecil types to add the few informations we want.
264 5) Sweep
266 This simple step will walk through all the elements of an assembly, and based
267 on their annotations, remove them or keep them.
269 6) Clean
271 This step will clean parts of the assemblies, like properties. If a proprety
272 used to have a getter and a setter, and that after the mark & sweep steps,
273 only the getter is linked, it will update the property to reflect that.
275 There is a few things to keep clean like properties has we've seen, events,
276 nested classes, and probably a few others.
278 7) Output
280 For each assembly in the context, this step will act on the action associated
281 to the assembly. If the assembly is marked as skip, it won't do anything,
282 if it's marked as copy, it will copy the assembly to the output directory,
283 and if it's link, it will save the modified assembly to the output directory.
285 * Reporting a bug
287 If you face a bug in the linker, please report it to:
289 http://bugzilla.ximian.com
291 Product: Mono tools, Component: linker.
293 * Mailing lists
295 You can ask questions about the linker of the cecil Google Group:
297 http://groups.google.com/group/mono-cecil
300 Jb Evain <jbevain@novell.com>