wok view glibc/stuff/glibc-2.14.1-sort-1.patch @ rev 23041
linphone: added build dependency cmake
author | Hans-G?nter Theisgen |
---|---|
date | Wed Mar 04 14:40:01 2020 +0100 (2020-03-04) |
parents | |
children |
line source
1 Submitted By: Bruce Dubbs <bdubbs_at_linuxfromscratch_dot_org>
2 Date: 2012-02-24
3 Initial Package Version: 2.14.1
4 Upstream Status: From upstream
5 Origin: Upstream
6 Description: Sort objects by dependency before relocation.
7 Fixes segfault in dlopen for several programs.
9 diff --git a/Makeconfig b/Makeconfig
10 index 2db2821..68547b2 100644
11 --- a/Makeconfig
12 +++ b/Makeconfig
13 @@ -938,6 +938,12 @@ libdl =
14 endif
15 endif
17 +ifeq ($(build-shared),yes)
18 +libm = $(common-objpfx)math/libm.so$(libm.so-version)
19 +else
20 +libm = $(common-objpfx)math/libm.a
21 +endif
22 +
23 # These are the subdirectories containing the library source. The order
24 # is more or less arbitrary. The sorting step will take care of the
25 # dependencies.
26 diff --git a/elf/Makefile b/elf/Makefile
27 index 052e763..3f1772a 100644
28 --- a/elf/Makefile
29 +++ b/elf/Makefile
30 @@ -124,7 +124,8 @@ distribute := rtld-Rules \
31 tst-initordera1.c tst-initordera2.c tst-initorderb1.c \
32 tst-initorderb2.c tst-initordera3.c tst-initordera4.c \
33 tst-initorder.c \
34 - tst-initorder2.c
35 + tst-initorder2.c \
36 + tst-relsort1.c tst-relsort1mod1.c tst-relsort1mod2.c
38 CFLAGS-dl-runtime.c = -fexceptions -fasynchronous-unwind-tables
39 CFLAGS-dl-lookup.c = -fexceptions -fasynchronous-unwind-tables
40 @@ -227,7 +228,7 @@ tests += loadtest restest1 preloadtest loadfail multiload origtest resolvfail \
41 tst-audit1 tst-audit2 \
42 tst-stackguard1 tst-addr1 tst-thrlock \
43 tst-unique1 tst-unique2 tst-unique3 tst-unique4 \
44 - tst-initorder tst-initorder2
45 + tst-initorder tst-initorder2 tst-relsort1
46 # reldep9
47 test-srcs = tst-pathopt
48 selinux-enabled := $(shell cat /selinux/enforce 2> /dev/null)
49 @@ -290,7 +291,9 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
50 tst-initordera1 tst-initorderb1 \
51 tst-initordera2 tst-initorderb2 \
52 tst-initordera3 tst-initordera4 \
53 - tst-initorder2a tst-initorder2b tst-initorder2c tst-initorder2d
54 + tst-initorder2a tst-initorder2b tst-initorder2c \
55 + tst-initorder2d \
56 + tst-relsort1mod1 tst-relsort1mod2
57 ifeq (yes,$(have-initfini-array))
58 modules-names += tst-array2dep tst-array5dep
59 endif
60 @@ -1195,3 +1198,9 @@ CFLAGS-tst-auditmod6b.c += $(AVX-CFLAGS)
61 CFLAGS-tst-auditmod6c.c += $(AVX-CFLAGS)
62 CFLAGS-tst-auditmod7b.c += $(AVX-CFLAGS)
63 endif
64 +
65 +$(objpfx)tst-relsort1: $(libdl)
66 +$(objpfx)tst-relsort1mod1.so: $(libm) $(objpfx)tst-relsort1mod2.so
67 +$(objpfx)tst-relsort1mod2.so: $(libm)
68 +$(objpfx)tst-relsort1.out: $(objpfx)tst-relsort1mod1.so \
69 + $(objpfx)tst-relsort1mod2.so
70 diff --git a/elf/dl-open.c b/elf/dl-open.c
71 index a0b5c50..a56bdc1 100644
72 --- a/elf/dl-open.c
73 +++ b/elf/dl-open.c
74 @@ -1,5 +1,5 @@
75 /* Load a shared object at runtime, relocate it, and run its initializer.
76 - Copyright (C) 1996-2007, 2009, 2010, 2011 Free Software Foundation, Inc.
77 + Copyright (C) 1996-2007, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
78 This file is part of the GNU C Library.
80 The GNU C Library is free software; you can redistribute it and/or
81 @@ -303,45 +303,109 @@ dl_open_worker (void *a)
82 if (GLRO(dl_lazy))
83 reloc_mode |= mode & RTLD_LAZY;
85 - /* Relocate the objects loaded. We do this in reverse order so that copy
86 - relocs of earlier objects overwrite the data written by later objects. */
87 -
88 + /* Sort the objects by dependency for the relocation process. This
89 + allows IFUNC relocations to work and it also means copy
90 + relocation of dependencies are if necessary overwritten. */
91 + size_t nmaps = 0;
92 struct link_map *l = new;
93 - while (l->l_next)
94 - l = l->l_next;
95 - while (1)
96 + do
97 + {
98 + if (! l->l_real->l_relocated)
99 + ++nmaps;
100 + l = l->l_next;
101 + }
102 + while (l != NULL);
103 + struct link_map *maps[nmaps];
104 + nmaps = 0;
105 + l = new;
106 + do
107 {
108 if (! l->l_real->l_relocated)
109 + maps[nmaps++] = l;
110 + l = l->l_next;
111 + }
112 + while (l != NULL);
113 + if (nmaps > 1)
114 + {
115 + char seen[nmaps];
116 + memset (seen, '\0', nmaps);
117 + size_t i = 0;
118 + while (1)
119 {
120 -#ifdef SHARED
121 - if (__builtin_expect (GLRO(dl_profile) != NULL, 0))
122 + ++seen[i];
123 + struct link_map *thisp = maps[i];
124 +
125 + /* Find the last object in the list for which the current one is
126 + a dependency and move the current object behind the object
127 + with the dependency. */
128 + size_t k = nmaps - 1;
129 + while (k > i)
130 {
131 - /* If this here is the shared object which we want to profile
132 - make sure the profile is started. We can find out whether
133 - this is necessary or not by observing the `_dl_profile_map'
134 - variable. If was NULL but is not NULL afterwars we must
135 - start the profiling. */
136 - struct link_map *old_profile_map = GL(dl_profile_map);
137 + struct link_map **runp = maps[k]->l_initfini;
138 + if (runp != NULL)
139 + /* Look through the dependencies of the object. */
140 + while (*runp != NULL)
141 + if (__builtin_expect (*runp++ == thisp, 0))
142 + {
143 + /* Move the current object to the back past the last
144 + object with it as the dependency. */
145 + memmove (&maps[i], &maps[i + 1],
146 + (k - i) * sizeof (maps[0]));
147 + maps[k] = thisp;
148 +
149 + if (seen[i + 1] > 1)
150 + {
151 + ++i;
152 + goto next_clear;
153 + }
154 +
155 + char this_seen = seen[i];
156 + memmove (&seen[i], &seen[i + 1],
157 + (k - i) * sizeof (seen[0]));
158 + seen[k] = this_seen;
159 +
160 + goto next;
161 + }
162 +
163 + --k;
164 + }
166 - _dl_relocate_object (l, l->l_scope, reloc_mode | RTLD_LAZY, 1);
167 + if (++i == nmaps)
168 + break;
169 + next_clear:
170 + memset (&seen[i], 0, (nmaps - i) * sizeof (seen[0]));
171 + next:;
172 + }
173 + }
175 - if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
176 - {
177 - /* We must prepare the profiling. */
178 - _dl_start_profile ();
179 + for (size_t i = nmaps; i-- > 0; )
180 + {
181 + l = maps[i];
183 - /* Prevent unloading the object. */
184 - GL(dl_profile_map)->l_flags_1 |= DF_1_NODELETE;
185 - }
186 +#ifdef SHARED
187 + if (__builtin_expect (GLRO(dl_profile) != NULL, 0))
188 + {
189 + /* If this here is the shared object which we want to profile
190 + make sure the profile is started. We can find out whether
191 + this is necessary or not by observing the `_dl_profile_map'
192 + variable. If it was NULL but is not NULL afterwars we must
193 + start the profiling. */
194 + struct link_map *old_profile_map = GL(dl_profile_map);
195 +
196 + _dl_relocate_object (l, l->l_scope, reloc_mode | RTLD_LAZY, 1);
197 +
198 + if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
199 + {
200 + /* We must prepare the profiling. */
201 + _dl_start_profile ();
202 +
203 + /* Prevent unloading the object. */
204 + GL(dl_profile_map)->l_flags_1 |= DF_1_NODELETE;
205 }
206 - else
207 -#endif
208 - _dl_relocate_object (l, l->l_scope, reloc_mode, 0);
209 }
210 -
211 - if (l == new)
212 - break;
213 - l = l->l_prev;
214 + else
215 +#endif
216 + _dl_relocate_object (l, l->l_scope, reloc_mode, 0);
217 }
219 /* If the file is not loaded now as a dependency, add the search
220 diff --git a/elf/tst-relsort1.c b/elf/tst-relsort1.c
221 new file mode 100644
222 index 0000000..972100c
223 --- /dev/null
224 +++ b/elf/tst-relsort1.c
225 @@ -0,0 +1,19 @@
226 +#include <dlfcn.h>
227 +#include <stdio.h>
228 +
229 +
230 +static int
231 +do_test ()
232 +{
233 + const char lib[] = "$ORIGIN/tst-relsort1mod1.so";
234 + void *h = dlopen (lib, RTLD_NOW);
235 + if (h == NULL)
236 + {
237 + puts (dlerror ());
238 + return 1;
239 + }
240 + return 0;
241 +}
242 +
243 +#define TEST_FUNCTION do_test ()
244 +#include "../test-skeleton.c"
245 diff --git a/elf/tst-relsort1mod1.c b/elf/tst-relsort1mod1.c
246 new file mode 100644
247 index 0000000..9e4a943
248 --- /dev/null
249 +++ b/elf/tst-relsort1mod1.c
250 @@ -0,0 +1,7 @@
251 +extern int foo (double);
252 +
253 +int
254 +bar (void)
255 +{
256 + return foo (1.2);
257 +}
258 diff --git a/elf/tst-relsort1mod2.c b/elf/tst-relsort1mod2.c
259 new file mode 100644
260 index 0000000..a2c3e55
261 --- /dev/null
262 +++ b/elf/tst-relsort1mod2.c
263 @@ -0,0 +1,7 @@
264 +#include <math.h>
265 +
266 +int
267 +foo (double d)
268 +{
269 + return floor (d) != 0.0;
270 +}
271 --
272 1.7.3.4