1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5
6#include "common.h"
7#include "assemblybinder.hpp"
8#include "coreclrbindercommon.h"
9#include "clrprivbindercoreclr.h"
10#include "clrprivbinderutil.h"
11
12using namespace BINDER_SPACE;
13
14//=============================================================================
15// Init code
16//-----------------------------------------------------------------------------
17/* static */
18HRESULT CCoreCLRBinderHelper::Init()
19{
20 STANDARD_VM_CONTRACT;
21 HRESULT hr = S_OK;
22 EX_TRY
23 {
24 hr = AssemblyBinder::Startup();
25 }
26 EX_CATCH_HRESULT(hr);
27
28 return hr;
29}
30
31HRESULT CCoreCLRBinderHelper::DefaultBinderSetupContext(DWORD dwAppDomainId,CLRPrivBinderCoreCLR **ppTPABinder)
32{
33 HRESULT hr = S_OK;
34 EX_TRY
35 {
36 if(ppTPABinder != NULL)
37 {
38 ReleaseHolder<CLRPrivBinderCoreCLR> pBinder;
39 SAFE_NEW(pBinder, CLRPrivBinderCoreCLR);
40
41 BINDER_SPACE::ApplicationContext *pApplicationContext = pBinder->GetAppContext();
42 hr = pApplicationContext->Init();
43 if(SUCCEEDED(hr))
44 {
45 pApplicationContext->SetAppDomainId(dwAppDomainId);
46 pBinder->SetManagedAssemblyLoadContext(NULL);
47 *ppTPABinder = clr::SafeAddRef(pBinder.Extract());
48 }
49 }
50 }
51 EX_CATCH_HRESULT(hr);
52
53Exit:
54 return hr;
55}
56
57HRESULT CCoreCLRBinderHelper::GetAssemblyIdentity(LPCSTR szTextualIdentity,
58 BINDER_SPACE::ApplicationContext *pApplicationContext,
59 NewHolder<AssemblyIdentityUTF8> &assemblyIdentityHolder)
60{
61 HRESULT hr = S_OK;
62 VALIDATE_ARG_RET(szTextualIdentity != NULL);
63
64 EX_TRY
65 {
66 AssemblyIdentityUTF8 *pAssemblyIdentity = NULL;
67 if (pApplicationContext != NULL)
68 {
69 // This returns a cached copy owned by application context
70 hr = pApplicationContext->GetAssemblyIdentity(szTextualIdentity, &pAssemblyIdentity);
71 if(SUCCEEDED(hr))
72 {
73 assemblyIdentityHolder = pAssemblyIdentity;
74 assemblyIdentityHolder.SuppressRelease();
75 }
76 }
77 else
78 {
79 SString sTextualIdentity;
80
81 sTextualIdentity.SetUTF8(szTextualIdentity);
82
83 // This is a private copy
84 pAssemblyIdentity = new AssemblyIdentityUTF8();
85 hr = TextualIdentityParser::Parse(sTextualIdentity, pAssemblyIdentity);
86 if(SUCCEEDED(hr))
87 {
88 pAssemblyIdentity->PopulateUTF8Fields();
89 assemblyIdentityHolder = pAssemblyIdentity;
90 }
91 }
92 }
93 EX_CATCH_HRESULT(hr);
94
95 return hr;
96}
97
98//=============================================================================
99// Functions that provides binding services beyond the ICLRPrivInterface
100//-----------------------------------------------------------------------------
101
102HRESULT CCoreCLRBinderHelper::BindToSystem(ICLRPrivAssembly **ppSystemAssembly, bool fBindToNativeImage)
103{
104 HRESULT hr = S_OK;
105 VALIDATE_ARG_RET(ppSystemAssembly != NULL);
106
107 EX_TRY
108 {
109 ReleaseHolder<BINDER_SPACE::Assembly> pAsm;
110 StackSString systemPath(SystemDomain::System()->SystemDirectory());
111 hr = AssemblyBinder::BindToSystem(systemPath, &pAsm, fBindToNativeImage);
112 if(SUCCEEDED(hr))
113 {
114 _ASSERTE(pAsm != NULL);
115 *ppSystemAssembly = pAsm.Extract();
116 }
117 }
118 EX_CATCH_HRESULT(hr);
119
120 return hr;
121}
122
123HRESULT CCoreCLRBinderHelper::BindToSystemSatellite(SString &systemPath,
124 SString &sSimpleName,
125 SString &sCultureName,
126 ICLRPrivAssembly **ppSystemAssembly)
127{
128 HRESULT hr = S_OK;
129 VALIDATE_ARG_RET(ppSystemAssembly != NULL && !systemPath.IsEmpty());
130
131 EX_TRY
132 {
133 ReleaseHolder<BINDER_SPACE::Assembly> pAsm;
134 hr = AssemblyBinder::BindToSystemSatellite(systemPath, sSimpleName, sCultureName, &pAsm);
135 if(SUCCEEDED(hr))
136 {
137 _ASSERTE(pAsm != NULL);
138 *ppSystemAssembly = pAsm.Extract();
139 }
140 }
141 EX_CATCH_HRESULT(hr);
142
143 return hr;
144}
145
146HRESULT CCoreCLRBinderHelper::GetAssemblyFromImage(PEImage *pPEImage,
147 PEImage *pNativePEImage,
148 ICLRPrivAssembly **ppAssembly)
149{
150 HRESULT hr = S_OK;
151 VALIDATE_ARG_RET(pPEImage != NULL && ppAssembly != NULL);
152
153 EX_TRY
154 {
155 ReleaseHolder<BINDER_SPACE::Assembly> pAsm;
156 hr = AssemblyBinder::GetAssemblyFromImage(pPEImage, pNativePEImage, &pAsm);
157 if(SUCCEEDED(hr))
158 {
159 _ASSERTE(pAsm != nullptr);
160 *ppAssembly = pAsm.Extract();
161 }
162 }
163 EX_CATCH_HRESULT(hr);
164
165 return hr;
166}
167
168//=============================================================================
169// Explicitly bind to an assembly by filepath
170//=============================================================================
171/* static */
172HRESULT CCoreCLRBinderHelper::GetAssembly(/* in */ SString &assemblyPath,
173 /* in */ BOOL fInspectionOnly,
174 /* in */ BOOL fIsInGAC,
175 /* in */ BOOL fExplicitBindToNativeImage,
176 /* out */ BINDER_SPACE::Assembly **ppAssembly)
177{
178 return AssemblyBinder::GetAssembly(assemblyPath,
179 fInspectionOnly,
180 fIsInGAC,
181 fExplicitBindToNativeImage,
182 ppAssembly
183 );
184}
185