Changeset 14935


Ignore:
Timestamp:
Oct 2, 2000, 3:35:23 PM (25 years ago)
Author:
bird
Message:

Checked the docs on TLS. Now implemented according to docs.

Location:
tags/trunk/src/kernel32
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified tags/trunk/src/kernel32/winimagepe2lx.cpp

    r14924 r14935  
    1 /* $Id: winimagepe2lx.cpp,v 1.16 2000-10-02 04:00:35 bird Exp $ */
     1/* $Id: winimagepe2lx.cpp,v 1.17 2000-10-02 13:35:23 bird Exp $ */
    22
    33/*
     
    324324        {
    325325            PVOID pv;
    326             ULONG ulBorlandRVAFix = 0UL;
    327326
    328327            /*
    329              * Borland seems to have problems getting things right...
    330              *      Uses real pointers with baserelocations.
    331              * Needs to subtract image loadaddress to make the TLSDir them RVAs.
     328             * According to the docs StartAddressOfRawData and EndAddressOfRawData is
     329             * real pointers with a baserelocs.
    332330             *
    333              * We'll check if the StartAddressOfRawData pointer is an RVA or an real address by
    334              * check if it is within the TLS section or not.
    335              * ASSUMES: StartAddressOfRawData is in the same section as the TLS Directory.
     331             * The docs says nothing about the two AddressOf pointers. So, we'll assume that
     332             * these also are real pointers. But, we'll try compensate if they should not have
     333             * base realocations.
    336334             */
    337             if (paSections[iSection].ulRVA > pTLSDir->StartAddressOfRawData ||
    338                 paSections[iSection].ulRVA + paSections[iSection].cbVirtual <= pTLSDir->StartAddressOfRawData)
    339                 { /* StartAddressOfRawData was not an RVA within the same section as the TLS directory */
    340                 ulBorlandRVAFix = paSections[iSection].ulAddress - paSections[iSection].ulRVA;
     335            if (validateRealPointer((PVOID)pTLSDir->StartAddressOfRawData)
     336                &&
     337                validateRealPointer((PVOID)pTLSDir->EndAddressOfRawData)
     338                )
     339            {
     340                setTLSAddress((PVOID)pTLSDir->StartAddressOfRawData);
     341                setTLSInitSize(pTLSDir->EndAddressOfRawData - pTLSDir->StartAddressOfRawData);
     342                setTLSTotalSize(pTLSDir->EndAddressOfRawData - pTLSDir->StartAddressOfRawData + pTLSDir->SizeOfZeroFill);
     343
     344                if (pTLSDir->AddressOfIndex)
     345                {
     346                    if (validateRealPointer(pTLSDir->AddressOfIndex))
     347                        /* assume baserelocations for thepointer; use it without any change. */
     348                        setTLSIndexAddr((LPDWORD)(void*)pTLSDir->AddressOfIndex);
     349                    else
     350                    {   /* assume no baserelocs for these pointers? Complain and debugint3 */
     351                        eprintf(("Win32Pe2LxImage::init: TLS - AddressOfIndex(%#8x) is not a pointer with basereloc.\n",
     352                                 pTLSDir->AddressOfIndex));
     353                        pv = getPointerFromPointer(pTLSDir->AddressOfIndex);
     354                        if (pv == NULL)
     355                        {
     356                            eprintf(("Win32Pe2LxImage::init: invalid RVA to TLS AddressOfIndex - %#8x.\n",
     357                                     pTLSDir->AddressOfIndex));
     358                            return FALSE;
     359                        }
     360                        setTLSIndexAddr((LPDWORD)pv);
     361                    }
    341362                }
    342             pv = getPointerFromRVA(pTLSDir->StartAddressOfRawData - ulBorlandRVAFix);
    343             if (pv == NULL || pTLSDir->StartAddressOfRawData == 0UL)
    344             {
    345                 eprintf(("Win32Pe2LxImage::init: invalid RVA to TLS StartAddressOfRawData - %#8x.\n",
    346                          pTLSDir->StartAddressOfRawData));
    347                 return FALSE;
    348             }
    349             setTLSAddress(pv);
    350             setTLSInitSize(pTLSDir->EndAddressOfRawData - pTLSDir->StartAddressOfRawData);
    351             setTLSTotalSize(pTLSDir->EndAddressOfRawData - pTLSDir->StartAddressOfRawData + pTLSDir->SizeOfZeroFill);
    352             pv = getPointerFromRVA((ULONG)pTLSDir->AddressOfIndex - ulBorlandRVAFix);
    353             if (pv == NULL)
    354             {
    355                 eprintf(("Win32Pe2LxImage::init: invalid RVA to TLS AddressOffIndex - %#8x.\n",
    356                          pTLSDir->AddressOfIndex));
    357                 return FALSE;
    358             }
    359             setTLSIndexAddr((LPDWORD)pv);
    360             if (pTLSDir->AddressOfCallBacks != 0)
    361             {
    362                 pv = getPointerFromRVA((ULONG)pTLSDir->AddressOfCallBacks - ulBorlandRVAFix);
    363                 if (pv == NULL)
     363
     364                if (pTLSDir->AddressOfCallBacks)
    364365                {
    365                     eprintf(("Win32Pe2LxImage::init: invalid RVA to TLS AddressOffIndex - %#8x.\n",
    366                              pTLSDir->AddressOfIndex));
    367                     return FALSE;
     366                    if (validateRealPointer(pTLSDir->AddressOfCallBacks))
     367                        /* assume baserelocations for thepointer; use it without any change. */
     368                        setTLSCallBackAddr(pTLSDir->AddressOfCallBacks);
     369                    else
     370                    {   /* assume no baserelocs for these pointers? Complain and debugint3 */
     371                        eprintf(("Win32Pe2LxImage::init: Warning: TLS - AddressOfCallBacks(%#8x) is not a pointer with basereloc.\n",
     372                                 pTLSDir->AddressOfCallBacks));
     373                        pv = getPointerFromPointer(pTLSDir->AddressOfCallBacks);
     374                        if (pv == NULL)
     375                        {
     376                            eprintf(("Win32Pe2LxImage::init: invalid pointer to TLS AddressOfCallBacks - %#8x.\n",
     377                                     pTLSDir->AddressOfIndex));
     378                            return FALSE;
     379                        }
     380                        setTLSCallBackAddr((PIMAGE_TLS_CALLBACK*)pv);
     381                    }
    368382                }
    369                 setTLSCallBackAddr((PIMAGE_TLS_CALLBACK*)pv);
    370383            }
    371384        }
     
    700713{
    701714    int i;
     715
    702716    #ifdef DEBUG
    703         if (paSections == NULL)
    704             return NULL;
     717    if (paSections == NULL)
     718    {
     719        eprintf(("Win32Pe2LxImage::getPointerFromRVA: paSections is NULL!\n"));
     720        return NULL;
     721    }
    705722    #endif
    706723
     
    710727    i = 0;
    711728    while (i < cSections &&
    712            !(paSections[i].ulRVA <= ulRVA && paSections[i].ulRVA + paSections[i].cbVirtual > ulRVA)) /* ALIGN on page too? */
     729           (paSections[i].ulRVA > ulRVA || paSections[i].ulRVA + paSections[i].cbVirtual <= ulRVA)) /* ALIGN on page too? */
    713730        i++;
    714731
     
    717734
    718735    return (PVOID)(ulRVA - paSections[i].ulRVA + paSections[i].ulAddress);
     736}
     737
     738
     739/**
     740 * Converts a pointer with not basereloc to a pointer.
     741 * @returns Pointer with baserelocation applied.
     742 * @param   pv  Pointer without baserelocation.
     743 * @status  completely implemented.
     744 * @author  knut st. osmundsen (knut.stange.osmundsen@mynd.no)
     745 */
     746PVOID  Win32Pe2LxImage::getPointerFromPointer(PVOID pv)
     747{
     748    if (pv == NULL)
     749        return NULL;
     750
     751    return getPointerFromRVA((ULONG)pv - pNtHdrs->OptionalHeader.ImageBase);
    719752}
    720753
     
    739772{
    740773    LONG i;
     774
    741775    #ifdef DEBUG
    742         if (paSections == NULL)
    743             return -1;
     776    if (paSections == NULL)
     777    {
     778        eprintf(("Win32Pe2LxImage::getSectionIndexFromRVA: paSections is NULL!\n"));
     779        return NULL;
     780    }
    744781    #endif
    745782
     
    749786    i = 0;
    750787    while (i < cSections &&
    751            !(paSections[i].ulRVA <= ulRVA && paSections[i].ulRVA + paSections[i].cbVirtual > ulRVA)) /* ALIGN on page too? */
     788           (paSections[i].ulRVA > ulRVA && paSections[i].ulRVA + paSections[i].cbVirtual <= ulRVA)) /* ALIGN on page too? */
    752789        i++;
    753790
    754791    return i < cSections ? i : -1;
     792}
     793
     794
     795/**
     796 * Validates that a given pointer is pointing to valid memory within
     797 * the loaded executable image.
     798 * @returns TRUE if the pointer is valid.
     799 *          FALSE if the pointer is invalid.
     800 * @param   pv  Pointer to validate.
     801 * @sketch
     802 * @status  completely implemented.
     803 * @author  knut st. osmundsen (knut.stange.osmundsen@mynd.no)
     804 */
     805BOOL Win32Pe2LxImage::validateRealPointer(PVOID pv)
     806{
     807    int i;
     808
     809    #ifdef DEBUG
     810    if (paSections == NULL)
     811    {
     812        eprintf(("Win32Pe2LxImage::validateRealPointer: paSections is NULL!\n"));
     813        return NULL;
     814    }
     815    #endif
     816
     817    if (pv == NULL)
     818        return FALSE;
     819
     820    i = 0;
     821    while (i < cSections &&
     822           (paSections[i].ulAddress < (ULONG)pv ||
     823            paSections[i].ulAddress + paSections[i].cbVirtual <= (ULONG)pv) /* Align on page too? */
     824           )
     825        i++;
     826
     827    return i < cSections;
    755828}
    756829
  • TabularUnified tags/trunk/src/kernel32/winimagepe2lx.h

    r14924 r14935  
    1 /* $Id: winimagepe2lx.h,v 1.5 2000-10-02 04:00:36 bird Exp $ */
     1/* $Id: winimagepe2lx.h,v 1.6 2000-10-02 13:35:23 bird Exp $ */
    22
    33/*
     
    5757    /* these should be moved to winimagebase some day... */
    5858    PVOID    getPointerFromRVA(ULONG ulRVA);
     59    PVOID    getPointerFromPointer(PVOID pv);
    5960    LONG     getSectionIndexFromRVA(ULONG ulRVA);
     61    BOOL     validateRealPointer(PVOID pv);
    6062
    6163    PSECTION            paSections; /* Used by getPointerFromRVA and created by getSections and
Note: See TracChangeset for help on using the changeset viewer.