iPXE
|
Linker tables. More...
Go to the source code of this file.
Macros | |
#define | __attribute__(x) |
#define | __table(type, name) ( type, name ) |
Declare a linker table. More... | |
#define | __table_type(table) __table_extract_type table |
Get linker table data type. More... | |
#define | __table_extract_type(type, name) type |
#define | __table_name(table) __table_extract_name table |
Get linker table name. More... | |
#define | __table_extract_name(type, name) name |
#define | __table_section(table, idx) ".tbl." __table_name ( table ) "." __table_str ( idx ) |
Get linker table section name. More... | |
#define | __table_str(x) #x |
#define | __table_alignment(table) __alignof__ ( __table_type ( table ) ) |
Get linker table alignment. More... | |
#define | __table_entry(table, idx) |
Declare a linker table entry. More... | |
#define | __table_entries(table, idx) |
Get start of linker table entries. More... | |
#define | __TABLE_ENTRIES(entries, table, idx) |
Declare start of linker table entries. More... | |
#define | table_start(table) __table_entries ( table, 00 ) |
Get start of linker table. More... | |
#define | TABLE_START(start, table) __TABLE_ENTRIES ( start, table, 00 ) |
Declare start of linker table. More... | |
#define | table_end(table) __table_entries ( table, 99 ) |
Get end of linker table. More... | |
#define | TABLE_END(end, table) __TABLE_ENTRIES ( end, table, 99 ) |
Declare end of linker table. More... | |
#define | table_num_entries(table) |
Get number of entries in linker table. More... | |
#define | table_index(table, entry) ( ( unsigned int ) ( (entry) - table_start ( table ) ) ) |
Get index of entry within linker table. More... | |
#define | for_each_table_entry(pointer, table) |
Iterate through all entries within a linker table. More... | |
#define | for_each_table_entry_continue(pointer, table) |
Iterate through all remaining entries within a linker table. More... | |
#define | for_each_table_entry_reverse(pointer, table) |
Iterate through all entries within a linker table in reverse order. More... | |
#define | for_each_table_entry_continue_reverse(pointer, table) |
Iterate through all remaining entries within a linker table in reverse order. More... | |
Functions | |
FILE_LICENCE (GPL2_OR_LATER_OR_UBDL) | |
Linker tables.
Read #ifdef considered harmful first for some background on the motivation for using linker tables.
This file provides macros for dealing with linker-generated tables of fixed-size symbols. We make fairly extensive use of these in order to avoid #ifdef
spaghetti and/or linker symbol pollution. For example, instead of having code such as
we make serial.c generate an entry in the initialisation function table, and then have a function call_init_fns() that simply calls all functions present in this table. If and only if serial.o gets linked in, then its initialisation function will be called. We avoid linker symbol pollution (i.e. always dragging in serial.o just because of a call to serial_init()) and we also avoid #ifdef
spaghetti (having to conditionalise every reference to functions in serial.c).
The linker script takes care of assembling the tables for us. All our table sections have names of the format .tbl.NAME.NN where
NAME
designates the data structure stored in the table (e.g. init_fns
) and NN
is a two-digit decimal number used to impose an ordering upon the tables if required. NN=00
is reserved for the symbol indicating "table start", and NN=99
is reserved for the symbol indicating "table end".
As an example, suppose that we want to create a "frobnicator" feature framework, and allow for several independent modules to provide frobnicating services. Then we would create a frob.h header file containing e.g.
Any module providing frobnicating services would look something like
The central frobnicator code (frob.c) would use the frobnicating modules as follows
See init.h and init.c for a real-life example.
Definition in file tables.h.
#define __attribute__ | ( | x | ) |
#define __table_type | ( | table | ) | __table_extract_type table |
#define __table_name | ( | table | ) | __table_extract_name table |
#define __table_section | ( | table, | |
idx | |||
) | ".tbl." __table_name ( table ) "." __table_str ( idx ) |
#define __table_alignment | ( | table | ) | __alignof__ ( __table_type ( table ) ) |
#define __table_entry | ( | table, | |
idx | |||
) |
Declare a linker table entry.
table | Linker table |
idx | Sub-table index |
Example usage:
#define __table_entries | ( | table, | |
idx | |||
) |
Get start of linker table entries.
table | Linker table |
idx | Sub-table index |
entries | Start of entries |
#define __TABLE_ENTRIES | ( | entries, | |
table, | |||
idx | |||
) |
Declare start of linker table entries.
entries | Start of entries |
table | Linker table |
idx | Sub-table index |
#define table_start | ( | table | ) | __table_entries ( table, 00 ) |
Get start of linker table.
table | Linker table |
start | Start of linker table |
Example usage:
#define TABLE_START | ( | start, | |
table | |||
) | __TABLE_ENTRIES ( start, table, 00 ) |
#define table_end | ( | table | ) | __table_entries ( table, 99 ) |
#define TABLE_END | ( | end, | |
table | |||
) | __TABLE_ENTRIES ( end, table, 99 ) |
#define table_num_entries | ( | table | ) |
Get number of entries in linker table.
table | Linker table |
num_entries | Number of entries in linker table |
Example usage:
#define table_index | ( | table, | |
entry | |||
) | ( ( unsigned int ) ( (entry) - table_start ( table ) ) ) |
Get index of entry within linker table.
table | Linker table |
entry | Table entry |
Example usage:
#define for_each_table_entry | ( | pointer, | |
table | |||
) |
Iterate through all entries within a linker table.
pointer | Entry pointer |
table | Linker table |
Example usage:
#define for_each_table_entry_continue | ( | pointer, | |
table | |||
) |
Iterate through all remaining entries within a linker table.
pointer | Entry pointer, preset to most recent entry |
table | Linker table |
Example usage:
#define for_each_table_entry_reverse | ( | pointer, | |
table | |||
) |
Iterate through all entries within a linker table in reverse order.
pointer | Entry pointer |
table | Linker table |
Example usage:
#define for_each_table_entry_continue_reverse | ( | pointer, | |
table | |||
) |
Iterate through all remaining entries within a linker table in reverse order.
pointer | Entry pointer, preset to most recent entry |
table | Linker table |
Example usage:
FILE_LICENCE | ( | GPL2_OR_LATER_OR_UBDL | ) |