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.
9 // ZapNode that wraps EE datastructure for zapping
11 // ======================================================================================
15 #include "zapwrapper.h"
17 void ZapWrapperTable::Resolve()
19 for (WrapperTable::Iterator i = m_entries.Begin(), end = m_entries.End(); i != end; i++)
21 (*i)->Resolve(m_pImage);
25 // ======================================================================================
26 // Actual placeholders
28 class ZapMethodHandle : public ZapWrapper
31 virtual void Resolve(ZapImage * pImage)
33 SetRVA(pImage->m_pPreloader->MapMethodHandle(CORINFO_METHOD_HANDLE(GetHandle())));
36 virtual ZapNodeType GetType()
38 return ZapNodeType_MethodHandle;
42 ZapNode * ZapWrapperTable::GetMethodHandle(CORINFO_METHOD_HANDLE handle)
44 return GetPlaceHolder<ZapMethodHandle, ZapNodeType_MethodHandle>(handle);
47 class ZapClassHandle : public ZapWrapper
50 virtual void Resolve(ZapImage * pImage)
52 SetRVA(pImage->m_pPreloader->MapClassHandle(CORINFO_CLASS_HANDLE(GetHandle())));
55 virtual ZapNodeType GetType()
57 return ZapNodeType_ClassHandle;
61 ZapNode * ZapWrapperTable::GetClassHandle(CORINFO_CLASS_HANDLE handle)
63 return GetPlaceHolder<ZapClassHandle, ZapNodeType_ClassHandle>(handle);
66 class ZapFieldHandle : public ZapWrapper
69 virtual void Resolve(ZapImage * pImage)
71 SetRVA(pImage->m_pPreloader->MapFieldHandle(CORINFO_FIELD_HANDLE(GetHandle())));
74 virtual ZapNodeType GetType()
76 return ZapNodeType_FieldHandle;
80 ZapNode * ZapWrapperTable::GetFieldHandle(CORINFO_FIELD_HANDLE handle)
82 return GetPlaceHolder<ZapFieldHandle, ZapNodeType_FieldHandle>(handle);
85 class ZapAddrOfPInvokeFixup : public ZapWrapper
88 virtual void Resolve(ZapImage * pImage)
90 SetRVA(pImage->m_pPreloader->MapAddressOfPInvokeFixup(CORINFO_METHOD_HANDLE((BYTE *)GetHandle() - 1)));
93 virtual ZapNodeType GetType()
95 return ZapNodeType_AddrOfPInvokeFixup;
99 ZapNode * ZapWrapperTable::GetAddrOfPInvokeFixup(CORINFO_METHOD_HANDLE handle)
101 // Disambiguate the normal method handle and address of P/Invoke fixup by adding 1
102 return GetPlaceHolder<ZapAddrOfPInvokeFixup, ZapNodeType_AddrOfPInvokeFixup>((BYTE *)handle + 1);
105 class ZapGenericHandle : public ZapWrapper
108 virtual void Resolve(ZapImage * pImage)
110 SetRVA(pImage->m_pPreloader->MapGenericHandle(CORINFO_GENERIC_HANDLE(GetHandle())));
113 virtual ZapNodeType GetType()
115 return ZapNodeType_GenericHandle;
119 ZapNode * ZapWrapperTable::GetGenericHandle(CORINFO_GENERIC_HANDLE handle)
121 return GetPlaceHolder<ZapGenericHandle, ZapNodeType_GenericHandle>(handle);
124 class ZapModuleIDHandle : public ZapWrapper
127 virtual void Resolve(ZapImage * pImage)
129 SetRVA(pImage->m_pPreloader->MapModuleIDHandle(CORINFO_MODULE_HANDLE(GetHandle())));
132 virtual ZapNodeType GetType()
134 return ZapNodeType_ModuleIDHandle;
138 ZapNode * ZapWrapperTable::GetModuleIDHandle(CORINFO_MODULE_HANDLE handle)
140 return GetPlaceHolder<ZapModuleIDHandle, ZapNodeType_ModuleIDHandle>(handle);
143 class ZapStub : public ZapWrapper
148 ZapStub(PVOID pStubData, DWORD dwStubSize)
149 : ZapWrapper(pStubData), m_dwStubSize(dwStubSize)
153 virtual DWORD GetSize()
158 virtual ZapNodeType GetType()
160 return ZapNodeType_Stub;
163 virtual UINT GetAlignment()
165 return DEFAULT_CODE_ALIGN;
168 virtual void Save(ZapWriter * pZapWriter)
170 DWORD dwSize = GetSize();
171 PVOID pStub = GetHandle();
173 SBuffer stubClone(dwSize);
175 ICorCompileInfo *pCompileInfo = ZapImage::GetImage(pZapWriter)->GetCompileInfo();
176 IfFailThrow(pCompileInfo->GetStubClone(pStub,
177 const_cast<BYTE *>(static_cast<const BYTE *>(stubClone)), dwSize));
179 pZapWriter->Write(const_cast<BYTE *>(static_cast<const BYTE *>(stubClone)), dwSize);
183 ZapNode * ZapWrapperTable::GetStub(void * pStub)
185 DWORD dwStubSize = 0;
186 void * pStubData = m_pImage->GetCompileInfo()->GetStubSize(pStub, &dwStubSize);
187 _ASSERTE(pStubData < pStub && pStub < (BYTE*)pStubData + dwStubSize);
189 ZapStub * pZapStub = (ZapStub *)m_entries.Lookup(pStubData);
190 if (pZapStub == NULL)
192 // did not find the delegate stub, need to emit the stub in the native image
193 pZapStub = new (m_pImage->GetHeap()) ZapStub(pStubData, dwStubSize);
195 m_entries.Add(pZapStub);
198 // Return inner ptr for the entrypoint
199 _ASSERTE(pZapStub->GetType() == ZapNodeType_Stub);
200 return m_pImage->GetInnerPtr(pZapStub, (PBYTE)pStub - (PBYTE)pStubData);