dotnet / ILMerge

ILMerge is a static linker for .NET Assemblies.
MIT License
1.23k stars 170 forks source link

An attempt was made to load a program with an incorrect format #44

Closed thomaslevesque closed 5 years ago

thomaslevesque commented 6 years ago

ILMerge fails with the following log:

ILMerge version 2.14.1208.65535
Copyright (C) Microsoft Corporation 2004-2006. All rights reserved.
ILMerge /keyfile:..\FakeItEasy.snk /lib:bin\Release\net45\ /targetplatform:v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5 /internalize:..\ILMerge.Internalize.Exclude.txt /out:bin\Release\net45\FakeItEasy.dll /log:bin\Release\net45\ILMerge.log obj\Release\net45\FakeItEasy.dll bin\Release\net45\Castle.Core.dll 
Set platform to 'v4', using directory 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5' for mscorlib.dll
Running on Microsoft (R) .NET Framework v4.0.30319
mscorlib.dll version = 4.0.0.0
Read 6 lines from the exclusion file '..\ILMerge.Internalize.Exclude.txt'.
The list of input assemblies is:
    obj\Release\net45\FakeItEasy.dll
    bin\Release\net45\Castle.Core.dll
Trying to read assembly from the file 'D:\Git\FakeItEasy\src\FakeItEasy\obj\Release\net45\FakeItEasy.dll'.
    Successfully read in assembly.
    There were no errors reported in FakeItEasy's metadata.
Trying to read assembly from the file 'D:\Git\FakeItEasy\src\FakeItEasy\bin\Release\net45\Castle.Core.dll'.
Can not find PDB file. Debug info will not be available for assembly 'bin\Release\net45\Castle.Core.dll'.
    Successfully read in assembly.
    There were no errors reported in Castle.Core's metadata.
Checking to see that all of the input assemblies have a compatible PeKind.
    FakeItEasy.PeKind = ILonly
    Castle.Core.PeKind = ILonly
All input assemblies have a compatible PeKind value.
AssemblyResolver: Assembly 'FakeItEasy' is referencing assembly 'System.Core'.
    AssemblyResolver: Attempting referencing assembly's directory.
    AssemblyResolver: Did not find assembly in referencing assembly's directory.
    AssemblyResolver: Attempting input directory.
    AssemblyResolver: Did not find assembly in input directory.
    AssemblyResolver: Attempting user-supplied directories.
    AssemblyResolver: Did not find assembly in user-supplied directories.
    AssemblyResolver: Attempting framework directory.
Can not find PDB file. Debug info will not be available for assembly 'System.Core'.
Resolved assembly reference 'System.Core' to 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll'. (Used framework directory.)
Using assembly 'FakeItEasy' for assembly-level attributes for the target assembly.
Merging assembly 'FakeItEasy' into target assembly.
Merging assembly 'Castle.Core' into target assembly.
Duplicate type name: modifying name of the type '<>f__AnonymousType0`2' (from assembly 'Castle.Core') to 'Castle.Core.<>f__AnonymousType0`2'
Duplicate type name: modifying name of the type '<>f__AnonymousType1`2' (from assembly 'Castle.Core') to 'Castle.Core.<>f__AnonymousType1`2'
Copying 1 Win32 Resources from assembly 'FakeItEasy' into target assembly.
ILMerge: Signing assembly with the key file '..\FakeItEasy.snk'.
    There were no errors reported in the target assembly's metadata.
ILMerge: Writing target assembly 'bin\Release\net45\FakeItEasy.dll'.
AssemblyResolver: Assembly 'Castle.Core' is referencing assembly 'System.Configuration'.
    AssemblyResolver: Attempting referencing assembly's directory.
    AssemblyResolver: Did not find assembly in referencing assembly's directory.
    AssemblyResolver: Attempting input directory.
    AssemblyResolver: Did not find assembly in input directory.
    AssemblyResolver: Attempting user-supplied directories.
    AssemblyResolver: Did not find assembly in user-supplied directories.
    AssemblyResolver: Attempting framework directory.
Can not find PDB file. Debug info will not be available for assembly 'System.Configuration'.
Resolved assembly reference 'System.Configuration' to 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Configuration.dll'. (Used framework directory.)
An exception occurred during merging:
An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
   at System.Compiler.Writer.MscorsnStrongNameSignatureGeneration(String wszFilePath, String wszKeyContainer, Byte[] pbKeyBlob, Int32 cbKeyBlob, IntPtr ppbSignatureBlob, IntPtr pcbSignatureBlob)
   at System.Compiler.Writer.WritePE(String location, Boolean writeDebugSymbols, Module module, Boolean delaySign, String keyFileName, String keyName)
   at System.Compiler.Writer.WritePE(CompilerParameters compilerParameters, Module module)
   at ILMerging.ILMerge.Merge()
   at ILMerging.ILMerge.Main(String[] args)

Note that this started happening only after I reset my Windows install. Everything else seems to be working fine, I can build other projects without problems. Only ILMerge seems to be having issues.

What could be causing this? I very much doubt that System.Configuration is a program with an incorrect format... Nothing would be working if it was.

thomaslevesque commented 6 years ago

Still having this problem. It only happens on one machine. Any idea, besides a full Windows reinstall?

thomaslevesque commented 6 years ago

I just realized that the error disappears if I run from an elevated process. What could it mean?

jnm2 commented 6 years ago

This was fixed in https://github.com/Microsoft/ILMerge/pull/3.

thomaslevesque commented 6 years ago

Thanks @jnm, but there has been no release since 2014... Will this fix ever be released ?

thomaslevesque commented 5 years ago

FWIW, I just realized the reason why it happened. I no longer had write permission on C:\Users\All Users\Microsoft\Crypto\RSA\MachineKeys. When I added the permission back, it started working again.