Blame elf/tst-dlopen-nodelete-reloc.c

Packit Service f6fae4
/* Test interactions of dlopen, NODELETE, and relocations.
Packit Service f6fae4
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Service f6fae4
   This file is part of the GNU C Library.
Packit Service f6fae4
Packit Service f6fae4
   The GNU C Library is free software; you can redistribute it and/or
Packit Service f6fae4
   modify it under the terms of the GNU Lesser General Public
Packit Service f6fae4
   License as published by the Free Software Foundation; either
Packit Service f6fae4
   version 2.1 of the License, or (at your option) any later version.
Packit Service f6fae4
Packit Service f6fae4
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service f6fae4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service f6fae4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service f6fae4
   Lesser General Public License for more details.
Packit Service f6fae4
Packit Service f6fae4
   You should have received a copy of the GNU Lesser General Public
Packit Service f6fae4
   License along with the GNU C Library; if not, see
Packit Service f6fae4
   <https://www.gnu.org/licenses/>.  */
Packit Service f6fae4
Packit Service f6fae4
/* This test exercises NODELETE propagation due to data relocations
Packit Service f6fae4
   and unique symbols, and the interaction with already-loaded
Packit Service f6fae4
   objects.  Some test objects are written in C++, to produce unique
Packit Service f6fae4
   symbol definitions.
Packit Service f6fae4
Packit Service f6fae4
   First test: Global scope variant, data relocation as the NODELETE
Packit Service f6fae4
   trigger.  mod1 is loaded first with a separate dlopen call.
Packit Service f6fae4
Packit Service f6fae4
      mod2 ---(may_finalize_mod1 relocation dependency)---> mod1
Packit Service f6fae4
    (NODELETE)                                   (marked as NODELETE)
Packit Service f6fae4
Packit Service f6fae4
   Second test: Local scope variant, data relocation.  mod3 is loaded
Packit Service f6fae4
   first, then mod5.
Packit Service f6fae4
Packit Service f6fae4
      mod5 ---(DT_NEEDED)--->  mod4  ---(DT_NEEDED)---> mod3
Packit Service f6fae4
    (NODELETE)           (not NODELETE)                  ^
Packit Service f6fae4
        \                                               / (marked as
Packit Service f6fae4
         `--(may_finalize_mod3 relocation dependency)--/   NODELETE)
Packit Service f6fae4
Packit Service f6fae4
   Third test: Shared local scope with unique symbol.  mod6 is loaded
Packit Service f6fae4
   first, then mod7.  No explicit dependencies between the two
Packit Service f6fae4
   objects, so first object has to be opened with RTLD_GLOBAL.
Packit Service f6fae4
Packit Service f6fae4
      mod7 ---(unique symbol)---> mod6
Packit Service f6fae4
                          (marked as NODELETE)
Packit Service f6fae4
Packit Service f6fae4
   Forth test: Non-shared scopes with unique symbol.  mod8 and mod10
Packit Service f6fae4
   are loaded from the main program.  mod8 loads mod9 from an ELF
Packit Service f6fae4
   constructor, mod10 loads mod11.  There are no DT_NEEDED
Packit Service f6fae4
   dependencies.  mod9 is promoted to the global scope form the main
Packit Service f6fae4
   program.  The unique symbol dependency is:
Packit Service f6fae4
Packit Service f6fae4
      mod9 ---(unique symbol)---> mod11
Packit Service f6fae4
                          (marked as NODELETE)
Packit Service f6fae4
Packit Service f6fae4
   Fifth test: Shared local scope with unique symbol, like test 3, but
Packit Service f6fae4
   this time, there is also a DT_NEEDED dependency (so no RTLD_GLOBAL
Packit Service f6fae4
   needed):
Packit Service f6fae4
Packit Service f6fae4
                 DT_NEEDED
Packit Service f6fae4
      mod13 ---(unique symbol)---> mod12
Packit Service f6fae4
                          (marked as NODELETE)
Packit Service f6fae4
Packit Service f6fae4
   Sixth test: NODELETE status is retained after relocation failure
Packit Service f6fae4
   with unique symbol dependency.  The object graph ensures that the
Packit Service f6fae4
   unique symbol binding is processed before the dlopen failure.
Packit Service f6fae4
Packit Service f6fae4
                                        DT_NEEDED
Packit Service f6fae4
     mod17  --(DT_NEEDED)--> mod15 --(unique symbol)--> mod14
Packit Service f6fae4
       \                       ^                  (RTLD_NODELETE)
Packit Service f6fae4
        \                 (DT_NEEDED)
Packit Service f6fae4
         \                     |
Packit Service f6fae4
          `---(DT_NEEDED)--> mod16
Packit Service f6fae4
                       (fails to relocate)
Packit Service f6fae4
Packit Service f6fae4
   mod14 is loaded first, and the loading mod17 is attempted.
Packit Service f6fae4
   mod14 must remain NODELETE after opening mod17 failed.  */
