dll_psp2.c 4.4 KB
Newer Older
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
/*
dll_vita.c - libdl function wrappers for vita
Copyright (C) 2018 fgsfds

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/

#include "dll_psp2.h"
#include <vitasdk.h>

#define MAX_DLNAMELEN 256

typedef struct dll_s
{
	SceUID handle;
	char name[MAX_DLNAMELEN];
	int refcnt;
	dllexport_t *exp;
	struct dll_s *next;
} dll_t;

typedef struct modarg_s
{
	sysfuncs_t imports;
	dllexport_t *exports;
} modarg_t;

static sysfuncs_t sys_exports = {
	// mem
	malloc,
	calloc,
	realloc,
	free,
	// io
	fopen,
	fclose,
	fseek,
	ftell,
	fprintf,
	fread,
	fwrite,
	// string
	sprintf,
	snprintf,
	vsnprintf,
};

static modarg_t modarg;

static dll_t *dll_list;
static char *dll_err = NULL;
static char dll_err_buf[1024];

static void *dlfind( const char *name )
{
	dll_t *d = NULL;
	for( d = dll_list; d; d = d->next )
		if( !strcmp( d->name, name ) )
			break;
	return d;
}

static const char *dlname( void *handle )
{
	dll_t *d = NULL;
	// iterate through all dll_ts to check if the handle is actually in the list
	// and not some bogus pointer from god knows where
	for( d = dll_list; d; d = d->next ) if( d == handle ) break;
	return d ? d->name : NULL;
}

void *dlopen( const char *name, int flag )
{
	if( !name ) return NULL;

	dll_t *old = dlfind( name );
	if( old ) { old->refcnt++; return old; }

	char fullpath[128];
	snprintf( fullpath, sizeof(fullpath), name );

	modarg.imports = sys_exports;
	modarg.exports = NULL;

	int status = 0;
	modarg_t *arg = &modarg;
	SceUID h = sceKernelLoadStartModule( fullpath, sizeof( arg ), &arg, 0, NULL, &status );

	if( !h ) { dll_err = "dlopen(): something went wrong"; return NULL; }
	if( h < 0 )
	{
		snprintf( dll_err_buf, sizeof( dll_err_buf ), "dlopen(%s): error 0x%X\n", name, h );
		dll_err = dll_err_buf;
		return NULL;
	}

	if( status == SCE_KERNEL_START_FAILED || status == SCE_KERNEL_START_NO_RESIDENT )
	{
		dll_err = "dlopen(): module_start() failed";
		return NULL;
	}

	if( !modarg.exports )
	{
		dll_err = "dlopen(): NULL exports";
		return NULL;
	}

	dll_t *new = calloc( 1, sizeof( dll_t ) );
	if( !new ) { dll_err = "dlopen(): out of memory";  return NULL; }
	snprintf( new->name, MAX_DLNAMELEN, name );
	new->handle = h;
	new->exp = modarg.exports;
	new->refcnt = 1;

	new->next = dll_list;
	dll_list = new;

	return new;
}

void *dlsym( void *handle, const char *symbol )
{
	if( !handle || !symbol ) { dll_err = "dlsym(): NULL args"; return NULL; }
	if( !dlname( handle ) ) { dll_err = "dlsym(): unknown handle"; return NULL; }
	dll_t *d = handle;
	if( !d->refcnt ) { dll_err = "dlsym(): call dlopen() first"; return NULL; }
	dllexport_t *f = NULL;
	for( f = d->exp; f && f->func; f++ )
		if( !strcmp( f->name, symbol ) )
			break;

	if( f && f->func )
	{
		return f->func;
	}
	else
	{
		dll_err = "dlsym(): symbol not found in dll";
		return NULL;
	}
}

int dlclose( void *handle )
{
	if( !handle ) { dll_err = "dlclose(): NULL arg"; return -1; }
	if( !dlname( handle ) ) { dll_err = "dlclose(): unknown handle"; return -2; }

	dll_t *d = handle;
	d->refcnt--;
	if( d->refcnt <= 0 )
	{
		int status = 0;
		int ret = sceKernelStopUnloadModule( d->handle, 0, NULL, 0, NULL, &status );
		if( ret != SCE_OK )
		{
			snprintf( dll_err_buf, sizeof( dll_err_buf ), "dlclose(): error %d", ret );
			dll_err = dll_err_buf;
		}
		else if( status == SCE_KERNEL_STOP_CANCEL )
		{
			dll_err = "dlclose(): module doesn't want to stop";
			return -3;
		}

		if( d == dll_list )
Rinnegatamante's avatar
Rinnegatamante committed
175
			dll_list = d->next;
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
		else
			for( dll_t *pd = dll_list; pd; pd = pd->next )
			{
				if( pd->next == d )
				{
					pd->next = d->next;
					break;
				}
			}

		free( d );
	}

	return 0;
}

char *dlerror( void )
{
	char *err = dll_err;
	dll_err = NULL;
	return err;
}

int dladdr( const void *addr, Dl_info *info )
{
	dll_t *d = NULL;
	dllexport_t *f = NULL;
	for( d = dll_list; d; d = d->next )
		for( f = d->exp; f && f->func; f++ )
			if( f->func == addr ) goto for_end;
for_end:
	if( d && f && f->func )
	{
		if( info )
		{
			info->dli_fhandle = d;
			info->dli_sname = f->name;
			info->dli_saddr = addr;
		}
		return 1;
	}
	return 0;
}