Skip to content

Commit e9a278c

Browse files
authored
Add description how to embed linker XML files (dotnet/linker#1674)
Some additional description of the various formats and when/how they can be used. Co-authored-by: Sven Boemer <[email protected]> Commit migrated from dotnet/linker@7b82b56
1 parent 77b19a4 commit e9a278c

File tree

1 file changed

+64
-13
lines changed

1 file changed

+64
-13
lines changed

src/tools/illink/docs/data-formats.md

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@ IL linker uses several data formats to control or influence the linking process.
44

55
## Descriptor Format
66

7-
The `fullname` attribute specifies the fullname of the type in the format specified by ECMA-335. This is in certain cases not the same as the one reported by Type.FullName for example for nested types.
7+
Descriptors are used to direct the linker to always keep some items in the assembly, regardless of if the linker can find any references to them.
8+
9+
Descriptor XML can be embedded in an assembly. In that case it must be stored as an embedded resource with logical name `ILLink.Descriptors.xml`. To achieve this when building an assembly use this in the project file to include the XML:
10+
11+
```xml
12+
<ItemGroup>
13+
<EmbeddedResource Include="ILLink.Descriptors.xml">
14+
<LogicalName>ILLink.Descriptors.xml</LogicalName>
15+
</EmbeddedResource>
16+
</ItemGroup>
17+
```
18+
19+
Embedded descriptors only take effect if the containing assembly is included in the linker output, so if something from that assembly is marked to be kept.
20+
21+
Descriptor XML can also be passed to the linker on the command via the [`-x` parameter](illink-options.md#linking-from-an-xml-descriptor).
822

923
### XML Examples
1024

@@ -166,6 +180,24 @@ The `required` attribute specifies that if the type is not marked, during the ma
166180

167181
## Substitution Format
168182

183+
Substitutions direct the linker to replace specific method's body with either a throw or return constant statements.
184+
185+
Substitutions have effect only on assemblies which are linked with assembly action `link`, any other assembly will not be affected. That said it is possible to have a `copy` assembly with the substitution on a method in it, and then a separate `link` assembly which calls such method. The `link` assembly will see the constant value of the method after the substitution and potentially remove unused branches and such.
186+
187+
Substitutions XML can be embedded in an assembly by including it as an embedded resource with logical name `ILLink.Substitutions.xml`. To include an XML file in an assembly this way, use this in the project file:
188+
189+
```xml
190+
<ItemGroup>
191+
<EmbeddedResource Include="ILLink.Substitutions.xml">
192+
<LogicalName>ILLink.Substitutions.xml</LogicalName>
193+
</EmbeddedResource>
194+
</ItemGroup>
195+
```
196+
197+
Embedded substitutions only take effect if the containing assembly is included in the linker output. Embedded substitutions should only address methods from the containing assembly.
198+
199+
Substitutions XML can be specified on the command line via the [`--substitutions` parameter](illink-options.md#using-custom-substitutions). Using substitutions with `ipconstprop` optimization (enabled by default) can help reduce output size as any dependencies under conditional logic which will be evaluated as unreachable will be removed.
200+
169201
### Substitute method body with a constant
170202

171203
The `value` attribute is optional and only required when the method should be hardcoded to return a non-default value and the return type is not `void`.
@@ -256,6 +288,24 @@ Note that this will only have an effect where it is applied - the default value
256288

257289
## Custom Attributes Annotations Format
258290

291+
Attribute annotations direct the linker to behave as if the specified item has the specified attribute.
292+
293+
Attribute annotations can only be used to add attributes which have effect on linker behavior, all other attributes will be ignored. Attributes added via attribute annotations only influence linker behavior, they are never added to the output assembly.
294+
295+
Attribute annotation XML can be embedded in an assembly by including it as an embedded resource with logical name `ILLink.LinkAttributes.xml`. To include an XML file in an assembly this way, use this in the project file:
296+
297+
```xml
298+
<ItemGroup>
299+
<EmbeddedResource Include="ILLink.LinkAttributes.xml">
300+
<LogicalName>ILLink.LinkAttributes.xml</LogicalName>
301+
</EmbeddedResource>
302+
</ItemGroup>
303+
```
304+
305+
Embedded attribute annotations should only address methods from the containing assembly.
306+
307+
Attribute annotations XML can be specified on the command line via the [`--link-attributes` parameter](illink-options.md#supplementary-custom-attributes).
308+
259309
### Custom attribute on assembly
260310

261311
```xml
@@ -270,7 +320,7 @@ Note that this will only have an effect where it is applied - the default value
270320

271321
### Custom attribute on type
272322

273-
This allows to add a custom attribute to a class, interface, delegate, struct or enum
323+
This allows to add a custom attribute to a class, interface, delegate, struct or enum.
274324

275325
```xml
276326
<linker>
@@ -427,13 +477,13 @@ This allows to add a custom attribute to a class, interface, delegate, struct or
427477
### Conditional custom attributes
428478

429479
The `feature` and `featurevalue` attributes are optional, but must be used together when used.
430-
They can be applied to any element to specify conditions under which the contained custom
480+
They can be applied to any element to specify conditions under which the contained custom
431481
attributes are applied.
432482

433483
```xml
434484
<linker>
435485
<assembly fullname="Assembly">
436-
<!-- The substitution will apply only if "--feature EnableOptionalFeature false" are also used -->
486+
<!-- The attribute will apply only if "EnableOptionalFeature" is set to "false" -->
437487
<type fullname="Assembly.A" feature="EnableOptionalFeature" featurevalue="false">
438488
<method signature="System.String TestMethod()">
439489
<return>
@@ -451,11 +501,11 @@ attributes are applied.
451501

452502
The attribute element requires 'fullname' attribute without it linker will generate a warning and skip
453503
the attribute. Optionally you can use the 'assembly' attribute to point to certain assembly to look
454-
for the attribute, if not specified the linker will look the attribute in any loaded assembly.
455-
Inside an attribute element in the xml you can define argument, field and property elements.
456-
An attribute could have several arguments, several fields or several properties. When writing
457-
custom attribute with multiple arguments you need to write the xml elements in an order dependent
458-
form. That is, the first xml argument element corresponds to the first custom attribute argument,
504+
for the attribute, if not specified the linker will look up the attribute in any loaded assembly.
505+
Inside an attribute element in the xml you can define argument, field and property elements.
506+
An attribute could have several arguments, several fields or several properties. When writing
507+
custom attribute with multiple arguments you need to write the xml elements in an order dependent
508+
form. That is, the first xml argument element corresponds to the first custom attribute argument,
459509
second xml argument element correspond to the second custom attribute argument and so on.
460510
For fields and properties, you need to include the name since they are not order dependent.
461511

@@ -470,13 +520,13 @@ For fields and properties, you need to include the name since they are not order
470520
```
471521

472522
Additionally the attribute element also supports the usage of the feature switch
523+
473524
```xml
474525
<attribute fullname="SomecustomAttribute" feature="EnableOptionalFeature" featurevalue="false"/>
475526
```
476527

477528
Also if the attribute is used in a type, a special property can be used to specify that the type
478-
is a Custom Attribute an it's instances should be removed by the linker. To do this the word "internal"
479-
and value "RemoveAttributeInstances" should be included in the attribute as described in the following
529+
is a Custom Attribute and it's instances should be removed by the linker. To do this use `internal="RemoveAttributeInstances"` instead of specifying `fullname` in the attribute as described in the following
480530
example:
481531

482532
```xml
@@ -488,6 +538,7 @@ example:
488538
</assembly>
489539
</linker>
490540
```
541+
491542
Notice that a descriptor file containing the custom attribute type overrides this behavior. In case the
492-
custom attribute type is being referenced in a descriptor xml file and in the linkattributes xml file
493-
for removal, the custom attribute will not be removed
543+
custom attribute type is being referenced in a descriptor file and in the attribute annotations file
544+
for removal, the custom attribute will not be removed.

0 commit comments

Comments
 (0)