Skip to content

Commit c686454

Browse files
committed
* Initial version 0.1 of decompiler sources.
0 parents  commit c686454

1,115 files changed

Lines changed: 115572 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎AUTHORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
John K�ll�n

‎COPYING‎

Lines changed: 340 additions & 0 deletions
Large diffs are not rendered by default.

‎INSTALL‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<TODO>

‎NEWS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

‎README‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
decompiler - a general purpose decompiler.
2+
3+
Decompiler is a C# project containing a decompiler for machine code
4+
binaries. This project is freely available under the GNU General
5+
Public License.
6+
7+
The project consists of front ends, core decompiler engine, and back
8+
ends to help it achieve its goals. A command-line, a Windows GUI,
9+
and a ASP.NET front end exist at the time of writing. The decompiler
10+
engine receives inputs from the front ends in the form of either
11+
individual executable files or decompiler project files. Decompiler
12+
project files contain additional information about a binary file,
13+
helpful to the decompilation process or for formatting the output.
14+
The decompiler engine then proceeds to analyze the source code.
15+
16+
Please note that many software licenses prohibit decompilation or
17+
other reverse engineering of their machine code binaries. Use this
18+
decompiler only if you have legal rights to decompiler the binary
19+
(for instance if the binary is your own.)
20+

‎TODO‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
The current version of decompiler is quite crude and could use assistance.
2+
3+
* Support for other processor architectures and platforms. Currently the decompiler
4+
is targeted mostly at x86 MS-DOS and Win32 binaries. Support for other
5+
processors (Motorola 68K, PDP-11, &c) and environments (MacOS, Windows 3.x, OS/2 &c)
6+
is desired.
7+
8+
* GUI: the work on a useful GUI hasn't left the planning stages; ideas about
9+
mouse/keyboard interaction to afford a convenient user interface are still mulled.
10+
11+
* Porting to other environments: a Mono/GTK# port is in the works.

