]> git.saurik.com Git - apple/libc.git/blob - os/linker_set.h
2bf02a28aaa0d8e7e8524e27d2d856ab8a29173c
[apple/libc.git] / os / linker_set.h
1 /*
2 * Copyright (c) 2006-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 *
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
58 #include <sys/appleapiopts.h>
59 #if !defined(KERNEL) || defined(__APPLE_API_PRIVATE)
60
61 /*
62 * The following macros are used to declare global sets of objects, which
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.
71 */
72
73 #ifdef KERNEL
74 # include <mach-o/loader.h>
75 # include <libkern/kernel_mach_header.h>
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
82 #else
83 # include <mach-o/ldsyms.h>
84 # include <mach-o/getsect.h>
85 # include <mach-o/loader.h>
86 # include <mach-o/dyld.h>
87
88 # if __LP64__
89 # define MACH_HEADER_TYPE struct mach_header_64
90 # define GETSECTIONDATA_VARIANT getsectdatafromheader_64
91 # define SECTDATA_SIZE_TYPE uint64_t
92 # define MH_EXECUTE_HEADER _NSGetMachExecuteHeader()
93 # else
94 # define MACH_HEADER_TYPE struct mach_header
95 # define GETSECTIONDATA_VARIANT getsectdatafromheader
96 # define SECTDATA_SIZE_TYPE uint32_t
97 # define MH_EXECUTE_HEADER _NSGetMachExecuteHeader()
98 # endif
99 #endif
100
101
102 /*
103 * Private macros, not to be used outside this header file.
104 *
105 * The objective of this macro stack is to produce the following output,
106 * given SET and SYM as arguments:
107 *
108 * void const * __set_SET_sym_SYM __attribute__((section("__DATA,SET"))) = & SYM
109 */
110
111 /* Wrap entries in a type that can be blacklisted from KASAN */
112 struct linker_set_entry {
113 void *ptr;
114 } __attribute__((packed));
115
116 #ifdef __LS_VA_STRINGIFY__
117 # undef __LS_VA_STRINGIFY__
118 #endif
119 #ifdef __LS_VA_STRCONCAT__
120 # undef __LS_VA_STRCONCAT__
121 #endif
122 #define __LS_VA_STRINGIFY(_x ...) #_x
123 #define __LS_VA_STRCONCAT(_x, _y) __LS_VA_STRINGIFY(_x,_y)
124 #define __LINKER_MAKE_SET(_set, _sym) \
125 /*__unused*/ /*static*/ const struct linker_set_entry /*const*/ __set_##_set##_sym_##_sym \
126 __attribute__ ((section(__LS_VA_STRCONCAT(__DATA,_set)),used)) = { (void *)&_sym }
127 /* the line above is very fragile - if your compiler breaks linker sets,
128 * just play around with "static", "const", "used" etc. :-) */
129
130 /*
131 * Public macros.
132 */
133 #define LINKER_SET_ENTRY(_set, _sym) __LINKER_MAKE_SET(_set, _sym)
134
135 /*
136 * FreeBSD compatibility.
137 */
138 #ifdef __APPLE_API_OBSOLETE
139 # define TEXT_SET(_set, _sym) __LINKER_MAKE_SET(_set, _sym)
140 # define DATA_SET(_set, _sym) __LINKER_MAKE_SET(_set, _sym)
141 # define BSS_SET(_set, _sym) __LINKER_MAKE_SET(_set, _sym)
142 # define ABS_SET(_set, _sym) __LINKER_MAKE_SET(_set, _sym)
143 # define SET_ENTRY(_set, _sym) __LINKER_MAKE_SET(_set, _sym)
144 #endif /* __APPLE_API_OBSOLETE */
145
146 /*
147 * Extended linker set API.
148 *
149 * Since linker sets are per-object-file, and we may have multiple
150 * object files, we need to be able to specify which object's set
151 * to scan.
152 *
153 * The set itself is a contiguous array of pointers to the objects
154 * within the set.
155 */
156
157 /*
158 * Public interface.
159 *
160 * void **LINKER_SET_OBJECT_BEGIN(_object, _set)
161 * Preferred interface to linker_set_object_begin(), takes set name unquoted.
162 * void **LINKER_SET_OBJECT_LIMIT(_object, _set)
163 * Preferred interface to linker_set_object_begin(), takes set name unquoted.
164 * LINKER_SET_OBJECT_FOREACH(_object, (set_member_type **)_pvar, _cast, _set)
165 * Iterates over the members of _set within _object. Since the set contains
166 * pointers to its elements, for a set of elements of type etyp, _pvar must
167 * be (etyp **).
168 * LINKER_SET_FOREACH((set_member_type **)_pvar, _cast, _set)
169 *
170 * Example of _cast: For the _pvar "struct sysctl_oid **oidpp", _cast would be
171 * "struct sysctl_oid **"
172 *
173 */
174
175 #define LINKER_SET_OBJECT_BEGIN(_object, _set) __linker_set_object_begin(_object, _set)
176 #define LINKER_SET_OBJECT_LIMIT(_object, _set) __linker_set_object_limit(_object, _set)
177
178 #define LINKER_SET_OBJECT_FOREACH(_object, _pvar, _cast, _set) \
179 for (_pvar = (_cast) LINKER_SET_OBJECT_BEGIN(_object, _set); \
180 _pvar < (_cast) LINKER_SET_OBJECT_LIMIT(_object, _set); \
181 _pvar++)
182
183 #define LINKER_SET_OBJECT_ITEM(_object, _cast, _set, _i) \
184 (((_cast)(LINKER_SET_OBJECT_BEGIN(_object, _set)))[_i])
185
186 #define LINKER_SET_FOREACH(_pvar, _cast, _set) \
187 LINKER_SET_OBJECT_FOREACH((MACH_HEADER_TYPE *)MH_EXECUTE_HEADER, _pvar, _cast, _set)
188
189 /*
190 * Implementation.
191 *
192 * void **__linker_set_object_begin(_header, _set)
193 * Returns a pointer to the first pointer in the linker set.
194 * void **__linker_set_object_limi(_header, _set)
195 * Returns an upper bound to the linker set (base + size).
196 */
197
198 static __inline intptr_t
199 __linker_get_slide(struct mach_header *_header)
200 {
201 #ifndef KERNEL
202 /*
203 * Gross.
204 *
205 * We cannot get the image slide directly from the header, so we need to
206 * determine the image's index and ask for the slide of that index.
207 */
208 uint32_t i = 0;
209 for (i = 0; i < _dyld_image_count(); i++) {
210 const struct mach_header *hdr = _dyld_get_image_header(i);
211 if (_header == hdr) {
212 return _dyld_get_image_vmaddr_slide(i);
213 }
214 }
215 return 0;
216 #else
217 return 0;
218 #endif
219 }
220
221 static __inline void **
222 __linker_set_object_begin(MACH_HEADER_TYPE *_header, const char *_set)
223 __attribute__((__const__));
224 static __inline void **
225 __linker_set_object_begin(MACH_HEADER_TYPE *_header, const char *_set)
226 {
227 void *_set_begin;
228 SECTDATA_SIZE_TYPE _size;
229
230 _set_begin = GETSECTIONDATA_VARIANT(_header, "__DATA", _set, &_size);
231 _set_begin += __linker_get_slide((struct mach_header *)_header);
232 return (void **) _set_begin;
233 }
234
235 static __inline void **
236 __linker_set_object_limit(MACH_HEADER_TYPE *_header, const char *_set)
237 __attribute__((__const__));
238 static __inline void **
239 __linker_set_object_limit(MACH_HEADER_TYPE *_header, const char *_set)
240 {
241 void *_set_begin;
242 SECTDATA_SIZE_TYPE _size;
243
244 _set_begin = GETSECTIONDATA_VARIANT(_header, "__DATA", _set, &_size);
245 _set_begin += __linker_get_slide((struct mach_header *)_header);
246
247 return (void **) ((uintptr_t) _set_begin + _size);
248 }
249
250 #endif /* !KERNEL || __APPLE_API_PRIVATE */
251
252 #endif /* _SYS_LINKER_SET_H_ */