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