linker_lists.h 10 KB

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