1 | /*- |
---|
2 | * Copyright (c) 1999 John D. Polstra |
---|
3 | * Copyright (c) 1999,2001 Peter Wemm <peter@FreeBSD.org> |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | * SUCH DAMAGE. |
---|
26 | * |
---|
27 | * $FreeBSD$ |
---|
28 | */ |
---|
29 | |
---|
30 | #ifndef _SYS_LINKER_SET_H_ |
---|
31 | #define _SYS_LINKER_SET_H_ |
---|
32 | |
---|
33 | #ifndef _SYS_CDEFS_H_ |
---|
34 | #error this file needs sys/cdefs.h as a prerequisite |
---|
35 | #endif |
---|
36 | #ifdef __rtems__ |
---|
37 | #include <sys/_types.h> |
---|
38 | #endif /* __rtems__ */ |
---|
39 | |
---|
40 | /* |
---|
41 | * The following macros are used to declare global sets of objects, which |
---|
42 | * are collected by the linker into a `linker_set' as defined below. |
---|
43 | * For ELF, this is done by constructing a separate segment for each set. |
---|
44 | */ |
---|
45 | |
---|
46 | #if defined(__powerpc64__) |
---|
47 | /* |
---|
48 | * Move the symbol pointer from ".text" to ".data" segment, to make |
---|
49 | * the GCC compiler happy: |
---|
50 | */ |
---|
51 | #define __MAKE_SET_CONST |
---|
52 | #else |
---|
53 | #define __MAKE_SET_CONST const |
---|
54 | #endif |
---|
55 | |
---|
56 | /* |
---|
57 | * Private macros, not to be used outside this header file. |
---|
58 | */ |
---|
59 | #ifdef __GNUCLIKE___SECTION |
---|
60 | #ifndef __rtems__ |
---|
61 | #define __MAKE_SET(set, sym) \ |
---|
62 | __GLOBL(__CONCAT(__start_set_,set)); \ |
---|
63 | __GLOBL(__CONCAT(__stop_set_,set)); \ |
---|
64 | static void const * __MAKE_SET_CONST \ |
---|
65 | __set_##set##_sym_##sym __section("set_" #set) \ |
---|
66 | __used = &(sym) |
---|
67 | #else /* __rtems__ */ |
---|
68 | #define RTEMS_BSD_DEFINE_SET(set, type) \ |
---|
69 | type const __CONCAT(_bsd__start_set_,set)[0] \ |
---|
70 | __section(".rtemsroset.bsd." __STRING(set) ".begin") __used; \ |
---|
71 | type const __CONCAT(_bsd__stop_set_,set)[0] \ |
---|
72 | __section(".rtemsroset.bsd." __STRING(set) ".end") __used |
---|
73 | |
---|
74 | #define RTEMS_BSD_DECLARE_SET(set, type) \ |
---|
75 | extern type const __CONCAT(_bsd__start_set_,set)[0]; \ |
---|
76 | extern type const __CONCAT(_bsd__stop_set_,set)[0] |
---|
77 | |
---|
78 | #define RTEMS_BSD_DEFINE_SET_ITEM(set, sym, type) \ |
---|
79 | static type const __set_##set##_sym_##sym \ |
---|
80 | __section(".rtemsroset.bsd." __STRING(set) ".content") __used |
---|
81 | |
---|
82 | #define __MAKE_SET(set, sym) \ |
---|
83 | RTEMS_BSD_DEFINE_SET_ITEM(set, sym, const void *) = &sym |
---|
84 | |
---|
85 | #define RTEMS_BSD_DEFINE_RWSET(set, type) \ |
---|
86 | type __CONCAT(_bsd__start_set_,set)[0] \ |
---|
87 | __section(".rtemsrwset.bsd." __STRING(set) ".begin") __used; \ |
---|
88 | type __CONCAT(_bsd__stop_set_,set)[0] \ |
---|
89 | __section(".rtemsrwset.bsd." __STRING(set) ".end") __used |
---|
90 | |
---|
91 | #define RTEMS_BSD_DECLARE_RWSET(set, type) \ |
---|
92 | extern type __CONCAT(_bsd__start_set_,set)[0]; \ |
---|
93 | extern type __CONCAT(_bsd__stop_set_,set)[0] |
---|
94 | |
---|
95 | #define RTEMS_BSD_DEFINE_RWSET_ITEM(set, sym, type) \ |
---|
96 | static type __set_##set##_sym_##sym \ |
---|
97 | __section(".rtemsrwset.bsd." __STRING(set) ".content") __used |
---|
98 | |
---|
99 | #define __MAKE_RWSET(set, sym) \ |
---|
100 | RTEMS_BSD_DEFINE_RWSET_ITEM(set, sym, const void *) = &sym |
---|
101 | #endif /* __rtems__ */ |
---|
102 | #else /* !__GNUCLIKE___SECTION */ |
---|
103 | #ifndef lint |
---|
104 | #error this file needs to be ported to your compiler |
---|
105 | #endif /* lint */ |
---|
106 | #define __MAKE_SET(set, sym) extern void const * const (__set_##set##_sym_##sym) |
---|
107 | #endif /* __GNUCLIKE___SECTION */ |
---|
108 | |
---|
109 | /* |
---|
110 | * Public macros. |
---|
111 | */ |
---|
112 | #define TEXT_SET(set, sym) __MAKE_SET(set, sym) |
---|
113 | #define DATA_SET(set, sym) __MAKE_SET(set, sym) |
---|
114 | #define BSS_SET(set, sym) __MAKE_SET(set, sym) |
---|
115 | #define ABS_SET(set, sym) __MAKE_SET(set, sym) |
---|
116 | #define SET_ENTRY(set, sym) __MAKE_SET(set, sym) |
---|
117 | #ifdef __rtems__ |
---|
118 | #define RWDATA_SET(set, sym) __MAKE_RWSET(set, sym) |
---|
119 | #endif /* __rtems__ */ |
---|
120 | |
---|
121 | /* |
---|
122 | * Initialize before referring to a given linker set. |
---|
123 | */ |
---|
124 | #ifndef __rtems__ |
---|
125 | #define SET_DECLARE(set, ptype) \ |
---|
126 | extern ptype __weak_symbol *__CONCAT(__start_set_,set); \ |
---|
127 | extern ptype __weak_symbol *__CONCAT(__stop_set_,set) |
---|
128 | |
---|
129 | #define SET_BEGIN(set) \ |
---|
130 | (&__CONCAT(__start_set_,set)) |
---|
131 | #define SET_LIMIT(set) \ |
---|
132 | (&__CONCAT(__stop_set_,set)) |
---|
133 | #else /* __rtems__ */ |
---|
134 | #define SET_DECLARE(set, ptype) \ |
---|
135 | RTEMS_BSD_DECLARE_SET(set, ptype *) |
---|
136 | |
---|
137 | #define RWSET_DECLARE(set, ptype) \ |
---|
138 | RTEMS_BSD_DECLARE_RWSET(set, ptype *) |
---|
139 | |
---|
140 | static __inline __uintptr_t |
---|
141 | _linker_set_obfuscate(const void *marker) |
---|
142 | { |
---|
143 | |
---|
144 | /* Obfuscate the variable, so that the compiler cannot optimize */ |
---|
145 | __asm__("" : "+r" (marker)); |
---|
146 | return ((__uintptr_t)marker); |
---|
147 | } |
---|
148 | |
---|
149 | #define SET_BEGIN(set) \ |
---|
150 | ((__typeof(&__CONCAT(_bsd__start_set_,set)[0])) \ |
---|
151 | _linker_set_obfuscate(__CONCAT(_bsd__start_set_,set))) |
---|
152 | #define SET_LIMIT(set) \ |
---|
153 | ((__typeof(&__CONCAT(_bsd__stop_set_,set)[0])) \ |
---|
154 | _linker_set_obfuscate(__CONCAT(_bsd__stop_set_,set))) |
---|
155 | #endif /* __rtems__ */ |
---|
156 | |
---|
157 | /* |
---|
158 | * Iterate over all the elements of a set. |
---|
159 | * |
---|
160 | * Sets always contain addresses of things, and "pvar" points to words |
---|
161 | * containing those addresses. Thus is must be declared as "type **pvar", |
---|
162 | * and the address of each set item is obtained inside the loop by "*pvar". |
---|
163 | */ |
---|
164 | #define SET_FOREACH(pvar, set) \ |
---|
165 | for (pvar = SET_BEGIN(set); pvar < SET_LIMIT(set); pvar++) |
---|
166 | |
---|
167 | #define SET_ITEM(set, i) \ |
---|
168 | ((SET_BEGIN(set))[i]) |
---|
169 | |
---|
170 | /* |
---|
171 | * Provide a count of the items in a set. |
---|
172 | */ |
---|
173 | #define SET_COUNT(set) \ |
---|
174 | (SET_LIMIT(set) - SET_BEGIN(set)) |
---|
175 | |
---|
176 | #endif /* _SYS_LINKER_SET_H_ */ |
---|