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 /* 17ef123c52SAlbert ARIBAUD * There is no use in including this from ASM files, but that happens 18ef123c52SAlbert ARIBAUD * anyway, e.g. PPC kgdb.S includes command.h which incluse us. 19ef123c52SAlbert ARIBAUD * So just don't define anything when included from ASM. 20ef123c52SAlbert ARIBAUD */ 21ef123c52SAlbert ARIBAUD 22ef123c52SAlbert ARIBAUD #if !defined(__ASSEMBLY__) 23ef123c52SAlbert ARIBAUD 24ef123c52SAlbert ARIBAUD /** 25ef123c52SAlbert ARIBAUD * A linker list is constructed by grouping together linker input 2644f145fdSGuilherme Maciel Ferreira * sections, each containing one entry of the list. Each input section 27ef123c52SAlbert ARIBAUD * contains a constant initialized variable which holds the entry's 28ef123c52SAlbert ARIBAUD * content. Linker list input sections are constructed from the list 29ef123c52SAlbert ARIBAUD * and entry names, plus a prefix which allows grouping all lists 30ef123c52SAlbert ARIBAUD * together. Assuming _list and _entry are the list and entry names, 31ef123c52SAlbert ARIBAUD * then the corresponding input section name is 32ef123c52SAlbert ARIBAUD * 3397d5e9d1SMasahiro Yamada * .u_boot_list_ + 2_ + @_list + _2_ + @_entry 34ef123c52SAlbert ARIBAUD * 35ef123c52SAlbert ARIBAUD * and the C variable name is 36ef123c52SAlbert ARIBAUD * 3797d5e9d1SMasahiro Yamada * _u_boot_list + _2_ + @_list + _2_ + @_entry 38ef123c52SAlbert ARIBAUD * 39ef123c52SAlbert ARIBAUD * This ensures uniqueness for both input section and C variable name. 40ef123c52SAlbert ARIBAUD * 41ef123c52SAlbert ARIBAUD * Note that the names differ only in the first character, "." for the 4244f145fdSGuilherme Maciel Ferreira * section and "_" for the variable, so that the linker cannot confuse 43ef123c52SAlbert ARIBAUD * section and symbol names. From now on, both names will be referred 44ef123c52SAlbert ARIBAUD * to as 45ef123c52SAlbert ARIBAUD * 46ef123c52SAlbert ARIBAUD * %u_boot_list_ + 2_ + @_list + _2_ + @_entry 47ef123c52SAlbert ARIBAUD * 48ef123c52SAlbert ARIBAUD * Entry variables need never be referred to directly. 49ef123c52SAlbert ARIBAUD * 50ef123c52SAlbert ARIBAUD * The naming scheme for input sections allows grouping all linker lists 51ef123c52SAlbert ARIBAUD * into a single linker output section and grouping all entries for a 52ef123c52SAlbert ARIBAUD * single list. 53ef123c52SAlbert ARIBAUD * 54ef123c52SAlbert ARIBAUD * Note the two '_2_' constant components in the names: their presence 55ef123c52SAlbert ARIBAUD * allows putting a start and end symbols around a list, by mapping 56ef123c52SAlbert ARIBAUD * these symbols to sections names with components "1" (before) and 57ef123c52SAlbert ARIBAUD * "3" (after) instead of "2" (within). 58ef123c52SAlbert ARIBAUD * Start and end symbols for a list can generally be defined as 59ef123c52SAlbert ARIBAUD * 60ef123c52SAlbert ARIBAUD * %u_boot_list_2_ + @_list + _1_... 61ef123c52SAlbert ARIBAUD * %u_boot_list_2_ + @_list + _3_... 62ef123c52SAlbert ARIBAUD * 63ef123c52SAlbert ARIBAUD * Start and end symbols for the whole of the linker lists area can be 64ef123c52SAlbert ARIBAUD * defined as 65ef123c52SAlbert ARIBAUD * 66ef123c52SAlbert ARIBAUD * %u_boot_list_1_... 67ef123c52SAlbert ARIBAUD * %u_boot_list_3_... 68ef123c52SAlbert ARIBAUD * 69ef123c52SAlbert ARIBAUD * Here is an example of the sorted sections which result from a list 70ef123c52SAlbert ARIBAUD * "array" made up of three entries : "first", "second" and "third", 71ef123c52SAlbert ARIBAUD * iterated at least once. 72ef123c52SAlbert ARIBAUD * 73ef123c52SAlbert ARIBAUD * .u_boot_list_2_array_1 74ef123c52SAlbert ARIBAUD * .u_boot_list_2_array_2_first 75ef123c52SAlbert ARIBAUD * .u_boot_list_2_array_2_second 76ef123c52SAlbert ARIBAUD * .u_boot_list_2_array_2_third 77ef123c52SAlbert ARIBAUD * .u_boot_list_2_array_3 78ef123c52SAlbert ARIBAUD * 79ef123c52SAlbert ARIBAUD * If lists must be divided into sublists (e.g. for iterating only on 80ef123c52SAlbert ARIBAUD * part of a list), one can simply give the list a name of the form 81ef123c52SAlbert ARIBAUD * 'outer_2_inner', where 'outer' is the global list name and 'inner' 82ef123c52SAlbert ARIBAUD * is the sub-list name. Iterators for the whole list should use the 83ef123c52SAlbert ARIBAUD * global list name ("outer"); iterators for only a sub-list should use 84ef123c52SAlbert ARIBAUD * the full sub-list name ("outer_2_inner"). 85ef123c52SAlbert ARIBAUD * 86ef123c52SAlbert ARIBAUD * Here is an example of the sections generated from a global list 87ef123c52SAlbert ARIBAUD * named "drivers", two sub-lists named "i2c" and "pci", and iterators 88ef123c52SAlbert ARIBAUD * defined for the whole list and each sub-list: 89ef123c52SAlbert ARIBAUD * 90ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_1 91ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_2_i2c_1 92ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_2_i2c_2_first 93ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_2_i2c_2_first 94ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_2_i2c_2_second 95ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_2_i2c_2_third 96ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_2_i2c_3 97ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_2_pci_1 98ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_2_pci_2_first 99ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_2_pci_2_second 100ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_2_pci_2_third 101ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_2_pci_3 102ef123c52SAlbert ARIBAUD * %u_boot_list_2_drivers_3 103ef123c52SAlbert ARIBAUD */ 104ef123c52SAlbert ARIBAUD 10542ebaae3SMarek Vasut /** 106*7e378b8bSBin Meng * llsym() - Access a linker-generated array entry 10772538f4cSSimon Glass * @_type: Data type of the entry 10872538f4cSSimon Glass * @_name: Name of the entry 10972538f4cSSimon Glass * @_list: name of the list. Should contain only characters allowed 11072538f4cSSimon Glass * in a C variable name! 11172538f4cSSimon Glass */ 11272538f4cSSimon Glass #define llsym(_type, _name, _list) \ 11372538f4cSSimon Glass ((_type *)&_u_boot_list_2_##_list##_2_##_name) 11472538f4cSSimon Glass 11572538f4cSSimon Glass /** 11642ebaae3SMarek Vasut * ll_entry_declare() - Declare linker-generated array entry 11742ebaae3SMarek Vasut * @_type: Data type of the entry 11842ebaae3SMarek Vasut * @_name: Name of the entry 119ef123c52SAlbert ARIBAUD * @_list: name of the list. Should contain only characters allowed 120ef123c52SAlbert ARIBAUD * in a C variable name! 12142ebaae3SMarek Vasut * 12242ebaae3SMarek Vasut * This macro declares a variable that is placed into a linker-generated 12342ebaae3SMarek Vasut * array. This is a basic building block for more advanced use of linker- 12442ebaae3SMarek Vasut * generated arrays. The user is expected to build their own macro wrapper 12542ebaae3SMarek Vasut * around this one. 12642ebaae3SMarek Vasut * 127ef123c52SAlbert ARIBAUD * A variable declared using this macro must be compile-time initialized. 12842ebaae3SMarek Vasut * 12942ebaae3SMarek Vasut * Special precaution must be made when using this macro: 13042ebaae3SMarek Vasut * 131ef123c52SAlbert ARIBAUD * 1) The _type must not contain the "static" keyword, otherwise the 132ef123c52SAlbert ARIBAUD * entry is generated and can be iterated but is listed in the map 133ef123c52SAlbert ARIBAUD * file and cannot be retrieved by name. 13442ebaae3SMarek Vasut * 135ef123c52SAlbert ARIBAUD * 2) In case a section is declared that contains some array elements AND 136ef123c52SAlbert ARIBAUD * a subsection of this section is declared and contains some elements, 137ef123c52SAlbert ARIBAUD * it is imperative that the elements are of the same type. 13842ebaae3SMarek Vasut * 13942ebaae3SMarek Vasut * 4) In case an outer section is declared that contains some array elements 140ef123c52SAlbert ARIBAUD * AND an inner subsection of this section is declared and contains some 14142ebaae3SMarek Vasut * elements, then when traversing the outer section, even the elements of 14242ebaae3SMarek Vasut * the inner sections are present in the array. 14342ebaae3SMarek Vasut * 14442ebaae3SMarek Vasut * Example: 145*7e378b8bSBin Meng * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = { 14642ebaae3SMarek Vasut * .x = 3, 14742ebaae3SMarek Vasut * .y = 4, 14842ebaae3SMarek Vasut * }; 14942ebaae3SMarek Vasut */ 150ef123c52SAlbert ARIBAUD #define ll_entry_declare(_type, _name, _list) \ 151ef123c52SAlbert ARIBAUD _type _u_boot_list_2_##_list##_2_##_name __aligned(4) \ 152ef123c52SAlbert ARIBAUD __attribute__((unused, \ 153ef123c52SAlbert ARIBAUD section(".u_boot_list_2_"#_list"_2_"#_name))) 154ef123c52SAlbert ARIBAUD 155ef123c52SAlbert ARIBAUD /** 1563fcc3af4SSimon Glass * ll_entry_declare_list() - Declare a list of link-generated array entries 1573fcc3af4SSimon Glass * @_type: Data type of each entry 1583fcc3af4SSimon Glass * @_name: Name of the entry 1593fcc3af4SSimon Glass * @_list: name of the list. Should contain only characters allowed 1603fcc3af4SSimon Glass * in a C variable name! 1613fcc3af4SSimon Glass * 1623fcc3af4SSimon Glass * This is like ll_entry_declare() but creates multiple entries. It should 1633fcc3af4SSimon Glass * be assigned to an array. 1643fcc3af4SSimon Glass * 165*7e378b8bSBin Meng * ll_entry_declare_list(struct my_sub_cmd, my_sub_cmd, cmd_sub) = { 1663fcc3af4SSimon Glass * { .x = 3, .y = 4 }, 1673fcc3af4SSimon Glass * { .x = 8, .y = 2 }, 1683fcc3af4SSimon Glass * { .x = 1, .y = 7 } 1693fcc3af4SSimon Glass * }; 1703fcc3af4SSimon Glass */ 1713fcc3af4SSimon Glass #define ll_entry_declare_list(_type, _name, _list) \ 1723fcc3af4SSimon Glass _type _u_boot_list_2_##_list##_2_##_name[] __aligned(4) \ 1733fcc3af4SSimon Glass __attribute__((unused, \ 1743fcc3af4SSimon Glass section(".u_boot_list_2_"#_list"_2_"#_name))) 1753fcc3af4SSimon Glass 1763fcc3af4SSimon Glass /** 177ef123c52SAlbert ARIBAUD * We need a 0-byte-size type for iterator symbols, and the compiler 178ef123c52SAlbert ARIBAUD * does not allow defining objects of C type 'void'. Using an empty 179ef123c52SAlbert ARIBAUD * struct is allowed by the compiler, but causes gcc versions 4.4 and 180ef123c52SAlbert ARIBAUD * below to complain about aliasing. Therefore we use the next best 181ef123c52SAlbert ARIBAUD * thing: zero-sized arrays, which are both 0-byte-size and exempt from 182ef123c52SAlbert ARIBAUD * aliasing warnings. 183ef123c52SAlbert ARIBAUD */ 18442ebaae3SMarek Vasut 18542ebaae3SMarek Vasut /** 18642ebaae3SMarek Vasut * ll_entry_start() - Point to first entry of linker-generated array 18742ebaae3SMarek Vasut * @_type: Data type of the entry 188ef123c52SAlbert ARIBAUD * @_list: Name of the list in which this entry is placed 18942ebaae3SMarek Vasut * 19042ebaae3SMarek Vasut * This function returns (_type *) pointer to the very first entry of a 19142ebaae3SMarek Vasut * linker-generated array placed into subsection of .u_boot_list section 192ef123c52SAlbert ARIBAUD * specified by _list argument. 193ef123c52SAlbert ARIBAUD * 194ef123c52SAlbert ARIBAUD * Since this macro defines an array start symbol, its leftmost index 195ef123c52SAlbert ARIBAUD * must be 2 and its rightmost index must be 1. 19642ebaae3SMarek Vasut * 19742ebaae3SMarek Vasut * Example: 19842ebaae3SMarek Vasut * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub); 19942ebaae3SMarek Vasut */ 200ef123c52SAlbert ARIBAUD #define ll_entry_start(_type, _list) \ 20142ebaae3SMarek Vasut ({ \ 202ef123c52SAlbert ARIBAUD static char start[0] __aligned(4) __attribute__((unused, \ 203ef123c52SAlbert ARIBAUD section(".u_boot_list_2_"#_list"_1"))); \ 204ef123c52SAlbert ARIBAUD (_type *)&start; \ 20542ebaae3SMarek Vasut }) 20642ebaae3SMarek Vasut 20742ebaae3SMarek Vasut /** 208ef123c52SAlbert ARIBAUD * ll_entry_end() - Point after last entry of linker-generated array 20942ebaae3SMarek Vasut * @_type: Data type of the entry 210ef123c52SAlbert ARIBAUD * @_list: Name of the list in which this entry is placed 21142ebaae3SMarek Vasut * (with underscores instead of dots) 21242ebaae3SMarek Vasut * 213ef123c52SAlbert ARIBAUD * This function returns (_type *) pointer after the very last entry of 214ef123c52SAlbert ARIBAUD * a linker-generated array placed into subsection of .u_boot_list 215ef123c52SAlbert ARIBAUD * section specified by _list argument. 216ef123c52SAlbert ARIBAUD * 217ef123c52SAlbert ARIBAUD * Since this macro defines an array end symbol, its leftmost index 218ef123c52SAlbert ARIBAUD * must be 2 and its rightmost index must be 3. 219ef123c52SAlbert ARIBAUD * 220ef123c52SAlbert ARIBAUD * Example: 221ef123c52SAlbert ARIBAUD * struct my_sub_cmd *msc = ll_entry_end(struct my_sub_cmd, cmd_sub); 222ef123c52SAlbert ARIBAUD */ 223ef123c52SAlbert ARIBAUD #define ll_entry_end(_type, _list) \ 224ef123c52SAlbert ARIBAUD ({ \ 225ef123c52SAlbert ARIBAUD static char end[0] __aligned(4) __attribute__((unused, \ 226ef123c52SAlbert ARIBAUD section(".u_boot_list_2_"#_list"_3"))); \ 227ef123c52SAlbert ARIBAUD (_type *)&end; \ 228ef123c52SAlbert ARIBAUD }) 229ef123c52SAlbert ARIBAUD /** 230ef123c52SAlbert ARIBAUD * ll_entry_count() - Return the number of elements in linker-generated array 231ef123c52SAlbert ARIBAUD * @_type: Data type of the entry 232ef123c52SAlbert ARIBAUD * @_list: Name of the list of which the number of elements is computed 233ef123c52SAlbert ARIBAUD * 23442ebaae3SMarek Vasut * This function returns the number of elements of a linker-generated array 235ef123c52SAlbert ARIBAUD * placed into subsection of .u_boot_list section specified by _list 23642ebaae3SMarek Vasut * argument. The result is of an unsigned int type. 23742ebaae3SMarek Vasut * 23842ebaae3SMarek Vasut * Example: 23942ebaae3SMarek Vasut * int i; 24042ebaae3SMarek Vasut * const unsigned int count = ll_entry_count(struct my_sub_cmd, cmd_sub); 24142ebaae3SMarek Vasut * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub); 24242ebaae3SMarek Vasut * for (i = 0; i < count; i++, msc++) 24342ebaae3SMarek Vasut * printf("Entry %i, x=%i y=%i\n", i, msc->x, msc->y); 24442ebaae3SMarek Vasut */ 245ef123c52SAlbert ARIBAUD #define ll_entry_count(_type, _list) \ 24642ebaae3SMarek Vasut ({ \ 247ef123c52SAlbert ARIBAUD _type *start = ll_entry_start(_type, _list); \ 248ef123c52SAlbert ARIBAUD _type *end = ll_entry_end(_type, _list); \ 249ef123c52SAlbert ARIBAUD unsigned int _ll_result = end - start; \ 25042ebaae3SMarek Vasut _ll_result; \ 25142ebaae3SMarek Vasut }) 25242ebaae3SMarek Vasut 25342ebaae3SMarek Vasut /** 25442ebaae3SMarek Vasut * ll_entry_get() - Retrieve entry from linker-generated array by name 25542ebaae3SMarek Vasut * @_type: Data type of the entry 25642ebaae3SMarek Vasut * @_name: Name of the entry 257ef123c52SAlbert ARIBAUD * @_list: Name of the list in which this entry is placed 25842ebaae3SMarek Vasut * 259*7e378b8bSBin Meng * This function returns a pointer to a particular entry in linker-generated 260*7e378b8bSBin Meng * array identified by the subsection of u_boot_list where the entry resides 26142ebaae3SMarek Vasut * and it's name. 26242ebaae3SMarek Vasut * 26342ebaae3SMarek Vasut * Example: 264af41d6b4SMateusz Zalega * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = { 26542ebaae3SMarek Vasut * .x = 3, 26642ebaae3SMarek Vasut * .y = 4, 26742ebaae3SMarek Vasut * }; 26842ebaae3SMarek Vasut * ... 26942ebaae3SMarek Vasut * struct my_sub_cmd *c = ll_entry_get(struct my_sub_cmd, my_sub_cmd, cmd_sub); 27042ebaae3SMarek Vasut */ 271ef123c52SAlbert ARIBAUD #define ll_entry_get(_type, _name, _list) \ 27242ebaae3SMarek Vasut ({ \ 273ef123c52SAlbert ARIBAUD extern _type _u_boot_list_2_##_list##_2_##_name; \ 274ef123c52SAlbert ARIBAUD _type *_ll_result = \ 275ef123c52SAlbert ARIBAUD &_u_boot_list_2_##_list##_2_##_name; \ 27642ebaae3SMarek Vasut _ll_result; \ 27742ebaae3SMarek Vasut }) 27842ebaae3SMarek Vasut 279ef123c52SAlbert ARIBAUD /** 280ef123c52SAlbert ARIBAUD * ll_start() - Point to first entry of first linker-generated array 281ef123c52SAlbert ARIBAUD * @_type: Data type of the entry 282ef123c52SAlbert ARIBAUD * 283ef123c52SAlbert ARIBAUD * This function returns (_type *) pointer to the very first entry of 284ef123c52SAlbert ARIBAUD * the very first linker-generated array. 285ef123c52SAlbert ARIBAUD * 286ef123c52SAlbert ARIBAUD * Since this macro defines the start of the linker-generated arrays, 287ef123c52SAlbert ARIBAUD * its leftmost index must be 1. 288ef123c52SAlbert ARIBAUD * 289ef123c52SAlbert ARIBAUD * Example: 290ef123c52SAlbert ARIBAUD * struct my_sub_cmd *msc = ll_start(struct my_sub_cmd); 291ef123c52SAlbert ARIBAUD */ 292ef123c52SAlbert ARIBAUD #define ll_start(_type) \ 293ef123c52SAlbert ARIBAUD ({ \ 294ef123c52SAlbert ARIBAUD static char start[0] __aligned(4) __attribute__((unused, \ 295ef123c52SAlbert ARIBAUD section(".u_boot_list_1"))); \ 296ef123c52SAlbert ARIBAUD (_type *)&start; \ 297ef123c52SAlbert ARIBAUD }) 298ef123c52SAlbert ARIBAUD 299ef123c52SAlbert ARIBAUD /** 300*7e378b8bSBin Meng * ll_end() - Point after last entry of last linker-generated array 301ef123c52SAlbert ARIBAUD * @_type: Data type of the entry 302ef123c52SAlbert ARIBAUD * 303ef123c52SAlbert ARIBAUD * This function returns (_type *) pointer after the very last entry of 304ef123c52SAlbert ARIBAUD * the very last linker-generated array. 305ef123c52SAlbert ARIBAUD * 306ef123c52SAlbert ARIBAUD * Since this macro defines the end of the linker-generated arrays, 307ef123c52SAlbert ARIBAUD * its leftmost index must be 3. 308ef123c52SAlbert ARIBAUD * 309ef123c52SAlbert ARIBAUD * Example: 310ef123c52SAlbert ARIBAUD * struct my_sub_cmd *msc = ll_end(struct my_sub_cmd); 311ef123c52SAlbert ARIBAUD */ 312ef123c52SAlbert ARIBAUD #define ll_end(_type) \ 313ef123c52SAlbert ARIBAUD ({ \ 314ef123c52SAlbert ARIBAUD static char end[0] __aligned(4) __attribute__((unused, \ 315ef123c52SAlbert ARIBAUD section(".u_boot_list_3"))); \ 316ef123c52SAlbert ARIBAUD (_type *)&end; \ 317ef123c52SAlbert ARIBAUD }) 318ef123c52SAlbert ARIBAUD 319ef123c52SAlbert ARIBAUD #endif /* __ASSEMBLY__ */ 320ef123c52SAlbert ARIBAUD 32142ebaae3SMarek Vasut #endif /* __LINKER_LISTS_H__ */ 322