Enable delay signing in ilasm
authorMichelle McDaniel <adiaaida@gmail.com>
Mon, 13 Feb 2017 19:00:58 +0000 (11:00 -0800)
committerMichelle McDaniel <adiaaida@gmail.com>
Wed, 15 Feb 2017 17:56:30 +0000 (09:56 -0800)
Corefx needs the .Net Core version of ilasm to create delay signable
assemblies. This change brings back AllocateStrongNameSignature that was
deleted in 2efbb92 and modifies it to use similar logic from Roslyn to
determine the size of the signature that we need to allocate space for.

Fixes #9292.

src/ilasm/assembler.h
src/ilasm/writer.cpp

index 2e3404f..39fadd5 100644 (file)
@@ -1051,6 +1051,8 @@ public:
                           PermissionSetDecl*pPermissionSets);
     BinStr* EncodeSecAttr(__in __nullterminated char* szReflName, BinStr* pbsSecAttrBlob, unsigned nProps);
 
+    HRESULT AllocateStrongNameSignature();
+
     // Custom values paraphernalia:
 public:
     mdToken m_tkCurrentCVOwner;
index 85f29af..624c474 100644 (file)
@@ -982,6 +982,62 @@ BOOL Assembler::EmitEventsProps(Class* pClass)
     return ret;
 }
 
+HRESULT Assembler::AllocateStrongNameSignature()
+{
+    HRESULT             hr = S_OK;
+    HCEESECTION         hSection;
+    DWORD               dwDataLength;
+    DWORD               dwDataOffset;
+    DWORD               dwDataRVA;
+    VOID               *pvBuffer;
+    AsmManStrongName   *pSN = &m_pManifest->m_sStrongName;
+
+    // pSN->m_cbPublicKey is the length of the m_pbPublicKey
+    dwDataLength = ((int)pSN->m_cbPublicKey < 128 + 32) ? 128 : (int)pSN->m_cbPublicKey - 32;
+
+    // Grab memory in the section for our stuff.
+    if (FAILED(hr = m_pCeeFileGen->GetIlSection(m_pCeeFile,
+                                                &hSection)))
+    {
+        return hr;
+    }
+
+    if (FAILED(hr = m_pCeeFileGen->GetSectionBlock(hSection,
+                                                   dwDataLength,
+                                                   4,
+                                                   &pvBuffer)))
+    {
+        return hr;
+    }
+
+    // Where did we get that memory?
+    if (FAILED(hr = m_pCeeFileGen->GetSectionDataLen(hSection,
+                                                     &dwDataOffset)))
+    {
+        return hr;
+    }
+
+    dwDataOffset -= dwDataLength;
+
+    // Convert to an RVA.
+    if (FAILED(hr = m_pCeeFileGen->GetMethodRVA(m_pCeeFile,
+                                                dwDataOffset,
+                                                &dwDataRVA)))
+    {
+        return hr;
+    }
+
+    // Emit the directory entry.
+    if (FAILED(hr = m_pCeeFileGen->SetStrongNameEntry(m_pCeeFile,
+                                                      dwDataLength,
+                                                      dwDataRVA)))
+    {
+        return hr;
+    }
+
+    return S_OK;
+}
+
 #ifdef _PREFAST_
 #pragma warning(push)
 #pragma warning(disable:21000) // Suppress PREFast warning about overly large function
@@ -1008,6 +1064,15 @@ HRESULT Assembler::CreatePEFile(__in __nullterminated WCHAR *pwzOutputFilename)
 
     if(bClock) bClock->cMDEmit1 = GetTickCount();
 
+    // Allocate space for a strong name signature if we're delay or full
+    // signing the assembly.
+    if (m_pManifest->m_sStrongName.m_pbPublicKey)
+    {
+        if (FAILED(hr = AllocateStrongNameSignature()))
+        {
+            goto exit;
+        }
+    }
 
     if(bClock) bClock->cMDEmit2 = GetTickCount();