xref: /rk3399_rockchip-uboot/include/linker_lists.h (revision ef123c525370463254a6f8e67563fdb0b0b46412)
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  *
842ebaae3SMarek Vasut  * See file CREDITS for list of people who contributed to this
942ebaae3SMarek Vasut  * project.
1042ebaae3SMarek Vasut  *
1142ebaae3SMarek Vasut  * This program is free software; you can redistribute it and/or
1242ebaae3SMarek Vasut  * modify it under the terms of the GNU General Public License as
1342ebaae3SMarek Vasut  * published by the Free Software Foundation; either version 2 of
1442ebaae3SMarek Vasut  * the License, or (at your option) any later version.
1542ebaae3SMarek Vasut  */
16*ef123c52SAlbert ARIBAUD 
17*ef123c52SAlbert ARIBAUD /*
18*ef123c52SAlbert ARIBAUD  * There is no use in including this from ASM files, but that happens
19*ef123c52SAlbert ARIBAUD  * anyway, e.g. PPC kgdb.S includes command.h which incluse us.
20*ef123c52SAlbert ARIBAUD  * So just don't define anything when included from ASM.
21*ef123c52SAlbert ARIBAUD  */
22*ef123c52SAlbert ARIBAUD 
23*ef123c52SAlbert ARIBAUD #if !defined(__ASSEMBLY__)
24*ef123c52SAlbert ARIBAUD 
25*ef123c52SAlbert ARIBAUD /**
26*ef123c52SAlbert ARIBAUD  * A linker list is constructed by grouping together linker input
27*ef123c52SAlbert ARIBAUD  * sections, each containning one entry of the list. Each input section
28*ef123c52SAlbert ARIBAUD  * contains a constant initialized variable which holds the entry's
29*ef123c52SAlbert ARIBAUD  * content. Linker list input sections are constructed from the list
30*ef123c52SAlbert ARIBAUD  * and entry names, plus a prefix which allows grouping all lists
31*ef123c52SAlbert ARIBAUD  * together. Assuming _list and _entry are the list and entry names,
32*ef123c52SAlbert ARIBAUD  * then the corresponding input section name is
33*ef123c52SAlbert ARIBAUD  *
34*ef123c52SAlbert ARIBAUD  *   _u_boot_list + _2_ + @_list + _2_ + @_entry
35*ef123c52SAlbert ARIBAUD  *
36*ef123c52SAlbert ARIBAUD  * and the C variable name is
37*ef123c52SAlbert ARIBAUD  *
38*ef123c52SAlbert ARIBAUD  *   .u_boot_list_ + 2_ + @_list + _2_ + @_entry
39*ef123c52SAlbert ARIBAUD  *
40*ef123c52SAlbert ARIBAUD  * This ensures uniqueness for both input section and C variable name.
41*ef123c52SAlbert ARIBAUD  *
42*ef123c52SAlbert ARIBAUD  * Note that the names differ only in the first character, "." for the
43*ef123c52SAlbert ARIBAUD  * setion and "_" for the variable, so that the linker cannot confuse
44*ef123c52SAlbert ARIBAUD  * section and symbol names. From now on, both names will be referred
45*ef123c52SAlbert ARIBAUD  * to as
46*ef123c52SAlbert ARIBAUD  *
47*ef123c52SAlbert ARIBAUD  *   %u_boot_list_ + 2_ + @_list + _2_ + @_entry
48*ef123c52SAlbert ARIBAUD  *
49*ef123c52SAlbert ARIBAUD  * Entry variables need never be referred to directly.
50*ef123c52SAlbert ARIBAUD  *
51*ef123c52SAlbert ARIBAUD  * The naming scheme for input sections allows grouping all linker lists
52*ef123c52SAlbert ARIBAUD  * into a single linker output section and grouping all entries for a
53*ef123c52SAlbert ARIBAUD  * single list.
54*ef123c52SAlbert ARIBAUD  *
55*ef123c52SAlbert ARIBAUD  * Note the two '_2_' constant components in the names: their presence
56*ef123c52SAlbert ARIBAUD  * allows putting a start and end symbols around a list, by mapping
57*ef123c52SAlbert ARIBAUD  * these symbols to sections names with components "1" (before) and
58*ef123c52SAlbert ARIBAUD  * "3" (after) instead of "2" (within).
59*ef123c52SAlbert ARIBAUD  * Start and end symbols for a list can generally be defined as
60*ef123c52SAlbert ARIBAUD  *
61*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_ + @_list + _1_...
62*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_ + @_list + _3_...
63*ef123c52SAlbert ARIBAUD  *
64*ef123c52SAlbert ARIBAUD  * Start and end symbols for the whole of the linker lists area can be
65*ef123c52SAlbert ARIBAUD  * defined as
66*ef123c52SAlbert ARIBAUD  *
67*ef123c52SAlbert ARIBAUD  *   %u_boot_list_1_...
68*ef123c52SAlbert ARIBAUD  *   %u_boot_list_3_...
69*ef123c52SAlbert ARIBAUD  *
70*ef123c52SAlbert ARIBAUD  * Here is an example of the sorted sections which result from a list
71*ef123c52SAlbert ARIBAUD  * "array" made up of three entries : "first", "second" and "third",
72*ef123c52SAlbert ARIBAUD  * iterated at least once.
73*ef123c52SAlbert ARIBAUD  *
74*ef123c52SAlbert ARIBAUD  *   .u_boot_list_2_array_1
75*ef123c52SAlbert ARIBAUD  *   .u_boot_list_2_array_2_first
76*ef123c52SAlbert ARIBAUD  *   .u_boot_list_2_array_2_second
77*ef123c52SAlbert ARIBAUD  *   .u_boot_list_2_array_2_third
78*ef123c52SAlbert ARIBAUD  *   .u_boot_list_2_array_3
79*ef123c52SAlbert ARIBAUD  *
80*ef123c52SAlbert ARIBAUD  * If lists must be divided into sublists (e.g. for iterating only on
81*ef123c52SAlbert ARIBAUD  * part of a list), one can simply give the list a name of the form
82*ef123c52SAlbert ARIBAUD  * 'outer_2_inner', where 'outer' is the global list name and 'inner'
83*ef123c52SAlbert ARIBAUD  * is the sub-list name. Iterators for the whole list should use the
84*ef123c52SAlbert ARIBAUD  * global list name ("outer"); iterators for only a sub-list should use
85*ef123c52SAlbert ARIBAUD  * the full sub-list name ("outer_2_inner").
86*ef123c52SAlbert ARIBAUD  *
87*ef123c52SAlbert ARIBAUD  *  Here is an example of the sections generated from a global list
88*ef123c52SAlbert ARIBAUD  * named "drivers", two sub-lists named "i2c" and "pci", and iterators
89*ef123c52SAlbert ARIBAUD  * defined for the whole list and each sub-list:
90*ef123c52SAlbert ARIBAUD  *
91*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_1
92*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_i2c_1
93*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_i2c_2_first
94*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_i2c_2_first
95*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_i2c_2_second
96*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_i2c_2_third
97*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_i2c_3
98*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_pci_1
99*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_pci_2_first
100*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_pci_2_second
101*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_pci_2_third
102*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_2_pci_3
103*ef123c52SAlbert ARIBAUD  *   %u_boot_list_2_drivers_3
104*ef123c52SAlbert ARIBAUD  */
105*ef123c52SAlbert ARIBAUD 
10642ebaae3SMarek Vasut #ifndef __LINKER_LISTS_H__
10742ebaae3SMarek Vasut #define __LINKER_LISTS_H__
10842ebaae3SMarek Vasut 
10942ebaae3SMarek Vasut /**
11042ebaae3SMarek Vasut  * ll_entry_declare() - Declare linker-generated array entry
11142ebaae3SMarek Vasut  * @_type:	Data type of the entry
11242ebaae3SMarek Vasut  * @_name:	Name of the entry
113*ef123c52SAlbert ARIBAUD  * @_list:	name of the list. Should contain only characters allowed
114*ef123c52SAlbert ARIBAUD  *		in a C variable name!
11542ebaae3SMarek Vasut  *
11642ebaae3SMarek Vasut  * This macro declares a variable that is placed into a linker-generated
11742ebaae3SMarek Vasut  * array. This is a basic building block for more advanced use of linker-
11842ebaae3SMarek Vasut  * generated arrays. The user is expected to build their own macro wrapper
11942ebaae3SMarek Vasut  * around this one.
12042ebaae3SMarek Vasut  *
121*ef123c52SAlbert ARIBAUD  * A variable declared using this macro must be compile-time initialized.
12242ebaae3SMarek Vasut  *
12342ebaae3SMarek Vasut  * Special precaution must be made when using this macro:
12442ebaae3SMarek Vasut  *
125*ef123c52SAlbert ARIBAUD  * 1) The _type must not contain the "static" keyword, otherwise the
126*ef123c52SAlbert ARIBAUD  *    entry is generated and can be iterated but is listed in the map
127*ef123c52SAlbert ARIBAUD  *    file and cannot be retrieved by name.
12842ebaae3SMarek Vasut  *
129*ef123c52SAlbert ARIBAUD  * 2) In case a section is declared that contains some array elements AND
130*ef123c52SAlbert ARIBAUD  *    a subsection of this section is declared and contains some elements,
131*ef123c52SAlbert ARIBAUD  *    it is imperative that the elements are of the same type.
13242ebaae3SMarek Vasut  *
13342ebaae3SMarek Vasut  * 4) In case an outer section is declared that contains some array elements
134*ef123c52SAlbert ARIBAUD  *    AND an inner subsection of this section is declared and contains some
13542ebaae3SMarek Vasut  *    elements, then when traversing the outer section, even the elements of
13642ebaae3SMarek Vasut  *    the inner sections are present in the array.
13742ebaae3SMarek Vasut  *
13842ebaae3SMarek Vasut  * Example:
13942ebaae3SMarek Vasut  * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub, cmd.sub) = {
14042ebaae3SMarek Vasut  *         .x = 3,
14142ebaae3SMarek Vasut  *         .y = 4,
14242ebaae3SMarek Vasut  * };
14342ebaae3SMarek Vasut  */
144*ef123c52SAlbert ARIBAUD #define ll_entry_declare(_type, _name, _list)				\
145*ef123c52SAlbert ARIBAUD 	_type _u_boot_list_2_##_list##_2_##_name __aligned(4)		\
146*ef123c52SAlbert ARIBAUD 			__attribute__((unused,				\
147*ef123c52SAlbert ARIBAUD 			section(".u_boot_list_2_"#_list"_2_"#_name)))
148*ef123c52SAlbert ARIBAUD 
149*ef123c52SAlbert ARIBAUD /**
150*ef123c52SAlbert ARIBAUD  * We need a 0-byte-size type for iterator symbols, and the compiler
151*ef123c52SAlbert ARIBAUD  * does not allow defining objects of C type 'void'. Using an empty
152*ef123c52SAlbert ARIBAUD  * struct is allowed by the compiler, but causes gcc versions 4.4 and
153*ef123c52SAlbert ARIBAUD  * below to complain about aliasing. Therefore we use the next best
154*ef123c52SAlbert ARIBAUD  * thing: zero-sized arrays, which are both 0-byte-size and exempt from
155*ef123c52SAlbert ARIBAUD  * aliasing warnings.
156*ef123c52SAlbert ARIBAUD  */
15742ebaae3SMarek Vasut 
15842ebaae3SMarek Vasut /**
15942ebaae3SMarek Vasut  * ll_entry_start() - Point to first entry of linker-generated array
16042ebaae3SMarek Vasut  * @_type:	Data type of the entry
161*ef123c52SAlbert ARIBAUD  * @_list:	Name of the list in which this entry is placed
16242ebaae3SMarek Vasut  *
16342ebaae3SMarek Vasut  * This function returns (_type *) pointer to the very first entry of a
16442ebaae3SMarek Vasut  * linker-generated array placed into subsection of .u_boot_list section
165*ef123c52SAlbert ARIBAUD  * specified by _list argument.
166*ef123c52SAlbert ARIBAUD  *
167*ef123c52SAlbert ARIBAUD  * Since this macro defines an array start symbol, its leftmost index
168*ef123c52SAlbert ARIBAUD  * must be 2 and its rightmost index must be 1.
16942ebaae3SMarek Vasut  *
17042ebaae3SMarek Vasut  * Example:
17142ebaae3SMarek Vasut  * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub);
17242ebaae3SMarek Vasut  */
173*ef123c52SAlbert ARIBAUD #define ll_entry_start(_type, _list)					\
17442ebaae3SMarek Vasut ({									\
175*ef123c52SAlbert ARIBAUD 	static char start[0] __aligned(4) __attribute__((unused,	\
176*ef123c52SAlbert ARIBAUD 		section(".u_boot_list_2_"#_list"_1")));			\
177*ef123c52SAlbert ARIBAUD 	(_type *)&start;						\
17842ebaae3SMarek Vasut })
17942ebaae3SMarek Vasut 
18042ebaae3SMarek Vasut /**
181*ef123c52SAlbert ARIBAUD  * ll_entry_end() - Point after last entry of linker-generated array
18242ebaae3SMarek Vasut  * @_type:	Data type of the entry
183*ef123c52SAlbert ARIBAUD  * @_list:	Name of the list in which this entry is placed
18442ebaae3SMarek Vasut  *		(with underscores instead of dots)
18542ebaae3SMarek Vasut  *
186*ef123c52SAlbert ARIBAUD  * This function returns (_type *) pointer after the very last entry of
187*ef123c52SAlbert ARIBAUD  * a linker-generated array placed into subsection of .u_boot_list
188*ef123c52SAlbert ARIBAUD  * section specified by _list argument.
189*ef123c52SAlbert ARIBAUD  *
190*ef123c52SAlbert ARIBAUD  * Since this macro defines an array end symbol, its leftmost index
191*ef123c52SAlbert ARIBAUD  * must be 2 and its rightmost index must be 3.
192*ef123c52SAlbert ARIBAUD  *
193*ef123c52SAlbert ARIBAUD  * Example:
194*ef123c52SAlbert ARIBAUD  * struct my_sub_cmd *msc = ll_entry_end(struct my_sub_cmd, cmd_sub);
195*ef123c52SAlbert ARIBAUD  */
196*ef123c52SAlbert ARIBAUD #define ll_entry_end(_type, _list)					\
197*ef123c52SAlbert ARIBAUD ({									\
198*ef123c52SAlbert ARIBAUD 	static char end[0] __aligned(4) __attribute__((unused,	\
199*ef123c52SAlbert ARIBAUD 		section(".u_boot_list_2_"#_list"_3")));			\
200*ef123c52SAlbert ARIBAUD 	(_type *)&end;							\
201*ef123c52SAlbert ARIBAUD })
202*ef123c52SAlbert ARIBAUD /**
203*ef123c52SAlbert ARIBAUD  * ll_entry_count() - Return the number of elements in linker-generated array
204*ef123c52SAlbert ARIBAUD  * @_type:	Data type of the entry
205*ef123c52SAlbert ARIBAUD  * @_list:	Name of the list of which the number of elements is computed
206*ef123c52SAlbert ARIBAUD  *
20742ebaae3SMarek Vasut  * This function returns the number of elements of a linker-generated array
208*ef123c52SAlbert ARIBAUD  * placed into subsection of .u_boot_list section specified by _list
20942ebaae3SMarek Vasut  * argument. The result is of an unsigned int type.
21042ebaae3SMarek Vasut  *
21142ebaae3SMarek Vasut  * Example:
21242ebaae3SMarek Vasut  * int i;
21342ebaae3SMarek Vasut  * const unsigned int count = ll_entry_count(struct my_sub_cmd, cmd_sub);
21442ebaae3SMarek Vasut  * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub);
21542ebaae3SMarek Vasut  * for (i = 0; i < count; i++, msc++)
21642ebaae3SMarek Vasut  *         printf("Entry %i, x=%i y=%i\n", i, msc->x, msc->y);
21742ebaae3SMarek Vasut  */
218*ef123c52SAlbert ARIBAUD #define ll_entry_count(_type, _list)					\
21942ebaae3SMarek Vasut 	({								\
220*ef123c52SAlbert ARIBAUD 		_type *start = ll_entry_start(_type, _list);		\
221*ef123c52SAlbert ARIBAUD 		_type *end = ll_entry_end(_type, _list);		\
222*ef123c52SAlbert ARIBAUD 		unsigned int _ll_result = end - start;			\
22342ebaae3SMarek Vasut 		_ll_result;						\
22442ebaae3SMarek Vasut 	})
22542ebaae3SMarek Vasut 
22642ebaae3SMarek Vasut /**
22742ebaae3SMarek Vasut  * ll_entry_get() - Retrieve entry from linker-generated array by name
22842ebaae3SMarek Vasut  * @_type:	Data type of the entry
22942ebaae3SMarek Vasut  * @_name:	Name of the entry
230*ef123c52SAlbert ARIBAUD  * @_list:	Name of the list in which this entry is placed
23142ebaae3SMarek Vasut  *
23242ebaae3SMarek Vasut  * This function returns a pointer to a particular entry in LG-array
23342ebaae3SMarek Vasut  * identified by the subsection of u_boot_list where the entry resides
23442ebaae3SMarek Vasut  * and it's name.
23542ebaae3SMarek Vasut  *
23642ebaae3SMarek Vasut  * Example:
23742ebaae3SMarek Vasut  * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub, cmd.sub) = {
23842ebaae3SMarek Vasut  *         .x = 3,
23942ebaae3SMarek Vasut  *         .y = 4,
24042ebaae3SMarek Vasut  * };
24142ebaae3SMarek Vasut  * ...
24242ebaae3SMarek Vasut  * struct my_sub_cmd *c = ll_entry_get(struct my_sub_cmd, my_sub_cmd, cmd_sub);
24342ebaae3SMarek Vasut  */
244*ef123c52SAlbert ARIBAUD #define ll_entry_get(_type, _name, _list)				\
24542ebaae3SMarek Vasut 	({								\
246*ef123c52SAlbert ARIBAUD 		extern _type _u_boot_list_2_##_list##_2_##_name;	\
247*ef123c52SAlbert ARIBAUD 		_type *_ll_result =					\
248*ef123c52SAlbert ARIBAUD 			&_u_boot_list_2_##_list##_2_##_name;	\
24942ebaae3SMarek Vasut 		_ll_result;						\
25042ebaae3SMarek Vasut 	})
25142ebaae3SMarek Vasut 
252*ef123c52SAlbert ARIBAUD /**
253*ef123c52SAlbert ARIBAUD  * ll_start() - Point to first entry of first linker-generated array
254*ef123c52SAlbert ARIBAUD  * @_type:	Data type of the entry
255*ef123c52SAlbert ARIBAUD  *
256*ef123c52SAlbert ARIBAUD  * This function returns (_type *) pointer to the very first entry of
257*ef123c52SAlbert ARIBAUD  * the very first linker-generated array.
258*ef123c52SAlbert ARIBAUD  *
259*ef123c52SAlbert ARIBAUD  * Since this macro defines the start of the linker-generated arrays,
260*ef123c52SAlbert ARIBAUD  * its leftmost index must be 1.
261*ef123c52SAlbert ARIBAUD  *
262*ef123c52SAlbert ARIBAUD  * Example:
263*ef123c52SAlbert ARIBAUD  * struct my_sub_cmd *msc = ll_start(struct my_sub_cmd);
264*ef123c52SAlbert ARIBAUD  */
265*ef123c52SAlbert ARIBAUD #define ll_start(_type)							\
266*ef123c52SAlbert ARIBAUD ({									\
267*ef123c52SAlbert ARIBAUD 	static char start[0] __aligned(4) __attribute__((unused,	\
268*ef123c52SAlbert ARIBAUD 		section(".u_boot_list_1")));				\
269*ef123c52SAlbert ARIBAUD 	(_type *)&start;						\
270*ef123c52SAlbert ARIBAUD })
271*ef123c52SAlbert ARIBAUD 
272*ef123c52SAlbert ARIBAUD /**
273*ef123c52SAlbert ARIBAUD  * ll_entry_end() - Point after last entry of last linker-generated array
274*ef123c52SAlbert ARIBAUD  * @_type:	Data type of the entry
275*ef123c52SAlbert ARIBAUD  *
276*ef123c52SAlbert ARIBAUD  * This function returns (_type *) pointer after the very last entry of
277*ef123c52SAlbert ARIBAUD  * the very last linker-generated array.
278*ef123c52SAlbert ARIBAUD  *
279*ef123c52SAlbert ARIBAUD  * Since this macro defines the end of the linker-generated arrays,
280*ef123c52SAlbert ARIBAUD  * its leftmost index must be 3.
281*ef123c52SAlbert ARIBAUD  *
282*ef123c52SAlbert ARIBAUD  * Example:
283*ef123c52SAlbert ARIBAUD  * struct my_sub_cmd *msc = ll_end(struct my_sub_cmd);
284*ef123c52SAlbert ARIBAUD  */
285*ef123c52SAlbert ARIBAUD #define ll_end(_type)							\
286*ef123c52SAlbert ARIBAUD ({									\
287*ef123c52SAlbert ARIBAUD 	static char end[0] __aligned(4) __attribute__((unused,	\
288*ef123c52SAlbert ARIBAUD 		section(".u_boot_list_3")));				\
289*ef123c52SAlbert ARIBAUD 	(_type *)&end;							\
290*ef123c52SAlbert ARIBAUD })
291*ef123c52SAlbert ARIBAUD 
292*ef123c52SAlbert ARIBAUD #endif /* __ASSEMBLY__ */
293*ef123c52SAlbert ARIBAUD 
29442ebaae3SMarek Vasut #endif	/* __LINKER_LISTS_H__ */
295