Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 2 | /* |
| 3 | * include/linker_lists.h |
| 4 | * |
| 5 | * Implementation of linker-generated arrays |
| 6 | * |
| 7 | * Copyright (C) 2012 Marek Vasut <marex@denx.de> |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 8 | */ |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 9 | |
Masahiro Yamada | babb444 | 2014-02-05 10:52:52 +0900 | [diff] [blame] | 10 | #ifndef __LINKER_LISTS_H__ |
| 11 | #define __LINKER_LISTS_H__ |
| 12 | |
Masahiro Yamada | dc7cb46 | 2014-10-07 14:48:22 +0900 | [diff] [blame] | 13 | #include <linux/compiler.h> |
| 14 | |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 15 | /* |
Heinrich Schuchardt | d72fd7b | 2017-09-07 03:55:11 +0200 | [diff] [blame] | 16 | * There is no use in including this from ASM files. |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 17 | * So just don't define anything when included from ASM. |
| 18 | */ |
| 19 | |
| 20 | #if !defined(__ASSEMBLY__) |
| 21 | |
| 22 | /** |
Bin Meng | 7e378b8 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 23 | * llsym() - Access a linker-generated array entry |
Simon Glass | 72538f4 | 2015-03-25 12:21:49 -0600 | [diff] [blame] | 24 | * @_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 Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 33 | * ll_entry_declare() - Declare linker-generated array entry |
| 34 | * @_type: Data type of the entry |
| 35 | * @_name: Name of the entry |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 36 | * @_list: name of the list. Should contain only characters allowed |
| 37 | * in a C variable name! |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 38 | * |
| 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 ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 44 | * A variable declared using this macro must be compile-time initialized. |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 45 | * |
| 46 | * Special precaution must be made when using this macro: |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 47 | * |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 48 | * 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 Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 51 | * |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 52 | * 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 Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 55 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 56 | * 3) In case an outer section is declared that contains some array elements |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 57 | * AND an inner subsection of this section is declared and contains some |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 58 | * elements, then when traversing the outer section, even the elements of |
| 59 | * the inner sections are present in the array. |
| 60 | * |
| 61 | * Example: |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 62 | * |
| 63 | * :: |
| 64 | * |
| 65 | * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = { |
| 66 | * .x = 3, |
| 67 | * .y = 4, |
| 68 | * }; |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 69 | */ |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 70 | #define ll_entry_declare(_type, _name, _list) \ |
| 71 | _type _u_boot_list_2_##_list##_2_##_name __aligned(4) \ |
Marek Behún | 236f2ec | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 72 | __attribute__((unused)) \ |
Andrew Scull | 99e2fbc | 2022-05-30 10:00:04 +0000 | [diff] [blame] | 73 | __section("__u_boot_list_2_"#_list"_2_"#_name) |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 74 | |
| 75 | /** |
Simon Glass | 3fcc3af | 2014-10-01 19:57:20 -0600 | [diff] [blame] | 76 | * 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 Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 85 | * :: |
| 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 Glass | 3fcc3af | 2014-10-01 19:57:20 -0600 | [diff] [blame] | 92 | */ |
| 93 | #define ll_entry_declare_list(_type, _name, _list) \ |
| 94 | _type _u_boot_list_2_##_list##_2_##_name[] __aligned(4) \ |
Marek Behún | 236f2ec | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 95 | __attribute__((unused)) \ |
Andrew Scull | 99e2fbc | 2022-05-30 10:00:04 +0000 | [diff] [blame] | 96 | __section("__u_boot_list_2_"#_list"_2_"#_name) |
Simon Glass | 3fcc3af | 2014-10-01 19:57:20 -0600 | [diff] [blame] | 97 | |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 98 | /* |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 99 | * 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 | */ |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * ll_entry_start() - Point to first entry of linker-generated array |
| 109 | * @_type: Data type of the entry |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 110 | * @_list: Name of the list in which this entry is placed |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 111 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 112 | * This function returns ``(_type *)`` pointer to the very first entry of a |
Andrew Scull | 99e2fbc | 2022-05-30 10:00:04 +0000 | [diff] [blame] | 113 | * linker-generated array placed into subsection of __u_boot_list section |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 114 | * 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 Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 118 | * |
| 119 | * Example: |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 120 | * |
| 121 | * :: |
| 122 | * |
| 123 | * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub); |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 124 | */ |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 125 | #define ll_entry_start(_type, _list) \ |
| 126 | ({ \ |
Simon Glass | 0b2fa98 | 2020-12-16 21:20:06 -0700 | [diff] [blame] | 127 | static char start[0] __aligned(CONFIG_LINKER_LIST_ALIGN) \ |
Marek Behún | 236f2ec | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 128 | __attribute__((unused)) \ |
Andrew Scull | 99e2fbc | 2022-05-30 10:00:04 +0000 | [diff] [blame] | 129 | __section("__u_boot_list_2_"#_list"_1"); \ |
Tom Rini | d0e3378 | 2023-03-28 14:54:51 -0400 | [diff] [blame] | 130 | _type * tmp = (_type *)&start; \ |
| 131 | asm("":"+r"(tmp)); \ |
| 132 | tmp; \ |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 133 | }) |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 134 | |
| 135 | /** |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 136 | * ll_entry_end() - Point after last entry of linker-generated array |
| 137 | * @_type: Data type of the entry |
| 138 | * @_list: Name of the list in which this entry is placed |
| 139 | * (with underscores instead of dots) |
| 140 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 141 | * This function returns ``(_type *)`` pointer after the very last entry of |
Andrew Scull | 99e2fbc | 2022-05-30 10:00:04 +0000 | [diff] [blame] | 142 | * a linker-generated array placed into subsection of __u_boot_list |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 143 | * 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 Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 149 | * |
| 150 | * :: |
| 151 | * |
| 152 | * struct my_sub_cmd *msc = ll_entry_end(struct my_sub_cmd, cmd_sub); |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 153 | */ |
| 154 | #define ll_entry_end(_type, _list) \ |
| 155 | ({ \ |
Marek Behún | 236f2ec | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 156 | static char end[0] __aligned(4) __attribute__((unused)) \ |
Andrew Scull | 99e2fbc | 2022-05-30 10:00:04 +0000 | [diff] [blame] | 157 | __section("__u_boot_list_2_"#_list"_3"); \ |
Tom Rini | d0e3378 | 2023-03-28 14:54:51 -0400 | [diff] [blame] | 158 | _type * tmp = (_type *)&end; \ |
| 159 | asm("":"+r"(tmp)); \ |
| 160 | tmp; \ |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 161 | }) |
| 162 | /** |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 163 | * ll_entry_count() - Return the number of elements in linker-generated array |
| 164 | * @_type: Data type of the entry |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 165 | * @_list: Name of the list of which the number of elements is computed |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 166 | * |
| 167 | * This function returns the number of elements of a linker-generated array |
Andrew Scull | 99e2fbc | 2022-05-30 10:00:04 +0000 | [diff] [blame] | 168 | * placed into subsection of __u_boot_list section specified by _list |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 169 | * argument. The result is of an unsigned int type. |
| 170 | * |
| 171 | * Example: |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 172 | * |
| 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 Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 180 | */ |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 181 | #define ll_entry_count(_type, _list) \ |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 182 | ({ \ |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 183 | _type *start = ll_entry_start(_type, _list); \ |
| 184 | _type *end = ll_entry_end(_type, _list); \ |
| 185 | unsigned int _ll_result = end - start; \ |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 186 | _ll_result; \ |
| 187 | }) |
| 188 | |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 189 | /** |
| 190 | * ll_entry_get() - Retrieve entry from linker-generated array by name |
| 191 | * @_type: Data type of the entry |
| 192 | * @_name: Name of the entry |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 193 | * @_list: Name of the list in which this entry is placed |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 194 | * |
Bin Meng | 7e378b8 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 195 | * This function returns a pointer to a particular entry in linker-generated |
| 196 | * array identified by the subsection of u_boot_list where the entry resides |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 197 | * and it's name. |
| 198 | * |
| 199 | * Example: |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 200 | * |
| 201 | * :: |
| 202 | * |
| 203 | * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = { |
| 204 | * .x = 3, |
| 205 | * .y = 4, |
| 206 | * }; |
| 207 | * ... |
| 208 | * struct my_sub_cmd *c = ll_entry_get(struct my_sub_cmd, my_sub_cmd, cmd_sub); |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 209 | */ |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 210 | #define ll_entry_get(_type, _name, _list) \ |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 211 | ({ \ |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 212 | extern _type _u_boot_list_2_##_list##_2_##_name; \ |
| 213 | _type *_ll_result = \ |
Bin Meng | 7e378b8 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 214 | &_u_boot_list_2_##_list##_2_##_name; \ |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 215 | _ll_result; \ |
| 216 | }) |
| 217 | |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 218 | /** |
Simon Glass | 8511443 | 2021-03-15 17:25:13 +1300 | [diff] [blame] | 219 | * ll_entry_ref() - Get a reference to a linker-generated array entry |
| 220 | * |
Simon Glass | 607f9bc | 2021-03-15 17:25:14 +1300 | [diff] [blame] | 221 | * Once an extern ll_entry_declare() has been used to declare the reference, |
| 222 | * this macro allows the entry to be accessed. |
Simon Glass | 8511443 | 2021-03-15 17:25:13 +1300 | [diff] [blame] | 223 | * |
| 224 | * This is like ll_entry_get(), but without the extra code, so it is suitable |
| 225 | * for putting into data structures. |
Heinrich Schuchardt | f44c214 | 2021-03-28 10:53:18 +0200 | [diff] [blame] | 226 | * |
| 227 | * @_type: C type of the list entry, e.g. 'struct foo' |
| 228 | * @_name: name of the entry |
| 229 | * @_list: name of the list |
Simon Glass | 8511443 | 2021-03-15 17:25:13 +1300 | [diff] [blame] | 230 | */ |
| 231 | #define ll_entry_ref(_type, _name, _list) \ |
| 232 | ((_type *)&_u_boot_list_2_##_list##_2_##_name) |
| 233 | |
| 234 | /** |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 235 | * ll_start() - Point to first entry of first linker-generated array |
| 236 | * @_type: Data type of the entry |
| 237 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 238 | * This function returns ``(_type *)`` pointer to the very first entry of |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 239 | * the very first linker-generated array. |
| 240 | * |
| 241 | * Since this macro defines the start of the linker-generated arrays, |
| 242 | * its leftmost index must be 1. |
| 243 | * |
| 244 | * Example: |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 245 | * |
| 246 | * :: |
| 247 | * |
| 248 | * struct my_sub_cmd *msc = ll_start(struct my_sub_cmd); |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 249 | */ |
| 250 | #define ll_start(_type) \ |
| 251 | ({ \ |
Marek Behún | 236f2ec | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 252 | static char start[0] __aligned(4) __attribute__((unused)) \ |
Andrew Scull | 99e2fbc | 2022-05-30 10:00:04 +0000 | [diff] [blame] | 253 | __section("__u_boot_list_1"); \ |
Tom Rini | d0e3378 | 2023-03-28 14:54:51 -0400 | [diff] [blame] | 254 | _type * tmp = (_type *)&start; \ |
| 255 | asm("":"+r"(tmp)); \ |
| 256 | tmp; \ |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 257 | }) |
| 258 | |
| 259 | /** |
Bin Meng | 7e378b8 | 2015-07-19 00:20:02 +0800 | [diff] [blame] | 260 | * ll_end() - Point after last entry of last linker-generated array |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 261 | * @_type: Data type of the entry |
| 262 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 263 | * This function returns ``(_type *)`` pointer after the very last entry of |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 264 | * the very last linker-generated array. |
| 265 | * |
| 266 | * Since this macro defines the end of the linker-generated arrays, |
| 267 | * its leftmost index must be 3. |
| 268 | * |
| 269 | * Example: |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 270 | * |
| 271 | * :: |
| 272 | * |
| 273 | * struct my_sub_cmd *msc = ll_end(struct my_sub_cmd); |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 274 | */ |
| 275 | #define ll_end(_type) \ |
| 276 | ({ \ |
Marek Behún | 236f2ec | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 277 | static char end[0] __aligned(4) __attribute__((unused)) \ |
Andrew Scull | 99e2fbc | 2022-05-30 10:00:04 +0000 | [diff] [blame] | 278 | __section("__u_boot_list_3"); \ |
Tom Rini | d0e3378 | 2023-03-28 14:54:51 -0400 | [diff] [blame] | 279 | _type * tmp = (_type *)&end; \ |
| 280 | asm("":"+r"(tmp)); \ |
| 281 | tmp; \ |
Albert ARIBAUD | ef123c5 | 2013-02-25 00:59:00 +0000 | [diff] [blame] | 282 | }) |
| 283 | |
| 284 | #endif /* __ASSEMBLY__ */ |
| 285 | |
Marek Vasut | 42ebaae | 2012-10-12 10:27:02 +0000 | [diff] [blame] | 286 | #endif /* __LINKER_LISTS_H__ */ |