* ld-shared: New directory, with new files to test generating ELF
[external/binutils.git] / ld / testsuite / ld-shared / sh1.c
1 /* This is part of the shared library ld test.  This file becomes part
2    of a shared library.  */
3
4 /* This variable is supplied by the main program.  */
5 extern int mainvar;
6
7 /* This variable is defined in the shared library, and overridden by
8    the main program.  */
9 int overriddenvar = -1;
10
11 /* This variable is defined in the shared library.  */
12 int shlibvar1 = 3;
13
14 /* This variable is defined by another object in the shared library.  */
15 extern int shlibvar2;
16
17 /* These functions return the values of the above variables as seen in
18    the shared library.  */
19
20 int
21 shlib_mainvar ()
22 {
23   return mainvar;
24 }
25
26 int
27 shlib_overriddenvar ()
28 {
29   return overriddenvar;
30 }
31
32 int
33 shlib_shlibvar1 ()
34 {
35   return shlibvar1;
36 }
37
38 int
39 shlib_shlibvar2 ()
40 {
41   return shlibvar2;
42 }
43
44 /* This function calls a function defined by another object in the
45    shared library.  */
46
47 extern int shlib_shlibcalled ();
48
49 int
50 shlib_shlibcall ()
51 {
52   return shlib_shlibcalled ();
53 }
54
55 /* This function calls a function defined by the main program.  */
56
57 extern int main_called ();
58
59 int
60 shlib_maincall ()
61 {
62   return main_called ();
63 }
64
65 /* This function is passed a function pointer to shlib_mainvar.  It
66    confirms that the pointer compares equally.  */
67
68 int 
69 shlib_checkfunptr1 (p)
70      int (*p) ();
71 {
72   return p == shlib_mainvar;
73 }
74
75 /* This function is passed a function pointer to main_called.  It
76    confirms that the pointer compares equally.  */
77
78 int
79 shlib_checkfunptr2 (p)
80      int (*p) ();
81 {
82   return p == main_called;
83 }
84
85 /* This function returns a pointer to shlib_mainvar.  */
86
87 int
88 (*shlib_getfunptr1 ()) ()
89 {
90   return shlib_mainvar;
91 }
92
93 /* This function returns a pointer to main_called.  */
94
95 int
96 (*shlib_getfunptr2 ()) ()
97 {
98   return main_called;
99 }
100
101 /* This function makes sure that constant data and local functions
102    work.  */
103
104 #ifndef __STDC__
105 #define const
106 #endif
107
108 static int i = 6;
109 static const char *str = "Hello, world\n";
110
111 int
112 shlib_check ()
113 {
114   const char *s1, *s2;
115
116   if (i != 6)
117     return 0;
118
119   /* To isolate the test, don't rely on any external functions, such
120      as strcmp.  */
121   s1 = "Hello, world\n";
122   s2 = str;
123   while (*s1 != '\0')
124     if (*s1++ != *s2++)
125       return 0;
126   if (*s2 != '\0')
127     return 0;
128
129   if (shlib_shlibvar1 () != 3)
130     return 0;
131
132   return 1;
133 }