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