]>
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@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
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 | |
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. | |
8f6c56a5 | 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 | |
65 | * __DATA section for each set. The contents of this segment are an array | |
66 | * of pointers to the objects in the set. | |
67 | * | |
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> | |
2d21ac55 A |
76 | #else |
77 | # include <mach-o/ldsyms.h> | |
78 | # include <mach-o/getsect.h> | |
79 | # include <mach-o/loader.h> | |
2d21ac55 | 80 | #endif |
1c79356b | 81 | |
1c79356b | 82 | |
2d21ac55 A |
83 | /* |
84 | * Private macros, not to be used outside this header file. | |
85 | * | |
86 | * The objective of this macro stack is to produce the following output, | |
87 | * given SET and SYM as arguments: | |
88 | * | |
89 | * void const * __set_SET_sym_SYM __attribute__((section("__DATA,SET"))) = & SYM | |
90 | */ | |
5ba3f43e A |
91 | |
92 | /* Wrap entries in a type that can be blacklisted from KASAN */ | |
93 | struct linker_set_entry { | |
94 | void *ptr; | |
95 | } __attribute__((packed)); | |
96 | ||
2d21ac55 A |
97 | #ifdef __LS_VA_STRINGIFY__ |
98 | # undef __LS_VA_STRINGIFY__ | |
99 | #endif | |
100 | #ifdef __LS_VA_STRCONCAT__ | |
101 | # undef __LS_VA_STRCONCAT__ | |
102 | #endif | |
103 | #define __LS_VA_STRINGIFY(_x...) #_x | |
104 | #define __LS_VA_STRCONCAT(_x,_y) __LS_VA_STRINGIFY(_x,_y) | |
105 | #define __LINKER_MAKE_SET(_set, _sym) \ | |
5ba3f43e A |
106 | /*__unused*/ /*static*/ const struct linker_set_entry /*const*/ __set_##_set##_sym_##_sym \ |
107 | __attribute__ ((section(__LS_VA_STRCONCAT(__DATA,_set)),used)) = { (void *)&_sym } | |
2d21ac55 | 108 | /* the line above is very fragile - if your compiler breaks linker sets, |
39236c6e | 109 | just play around with "static", "const", "used" etc. :-) */ |
1c79356b | 110 | |
2d21ac55 A |
111 | /* |
112 | * Public macros. | |
113 | */ | |
114 | #define LINKER_SET_ENTRY(_set, _sym) __LINKER_MAKE_SET(_set, _sym) | |
1c79356b | 115 | |
2d21ac55 A |
116 | /* |
117 | * FreeBSD compatibility. | |
118 | */ | |
119 | #ifdef __APPLE_API_OBSOLETE | |
120 | # define TEXT_SET(_set, _sym) __LINKER_MAKE_SET(_set, _sym) | |
121 | # define DATA_SET(_set, _sym) __LINKER_MAKE_SET(_set, _sym) | |
122 | # define BSS_SET(_set, _sym) __LINKER_MAKE_SET(_set, _sym) | |
123 | # define ABS_SET(_set, _sym) __LINKER_MAKE_SET(_set, _sym) | |
124 | # define SET_ENTRY(_set, _sym) __LINKER_MAKE_SET(_set, _sym) | |
125 | #endif /* __APPLE_API_OBSOLETE */ | |
1c79356b | 126 | |
2d21ac55 A |
127 | /* |
128 | * Extended linker set API. | |
129 | * | |
130 | * Since linker sets are per-object-file, and we may have multiple | |
131 | * object files, we need to be able to specify which object's set | |
132 | * to scan. | |
133 | * | |
134 | * The set itself is a contiguous array of pointers to the objects | |
135 | * within the set. | |
136 | */ | |
1c79356b A |
137 | |
138 | /* | |
2d21ac55 A |
139 | * Public interface. |
140 | * | |
141 | * void **LINKER_SET_OBJECT_BEGIN(_object, _set) | |
142 | * Preferred interface to linker_set_object_begin(), takes set name unquoted. | |
143 | * void **LINKER_SET_OBJECT_LIMIT(_object, _set) | |
144 | * Preferred interface to linker_set_object_begin(), takes set name unquoted. | |
b0d623f7 | 145 | * LINKER_SET_OBJECT_FOREACH(_object, (set_member_type **)_pvar, _cast, _set) |
2d21ac55 A |
146 | * Iterates over the members of _set within _object. Since the set contains |
147 | * pointers to its elements, for a set of elements of type etyp, _pvar must | |
148 | * be (etyp **). | |
149 | * set_member_type **LINKER_SET_OBJECT_ITEM(_object, _set, _i) | |
150 | * Returns a pointer to the _i'th element of _set within _object. | |
151 | * | |
152 | * void **LINKER_SET_BEGIN(_set) | |
153 | * void **LINKER_SET_LIMINT(_set) | |
b0d623f7 | 154 | * LINKER_SET_FOREACH((set_member_type **)_pvar, _cast, _set) |
2d21ac55 A |
155 | * set_member_type **LINKER_SET_ITEM(_set, _i) |
156 | * These versions implicitly reference the kernel/application object. | |
b0d623f7 A |
157 | * |
158 | * Example of _cast: For the _pvar "struct sysctl_oid **oidpp", _cast would be | |
159 | * "struct sysctl_oid **" | |
160 | * | |
1c79356b | 161 | */ |
1c79356b | 162 | |
2d21ac55 A |
163 | #define LINKER_SET_OBJECT_BEGIN(_object, _set) __linker_set_object_begin(_object, _set) |
164 | #define LINKER_SET_OBJECT_LIMIT(_object, _set) __linker_set_object_limit(_object, _set) | |
1c79356b | 165 | |
b0d623f7 A |
166 | #define LINKER_SET_OBJECT_FOREACH(_object, _pvar, _cast, _set) \ |
167 | for (_pvar = (_cast) LINKER_SET_OBJECT_BEGIN(_object, _set); \ | |
168 | _pvar < (_cast) LINKER_SET_OBJECT_LIMIT(_object, _set); \ | |
169 | _pvar++) | |
2d21ac55 A |
170 | |
171 | #define LINKER_SET_OBJECT_ITEM(_object, _set, _i) \ | |
172 | ((LINKER_SET_OBJECT_BEGIN(_object, _set))[_i]) | |
173 | ||
174 | #define LINKER_SET_BEGIN(_set) \ | |
b0d623f7 | 175 | LINKER_SET_OBJECT_BEGIN((kernel_mach_header_t *)&_mh_execute_header, _set) |
2d21ac55 | 176 | #define LINKER_SET_LIMIT(_set) \ |
b0d623f7 A |
177 | LINKER_SET_OBJECT_LIMIT((kernel_mach_header_t *)&_mh_execute_header, _set) |
178 | #define LINKER_SET_FOREACH(_pvar, _cast, _set) \ | |
179 | LINKER_SET_OBJECT_FOREACH((kernel_mach_header_t *)&_mh_execute_header, _pvar, _cast, _set) | |
2d21ac55 | 180 | #define LINKER_SET_ITEM(_set, _i) \ |
b0d623f7 | 181 | LINKER_SET_OBJECT_ITEM((kernel_mach_header_t *)&_mh_execute_header, _set, _i) |
2d21ac55 A |
182 | |
183 | /* | |
184 | * Implementation. | |
185 | * | |
186 | * void **__linker_set_object_begin(_header, _set) | |
187 | * Returns a pointer to the first pointer in the linker set. | |
188 | * void **__linker_set_object_limi(_header, _set) | |
189 | * Returns an upper bound to the linker set (base + size). | |
190 | */ | |
191 | ||
192 | static __inline void ** | |
b0d623f7 | 193 | __linker_set_object_begin(kernel_mach_header_t *_header, const char *_set) |
2d21ac55 A |
194 | __attribute__((__const__)); |
195 | static __inline void ** | |
b0d623f7 | 196 | __linker_set_object_begin(kernel_mach_header_t *_header, const char *_set) |
2d21ac55 A |
197 | { |
198 | void *_set_begin; | |
b0d623f7 | 199 | unsigned long _size; |
2d21ac55 A |
200 | |
201 | _set_begin = getsectdatafromheader(_header, "__DATA", _set, &_size); | |
b0d623f7 | 202 | return( (void **) _set_begin ); |
2d21ac55 A |
203 | } |
204 | ||
205 | static __inline void ** | |
b0d623f7 | 206 | __linker_set_object_limit(kernel_mach_header_t *_header, const char *_set) |
2d21ac55 A |
207 | __attribute__((__const__)); |
208 | static __inline void ** | |
b0d623f7 | 209 | __linker_set_object_limit(kernel_mach_header_t *_header, const char *_set) |
2d21ac55 A |
210 | { |
211 | void *_set_begin; | |
b0d623f7 | 212 | unsigned long _size; |
2d21ac55 A |
213 | |
214 | _set_begin = getsectdatafromheader(_header, "__DATA", _set, &_size); | |
b0d623f7 A |
215 | |
216 | return ((void **) ((uintptr_t) _set_begin + _size)); | |
2d21ac55 | 217 | } |
1c79356b | 218 | |
9bccf70c | 219 | #endif /* !KERNEL || __APPLE_API_PRIVATE */ |
1c79356b A |
220 | |
221 | #endif /* _SYS_LINKER_SET_H_ */ | |
222 | ||
2d21ac55 | 223 |