Changeset 2944


Ignore:
Timestamp:
Jan 13, 2007, 4:55:40 PM (19 years ago)
Author:
bird
Message:

split up kLdrHlp.c and kLdr.c to make it more flexible (like using the module interpreters without the dynamic loader bit and similar).

Location:
trunk/kLdr
Files:
5 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/kLdr/Makefile.kmk

    r2899 r2944  
    4141 TEMPLATE_TST_ASFLAGS = -f win
    4242 TEMPLATE_TST_DEFS = __WIN__
    43  TEMPLATE_TST_SDKS = WIN32SDK W2K3DDKX86
    44  #TEMPLATE_SDKS.x86 = WIN32SDK W2K3DDKX86
    45  #TEMPLATE_SDKS.amd64 = WIN64SDK W2K3DDKAMD64
     43 TEMPLATE_SDKS.x86 = WIN32SDK W2K3DDKX86
     44 TEMPLATE_SDKS.amd64 = WIN64SDK W2K3DDKAMD64
    4645
    47 ## @todo this is a kBuild bug!
     46## @todo this is a kBuild bug? $$(PATH_TOOL_VCC70_LIB)
    4847 TEMPLATE_TST_LIBS = \
    4948        $$(PATH_TOOL_VCC70_LIB)/msvcrt.lib
     
    7776 kLdr_LDFLAGS = -Entry:DllMain@12 -Debug
    7877 kLdr_DEFS = __WIN__
    79  # crap, this doesn't work right either:
    80  #kLdr_SDKS = WIN32SDK W2K3DDKX86
    81  kLdr_SDKS = W2K3DDKX86 WIN32SDK
    82  #kLdr_SDKS.x86 = WIN32SDK W2K3DDKX86
    83  #kLdr_SDKS.amd64 = WIN64SDK W2K3DDKAMD64
     78 kLdr_SDKS.x86 = LIBSDL WIN32SDK W2K3DDKX86
     79 kLdr_SDKS.amd64 = WIN64SDK W2K3DDKAMD64
    8480 kLdr_LIBS = \
    85         $$(PATH_TOOL_VCC70_LIB)/LIBC.lib \
     81        $(PATH_TOOL_VCC70_LIB)/LIBC.lib \
    8682        $(PATH_SDK_W2K3DDKX86_LIB)/ntdll.lib
    8783else
     
    107103        kLdrHlp.c \
    108104        kLdrHlpHeap.c \
     105        kLdrHlpMem.c \
     106        kLdrHlpPath.c \
     107        kLdrHlpSem.c \
     108        kLdrHlpStr.c \
     109        kLdrMisc.c \
    109110        kLdrRdr.c \
    110111        kLdrRdrFile.c \
     
    169170        kLdrHlp.c \
    170171        kLdrHlpHeap.c \
     172        kLdrHlpMem.c \
     173        kLdrHlpPath.c \
     174        kLdrHlpSem.c \
     175        kLdrHlpStr.c \
    171176
    172177#
  • trunk/kLdr/kLdr.c

    r2883 r2944  
    142142}
    143143
    144 
    145 /**
    146  * Compares arch+cpu some code was generated for with a arch+cpu for executing it
    147  * to see if it'll work out fine or not.
    148  *
    149  * @returns 0 if the code is compatible with the cpu.
    150  * @returns KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE if the arch+cpu isn't compatible with the code.
    151  * @param   enmCodeArch The architecture the code was generated for.
    152  * @param   enmCodeCpu  The cpu the code was generated for.
    153  * @param   enmArch     The architecture to run it on.
    154  * @param   enmCpu      The cpu to run it on.
    155  */
    156 int kLdrCompareCpus(KLDRARCH enmCodeArch, KLDRCPU enmCodeCpu, KLDRARCH enmArch, KLDRCPU enmCpu)
    157 {
    158     /*
    159      * Compare arch and cpu.
    160      */
    161     if (enmCodeArch != enmArch)
    162         return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
    163 
    164     /* exact match is nice. */
    165     if (enmCodeCpu == enmCpu)
    166         return 0;
    167     switch (enmArch)
    168     {
    169         case KLDRARCH_X86_16:
    170             if (enmCpu < KLDRCPU_FIRST_X86_16 || enmCpu > KLDRCPU_LAST_X86_16)
    171                 return KLDR_ERR_INVALID_PARAMETER;
    172 
    173             /* intel? */
    174             if (enmCodeCpu <= KLDRCPU_CORE2_16)
    175             {
    176                 /* also intel? */
    177                 if (enmCpu <= KLDRCPU_CORE2_16)
    178                     return enmCodeCpu <= enmCpu ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
    179                 switch (enmCpu)
    180                 {
    181                     case KLDRCPU_K6_16:
    182                         return enmCodeCpu <= KLDRCPU_I586 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
    183                     case KLDRCPU_K7_16:
    184                     case KLDRCPU_K8_16:
    185                     default:
    186                         return enmCodeCpu <= KLDRCPU_I686 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
    187                 }
    188             }
    189             /* amd */
    190             return enmCpu >= KLDRCPU_K6_16 && enmCpu <= KLDRCPU_K8_16
    191                     ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
    192 
    193         case KLDRARCH_X86_32:
    194             if (enmCpu < KLDRCPU_FIRST_X86_32 || enmCpu > KLDRCPU_LAST_X86_32)
    195                 return KLDR_ERR_INVALID_PARAMETER;
    196 
    197             /* blend? */
    198             if (enmCodeCpu == KLDRCPU_X86_32_BLEND)
    199                 return 0;
    200 
    201             /* intel? */
    202             if (enmCodeCpu <= KLDRCPU_CORE2_32)
    203             {
    204                 /* also intel? */
    205                 if (enmCpu <= KLDRCPU_CORE2_32)
    206                     return enmCodeCpu <= enmCpu ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
    207                 switch (enmCpu)
    208                 {
    209                     case KLDRCPU_K6:
    210                         return enmCodeCpu <= KLDRCPU_I586 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
    211                     case KLDRCPU_K7:
    212                     case KLDRCPU_K8_32:
    213                     default:
    214                         return enmCodeCpu <= KLDRCPU_I686 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
    215                 }
    216             }
    217             /* amd */
    218             return enmCpu >= KLDRCPU_K6 && enmCpu <= KLDRCPU_K8_32
    219                     ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
    220 
    221         case KLDRARCH_AMD64:
    222             if (enmCpu < KLDRCPU_FIRST_AMD64 || enmCpu > KLDRCPU_LAST_AMD64)
    223                 return KLDR_ERR_INVALID_PARAMETER;
    224 
    225             /* blend? */
    226             if (enmCodeCpu == KLDRCPU_AMD64_BLEND)
    227                 return 0;
    228             /* this is simple for now. */
    229             return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
    230 
    231         default:
    232             break;
    233     }
    234     return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
    235 }
    236 
    237 
    238 /**
    239  * Gets the arch+cpu of the calling cpu.
    240  *
    241  * @param   penmArch    Where to store the cpu architecture.
    242  * @param   penmCpu     Where to store the cpu brand/model.
    243  */
    244 void kLdrGetArchCpu(PKLDRARCH penmArch, PKLDRCPU penmCpu)
    245 {
    246 #ifdef __AMD64__
    247     *penmArch = KLDRARCH_AMD64;
    248     *penmCpu = KLDRCPU_AMD64_BLEND; /** @todo check it using cpu. */
    249 
    250 #elif defined(__X86__)
    251     *penmArch = KLDRARCH_X86_32;
    252     *penmCpu = KLDRCPU_X86_32_BLEND; /** @todo check it using cpu. */
    253 
    254 #else
    255 # error "Port me"
    256 #endif
    257 }
  • trunk/kLdr/kLdr.h

    r2891 r2944  
    3232#endif
    3333
    34 /* kLdr depend on size_t, [u]intNN_t, [u]intptr_t and some related constants. */
    35 #include <sys/types.h>
    36 #include <stddef.h>
    37 #ifdef _MSC_VER
    38   typedef signed char         int8_t;
    39   typedef unsigned char       uint8_t;
    40   typedef signed short        int16_t;
    41   typedef unsigned short      uint16_t;
    42   typedef signed int          int32_t;
    43   typedef unsigned int        uint32_t;
    44   typedef signed __int64      int64_t;
    45   typedef unsigned __int64    uint64_t;
    46   typedef int64_t             intmax_t;
    47   typedef uint64_t            uintmax_t;
    48 # define UINT16_C(c)          (c ## U)
    49 # define UINT32_C(c)          (c ## U)
    50 # define UINT64_C(c)          (c ## ULL)
    51 #else
    52 # include <stdint.h>
    53 #endif
     34/*
     35 * kLdr depend on size_t, [u]intNN_t, [u]intptr_t and some related constants.
     36 * If KLDR_NO_KLDR_H_INCLUDES is defined, these has already been defined.
     37 */
     38#ifndef KLDR_NO_KLDR_H_INCLUDES
     39# include <sys/types.h>
     40# include <stddef.h>
     41# ifdef _MSC_VER
     42   typedef signed char          int8_t;
     43   typedef unsigned char        uint8_t;
     44   typedef signed short         int16_t;
     45   typedef unsigned short       uint16_t;
     46   typedef signed int           int32_t;
     47   typedef unsigned int         uint32_t;
     48   typedef signed __int64       int64_t;
     49   typedef unsigned __int64     uint64_t;
     50   typedef int64_t              intmax_t;
     51   typedef uint64_t             uintmax_t;
     52#  define UINT16_C(c)           (c ## U)
     53#  define UINT32_C(c)           (c ## U)
     54#  define UINT64_C(c)           (c ## ULL)
     55# else
     56#  include <stdint.h>
     57# endif
     58#endif /* !KLDR_NO_KLDR_H_INCLUDES */
    5459
    5560
  • trunk/kLdr/kLdrHlp-gcc.c

    • Property svn:keywords set to Id
    r2899 r2944  
    1 /* $Id: kLdrHlp-os2.c 2826 2006-10-22 15:58:55Z bird $ */
     1/* $Id$ */
    22/** @file
    33 *
  • trunk/kLdr/kLdrHlp.c

    r2898 r2944  
    22/** @file
    33 *
    4  * kLdr - The Dynamic Loader, Helper Functions.
     4 * kLdr - The Dynamic Loader, Misc Helper Functions.
    55 *
    66 * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@anduin.net>
     
    4141#include <kLdr.h>
    4242#include "kLdrHlp.h"
    43 
    44 
    45 /*******************************************************************************
    46 *   Global Variables                                                           *
    47 *******************************************************************************/
    48 #ifdef __OS2__
    49 /** The loader sempahore. */
    50 static HMTX             g_hmtx;
    51 /** The base of the stub object.
    52  * The OS/2 exe stub consists of a single data object. When allocating memory
    53  * for an executable, we'll have to reuse this.  */
    54 static void            *g_pvStub = NULL;
    55 /** The size of the stub object - 0 if no stub. */
    56 static size_t           g_cbStub = 0;
    57 
    58 #elif defined(__WIN__)
    59 /** The loader sempahore. */
    60 static CRITICAL_SECTION g_CritSect;
    61 /** The system info. */
    62 static SYSTEM_INFO      g_SystemInfo;
    63 #else
    64 # error "port me"
    65 #endif
    66 
    67 
    68 /**
    69  * Initializes the loader semaphore.
    70  *
    71  * @returns 0 on success, non-zero OS status code on failure.
    72  */
    73 int     kldrHlpSemInit(void)
    74 {
    75 #ifdef __OS2__
    76     APIRET rc;
    77     g_hmtx = NULLHANDLE;
    78     rc = DosCreateMutexSem(NULL, &g_hmtx, 0, FALSE);
    79     if (rc)
    80         return rc;
    81 
    82 #elif defined(__WIN__)
    83     InitializeCriticalSection(&g_CritSect);
    84 
    85 #else
    86 # error "port me"
    87 #endif
    88     return 0;
    89 }
    90 
    91 
    92 /**
    93  * Terminates the loader semaphore.
    94  */
    95 void    kldrHlpSemTerm(void)
    96 {
    97 #ifdef __OS2__
    98     HMTX hmtx = g_hmtx;
    99     g_hmtx = NULLHANDLE;
    100     DosCloseMutexSem(hmtx);
    101 
    102 #elif defined(__WIN__)
    103     DeleteCriticalSection(&g_CritSect);
    104 
    105 #else
    106 # error "port me"
    107 #endif
    108 }
    109 
    110 
    111 /**
    112  * Requests the loader sempahore ownership.
    113  * This can be done recursivly.
    114  *
    115  * @returns 0 on success, non-zero OS status code on failure.
    116  */
    117 int     kldrHlpSemRequest(void)
    118 {
    119 #ifdef __OS2__
    120     APIRET rc = DosRequestMutexSem(g_hmtx, 5000);
    121     if (rc == ERROR_TIMEOUT || rc == ERROR_SEM_TIMEOUT || rc == ERROR_INTERRUPT)
    122     {
    123         unsigned i = 0;
    124         do
    125         {
    126             /** @todo check for deadlocks etc. */
    127             rc = DosRequestMutexSem(g_hmtx, 1000);
    128         } while (   (   rc == ERROR_TIMEOUT
    129                      || rc == ERROR_SEM_TIMEOUT
    130                      || rc == ERROR_INTERRUPT)
    131                  && i++ < 120);
    132     }
    133     return rc;
    134 
    135 #elif defined(__WIN__)
    136     EnterCriticalSection(&g_CritSect);
    137     return 0;
    138 
    139 #else
    140 # error "port me"
    141 #endif
    142 }
    143 
    144 
    145 /**
    146  * Releases the loader semaphore ownership.
    147  * The caller is responsible for making sure it's the semaphore owner!
    148  */
    149 void    kldrHlpSemRelease(void)
    150 {
    151 #ifdef __OS2__
    152     APIRET rc = DosReleaseMutexSem(g_hmtx);
    153     kldrHlpAssert(!rc); (void)rc;
    154 
    155 #elif defined(__WIN__)
    156     LeaveCriticalSection(&g_CritSect);
    157 
    158 #else
    159 # error "port me"
    160 #endif
    161 
    162 }
    163 
    164 #ifdef __OS2__
    165 static ULONG kldrHlpPageProtToNative(KLDRPROT enmProt)
    166 {
    167     switch (enmProt)
    168     {
    169         case KLDRPROT_NOACCESS:             return PAG_EXECUTE | PAG_READ | PAG_WRITE;
    170         case KLDRPROT_READONLY:             return PAG_COMMIT | PAG_READ;
    171         case KLDRPROT_READWRITE:            return PAG_COMMIT | PAG_READ | PAG_WRITE;
    172         case KLDRPROT_EXECUTE:              return PAG_COMMIT | PAG_EXECUTE;
    173         case KLDRPROT_EXECUTE_READ:         return PAG_COMMIT | PAG_EXECUTE | PAG_READ;
    174         case KLDRPROT_EXECUTE_READWRITE:    return PAG_COMMIT | PAG_EXECUTE | PAG_READ | PAG_WRITE;
    175         default:
    176             kldrHlpAssert(0);
    177             return ~0U;
    178     }
    179 }
    180 #elif defined(__WIN__)
    181 static DWORD kldrHlpPageProtToNative(KLDRPROT enmProt)
    182 {
    183     switch (enmProt)
    184     {
    185         case KLDRPROT_NOACCESS:             return PAGE_NOACCESS;
    186         case KLDRPROT_READONLY:             return PAGE_READONLY;
    187         case KLDRPROT_READWRITE:            return PAGE_READWRITE;
    188         case KLDRPROT_EXECUTE:              return PAGE_EXECUTE;
    189         case KLDRPROT_EXECUTE_READ:         return PAGE_EXECUTE_READ;
    190         case KLDRPROT_EXECUTE_READWRITE:    return PAGE_EXECUTE_READWRITE;
    191         default:
    192             kldrHlpAssert(0);
    193             return ~0U;
    194     }
    195 }
    196 #endif
    197 
    198 
    199 /**
    200  * Allocate a chunk of memory with page granularity.
    201  *
    202  * @returns 0 on success, non-zero OS status code on failure.
    203  * @param   ppv         Where to store the address of the allocated memory.
    204  *                      If fFixed is set, *ppv will on entry contain the desired address (page aligned).
    205  * @param   cb          Number of bytes. Page aligned.
    206  * @param   enmProt     The new protection. Copy-on-write is invalid.
    207  */
    208 int     kldrHlpPageAlloc(void **ppv, size_t cb, KLDRPROT enmProt, unsigned fFixed)
    209 {
    210 #ifdef __OS2__
    211     APIRET  rc;
    212     ULONG   fFlags = kldrHlpPageProtToNative(enmProt);;
    213 
    214     if (!fFixed)
    215     {
    216         /* simple */
    217         rc = DosAllocMem(ppv, cb, fFlags | OBJ_ANY);
    218         if (rc == ERROR_INVALID_PARAMETER)
    219             rc = DosAllocMem(ppv, cb, fFlags);
    220     }
    221     else
    222     {
    223         /* not so simple. */
    224         /** @todo I've got code for this in libc somewhere. */
    225         rc = -1;
    226     }
    227     if (!rc)
    228         return 0;
    229     kldrHlpAssert(0);
    230     return rc;
    231 
    232 #elif defined(__WIN__)
    233     /* (We don't have to care about the stub here, because the stub will be unmapped before we get here.) */
    234     int     rc;
    235     DWORD   fProt = kldrHlpPageProtToNative(enmProt);
    236 
    237     if (!g_SystemInfo.dwPageSize)
    238         GetSystemInfo(&g_SystemInfo);
    239 
    240     *ppv = VirtualAlloc(fFixed ? *ppv : NULL, cb, MEM_COMMIT, fProt);
    241     if (*ppv != NULL)
    242         return 0;
    243     rc = GetLastError();
    244     kldrHlpAssert(0);
    245     return rc;
    246 
    247 #else
    248 # error "port me"
    249 #endif
    250 }
    251 
    252 
    253 /**
    254  * Change the protection of one or more pages in an allocation.
    255  *
    256  * (This will of course only work correctly on memory allocated by kldrHlpPageAlloc().)
    257  *
    258  * @returns 0 on success, non-zero OS status code on failure.
    259  * @param   pv          First page. Page aligned.
    260  * @param   cb          Number of bytes. Page aligned.
    261  * @param   enmProt     The new protection. Copy-on-write is invalid.
    262  */
    263 int     kldrHlpPageProtect(void *pv, size_t cb, KLDRPROT enmProt)
    264 {
    265 #ifdef __OS2__
    266     APIRET      rc;
    267     ULONG       fFlags = kldrHlpPageProtToNative(enmProt);;
    268 
    269     /*
    270      * The non-stub pages.
    271      */
    272     rc = DosSetMem(pv, cb, fFlags);
    273     if (rc && fFlags != PAG_DECOMMIT)
    274         rc = DosSetMem(pv, cb, fFlags | PAG_COMMIT);
    275     if (rc)
    276     {
    277         /* Try page by page. */
    278         while (cb > 0)
    279         {
    280             rc = DosSetMem(pv, 0x1000, fFlags);
    281             if (rc && fFlags != PAG_DECOMMIT)
    282                 rc = DosSetMem(pv, 0x1000, fFlags | PAG_COMMIT);
    283             if (rc)
    284                 return rc;
    285             pv = (void *)((uintptr_t)pv + 0x1000);
    286             cb -= 0x1000;
    287         }
    288     }
    289     kldrHlpAssert(!rc);
    290     return rc;
    291 
    292 #elif defined(__WIN__)
    293     DWORD fOldProt = 0;
    294     DWORD fProt = kldrHlpPageProtToNative(enmProt);
    295     int rc = 0;
    296 
    297     if (!VirtualProtect(pv, cb, fProt, &fOldProt))
    298     {
    299         rc = GetLastError();
    300         kldrHlpAssert(0);
    301     }
    302     return rc;
    303 #else
    304 # error "port me"
    305 #endif
    306 }
    307 
    308 
    309 /**
    310  * Free memory allocated by kldrHlpPageAlloc().
    311  *
    312  * @returns 0 on success, non-zero OS status code on failure.
    313  * @param   pv          The address returned by kldrHlpPageAlloc().
    314  * @param   cb          The byte count requested from kldrHlpPageAlloc().
    315  */
    316 int     kldrHlpPageFree(void *pv, size_t cb)
    317 {
    318 #ifdef __OS2__
    319     APIRET rc;
    320 
    321     /*
    322      * Deal with any portion overlapping with the stub.
    323      */
    324     uintptr_t offStub = (uintptr_t)pv - (uintptr_t)g_pvStub;
    325     if (offStub < g_cbStub)
    326     {
    327         /* decommit the pages in the stub. */
    328         size_t cbStub = KLDR_MIN(g_cbStub - offStub, cb);
    329         rc = DosSetMem(pv, cbStub, PAG_DECOMMIT);
    330         if (rc)
    331         {
    332             /* Page by page, ignoring errors after the first success. */
    333             while (cbStub > 0)
    334             {
    335                 if (!DosSetMem(pv, 0x1000, PAG_DECOMMIT))
    336                     rc = 0;
    337                 pv = (void *)((uintptr_t)pv + 0x1000);
    338                 cbStub -= 0x1000;
    339                 cb -= 0x1000;
    340             }
    341             if (rc)
    342             {
    343                 kldrHlpAssert(!rc);
    344                 return rc;
    345             }
    346         }
    347         else
    348         {
    349             cb -= cbStub;
    350             if (!cb)
    351                 return 0;
    352             pv = (void *)((uintptr_t)pv + cbStub);
    353         }
    354     }
    355 
    356     /*
    357      * Free the object.
    358      */
    359     rc = DosFreeMem(pv);
    360     kldrHlpAssert(!rc);
    361     return rc;
    362 
    363 #elif defined(__WIN__)
    364     /*
    365      * Free the object.
    366      */
    367     int rc = 0;
    368     if (!VirtualFree(pv, 0 /*cb*/, MEM_RELEASE))
    369     {
    370         rc = GetLastError();
    371         kldrHlpAssert(0);
    372     }
    373     return rc;
    374 
    375 #else
    376 # error "port me"
    377 #endif
    378 }
    37943
    38044
     
    503167
    504168/**
    505  * Get the pointer to the filename part of the name.
    506  *
    507  * @returns Pointer to where the filename starts within the string pointed to by pszFilename.
    508  * @returns Pointer to the terminator char if no filename.
    509  * @param   pszFilename     The filename to parse.
    510  */
    511 char   *kldrHlpGetFilename(const char *pszFilename)
    512 {
    513     const char *pszLast = NULL;
    514     for (;;)
    515     {
    516         char ch = *pszFilename;
    517 #if defined(__OS2__) || defined(__WIN__)
    518         if (ch == '/' || ch == '\\' || ch == ':')
    519         {
    520             while ((ch = *++pszFilename) == '/' || ch == '\\' || ch == ':')
    521                 /* nothing */;
    522             pszLast = pszFilename;
    523         }
    524 #else
    525         if (ch == '/')
    526         {
    527             while ((ch = *++pszFilename) == '/')
    528                 /* betsuni */;
    529             pszLast = pszFilename;
    530         }
    531 #endif
    532         if (!ch)
    533             return (char *)(pszLast ? pszLast : pszFilename);
    534         pszFilename++;
    535     }
    536 }
    537 
    538 
    539 /**
    540  * Gets the filename suffix.
    541  *
    542  * @returns Pointer to where the suffix starts within the string pointed to by pszFilename.
    543  * @returns Pointer to the terminator char if no suffix.
    544  * @param   pszFilename     The filename to parse.
    545  */
    546 char   *kldrHlpGetSuff(const char *pszFilename)
    547 {
    548     const char *pszDot = NULL;
    549     pszFilename = kldrHlpGetFilename(pszFilename);
    550     for (;;)
    551     {
    552         char ch = *pszFilename;
    553         if (ch == '.')
    554         {
    555             while ((ch = *++pszFilename) == '.')
    556                 /* nothing */;
    557             if (ch)
    558                 pszDot = pszFilename - 1;
    559         }
    560         if (!ch)
    561             return (char *)(pszDot ? pszDot : pszFilename);
    562         pszFilename++;
    563     }
    564 }
    565 
    566 
    567 /**
    568  * Gets the filename extention.
    569  *
    570  * @returns Pointer to where the extension starts within the string pointed to by pszFilename.
    571  * @returns Pointer to the terminator char if no extension.
    572  * @param   pszFilename     The filename to parse.
    573  */
    574 char   *kldrHlpGetExt(const char *pszFilename)
    575 {
    576     char *psz = kldrHlpGetSuff(pszFilename);
    577     return *psz ? psz + 1 : psz;
    578 }
    579 
    580 
    581 /**
    582  * Checks if this is only a filename or if it contains any kind
    583  * of drive, directory, or server specs.
    584  *
    585  * @returns 1 if this is a filename only.
    586  * @returns 0 of it's isn't only a filename.
    587  * @param   pszFilename     The filename to parse.
    588  */
    589 int kldrHlpIsFilenameOnly(const char *pszFilename)
    590 {
    591     for (;;)
    592     {
    593         const char ch = *pszFilename++;
    594 #if defined(__OS2__) || defined(__WIN__)
    595         if (ch == '/' || ch == '\\' || ch == ':')
    596 #else
    597         if (ch == '/')
    598 #endif
    599             return 0;
    600         if (!ch)
    601             return 1;
    602     }
    603 }
    604 
    605 
    606 /**
    607169 * Terminate the process.
    608170 *
     
    640202    usleep(cMillies * 1000);
    641203#endif
    642 }
    643 
    644 
    645 /**
    646  * Converts an signed integer to an ascii string.
    647  *
    648  * @returns psz.
    649  * @param   psz         Pointer to the output buffer.
    650  * @param   cch         The size of the output buffer.
    651  * @param   lVal        The value.
    652  * @param   iBase       The base to format it. (2,8,10 or 16)
    653  */
    654 char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase)
    655 {
    656     static const char s_szDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
    657     char *pszRet = psz;
    658 
    659     if (cch >= (lVal < 0 ? 3U : 2U) && psz)
    660     {
    661         /* prefix */
    662         if (lVal < 0)
    663         {
    664             *psz++ = '-';
    665             cch--;
    666             lVal = -lVal;
    667         }
    668 
    669         /* the digits */
    670         do
    671         {
    672             *psz++ = s_szDigits[lVal % iBase];
    673             cch--;
    674             lVal /= iBase;
    675         } while (lVal && cch > 1);
    676 
    677         /* overflow indicator */
    678         if (lVal)
    679             psz[-1] = '+';
    680     }
    681     else if (!pszRet)
    682         return pszRet;
    683     else if (cch < 1 || !pszRet)
    684         return pszRet;
    685     else
    686         *psz++ = '+';
    687     *psz = '\0';
    688 
    689     return pszRet;
    690204}
    691205
     
    765279}
    766280
    767 
    768 #ifdef kLdrHlpStrChr_needed
    769 char   *kLdrHlpStrChr(const char *psz, int ch)
    770 {
    771     while (*psz)
    772     {
    773         if (*psz == ch)
    774             return (char *)psz;
    775         psz++;
    776     }
    777     return NULL;
    778 }
    779 #endif
    780 
    781 
    782 #ifdef kLdrHlpMemChr_needed
    783 void   *kLdrHlpMemChr(const void *pv, int ch, size_t cb)
    784 {
    785     const uint8_t *pb = (const uint8_t *)pv;
    786     const uint8_t  b = (uint8_t)ch;
    787     while (cb-- > 0)
    788     {
    789         if (*pb == b)
    790             return (void *)pb;
    791         pb++;
    792     }
    793     return NULL;
    794 }
    795 #endif
    796 
    797 
    798 #ifdef kLdrHlpMemMove_needed
    799 void   *kLdrHlpMemMove(void *pv1, const void *pv2, size_t cb)
    800 {
    801     uint8_t        *pbDst = (uint8_t *)pv1;
    802     const uint8_t  *pbSrc = (const uint8_t *)pv2;
    803     while (cb-- > 0)
    804     {
    805         const uint8_t b = *pbSrc++;
    806         *pbDst++ = b;
    807     }
    808     return pv1;
    809 }
    810 #endif
    811 
    812 
    813 int     kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb)
    814 {
    815     const uint8_t *pb1 = (const uint8_t *)pv1;
    816     const uint8_t *pb2 = (const uint8_t *)pv2;
    817     while (cb-- > 0)
    818     {
    819         if (*pb1 != *pb2)
    820         {
    821             const uint8_t u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1;
    822             const uint8_t u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2;
    823             if (u1 != u2)
    824                 return (int)*pb1 - (int)*pb2;
    825         }
    826         pb1++;
    827         pb2++;
    828     }
    829     return 0;
    830 }
    831 
    832 
    833 int     kLdrHlpStrIComp(const char *pv1, const char *pv2)
    834 {
    835     const uint8_t *pb1 = (const uint8_t *)pv1;
    836     const uint8_t *pb2 = (const uint8_t *)pv2;
    837     for (;;)
    838     {
    839         if (*pb1 != *pb2)
    840         {
    841             const uint8_t u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1;
    842             const uint8_t u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2;
    843             if (u1 != u2)
    844                 return (int)*pb1 - (int)*pb2;
    845         }
    846         if (!*pb1)
    847             break;
    848         pb1++;
    849         pb2++;
    850     }
    851     return 0;
    852 }
    853 
    854 
    855 #ifdef kLdrHlpStrNComp_needed
    856 int     kLdrHlpStrNComp(const char *psz1, const char *psz2, size_t cch)
    857 {
    858     while (cch-- > 0)
    859     {
    860         if (*psz1 != *psz2)
    861             return (int)*psz1 - (int)*psz2;
    862         if (!*psz1)
    863             break;
    864         psz1++;
    865         psz2++;
    866     }
    867     return 0;
    868 }
    869 #endif
    870 
  • trunk/kLdr/kLdrHlp.h

    r2891 r2944  
    6363
    6464/*
    65  * Compiler specific helpers.
     65 * Compiler specific helpers / CRT.
    6666 * (I.e. operations that tend to have compiler intrinsic implementations).
    6767 */
    68 #ifdef __GNUC__
     68#ifndef KLDR_USE_CRT
     69
     70# ifdef __GNUC__
    6971/** memchr */
    70 # define kLdrHlpMemChr(a,b,c)  __builtin_memchr(a,b,c)
     72#  define kLdrHlpMemChr(a,b,c)  __builtin_memchr(a,b,c)
    7173/** memcmp */
    72 # define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c)
     74#  define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c)
    7375/** memcpy */
    74 # define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c)
     76#  define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c)
    7577/** memmove */
    7678/*# define kLdrHlpMemMove(a,b,c) __builtin_memmove(a,b,c)*/
    77 # define kLdrHlpMemMove_needed
     79#  define kLdrHlpMemMove_needed
    7880/** memset */
    79 # define kLdrHlpMemSet(a,b,c)  __builtin_memset(a,b,c)
     81#  define kLdrHlpMemSet(a,b,c)  __builtin_memset(a,b,c)
    8082/** strchr */
    81 # define kLdrHlpStrChr(a, b)   __builtin_strchr(a, b)
     83#  define kLdrHlpStrChr(a, b)   __builtin_strchr(a, b)
    8284/** strcmp */
    83 # define kLdrHlpStrComp(a, b)  __builtin_strcmp(a, b)
     85#  define kLdrHlpStrComp(a, b)  __builtin_strcmp(a, b)
    8486/** strncmp */
    85 # define kLdrHlpStrNComp(a,b,c) __builtin_strncmp(a, b, c)
     87#  define kLdrHlpStrNComp(a,b,c) __builtin_strncmp(a, b, c)
    8688/** strlen */
    87 # define kLdrHlpStrLen(a)      __builtin_strlen(a)
     89#  define kLdrHlpStrLen(a)      __builtin_strlen(a)
    8890/** alloca */
    89 # define kLdrHlpAllocA(a)      __builtin_alloca(a)
     91#  define kLdrHlpAllocA(a)      __builtin_alloca(a)
    9092/** int3 */
    91 # define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
     93#  define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
    9294/** NULL */
    93 # ifndef NULL
    94 define NULL 0
    95 # endif
    96 #endif
    97 
    98 #ifdef _MSC_VER
    99 # include <string.h>
    100 # include <malloc.h>
    101 # pragma intrinsic(memcmp, memcpy, memset, strcmp, strlen, __debugbreak)
     95#  ifndef NULL
     96 define NULL 0
     97#  endif
     98# endif
     99
     100# ifdef _MSC_VER
     101#  include <string.h>
     102#  include <malloc.h>
     103#  pragma intrinsic(memcmp, memcpy, memset, strcmp, strlen, __debugbreak)
    102104/** memchr */
    103 # define kLdrHlpMemChr_needed
     105#  define kLdrHlpMemChr_needed
    104106/** memcmp */
    105 # define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
     107#  define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
    106108/** memcpy */
    107 # define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
     109#  define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
    108110/** memmove */
    109 # define kLdrHlpMemMove_needed
     111#  define kLdrHlpMemMove_needed
    110112/** memset */
    111 # define kLdrHlpMemSet(a,b,c)  memset(a,b,c)
     113#  define kLdrHlpMemSet(a,b,c)  memset(a,b,c)
    112114/** strcmp */
    113 # define kLdrHlpStrComp(a, b)  strcmp(a, b)
     115#  define kLdrHlpStrComp(a, b)  strcmp(a, b)
    114116/** strncmp */
    115 # define kLdrHlpStrNComp_needed
     117#  define kLdrHlpStrNComp_needed
    116118/** strlen */
    117 # define kLdrHlpStrLen(a)      strlen(a)
     119#  define kLdrHlpStrLen(a)      strlen(a)
    118120/** strchr */
    119 # define kLdrHlpStrChr_needed
     121#  define kLdrHlpStrChr_needed
    120122/** alloca */
    121 # define kLdrHlpAllocA(a)      alloca(a)
     123#  define kLdrHlpAllocA(a)      alloca(a)
    122124/** int3 */
    123 # define kldrHlpBreakpoint() __debugbreak()
     125#  define kldrHlpBreakpoint() __debugbreak()
    124126/** NULL */
    125 # ifndef NULL
    126 define NULL 0
    127 # endif
    128 #endif
    129 
    130 #ifdef kLdrHlpStrChr_needed
     127#  ifndef NULL
     128 define NULL 0
     129#  endif
     130# endif
     131
     132# ifdef kLdrHlpStrChr_needed
    131133char   *kLdrHlpStrChr(const char *psz, int ch);
    132 #endif
    133 #ifdef kLdrHlpStrChr_needed
     134# endif
     135# ifdef kLdrHlpStrChr_needed
    134136int     kLdrHlpStrNComp(const char *psz1, const char *psz2, size_t cch);
    135 #endif
    136 #ifdef kLdrHlpMemChr_needed
     137# endif
     138# ifdef kLdrHlpMemChr_needed
    137139void   *kLdrHlpMemChr(const void *pv, int ch, size_t cb);
    138 #endif
    139 #ifdef kLdrHlpMemMove_needed
     140# endif
     141# ifdef kLdrHlpMemMove_needed
    140142void   *kLdrHlpMemMove(void *pv1, const void *pv2, size_t cb);
    141 #endif
    142 int     kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb);
    143 int     kLdrHlpStrIComp(const char *pv1, const char *pv2);
    144 
     143# endif
     144
     145
     146#else /* KLDR_USE_CRT */
     147
     148#  include <string.h>
     149#  include <stdlib.h>
     150#  ifdef _MSC_VER
     151#   include <malloc.h>
     152#  endif
     153
     154#  define kLdrHlpMemChr(a,b,c)  memchr(a,b,c)
     155/** memcmp */
     156#  define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
     157/** memcpy */
     158#  define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
     159/** memmove */
     160#  define kLdrHlpMemMove(a,b,c) memmove(a,b,c)
     161/** memset */
     162#  define kLdrHlpMemSet(a,b,c)  memset(a,b,c)
     163/** strchr */
     164#  define kLdrHlpStrChr(a, b)   strchr(a, b)
     165/** strcmp */
     166#  define kLdrHlpStrComp(a, b)  strcmp(a, b)
     167/** strncmp */
     168#  define kLdrHlpStrNComp(a,b,c) strncmp(a, b, c)
     169/** strlen */
     170#  define kLdrHlpStrLen(a)      strlen(a)
     171/** alloca */
     172#  define kLdrHlpAllocA(a)      alloca(a)
     173/** int3 */
     174#  ifdef __GNUC__
     175#   define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
     176#  endif
     177#  ifdef _MSC_VER
     178#   define kldrHlpBreakpoint() __debugbreak()
     179#  endif
     180
     181#endif
    145182
    146183#if (!defined(kLdrHlpMemChr) && !defined(kLdrHlpStrChr_needed))\
     
    157194#endif
    158195
     196#ifdef __cplusplus
     197extern "C" {
     198#endif
     199
     200int     kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb);
     201int     kLdrHlpStrIComp(const char *pv1, const char *pv2);
     202
    159203int     kldrHlpSemInit(void);
    160204void    kldrHlpSemTerm(void);
     
    183227char   *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase);
    184228void    kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
     229
     230#ifdef __cplusplus
     231}
     232#endif
     233
    185234
    186235/** Assertion macro.
  • trunk/kLdr/kLdrInternal.h

    r2883 r2944  
    3333#endif
    3434
    35 #if !defined(__X86__) || !defined(__AMD64__)
     35#if !defined(__X86__) && !defined(__AMD64__)
    3636# if defined(__i386__) || defined(_M_IX86)
    3737#  define __X86__
Note: See TracChangeset for help on using the changeset viewer.