4 /** @page ifdef_harmful #ifdef considered harmful
6 * Overuse of @c #ifdef has long been a problem in Etherboot.
7 * Etherboot provides a rich array of features, but all these features
8 * take up valuable space in a ROM image. The traditional solution to
9 * this problem has been for each feature to have its own @c #ifdef
10 * option, allowing the feature to be compiled in only if desired.
12 * The problem with this is that it becomes impossible to compile, let
13 * alone test, all possible versions of Etherboot. Code that is not
14 * typically used tends to suffer from bit-rot over time. It becomes
15 * extremely difficult to predict which combinations of compile-time
16 * options will result in code that can even compile and link
19 * To solve this problem, we have adopted a new approach from
20 * Etherboot 5.5 onwards. @c #ifdef is now "considered harmful", and
21 * its use should be minimised. Separate features should be
22 * implemented in separate @c .c files, and should \b always be
23 * compiled (i.e. they should \b not be guarded with a @c #ifdef @c
24 * MY_PET_FEATURE statement). By making (almost) all code always
25 * compile, we avoid the problem of bit-rot in rarely-used code.
27 * The file config.h, in combination with the @c make command line,
28 * specifies the objects that will be included in any particular build
29 * of Etherboot. For example, suppose that config.h includes the line
33 * #define CONSOLE_SERIAL
34 * #define DOWNLOAD_PROTO_TFTP
38 * When a particular Etherboot image (e.g. @c bin/rtl8139.zdsk) is
39 * built, the options specified in config.h are used to drag in the
40 * relevant objects at link-time. For the above example, serial.o and
41 * tftp.o would be linked in.
43 * There remains one problem to solve: how do these objects get used?
44 * Traditionally, we had code such as
48 * #ifdef CONSOLE_SERIAL
54 * in main.c, but this reintroduces @c #ifdef and so is a Bad Idea.
55 * We cannot simply remove the @c #ifdef and make it
63 * because then serial.o would end up always being linked in.
65 * The solution is to use @link tables.h linker tables @endlink.
73 * Read @ref ifdef_harmful first for some background on the motivation
74 * for using linker tables.
76 * This file provides macros for dealing with linker-generated tables
77 * of fixed-size symbols. We make fairly extensive use of these in
78 * order to avoid @c #ifdef spaghetti and/or linker symbol pollution.
79 * For example, instead of having code such as
83 * #ifdef CONSOLE_SERIAL
89 * we make serial.c generate an entry in the initialisation function
90 * table, and then have a function call_init_fns() that simply calls
91 * all functions present in this table. If and only if serial.o gets
92 * linked in, then its initialisation function will be called. We
93 * avoid linker symbol pollution (i.e. always dragging in serial.o
94 * just because of a call to serial_init()) and we also avoid @c
95 * #ifdef spaghetti (having to conditionalise every reference to
96 * functions in serial.c).
98 * The linker script takes care of assembling the tables for us. All
99 * our table sections have names of the format @c .tbl.NAME.NN where
100 * @c NAME designates the data structure stored in the table (e.g. @c
101 * init_fn) and @c NN is a two-digit decimal number used to impose an
102 * ordering upon the tables if required. @c NN=00 is reserved for the
103 * symbol indicating "table start", and @c NN=99 is reserved for the
104 * symbol indicating "table end".
106 * As an example, suppose that we want to create a "frobnicator"
107 * feature framework, and allow for several independent modules to
108 * provide frobnicating services. Then we would create a frob.h
109 * header file containing e.g.
113 * struct frobnicator {
114 * const char *name; // Name of the frobnicator
115 * void ( *frob ) ( void ); // The frobnicating function itself
118 * #define __frobnicator __table ( struct frobnicator, "frobnicators", 01 )
122 * Any module providing frobnicating services would look something
129 * static void my_frob ( void ) {
130 * // Do my frobnicating
134 * struct frob my_frobnicator __frobnicator = {
141 * The central frobnicator code (frob.c) would use the frobnicating
148 * // Call all linked-in frobnicators
149 * void frob_all ( void ) {
152 * for_each_table ( frob, "frobnicators" ) {
153 * printf ( "Calling frobnicator \"%s\"\n", frob->name );
160 * See init.h and init.c for a real-life example.
165 #define __attribute__( x )
168 #define __table_str( x ) #x
169 #define __table_section( table, idx ) \
170 __section__ ( ".tbl." table "." __table_str ( idx ) )
172 #define __table_section_start( table ) __table_section ( table, 00 )
173 #define __table_section_end( table ) __table_section ( table, 99 )
175 #define __natural_alignment( type ) __aligned__ ( __alignof__ ( type ) )
178 * Linker table entry.
180 * Declares a data structure to be part of a linker table. Use as
185 * #define __frobnicator __table ( struct frobnicator, "frobnicators", 01 )
187 * struct frobnicator my_frob __frobnicator = {
194 #define __table( type, table, idx ) \
195 __attribute__ (( __table_section ( table, idx ), \
196 __natural_alignment ( type ) ))
199 * Start of linker table.
201 * Return the start of a linker table. Use as e.g.
205 * struct frobnicator *frobs =
206 * table_start ( struct frobnicator, "frobnicators" );
211 #define table_start( type, table ) ( { \
212 static type __table_start[0] __table ( type, table, 00 ); \
216 * End of linker table.
218 * Return the end of a linker table. Use as e.g.
222 * struct frobnicator *frobs_end =
223 * table_end ( struct frobnicator, "frobnicators" );
228 #define table_end( type, table ) ( { \
229 static type __table_end[0] __table ( type, table, 99 ); \
233 * Calculate number of entries in linker table.
235 * Return the number of entries within a linker table. Use as e.g.
239 * unsigned int num_frobs =
240 * table_num_entries ( struct frobnicator, "frobnicators" );
245 #define table_num_entries( type, table ) \
246 ( ( unsigned int ) ( table_end ( type, table ) - \
247 table_start ( type, table ) ) )
250 * Iterate through all entries within a linker table.
256 * struct frobnicator *frob;
258 * for_each_table_entry ( frob, "frobnicators" ) {
265 #define for_each_table_entry( pointer, table ) \
266 for ( pointer = table_start ( typeof ( * pointer ), table ) ; \
267 pointer < table_end ( typeof ( * pointer ), table ) ; \
271 * Iterate through all entries within a linker table in reverse order.
277 * struct frobnicator *frob;
279 * for_each_table_entry_reverse ( frob, "frobnicators" ) {
286 #define for_each_table_entry_reverse( pointer, table ) \
287 for ( pointer = table_end ( typeof ( * pointer ), table ) - 1 ; \
288 pointer >= table_start ( typeof ( * pointer ), table ) ; \
291 #endif /* _GPXE_TABLES_H */