Browse Source

[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>
tags/v1.0.0-rc1
Joshua Oreman 15 years ago
parent
commit
2d58a62330
1 changed files with 36 additions and 0 deletions
  1. 36
    0
      src/include/compiler.h

+ 36
- 0
src/include/compiler.h View File

@@ -179,6 +179,42 @@ REQUEST_EXPANDED ( CONFIG_SYMBOL );
179 179
 /** Select file identifier for errno.h (if used) */
180 180
 #define ERRFILE PREFIX_OBJECT ( ERRFILE_ )
181 181
 
182
+/**
183
+ * @defgroup weakmacros Macros to manage weak symbol definitions
184
+ *
185
+ * Weak symbols allow one to reference a function in another file
186
+ * without necessarily requiring that file to be linked in. In their
187
+ * native form, the function will be @c NULL if its file is not linked
188
+ * in; these macros provide an inline wrapper that returns an
189
+ * appropriate error indication or default value.
190
+ *
191
+ * @{
192
+ */
193
+#ifndef ASSEMBLY
194
+
195
+/** Mangle @a name into its weakly-referenced implementation */
196
+#define __weak_impl( name )   _w_ ## name
197
+
198
+/**
199
+ * Declare a weak function with inline safety wrapper
200
+ *
201
+ * @v ret	Return type of weak function
202
+ * @v name	Name of function to expose
203
+ * @v proto	Parenthesized list of arguments with types
204
+ * @v args	Parenthesized list of argument names
205
+ * @v dfl	Value to return if weak function is not available
206
+ */
207
+#define __weak_decl( ret, name, proto, args, dfl )		\
208
+        ret __weak_impl( name ) proto __attribute__ (( weak ));	\
209
+        static inline ret name proto {				\
210
+                if ( __weak_impl( name ) )			\
211
+                        return __weak_impl( name ) args;	\
212
+                return dfl;					\
213
+        }
214
+
215
+#endif
216
+/** @} */
217
+
182 218
 /** @defgroup dbg Debugging infrastructure
183 219
  * @{
184 220
  */

Loading…
Cancel
Save