aboutsummaryrefslogtreecommitdiff
path: root/elf/tst-dlopen-constructor-null-mod1.c
blob: 70a7a0ad46a1a666b7b0881942b7bd66c6fa4729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Module calling dlopen (NULL, RTLD_LAZY) to obtain the global scope.
   Copyright (C) 2024 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
   <https://www.gnu.org/licenses/>.  */

#include <dlfcn.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

int mod1_status;

static void __attribute__ ((constructor))
init (void)
{
  puts ("info: tst-dlopen-constructor-null-mod1.so constructor");

  void *handle = dlopen (NULL, RTLD_LAZY);
  if (handle == NULL)
    {
      printf ("error: %s\n", dlerror ());
      exit (1);
    }
  puts ("info: dlopen returned");
  if (dlsym (handle, "malloc") != malloc)
    {
      puts ("error: dlsym did not produce expected result");
      exit (1);
    }
  dlclose (handle);

  /* Check that the second module's constructor has not executed.   */
  if (getenv ("mod2_status") != NULL)
    {
      printf ("error: mod2_status environment variable set: %s\n",
              getenv ("mod2_status"));
      exit (1);
    }

  /* Communicate to the second module that the constructor executed.   */
  mod1_status = 1;
}