summaryrefslogtreecommitdiff
path: root/src/zap/zapimport.h
blob: 34a673ecf517ab6593d92b5f447202fff9609df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// ZapImport.h
//

//
// Import is soft bound references to elements outside the current module
// 
// ======================================================================================

#ifndef __ZAPIMPORT_H__
#define __ZAPIMPORT_H__

class ZapImportTable;
class ZapGCRefMapTable;
class NibbleWriter;

//---------------------------------------------------------------------------------------
//
// ZapImport is the import cell itself
//
// Every import cell is uniquely identified by its ZapNodeType and two handles 
// (the second handle is optional and is often NULL)
//
// Actual implementations inherits from this abstract base class.
//
class ZapImport : public ZapNode
{
    COUNT_T m_index;
    DWORD m_offset;

    PVOID m_handle;
    PVOID m_handle2;

    ZapBlob * m_pBlob;

public:
    void SetHandle(PVOID handle)
    {
        _ASSERTE(m_handle == NULL);
        m_handle = handle;
    }

    void SetHandle2(PVOID handle2)
    {
        _ASSERTE(m_handle2 == NULL);
        m_handle2 = handle2;
    }

    PVOID GetHandle()
    {
        return m_handle;
    }

    PVOID GetHandle2()
    {
        return m_handle2;
    }

    void SetBlob(ZapBlob * pBlob)
    {
        _ASSERTE(m_pBlob == NULL);
        m_pBlob = pBlob;
    }

    ZapBlob * GetBlob()
    {
        _ASSERTE(m_pBlob != NULL);
        return m_pBlob;
    }

    BOOL HasBlob()
    {
        return m_pBlob != NULL;
    }

    virtual ZapImportSectionType ComputePlacement(ZapImage * pImage, BOOL * pfIsEager, BOOL * pfNeedsSignature)
    {
        *pfIsEager = FALSE;
        *pfNeedsSignature = TRUE;
        return ZapImportSectionType_Handle;
    }

    // All subtypes have to override
    virtual void EncodeSignature(ZapImportTable * pTable, SigBuilder * pSigBuilder) = 0;

    virtual DWORD GetSize()
    {
        return sizeof(TADDR);
    }

    virtual UINT GetAlignment()
    {
        return sizeof(TADDR);
    }

    virtual void Save(ZapWriter * pZapWriter);
    
    //
    // Offset of the fixup cell within its section
    //

    void SetSectionIndexAndOffset(COUNT_T index, DWORD offset)
    {
        m_index = index;
        m_offset = offset;
    }

    DWORD GetSectionIndex()
    {
        return m_index;
    }

    DWORD GetOffset()
    {
        return m_offset;
    }
};

//---------------------------------------------------------------------------------------
//
// ZapGenericSignature is signature of generic dictionary entry.
//
class ZapGenericSignature : public ZapBlob
{
public:
    ZapGenericSignature(SIZE_T cbSize)
        : ZapBlob(cbSize)
    {
    }

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_GenericSignature;
    }
};

//---------------------------------------------------------------------------------------
//
// ZapImportTable is the main class that keeps track of all ZapImports.
//
// There is a single instance of it per image.
//
class ZapImportTable : public ZapNode
{
    //
    // Hashtable key of the import
    // The same key is used for both ZapImport and ZapImportBlob
    //
    struct ImportKey
    {
        FORCEINLINE ImportKey(PVOID handle, ZapNodeType type)
            : m_handle(handle), m_handle2(NULL), m_type(type)
        {
        }

        FORCEINLINE ImportKey(PVOID handle, PVOID handle2, ZapNodeType type)
            : m_handle(handle), m_handle2(handle2), m_type(type)
        {
        }

        PVOID m_handle;
        PVOID m_handle2;
        ZapNodeType m_type;
    };

    //
    // Hashtable of ZapImports
    //
    class ImportTraits : public NoRemoveSHashTraits< DefaultSHashTraits<ZapImport *> >
    {
    public:
        typedef ImportKey key_t;

