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