Переглянути джерело

[linker] Expand and correct symbol requirement macros

REQUIRE_SYMBOL() formerly used a formulation of symbol requirement
that would allow a link to succeed despite lacking a required symbol,
because it did not introduce any relocations. Fix by renaming it to
REQUEST_SYMBOL() (since the soft-requirement behavior can be useful)
and add a REQUIRE_SYMBOL() that truly requires.

Add EXPORT_SYMBOL() and IMPORT_SYMBOL() for REQUEST_SYMBOL()-like
behavior that allows one to make use of the symbol, by combining a
weak external on the symbol itself with a REQUEST_SYMBOL() of a second
symbol.

Signed-off-by: Marty Connor <mdc@etherboot.org>
tags/v1.0.0-rc1
Joshua Oreman 15 роки тому
джерело
коміт
572e61754a

+ 1
- 0
src/arch/i386/scripts/i386-kir.lds Переглянути файл

@@ -130,6 +130,7 @@ SECTIONS {
130 130
     /DISCARD/ : {
131 131
 	*(.comment)
132 132
 	*(.note)
133
+	*(.discard)
133 134
     }
134 135
 
135 136
     /*

+ 1
- 0
src/arch/i386/scripts/i386.lds Переглянути файл

@@ -152,6 +152,7 @@ SECTIONS {
152 152
 	*(.eh_frame.*)
153 153
 	*(.rel)
154 154
 	*(.rel.*)
155
+	*(.discard)
155 156
     }
156 157
 
157 158
     /*

+ 1
- 0
src/arch/x86/scripts/efi.lds Переглянути файл

@@ -101,5 +101,6 @@ SECTIONS {
101 101
 	*(.eh_frame.*)
102 102
 	*(.rel)
103 103
 	*(.rel.*)
104
+	*(.discard)
104 105
     }
105 106
 }

+ 95
- 10
src/include/compiler.h Переглянути файл

@@ -37,6 +37,21 @@
37 37
 #endif
38 38
 #endif /* ASSEMBLY */
39 39
 
40
+#undef _S1
41
+#undef _S2
42
+#undef _C1
43
+#undef _C2
44
+
45
+/** Concatenate non-expanded arguments */
46
+#define _C1( x, y ) x ## y
47
+/** Concatenate expanded arguments */
48
+#define _C2( x, y ) _C1 ( x, y )
49
+
50
+/** Stringify non-expanded argument */
51
+#define _S1( x ) #x
52
+/** Stringify expanded argument */
53
+#define _S2( x ) _S1 ( x )
54
+
40 55
 /**
41 56
  * @defgroup symmacros Macros to provide or require explicit symbols
42 57
  * @{
@@ -52,15 +67,89 @@
52 67
 	char _sym[0]
53 68
 #endif /* ASSEMBLY */
54 69
 
55
-/** Require a symbol within this object file */
70
+/** Require a symbol within this object file
71
+ *
72
+ * The symbol is referenced by a relocation in a discarded section, so
73
+ * if it is not available at link time the link will fail.
74
+ */
56 75
 #ifdef ASSEMBLY
57 76
 #define REQUIRE_SYMBOL( _sym )				\
58
-	.equ	__need_ # _sym, _sym
77
+	.section ".discard", "a", @progbits ;		\
78
+	.extern	_sym ;					\
79
+	.long	_sym ;					\
80
+	.previous
59 81
 #else /* ASSEMBLY */
60 82
 #define REQUIRE_SYMBOL( _sym )				\
83
+	extern char _sym;				\
84
+	static char * _C2 ( _C2 ( __require_, _sym ), _C2 ( _, __LINE__ ) ) \
85
+		__attribute__ (( section ( ".discard" ), used )) \
86
+		= &_sym
87
+#endif
88
+
89
+/** Request that a symbol be available at runtime
90
+ *
91
+ * The requested symbol is entered as undefined into the symbol table
92
+ * for this object, so the linker will pull in other object files as
93
+ * necessary to satisfy the reference. However, the undefined symbol
94
+ * is not referenced in any relocations, so the link can still succeed
95
+ * if no file contains it.
96
+ *
97
+ * A symbol passed to this macro may not be referenced anywhere
98
+ * else in the file. If you want to do that, see IMPORT_SYMBOL().
99
+ */
100
+#ifdef ASSEMBLY
101
+#define REQUEST_SYMBOL( _sym )				\
102
+	.equ	__need_ ## _sym, _sym
103
+#else /* ASSEMBLY */
104
+#define REQUEST_SYMBOL( _sym )				\
61 105
 	__asm__ ( ".equ\t__need_" #_sym ", " #_sym )
62 106
 #endif /* ASSEMBLY */
63 107
 
108
+/** Set up a symbol to be usable in another file by IMPORT_SYMBOL()
109
+ *
110
+ * The symbol must already be marked as global.
111
+ */
112
+#define EXPORT_SYMBOL( _sym )	PROVIDE_SYMBOL ( __export_ ## _sym )
113
+
114
+/** Make a symbol usable to this file if available at link time
115
+ *
116
+ * If no file passed to the linker contains the symbol, it will have
117
+ * @c NULL value to future uses. Keep in mind that the symbol value is
118
+ * really the @e address of a variable or function; see the code
119
+ * snippet below.
120
+ *
121
+ * In C using IMPORT_SYMBOL, you must specify the declaration as the
122
+ * second argument, for instance
123
+ *
124
+ * @code
125
+ *   IMPORT_SYMBOL ( my_func, int my_func ( int arg ) );
126
+ *   IMPORT_SYMBOL ( my_var, int my_var );
127
+ *
128
+ *   void use_imports ( void ) {
129
+ * 	if ( my_func && &my_var )
130
+ * 	   my_var = my_func ( my_var );
131
+ *   }
132
+ * @endcode
133
+ *
134
+ * GCC considers a weak declaration to override a strong one no matter
135
+ * which comes first, so it is safe to include a header file declaring
136
+ * the imported symbol normally, but providing the declaration to
137
+ * IMPORT_SYMBOL is still required.
138
+ *
139
+ * If no EXPORT_SYMBOL declaration exists for the imported symbol in
140
+ * another file, the behavior will be most likely be identical to that
141
+ * for an unavailable symbol.
142
+ */
143
+#ifdef ASSEMBLY
144
+#define IMPORT_SYMBOL( _sym )				\
145
+	REQUEST_SYMBOL ( __export_ ## _sym ) ;		\
146
+	.weak	_sym
147
+#else /* ASSEMBLY */
148
+#define IMPORT_SYMBOL( _sym, _decl )			\
149
+	REQUEST_SYMBOL ( __export_ ## _sym ) ;		\
150
+	extern _decl __attribute__ (( weak ))
151
+#endif
152
+
64 153
 /** @} */
65 154
 
66 155
 /**
@@ -68,14 +157,7 @@
68 157
  * @{
69 158
  */
70 159
 
71
-/* Not quite sure why cpp requires two levels of macro call in order
72
- * to actually expand OBJECT...
73
- */
74
-#undef _H1
75
-#define _H1( x, y ) x ## y
76
-#undef _H2
77
-#define _H2( x, y ) _H1 ( x, y )
78
-#define PREFIX_OBJECT( _prefix ) _H2 ( _prefix, OBJECT )
160
+#define PREFIX_OBJECT( _prefix ) _C2 ( _prefix, OBJECT )
79 161
 #define OBJECT_SYMBOL PREFIX_OBJECT ( obj_ )
80 162
 
81 163
 /** Always provide the symbol for the current object (defined by -DOBJECT) */
@@ -84,6 +166,9 @@ PROVIDE_SYMBOL ( OBJECT_SYMBOL );
84 166
 /** Explicitly require another object */
85 167
 #define REQUIRE_OBJECT( _obj ) REQUIRE_SYMBOL ( obj_ ## _obj )
86 168
 
169
+/** Pull in another object if it exists */
170
+#define REQUEST_OBJECT( _obj ) REQUEST_SYMBOL ( obj_ ## _obj )
171
+
87 172
 /** @} */
88 173
 
89 174
 /** Select file identifier for errno.h (if used) */

Завантаження…
Відмінити
Зберегти