        static FORCEINLINE key_t GetKey(element_t e)
        { 
            LIMITED_METHOD_CONTRACT;
            return ImportKey(e->GetHandle(), e->GetHandle2(), e->GetType());
        }
        static FORCEINLINE BOOL Equals(key_t k1, key_t k2)
        { 
            LIMITED_METHOD_CONTRACT;
            return (k1.m_handle == k2.m_handle) && (k1.m_handle2 == k2.m_handle2) && (k1.m_type == k2.m_type);
        }
        static FORCEINLINE count_t Hash(key_t k) 
        {
            LIMITED_METHOD_CONTRACT;
            return (count_t)(size_t)k.m_handle ^ ((count_t)(size_t)k.m_handle2 << 1) ^ k.m_type;
        }

        static const element_t Null() { LIMITED_METHOD_CONTRACT; return NULL; }
        static bool IsNull(const element_t &e) { LIMITED_METHOD_CONTRACT; return e == NULL; }
    };

    typedef SHash< ImportTraits > ImportTable;

    //
    // Hashtable of module indices
    //
    struct ModuleReferenceEntry
    {
        CORINFO_MODULE_HANDLE m_module;
        DWORD m_index;

        USHORT m_wAssemblyRid;
        USHORT m_wModuleRid;
    };

    class ModuleReferenceTraits : public NoRemoveSHashTraits< DefaultSHashTraits<ModuleReferenceEntry *> >
    {
    public:
        typedef CORINFO_MODULE_HANDLE key_t;

        static key_t GetKey(element_t e)
        { 
            LIMITED_METHOD_CONTRACT;
            return e->m_module;
        }
        static BOOL Equals(key_t k1, key_t k2) 
        { 
            LIMITED_METHOD_CONTRACT;
            return (k1 == k2);
        }
        static count_t Hash(key_t k) 
        {
            LIMITED_METHOD_CONTRACT;
            return (count_t)(size_t)k;
        }

        static const element_t Null() { LIMITED_METHOD_CONTRACT; return NULL; }
        static bool IsNull(const element_t &e) { LIMITED_METHOD_CONTRACT; return e == NULL; }
    };

    typedef SHash< ModuleReferenceTraits > ModuleReferenceTable;

    //
    // Helpers for inserting actual implementations of ZapImports into hashtable
    //
    template < typename impl, ZapNodeType type >
    ZapImport * GetImport(PVOID handle)
    {
        ZapImport * pImport = m_imports.Lookup(ImportKey(handle, type));

        if (pImport != NULL)
        {
            return pImport;
        }

        pImport = new (m_pImage->GetHeap()) impl();
        _ASSERTE(pImport->GetType() == type);
        pImport->SetHandle(handle);
        m_imports.Add(pImport);
        return pImport;
    }

    template < typename impl, ZapNodeType type >
    ZapImport * GetImport(PVOID handle, PVOID handle2)
    {
        ZapImport * pImport = m_imports.Lookup(ImportKey(handle, handle2, type));

        if (pImport != NULL)
        {
            return pImport;
        }

        pImport = new (m_pImage->GetHeap()) impl();
        _ASSERTE(pImport->GetType() == type);
        pImport->SetHandle(handle);
        pImport->SetHandle2(handle2);
        m_imports.Add(pImport);
        return pImport;
    }

    template < typename impl, ZapNodeType type >
    ZapImport * GetImportForSignature(PVOID handle, SigBuilder * pSigBuilder)
    {
        ZapBlob * pBlob = GetBlob(pSigBuilder);

        ZapImport * pImport = m_imports.Lookup(ImportKey(handle, pBlob, type));

        if (pImport != NULL)
        {
            return pImport;
        }

        pImport = new (m_pImage->GetHeap()) impl();
        _ASSERTE(pImport->GetType() == type);
        pImport->SetHandle(handle);
        pImport->SetHandle2(pBlob);
        pImport->SetBlob(pBlob);
        m_imports.Add(pImport);
        return pImport;
    }

    ZapImport * GetExistingImport(ZapNodeType type, PVOID handle)
    {
        return m_imports.Lookup(ImportKey(handle, type));
    }

    ModuleReferenceEntry * GetModuleReference(CORINFO_MODULE_HANDLE handle);

    static DWORD __stdcall EncodeModuleHelper(LPVOID referencingModule, CORINFO_MODULE_HANDLE referencedModule);

    ImportTable m_imports;          // Interned ZapImport *
    SHash< NoRemoveSHashTraits < ZapBlob::SHashTraits > > m_blobs; // Interned ZapBlos for signatures and fixups

