summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Reflection/Emit/ISymWrapperCore.cs
blob: 00fdd00315ef35ce5b9da2111667c489b240f109 (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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
// 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.

// 

#if FEATURE_CORECLR

namespace System.Reflection.Emit
{
    using System;
    using System.Security;
    using System.Runtime.InteropServices;
    using System.Runtime.CompilerServices;
    using System.Diagnostics.SymbolStore;


    //-----------------------------------------------------------------------------------
    // On Telesto, we don't ship the ISymWrapper.dll assembly. However, ReflectionEmit
    // relies on that assembly to write out managed PDBs.
    //
    // This file implements the minimum subset of ISymWrapper.dll required to restore
    // that functionality. Namely, the SymWriter and SymDocumentWriter objects.
    // 
    // Ideally we wouldn't need ISymWrapper.dll on desktop either - it's an ugly piece
    // of legacy.  We could just use this (or COM-interop code) everywhere, but we might
    // have to worry about compatibility.
    // 
    // We've now got a real implementation even when no debugger is attached.  It's
    // up to the runtime to ensure it doesn't provide us with an insecure writer 
    // (eg. diasymreader) in the no-trust scenarios (no debugger, partial-trust code).
    //-----------------------------------------------------------------------------------


    //------------------------------------------------------------------------------
    // SymWrapperCore is never instantiated and is used as an encapsulation class.
    // It is our "ISymWrapper.dll" assembly within an assembly.
    //------------------------------------------------------------------------------
    class SymWrapperCore
    {
        //------------------------------------------------------------------------------
        // Block instantiation
        //------------------------------------------------------------------------------
        private SymWrapperCore()
        {
        }

        //------------------------------------------------------------------------------
        // Implements Telesto's version of SymDocumentWriter (in the desktop world,
        // this type is exposed from ISymWrapper.dll.)
        //
        // The only thing user code can do with this wrapper is to receive it from
        // SymWriter.DefineDocument and pass it back to SymWriter.DefineSequencePoints.
        //------------------------------------------------------------------------------
        private unsafe class SymDocumentWriter : ISymbolDocumentWriter
        {
    
            //------------------------------------------------------------------------------
            // Ctor
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            internal SymDocumentWriter(PunkSafeHandle pDocumentWriterSafeHandle)
            {
                m_pDocumentWriterSafeHandle = pDocumentWriterSafeHandle;
                // The handle is actually a pointer to a native ISymUnmanagedDocumentWriter.
                m_pDocWriter = (ISymUnmanagedDocumentWriter *)m_pDocumentWriterSafeHandle.DangerousGetHandle();
                m_vtable = (ISymUnmanagedDocumentWriterVTable)(Marshal.PtrToStructure(m_pDocWriter->m_unmanagedVTable, typeof(ISymUnmanagedDocumentWriterVTable)));
            }
    
            //------------------------------------------------------------------------------
            // Returns the underlying ISymUnmanagedDocumentWriter* (as a safehandle.)
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            internal PunkSafeHandle GetUnmanaged()
            {
                return m_pDocumentWriterSafeHandle;
            }
    

            //=========================================================================================
            // Public interface methods start here. (Well actually, they're all NotSupported
            // stubs since that's what they are on the real ISymWrapper.dll.)
            //=========================================================================================

            //------------------------------------------------------------------------------
            // SetSource() wrapper
            //------------------------------------------------------------------------------
            void ISymbolDocumentWriter.SetSource(byte[] source)
            {
                throw new NotSupportedException();   // Intentionally not supported to match desktop CLR
            }
    
            //------------------------------------------------------------------------------
            // SetCheckSum() wrapper
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecuritySafeCritical]
            #endif
            void ISymbolDocumentWriter.SetCheckSum(Guid algorithmId, byte [] checkSum)
            {
                int hr = m_vtable.SetCheckSum(m_pDocWriter, algorithmId, (uint)checkSum.Length, checkSum);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
    
            [System.Security.SecurityCritical]
            private delegate int DSetCheckSum(ISymUnmanagedDocumentWriter * pThis, Guid algorithmId, uint checkSumSize, [In] byte[] checkSum);

            //------------------------------------------------------------------------------
            // This layout must match the unmanaged ISymUnmanagedDocumentWriter* COM vtable
            // exactly. If a member is declared as an IntPtr rather than a delegate, it means
            // we don't call that particular member.
            //------------------------------------------------------------------------------
            [System.Security.SecurityCritical]
            [StructLayout(LayoutKind.Sequential)]
            private struct ISymUnmanagedDocumentWriterVTable
            {
                internal IntPtr                QueryInterface;
                internal IntPtr                AddRef;
                internal IntPtr                Release;

                internal IntPtr                SetSource;
                #if FEATURE_CORECLR
                [System.Security.SecurityCritical]
                #endif
                internal DSetCheckSum          SetCheckSum;
            }
    
            //------------------------------------------------------------------------------
            // This layout must match the (start) of the unmanaged ISymUnmanagedDocumentWriter
            // COM object.
            //------------------------------------------------------------------------------
            [System.Security.SecurityCritical]
            [StructLayout(LayoutKind.Sequential)]
            private struct ISymUnmanagedDocumentWriter
            {
                internal IntPtr m_unmanagedVTable;
            }

            //------------------------------------------------------------------------------
            // Stores underlying ISymUnmanagedDocumentWriter* pointer (wrapped in a safehandle.)
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            private PunkSafeHandle m_pDocumentWriterSafeHandle;

            [SecurityCritical]
            private ISymUnmanagedDocumentWriter * m_pDocWriter;

            //------------------------------------------------------------------------------
            // Stores the "managed vtable" (actually a structure full of delegates that
            // P/Invoke to the corresponding unmanaged COM methods.)
            //------------------------------------------------------------------------------
            [SecurityCritical]
            private ISymUnmanagedDocumentWriterVTable m_vtable;

    
        } // class SymDocumentWriter


        //------------------------------------------------------------------------------
        // Implements Telesto's version of SymWriter (in the desktop world,
        // this type is expored from ISymWrapper.dll.)
        //------------------------------------------------------------------------------
        internal unsafe class SymWriter : ISymbolWriter
        {


            //------------------------------------------------------------------------------
            // Creates a SymWriter. The SymWriter is a managed wrapper around the unmanaged 
            // symbol writer provided by the runtime (ildbsymlib or diasymreader.dll).
            //------------------------------------------------------------------------------
            internal static ISymbolWriter CreateSymWriter()
            {
                return new SymWriter();
            }


            //------------------------------------------------------------------------------
            // Basic ctor. You'd think this ctor would take the unmanaged symwriter object as an argument
            // but to fit in with existing desktop code, the unmanaged writer is passed in 
            // through a subsequent call to InternalSetUnderlyingWriter
            //------------------------------------------------------------------------------
            private SymWriter()
            {
            }

            //=========================================================================================
            // Public interface methods start here.
            //=========================================================================================


            //------------------------------------------------------------------------------
            // Initialize() wrapper
            //------------------------------------------------------------------------------
            void ISymbolWriter.Initialize(IntPtr emitter, String filename, bool fFullBuild)
            {
                int hr = m_vtable.Initialize(m_pWriter, emitter, filename, (IntPtr)0, fFullBuild);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
            
            //------------------------------------------------------------------------------
            // DefineDocument() wrapper
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            ISymbolDocumentWriter ISymbolWriter.DefineDocument(String url,
                                                               Guid language,
                                                               Guid languageVendor,
                                                               Guid documentType)
            {
                PunkSafeHandle psymUnmanagedDocumentWriter = new PunkSafeHandle();

                int hr = m_vtable.DefineDocument(m_pWriter, url, ref language, ref languageVendor, ref documentType, out psymUnmanagedDocumentWriter);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
                if (psymUnmanagedDocumentWriter.IsInvalid)
                {
                    return null;
                }
                return new SymDocumentWriter(psymUnmanagedDocumentWriter);
            }
        
            //------------------------------------------------------------------------------
            // SetUserEntryPoint() wrapper
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            void ISymbolWriter.SetUserEntryPoint(SymbolToken entryMethod)
            {
                int hr = m_vtable.SetUserEntryPoint(m_pWriter, entryMethod.GetToken());
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
        
            //------------------------------------------------------------------------------
            // OpenMethod() wrapper
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            void ISymbolWriter.OpenMethod(SymbolToken method)
            {
                int hr = m_vtable.OpenMethod(m_pWriter, method.GetToken());
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
        
            //------------------------------------------------------------------------------
            // CloseMethod() wrapper
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            void ISymbolWriter.CloseMethod()
            {
                int hr = m_vtable.CloseMethod(m_pWriter);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
        
            //------------------------------------------------------------------------------
            // DefineSequencePoints() wrapper
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            void ISymbolWriter.DefineSequencePoints(ISymbolDocumentWriter document,
                                                    int[] offsets,
                                                    int[] lines,
                                                    int[] columns,
                                                    int[] endLines,
                                                    int[] endColumns)
            {
                int spCount = 0;
                if (offsets != null)
                {
                    spCount = offsets.Length;
                }
                else if (lines != null)
                {
                    spCount = lines.Length;
                }
                else if (columns != null)
                {
                    spCount = columns.Length;
                }
                else if (endLines != null)
                {
                    spCount = endLines.Length;
                }
                else if (endColumns != null)
                {
                    spCount = endColumns.Length;
                }
                if (spCount == 0)
                {
                    return;
                }
                if ( (offsets != null && offsets.Length != spCount) ||
                     (lines != null && lines.Length != spCount)     ||
                     (columns != null && columns.Length != spCount) ||
                     (endLines != null && endLines.Length != spCount) ||
                     (endColumns != null && endColumns.Length != spCount) )
                {
                    throw new ArgumentException();
                }

                // Sure, claim to accept any type that implements ISymbolDocumentWriter but the only one that actually
                // works is the one returned by DefineDocument. The desktop ISymWrapper commits the same signature fraud.
                // Ideally we'd just return a sealed opaque cookie type, which had an internal accessor to
                // get the writer out.
                // Regardless, this cast is important for security - we cannot allow our caller to provide
                // arbitrary instances of this interface.
                SymDocumentWriter docwriter = (SymDocumentWriter)document;
                int hr = m_vtable.DefineSequencePoints(m_pWriter, docwriter.GetUnmanaged(), spCount, offsets, lines, columns, endLines, endColumns);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }

            }
        
            //------------------------------------------------------------------------------
            // OpenScope() wrapper
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            int ISymbolWriter.OpenScope(int startOffset)
            {
                int ret;
                int hr = m_vtable.OpenScope(m_pWriter, startOffset, out ret);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
                return ret;
            }
        
            //------------------------------------------------------------------------------
            // CloseScope() wrapper
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            void ISymbolWriter.CloseScope(int endOffset)
            {
                int hr = m_vtable.CloseScope(m_pWriter, endOffset);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
        
            //------------------------------------------------------------------------------
            // SetScopeRange() wrapper
            //------------------------------------------------------------------------------
            void ISymbolWriter.SetScopeRange(int scopeID, int startOffset, int endOffset)
            {
                int hr = m_vtable.SetScopeRange(m_pWriter, scopeID, startOffset, endOffset);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
    
            //------------------------------------------------------------------------------
            // DefineLocalVariable() wrapper
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            void ISymbolWriter.DefineLocalVariable(String name,
                                                   FieldAttributes attributes,
                                                   byte[] signature,
                                                   SymAddressKind addrKind,
                                                   int addr1,
                                                   int addr2,
                                                   int addr3,
                                                   int startOffset,
                                                   int endOffset)
            {
                int hr = m_vtable.DefineLocalVariable(m_pWriter,
                                                      name,
                                                      (int)attributes,
                                                      signature.Length,
                                                      signature,
                                                      (int)addrKind,
                                                      addr1,
                                                      addr2,
                                                      addr3,
                                                      startOffset,
                                                      endOffset);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
        
            //------------------------------------------------------------------------------
            // DefineParameter() wrapper
            //------------------------------------------------------------------------------
            void ISymbolWriter.DefineParameter(String name,
                                               ParameterAttributes attributes,
                                               int sequence,
                                               SymAddressKind addrKind,
                                               int addr1,
                                               int addr2,
                                               int addr3)
            {
                throw new NotSupportedException();  // Intentionally not supported to match desktop CLR
            }
        
            //------------------------------------------------------------------------------
            // DefineField() wrapper
            //------------------------------------------------------------------------------
            void ISymbolWriter.DefineField(SymbolToken parent,
                                           String name,
                                           FieldAttributes attributes,
                                           byte[] signature,
                                           SymAddressKind addrKind,
                                           int addr1,
                                           int addr2,
                                           int addr3)
            {
                throw new NotSupportedException();  // Intentionally not supported to match desktop CLR
            }
        
            //------------------------------------------------------------------------------
            // DefineGlobalVariable() wrapper
            //------------------------------------------------------------------------------
            void ISymbolWriter.DefineGlobalVariable(String name,
                                                    FieldAttributes attributes,
                                                    byte[] signature,
                                                    SymAddressKind addrKind,
                                                    int addr1,
                                                    int addr2,
                                                    int addr3)
            {
                throw new NotSupportedException();  // Intentionally not supported to match desktop CLR
            }
        
            //------------------------------------------------------------------------------
            // Close() wrapper
            //------------------------------------------------------------------------------
            void ISymbolWriter.Close()
            {
                int hr = m_vtable.Close(m_pWriter);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
        
            //------------------------------------------------------------------------------
            // SetSymAttribute() wrapper
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            void ISymbolWriter.SetSymAttribute(SymbolToken parent, String name, byte[] data)
            {
                int hr = m_vtable.SetSymAttribute(m_pWriter, parent.GetToken(), name, data.Length, data);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
        
            //------------------------------------------------------------------------------
            // OpenNamespace() wrapper
            //------------------------------------------------------------------------------
            void ISymbolWriter.OpenNamespace(String name)
            {
                int hr = m_vtable.OpenNamespace(m_pWriter, name);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
        
            //------------------------------------------------------------------------------
            // CloseNamespace() wrapper
            //------------------------------------------------------------------------------
            void ISymbolWriter.CloseNamespace()
            {
                int hr = m_vtable.CloseNamespace(m_pWriter);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
        
            //------------------------------------------------------------------------------
            // UsingNamespace() wrapper
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            void ISymbolWriter.UsingNamespace(String name)
            {
                int hr = m_vtable.UsingNamespace(m_pWriter, name);
                if (hr < 0)
                {
                    throw Marshal.GetExceptionForHR(hr);
                }
            }
            
            //------------------------------------------------------------------------------
            // SetMethodSourceRange() wrapper
            //------------------------------------------------------------------------------
            void ISymbolWriter.SetMethodSourceRange(ISymbolDocumentWriter startDoc,
                                                    int startLine,
                                                    int startColumn,
                                                    ISymbolDocumentWriter endDoc,
                                                    int endLine,
                                                    int endColumn)
            {
                throw new NotSupportedException();   // Intentionally not supported to match desktop CLR
            }
    
            //------------------------------------------------------------------------------
            // SetUnderlyingWriter() wrapper.
            //------------------------------------------------------------------------------
            void ISymbolWriter.SetUnderlyingWriter(IntPtr ppUnderlyingWriter)
            {
                throw new NotSupportedException();   // Intentionally not supported on Telesto as it's a very unsafe api
            }

            //------------------------------------------------------------------------------
            // InternalSetUnderlyingWriter() wrapper.
            //
            // Furnishes the native ISymUnmanagedWriter* pointer. 
            //
            // The parameter is actually a pointer to a pointer to an ISymUnmanagedWriter. As
            // with the real ISymWrapper.dll, ISymWrapper performs *no* Release (or AddRef) on pointers
            // furnished through SetUnderlyingWriter. Lifetime management is entirely up to the caller.
            //------------------------------------------------------------------------------
            #if FEATURE_CORECLR
            [System.Security.SecurityCritical] // auto-generated
            #endif
            internal void InternalSetUnderlyingWriter(IntPtr ppUnderlyingWriter)
            {
                m_pWriter = *((ISymUnmanagedWriter**)ppUnderlyingWriter);
                m_vtable  = (ISymUnmanagedWriterVTable) (Marshal.PtrToStructure(m_pWriter->m_unmanagedVTable, typeof(ISymUnmanagedWriterVTable)));
            }

            //------------------------------------------------------------------------------
            // Define delegates for the unmanaged COM methods we invoke.
            //------------------------------------------------------------------------------
            [System.Security.SecurityCritical]
            private delegate int DInitialize(ISymUnmanagedWriter*                     pthis,
                                             IntPtr                                   emitter,  //IUnknown*
                                             [MarshalAs(UnmanagedType.LPWStr)] String filename, //WCHAR*
                                             IntPtr                                   pIStream, //IStream*
                                             [MarshalAs(UnmanagedType.Bool)] bool     fFullBuild
                                             );

            [System.Security.SecurityCritical]
            private delegate int DDefineDocument(ISymUnmanagedWriter*                     pthis,
                                                 [MarshalAs(UnmanagedType.LPWStr)] String url,
                                                 [In] ref Guid                            language,
                                                 [In] ref Guid                            languageVender,
                                                 [In] ref Guid                            documentType,
                                                 [Out] out PunkSafeHandle                 ppsymUnmanagedDocumentWriter
                                                );
                                                                              
            [System.Security.SecurityCritical]
            private delegate int DSetUserEntryPoint(ISymUnmanagedWriter* pthis, int entryMethod);
            [System.Security.SecurityCritical]
            private delegate int DOpenMethod(ISymUnmanagedWriter* pthis, int entryMethod);
            [System.Security.SecurityCritical]
            private delegate int DCloseMethod(ISymUnmanagedWriter* pthis);

            [System.Security.SecurityCritical]
            private delegate int DDefineSequencePoints(ISymUnmanagedWriter* pthis,
                                                       PunkSafeHandle       document,
                                                       int                  spCount,
                                                       [In] int[]           offsets,
                                                       [In] int[]           lines,
                                                       [In] int[]           columns,
                                                       [In] int[]           endLines,
                                                       [In] int[]           endColumns);

            [System.Security.SecurityCritical]
            private delegate int DOpenScope(ISymUnmanagedWriter* pthis, int startOffset, [Out] out int pretval);
            [System.Security.SecurityCritical]
            private delegate int DCloseScope(ISymUnmanagedWriter* pthis, int endOffset);

            [System.Security.SecurityCritical]
            private delegate int DSetScopeRange(ISymUnmanagedWriter* pthis, int scopeID, int startOffset, int endOffset);

            [System.Security.SecurityCritical]
            private delegate int DDefineLocalVariable(ISymUnmanagedWriter*                     pthis,
                                                      [MarshalAs(UnmanagedType.LPWStr)] String name,
                                                      int                                      attributes,
                                                      int                                      cSig,
                                                      [In] byte[]                              signature,
                                                      int                                      addrKind,
                                                      int                                      addr1,
                                                      int                                      addr2,
                                                      int                                      addr3,
                                                      int                                      startOffset,
                                                      int                                      endOffset
                                                     );

            [System.Security.SecurityCritical]
            private delegate int DClose(ISymUnmanagedWriter* pthis);

            [System.Security.SecurityCritical]
            private delegate int DSetSymAttribute(ISymUnmanagedWriter*                     pthis,
                                                  int                                      parent,
                                                  [MarshalAs(UnmanagedType.LPWStr)] String name,
                                                  int                                      cData,
                                                  [In] byte[]                              data
                                                 );


            [System.Security.SecurityCritical]
            private delegate int DOpenNamespace(ISymUnmanagedWriter* pthis, [MarshalAs(UnmanagedType.LPWStr)] String name);
            [System.Security.SecurityCritical]
            private delegate int DCloseNamespace(ISymUnmanagedWriter* pthis);
            [System.Security.SecurityCritical]
            private delegate int DUsingNamespace(ISymUnmanagedWriter* pthis, [MarshalAs(UnmanagedType.LPWStr)] String name);



            //------------------------------------------------------------------------------
            // This layout must match the unmanaged ISymUnmanagedWriter* COM vtable
            // exactly. If a member is declared as an IntPtr rather than a delegate, it means
            // we don't call that particular member.
            //------------------------------------------------------------------------------
            [StructLayout(LayoutKind.Sequential)]
            private struct ISymUnmanagedWriterVTable
            {
                internal IntPtr                QueryInterface;
                internal IntPtr                AddRef;
                internal IntPtr                Release;

                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DDefineDocument       DefineDocument;
                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DSetUserEntryPoint    SetUserEntryPoint;

                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DOpenMethod           OpenMethod;
                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DCloseMethod          CloseMethod;

                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DOpenScope            OpenScope;
                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DCloseScope           CloseScope;

                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DSetScopeRange        SetScopeRange;

                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DDefineLocalVariable  DefineLocalVariable;
                internal IntPtr                DefineParameter;
                internal IntPtr                DefineField;
                internal IntPtr                DefineGlobalVariable;

                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DClose                Close;
                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DSetSymAttribute      SetSymAttribute;

                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DOpenNamespace        OpenNamespace;
                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DCloseNamespace       CloseNamespace;
                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DUsingNamespace       UsingNamespace;

                internal IntPtr                SetMethodSourceRange;
                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DInitialize           Initialize;
                internal IntPtr                GetDebugInfo;
                #if FEATURE_CORECLR
                [System.Security.SecurityCritical] // auto-generated
                #endif
                internal DDefineSequencePoints DefineSequencePoints;
        
            }

            //------------------------------------------------------------------------------
            // This layout must match the (start) of the unmanaged ISymUnmanagedWriter
            // COM object.
            //------------------------------------------------------------------------------
            [StructLayout(LayoutKind.Sequential)]
            private struct ISymUnmanagedWriter
            {
                internal IntPtr m_unmanagedVTable;
            }

            //------------------------------------------------------------------------------
            // Stores native ISymUnmanagedWriter* pointer.
            //
            // As with the real ISymWrapper.dll, ISymWrapper performs *no* Release (or AddRef) on this pointer.
            // Managing lifetime is up to the caller (coreclr.dll).
            //------------------------------------------------------------------------------
            [SecurityCritical]
            private ISymUnmanagedWriter      *m_pWriter;

            //------------------------------------------------------------------------------
            // Stores the "managed vtable" (actually a structure full of delegates that
            // P/Invoke to the corresponding unmanaged COM methods.)
            //------------------------------------------------------------------------------
            private ISymUnmanagedWriterVTable m_vtable;

        } // class SymWriter




    } //class SymWrapperCore



    //--------------------------------------------------------------------------------------
    // SafeHandle for RAW MTA IUnknown's.
    //
    // ! Because the Release occurs in the finalizer thread, this safehandle really takes
    // ! an ostrich approach to apartment issues. We only tolerate this here because we're emulating
    // ! the desktop CLR's use of ISymWrapper which also pays lip service to COM apartment rules.
    // !
    // ! However, think twice about pulling this safehandle out for other uses.
    //
    // Had to make this a non-nested class since FCall's don't like to bind to nested classes.
    //--------------------------------------------------------------------------------------
    #if FEATURE_CORECLR
    [System.Security.SecurityCritical] // auto-generated
    #endif
    sealed class PunkSafeHandle : SafeHandle
    {
        #if FEATURE_CORECLR
        [System.Security.SecurityCritical] // auto-generated
        #endif
        internal PunkSafeHandle()
            : base((IntPtr)0, true)
        {
        }

        [SecurityCritical]
        override protected bool ReleaseHandle()
        {
            m_Release(handle);
            return true;
        }

        public override bool IsInvalid
        {
            [SecurityCritical]
            get { return handle == ((IntPtr)0); }
        }

        private delegate void DRelease(IntPtr punk);         // Delegate type for P/Invoking to coreclr.dll and doing an IUnknown::Release()
        private static DRelease m_Release;

        [MethodImplAttribute(MethodImplOptions.InternalCall)]
        private static extern IntPtr nGetDReleaseTarget();     // FCall gets us the native DRelease target (so we don't need named dllexport from coreclr.dll)

        #if FEATURE_CORECLR
        [System.Security.SecurityCritical] // auto-generated
        #endif
        static PunkSafeHandle()
        {
            m_Release = (DRelease)(Marshal.GetDelegateForFunctionPointer(nGetDReleaseTarget(), typeof(DRelease)));
            m_Release((IntPtr)0); // make one call to make sure the delegate is fully prepped before we're in the critical finalizer situation.
        }

    } // PunkSafeHandle

} //namespace System.Reflection.Emit


#endif //FEATURE_CORECLR