source: trunk/kLdr/kLdrHlp.h@ 2955

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

Completed kldrModMachOParseLoadCommands and kldrModMachOSize. Added an kLdrErrStr API.

  • Property svn:keywords set to Id
File size: 11.8 KB
Line 
1/* $Id: kLdrHlp.h 2955 2007-02-07 07:07:16Z 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#if 1
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#else
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#endif
103
104/** @} */
105
106
107/*
108 * Compiler specific helpers / CRT.
109 * (I.e. operations that tend to have compiler intrinsic implementations).
110 */
111#ifndef KLDR_USE_CRT
112
113# ifdef __GNUC__
114/** memchr */
115# define kLdrHlpMemChr(a,b,c) __builtin_memchr(a,b,c)
116/** memcmp */
117# define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c)
118/** memcpy */
119# define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c)
120/** memmove */
121/*# define kLdrHlpMemMove(a,b,c) __builtin_memmove(a,b,c)*/
122# define kLdrHlpMemMove_needed
123/** memset */
124# define kLdrHlpMemSet(a,b,c) __builtin_memset(a,b,c)
125/** strchr */
126# define kLdrHlpStrChr(a, b) __builtin_strchr(a, b)
127/** strcmp */
128# define kLdrHlpStrComp(a, b) __builtin_strcmp(a, b)
129/** strncmp */
130# define kLdrHlpStrNComp(a,b,c) __builtin_strncmp(a, b, c)
131/** strlen */
132# define kLdrHlpStrLen(a) __builtin_strlen(a)
133/** alloca */
134# define kLdrHlpAllocA(a) __builtin_alloca(a)
135/** int3 */
136# define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
137/** NULL */
138# ifndef NULL
139# define NULL 0
140# endif
141# endif
142
143# ifdef _MSC_VER
144# include <string.h>
145# include <malloc.h>
146# pragma intrinsic(memcmp, memcpy, memset, strcmp, strlen, __debugbreak)
147/** memchr */
148# define kLdrHlpMemChr_needed
149/** memcmp */
150# define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
151/** memcpy */
152# define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
153/** memmove */
154# define kLdrHlpMemMove_needed
155/** memset */
156# define kLdrHlpMemSet(a,b,c) memset(a,b,c)
157/** strcmp */
158# define kLdrHlpStrComp(a, b) strcmp(a, b)
159/** strncmp */
160# define kLdrHlpStrNComp_needed
161/** strlen */
162# define kLdrHlpStrLen(a) strlen(a)
163/** strchr */
164# define kLdrHlpStrChr_needed
165/** alloca */
166# define kLdrHlpAllocA(a) alloca(a)
167/** int3 */
168# define kldrHlpBreakpoint() __debugbreak()
169/** NULL */
170# ifndef NULL
171# define NULL 0
172# endif
173# endif
174
175# ifdef kLdrHlpStrChr_needed
176char *kLdrHlpStrChr(const char *psz, int ch);
177# endif
178# ifdef kLdrHlpStrChr_needed
179int kLdrHlpStrNComp(const char *psz1, const char *psz2, size_t cch);
180# endif
181# ifdef kLdrHlpMemChr_needed
182void *kLdrHlpMemChr(const void *pv, int ch, size_t cb);
183# endif
184# ifdef kLdrHlpMemMove_needed
185void *kLdrHlpMemMove(void *pv1, const void *pv2, size_t cb);
186# endif
187
188
189#else /* KLDR_USE_CRT */
190
191# include <string.h>
192# include <stdlib.h>
193# ifdef _MSC_VER
194# include <malloc.h>
195# endif
196
197# define kLdrHlpMemChr(a,b,c) memchr(a,b,c)
198/** memcmp */
199# define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
200/** memcpy */
201# define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
202/** memmove */
203# define kLdrHlpMemMove(a,b,c) memmove(a,b,c)
204/** memset */
205# define kLdrHlpMemSet(a,b,c) memset(a,b,c)
206/** strchr */
207# define kLdrHlpStrChr(a, b) strchr(a, b)
208/** strcmp */
209# define kLdrHlpStrComp(a, b) strcmp(a, b)
210/** strncmp */
211# define kLdrHlpStrNComp(a,b,c) strncmp(a, b, c)
212/** strlen */
213# define kLdrHlpStrLen(a) strlen(a)
214/** alloca */
215# define kLdrHlpAllocA(a) alloca(a)
216/** int3 */
217# ifdef __GNUC__
218# define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
219# endif
220# ifdef _MSC_VER
221# define kldrHlpBreakpoint() __debugbreak()
222# endif
223
224#endif
225
226#if (!defined(kLdrHlpMemChr) && !defined(kLdrHlpStrChr_needed))\
227 || !defined(kLdrHlpMemComp) \
228 || !defined(kLdrHlpMemCopy) \
229 || !defined(kLdrHlpMemSet) \
230 || (!defined(kLdrHlpStrChr) && !defined(kLdrHlpStrChr_needed)) \
231 || !defined(kLdrHlpStrComp) \
232 || (!defined(kLdrHlpStrNComp) && !defined(kLdrHlpStrNComp_needed)) \
233 || !defined(kLdrHlpStrLen) \
234 || !defined(kLdrHlpAllocA) \
235 || !defined(kldrHlpBreakpoint)
236# error "Needs porting to your compiler."
237#endif
238
239#ifdef __cplusplus
240extern "C" {
241#endif
242
243size_t kLdrHlpStrNLen(const char *psz, size_t cchMax);
244int kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb);
245int kLdrHlpStrIComp(const char *pv1, const char *pv2);
246
247int kldrHlpSemInit(void);
248void kldrHlpSemTerm(void);
249int kldrHlpSemRequest(void);
250void kldrHlpSemRelease(void);
251
252int kldrHlpPageAlloc(void **ppv, size_t cb, KLDRPROT enmProt, unsigned fFixed);
253int kldrHlpPageProtect(void *pv, size_t cb, KLDRPROT enmProt);
254int kldrHlpPageFree(void *pv, size_t cb);
255
256int kldrHlpHeapInit(void);
257void kldrHlpHeapTerm(void);
258void kldrHlpHeapDonate(void *pv, size_t cb);
259void * kldrHlpAlloc(size_t cb);
260void * kldrHlpAllocZ(size_t cb);
261void kldrHlpFree(void *pv);
262
263int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t cchVal);
264int kldrHlpGetEnvUZ(const char *pszVar, size_t *pcb);
265char *kldrHlpGetFilename(const char *pszFilename);
266char *kldrHlpGetSuff(const char *pszFilename);
267char *kldrHlpGetExt(const char *pszFilename);
268int kldrHlpIsFilenameOnly(const char *pszFilename);
269void kldrHlpExit(int rc);
270void kldrHlpSleep(unsigned cMillies);
271char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase);
272void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
273
274#ifdef __cplusplus
275}
276#endif
277
278
279/** Assertion macro.
280 * Users should wrap it since this is ALWAYS compiled in. */
281#define kldrHlpAssert(expr) \
282 do { \
283 if (!(expr)) \
284 { \
285 kldrHlpAssertMsg(#expr, __FILE__, __LINE__, __FUNCTION__); \
286 kldrHlpBreakpoint(); \
287 } \
288 } while (0)
289
290
291/** @name Parameter validation macros
292 * @{ */
293
294/** Crash validation of a string argument. */
295#define KLDRHLP_VALIDATE_STRING(str) \
296 do { kLdrHlpStrLen(str); } while (0)
297
298/** Crash validation of an optional string argument. */
299#define KLDRHLP_VALIDATE_OPTIONAL_STRING(str) \
300 do { if (str) { KLDRHLP_VALIDATE_STRING(str); } } while (0)
301
302/** Return/Crash validation of an output buffer. */
303#define KLDRHLP_VALIDATE_BUFFER(buf, cb) \
304 do { \
305 if ((cb)) \
306 { \
307 uint8_t __b; \
308 uint8_t volatile * __pb = (uint8_t volatile *)(buf); \
309 size_t __cbPage1 = 0x1000 - ((uintptr_t)(__pb) & 0xfff); /* ASSUMES page size! */ \
310 __b = *__pb; *__pb = 0xff; *__pb = __b; \
311 if ((cb) > __cbPage1) \
312 { \
313 size_t __cb = (cb) - __cbPage1; \
314 __pb -= __cbPage1; \
315 for (;;) \
316 { \
317 __b = *__pb; *__pb = 0xff; *__pb = __b; \
318 if (__cb < 0x1000) \
319 break; \
320 __pb += 0x1000; \
321 __cb -= 0x1000; \
322 } \
323 } \
324 } \
325 else \
326 return KLDR_ERR_INVALID_PARAMETER; \
327 } while (0)
328
329/** Crash validation of an optional output buffer. */
330#define KLDRHLP_VALIDATE_OPTIONAL_BUFFER(buf, cb) \
331 do { \
332 if ((buf) != NULL && (cb) != 0) \
333 { \
334 KLDRHLP_VALIDATE_BUFFER(buf, cb); \
335 } \
336 } while (0)
337
338/** Return validation of an enum argument. */
339#define KLDRHLP_VALIDATE_ENUM(arg, enumname) \
340 do { \
341 if ((arg) <= enumname##_INVALID || (arg) >= enumname##_END) \
342 { \
343 return KLDR_ERR_INVALID_PARAMETER; \
344 } \
345 } while (0)
346
347/** Return validation of a flags argument. */
348#define KLDRHLP_VALIDATE_FLAGS(arg, AllowedMask) \
349 do { \
350 if ((arg) & ~(AllowedMask)) \
351 { \
352 return KLDR_ERR_INVALID_PARAMETER; \
353 } \
354 } while (0)
355
356/** @} */
357
358
359/** @} */
360
361#endif /* __kLdrHlp_h__ */
362
Note: See TracBrowser for help on using the repository browser.