    ModuleReferenceTable m_moduleReferences;
    SArray<ModuleReferenceEntry *> m_modules;   // Secondary table of ModuleReferences to allow fast index based lookup

    SHash< NoRemoveSHashTraits < ZapBlob::SHashTraits > > m_genericSignatures;

    DWORD   m_nImportSectionSizes[ZapImportSectionType_Total];
    COUNT_T m_nImportSectionIndices[ZapImportSectionType_Total];

    ZapImage * m_pImage;

public:
    ZapImportTable(ZapImage * pImage)
        : m_pImage(pImage)
    {
         // Everything else is zero initialized by the allocator
    }

    void Preallocate(COUNT_T cbILImage)
    {
        PREALLOCATE_HASHTABLE(ZapImportTable::m_imports, 0.0030, cbILImage);
        PREALLOCATE_HASHTABLE(ZapImportTable::m_blobs, 0.0025, cbILImage);

        PREALLOCATE_HASHTABLE_NOT_NEEDED(ZapImportTable::m_moduleReferences, cbILImage);
    }

    //
    // Helpers for encoding import blobs
    //

    void EncodeModule(CORCOMPILE_FIXUP_BLOB_KIND kind, CORINFO_MODULE_HANDLE module, SigBuilder * pSigBuilder);
    void EncodeClass(CORCOMPILE_FIXUP_BLOB_KIND kind, CORINFO_CLASS_HANDLE handle, SigBuilder * pSigBuilder);
    void EncodeClassInContext(CORINFO_MODULE_HANDLE context, CORINFO_CLASS_HANDLE handle, SigBuilder * pSigBuilder);
    void EncodeField(CORCOMPILE_FIXUP_BLOB_KIND kind, CORINFO_FIELD_HANDLE handle, SigBuilder * pSigBuilder,
            CORINFO_RESOLVED_TOKEN * pResolvedToken = NULL, BOOL fEncodeUsingResolvedTokenSpecStreams = FALSE);
    void EncodeMethod(CORCOMPILE_FIXUP_BLOB_KIND kind, CORINFO_METHOD_HANDLE handle, SigBuilder * pSigBuilder, 
            CORINFO_RESOLVED_TOKEN * pResolvedToken = NULL, CORINFO_RESOLVED_TOKEN * pConstrainedResolvedToken = NULL,
            BOOL fEncodeUsingResolvedTokenSpecStreams = FALSE);

    // Encode module if the reference is within current version bubble. If not, return a suitable module within current version bubble.
    CORINFO_MODULE_HANDLE TryEncodeModule(CORCOMPILE_FIXUP_BLOB_KIND kind, CORINFO_MODULE_HANDLE module, SigBuilder * pSigBuilder);

    ICorDynamicInfo * GetJitInfo()
    {
        return m_pImage->GetJitInfo();
    }

    ICorCompileInfo * GetCompileInfo()
    {
        return m_pImage->GetCompileInfo();
    }

    ZapImage * GetImage()
    {
        return m_pImage;
    }

    // Returns index of module in the import table for encoding module fixups in EE datastructures.
    DWORD GetIndexOfModule(CORINFO_MODULE_HANDLE handle)
    {
        ZapImportTable::ModuleReferenceEntry * pModuleReference = GetModuleReference(handle);
        _ASSERTE(pModuleReference != NULL);
        return pModuleReference->m_index;
    }

    // Get the import blob for given signature
    ZapBlob * GetBlob(SigBuilder * pSigBuilder, BOOL fEager = FALSE);

    // Place give import blob
    void PlaceBlob(ZapBlob * pBlob, BOOL fEager = FALSE);

    // Encodes the import blob and places it into the image
    ZapBlob * PlaceImportBlob(ZapImport * pImport, BOOL fEager = FALSE);

    // Places import cell into the image.
    // This also encoded and places all the import blobs if they are not placed yet.
    void PlaceImport(ZapImport * pImport);

    // Encodes list of fixups and places it into the image. 
    // This also places all the import cells if they are not placed yet.
    ZapFixupInfo * PlaceFixups(ZapImport ** pImports);
    void PlaceFixups(ZapImport ** pImports, NibbleWriter& writer);

    ZapGenericSignature * GetGenericSignature(PVOID signature, BOOL fMethod);

