-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #304 from ds5678/TrimmableDotNetProject
[Feature]: Trimmable DotNet Project
- Loading branch information
Showing
17 changed files
with
358 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/AsmResolver.DotNet.Dynamic/AsmResolver.DotNet.Dynamic.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Title>AsmResolver.DotNet.Dynamic</Title> | ||
<Description>Dynamic method support for the AsmResolver executable file inspection toolsuite.</Description> | ||
<PackageTags>exe pe directories imports exports resources dotnet cil inspection manipulation assembly disassembly dynamic</PackageTags> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<NoWarn>1701;1702;NU5105</NoWarn> | ||
<TargetFrameworks>net6.0;netcoreapp3.1;netstandard2.0</TargetFrameworks> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> | ||
<DocumentationFile>bin\Debug\AsmResolver.DotNet.xml</DocumentationFile> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DocumentationFile>bin\Release\AsmResolver.DotNet.xml</DocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\AsmResolver.DotNet\AsmResolver.DotNet.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Nullable" Version="1.3.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/AsmResolver.DotNet.Dynamic/DynamicMethodDefinition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using AsmResolver.DotNet.Code.Cil; | ||
using AsmResolver.DotNet.Serialized; | ||
using AsmResolver.DotNet.Signatures; | ||
using AsmResolver.DotNet.Signatures.Types; | ||
using AsmResolver.IO; | ||
using AsmResolver.PE.DotNet.Cil; | ||
using AsmResolver.PE.DotNet.Metadata.Tables; | ||
using MethodAttributes = AsmResolver.PE.DotNet.Metadata.Tables.Rows.MethodAttributes; | ||
|
||
namespace AsmResolver.DotNet.Dynamic | ||
{ | ||
/// <summary> | ||
/// Represents a single method in a type definition of a .NET module. | ||
/// </summary> | ||
public class DynamicMethodDefinition : MethodDefinition | ||
{ | ||
/// <summary> | ||
/// Create a Dynamic Method Definition | ||
/// </summary> | ||
/// <param name="module">Target Module</param> | ||
/// <param name="dynamicMethodObj">Dynamic Method / Delegate / DynamicResolver</param> | ||
public DynamicMethodDefinition(ModuleDefinition module, object dynamicMethodObj) : | ||
base(new MetadataToken(TableIndex.Method, 0)) | ||
{ | ||
dynamicMethodObj = DynamicMethodHelper.ResolveDynamicResolver(dynamicMethodObj); | ||
var methodBase = FieldReader.ReadField<MethodBase>(dynamicMethodObj, "m_method"); | ||
if (methodBase is null) | ||
{ | ||
throw new ArgumentException( | ||
"Could not get the underlying method base in the provided dynamic method object."); | ||
} | ||
|
||
Module = module; | ||
Name = methodBase.Name; | ||
Attributes = (MethodAttributes)methodBase.Attributes; | ||
Signature = new ReferenceImporter(module).ImportMethodSignature(ResolveSig(methodBase, module)); | ||
CilMethodBody = CreateDynamicMethodBody(this, dynamicMethodObj); | ||
} | ||
|
||
private MethodSignature ResolveSig(MethodBase methodBase, ModuleDefinition module) | ||
{ | ||
var imp = new ReferenceImporter(module); | ||
var returnType = methodBase is MethodInfo info | ||
? imp.ImportTypeSignature(info.ReturnType) | ||
: module.CorLibTypeFactory.Void; | ||
|
||
var parameters = methodBase.GetParameters(); | ||
|
||
var parameterTypes = new TypeSignature[parameters.Length]; | ||
for (int i = 0; i < parameterTypes.Length; i++) | ||
parameterTypes[i] = imp.ImportTypeSignature(parameters[i].ParameterType); | ||
|
||
return new MethodSignature( | ||
methodBase.IsStatic ? 0 : CallingConventionAttributes.HasThis, | ||
returnType, parameterTypes); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override ModuleDefinition Module { get; } | ||
|
||
/// <summary> | ||
/// Creates a CIL method body from a dynamic method. | ||
/// </summary> | ||
/// <param name="method">The method that owns the method body.</param> | ||
/// <param name="dynamicMethodObj">The Dynamic Method/Delegate/DynamicResolver.</param> | ||
/// <returns>The method body.</returns> | ||
private static CilMethodBody CreateDynamicMethodBody(MethodDefinition method, object dynamicMethodObj) | ||
{ | ||
if (!(method.Module is SerializedModuleDefinition module)) | ||
throw new ArgumentException("Method body should reference a serialized module."); | ||
|
||
var result = new CilMethodBody(method); | ||
dynamicMethodObj = DynamicMethodHelper.ResolveDynamicResolver(dynamicMethodObj); | ||
|
||
//Get Runtime Fields | ||
byte[] code = FieldReader.ReadField<byte[]>(dynamicMethodObj, "m_code")!; | ||
object scope = FieldReader.ReadField<object>(dynamicMethodObj, "m_scope")!; | ||
var tokenList = FieldReader.ReadField<List<object?>>(scope, "m_tokens")!; | ||
byte[] localSig = FieldReader.ReadField<byte[]>(dynamicMethodObj, "m_localSignature")!; | ||
byte[] ehHeader = FieldReader.ReadField<byte[]>(dynamicMethodObj, "m_exceptionHeader")!; | ||
var ehInfos = FieldReader.ReadField<IList<object>>(dynamicMethodObj, "m_exceptions")!; | ||
|
||
//Local Variables | ||
DynamicMethodHelper.ReadLocalVariables(result, method, localSig); | ||
|
||
// Read raw instructions. | ||
var reader = ByteArrayDataSource.CreateReader(code); | ||
var disassembler = new CilDisassembler(reader, new DynamicCilOperandResolver(module, result, tokenList)); | ||
result.Instructions.AddRange(disassembler.ReadInstructions()); | ||
|
||
//Exception Handlers | ||
DynamicMethodHelper.ReadReflectionExceptionHandlers(result, ehInfos, ehHeader, new ReferenceImporter(module)); | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/AsmResolver.DotNet/FieldReader.cs → ...AsmResolver.DotNet.Dynamic/FieldReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.