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 <sys/types.h> 32 #if KERNEL 33 #include <libkern/kxld_types.h> 34 #include <mach/machine.h> 35 #else 36 #include <architecture/byte_order.h> 37 #include "kxld_types.h" 38 39 /* Get machine.h from the kernel source so we can support all platforms 40 * that the kernel supports. Otherwise we're at the mercy of the host. 41 */ 42 #include "../../osfmk/mach/machine.h" 43 #endif 44 45 /* 64-bit helpers */ 46 #if !defined(KERNEL) 47 48 #define KXLD_3264_FUNC(cond32, rval, func32, func64, ...) \ 49 do { \ 50 if (cond32) { \ 51 (rval) = (func32)(__VA_ARGS__); \ 52 } else { \ 53 (rval) = (func64)(__VA_ARGS__); \ 54 } \ 55 } while(0) 56 57 #elif defined(__LP64__) 58 59 #define KXLD_3264_FUNC(cond32, rval, func32, func64, ...) \ 60 do { \ 61 (rval) = (func64)(__VA_ARGS__); \ 62 } while(0) 63 64 #else 65 66 #define KXLD_3264_FUNC(cond32, rval, func32, func64, ...) \ 67 do { \ 68 (rval) = (func32)(__VA_ARGS__); \ 69 } while(0) \ 70 71 #endif 72 73 /* Misc string functions */ 74 #define streq(str1, str2) (((str1) && (str2)) ? !strcmp((str1), (str2)) : 0) 75 #define streq_safe(str1, str2, len) (((str1) && (str2)) ? \ 76 !strncmp((str1), (str2), (len)) : 0) 77 #define const_strlen(str) (sizeof(str) - 1) 78 79 #define const_array_len(array) sizeof(array) / sizeof(*array) 80 81 /* Timing */ 82 #define DECL_TIMER() struct timeval start, end; 83 #define START_TIMER() gettimeofday(&start, NULL); 84 #define END_TIMER() gettimeofday(&end, NULL); 85 #define PRINT_TIMER(msg) kxld_log("%s: %ds, %dus\n", (msg), \ 86 (end.tv_sec - start.tv_sec), (end.tv_usec - start.tv_usec)); 87 88 /* Misc definitions */ 89 #define KXLD_MAX_NAME_LEN 256 90 #define KXLD_SEG_GOT "__DATA" 91 #define KXLD_SECT_GOT "__kxld_got" 92 #define KXLD_KMOD_INFO_SYMBOL "_kmod_info" 93 #define KXLD_WEAK_TEST_SYMBOL "_gOSKextUnresolved" 94 #define KXLD_OPERATOR_NEW_SYMBOL "__Znwm" 95 #define KXLD_OPERATOR_NEW_ARRAY_SYMBOL "__Znam" 96 #define KXLD_OPERATOR_DELETE_SYMBOL "__ZdlPv" 97 #define KXLD_OPERATOR_DELETE_ARRAY_SYMBOL "__ZdaPv" 98 99 struct kxld_section_name { 100 char segname[16]; 101 char sectname[16]; 102 }; 103 typedef struct kxld_section_name KXLDSectionName; 104 105 /******************************************************************************* 106 * Logging 107 *******************************************************************************/ 108 109 void kxld_set_logging_callback(KXLDLoggingCallback logging_callback) 110 __attribute__((visibility("hidden"))); 111 112 void kxld_set_logging_callback_data(const char * name, void *user_data) 113 __attribute__((visibility("hidden"))); 114 115 void kxld_log(KXLDLogSubsystem subsystem, KXLDLogLevel level, 116 const char *format, ...) 117 __attribute__((visibility("hidden"), format(printf, 3, 4))); 118 119 /* Common logging strings */ 120 #define kKxldLogArchNotSupported "The target architecture (cputype 0x%x) is not supported by kxld." 121 #define kKxldLogArchNotFound "The kext does not contain a fat slice for the target architecture." 122 #define kKxldLogFiletypeNotSupported "The Mach-O filetype 0x%x is not supported on the target architecture." 123 #define kKxldLogTruncatedMachO "The Mach-O file has been truncated. Make sure the Mach-O header structures are correct." 124 #define kKxldLogMalformedMachO "The Mach-O file is malformed: " 125 #define kKxldLogMalformedVTable "The vtable '%s' is malformed. Make sure your kext has been built against the correct headers." 126 #define kKxldLogMissingVtable "Cannot find the vtable '%s' for class '%s'. This vtable symbol is required for binary compatibility, and it may have been stripped." 127 #define kKxldLogDirectPureVirtualCall "This kext calls a pure virtual function. Make sure your kext's OSObject-derived classes implement all pure virtual functions." 128 #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." 129 #define kKxldLogNoKmodInfo "The kext is missing its kmod_info structure." 130 #define kKxldLogRelocationOverflow "A relocation entry has overflowed. The kext may be too far from one " \ 131 "of its dependencies. Check your kext's load address." 132 #define kKxldLogRelocatingPatchedSym "Warning: relocation failed because some class in this kext " \ 133 "didn't use the OSDeclareDefaultStructors and OSDefineMetaClassAndStructors, " \ 134 "or didn't export all vtable symbols, so it still " \ 135 "references %s, which has been patched with another symbol for binary compatibility. " \ 136 "Please make sure all classes that inherit from OSObject use these macros." 137 138 /******************************************************************************* 139 * Allocators 140 *******************************************************************************/ 141 142 void * kxld_calloc(size_t size) 143 __attribute__((malloc, visibility("hidden"))); 144 145 void * kxld_alloc(size_t size) 146 __attribute__((malloc, visibility("hidden"))); 147 148 void * kxld_page_alloc(size_t size) 149 __attribute__((malloc, visibility("hidden"))); 150 151 void * kxld_page_alloc_untracked(size_t size) 152 __attribute__((malloc, visibility("hidden"))); 153 154 /******************************************************************************* 155 * Deallocators 156 *******************************************************************************/ 157 158 void kxld_free(void *ptr, size_t size) 159 __attribute__((visibility("hidden"))); 160 161 void kxld_page_free(void *ptr, size_t size) 162 __attribute__((visibility("hidden"))); 163 164 void kxld_page_free_untracked(void *ptr, size_t size) 165 __attribute__((visibility("hidden"))); 166 167 /******************************************************************************* 168 * Mach-O Functions 169 *******************************************************************************/ 170 171 kern_return_t validate_and_swap_macho_32(u_char *file, u_long size 172 #if !KERNEL 173 , enum NXByteOrder host_order 174 #endif /* !KERNEL */ 175 ) __attribute__((visibility("hidden"))); 176 177 kern_return_t validate_and_swap_macho_64(u_char *file, u_long size 178 #if !KERNEL 179 , enum NXByteOrder host_order 180 #endif /* !KERNEL */ 181 ) __attribute__((visibility("hidden"))); 182 183 #if !KERNEL 184 void unswap_macho(u_char *file, enum NXByteOrder host_order, 185 enum NXByteOrder target_order) 186 __attribute__((visibility("hidden"))); 187 #endif /* !KERNEL */ 188 189 /******************************************************************************* 190 * Miscellaneous 191 *******************************************************************************/ 192 193 kxld_addr_t kxld_align_address(kxld_addr_t address, u_int align) 194 __attribute__((const, visibility("hidden"))); 195 196 boolean_t kxld_is_32_bit(cpu_type_t) 197 __attribute__((const, visibility("hidden"))); 198 199 /******************************************************************************* 200 * Debugging 201 *******************************************************************************/ 202 203 void kxld_print_memory_report(void) 204 __attribute__((visibility("hidden"))); 205 206 /******************************************************************************* 207 * Cross Linking 208 *******************************************************************************/ 209 #if !KERNEL 210 boolean_t kxld_set_cross_link_page_size(kxld_size_t target_page_size); 211 #endif /* !KERNEL */ 212 kxld_size_t kxld_get_effective_page_size(void); 213 kxld_addr_t kxld_round_page_cross_safe(kxld_addr_t addr); 214 215 #if SPLIT_KEXTS_DEBUG 216 void kxld_show_split_info(splitKextLinkInfo *info); 217 #endif 218 219 #endif /* _KXLD_UTIL_H_ */ 220