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 ( 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 * static struct frob frob_start[0] __table_start ( frobnicators );
149 * static struct frob frob_end[0] __table_end ( frobnicators );
151 * // Call all linked-in frobnicators
152 * void frob_all ( void ) {
155 * for ( frob = frob_start ; frob < frob_end ; frob++ ) {
156 * printf ( "Calling frobnicator \"%s\"\n", frob->name );
163 * See init.h and init.c for a real-life example.
168 #define __attribute__( x )
171 #define __table_str( x ) #x
172 #define __table_section( table, idx ) \
173 __section__ ( ".tbl." __table_str ( table ) "." __table_str ( idx ) )
175 #define __table_section_start( table ) __table_section ( table, 00 )
176 #define __table_section_end( table ) __table_section ( table, 99 )
178 #define __natural_alignment( type ) __aligned__ ( __alignof__ ( type ) )
181 * Linker table entry.
183 * Declares a data structure to be part of a linker table. Use as
188 * struct my_foo __table ( foo, 01 ) = {
195 #define __table( type, table, idx ) \
196 __attribute__ (( __table_section ( table, idx ), \
197 __natural_alignment ( type ) ))
200 * Linker table start marker.
202 * Declares a data structure (usually an empty data structure) to be
203 * the start of a linker table. Use as e.g.
207 * static struct foo_start[0] __table_start ( foo );
212 #define __table_start( type, table ) __table ( type, table, 00 )
215 * Linker table end marker.
217 * Declares a data structure (usually an empty data structure) to be
218 * the end of a linker table. Use as e.g.
222 * static struct foo_end[0] __table_end ( foo );
227 #define __table_end( type, table ) __table ( type, table, 99 )
229 #endif /* _GPXE_TABLES_H */