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 #ifdef SHARED
17 /* SHARED is defined if we are compiling with -fpic/-fPIC.  */
18 int overriddenvar = -1;
19 #else
20 /* Without -fpic, newer versions of gcc assume that we are not
21    compiling for a shared library, and thus that overriddenvar is
22    local.  */
23 extern int overriddenvar;
24 #endif
25 #endif
26 
27 /* This variable is defined in the shared library.  */
28 int shlibvar1 = 3;
29 
30 /* This variable is defined by another object in the shared library.  */
31 extern int shlibvar2;
32 
33 /* These functions return the values of the above variables as seen in
34    the shared library.  */
35 
36 #ifndef XCOFF_TEST
37 int
shlib_mainvar()38 shlib_mainvar ()
39 {
40   return mainvar;
41 }
42 #endif
43 
44 #ifndef XCOFF_TEST
45 int
shlib_overriddenvar()46 shlib_overriddenvar ()
47 {
48   return overriddenvar;
49 }
50 #endif
51 
52 int
shlib_shlibvar1()53 shlib_shlibvar1 ()
54 {
55   return shlibvar1;
56 }
57 
58 int
shlib_shlibvar2()59 shlib_shlibvar2 ()
60 {
61   return shlibvar2;
62 }
63 
64 /* This function calls a function defined by another object in the
65    shared library.  */
66 
67 extern int shlib_shlibcalled ();
68 
69 int
shlib_shlibcall()70 shlib_shlibcall ()
71 {
72   return shlib_shlibcalled ();
73 }
74 
75 #ifndef XCOFF_TEST
76 /* This function calls a function defined in this object in the shared
77    library.  The main program will override the called function.  */
78 
79 extern int shlib_overriddencall2 ();
80 
81 int
shlib_shlibcall2()82 shlib_shlibcall2 ()
83 {
84   return shlib_overriddencall2 ();
85 }
86 
87 #ifdef SHARED
88 int
shlib_overriddencall2()89 shlib_overriddencall2 ()
90 {
91   return 7;
92 }
93 #endif
94 #endif
95 
96 /* This function calls a function defined by the main program.  */
97 
98 #ifndef XCOFF_TEST
99 extern int main_called ();
100 
101 int
shlib_maincall()102 shlib_maincall ()
103 {
104   return main_called ();
105 }
106 #endif
107 
108 /* This function is passed a function pointer to shlib_mainvar.  It
109    confirms that the pointer compares equally.  */
110 
111 int
112 shlib_checkfunptr1 (p)
113      int (*p) ();
114 {
115   return p == shlib_shlibvar1;
116 }
117 
118 /* This function is passed a function pointer to main_called.  It
119    confirms that the pointer compares equally.  */
120 
121 #ifndef XCOFF_TEST
122 int
123 shlib_checkfunptr2 (p)
124      int (*p) ();
125 {
126   return p == main_called;
127 }
128 #endif
129 
130 /* This function returns a pointer to shlib_mainvar.  */
131 
132 int
shlib_getfunptr1()133 (*shlib_getfunptr1 ()) ()
134 {
135   return shlib_shlibvar1;
136 }
137 
138 /* This function returns a pointer to main_called.  */
139 
140 #ifndef XCOFF_TEST
141 int
shlib_getfunptr2()142 (*shlib_getfunptr2 ()) ()
143 {
144   return main_called;
145 }
146 #endif
147 
148 /* This function makes sure that constant data and local functions
149    work.  */
150 
151 #ifndef __STDC__
152 #define const
153 #endif
154 
155 static int i = 6;
156 static const char *str = "Hello, world\n";
157 
158 int
shlib_check()159 shlib_check ()
160 {
161   const char *s1, *s2;
162 
163   if (i != 6)
164     return 0;
165 
166   /* To isolate the test, don't rely on any external functions, such
167      as strcmp.  */
168   s1 = "Hello, world\n";
169   s2 = str;
170   while (*s1 != '\0')
171     if (*s1++ != *s2++)
172       return 0;
173   if (*s2 != '\0')
174     return 0;
175 
176   if (shlib_shlibvar1 () != 3)
177     return 0;
178 
179   return 1;
180 }
181 
182 #ifdef HIDDEN_WEAK_TEST
183 #define HIDDEN_UNDEF_TEST
184 #define WEAK_TEST
185 #endif
186 
187 #ifdef PROTECTED_WEAK_TEST
188 #define PROTECTED_UNDEF_TEST
189 #define WEAK_TEST
190 #endif
191 
192 #if defined (HIDDEN_UNDEF_TEST) || defined (PROTECTED_UNDEF_TEST)
193 #ifdef WEAK_TEST
194 #pragma weak visibility
195 #endif
196 extern int visibility ();
197 #else
198 int
visibility()199 visibility ()
200 {
201   return 2;
202 }
203 #endif
204 
205 #ifdef HIDDEN_NORMAL_TEST
206 asm (".hidden visibility_normal");
207 
208 int
visibility_normal()209 visibility_normal ()
210 {
211   return 2;
212 }
213 #endif
214 
215 int
visibility_checkfunptr()216 visibility_checkfunptr ()
217 {
218 #ifdef WEAK_TEST
219   return 1;
220 #else
221 #ifdef HIDDEN_NORMAL_TEST
222   int (*v) () = visibility_normal;
223 #else
224   int (*v) () = visibility;
225 #endif
226   return (*v) () == 2;
227 #endif
228 }
229 
230 int
visibility_check()231 visibility_check ()
232 {
233 #ifdef WEAK_TEST
234   if (&visibility)
235     return visibility () == 1;
236   else
237     return 1;
238 #else
239 #ifdef HIDDEN_NORMAL_TEST
240   return visibility_normal () == 2;
241 #else
242   return visibility () == 2;
243 #endif
244 #endif
245 }
246 
247 void *
visibility_funptr()248 visibility_funptr ()
249 {
250 #ifdef WEAK_TEST
251   if (&visibility == NULL)
252     return NULL;
253   else
254 #endif
255     return visibility;
256 }
257 
258 #if defined (HIDDEN_UNDEF_TEST) || defined (PROTECTED_UNDEF_TEST)
259 #ifdef WEAK_TEST
260 #pragma weak visibility_var
261 #endif
262 extern int visibility_var;
263 #else
264 int visibility_var = 2;
265 #endif
266 
267 #ifdef HIDDEN_NORMAL_TEST
268 asm (".hidden visibility_var_normal");
269 
270 int visibility_var_normal = 2;
271 #endif
272 
273 int
visibility_checkvarptr()274 visibility_checkvarptr ()
275 {
276 #ifdef WEAK_TEST
277   if (&visibility_var)
278     return visibility_var == 1;
279   else
280     return 1;
281 #else
282 #ifdef HIDDEN_NORMAL_TEST
283   int *v = &visibility_var_normal;
284 #else
285   int *v = &visibility_var;
286 #endif
287   return *v == 2;
288 #endif
289 }
290 
291 int
visibility_checkvar()292 visibility_checkvar ()
293 {
294 #ifdef WEAK_TEST
295   return 1;
296 #else
297 #ifdef HIDDEN_NORMAL_TEST
298   return visibility_var_normal == 2;
299 #else
300   return visibility_var == 2;
301 #endif
302 #endif
303 }
304 
305 void *
visibility_varptr()306 visibility_varptr ()
307 {
308 #ifdef WEAK_TEST
309   if (&visibility_var == NULL)
310     return NULL;
311   else
312 #endif
313     return &visibility_var;
314 }
315 
316 int
visibility_varval()317 visibility_varval ()
318 {
319 #ifdef WEAK_TEST
320   if (&visibility_var == NULL)
321     return 0;
322   else
323 #endif
324     return visibility_var;
325 }
326 
327 #if defined (HIDDEN_TEST) || defined (HIDDEN_UNDEF_TEST)
328 asm (".hidden visibility");
329 asm (".hidden visibility_var");
330 #else
331 #if defined (PROTECTED_TEST) || defined (PROTECTED_UNDEF_TEST) || defined (PROTECTED_WEAK_TEST)
332 asm (".protected visibility");
333 asm (".protected visibility_var");
334 #endif
335 #endif
336 
337 #ifdef HIDDEN_NORMAL_TEST
338 int shlib_visibility_com;
339 asm (".hidden shlib_visibility_com");
340 
341 int
shlib_visibility_checkcom()342 shlib_visibility_checkcom ()
343 {
344   return shlib_visibility_com == 0;
345 }
346 
347 int
shlib_visibility_checkweak()348 shlib_visibility_checkweak ()
349 {
350   return 1;
351 }
352 #elif defined (HIDDEN_WEAK_TEST)
353 #pragma weak shlib_visibility_undef_var_weak
354 extern int shlib_visibility_undef_var_weak;
355 asm (".hidden shlib_visibility_undef_var_weak");
356 
357 #pragma weak shlib_visibility_undef_func_weak
358 extern int shlib_visibility_undef_func_weak ();
359 asm (".hidden shlib_visibility_undef_func_weak");
360 
361 #pragma weak shlib_visibility_var_weak
362 extern int shlib_visibility_var_weak;
363 asm (".hidden shlib_visibility_var_weak");
364 
365 #pragma weak shlib_visibility_func_weak
366 extern int shlib_visibility_func_weak ();
367 asm (".hidden shlib_visibility_func_weak");
368 
369 int
shlib_visibility_checkcom()370 shlib_visibility_checkcom ()
371 {
372   return 1;
373 }
374 
375 int
shlib_visibility_checkweak()376 shlib_visibility_checkweak ()
377 {
378   return &shlib_visibility_undef_var_weak == NULL
379 	 && &shlib_visibility_undef_func_weak == NULL
380 	 && &shlib_visibility_func_weak == NULL
381 	 && &shlib_visibility_var_weak == NULL;
382 }
383 #else
384 int
shlib_visibility_checkcom()385 shlib_visibility_checkcom ()
386 {
387   return 1;
388 }
389 
390 int
shlib_visibility_checkweak()391 shlib_visibility_checkweak ()
392 {
393   return 1;
394 }
395 #endif
396 
397 #ifdef PROTECTED_TEST
398 #ifdef SHARED
399 int shared_data = 100;
400 #else
401 extern int shared_data;
402 #endif
403 
404 int *
shared_data_p()405 shared_data_p ()
406 {
407   return &shared_data;
408 }
409 
410 int
shared_func()411 shared_func ()
412 {
413   return 100;
414 }
415 
416 void *
shared_func_p()417 shared_func_p ()
418 {
419   return shared_func;
420 }
421 #endif
422