Skip to content

Commit

Permalink
Migrate to vanilla 0.10 cecil
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexx999 committed Aug 31, 2018
1 parent 71e0ea4 commit b9b8f9f
Show file tree
Hide file tree
Showing 19 changed files with 1,280 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "cecil"]
path = cecil
url = https://github.com/gluck/cecil.git
url = https://github.com/jbevain/cecil.git
36 changes: 18 additions & 18 deletions ILRepack.sln
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,24 @@ Global
{4A253A60-D998-4CA2-B9D5-46567A2FBF80}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A253A60-D998-4CA2-B9D5-46567A2FBF80}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A253A60-D998-4CA2-B9D5-46567A2FBF80}.Release|Any CPU.Build.0 = Release|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Debug_unix|Any CPU.ActiveCfg = net_3_5_Debug|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Debug_unix|Any CPU.Build.0 = net_3_5_Debug|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Debug|Any CPU.ActiveCfg = net_3_5_Debug|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Debug|Any CPU.Build.0 = net_3_5_Debug|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Release|Any CPU.ActiveCfg = net_3_5_Release|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Release|Any CPU.Build.0 = net_3_5_Release|Any CPU
{63E6915C-7EA4-4D76-AB28-0D7191EEA626}.Debug_unix|Any CPU.ActiveCfg = net_3_5_Debug|Any CPU
{63E6915C-7EA4-4D76-AB28-0D7191EEA626}.Debug_unix|Any CPU.Build.0 = net_3_5_Debug|Any CPU
{63E6915C-7EA4-4D76-AB28-0D7191EEA626}.Debug|Any CPU.ActiveCfg = net_3_5_Debug|Any CPU
{63E6915C-7EA4-4D76-AB28-0D7191EEA626}.Debug|Any CPU.Build.0 = net_3_5_Debug|Any CPU
{63E6915C-7EA4-4D76-AB28-0D7191EEA626}.Release|Any CPU.ActiveCfg = net_3_5_Release|Any CPU
{63E6915C-7EA4-4D76-AB28-0D7191EEA626}.Release|Any CPU.Build.0 = net_3_5_Release|Any CPU
{8559DD7F-A16F-46D0-A05A-9139FAEBA8FD}.Debug_unix|Any CPU.ActiveCfg = net_3_5_Debug|Any CPU
{8559DD7F-A16F-46D0-A05A-9139FAEBA8FD}.Debug_unix|Any CPU.Build.0 = net_3_5_Debug|Any CPU
{8559DD7F-A16F-46D0-A05A-9139FAEBA8FD}.Debug|Any CPU.ActiveCfg = net_3_5_Debug|Any CPU
{8559DD7F-A16F-46D0-A05A-9139FAEBA8FD}.Debug|Any CPU.Build.0 = net_3_5_Debug|Any CPU
{8559DD7F-A16F-46D0-A05A-9139FAEBA8FD}.Release|Any CPU.ActiveCfg = net_3_5_Release|Any CPU
{8559DD7F-A16F-46D0-A05A-9139FAEBA8FD}.Release|Any CPU.Build.0 = net_3_5_Release|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Debug_unix|Any CPU.ActiveCfg = net_4_0_Debug|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Debug_unix|Any CPU.Build.0 = net_4_0_Debug|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Debug|Any CPU.ActiveCfg = net_4_0_Debug|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Debug|Any CPU.Build.0 = net_4_0_Debug|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Release|Any CPU.ActiveCfg = net_4_0_Release|Any CPU
{D68133BD-1E63-496E-9EDE-4FBDBF77B486}.Release|Any CPU.Build.0 = net_4_0_Release|Any CPU
{63E6915C-7EA4-4D76-AB28-0D7191EEA626}.Debug_unix|Any CPU.ActiveCfg = net_4_0_Debug|Any CPU
{63E6915C-7EA4-4D76-AB28-0D7191EEA626}.Debug_unix|Any CPU.Build.0 = net_4_0_Debug|Any CPU
{63E6915C-7EA4-4D76-AB28-0D7191EEA626}.Debug|Any CPU.ActiveCfg = net_4_0_Debug|Any CPU
{63E6915C-7EA4-4D76-AB28-0D7191EEA626}.Debug|Any CPU.Build.0 = net_4_0_Debug|Any CPU
{63E6915C-7EA4-4D76-AB28-0D7191EEA626}.Release|Any CPU.ActiveCfg = net_4_0_Release|Any CPU
{63E6915C-7EA4-4D76-AB28-0D7191EEA626}.Release|Any CPU.Build.0 = net_4_0_Release|Any CPU
{8559DD7F-A16F-46D0-A05A-9139FAEBA8FD}.Debug_unix|Any CPU.ActiveCfg = net_4_0_Debug|Any CPU
{8559DD7F-A16F-46D0-A05A-9139FAEBA8FD}.Debug_unix|Any CPU.Build.0 = net_4_0_Debug|Any CPU
{8559DD7F-A16F-46D0-A05A-9139FAEBA8FD}.Debug|Any CPU.ActiveCfg = net_4_0_Debug|Any CPU
{8559DD7F-A16F-46D0-A05A-9139FAEBA8FD}.Debug|Any CPU.Build.0 = net_4_0_Debug|Any CPU
{8559DD7F-A16F-46D0-A05A-9139FAEBA8FD}.Release|Any CPU.ActiveCfg = net_4_0_Release|Any CPU
{8559DD7F-A16F-46D0-A05A-9139FAEBA8FD}.Release|Any CPU.Build.0 = net_4_0_Release|Any CPU
{468ED600-2E3B-471C-BB8A-B0D968FD7FA2}.Debug_unix|Any CPU.ActiveCfg = Debug_unix|Any CPU
{468ED600-2E3B-471C-BB8A-B0D968FD7FA2}.Debug_unix|Any CPU.Build.0 = Debug_unix|Any CPU
{468ED600-2E3B-471C-BB8A-B0D968FD7FA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down
2 changes: 1 addition & 1 deletion ILRepack/IKVMLineIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void PostRepackReferences()
IMetadataScope ikvmRuntimeReference = repack.TargetAssemblyMainModule.AssemblyReferences.FirstOrDefault(r => r.Name == "IKVM.Runtime");
if (ikvmRuntimeReference == null)
{
ikvmRuntimeReference = repack.MergeScope(repack.GlobalAssemblyResolver.Resolve("IKVM.Runtime").Name);
ikvmRuntimeReference = repack.MergeScope(repack.GlobalAssemblyResolver.Resolve(new AssemblyNameReference("IKVM.Runtime", null)).Name);
}
if (ikvmRuntimeReference == null)
{
Expand Down
87 changes: 18 additions & 69 deletions ILRepack/ILRepack.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Copyright (c) 2011 Francois Valdy
// Copyright (c) 2018 Alexander Vostres
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +28,8 @@
using System.Runtime.Serialization.Formatters.Binary;
using System.Diagnostics;
using ILRepacking.Steps.SourceServerData;
using ILRepacking.Steps.Win32Resources;
using Mono.Cecil.Cil;

namespace ILRepacking
{
Expand Down Expand Up @@ -111,10 +114,11 @@ private AssemblyDefinitionContainer ReadInputAssembly(string assembly, bool isPr
try
{
ReaderParameters rp = new ReaderParameters(ReadingMode.Immediate) { AssemblyResolver = GlobalAssemblyResolver };
// read PDB/MDB?
if (Options.DebugInfo && (File.Exists(Path.ChangeExtension(assembly, "pdb")) || File.Exists(assembly + ".mdb")))

if (Options.DebugInfo)
{
rp.ReadSymbols = true;
rp.SymbolReaderProvider = new DefaultSymbolReaderProvider(false);
}
AssemblyDefinition mergeAsm;
try
Expand Down Expand Up @@ -309,7 +313,7 @@ public void Repack()
}
// set the main module attributes
TargetAssemblyMainModule.Attributes = PrimaryAssemblyMainModule.Attributes;
TargetAssemblyMainModule.Win32ResourceDirectory = MergeWin32Resources(PrimaryAssemblyMainModule.Win32ResourceDirectory);
var win32ResourceStep = new Win32ResourceStep(Logger, this, _aspOffsets);

if (Options.Version != null)
TargetAssemblyDefinition.Name.Version = Options.Version;
Expand All @@ -322,6 +326,7 @@ public void Repack()
{
List<IRepackStep> repackSteps = new List<IRepackStep>
{
win32ResourceStep,
signingStep,
new ReferencesRepackStep(Logger, this),
new TypesRepackStep(Logger, this, _repackImporter, Options),
Expand All @@ -336,11 +341,6 @@ public void Repack()
{
step.Perform();
}

for (int i = 1; i < MergedAssemblies.Count; ++i)
{
MergedAssemblies[i].Dispose();
}

var parameters = new WriterParameters
{
Expand All @@ -360,8 +360,18 @@ public void Repack()

Logger.Info("Writing output assembly to disk");
TargetAssemblyDefinition.Write(Options.OutputFile, parameters);


for (int i = 1; i < MergedAssemblies.Count; ++i)
{
MergedAssemblies[i].Dispose();
}

TargetAssemblyDefinition.Dispose();
GlobalAssemblyResolver.Dispose();

win32ResourceStep.Patch(Options.OutputFile);

// If this is an executable and we are on linux/osx we should copy file permissions from
// the primary assembly
if (isUnixEnvironment)
Expand Down Expand Up @@ -409,67 +419,6 @@ private void ResolveSearchDirectories()
}
}

private ResourceDirectory MergeWin32Resources(ResourceDirectory primary)
{
if (primary == null)
return null;
foreach (var ass in OtherAssemblies)
{
MergeDirectory(new List<ResourceEntry>(), primary, ass, ass.MainModule.Win32ResourceDirectory);
}
return primary;
}

private void MergeDirectory(List<ResourceEntry> parents, ResourceDirectory ret, AssemblyDefinition ass, ResourceDirectory directory)
{
foreach (var entry in directory.Entries)
{
var exist = ret.Entries.FirstOrDefault(x => entry.Name == null ? entry.Id == x.Id : entry.Name == x.Name);
if (exist == null)
ret.Entries.Add(entry);
else
MergeEntry(parents, exist, ass, entry);
}
}

private void MergeEntry(List<ResourceEntry> parents, ResourceEntry exist, AssemblyDefinition ass, ResourceEntry entry)
{
if (exist.Data != null && entry.Data != null)
{
if (IsAspResourceEntry(parents, exist))
{
_aspOffsets[ass] = exist.Data.Length;
byte[] newData = new byte[exist.Data.Length + entry.Data.Length];
Array.Copy(exist.Data, 0, newData, 0, exist.Data.Length);
Array.Copy(entry.Data, 0, newData, exist.Data.Length, entry.Data.Length);
exist.Data = newData;
}
else if (!IsVersionInfoResource(parents, exist))
{
Logger.Warn(string.Format("Duplicate Win32 resource with id={0}, parents=[{1}], name={2} in assembly {3}, ignoring", entry.Id, string.Join(",", parents.Select(p => p.Name ?? p.Id.ToString()).ToArray()), entry.Name, ass.Name));
}
return;
}
if (exist.Data != null || entry.Data != null)
{
Logger.Warn("Inconsistent Win32 resources, ignoring");
return;
}
parents.Add(exist);
MergeDirectory(parents, exist.Directory, ass, entry.Directory);
parents.RemoveAt(parents.Count - 1);
}

private static bool IsAspResourceEntry(List<ResourceEntry> parents, ResourceEntry exist)
{
return exist.Id == 101 && parents.Count == 1 && parents[0].Id == 3771;
}

private static bool IsVersionInfoResource(List<ResourceEntry> parents, ResourceEntry exist)
{
return exist.Id == 0 && parents.Count == 2 && parents[0].Id == 16 && parents[1].Id == 1;
}

string IRepackContext.FixStr(string content)
{
return FixStr(content, false);
Expand Down
12 changes: 12 additions & 0 deletions ILRepack/ILRepack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@
<Compile Include="Steps\SourceServerData\SourceServerDataRepackStep.cs" />
<Compile Include="Steps\SourceServerData\StringMixins.cs" />
<Compile Include="Steps\TypesRepackStep.cs" />
<Compile Include="Steps\Win32Resources\PE\BinaryStreamWriter.cs" />
<Compile Include="Steps\Win32Resources\PE\ByteBuffer.cs" />
<Compile Include="Steps\Win32Resources\PE\ImageReader.cs" />
<Compile Include="Steps\Win32Resources\PE\BinaryStreamReader.cs" />
<Compile Include="Steps\Win32Resources\PE\ImageWriter.cs" />
<Compile Include="Steps\Win32Resources\PE\Section.cs" />
<Compile Include="Steps\Win32Resources\PE\SectionData.cs" />
<Compile Include="Steps\Win32Resources\ResourceDirectory.cs" />
<Compile Include="Steps\Win32Resources\ResourceEntry.cs" />
<Compile Include="Steps\Win32Resources\RsrcReader.cs" />
<Compile Include="Steps\Win32Resources\RsrcWriter.cs" />
<Compile Include="Steps\Win32Resources\Win32ResourceStep.cs" />
<Compile Include="Steps\XamlResourcePathPatcherStep.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion ILRepack/RepackImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ private void CloneTo(MethodBody body, MethodDefinition parent)
nb.Variables.Add(new VariableDefinition(
Import(var.VariableType, parent)));

nb.Instructions.SetCapacity(body.Instructions.Count);
nb.Instructions.Capacity = Math.Max(nb.Instructions.Capacity, body.Instructions.Count);
_repackContext.LineIndexer.PreMethodBodyRepack(body, parent);
foreach (Instruction instr in body.Instructions)
{
Expand Down
44 changes: 44 additions & 0 deletions ILRepack/Steps/Win32Resources/PE/BinaryStreamReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Copyright (c) 2008 - 2015 Jb Evain
// Copyright (c) 2008 - 2011 Novell, Inc.
// Copyright (c) 2018 Alexander Vostres
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System.IO;

namespace ILRepacking.Steps.Win32Resources.PE {

class BinaryStreamReader : BinaryReader {

public int Position {
get { return (int) BaseStream.Position; }
set { BaseStream.Position = value; }
}

public BinaryStreamReader (Stream stream)
: base (stream)
{
}

public void Advance (int bytes)
{
BaseStream.Seek (bytes, SeekOrigin.Current);
}

public void MoveTo (uint position)
{
BaseStream.Seek (position, SeekOrigin.Begin);
}
}
}
49 changes: 49 additions & 0 deletions ILRepack/Steps/Win32Resources/PE/BinaryStreamWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Copyright (c) 2008 - 2015 Jb Evain
// Copyright (c) 2008 - 2011 Novell, Inc.
// Copyright (c) 2018 Alexander Vostres
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System.IO;

namespace ILRepacking.Steps.Win32Resources.PE {

class BinaryStreamWriter : BinaryWriter {

public int Position {
get { return (int) BaseStream.Position; }
set { BaseStream.Position = value; }
}

public BinaryStreamWriter (Stream stream)
: base (stream)
{
}

public void WriteUInt16 (ushort value)
{
Write (value);
}

public void WriteUInt32 (uint value)
{
Write (value);
}

public void WriteBytes (byte [] bytes)
{
Write (bytes);
}
}
}
Loading

0 comments on commit b9b8f9f

Please sign in to comment.