]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/kxld_types.h
cad04f811a7b4feec463155d5690e099831641d5
[apple/xnu.git] / libkern / libkern / kxld_types.h
1 /*
2 * Copyright (c) 2007-2012 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_TYPES_H
29 #define _KXLD_TYPES_H
30
31 #include <stdarg.h>
32 #include <stdint.h>
33 #include <mach/boolean.h> // boolean_t
34 #include <mach/kern_return.h>
35
36 /*******************************************************************************
37 * Macros
38 *******************************************************************************/
39
40 /* For 32-bit-specific linking code */
41 #if (!KERNEL || !__LP64__)
42 #define KXLD_USER_OR_ILP32 1
43 #endif
44
45 /* For 64-bit-specific linking code */
46 #if (!KERNEL || __LP64__)
47 #define KXLD_USER_OR_LP64 1
48 #endif
49
50 /* For i386-specific linking code */
51 #if (!KERNEL || __i386__)
52 #define KXLD_USER_OR_I386 1
53 #endif
54
55 /* For x86_64-specific linking code */
56 #if (!KERNEL || __x86_64__)
57 #define KXLD_USER_OR_X86_64 1
58 #endif
59
60 /* For arm-specific linking code */
61 #if (!KERNEL || __arm__)
62 #define KXLD_USER_OR_ARM 1
63 #endif
64
65 /* For arm64-specific linking code */
66 #if (!KERNEL || __arm64__)
67 #define KXLD_USER_OR_ARM64 1
68 #endif
69
70 /* For linking code specific to architectures that support common symbols */
71 #if (!KERNEL || __i386__)
72 #define KXLD_USER_OR_COMMON 1
73 #endif
74
75 /* For linking code specific to architectures that support strict patching */
76 #define KXLD_USER_OR_STRICT_PATCHING 1
77
78 /* For linking code specific to architectures that use MH_OBJECT */
79 #if (!KERNEL || __i386__)
80 #define KXLD_USER_OR_OBJECT 1
81 #endif
82
83 /* For linking code specific to architectures that use MH_KEXT_BUNDLE */
84 #define KXLD_USER_OR_BUNDLE 1
85
86 /* We no longer need to generate our own GOT for any architectures, but the code
87 * required to do this will be saved inside this macro.
88 */
89 #define KXLD_USER_OR_GOT 0
90
91 /* for building the dysymtab command generation into the dylib */
92 #if (!KERNEL)
93 #define KXLD_PIC_KEXTS 1
94 // #define SPLIT_KEXTS 1
95 #define SPLIT_KEXTS_DEBUG 0
96 #endif
97
98 /*******************************************************************************
99 * Types
100 *******************************************************************************/
101
102 /* Maintains linker state across links. One context should be allocated for
103 * each link thread.
104 */
105 typedef struct kxld_context KXLDContext;
106
107 /* Unless we're in a 32-bit kernel, all internal math is performed in 64 bits
108 * and cast to smaller values as needed by the architecture for which we are
109 * linking. All returned arguments should be handled similarly.
110 * Note: This size can be increased for future architectural size increases
111 */
112 #if KERNEL && !__LP64__
113 typedef uint32_t kxld_addr_t;
114 typedef uint32_t kxld_size_t;
115 #else
116 typedef uint64_t kxld_addr_t;
117 typedef uint64_t kxld_size_t;
118 #endif /* KERNEL && !__LP64__ */
119
120 typedef struct splitKextLinkInfo {
121 u_char * kextExecutable; // kext we will link
122 size_t kextSize; // size of kextExecutable
123 u_char * linkedKext; // linked kext
124 size_t linkedKextSize; // size of linkedKext
125 uint64_t vmaddr_TEXT; // vmaddr of kext __TEXT segment
126 uint64_t vmaddr_TEXT_EXEC; // vmaddr of kext __TEXT_EXEC segment
127 uint64_t vmaddr_DATA; // vmaddr of kext __DATA segment
128 uint64_t vmaddr_DATA_CONST; // vmaddr of kext __DATA_CONST segment
129 uint64_t vmaddr_LINKEDIT; // vmaddr of kext __LINKEDIT segment
130 uint32_t kaslr_offsets_count; // offsets into the kext to slide
131 uint32_t * kaslr_offsets; // offsets into the kext to slide
132 } splitKextLinkInfo;
133
134 /* Flags for general linker behavior */
135 enum kxld_flags {
136 kKxldFlagDefault = 0x0,
137 kKXLDFlagIncludeRelocs = 0x01
138 };
139 typedef enum kxld_flags KXLDFlags;
140
141 /* Flags for the allocation callback */
142 enum kxld_allocate_flags {
143 kKxldAllocateDefault = 0x0,
144 kKxldAllocateWritable = 0x1, /* kxld may write into the allocated memory */
145 };
146 typedef enum kxld_allocate_flags KXLDAllocateFlags;
147
148 /* This specifies the function type of the callback that the linker uses to get
149 * the base address and allocated memory for relocation and linker output,
150 * respectively. Note that it is compatible with the standard allocators (e.g.
151 * malloc).
152 */
153 typedef kxld_addr_t (*KXLDAllocateCallback)(size_t size,
154 KXLDAllocateFlags *flags, void *user_data);
155
156 /* Flags for the logging callback */
157 typedef enum kxld_log_subsystem {
158 kKxldLogLinking = 0x0,
159 kKxldLogPatching = 0x01
160 } KXLDLogSubsystem;
161
162 typedef enum kxld_log_level {
163 kKxldLogExplicit = 0x0,
164 kKxldLogErr = 0x1,
165 kKxldLogWarn = 0x2,
166 kKxldLogBasic = 0x3,
167 kKxldLogDetail = 0x4,
168 kKxldLogDebug = 0x5
169 } KXLDLogLevel;
170
171 /* This structure is used to describe a dependency kext. The kext field
172 * is a pointer to the binary executable of the dependency. The interface
173 * field is a pointer to an optional interface kext that restricts the
174 * symbols that may be accessed in the dependency kext.
175 *
176 * For example, to use this structure with the KPIs, set the kext field
177 * to point to the kernel's Mach-O binary, and set interface to point
178 * to the KPI's Mach-O binary.
179 */
180 typedef struct kxld_dependency {
181 u_char * kext;
182 u_long kext_size;
183 char * kext_name;
184 u_char * interface;
185 u_long interface_size;
186 char * interface_name;
187 boolean_t is_direct_dependency;
188 } KXLDDependency;
189
190 typedef void (*KXLDLoggingCallback) (KXLDLogSubsystem sys, KXLDLogLevel level,
191 const char *format, va_list ap, void *user_data);
192
193 #endif /* _KXLD_TYPES_H */
194