forked from icsharpcode/ILSpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadyToRunLanguage.cs
193 lines (176 loc) · 7.45 KB
/
ReadyToRunLanguage.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
// Copyright (c) 2018 Siegfried Pammer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Runtime.CompilerServices;
using Iced.Intel;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.Solution;
using ICSharpCode.Decompiler.TypeSystem;
using ILCompiler.Reflection.ReadyToRun;
namespace ICSharpCode.ILSpy.ReadyToRun
{
[Export(typeof(Language))]
internal class ReadyToRunLanguage : Language
{
private static readonly ConditionalWeakTable<PEFile, ReadyToRunReaderCacheEntry> readyToRunReaders = new ConditionalWeakTable<PEFile, ReadyToRunReaderCacheEntry>();
public override string Name => "ReadyToRun";
public override string FileExtension {
get { return ".asm"; }
}
public override ProjectId DecompileAssembly(LoadedAssembly assembly, ITextOutput output, DecompilationOptions options)
{
PEFile module = assembly.GetPEFileOrNull();
ReadyToRunReaderCacheEntry cacheEntry = GetReader(assembly, module);
if (cacheEntry.readyToRunReader == null) {
WriteCommentLine(output, cacheEntry.failureReason);
} else {
ReadyToRunReader reader = cacheEntry.readyToRunReader;
WriteCommentLine(output, reader.Machine.ToString());
WriteCommentLine(output, reader.OperatingSystem.ToString());
WriteCommentLine(output, reader.CompilerIdentifier);
WriteCommentLine(output, "TODO - display more header information");
}
return base.DecompileAssembly(assembly, output, options);
}
public override void DecompileMethod(IMethod method, ITextOutput output, DecompilationOptions options)
{
PEFile module = method.ParentModule.PEFile;
ReadyToRunReaderCacheEntry cacheEntry = GetReader(module.GetLoadedAssembly(), module);
if (cacheEntry.readyToRunReader == null) {
WriteCommentLine(output, cacheEntry.failureReason);
} else {
ReadyToRunReader reader = cacheEntry.readyToRunReader;
int bitness = -1;
if (reader.Machine == Machine.Amd64) {
bitness = 64;
} else {
Debug.Assert(reader.Machine == Machine.I386);
bitness = 32;
}
foreach (ReadyToRunMethod readyToRunMethod in reader.Methods) {
if (readyToRunMethod.MethodHandle == method.MetadataToken) {
// TODO: Indexing
foreach (RuntimeFunction runtimeFunction in readyToRunMethod.RuntimeFunctions) {
WriteCommentLine(output, readyToRunMethod.SignatureString);
byte[] code = new byte[runtimeFunction.Size];
for (int i = 0; i < runtimeFunction.Size; i++) {
code[i] = reader.Image[reader.GetOffset(runtimeFunction.StartAddress) + i];
}
Disassemble(output, code, bitness, (ulong)runtimeFunction.StartAddress);
output.WriteLine();
}
}
}
}
}
public override void WriteCommentLine(ITextOutput output, string comment)
{
output.WriteLine("; " + comment);
}
private void Disassemble(ITextOutput output, byte[] codeBytes, int bitness, ulong address)
{
// TODO: Decorate the disassembly with Unwind, GC and debug info
var codeReader = new ByteArrayCodeReader(codeBytes);
var decoder = Decoder.Create(bitness, codeReader);
decoder.IP = address;
ulong endRip = decoder.IP + (uint)codeBytes.Length;
var instructions = new InstructionList();
while (decoder.IP < endRip) {
decoder.Decode(out instructions.AllocUninitializedElement());
}
string disassemblyFormat = ReadyToRunOptions.GetDisassemblyFormat(null);
Formatter formatter = null;
if (disassemblyFormat.Equals(ReadyToRunOptions.intel)) {
formatter = new NasmFormatter();
} else {
Debug.Assert(disassemblyFormat.Equals(ReadyToRunOptions.gas));
formatter = new GasFormatter();
}
formatter.Options.DigitSeparator = "`";
formatter.Options.FirstOperandCharIndex = 10;
var tempOutput = new StringBuilderFormatterOutput();
foreach (var instr in instructions) {
formatter.Format(instr, tempOutput);
output.Write(instr.IP.ToString("X16"));
output.Write(" ");
int instrLen = instr.ByteLength;
int byteBaseIndex = (int)(instr.IP - address);
for (int i = 0; i < instrLen; i++)
output.Write(codeBytes[byteBaseIndex + i].ToString("X2"));
int missingBytes = 10 - instrLen;
for (int i = 0; i < missingBytes; i++)
output.Write(" ");
output.Write(" ");
output.WriteLine(tempOutput.ToStringAndReset());
}
}
private ReadyToRunReaderCacheEntry GetReader(LoadedAssembly assembly, PEFile module)
{
ReadyToRunReaderCacheEntry result;
lock (readyToRunReaders) {
if (!readyToRunReaders.TryGetValue(module, out result)) {
result = new ReadyToRunReaderCacheEntry();
try {
// TODO: avoid eager parsing
result.readyToRunReader = new ReadyToRunReader(new ReadyToRunAssemblyResolver(assembly), module.Metadata, module.Reader, module.FileName);
if (result.readyToRunReader.Machine != Machine.Amd64 && result.readyToRunReader.Machine != Machine.I386) {
result.failureReason = $"Architecture {result.readyToRunReader.Machine} is not currently supported.";
result.readyToRunReader = null;
}
} catch (BadImageFormatException e) {
result.failureReason = e.Message;
}
readyToRunReaders.Add(module, result);
}
}
return result;
}
private class ReadyToRunAssemblyResolver : ILCompiler.Reflection.ReadyToRun.IAssemblyResolver
{
private LoadedAssembly loadedAssembly;
public ReadyToRunAssemblyResolver(LoadedAssembly loadedAssembly)
{
this.loadedAssembly = loadedAssembly;
}
public bool Naked => false;
public bool SignatureBinary => false;
public bool InlineSignatureBinary => false;
public MetadataReader FindAssembly(MetadataReader metadataReader, AssemblyReferenceHandle assemblyReferenceHandle, string parentFile)
{
LoadedAssembly loadedAssembly = this.loadedAssembly.LookupReferencedAssembly(new Decompiler.Metadata.AssemblyReference(metadataReader, assemblyReferenceHandle));
return loadedAssembly?.GetPEFileOrNull()?.Metadata;
}
public MetadataReader FindAssembly(string simpleName, string parentFile)
{
// This is called only for the composite R2R scenario,
// So it will never be called before the feature is released.
throw new NotSupportedException("Composite R2R format is not currently supported");
}
}
private class ReadyToRunReaderCacheEntry
{
public ReadyToRunReader readyToRunReader;
public string failureReason;
}
}
}