Ignore:
Timestamp:
Jan 15, 2004, 11:14:58 AM (22 years ago)
Author:
sandervl
Message:

KSO: Updates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/win32k/pe2lx/pe2lx.cpp

    r9977 r10394  
    1 /* $Id: pe2lx.cpp,v 1.36 2003-04-02 14:27:32 bird Exp $
     1/* $Id: pe2lx.cpp,v 1.37 2004-01-15 10:14:58 sandervl Exp $
    22 *
    33 * Pe2Lx class implementation. Ring 0 and Ring 3
     
    500500            if ((paSections[i].Characteristics & paSecChars2Flags[j].Characteristics) == paSecChars2Flags[j].Characteristics)
    501501                flFlags |= paSecChars2Flags[j].flFlags;
     502        /* make sure the import table is writable */
     503        if (    !(flFlags & OBJWRITE)
     504            &&  pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size
     505            &&  paSections[i].VirtualAddress <= pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress
     506            &&  paSections[i].VirtualAddress + paSections[i].Misc.VirtualSize > pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress)
     507        {
     508            printInf(("Forcing section read/write since it contains import directory\n"));
     509            flFlags |= OBJWRITE | OBJREAD;
     510        }
    502511        if ((flFlags & (OBJEXEC | OBJWRITE)) == (OBJEXEC | OBJWRITE))
    503512            flFlags &= (ULONG)~OBJEXEC;
     
    27182727 *            Validate pNtHdrs.
    27192728 *            IF forwarders present and exports not made THEN makeExports!
    2720  *            Check and read directories for imports and relocations.
    27212729 *            Create necessary Buffered RVA Readers.
    2722  *            Make sure kernel32 is the first imported module.
    2723  *            Initiate the import variables (if any imports):
    2724  *                Loop thru the import descriptiors looking for the lowest FirstThunk RVA. (ulRVAOrgFirstThunk and ulRVAFirstThunk)
    2725  *                When found read the module name and add it. (ulModuleOrdinal)
     2730 *            Make sure kernel32/custombuild is the first imported module.
    27262731 *            Initiate base relocations by reading the first Base Relocation.
    27272732 *            Initiate iObj to 0UL, ulRVAPage to the RVA for the first object (which is allways 0UL!).
     
    27812786ULONG Pe2Lx::makeFixups()
    27822787{
    2783     BOOL                        fImports;           /* fImport is set when a valid import directory is present. */
    2784     ULONG                       ulRVAImportDesc;    /* RVA of the first import descriptor. */
    2785     IMAGE_IMPORT_DESCRIPTOR     ImportDesc;         /* Import descriptor struct used while reading. */
    2786     BufferedRVARead            *pImportReader;      /* Buffered reader import descriptors reads. */
    2787     BufferedRVARead            *pImpNameReader;     /* Buffered reader for names reads; ie. function and module names. */
    2788     BufferedRVARead            *pImpThunkReader;    /* Buffered reader for thunk reads; ie. reading from the OrgiginalFirstThunk array. */
    2789     ULONG                       ulModuleOrdinal;    /* Module ordinal. Valid as long as fImport is set. */
    2790     #ifndef RING0
    2791     ULONG                       ulCustomModOrdinal; /* Module ordinal of custom Odin dll. Valid as long as fImport is set. */
    2792     char                        szModuleName[128];
    2793     #endif
    2794     char *                      pszModuleName;      /* Pointer to the current modulename or NULL. Only used with Custombuild. */
    2795     ULONG                       ulRVAFirstThunk;    /* Current first thunk array RVA. Points at current entry. */
    2796     ULONG                       ulRVAOrgFirstThunk; /* Current original first thunk array RVA. Points at current entry. */
    27972788    #ifndef RING0
    27982789    BOOL                        fBaseRelocs;        /* fBaseReloc is set when a valid base reloc directory is present. */
     
    28052796    ULONG                       ulRVAPage;          /* RVA for the current page. */
    28062797    ULONG                       ul;                 /* temporary unsigned long variable. Many uses. */
    2807     PSZ                         psz;                /* temporary string pointer. Used for modulenames and functionnames. */
    28082798    ULONG                       iObj;               /* Object iterator. (Index into paObjects) */
    28092799    APIRET                      rc;                 /* return code. */
     
    28302820            return rc;
    28312821    }
    2832 
    2833     /* Check if empty import directory. */
    2834     fImports = pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size > 0UL
    2835         &&
    2836         (ulRVAImportDesc = pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress) > 0UL
    2837         &&
    2838         ulRVAImportDesc < pNtHdrs->OptionalHeader.SizeOfImage;
    28392822
    28402823    /* Check if empty base relocation directory. */
     
    28502833    printInf(("\n"));
    28512834    #ifndef RING0
    2852     printInf(("Make fixups, fBaseReloc=%s, fImports=%s\n",
    2853               fBaseRelocs ? "true" : "false",
    2854               fImports ? "true" : "false"));
    2855     #else
    2856     printInf(("Make fixups, fImports=%s\n", fImports ? "true" : "false"));
     2835    printInf(("Make fixups, fBaseReloc=%s\n",
     2836              fBaseRelocs ? "true" : "false"));
    28572837    #endif
    28582838    printInf(("\n"));
     
    28702850        pRelocReader = pPageReader = NULL;
    28712851    #endif
    2872     if (fImports)
    2873     {
    2874         pImportReader = new BufferedRVARead(hFile, cObjects, paObjects);
    2875         pImpNameReader = new BufferedRVARead(hFile, cObjects, paObjects);
    2876         pImpThunkReader = new BufferedRVARead(hFile, cObjects, paObjects);
    2877         if (pImportReader == NULL || pImpNameReader == NULL || pImpThunkReader == NULL)
    2878             rc = ERROR_NOT_ENOUGH_MEMORY;
    2879     }
    2880     else
    2881         pImpThunkReader = pImpNameReader = pImportReader = NULL;
    28822852
    28832853    /* check for errors */
     
    28902860            delete pRelocReader;
    28912861        #endif
    2892         if (pImportReader != NULL)
    2893             delete pImportReader;
    2894         if (pImpNameReader  != NULL)
    2895             delete pImpNameReader;
    2896         if (pImpThunkReader != NULL)
    2897             delete pImpThunkReader;
    28982862        return rc;
    28992863    }
     
    29022866    #ifndef RING0
    29032867    if (hasCustomDll())
    2904     {
    29052868        rc = addModule(options.pszCustomDll, (PULONG)SSToDS(&ul));
    2906         ulCustomModOrdinal = ul;
    2907     }
    29082869    else
    29092870    #endif
    29102871        rc = addModule("KERNEL32.DLL", (PULONG)SSToDS(&ul));
    2911 
    2912     /* initiate the import variables */
    2913     if (fImports && rc == NO_ERROR)
    2914     {
    2915         rc = pImportReader->readAtRVA(ulRVAImportDesc, SSToDS(&ImportDesc), sizeof(ImportDesc));
    2916         if (rc == NO_ERROR)
    2917         {
    2918             ul = 0;
    2919             ulRVAFirstThunk = ~0UL;
    2920             while (rc == NO_ERROR && ImportDesc.Name != 0UL && ImportDesc.FirstThunk != 0UL)
    2921             {
    2922                 if ((ULONG)ImportDesc.FirstThunk < ulRVAFirstThunk || ulRVAFirstThunk == 0UL) /* 0 test: just in case... */
    2923                 {
    2924                     ulRVAFirstThunk = (ULONG)ImportDesc.FirstThunk;
    2925                     ulRVAOrgFirstThunk = (ULONG)ImportDesc.u.OriginalFirstThunk != 0UL ?
    2926                         (ULONG)ImportDesc.u.OriginalFirstThunk : (ULONG)ImportDesc.FirstThunk;
    2927                     ulModuleOrdinal = ImportDesc.Name;
    2928                 }
    2929 
    2930                 /* next */
    2931                 ul++;
    2932                 rc = pImportReader->readAtRVA(ulRVAImportDesc + ul * sizeof(ImportDesc), SSToDS(&ImportDesc), sizeof(ImportDesc));
    2933             }
    2934 
    2935             if (ulRVAFirstThunk != ~0UL)
    2936             {
    2937                 rc = pImpNameReader->dupString(ulModuleOrdinal, (PSZ*)SSToDS(&psz));
    2938                 if (rc == NO_ERROR)
    2939                 {
    2940                     pszModuleName = NULL;
    2941                     #ifdef RING0
    2942                     rc = addModule(psz, (PULONG)SSToDS(&ulModuleOrdinal));
    2943 
    2944                     #else
    2945                     /* Uppercase the module names, and skip the extension. */
    2946                     if (hasCustomDll())
    2947                     {
    2948                         strcpy((pszModuleName = (char*)SSToDS(szModuleName)), psz);
    2949                         ulModuleOrdinal = ulCustomModOrdinal;
    2950                         for (char *pszTmp = pszModuleName; *pszTmp !='\0' && *pszTmp != '.'; pszTmp++)
    2951                             if (*pszTmp >= 'a' && *pszTmp <= 'z')
    2952                                 *pszTmp += ('A' - 'a');
    2953                         *pszTmp = '\0';
    2954                     }
    2955                     if (!hasCustomDll() || isCustomDllExcluded(pszModuleName))
    2956                     {
    2957                         rc = addModule(psz, (PULONG)SSToDS(&ulModuleOrdinal));
    2958                         szModuleName[0] = '\0';
    2959                         pszModuleName = NULL;
    2960                     }
    2961                     #endif
    2962                     free(psz);
    2963                 }
    2964             }
    2965             else
    2966                 fImports = FALSE;
    2967         }
    2968     }
    29692872
    29702873
     
    30132916                                            ORD_REGISTERPE2LXDLL : ORD_REGISTERPE2LXEXE, pszTmp);
    30142917            }
    3015             if (rc != NO_ERROR)
    3016                 break;
    3017         }
    3018 
    3019 
    3020         /* check for imports at this page */
    3021         if (fImports && ulRVAFirstThunk < ulRVAPage + PAGESIZE)
    3022         {
    3023             while (rc == NO_ERROR && ulRVAFirstThunk < ulRVAPage + PAGESIZE)
    3024             {
    3025                 IMAGE_THUNK_DATA Thunk;
    3026                 rc = pImpThunkReader->readAtRVA(ulRVAOrgFirstThunk, SSToDS(&Thunk), sizeof(Thunk));
    3027                 if (rc != NO_ERROR)
    3028                     break;
    3029                 if (Thunk.u1.Ordinal != 0UL)
    3030                 {
    3031                     if (Thunk.u1.Ordinal & (ULONG)IMAGE_ORDINAL_FLAG)
    3032                         rc = add32OrdImportFixup((WORD)(ulRVAFirstThunk & (PAGESIZE-1)),
    3033                                                  ulModuleOrdinal, Thunk.u1.Ordinal & 0xffff, pszModuleName);
    3034                     else if (Thunk.u1.Ordinal > 0UL && Thunk.u1.Ordinal < pNtHdrs->OptionalHeader.SizeOfImage)
    3035                     {
    3036                         rc = pImpNameReader->dupString(Thunk.u1.Ordinal + offsetof(IMAGE_IMPORT_BY_NAME, Name),
    3037                                                        (PSZ*)SSToDS(&psz));
    3038                         if (rc != NO_ERROR)
    3039                             break;
    3040                         rc = add32NameImportFixup((WORD)(ulRVAFirstThunk & (PAGESIZE-1)),
    3041                                                   ulModuleOrdinal, psz, pszModuleName);
    3042                         free(psz);
    3043                     }
    3044                     else
    3045                     {
    3046                         printErr(("invalid value in thunk array, neither an ordinal value nor an valid RVA. 0x%08x\n", Thunk.u1.Ordinal));
    3047                         rc = ERROR_INVALID_ADDRESS;
    3048                         break;
    3049                     }
    3050 
    3051                     /* next */
    3052                     ulRVAFirstThunk += sizeof(IMAGE_THUNK_DATA);
    3053                     ulRVAOrgFirstThunk += sizeof(IMAGE_THUNK_DATA);
    3054                 }
    3055                 else
    3056                 {   /* next module */
    3057                     rc = pImportReader->readAtRVA(ulRVAImportDesc, SSToDS(&ImportDesc), sizeof(ImportDesc));
    3058                     if (rc == NO_ERROR)
    3059                     {
    3060                         ULONG ulRVAFirstThunkPrev = ulRVAFirstThunk;
    3061                         ul = 0;
    3062                         ulRVAFirstThunk = ~0UL;
    3063                         while (rc == NO_ERROR && ImportDesc.Name != 0UL && ImportDesc.FirstThunk != 0UL)
    3064                         {
    3065                             if ((ULONG)ImportDesc.FirstThunk < ulRVAFirstThunk
    3066                                 && (ULONG)ImportDesc.FirstThunk > ulRVAFirstThunkPrev)
    3067                             {
    3068                                 ulRVAFirstThunk = (ULONG)ImportDesc.FirstThunk;
    3069                                 ulRVAOrgFirstThunk = (ULONG)ImportDesc.u.OriginalFirstThunk != 0UL ?
    3070                                     (ULONG)ImportDesc.u.OriginalFirstThunk : (ULONG)ImportDesc.FirstThunk;
    3071                                 ulModuleOrdinal = ImportDesc.Name;
    3072                             }
    3073 
    3074                             /* next */
    3075                             ul++;
    3076                             rc = pImportReader->readAtRVA(ulRVAImportDesc + ul * sizeof(ImportDesc), SSToDS(&ImportDesc), sizeof(ImportDesc));
    3077                         }
    3078 
    3079                         if (ulRVAFirstThunk != ~0UL)
    3080                         {   /* modulename */
    3081                             rc = pImpNameReader->dupString(ulModuleOrdinal, (PSZ*)SSToDS(&psz));
    3082                             if (rc == NO_ERROR)
    3083                             {
    3084                                 pszModuleName = NULL;
    3085                                 #ifdef RING0
    3086                                 rc = addModule(psz, (PULONG)SSToDS(&ulModuleOrdinal));
    3087 
    3088                                 #else
    3089                                 if (hasCustomDll())
    3090                                 {
    3091                                     strcpy((pszModuleName = (char*)SSToDS(szModuleName)), psz);
    3092                                     ulModuleOrdinal = ulCustomModOrdinal;
    3093                                     for (char *pszTmp = pszModuleName; *pszTmp !='\0' && *pszTmp != '.'; pszTmp++)
    3094                                         if (*pszTmp >= 'a' && *pszTmp <= 'z')
    3095                                             *pszTmp += ('A' - 'a');
    3096                                     *pszTmp = '\0';
    3097                                 }
    3098                                 if (!hasCustomDll() || isCustomDllExcluded(pszModuleName))
    3099                                 {
    3100                                     rc = addModule(psz, (PULONG)SSToDS(&ulModuleOrdinal));
    3101                                     szModuleName[0] = '\0';
    3102                                     pszModuleName = NULL;
    3103                                 }
    3104                                 #endif
    3105                                 free(psz);
    3106                             }
    3107                         }
    3108                         else
    3109                             fImports = FALSE;
    3110                     }
    3111                 }
    3112             } /* while */
    31132918            if (rc != NO_ERROR)
    31142919                break;
     
    32123017        delete pRelocReader;
    32133018    #endif
    3214     if (pImportReader != NULL)
    3215         delete pImportReader;
    3216     if (pImpNameReader  != NULL)
    3217         delete pImpNameReader;
    3218     if (pImpThunkReader != NULL)
    3219         delete pImpThunkReader;
    32203019
    32213020    /* Release unused memory in fixup and import structures. */
Note: See TracChangeset for help on using the changeset viewer.