summaryrefslogtreecommitdiffhomepage
path: root/src/ldso/nt64/dynlink.c
blob: c0a1f04dcf4826003f52141f35b40900d42f4778 (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
#define _BSD_SOURCE

#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include "psxglue.h"
#include "pthread_impl.h"

extern const struct __ldso_vtbl * __ldso_vtbl;
extern const struct __psx_vtbl *  __psx_vtbl;

static pthread_rwlock_t __ldso_lock;

void * dlopen(const char * file, int mode)
{
	int		status;
	void *		base;
	int		cs;
	char *		ch;
	char *		next;
	char *		epath;
	char *		lpath;
	const char **	lpathv;
	const char **	epathv;
	char		lpathbuf[2048];
	const char *	lpathvbuf[64];
	int		i;

	/* prolog */
	if (!file)
		return __ldso_vtbl->dlopen(0,mode,0,&status);

	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
	pthread_rwlock_wrlock(&__ldso_lock);
	__inhibit_ptc();

	/* loader path environment variable to loader path vector */
	if ((epath = getenv("LD_LIBRARY_PATH"))) {
		lpath = (strncpy(lpathbuf,epath,2048) < &lpathbuf[2048])
			? lpathbuf
			: strdup(epath);

		if ((i = !!lpath))
			for (ch=lpath; *ch; ch++)
				if (*ch == ':')
					i++;

		lpathv = (++i < 64)
			? lpathvbuf
			: calloc(++i,sizeof(char *));
	} else {
		lpath    = lpathbuf;
		lpathv   = lpathvbuf;
		lpath[0] = 0;
	}

	if (lpath && lpathv) {
		ch     = lpath;
		next   = *ch ? ch : 0;
		epathv = lpathv;

		for (; next; ) {
			*epathv++ = (*next == ':')
				? "."
				: next;

			ch = &next[1];

			for (; *ch; ) {
				if (*ch == ':') {
					*ch = 0;
					ch  = 0;
				} else {
					ch++;
				}
			}

			next = *ch ? ch : 0;
		}

		*epathv = 0;
	}

	/* dlopen */
	base = (lpath && lpathv)
		? __ldso_vtbl->dlopen(file,mode,lpathv,&status)
		: 0;

	/* epilog */
	if (lpath && (lpath != lpathbuf))
		free(lpath);

	if (lpathv && (lpathv != lpathvbuf))
		free(lpathv);

	__release_ptc();
	pthread_rwlock_unlock(&__ldso_lock);

	if (base)
		__psx_vtbl->do_global_ctors_fn();

	pthread_setcancelstate(cs, 0);

	return base;
}

void * __dldopen(int fd, int mode)
{
	int		status;
	void *		base;
	int		cs;

	/* prolog */
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
	pthread_rwlock_wrlock(&__ldso_lock);
	__inhibit_ptc();

	/* dldopen */
	base = __ldso_vtbl->dldopen(fd,mode,&status);

	/* epilog */
	__release_ptc();
	pthread_rwlock_unlock(&__ldso_lock);

	if (base)
		__psx_vtbl->do_global_ctors_fn();

	pthread_setcancelstate(cs, 0);

	return base;
}

void * __dlsopen(const char * file, int mode)
{
	int		status;
	void *		base;
	int		cs;

	/* prolog */
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
	pthread_rwlock_wrlock(&__ldso_lock);
	__inhibit_ptc();

	/* dlsopen */
	base = __ldso_vtbl->dlsopen(file,mode,&status);

	/* epilog */
	__release_ptc();
	pthread_rwlock_unlock(&__ldso_lock);

	if (base)
		__psx_vtbl->do_global_ctors_fn();

	pthread_setcancelstate(cs, 0);

	return base;
}

int __dladdr(const void * addr, Dl_info * info)
{
	return __ldso_vtbl->dladdr(addr,info);
}

int __dlinfo(void * dso, int req, void * res)
{
	return (__ldso_vtbl->dlinfo(dso,req,res)) ? -1 : 0;
}

void *__dlsym(void * restrict p, const char * restrict s)
{
	return __ldso_vtbl->dlsym(p,s,0);
}

int dlclose(void *p)
{
        return __ldso_vtbl->dlclose(p);
}

char * dlerror(void)
{
	return __ldso_vtbl->dlerror();
}

weak_alias(__dlsym,dlsym);
weak_alias(__dladdr,dladdr);
weak_alias(__dlinfo,dlinfo);