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
RevLine 
[2826]1/* $Id: kLdrHlp.h 2954 2007-02-07 04:42:32Z 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) ) )
[2861]45/** Number of elements in an array. */
46#define KLDR_ELEMENTS(a) ( sizeof(a) / sizeof((a)[0]) )
47
[2954]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
[2827]74/** @def KLDRHLP_LE2H_U16
[2954]75 * Unsigned 16-bit little-endian to host endian. */
[2827]76/** @def KLDRHLP_LE2H_U32
[2954]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. */
[2827]86#if 1
87# define KLDRHLP_LE2H_U16(u16) ((uint16_t)(u16))
88# define KLDRHLP_LE2H_U32(u32) ((uint32_t)(u32))
[2954]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)
[2827]93#else
[2954]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))
[2827]100#endif
[2821]101
[2954]102/** @} */
103
104
[2825]105/*
[2944]106 * Compiler specific helpers / CRT.
[2825]107 * (I.e. operations that tend to have compiler intrinsic implementations).
108 */
[2944]109#ifndef KLDR_USE_CRT
110
111# ifdef __GNUC__
[2832]112/** memchr */
[2944]113# define kLdrHlpMemChr(a,b,c) __builtin_memchr(a,b,c)
[2825]114/** memcmp */
[2944]115# define kLdrHlpMemComp(a,b,c) __builtin_memcmp(a,b,c)
[2825]116/** memcpy */
[2944]117# define kLdrHlpMemCopy(a,b,c) __builtin_memcpy(a,b,c)
[2880]118/** memmove */
[2883]119/*# define kLdrHlpMemMove(a,b,c) __builtin_memmove(a,b,c)*/
[2944]120# define kLdrHlpMemMove_needed
[2825]121/** memset */
[2944]122# define kLdrHlpMemSet(a,b,c) __builtin_memset(a,b,c)
[2832]123/** strchr */
[2944]124# define kLdrHlpStrChr(a, b) __builtin_strchr(a, b)
[2854]125/** strcmp */
[2944]126# define kLdrHlpStrComp(a, b) __builtin_strcmp(a, b)
[2891]127/** strncmp */
[2944]128# define kLdrHlpStrNComp(a,b,c) __builtin_strncmp(a, b, c)
[2825]129/** strlen */
[2944]130# define kLdrHlpStrLen(a) __builtin_strlen(a)
[2825]131/** alloca */
[2944]132# define kLdrHlpAllocA(a) __builtin_alloca(a)
[2825]133/** int3 */
[2944]134# define kldrHlpBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
[2825]135/** NULL */
[2944]136# ifndef NULL
137# define NULL 0
138# endif
[2825]139# endif
140
[2944]141# ifdef _MSC_VER
142# include <string.h>
143# include <malloc.h>
144# pragma intrinsic(memcmp, memcpy, memset, strcmp, strlen, __debugbreak)
[2832]145/** memchr */
[2944]146# define kLdrHlpMemChr_needed
[2825]147/** memcmp */
[2944]148# define kLdrHlpMemComp(a,b,c) memcmp(a,b,c)
[2825]149/** memcpy */
[2944]150# define kLdrHlpMemCopy(a,b,c) memcpy(a,b,c)
[2880]151/** memmove */
[2944]152# define kLdrHlpMemMove_needed
[2825]153/** memset */
[2944]154# define kLdrHlpMemSet(a,b,c) memset(a,b,c)
[2854]155/** strcmp */
[2944]156# define kLdrHlpStrComp(a, b) strcmp(a, b)
[2891]157/** strncmp */
[2944]158# define kLdrHlpStrNComp_needed
[2825]159/** strlen */
[2944]160# define kLdrHlpStrLen(a) strlen(a)
[2832]161/** strchr */
[2944]162# define kLdrHlpStrChr_needed
[2825]163/** alloca */
[2944]164# define kLdrHlpAllocA(a) alloca(a)
[2825]165/** int3 */
[2944]166# define kldrHlpBreakpoint() __debugbreak()
[2825]167/** NULL */
[2944]168# ifndef NULL
169# define NULL 0
170# endif
[2825]171# endif
172
[2944]173# ifdef kLdrHlpStrChr_needed
[2832]174char *kLdrHlpStrChr(const char *psz, int ch);
[2944]175# endif
176# ifdef kLdrHlpStrChr_needed
[2891]177int kLdrHlpStrNComp(const char *psz1, const char *psz2, size_t cch);
[2944]178# endif
179# ifdef kLdrHlpMemChr_needed
[2832]180void *kLdrHlpMemChr(const void *pv, int ch, size_t cb);
[2944]181# endif
182# ifdef kLdrHlpMemMove_needed
[2880]183void *kLdrHlpMemMove(void *pv1, const void *pv2, size_t cb);
[2944]184# endif
[2832]185
[2854]186
[2944]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)
[2954]217# endif
[2944]218# ifdef _MSC_VER
219# define kldrHlpBreakpoint() __debugbreak()
[2954]220# endif
[2944]221
[2954]222#endif
[2944]223
[2832]224#if (!defined(kLdrHlpMemChr) && !defined(kLdrHlpStrChr_needed))\
225 || !defined(kLdrHlpMemComp) \
[2828]226 || !defined(kLdrHlpMemCopy) \
227 || !defined(kLdrHlpMemSet) \
[2832]228 || (!defined(kLdrHlpStrChr) && !defined(kLdrHlpStrChr_needed)) \
[2891]229 || !defined(kLdrHlpStrComp) \
230 || (!defined(kLdrHlpStrNComp) && !defined(kLdrHlpStrNComp_needed)) \
[2828]231 || !defined(kLdrHlpStrLen) \
232 || !defined(kLdrHlpAllocA) \
[2825]233 || !defined(kldrHlpBreakpoint)
234# error "Needs porting to your compiler."
235#endif
236
[2944]237#ifdef __cplusplus
238extern "C" {
[2954]239#endif
[2944]240
241int kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb);
242int kLdrHlpStrIComp(const char *pv1, const char *pv2);
243
[2830]244int kldrHlpSemInit(void);
245void kldrHlpSemTerm(void);
246int kldrHlpSemRequest(void);
247void kldrHlpSemRelease(void);
[2821]248
[2830]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);
[2821]252
[2825]253int kldrHlpHeapInit(void);
254void kldrHlpHeapTerm(void);
255void kldrHlpHeapDonate(void *pv, size_t cb);
256void * kldrHlpAlloc(size_t cb);
[2847]257void * kldrHlpAllocZ(size_t cb);
[2825]258void kldrHlpFree(void *pv);
[2821]259
[2867]260int kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t cchVal);
[2846]261int kldrHlpGetEnvUZ(const char *pszVar, size_t *pcb);
[2854]262char *kldrHlpGetFilename(const char *pszFilename);
[2867]263char *kldrHlpGetSuff(const char *pszFilename);
[2854]264char *kldrHlpGetExt(const char *pszFilename);
[2867]265int kldrHlpIsFilenameOnly(const char *pszFilename);
[2825]266void kldrHlpExit(int rc);
[2830]267void kldrHlpSleep(unsigned cMillies);
[2836]268char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase);
[2825]269void kldrHlpAssertMsg(const char *pszExpr, const char *pszFile, unsigned iLine, const char *pszFunction);
[2821]270
[2944]271#ifdef __cplusplus
272}
[2954]273#endif
[2944]274
275
[2825]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)
[2821]286
287
[2833]288/** @name Parameter validation macros
289 * @{ */
290
291/** Crash validation of a string argument. */
292#define KLDRHLP_VALIDATE_STRING(str) \
[2883]293 do { kLdrHlpStrLen(str); } while (0)
[2833]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
[2851]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
[2825]353/** @} */
[2821]354
355
[2833]356/** @} */
357
[2825]358#endif /* __kLdrHlp_h__ */
[2821]359
Note: See TracBrowser for help on using the repository browser.