diff --git a/source/MetadataProcessor.Core/Extensions/ByteArrayExtensions.cs b/source/MetadataProcessor.Core/Extensions/ByteArrayExtensions.cs new file mode 100644 index 00000000..4c64220d --- /dev/null +++ b/source/MetadataProcessor.Core/Extensions/ByteArrayExtensions.cs @@ -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(); + } + } +} diff --git a/source/MetadataProcessor.Core/MetadataProcessor.Core.csproj b/source/MetadataProcessor.Core/MetadataProcessor.Core.csproj index 74a907e5..4a01884a 100644 --- a/source/MetadataProcessor.Core/MetadataProcessor.Core.csproj +++ b/source/MetadataProcessor.Core/MetadataProcessor.Core.csproj @@ -81,6 +81,7 @@ + diff --git a/source/MetadataProcessor.Core/Tables/nanoSignaturesTable.cs b/source/MetadataProcessor.Core/Tables/nanoSignaturesTable.cs index 9708402a..ad8b4b6b 100644 --- a/source/MetadataProcessor.Core/Tables/nanoSignaturesTable.cs +++ b/source/MetadataProcessor.Core/Tables/nanoSignaturesTable.cs @@ -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; @@ -70,7 +71,7 @@ static nanoSignaturesTable() } /// - /// Stores list of unique signatures and corresspoinding identifiers. + /// Stores list of unique signatures and corresponding identifiers. /// private readonly IDictionary _idsBySignatures = new Dictionary(new ByteArrayComparer()); @@ -80,6 +81,8 @@ static nanoSignaturesTable() /// private readonly nanoTablesContext _context; + private readonly bool _verbose; + /// /// Last available signature id (offset in resulting table). /// @@ -91,10 +94,11 @@ static nanoSignaturesTable() /// /// Assembly tables context - contains all tables used for building target assembly. /// - public nanoSignaturesTable( - nanoTablesContext context) + public nanoSignaturesTable(nanoTablesContext context) { _context = context; + + _verbose = true; } /// @@ -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; } /// @@ -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; } /// @@ -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; } /// @@ -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; } /// @@ -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; } /// @@ -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; } /// @@ -208,7 +242,12 @@ public ushort GetOrCreateSignatureId( /// Custom attribute in Mono.Cecil format. 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; } /// @@ -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)) { diff --git a/source/native/Tools.MetaDataProcessor/WatchAssemblyBuilder.h b/source/native/Tools.MetaDataProcessor/WatchAssemblyBuilder.h index e600dcf2..e2a0953d 100644 --- a/source/native/Tools.MetaDataProcessor/WatchAssemblyBuilder.h +++ b/source/native/Tools.MetaDataProcessor/WatchAssemblyBuilder.h @@ -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(); diff --git a/source/native/Tools.Parser/Linker.cpp b/source/native/Tools.Parser/Linker.cpp index 4607b893..093b5e0b 100644 --- a/source/native/Tools.Parser/Linker.cpp +++ b/source/native/Tools.Parser/Linker.cpp @@ -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 ); @@ -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 ); @@ -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; @@ -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. @@ -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 )