Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions source/MetadataProcessor.Core/Extensions/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright (c) 2019 The nanoFramework project contributors
// See LICENSE file in the project root for full license information.
//

using Mono.Cecil;
using System.Text;
using System.Linq;

namespace nanoFramework.Tools.MetadataProcessor.Core.Extensions
{
internal static class ByteArrayExtensions
{
public static string BufferToHexString(this byte[] buffer)
{
StringBuilder output = new StringBuilder();

foreach(byte b in buffer)
{
output.Append(b.ToString("X2"));
}

return output.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Endianness\nanoBinaryWriter.cs" />
<Compile Include="Extensions\ByteArrayExtensions.cs" />
<Compile Include="Extensions\TypeDefinitionExtensions.cs" />
<Compile Include="InanoTable.cs" />
<Compile Include="Mono.Cecil\CodeWriter.cs" />
Expand Down
61 changes: 50 additions & 11 deletions source/MetadataProcessor.Core/Tables/nanoSignaturesTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
using nanoFramework.Tools.MetadataProcessor.Core.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -70,7 +71,7 @@ static nanoSignaturesTable()
}

/// <summary>
/// Stores list of unique signatures and corresspoinding identifiers.
/// Stores list of unique signatures and corresponding identifiers.
/// </summary>
private readonly IDictionary<byte[], ushort> _idsBySignatures =
new Dictionary<byte[], ushort>(new ByteArrayComparer());
Expand All @@ -80,6 +81,8 @@ static nanoSignaturesTable()
/// </summary>
private readonly nanoTablesContext _context;

private readonly bool _verbose;

/// <summary>
/// Last available signature id (offset in resulting table).
/// </summary>
Expand All @@ -91,10 +94,11 @@ static nanoSignaturesTable()
/// <param name="context">
/// Assembly tables context - contains all tables used for building target assembly.
/// </param>
public nanoSignaturesTable(
nanoTablesContext context)
public nanoSignaturesTable(nanoTablesContext context)
{
_context = context;

_verbose = true;
}

/// <summary>
Expand All @@ -104,7 +108,12 @@ public nanoSignaturesTable(
public ushort GetOrCreateSignatureId(
MethodDefinition methodDefinition)
{
return GetOrCreateSignatureIdImpl(GetSignature(methodDefinition));
var sig = GetSignature(methodDefinition);
var sigId = GetOrCreateSignatureIdImpl(sig);

if (_verbose) Console.WriteLine($"{methodDefinition.MetadataToken.ToInt32()} -> {sig.BufferToHexString()} -> {sigId.ToString("X4")}");

return sigId;
}

/// <summary>
Expand All @@ -114,7 +123,12 @@ public ushort GetOrCreateSignatureId(
public ushort GetOrCreateSignatureId(
FieldDefinition fieldDefinition)
{
return GetOrCreateSignatureIdImpl(GetSignature(fieldDefinition.FieldType, true));
var sig = GetSignature(fieldDefinition.FieldType, true);
var sigId = GetOrCreateSignatureIdImpl(sig);

if (_verbose) Console.WriteLine($"{fieldDefinition.MetadataToken.ToInt32()} -> {sig.BufferToHexString()} -> {sigId.ToString("X4")}");

return sigId;
}

/// <summary>
Expand All @@ -124,7 +138,12 @@ public ushort GetOrCreateSignatureId(
public ushort GetOrCreateSignatureId(
FieldReference fieldReference)
{
return GetOrCreateSignatureIdImpl(GetSignature(fieldReference));
var sig = GetSignature(fieldReference);
var sigId = GetOrCreateSignatureIdImpl(sig);

if (_verbose) Console.WriteLine($"{fieldReference.MetadataToken.ToInt32()} -> {sig.BufferToHexString()} -> {sigId.ToString("X4")}");

return sigId;
}

/// <summary>
Expand All @@ -134,7 +153,12 @@ public ushort GetOrCreateSignatureId(
public ushort GetOrCreateSignatureId(
MethodReference methodReference)
{
return GetOrCreateSignatureIdImpl(GetSignature(methodReference));
var sig = GetSignature(methodReference);
var sigId = GetOrCreateSignatureIdImpl(sig);

if (_verbose) Console.WriteLine($"{methodReference.MetadataToken.ToInt32()} -> {sig.BufferToHexString()} -> {sigId.ToString("X4")}");

return sigId;
}

/// <summary>
Expand Down Expand Up @@ -189,7 +213,12 @@ public ushort GetOrCreateSignatureId(
public ushort GetOrCreateSignatureId(
InterfaceImplementation interfaceImplementation)
{
return GetOrCreateSignatureIdImpl(GetSignature(interfaceImplementation, false));
var sig = GetSignature(interfaceImplementation, false);
var sigId = GetOrCreateSignatureIdImpl(sig);

if (_verbose) Console.WriteLine($"{interfaceImplementation.MetadataToken.ToInt32()} -> {sig.BufferToHexString()} -> {sigId.ToString("X4")}");

return sigId;
}

/// <summary>
Expand All @@ -199,7 +228,12 @@ public ushort GetOrCreateSignatureId(
public ushort GetOrCreateSignatureId(
TypeReference typeReference)
{
return GetOrCreateSignatureIdImpl(GetSignature(typeReference, false));
var sig = GetSignature(typeReference, false);
var sigId = GetOrCreateSignatureIdImpl(sig);

if (_verbose) Console.WriteLine($"{typeReference.MetadataToken.ToInt32()} -> {sig.BufferToHexString()} -> {sigId.ToString("X4")}");

return sigId;
}

/// <summary>
Expand All @@ -208,7 +242,12 @@ public ushort GetOrCreateSignatureId(
/// <param name="customAttribute">Custom attribute in Mono.Cecil format.</param>
public ushort GetOrCreateSignatureId(CustomAttribute customAttribute)
{
return GetOrCreateSignatureIdImpl(GetSignature(customAttribute));
var sig = GetSignature(customAttribute);
var sigId = GetOrCreateSignatureIdImpl(sig);

if (_verbose) Console.WriteLine($"{customAttribute.ToString()} -> {sig.BufferToHexString()} -> {sigId.ToString("X4")}");

return sigId;
}

/// <summary>
Expand Down Expand Up @@ -589,7 +628,7 @@ private void WriteSubTypeInfo(TypeReference typeDefinition, nanoBinaryWriter wri
if (typeDefinition is TypeSpecification &&
_context.TypeSpecificationsTable.TryGetTypeReferenceId(typeDefinition, out referenceId))
{
writer.WriteMetadataToken(((uint)referenceId << 2) | 0x04);
writer.WriteMetadataToken(((uint)referenceId << 2) | 0x02);
}
else if (_context.TypeReferencesTable.TryGetTypeReferenceId(typeDefinition, out referenceId))
{
Expand Down
2 changes: 2 additions & 0 deletions source/native/Tools.MetaDataProcessor/WatchAssemblyBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ namespace WatchAssemblyBuilder

HRESULT DumpPdbxToken(CLR_XmlUtil& xml, IXMLDOMNodePtr pNodeParent, mdToken tk);

void DumpSig(CLR_UINT32 token, CLR_UINT16 sig, const BYTE* sigRaw, size_t sigLen);

public:
Linker();
~Linker();
Expand Down
24 changes: 23 additions & 1 deletion source/native/Tools.Parser/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,8 @@ HRESULT WatchAssemblyBuilder::Linker::ProcessMemberRef()
if(!AllocString ( mr.m_name , dst->name, false )) REPORT_NO_MEMORY();
if(!AllocSignatureForField( &mr.m_sig.m_sigField, dst->sig )) REPORT_NO_MEMORY();

DumpSig(tk, dst->sig, m_tmpSig, (int)(m_tmpSigPtr - m_tmpSig));

tk2 = m_lookupIDs[ mr.m_tr ]; if(CLR_TypeFromTk(tk2) != TBL_TypeRef) REPORT_NO_MEMORY();

dst->container = CLR_DataFromTk( tk2 );
Expand All @@ -1029,6 +1031,8 @@ HRESULT WatchAssemblyBuilder::Linker::ProcessMemberRef()
if(!AllocString ( mr.m_name , dst->name, false )) REPORT_NO_MEMORY();
if(!AllocSignatureForMethod( &mr.m_sig.m_sigMethod, dst->sig )) REPORT_NO_MEMORY();

DumpSig(tk, dst->sig, m_tmpSig, (int)(m_tmpSigPtr - m_tmpSig));

tk2 = m_lookupIDs[ mr.m_tr ]; if(CLR_TypeFromTk(tk2) != TBL_TypeRef) REPORT_NO_MEMORY();

dst->container = CLR_DataFromTk( tk2 );
Expand Down Expand Up @@ -1509,10 +1513,11 @@ HRESULT WatchAssemblyBuilder::Linker::ProcessFieldDef( MetaData::TypeDef& td, CL

CLR_RECORD_FIELDDEF* dst = m_tableFieldDef.Alloc( 1 ); if(dst == NULL) REPORT_NO_MEMORY();


if(!AllocString ( fd.m_name , dst->name, false )) REPORT_NO_MEMORY();
if(!AllocSignatureForField( &fd.m_sig.m_sigField, dst->sig )) REPORT_NO_MEMORY();

DumpSig(fd.m_fd, dst->sig, m_tmpSig, (int)(m_tmpSigPtr - m_tmpSig));

switch(fd.m_flags & fdFieldAccessMask)
{
case fdPrivateScope: dst->flags = CLR_RECORD_FIELDDEF::FD_Scope_PrivateScope; break;
Expand Down Expand Up @@ -1695,6 +1700,8 @@ HRESULT WatchAssemblyBuilder::Linker::ProcessMethodDef( MetaData::TypeDef& td, C
if(!AllocString ( md.m_name , dst->name , false )) REPORT_NO_MEMORY();
if(!AllocSignatureForMethod( &md.m_method, dst->sig )) REPORT_NO_MEMORY();

DumpSig(md.m_md, dst->sig, m_tmpSig, (int)(m_tmpSigPtr - m_tmpSig));

NANOCLR_CHECK_HRESULT(CheckRange( md.m_method.m_lstParams, dst->numArgs, L"number", L"arguments" ));

dst->numArgs = (CLR_UINT8) md.m_method.m_lstParams.size() ; if((md.m_flags & mdStatic) == 0) dst->numArgs++; // Include the 'this' pointer in the number of arguments.
Expand Down Expand Up @@ -1725,6 +1732,21 @@ HRESULT WatchAssemblyBuilder::Linker::ProcessMethodDef( MetaData::TypeDef& td, C
NANOCLR_CLEANUP_END();
}

void WatchAssemblyBuilder::Linker::DumpSig(CLR_UINT32 token, CLR_UINT16 sig, const BYTE* sigRaw, size_t sigLen)
{
printf("%d -> ", token);

const BYTE* sigCopy = sigRaw;
size_t sigLenCopy = sigLen;

while (sigLenCopy-- > 0)
{
printf("%02X", *sigCopy++);
}

printf(" -> %04X\n", sig);
}

//--//

HRESULT WatchAssemblyBuilder::Linker::ProcessMethodDef_ByteCode( MetaData::TypeDef& td, CLR_RECORD_TYPEDEF* tdDst, MetaData::MethodDef& md, CLR_UINT32 mode )
Expand Down