Ignore:
Timestamp:
Nov 8, 2006, 5:10:14 AM (19 years ago)
Author:
bird
Message:

Working on the mapping stuff (windows is gonna be a headache).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kLdr/kLdrModPE.c

    r2859 r2860  
    551551static int kldrModPEDoMap(PKLDRMODPE pModPE, unsigned fForReal)
    552552{
    553     return -1;
     553    PKLDRMOD    pMod = pModPE->pMod;
     554    unsigned    fFixed;
     555    void       *pvBase;
     556    size_t      cb;
     557    int         rc;
     558    uint32_t    i;
     559
     560    /*
     561     * Prepare it.
     562     */
     563    /* size */
     564    cb = (size_t)pMod->pOps->pfnSize(pModPE->pMod);
     565    if (cb != pMod->pOps->pfnSize(pModPE->pMod))
     566        return KLDR_ERR_ADDRESS_OVERFLOW;
     567
     568    /* fixed image? */
     569    fFixed = fForReal
     570          && (   pMod->enmType == KLDRTYPE_EXECUTABLE_FIXED
     571              || pMod->enmType == KLDRTYPE_SHARED_LIBRARY_FIXED);
     572    if (!fFixed)
     573        pvBase = NULL;
     574    else
     575    {
     576        pvBase = (void *)(uintptr_t)pMod->aSegments[0].LinkAddress;
     577        if ((uintptr_t)pvBase != pMod->aSegments[0].LinkAddress)
     578            return KLDR_ERR_ADDRESS_OVERFLOW;
     579    }
     580
     581    /* try do the prepare */
     582    rc = kLdrRdrPrepare(pMod->pRdr, &pvBase, cb, fFixed);
     583    if (rc)
     584        return rc;
     585
     586    /*
     587     * Map the segments (sub sections in PE terms).
     588     */
     589    for (i = 0; i < pMod->cSegments; i++)
     590    {
     591        void *pvSeg;
     592
     593        if (!pMod->aSegments[i].Alignment)
     594            continue;
     595
     596        pvSeg = (uint8_t *)pvBase + pMod->aSegments[i].RVA;
     597        rc = kLdrRdrMap(pMod->pRdr, pvSeg, pMod->aSegments[i].cbMapped, pMod->aSegments[i].enmProt,
     598                        pMod->aSegments[i].offFile, pMod->aSegments[i].cbFile);
     599        if (rc)
     600        {
     601            /* bailout */
     602            while (i-- > 0)
     603            {
     604                if (!pMod->aSegments[i].Alignment)
     605                    continue;
     606
     607                kLdrRdrUnmap(pMod->pRdr, (void *)pMod->aSegments[i].MapAddress, pMod->aSegments[i].cbMapped);
     608                pMod->aSegments[i].MapAddress = 0;
     609            }
     610            break;
     611        }
     612        pMod->aSegments[i].MapAddress = (uintptr_t)pvSeg;
     613    }
     614
     615    if (!rc)
     616    {
     617        if (fForReal)
     618            pModPE->pvMapping = pvBase;
     619        else
     620            pModPE->pvBits = pvBase;
     621    }
     622    else
     623        kLdrRdrUnprepare(pMod->pRdr, pvBase, cb);
     624    return rc;
    554625}
    555626
     
    567638static int kldrModPEDoUnmap(PKLDRMODPE pModPE, const void *pvMapping)
    568639{
    569     return -1;
     640    PKLDRMOD    pMod = pModPE->pMod;
     641    size_t      cb = (size_t)pMod->pOps->pfnSize(pModPE->pMod);
     642    int         rc2;
     643    int         rc = 0;
     644    uint32_t    i;
     645
     646    /*
     647     * Unmap the segments (sub sections in PE terms).
     648     */
     649    for (i = 0; i < pMod->cSegments; i++)
     650    {
     651        if (!pMod->aSegments[i].MapAddress)
     652            continue;
     653
     654        rc2 = kLdrRdrUnmap(pMod->pRdr, (void *)pMod->aSegments[i].MapAddress, pMod->aSegments[i].cbMapped);
     655        if (!rc2)
     656            pMod->aSegments[i].MapAddress = 0;
     657        else if (!rc)
     658            rc = rc2;
     659    }
     660
     661    /*
     662     * 'Unprepare' the mapping region.
     663     */
     664    if (!rc)
     665    {
     666        rc = kLdrRdrUnprepare(pMod->pRdr, (void *)pvMapping, cb);
     667        if (!rc)
     668        {
     669            if (pModPE->pvBits == pvMapping)
     670                pModPE->pvBits = NULL;
     671            if (pModPE->pvMapping == pvMapping)
     672                pModPE->pvMapping = NULL;
     673        }
     674    }
     675
     676    return rc;
    570677}
    571678
Note: See TracChangeset for help on using the changeset viewer.