* elf/Makefile (tests): Substitute tests-vis-yes here.
[platform/upstream/glibc.git] / elf / vismain.c
1 /* Copyright (C) 2000, 2003 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "vismod.h"
24
25 /* Prototype for our test function.  */
26 extern int do_test (void);
27
28 #define TEST_FUNCTION do_test ()
29
30 /* This defines the `main' function and some more.  */
31 #include <test-skeleton.c>
32
33
34 /* Prototypes for local functions.  */
35 extern int protlocal (void);
36
37 const char *protvarlocal = __FILE__;
38 extern const char *protvarinmod;
39 extern const char *protvaritcpt;
40
41 int
42 do_test (void)
43 {
44   int res = 0;
45   int val;
46
47   /* First test: check whether .protected is handled correctly by the
48      assembler/linker.  The uses of `protlocal' in the DSOs and in the
49      main program should all be resolved with the local definitions.  */
50   val = protlocal () + calllocal1 () + calllocal2 ();
51   if (val != 0x155)
52     {
53       puts ("\
54 The handling of `.protected' seems to be implemented incorrectly: giving up");
55       abort ();
56     }
57   puts ("`.protected' seems to be handled correctly, good!");
58
59   /* Function pointers: for functions which are marked local and for
60      which definitions are available all function pointers must be
61      distinct.  */
62   if (protlocal == getlocal1 ())
63     {
64       puts ("`protlocal' in main and mod1 have same address");
65       res = 1;
66     }
67   if (protlocal == getlocal2 ())
68     {
69       puts ("`protlocal' in main and mod2 have same address");
70       res = 1;
71     }
72   if (getlocal1 () == getlocal2 ())
73     {
74       puts ("`protlocal' in mod1 and mod2 have same address");
75       res = 1;
76     }
77   if (getlocal1 () () + getlocal2 () () != 0x44)
78     {
79       puts ("pointers to `protlocal' in mod1 or mod2 incorrect");
80       res = 1;
81     }
82
83   /* Next test.  This is similar to the last one but the function we
84      are calling is not defined in the main object.  This means that
85      the invocation in the main object uses the definition in the
86      first DSO.  */
87   if (protinmod != getinmod1 ())
88     {
89       printf ("&protinmod in main (%p) != &protinmod in mod1 (%p)\n",
90               protinmod, getinmod1 ());
91       res = 1;
92     }
93   if (protinmod == getinmod2 ())
94     {
95       puts ("`protinmod' in main and mod2 have same address");
96       res = 1;
97     }
98   if (getinmod1 () == getinmod2 ())
99     {
100       puts ("`protinmod' in mod1 and mod2 have same address");
101       res = 1;
102     }
103   if (protinmod () + getinmod1 () () + getinmod2 () () != 0x4800)
104     {
105       puts ("pointers to `protinmod' in mod1 or mod2 incorrect");
106       res = 1;
107     }
108   val = protinmod () + callinmod1 () + callinmod2 ();
109   if (val != 0x15800)
110     {
111       printf ("calling of `protinmod' leads to wrong result (%#x)\n", val);
112       res = 1;
113     }
114
115   /* A very similar text.  Same setup for the main object and the modules
116      but this time we have another definition in a preloaded module. This
117      one intercepts the references from the main object.  */
118   if (protitcpt != getitcpt3 ())
119     {
120       printf ("&protitcpt in main (%p) != &protitcpt in mod3 (%p)\n",
121               &protitcpt, getitcpt3 ());
122       res = 1;
123     }
124   if (protitcpt == getitcpt1 ())
125     {
126       puts ("`protitcpt' in main and mod1 have same address");
127       res = 1;
128     }
129   if (protitcpt == getitcpt2 ())
130     {
131       puts ("`protitcpt' in main and mod2 have same address");
132       res = 1;
133     }
134   if (getitcpt1 () == getitcpt2 ())
135     {
136       puts ("`protitcpt' in mod1 and mod2 have same address");
137       res = 1;
138     }
139   val = protitcpt () + getitcpt1 () () + getitcpt2 () () + getitcpt3 () ();
140   if (val != 0x8440000)
141     {
142       printf ("\
143 pointers to `protitcpt' in mod1 or mod2 or mod3 incorrect (%#x)\n", val);
144       res = 1;
145     }
146   val = protitcpt () + callitcpt1 () + callitcpt2 () + callitcpt3 ();
147   if (val != 0x19540000)
148     {
149       printf ("calling of `protitcpt' leads to wrong result (%#x)\n", val);
150       res = 1;
151     }
152
153   /* Now look at variables.  First a variable which is available
154      everywhere.  We must have three different addresses.  */
155   if (&protvarlocal == getvarlocal1 ())
156     {
157       puts ("`protvarlocal' in main and mod1 have same address");
158       res = 1;
159     }
160   if (&protvarlocal == getvarlocal2 ())
161     {
162       puts ("`protvarlocal' in main and mod2 have same address");
163       res = 1;
164     }
165   if (getvarlocal1 () == getvarlocal2 ())
166     {
167       puts ("`protvarlocal' in mod1 and mod2 have same address");
168       res = 1;
169     }
170   if (strcmp (protvarlocal, __FILE__) != 0)
171     {
172       puts ("`protvarlocal in main has wrong value");
173       res = 1;
174     }
175   if (strcmp (*getvarlocal1 (), "vismod1.c") != 0)
176     {
177       puts ("`getvarlocal1' returns wrong value");
178       res = 1;
179     }
180   if (strcmp (*getvarlocal2 (), "vismod2.c") != 0)
181     {
182       puts ("`getvarlocal2' returns wrong value");
183       res = 1;
184     }
185
186   /* Now the case where there is no local definition.  */
187   if (&protvarinmod != getvarinmod1 ())
188     {
189       printf ("&protvarinmod in main (%p) != &protitcpt in mod1 (%p)\n",
190               &protvarinmod, getvarinmod1 ());
191       // XXX Possibly enable once fixed.
192       // res = 1;
193     }
194   if (&protvarinmod == getvarinmod2 ())
195     {
196       puts ("`protvarinmod' in main and mod2 have same address");
197       res = 1;
198     }
199   if (strcmp (*getvarinmod1 (), "vismod1.c") != 0)
200     {
201       puts ("`getvarinmod1' returns wrong value");
202       res = 1;
203     }
204   if (strcmp (*getvarinmod2 (), "vismod2.c") != 0)
205     {
206       puts ("`getvarinmod2' returns wrong value");
207       res = 1;
208     }
209
210   /* And a test where a variable definition is intercepted.  */
211   if (&protvaritcpt == getvaritcpt1 ())
212     {
213       puts ("`protvaritcpt' in main and mod1 have same address");
214       res = 1;
215     }
216   if (&protvaritcpt == getvaritcpt2 ())
217     {
218       puts ("`protvaritcpt' in main and mod2 have same address");
219       res = 1;
220     }
221   if (&protvaritcpt != getvaritcpt3 ())
222     {
223       printf ("&protvaritcpt in main (%p) != &protvaritcpt in mod3 (%p)\n",
224               &protvaritcpt, getvaritcpt3 ());
225       // XXX Possibly enable once fixed.
226       // res = 1;
227     }
228   if (getvaritcpt1 () == getvaritcpt2 ())
229     {
230       puts ("`protvaritcpt' in mod1 and mod2 have same address");
231       res = 1;
232     }
233   if (strcmp (protvaritcpt, "vismod3.c") != 0)
234     {
235       puts ("`protvaritcpt in main has wrong value");
236       res = 1;
237     }
238   if (strcmp (*getvaritcpt1 (), "vismod1.c") != 0)
239     {
240       puts ("`getvaritcpt1' returns wrong value");
241       res = 1;
242     }
243   if (strcmp (*getvaritcpt2 (), "vismod2.c") != 0)
244     {
245       puts ("`getvaritcpt2' returns wrong value");
246       res = 1;
247     }
248
249   return res;
250 }
251
252
253 int
254 protlocal (void)
255 {
256   return 0x1;
257 }