aboutsummaryrefslogtreecommitdiff
path: root/pd/src/s_loader.c
blob: 4c0ef9725ac39a4aefd650311d8513f4b3e23a03 (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
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#ifdef DL_OPEN
#include <dlfcn.h>
#endif
#ifdef UNIX
#include <stdlib.h>
#include <unistd.h>
#endif
#ifdef NT
#include <io.h>
#include <windows.h>
#endif
#ifdef MACOSX
#include <mach-o/dyld.h> 
#endif
#include <string.h>
#include "m_imp.h"
#include <stdio.h>

typedef void (*t_xxx)(void);

static char sys_dllextent[] = 
#ifdef __FreeBSD__
    ".pd_freebsd";
#endif
#ifdef IRIX
#ifdef N32
    ".pd_irix6";
#else
    ".pd_irix5";
#endif
#endif
#ifdef __linux__
    ".pd_linux";
#endif
#ifdef MACOSX
    ".pd_darwin";
#endif
#ifdef NT
    ".dll";
#endif


int sys_load_lib(char *dirname, char *classname)
{
    char symname[MAXPDSTRING], filename[MAXPDSTRING], dirbuf[MAXPDSTRING],
    	*nameptr, *lastdot;
    void *dlobj;
    t_xxx makeout;
    int fd;
#ifdef NT
    HINSTANCE ntdll;
#endif
#if 0
    fprintf(stderr, "lib %s %s\n", dirname, classname);
#endif
    if ((fd = open_via_path(dirname, classname, sys_dllextent,
    	dirbuf, &nameptr, MAXPDSTRING, 1)) < 0)
    {
    	return (0);
    }
    else
    {
    	close(fd);
    	    /* refabricate the pathname */
	strcpy(filename, dirbuf);
	strcat(filename, "/");
	strcat(filename, nameptr);
    	    /* extract the setup function name */
    	if (lastdot = strrchr(nameptr, '.'))
	    *lastdot = 0;

#ifdef MACOSX
    	strcpy(symname, "_");
    	strcat(symname, nameptr);
#else
    	strcpy(symname, nameptr);
#endif
	    /* if the last character is a tilde, replace with "_tilde" */
	if (symname[strlen(symname) - 1] == '~')
	    strcpy(symname + (strlen(symname) - 1), "_tilde");
	    /* and append _setup to form the C setup function name */
    	strcat(symname, "_setup");
#ifdef DL_OPEN
	dlobj = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
	if (!dlobj)
	{
	    post("%s: %s", filename, dlerror());
	    return (0);
	}
	makeout = (t_xxx)dlsym(dlobj,  symname);
#endif
#ifdef NT
	sys_bashfilename(filename, filename);
    	ntdll = LoadLibrary(filename);
    	if (!ntdll)
    	{
	    post("%s: couldn't load", filename);
	    return (0);
	}
    	makeout = (t_xxx)GetProcAddress(ntdll, symname);  
#endif
#ifdef MACOSX
        {
            NSObjectFileImage image; 
            void *ret;
            NSSymbol s; 
            if ( NSCreateObjectFileImageFromFile( filename, &image) != NSObjectFileImageSuccess )
            {
                post("%s: couldn't load", filename);
                return 0;
            }
            ret = NSLinkModule( image, filename, NSLINKMODULE_OPTION_BINDNOW); 
            
            s = NSLookupSymbolInModule(ret, symname); 
        
            if (s)
                makeout = (t_xxx)NSAddressOfSymbol( s);
            else makeout = 0;
        }
#endif
    }
    if (!makeout)
    {
    	post("load_object: Symbol \"%s\" not found", symname);
    	return 0;
    }
    (*makeout)();
    return (1);
}