]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
b0d623f7 | 2 | * Copyright (c) 2006-2008 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
b0d623f7 A |
27 | * |
28 | * | |
1c79356b A |
29 | * Copyright (c) 1999 John D. Polstra |
30 | * All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * | |
41 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
51 | * SUCH DAMAGE. | |
52 | * | |
53 | */ | |
54 | ||
55 | #ifndef _SYS_LINKER_SET_H_ | |
56 | #define _SYS_LINKER_SET_H_ | |
57 | ||
9bccf70c | 58 | #include <sys/appleapiopts.h> |
9bccf70c | 59 | #if !defined(KERNEL) || defined(__APPLE_API_PRIVATE) |
2d21ac55 | 60 | |
1c79356b A |
61 | /* |
62 | * The following macros are used to declare global sets of objects, which | |
2d21ac55 A |
63 | * are collected by the linker into a `linker set' as defined below. |
64 | * For Mach-O, this is done by constructing a separate segment inside the | |
f427ee49 | 65 | * __DATA_CONST section for each set. The contents of this segment are an array |
2d21ac55 | 66 | * of pointers to the objects in the set. |
0a7de745 | 67 | * |
2d21ac55 A |
68 | * Note that due to limitations of the Mach-O format, there cannot |
69 | * be more than 255 sections in a segment, so linker set usage should be | |
70 | * conserved. Set names may not exceed 16 characters. | |
1c79356b A |
71 | */ |
72 | ||
2d21ac55 | 73 | #ifdef KERNEL |
b0d623f7 A |
74 | # include <mach-o/loader.h> |
75 | # include <libkern/kernel_mach_header.h> | |
f427ee49 A |
76 | |
77 | # define MACH_HEADER_TYPE kernel_mach_header_t | |
78 | # define GETSECTIONDATA_VARIANT getsectdatafromheader | |
79 | # define SECTDATA_SIZE_TYPE unsigned long | |
80 | # define MH_EXECUTE_HEADER &_mh_execute_header | |
81 | # define IMAGE_SLIDE_CORRECT 0 | |
2d21ac55 A |
82 | #else |
83 | # include <mach-o/ldsyms.h> | |
84 | # include <mach-o/getsect.h> | |
85 | # include <mach-o/loader.h> | |
f427ee49 A |
86 | # include <mach-o/dyld.h> |
87 | # include <crt_externs.h> | |
88 | ||
89 | # if __LP64__ | |
90 | # define MACH_HEADER_TYPE struct mach_header_64 | |
91 | # define GETSECTIONDATA_VARIANT getsectdatafromheader_64 | |
92 | # define SECTDATA_SIZE_TYPE uint64_t | |
93 | # define MH_EXECUTE_HEADER _NSGetMachExecuteHeader() | |
94 | # else | |
95 | # define MACH_HEADER_TYPE struct mach_header | |
96 | # define GETSECTIONDATA_VARIANT getsectdatafromheader | |
97 | # define SECTDATA_SIZE_TYPE uint32_t | |
98 | # define MH_EXECUTE_HEADER _NSGetMachExecuteHeader() | |
99 | # endif | |
2d21ac55 | 100 | #endif |
1c79356b | 101 | |
f427ee49 A |
102 | #if __LP64__ |
103 | # define LINKER_SET_ENTRY_PACKED | |
104 | # define LINKER_SET_SEGMENT __DATA_CONST | |
105 | # define LINKER_SET_SEGMENT_CSTR "__DATA_CONST" | |
106 | #else | |
107 | # define LINKER_SET_ENTRY_PACKED __attribute__((packed)) | |
108 | # define LINKER_SET_SEGMENT __DATA | |
109 | # define LINKER_SET_SEGMENT_CSTR "__DATA" | |
110 | #endif | |
1c79356b | 111 | |
2d21ac55 A |
112 | /* |
113 | * Private macros, not to be used outside this header file. | |
114 | * | |
115 | * The objective of this macro stack is to produce the following output, | |
116 | * given SET and SYM as arguments: | |
117 | * | |
f427ee49 | 118 | * void const * __set_SET_sym_SYM __attribute__((section("__DATA_CONST,SET"))) = & SYM |
2d21ac55 | 119 | */ |
5ba3f43e A |
120 | |
121 | /* Wrap entries in a type that can be blacklisted from KASAN */ | |
122 | struct linker_set_entry { | |
123 | void *ptr; | |
f427ee49 | 124 | } LINKER_SET_ENTRY_PACKED; |
5ba3f43e | 125 | |
2d21ac55 A |
126 | #ifdef __LS_VA_STRINGIFY__ |
127 | # undef __LS_VA_STRINGIFY__ | |
128 | #endif | |
129 | #ifdef __LS_VA_STRCONCAT__ | |
130 | # undef __LS_VA_STRCONCAT__ | |
131 | #endif | |
0a7de745 A |
132 | #define __LS_VA_STRINGIFY(_x ...) #_x |
133 | #define __LS_VA_STRCONCAT(_x, _y) __LS_VA_STRINGIFY(_x,_y) | |
134 | #define __LINKER_MAKE_SET(_set, _sym) \ | |
135 | /*__unused*/ /*static*/ const struct linker_set_entry /*const*/ __set_##_set##_sym_##_sym \ | |
f427ee49 | 136 | __attribute__ ((section(__LS_VA_STRCONCAT(LINKER_SET_SEGMENT,_set)),used)) = { (void *)&_sym } |
2d21ac55 | 137 | /* the line above is very fragile - if your compiler breaks linker sets, |
0a7de745 | 138 | * just play around with "static", "const", "used" etc. :-) */ |
1c79356b | 139 | |
2d21ac55 A |
140 | /* |
141 | * Public macros. | |
142 | */ | |
0a7de745 | 143 | #define LINKER_SET_ENTRY(_set, _sym) __LINKER_MAKE_SET(_set, _sym) |
1c79356b | 144 | |
2d21ac55 A |
145 | /* |
146 | * FreeBSD compatibility. | |
147 | */ | |
148 | #ifdef __APPLE_API_OBSOLETE | |
0a7de745 A |
149 | # define TEXT_SET(_set, _sym) __LINKER_MAKE_SET(_set, _sym) |
150 | # define DATA_SET(_set, _sym) __LINKER_MAKE_SET(_set, _sym) | |
151 | # define BSS_SET(_set, _sym) __LINKER_MAKE_SET(_set, _sym) | |
152 | # define ABS_SET(_set, _sym) __LINKER_MAKE_SET(_set, _sym) | |
153 | # define SET_ENTRY(_set, _sym) __LINKER_MAKE_SET(_set, _sym) | |
2d21ac55 | 154 | #endif /* __APPLE_API_OBSOLETE */ |
1c79356b | 155 | |
2d21ac55 A |
156 | /* |
157 | * Extended linker set API. | |
158 | * | |
159 | * Since linker sets are per-object-file, and we may have multiple | |
160 | * object files, we need to be able to specify which object's set | |
161 | * to scan. | |
162 | * | |
163 | * The set itself is a contiguous array of pointers to the objects | |
164 | * within the set. | |
165 | */ | |
1c79356b A |
166 | |
167 | /* | |
2d21ac55 A |
168 | * Public interface. |
169 | * | |
170 | * void **LINKER_SET_OBJECT_BEGIN(_object, _set) | |
171 | * Preferred interface to linker_set_object_begin(), takes set name unquoted. | |
172 | * void **LINKER_SET_OBJECT_LIMIT(_object, _set) | |
173 | * Preferred interface to linker_set_object_begin(), takes set name unquoted. | |
b0d623f7 | 174 | * LINKER_SET_OBJECT_FOREACH(_object, (set_member_type **)_pvar, _cast, _set) |
2d21ac55 A |
175 | * Iterates over the members of _set within _object. Since the set contains |
176 | * pointers to its elements, for a set of elements of type etyp, _pvar must | |
177 | * be (etyp **). | |
b0d623f7 | 178 | * LINKER_SET_FOREACH((set_member_type **)_pvar, _cast, _set) |
d9a64523 | 179 | * |
b0d623f7 | 180 | * Example of _cast: For the _pvar "struct sysctl_oid **oidpp", _cast would be |
0a7de745 | 181 | * "struct sysctl_oid **" |
b0d623f7 | 182 | * |
1c79356b | 183 | */ |
1c79356b | 184 | |
0a7de745 A |
185 | #define LINKER_SET_OBJECT_BEGIN(_object, _set) __linker_set_object_begin(_object, _set) |
186 | #define LINKER_SET_OBJECT_LIMIT(_object, _set) __linker_set_object_limit(_object, _set) | |
1c79356b | 187 | |
0a7de745 A |
188 | #define LINKER_SET_OBJECT_FOREACH(_object, _pvar, _cast, _set) \ |
189 | for (_pvar = (_cast) LINKER_SET_OBJECT_BEGIN(_object, _set); \ | |
190 | _pvar < (_cast) LINKER_SET_OBJECT_LIMIT(_object, _set); \ | |
b0d623f7 | 191 | _pvar++) |
2d21ac55 | 192 | |
0a7de745 | 193 | #define LINKER_SET_OBJECT_ITEM(_object, _cast, _set, _i) \ |
d9a64523 | 194 | (((_cast)(LINKER_SET_OBJECT_BEGIN(_object, _set)))[_i]) |
2d21ac55 | 195 | |
0a7de745 | 196 | #define LINKER_SET_FOREACH(_pvar, _cast, _set) \ |
f427ee49 | 197 | LINKER_SET_OBJECT_FOREACH((MACH_HEADER_TYPE *)MH_EXECUTE_HEADER, _pvar, _cast, _set) |
2d21ac55 A |
198 | |
199 | /* | |
200 | * Implementation. | |
201 | * | |
202 | * void **__linker_set_object_begin(_header, _set) | |
203 | * Returns a pointer to the first pointer in the linker set. | |
204 | * void **__linker_set_object_limi(_header, _set) | |
205 | * Returns an upper bound to the linker set (base + size). | |
206 | */ | |
207 | ||
f427ee49 A |
208 | static __inline intptr_t |
209 | __linker_get_slide(struct mach_header *_header) | |
210 | { | |
211 | #ifndef KERNEL | |
212 | /* | |
213 | * Gross. | |
214 | * | |
215 | * We cannot get the image slide directly from the header, so we need to | |
216 | * determine the image's index and ask for the slide of that index. | |
217 | */ | |
218 | uint32_t i = 0; | |
219 | for (i = 0; i < _dyld_image_count(); i++) { | |
220 | const struct mach_header *hdr = _dyld_get_image_header(i); | |
221 | if (_header == hdr) { | |
222 | return _dyld_get_image_vmaddr_slide(i); | |
223 | } | |
224 | } | |
225 | return 0; | |
226 | #else | |
227 | (void)_header; | |
228 | return 0; | |
229 | #endif | |
230 | } | |
231 | ||
2d21ac55 | 232 | static __inline void ** |
f427ee49 | 233 | __linker_set_object_begin(MACH_HEADER_TYPE *_header, const char *_set) |
0a7de745 | 234 | __attribute__((__const__)); |
2d21ac55 | 235 | static __inline void ** |
f427ee49 | 236 | __linker_set_object_begin(MACH_HEADER_TYPE *_header, const char *_set) |
2d21ac55 | 237 | { |
f427ee49 A |
238 | char *_set_begin; |
239 | SECTDATA_SIZE_TYPE _size; | |
2d21ac55 | 240 | |
f427ee49 A |
241 | _set_begin = (char *)GETSECTIONDATA_VARIANT(_header, LINKER_SET_SEGMENT_CSTR, _set, &_size); |
242 | _set_begin += __linker_get_slide((struct mach_header *)_header); | |
243 | return (void **)(uintptr_t)_set_begin; | |
2d21ac55 A |
244 | } |
245 | ||
246 | static __inline void ** | |
f427ee49 | 247 | __linker_set_object_limit(MACH_HEADER_TYPE *_header, const char *_set) |
0a7de745 | 248 | __attribute__((__const__)); |
2d21ac55 | 249 | static __inline void ** |
f427ee49 | 250 | __linker_set_object_limit(MACH_HEADER_TYPE *_header, const char *_set) |
2d21ac55 | 251 | { |
f427ee49 A |
252 | char *_set_begin; |
253 | SECTDATA_SIZE_TYPE _size; | |
2d21ac55 | 254 | |
f427ee49 A |
255 | _set_begin = (char *)GETSECTIONDATA_VARIANT(_header, LINKER_SET_SEGMENT_CSTR, _set, &_size); |
256 | _set_begin += __linker_get_slide((struct mach_header *)_header); | |
0a7de745 A |
257 | |
258 | return (void **) ((uintptr_t) _set_begin + _size); | |
2d21ac55 | 259 | } |
1c79356b | 260 | |
9bccf70c | 261 | #endif /* !KERNEL || __APPLE_API_PRIVATE */ |
1c79356b A |
262 | |
263 | #endif /* _SYS_LINKER_SET_H_ */ |