xref: /illumos-gate/usr/src/uts/common/sys/linker_set.h (revision d56b5f9f)
190ce8b93SToomas Soome /*
290ce8b93SToomas Soome  * Copyright (c) 1999 John D. Polstra
390ce8b93SToomas Soome  * Copyright (c) 1999,2001 Peter Wemm <peter@FreeBSD.org>
490ce8b93SToomas Soome  * All rights reserved.
590ce8b93SToomas Soome  *
690ce8b93SToomas Soome  * Redistribution and use in source and binary forms, with or without
790ce8b93SToomas Soome  * modification, are permitted provided that the following conditions
890ce8b93SToomas Soome  * are met:
990ce8b93SToomas Soome  * 1. Redistributions of source code must retain the above copyright
1090ce8b93SToomas Soome  *    notice, this list of conditions and the following disclaimer.
1190ce8b93SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
1290ce8b93SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
1390ce8b93SToomas Soome  *    documentation and/or other materials provided with the distribution.
1490ce8b93SToomas Soome  *
1590ce8b93SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1690ce8b93SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1790ce8b93SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1890ce8b93SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1990ce8b93SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2090ce8b93SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2190ce8b93SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2290ce8b93SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2390ce8b93SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2490ce8b93SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2590ce8b93SToomas Soome  * SUCH DAMAGE.
2690ce8b93SToomas Soome  */
2790ce8b93SToomas Soome 
2890ce8b93SToomas Soome #ifndef _SYS_LINKER_SET_H_
2990ce8b93SToomas Soome #define	_SYS_LINKER_SET_H_
3090ce8b93SToomas Soome 
3190ce8b93SToomas Soome #include <sys/ccompile.h>
3290ce8b93SToomas Soome 
3390ce8b93SToomas Soome /*
3490ce8b93SToomas Soome  * The following macros are used to declare global sets of objects, which
3590ce8b93SToomas Soome  * are collected by the linker into a `linker_set' as defined below.
3690ce8b93SToomas Soome  * For ELF, this is done by constructing a separate segment for each set.
3790ce8b93SToomas Soome  */
3890ce8b93SToomas Soome 
3990ce8b93SToomas Soome #define	__MAKE_SET_CONST const
4090ce8b93SToomas Soome 
4190ce8b93SToomas Soome #define	__CONCAT1(x, y)	x ## y
4290ce8b93SToomas Soome #define	__CONCAT(x, y)	__CONCAT1(x, y)
4390ce8b93SToomas Soome 
44*d56b5f9fSToomas Soome #define	__STRING(x)	#x		/* stringify without expanding x */
45*d56b5f9fSToomas Soome #define	__XSTRING(x)	__STRING(x)	/* expand x, then stringify */
46*d56b5f9fSToomas Soome 
47*d56b5f9fSToomas Soome #define	__GLOBL(sym)	__asm__(".globl " __XSTRING(sym))
48*d56b5f9fSToomas Soome #define	__WEAK(sym)	__asm__(".weak " __XSTRING(sym))
4990ce8b93SToomas Soome /*
5090ce8b93SToomas Soome  * Private macros, not to be used outside this header file.
5190ce8b93SToomas Soome  */
5290ce8b93SToomas Soome #define	__MAKE_SET(set, sym)				\
53*d56b5f9fSToomas Soome 	__WEAK(__CONCAT(__start_set_, set));		\
54*d56b5f9fSToomas Soome 	__WEAK(__CONCAT(__stop_set_, set));		\
5590ce8b93SToomas Soome 	static void const * __MAKE_SET_CONST		\
5690ce8b93SToomas Soome 	__set_##set##_sym_##sym __section("set_" #set)	\
5790ce8b93SToomas Soome 	__used = &(sym)
5890ce8b93SToomas Soome 
5990ce8b93SToomas Soome /*
6090ce8b93SToomas Soome  * Public macros.
6190ce8b93SToomas Soome  */
6290ce8b93SToomas Soome #define	TEXT_SET(set, sym)	__MAKE_SET(set, sym)
6390ce8b93SToomas Soome #define	DATA_SET(set, sym)	__MAKE_SET(set, sym)
6490ce8b93SToomas Soome #define	BSS_SET(set, sym)	__MAKE_SET(set, sym)
6590ce8b93SToomas Soome #define	ABS_SET(set, sym)	__MAKE_SET(set, sym)
6690ce8b93SToomas Soome #define	SET_ENTRY(set, sym)	__MAKE_SET(set, sym)
6790ce8b93SToomas Soome 
6890ce8b93SToomas Soome /*
6990ce8b93SToomas Soome  * Initialize before referring to a given linker set.
7090ce8b93SToomas Soome  */
7190ce8b93SToomas Soome #define	SET_DECLARE(set, ptype)						\
7290ce8b93SToomas Soome 	extern ptype __weak_symbol *__CONCAT(__start_set_, set);	\
7390ce8b93SToomas Soome 	extern ptype __weak_symbol *__CONCAT(__stop_set_, set)
7490ce8b93SToomas Soome 
7590ce8b93SToomas Soome #define	SET_BEGIN(set)							\
7690ce8b93SToomas Soome 	(&__CONCAT(__start_set_, set))
7790ce8b93SToomas Soome #define	SET_LIMIT(set)							\
7890ce8b93SToomas Soome 	(&__CONCAT(__stop_set_, set))
7990ce8b93SToomas Soome 
8090ce8b93SToomas Soome /*
8190ce8b93SToomas Soome  * Iterate over all the elements of a set.
8290ce8b93SToomas Soome  *
8390ce8b93SToomas Soome  * Sets always contain addresses of things, and "pvar" points to words
8490ce8b93SToomas Soome  * containing those addresses.  Thus is must be declared as "type **pvar",
8590ce8b93SToomas Soome  * and the address of each set item is obtained inside the loop by "*pvar".
8690ce8b93SToomas Soome  */
8790ce8b93SToomas Soome #define	SET_FOREACH(pvar, set)						\
8890ce8b93SToomas Soome 	for (pvar = SET_BEGIN(set); pvar < SET_LIMIT(set); pvar++)
8990ce8b93SToomas Soome 
9090ce8b93SToomas Soome #define	SET_ITEM(set, i)						\
9190ce8b93SToomas Soome 	((SET_BEGIN(set))[i])
9290ce8b93SToomas Soome 
9390ce8b93SToomas Soome /*
9490ce8b93SToomas Soome  * Provide a count of the items in a set.
9590ce8b93SToomas Soome  */
9690ce8b93SToomas Soome #define	SET_COUNT(set)							\
9790ce8b93SToomas Soome 	(SET_LIMIT(set) - SET_BEGIN(set))
9890ce8b93SToomas Soome 
9990ce8b93SToomas Soome #endif	/* _SYS_LINKER_SET_H_ */
100