(root)/
glibc-2.38/
elf/
tst-dlopen-self.c
       1  /* Check that dlopen'ing the executable itself fails (bug 24900).
       2     Copyright (C) 2014-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <dlfcn.h>
      20  #include <stdlib.h>
      21  #include <string.h>
      22  #include <support/check.h>
      23  
      24  /* Call dlopen and check that fails with an error message indicating
      25     an attempt to open an ET_EXEC or PIE object.  */
      26  static void
      27  check_dlopen_failure (const char *path)
      28  {
      29    void *handle = dlopen (path, RTLD_LAZY);
      30    if (handle != NULL)
      31      FAIL_EXIT1 ("dlopen succeeded unexpectedly: %s", path);
      32  
      33    const char *message = dlerror ();
      34    TEST_VERIFY_EXIT (message != NULL);
      35    if ((strstr (message,
      36  	       "cannot dynamically load position-independent executable")
      37         == NULL)
      38        && strstr (message, "cannot dynamically load executable") == NULL)
      39      FAIL_EXIT1 ("invalid dlopen error message: \"%s\"", message);
      40  }
      41  
      42  static int
      43  do_test (int argc, char *argv[])
      44  {
      45    check_dlopen_failure (argv[0]);
      46  
      47    char *full_path = realpath (argv[0], NULL);
      48    check_dlopen_failure  (full_path);
      49    free (full_path);
      50  
      51    return 0;
      52  }
      53  
      54  #define TEST_FUNCTION_ARGV do_test
      55  #include <support/test-driver.c>