linker_lists.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 containing 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. * section 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. * llsym() - Access a 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. #define llsym(_type, _name, _list) \
  107. ((_type *)&_u_boot_list_2_##_list##_2_##_name)
  108. /**
  109. * ll_entry_declare() - Declare linker-generated array entry
  110. * @_type: Data type of the entry
  111. * @_name: Name of the entry
  112. * @_list: name of the list. Should contain only characters allowed
  113. * in a C variable name!
  114. *
  115. * This macro declares a variable that is placed into a linker-generated
  116. * array. This is a basic building block for more advanced use of linker-
  117. * generated arrays. The user is expected to build their own macro wrapper
  118. * around this one.
  119. *
  120. * A variable declared using this macro must be compile-time initialized.
  121. *
  122. * Special precaution must be made when using this macro:
  123. *
  124. * 1) The _type must not contain the "static" keyword, otherwise the
  125. * entry is generated and can be iterated but is listed in the map
  126. * file and cannot be retrieved by name.
  127. *
  128. * 2) In case a section is declared that contains some array elements AND
  129. * a subsection of this section is declared and contains some elements,
  130. * it is imperative that the elements are of the same type.
  131. *
  132. * 4) In case an outer section is declared that contains some array elements
  133. * AND an inner subsection of this section is declared and contains some
  134. * elements, then when traversing the outer section, even the elements of
  135. * the inner sections are present in the array.
  136. *
  137. * Example:
  138. * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = {
  139. * .x = 3,
  140. * .y = 4,
  141. * };
  142. */
  143. #define ll_entry_declare(_type, _name, _list) \
  144. _type _u_boot_list_2_##_list##_2_##_name __aligned(4) \
  145. __attribute__((unused, \
  146. section(".u_boot_list_2_"#_list"_2_"#_name)))
  147. /**
  148. * ll_entry_declare_list() - Declare a list of link-generated array entries
  149. * @_type: Data type of each entry
  150. * @_name: Name of the entry
  151. * @_list: name of the list. Should contain only characters allowed
  152. * in a C variable name!
  153. *
  154. * This is like ll_entry_declare() but creates multiple entries. It should
  155. * be assigned to an array.
  156. *
  157. * ll_entry_declare_list(struct my_sub_cmd, my_sub_cmd, cmd_sub) = {
  158. * { .x = 3, .y = 4 },
  159. * { .x = 8, .y = 2 },
  160. * { .x = 1, .y = 7 }
  161. * };
  162. */
  163. #define ll_entry_declare_list(_type, _name, _list) \
  164. _type _u_boot_list_2_##_list##_2_##_name[] __aligned(4) \
  165. __attribute__((unused, \
  166. section(".u_boot_list_2_"#_list"_2_"#_name)))
  167. /**
  168. * We need a 0-byte-size type for iterator symbols, and the compiler
  169. * does not allow defining objects of C type 'void'. Using an empty
  170. * struct is allowed by the compiler, but causes gcc versions 4.4 and
  171. * below to complain about aliasing. Therefore we use the next best
  172. * thing: zero-sized arrays, which are both 0-byte-size and exempt from
  173. * aliasing warnings.
  174. */
  175. /**
  176. * ll_entry_start() - Point to first entry of linker-generated array
  177. * @_type: Data type of the entry
  178. * @_list: Name of the list in which this entry is placed
  179. *
  180. * This function returns (_type *) pointer to the very first entry of a
  181. * linker-generated array placed into subsection of .u_boot_list section
  182. * specified by _list argument.
  183. *
  184. * Since this macro defines an array start symbol, its leftmost index
  185. * must be 2 and its rightmost index must be 1.
  186. *
  187. * Example:
  188. * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub);
  189. */
  190. #define ll_entry_start(_type, _list) \
  191. ({ \
  192. static char start[0] __aligned(4) __attribute__((unused, \
  193. section(".u_boot_list_2_"#_list"_1"))); \
  194. (_type *)&start; \
  195. })
  196. /**
  197. * ll_entry_end() - Point after last entry of linker-generated array
  198. * @_type: Data type of the entry
  199. * @_list: Name of the list in which this entry is placed
  200. * (with underscores instead of dots)
  201. *
  202. * This function returns (_type *) pointer after the very last entry of
  203. * a linker-generated array placed into subsection of .u_boot_list
  204. * section specified by _list argument.
  205. *
  206. * Since this macro defines an array end symbol, its leftmost index
  207. * must be 2 and its rightmost index must be 3.
  208. *
  209. * Example:
  210. * struct my_sub_cmd *msc = ll_entry_end(struct my_sub_cmd, cmd_sub);
  211. */
  212. #define ll_entry_end(_type, _list) \
  213. ({ \
  214. static char end[0] __aligned(4) __attribute__((unused, \
  215. section(".u_boot_list_2_"#_list"_3"))); \
  216. (_type *)&end; \
  217. })
  218. /**
  219. * ll_entry_count() - Return the number of elements in linker-generated array
  220. * @_type: Data type of the entry
  221. * @_list: Name of the list of which the number of elements is computed
  222. *
  223. * This function returns the number of elements of a linker-generated array
  224. * placed into subsection of .u_boot_list section specified by _list
  225. * argument. The result is of an unsigned int type.
  226. *
  227. * Example:
  228. * int i;
  229. * const unsigned int count = ll_entry_count(struct my_sub_cmd, cmd_sub);
  230. * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub);
  231. * for (i = 0; i < count; i++, msc++)
  232. * printf("Entry %i, x=%i y=%i\n", i, msc->x, msc->y);
  233. */
  234. #define ll_entry_count(_type, _list) \
  235. ({ \
  236. _type *start = ll_entry_start(_type, _list); \
  237. _type *end = ll_entry_end(_type, _list); \
  238. unsigned int _ll_result = end - start; \
  239. _ll_result; \
  240. })
  241. /**
  242. * ll_entry_get() - Retrieve entry from linker-generated array by name
  243. * @_type: Data type of the entry
  244. * @_name: Name of the entry
  245. * @_list: Name of the list in which this entry is placed
  246. *
  247. * This function returns a pointer to a particular entry in linker-generated
  248. * array identified by the subsection of u_boot_list where the entry resides
  249. * and it's name.
  250. *
  251. * Example:
  252. * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = {
  253. * .x = 3,
  254. * .y = 4,
  255. * };
  256. * ...
  257. * struct my_sub_cmd *c = ll_entry_get(struct my_sub_cmd, my_sub_cmd, cmd_sub);
  258. */
  259. #define ll_entry_get(_type, _name, _list) \
  260. ({ \
  261. extern _type _u_boot_list_2_##_list##_2_##_name; \
  262. _type *_ll_result = \
  263. &_u_boot_list_2_##_list##_2_##_name; \
  264. _ll_result; \
  265. })
  266. /**
  267. * ll_start() - Point to first entry of first linker-generated array
  268. * @_type: Data type of the entry
  269. *
  270. * This function returns (_type *) pointer to the very first entry of
  271. * the very first linker-generated array.
  272. *
  273. * Since this macro defines the start of the linker-generated arrays,
  274. * its leftmost index must be 1.
  275. *
  276. * Example:
  277. * struct my_sub_cmd *msc = ll_start(struct my_sub_cmd);
  278. */
  279. #define ll_start(_type) \
  280. ({ \
  281. static char start[0] __aligned(4) __attribute__((unused, \
  282. section(".u_boot_list_1"))); \
  283. (_type *)&start; \
  284. })
  285. /**
  286. * ll_end() - Point after last entry of last linker-generated array
  287. * @_type: Data type of the entry
  288. *
  289. * This function returns (_type *) pointer after the very last entry of
  290. * the very last linker-generated array.
  291. *
  292. * Since this macro defines the end of the linker-generated arrays,
  293. * its leftmost index must be 3.
  294. *
  295. * Example:
  296. * struct my_sub_cmd *msc = ll_end(struct my_sub_cmd);
  297. */
  298. #define ll_end(_type) \
  299. ({ \
  300. static char end[0] __aligned(4) __attribute__((unused, \
  301. section(".u_boot_list_3"))); \
  302. (_type *)&end; \
  303. })
  304. #endif /* __ASSEMBLY__ */
  305. #endif /* __LINKER_LISTS_H__ */