source: trunk/kLdr/kLdrHlp.h@ 2954

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

implemented kldrModMachOPreParseLoadCommands

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