1/* Module calling dlopen (NULL, RTLD_LAZY) to obtain the global scope.
2 Copyright (C) 2024 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 <stddef.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24int mod1_status;
25
26static void __attribute__ ((constructor))
27init (void)
28{
29 puts (s: "info: tst-dlopen-constructor-null-mod1.so constructor");
30
31 void *handle = dlopen (NULL, RTLD_LAZY);
32 if (handle == NULL)
33 {
34 printf (format: "error: %s\n", dlerror ());
35 exit (status: 1);
36 }
37 puts (s: "info: dlopen returned");
38 if (dlsym (handle: handle, name: "malloc") != malloc)
39 {
40 puts (s: "error: dlsym did not produce expected result");
41 exit (status: 1);
42 }
43 dlclose (handle: handle);
44
45 /* Check that the second module's constructor has not executed. */
46 if (getenv (name: "mod2_status") != NULL)
47 {
48 printf (format: "error: mod2_status environment variable set: %s\n",
49 getenv (name: "mod2_status"));
50 exit (status: 1);
51 }
52
53 /* Communicate to the second module that the constructor executed. */
54 mod1_status = 1;
55}
56

source code of glibc/elf/tst-dlopen-constructor-null-mod1.c