source: trunk/kLdr/kLdrHlp.h@ 3232

Last change on this file since 3232 was 2958, checked in by bird, 19 years ago

Relocations (generic only - x86 is generic).

  • Property svn:keywords set to Id
File size: 11.9 KB
RevLine 
[2826]1/* $Id: kLdrHlp.h 2958 2007-02-09 05:12:22Z bird $ */
[2821]2/** @file
3 *
4 * kLdr - The Dynamic Loader, Helper Functions.
5 *
6 * Copyright (c) 2006 knut st. osmundsen <bird@anduin.net>
7 *
8 *
9 * This file is part of kLdr.
10 *
11 * kLdr is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kLdr is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kLdr; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27
28#ifndef __kLdrHlp_h__
29#define __kLdrHlp_h__
30
[2825]31/** @defgroup grp_kLdrHlp kLdrHlp - Helper Functions
32 * @internal
33 * @{ */
[2827]34
[2824]35/** Get the minimum of two values. */
[2861]36#define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b))
[2846]37/** Get the maximum of two values. */
[2861]38#define KLDR_MAX(a, b) ((a) >= (b) ? (a) : (b))
[2827]39/** Calculate the offset of a structure member. */
[2858]40#define KLDR_OFFSETOF(strct, memb) ( (size_t)( &((strct *)0)->memb ) )
[2825]41/** Align a size_t value. */
42#define KLDR_ALIGN_Z(val, align) ( ((val) + ((align) - 1)) & ~(size_t)((align) - 1) )
43/** Align a void * value. */
44#define KLDR_ALIGN_P(pv, align) ( (void *)( ((uintptr_t)(pv) + ((align) - 1)) & ~(uintptr_t)((align) - 1) ) )
[2955]45/** Align a size_t value. */
46#define KLDR_ALIGN_ADDR(val, align) ( ((val) + ((align) - 1)) & ~(KLDRADDR)((align) - 1) )
[2861]47/** Number of elements in an array. */
48#define KLDR_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
49
[2954]50
51/** @name Endian Conversion
52 * @{ */
53
54/** @def KLDRHLP_E2E_U16
55 * Convert the endian of an unsigned 16-bit value. */
56# define KLDRHLP_E2E_U16(u16) ( (uint16_t) (((u16) >> 8) | ((u16) << 8)) )
57/** @def KLDRHLP_E2E_U32
58 * Convert the endian of an unsigned 32-bit value. */
59# define KLDRHLP_E2E_U32(u32) ( ( ((u32) & UINT32_C(0xff000000)) >> 24 ) \
60 | ( ((u32) & UINT32_C(0x00ff0000)) >> 8 ) \
61 | ( ((u32) & UINT32_C(0x0000ff00)) << 8 ) \
62 | ( ((u32) & UINT32_C(0x000000ff)) << 24 ) \
63 )
64/** @def KLDRHLP_E2E_U64
65 * Convert the endian of an unsigned 64-bit value. */
66# define KLDRHLP_E2E_U64(u64) ( ( ((u64) & UINT64_C(0xff00000000000000)) >> 56 ) \
67 | ( ((u64) & UINT64_C(0x00ff000000000000)) >> 40 ) \
68 | ( ((u64) & UINT64_C(0x0000ff0000000000)) >> 24 ) \
69 | ( ((u64) & UINT64_C(0x000000ff00000000)) >> 8 ) \
70 | ( ((u64) & UINT64_C(0x00000000ff000000)) << 8 ) \
71 | ( ((u64) & UINT64_C(0x0000000000ff0000)) << 24 ) \
72 | ( ((u64) & UINT64_C(0x000000000000ff00)) << 40 ) \
73 | ( ((u64) & UINT64_C(0x00000000000000ff)) << 56 ) \
74 )
75
[2827]76/** @def KLDRHLP_LE2H_U16
[2954]77 * Unsigned 16-bit little-endian to host endian. */
[2827]78/** @def KLDRHLP_LE2H_U32
[2954]79 * Unsigned 32-bit little-endian to host endian. */
80/** @def KLDRHLP_LE2H_U64
81 * Unsigned 64-bit little-endian to host endian. */
82/** @def KLDRHLP_BE2H_U16
83 * Unsigned 16-bit big-endian to host endian. */
84/** @def KLDRHLP_BE2H_U32
85 * Unsigned 32-bit big-endian to host endian. */
86/** @def KLDRHLP_BE2H_U64
87 * Unsigned 64-bit big-endian to host endian. */
[2958]88#ifdef KLDR_LITTLE_ENDIAN
[2827]89# define KLDRHLP_LE2H_U16(u16) ((uint16_t)(u16))
90# define KLDRHLP_LE2H_U32(u32) ((uint32_t)(u32))
[2954]91# define KLDRHLP_LE2H_U64(u64) ((uint32_t)(u32))
92# define KLDRHLP_BE2H_U16(u16) KLDRHLP_E2E_U16(u16)
93# define KLDRHLP_BE2H_U32(u32) KLDRHLP_E2E_U32(u32)
94# define KLDRHLP_BE2H_U64(u64) KLDRHLP_E2E_U64(u64)
[2958]95#elif defined(KLDR_BIG_ENDIAN)
[2954]96# define KLDRHLP_LE2H_U16(u16) KLDRHLP_E2E_U16(u16)
97# define KLDRHLP_LE2H_U32(u32) KLDRHLP_E2E_U32(u32)
98# define KLDRHLP_LE2H_U32(u64) KLDRHLP_E2E_U64(u64)
99# define KLDRHLP_BE2H_U16(u16) ((uint16_t)(u16))
100# define KLDRHLP_BE2H_U32(u32) ((uint32_t)(u32))
101# define KLDRHLP_BE2H_U64(u64) ((uint32_t)(u32))
[2958]102#else
103# error "KLDR_BIG_ENDIAN or KLDR_LITTLE_ENDIAN is supposed to be defined."
[2827]104#endif
[2821]105
[2954]106/** @} */
107
108
[2825]109/*
[2944]110 * Compiler specific helpers / CRT.
[2825]111 * (I.e. operations that tend to have compiler intrinsic implementations).
112 */
[2944]113#ifndef KLDR_USE_CRT
114
115# ifdef __GNUC__
[2832]116/** memchr */
[2944]117# define kLdrHlpMemChr(a,b,c) __builtin_memchr(a,b,c)
[2825]118/** memcmp */
[2944]119# define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c)
[2825]120/** memcpy */
[2944]121# define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c)
[2880]122/** memmove */
[2883]123/*# define kLdrHlpMemMove(a,b,c) __builtin_memmove(a,b,c)*/
[2944]124# define kLdrHlpMemMove_needed
[2825]125/** memset */
[2944]126# define kLdrHlpMemSet(a,b,c) __builtin_memset(a,b,c)
[2832]127/** strchr */
[2944]128# define kLdrHlpStrChr(a, b) __builtin_strchr(a, b)
[2854]129/** strcmp */
[2944]130# define kLdrHlpStrComp(a, b) __builtin_strcmp(a, b)
[2891]131/** strncmp */
[2944]132# define kLdrHlpStrNComp(a,b,c) __builtin_strncmp(a, b, c)
[2825]133/** strlen */
[2944]134# define kLdrHlpStrLen(a) __builtin_strlen(a)
[2825]135/** alloca */
[2944]136# define kLdrHlpAllocA(a) __builtin_alloca(a)
[2825]137/** int3 */
[2944]138# define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
[2825]139/** NULL */
[2944]140# ifndef NULL
141# define NULL 0
142# endif
[2825]143# endif
144
[2944]145# ifdef _MSC_VER
146# include <string.h>
147# include <malloc.h>
148# pragma intrinsic(memcmp, memcpy, memset, strcmp, strlen, __debugbreak)
[2832]149/** memchr */
[2944]150# define kLdrHlpMemChr_needed
[2825]151/** memcmp */
[2944]152# define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
[2825]153/** memcpy */
[2944]154# define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
[2880]155/** memmove */
[2944]156# define kLdrHlpMemMove_needed
[2825]157/** memset */
[2944]158# define kLdrHlpMemSet(a,b,c) memset(a,b,c)
[2854]159/** strcmp */
[2944]160# define kLdrHlpStrComp(a, b) strcmp(a, b)
[2891]161/** strncmp */
[2944]162# define kLdrHlpStrNComp_needed
[2825]163/** strlen */
[2944]164# define kLdrHlpStrLen(a) strlen(a)
[2832]165/** strchr */
[2944]166# define kLdrHlpStrChr_needed
[2825]167/** alloca */
[2944]168# define kLdrHlpAllocA(a) alloca(a)
[2825]169/** int3 */
[2944]170# define kldrHlpBreakpoint() __debugbreak()
[2825]171/** NULL */
[2944]172# ifndef NULL
173# define NULL 0
174# endif
[2825]175# endif
176
[2944]177# ifdef kLdrHlpStrChr_needed
[2832]178char *kLdrHlpStrChr(const char *psz, int ch);
[2944]179# endif
180# ifdef kLdrHlpStrChr_needed
[2891]181int kLdrHlpStrNComp(const char *psz1, const char *psz2, size_t cch);
[2944]182# endif
183# ifdef kLdrHlpMemChr_needed
[2832]184void *kLdrHlpMemChr(const void *pv, int ch, size_t cb);
[2944]185# endif
186# ifdef kLdrHlpMemMove_needed
[2880]187void *kLdrHlpMemMove(void *pv1, const void *pv2, size_t cb);
[2944]188# endif
[2832]189
[2854]190
[2944]191#else /* KLDR_USE_CRT */
192
193# include <string.h>
194# include <stdlib.h>
195# ifdef _MSC_VER
196# include <malloc.h>
197# endif
198
199# define kLdrHlpMemChr(a,b,c) memchr(a,b,c)
200/** memcmp */
201# define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
202/** memcpy */
203# define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
204/** memmove */
205# define kLdrHlpMemMove(a,b,c) memmove(a,b,c)
206/** memset */
207# define kLdrHlpMemSet(a,b,c) memset(a,b,c)
208/** strchr */
209# define kLdrHlpStrChr(a, b) strchr(a, b)
210/** strcmp */
211# define kLdrHlpStrComp(a, b) strcmp(a, b)
212/** strncmp */
213# define kLdrHlpStrNComp(a,b,c) strncmp(a, b, c)
214/** strlen */
215# define kLdrHlpStrLen(a) strlen(a)
216/** alloca */
217# define kLdrHlpAllocA(a) alloca(a)
218/** int3 */
219# ifdef __GNUC__
220# define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
[2954]221# endif
[2944]222# ifdef _MSC_VER
223# define kldrHlpBreakpoint() __debugbreak()
[2954]224# endif
[2944]225
[2954]226#endif
[2944]227
[2832]228#if (!defined(kLdrHlpMemChr) && !defined(kLdrHlpStrChr_needed))\
229 || !defined(kLdrHlpMemComp) \
[2828]230 || !defined(kLdrHlpMemCopy) \
231 || !defined(kLdrHlpMemSet) \
[2832]232 || (!defined(kLdrHlpStrChr) && !defined(kLdrHlpStrChr_needed)) \
[2891]233 || !defined(kLdrHlpStrComp) \
234 || (!defined(kLdrHlpStrNComp) && !defined(kLdrHlpStrNComp_needed)) \
[2828]235 || !defined(kLdrHlpStrLen) \
236 || !defined(kLdrHlpAllocA) \
[2825]237 || !defined(kldrHlpBreakpoint)
238# error "Needs porting to your compiler."
239#endif
240
[2944]241#ifdef __cplusplus
242extern "C" {
[2954]243#endif
[2944]244
[2955]245size_t kLdrHlpStrNLen(const char *psz, size_t cchMax);
[2944]246int kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb);
247int kLdrHlpStrIComp(const char *pv1, const char *pv2);
248
[2830]249int kldrHlpSemInit(void);
250void kldrHlpSemTerm(void);
251int kldrHlpSemRequest(void);
252void kldrHlpSemRelease(void);
[2821]253
[2830]254int kldrHlpPageAlloc(void **ppv, size_t cb, KLDRPROT enmProt, unsigned fFixed);
255int kldrHlpPageProtect(void *pv, size_t cb, KLDRPROT enmProt);
256int kldrHlpPageFree(void *pv, size_t cb);
[2821]257
[2825]258int kldrHlpHeapInit(void);
259void kldrHlpHeapTerm(void);
260void kldrHlpHeapDonate(void *pv, size_t cb);
261void * kldrHlpAlloc(size_t cb);
[2847]262void * kldrHlpAllocZ(size_t cb);
[2825]263void kldrHlpFree(void *pv);
[2821]264
[2867]265int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t cchVal);
[2846]266int kldrHlpGetEnvUZ(const char *pszVar, size_t *pcb);
[2854]267char *kldrHlpGetFilename(const char *pszFilename);
[2867]268char *kldrHlpGetSuff(const char *pszFilename);
[2854]269char *kldrHlpGetExt(const char *pszFilename);
[2867]270int kldrHlpIsFilenameOnly(const char *pszFilename);
[2825]271void kldrHlpExit(int rc);
[2830]272void kldrHlpSleep(unsigned cMillies);
[2836]273char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase);
[2825]274void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
[2821]275
[2944]276#ifdef __cplusplus
277}
[2954]278#endif
[2944]279
280
[2825]281/** Assertion macro.
282 * Users should wrap it since this is ALWAYS compiled in. */
283#define kldrHlpAssert(expr) \
284 do { \
285 if (!(expr)) \
286 { \
287 kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \
288 kldrHlpBreakpoint(); \
289 } \
290 } while (0)
[2821]291
292
[2833]293/** @name Parameter validation macros
294 * @{ */
295
296/** Crash validation of a string argument. */
297#define KLDRHLP_VALIDATE_STRING(str) \
[2883]298 do { kLdrHlpStrLen(str); } while (0)
[2833]299
300/** Crash validation of an optional string argument. */
301#define KLDRHLP_VALIDATE_OPTIONAL_STRING(str) \
302 do { if (str) { KLDRHLP_VALIDATE_STRING(str); } } while (0)
303
304/** Return/Crash validation of an output buffer. */
305#define KLDRHLP_VALIDATE_BUFFER(buf, cb) \
306 do { \
307 if ((cb)) \
308 { \
309 uint8_t __b; \
310 uint8_t volatile * __pb = (uint8_t volatile *)(buf); \
311 size_t __cbPage1 = 0x1000 - ((uintptr_t)(__pb) & 0xfff); /* ASSUMES page size! */ \
312 __b = *__pb; *__pb = 0xff; *__pb = __b; \
313 if ((cb) > __cbPage1) \
314 { \
315 size_t __cb = (cb) - __cbPage1; \
316 __pb -= __cbPage1; \
317 for (;;) \
318 { \
319 __b = *__pb; *__pb = 0xff; *__pb = __b; \
320 if (__cb < 0x1000) \
321 break; \
322 __pb += 0x1000; \
323 __cb -= 0x1000; \
324 } \
325 } \
326 } \
327 else \
328 return KLDR_ERR_INVALID_PARAMETER; \
329 } while (0)
330
331/** Crash validation of an optional output buffer. */
332#define KLDRHLP_VALIDATE_OPTIONAL_BUFFER(buf, cb) \
333 do { \
334 if ((buf) != NULL && (cb) != 0) \
335 { \
336 KLDRHLP_VALIDATE_BUFFER(buf, cb); \
337 } \
338 } while (0)
339
340/** Return validation of an enum argument. */
341#define KLDRHLP_VALIDATE_ENUM(arg, enumname) \
342 do { \
343 if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \
344 { \
345 return KLDR_ERR_INVALID_PARAMETER; \
346 } \
347 } while (0)
348
[2851]349/** Return validation of a flags argument. */
350#define KLDRHLP_VALIDATE_FLAGS(arg, AllowedMask) \
351 do { \
352 if ((arg) & ~(AllowedMask)) \
353 { \
354 return KLDR_ERR_INVALID_PARAMETER; \
355 } \
356 } while (0)
357
[2825]358/** @} */
[2821]359
360
[2833]361/** @} */
362
[2825]363#endif /* __kLdrHlp_h__ */
[2821]364
Note: See TracBrowser for help on using the repository browser.