‎src/AUTHORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
John K�ll�n
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
* Copyright (C) 1999-2007 John Källén.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2, or (at your option)
7+
* any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; see the file COPYING. If not, write to
16+
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17+
*/
18+
19+
using Decompiler.Core;
20+
using Decompiler.Core.Types;
21+
using System;
22+
using System.Diagnostics;
23+
using System.IO;
24+
25+
namespace Decompiler.Arch.Intel.Assembler
26+
{
27+
/// <summary>
28+
/// Emits bytes into a bytestream belonging to a segment/section.
29+
/// </summary>
30+
public class Emitter
31+
{
32+
private IntelRegister segOverride;
33+
private PrimitiveType dataWidthSeg; // Default data width for this segment.
34+
private PrimitiveType addrWidthSeg; // Default address width for this segment.
35+
private PrimitiveType addrWidth; // width of the address of this opcode.
36+
37+
private MemoryStream stmOut = new MemoryStream();
38+
39+
public Emitter()
40+
{
41+
segOverride = Registers.None;
42+
}
43+
44+
public PrimitiveType AddressWidth
45+
{
46+
get { return addrWidth; }
47+
set { addrWidth = value; }
48+
}
49+
50+
public byte [] Bytes
51+
{
52+
get { return stmOut.ToArray(); }
53+
}
54+
55+
56+
57+
public void EmitByte(int b)
58+
{
59+
stmOut.WriteByte((byte) b);
60+
}
61+
62+
public void EmitBytes(int b, int count)
63+
{
64+
byte by = (byte) b;
65+
for (int i = 0; i < count; ++i)
66+
{
67+
stmOut.WriteByte(by);
68+
}
69+
}
70+
71+
public void EmitDword(uint l)
72+
{
73+
stmOut.WriteByte((byte)(l));
74+
l >>= 8;
75+
stmOut.WriteByte((byte)(l));
76+
l >>= 8;
77+
stmOut.WriteByte((byte)(l));
78+
l >>= 8;
79+
stmOut.WriteByte((byte)(l));
80+
}
81+
82+
public void EmitImmediate(Value v, PrimitiveType w)
83+
{
84+
switch (w.Size)
85+
{
86+
case 1: EmitByte(v.Byte); return;
87+
case 2: EmitWord(v.Word); return;
88+
case 4: EmitDword(v.Unsigned); return;
89+
}
90+
}
91+
92+
public void EmitInteger(PrimitiveType vt, int v)
93+
{
94+
switch (vt.Size)
95+
{
96+
case 1: EmitByte(v); return;
97+
case 2: EmitWord(v); return;
98+
case 4: EmitDword((uint) v); return;
99+
default: throw new ArgumentException();
100+
}
101+
}
102+
103+
public void EmitOpcode(int b, PrimitiveType dataWidth)
104+
{
105+
if (SegmentOverride != Registers.None)
106+
{
107+
byte bOv;
108+
if (SegmentOverride == Registers.es) bOv = 0x26; else
109+
if (SegmentOverride == Registers.cs) bOv = 0x2E; else
110+
if (SegmentOverride == Registers.ss) bOv = 0x36; else
111+
if (SegmentOverride == Registers.ds) bOv = 0x3E; else
112+
if (SegmentOverride == Registers.fs) bOv = 0x64; else
113+
if (SegmentOverride == Registers.gs) bOv = 0x65; else
114+
throw new ArgumentOutOfRangeException("Invalid segment register: " + SegmentOverride);
115+
EmitByte(bOv);
116+
SegmentOverride = Registers.None;
117+
}
118+
if (IsDataWidthOverridden(dataWidth))
119+
{
120+
EmitByte(0x66);
121+
}
122+
if (AddressWidth != null && AddressWidth != SegmentAddressWidth)
123+
{
124+
EmitByte(0x67);
125+
}
126+
EmitByte(b);
127+
}
128+
129+
public void EmitString(string pstr)
130+
{
131+
for (int i = 0; i != pstr.Length; ++i)
132+
{
133+
EmitByte(pstr[i]);
134+
}
135+
}
136+
137+
public void EmitWord(int s)
138+
{
139+
stmOut.WriteByte((byte)(s & 0xFF));
140+
stmOut.WriteByte((byte)(s >> 8));
141+
}
142+
143+
private bool IsDataWidthOverridden(PrimitiveType dataWidth)
144+
{
145+
return dataWidth != null &&
146+
dataWidth.Domain != Domain.Real &&
147+
dataWidth.Size != 1 &&
148+
dataWidth != SegmentDataWidth;
149+
}
150+
151+
152+
public int Length
153+
{
154+
get { return (int) stmOut.Length; }
155+
}
156+
157+
/// <summary>
158+
/// Patches a value by fetching it from the stream and adding an offset.
159+
/// </summary>
160+
/// <param name="offsetPatch"></param>
161+
/// <param name="offsetRef"></param>
162+
/// <param name="width"></param>
163+
public void Patch(int offsetPatch, int offsetRef, PrimitiveType width)
164+
{
165+
Debug.Assert(offsetPatch < stmOut.Length);
166+
long posOrig = stmOut.Position;
167+
stmOut.Position = offsetPatch;
168+
int patchVal;
169+
switch (width.Size)
170+
{
171+
default:
172+
throw new ApplicationException("unexpected");
173+
case 1:
174+
patchVal = stmOut.ReadByte() + offsetRef;
175+
stmOut.Position = offsetPatch;
176+
stmOut.WriteByte((byte) patchVal);
177+
break;
178+
case 2:
179+
patchVal = stmOut.ReadByte();
180+
patchVal |= stmOut.ReadByte() << 8;
181+
patchVal += offsetRef;
182+
stmOut.Position = offsetPatch;
183+
stmOut.WriteByte((byte) patchVal);
184+
stmOut.WriteByte((byte) (patchVal >> 8));
185+
break;
186+
case 4:
187+
patchVal = stmOut.ReadByte();
188+
patchVal |= stmOut.ReadByte() << 8;
189+
patchVal |= stmOut.ReadByte() << 16;
190+
patchVal |= stmOut.ReadByte() << 24;
191+
patchVal += offsetRef;
192+
stmOut.Position = offsetPatch;
193+
stmOut.WriteByte((byte) patchVal);
194+
stmOut.WriteByte((byte) (patchVal >>= 8));
195+
stmOut.WriteByte((byte) (patchVal >>= 8));
196+
stmOut.WriteByte((byte) (patchVal >>= 8));
197+
break;
198+
}
199+
stmOut.Position = posOrig;
200+
}
201+
202+
203+
public int Position
204+
{
205+
get { return (int) stmOut.Position; }
206+
}
207+
208+
public PrimitiveType SegmentAddressWidth
209+
{
210+
get { return addrWidthSeg; }
211+
set { addrWidthSeg = value; }
212+
}
213+
214+
public PrimitiveType SegmentDataWidth
215+
{
216+
get { return dataWidthSeg; }
217+
set { dataWidthSeg = value; }
218+
}
219+
220+
public IntelRegister SegmentOverride
221+
{
222+
get { return segOverride; }
223+
set { segOverride = value; }
224+
}
225+
}
226+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 1999-2007 John Källén.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2, or (at your option)
7+
* any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; see the file COPYING. If not, write to
16+
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17+
*/
18+
19+
using System;
20+
21+
namespace Decompiler.Arch.Intel.Assembler
22+
{
23+
public delegate void ErrorEventHandler(object sender, ErrorEventArgs args);
24+
25+
/// <summary>
26+
/// Encapsulates information about an assembler error.
27+
/// </summary>
28+
public class ErrorEventArgs
29+
{
30+
private string message;
31+
private int lineNumber;
32+
33+
public ErrorEventArgs(string message)
34+
{
35+
this.message = message;
36+
}
37+
38+
public ErrorEventArgs(int lineNumber, string message)
39+
{
40+
this.message = message;
41+
this.lineNumber = lineNumber;
42+
}
43+
44+
public int LineNumber
45+
{
46+
get { return lineNumber; }
47+
set { lineNumber = value; }
48+
}
49+
50+
public string Message
51+
{
52+
get { return message; }
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)