    //
    // The actual implementations of import cells
    //
    ZapImport * GetFunctionEntryImport(CORINFO_METHOD_HANDLE handle);
    ZapImport * GetModuleHandleImport(CORINFO_MODULE_HANDLE handle);
    ZapImport * GetClassHandleImport(CORINFO_CLASS_HANDLE handle, PVOID pUniqueId = NULL);
    ZapImport * GetMethodHandleImport(CORINFO_METHOD_HANDLE handle);
    ZapImport * GetFieldHandleImport(CORINFO_FIELD_HANDLE handle);
    ZapImport * GetStringHandleImport(CORINFO_MODULE_HANDLE tokenScope, mdString metaTok);
    ZapImport * GetStaticFieldAddressImport(CORINFO_FIELD_HANDLE handle);
    ZapImport * GetClassDomainIdImport(CORINFO_CLASS_HANDLE handle);
    ZapImport * GetModuleDomainIdImport(CORINFO_MODULE_HANDLE handleToModule, CORINFO_CLASS_HANDLE handleToClass);
    ZapImport * GetSyncLockImport(CORINFO_CLASS_HANDLE handle); 
    ZapImport * GetIndirectPInvokeTargetImport(CORINFO_METHOD_HANDLE handle);
    ZapImport * GetProfilingHandleImport(CORINFO_METHOD_HANDLE handle);
    ZapImport * GetVarArgImport(CORINFO_MODULE_HANDLE handle, mdToken sigOrMemberRefOrDef);
    ZapImport * GetActiveDependencyImport(CORINFO_MODULE_HANDLE moduleFrom, CORINFO_MODULE_HANDLE moduleTo);

    ZapImport * GetExistingClassHandleImport(CORINFO_CLASS_HANDLE handle);
    ZapImport * GetExistingMethodHandleImport(CORINFO_METHOD_HANDLE handle);
    ZapImport * GetExistingFieldHandleImport(CORINFO_FIELD_HANDLE handle);

    ZapImport * GetVirtualImportThunk(CORINFO_METHOD_HANDLE handle, int slot);
    void        PlaceVirtualImportThunk(ZapImport * pImportThunk);

    ZapImport * GetExternalMethodThunk(CORINFO_METHOD_HANDLE handle);
    ZapImport * GetExternalMethodCell(CORINFO_METHOD_HANDLE handle);
    ZapImport * GetStubDispatchCell(CORINFO_CLASS_HANDLE typeHnd, CORINFO_METHOD_HANDLE methHnd);

    //
    // Ready-to-run imports
    //
    ZapImport * GetClassImport(CORCOMPILE_FIXUP_BLOB_KIND kind, CORINFO_RESOLVED_TOKEN * pResolvedToken);
    ZapImport * GetMethodImport(CORCOMPILE_FIXUP_BLOB_KIND kind, CORINFO_METHOD_HANDLE handle, CORINFO_RESOLVED_TOKEN * pResolvedToken, CORINFO_RESOLVED_TOKEN * pConstrainedResolvedToken = NULL);
    ZapImport * GetFieldImport(CORCOMPILE_FIXUP_BLOB_KIND kind, CORINFO_FIELD_HANDLE handle, CORINFO_RESOLVED_TOKEN * pResolvedToken);

    ZapImport * GetCheckTypeLayoutImport(CORINFO_CLASS_HANDLE handle);
    ZapImport * GetCheckFieldOffsetImport(CORINFO_FIELD_HANDLE handle, CORINFO_RESOLVED_TOKEN * pResolvedToken, DWORD offset);

    ZapImport * GetStubDispatchCell(CORINFO_RESOLVED_TOKEN * pResolvedToken);
    ZapImport * GetExternalMethodCell(CORINFO_METHOD_HANDLE handle, CORINFO_RESOLVED_TOKEN * pResolvedToken, CORINFO_RESOLVED_TOKEN * pConstrainedResolvedToken);

