1 From 6ee65ed6ddbf04402fad0bec6aa9c73b9d982ae4 Mon Sep 17 00:00:00 2001
2 From: Ulrich Drepper <drepper@gmail.com>
3 Date: Fri, 27 Jan 2012 15:05:19 -0500
4 Subject: [PATCH] Sort objects before relocations
10 elf/Makefile | 15 +++++-
11 elf/dl-open.c | 128 ++++++++++++++++++++++++++++++++++++------------
12 elf/tst-relsort1.c | 19 +++++++
13 elf/tst-relsort1mod1.c | 7 +++
14 elf/tst-relsort1mod2.c | 7 +++
15 8 files changed, 160 insertions(+), 37 deletions(-)
16 create mode 100644 elf/tst-relsort1.c
17 create mode 100644 elf/tst-relsort1mod1.c
18 create mode 100644 elf/tst-relsort1mod2.c
20 diff --git a/Makeconfig b/Makeconfig
21 index 2db2821..68547b2 100644
24 @@ -900,6 +900,12 @@ else
25 libdl = $(common-objpfx)dlfcn/libdl.a
28 +ifeq ($(build-shared),yes)
29 +libm = $(common-objpfx)math/libm.so$(libm.so-version)
31 +libm = $(common-objpfx)math/libm.a
34 # These are the subdirectories containing the library source. The order
35 # is more or less arbitrary. The sorting step will take care of the
37 diff --git a/elf/Makefile b/elf/Makefile
38 index 052e763..3f1772a 100644
41 @@ -124,7 +124,8 @@ distribute := rtld-Rules \
42 tst-initordera1.c tst-initordera2.c tst-initorderb1.c \
43 tst-initorderb2.c tst-initordera3.c tst-initordera4.c \
47 + tst-relsort1.c tst-relsort1mod1.c tst-relsort1mod2.c
49 CFLAGS-dl-runtime.c = -fexceptions -fasynchronous-unwind-tables
50 CFLAGS-dl-lookup.c = -fexceptions -fasynchronous-unwind-tables
51 @@ -227,7 +228,7 @@ tests += loadtest restest1 preloadtest loadfail multiload origtest resolvfail \
52 tst-audit1 tst-audit2 \
53 tst-stackguard1 tst-addr1 tst-thrlock \
54 tst-unique1 tst-unique2 tst-unique3 tst-unique4 \
55 - tst-initorder tst-initorder2
56 + tst-initorder tst-initorder2 tst-relsort1
58 test-srcs = tst-pathopt
59 selinux-enabled := $(shell cat /selinux/enforce 2> /dev/null)
60 @@ -290,7 +291,9 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
61 tst-initordera1 tst-initorderb1 \
62 tst-initordera2 tst-initorderb2 \
63 tst-initordera3 tst-initordera4 \
64 - tst-initorder2a tst-initorder2b tst-initorder2c tst-initorder2d
65 + tst-initorder2a tst-initorder2b tst-initorder2c \
67 + tst-relsort1mod1 tst-relsort1mod2
68 ifeq (yes,$(have-initfini-array))
69 modules-names += tst-array2dep tst-array5dep
71 @@ -1195,3 +1198,9 @@ CFLAGS-tst-auditmod6b.c += $(AVX-CFLAGS)
72 CFLAGS-tst-auditmod6c.c += $(AVX-CFLAGS)
73 CFLAGS-tst-auditmod7b.c += $(AVX-CFLAGS)
76 +$(objpfx)tst-relsort1: $(libdl)
77 +$(objpfx)tst-relsort1mod1.so: $(libm) $(objpfx)tst-relsort1mod2.so
78 +$(objpfx)tst-relsort1mod2.so: $(libm)
79 +$(objpfx)tst-relsort1.out: $(objpfx)tst-relsort1mod1.so \
80 + $(objpfx)tst-relsort1mod2.so
81 diff --git a/elf/dl-open.c b/elf/dl-open.c
82 index a0b5c50..a56bdc1 100644
86 /* Load a shared object at runtime, relocate it, and run its initializer.
87 - Copyright (C) 1996-2007, 2009, 2010, 2011 Free Software Foundation, Inc.
88 + Copyright (C) 1996-2007, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
89 This file is part of the GNU C Library.
91 The GNU C Library is free software; you can redistribute it and/or
92 @@ -302,45 +302,109 @@ dl_open_worker (void *a)
94 reloc_mode |= mode & RTLD_LAZY;
96 - /* Relocate the objects loaded. We do this in reverse order so that copy
97 - relocs of earlier objects overwrite the data written by later objects. */
99 + /* Sort the objects by dependency for the relocation process. This
100 + allows IFUNC relocations to work and it also means copy
101 + relocation of dependencies are if necessary overwritten. */
103 struct link_map *l = new;
109 + if (! l->l_real->l_relocated)
114 + struct link_map *maps[nmaps];
119 if (! l->l_real->l_relocated)
127 + memset (seen, '\0', nmaps);
132 - if (__builtin_expect (GLRO(dl_profile) != NULL, 0))
134 + struct link_map *thisp = maps[i];
136 + /* Find the last object in the list for which the current one is
137 + a dependency and move the current object behind the object
138 + with the dependency. */
139 + size_t k = nmaps - 1;
142 - /* If this here is the shared object which we want to profile
143 - make sure the profile is started. We can find out whether
144 - this is necessary or not by observing the `_dl_profile_map'
145 - variable. If was NULL but is not NULL afterwars we must
146 - start the profiling. */
147 - struct link_map *old_profile_map = GL(dl_profile_map);
148 + struct link_map **runp = maps[k]->l_initfini;
150 + /* Look through the dependencies of the object. */
151 + while (*runp != NULL)
152 + if (__builtin_expect (*runp++ == thisp, 0))
154 + /* Move the current object to the back past the last
155 + object with it as the dependency. */
156 + memmove (&maps[i], &maps[i + 1],
157 + (k - i) * sizeof (maps[0]));
160 + if (seen[i + 1] > 1)
166 + char this_seen = seen[i];
167 + memmove (&seen[i], &seen[i + 1],
168 + (k - i) * sizeof (seen[0]));
169 + seen[k] = this_seen;
177 - _dl_relocate_object (l, l->l_scope, reloc_mode | RTLD_LAZY, 1);
181 + memset (&seen[i], 0, (nmaps - i) * sizeof (seen[0]));
186 - if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
188 - /* We must prepare the profiling. */
189 - _dl_start_profile ();
190 + for (size_t i = nmaps; i-- > 0; )
194 - /* Prevent unloading the object. */
195 - GL(dl_profile_map)->l_flags_1 |= DF_1_NODELETE;
198 + if (__builtin_expect (GLRO(dl_profile) != NULL, 0))
200 + /* If this here is the shared object which we want to profile
201 + make sure the profile is started. We can find out whether
202 + this is necessary or not by observing the `_dl_profile_map'
203 + variable. If it was NULL but is not NULL afterwars we must
204 + start the profiling. */
205 + struct link_map *old_profile_map = GL(dl_profile_map);
207 + _dl_relocate_object (l, l->l_scope, reloc_mode | RTLD_LAZY, 1);
209 + if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
211 + /* We must prepare the profiling. */
212 + _dl_start_profile ();
214 + /* Prevent unloading the object. */
215 + GL(dl_profile_map)->l_flags_1 |= DF_1_NODELETE;
219 - _dl_relocate_object (l, l->l_scope, reloc_mode, 0);
227 + _dl_relocate_object (l, l->l_scope, reloc_mode, 0);
230 /* If the file is not loaded now as a dependency, add the search
231 diff --git a/elf/tst-relsort1.c b/elf/tst-relsort1.c
233 index 0000000..972100c
235 +++ b/elf/tst-relsort1.c
244 + const char lib[] = "$ORIGIN/tst-relsort1mod1.so";
245 + void *h = dlopen (lib, RTLD_NOW);
254 +#define TEST_FUNCTION do_test ()
255 +#include "../test-skeleton.c"
256 diff --git a/elf/tst-relsort1mod1.c b/elf/tst-relsort1mod1.c
258 index 0000000..9e4a943
260 +++ b/elf/tst-relsort1mod1.c
262 +extern int foo (double);
269 diff --git a/elf/tst-relsort1mod2.c b/elf/tst-relsort1mod2.c
271 index 0000000..a2c3e55
273 +++ b/elf/tst-relsort1mod2.c
280 + return floor (d) != 0.0;