Skip to content

Commit

Permalink
Some more cleanups in Assembly/Binder area (#59288)
Browse files Browse the repository at this point in the history
* Reducing BINDER_SPACE::Assembly closer to what it represents

* make GCC happy

* Removed `Binder[AddRef/Release]PEImage`. Can just use `AddRef/Release`

* moved IsValidArchitecture helper to bindercommon

* Removed the notion of PEModule - it does not exist

* some more unused code

* fix for GCC

* associate System lib with DefaultBinder - makes things simpler

* PR feedback
  • Loading branch information
VSadov committed Sep 24, 2021
1 parent ae11fef commit 172059a
Show file tree
Hide file tree
Showing 29 changed files with 200 additions and 687 deletions.
92 changes: 16 additions & 76 deletions src/coreclr/binder/assembly.cpp
Expand Up @@ -13,124 +13,64 @@
#include "common.h"
#include "assembly.hpp"
#include "utils.hpp"
#include "assemblybindercommon.hpp"

namespace BINDER_SPACE
{
namespace
{
BOOL IsPlatformArchitecture(PEKIND kArchitecture)
{
return ((kArchitecture != peMSIL) && (kArchitecture != peNone));
}
};

Assembly::Assembly()
{
m_cRef = 1;
m_pPEImage = NULL;
m_pAssemblyName = NULL;
m_pMDImport = NULL;
m_dwAssemblyFlags = FLAG_NONE;
m_isInTPA = false;
m_pBinder = NULL;
}

Assembly::~Assembly()
{
if (m_pPEImage != NULL)
{
BinderReleasePEImage(m_pPEImage);
m_pPEImage = NULL;
}

SAFE_RELEASE(m_pPEImage);
SAFE_RELEASE(m_pAssemblyName);
SAFE_RELEASE(m_pMDImport);
}

HRESULT Assembly::Init(IMDInternalImport *pIMetaDataAssemblyImport,
PEKIND PeKind,
PEImage *pPEImage,
SString &assemblyPath,
BOOL fIsInTPA)
HRESULT Assembly::Init(PEImage *pPEImage, BOOL fIsInTPA)
{
HRESULT hr = S_OK;

ReleaseHolder<AssemblyName> pAssemblyName;
SAFE_NEW(pAssemblyName, AssemblyName);

// Get assembly name def from meta data import and store it for later refs access
IF_FAIL_GO(pAssemblyName->Init(pIMetaDataAssemblyImport, PeKind));
SetMDImport(pIMetaDataAssemblyImport);
if (!fIsInTPA)
{
GetPath().Set(assemblyPath);
}

// Safe architecture for validation
PEKIND kAssemblyArchitecture;
kAssemblyArchitecture = pAssemblyName->GetArchitecture();
SetIsInTPA(fIsInTPA);
SetPEImage(pPEImage);
IF_FAIL_GO(pAssemblyName->Init(pPEImage));
pAssemblyName->SetIsDefinition(TRUE);

// Now take ownership of assembly names
SetAssemblyName(pAssemblyName.Extract(), FALSE /* fAddRef */);

// Finally validate architecture
if (!IsValidArchitecture(kAssemblyArchitecture))
// validate architecture
if (!AssemblyBinderCommon::IsValidArchitecture(pAssemblyName->GetArchitecture()))
{
// Assembly image can't be executed on this platform
IF_FAIL_GO(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT));
}

Exit:
return hr;
}
m_isInTPA = fIsInTPA;

HRESULT Assembly::GetMVID(GUID *pMVID)
{
// Zero init the GUID incase we fail
ZeroMemory(pMVID, sizeof(GUID));
pPEImage->AddRef();
m_pPEImage = pPEImage;

return m_pMDImport->GetScopeProps(NULL, pMVID);
}
// Now take ownership of assembly name
m_pAssemblyName = pAssemblyName.Extract();

/* static */
PEKIND Assembly::GetSystemArchitecture()
{
#if defined(TARGET_X86)
return peI386;
#elif defined(TARGET_AMD64)
return peAMD64;
#elif defined(TARGET_ARM)
return peARM;
#elif defined(TARGET_ARM64)
return peARM64;
#else
PORTABILITY_ASSERT("Assembly::GetSystemArchitecture");
#endif
Exit:
return hr;
}

/* static */
BOOL Assembly::IsValidArchitecture(PEKIND kArchitecture)
PEImage* Assembly::GetPEImage()
{
if (!IsPlatformArchitecture(kArchitecture))
return TRUE;

return (kArchitecture == GetSystemArchitecture());
return m_pPEImage;
}

// --------------------------------------------------------------------
// BINDER_SPACE::Assembly methods
// --------------------------------------------------------------------
LPCWSTR Assembly::GetSimpleName()
{
AssemblyName *pAsmName = GetAssemblyName();
return (pAsmName == nullptr ? nullptr : (LPCWSTR)pAsmName->GetSimpleName());
}

AssemblyLoaderAllocator* Assembly::GetLoaderAllocator()
{
return m_pBinder ? m_pBinder->GetLoaderAllocator() : NULL;
}
}

0 comments on commit 172059a

Please sign in to comment.