Changeset 2827


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

image format headers.

Location:
trunk/kLdr
Files:
7 added
5 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/kLdr/Makefile.kmk

    r2826 r2827  
    9494        kLdrRdr.c \
    9595        kLdrRdrFile.c \
    96         kLdrLX.c
     96        kLdrMod.c \
     97        kLdrModLX.c
    9798kLdr_SOURCES.os2 = \
    9899        kLdr-os2.def \
  • trunk/kLdr/kLdr.h

    r2826 r2827  
    282282/** @} */
    283283
     284
     285/** @defgroup grp_kLdrErr   kLdr Status Codes
     286 * kLdr uses a mix of native status codes and it's own status codes.
     287 * A status code of 0 means success, all other status codes means failure.
     288 * @{
     289 */
     290#ifdef __OS2__
     291# define KLDR_ERR_BASE          0x7face000
     292#elif defined(__WIN__)
     293# define KLDR_ERR_BASE          0x7face000
     294#else
     295# error "port me"
     296#endif
     297/** The image format is unknown. */
     298#define KLDR_ERR_UNKNOWN_FORMAT         (KLDR_ERR_BASE + 0)
     299/** The MZ image format isn't supported by this kLdr build. */
     300#define KLDR_ERR_MZ_NOT_SUPPORTED       (KLDR_ERR_BASE + 1)
     301/** The NE image format isn't supported by this kLdr build. */
     302#define KLDR_ERR_NE_NOT_SUPPORTED       (KLDR_ERR_BASE + 2)
     303/** The LX image format isn't supported by this kLdr build. */
     304#define KLDR_ERR_LX_NOT_SUPPORTED       (KLDR_ERR_BASE + 3)
     305/** The LE image format isn't supported by this kLdr build. */
     306#define KLDR_ERR_LE_NOT_SUPPORTED       (KLDR_ERR_BASE + 4)
     307/** The PE image format isn't supported by this kLdr build. */
     308#define KLDR_ERR_PE_NOT_SUPPORTED       (KLDR_ERR_BASE + 5)
     309/** The ELF image format isn't supported by this kLdr build. */
     310#define KLDR_ERR_ELF_NOT_SUPPORTED      (KLDR_ERR_BASE + 6)
     311/** The mach-o image format isn't supported by this kLdr build. */
     312#define KLDR_ERR_MACHO_NOT_SUPPORTED    (KLDR_ERR_BASE + 7)
     313/** The mach-o image format isn't supported by this kLdr build. */
     314#define KLDR_ERR_AOUT_NOT_SUPPORTED     (KLDR_ERR_BASE + 8)
     315/** The mach-o image format isn't supported by this kLdr build. */
     316#define KLDR_ERR_BAD_FIXUP              (KLDR_ERR_BASE + 32)
     317
     318/** @} */
     319
     320
    284321#ifdef __cplusplus
    285322}
  • trunk/kLdr/kLdrHlp.h

    r2826 r2827  
    3232 * @internal
    3333 * @{ */
     34
    3435/** Get the minimum of two values. */
    3536#define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b))
    36 
     37/** Calculate the offset of a structure member. */
     38#define KLDR_OFFSETOF(strct, memb)  ( (size_t)( ((strct *)0)->memb ) )
    3739/** Align a size_t value. */
    3840#define KLDR_ALIGN_Z(val, align)    ( ((val) + ((align) - 1)) & ~(size_t)((align) - 1) )
    3941/** Align a void * value. */
    4042#define KLDR_ALIGN_P(pv, align)     ( (void *)( ((uintptr_t)(pv) + ((align) - 1)) & ~(uintptr_t)((align) - 1) ) )
     43/** @def KLDRHLP_LE2H_U16
     44 * Unsigned 16-bit little-endian to host translation. */
     45/** @def KLDRHLP_LE2H_U32
     46 * Unsigned 32-bit little-endian to host translation. */
     47#if 1
     48# define KLDRHLP_LE2H_U16(u16)  ((uint16_t)(u16))
     49# define KLDRHLP_LE2H_U32(u32)  ((uint32_t)(u32))
     50#else
     51# define KLDRHLP_LE2H_U16(u16)  ( (uint16_t) (((u16) >> 8) | ((u16) << 8)) )
     52# define KLDRHLP_LE2H_U32(u32)  (   ( ((u32) & UINT32_C(0xff000000)) >> 24 ) \
     53                                  | ( ((u32) & UINT32_C(0x00ff0000)) >>  8 ) \
     54                                  | ( ((u32) & UINT32_C(0x0000ff00)) <<  8 ) \
     55                                  | ( ((u32) & UINT32_C(0x000000ff)) << 24 ) \
     56                                )
     57#endif
    4158
    4259/*
  • trunk/kLdr/kLdrInternal.h

    r2826 r2827  
    3232extern "C" {
    3333#endif
     34
     35/* ignore definitions in winnt.h */
     36#undef IMAGE_DOS_SIGNATURE
     37#undef IMAGE_NT_SIGNATURE
     38
     39/** @name Signatures we know
     40 * @{ */
     41/** ELF signature ("\x7fELF"). */
     42#define IMAGE_ELF_SIGNATURE KLDRHLP_LE2H_U32(0x7f | ('E' << 8) | ((uint32_t)'L' << 16) | ((uint32_t)'F' << 24))
     43/** PE signature ("PE\0\0"). */
     44#define IMAGE_NT_SIGNATURE  KLDRHLP_LE2H_U32('P' | ('E' << 8))
     45/** LX signature ("LX") */
     46#define IMAGE_LX_SIGNATURE  KLDRHLP_LE2H_U16('L' | ('X' << 8))
     47/** LE signature ("LE") */
     48#define IMAGE_LE_SIGNATURE  KLDRHLP_LE2H_U16('L' | ('E' << 8))
     49/** NE signature ("NE") */
     50#define IMAGE_NE_SIGNATURE  KLDRHLP_LE2H_U16('N' | ('E' << 8))
     51/** MZ signature ("MZ"). */
     52#define IMAGE_DOS_SIGNATURE KLDRHLP_LE2H_U16('M' | ('Z' << 8))
     53/** @} */
     54
    3455
    3556/** Native file provider operations. */
  • 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
  • trunk/kLdr/kLdrModLX.c

    r2826 r2827  
    2626
    2727#include "kLdr.h"
     28
Note: See TracChangeset for help on using the changeset viewer.