Changeset 2825


Ignore:
Timestamp:
Oct 22, 2006, 5:57:19 PM (19 years ago)
Author:
bird
Message:

heap and file provider.

Location:
trunk/kLdr
Files:
8 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/kLdr/Makefile.kmk

    r2821 r2825  
    2727
    2828#
    29 # The kLDr DLL.
    30 #               
     29# Template for testcases.
     30#
     31TEMPLATE_TST = Testcase template
     32ifneq ($(filter win nt win32 win64,$(BUILD_TARGET)),)
     33 TEMPLATE_TST_TOOL = VCC70
     34 TEMPLATE_TST_CFLAGS = -W3 -Zi -Zl -MD
     35 TEMPLATE_TST_CFLAGS.release = -O2
     36 TEMPLATE_TST_ASFLAGS = -f win
     37 TEMPLATE_TST_DEFS = __WIN__
     38 TEMPLATE_TST_SDKS = WIN32SDK
     39 #kLdr_SDKS.x86 = WIN32SDK
     40 #kLdr_SDKS.amd64 = WIN64SDK
     41
     42## @todo this is a kBuild bug!
     43 TEMPLATE_TST_LIBS = \
     44        $$(PATH_TOOL_VCC70_LIB)/msvcrt.lib
     45else
     46 ifneq ($(filter os2,$(BUILD_TARGET)),)
     47  TEMPLATE_TST_TOOL = GCC3OMF
     48  TEMPLATE_TST_ASFLAGS = -f obj
     49  TEMPLATE_TST_LIBS = os2 gcc end
     50 else
     51  TEMPLATE_TST_TOOL = GCC3
     52  TEMPLATE_TST_ASFLAGS = -f elf
     53  TEMPLATE_TST_LIBS = gcc
     54 endif
     55 TEMPLATE_TST_CFLAGS = -Wall -pedantic -g
     56 TEMPLATE_TST_CFLAGS.release = -O2
     57 TEMPLATE_TST_LDFLAGS =
     58endif
     59TEMPLATE_TST_INCS = .
     60
     61
     62#
     63# The kLdr DLL.
     64#
    3165DLLS = kLdr
    32 kLdr_TOOL = GCC3OMF
    3366kLdr_ASTOOL = NASM
    34 kLdr_ASFLAGS = -f obj
    35 kLdr_CFLAGS = -Wall -pedantic
    36 kLdr_LDFLAGS = -nostdlib
    37 kLdr_LIBS = os2 gcc end
     67ifneq ($(filter win nt win32 win64,$(BUILD_TARGET)),)
     68 kLdr_TOOL = GCC3
     69 kLdr_TOOL = VCC70
     70 kLdr_CFLAGS = -W3 -Zl
     71 kLdr_ASFLAGS = -f win
     72 kLdr_DEFS = __WIN__
     73 kLdr_SDKS = WIN32SDK
     74 #kLdr_SDKS.x86 = WIN32SDK
     75 #kLdr_SDKS.amd64 = WIN64SDK
     76else
     77 ifneq ($(filter os2,$(BUILD_TARGET)),)
     78  kLdr_TOOL = GCC3OMF
     79  kLdr_ASFLAGS = -f obj
     80  kLdr_LIBS = os2 gcc end
     81 else
     82  kLdr_TOOL = GCC3
     83  kLdr_ASFLAGS = -f elf
     84  kLdr_LIBS = gcc
     85 endif
     86 kLdr_CFLAGS = -Wall -pedantic
     87 kLdr_LDFLAGS = -nostdlib
     88endif
     89kLdr_INCS = .
    3890kLdr_SOURCES = \
    3991        kLdr.c \
     92        kLdrHlp.c \
     93        kLdrHlpHeap.c \
     94        kLdrRdr.c \
     95        kLdrRdrFile.c \
    4096        kLdrLX.c
    4197kLdr_SOURCES.os2 = \
     
    45101#
    46102# The OS/2 stub program.
    47 #               
    48 PROGRAMS = kLdrExeStub-os2     
     103#
     104PROGRAMS.os2 = kLdrExeStub-os2
    49105kLdrExeStub-os2_TOOL = GCC3OMF
    50106kLdrExeStub-os2_ASTOOL = NASM
     
    53109kLdrExeStub-os2_LIBS = $(TARGET_kLdr)
    54110kLdrExeStub-os2_SOURCES = kLdrExeStub-os2.asm
    55        
     111
    56112##
    57113## The (stub) utility.
    58 ##     
     114##
    59115#PROGRAMS = kLdrUtil
    60        
     116
     117
     118#
     119# Heap testcase.
     120#
     121PROGRAMS += tstkLdrHeap
     122tstkLdrHeap_TEMPLATE = TST
     123tstkLdrHeap_SOURCES = \
     124        tstkLdrHeap.c \
     125        kLdrHlp.c \
     126        kLdrHlpHeap.c \
     127
    61128
    62129# generate rules.
  • trunk/kLdr/kLdr.c

    r2824 r2825  
    4949#include "kLdrHlp.h"
    5050
    51 
     51#if 0
    5252/*******************************************************************************
    5353*   Global Variables                                                           *
     
    195195}
    196196
     197#endif
  • trunk/kLdr/kLdr.h

    r2824 r2825  
    3434/* kLdr depend on size_t, [u]intNN_t, [u]intptr_t and some related constants. */
    3535#include <sys/types.h>
    36 #include <stdint.h>
    37 
    38 
    39 /** A KLDRMOD handle. */
    40 typedef struct KLDRMOD *HKLDRMOD;
     36#include <stddef.h>
     37#ifdef _MSC_VER
     38typedef signed char         int8_t;
     39typedef unsigned char       uint8_t;
     40typedef signed short        int16_t;
     41typedef unsigned short      uint16_t;
     42typedef signed int          int32_t;
     43typedef unsigned int        uint32_t;
     44typedef signed __int64      int64_t;
     45typedef unsigned __int64    uint64_t;
     46typedef uint64_t            uintmax_t;
     47#else
     48# include <stdint.h>
     49#endif
     50
     51
     52/** @defgroup grp_kLdrRdr   kLdrRdr - The file provider
     53 * @{ */
     54
     55/** Pointer to a file provider instance core. */
     56typedef struct KLDRRDR *PKLDRRDR;
     57/** Pointer to a file provider instance core pointer. */
     58typedef struct KLDRRDR **PPKLDRRDR;
     59
     60/**
     61 * File provider instance operations.
     62 */
     63typedef struct KLDRRDROPS
     64{
     65    /** The name of this file provider. */
     66    const char *pszName;
     67    /** Pointer to the next file provider. */
     68    const struct KLDRRDROPS *pNext;
     69
     70    /** Try create a new file provider instance.
     71     *
     72     * @returns 0 on success, OS specific error code on failure.
     73     * @param   ppRdr       Where to store the file provider instance.
     74     * @param   pszFilename The filename to open.
     75     */
     76    int     (* pfnCreate)(  PPKLDRRDR ppRdr, const char *pszFilename);
     77    /** Destroy the file provider instance.
     78     *
     79     * @returns 0 on success, OS specific error code on failure.
     80     *          On failure, the file provider instance will be in an indeterminate state - don't touch it!
     81     * @param   pRdr        The file provider instance.
     82     */
     83    int     (* pfnDestroy)( PKLDRRDR pRdr);
     84    /** Read bits from the file.
     85     *
     86     * @returns 0 on success, OS specific error code on failure.
     87     * @param   pRdr        The file provider instance.
     88     * @param   pvBuf       Where to put the bits.
     89     * @param   cb          The number of bytes to read.
     90     * @param   off         Where to start reading.
     91     */
     92    int     (* pfnRead)(    PKLDRRDR pRdr, void *pvBuf, size_t cb, off_t off);
     93    /** Map all the file bits into memory (read only).
     94     *
     95     * @returns 0 on success, OS specific error code on failure.
     96     * @param   pRdr        The file provider instance.
     97     * @param   ppvBits     Where to store the address of the mapping.
     98     *                      The size can be obtained using pfnSize.
     99     */
     100    int     (* pfnAllMap)(  PKLDRRDR pRdr, const void **ppvBits);
     101    /** Unmap a file bits mapping obtained by KLDRRDROPS::pfnAllMap.
     102     *
     103     * @returns 0 on success, OS specific error code on failure.
     104     * @param   pRdr        The file provider instance.
     105     * @param   pvBits      The mapping address.
     106     */
     107    int     (* pfnAllUnmap)(PKLDRRDR pRdr, const void *pvBits);
     108/** @todo generic mmap/MapViewOfFile */
     109    /** Get the file size.
     110     *
     111     * @returns The file size. Returns -1 on failure.
     112     * @param   pRdr        The file provider instance.
     113     */
     114    off_t   (* pfnSize)(    PKLDRRDR pRdr);
     115    /** Get the file pointer offset.
     116     *
     117     * @returns The file pointer offset. Returns -1 on failure.
     118     * @param   pRdr        The file provider instance.
     119     */
     120    off_t   (* pfnTell)(    PKLDRRDR pRdr);
     121    /** Get the file name.
     122     *
     123     * @returns The file size. Returns -1 on failure.
     124     * @param   pRdr        The file provider instance.
     125     */
     126    const char * (* pfnName)(PKLDRRDR pRdr);
     127} KLDRRDROPS;
     128/** Pointer to file provider operations. */
     129typedef KLDRRDROPS *PKLDRRDROPS;
     130/** Pointer to const file provider operations. */
     131typedef const KLDRRDROPS *PCKLDRRDROPS;
     132
     133
     134/**
     135 * File provider instance core.
     136 */
     137typedef struct KLDRRDR
     138{
     139    /** Pointer to the file provider operations. */
     140    PCKLDRRDROPS pOps;
     141} KLDRRDR;
     142
     143void    kLdrRdrAddProvider(PKLDRRDROPS pAdd);
     144
     145int     kLdrRdrOpen(    PPKLDRRDR ppRdr, const char *pszFilename);
     146int     kLdrRdrClose(   PKLDRRDR pRdr);
     147int     kLdrRdrRead(    PKLDRRDR pRdr, void *pvBuf, size_t cb, off_t off);
     148int     kLdrRdrAllMap(  PKLDRRDR pRdr, const void **ppvBits);
     149int     kLdrRdrAllUnmap(PKLDRRDR pRdr, const void *pvBits);
     150off_t   kLdrRdrSize(    PKLDRRDR pRdr);
     151off_t   kLdrRdrTell(    PKLDRRDR pRdr);
     152const char *kLdrRdrName(PKLDRRDR pRdr);
     153
     154/** @} */
     155
     156
     157
     158/** @defgroup grp_kLdrMod   kLdrMod - The executable image intepreter
     159 * @{ */
    41160
    42161/**
     
    81200
    82201/**
    83  * The state of a module.
    84  */
    85 typedef enum KLDRSTATE
    86 {
    87     /** The usual invalid 0 enum. */
    88     KLDRSTATE_INVALID = 0,
    89     /** kldrOpen succeeded.
    90      * Modules in this state will be freed at  */
    91     KLDRSTATE_OPEN,
    92     /** Dependencies has been loaded. */
    93     KLDRSTATE_DEPS,
    94     /** Fixups has been applied. */
    95     KLDRSTATE_FIXED,
    96     /** The module has been initialized. */
    97     KLDRSTATE_INITED,
    98     /** The module is loaded successfully. */
    99     KLDRSTATE_LOADED,
    100     /** The end of valid states (exclusive) */
    101     KLDRSTATE_END,
    102     /** The usual 32-bit blowup. */
    103     KLDRSTATE_32BIT_HACK = 0x7fffffff
    104 } KLDRSTATE;
    105 
    106 
    107 /**
    108202 * Loader module.
    109203 */
    110204typedef struct KLDRMOD
    111205{
    112     /** Pointer to the next module. */
    113     struct KLDRMOD     *pNext;
    114     /** Pointer to the previous module. */
    115     struct KLDRMOD     *pPrev;
    116     /** Our module handle. */
    117     HKLDRMOD            hmod;
     206    /** Magic number. */
     207    uint32_t            u32Magic;
     208    /** The type of module this is. */
     209    KLDRTYPE            enmType;
    118210    /** The module data. */
    119211    void               *pvData;
    120     /** The type of module this is. */
    121     KLDRTYPE            enmType;
    122     /** The module state. */
    123     KLDRSTATE           enmState;
    124     /** The number of references. */
    125     uint32_t            cRefs;
    126     /** The number of dynamic references. */
    127     uint32_t            cDynRefs;
    128     /** Magic number. */
    129     uint32_t            u32Magic;
    130     /** Set if this is the executable module. */
    131     uint32_t            fExecutable : 1;
    132     /** Global DLL (set) or specific DLL (clear). */
    133     uint32_t            fGlobal : 1;
    134     /** Load stage one. */
    135     uint32_t            fLoadStageOne : 1;
    136     /** Reserved for future use. */
    137     uint32_t            fReserved : 29;
    138212    /** The filename length (bytes). */
    139213    uint32_t            cchFilename;
     
    150224} KLDRMOD, *PKLDRMOD, **PPKLDRMOD;
    151225
    152 /** Pointer to the head module (the executable). */
    153 extern PKLDRMOD         kLdrModuleHead;
    154 /** Pointer to the tail module. */
    155 extern PKLDRMOD         kLdrModuleTail;
    156 /** The Library search path. */
    157 extern char             kLdrLibraryPath[4096];
     226
     227int kLdrModOpen(const char *pszFilename, PPKLDRMOD ppMod);
     228int kLdrModClose(PKLDRMOD pMod);
     229int kLdrModGetSymbol(PKLDRMOD pMod, const void *pvBits, uintmax_t BaseAddress, const char *pszSymbol, uintmax_t *pValue, unsigned *pfType);
     230int kLdrModGetSymbol(PKLDRMOD pMod, const void *pvBits, uintmax_t BaseAddress, const char *pszSymbol, uintmax_t *pValue, unsigned *pfType);
     231
     232size_t kLdrModSize(PKLDRMOD pMod);
     233
     234typedef int FNKLDRMODGETIMPORT(PKLDRMOD pMod, const char *pszModule, const char *pszSymbol, unsigned uSymbol, uintmax_t *pValue, void *pvUser);
     235typedef FNKLDRMODGETIMPORT *PFNKLDRMODGETIMPORT;
     236int kLdrModGetBits(PKLDRMOD pMod, void *pvBits, uintmax_t BaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
     237int kLdrModRelocate(PKLDRMOD pMod, void *pvBits, uintmax_t NewBaseAddress, uintmax_t OldBaseAddress, PFNKLDRMODGETIMPORT pfnGetImport, void *pvUser);
     238
     239typedef int FNKLDRMODENUMSYMS(PKLDRMOD pMod, const char *pszSymbol, unsigned uSymbol, uintmax_t Value, void *pvUser);
     240typedef FNKLDRMODENUMSYMS *PFNKLDRMODENUMSYMS;
     241int kLdrModEnumSymbols(PKLDRMOD pMod, unsigned fFlags, const void *pvBits, uintmax_t BaseAddress, PFNKLDRMODENUMSYMS pfnCallback, void *pvUser);
     242/** @name kLdrModEnumSymbols flags.
     243 * @{ */
     244/** Returns ALL kinds of symbols. The default is to only return public/exported symbols. */
     245#define KLDRMOD_ENUM_SYMBOL_FLAGS_ALL       0x00000001
     246/** @} */
     247
     248/** @} */
     249
     250
     251
     252
     253/** @defgroup grp_kLdrDy   kLdrDy - The dynamic loader
     254 * @{ */
     255
     256/** The h*/
     257typedef struct KLDRDY *PKLDRDY;
     258
     259/*
     260int kLdrLoadDll(const char *pszFilename, unsigned fFlags, unsigned long *pvmod);
     261*/
    158262
    159263/** @name Process Bootstrapping
     
    176280/** @} */
    177281
    178 /** @name The Internal APIs
    179  * @internal
    180  * @{ */
    181 int kldrOpenExe(const char *pszFilename, PPKLDRMOD ppMod)
    182 int kldrOpen(const char *pszFilename, unsigned fFlags, PPKLDRMOD ppMod);
    183 int kldrClose(PKLDRMOD pMod);
    184 
    185 void kldrFailure(const char *pszFilename, ...);
    186 /** @} */
    187 
    188 /*
    189 int kLdrLoadDll(const char *pszFilename, unsigned fFlags, unsigned long *pvmod);
    190 */
    191 
    192 #ifndef NULL
    193 # define NULL 0
    194 #endif
     282/** @} */
    195283
    196284#ifdef __cplusplus
  • trunk/kLdr/kLdrHlp.h

    r2824 r2825  
    2929#define __kLdrHlp_h__
    3030
     31/** @defgroup grp_kLdrHlp  kLdrHlp - Helper Functions
     32 * @internal
     33 * @{ */
    3134/** Get the minimum of two values. */
    3235#define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b))
    3336
    34 #ifdef __OS2__
     37/** Align a size_t value. */
     38#define KLDR_ALIGN_Z(val, align)    ( ((val) + ((align) - 1)) & ~(size_t)((align) - 1) )
     39/** Align a void * value. */
     40#define KLDR_ALIGN_P(pv, align)     ( (void *)( ((uintptr_t)(pv) + ((align) - 1)) & ~(uintptr_t)((align) - 1) ) )
     41
     42/*
     43 * Compiler specific helpers.
     44 * (I.e. operations that tend to have compiler intrinsic implementations).
     45 */
     46#ifdef __GNUC__
     47/** memcmp */
     48# define kLdrMemComp(a,b,c) __builtin_memcmp(a,b,c)
     49/** memcpy */
     50# define kLdrMemCopy(a,b,c) __builtin_memcpy(a,b,c)
     51/** memset */
     52# define kLdrMemSet(a,b,c)  __builtin_memset(a,b,c)
     53/** strlen */
     54# define kLdrStrLen(a)      __builtin_strlen(a)
     55/** alloca */
     56# define kLdrAllocA(a)      __builtin_alloca(a)
     57/** int3 */
     58# define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
     59/** NULL */
     60# ifndef NULL
     61#  define NULL 0
     62# endif
     63#endif
     64
     65#ifdef _MSC_VER
     66# include <string.h>
     67# include <malloc.h>
     68# pragma intrinsic(memcmp, memcpy, memset, strlen, __debugbreak)
     69/** memcmp */
     70# define kLdrMemComp(a,b,c) memcmp(a,b,c)
     71/** memcpy */
     72# define kLdrMemCopy(a,b,c) memcpy(a,b,c)
     73/** memset */
     74# define kLdrMemSet(a,b,c)  memset(a,b,c)
     75/** strlen */
     76# define kLdrStrLen(a)      strlen(a)
     77/** alloca */
     78# define kLdrAllocA(a)      alloca(a)
     79/** int3 */
     80# define kldrHlpBreakpoint() __debugbreak()
     81/** NULL */
     82# ifndef NULL
     83#  define NULL 0
     84# endif
     85#endif
     86
     87#if !defined(kLdrMemComp) \
     88 || !defined(kLdrMemCopy) \
     89 || !defined(kLdrMemSet) \
     90 || !defined(kLdrStrLen) \
     91 || !defined(kLdrAllocA) \
     92 || !defined(kldrHlpBreakpoint)
     93# error "Needs porting to your compiler."
     94#endif
     95
    3596
    3697int kldrSemInit(void);
     
    41102int kldrSemGlobalRelease(void);
    42103
    43 /** Loader file handle. */
    44 typedef unsigned long KLDRFILE;
    45 /** Pointer to a loader file handle. */
    46 typedef KLDRFILE *PKLDRFILE;
    47 
    48 int kldrFileOpen(const char *pszFilename, PKLDRFILE pFile);
    49 int kldrFileClose(KLDRFILE File);
    50 int kldrFileRead(KLDRFILE File, off_t off, void *pv, size_t cb);
    51 
    52 int kldrGetEnv(const char *pszVar, char *pszVar, size_t *pcchVar)
    53 
    54 void *kldrAlloc(size_t cb);
    55 void kldrFree(void *pv);
    56 
    57104int kldrPrivateAlloc(void *pv, size_t cb, unsigned fFlags, void **ppv);
    58105int kldrPrivateFree(void *pv, size_t cb);
    59 int kldrSharedAlloc(void *pv, size_t cb, unsigned fFlags, const char *pszFilename, KLDRFILE File, void **ppv);
     106int kldrSharedAlloc(void *pv, size_t cb, unsigned fFlags, const char *pszFilename, void *File, void **ppv);
    60107int kldrSharedFree(void *pv, size_t cb);
    61108
    62 void kldrExit(int rc);
     109int     kldrHlpHeapInit(void);
     110void    kldrHlpHeapTerm(void);
     111void    kldrHlpHeapDonate(void *pv, size_t cb);
     112void *  kldrHlpAlloc(size_t cb);
     113void    kldrHlpFree(void *pv);
    63114
    64 /* #elif __WIN__ */
     115int     kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t *pcchVal);
     116void    kldrHlpExit(int rc);
     117void    kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
    65118
    66 #else
     119/** Assertion macro.
     120 * Users should wrap it since this is ALWAYS compiled in. */
     121#define kldrHlpAssert(expr) \
     122    do { \
     123        if (!(expr)) \
     124        { \
     125            kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \
     126            kldrHlpBreakpoint(); \
     127        } \
     128    } while (0)
    67129
    68130
     131/** @} */
    69132
    70 #endif
    71 
    72 /*
    73  * Compiler specific helpers.
    74  * (I.e. operations that tend to have compiler intrinsic implementations).
    75  */
    76 #ifdef __GNUC__
    77 /** memcpy */
    78 # define kLdrMemCopy(a,b,c) __builtin_memcpy(a,b,c)
    79 /** memset */
    80 # define kLdrMemSet(a,b,c) __builtin_memset(a,b,c)
    81 #endif
    82 
    83 #ifndef kLdrMemCopy
    84 # error "Needs porting to your compiler."
    85 #endif
    86133
    87134#endif /* __kLdrHlp_h__ */
     135
Note: See TracChangeset for help on using the changeset viewer.