summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoshua Oreman2009-11-26 03:10:05 +0100
committerMarty Connor2010-01-20 23:05:25 +0100
commit2d58a62330c755dcde93ccaf09def7f8c2fae158 (patch)
tree9136f497f512da4359512286709ae1a2c01c6ca8
parent[contrib] Add README file to rom-o-matic (diff)
downloadipxe-2d58a62330c755dcde93ccaf09def7f8c2fae158.tar.gz
ipxe-2d58a62330c755dcde93ccaf09def7f8c2fae158.tar.xz
ipxe-2d58a62330c755dcde93ccaf09def7f8c2fae158.zip
[linker] Add safe weak symbol macros
Weak symbols are a useful tool in eliminating unnecessary dependencies between object files, but they are somewhat dangerous because one must remember to test the weak symbol against NULL before using it. To rectify that, add macros for declaring weak functions that will return a default value inline if the file defining them is not available at link time. Signed-off-by: Marty Connor <mdc@etherboot.org>
-rw-r--r--src/include/compiler.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/include/compiler.h b/src/include/compiler.h
index f481f38a..ea81fc89 100644
--- a/src/include/compiler.h
+++ b/src/include/compiler.h
@@ -179,6 +179,42 @@ REQUEST_EXPANDED ( CONFIG_SYMBOL );
/** Select file identifier for errno.h (if used) */
#define ERRFILE PREFIX_OBJECT ( ERRFILE_ )
+/**
+ * @defgroup weakmacros Macros to manage weak symbol definitions
+ *
+ * Weak symbols allow one to reference a function in another file
+ * without necessarily requiring that file to be linked in. In their
+ * native form, the function will be @c NULL if its file is not linked
+ * in; these macros provide an inline wrapper that returns an
+ * appropriate error indication or default value.
+ *
+ * @{
+ */
+#ifndef ASSEMBLY
+
+/** Mangle @a name into its weakly-referenced implementation */
+#define __weak_impl( name ) _w_ ## name
+
+/**
+ * Declare a weak function with inline safety wrapper
+ *
+ * @v ret Return type of weak function
+ * @v name Name of function to expose
+ * @v proto Parenthesized list of arguments with types
+ * @v args Parenthesized list of argument names
+ * @v dfl Value to return if weak function is not available
+ */
+#define __weak_decl( ret, name, proto, args, dfl ) \
+ ret __weak_impl( name ) proto __attribute__ (( weak )); \
+ static inline ret name proto { \
+ if ( __weak_impl( name ) ) \
+ return __weak_impl( name ) args; \
+ return dfl; \
+ }
+
+#endif
+/** @} */
+
/** @defgroup dbg Debugging infrastructure
* @{
*/