linker_lists.h 10 KB

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