2000-06-05 H.J. Lu (hjl@gnu.org)
[external/binutils.git] / ld / testsuite / ld-elfvsb / sh1.c
1 #ifndef NULL
2 #define NULL ((void *) 0)
3 #endif
4
5 /* This is part of the shared library ld test.  This file becomes part
6    of a shared library.  */
7
8 /* This variable is supplied by the main program.  */
9 #ifndef XCOFF_TEST
10 extern int mainvar;
11 #endif
12
13 /* This variable is defined in the shared library, and overridden by
14    the main program.  */
15 #ifndef XCOFF_TEST
16 int overriddenvar = -1;
17 #endif
18
19 /* This variable is defined in the shared library.  */
20 int shlibvar1 = 3;
21
22 /* This variable is defined by another object in the shared library.  */
23 extern int shlibvar2;
24
25 /* These functions return the values of the above variables as seen in
26    the shared library.  */
27
28 #ifndef XCOFF_TEST
29 int
30 shlib_mainvar ()
31 {
32   return mainvar;
33 }
34 #endif
35
36 #ifndef XCOFF_TEST
37 int
38 shlib_overriddenvar ()
39 {
40   return overriddenvar;
41 }
42 #endif
43
44 int
45 shlib_shlibvar1 ()
46 {
47   return shlibvar1;
48 }
49
50 int
51 shlib_shlibvar2 ()
52 {
53   return shlibvar2;
54 }
55
56 /* This function calls a function defined by another object in the
57    shared library.  */
58
59 extern int shlib_shlibcalled ();
60
61 int
62 shlib_shlibcall ()
63 {
64   return shlib_shlibcalled ();
65 }
66
67 #ifndef XCOFF_TEST
68 /* This function calls a function defined in this object in the shared
69    library.  The main program will override the called function.  */
70
71 extern int shlib_overriddencall2 ();
72
73 int
74 shlib_shlibcall2 ()
75 {
76   return shlib_overriddencall2 ();
77 }
78
79 int
80 shlib_overriddencall2 ()
81 {
82   return 7;
83 }
84 #endif
85
86 /* This function calls a function defined by the main program.  */
87
88 #ifndef XCOFF_TEST
89 extern int main_called ();
90
91 int
92 shlib_maincall ()
93 {
94   return main_called ();
95 }
96 #endif
97
98 /* This function is passed a function pointer to shlib_mainvar.  It
99    confirms that the pointer compares equally.  */
100
101 int 
102 shlib_checkfunptr1 (p)
103      int (*p) ();
104 {
105   return p == shlib_shlibvar1;
106 }
107
108 /* This function is passed a function pointer to main_called.  It
109    confirms that the pointer compares equally.  */
110
111 #ifndef XCOFF_TEST
112 int
113 shlib_checkfunptr2 (p)
114      int (*p) ();
115 {
116   return p == main_called;
117 }
118 #endif
119
120 /* This function returns a pointer to shlib_mainvar.  */
121
122 int
123 (*shlib_getfunptr1 ()) ()
124 {
125   return shlib_shlibvar1;
126 }
127
128 /* This function returns a pointer to main_called.  */
129
130 #ifndef XCOFF_TEST
131 int
132 (*shlib_getfunptr2 ()) ()
133 {
134   return main_called;
135 }
136 #endif
137
138 /* This function makes sure that constant data and local functions
139    work.  */
140
141 #ifndef __STDC__
142 #define const
143 #endif
144
145 static int i = 6;
146 static const char *str = "Hello, world\n";
147
148 int
149 shlib_check ()
150 {
151   const char *s1, *s2;
152
153   if (i != 6)
154     return 0;
155
156   /* To isolate the test, don't rely on any external functions, such
157      as strcmp.  */
158   s1 = "Hello, world\n";
159   s2 = str;
160   while (*s1 != '\0')
161     if (*s1++ != *s2++)
162       return 0;
163   if (*s2 != '\0')
164     return 0;
165
166   if (shlib_shlibvar1 () != 3)
167     return 0;
168
169   return 1;
170 }
171
172 #ifdef HIDDEN_WEAK_TEST
173 #define HIDDEN_UNDEF_TEST
174 #define WEAK_TEST
175 #endif
176
177 #ifdef PROTECTED_WEAK_TEST
178 #define PROTECTED_UNDEF_TEST
179 #define WEAK_TEST
180 #endif
181
182 #if defined (HIDDEN_UNDEF_TEST) || defined (PROTECTED_UNDEF_TEST)
183 extern int visibility ();
184 #else
185 int
186 visibility ()
187 {
188   return 2;
189 }
190 #endif
191
192 #ifdef HIDDEN_NORMAL_TEST
193 asm (".hidden visibility_normal");
194
195 int
196 visibility_normal ()
197 {
198   return 2;
199 }
200 #endif
201
202 int
203 visibility_checkfunptr ()
204 {
205 #ifdef WEAK_TEST
206   return 1;
207 #else
208 #ifdef HIDDEN_NORMAL_TEST
209   int (*v) () = visibility_normal;
210 #else
211   int (*v) () = visibility;
212 #endif
213   return (*v) () == 2;
214 #endif
215 }
216
217 int
218 visibility_check ()
219 {
220 #ifdef WEAK_TEST
221   if (&visibility)
222     return visibility () == 1;
223   else
224     return 1;
225 #else
226 #ifdef HIDDEN_NORMAL_TEST
227   return visibility_normal () == 2;
228 #else
229   return visibility () == 2;
230 #endif
231 #endif
232 }
233
234 void *
235 visibility_funptr ()
236 {
237 #ifdef WEAK_TEST
238   if (&visibility == NULL)
239     return NULL;
240   else
241 #endif
242     return visibility;
243 }
244
245 #if defined (HIDDEN_UNDEF_TEST) || defined (PROTECTED_UNDEF_TEST)
246 extern int visibility_var;
247 #else
248 int visibility_var = 2;
249 #endif
250
251 #ifdef HIDDEN_NORMAL_TEST
252 asm (".hidden visibility_var_normal");
253
254 int visibility_var_normal = 2;
255 #endif
256
257 int
258 visibility_checkvarptr ()
259 {
260 #ifdef WEAK_TEST
261   if (&visibility_var)
262     return visibility_var == 1;
263   else
264     return 1;
265 #else
266 #ifdef HIDDEN_NORMAL_TEST
267   int *v = &visibility_var_normal;
268 #else
269   int *v = &visibility_var;
270 #endif
271   return *v == 2;
272 #endif
273 }
274
275 int
276 visibility_checkvar ()
277 {
278 #ifdef WEAK_TEST
279   return 1;
280 #else
281 #ifdef HIDDEN_NORMAL_TEST
282   return visibility_var_normal == 2;
283 #else
284   return visibility_var == 2;
285 #endif
286 #endif
287 }
288
289 void *
290 visibility_varptr ()
291 {
292 #ifdef WEAK_TEST
293   if (&visibility_var == NULL)
294     return NULL;
295   else
296 #endif
297     return &visibility_var;
298 }
299
300 int
301 visibility_varval ()
302 {
303 #ifdef WEAK_TEST
304   if (&visibility_var == NULL)
305     return 0;
306   else
307 #endif
308     return visibility_var;
309 }
310
311 #if defined (HIDDEN_TEST) || defined (HIDDEN_UNDEF_TEST)
312 asm (".hidden visibility");
313 asm (".hidden visibility_var");
314 #else
315 asm (".protected visibility");
316 asm (".protected visibility_var");
317 #endif
318
319 #ifdef WEAK_TEST
320 asm (".weak visibility");
321 asm (".weak visibility_var");
322 #endif