relative pointers
[archipelago] / xseg / sys / xseg_user.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <string.h>
4 #include <dlfcn.h>
5 #include <unistd.h>
6 #include <stdint.h>
7 #include <sys/syscall.h>
8 #include <errno.h>
9 #include <sys/util.h>
10
11 int (*xseg_snprintf)(char *str, size_t size, const char *format, ...) = snprintf;
12
13 char __xseg_errbuf[4096];
14
15 void __load_plugin(const char *name)
16 {
17         void *dl;
18         void (*init)(void);
19         char _name[128];
20         unsigned int namesize = strlen(name);
21
22         strncpy(_name, "xseg_", 5);
23         strncpy(_name + 5, name, 80);
24         strncpy(_name + 5 + namesize, ".so", 3);
25         _name[5 + namesize + 3 ] = 0;
26         dl = dlopen(_name, RTLD_NOW);
27         if (!dl) {
28                 LOGMSG("Cannot load plugin '%s': %s\n", _name, dlerror());
29                 return;
30         }
31
32         strncpy(_name + 5 + namesize, "_init", 5);
33         _name[127] = 0;
34         init = (void (*)(void))(long)dlsym(dl, _name);
35         if (!init) {
36                 LOGMSG("Init function '%s' not found!\n", _name);
37                 return;
38         }
39
40         init();
41         //LOGMSG("Plugin '%s' loaded.\n", name);
42 }
43
44 uint32_t __get_id(void)
45 {
46         return syscall(SYS_gettid);
47 }
48
49 void __xseg_log(const char *msg)
50 {
51         (void)puts(msg);
52 }
53
54 void *xq_malloc(unsigned long size)
55 {
56         return malloc(size);
57 }
58
59 void xq_mfree(void *ptr)
60 {
61         free(ptr);
62 }