diff --git a/elf/Makefile b/elf/Makefile index f1a16fe..b840891 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -192,7 +192,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \ tst-latepthread tst-tls-manydynamic tst-nodelete-dlclose \ tst-debug1 tst-main1 tst-absolute-sym tst-absolute-zero tst-big-note \ tst-sonamemove-link tst-sonamemove-dlopen tst-initfinilazyfail \ - tst-dlopenfail tst-dlopenfail-2 + tst-dlopenfail tst-dlopenfail-2 \ + tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen # reldep9 tests-internal += loadtest unload unload2 circleload1 \ neededtest neededtest2 neededtest3 neededtest4 \ @@ -302,7 +303,8 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \ tst-sonamemove-runmod1 tst-sonamemove-runmod2 \ tst-initlazyfailmod tst-finilazyfailmod \ tst-dlopenfailmod1 tst-dlopenfaillinkmod tst-dlopenfailmod2 \ - tst-dlopenfailmod3 + tst-dlopenfailmod3 \ + tst-filterobj-flt tst-filterobj-aux tst-filterobj-filtee ifeq (yes,$(have-mtls-dialect-gnu2)) tests += tst-gnu2-tls1 @@ -1619,3 +1621,15 @@ $(objpfx)tst-dlopen-nodelete-reloc-mod17.so: \ $(objpfx)tst-dlopen-nodelete-reloc-mod15.so \ $(objpfx)tst-dlopen-nodelete-reloc-mod16.so LDFLAGS-tst-dlopen-nodelete-reloc-mod17.so = -Wl,--no-as-needed + +LDFLAGS-tst-filterobj-flt.so = -Wl,--filter=$(objpfx)tst-filterobj-filtee.so +$(objpfx)tst-filterobj: $(objpfx)tst-filterobj-flt.so +$(objpfx)tst-filterobj-dlopen: $(libdl) +$(objpfx)tst-filterobj.out: $(objpfx)tst-filterobj-filtee.so +$(objpfx)tst-filterobj-dlopen.out: $(objpfx)tst-filterobj-filtee.so + +LDFLAGS-tst-filterobj-aux.so = -Wl,--auxiliary=$(objpfx)tst-filterobj-filtee.so +$(objpfx)tst-auxobj: $(objpfx)tst-filterobj-aux.so +$(objpfx)tst-auxobj-dlopen: $(libdl) +$(objpfx)tst-auxobj.out: $(objpfx)tst-filterobj-filtee.so +$(objpfx)tst-auxobj-dlopen.out: $(objpfx)tst-filterobj-filtee.so diff --git a/elf/dl-deps.c b/elf/dl-deps.c index 9d9b1ba..50f053a 100644 --- a/elf/dl-deps.c +++ b/elf/dl-deps.c @@ -485,14 +485,18 @@ _dl_map_object_deps (struct link_map *map, map->l_searchlist.r_list = &l_initfini[nlist + 1]; map->l_searchlist.r_nlist = nlist; + unsigned int map_index = UINT_MAX; for (nlist = 0, runp = known; runp; runp = runp->next) { if (__builtin_expect (trace_mode, 0) && runp->map->l_faked) /* This can happen when we trace the loading. */ --map->l_searchlist.r_nlist; - else + else { + if (runp->map == map) + map_index = nlist; map->l_searchlist.r_list[nlist++] = runp->map; + } /* Now clear all the mark bits we set in the objects on the search list to avoid duplicates, so the next call starts fresh. */ @@ -550,13 +554,14 @@ Filters not supported with LD_TRACE_PRELINKING")); } /* Maybe we can remove some relocation dependencies now. */ - assert (map->l_searchlist.r_list[0] == map); struct link_map_reldeps *l_reldeps = NULL; if (map->l_reldeps != NULL) { - for (i = 1; i < nlist; ++i) + for (i = 0; i < nlist; ++i) map->l_searchlist.r_list[i]->l_reserved = 1; + /* Avoid removing relocation dependencies of the main binary. */ + map->l_reserved = 0; struct link_map **list = &map->l_reldeps->list[0]; for (i = 0; i < map->l_reldeps->act; ++i) if (list[i]->l_reserved) @@ -581,16 +586,30 @@ Filters not supported with LD_TRACE_PRELINKING")); } } - for (i = 1; i < nlist; ++i) + for (i = 0; i < nlist; ++i) map->l_searchlist.r_list[i]->l_reserved = 0; } - /* Sort the initializer list to take dependencies into account. The binary - itself will always be initialize last. */ - memcpy (l_initfini, map->l_searchlist.r_list, - nlist * sizeof (struct link_map *)); - /* We can skip looking for the binary itself which is at the front of - the search list. */ + /* Sort the initializer list to take dependencies into account. Always + initialize the binary itself last. */ + assert (map_index < nlist); + if (map_index > 0) + { + /* Copy the binary into position 0. */ + l_initfini[0] = map->l_searchlist.r_list[map_index]; + + /* Copy the filtees. */ + for (i = 0; i < map_index; ++i) + l_initfini[i+1] = map->l_searchlist.r_list[i]; + + /* Copy the remainder. */ + for (i = map_index + 1; i < nlist; ++i) + l_initfini[i] = map->l_searchlist.r_list[i]; + } + else + memcpy (l_initfini, map->l_searchlist.r_list, + nlist * sizeof (struct link_map *)); + _dl_sort_maps (&l_initfini[1], nlist - 1, NULL, false); /* Terminate the list of dependencies. */ diff --git a/elf/dl-open.c b/elf/dl-open.c index d834b89..d31356f 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -618,22 +618,25 @@ dl_open_worker (void *a) allows IFUNC relocations to work and it also means copy relocation of dependencies are if necessary overwritten. */ unsigned int nmaps = 0; - struct link_map *l = new; + unsigned int j = 0; + struct link_map *l = new->l_initfini[0]; do { if (! l->l_real->l_relocated) ++nmaps; - l = l->l_next; + l = new->l_initfini[++j]; } while (l != NULL); + /* Stack allocation is limited by the number of loaded objects. */ struct link_map *maps[nmaps]; nmaps = 0; - l = new; + j = 0; + l = new->l_initfini[0]; do { if (! l->l_real->l_relocated) maps[nmaps++] = l; - l = l->l_next; + l = new->l_initfini[++j]; } while (l != NULL); _dl_sort_maps (maps, nmaps, NULL, false); diff --git a/elf/tst-auxobj-dlopen.c b/elf/tst-auxobj-dlopen.c new file mode 100644 index 0000000..cb54aba --- /dev/null +++ b/elf/tst-auxobj-dlopen.c @@ -0,0 +1,47 @@ +/* Test for BZ#16272, dlopen'ing an auxiliary filter object. + Ensure that symbols from the resolve correctly. + + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +static int do_test (void) +{ + void *lib = xdlopen ("tst-filterobj-aux.so", RTLD_LAZY); + char *(*fn)(void) = xdlsym (lib, "get_text"); + const char* text = fn (); + + printf ("%s\n", text); + + /* Verify the text matches what we expect from the filtee */ + TEST_COMPARE_STRING (text, "Hello from filtee (PASS)"); + + fn = xdlsym (lib, "get_text2"); + text = fn (); + + printf ("%s\n", text); + + /* Verify the text matches what we expect from the auxiliary object */ + TEST_COMPARE_STRING (text, "Hello from auxiliary filter object (PASS)"); + + return 0; +} + +#include diff --git a/elf/tst-auxobj.c b/elf/tst-auxobj.c new file mode 100644 index 0000000..bdc7713 --- /dev/null +++ b/elf/tst-auxobj.c @@ -0,0 +1,42 @@ +/* Test that symbols from auxiliary filter objects are resolved to the + filtee. + + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include "tst-filterobj-filtee.h" + +static int do_test (void) +{ + const char* text = get_text (); + printf ("%s\n", text); + + /* Verify the text matches what we expect from the filtee */ + TEST_COMPARE_STRING (text, "Hello from filtee (PASS)"); + + text = get_text2 (); + printf ("%s\n", text); + + /* Verify the text matches what we expect from the auxiliary object */ + TEST_COMPARE_STRING (text, "Hello from auxiliary filter object (PASS)"); + + return 0; +} + +#include diff --git a/elf/tst-filterobj-aux.c b/elf/tst-filterobj-aux.c new file mode 100644 index 0000000..0b732f2 --- /dev/null +++ b/elf/tst-filterobj-aux.c @@ -0,0 +1,33 @@ +/* Auxiliary filter object. + Contains symbols to be resolved in filtee, and one which doesn't. + + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include "tst-filterobj-filtee.h" + +/* We never want to see the output of the auxiliary object. */ +const char *get_text (void) +{ + return "Hello from auxiliary filter object (FAIL)"; +} + +/* The filtee doesn't implement this symbol, so this should resolve. */ +const char *get_text2 (void) +{ + return "Hello from auxiliary filter object (PASS)"; +} diff --git a/elf/tst-filterobj-dlopen.c b/elf/tst-filterobj-dlopen.c new file mode 100644 index 0000000..c5b5072 --- /dev/null +++ b/elf/tst-filterobj-dlopen.c @@ -0,0 +1,39 @@ +/* Test for BZ#16272, dlopen'ing a filter object. + Ensure that symbols from the filter object resolve to the filtee. + + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +static int do_test (void) +{ + void *lib = xdlopen ("tst-filterobj-flt.so", RTLD_LAZY); + char *(*fn)(void) = xdlsym (lib, "get_text"); + const char* text = fn (); + + printf ("%s\n", text); + + /* Verify the text matches what we expect from the filtee */ + TEST_COMPARE_STRING (text, "Hello from filtee (PASS)"); + + return 0; +} + +#include diff --git a/elf/tst-filterobj-filtee.c b/elf/tst-filterobj-filtee.c new file mode 100644 index 0000000..8fa557c --- /dev/null +++ b/elf/tst-filterobj-filtee.c @@ -0,0 +1,27 @@ +/* Filtee for BZ#16272 test. + Contains desired symbol implementations. + + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include "tst-filterobj-filtee.h" + +/* This is the real implementation that wants to be called */ +const char *get_text (void) +{ + return "Hello from filtee (PASS)"; +} diff --git a/elf/tst-filterobj-filtee.h b/elf/tst-filterobj-filtee.h new file mode 100644 index 0000000..46aee28 --- /dev/null +++ b/elf/tst-filterobj-filtee.h @@ -0,0 +1,24 @@ +/* Filtee header for BZ#16272 test. + Contains prototypes for symbols implemented in the filtee. + + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +const char *get_text (void); + +/* For testing auxiliary filter object. */ +const char *get_text2 (void); diff --git a/elf/tst-filterobj-flt.c b/elf/tst-filterobj-flt.c new file mode 100644 index 0000000..5062654 --- /dev/null +++ b/elf/tst-filterobj-flt.c @@ -0,0 +1,27 @@ +/* Filter object for BZ#16272 test. + Contains symbols to be resolved in filtee. + + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include "tst-filterobj-filtee.h" + +/* We never want to see the output of the filter object */ +const char *get_text (void) +{ + return "Hello from filter object (FAIL)"; +} diff --git a/elf/tst-filterobj.c b/elf/tst-filterobj.c new file mode 100644 index 0000000..96bfae0 --- /dev/null +++ b/elf/tst-filterobj.c @@ -0,0 +1,36 @@ +/* Test that symbols from filter objects are resolved to the filtee. + + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include "tst-filterobj-filtee.h" + +static int do_test (void) +{ + const char* text = get_text (); + + printf ("%s\n", text); + + /* Verify the text matches what we expect from the filtee */ + TEST_COMPARE_STRING (text, "Hello from filtee (PASS)"); + + return 0; +} + +#include