]> git.saurik.com Git - apple/xnu.git/blob - libkern/kxld/kxld_util.h
xnu-1504.7.4.tar.gz
[apple/xnu.git] / libkern / kxld / kxld_util.h
1 /*
2 * Copyright (c) 2007-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 #ifndef _KXLD_UTIL_H_
29 #define _KXLD_UTIL_H_
30
31 #include <mach/machine.h>
32 #include <sys/types.h>
33 #if KERNEL
34 #include <libkern/kxld_types.h>
35 #else
36 #include <architecture/byte_order.h>
37 #include "kxld_types.h"
38 #endif
39
40 /* 64-bit helpers */
41 #if !defined(KERNEL)
42
43 #define KXLD_3264_FUNC(cond32, rval, func32, func64, ...) \
44 do { \
45 if (cond32) { \
46 (rval) = (func32)(__VA_ARGS__); \
47 } else { \
48 (rval) = (func64)(__VA_ARGS__); \
49 } \
50 } while(0)
51
52 #elif defined(__LP64__)
53
54 #define KXLD_3264_FUNC(cond32, rval, func32, func64, ...) \
55 do { \
56 (rval) = (func64)(__VA_ARGS__); \
57 } while(0)
58
59 #else
60
61 #define KXLD_3264_FUNC(cond32, rval, func32, func64, ...) \
62 do { \
63 (rval) = (func32)(__VA_ARGS__); \
64 } while(0) \
65
66 #endif
67
68 /* Misc string functions */
69 #define streq(str1, str2) (((str1) && (str2)) ? !strcmp((str1), (str2)) : 0)
70 #define streq_safe(str1, str2, len) (((str1) && (str2)) ? \
71 !strncmp((str1), (str2), (len)) : 0)
72 #define const_strlen(str) (sizeof(str) - 1)
73
74 #define const_array_len(array) sizeof(array) / sizeof(*array)
75
76 /* Timing */
77 #define DECL_TIMER() struct timeval start, end;
78 #define START_TIMER() gettimeofday(&start, NULL);
79 #define END_TIMER() gettimeofday(&end, NULL);
80 #define PRINT_TIMER(msg) kxld_log("%s: %ds, %dus\n", (msg), \
81 (end.tv_sec - start.tv_sec), (end.tv_usec - start.tv_usec));
82
83 /* Misc definitions */
84 #define KXLD_MAX_NAME_LEN 256
85 #define KXLD_SEG_GOT "__DATA"
86 #define KXLD_SECT_GOT "__kxld_got"
87 #define KXLD_KMOD_INFO_SYMBOL "_kmod_info"
88 #define KXLD_WEAK_TEST_SYMBOL "_gOSKextUnresolved"
89 #define KXLD_OPERATOR_NEW_SYMBOL "__Znwm"
90 #define KXLD_OPERATOR_NEW_ARRAY_SYMBOL "__Znam"
91 #define KXLD_OPERATOR_DELETE_SYMBOL "__ZdlPv"
92 #define KXLD_OPERATOR_DELETE_ARRAY_SYMBOL "__ZdaPv"
93
94 struct kxld_section_name {
95 char segname[16];
96 char sectname[16];
97 };
98 typedef struct kxld_section_name KXLDSectionName;
99
100 /*******************************************************************************
101 * Logging
102 *******************************************************************************/
103
104 void kxld_set_logging_callback(KXLDLoggingCallback logging_callback)
105 __attribute__((visibility("hidden")));
106
107 void kxld_set_logging_callback_data(const char * name, void *user_data)
108 __attribute__((visibility("hidden")));
109
110 void kxld_log(KXLDLogSubsystem subsystem, KXLDLogLevel level,
111 const char *format, ...)
112 __attribute__((visibility("hidden"), format(printf, 3, 4)));
113
114 /* Common logging strings */
115 #define kKxldLogArchNotSupported "The target architecture (cputype 0x%x) is not supported by kxld."
116 #define kKxldLogArchNotFound "The kext does not contain a fat slice for the target architecture."
117 #define kKxldLogFiletypeNotSupported "The Mach-O filetype 0x%x is not supported on the target architecture."
118 #define kKxldLogTruncatedMachO "The Mach-O file has been truncated. Make sure the Mach-O header structures are correct."
119 #define kKxldLogMalformedMachO "The Mach-O file is malformed: "
120 #define kKxldLogMalformedVTable "The vtable '%s' is malformed. Make sure your kext has been built against the correct headers."
121 #define kKxldLogMissingVtable "Cannot find the vtable '%s' for class '%s'. This vtable symbol is required for binary compatibility, and it may have been stripped."
122 #define kKxldLogParentOutOfDate "The super class vtable '%s' for vtable '%s' is out of date. Make sure your kext has been built against the correct headers."
123 #define kKxldLogNoKmodInfo "The kext is missing its kmod_info structure."
124 #define kKxldLogInvalidSectReloc "Relocation entry %u from section %s,%s cannot be processed."
125 #define kKxldLogInvalidExtReloc "External relocation entry %u cannot be processed."
126 #define kKxldLogInvalidIntReloc "Internal relocation entry %u cannot be processed."
127 #define kKxldLogRelocationOverflow "A relocation entry has overflowed. The kext may be too far from one " \
128 "of its dependencies. Check your kext's load address."
129
130 /*******************************************************************************
131 * Allocators
132 *******************************************************************************/
133
134 void * kxld_alloc(size_t size)
135 __attribute__((malloc, visibility("hidden")));
136
137 void * kxld_page_alloc(size_t size)
138 __attribute__((malloc, visibility("hidden")));
139
140 void * kxld_page_alloc_untracked(size_t size)
141 __attribute__((malloc, visibility("hidden")));
142
143 void * kxld_alloc_pageable(size_t size)
144 __attribute__((malloc, visibility("hidden")));
145
146 /*******************************************************************************
147 * Deallocators
148 *******************************************************************************/
149
150 void kxld_free(void *ptr, size_t size)
151 __attribute__((visibility("hidden")));
152
153 void kxld_page_free(void *ptr, size_t size)
154 __attribute__((visibility("hidden")));
155
156 void kxld_page_free_untracked(void *ptr, size_t size)
157 __attribute__((visibility("hidden")));
158
159 /*******************************************************************************
160 * Mach-O Functions
161 *******************************************************************************/
162
163 kern_return_t validate_and_swap_macho_32(u_char *file, u_long size
164 #if !KERNEL
165 , enum NXByteOrder host_order
166 #endif /* !KERNEL */
167 ) __attribute__((visibility("hidden")));
168
169 kern_return_t validate_and_swap_macho_64(u_char *file, u_long size
170 #if !KERNEL
171 , enum NXByteOrder host_order
172 #endif /* !KERNEL */
173 ) __attribute__((visibility("hidden")));
174
175 #if !KERNEL
176 void unswap_macho(u_char *file, enum NXByteOrder host_order,
177 enum NXByteOrder target_order)
178 __attribute__((visibility("hidden")));
179 #endif /* !KERNEL */
180
181 /*******************************************************************************
182 * Miscellaneous
183 *******************************************************************************/
184
185 kxld_addr_t kxld_align_address(kxld_addr_t address, u_int align)
186 __attribute__((const, nonnull, visibility("hidden")));
187
188 boolean_t kxld_is_32_bit(cpu_type_t)
189 __attribute__((const, nonnull, visibility("hidden")));
190
191 const char * kxld_strstr(const char *s, const char *find)
192 __attribute__((pure, nonnull, visibility("hidden")));
193
194 /*******************************************************************************
195 * Debugging
196 *******************************************************************************/
197
198 void kxld_print_memory_report(void)
199 __attribute__((visibility("hidden")));
200
201 #endif /* _KXLD_UTIL_H_ */