Packit Service f6fae4
Packit Service f6fae4
#include <stdio.h>
Packit Service f6fae4
#include <string.h>
Packit Service f6fae4
#include <stdbool.h>
Packit Service f6fae4
#include <support/check.h>
Packit Service f6fae4
#include <support/xdlfcn.h>
Packit Service f6fae4
Packit Service f6fae4
static int
Packit Service f6fae4
do_test (void)
Packit Service f6fae4
{
Packit Service f6fae4
  /* First case: global scope, regular data symbol.  Open the object
Packit Service f6fae4
     which is not NODELETE initially.  */
Packit Service f6fae4
  void *mod1 = xdlopen ("tst-dlopen-nodelete-reloc-mod1.so",
Packit Service f6fae4
                        RTLD_NOW | RTLD_GLOBAL);
Packit Service f6fae4
  /* This is used to indicate that the ELF destructor may be
Packit Service f6fae4
     called.  */
Packit Service f6fae4
  bool *may_finalize_mod1 = xdlsym (mod1, "may_finalize_mod1");
Packit Service f6fae4
  /* Open the NODELETE object.  */
Packit Service f6fae4
  void *mod2 = xdlopen ("tst-dlopen-nodelete-reloc-mod2.so", RTLD_NOW);
Packit Service f6fae4
  /* This has no effect because the DSO is directly marked as
Packit Service f6fae4
     NODELETE.  */
Packit Service f6fae4
  xdlclose (mod2);
Packit Service f6fae4
  /* This has no effect because the DSO has been indirectly marked as
Packit Service f6fae4
     NODELETE due to a relocation dependency.  */
Packit Service f6fae4
  xdlclose (mod1);
Packit Service f6fae4
Packit Service f6fae4
  /* Second case: local scope, regular data symbol.  Open the object
Packit Service f6fae4
     which is not NODELETE initially.  */
Packit Service f6fae4
  void *mod3 = xdlopen ("tst-dlopen-nodelete-reloc-mod3.so", RTLD_NOW);
Packit Service f6fae4
  bool *may_finalize_mod3 = xdlsym (mod3, "may_finalize_mod3");
Packit Service f6fae4
  /* Open the NODELETE object.  */
Packit Service f6fae4
  void *mod5 = xdlopen ("tst-dlopen-nodelete-reloc-mod5.so", RTLD_NOW);
Packit Service f6fae4
  /* Again those have no effect because of NODELETE.  */
Packit Service f6fae4
  xdlclose (mod5);
Packit Service f6fae4
  xdlclose (mod3);
Packit Service f6fae4
Packit Service f6fae4
  /* Third case: Unique symbol.  */
Packit Service f6fae4
  void *mod6 = xdlopen ("tst-dlopen-nodelete-reloc-mod6.so",
Packit Service f6fae4
                        RTLD_NOW | RTLD_GLOBAL);
Packit Service f6fae4
  bool *may_finalize_mod6 = xdlsym (mod6, "may_finalize_mod6");
Packit Service f6fae4
  void *mod7 = xdlopen ("tst-dlopen-nodelete-reloc-mod7.so", RTLD_NOW);
Packit Service f6fae4
  bool *may_finalize_mod7 = xdlsym (mod7, "may_finalize_mod7");
Packit Service f6fae4
  /* This should not have any effect because of the unique symbol and
Packit Service f6fae4
     the resulting NODELETE status.  */
Packit Service f6fae4
  xdlclose (mod6);
Packit Service f6fae4
  /* mod7 is not NODELETE and can be closed.  */
Packit Service f6fae4
  *may_finalize_mod7 = true;
Packit Service f6fae4
  xdlclose (mod7);
Packit Service f6fae4
Packit Service f6fae4
  /* Fourth case: Unique symbol, indirect loading.  */
Packit Service f6fae4
  void *mod8 = xdlopen ("tst-dlopen-nodelete-reloc-mod8.so", RTLD_NOW);
Packit Service f6fae4
  /* Also promote to global scope.  */
Packit Service f6fae4
  void *mod9 = xdlopen ("tst-dlopen-nodelete-reloc-mod9.so",
Packit Service f6fae4
                        RTLD_NOW | RTLD_NOLOAD | RTLD_GLOBAL);
Packit Service f6fae4
  bool *may_finalize_mod9 = xdlsym (mod9, "may_finalize_mod9");
Packit Service f6fae4
  xdlclose (mod9);              /* Drop mod9 reference.  */
Packit Service f6fae4
  void *mod10 = xdlopen ("tst-dlopen-nodelete-reloc-mod10.so", RTLD_NOW);
Packit Service f6fae4
  void *mod11 = xdlopen ("tst-dlopen-nodelete-reloc-mod11.so",
Packit Service f6fae4
                        RTLD_NOW | RTLD_NOLOAD);
Packit Service f6fae4
  bool *may_finalize_mod11 = xdlsym (mod11, "may_finalize_mod11");
Packit Service f6fae4
  xdlclose (mod11);              /* Drop mod11 reference.  */
Packit Service f6fae4
  /* mod11 is not NODELETE and can be closed.  */
Packit Service f6fae4
  *may_finalize_mod11 = true;
Packit Service f6fae4
  /* Trigger closing of mod11, too.  */
Packit Service f6fae4
  xdlclose (mod10);
Packit Service f6fae4
  /* Does not trigger closing of mod9.  */
Packit Service f6fae4
  xdlclose (mod8);
Packit Service f6fae4
Packit Service f6fae4
  /* Fifth case: Unique symbol, with DT_NEEDED dependency.  */
Packit Service f6fae4
  void *mod12 = xdlopen ("tst-dlopen-nodelete-reloc-mod12.so", RTLD_NOW);
Packit Service f6fae4
  bool *may_finalize_mod12 = xdlsym (mod12, "may_finalize_mod12");
Packit Service f6fae4
  void *mod13 = xdlopen ("tst-dlopen-nodelete-reloc-mod13.so", RTLD_NOW);
Packit Service f6fae4
  bool *may_finalize_mod13 = xdlsym (mod13, "may_finalize_mod13");
Packit Service f6fae4
  /* This should not have any effect because of the unique symbol. */
Packit Service f6fae4
  xdlclose (mod12);
Packit Service f6fae4
  /* mod13 is not NODELETE and can be closed.  */
Packit Service f6fae4
  *may_finalize_mod13 = true;
Packit Service f6fae4
  xdlclose (mod13);
Packit Service f6fae4
Packit Service f6fae4
  /* Sixth case: Unique symbol binding must not cause loss of NODELETE
Packit Service f6fae4
     status.  */
Packit Service f6fae4
  void *mod14 = xdlopen ("tst-dlopen-nodelete-reloc-mod14.so",
Packit Service f6fae4
                         RTLD_NOW | RTLD_NODELETE);
Packit Service f6fae4
  bool *may_finalize_mod14 = xdlsym (mod14, "may_finalize_mod14");
Packit Service f6fae4
  TEST_VERIFY (dlopen ("tst-dlopen-nodelete-reloc-mod17.so", RTLD_NOW)
Packit Service f6fae4
               == NULL);
Packit Service f6fae4
  const char *message = dlerror ();
Packit Service f6fae4
  printf ("info: test 6 message: %s\n", message);
Packit Service f6fae4
  /* This must not close the object, it must still be NODELETE.  */
Packit Service f6fae4
  xdlclose (mod14);
Packit Service f6fae4
  xdlopen ("tst-dlopen-nodelete-reloc-mod14.so", RTLD_NOW | RTLD_NOLOAD);
Packit Service f6fae4
Packit Service f6fae4
  /* Prepare for process exit.  Destructors for NODELETE objects will
Packit Service f6fae4
     be invoked.  */
Packit Service f6fae4
  *may_finalize_mod1 = true;
Packit Service f6fae4
  *may_finalize_mod3 = true;
Packit Service f6fae4
  *may_finalize_mod6 = true;
Packit Service f6fae4
  *may_finalize_mod9 = true;
Packit Service f6fae4
  *may_finalize_mod12 = true;
Packit Service f6fae4
  *may_finalize_mod14 = true;
Packit Service f6fae4
  return 0;
Packit Service f6fae4
}
Packit Service f6fae4
Packit Service f6fae4
#include <support/test-driver.c>