summaryrefslogtreecommitdiff
path: root/src/md/ceefilegen/cceegen.cpp
blob: 268093cd6b49735f94d7423fd03f493b673cf63c (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
// 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.

// 


#include "stdafx.h"

#include "corerror.h"

#ifdef EnC_SUPPORTED
#define ENC_DELTA_HACK
#endif


//*****************************************************************************
// Creation for new CCeeGen instances
//
// Both allocate and call virtual Init() (Can't call v-func in a ctor, 
// but we want to create in 1 call);
//*****************************************************************************

HRESULT STDMETHODCALLTYPE CreateICeeGen(REFIID riid, void **pCeeGen)
{
    if (riid != IID_ICeeGen)
        return E_NOTIMPL;
    if (!pCeeGen)
        return E_POINTER;
    CCeeGen *pCeeFileGen;
    HRESULT hr = CCeeGen::CreateNewInstance(pCeeFileGen);
    if (FAILED(hr))
        return hr;
    pCeeFileGen->AddRef();
    *(CCeeGen**)pCeeGen = pCeeFileGen;
    return S_OK;
}

HRESULT CCeeGen::CreateNewInstance(CCeeGen* & pGen) // static, public
{
    pGen = new CCeeGen();
    _ASSERTE(pGen != NULL);
    TESTANDRETURNMEMORY(pGen);
    
    pGen->m_peSectionMan = new PESectionMan;    
    _ASSERTE(pGen->m_peSectionMan != NULL);
    TESTANDRETURNMEMORY(pGen->m_peSectionMan);

    HRESULT hr = pGen->m_peSectionMan->Init();
    TESTANDRETURNHR(hr);

    hr = pGen->Init();
    TESTANDRETURNHR(hr);

    return hr;

}

STDMETHODIMP CCeeGen::QueryInterface(REFIID riid, void** ppv)
{
    if (!ppv)
        return E_POINTER;

    *ppv = NULL;

    if (riid == IID_IUnknown)
        *ppv = (IUnknown*)(ICeeGen*)this;
    else if (riid == IID_ICeeGen)
        *ppv = (ICeeGen*)this;
    else if (riid == IID_ICeeGenInternal)
        *ppv = (ICeeGenInternal*)this;
    if (*ppv == NULL)
        return E_NOINTERFACE;
    AddRef();
    return S_OK;
}

STDMETHODIMP_(ULONG) CCeeGen::AddRef(void)
{
    return InterlockedIncrement(&m_cRefs);
}
 
STDMETHODIMP_(ULONG) CCeeGen::Release(void)
{
    if (InterlockedDecrement(&m_cRefs) == 0) {
        Cleanup();
        delete this;
        return 0;
    }
    return 1;
}

STDMETHODIMP CCeeGen::SetInitialGrowth(DWORD growth)
{
    getIlSection().SetInitialGrowth(growth);

    return S_OK;
}

STDMETHODIMP CCeeGen::EmitString (__in LPWSTR lpString, ULONG *RVA)
{
    HRESULT hr = S_OK;
    BEGIN_ENTRYPOINT_NOTHROW;

    if (! RVA)
        IfFailGo(E_POINTER);
    hr = getStringSection().getEmittedStringRef(lpString, RVA);
ErrExit:
    
    END_ENTRYPOINT_NOTHROW;
    return hr;
}

STDMETHODIMP CCeeGen::GetString(ULONG RVA, __inout LPWSTR *lpString)
{
    HRESULT hr = E_FAIL;
    BEGIN_ENTRYPOINT_NOTHROW;

    if (! lpString)
        IfFailGo(E_POINTER);
    *lpString = (LPWSTR)getStringSection().computePointer(RVA);


ErrExit:

    END_ENTRYPOINT_NOTHROW;
    if (*lpString)
        return S_OK;
    return hr;
}

STDMETHODIMP CCeeGen::AllocateMethodBuffer(ULONG cchBuffer, UCHAR **lpBuffer, ULONG *RVA)
{
    HRESULT hr = S_OK;
    BEGIN_ENTRYPOINT_NOTHROW;

    ULONG methodOffset = 0;

    if (! cchBuffer)
        IfFailGo(E_INVALIDARG);
    if (! lpBuffer || ! RVA)
        IfFailGo(E_POINTER);
    *lpBuffer = (UCHAR*) getIlSection().getBlock(cchBuffer, 4); // Dword align
    IfNullGo(*lpBuffer);

        // have to compute the method offset after getting the block, not
        // before (since alignment might shift it up 
    // for in-memory, just return address and will calc later
    methodOffset = getIlSection().dataLen() - cchBuffer;

    *RVA = methodOffset;

ErrExit:
    END_ENTRYPOINT_NOTHROW;

    return hr;
}

STDMETHODIMP CCeeGen::GetMethodBuffer(ULONG RVA, UCHAR **lpBuffer)
{
    HRESULT hr = E_FAIL;
    BEGIN_ENTRYPOINT_NOTHROW;

    if (! lpBuffer)
        IfFailGo(E_POINTER);
    *lpBuffer = (UCHAR*)getIlSection().computePointer(RVA);


ErrExit:
    END_ENTRYPOINT_NOTHROW;

    if (lpBuffer != NULL && *lpBuffer != 0)
        return S_OK;

    return hr;
}

STDMETHODIMP CCeeGen::ComputePointer(HCEESECTION section, ULONG RVA, UCHAR **lpBuffer)
{
    HRESULT hr = E_FAIL;
    BEGIN_ENTRYPOINT_NOTHROW;

    if (! lpBuffer)
        IfFailGo(E_POINTER);
    *lpBuffer = (UCHAR*) ((CeeSection *)section)->computePointer(RVA);

ErrExit:
    END_ENTRYPOINT_NOTHROW;

    if (lpBuffer != NULL && *lpBuffer != 0)
        return S_OK;
    return hr;
}

STDMETHODIMP CCeeGen::GetIMapTokenIface (  
        IUnknown **pIMapToken)
{
    BEGIN_ENTRYPOINT_NOTHROW;

    _ASSERTE(!"E_NOTIMPL");
    END_ENTRYPOINT_NOTHROW;

    return E_NOTIMPL;
}

STDMETHODIMP CCeeGen::AddNotificationHandler (  
        IUnknown *pHandler)
{
    BEGIN_ENTRYPOINT_NOTHROW;
    _ASSERTE(!"E_NOTIMPL");
    END_ENTRYPOINT_NOTHROW;

    return E_NOTIMPL;
}

STDMETHODIMP CCeeGen::GenerateCeeFile ()
{
    BEGIN_ENTRYPOINT_NOTHROW;

    _ASSERTE(!"E_NOTIMPL");
    END_ENTRYPOINT_NOTHROW;

    return E_NOTIMPL;
}

STDMETHODIMP CCeeGen::GenerateCeeMemoryImage (void **)
{
    BEGIN_ENTRYPOINT_NOTHROW;

    _ASSERTE(!"E_NOTIMPL");
    END_ENTRYPOINT_NOTHROW;

    return E_NOTIMPL;
}

STDMETHODIMP CCeeGen::GetIlSection ( 
        HCEESECTION *section)
{
    BEGIN_ENTRYPOINT_NOTHROW;

    *section = (HCEESECTION)(m_sections[m_ilIdx]);
    END_ENTRYPOINT_NOTHROW;

    return S_OK;
}

STDMETHODIMP CCeeGen::GetStringSection(HCEESECTION *section)
{
    BEGIN_ENTRYPOINT_NOTHROW;

    _ASSERTE(!"E_NOTIMPL");
    END_ENTRYPOINT_NOTHROW;

    return E_NOTIMPL;
}

STDMETHODIMP CCeeGen::AddSectionReloc ( 
        HCEESECTION section, 
        ULONG offset, 
        HCEESECTION relativeTo, 
        CeeSectionRelocType relocType)
{
    HRESULT hr = S_OK;
    BEGIN_ENTRYPOINT_NOTHROW;

    hr = m_sections[m_ilIdx]->addSectReloc(offset, *(m_sections[m_ilIdx]), relocType);
    END_ENTRYPOINT_NOTHROW;
    return hr;
}

STDMETHODIMP CCeeGen::GetSectionCreate ( 
        const char *name, 
        DWORD flags, 
        HCEESECTION *section)
{
    HRESULT hr = S_OK;
    BEGIN_ENTRYPOINT_NOTHROW;

    short       sectionIdx;
    hr = getSectionCreate (name, flags, (CeeSection **)section, &sectionIdx);
    END_ENTRYPOINT_NOTHROW;
    return hr;
}

STDMETHODIMP CCeeGen::GetSectionDataLen ( 
        HCEESECTION section, 
        ULONG *dataLen)
{
    BEGIN_ENTRYPOINT_NOTHROW;

    CeeSection *pSection = (CeeSection*) section;
    *dataLen = pSection->dataLen();
    END_ENTRYPOINT_NOTHROW;

    return NOERROR;
}

STDMETHODIMP CCeeGen::GetSectionBlock ( 
        HCEESECTION section, 
        ULONG len, 
        ULONG align, 
        void **ppBytes)
{
    BEGIN_ENTRYPOINT_NOTHROW;

    CeeSection *pSection = (CeeSection*) section;
    *ppBytes = (BYTE *)pSection->getBlock(len, align);
    END_ENTRYPOINT_NOTHROW;

    if (*ppBytes == 0)
        return E_OUTOFMEMORY;
    return NOERROR;
}

STDMETHODIMP CCeeGen::TruncateSection ( 
        HCEESECTION section, 
        ULONG len)
{
    BEGIN_ENTRYPOINT_NOTHROW;

    _ASSERTE(!"E_NOTIMPL");
    END_ENTRYPOINT_NOTHROW;
    return E_NOTIMPL;
}



CCeeGen::CCeeGen() // protected ctor
{
// All other init done in InitCommon()
    m_cRefs = 0;
    m_peSectionMan = NULL;
    m_pTokenMap = NULL;
    m_pRemapHandler = NULL;

}

// Shared init code between derived classes, called by virtual Init()
HRESULT CCeeGen::Init() // not-virtual, protected
{
// Public, Virtual init must create our SectionManager, and 
// Common init does the rest
    _ASSERTE(m_peSectionMan != NULL);

    HRESULT hr = S_OK;
  
    PESection *section = NULL;
    CeeSection *ceeSection = NULL;

    m_corHeader = NULL;

    m_numSections = 0;
    m_allocSections = 10;
    m_sections = new CeeSection * [ m_allocSections ];
    if (m_sections == NULL) {
        hr = E_OUTOFMEMORY;
        goto LExit;
    }

    m_pTokenMap = NULL;
    m_fTokenMapSupported = FALSE;
    m_pRemapHandler = NULL;

    // These text section needs special support for handling string management now that we have
    // merged the sections together, so create it with an underlying CeeSectionString rather than the
    // more generic CeeSection

    hr = m_peSectionMan->getSectionCreate(".text", sdExecute, &section);
    if (FAILED(hr)) {
        goto LExit;
    }

    ceeSection = new CeeSectionString(*this, *section);
    if (ceeSection == NULL) {
        hr = E_OUTOFMEMORY;
        goto LExit;
    }

    hr = addSection(ceeSection, &m_stringIdx);

    m_textIdx = m_stringIdx;    

    m_metaIdx = m_textIdx;  // meta section is actually in .text
    m_ilIdx = m_textIdx;    // il section is actually in .text
    m_corHdrIdx = -1;
    m_encMode = FALSE;

LExit:
    if (FAILED(hr)) {
        Cleanup();
    }

    return hr;
}

// For EnC mode, generate strings into .rdata section rather than .text section
HRESULT CCeeGen::setEnCMode()
{
    PESection *section = NULL;
    HRESULT hr = m_peSectionMan->getSectionCreate(".rdata", sdExecute, &section);
    TESTANDRETURNHR(hr);
    CeeSection *ceeSection = new CeeSectionString(*this, *section);
    if (ceeSection == NULL)
    {
        return E_OUTOFMEMORY;
    }
    hr = addSection(ceeSection, &m_stringIdx);
    if (SUCCEEDED(hr))
        m_encMode = TRUE;
    return hr;
}


HRESULT CCeeGen::cloneInstance(CCeeGen *destination) { //public, virtual
    _ASSERTE(destination);
    
    destination->m_pTokenMap =          m_pTokenMap;
    destination->m_fTokenMapSupported = m_fTokenMapSupported;
    destination->m_pRemapHandler =      m_pRemapHandler;

    //Create a deep copy of the section manager (and each of it's sections);
    return m_peSectionMan->cloneInstance(destination->m_peSectionMan);
}

HRESULT CCeeGen::Cleanup() // virtual 
{
    HRESULT hr;
    for (int i = 0; i < m_numSections; i++) {
        delete m_sections[i];
    }

    delete [] m_sections;

    CeeGenTokenMapper *pMapper = m_pTokenMap;
    if (pMapper) {
        if (pMapper->m_pIImport) {
            IMetaDataEmit *pIIEmit;
            if (SUCCEEDED( hr = pMapper->m_pIImport->QueryInterface(IID_IMetaDataEmit, (void **) &pIIEmit)))
            {
                pIIEmit->SetHandler(NULL);
                pIIEmit->Release();
            }
            _ASSERTE(SUCCEEDED(hr));
            pMapper->m_pIImport->Release();
        }
        pMapper->Release();
        m_pTokenMap = NULL;
    }

    if (m_pRemapHandler)
    {
        m_pRemapHandler->Release();
        m_pRemapHandler = NULL;
    }

    if (m_peSectionMan) {
        m_peSectionMan->Cleanup();
        delete m_peSectionMan;
    }

    return S_OK;
}

HRESULT CCeeGen::addSection(CeeSection *section, short *sectionIdx)
{
    if (m_numSections >= m_allocSections)
    {
        _ASSERTE(m_allocSections > 0);
        while (m_numSections >= m_allocSections)
            m_allocSections <<= 1;
        CeeSection **newSections = new CeeSection * [m_allocSections];
        if (newSections == NULL)
            return E_OUTOFMEMORY;
        CopyMemory(newSections, m_sections, m_numSections * sizeof(*m_sections));
        if (m_sections != NULL)
            delete [] m_sections;
        m_sections = newSections;
    }

    if (sectionIdx)
        *sectionIdx = m_numSections;

    m_sections[m_numSections++] = section;
    return S_OK;
}

HRESULT CCeeGen::getSectionCreate (const char *name, DWORD flags, CeeSection **section, short *sectionIdx)
{
    if (strcmp(name, ".il") == 0)
        name = ".text";
    else if (strcmp(name, ".meta") == 0)
        name = ".text";
    else if (strcmp(name, ".rdata") == 0 && !m_encMode)
        name = ".text";
    for (int i=0; i<m_numSections; i++) {
        if (strcmp((const char *)m_sections[i]->name(), name) == 0) {
            if (section)
                *section = m_sections[i];
            if (sectionIdx)
                *sectionIdx = i;
            return S_OK;
        }
    }
    PESection *pewSect = NULL;
    HRESULT hr = m_peSectionMan->getSectionCreate(name, flags, &pewSect);
    TESTANDRETURNHR(hr);
    CeeSection *newSect = new CeeSection(*this, *pewSect);
    // if this fails, the PESection will get nuked in the destructor for CCeeGen
    if (newSect == NULL)
    {
        return E_OUTOFMEMORY;
    }
    
    hr = addSection(newSect, sectionIdx);
    TESTANDRETURNHR(hr);
    if (section)
        *section = newSect;
    return S_OK;
}


HRESULT CCeeGen::emitMetaData(IMetaDataEmit *emitter, CeeSection* section, DWORD offset, BYTE* buffer, unsigned buffLen)
{
    HRESULT hr = S_OK;

    ReleaseHolder<IStream> metaStream(NULL);
    
    IfFailRet((HRESULT)CreateStreamOnHGlobal(NULL, TRUE, &metaStream));
    
    if (! m_fTokenMapSupported) {
        IUnknown *pMapTokenIface;
        IfFailGoto(getMapTokenIface(&pMapTokenIface, emitter), Exit);

        // Set a callback for token remap and save the tokens which change.
        IfFailGoto(emitter->SetHandler(pMapTokenIface), Exit);
    }

    // generate the metadata
    IfFailGoto(emitter->SaveToStream(metaStream, 0), Exit);

    // get size of stream and get sufficient storage for it

    if (section == 0) {
        section = &getMetaSection();
        STATSTG statStg;
        IfFailGoto((HRESULT)(metaStream->Stat(&statStg, STATFLAG_NONAME)), Exit);

        buffLen = statStg.cbSize.u.LowPart;
        if(m_objSwitch)
        {
            CeeSection* pSect;
            DWORD flags = IMAGE_SCN_LNK_INFO | IMAGE_SCN_LNK_REMOVE | IMAGE_SCN_ALIGN_1BYTES; // 0x00100A00
            IfFailGoto(getSectionCreate(".cormeta",flags,&pSect,&m_metaIdx), Exit);
        }
        buffer = (BYTE *)section->getBlock(buffLen, sizeof(DWORD));
        IfNullGoto(buffer, Exit);
        offset = getMetaSection().dataLen() - buffLen;
    }
    else {
        _ASSERTE(buffer[buffLen-1] || true); // Dereference 'buffer'
        _ASSERTE(section->computeOffset(PCHAR(buffer)) == offset);
    }

    // reset seek pointer and read from stream
    {
        LARGE_INTEGER disp = { {0, 0} };
        IfFailGoto((HRESULT)metaStream->Seek(disp, STREAM_SEEK_SET, NULL), Exit);
    }
    ULONG metaDataLen;
    IfFailGoto((HRESULT)metaStream->Read(buffer, buffLen+1, &metaDataLen), Exit);

    _ASSERTE(metaDataLen <= buffLen);

#ifdef ENC_DELTA_HACK
    extern int __cdecl fclose(FILE *);
    WCHAR szFileName[256];
    DWORD len = GetEnvironmentVariable(W("COMP_ENC_EMIT"), szFileName, ARRAYSIZE(szFileName));
    _ASSERTE(len < (ARRAYSIZE(szFileName) + 6)); // +6 for the .dmeta
    if (len > 0 && len < (ARRAYSIZE(szFileName) + 6)) 
    {
        wcscat_s(szFileName, ARRAYSIZE(szFileName), W(".dmeta"));
        FILE *pDelta;
        int ec = _wfopen_s(&pDelta, szFileName, W("wb"));
        if (FAILED(ec)) { return HRESULT_FROM_WIN32(ERROR_OPEN_FAILED); }
        fwrite(buffer, 1, metaDataLen, pDelta);
        fclose(pDelta);
    }
#endif


    // Set meta virtual address to offset of metadata within .meta, and 
    // and add a reloc for this offset, which will get turned 
    // into an rva when the pewriter writes out the file. 

    m_corHeader->MetaData.VirtualAddress = VAL32(offset);
    getCorHeaderSection().addSectReloc(m_corHeaderOffset + offsetof(IMAGE_COR20_HEADER, MetaData), *section, srRelocAbsolute);
    m_corHeader->MetaData.Size = VAL32(metaDataLen);

Exit:
    
    if (! m_fTokenMapSupported) {
        // Remove the handler that we set
        hr = emitter->SetHandler(NULL);
    }

#ifdef _DEBUG
    if (FAILED(hr) && hr != E_OUTOFMEMORY)
        _ASSERTE(!"Unexpected Failure");
#endif
    
    return hr;
}

// Create the COM header - it goes at front of .meta section
// Need to do this before the meta data is copied in, but don't do at
// the same time because may not have metadata
HRESULT CCeeGen::allocateCorHeader()
{
    HRESULT hr = S_OK;
    CeeSection *corHeaderSection = NULL;
    if (m_corHdrIdx < 0) {
        hr = getSectionCreate(".text0", sdExecute, &corHeaderSection, &m_corHdrIdx);
        TESTANDRETURNHR(hr);
        
        m_corHeaderOffset = corHeaderSection->dataLen();
        m_corHeader = (IMAGE_COR20_HEADER*)corHeaderSection->getBlock(sizeof(IMAGE_COR20_HEADER));
        if (! m_corHeader)
            return E_OUTOFMEMORY;
        memset(m_corHeader, 0, sizeof(IMAGE_COR20_HEADER));
    }
    return S_OK;
}

HRESULT CCeeGen::getMethodRVA(ULONG codeOffset, ULONG *codeRVA)
{
    _ASSERTE(codeRVA);
    // for runtime conversion, just return the offset and will calculate real address when need the code
    *codeRVA = codeOffset;
    return S_OK;
} 

HRESULT CCeeGen::getMapTokenIface(IUnknown **pIMapToken, IMetaDataEmit *emitter) 
{
    if (! pIMapToken)
        return E_POINTER;
    if (! m_pTokenMap) {
        // Allocate the token mapper. As code is generated, each moved token will be added to
        // the mapper and the client will also add a TokenMap reloc for it so we can update later
        CeeGenTokenMapper *pMapper = new CeeGenTokenMapper;
        if (pMapper == NULL)
        {
            return E_OUTOFMEMORY;
        }
        
        if (emitter) {
            HRESULT hr;
            hr = emitter->QueryInterface(IID_IMetaDataImport, (PVOID *) &pMapper->m_pIImport);
            _ASSERTE(SUCCEEDED(hr));
        }
        m_pTokenMap = pMapper;
        m_fTokenMapSupported = (emitter == 0);

        // If we've been holding onto a token remap handler waiting
        // for the token mapper to get created, add it to the token
        // mapper now and release our hold on it.
        if (m_pRemapHandler && m_pTokenMap)
        {
            m_pTokenMap->AddTokenMapper(m_pRemapHandler);
            m_pRemapHandler->Release();
            m_pRemapHandler = NULL;
        }
    }
    *pIMapToken = getTokenMapper()->GetMapTokenIface();
    return S_OK;
}

HRESULT CCeeGen::addNotificationHandler(IUnknown *pHandler)
{
    // Null is no good...
    if (!pHandler)
        return E_POINTER;

    HRESULT hr = S_OK;
    IMapToken *pIMapToken = NULL;

    // Is this an IMapToken? If so, we can put it to good use...
    if (SUCCEEDED(pHandler->QueryInterface(IID_IMapToken,
                                           (void**)&pIMapToken)))
    {
        // You gotta have a token mapper to use an IMapToken, though.
        if (m_pTokenMap)
        {
            hr = m_pTokenMap->AddTokenMapper(pIMapToken);
            pIMapToken->Release();
        }
        else
        {
            // Hold onto it for later, just in case a token mapper
            // gets created. We're holding a reference to it here,
            // too.
            m_pRemapHandler = pIMapToken;
        }
    }

    return hr;
}