简体   繁体   English

C#(MSIL)转换为原生X86代码?

[英]C# (MSIL) into native X86 code?

I would first like to say my goal is to convert MSIL into native X86 code. 我首先要说的是我的目标是将MSIL转换为本机X86代码。 I am fine with my assembly's still needing the .net framework installed. 我的程序集仍然需要安装.net框架,我很好。 NGEN is not what I want as you still need the original assembly's. NGEN不是我想要的,因为你仍然需要原装配件。

I came across ilasm , and what I am wondering is this what I want, will this make pure assembly code? 我遇到了ilasm ,我想知道这就是我想要的,这会产生纯粹的汇编代码吗?

I have looked at other projects like mono (which does not support some of the key features my app uses) and .net linkers but they simple just make a single EXE with the .net framework which is not what I am looking for. 我看过其他项目,比如mono(它不支持我的应用程序使用的一些关键功能)和.net链接器,但它们很简单,只需用.net框架创建一个EXE,这不是我想要的。

So far any research has come up with...you can't do it. 到目前为止,任何研究都已提出......你做不到。 I am really no sure as to why as the JIT does it when it loads the MSIL assembly. 我真的不确定为什么JIT在加载MSIL程序集时会这样做。 I have my own reasons for wanting this, so I guess my question(s) come down to this. 我有自己的理由想要这个,所以我想我的问题归结为此。

  1. Is the link I posted helpful in anyway? 我发布的链接是否有用?

  2. Is there anything out there that can turn MSIL into x86 assembly? 有什么可以将MSIL变成x86程序集吗?

There are various third-party code-protection packages available that hide the IL by encrypting it and packing it with a special bootloader that only unpacks it during runtime. 有各种第三方代码保护包可用于隐藏IL,并通过一个特殊的引导加载程序将其打包,该引导加载程序仅在运行时解包。 This might be an option if you're concerned about disassembly of your code, though most of these third-party packages are also already cracked (somewhat unavoidable, unfortunately.) Simple obfuscation may ultimately be just as effective, assuming this is your underlying goal. 如果您担心代码的反汇编,这可能是一个选项,尽管大多数这些第三方软件包也已经破解(遗憾的是,有些不可避免。)简单的混淆最终可能同样有效,假设这是您的基本目标。

One the major challenges associated with 'pre-jitting' the IL is that you end up including fixed address references in the native code. 与“预先评估”IL相关的主要挑战之一是您最终在本机代码中包含固定地址引用。 These in turn will need to be 're-based' when the native code is loaded for execution under the CLR. 当加载本机代码以在CLR下执行时,这些又需要“重新基础”。 This means you need more than just the logic that gets compiled; 这意味着您需要的不仅仅是编译的逻辑; you also need all of the reference context information necessary to rebase the fixed references when the code is loaded. 您还需要在加载代码时重新定义固定引用所需的所有引用上下文信息。 It's a lot more than just caching code. 它不仅仅是缓存代码。

As with most things, the first question should be why instead of how. 与大多数事情一样,第一个问题应该是为什么而不是如何。 I assume you have a specific goal in mind, if you want to generate native code yourself (also, why x86? Why not x64 too?). 我假设你有一个特定的目标,如果你想自己生成本机代码(同样,为什么x86?为什么不是x64呢?)。 This is the job of the JIT compiler - to compile an optimized instruction set on a particular platform only when needed, and execute it later. 这是JIT编译器的工作 - 仅在需要时在特定平台上编译优化指令集,并在以后执行。

The best source I can recommend to try and understand how the CLR works and how JIT works is taking a look at SSCLI - an implementation of the CLR based on the ECMA-335 spec. 我可以推荐尝试理解CLR如何工作以及JIT如何工作的最佳来源是查看SSCLI - 基于ECMA-335规范的CLR实现。

Have you considered not using C#? 你考虑过不使用C#吗? Given that the output of the C# compiler is MSIL, it would make sense to develop on a different platform if that is not what you want. 鉴于C#编译器的输出是MSIL,如果不是你想要的话,在不同的平台上开发是有意义的。

Alternatively it sounds like NGEN does the operation you are wanting, it just doesn't handle putting the entire thing into an executable. 或者,听起来NGEN会执行您想要的操作,它只是不处理将整个事物放入可执行文件中。 You could analyze the resultant NGEN image to determine what needs to be done to accomplish that (note that NGENed images are PE files per the documentation) 您可以分析生成的NGEN图像,以确定需要完成哪些操作(请注意,根据文档,NGENed图像是PE文件)

Here is a link on NGEN that contains information on where the images are stored: C:\\windows\\assembly\\NativeImages_CLR_Bit for instance C:\\windows\\assembly\\NativeImages_v2.0.50727_86 . 以下是NGEN的链接,其中包含有关图像存储位置的信息: C:\\windows\\assembly\\NativeImages_CLR_Bit ,例如C:\\windows\\assembly\\NativeImages_v2.0.50727_86 Note that .NET 3.0 and 3.5 are both part of 2.0. 请注意,.NET 3.0和3.5都是2.0的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM