about summary refs log tree commit diff
path: root/elf/reldep6.c
blob: 1eeec6c862b59d41101b85e6e5a775b1b64a7e95 (plain) (blame)
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
#include <dlfcn.h>
#include <mcheck.h>
#include <stdio.h>
#include <stdlib.h>

typedef int (*fn)(void);
#define CHUNKS 1024
#define REPEAT 64

int
main (void)
{
  void *h1;
  void *h2;
  fn **foopp;
  fn bar, baz;
  int i, j;
  int n;
  void *allocs[REPEAT][CHUNKS];

  mtrace ();

  /* Open the two objects.  */
  h1 = dlopen ("reldep6mod3.so", RTLD_LAZY);
  if (h1 == NULL)
    {
      printf ("cannot open reldep6mod3.so: %s\n", dlerror ());
      exit (1);
    }

  foopp = dlsym (h1, "foopp");
  if (foopp == NULL)
    {
      printf ("cannot get address of \"foopp\": %s\n", dlerror ());
      exit (1);
    }
  n = (**foopp) ();
  if (n != 20)
    {
      printf ("(**foopp)() return %d, not return 20\n", n);
      exit (1);
    }

  h2 = dlopen ("reldep6mod4.so", RTLD_LAZY);
  if (h2 == NULL)
    {
      printf ("cannot open reldep6mod4.so: %s\n", dlerror ());
      exit (1);
    }

  baz = dlsym (h2, "baz");
  if (baz == NULL)
    {
      printf ("cannot get address of \"baz\": %s\n", dlerror ());
      exit (1);
    }
  if (baz () != 31)
    {
      printf ("baz() did not return 31\n");
      exit (1);
    }

  if (dlclose (h1) != 0)
    {
      printf ("closing h1 failed: %s\n", dlerror ());
      exit (1);
    }

  /* Clobber memory.  */
  for (i = 0; i < REPEAT; ++i)
    for (j = 0; j < CHUNKS; ++j)
      allocs[i][j] = calloc (1, j + 1);

  bar = dlsym (h2, "bar");
  if (bar == NULL)
    {
      printf ("cannot get address of \"bar\": %s\n", dlerror ());
      exit (1);
    }
  if (bar () != 40)
    {
      printf ("bar() did not return 40\n");
      exit (1);
    }

  baz = dlsym (h2, "baz");
  if (baz == NULL)
    {
      printf ("cannot get address of \"baz\": %s\n", dlerror ());
      exit (1);
    }
  if (baz () != 31)
    {
      printf ("baz() did not return 31\n");
      exit (1);
    }

  for (i = 0; i < REPEAT; ++i)
    for (j = 0; j < CHUNKS; ++j)
      free (allocs[i][j]);

  if (dlclose (h2) != 0)
    {
      printf ("closing h2 failed: %s\n", dlerror ());
      exit (1);
    }

  return 0;
}