blob: 1eebb95fbb9b7efb9c01a3cbb57303875baf9bae [file] [log] [blame]
Marek Vasut42ebaae2012-10-12 10:27:02 +00001/*
2 * include/linker_lists.h
3 *
4 * Implementation of linker-generated arrays
5 *
6 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Marek Vasut42ebaae2012-10-12 10:27:02 +00009 */
Albert ARIBAUDef123c52013-02-25 00:59:00 +000010
11/*
12 * There is no use in including this from ASM files, but that happens
13 * anyway, e.g. PPC kgdb.S includes command.h which incluse us.
14 * So just don't define anything when included from ASM.
15 */
16
17#if !defined(__ASSEMBLY__)
18
19/**
20 * A linker list is constructed by grouping together linker input
21 * sections, each containning one entry of the list. Each input section
22 * contains a constant initialized variable which holds the entry's
23 * content. Linker list input sections are constructed from the list
24 * and entry names, plus a prefix which allows grouping all lists
25 * together. Assuming _list and _entry are the list and entry names,
26 * then the corresponding input section name is
27 *
28 * _u_boot_list + _2_ + @_list + _2_ + @_entry
29 *
30 * and the C variable name is
31 *
32 * .u_boot_list_ + 2_ + @_list + _2_ + @_entry
33 *
34 * This ensures uniqueness for both input section and C variable name.
35 *
36 * Note that the names differ only in the first character, "." for the
37 * setion and "_" for the variable, so that the linker cannot confuse
38 * section and symbol names. From now on, both names will be referred
39 * to as
40 *
41 * %u_boot_list_ + 2_ + @_list + _2_ + @_entry
42 *
43 * Entry variables need never be referred to directly.
44 *
45 * The naming scheme for input sections allows grouping all linker lists
46 * into a single linker output section and grouping all entries for a
47 * single list.
48 *
49 * Note the two '_2_' constant components in the names: their presence
50 * allows putting a start and end symbols around a list, by mapping
51 * these symbols to sections names with components "1" (before) and
52 * "3" (after) instead of "2" (within).
53 * Start and end symbols for a list can generally be defined as
54 *
55 * %u_boot_list_2_ + @_list + _1_...
56 * %u_boot_list_2_ + @_list + _3_...
57 *
58 * Start and end symbols for the whole of the linker lists area can be
59 * defined as
60 *
61 * %u_boot_list_1_...
62 * %u_boot_list_3_...
63 *
64 * Here is an example of the sorted sections which result from a list
65 * "array" made up of three entries : "first", "second" and "third",
66 * iterated at least once.
67 *
68 * .u_boot_list_2_array_1
69 * .u_boot_list_2_array_2_first
70 * .u_boot_list_2_array_2_second
71 * .u_boot_list_2_array_2_third
72 * .u_boot_list_2_array_3
73 *
74 * If lists must be divided into sublists (e.g. for iterating only on
75 * part of a list), one can simply give the list a name of the form
76 * 'outer_2_inner', where 'outer' is the global list name and 'inner'
77 * is the sub-list name. Iterators for the whole list should use the
78 * global list name ("outer"); iterators for only a sub-list should use
79 * the full sub-list name ("outer_2_inner").
80 *
81 * Here is an example of the sections generated from a global list
82 * named "drivers", two sub-lists named "i2c" and "pci", and iterators
83 * defined for the whole list and each sub-list:
84 *
85 * %u_boot_list_2_drivers_1
86 * %u_boot_list_2_drivers_2_i2c_1
87 * %u_boot_list_2_drivers_2_i2c_2_first
88 * %u_boot_list_2_drivers_2_i2c_2_first
89 * %u_boot_list_2_drivers_2_i2c_2_second
90 * %u_boot_list_2_drivers_2_i2c_2_third
91 * %u_boot_list_2_drivers_2_i2c_3
92 * %u_boot_list_2_drivers_2_pci_1
93 * %u_boot_list_2_drivers_2_pci_2_first
94 * %u_boot_list_2_drivers_2_pci_2_second
95 * %u_boot_list_2_drivers_2_pci_2_third
96 * %u_boot_list_2_drivers_2_pci_3
97 * %u_boot_list_2_drivers_3
98 */
99
Marek Vasut42ebaae2012-10-12 10:27:02 +0000100#ifndef __LINKER_LISTS_H__
101#define __LINKER_LISTS_H__
102
103/**
104 * ll_entry_declare() - Declare linker-generated array entry
105 * @_type: Data type of the entry
106 * @_name: Name of the entry
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000107 * @_list: name of the list. Should contain only characters allowed
108 * in a C variable name!
Marek Vasut42ebaae2012-10-12 10:27:02 +0000109 *
110 * This macro declares a variable that is placed into a linker-generated
111 * array. This is a basic building block for more advanced use of linker-
112 * generated arrays. The user is expected to build their own macro wrapper
113 * around this one.
114 *
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000115 * A variable declared using this macro must be compile-time initialized.
Marek Vasut42ebaae2012-10-12 10:27:02 +0000116 *
117 * Special precaution must be made when using this macro:
Marek Vasut42ebaae2012-10-12 10:27:02 +0000118 *
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000119 * 1) The _type must not contain the "static" keyword, otherwise the
120 * entry is generated and can be iterated but is listed in the map
121 * file and cannot be retrieved by name.
Marek Vasut42ebaae2012-10-12 10:27:02 +0000122 *
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000123 * 2) In case a section is declared that contains some array elements AND
124 * a subsection of this section is declared and contains some elements,
125 * it is imperative that the elements are of the same type.
Marek Vasut42ebaae2012-10-12 10:27:02 +0000126 *
127 * 4) In case an outer section is declared that contains some array elements
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000128 * AND an inner subsection of this section is declared and contains some
Marek Vasut42ebaae2012-10-12 10:27:02 +0000129 * elements, then when traversing the outer section, even the elements of
130 * the inner sections are present in the array.
131 *
132 * Example:
133 * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub, cmd.sub) = {
134 * .x = 3,
135 * .y = 4,
136 * };
137 */
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000138#define ll_entry_declare(_type, _name, _list) \
139 _type _u_boot_list_2_##_list##_2_##_name __aligned(4) \
140 __attribute__((unused, \
141 section(".u_boot_list_2_"#_list"_2_"#_name)))
142
143/**
144 * We need a 0-byte-size type for iterator symbols, and the compiler
145 * does not allow defining objects of C type 'void'. Using an empty
146 * struct is allowed by the compiler, but causes gcc versions 4.4 and
147 * below to complain about aliasing. Therefore we use the next best
148 * thing: zero-sized arrays, which are both 0-byte-size and exempt from
149 * aliasing warnings.
150 */
Marek Vasut42ebaae2012-10-12 10:27:02 +0000151
152/**
153 * ll_entry_start() - Point to first entry of linker-generated array
154 * @_type: Data type of the entry
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000155 * @_list: Name of the list in which this entry is placed
Marek Vasut42ebaae2012-10-12 10:27:02 +0000156 *
157 * This function returns (_type *) pointer to the very first entry of a
158 * linker-generated array placed into subsection of .u_boot_list section
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000159 * specified by _list argument.
160 *
161 * Since this macro defines an array start symbol, its leftmost index
162 * must be 2 and its rightmost index must be 1.
Marek Vasut42ebaae2012-10-12 10:27:02 +0000163 *
164 * Example:
165 * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub);
166 */
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000167#define ll_entry_start(_type, _list) \
168({ \
169 static char start[0] __aligned(4) __attribute__((unused, \
170 section(".u_boot_list_2_"#_list"_1"))); \
171 (_type *)&start; \
172})
Marek Vasut42ebaae2012-10-12 10:27:02 +0000173
174/**
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000175 * ll_entry_end() - Point after last entry of linker-generated array
176 * @_type: Data type of the entry
177 * @_list: Name of the list in which this entry is placed
178 * (with underscores instead of dots)
179 *
180 * This function returns (_type *) pointer after the very last entry of
181 * a linker-generated array placed into subsection of .u_boot_list
182 * section specified by _list argument.
183 *
184 * Since this macro defines an array end symbol, its leftmost index
185 * must be 2 and its rightmost index must be 3.
186 *
187 * Example:
188 * struct my_sub_cmd *msc = ll_entry_end(struct my_sub_cmd, cmd_sub);
189 */
190#define ll_entry_end(_type, _list) \
191({ \
192 static char end[0] __aligned(4) __attribute__((unused, \
193 section(".u_boot_list_2_"#_list"_3"))); \
194 (_type *)&end; \
195})
196/**
Marek Vasut42ebaae2012-10-12 10:27:02 +0000197 * ll_entry_count() - Return the number of elements in linker-generated array
198 * @_type: Data type of the entry
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000199 * @_list: Name of the list of which the number of elements is computed
Marek Vasut42ebaae2012-10-12 10:27:02 +0000200 *
201 * This function returns the number of elements of a linker-generated array
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000202 * placed into subsection of .u_boot_list section specified by _list
Marek Vasut42ebaae2012-10-12 10:27:02 +0000203 * argument. The result is of an unsigned int type.
204 *
205 * Example:
206 * int i;
207 * const unsigned int count = ll_entry_count(struct my_sub_cmd, cmd_sub);
208 * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub);
209 * for (i = 0; i < count; i++, msc++)
210 * printf("Entry %i, x=%i y=%i\n", i, msc->x, msc->y);
211 */
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000212#define ll_entry_count(_type, _list) \
Marek Vasut42ebaae2012-10-12 10:27:02 +0000213 ({ \
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000214 _type *start = ll_entry_start(_type, _list); \
215 _type *end = ll_entry_end(_type, _list); \
216 unsigned int _ll_result = end - start; \
Marek Vasut42ebaae2012-10-12 10:27:02 +0000217 _ll_result; \
218 })
219
Marek Vasut42ebaae2012-10-12 10:27:02 +0000220/**
221 * ll_entry_get() - Retrieve entry from linker-generated array by name
222 * @_type: Data type of the entry
223 * @_name: Name of the entry
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000224 * @_list: Name of the list in which this entry is placed
Marek Vasut42ebaae2012-10-12 10:27:02 +0000225 *
226 * This function returns a pointer to a particular entry in LG-array
227 * identified by the subsection of u_boot_list where the entry resides
228 * and it's name.
229 *
230 * Example:
231 * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub, cmd.sub) = {
232 * .x = 3,
233 * .y = 4,
234 * };
235 * ...
236 * struct my_sub_cmd *c = ll_entry_get(struct my_sub_cmd, my_sub_cmd, cmd_sub);
237 */
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000238#define ll_entry_get(_type, _name, _list) \
Marek Vasut42ebaae2012-10-12 10:27:02 +0000239 ({ \
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000240 extern _type _u_boot_list_2_##_list##_2_##_name; \
241 _type *_ll_result = \
242 &_u_boot_list_2_##_list##_2_##_name; \
Marek Vasut42ebaae2012-10-12 10:27:02 +0000243 _ll_result; \
244 })
245
Albert ARIBAUDef123c52013-02-25 00:59:00 +0000246/**
247 * ll_start() - Point to first entry of first linker-generated array
248 * @_type: Data type of the entry
249 *
250 * This function returns (_type *) pointer to the very first entry of
251 * the very first linker-generated array.
252 *
253 * Since this macro defines the start of the linker-generated arrays,
254 * its leftmost index must be 1.
255 *
256 * Example:
257 * struct my_sub_cmd *msc = ll_start(struct my_sub_cmd);
258 */
259#define ll_start(_type) \
260({ \
261 static char start[0] __aligned(4) __attribute__((unused, \
262 section(".u_boot_list_1"))); \
263 (_type *)&start; \
264})
265
266/**
267 * ll_entry_end() - Point after last entry of last linker-generated array
268 * @_type: Data type of the entry
269 *
270 * This function returns (_type *) pointer after the very last entry of
271 * the very last linker-generated array.
272 *
273 * Since this macro defines the end of the linker-generated arrays,
274 * its leftmost index must be 3.
275 *
276 * Example:
277 * struct my_sub_cmd *msc = ll_end(struct my_sub_cmd);
278 */
279#define ll_end(_type) \
280({ \
281 static char end[0] __aligned(4) __attribute__((unused, \
282 section(".u_boot_list_3"))); \
283 (_type *)&end; \
284})
285
286#endif /* __ASSEMBLY__ */
287
Marek Vasut42ebaae2012-10-12 10:27:02 +0000288#endif /* __LINKER_LISTS_H__ */