Skip to content

Commit 9e5b66a

Browse files
authored
Fixes on minimize assembly (#34)
1 parent ee1a76d commit 9e5b66a

File tree

3 files changed

+71
-14
lines changed

3 files changed

+71
-14
lines changed

source/MetadataProcessor.Console/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ public static void Main(string[] args)
244244
{
245245
md.AddClassToExclude(args[++i]);
246246
}
247-
else if (arg == "-minimize" && i + 1 < args.Length)
247+
else if (arg == "-minimize")
248248
{
249249
md.Minimize(md.PeFileName);
250250
}
251-
else if (arg == "-verbose" && i + 1 < args.Length)
251+
else if (arg == "-verbose")
252252
{
253253
md.Verbose = true;
254254
}

source/MetadataProcessor.Core/Tables/nanoAttributesTable.cs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ public sealed class nanoAttributesTable : InanoTable
2020
/// <summary>
2121
/// List of custom attributes in Mono.Cecil format for all internal types.
2222
/// </summary>
23-
private readonly IEnumerable<Tuple<CustomAttribute, ushort>> _typesAttributes;
23+
private IEnumerable<Tuple<CustomAttribute, ushort>> _typesAttributes;
2424

2525
/// <summary>
2626
/// List of custom attributes in Mono.Cecil format for all internal fields.
2727
/// </summary>
2828
///
29-
private readonly IEnumerable<Tuple<CustomAttribute, ushort>> _fieldsAttributes;
29+
private IEnumerable<Tuple<CustomAttribute, ushort>> _fieldsAttributes;
3030

3131
/// <summary>
3232
/// List of custom attributes in Mono.Cecil format for all internal methods.
3333
/// </summary>
34-
private readonly IEnumerable<Tuple<CustomAttribute, ushort>> _methodsAttributes;
34+
private IEnumerable<Tuple<CustomAttribute, ushort>> _methodsAttributes;
3535

3636
/// <summary>
3737
/// Assembly tables context - contains all tables used for building target assembly.
@@ -92,5 +92,51 @@ private void WriteAttributes(
9292
writer.WriteUInt16(_context.SignaturesTable.GetOrCreateSignatureId(attribute));
9393
}
9494
}
95+
96+
/// <summary>
97+
/// Remove unused items from attribute tables.
98+
/// </summary>
99+
public void RemoveUnusedItems(HashSet<MetadataToken> set)
100+
{
101+
// build a collection of the current items that are present in the used items set
102+
List<Tuple<CustomAttribute, ushort>> usedItems = new List<Tuple<CustomAttribute, ushort>>();
103+
104+
// types attributes
105+
foreach (var item in _typesAttributes
106+
.Where(item => set.Contains(((IMetadataTokenProvider)item.Item1.Constructor).MetadataToken)))
107+
{
108+
usedItems.Add(item);
109+
}
110+
111+
// re-create the items dictionary with the used items only
112+
_typesAttributes = usedItems
113+
.Select(a => new Tuple<CustomAttribute, ushort>(a.Item1, a.Item2));
114+
115+
// fields attributes
116+
usedItems = new List<Tuple<CustomAttribute, ushort>>();
117+
118+
foreach (var item in _fieldsAttributes
119+
.Where(item => set.Contains(((IMetadataTokenProvider)item.Item1.Constructor).MetadataToken)))
120+
{
121+
usedItems.Add(item);
122+
}
123+
124+
// re-create the items dictionary with the used items only
125+
_fieldsAttributes = usedItems
126+
.Select(a => new Tuple<CustomAttribute, ushort>(a.Item1, a.Item2));
127+
128+
// methods attributes
129+
usedItems = new List<Tuple<CustomAttribute, ushort>>();
130+
131+
foreach (var item in _methodsAttributes
132+
.Where(item => set.Contains(((IMetadataTokenProvider)item.Item1.Constructor).MetadataToken)))
133+
{
134+
usedItems.Add(item);
135+
}
136+
137+
// re-create the items dictionary with the used items only
138+
_methodsAttributes = usedItems
139+
.Select(a => new Tuple<CustomAttribute, ushort>(a.Item1, a.Item2));
140+
}
95141
}
96142
}

source/MetadataProcessor.Core/nanoAssemblyBuilder.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,12 @@ public void Minimize()
161161
_tablesContext.TypeDefinitionTable.RemoveUnusedItems(set);
162162
_tablesContext.TypeReferencesTable.RemoveUnusedItems(set);
163163
_tablesContext.TypeDefinitionTable.ResetByteCodeOffsets();
164+
_tablesContext.AttributesTable.RemoveUnusedItems(set);
164165
_tablesContext.ResetByteCodeTable();
165166
_tablesContext.ResetSignaturesTable();
166167
_tablesContext.ResetStringsTable();
167-
168-
// renormalize type definitions look-up tables
168+
169+
// renormalise type definitions look-up tables
169170
// by removing items that are not used
170171

171172
foreach (var c in _tablesContext.TypeDefinitionTable.Items)
@@ -277,7 +278,13 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
277278

278279
case TokenType.MemberRef:
279280
var mr = _tablesContext.MethodReferencesTable.Items.FirstOrDefault(i => i.MetadataToken == token);
280-
// TODO
281+
282+
if (mr != null &&
283+
mr.DeclaringType != null)
284+
{
285+
set.Add(mr.DeclaringType.MetadataToken);
286+
}
287+
281288
break;
282289

283290
case TokenType.TypeSpec:
@@ -345,14 +352,18 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
345352

346353
case TokenType.Field:
347354
var fd = _tablesContext.FieldsTable.Items.FirstOrDefault(i => i.MetadataToken == token);
348-
set.Add(fd.MetadataToken);
355+
356+
if (fd is TypeReference)
357+
{
358+
set.Add(fd.MetadataToken);
359+
}
349360

350361
// attributes
351362
foreach (var c in fd.CustomAttributes)
352363
{
353-
if (!_tablesContext.ClassNamesToExclude.Contains(c.AttributeType.FullName))
354-
{
355-
set.Add(c.AttributeType.MetadataToken);
364+
if (!_tablesContext.ClassNamesToExclude.Contains(c.AttributeType.Name))
365+
{
366+
set.Add(c.Constructor.MetadataToken);
356367
}
357368
}
358369

@@ -427,9 +438,9 @@ i.Operand is TypeDefinition ||
427438
// attributes
428439
foreach (var c in md.CustomAttributes)
429440
{
430-
if (!_tablesContext.ClassNamesToExclude.Contains(c.AttributeType.FullName))
441+
if (!_tablesContext.ClassNamesToExclude.Contains(c.AttributeType.Name))
431442
{
432-
set.Add(c.AttributeType.MetadataToken);
443+
set.Add(c.Constructor.MetadataToken);
433444
}
434445
}
435446

0 commit comments

Comments
 (0)