Changeset 2827 for trunk/kLdr/kLdrMod.c


Ignore:
Timestamp:
Oct 22, 2006, 8:05:28 PM (19 years ago)
Author:
bird
Message:

image format headers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kLdr/kLdrMod.c

    r2826 r2827  
    3232#include "kLdrHlp.h"
    3333#include "kLdrInternal.h"
     34#include "kLdrModMZ.h"
     35#if 1 /* testing headers */
     36# include "kLdrModPE.h"
     37//# include "kLdrModLX.h"
     38# include "kLdrModELF32.h"
     39# include "kLdrModELF64.h"
     40#endif
    3441
    3542
     43/**
     44 * Open a executable image from a file provider instance.
     45 *
     46 * @returns 0 on success and *ppMod pointing to a module instance.
     47 *          On failure, a non-zero OS specific error code is returned.
     48 * @param   pRdr            The file provider instance to use.
     49 *                          On success, the ownership of the instance is taken by the
     50 *                          module and the caller must not ever touch it again.
     51 *                          (The instance is not closed on failure, the call has to do that.)
     52 * @param   ppMod           Where to store the module handle.
     53 */
     54int kLdrModOpenFromRdr(PKLDRRDR pRdr, PPKLDRMOD ppMod)
     55{
     56    union
     57    {
     58        uint32_t    u32;
     59        uint16_t    u16;
     60        uint16_t    au16[2];
     61        uint8_t     au8[4];
     62    }       u;
     63    off_t   offHdr = 0;
     64    int     rc;
     65
     66    /*
     67     * Try figure out what kind of image this is.
     68     * Always read the 'new header' if we encounter MZ.
     69     */
     70    rc = kLdrRdrRead(pRdr, &u, sizeof(u), 0);
     71    if (rc)
     72        return rc;
     73    if (    u.u16 == IMAGE_DOS_SIGNATURE
     74        &&  kLdrRdrSize(pRdr) > sizeof(IMAGE_DOS_HEADER))
     75    {
     76        rc = kLdrRdrRead(pRdr, &u, sizeof(u.u32), KLDR_OFFSETOF(IMAGE_DOS_HEADER, e_lfanew));
     77        if (rc)
     78            return rc;
     79        if ((off_t)u.u32 < kLdrRdrSize(pRdr))
     80        {
     81            offHdr = u.u32;
     82            rc = kLdrRdrRead(pRdr, &u, sizeof(u.u32), offHdr);
     83            if (rc)
     84                return rc;
     85        }
     86        else
     87            u.u16 = IMAGE_DOS_SIGNATURE;
     88    }
     89
     90    /*
     91     * Use the magic to select the appropriate image interpreter.
     92     */
     93    if (u.u16 == IMAGE_DOS_SIGNATURE)
     94        return KLDR_ERR_MZ_NOT_SUPPORTED;
     95    else if (u.u16 == IMAGE_NE_SIGNATURE)
     96        return KLDR_ERR_NE_NOT_SUPPORTED;
     97    else if (u.u16 == IMAGE_LX_SIGNATURE)
     98        return KLDR_ERR_LX_NOT_SUPPORTED;
     99    else if (u.u16 == IMAGE_LE_SIGNATURE)
     100        return KLDR_ERR_LE_NOT_SUPPORTED;
     101    else if (u.u32 == IMAGE_NT_SIGNATURE)
     102        return KLDR_ERR_PE_NOT_SUPPORTED;
     103    else if (u.u32 == IMAGE_ELF_SIGNATURE)
     104        return KLDR_ERR_ELF_NOT_SUPPORTED;
     105    return KLDR_ERR_UNKNOWN_FORMAT;
     106}
     107
     108
     109/**
     110 * Open a executable image by file name.
     111 *
     112 * @returns 0 on success and *ppMod pointing to a module instance.
     113 *          On failure, a non-zero OS specific error code is returned.
     114 * @param   pszFilename     The filename to open.
     115 * @param   ppMod           Where to store the module handle.
     116 */
    36117int kLdrModOpen(const char *pszFilename, PPKLDRMOD ppMod)
    37118{
     
    39120     * Open the file using a bit provider.
    40121     */
    41 
    42 
     122    PKLDRRDR pRdr;
     123    int rc = kLdrRdrOpen(&pRdr, pszFilename);
     124    if (!rc)
     125    {
     126        rc = kLdrModOpenFromRdr(pRdr, ppMod);
     127        if (!rc)
     128            return 0;
     129       kLdrRdrClose(pRdr);
     130    }
     131    return rc;
    43132}
    44133
Note: See TracChangeset for help on using the changeset viewer.