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