Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * include/linker_lists.h |
| 3 | * |
| 4 | * Implementation of linker-generated arrays |
| 5 | * |
| 6 | * Copyright (C) 2012 Marek Vasut <marex@denx.de> |
| 7 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 9 | */ |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 10 | |
Masahiro Yamada | b9f9ce4 | 2014-02-05 10:52:52 +0900 | [diff] [blame] | 11 | #ifndef __LINKER_LISTS_H__ |
| 12 | #define __LINKER_LISTS_H__ |
| 13 | |
Masahiro Yamada | d7a73f2 | 2014-10-07 14:48:22 +0900 | [diff] [blame] | 14 | #include <linux/compiler.h> |
| 15 | |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 16 | /* |
Heinrich Schuchardt | 6da113c | 2017-09-07 03:55:11 +0200 | [diff] [blame] | 17 | * There is no use in including this from ASM files. |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 18 | * So just don't define anything when included from ASM. |
| 19 | */ |
| 20 | |
| 21 | #if !defined(__ASSEMBLY__) |
| 22 | |
| 23 | /** |
| 24 | * A linker list is constructed by grouping together linker input |
Guilherme Maciel Ferreira | 0d725c7 | 2015-01-15 02:39:59 -0200 | [diff] [blame] | 25 | * sections, each containing one entry of the list. Each input section |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 26 | * contains a constant initialized variable which holds the entry's |
| 27 | * content. Linker list input sections are constructed from the list |
| 28 | * and entry names, plus a prefix which allows grouping all lists |
| 29 | * together. Assuming _list and _entry are the list and entry names, |
| 30 | * then the corresponding input section name is |
| 31 | * |
Masahiro Yamada | d4a533f | 2014-09-16 20:21:15 +0900 | [diff] [blame] | 32 | * .u_boot_list_ + 2_ + @_list + _2_ + @_entry |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 33 | * |
| 34 | * and the C variable name is |
| 35 | * |
Masahiro Yamada | d4a533f | 2014-09-16 20:21:15 +0900 | [diff] [blame] | 36 | * _u_boot_list + _2_ + @_list + _2_ + @_entry |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 37 | * |
| 38 | * This ensures uniqueness for both input section and C variable name. |
| 39 | * |
| 40 | * Note that the names differ only in the first character, "." for the |
Guilherme Maciel Ferreira | 0d725c7 | 2015-01-15 02:39:59 -0200 | [diff] [blame] | 41 | * section and "_" for the variable, so that the linker cannot confuse |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 42 | * section and symbol names. From now on, both names will be referred |
| 43 | * to as |
| 44 | * |
| 45 | * %u_boot_list_ + 2_ + @_list + _2_ + @_entry |
| 46 | * |
| 47 | * Entry variables need never be referred to directly. |
| 48 | * |
| 49 | * The naming scheme for input sections allows grouping all linker lists |
| 50 | * into a single linker output section and grouping all entries for a |
| 51 | * single list. |
| 52 | * |
| 53 | * Note the two '_2_' constant components in the names: their presence |
| 54 | * allows putting a start and end symbols around a list, by mapping |
| 55 | * these symbols to sections names with components "1" (before) and |
| 56 | * "3" (after) instead of "2" (within). |
| 57 | * Start and end symbols for a list can generally be defined as |
| 58 | * |
| 59 | * %u_boot_list_2_ + @_list + _1_... |
| 60 | * %u_boot_list_2_ + @_list + _3_... |
| 61 | * |
| 62 | * Start and end symbols for the whole of the linker lists area can be |
| 63 | * defined as |
| 64 | * |
| 65 | * %u_boot_list_1_... |
| 66 | * %u_boot_list_3_... |
| 67 | * |
| 68 | * Here is an example of the sorted sections which result from a list |
| 69 | * "array" made up of three entries : "first", "second" and "third", |
| 70 | * iterated at least once. |
| 71 | * |
| 72 | * .u_boot_list_2_array_1 |
| 73 | * .u_boot_list_2_array_2_first |
| 74 | * .u_boot_list_2_array_2_second |
| 75 | * .u_boot_list_2_array_2_third |
| 76 | * .u_boot_list_2_array_3 |
| 77 | * |
| 78 | * If lists must be divided into sublists (e.g. for iterating only on |
| 79 | * part of a list), one can simply give the list a name of the form |
| 80 | * 'outer_2_inner', where 'outer' is the global list name and 'inner' |
| 81 | * is the sub-list name. Iterators for the whole list should use the |
| 82 | * global list name ("outer"); iterators for only a sub-list should use |
| 83 | * the full sub-list name ("outer_2_inner"). |
| 84 | * |
Bin Meng | 02a31b7 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 85 | * Here is an example of the sections generated from a global list |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 86 | * named "drivers", two sub-lists named "i2c" and "pci", and iterators |
| 87 | * defined for the whole list and each sub-list: |
| 88 | * |
| 89 | * %u_boot_list_2_drivers_1 |
| 90 | * %u_boot_list_2_drivers_2_i2c_1 |
| 91 | * %u_boot_list_2_drivers_2_i2c_2_first |
| 92 | * %u_boot_list_2_drivers_2_i2c_2_first |
| 93 | * %u_boot_list_2_drivers_2_i2c_2_second |
| 94 | * %u_boot_list_2_drivers_2_i2c_2_third |
| 95 | * %u_boot_list_2_drivers_2_i2c_3 |
| 96 | * %u_boot_list_2_drivers_2_pci_1 |
| 97 | * %u_boot_list_2_drivers_2_pci_2_first |
| 98 | * %u_boot_list_2_drivers_2_pci_2_second |
| 99 | * %u_boot_list_2_drivers_2_pci_2_third |
| 100 | * %u_boot_list_2_drivers_2_pci_3 |
| 101 | * %u_boot_list_2_drivers_3 |
| 102 | */ |
| 103 | |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 104 | /** |
Bin Meng | 02a31b7 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 105 | * llsym() - Access a linker-generated array entry |
Simon Glass | 629bf85 | 2015-03-25 12:21:49 -0600 | [diff] [blame] | 106 | * @_type: Data type of the entry |
| 107 | * @_name: Name of the entry |
| 108 | * @_list: name of the list. Should contain only characters allowed |
| 109 | * in a C variable name! |
| 110 | */ |
| 111 | #define llsym(_type, _name, _list) \ |
| 112 | ((_type *)&_u_boot_list_2_##_list##_2_##_name) |
| 113 | |
| 114 | /** |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 115 | * ll_entry_declare() - Declare linker-generated array entry |
| 116 | * @_type: Data type of the entry |
| 117 | * @_name: Name of the entry |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 118 | * @_list: name of the list. Should contain only characters allowed |
| 119 | * in a C variable name! |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 120 | * |
| 121 | * This macro declares a variable that is placed into a linker-generated |
| 122 | * array. This is a basic building block for more advanced use of linker- |
| 123 | * generated arrays. The user is expected to build their own macro wrapper |
| 124 | * around this one. |
| 125 | * |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 126 | * A variable declared using this macro must be compile-time initialized. |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 127 | * |
| 128 | * Special precaution must be made when using this macro: |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 129 | * |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 130 | * 1) The _type must not contain the "static" keyword, otherwise the |
| 131 | * entry is generated and can be iterated but is listed in the map |
| 132 | * file and cannot be retrieved by name. |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 133 | * |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 134 | * 2) In case a section is declared that contains some array elements AND |
| 135 | * a subsection of this section is declared and contains some elements, |
| 136 | * it is imperative that the elements are of the same type. |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 137 | * |
| 138 | * 4) In case an outer section is declared that contains some array elements |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 139 | * AND an inner subsection of this section is declared and contains some |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 140 | * elements, then when traversing the outer section, even the elements of |
| 141 | * the inner sections are present in the array. |
| 142 | * |
| 143 | * Example: |
Bin Meng | 02a31b7 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 144 | * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = { |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 145 | * .x = 3, |
| 146 | * .y = 4, |
| 147 | * }; |
| 148 | */ |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 149 | #define ll_entry_declare(_type, _name, _list) \ |
| 150 | _type _u_boot_list_2_##_list##_2_##_name __aligned(4) \ |
| 151 | __attribute__((unused, \ |
| 152 | section(".u_boot_list_2_"#_list"_2_"#_name))) |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 153 | |
| 154 | /** |
Simon Glass | 5d169aa | 2014-10-01 19:57:20 -0600 | [diff] [blame] | 155 | * ll_entry_declare_list() - Declare a list of link-generated array entries |
| 156 | * @_type: Data type of each entry |
| 157 | * @_name: Name of the entry |
| 158 | * @_list: name of the list. Should contain only characters allowed |
| 159 | * in a C variable name! |
| 160 | * |
| 161 | * This is like ll_entry_declare() but creates multiple entries. It should |
| 162 | * be assigned to an array. |
| 163 | * |
Bin Meng | 02a31b7 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 164 | * ll_entry_declare_list(struct my_sub_cmd, my_sub_cmd, cmd_sub) = { |
Simon Glass | 5d169aa | 2014-10-01 19:57:20 -0600 | [diff] [blame] | 165 | * { .x = 3, .y = 4 }, |
| 166 | * { .x = 8, .y = 2 }, |
| 167 | * { .x = 1, .y = 7 } |
| 168 | * }; |
| 169 | */ |
| 170 | #define ll_entry_declare_list(_type, _name, _list) \ |
| 171 | _type _u_boot_list_2_##_list##_2_##_name[] __aligned(4) \ |
| 172 | __attribute__((unused, \ |
| 173 | section(".u_boot_list_2_"#_list"_2_"#_name))) |
| 174 | |
| 175 | /** |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 176 | * We need a 0-byte-size type for iterator symbols, and the compiler |
| 177 | * does not allow defining objects of C type 'void'. Using an empty |
| 178 | * struct is allowed by the compiler, but causes gcc versions 4.4 and |
| 179 | * below to complain about aliasing. Therefore we use the next best |
| 180 | * thing: zero-sized arrays, which are both 0-byte-size and exempt from |
| 181 | * aliasing warnings. |
| 182 | */ |
| 183 | |
| 184 | /** |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 185 | * ll_entry_start() - Point to first entry of linker-generated array |
| 186 | * @_type: Data type of the entry |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 187 | * @_list: Name of the list in which this entry is placed |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 188 | * |
| 189 | * This function returns (_type *) pointer to the very first entry of a |
| 190 | * linker-generated array placed into subsection of .u_boot_list section |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 191 | * specified by _list argument. |
| 192 | * |
| 193 | * Since this macro defines an array start symbol, its leftmost index |
| 194 | * must be 2 and its rightmost index must be 1. |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 195 | * |
| 196 | * Example: |
| 197 | * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub); |
| 198 | */ |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 199 | #define ll_entry_start(_type, _list) \ |
| 200 | ({ \ |
| 201 | static char start[0] __aligned(4) __attribute__((unused, \ |
| 202 | section(".u_boot_list_2_"#_list"_1"))); \ |
| 203 | (_type *)&start; \ |
| 204 | }) |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 205 | |
| 206 | /** |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 207 | * ll_entry_end() - Point after last entry of linker-generated array |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 208 | * @_type: Data type of the entry |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 209 | * @_list: Name of the list in which this entry is placed |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 210 | * (with underscores instead of dots) |
| 211 | * |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 212 | * This function returns (_type *) pointer after the very last entry of |
| 213 | * a linker-generated array placed into subsection of .u_boot_list |
| 214 | * section specified by _list argument. |
| 215 | * |
| 216 | * Since this macro defines an array end symbol, its leftmost index |
| 217 | * must be 2 and its rightmost index must be 3. |
| 218 | * |
| 219 | * Example: |
| 220 | * struct my_sub_cmd *msc = ll_entry_end(struct my_sub_cmd, cmd_sub); |
| 221 | */ |
| 222 | #define ll_entry_end(_type, _list) \ |
| 223 | ({ \ |
Bin Meng | 02a31b7 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 224 | static char end[0] __aligned(4) __attribute__((unused, \ |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 225 | section(".u_boot_list_2_"#_list"_3"))); \ |
| 226 | (_type *)&end; \ |
| 227 | }) |
| 228 | /** |
| 229 | * ll_entry_count() - Return the number of elements in linker-generated array |
| 230 | * @_type: Data type of the entry |
| 231 | * @_list: Name of the list of which the number of elements is computed |
| 232 | * |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 233 | * This function returns the number of elements of a linker-generated array |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 234 | * placed into subsection of .u_boot_list section specified by _list |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 235 | * argument. The result is of an unsigned int type. |
| 236 | * |
| 237 | * Example: |
| 238 | * int i; |
| 239 | * const unsigned int count = ll_entry_count(struct my_sub_cmd, cmd_sub); |
| 240 | * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub); |
| 241 | * for (i = 0; i < count; i++, msc++) |
| 242 | * printf("Entry %i, x=%i y=%i\n", i, msc->x, msc->y); |
| 243 | */ |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 244 | #define ll_entry_count(_type, _list) \ |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 245 | ({ \ |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 246 | _type *start = ll_entry_start(_type, _list); \ |
| 247 | _type *end = ll_entry_end(_type, _list); \ |
| 248 | unsigned int _ll_result = end - start; \ |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 249 | _ll_result; \ |
| 250 | }) |
| 251 | |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 252 | /** |
| 253 | * ll_entry_get() - Retrieve entry from linker-generated array by name |
| 254 | * @_type: Data type of the entry |
| 255 | * @_name: Name of the entry |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 256 | * @_list: Name of the list in which this entry is placed |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 257 | * |
Bin Meng | 02a31b7 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 258 | * This function returns a pointer to a particular entry in linker-generated |
| 259 | * array identified by the subsection of u_boot_list where the entry resides |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 260 | * and it's name. |
| 261 | * |
| 262 | * Example: |
Mateusz Zalega | 463ce69 | 2014-04-29 20:14:22 +0200 | [diff] [blame] | 263 | * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = { |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 264 | * .x = 3, |
| 265 | * .y = 4, |
| 266 | * }; |
| 267 | * ... |
| 268 | * struct my_sub_cmd *c = ll_entry_get(struct my_sub_cmd, my_sub_cmd, cmd_sub); |
| 269 | */ |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 270 | #define ll_entry_get(_type, _name, _list) \ |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 271 | ({ \ |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 272 | extern _type _u_boot_list_2_##_list##_2_##_name; \ |
| 273 | _type *_ll_result = \ |
Bin Meng | 02a31b7 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 274 | &_u_boot_list_2_##_list##_2_##_name; \ |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 275 | _ll_result; \ |
| 276 | }) |
| 277 | |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 278 | /** |
| 279 | * ll_start() - Point to first entry of first linker-generated array |
| 280 | * @_type: Data type of the entry |
| 281 | * |
| 282 | * This function returns (_type *) pointer to the very first entry of |
| 283 | * the very first linker-generated array. |
| 284 | * |
| 285 | * Since this macro defines the start of the linker-generated arrays, |
| 286 | * its leftmost index must be 1. |
| 287 | * |
| 288 | * Example: |
| 289 | * struct my_sub_cmd *msc = ll_start(struct my_sub_cmd); |
| 290 | */ |
| 291 | #define ll_start(_type) \ |
| 292 | ({ \ |
| 293 | static char start[0] __aligned(4) __attribute__((unused, \ |
| 294 | section(".u_boot_list_1"))); \ |
| 295 | (_type *)&start; \ |
| 296 | }) |
| 297 | |
| 298 | /** |
Bin Meng | 02a31b7 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 299 | * ll_end() - Point after last entry of last linker-generated array |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 300 | * @_type: Data type of the entry |
| 301 | * |
| 302 | * This function returns (_type *) pointer after the very last entry of |
| 303 | * the very last linker-generated array. |
| 304 | * |
| 305 | * Since this macro defines the end of the linker-generated arrays, |
| 306 | * its leftmost index must be 3. |
| 307 | * |
| 308 | * Example: |
| 309 | * struct my_sub_cmd *msc = ll_end(struct my_sub_cmd); |
| 310 | */ |
| 311 | #define ll_end(_type) \ |
| 312 | ({ \ |
Bin Meng | 02a31b7 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 313 | static char end[0] __aligned(4) __attribute__((unused, \ |
Albert ARIBAUD | c24895e | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 314 | section(".u_boot_list_3"))); \ |
| 315 | (_type *)&end; \ |
| 316 | }) |
| 317 | |
| 318 | #endif /* __ASSEMBLY__ */ |
| 319 | |
Marek Vasut | 9b8899d | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 320 | #endif /* __LINKER_LISTS_H__ */ |