    ZapImport * GetDynamicHelperCell(CORCOMPILE_FIXUP_BLOB_KIND kind, CORINFO_CLASS_HANDLE handle);
    ZapImport * GetDynamicHelperCell(CORCOMPILE_FIXUP_BLOB_KIND kind, CORINFO_METHOD_HANDLE handle, CORINFO_RESOLVED_TOKEN * pResolvedToken, CORINFO_CLASS_HANDLE delegateType = NULL);
    ZapImport * GetDynamicHelperCell(CORCOMPILE_FIXUP_BLOB_KIND kind, CORINFO_FIELD_HANDLE handle, CORINFO_RESOLVED_TOKEN * pResolvedToken);

    ZapImport * GetDictionaryLookupCell(CORCOMPILE_FIXUP_BLOB_KIND kind, CORINFO_RESOLVED_TOKEN * pResolvedToken, CORINFO_LOOKUP_KIND * pLookup);

#ifdef FEATURE_READYTORUN_COMPILER
    ZapNode * GetPlacedIndirectHelperThunk(ReadyToRunHelper helperNum, PVOID pArg = NULL, ZapNode * pCell = NULL);
    ZapNode * GetIndirectHelperThunk(ReadyToRunHelper helperNum, PVOID pArg = NULL);
    void PlaceIndirectHelperThunk(ZapNode * pImport);

    ZapImport * GetPlacedHelperImport(ReadyToRunHelper helperNum);
    ZapImport * GetHelperImport(ReadyToRunHelper helperNum);
#endif

    virtual DWORD GetSize()
    {
        return m_modules.GetCount() * sizeof(CORCOMPILE_IMPORT_TABLE_ENTRY);
    }

    virtual UINT GetAlignment()
    {
        return sizeof(DWORD);
    }

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_ImportTable;
    }

    virtual void Save(ZapWriter * pZapWriter);
};

//
// CORCOMPILE_CODE_IMPORT_SECTION
//
class ZapImportSectionsTable : public ZapNode
{
    struct ImportSection
    {
        ZapVirtualSection * m_pSection;
        ZapNode * m_pSignatures;
        ZapNode * m_pAuxiliaryData;
        USHORT    m_Flags;
        BYTE      m_Type;
        BYTE      m_EntrySize;
    };

    SArray<ImportSection> m_ImportSectionsTable;

public:
    ZapImportSectionsTable(ZapImage * pImage)
    {
    }

    COUNT_T Append(BYTE Type, USHORT Flags, BYTE EntrySize, ZapVirtualSection * pSection, ZapNode * pSignatures = NULL, ZapNode * pAuxiliaryData = NULL);

    virtual UINT GetAlignment()
    {
        return sizeof(DWORD);
    }

    virtual DWORD GetSize();

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_ImportSectionsTable;
    }

    virtual void Save(ZapWriter * pZapWriter);
};

//
// ZapImportSectionSignatures contains an array of signature RVAs for given import section.
//
class ZapImportSectionSignatures : public ZapNode
{
    ZapVirtualSection * m_pImportSection;
    ZapGCRefMapTable * m_pGCRefMapTable;

    DWORD m_dwIndex;

    ZapImage * m_pImage;

public:
    ZapImportSectionSignatures(ZapImage * pImage, ZapVirtualSection * pImportSection, ZapVirtualSection * pGCSection = NULL);
    ~ZapImportSectionSignatures();

    void PlaceExternalMethodThunk(ZapImport * pImport);
    void PlaceExternalMethodCell(ZapImport * pImport);
    void PlaceStubDispatchCell(ZapImport * pImport);
    void PlaceDynamicHelperCell(ZapImport * pImport);

    virtual DWORD GetSize();

    virtual UINT GetAlignment()
    {
        return sizeof(DWORD);
    }

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_ImportSectionSignatures;
    }

    virtual void Save(ZapWriter * pZapWriter);
};

#include "gcrefmap.h"

class ZapGCRefMapTable : public ZapNode
{
    ZapImage * m_pImage;
    GCRefMapBuilder m_GCRefMapBuilder;
    COUNT_T m_nCount;

public:
    ZapGCRefMapTable(ZapImage * pImage)
        : m_pImage(pImage)
    {
    }

    void Append(CORINFO_METHOD_HANDLE handle);

    virtual DWORD GetSize();

    virtual UINT GetAlignment()
    {
        return sizeof(DWORD);
    }

    virtual ZapNodeType GetType()
    {
        return ZapNodeType_GCRefMapTable;
    }

    virtual void Save(ZapWriter * pZapWriter);
};

#endif // __ZAPIMPORT_H__