summaryrefslogtreecommitdiff
path: root/src/lib/libdll/dllnext.c
blob: 9e81c4ab26d4fc09f3521f30929934cf5e0030f8 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1997-2011 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                 Eclipse Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*          http://www.eclipse.org/org/documents/epl-v10.html           *
*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                                                                      *
***********************************************************************/
#pragma prototyped
/*
 * Glenn Fowler
 * AT&T Research
 */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE	1
#endif
#ifndef __EXTENSIONS__
#define __EXTENSIONS__	1
#endif

#include <ast.h>
#include <dlldefs.h>

#if _hdr_rld_interface
#include <rld_interface.h>
#endif

/*
 * return a handle for the next layer down,
 * i.e., the next layer that has symbols covered
 * by the main prog and dll's loaded so far
 *
 * intentionally light on external lib calls
 * so this routine can be used early in process
 * startup
 */

#ifdef	_DLL_RLD_SYM

#define DEBUG		1

#if DEBUG

typedef ssize_t (*Write_f)(int, const void*, size_t);

#endif

#undef	dllnext

void*
_dll_next(int flags, _DLL_RLD_SYM_TYPE* here)
{
	register char*	vp;
	register void*	lp;
	register int	found = 0;
	char*		s;
	char*		b;
	char*		e;
	char		dummy[256];
#if DEBUG
	Write_f		wr = 0;
	Write_f		xr;
	char		buf[1024];
#endif

#if DEBUG
	if (getenv("DLL_DEBUG") && (vp = (char*)_rld_new_interface(_RLD_FIRST_PATHNAME)))
	{
		do
		{
			if (strcmp(vp, "MAIN") && (lp = dllopen(vp, flags)))
			{
				if (xr = (Write_f)dlsym(lp, "write"))
					wr = xr;
			}
		} while (vp = (char*)_rld_new_interface(_RLD_NEXT_PATHNAME));
	}
#endif
	if (vp = (char*)_rld_new_interface(_RLD_FIRST_PATHNAME))
	{
		do
		{
			if (lp = dllopen(strcmp(vp, "MAIN") ? vp : (char*)0, flags))
			{
				if (found)
				{
					b = e = 0;
					s = vp;
					for (;;)
					{
						switch (*s++)
						{
						case 0:
							break;
						case '/':
							b = s;
							e = 0;
							continue;
						case '.':
							if (!e)
								e = s - 1;
							continue;
						default:
							continue;
						}
						break;
					}
					if (b && e)
					{
						s = dummy;
						*s++ = '_';
						*s++ = '_';
						while (b < e)
							*s++ = *b++;
						b = "_dummy";
						while (*s++ = *b++);
						if (dlsym(lp, dummy))
						{
							dlclose(lp);
							lp = 0;
						}
					}
					if (lp)
					{
#if DEBUG
						if (wr)
							(*wr)(2, buf, sfsprintf(buf, sizeof(buf), "dll: next %s\n", vp));
#endif
						return lp;
					}
#if DEBUG
					else if (wr)
						(*wr)(2, buf, sfsprintf(buf, sizeof(buf), "dll: skip %s\n", vp));
#endif
				}
				else if ((_DLL_RLD_SYM_TYPE*)dlsym(lp, _DLL_RLD_SYM_STR) == here)
				{
#if DEBUG
					if (wr)
						(*wr)(2, buf, sfsprintf(buf, sizeof(buf), "dll: this %s\n", vp));
#endif
					found = 1;
				}
			}
		} while (vp = (char*)_rld_new_interface(_RLD_NEXT_PATHNAME));
	}
	return dllnext(flags);
}

#endif

#ifndef RTLD_NEXT
#if _dll_DYNAMIC

#include <link.h>

extern struct link_dynamic	_DYNAMIC;

#endif
#endif

void*
dllnext(int flags)
{
	register void*			dll;
#ifndef RTLD_NEXT
#if _dll_DYNAMIC
	register struct link_map*	map;
	register char*			s;
	register char*			b;
#endif
	register char*			ver;
	char*				path;

	static char			next[] = { _DLL_NEXT_PATH };
#endif

#ifdef RTLD_NEXT
	dll = RTLD_NEXT;
#else
	path = next;
#if _dll_DYNAMIC
	for (map = _DYNAMIC.ld_un.ld_1->ld_loaded; map; map = map->lm_next)
	{
		b = 0;
		s = map->lm_name;
		while (*s)
			if (*s++ == '/')
				b = s;
		if (b && b[0] == 'l' && b[1] == 'i' && b[2] == 'b' && b[3] == 'c' && b[4] == '.')
		{
			path = map->lm_name;
			break;
		}
	}
#endif
	ver = path + strlen(path);
	while (!(dll = dllopen(path, flags)))
	{
		do
		{
			if (ver <= path)
				return 0;
		} while (*--ver != '.');
		if (*(ver + 1) <= '0' || *(ver + 1) >= '9')
			return 0;
		*ver = 0;
	}
#endif
	return dll;
}