linker_lists.h 9.8 KB

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