aboutsummaryrefslogtreecommitdiff
blob: 5d8a7831b23cdeb6a1c46876d8ac43584c7e5595 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <dlfcn.h>
#include <link.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gnu/lib-names.h>
#include <first-versions.h>

static int
do_test (void)
{
  void *handle = dlopen ("modstatic2-nonexistent.so", RTLD_LAZY);
  if (handle == NULL)
    printf ("nonexistent: %s\n", dlerror ());
  else
    exit (1);

  handle = dlopen ("modstatic2.so", RTLD_LAZY);
  if (handle == NULL)
    {
      printf ("%s\n", dlerror ());
      exit (1);
    }

  int (*test) (FILE *, int);
  test = dlsym (handle, "test");
  if (test == NULL)
    {
      printf ("%s\n", dlerror ());
      exit (1);
    }

  Dl_info info;
  int res = dladdr (test, &info);
  if (res == 0)
    {
      puts ("dladdr returned 0");
      exit (1);
    }
  else
    {
      if (strstr (info.dli_fname, "modstatic2.so") == NULL
	  || strcmp (info.dli_sname, "test") != 0)
	{
	  printf ("fname %s sname %s\n", info.dli_fname, info.dli_sname);
	  exit (1);
	}
      if (info.dli_saddr != (void *) test)
	{
	  printf ("saddr %p != test %p\n", info.dli_saddr, test);
	  exit (1);
	}
    }

  ElfW(Sym) *sym;
  void *symp;
  res = dladdr1 (test, &info, &symp, RTLD_DL_SYMENT);
  if (res == 0)
    {
      puts ("dladdr1 returned 0");
      exit (1);
    }
  else
    {
      if (strstr (info.dli_fname, "modstatic2.so") == NULL
	  || strcmp (info.dli_sname, "test") != 0)
	{
	  printf ("fname %s sname %s\n", info.dli_fname, info.dli_sname);
	  exit (1);
	}
      if (info.dli_saddr != (void *) test)
	{
	  printf ("saddr %p != test %p\n", info.dli_saddr, test);
	  exit (1);
	}
      sym = symp;
      if (sym == NULL)
	{
	  puts ("sym == NULL\n");
	  exit (1);
	}
      if (ELF32_ST_BIND (sym->st_info) != STB_GLOBAL
	  || ELF32_ST_VISIBILITY (sym->st_other) != STV_DEFAULT)
	{
	  printf ("bind %d visibility %d\n",
		  (int) ELF32_ST_BIND (sym->st_info),
		  (int) ELF32_ST_VISIBILITY (sym->st_other));
	  exit (1);
	}
    }

  Lmid_t lmid;
  res = dlinfo (handle, RTLD_DI_LMID, &lmid);
  if (res != 0)
    {
      printf ("dlinfo returned %d %s\n", res, dlerror ());
      exit (1);
    }
  else if (lmid != LM_ID_BASE)
    {
      printf ("lmid %d != %d\n", (int) lmid, (int) LM_ID_BASE);
      exit (1);
    }

  res = test (stdout, 2);
  if (res != 4)
    {
      printf ("Got %i, expected 4\n", res);
      exit (1);
    }

  void *handle2 = dlopen (LIBDL_SO, RTLD_LAZY);
  if (handle2 == NULL)
    {
      printf ("libdl.so: %s\n", dlerror ());
      exit (1);
    }

  /* _exit is very unlikely to receive a second symbol version.  */
  void *exit_ptr = dlvsym (handle2, "_exit", FIRST_VERSION_libc__exit_STRING);
  if (exit_ptr == NULL)
    {
      printf ("dlvsym: %s\n", dlerror ());
      exit (1);
    }
  if (exit_ptr != dlsym (handle2, "_exit"))
    {
      printf ("dlvsym for _exit does not match dlsym\n");
      exit (1);
    }

  void *(*dlsymfn) (void *, const char *);
  dlsymfn = dlsym (handle2, "dlsym");
  if (dlsymfn == NULL)
    {
      printf ("dlsym \"dlsym\": %s\n", dlerror ());
      exit (1);
    }
  void *test2 = dlsymfn (handle, "test");
  if (test2 == NULL)
    {
      printf ("%s\n", dlerror ());
      exit (1);
    }
  else if (test2 != (void *) test)
    {
      printf ("test %p != test2 %p\n", test, test2);
      exit (1);
    }

  dlclose (handle2);
  dlclose (handle);

  handle = dlmopen (LM_ID_BASE, "modstatic2.so", RTLD_LAZY);
  if (handle == NULL)
    {
      printf ("%s\n", dlerror ());
      exit (1);
    }
  dlclose (handle);

  handle = dlmopen (LM_ID_NEWLM, "modstatic2.so", RTLD_LAZY);
  if (handle == NULL)
    printf ("LM_ID_NEWLM: %s\n", dlerror ());
  else
    {
      puts ("LM_ID_NEWLM unexpectedly succeeded");
      exit (1);
    }

  return 0;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"