1/* $NetBSD: cdefs_elf.h,v 1.53 2017/08/10 19:03:27 joerg Exp $ */
2
3/*
4 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Author: Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30#ifndef _SYS_CDEFS_ELF_H_
31#define _SYS_CDEFS_ELF_H_
32
33#ifdef __LEADING_UNDERSCORE
34#define _C_LABEL(x) __CONCAT(_,x)
35#define _C_LABEL_STRING(x) "_"x
36#else
37#define _C_LABEL(x) x
38#define _C_LABEL_STRING(x) x
39#endif
40
41#if __STDC__
42#define ___RENAME(x) __asm(___STRING(_C_LABEL(x)))
43#else
44#ifdef __LEADING_UNDERSCORE
45#define ___RENAME(x) ____RENAME(_/**/x)
46#define ____RENAME(x) __asm(___STRING(x))
47#else
48#define ___RENAME(x) __asm(___STRING(x))
49#endif
50#endif
51
52#define __indr_reference(sym,alias) /* nada, since we do weak refs */
53
54#if __STDC__
55#define __strong_alias(alias,sym) \
56 __asm(".global " _C_LABEL_STRING(#alias) "\n" \
57 _C_LABEL_STRING(#alias) " = " _C_LABEL_STRING(#sym));
58
59#define __weak_alias(alias,sym) \
60 __asm(".weak " _C_LABEL_STRING(#alias) "\n" \
61 _C_LABEL_STRING(#alias) " = " _C_LABEL_STRING(#sym));
62
63/* Do not use __weak_extern, use __weak_reference instead */
64#define __weak_extern(sym) \
65 __asm(".weak " _C_LABEL_STRING(#sym));
66
67#if __GNUC_PREREQ__(4, 0)
68#define __weak __attribute__((__weak__))
69#else
70#define __weak
71#endif
72
73#if __GNUC_PREREQ__(4, 0)
74#define __weak_reference(sym) __attribute__((__weakref__(#sym)))
75#else
76#define __weak_reference(sym) ; __asm(".weak " _C_LABEL_STRING(#sym))
77#endif
78
79#if __GNUC_PREREQ__(4, 2)
80#define __weakref_visible static
81#else
82#define __weakref_visible extern
83#endif
84
85#define __warn_references(sym,msg) \
86 __asm(".pushsection .gnu.warning." #sym "\n" \
87 ".ascii \"" msg "\"\n" \
88 ".popsection");
89
90#else /* !__STDC__ */
91
92#ifdef __LEADING_UNDERSCORE
93#define __weak_alias(alias,sym) ___weak_alias(_/**/alias,_/**/sym)
94#define ___weak_alias(alias,sym) \
95 __asm(".weak alias\nalias = sym");
96#else
97#define __weak_alias(alias,sym) \
98 __asm(".weak alias\nalias = sym");
99#endif
100#ifdef __LEADING_UNDERSCORE
101#define __weak_extern(sym) ___weak_extern(_/**/sym)
102#define ___weak_extern(sym) \
103 __asm(".weak sym");
104#else
105#define __weak_extern(sym) \
106 __asm(".weak sym");
107#endif
108#define __warn_references(sym,msg) \
109 __asm(".pushsection .gnu.warning.sym\n" \
110 ".ascii \"" msg "\"\n" \
111 ".popsection");
112
113#endif /* !__STDC__ */
114
115#if __arm__
116#define __ifunc(name, resolver) \
117 __asm(".globl " _C_LABEL_STRING(#name) "\n" \
118 ".type " _C_LABEL_STRING(#name) ", %gnu_indirect_function\n" \
119 _C_LABEL_STRING(#name) " = " _C_LABEL_STRING(#resolver))
120#define __hidden_ifunc(name, resolver) \
121 __asm(".globl " _C_LABEL_STRING(#name) "\n" \
122 ".hidden " _C_LABEL_STRING(#name) "\n" \
123 ".type " _C_LABEL_STRING(#name) ", %gnu_indirect_function\n" \
124 _C_LABEL_STRING(#name) " = " _C_LABEL_STRING(#resolver))
125#else
126#define __ifunc(name, resolver) \
127 __asm(".globl " _C_LABEL_STRING(#name) "\n" \
128 ".type " _C_LABEL_STRING(#name) ", @gnu_indirect_function\n" \
129 _C_LABEL_STRING(#name) " = " _C_LABEL_STRING(#resolver))
130#define __hidden_ifunc(name, resolver) \
131 __asm(".globl " _C_LABEL_STRING(#name) "\n" \
132 ".hidden " _C_LABEL_STRING(#name) "\n" \
133 ".type " _C_LABEL_STRING(#name) ", @gnu_indirect_function\n" \
134 _C_LABEL_STRING(#name) " = " _C_LABEL_STRING(#resolver))
135#endif
136
137#if __STDC__
138#define __SECTIONSTRING(_sec, _str) \
139 __asm(".pushsection " #_sec "\n" \
140 ".asciz \"" _str "\"\n" \
141 ".popsection")
142#else
143#define __SECTIONSTRING(_sec, _str) \
144 __asm(".pushsection _sec\n" \
145 ".asciz \"" _str "\"\n" \
146 ".popsection")
147#endif
148
149#define __IDSTRING(_n,_s) __SECTIONSTRING(.ident,_s)
150
151#define __RCSID(_s) __IDSTRING(rcsid,_s)
152#define __SCCSID(_s)
153#define __SCCSID2(_s)
154#define __COPYRIGHT(_s) __SECTIONSTRING(.copyright,_s)
155
156#define __KERNEL_RCSID(_n, _s) __RCSID(_s)
157#define __KERNEL_SCCSID(_n, _s)
158#define __KERNEL_COPYRIGHT(_n, _s) __COPYRIGHT(_s)
159
160#ifndef __lint__
161#define __link_set_make_entry(set, sym) \
162 static void const * const __link_set_##set##_sym_##sym \
163 __section("link_set_" #set) __used = (const void *)&sym
164#define __link_set_make_entry2(set, sym, n) \
165 static void const * const __link_set_##set##_sym_##sym##_##n \
166 __section("link_set_" #set) __used = (const void *)&sym[n]
167#else
168#define __link_set_make_entry(set, sym) \
169 extern void const * const __link_set_##set##_sym_##sym
170#define __link_set_make_entry2(set, sym, n) \
171 extern void const * const __link_set_##set##_sym_##sym##_##n
172#endif /* __lint__ */
173
174#define __link_set_add_text(set, sym) __link_set_make_entry(set, sym)
175#define __link_set_add_rodata(set, sym) __link_set_make_entry(set, sym)
176#define __link_set_add_data(set, sym) __link_set_make_entry(set, sym)
177#define __link_set_add_bss(set, sym) __link_set_make_entry(set, sym)
178#define __link_set_add_text2(set, sym, n) __link_set_make_entry2(set, sym, n)
179#define __link_set_add_rodata2(set, sym, n) __link_set_make_entry2(set, sym, n)
180#define __link_set_add_data2(set, sym, n) __link_set_make_entry2(set, sym, n)
181#define __link_set_add_bss2(set, sym, n) __link_set_make_entry2(set, sym, n)
182
183#define __link_set_start(set) (__start_link_set_##set)
184#define __link_set_end(set) (__stop_link_set_##set)
185
186#define __link_set_decl(set, ptype) \
187 extern ptype * const __link_set_start(set)[] __dso_hidden; \
188 __asm__(".hidden " __STRING(__stop_link_set_##set)); \
189 extern ptype * const __link_set_end(set)[] __weak __dso_hidden
190
191#define __link_set_count(set) \
192 (__link_set_end(set) - __link_set_start(set))
193
194
195#ifdef _KERNEL
196
197/*
198 * On multiprocessor systems we can gain an improvement in performance
199 * by being mindful of which cachelines data is placed in.
200 *
201 * __read_mostly:
202 *
203 * It makes sense to ensure that rarely modified data is not
204 * placed in the same cacheline as frequently modified data.
205 * To mitigate the phenomenon known as "false-sharing" we
206 * can annotate rarely modified variables with __read_mostly.
207 * All such variables are placed into the .data.read_mostly
208 * section in the kernel ELF.
209 *
210 * Prime candidates for __read_mostly annotation are variables
211 * which are hardly ever modified and which are used in code
212 * hot-paths, e.g. pmap_initialized.
213 *
214 * __cacheline_aligned:
215 *
216 * Some data structures (mainly locks) benefit from being aligned
217 * on a cacheline boundary, and having a cacheline to themselves.
218 * This way, the modification of other data items cannot adversely
219 * affect the lock and vice versa.
220 *
221 * Any variables annotated with __cacheline_aligned will be
222 * placed into the .data.cacheline_aligned ELF section.
223 */
224#define __read_mostly \
225 __attribute__((__section__(".data.read_mostly")))
226
227#define __cacheline_aligned \
228 __attribute__((__aligned__(COHERENCY_UNIT), \
229 __section__(".data.cacheline_aligned")))
230
231#endif /* _KERNEL */
232
233#endif /* !_SYS_CDEFS_ELF_H_ */
234