MSIL to Machine Langague Code

  • Thread starter Thread starter ARVIND LAKRA
  • Start date Start date
A

ARVIND LAKRA

I am looking forward for converting MSIL or exe or dll genrated
by .Net framwrok into machine level or low level instruction set.
It can be looked as obtaining the same instruction set that
unmanaged(c
++) code genrate .
As we can get all machine level instruction set from C++ genrated exe
along with PDb file associated by using various tools. Tools like IDA
Pro , Zynamics , etc.
I want to know , is there a way to do the same for .net exe or
libraries.
 
Hi, Avrind,

It depends on how strictly you see this.

First, the .NET Framework implementations convert the CIL code into
machine code while executing (this is called JIT-compiling).

Second, some framework implementations come with tools like "ngen.exe"
or "mono --aot" which allow the user to compile the code to native
code ahead of time. But there is no obligation for frameworks to
provide this (e. G. the .NET compact framework has no tool for ahead
of time compilation), and this code still needs the framework itsself
as runtime environment, so the code will still work in the managed
environment.

What exactly is your requirement, why do you need native code
compilation?

HTH,
Markus
 
Hi, Avrind,

It depends on how strictly you see this.

First, the .NET Framework implementations convert the CIL code into
machine code while executing (this is called JIT-compiling).

Second, some framework implementations come with tools like "ngen.exe"
or "mono --aot" which allow the user to compile the code to native
code ahead of time. But there is no obligation for frameworks to
provide this (e. G. the .NET compact framework has no tool for ahead
of time compilation), and this code still needs the framework itsself
as runtime environment, so the code will still work in the managed
environment.

What exactly is your requirement, why do you need native code
compilation?

HTH,
Markus



- Show quoted text -

Hi Markus
Thanks for the reply.

Actually , I want the native code to do the following things :-
i want to know and automate the flow of any project . I have done this
for C++. For eg:- If we create any project in C++ or VC++ , we get exe
or dll and pdb. Now using these files , it can be converted into
machine instructions and their flow can be determined . Also ,
analying the machine code instructions can help us to optimise the
project .

Now the following thing have been completed for C++ . Now my next
target is .Net Framework . Since everything i have done till now
depends on machine level instruction set, i need same for any binary
of .Net framework.

Now , as u told ngen , i have done reasearch on that but actually in
reality it doesnt create the native code. it create a exe with
different name and assosciate all symbols somewhere else in windows
folder. So , ngen.exe is not of much use.

So , i want to know whether there is a way to obtain a exe from .net
exe independent of dependecy of .net framework.
it should be capable enough to run standalone. and if thats possible ,
then it means , it has stored instruction at machine level or you can
say assembly level. then i can do my work as i have done for c++
else , i would have to make completely new project for .Net framework

With Regards,
Arvind
 
Hi Markus
Thanks for the reply.

Actually , I want the native code to do the following things :-
i want to know and automate the flow of any project . I have done this
for C++. For eg:- If we create any project in C++ or VC++ , we get exe
or dll and pdb. Now using these files , it can be converted into
machine instructions and their flow can be determined . Also ,
analying the machine code instructions can help us to optimise the
project .

Now the following thing have been completed for C++ . Now my next
target is .Net Framework . Since everything i have done till now
depends on machine level instruction set, i need same for any binary
of .Net framework.

Now , as u told ngen ,  i have done reasearch on that but actually in
reality it doesnt create the native code. it create a exe with
different name and assosciate all symbols somewhere else in windows
folder. So , ngen.exe is not of much use.

So , i want to know whether there is a way to obtain a exe from .net
exe independent of dependecy of .net framework.
it should be capable enough to run standalone. and if thats possible ,
then it means , it has stored instruction at machine level or you can
say assembly level. then i can do my work as i have done for c++
else , i would have to make completely new project for .Net framework

With Regards,
Arvind- Hide quoted text -

- Show quoted text -

hello ,

i ma looking for something like this ... as it is shown here in this
link
http://forums.asp.net/t/1522409.aspx
 
hello ,

i ma looking for something like this ... as it is shown here in this
linkhttp://forums.asp.net/t/1522409.aspx- Hide quoted text -

- Show quoted text -

hi ,
i am looking generate this corresponding Assembly Code for complete
EXE with all the symbols resolved. I know that machine code is
generated by JIT compiler when the EXE is run, but for my purpose I
need the assembly code for complete EXE.

eg:-
C# Code:

static void Main(String[] args)
{
int Pick = 1;

switch (Pick)
{
case 0:
Console.WriteLine("You picked 0");
break;
case 1:
Console.WriteLine("You picked 1");
break;
}

C# and VB are both converted to IL:

..method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 98 (0x62)
.maxstack 2
.locals init ([0] int32 Pick,
[1] int32 CS$4$0000,
[2] bool CS$4$0001)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: stloc.1
IL_0005: ldloc.1
IL_0006: switch (
IL_0015,
IL_0022)
IL_0013: br.s IL_002f
IL_0015: ldstr "You picked 0"
IL_001a: call void
[mscorlib]System.Console::WriteLine(string)
IL_001f: nop
IL_0020: br.s IL_002f
IL_0022: ldstr "You picked 1"
IL_0027: call void
[mscorlib]System.Console::WriteLine(string)
IL_002c: nop
IL_002d: br.s IL_002f


IL is compiled to assembly (C# included for readability):

static void Main(String[] args)
{
00000000 mov qword ptr [rsp+8],rcx
00000005 sub rsp,68h
00000009 mov dword ptr [rsp+20h],0
00000011 mov dword ptr [rsp+24h],0
00000019 mov byte ptr [rsp+28h],0
0000001e mov rax,7FF001E1E00h
00000028 mov eax,dword ptr [rax]
0000002a test eax,eax
0000002c je 0000000000000033
0000002e call FFFFFFFFF0928280
00000033 nop
int Pick = 1;
00000034 mov dword ptr [rsp+20h],1

switch (Pick)
0000003c mov eax,dword ptr [rsp+20h]
00000040 mov dword ptr [rsp+24h],eax
00000044 mov eax,dword ptr [rsp+24h]
00000048 mov dword ptr [rsp+40h],eax
0000004c cmp dword ptr [rsp+40h],2
00000051 jae 0000000000000071
00000053 movsxd rcx,dword ptr [rsp+40h]
00000058 lea rax,[0000011Bh]
0000005f movsxd rcx,dword ptr [rax+rcx*4]
00000063 lea rax,[0000006Fh]
0000006a add rcx,rax
0000006d jmp rcx
0000006f jmp 000000000000006F
00000071 jmp 000000000000009D
{
case 0:
Console.WriteLine("You picked 0");
00000073 mov rcx,126E3100h
0000007d mov rcx,qword ptr [rcx]
00000080 call FFFFFFFFEF826100
00000085 nop
break;
00000086 jmp 000000000000009D
case 1:
Console.WriteLine("You picked 1");
00000088 mov rcx,126E3108h
00000092 mov rcx,qword ptr [rcx]
00000095 call FFFFFFFFEF826100
0000009a nop
break;
0000009b jmp 000000000000009D
}

With regards
Arvind
 
Hi, Avrind,

Actually , I want the native code to do the following things :-
i want to know and automate the flow of any project . I have done this
for C++. For eg:- If we create any project in C++ or VC++ , we get exe
or dll and pdb. Now using these files , it can be converted into
machine instructions and their flow can be determined . Also ,
analying the machine code instructions can help us to optimise the
project .

There already exist similar tools for .NET (like ReSharper has a data
flow analysis tool built in, which seems rather capable, it can even
follow calls across event subscriptions) and libraries like CeCil
which help to build one. So do you really have the need to build one
yourself?
Now the following thing have been completed for C++ . Now my next
target is .Net Framework . Since everything i have done till now
depends on machine level instruction set, i need same for any binary
of .Net framework.

I think the best way is to re-implement this for .NET by parsing the
IL using CeCil or a similar library.
Now , as u told ngen ,  i have done reasearch on that but actually in
reality it doesnt create the native code. it create a exe with
different name and assosciate all symbols somewhere else in windows
folder. So , ngen.exe is not of much use.

Ngen.exe in fact generates native code (ngen is short for Native
Generation), see http://msdn.microsoft.com/en-us/library/6t9t5wcf(v=VS.90).aspx:
"Ngen.exe creates native images, which are files containing compiled
processor-specific machine code, and installs them into the native
image cache on the local computer".
So , i want to know whether there is a way to obtain a exe from .net
exe independent of dependecy of .net framework.
it should be capable enough to run standalone. and if thats possible ,
then it means , it has stored instruction at machine level or you can
say assembly level. then i can do my work as i have done for c++
else , i would have to make completely new project for .Net framework

How do you think that creating a .EXE which is independent from .NET
framework could work? C# code heavily depends on the .NET class
libraries (which are delivered in CIL) and on the framework (e. G. for
class loading, garbage collection etc.). It is the same as, say, you
want a C++ compiler which removes the dependencies to the runtime
(like win32.dll and MFC) by compiling Windows specific C++ code to
native code.

Markus
 
Hi, Arvind,

Another question: How do you currently handle 64-Bit Code (IA65/
Itanium and AMD64/x86-64)? You already need different back-ends for
those binaries.

And how do you cope with the always appearing new opcodes (MMX / SSE /
3Dnow)?

Markus
 
Back
Top