-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisassembler.cs
279 lines (258 loc) · 12.5 KB
/
Disassembler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System.Buffers.Binary;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
namespace AssEmbly
{
public record DisassemblerOptions(
bool DetectStrings = true,
bool DetectPads = true,
bool DetectFloats = true,
bool DetectSigned = true,
bool AllowFullyQualifiedBaseOpcodes = false);
public static class Disassembler
{
/// <summary>
/// Disassemble a program to AssEmbly code from it's assembled bytecode.
/// </summary>
public static string DisassembleProgram(byte[] program, DisassemblerOptions options)
{
ulong offset = 0;
List<string> result = new();
Dictionary<ulong, int> offsetToLine = new();
// address -> referencing instruction start offset
Dictionary<ulong, List<ulong>> addressReferences = new();
// %DAT insertions that correspond to ASCII values awaiting insertion as a full string
List<byte> pendingStringCharacters = new();
// The number of chained "%DAT 0" directives awaiting merge into a single %PAD
int pendingZeroPad = 0;
while (offset < (ulong)program.LongLength)
{
(string line, ulong additionalOffset, List<ulong> referencedAddresses, bool datFallback) =
DisassembleInstruction(new ReadOnlySpan<byte>(program)[(int)offset..], options, true);
foreach (ulong address in referencedAddresses)
{
_ = addressReferences.TryAdd(address, new List<ulong>());
addressReferences[address].Add(offset);
}
if (options.DetectStrings
&& char.IsAscii((char)program[offset]) && !char.IsControl((char)program[offset]) && datFallback)
{
DumpPendingZeroPad(ref pendingZeroPad, result, offset, offsetToLine);
pendingStringCharacters.Add(program[offset]);
}
else if (options.DetectPads && program[offset] == 0 && additionalOffset == 1)
{
DumpPendingString(pendingStringCharacters, result, offset, offsetToLine);
pendingZeroPad++;
}
else
{
DumpPendingString(pendingStringCharacters, result, offset, offsetToLine);
DumpPendingZeroPad(ref pendingZeroPad, result, offset, offsetToLine);
offsetToLine[offset] = result.Count;
result.Add(line);
}
offset += additionalOffset;
}
DumpPendingString(pendingStringCharacters, result, offset, offsetToLine);
DumpPendingZeroPad(ref pendingZeroPad, result, offset, offsetToLine);
// Insert label definitions
foreach ((ulong address, List<ulong> startOffsets) in addressReferences)
{
if (offsetToLine.TryGetValue(address, out int destLineIndex))
{
result[destLineIndex] = $":ADDR_{address:X}{Environment.NewLine}" + result[destLineIndex];
}
else
{
foreach (ulong start in startOffsets)
{
if (offsetToLine.TryGetValue(start, out int lineIndex))
{
result[lineIndex] = result[lineIndex].Replace($":ADDR_{address:X}", $":0x{address:X}") + " ; Address does not align to a disassembled instruction";
}
}
}
}
return string.Join(Environment.NewLine, result);
}
/// <summary>
/// Disassemble a single line of AssEmbly code from it's assembled bytecode.
/// </summary>
/// <param name="instruction">The instruction to disassemble. More bytes than needed may be given.</param>
/// <param name="useLabelNames">
/// Whether address references should be written as literal addresses, or as label names generated from those addresses.
/// </param>
/// <returns>(Disassembled line, Number of bytes instruction was, Referenced addresses [if present], Used %DAT directive)</returns>
public static (string Line, ulong AdditionalOffset, List<ulong> References, bool DatFallback) DisassembleInstruction(
ReadOnlySpan<byte> instruction, DisassemblerOptions options, bool useLabelNames)
{
if (instruction.Length == 0)
{
return ("", 0, new List<ulong>(), false);
}
bool fallbackToDat = false;
ulong totalBytes = 0;
Opcode opcode;
if (instruction[0] == Opcode.FullyQualifiedMarker && instruction.Length < 3)
{
// We can't parse this data as an opcode properly,
// as it starts with 0xFF but there are not enough bytes for it to be a fully qualified opcode.
// Can happen with non-instruction statements like "%DAT 0xFF".
fallbackToDat = true;
totalBytes = 1;
opcode = default;
}
else
{
opcode = Opcode.ParseBytes(instruction, ref totalBytes);
totalBytes++;
}
if (!fallbackToDat && !options.AllowFullyQualifiedBaseOpcodes
&& instruction[0] == Opcode.FullyQualifiedMarker && instruction[1] == 0x00)
{
// Opcode is fully qualified but is for the base instruction set
// - technically valid but never done by the assembler, so interpret as data.
// Will ensure that a re-assembly of the disassembled program remains byte-perfect.
fallbackToDat = true;
}
if (!fallbackToDat && Data.MnemonicsReverse.TryGetValue(opcode, out (string Mnemonic, OperandType[] OperandTypes) matching))
{
(string mnemonic, OperandType[] operandTypes) = matching;
List<string> operandStrings = new();
List<ulong> referencedAddresses = new();
foreach (OperandType type in operandTypes)
{
if (totalBytes >= (uint)instruction.Length)
{
fallbackToDat = true;
break;
}
switch (type)
{
case OperandType.Register:
if (Enum.IsDefined((Register)instruction[(int)totalBytes]))
{
operandStrings.Add(((Register)instruction[(int)totalBytes]).ToString());
totalBytes++;
}
else
{
fallbackToDat = true;
}
break;
case OperandType.Literal:
if (totalBytes + 8 > (uint)instruction.Length)
{
fallbackToDat = true;
break;
}
ulong value;
if (options.DetectFloats)
{
double floatingValue = BinaryPrimitives.ReadDoubleLittleEndian(instruction[(int)totalBytes..]);
if (Math.Abs(floatingValue) is >= 0.0000000000000001 and <= ulong.MaxValue)
{
operandStrings.Add(floatingValue.ToString(".0###############"));
totalBytes += 8;
break;
}
value = BitConverter.DoubleToUInt64Bits(floatingValue);
}
else
{
value = BinaryPrimitives.ReadUInt64LittleEndian(instruction[(int)totalBytes..]);
}
if (options.DetectSigned && value > long.MaxValue)
{
operandStrings.Add(unchecked((long)value).ToString());
}
else
{
operandStrings.Add(value.ToString());
}
totalBytes += 8;
break;
case OperandType.Address:
if (totalBytes + 8 > (uint)instruction.Length)
{
fallbackToDat = true;
break;
}
referencedAddresses.Add(BinaryPrimitives.ReadUInt64LittleEndian(instruction[(int)totalBytes..]));
operandStrings.Add(useLabelNames ? $":ADDR_{referencedAddresses[^1]:X}" : $":0x{referencedAddresses[^1]:X}");
totalBytes += 8;
break;
case OperandType.Pointer:
#if DISPLACEMENT
DisplacementMode mode = Pointer.GetDisplacementMode(instruction[(int)totalBytes]);
if (mode.GetByteCount() + totalBytes >= (ulong)instruction.Length)
{
fallbackToDat = true;
break;
}
Pointer pointer = new(instruction[(int)totalBytes..], out ulong pointerBytes);
if (!Enum.IsDefined(pointer.Mode) || !Enum.IsDefined(pointer.PointerRegister)
|| !Enum.IsDefined(pointer.OtherRegister) || !Enum.IsDefined(pointer.ReadSize)
|| !Enum.IsDefined(pointer.OtherRegisterMultiplier))
{
fallbackToDat = true;
break;
}
operandStrings.Add(pointer.ToString());
totalBytes += pointerBytes;
#else
if (Enum.IsDefined((Register)instruction[(int)totalBytes]))
{
operandStrings.Add("*" + (Register)instruction[(int)totalBytes]);
totalBytes++;
}
else
{
fallbackToDat = true;
}
#endif
break;
default:
fallbackToDat = true;
break;
}
if (fallbackToDat)
{
break;
}
}
if (!fallbackToDat)
{
return ($"{mnemonic} {string.Join(", ", operandStrings)}".Trim(), totalBytes, referencedAddresses, false);
}
}
return ($"%DAT {instruction[0]}", 1, new List<ulong>(), true);
}
private static void DumpPendingString(List<byte> pendingStringCharacters, List<string> result, ulong offset, Dictionary<ulong, int> offsetToLine)
{
if (pendingStringCharacters.Count > 0)
{
string newString = Encoding.ASCII.GetString(CollectionsMarshal.AsSpan(pendingStringCharacters));
offsetToLine[offset - (ulong)pendingStringCharacters.Count] = result.Count;
result.Add($"%DAT \"{newString.EscapeCharacters()}\"");
pendingStringCharacters.Clear();
}
}
private static void DumpPendingZeroPad(ref int pendingZeroPad, List<string> result, ulong offset, Dictionary<ulong, int> offsetToLine)
{
if (pendingZeroPad > 0)
{
offsetToLine[offset - (ulong)pendingZeroPad] = result.Count;
result.Add("HLT");
if (pendingZeroPad > 1)
{
offsetToLine[offset - (ulong)pendingZeroPad + 1] = result.Count;
result.Add($"%PAD {pendingZeroPad - 1}");
}
pendingZeroPad = 0;
}
}
}
}