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 26ef123c52SAlbert ARIBAUD * sections, each containning 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 42ef123c52SAlbert ARIBAUD * setion 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 /** 10642ebaae3SMarek Vasut * ll_entry_declare() - Declare linker-generated array entry 10742ebaae3SMarek Vasut * @_type: Data type of the entry 10842ebaae3SMarek Vasut * @_name: Name of the entry 109ef123c52SAlbert ARIBAUD * @_list: name of the list. Should contain only characters allowed 110ef123c52SAlbert ARIBAUD * in a C variable name! 11142ebaae3SMarek Vasut * 11242ebaae3SMarek Vasut * This macro declares a variable that is placed into a linker-generated 11342ebaae3SMarek Vasut * array. This is a basic building block for more advanced use of linker- 11442ebaae3SMarek Vasut * generated arrays. The user is expected to build their own macro wrapper 11542ebaae3SMarek Vasut * around this one. 11642ebaae3SMarek Vasut * 117ef123c52SAlbert ARIBAUD * A variable declared using this macro must be compile-time initialized. 11842ebaae3SMarek Vasut * 11942ebaae3SMarek Vasut * Special precaution must be made when using this macro: 12042ebaae3SMarek Vasut * 121ef123c52SAlbert ARIBAUD * 1) The _type must not contain the "static" keyword, otherwise the 122ef123c52SAlbert ARIBAUD * entry is generated and can be iterated but is listed in the map 123ef123c52SAlbert ARIBAUD * file and cannot be retrieved by name. 12442ebaae3SMarek Vasut * 125ef123c52SAlbert ARIBAUD * 2) In case a section is declared that contains some array elements AND 126ef123c52SAlbert ARIBAUD * a subsection of this section is declared and contains some elements, 127ef123c52SAlbert ARIBAUD * it is imperative that the elements are of the same type. 12842ebaae3SMarek Vasut * 12942ebaae3SMarek Vasut * 4) In case an outer section is declared that contains some array elements 130ef123c52SAlbert ARIBAUD * AND an inner subsection of this section is declared and contains some 13142ebaae3SMarek Vasut * elements, then when traversing the outer section, even the elements of 13242ebaae3SMarek Vasut * the inner sections are present in the array. 13342ebaae3SMarek Vasut * 13442ebaae3SMarek Vasut * Example: 13542ebaae3SMarek Vasut * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub, cmd.sub) = { 13642ebaae3SMarek Vasut * .x = 3, 13742ebaae3SMarek Vasut * .y = 4, 13842ebaae3SMarek Vasut * }; 13942ebaae3SMarek Vasut */ 140ef123c52SAlbert ARIBAUD #define ll_entry_declare(_type, _name, _list) \ 141ef123c52SAlbert ARIBAUD _type _u_boot_list_2_##_list##_2_##_name __aligned(4) \ 142ef123c52SAlbert ARIBAUD __attribute__((unused, \ 143ef123c52SAlbert ARIBAUD section(".u_boot_list_2_"#_list"_2_"#_name))) 144ef123c52SAlbert ARIBAUD 145ef123c52SAlbert ARIBAUD /** 146*3fcc3af4SSimon Glass * ll_entry_declare_list() - Declare a list of link-generated array entries 147*3fcc3af4SSimon Glass * @_type: Data type of each entry 148*3fcc3af4SSimon Glass * @_name: Name of the entry 149*3fcc3af4SSimon Glass * @_list: name of the list. Should contain only characters allowed 150*3fcc3af4SSimon Glass * in a C variable name! 151*3fcc3af4SSimon Glass * 152*3fcc3af4SSimon Glass * This is like ll_entry_declare() but creates multiple entries. It should 153*3fcc3af4SSimon Glass * be assigned to an array. 154*3fcc3af4SSimon Glass * 155*3fcc3af4SSimon Glass * ll_entry_declare_list(struct my_sub_cmd, my_sub_cmd, cmd_sub, cmd.sub) = { 156*3fcc3af4SSimon Glass * { .x = 3, .y = 4 }, 157*3fcc3af4SSimon Glass * { .x = 8, .y = 2 }, 158*3fcc3af4SSimon Glass * { .x = 1, .y = 7 } 159*3fcc3af4SSimon Glass * }; 160*3fcc3af4SSimon Glass */ 161*3fcc3af4SSimon Glass #define ll_entry_declare_list(_type, _name, _list) \ 162*3fcc3af4SSimon Glass _type _u_boot_list_2_##_list##_2_##_name[] __aligned(4) \ 163*3fcc3af4SSimon Glass __attribute__((unused, \ 164*3fcc3af4SSimon Glass section(".u_boot_list_2_"#_list"_2_"#_name))) 165*3fcc3af4SSimon Glass 166*3fcc3af4SSimon Glass /** 167ef123c52SAlbert ARIBAUD * We need a 0-byte-size type for iterator symbols, and the compiler 168ef123c52SAlbert ARIBAUD * does not allow defining objects of C type 'void'. Using an empty 169ef123c52SAlbert ARIBAUD * struct is allowed by the compiler, but causes gcc versions 4.4 and 170ef123c52SAlbert ARIBAUD * below to complain about aliasing. Therefore we use the next best 171ef123c52SAlbert ARIBAUD * thing: zero-sized arrays, which are both 0-byte-size and exempt from 172ef123c52SAlbert ARIBAUD * aliasing warnings. 173ef123c52SAlbert ARIBAUD */ 17442ebaae3SMarek Vasut 17542ebaae3SMarek Vasut /** 17642ebaae3SMarek Vasut * ll_entry_start() - Point to first entry of linker-generated array 17742ebaae3SMarek Vasut * @_type: Data type of the entry 178ef123c52SAlbert ARIBAUD * @_list: Name of the list in which this entry is placed 17942ebaae3SMarek Vasut * 18042ebaae3SMarek Vasut * This function returns (_type *) pointer to the very first entry of a 18142ebaae3SMarek Vasut * linker-generated array placed into subsection of .u_boot_list section 182ef123c52SAlbert ARIBAUD * specified by _list argument. 183ef123c52SAlbert ARIBAUD * 184ef123c52SAlbert ARIBAUD * Since this macro defines an array start symbol, its leftmost index 185ef123c52SAlbert ARIBAUD * must be 2 and its rightmost index must be 1. 18642ebaae3SMarek Vasut * 18742ebaae3SMarek Vasut * Example: 18842ebaae3SMarek Vasut * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub); 18942ebaae3SMarek Vasut */ 190ef123c52SAlbert ARIBAUD #define ll_entry_start(_type, _list) \ 19142ebaae3SMarek Vasut ({ \ 192ef123c52SAlbert ARIBAUD static char start[0] __aligned(4) __attribute__((unused, \ 193ef123c52SAlbert ARIBAUD section(".u_boot_list_2_"#_list"_1"))); \ 194ef123c52SAlbert ARIBAUD (_type *)&start; \ 19542ebaae3SMarek Vasut }) 19642ebaae3SMarek Vasut 19742ebaae3SMarek Vasut /** 198ef123c52SAlbert ARIBAUD * ll_entry_end() - Point after last entry of linker-generated array 19942ebaae3SMarek Vasut * @_type: Data type of the entry 200ef123c52SAlbert ARIBAUD * @_list: Name of the list in which this entry is placed 20142ebaae3SMarek Vasut * (with underscores instead of dots) 20242ebaae3SMarek Vasut * 203ef123c52SAlbert ARIBAUD * This function returns (_type *) pointer after the very last entry of 204ef123c52SAlbert ARIBAUD * a linker-generated array placed into subsection of .u_boot_list 205ef123c52SAlbert ARIBAUD * section specified by _list argument. 206ef123c52SAlbert ARIBAUD * 207ef123c52SAlbert ARIBAUD * Since this macro defines an array end symbol, its leftmost index 208ef123c52SAlbert ARIBAUD * must be 2 and its rightmost index must be 3. 209ef123c52SAlbert ARIBAUD * 210ef123c52SAlbert ARIBAUD * Example: 211ef123c52SAlbert ARIBAUD * struct my_sub_cmd *msc = ll_entry_end(struct my_sub_cmd, cmd_sub); 212ef123c52SAlbert ARIBAUD */ 213ef123c52SAlbert ARIBAUD #define ll_entry_end(_type, _list) \ 214ef123c52SAlbert ARIBAUD ({ \ 215ef123c52SAlbert ARIBAUD static char end[0] __aligned(4) __attribute__((unused, \ 216ef123c52SAlbert ARIBAUD section(".u_boot_list_2_"#_list"_3"))); \ 217ef123c52SAlbert ARIBAUD (_type *)&end; \ 218ef123c52SAlbert ARIBAUD }) 219ef123c52SAlbert ARIBAUD /** 220ef123c52SAlbert ARIBAUD * ll_entry_count() - Return the number of elements in linker-generated array 221ef123c52SAlbert ARIBAUD * @_type: Data type of the entry 222ef123c52SAlbert ARIBAUD * @_list: Name of the list of which the number of elements is computed 223ef123c52SAlbert ARIBAUD * 22442ebaae3SMarek Vasut * This function returns the number of elements of a linker-generated array 225ef123c52SAlbert ARIBAUD * placed into subsection of .u_boot_list section specified by _list 22642ebaae3SMarek Vasut * argument. The result is of an unsigned int type. 22742ebaae3SMarek Vasut * 22842ebaae3SMarek Vasut * Example: 22942ebaae3SMarek Vasut * int i; 23042ebaae3SMarek Vasut * const unsigned int count = ll_entry_count(struct my_sub_cmd, cmd_sub); 23142ebaae3SMarek Vasut * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub); 23242ebaae3SMarek Vasut * for (i = 0; i < count; i++, msc++) 23342ebaae3SMarek Vasut * printf("Entry %i, x=%i y=%i\n", i, msc->x, msc->y); 23442ebaae3SMarek Vasut */ 235ef123c52SAlbert ARIBAUD #define ll_entry_count(_type, _list) \ 23642ebaae3SMarek Vasut ({ \ 237ef123c52SAlbert ARIBAUD _type *start = ll_entry_start(_type, _list); \ 238ef123c52SAlbert ARIBAUD _type *end = ll_entry_end(_type, _list); \ 239ef123c52SAlbert ARIBAUD unsigned int _ll_result = end - start; \ 24042ebaae3SMarek Vasut _ll_result; \ 24142ebaae3SMarek Vasut }) 24242ebaae3SMarek Vasut 24342ebaae3SMarek Vasut /** 24442ebaae3SMarek Vasut * ll_entry_get() - Retrieve entry from linker-generated array by name 24542ebaae3SMarek Vasut * @_type: Data type of the entry 24642ebaae3SMarek Vasut * @_name: Name of the entry 247ef123c52SAlbert ARIBAUD * @_list: Name of the list in which this entry is placed 24842ebaae3SMarek Vasut * 24942ebaae3SMarek Vasut * This function returns a pointer to a particular entry in LG-array 25042ebaae3SMarek Vasut * identified by the subsection of u_boot_list where the entry resides 25142ebaae3SMarek Vasut * and it's name. 25242ebaae3SMarek Vasut * 25342ebaae3SMarek Vasut * Example: 254af41d6b4SMateusz Zalega * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = { 25542ebaae3SMarek Vasut * .x = 3, 25642ebaae3SMarek Vasut * .y = 4, 25742ebaae3SMarek Vasut * }; 25842ebaae3SMarek Vasut * ... 25942ebaae3SMarek Vasut * struct my_sub_cmd *c = ll_entry_get(struct my_sub_cmd, my_sub_cmd, cmd_sub); 26042ebaae3SMarek Vasut */ 261ef123c52SAlbert ARIBAUD #define ll_entry_get(_type, _name, _list) \ 26242ebaae3SMarek Vasut ({ \ 263ef123c52SAlbert ARIBAUD extern _type _u_boot_list_2_##_list##_2_##_name; \ 264ef123c52SAlbert ARIBAUD _type *_ll_result = \ 265ef123c52SAlbert ARIBAUD &_u_boot_list_2_##_list##_2_##_name; \ 26642ebaae3SMarek Vasut _ll_result; \ 26742ebaae3SMarek Vasut }) 26842ebaae3SMarek Vasut 269ef123c52SAlbert ARIBAUD /** 270ef123c52SAlbert ARIBAUD * ll_start() - Point to first entry of first linker-generated array 271ef123c52SAlbert ARIBAUD * @_type: Data type of the entry 272ef123c52SAlbert ARIBAUD * 273ef123c52SAlbert ARIBAUD * This function returns (_type *) pointer to the very first entry of 274ef123c52SAlbert ARIBAUD * the very first linker-generated array. 275ef123c52SAlbert ARIBAUD * 276ef123c52SAlbert ARIBAUD * Since this macro defines the start of the linker-generated arrays, 277ef123c52SAlbert ARIBAUD * its leftmost index must be 1. 278ef123c52SAlbert ARIBAUD * 279ef123c52SAlbert ARIBAUD * Example: 280ef123c52SAlbert ARIBAUD * struct my_sub_cmd *msc = ll_start(struct my_sub_cmd); 281ef123c52SAlbert ARIBAUD */ 282ef123c52SAlbert ARIBAUD #define ll_start(_type) \ 283ef123c52SAlbert ARIBAUD ({ \ 284ef123c52SAlbert ARIBAUD static char start[0] __aligned(4) __attribute__((unused, \ 285ef123c52SAlbert ARIBAUD section(".u_boot_list_1"))); \ 286ef123c52SAlbert ARIBAUD (_type *)&start; \ 287ef123c52SAlbert ARIBAUD }) 288ef123c52SAlbert ARIBAUD 289ef123c52SAlbert ARIBAUD /** 290ef123c52SAlbert ARIBAUD * ll_entry_end() - Point after last entry of last linker-generated array 291ef123c52SAlbert ARIBAUD * @_type: Data type of the entry 292ef123c52SAlbert ARIBAUD * 293ef123c52SAlbert ARIBAUD * This function returns (_type *) pointer after the very last entry of 294ef123c52SAlbert ARIBAUD * the very last linker-generated array. 295ef123c52SAlbert ARIBAUD * 296ef123c52SAlbert ARIBAUD * Since this macro defines the end of the linker-generated arrays, 297ef123c52SAlbert ARIBAUD * its leftmost index must be 3. 298ef123c52SAlbert ARIBAUD * 299ef123c52SAlbert ARIBAUD * Example: 300ef123c52SAlbert ARIBAUD * struct my_sub_cmd *msc = ll_end(struct my_sub_cmd); 301ef123c52SAlbert ARIBAUD */ 302ef123c52SAlbert ARIBAUD #define ll_end(_type) \ 303ef123c52SAlbert ARIBAUD ({ \ 304ef123c52SAlbert ARIBAUD static char end[0] __aligned(4) __attribute__((unused, \ 305ef123c52SAlbert ARIBAUD section(".u_boot_list_3"))); \ 306ef123c52SAlbert ARIBAUD (_type *)&end; \ 307ef123c52SAlbert ARIBAUD }) 308ef123c52SAlbert ARIBAUD 309ef123c52SAlbert ARIBAUD #endif /* __ASSEMBLY__ */ 310ef123c52SAlbert ARIBAUD 31142ebaae3SMarek Vasut #endif /* __LINKER_LISTS_H__ */ 312