1 /* Test the protected visibility when main is linked with modb and moda
2    in that order:
3    1. Protected symbols, protected1, protected2 and protected3, defined
4       in moda, are used in moda.
5    2.  Protected symbol, protected3, defined in modb, are used in modb
6    3. Symbol, protected1, defined in modb, is used in main and modb.
7    4. Symbol, protected2, defined in main, is used in main.
8    5. Symbol, protected3, defined in modb, is also used in main.
9 
10    Copyright (C) 2015-2022 Free Software Foundation, Inc.
11    This file is part of the GNU C Library.
12 
13    The GNU C Library is free software; you can redistribute it and/or
14    modify it under the terms of the GNU Lesser General Public
15    License as published by the Free Software Foundation; either
16    version 2.1 of the License, or (at your option) any later version.
17 
18    The GNU C Library is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21    Lesser General Public License for more details.
22 
23    You should have received a copy of the GNU Lesser General Public
24    License along with the GNU C Library; if not, see
25    <https://www.gnu.org/licenses/>.  */
26 
27 /* This file must be compiled as PIE to avoid copy relocation when
28    accessing protected symbols defined in shared libaries since copy
29    relocation doesn't work with protected symbols and linker in
30    binutils 2.26 enforces this rule.  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "tst-protected1mod.h"
37 
38 /* Prototype for our test function.  */
39 extern int do_test (void);
40 
41 int protected2 = -1;
42 
43 /* This defines the `main' function and some more.  */
44 #include <support/test-driver.c>
45 
46 int
do_test(void)47 do_test (void)
48 {
49   int res = 0;
50 
51   /* Check if we get the same address for the protected data symbol.  */
52   if (&protected1 == protected1a_p ())
53     {
54       puts ("`protected1' in main and moda has  same address");
55       res = 1;
56     }
57   if (&protected1 != protected1b_p ())
58     {
59       puts ("`protected1' in main and modb doesn't have same address");
60       res = 1;
61     }
62 
63   /* Check if we get the right value for the protected data symbol.  */
64   if (protected1 != -3)
65     {
66       puts ("`protected1' in main and modb doesn't have same value");
67       res = 1;
68     }
69 
70   /* Check if we get the right value for data defined in executable.  */
71   if (protected2 != -1)
72     {
73       puts ("`protected2' in main has the wrong value");
74       res = 1;
75     }
76 
77   /* Check `protected1' in moda.  */
78   if (!check_protected1 ())
79     {
80       puts ("`protected1' in moda has the wrong value");
81       res = 1;
82     }
83 
84   /* Check `protected2' in moda.  */
85   if (!check_protected2 ())
86     {
87       puts ("`protected2' in moda has the wrong value");
88       res = 1;
89     }
90 
91   /* Check if we get the same address for the protected data symbol.  */
92   if (&protected3 == protected3a_p ())
93     {
94       puts ("`protected3' in main and moda has same address");
95       res = 1;
96     }
97   if (&protected3 != protected3b_p ())
98     {
99       puts ("`protected3' in main and modb doesn't have same address");
100       res = 1;
101     }
102 
103   /* Check if we get the right value for the protected data symbol.  */
104   if (protected3 != -5)
105     {
106       puts ("`protected3' in main and modb doesn't have same value");
107       res = 1;
108     }
109 
110   /* Check `protected3' in moda.  */
111   if (!check_protected3a ())
112     {
113       puts ("`protected3' in moda has the wrong value");
114       res = 1;
115     }
116 
117   /* Check `protected3' in modb.  */
118   if (!check_protected3b ())
119     {
120       puts ("`protected3' in modb has the wrong value");
121       res = 1;
122     }
123 
124   /* Set `protected2' in moda to 30.  */
125   set_protected2 (300);
126 
127   /* Check `protected2' in moda.  */
128   if (!check_protected2 ())
129     {
130       puts ("`protected2' in moda has the wrong value");
131       res = 1;
132     }
133 
134   /* Check if we get the right value for data defined in executable.  */
135   if (protected2 != -1)
136     {
137       puts ("`protected2' in main has the wrong value");
138       res = 1;
139     }
140 
141   /* Set `protected1' in moda to 30.  */
142   set_protected1a (30);
143 
144   /* Check `protected1' in moda.  */
145   if (!check_protected1 ())
146     {
147       puts ("`protected1' in moda has the wrong value");
148       res = 1;
149     }
150 
151   /* Check if we get the same value for the protected data symbol.  */
152   if (protected1 != -3)
153     {
154       puts ("`protected1' in main has the wrong value");
155       res = 1;
156     }
157 
158   protected2 = -300;
159 
160   /* Check `protected2' in moda.  */
161   if (!check_protected2 ())
162     {
163       puts ("`protected2' in moda has the wrong value");
164       res = 1;
165     }
166 
167   /* Check if data defined in executable is changed.  */
168   if (protected2 != -300)
169     {
170       puts ("`protected2' in main is changed");
171       res = 1;
172     }
173 
174   /* Set `protected1' in modb to 40.  */
175   set_protected1b (40);
176 
177   /* Check `protected1' in moda.  */
178   if (!check_protected1 ())
179     {
180       puts ("`protected1' in moda has the wrong value");
181       res = 1;
182     }
183 
184   /* Check if we get the updated value for the protected data symbol.  */
185   if (protected1 != 40)
186     {
187       puts ("`protected1' in main doesn't have the updated value");
188       res = 1;
189     }
190 
191   /* Set `protected3' in moda to 80.  */
192   set_protected3a (80);
193 
194   /* Check `protected3' in moda.  */
195   if (!check_protected3a ())
196     {
197       puts ("`protected3' in moda has the wrong value");
198       res = 1;
199     }
200 
201   /* Check if we get the updated value for the protected data symbol.  */
202   if (protected3 != -5)
203     {
204       puts ("`protected3' in main doesn't have the updated value");
205       res = 1;
206     }
207 
208   /* Check `protected3' in modb.  */
209   if (!check_protected3b ())
210     {
211       puts ("`protected3' in modb has the wrong value");
212       res = 1;
213     }
214 
215   /* Set `protected3' in modb to 100.  */
216   set_protected3b (100);
217 
218   /* Check `protected3' in moda.  */
219   if (!check_protected3a ())
220     {
221       puts ("`protected3' in moda has the wrong value");
222       res = 1;
223     }
224 
225   /* Check if we get the updated value for the protected data symbol.  */
226   if (protected3 != 100)
227     {
228       puts ("`protected3' in main doesn't have the updated value");
229       res = 1;
230     }
231 
232   /* Check `protected3' in modb.  */
233   if (!check_protected3b ())
234     {
235       puts ("`protected3' in modb has the wrong value");
236       res = 1;
237     }
238 
239   return res;
240 }
241