blob: 0f4a2d686e26155ac13d49c6f4cb38085296e058 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Marek Vasut9b8899d2012-10-12 10:27:02 +00002/*
3 * include/linker_lists.h
4 *
5 * Implementation of linker-generated arrays
6 *
7 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
Marek Vasut9b8899d2012-10-12 10:27:02 +00008 */
Albert ARIBAUDc24895e2013-02-25 00:59:00 +00009
Masahiro Yamadab9f9ce42014-02-05 10:52:52 +090010#ifndef __LINKER_LISTS_H__
11#define __LINKER_LISTS_H__
12
Masahiro Yamadad7a73f22014-10-07 14:48:22 +090013#include <linux/compiler.h>
14
Albert ARIBAUDc24895e2013-02-25 00:59:00 +000015/*
Heinrich Schuchardt6da113c2017-09-07 03:55:11 +020016 * There is no use in including this from ASM files.
Albert ARIBAUDc24895e2013-02-25 00:59:00 +000017 * So just don't define anything when included from ASM.
18 */
19
20#if !defined(__ASSEMBLY__)
21
22/**
Bin Meng02a31b72015-07-19 00:20:02 +080023 * llsym() - Access a linker-generated array entry
Simon Glass629bf852015-03-25 12:21:49 -060024 * @_type: Data type of the entry
25 * @_name: Name of the entry
26 * @_list: name of the list. Should contain only characters allowed
27 * in a C variable name!
28 */
29#define llsym(_type, _name, _list) \
30 ((_type *)&_u_boot_list_2_##_list##_2_##_name)
31
32/**
Marek Vasut9b8899d2012-10-12 10:27:02 +000033 * ll_entry_declare() - Declare linker-generated array entry
34 * @_type: Data type of the entry
35 * @_name: Name of the entry
Albert ARIBAUDc24895e2013-02-25 00:59:00 +000036 * @_list: name of the list. Should contain only characters allowed
37 * in a C variable name!
Marek Vasut9b8899d2012-10-12 10:27:02 +000038 *
39 * This macro declares a variable that is placed into a linker-generated
40 * array. This is a basic building block for more advanced use of linker-
41 * generated arrays. The user is expected to build their own macro wrapper
42 * around this one.
43 *
Albert ARIBAUDc24895e2013-02-25 00:59:00 +000044 * A variable declared using this macro must be compile-time initialized.
Marek Vasut9b8899d2012-10-12 10:27:02 +000045 *
46 * Special precaution must be made when using this macro:
Marek Vasut9b8899d2012-10-12 10:27:02 +000047 *
Albert ARIBAUDc24895e2013-02-25 00:59:00 +000048 * 1) The _type must not contain the "static" keyword, otherwise the
49 * entry is generated and can be iterated but is listed in the map
50 * file and cannot be retrieved by name.
Marek Vasut9b8899d2012-10-12 10:27:02 +000051 *
Albert ARIBAUDc24895e2013-02-25 00:59:00 +000052 * 2) In case a section is declared that contains some array elements AND
53 * a subsection of this section is declared and contains some elements,
54 * it is imperative that the elements are of the same type.
Marek Vasut9b8899d2012-10-12 10:27:02 +000055 *
Mario Six8fac2912018-07-10 08:40:17 +020056 * 3) In case an outer section is declared that contains some array elements
Albert ARIBAUDc24895e2013-02-25 00:59:00 +000057 * AND an inner subsection of this section is declared and contains some
Marek Vasut9b8899d2012-10-12 10:27:02 +000058 * elements, then when traversing the outer section, even the elements of
59 * the inner sections are present in the array.
60 *
61 * Example:
Mario Six8fac2912018-07-10 08:40:17 +020062 *
63 * ::
64 *
65 * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = {
66 * .x = 3,
67 * .y = 4,
68 * };
Marek Vasut9b8899d2012-10-12 10:27:02 +000069 */
Albert ARIBAUDc24895e2013-02-25 00:59:00 +000070#define ll_entry_declare(_type, _name, _list) \
71 _type _u_boot_list_2_##_list##_2_##_name __aligned(4) \
Marek Behún4bebdd32021-05-20 13:23:52 +020072 __attribute__((unused)) \
Andrew Scull5a9095c2022-05-30 10:00:04 +000073 __section("__u_boot_list_2_"#_list"_2_"#_name)
Marek Vasut9b8899d2012-10-12 10:27:02 +000074
75/**
Simon Glass5d169aa2014-10-01 19:57:20 -060076 * ll_entry_declare_list() - Declare a list of link-generated array entries
77 * @_type: Data type of each entry
78 * @_name: Name of the entry
79 * @_list: name of the list. Should contain only characters allowed
80 * in a C variable name!
81 *
82 * This is like ll_entry_declare() but creates multiple entries. It should
83 * be assigned to an array.
84 *
Mario Six8fac2912018-07-10 08:40:17 +020085 * ::
86 *
87 * ll_entry_declare_list(struct my_sub_cmd, my_sub_cmd, cmd_sub) = {
88 * { .x = 3, .y = 4 },
89 * { .x = 8, .y = 2 },
90 * { .x = 1, .y = 7 }
91 * };
Simon Glass5d169aa2014-10-01 19:57:20 -060092 */
93#define ll_entry_declare_list(_type, _name, _list) \
94 _type _u_boot_list_2_##_list##_2_##_name[] __aligned(4) \
Marek Behún4bebdd32021-05-20 13:23:52 +020095 __attribute__((unused)) \
Andrew Scull5a9095c2022-05-30 10:00:04 +000096 __section("__u_boot_list_2_"#_list"_2_"#_name)
Simon Glass5d169aa2014-10-01 19:57:20 -060097
Mario Six8fac2912018-07-10 08:40:17 +020098/*
Albert ARIBAUDc24895e2013-02-25 00:59:00 +000099 * We need a 0-byte-size type for iterator symbols, and the compiler
100 * does not allow defining objects of C type 'void'. Using an empty
101 * struct is allowed by the compiler, but causes gcc versions 4.4 and
102 * below to complain about aliasing. Therefore we use the next best
103 * thing: zero-sized arrays, which are both 0-byte-size and exempt from
104 * aliasing warnings.
105 */
106
107/**
Marek Vasut9b8899d2012-10-12 10:27:02 +0000108 * ll_entry_start() - Point to first entry of linker-generated array
109 * @_type: Data type of the entry
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000110 * @_list: Name of the list in which this entry is placed
Marek Vasut9b8899d2012-10-12 10:27:02 +0000111 *
Mario Six8fac2912018-07-10 08:40:17 +0200112 * This function returns ``(_type *)`` pointer to the very first entry of a
Andrew Scull5a9095c2022-05-30 10:00:04 +0000113 * linker-generated array placed into subsection of __u_boot_list section
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000114 * specified by _list argument.
115 *
116 * Since this macro defines an array start symbol, its leftmost index
117 * must be 2 and its rightmost index must be 1.
Marek Vasut9b8899d2012-10-12 10:27:02 +0000118 *
119 * Example:
Mario Six8fac2912018-07-10 08:40:17 +0200120 *
121 * ::
122 *
123 * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub);
Marek Vasut9b8899d2012-10-12 10:27:02 +0000124 */
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000125#define ll_entry_start(_type, _list) \
126({ \
Simon Glassb87153c2020-12-16 21:20:06 -0700127 static char start[0] __aligned(CONFIG_LINKER_LIST_ALIGN) \
Marek Behún4bebdd32021-05-20 13:23:52 +0200128 __attribute__((unused)) \
Andrew Scull5a9095c2022-05-30 10:00:04 +0000129 __section("__u_boot_list_2_"#_list"_1"); \
Tom Rini04b5de32023-03-28 14:54:51 -0400130 _type * tmp = (_type *)&start; \
131 asm("":"+r"(tmp)); \
132 tmp; \
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000133})
Marek Vasut9b8899d2012-10-12 10:27:02 +0000134
135/**
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000136 * ll_entry_end() - Point after last entry of linker-generated array
Marek Vasut9b8899d2012-10-12 10:27:02 +0000137 * @_type: Data type of the entry
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000138 * @_list: Name of the list in which this entry is placed
Marek Vasut9b8899d2012-10-12 10:27:02 +0000139 * (with underscores instead of dots)
140 *
Mario Six8fac2912018-07-10 08:40:17 +0200141 * This function returns ``(_type *)`` pointer after the very last entry of
Andrew Scull5a9095c2022-05-30 10:00:04 +0000142 * a linker-generated array placed into subsection of __u_boot_list
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000143 * section specified by _list argument.
144 *
145 * Since this macro defines an array end symbol, its leftmost index
146 * must be 2 and its rightmost index must be 3.
147 *
148 * Example:
Mario Six8fac2912018-07-10 08:40:17 +0200149 *
150 * ::
151 *
152 * struct my_sub_cmd *msc = ll_entry_end(struct my_sub_cmd, cmd_sub);
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000153 */
154#define ll_entry_end(_type, _list) \
155({ \
Marek Behún4bebdd32021-05-20 13:23:52 +0200156 static char end[0] __aligned(4) __attribute__((unused)) \
Andrew Scull5a9095c2022-05-30 10:00:04 +0000157 __section("__u_boot_list_2_"#_list"_3"); \
Tom Rini04b5de32023-03-28 14:54:51 -0400158 _type * tmp = (_type *)&end; \
159 asm("":"+r"(tmp)); \
160 tmp; \
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000161})
162/**
163 * ll_entry_count() - Return the number of elements in linker-generated array
164 * @_type: Data type of the entry
165 * @_list: Name of the list of which the number of elements is computed
166 *
Marek Vasut9b8899d2012-10-12 10:27:02 +0000167 * This function returns the number of elements of a linker-generated array
Andrew Scull5a9095c2022-05-30 10:00:04 +0000168 * placed into subsection of __u_boot_list section specified by _list
Marek Vasut9b8899d2012-10-12 10:27:02 +0000169 * argument. The result is of an unsigned int type.
170 *
171 * Example:
Mario Six8fac2912018-07-10 08:40:17 +0200172 *
173 * ::
174 *
175 * int i;
176 * const unsigned int count = ll_entry_count(struct my_sub_cmd, cmd_sub);
177 * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub);
178 * for (i = 0; i < count; i++, msc++)
179 * printf("Entry %i, x=%i y=%i\n", i, msc->x, msc->y);
Marek Vasut9b8899d2012-10-12 10:27:02 +0000180 */
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000181#define ll_entry_count(_type, _list) \
Marek Vasut9b8899d2012-10-12 10:27:02 +0000182 ({ \
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000183 _type *start = ll_entry_start(_type, _list); \
184 _type *end = ll_entry_end(_type, _list); \
185 unsigned int _ll_result = end - start; \
Marek Vasut9b8899d2012-10-12 10:27:02 +0000186 _ll_result; \
187 })
188
Marek Vasut9b8899d2012-10-12 10:27:02 +0000189/**
Simon Glassc2e8b2e2025-01-20 14:25:27 -0700190 * Declares a symbol that points to the start/end of the list.
191 *
192 * @_sym: Arbitrary name for the symbol (to use later in the file)
193 * @_type: Data type of the entry
194 * @_list: Name of the list in which this entry is placed
195 *
196 * The name of the (new) symbol is arbitrary and can be anything that is not
197 * already declared in the file where it appears. It is provided in _sym and
198 * can then be used (later in the same file) within a data structure.
199 * The _type and _list arguments must match those passed to ll_entry_start/end()
200 *
201 * Example:
202 *
203 * Here we want to record the start of each sub-command in a list. We have two
204 * sub-commands, 'bob' and 'mary'.
205 *
206 * In bob.c::
207 *
208 * ll_entry_declare(struct my_sub_cmd, bob_cmd, cmd_sub) = {...};
209 *
210 * In mary.c::
211 *
212 * ll_entry_declare(struct my_sub_cmd, mary_cmd, cmd_sub) = {...};
213 *
214 * In a different file where we want a list the start of all sub-commands.
215 * It is not possible to use ll_entry_start() in a data structure, due to its
216 * use of code inside expressions - ({ ... }) - so this fails to compile:
217 *
218 * In sub_cmds.c::
219 *
220 * struct cmd_sub *my_list[] = {
221 * ll_entry_start(cmd_sub, bob),
222 * ll_entry_start(cmd_sub, bob),
223 * };
224 *
225 * Instead, we can use::
226 *
227 * ll_start_decl(bob, struct my_sub_cmd, cmd_sub);
228 * ll_start_decl(mary, struct my_sub_cmd, cmd_sub);
229 *
230 * struct cmd_sub *my_list[] = {
231 * bob,
232 * mary,
233 * };
234 *
235 * So 'bob' is declared as symbol, a struct my_list * which points to the
236 * start of the bob sub-commands. It is then used in my_list[]
237 */
238#define ll_start_decl(_sym, _type, _list) \
239 static _type _sym[0] __aligned(CONFIG_LINKER_LIST_ALIGN) \
240 __maybe_unused __section("__u_boot_list_2_" #_list "_1")
241
242#define ll_end_decl(_sym, _type, _list) \
243 static _type _sym[0] __aligned(CONFIG_LINKER_LIST_ALIGN) \
244 __maybe_unused __section("__u_boot_list_2_" #_list "_3")
245
246/**
Marek Vasut9b8899d2012-10-12 10:27:02 +0000247 * ll_entry_get() - Retrieve entry from linker-generated array by name
248 * @_type: Data type of the entry
249 * @_name: Name of the entry
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000250 * @_list: Name of the list in which this entry is placed
Marek Vasut9b8899d2012-10-12 10:27:02 +0000251 *
Bin Meng02a31b72015-07-19 00:20:02 +0800252 * This function returns a pointer to a particular entry in linker-generated
253 * array identified by the subsection of u_boot_list where the entry resides
Marek Vasut9b8899d2012-10-12 10:27:02 +0000254 * and it's name.
255 *
256 * Example:
Mario Six8fac2912018-07-10 08:40:17 +0200257 *
258 * ::
259 *
260 * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = {
261 * .x = 3,
262 * .y = 4,
263 * };
264 * ...
265 * struct my_sub_cmd *c = ll_entry_get(struct my_sub_cmd, my_sub_cmd, cmd_sub);
Marek Vasut9b8899d2012-10-12 10:27:02 +0000266 */
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000267#define ll_entry_get(_type, _name, _list) \
Marek Vasut9b8899d2012-10-12 10:27:02 +0000268 ({ \
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000269 extern _type _u_boot_list_2_##_list##_2_##_name; \
270 _type *_ll_result = \
Bin Meng02a31b72015-07-19 00:20:02 +0800271 &_u_boot_list_2_##_list##_2_##_name; \
Marek Vasut9b8899d2012-10-12 10:27:02 +0000272 _ll_result; \
273 })
274
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000275/**
Simon Glass735ccf92021-03-15 17:25:13 +1300276 * ll_entry_ref() - Get a reference to a linker-generated array entry
277 *
Simon Glass70743542021-03-15 17:25:14 +1300278 * Once an extern ll_entry_declare() has been used to declare the reference,
279 * this macro allows the entry to be accessed.
Simon Glass735ccf92021-03-15 17:25:13 +1300280 *
281 * This is like ll_entry_get(), but without the extra code, so it is suitable
282 * for putting into data structures.
Heinrich Schuchardt48863132021-03-28 10:53:18 +0200283 *
284 * @_type: C type of the list entry, e.g. 'struct foo'
285 * @_name: name of the entry
286 * @_list: name of the list
Simon Glass735ccf92021-03-15 17:25:13 +1300287 */
288#define ll_entry_ref(_type, _name, _list) \
289 ((_type *)&_u_boot_list_2_##_list##_2_##_name)
290
291/**
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000292 * ll_start() - Point to first entry of first linker-generated array
293 * @_type: Data type of the entry
294 *
Mario Six8fac2912018-07-10 08:40:17 +0200295 * This function returns ``(_type *)`` pointer to the very first entry of
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000296 * the very first linker-generated array.
297 *
298 * Since this macro defines the start of the linker-generated arrays,
299 * its leftmost index must be 1.
300 *
301 * Example:
Mario Six8fac2912018-07-10 08:40:17 +0200302 *
303 * ::
304 *
305 * struct my_sub_cmd *msc = ll_start(struct my_sub_cmd);
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000306 */
307#define ll_start(_type) \
308({ \
Marek Behún4bebdd32021-05-20 13:23:52 +0200309 static char start[0] __aligned(4) __attribute__((unused)) \
Andrew Scull5a9095c2022-05-30 10:00:04 +0000310 __section("__u_boot_list_1"); \
Tom Rini04b5de32023-03-28 14:54:51 -0400311 _type * tmp = (_type *)&start; \
312 asm("":"+r"(tmp)); \
313 tmp; \
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000314})
315
316/**
Bin Meng02a31b72015-07-19 00:20:02 +0800317 * ll_end() - Point after last entry of last linker-generated array
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000318 * @_type: Data type of the entry
319 *
Mario Six8fac2912018-07-10 08:40:17 +0200320 * This function returns ``(_type *)`` pointer after the very last entry of
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000321 * the very last linker-generated array.
322 *
323 * Since this macro defines the end of the linker-generated arrays,
324 * its leftmost index must be 3.
325 *
326 * Example:
Mario Six8fac2912018-07-10 08:40:17 +0200327 *
328 * ::
329 *
330 * struct my_sub_cmd *msc = ll_end(struct my_sub_cmd);
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000331 */
332#define ll_end(_type) \
333({ \
Marek Behún4bebdd32021-05-20 13:23:52 +0200334 static char end[0] __aligned(4) __attribute__((unused)) \
Andrew Scull5a9095c2022-05-30 10:00:04 +0000335 __section("__u_boot_list_3"); \
Tom Rini04b5de32023-03-28 14:54:51 -0400336 _type * tmp = (_type *)&end; \
337 asm("":"+r"(tmp)); \
338 tmp; \
Albert ARIBAUDc24895e2013-02-25 00:59:00 +0000339})
340
341#endif /* __ASSEMBLY__ */
342
Marek Vasut9b8899d2012-10-12 10:27:02 +0000343#endif /* __LINKER_LISTS_H__ */