samedi 9 mai 2015

Very Specific Unresolved External Symbol type_info::`vftable When using Virtual Functions in /NODEFAULTLIB executable

I have built a executable with /nodefaultlib for learning purposes, but I feel I have done something wrong in the entrypoint before winmain because whenever I use virtual methods I receive the linker error:

error LNK2001: unresolved external symbol "const type_info::`vftable'" (??_7type_info@@6B@)

Here is my entry point:

typedef void (__cdecl *_PVFV)(void);
typedef int (__cdecl *_PIFV)(void);

// Standard C++ Runtime (STD CRT) __xc_a points to beginning of initializer table
#pragma data_seg(".CRT$XCA")
_PVFV __xc_a[] = { 0 };

// Standard C++ Runtime (STD CRT) __xc_z points to end of initializer table
#pragma data_seg(".CRT$XCZ")
_PVFV __xc_z[] = { 0 };

#pragma data_seg(".CRT$XIA")
_PIFV __xi_a[] = {0};

#pragma data_seg(".CRT$XIZ")
_PIFV __xi_z[] = {0};

#pragma data_seg(".CRT$XPA")
_PVFV __xp_a[] = {0};

#pragma data_seg(".CRT$XPZ")
_PVFV __xp_z[] = {0};

#pragma data_seg(".CRT$XTA")
_PVFV __xt_a[] = {0};

#pragma data_seg(".CRT$XTZ")
_PVFV __xt_z[] = {0};

#pragma data_seg()

#pragma comment(linker, "/MERGE:.CRT=.rdata")
#pragma comment(linker, "/MERGE:.rdata=.data")

// function pointer table to global deinitializer table
static _PVFV * pf_atexitlist = 0;

// Maximum entries allowed in table
static unsigned max_atexitlist_entries = 32;

// Current amount of entries in table //
static unsigned cur_atexitlist_entries = 0;


void __cdecl _initterm ( _PVFV *pfbegin, _PVFV *pfend ) 
{
    for ( ; pfbegin < pfend; pfbegin++ ) 
    {
        if ( *pfbegin != nullptr ) (**pfbegin)();
    }
}

void _cdecl Exit () {

    // Go through the list, and execute all global exit routines
    while (cur_atexitlist_entries--) {

            // execute function
            (*(--pf_atexitlist)) ();
    }
}

void _cdecl InitializeConstructors()
{
   _initterm(__xc_a, __xc_z); 
}


int WINAPI __tmainCRTStartup(void) 
{
//This is obviously lacking
    InitializeConstructors();

    WinMain((HINSTANCE)0x400000,0,0,0);

    Exit ();

    return 0;
}

I feel there is something very clearly wrong in __tmainCRTStartup, as I believe I have done the initialization of a dll not an exe. But still I'm a little lost on what it is that I am missing.

Aucun commentaire:

Enregistrer un commentaire