source: rtems-libbsd/freebsd/sys/sys/linker_set.h @ c5e81a2

6-freebsd-12
Last change on this file since c5e81a2 was c5e81a2, checked in by Joel Sherrill <joel@…>, on 07/20/21 at 21:02:50

linker_set.h: Add alignof implementation for when not C11 or C++11

The default implementation was completely broken. Use the GCC specific
alignof if compiling for C99 or C++03. If not C++11, C11, or
GCC, then it is an error.

  • Property mode set to 100755
File size: 7.0 KB
Line 
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 * $FreeBSD$
30 */
31
32#ifndef _SYS_LINKER_SET_H_
33#define _SYS_LINKER_SET_H_
34
35#ifndef _SYS_CDEFS_H_
36#error this file needs sys/cdefs.h as a prerequisite
37#endif
38#ifdef __rtems__
39#include <sys/_types.h>
40#endif /* __rtems__ */
41
42/*
43 * The following macros are used to declare global sets of objects, which
44 * are collected by the linker into a `linker_set' as defined below.
45 * For ELF, this is done by constructing a separate segment for each set.
46 */
47
48#if defined(__powerpc64__)
49/*
50 * Move the symbol pointer from ".text" to ".data" segment, to make
51 * the GCC compiler happy:
52 */
53#define __MAKE_SET_CONST
54#else
55#define __MAKE_SET_CONST const
56#endif
57
58/*
59 * Private macros, not to be used outside this header file.
60 */
61#ifdef __GNUCLIKE___SECTION
62#ifndef __rtems__
63#define __MAKE_SET_QV(set, sym, qv)                     \
64        __GLOBL(__CONCAT(__start_set_,set));            \
65        __GLOBL(__CONCAT(__stop_set_,set));             \
66        static void const * qv                          \
67        __set_##set##_sym_##sym __section("set_" #set)  \
68        __used = &(sym)
69#define __MAKE_SET(set, sym)    __MAKE_SET_QV(set, sym, __MAKE_SET_CONST)
70#else /* __rtems__ */
71
72#if __cplusplus >= 201103L
73  #define RTEMS_BSD_ALIGNOF( _type_name ) alignof( _type_name )
74#elif __STDC_VERSION__ >= 201112L
75  #define RTEMS_BSD_ALIGNOF( _type_name ) _Alignof( _type_name )
76#elif defined(__GNUC__)
77  #define RTEMS_BSD_ALIGNOF( _type_name ) __alignof__( _type_name )
78#else
79  #error "FIX ME! Implement RTEMS_BSD_ALIGNOF() for this environment"
80#endif
81
82#define RTEMS_BSD_SET_ALIGN( type )     \
83        __attribute__(( __aligned__( RTEMS_BSD_ALIGNOF( type ) ) ))
84
85#define RTEMS_BSD_DEFINE_SET(set, type)                                 \
86        RTEMS_BSD_SET_ALIGN( type ) type                                \
87        const __CONCAT(_bsd__start_set_,set)[0]                         \
88        __section(".rtemsroset.bsd." __STRING(set) ".begin") __used;    \
89        RTEMS_BSD_SET_ALIGN( type ) type                                \
90        const __CONCAT(_bsd__stop_set_,set)[0]                          \
91        __section(".rtemsroset.bsd." __STRING(set) ".end") __used
92
93#define RTEMS_BSD_DECLARE_SET(set, type)                                \
94        extern RTEMS_BSD_SET_ALIGN( type ) type                         \
95        const __CONCAT(_bsd__start_set_,set)[0];                        \
96        extern RTEMS_BSD_SET_ALIGN( type ) type                         \
97        const __CONCAT(_bsd__stop_set_,set)[0]
98
99#define RTEMS_BSD_DEFINE_SET_ITEM(set, sym, type)                       \
100        static RTEMS_BSD_SET_ALIGN( type ) type                         \
101        const __set_##set##_sym_##sym                                   \
102        __section(".rtemsroset.bsd." __STRING(set) ".content.1") __used
103
104#define RTEMS_BSD_DEFINE_SET_ITEM_ORDERED(set, sym, order, type)        \
105        static RTEMS_BSD_SET_ALIGN( type ) type                         \
106        const __set_##set##_sym_##sym                                   \
107        __section(".rtemsroset.bsd." __STRING(set) ".content.0."  RTEMS_XSTRING( order )) __used
108
109#define __MAKE_SET(set, sym)                                            \
110        RTEMS_BSD_DEFINE_SET_ITEM(set, sym, const void *) = &sym
111
112#define RTEMS_BSD_DEFINE_RWSET(set, type)                               \
113        RTEMS_BSD_SET_ALIGN( type ) type                                \
114        __CONCAT(_bsd__start_set_,set)[0]                               \
115        __section(".rtemsrwset.bsd." __STRING(set) ".begin") __used;    \
116        RTEMS_BSD_SET_ALIGN( type ) type                                \
117        __CONCAT(_bsd__stop_set_,set)[0]                                \
118        __section(".rtemsrwset.bsd." __STRING(set) ".end") __used
119
120#define RTEMS_BSD_DECLARE_RWSET(set, type)                              \
121        extern RTEMS_BSD_SET_ALIGN( type ) type                         \
122        __CONCAT(_bsd__start_set_,set)[0];                              \
123        extern RTEMS_BSD_SET_ALIGN( type ) type                         \
124        __CONCAT(_bsd__stop_set_,set)[0]
125
126#define RTEMS_BSD_DEFINE_RWSET_ITEM(set, sym, type)                     \
127        static RTEMS_BSD_SET_ALIGN( type ) type                         \
128        __set_##set##_sym_##sym                                         \
129        __section(".rtemsrwset.bsd." __STRING(set) ".content") __used
130
131#define __MAKE_RWSET(set, sym)                                          \
132        RTEMS_BSD_DEFINE_RWSET_ITEM(set, sym, const void *) = &sym
133#endif /* __rtems__ */
134#else /* !__GNUCLIKE___SECTION */
135#error this file needs to be ported to your compiler
136#endif /* __GNUCLIKE___SECTION */
137
138/*
139 * Public macros.
140 */
141#define TEXT_SET(set, sym)      __MAKE_SET(set, sym)
142#define DATA_SET(set, sym)      __MAKE_SET(set, sym)
143#ifndef __rtems__
144#define DATA_WSET(set, sym)     __MAKE_SET_QV(set, sym, )
145#else /* __rtems__ */
146#define DATA_WSET(set, sym)     __MAKE_RWSET(set, sym)
147#endif /* __rtems__ */
148#define BSS_SET(set, sym)       __MAKE_SET(set, sym)
149#define ABS_SET(set, sym)       __MAKE_SET(set, sym)
150#define SET_ENTRY(set, sym)     __MAKE_SET(set, sym)
151
152/*
153 * Initialize before referring to a given linker set.
154 */
155#ifndef __rtems__
156#define SET_DECLARE(set, ptype)                                 \
157        extern ptype __weak_symbol *__CONCAT(__start_set_,set); \
158        extern ptype __weak_symbol *__CONCAT(__stop_set_,set)
159
160#define SET_BEGIN(set)                                                  \
161        (&__CONCAT(__start_set_,set))
162#define SET_LIMIT(set)                                                  \
163        (&__CONCAT(__stop_set_,set))
164#else /* __rtems__ */
165#define SET_DECLARE(set, ptype)                                         \
166        RTEMS_BSD_DECLARE_SET(set, ptype *)
167
168#define RWSET_DECLARE(set, ptype)                                       \
169        RTEMS_BSD_DECLARE_RWSET(set, ptype *)
170
171static __inline __uintptr_t
172_linker_set_obfuscate(const void *marker)
173{
174
175        /* Obfuscate the variable, so that the compiler cannot optimize */
176        __asm__("" : "+r" (marker));
177        return ((__uintptr_t)marker);
178}
179
180#define SET_BEGIN(set)                                                  \
181        ((__typeof(&__CONCAT(_bsd__start_set_,set)[0]))                 \
182             _linker_set_obfuscate(__CONCAT(_bsd__start_set_,set)))
183#define SET_LIMIT(set)                                                  \
184        ((__typeof(&__CONCAT(_bsd__stop_set_,set)[0]))                  \
185             _linker_set_obfuscate(__CONCAT(_bsd__stop_set_,set)))
186#endif /* __rtems__ */
187
188/*
189 * Iterate over all the elements of a set.
190 *
191 * Sets always contain addresses of things, and "pvar" points to words
192 * containing those addresses.  Thus is must be declared as "type **pvar",
193 * and the address of each set item is obtained inside the loop by "*pvar".
194 */
195#define SET_FOREACH(pvar, set)                                          \
196        for (pvar = SET_BEGIN(set); pvar < SET_LIMIT(set); pvar++)
197
198#define SET_ITEM(set, i)                                                \
199        ((SET_BEGIN(set))[i])
200
201/*
202 * Provide a count of the items in a set.
203 */
204#define SET_COUNT(set)                                                  \
205        (SET_LIMIT(set) - SET_BEGIN(set))
206
207#endif  /* _SYS_LINKER_SET_H_ */
Note: See TracBrowser for help on using the repository browser.