xref: /illumos-gate/usr/src/boot/sys/sys/linker_set.h (revision d56b5f9f)
1199767f8SToomas Soome /*-
2199767f8SToomas Soome  * Copyright (c) 1999 John D. Polstra
3199767f8SToomas Soome  * Copyright (c) 1999,2001 Peter Wemm <peter@FreeBSD.org>
4199767f8SToomas Soome  * All rights reserved.
5199767f8SToomas Soome  *
6199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
7199767f8SToomas Soome  * modification, are permitted provided that the following conditions
8199767f8SToomas Soome  * are met:
9199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
10199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
11199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
12199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
13199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
14199767f8SToomas Soome  *
15199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25199767f8SToomas Soome  * SUCH DAMAGE.
26199767f8SToomas Soome  *
27199767f8SToomas Soome  * $FreeBSD$
28199767f8SToomas Soome  */
29199767f8SToomas Soome 
30199767f8SToomas Soome #ifndef _SYS_LINKER_SET_H_
31199767f8SToomas Soome #define _SYS_LINKER_SET_H_
32199767f8SToomas Soome 
33199767f8SToomas Soome #ifndef _SYS_CDEFS_H_
34199767f8SToomas Soome #error this file needs sys/cdefs.h as a prerequisite
35199767f8SToomas Soome #endif
36199767f8SToomas Soome 
37199767f8SToomas Soome /*
38199767f8SToomas Soome  * The following macros are used to declare global sets of objects, which
39199767f8SToomas Soome  * are collected by the linker into a `linker_set' as defined below.
40199767f8SToomas Soome  * For ELF, this is done by constructing a separate segment for each set.
41199767f8SToomas Soome  */
42199767f8SToomas Soome 
43199767f8SToomas Soome #if defined(__powerpc64__)
44199767f8SToomas Soome /*
45199767f8SToomas Soome  * Move the symbol pointer from ".text" to ".data" segment, to make
46199767f8SToomas Soome  * the GCC compiler happy:
47199767f8SToomas Soome  */
48199767f8SToomas Soome #define	__MAKE_SET_CONST
49199767f8SToomas Soome #else
50199767f8SToomas Soome #define	__MAKE_SET_CONST const
51199767f8SToomas Soome #endif
52199767f8SToomas Soome 
53199767f8SToomas Soome /*
54199767f8SToomas Soome  * Private macros, not to be used outside this header file.
55199767f8SToomas Soome  */
56199767f8SToomas Soome #ifdef __GNUCLIKE___SECTION
57199767f8SToomas Soome #define __MAKE_SET(set, sym)				\
58*d56b5f9fSToomas Soome 	__WEAK(__CONCAT(__start_set_,set));		\
59*d56b5f9fSToomas Soome 	__WEAK(__CONCAT(__stop_set_,set));		\
60199767f8SToomas Soome 	static void const * __MAKE_SET_CONST		\
61199767f8SToomas Soome 	__set_##set##_sym_##sym __section("set_" #set)	\
62199767f8SToomas Soome 	__used = &(sym)
63199767f8SToomas Soome #else /* !__GNUCLIKE___SECTION */
64199767f8SToomas Soome #ifndef lint
65199767f8SToomas Soome #error this file needs to be ported to your compiler
66199767f8SToomas Soome #endif /* lint */
67199767f8SToomas Soome #define __MAKE_SET(set, sym)	extern void const * const (__set_##set##_sym_##sym)
68199767f8SToomas Soome #endif /* __GNUCLIKE___SECTION */
69199767f8SToomas Soome 
70199767f8SToomas Soome /*
71199767f8SToomas Soome  * Public macros.
72199767f8SToomas Soome  */
73199767f8SToomas Soome #define TEXT_SET(set, sym)	__MAKE_SET(set, sym)
74199767f8SToomas Soome #define DATA_SET(set, sym)	__MAKE_SET(set, sym)
75199767f8SToomas Soome #define BSS_SET(set, sym)	__MAKE_SET(set, sym)
76199767f8SToomas Soome #define ABS_SET(set, sym)	__MAKE_SET(set, sym)
77199767f8SToomas Soome #define SET_ENTRY(set, sym)	__MAKE_SET(set, sym)
78199767f8SToomas Soome 
79199767f8SToomas Soome /*
80199767f8SToomas Soome  * Initialize before referring to a given linker set.
81199767f8SToomas Soome  */
82199767f8SToomas Soome #define SET_DECLARE(set, ptype)					\
83199767f8SToomas Soome 	extern ptype __weak_symbol *__CONCAT(__start_set_,set);	\
84199767f8SToomas Soome 	extern ptype __weak_symbol *__CONCAT(__stop_set_,set)
85199767f8SToomas Soome 
86199767f8SToomas Soome #define SET_BEGIN(set)							\
87199767f8SToomas Soome 	(&__CONCAT(__start_set_,set))
88199767f8SToomas Soome #define SET_LIMIT(set)							\
89199767f8SToomas Soome 	(&__CONCAT(__stop_set_,set))
90199767f8SToomas Soome 
91199767f8SToomas Soome /*
92199767f8SToomas Soome  * Iterate over all the elements of a set.
93199767f8SToomas Soome  *
94199767f8SToomas Soome  * Sets always contain addresses of things, and "pvar" points to words
95199767f8SToomas Soome  * containing those addresses.  Thus is must be declared as "type **pvar",
96199767f8SToomas Soome  * and the address of each set item is obtained inside the loop by "*pvar".
97199767f8SToomas Soome  */
98199767f8SToomas Soome #define SET_FOREACH(pvar, set)						\
99199767f8SToomas Soome 	for (pvar = SET_BEGIN(set); pvar < SET_LIMIT(set); pvar++)
100199767f8SToomas Soome 
101199767f8SToomas Soome #define SET_ITEM(set, i)						\
102199767f8SToomas Soome 	((SET_BEGIN(set))[i])
103199767f8SToomas Soome 
104199767f8SToomas Soome /*
105199767f8SToomas Soome  * Provide a count of the items in a set.
106199767f8SToomas Soome  */
107199767f8SToomas Soome #define SET_COUNT(set)							\
108199767f8SToomas Soome 	(SET_LIMIT(set) - SET_BEGIN(set))
109199767f8SToomas Soome 
110199767f8SToomas Soome #endif	/* _SYS_LINKER_SET_H_ */
111