2 * Copyright (c) 1999 John D. Polstra
3 * Copyright (c) 1999,2001 Peter Wemm <peter@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #ifndef _SYS_LINKER_SET_H_
31 #define _SYS_LINKER_SET_H_
34 * The following macros are used to declare global sets of objects, which
35 * are collected by the linker into a `linker_set' as defined below.
36 * For ELF, this is done by constructing a separate segment for each set.
37 * For a.out, it is done automatically by the linker.
42 * Private macros, not to be used outside this header file.
44 /* this bit of h0h0magic brought to you by cpp */
45 #define __GLOBL(sym) __GLOBL2(sym)
46 #define __GLOBL2(sym) __asm(".globl " #sym)
48 #define __MAKE_SET(set, sym) \
49 __GLOBL(__CONCAT(__start_set_,set)); \
50 __GLOBL(__CONCAT(__stop_set_,set)); \
51 static void const * const __set_##set##_sym_##sym \
52 __attribute__((__section__("set_" #set),__unused__)) = &sym
57 #define TEXT_SET(set, sym) __MAKE_SET(set, sym)
58 #define DATA_SET(set, sym) __MAKE_SET(set, sym)
59 #define BSS_SET(set, sym) __MAKE_SET(set, sym)
60 #define ABS_SET(set, sym) __MAKE_SET(set, sym)
61 #define SET_ENTRY(set, sym) __MAKE_SET(set, sym)
64 * Initialize before referring to a give linker set
66 #define SET_DECLARE(set, ptype) \
67 extern ptype *__CONCAT(__start_set_,set); \
68 extern ptype *__CONCAT(__stop_set_,set)
70 #define SET_BEGIN(set) \
71 (&__CONCAT(__start_set_,set))
72 #define SET_LIMIT(set) \
73 (&__CONCAT(__stop_set_,set))
78 * The old way. This depends on GNU ld extensions that are not widely
79 * available outside of the a.out format.
81 * NB: the constants defined below must match those defined in
82 * ld/ld.h. Since their calculation requires arithmetic, we
83 * can't name them symbolically (e.g., 23 is N_SETT | N_EXT).
85 * In the __MAKE_SET macro below, the line:
86 * static void const * const __set_##set##_sym_##sym = &sym;
87 * is present only to prevent the compiler from producing bogus
88 * warnings about unused symbols.
91 #ifdef __UNDERSCORES__
92 #define __MAKE_SET(set, sym, type) \
93 static void const * const __set_##set##_sym_##sym = &sym; \
94 __asm(".stabs \"_" #set "\", " #type ", 0, 0, _" #sym)
96 #define __MAKE_SET(set, sym, type) \
97 static void const * const __set_##set##_sym_##sym = &sym; \
98 __asm(".stabs \"" #set "\", " #type ", 0, 0, " #sym)
102 #define TEXT_SET(set, sym) __MAKE_SET(set, sym, 23)
103 #define DATA_SET(set, sym) __MAKE_SET(set, sym, 25)
104 #define BSS_SET(set, sym) __MAKE_SET(set, sym, 27)
105 #define ABS_SET(set, sym) __MAKE_SET(set, sym, 21)
106 #define SET_ENTRY(set, sym) error error must provide text/data type
108 #define SET_DECLARE(set, ptype) \
111 ptype *ls_items[1]; \
114 #define SET_BEGIN(set) \
115 (&((set).ls_items[0]))
116 #define SET_LIMIT(set) \
117 (&((set).ls_items[(set).ls_length]))
122 * Iterate over all the elements of a set.
124 * Sets always contain addresses of things, and "pvar" points to words
125 * containing those addresses. Thus is must be declared as "type **pvar",
126 * and the address of each set item is obtained inside the loop by "*pvar".
128 #define SET_FOREACH(pvar, set) \
129 for (pvar = SET_BEGIN(set); pvar < SET_LIMIT(set); pvar++)
131 #define SET_ITEM(set, i) \
132 ((SET_BEGIN(set))[i])
135 * Provide a count of the items in a set.
137 #define SET_COUNT(set) \
138 (SET_LIMIT(set) - SET_BEGIN(set))
140 #endif /* _SYS_LINKER_SET_H_ */