3

My question is simple: For what architecture does the .net compiler target the resulting exe? I refer to the actual exe-file, not the CLR that is loaded by the binary to execute the actual code itself. And there has to be a target cpu architecture, as the PE-Header of executables can't be platform agnostic (to my knowledge).

Why does this binary work everytime especially when looking at x64 vs IA64?

Edit: I just looked at the hex code of some x86, x64 and AnyCPU builds. For x86 and AnyCPU the target is x86, for x64 it is x64.

Now remain two questions: How does the AnyCPU change it's target architecture at runtime?? Because the PE-Header clearly says it is targeted for x86, but the program then launches as a x64 binary.

And secondly, how can a x64 exe throw a BadImageFormatException (which is clearly a CLR thing), when it can't even be executed?

Edit 2: https://msdn.microsoft.com/en-us/library/8dkk3ek4(v=vs.80).aspx describes some details that only confuse me further. It states, that the CLR reads PE-Files to run them, which makes sense. It would explain why a x86 PE-File can be launched as a x64 process.

But thats about it. How does the CLR launch with the correct process name, binary path and stuff? If the PE launches & hosts the CLR, that would be explained, but the x86-x64 change would be a mystery again. But nevertheless the BadImageFormatException is something I can't explain with those information.

CShark
  • 1,413
  • 15
  • 25
  • There's nothing special. All Windows programs are built for specific architectures. .NET is no exception. You get *different* binaries if you target 64-bit. x86 binaries run just fine on x64, which is why an x86application runs on x64. – Panagiotis Kanavos Jun 12 '17 at 09:13
  • And there's no IA64. It's officially dead. The Itanium chips never took off. I don't remember .NET ever targetting them – Panagiotis Kanavos Jun 12 '17 at 09:15
  • So does the PE-referred platform change with the target architecture? Does it always have x86? In the first case, what happens when I have an assembly with multiple target architectures? And IA64 was supported by .NET 2.0, so there has to be a solution for that platform. – CShark Jun 12 '17 at 09:16
  • @PanagiotisKanavos: I can set the [platform target](https://learn.microsoft.com/en-us/visualstudio/ide/reference/build-page-project-designer-csharp) of my .NET console exe to "Any CPU" (`/platform:anycpu`) which causes it to run as a 32-bit appliation on 32-bit systems and as a 64-bit application on 64-bit systems. Still, I have only one EXE. – Heinzi Jun 12 '17 at 09:17
  • If you want specifics, you should check MSDN or "Windows Internals" or "CLR Internals". – Panagiotis Kanavos Jun 12 '17 at 09:18
  • On a sidenode: I don't know exactly whether the target architecture is stored in the modules or in the assembly manifest, so that last question might be totally idiotic. – CShark Jun 12 '17 at 09:18
  • @Heinzi that is done by dynamically loading the right CLR from the exe, from there everything is kinda clear for me. But right before that is what interrests me. – CShark Jun 12 '17 at 09:19
  • You can also check the Wikipedia article which explains how this works, especially the section on [.NET, Metadata and the PE Format](https://en.wikipedia.org/wiki/Portable_Executable#.NET.2C_metadata.2C_and_the_PE_format). – Panagiotis Kanavos Jun 12 '17 at 09:21
  • @PanagiotisKanavos: That only explains the later part, which is clear to CShark (and to me as well). The real question is: How do they create a single EXE which is a x86 binary on x86 systems, a x64 binary on x64 systems and an IA64 binary on IA64 systems? (And if it isn't, what is it?) – Heinzi Jun 12 '17 at 09:23
  • But on an interesting side note: .net 4.7 atleast seems to execute AnyCPU as wow64, according to process hacker. Maybe for compatibility or something like that(?) which seems stupid imo. – CShark Jun 12 '17 at 09:25
  • 1
    It's a PE file. Not a x64 or x86 executable. A PE file contains executables. The Wikipedia article explains all this. With .NET, that PE file contains a stub that picks the proper runtime to compile and run the IL payload – Panagiotis Kanavos Jun 12 '17 at 09:29
  • Yeah, but a PE-file has to target a specific architecture, it can't be platform agnostic. So which architecture does it target? – CShark Jun 12 '17 at 09:33
  • @CShark: AnyCPU runs on WOW64 only if **Prefer 32-bit** option is enabled (in Visual Studio go to project properties -> build tab -> Prefer 32-bit), otherwise it runs as normal 64-bit process. – Ňuf Jun 12 '17 at 12:20
  • @Panagiotis Kanavos Are you saying that that the architecture within the PE header might only reflect the PE stub code? Then, once the stub finishes, the module can then become x86 or x64 depending on various other factors? – byteptr Jun 14 '17 at 21:09
  • No, I'm saying that you should read the books, spec, MSDN, Wikipedia article. You didn't have to check the binary when all this is very, *very* well documented and explained. Few people remember all the details because it *doesn't matter*. It works, and the docs are readily available when needed. – Panagiotis Kanavos Jun 15 '17 at 07:08
  • Of course it doesn't matter, it just works. Nevertheless it interrests me how it works. Wikipedia only tells me stuff I already know / have not much to do with my question. MSDN only starts with the CLR freshly launched, but I could not find anything about the stuff before that. I also don't have access to those books and won't buy them just for my interests sake - they're too expensive. if it is well documented (online), I don't know where to look. That's why I asked. And maybe someone already knows the answer. – CShark Jun 15 '17 at 08:51
  • Possible duplicate of [How does CorFlags.exe /32BIT+ work?](https://stackoverflow.com/questions/10389756/how-does-corflags-exe-32bit-work) – Hans Passant Jun 15 '17 at 10:45
  • @HansPassant The accepted answer and linked resources explain pretty much what is going on, but the question is different, so I don't think this really counts as a duplicate question per se. If you create an answer and link to the mentioned question / recap the resources, I will accept it. – CShark Jun 15 '17 at 11:00
  • I have completely no idea how this question is different so could not possibly post a different answer. You'll have to wait for somebody that can see it. – Hans Passant Jun 15 '17 at 12:01

0 Answers0