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
Line 
1/* $Id: kLdrHlp.h 2958 2007-02-09 05:12:22Z bird $ */
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
31/** @defgroup grp_kLdrHlp kLdrHlp - Helper Functions
32 * @internal
33 * @{ */
34
35/** Get the minimum of two values. */
36#define KLDR_MIN(a, b) ((a) <= (b) ? (a) : (b))
37/** Get the maximum of two values. */
38#define KLDR_MAX(a, b) ((a) >= (b) ? (a) : (b))
39/** Calculate the offset of a structure member. */
40#define KLDR_OFFSETOF(strct, memb) ( (size_t)( &((strct *)0)->memb ) )
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) ) )
45/** Align a size_t value. */
46#define KLDR_ALIGN_ADDR(val, align) ( ((val) + ((align) - 1)) & ~(KLDRADDR)((align) - 1) )
47/** Number of elements in an array. */
48#define KLDR_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
49
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
76/** @def KLDRHLP_LE2H_U16
77 * Unsigned 16-bit little-endian to host endian. */
78/** @def KLDRHLP_LE2H_U32
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. */
88#ifdef KLDR_LITTLE_ENDIAN
89# define KLDRHLP_LE2H_U16(u16) ((uint16_t)(u16))
90# define KLDRHLP_LE2H_U32(u32) ((uint32_t)(u32))
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)
95#elif defined(KLDR_BIG_ENDIAN)
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))
102#else
103# error "KLDR_BIG_ENDIAN or KLDR_LITTLE_ENDIAN is supposed to be defined."
104#endif
105
106/** @} */
107
108
109/*
110 * Compiler specific helpers / CRT.
111 * (I.e. operations that tend to have compiler intrinsic implementations).
112 */
113#ifndef KLDR_USE_CRT
114
115# ifdef __GNUC__
116/** memchr */
117# define kLdrHlpMemChr(a,b,c) __builtin_memchr(a,b,c)
118/** memcmp */
119# define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c)
120/** memcpy */
121# define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c)
122/** memmove */
123/*# define kLdrHlpMemMove(a,b,c) __builtin_memmove(a,b,c)*/
124# define kLdrHlpMemMove_needed
125/** memset */
126# define kLdrHlpMemSet(a,b,c) __builtin_memset(a,b,c)
127/** strchr */
128# define kLdrHlpStrChr(a, b) __builtin_strchr(a, b)
129/** strcmp */
130# define kLdrHlpStrComp(a, b) __builtin_strcmp(a, b)
131/** strncmp */
132# define kLdrHlpStrNComp(a,b,c) __builtin_strncmp(a, b, c)
133/** strlen */
134# define kLdrHlpStrLen(a) __builtin_strlen(a)
135/** alloca */
136# define kLdrHlpAllocA(a) __builtin_alloca(a)
137/** int3 */
138# define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
139/** NULL */
140# ifndef NULL
141# define NULL 0
142# endif
143# endif
144
145# ifdef _MSC_VER
146# include <string.h>
147# include <malloc.h>
148# pragma intrinsic(memcmp, memcpy, memset, strcmp, strlen, __debugbreak)
149/** memchr */
150# define kLdrHlpMemChr_needed
151/** memcmp */
152# define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
153/** memcpy */
154# define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
155/** memmove */
156# define kLdrHlpMemMove_needed
157/** memset */
158# define kLdrHlpMemSet(a,b,c) memset(a,b,c)
159/** strcmp */
160# define kLdrHlpStrComp(a, b) strcmp(a, b)
161/** strncmp */
162# define kLdrHlpStrNComp_needed
163/** strlen */
164# define kLdrHlpStrLen(a) strlen(a)
165/** strchr */
166# define kLdrHlpStrChr_needed
167/** alloca */
168# define kLdrHlpAllocA(a) alloca(a)
169/** int3 */
170# define kldrHlpBreakpoint() __debugbreak()
171/** NULL */
172# ifndef NULL
173# define NULL 0
174# endif
175# endif
176
177# ifdef kLdrHlpStrChr_needed
178char *kLdrHlpStrChr(const char *psz, int ch);
179# endif
180# ifdef kLdrHlpStrChr_needed
181int kLdrHlpStrNComp(const char *psz1, const char *psz2, size_t cch);
182# endif
183# ifdef kLdrHlpMemChr_needed
184void *kLdrHlpMemChr(const void *pv, int ch, size_t cb);
185# endif
186# ifdef kLdrHlpMemMove_needed
187void *kLdrHlpMemMove(void *pv1, const void *pv2, size_t cb);
188# endif
189
190
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)
221# endif
222# ifdef _MSC_VER
223# define kldrHlpBreakpoint() __debugbreak()
224# endif
225
226#endif
227
228#if (!defined(kLdrHlpMemChr) && !defined(kLdrHlpStrChr_needed))\
229 || !defined(kLdrHlpMemComp) \
230 || !defined(kLdrHlpMemCopy) \
231 || !defined(kLdrHlpMemSet) \
232 || (!defined(kLdrHlpStrChr) && !defined(kLdrHlpStrChr_needed)) \
233 || !defined(kLdrHlpStrComp) \
234 || (!defined(kLdrHlpStrNComp) && !defined(kLdrHlpStrNComp_needed)) \
235 || !defined(kLdrHlpStrLen) \
236 || !defined(kLdrHlpAllocA) \
237 || !defined(kldrHlpBreakpoint)
238# error "Needs porting to your compiler."
239#endif
240
241#ifdef __cplusplus
242extern "C" {
243#endif
244
245size_t kLdrHlpStrNLen(const char *psz, size_t cchMax);
246int kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb);
247int kLdrHlpStrIComp(const char *pv1, const char *pv2);
248
249int kldrHlpSemInit(void);
250void kldrHlpSemTerm(void);
251int kldrHlpSemRequest(void);
252void kldrHlpSemRelease(void);
253
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);
257
258int kldrHlpHeapInit(void);
259void kldrHlpHeapTerm(void);
260void kldrHlpHeapDonate(void *pv, size_t cb);
261void * kldrHlpAlloc(size_t cb);
262void * kldrHlpAllocZ(size_t cb);
263void kldrHlpFree(void *pv);
264
265int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t cchVal);
266int kldrHlpGetEnvUZ(const char *pszVar, size_t *pcb);
267char *kldrHlpGetFilename(const char *pszFilename);
268char *kldrHlpGetSuff(const char *pszFilename);
269char *kldrHlpGetExt(const char *pszFilename);
270int kldrHlpIsFilenameOnly(const char *pszFilename);
271void kldrHlpExit(int rc);
272void kldrHlpSleep(unsigned cMillies);
273char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase);
274void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
275
276#ifdef __cplusplus
277}
278#endif
279
280
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)
291
292
293/** @name Parameter validation macros
294 * @{ */
295
296/** Crash validation of a string argument. */
297#define KLDRHLP_VALIDATE_STRING(str) \
298 do { kLdrHlpStrLen(str); } while (0)
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
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
358/** @} */
359
360
361/** @} */
362
363#endif /* __kLdrHlp_h__ */
364
Note: See TracBrowser for help on using the repository browser.