Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

tables.h 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #ifndef _GPXE_TABLES_H
  2. #define _GPXE_TABLES_H
  3. /** @page ifdef_harmful #ifdef considered harmful
  4. *
  5. * Overuse of @c #ifdef has long been a problem in Etherboot.
  6. * Etherboot provides a rich array of features, but all these features
  7. * take up valuable space in a ROM image. The traditional solution to
  8. * this problem has been for each feature to have its own @c #ifdef
  9. * option, allowing the feature to be compiled in only if desired.
  10. *
  11. * The problem with this is that it becomes impossible to compile, let
  12. * alone test, all possible versions of Etherboot. Code that is not
  13. * typically used tends to suffer from bit-rot over time. It becomes
  14. * extremely difficult to predict which combinations of compile-time
  15. * options will result in code that can even compile and link
  16. * correctly.
  17. *
  18. * To solve this problem, we have adopted a new approach from
  19. * Etherboot 5.5 onwards. @c #ifdef is now "considered harmful", and
  20. * its use should be minimised. Separate features should be
  21. * implemented in separate @c .c files, and should \b always be
  22. * compiled (i.e. they should \b not be guarded with a @c #ifdef @c
  23. * MY_PET_FEATURE statement). By making (almost) all code always
  24. * compile, we avoid the problem of bit-rot in rarely-used code.
  25. *
  26. * The file config.h, in combination with the @c make command line,
  27. * specifies the objects that will be included in any particular build
  28. * of Etherboot. For example, suppose that config.h includes the line
  29. *
  30. * @code
  31. *
  32. * #define CONSOLE_SERIAL
  33. * #define DOWNLOAD_PROTO_TFTP
  34. *
  35. * @endcode
  36. *
  37. * When a particular Etherboot image (e.g. @c bin/rtl8139.zdsk) is
  38. * built, the options specified in config.h are used to drag in the
  39. * relevant objects at link-time. For the above example, serial.o and
  40. * tftp.o would be linked in.
  41. *
  42. * There remains one problem to solve: how do these objects get used?
  43. * Traditionally, we had code such as
  44. *
  45. * @code
  46. *
  47. * #ifdef CONSOLE_SERIAL
  48. * serial_init();
  49. * #endif
  50. *
  51. * @endcode
  52. *
  53. * in main.c, but this reintroduces @c #ifdef and so is a Bad Idea.
  54. * We cannot simply remove the @c #ifdef and make it
  55. *
  56. * @code
  57. *
  58. * serial_init();
  59. *
  60. * @endcode
  61. *
  62. * because then serial.o would end up always being linked in.
  63. *
  64. * The solution is to use @link tables.h linker tables @endlink.
  65. *
  66. */
  67. /** @file
  68. *
  69. * Linker tables
  70. *
  71. * Read @ref ifdef_harmful first for some background on the motivation
  72. * for using linker tables.
  73. *
  74. * This file provides macros for dealing with linker-generated tables
  75. * of fixed-size symbols. We make fairly extensive use of these in
  76. * order to avoid @c #ifdef spaghetti and/or linker symbol pollution.
  77. * For example, instead of having code such as
  78. *
  79. * @code
  80. *
  81. * #ifdef CONSOLE_SERIAL
  82. * serial_init();
  83. * #endif
  84. *
  85. * @endcode
  86. *
  87. * we make serial.c generate an entry in the initialisation function
  88. * table, and then have a function call_init_fns() that simply calls
  89. * all functions present in this table. If and only if serial.o gets
  90. * linked in, then its initialisation function will be called. We
  91. * avoid linker symbol pollution (i.e. always dragging in serial.o
  92. * just because of a call to serial_init()) and we also avoid @c
  93. * #ifdef spaghetti (having to conditionalise every reference to
  94. * functions in serial.c).
  95. *
  96. * The linker script takes care of assembling the tables for us. All
  97. * our table sections have names of the format @c .tbl.NAME.NN where
  98. * @c NAME designates the data structure stored in the table (e.g. @c
  99. * init_fn) and @c NN is a two-digit decimal number used to impose an
  100. * ordering upon the tables if required. @c NN=00 is reserved for the
  101. * symbol indicating "table start", and @c NN=99 is reserved for the
  102. * symbol indicating "table end".
  103. *
  104. * As an example, suppose that we want to create a "frobnicator"
  105. * feature framework, and allow for several independent modules to
  106. * provide frobnicating services. Then we would create a frob.h
  107. * header file containing e.g.
  108. *
  109. * @code
  110. *
  111. * struct frobnicator {
  112. * const char *name; // Name of the frobnicator
  113. * void ( *frob ) ( void ); // The frobnicating function itself
  114. * };
  115. *
  116. * #define __frobnicator __table ( frobnicators, 01 )
  117. *
  118. * @endcode
  119. *
  120. * Any module providing frobnicating services would look something
  121. * like
  122. *
  123. * @code
  124. *
  125. * #include "frob.h"
  126. *
  127. * static void my_frob ( void ) {
  128. * // Do my frobnicating
  129. * ...
  130. * }
  131. *
  132. * struct frob my_frobnicator __frobnicator = {
  133. * .name = "my_frob",
  134. * .frob = my_frob,
  135. * };
  136. *
  137. * @endcode
  138. *
  139. * The central frobnicator code (frob.c) would use the frobnicating
  140. * modules as follows
  141. *
  142. * @code
  143. *
  144. * #include "frob.h"
  145. *
  146. * static struct frob frob_start[0] __table_start ( frobnicators );
  147. * static struct frob frob_end[0] __table_end ( frobnicators );
  148. *
  149. * // Call all linked-in frobnicators
  150. * void frob_all ( void ) {
  151. * struct frob *frob;
  152. *
  153. * for ( frob = frob_start ; frob < frob_end ; frob++ ) {
  154. * printf ( "Calling frobnicator \"%s\"\n", frob->name );
  155. * frob->frob ();
  156. * }
  157. * }
  158. *
  159. * @endcode
  160. *
  161. * See init.h and init.c for a real-life example.
  162. *
  163. */
  164. #ifdef DOXYGEN
  165. #define __attribute__( x )
  166. #endif
  167. #define __table_str( x ) #x
  168. #define __table_section( table, idx ) \
  169. __section__ ( ".tbl." __table_str ( table ) "." __table_str ( idx ) )
  170. #define __table_section_start( table ) __table_section ( table, 00 )
  171. #define __table_section_end( table ) __table_section ( table, 99 )
  172. #define __natural_alignment( type ) __aligned__ ( __alignof__ ( type ) )
  173. /**
  174. * Linker table entry.
  175. *
  176. * Declares a data structure to be part of a linker table. Use as
  177. * e.g.
  178. *
  179. * @code
  180. *
  181. * struct my_foo __table ( foo, 01 ) = {
  182. * ...
  183. * };
  184. *
  185. * @endcode
  186. *
  187. */
  188. #define __table( type, table, idx ) \
  189. __attribute__ (( __table_section ( table, idx ), \
  190. __natural_alignment ( type ) ))
  191. /**
  192. * Linker table start marker.
  193. *
  194. * Declares a data structure (usually an empty data structure) to be
  195. * the start of a linker table. Use as e.g.
  196. *
  197. * @code
  198. *
  199. * static struct foo_start[0] __table_start ( foo );
  200. *
  201. * @endcode
  202. *
  203. */
  204. #define __table_start( type, table ) __table ( type, table, 00 )
  205. /**
  206. * Linker table end marker.
  207. *
  208. * Declares a data structure (usually an empty data structure) to be
  209. * the end of a linker table. Use as e.g.
  210. *
  211. * @code
  212. *
  213. * static struct foo_end[0] __table_end ( foo );
  214. *
  215. * @endcode
  216. *
  217. */
  218. #define __table_end( type, table ) __table ( type, table, 99 )
  219. #endif /* _GPXE_TABLES_H */