142645Sjdp/*-
242645Sjdp * Copyright (c) 1999 John D. Polstra
378161Speter * Copyright (c) 1999,2001 Peter Wemm <peter@FreeBSD.org>
442645Sjdp * All rights reserved.
542645Sjdp *
642645Sjdp * Redistribution and use in source and binary forms, with or without
742645Sjdp * modification, are permitted provided that the following conditions
842645Sjdp * are met:
942645Sjdp * 1. Redistributions of source code must retain the above copyright
1042645Sjdp *    notice, this list of conditions and the following disclaimer.
1142645Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1242645Sjdp *    notice, this list of conditions and the following disclaimer in the
1342645Sjdp *    documentation and/or other materials provided with the distribution.
1442645Sjdp *
1542645Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1642645Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1742645Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1842645Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1942645Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2042645Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2142645Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2242645Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2342645Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2442645Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2542645Sjdp * SUCH DAMAGE.
2642645Sjdp *
2750477Speter * $FreeBSD$
2842645Sjdp */
2942645Sjdp
3042645Sjdp#ifndef _SYS_LINKER_SET_H_
3142645Sjdp#define _SYS_LINKER_SET_H_
3242645Sjdp
33143063Sjoerg#ifndef _SYS_CDEFS_H_
34143063Sjoerg#error this file needs sys/cdefs.h as a prerequisite
35143063Sjoerg#endif
36143063Sjoerg
3742645Sjdp/*
3842645Sjdp * The following macros are used to declare global sets of objects, which
3978161Speter * are collected by the linker into a `linker_set' as defined below.
4042645Sjdp * For ELF, this is done by constructing a separate segment for each set.
4178161Speter */
4278161Speter
43268055Shselasky#if defined(__powerpc64__)
4478161Speter/*
45268055Shselasky * Move the symbol pointer from ".text" to ".data" segment, to make
46268055Shselasky * the GCC compiler happy:
47268055Shselasky */
48268055Shselasky#define	__MAKE_SET_CONST
49268055Shselasky#else
50268055Shselasky#define	__MAKE_SET_CONST const
51268055Shselasky#endif
52268055Shselasky
53268055Shselasky/*
5478161Speter * Private macros, not to be used outside this header file.
5578161Speter */
56143063Sjoerg#ifdef __GNUCLIKE___SECTION
57268055Shselasky#define __MAKE_SET(set, sym)				\
58268055Shselasky	__GLOBL(__CONCAT(__start_set_,set));		\
59268055Shselasky	__GLOBL(__CONCAT(__stop_set_,set));		\
60268055Shselasky	static void const * __MAKE_SET_CONST		\
61268055Shselasky	__set_##set##_sym_##sym __section("set_" #set)	\
62268055Shselasky	__used = &(sym)
63143063Sjoerg#else /* !__GNUCLIKE___SECTION */
6495200Smarkm#ifndef lint
65143063Sjoerg#error this file needs to be ported to your compiler
6695200Smarkm#endif /* lint */
6795200Smarkm#define __MAKE_SET(set, sym)	extern void const * const (__set_##set##_sym_##sym)
68143063Sjoerg#endif /* __GNUCLIKE___SECTION */
6978161Speter
7078161Speter/*
7178161Speter * Public macros.
7278161Speter */
7378161Speter#define TEXT_SET(set, sym)	__MAKE_SET(set, sym)
7478161Speter#define DATA_SET(set, sym)	__MAKE_SET(set, sym)
7578161Speter#define BSS_SET(set, sym)	__MAKE_SET(set, sym)
7678161Speter#define ABS_SET(set, sym)	__MAKE_SET(set, sym)
7778161Speter#define SET_ENTRY(set, sym)	__MAKE_SET(set, sym)
7878161Speter
7978161Speter/*
80140231Skeramida * Initialize before referring to a given linker set.
8178161Speter */
82268138Shselasky#define SET_DECLARE(set, ptype)					\
83284743Shselasky	extern ptype __weak_symbol *__CONCAT(__start_set_,set);	\
84284743Shselasky	extern ptype __weak_symbol *__CONCAT(__stop_set_,set)
8578161Speter
8678161Speter#define SET_BEGIN(set)							\
8778161Speter	(&__CONCAT(__start_set_,set))
8878161Speter#define SET_LIMIT(set)							\
8978161Speter	(&__CONCAT(__stop_set_,set))
9078161Speter
9178161Speter/*
9278161Speter * Iterate over all the elements of a set.
9378161Speter *
9478161Speter * Sets always contain addresses of things, and "pvar" points to words
9578161Speter * containing those addresses.  Thus is must be declared as "type **pvar",
9678161Speter * and the address of each set item is obtained inside the loop by "*pvar".
9742645Sjdp */
9878161Speter#define SET_FOREACH(pvar, set)						\
9978161Speter	for (pvar = SET_BEGIN(set); pvar < SET_LIMIT(set); pvar++)
10042645Sjdp
10178161Speter#define SET_ITEM(set, i)						\
10278161Speter	((SET_BEGIN(set))[i])
10342645Sjdp
10478161Speter/*
10578161Speter * Provide a count of the items in a set.
10678161Speter */
10778161Speter#define SET_COUNT(set)							\
10878161Speter	(SET_LIMIT(set) - SET_BEGIN(set))
10942645Sjdp
11078161Speter#endif	/* _SYS_LINKER_SET_H_ */
111