summary refs log tree commit diff
path: root/elf/neededtest.c
blob: 053d5528aaa534ef3302105795a9d3053782e6e1 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <link.h>

static int
check_loaded_objects (const char **loaded)
{
  struct link_map *lm;
  int n;
  int *found = NULL;
  int errors = 0;

  for (n = 0; loaded[n]; n++)
    /* NOTHING */;

  if (n)
    {
      found = (int *) alloca (sizeof (int) * n);
      memset (found, 0, sizeof (int) * n);
    }

  printf("   Name\n");
  printf(" --------------------------------------------------------\n");
  for (lm = _r_debug.r_map; lm; lm = lm->l_next)
    {
      if (lm->l_name && lm->l_name[0])
	printf(" %s\n", lm->l_name);
      if (lm->l_type == lt_loaded && lm->l_name)
	{
	  int match = 0;
	  for (n = 0; loaded[n] != NULL; n++)
	    {
	      if (strcmp (basename (loaded[n]), basename (lm->l_name)) == 0)
	        {
		  found[n] = 1;
		  match = 1;
		  break;
		}
	    }

	  if (match == 0)
	    {
	      ++errors;
	      printf ("ERRORS: %s is not unloaded\n", lm->l_name);
	    }
	}
    }

  for (n = 0; loaded[n] != NULL; n++)
    {
      if (found[n] == 0)
        {
	  ++errors;
	  printf ("ERRORS: %s is not loaded\n", loaded[n]);
	}
    }

  return errors;
}

int
main (void)
{
  void *obj2;
  void *obj3;
  const char *loaded[] = { NULL, NULL, NULL, NULL };
  int errors = 0;

  printf ("\nThis is what is in memory now:\n");
  errors += check_loaded_objects (loaded);
  printf( "Loading shared object neededobj3.so\n");
  obj3 = dlopen( "neededobj3.so", RTLD_LAZY);
  if (obj3 == NULL)
    {
      printf ("%s\n", dlerror ());
      exit (1);
    }
  printf ("And this is what is now in memory\n");
  loaded[0] = "neededobj1.so";
  loaded[1] = "neededobj2.so";
  loaded[2] = "neededobj3.so";
  errors += check_loaded_objects (loaded);
  printf ("Now loading shared object neededobj2.so\n");
  obj2 = dlopen ("neededobj2.so", RTLD_LAZY);
  if (obj2 == NULL)
    {
      printf ("%s\n", dlerror ());
      exit (1);
    }
  printf ("Again, this is what is in memory\n");
  errors += check_loaded_objects (loaded);
  printf ("Closing neededobj2.so\n");
  dlclose (obj2);
  errors += check_loaded_objects (loaded);
  printf ("Closing neededobj3.so\n");
  dlclose (obj3);
  loaded[0] = NULL;
  errors += check_loaded_objects (loaded);
  if (errors != 0)
    printf ("%d errorss found\n", errors);
  return errors;
}