linker_lists.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * include/linker_lists.h
  4. *
  5. * Implementation of linker-generated arrays
  6. *
  7. * Copyright (C) 2012 Marek Vasut <marex@denx.de>
  8. */
  9. #ifndef __LINKER_LISTS_H__
  10. #define __LINKER_LISTS_H__
  11. #include <linux/compiler.h>
  12. /*
  13. * There is no use in including this from ASM files.
  14. * So just don't define anything when included from ASM.
  15. */
  16. #if !defined(__ASSEMBLY__)
  17. /**
  18. * llsym() - Access a linker-generated array entry
  19. * @_type: Data type of the entry
  20. * @_name: Name of the entry
  21. * @_list: name of the list. Should contain only characters allowed
  22. * in a C variable name!
  23. */
  24. #define llsym(_type, _name, _list) \
  25. ((_type *)&_u_boot_list_2_##_list##_2_##_name)
  26. /**
  27. * ll_entry_declare() - Declare linker-generated array entry
  28. * @_type: Data type of the entry
  29. * @_name: Name of the entry
  30. * @_list: name of the list. Should contain only characters allowed
  31. * in a C variable name!
  32. *
  33. * This macro declares a variable that is placed into a linker-generated
  34. * array. This is a basic building block for more advanced use of linker-
  35. * generated arrays. The user is expected to build their own macro wrapper
  36. * around this one.
  37. *
  38. * A variable declared using this macro must be compile-time initialized.
  39. *
  40. * Special precaution must be made when using this macro:
  41. *
  42. * 1) The _type must not contain the "static" keyword, otherwise the
  43. * entry is generated and can be iterated but is listed in the map
  44. * file and cannot be retrieved by name.
  45. *
  46. * 2) In case a section is declared that contains some array elements AND
  47. * a subsection of this section is declared and contains some elements,
  48. * it is imperative that the elements are of the same type.
  49. *
  50. * 3) In case an outer section is declared that contains some array elements
  51. * AND an inner subsection of this section is declared and contains some
  52. * elements, then when traversing the outer section, even the elements of
  53. * the inner sections are present in the array.
  54. *
  55. * Example:
  56. *
  57. * ::
  58. *
  59. * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = {
  60. * .x = 3,
  61. * .y = 4,
  62. * };
  63. */
  64. #define ll_entry_declare(_type, _name, _list) \
  65. _type _u_boot_list_2_##_list##_2_##_name __aligned(4) \
  66. __attribute__((unused, \
  67. section(".u_boot_list_2_"#_list"_2_"#_name)))
  68. /**
  69. * ll_entry_declare_list() - Declare a list of link-generated array entries
  70. * @_type: Data type of each entry
  71. * @_name: Name of the entry
  72. * @_list: name of the list. Should contain only characters allowed
  73. * in a C variable name!
  74. *
  75. * This is like ll_entry_declare() but creates multiple entries. It should
  76. * be assigned to an array.
  77. *
  78. * ::
  79. *
  80. * ll_entry_declare_list(struct my_sub_cmd, my_sub_cmd, cmd_sub) = {
  81. * { .x = 3, .y = 4 },
  82. * { .x = 8, .y = 2 },
  83. * { .x = 1, .y = 7 }
  84. * };
  85. */
  86. #define ll_entry_declare_list(_type, _name, _list) \
  87. _type _u_boot_list_2_##_list##_2_##_name[] __aligned(4) \
  88. __attribute__((unused, \
  89. section(".u_boot_list_2_"#_list"_2_"#_name)))
  90. /*
  91. * We need a 0-byte-size type for iterator symbols, and the compiler
  92. * does not allow defining objects of C type 'void'. Using an empty
  93. * struct is allowed by the compiler, but causes gcc versions 4.4 and
  94. * below to complain about aliasing. Therefore we use the next best
  95. * thing: zero-sized arrays, which are both 0-byte-size and exempt from
  96. * aliasing warnings.
  97. */
  98. /**
  99. * ll_entry_start() - Point to first entry of linker-generated array
  100. * @_type: Data type of the entry
  101. * @_list: Name of the list in which this entry is placed
  102. *
  103. * This function returns ``(_type *)`` pointer to the very first entry of a
  104. * linker-generated array placed into subsection of .u_boot_list section
  105. * specified by _list argument.
  106. *
  107. * Since this macro defines an array start symbol, its leftmost index
  108. * must be 2 and its rightmost index must be 1.
  109. *
  110. * Example:
  111. *
  112. * ::
  113. *
  114. * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub);
  115. */
  116. #define ll_entry_start(_type, _list) \
  117. ({ \
  118. static char start[0] __aligned(4) __attribute__((unused, \
  119. section(".u_boot_list_2_"#_list"_1"))); \
  120. (_type *)&start; \
  121. })
  122. /**
  123. * ll_entry_end() - Point after last entry of linker-generated array
  124. * @_type: Data type of the entry
  125. * @_list: Name of the list in which this entry is placed
  126. * (with underscores instead of dots)
  127. *
  128. * This function returns ``(_type *)`` pointer after the very last entry of
  129. * a linker-generated array placed into subsection of .u_boot_list
  130. * section specified by _list argument.
  131. *
  132. * Since this macro defines an array end symbol, its leftmost index
  133. * must be 2 and its rightmost index must be 3.
  134. *
  135. * Example:
  136. *
  137. * ::
  138. *
  139. * struct my_sub_cmd *msc = ll_entry_end(struct my_sub_cmd, cmd_sub);
  140. */
  141. #define ll_entry_end(_type, _list) \
  142. ({ \
  143. static char end[0] __aligned(4) __attribute__((unused, \
  144. section(".u_boot_list_2_"#_list"_3"))); \
  145. (_type *)&end; \
  146. })
  147. /**
  148. * ll_entry_count() - Return the number of elements in linker-generated array
  149. * @_type: Data type of the entry
  150. * @_list: Name of the list of which the number of elements is computed
  151. *
  152. * This function returns the number of elements of a linker-generated array
  153. * placed into subsection of .u_boot_list section specified by _list
  154. * argument. The result is of an unsigned int type.
  155. *
  156. * Example:
  157. *
  158. * ::
  159. *
  160. * int i;
  161. * const unsigned int count = ll_entry_count(struct my_sub_cmd, cmd_sub);
  162. * struct my_sub_cmd *msc = ll_entry_start(struct my_sub_cmd, cmd_sub);
  163. * for (i = 0; i < count; i++, msc++)
  164. * printf("Entry %i, x=%i y=%i\n", i, msc->x, msc->y);
  165. */
  166. #define ll_entry_count(_type, _list) \
  167. ({ \
  168. _type *start = ll_entry_start(_type, _list); \
  169. _type *end = ll_entry_end(_type, _list); \
  170. unsigned int _ll_result = end - start; \
  171. _ll_result; \
  172. })
  173. /**
  174. * ll_entry_get() - Retrieve entry from linker-generated array by name
  175. * @_type: Data type of the entry
  176. * @_name: Name of the entry
  177. * @_list: Name of the list in which this entry is placed
  178. *
  179. * This function returns a pointer to a particular entry in linker-generated
  180. * array identified by the subsection of u_boot_list where the entry resides
  181. * and it's name.
  182. *
  183. * Example:
  184. *
  185. * ::
  186. *
  187. * ll_entry_declare(struct my_sub_cmd, my_sub_cmd, cmd_sub) = {
  188. * .x = 3,
  189. * .y = 4,
  190. * };
  191. * ...
  192. * struct my_sub_cmd *c = ll_entry_get(struct my_sub_cmd, my_sub_cmd, cmd_sub);
  193. */
  194. #define ll_entry_get(_type, _name, _list) \
  195. ({ \
  196. extern _type _u_boot_list_2_##_list##_2_##_name; \
  197. _type *_ll_result = \
  198. &_u_boot_list_2_##_list##_2_##_name; \
  199. _ll_result; \
  200. })
  201. /**
  202. * ll_start() - Point to first entry of first linker-generated array
  203. * @_type: Data type of the entry
  204. *
  205. * This function returns ``(_type *)`` pointer to the very first entry of
  206. * the very first linker-generated array.
  207. *
  208. * Since this macro defines the start of the linker-generated arrays,
  209. * its leftmost index must be 1.
  210. *
  211. * Example:
  212. *
  213. * ::
  214. *
  215. * struct my_sub_cmd *msc = ll_start(struct my_sub_cmd);
  216. */
  217. #define ll_start(_type) \
  218. ({ \
  219. static char start[0] __aligned(4) __attribute__((unused, \
  220. section(".u_boot_list_1"))); \
  221. (_type *)&start; \
  222. })
  223. /**
  224. * ll_end() - Point after last entry of last linker-generated array
  225. * @_type: Data type of the entry
  226. *
  227. * This function returns ``(_type *)`` pointer after the very last entry of
  228. * the very last linker-generated array.
  229. *
  230. * Since this macro defines the end of the linker-generated arrays,
  231. * its leftmost index must be 3.
  232. *
  233. * Example:
  234. *
  235. * ::
  236. *
  237. * struct my_sub_cmd *msc = ll_end(struct my_sub_cmd);
  238. */
  239. #define ll_end(_type) \
  240. ({ \
  241. static char end[0] __aligned(4) __attribute__((unused, \
  242. section(".u_boot_list_3"))); \
  243. (_type *)&end; \
  244. })
  245. #endif /* __ASSEMBLY__ */
  246. #endif /* __LINKER_LISTS_H__ */