xref: /illumos-gate/usr/src/boot/sys/sys/linker_set.h (revision 199767f8)
1*199767f8SToomas Soome /*-
2*199767f8SToomas Soome  * Copyright (c) 1999 John D. Polstra
3*199767f8SToomas Soome  * Copyright (c) 1999,2001 Peter Wemm <peter@FreeBSD.org>
4*199767f8SToomas Soome  * All rights reserved.
5*199767f8SToomas Soome  *
6*199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
7*199767f8SToomas Soome  * modification, are permitted provided that the following conditions
8*199767f8SToomas Soome  * are met:
9*199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
10*199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
11*199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
12*199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
13*199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
14*199767f8SToomas Soome  *
15*199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*199767f8SToomas Soome  * SUCH DAMAGE.
26*199767f8SToomas Soome  *
27*199767f8SToomas Soome  * $FreeBSD$
28*199767f8SToomas Soome  */
29*199767f8SToomas Soome 
30*199767f8SToomas Soome #ifndef _SYS_LINKER_SET_H_
31*199767f8SToomas Soome #define _SYS_LINKER_SET_H_
32*199767f8SToomas Soome 
33*199767f8SToomas Soome #ifndef _SYS_CDEFS_H_
34*199767f8SToomas Soome #error this file needs sys/cdefs.h as a prerequisite
35*199767f8SToomas Soome #endif
36*199767f8SToomas Soome 
37*199767f8SToomas Soome /*
38*199767f8SToomas Soome  * The following macros are used to declare global sets of objects, which
39*199767f8SToomas Soome  * are collected by the linker into a `linker_set' as defined below.
40*199767f8SToomas Soome  * For ELF, this is done by constructing a separate segment for each set.
41*199767f8SToomas Soome  */
42*199767f8SToomas Soome 
43*199767f8SToomas Soome #if defined(__powerpc64__)
44*199767f8SToomas Soome /*
45*199767f8SToomas Soome  * Move the symbol pointer from ".text" to ".data" segment, to make
46*199767f8SToomas Soome  * the GCC compiler happy:
47*199767f8SToomas Soome  */
48*199767f8SToomas Soome #define	__MAKE_SET_CONST
49*199767f8SToomas Soome #else
50*199767f8SToomas Soome #define	__MAKE_SET_CONST const
51*199767f8SToomas Soome #endif
52*199767f8SToomas Soome 
53*199767f8SToomas Soome /*
54*199767f8SToomas Soome  * Private macros, not to be used outside this header file.
55*199767f8SToomas Soome  */
56*199767f8SToomas Soome #ifdef __GNUCLIKE___SECTION
57*199767f8SToomas Soome #define __MAKE_SET(set, sym)				\
58*199767f8SToomas Soome 	__GLOBL(__CONCAT(__start_set_,set));		\
59*199767f8SToomas Soome 	__GLOBL(__CONCAT(__stop_set_,set));		\
60*199767f8SToomas Soome 	static void const * __MAKE_SET_CONST		\
61*199767f8SToomas Soome 	__set_##set##_sym_##sym __section("set_" #set)	\
62*199767f8SToomas Soome 	__used = &(sym)
63*199767f8SToomas Soome #else /* !__GNUCLIKE___SECTION */
64*199767f8SToomas Soome #ifndef lint
65*199767f8SToomas Soome #error this file needs to be ported to your compiler
66*199767f8SToomas Soome #endif /* lint */
67*199767f8SToomas Soome #define __MAKE_SET(set, sym)	extern void const * const (__set_##set##_sym_##sym)
68*199767f8SToomas Soome #endif /* __GNUCLIKE___SECTION */
69*199767f8SToomas Soome 
70*199767f8SToomas Soome /*
71*199767f8SToomas Soome  * Public macros.
72*199767f8SToomas Soome  */
73*199767f8SToomas Soome #define TEXT_SET(set, sym)	__MAKE_SET(set, sym)
74*199767f8SToomas Soome #define DATA_SET(set, sym)	__MAKE_SET(set, sym)
75*199767f8SToomas Soome #define BSS_SET(set, sym)	__MAKE_SET(set, sym)
76*199767f8SToomas Soome #define ABS_SET(set, sym)	__MAKE_SET(set, sym)
77*199767f8SToomas Soome #define SET_ENTRY(set, sym)	__MAKE_SET(set, sym)
78*199767f8SToomas Soome 
79*199767f8SToomas Soome /*
80*199767f8SToomas Soome  * Initialize before referring to a given linker set.
81*199767f8SToomas Soome  */
82*199767f8SToomas Soome #define SET_DECLARE(set, ptype)					\
83*199767f8SToomas Soome 	extern ptype __weak_symbol *__CONCAT(__start_set_,set);	\
84*199767f8SToomas Soome 	extern ptype __weak_symbol *__CONCAT(__stop_set_,set)
85*199767f8SToomas Soome 
86*199767f8SToomas Soome #define SET_BEGIN(set)							\
87*199767f8SToomas Soome 	(&__CONCAT(__start_set_,set))
88*199767f8SToomas Soome #define SET_LIMIT(set)							\
89*199767f8SToomas Soome 	(&__CONCAT(__stop_set_,set))
90*199767f8SToomas Soome 
91*199767f8SToomas Soome /*
92*199767f8SToomas Soome  * Iterate over all the elements of a set.
93*199767f8SToomas Soome  *
94*199767f8SToomas Soome  * Sets always contain addresses of things, and "pvar" points to words
95*199767f8SToomas Soome  * containing those addresses.  Thus is must be declared as "type **pvar",
96*199767f8SToomas Soome  * and the address of each set item is obtained inside the loop by "*pvar".
97*199767f8SToomas Soome  */
98*199767f8SToomas Soome #define SET_FOREACH(pvar, set)						\
99*199767f8SToomas Soome 	for (pvar = SET_BEGIN(set); pvar < SET_LIMIT(set); pvar++)
100*199767f8SToomas Soome 
101*199767f8SToomas Soome #define SET_ITEM(set, i)						\
102*199767f8SToomas Soome 	((SET_BEGIN(set))[i])
103*199767f8SToomas Soome 
104*199767f8SToomas Soome /*
105*199767f8SToomas Soome  * Provide a count of the items in a set.
106*199767f8SToomas Soome  */
107*199767f8SToomas Soome #define SET_COUNT(set)							\
108*199767f8SToomas Soome 	(SET_LIMIT(set) - SET_BEGIN(set))
109*199767f8SToomas Soome 
110*199767f8SToomas Soome #endif	/* _SYS_LINKER_SET_H_ */
111