xref: /rk3399_rockchip-uboot/include/linker_lists.h (revision d72fd7b3405b9696316426a0b64316cd99ea0c7f)
142ebaae3SMarek Vasut /*
242ebaae3SMarek Vasut  * include/linker_lists.h
342ebaae3SMarek Vasut  *
442ebaae3SMarek Vasut  * Implementation of linker-generated arrays
542ebaae3SMarek Vasut  *
642ebaae3SMarek Vasut  * Copyright (C) 2012 Marek Vasut <marex@denx.de>
742ebaae3SMarek Vasut  *
81a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
942ebaae3SMarek Vasut  */
10ef123c52SAlbert ARIBAUD 
11babb4440SMasahiro Yamada #ifndef __LINKER_LISTS_H__
12babb4440SMasahiro Yamada #define __LINKER_LISTS_H__
13babb4440SMasahiro Yamada 
14dc7cb46fSMasahiro Yamada #include <linux/compiler.h>
15dc7cb46fSMasahiro Yamada 
16ef123c52SAlbert ARIBAUD /*
17*d72fd7b3SHeinrich Schuchardt  * There is no use in including this from ASM files.
18ef123c52SAlbert ARIBAUD  * So just don't define anything when included from ASM.
19ef123c52SAlbert ARIBAUD  */
20ef123c52SAlbert ARIBAUD 
21ef123c52SAlbert ARIBAUD #if !defined(__ASSEMBLY__)
22ef123c52SAlbert ARIBAUD 
23ef123c52SAlbert ARIBAUD /**
24ef123c52SAlbert ARIBAUD  * A linker list is constructed by grouping together linker input
2544f145fdSGuilherme Maciel Ferreira  * sections, each containing one entry of the list. Each input section
26ef123c52SAlbert ARIBAUD  * contains a constant initialized variable which holds the entry's
27ef123c52SAlbert ARIBAUD  * content. Linker list input sections are constructed from the list
28ef123c52SAlbert ARIBAUD  * and entry names, plus a prefix which allows grouping all lists
29ef123c52SAlbert ARIBAUD  * together. Assuming _list and _entry are the list and entry names,
30ef123c52SAlbert ARIBAUD  * then the corresponding input section name is
31ef123c52SAlbert ARIBAUD  *
3297d5e9d1SMasahiro Yamada  *   .u_boot_list_ + 2_ + @_list + _2_ + @_entry
33ef123c52SAlbert ARIBAUD  *
34ef123c52SAlbert ARIBAUD  * and the C variable name is
35ef123c52SAlbert ARIBAUD  *
3697d5e9d1SMasahiro Yamada  *   _u_boot_list + _2_ + @_list + _2_ + @_entry
37ef123c52SAlbert ARIBAUD  *
38ef123c52SAlbert ARIBAUD  * This ensures uniqueness for both input section and C variable name.
39ef123c52SAlbert ARIBAUD  *
40ef123c52SAlbert ARIBAUD  * Note that the names differ only in the first character, "." for the
4144f145fdSGuilherme Maciel Ferreira  * section and "_" for the variable, so that the linker cannot confuse
42ef123c52SAlbert ARIBAUD  * section and symbol names. From now on, both names will be referred
43ef123c52SAlbert ARIBAUD  * to as
44ef123c52SAlbert ARIBAUD  *
45ef123c52SAlbert ARIBAUD  *   %u_boot_list_ + 2_ + @_list + _2_ + @_entry
46ef123c52SAlbert ARIBAUD  *
47ef123c52SAlbert ARIBAUD  * Entry variables need never be referred to directly.
48ef123c52SAlbert ARIBAUD  *
49ef123c52SAlbert ARIBAUD  * The naming scheme for input sections allows grouping all linker lists
50ef123c52SAlbert ARIBAUD  * into a single linker output section and grouping all entries for a
51ef123c52SAlbert ARIBAUD  * single list.
52ef123c52SAlbert ARIBAUD  *
53ef123c52SAlbert ARIBAUD  * Note the two '_2_' constant components in the names: their presence
54ef123c52SAlbert ARIBAUD  * allows putting a start and end symbols around a list, by mapping
55ef123c52SAlbert ARIBAUD  * these symbols to sections names with components "1" (before) and
56ef123c52SAlbert ARIBAUD  * "3" (after) instead of "2" (within).
57ef123c52SAlbert ARIBAUD  * Start and end symbols for a list can generally be defined as
58ef123c52SAlbert ARIBAUD  *
59ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_ + @_list + _1_...
60ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_ + @_list + _3_...
61ef123c52SAlbert ARIBAUD  *
62ef123c52SAlbert ARIBAUD  * Start and end symbols for the whole of the linker lists area can be
63ef123c52SAlbert ARIBAUD  * defined as
64ef123c52SAlbert ARIBAUD  *
65ef123c52SAlbert ARIBAUD  *   %u_boot_list_1_...
66ef123c52SAlbert ARIBAUD  *   %u_boot_list_3_...
67ef123c52SAlbert ARIBAUD  *
68ef123c52SAlbert ARIBAUD  * Here is an example of the sorted sections which result from a list
69ef123c52SAlbert ARIBAUD  * "array" made up of three entries : "first", "second" and "third",
70ef123c52SAlbert ARIBAUD  * iterated at least once.
71ef123c52SAlbert ARIBAUD  *
72ef123c52SAlbert ARIBAUD  *   .u_boot_list_2_array_1
73ef123c52SAlbert ARIBAUD  *   .u_boot_list_2_array_2_first
74ef123c52SAlbert ARIBAUD  *   .u_boot_list_2_array_2_second
75ef123c52SAlbert ARIBAUD  *   .u_boot_list_2_array_2_third
76ef123c52SAlbert ARIBAUD  *   .u_boot_list_2_array_3
77ef123c52SAlbert ARIBAUD  *
78ef123c52SAlbert ARIBAUD  * If lists must be divided into sublists (e.g. for iterating only on
79ef123c52SAlbert ARIBAUD  * part of a list), one can simply give the list a name of the form
80ef123c52SAlbert ARIBAUD  * 'outer_2_inner', where 'outer' is the global list name and 'inner'
81ef123c52SAlbert ARIBAUD  * is the sub-list name. Iterators for the whole list should use the
82ef123c52SAlbert ARIBAUD  * global list name ("outer"); iterators for only a sub-list should use
83ef123c52SAlbert ARIBAUD  * the full sub-list name ("outer_2_inner").
84ef123c52SAlbert ARIBAUD  *
85ef123c52SAlbert ARIBAUD  * Here is an example of the sections generated from a global list
86ef123c52SAlbert ARIBAUD  * named "drivers", two sub-lists named "i2c" and "pci", and iterators
87ef123c52SAlbert ARIBAUD  * defined for the whole list and each sub-list:
88ef123c52SAlbert ARIBAUD  *
89ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_1
90ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_i2c_1
91ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_i2c_2_first
92ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_i2c_2_first
93ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_i2c_2_second
94ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_i2c_2_third
95ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_i2c_3
96ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_pci_1
97ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_pci_2_first
98ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_pci_2_second
99ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_pci_2_third
100ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_pci_3
101ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_3
102ef123c52SAlbert ARIBAUD  */
103ef123c52SAlbert ARIBAUD 
10442ebaae3SMarek Vasut /**
1057e378b8bSBin Meng  * llsym() - Access a linker-generated array entry
10672538f4cSSimon Glass  * @_type:	Data type of the entry
10772538f4cSSimon Glass  * @_name:	Name of the entry
10872538f4cSSimon Glass  * @_list:	name of the list. Should contain only characters allowed
10972538f4cSSimon Glass  *		in a C variable name!
11072538f4cSSimon Glass  */
11172538f4cSSimon Glass #define llsym(_type, _name, _list) \
11272538f4cSSimon Glass 		((_type *)&_u_boot_list_2_##_list##_2_##_name)
11372538f4cSSimon Glass 
11472538f4cSSimon Glass /**
11542ebaae3SMarek Vasut  * ll_entry_declare() - Declare linker-generated array entry
11642ebaae3SMarek Vasut  * @_type:	Data type of the entry
11742ebaae3SMarek Vasut  * @_name:	Name of the entry
118ef123c52SAlbert ARIBAUD  * @_list:	name of the list. Should contain only characters allowed
119ef123c52SAlbert ARIBAUD  *		in a C variable name!
12042ebaae3SMarek Vasut  *
12142ebaae3SMarek Vasut  * This macro declares a variable that is placed into a linker-generated
12242ebaae3SMarek Vasut  * array. This is a basic building block for more advanced use of linker-
12342ebaae3SMarek Vasut  * generated arrays. The user is expected to build their own macro wrapper
12442ebaae3SMarek Vasut  * around this one.
12542ebaae3SMarek Vasut  *
126ef123c52SAlbert ARIBAUD  * A variable declared using this macro must be compile-time initialized.
12742ebaae3SMarek Vasut  *
12842ebaae3SMarek Vasut  * Special precaution must be made when using this macro:
12942ebaae3SMarek Vasut  *
130ef123c52SAlbert ARIBAUD  * 1) The _type must not contain the "static" keyword, otherwise the
131ef123c52SAlbert ARIBAUD  *    entry is generated and can be iterated but is listed in the map
132ef123c52SAlbert ARIBAUD  *    file and cannot be retrieved by name.
13342ebaae3SMarek Vasut  *
134ef123c52SAlbert ARIBAUD  * 2) In case a section is declared that contains some array elements AND
135ef123c52SAlbert ARIBAUD  *    a subsection of this section is declared and contains some elements,
136ef123c52SAlbert ARIBAUD  *    it is imperative that the elements are of the same type.
13742ebaae3SMarek Vasut  *
13842ebaae3SMarek Vasut  * 4) In case an outer section is declared that contains some array elements
139ef123c52SAlbert ARIBAUD  *    AND an inner subsection of this section is declared and contains some
14042ebaae3SMarek Vasut  *    elements, then when traversing the outer section, even the elements of
14142ebaae3SMarek Vasut  *    the inner sections are present in the array.
14242ebaae3SMarek Vasut  *
14342ebaae3SMarek Vasut  * Example:
1447e378b8bSBin Meng  * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = {
14542ebaae3SMarek Vasut  *         .x = 3,
14642ebaae3SMarek Vasut  *         .y = 4,
14742ebaae3SMarek Vasut  * };
14842ebaae3SMarek Vasut  */
149ef123c52SAlbert ARIBAUD #define ll_entry_declare(_type, _name, _list)				\
150ef123c52SAlbert ARIBAUD 	_type _u_boot_list_2_##_list##_2_##_name __aligned(4)		\
151ef123c52SAlbert ARIBAUD 			__attribute__((unused,				\
152ef123c52SAlbert ARIBAUD 			section(".u_boot_list_2_"#_list"_2_"#_name)))
153ef123c52SAlbert ARIBAUD 
154ef123c52SAlbert ARIBAUD /**
1553fcc3af4SSimon Glass  * ll_entry_declare_list() - Declare a list of link-generated array entries
1563fcc3af4SSimon Glass  * @_type:	Data type of each entry
1573fcc3af4SSimon Glass  * @_name:	Name of the entry
1583fcc3af4SSimon Glass  * @_list:	name of the list. Should contain only characters allowed
1593fcc3af4SSimon Glass  *		in a C variable name!
1603fcc3af4SSimon Glass  *
1613fcc3af4SSimon Glass  * This is like ll_entry_declare() but creates multiple entries. It should
1623fcc3af4SSimon Glass  * be assigned to an array.
1633fcc3af4SSimon Glass  *
1647e378b8bSBin Meng  * ll_entry_declare_list(struct my_sub_cmd, my_sub_cmd, cmd_sub) = {
1653fcc3af4SSimon Glass  *	{ .x = 3, .y = 4 },
1663fcc3af4SSimon Glass  *	{ .x = 8, .y = 2 },
1673fcc3af4SSimon Glass  *	{ .x = 1, .y = 7 }
1683fcc3af4SSimon Glass  * };
1693fcc3af4SSimon Glass  */
1703fcc3af4SSimon Glass #define ll_entry_declare_list(_type, _name, _list)			\
1713fcc3af4SSimon Glass 	_type _u_boot_list_2_##_list##_2_##_name[] __aligned(4)		\
1723fcc3af4SSimon Glass 			__attribute__((unused,				\
1733fcc3af4SSimon Glass 			section(".u_boot_list_2_"#_list"_2_"#_name)))
1743fcc3af4SSimon Glass 
1753fcc3af4SSimon Glass /**
176ef123c52SAlbert ARIBAUD  * We need a 0-byte-size type for iterator symbols, and the compiler
177ef123c52SAlbert ARIBAUD  * does not allow defining objects of C type 'void'. Using an empty
178ef123c52SAlbert ARIBAUD  * struct is allowed by the compiler, but causes gcc versions 4.4 and
179ef123c52SAlbert ARIBAUD  * below to complain about aliasing. Therefore we use the next best
180ef123c52SAlbert ARIBAUD  * thing: zero-sized arrays, which are both 0-byte-size and exempt from
181ef123c52SAlbert ARIBAUD  * aliasing warnings.
182ef123c52SAlbert ARIBAUD  */
18342ebaae3SMarek Vasut 
18442ebaae3SMarek Vasut /**
18542ebaae3SMarek Vasut  * ll_entry_start() - Point to first entry of linker-generated array
18642ebaae3SMarek Vasut  * @_type:	Data type of the entry
187ef123c52SAlbert ARIBAUD  * @_list:	Name of the list in which this entry is placed
18842ebaae3SMarek Vasut  *
18942ebaae3SMarek Vasut  * This function returns (_type *) pointer to the very first entry of a
19042ebaae3SMarek Vasut  * linker-generated array placed into subsection of .u_boot_list section
191ef123c52SAlbert ARIBAUD  * specified by _list argument.
192ef123c52SAlbert ARIBAUD  *
193ef123c52SAlbert ARIBAUD  * Since this macro defines an array start symbol, its leftmost index
194ef123c52SAlbert ARIBAUD  * must be 2 and its rightmost index must be 1.
19542ebaae3SMarek Vasut  *
19642ebaae3SMarek Vasut  * Example:
19742ebaae3SMarek Vasut  * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub);
19842ebaae3SMarek Vasut  */
199ef123c52SAlbert ARIBAUD #define ll_entry_start(_type, _list)					\
20042ebaae3SMarek Vasut ({									\
201ef123c52SAlbert ARIBAUD 	static char start[0] __aligned(4) __attribute__((unused,	\
202ef123c52SAlbert ARIBAUD 		section(".u_boot_list_2_"#_list"_1")));			\
203ef123c52SAlbert ARIBAUD 	(_type *)&start;						\
20442ebaae3SMarek Vasut })
20542ebaae3SMarek Vasut 
20642ebaae3SMarek Vasut /**
207ef123c52SAlbert ARIBAUD  * ll_entry_end() - Point after last entry of linker-generated array
20842ebaae3SMarek Vasut  * @_type:	Data type of the entry
209ef123c52SAlbert ARIBAUD  * @_list:	Name of the list in which this entry is placed
21042ebaae3SMarek Vasut  *		(with underscores instead of dots)
21142ebaae3SMarek Vasut  *
212ef123c52SAlbert ARIBAUD  * This function returns (_type *) pointer after the very last entry of
213ef123c52SAlbert ARIBAUD  * a linker-generated array placed into subsection of .u_boot_list
214ef123c52SAlbert ARIBAUD  * section specified by _list argument.
215ef123c52SAlbert ARIBAUD  *
216ef123c52SAlbert ARIBAUD  * Since this macro defines an array end symbol, its leftmost index
217ef123c52SAlbert ARIBAUD  * must be 2 and its rightmost index must be 3.
218ef123c52SAlbert ARIBAUD  *
219ef123c52SAlbert ARIBAUD  * Example:
220ef123c52SAlbert ARIBAUD  * struct my_sub_cmd *msc = ll_entry_end(struct my_sub_cmd, cmd_sub);
221ef123c52SAlbert ARIBAUD  */
222ef123c52SAlbert ARIBAUD #define ll_entry_end(_type, _list)					\
223ef123c52SAlbert ARIBAUD ({									\
224ef123c52SAlbert ARIBAUD 	static char end[0] __aligned(4) __attribute__((unused,		\
225ef123c52SAlbert ARIBAUD 		section(".u_boot_list_2_"#_list"_3")));			\
226ef123c52SAlbert ARIBAUD 	(_type *)&end;							\
227ef123c52SAlbert ARIBAUD })
228ef123c52SAlbert ARIBAUD /**
229ef123c52SAlbert ARIBAUD  * ll_entry_count() - Return the number of elements in linker-generated array
230ef123c52SAlbert ARIBAUD  * @_type:	Data type of the entry
231ef123c52SAlbert ARIBAUD  * @_list:	Name of the list of which the number of elements is computed
232ef123c52SAlbert ARIBAUD  *
23342ebaae3SMarek Vasut  * This function returns the number of elements of a linker-generated array
234ef123c52SAlbert ARIBAUD  * placed into subsection of .u_boot_list section specified by _list
23542ebaae3SMarek Vasut  * argument. The result is of an unsigned int type.
23642ebaae3SMarek Vasut  *
23742ebaae3SMarek Vasut  * Example:
23842ebaae3SMarek Vasut  * int i;
23942ebaae3SMarek Vasut  * const unsigned int count = ll_entry_count(struct my_sub_cmd, cmd_sub);
24042ebaae3SMarek Vasut  * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub);
24142ebaae3SMarek Vasut  * for (i = 0; i < count; i++, msc++)
24242ebaae3SMarek Vasut  *         printf("Entry %i, x=%i y=%i\n", i, msc->x, msc->y);
24342ebaae3SMarek Vasut  */
244ef123c52SAlbert ARIBAUD #define ll_entry_count(_type, _list)					\
24542ebaae3SMarek Vasut 	({								\
246ef123c52SAlbert ARIBAUD 		_type *start = ll_entry_start(_type, _list);		\
247ef123c52SAlbert ARIBAUD 		_type *end = ll_entry_end(_type, _list);		\
248ef123c52SAlbert ARIBAUD 		unsigned int _ll_result = end - start;			\
24942ebaae3SMarek Vasut 		_ll_result;						\
25042ebaae3SMarek Vasut 	})
25142ebaae3SMarek Vasut 
25242ebaae3SMarek Vasut /**
25342ebaae3SMarek Vasut  * ll_entry_get() - Retrieve entry from linker-generated array by name
25442ebaae3SMarek Vasut  * @_type:	Data type of the entry
25542ebaae3SMarek Vasut  * @_name:	Name of the entry
256ef123c52SAlbert ARIBAUD  * @_list:	Name of the list in which this entry is placed
25742ebaae3SMarek Vasut  *
2587e378b8bSBin Meng  * This function returns a pointer to a particular entry in linker-generated
2597e378b8bSBin Meng  * array identified by the subsection of u_boot_list where the entry resides
26042ebaae3SMarek Vasut  * and it's name.
26142ebaae3SMarek Vasut  *
26242ebaae3SMarek Vasut  * Example:
263af41d6b4SMateusz Zalega  * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = {
26442ebaae3SMarek Vasut  *         .x = 3,
26542ebaae3SMarek Vasut  *         .y = 4,
26642ebaae3SMarek Vasut  * };
26742ebaae3SMarek Vasut  * ...
26842ebaae3SMarek Vasut  * struct my_sub_cmd *c = ll_entry_get(struct my_sub_cmd, my_sub_cmd, cmd_sub);
26942ebaae3SMarek Vasut  */
270ef123c52SAlbert ARIBAUD #define ll_entry_get(_type, _name, _list)				\
27142ebaae3SMarek Vasut 	({								\
272ef123c52SAlbert ARIBAUD 		extern _type _u_boot_list_2_##_list##_2_##_name;	\
273ef123c52SAlbert ARIBAUD 		_type *_ll_result =					\
274ef123c52SAlbert ARIBAUD 			&_u_boot_list_2_##_list##_2_##_name;		\
27542ebaae3SMarek Vasut 		_ll_result;						\
27642ebaae3SMarek Vasut 	})
27742ebaae3SMarek Vasut 
278ef123c52SAlbert ARIBAUD /**
279ef123c52SAlbert ARIBAUD  * ll_start() - Point to first entry of first linker-generated array
280ef123c52SAlbert ARIBAUD  * @_type:	Data type of the entry
281ef123c52SAlbert ARIBAUD  *
282ef123c52SAlbert ARIBAUD  * This function returns (_type *) pointer to the very first entry of
283ef123c52SAlbert ARIBAUD  * the very first linker-generated array.
284ef123c52SAlbert ARIBAUD  *
285ef123c52SAlbert ARIBAUD  * Since this macro defines the start of the linker-generated arrays,
286ef123c52SAlbert ARIBAUD  * its leftmost index must be 1.
287ef123c52SAlbert ARIBAUD  *
288ef123c52SAlbert ARIBAUD  * Example:
289ef123c52SAlbert ARIBAUD  * struct my_sub_cmd *msc = ll_start(struct my_sub_cmd);
290ef123c52SAlbert ARIBAUD  */
291ef123c52SAlbert ARIBAUD #define ll_start(_type)							\
292ef123c52SAlbert ARIBAUD ({									\
293ef123c52SAlbert ARIBAUD 	static char start[0] __aligned(4) __attribute__((unused,	\
294ef123c52SAlbert ARIBAUD 		section(".u_boot_list_1")));				\
295ef123c52SAlbert ARIBAUD 	(_type *)&start;						\
296ef123c52SAlbert ARIBAUD })
297ef123c52SAlbert ARIBAUD 
298ef123c52SAlbert ARIBAUD /**
2997e378b8bSBin Meng  * ll_end() - Point after last entry of last linker-generated array
300ef123c52SAlbert ARIBAUD  * @_type:	Data type of the entry
301ef123c52SAlbert ARIBAUD  *
302ef123c52SAlbert ARIBAUD  * This function returns (_type *) pointer after the very last entry of
303ef123c52SAlbert ARIBAUD  * the very last linker-generated array.
304ef123c52SAlbert ARIBAUD  *
305ef123c52SAlbert ARIBAUD  * Since this macro defines the end of the linker-generated arrays,
306ef123c52SAlbert ARIBAUD  * its leftmost index must be 3.
307ef123c52SAlbert ARIBAUD  *
308ef123c52SAlbert ARIBAUD  * Example:
309ef123c52SAlbert ARIBAUD  * struct my_sub_cmd *msc = ll_end(struct my_sub_cmd);
310ef123c52SAlbert ARIBAUD  */
311ef123c52SAlbert ARIBAUD #define ll_end(_type)							\
312ef123c52SAlbert ARIBAUD ({									\
313ef123c52SAlbert ARIBAUD 	static char end[0] __aligned(4) __attribute__((unused,		\
314ef123c52SAlbert ARIBAUD 		section(".u_boot_list_3")));				\
315ef123c52SAlbert ARIBAUD 	(_type *)&end;							\
316ef123c52SAlbert ARIBAUD })
317ef123c52SAlbert ARIBAUD 
318ef123c52SAlbert ARIBAUD #endif /* __ASSEMBLY__ */
319ef123c52SAlbert ARIBAUD 
32042ebaae3SMarek Vasut #endif	/* __LINKER_LISTS_H__ */
321