1 /*_. Kernel 0.0: dynload.c */
4 /* Dynamic Loader for Kernel */
17 # define MAXPATHLEN 1024
19 static void make_filename (const char *name
, char *filename
);
20 static void make_init_fn (const char *name
, char *init_fn
);
22 /*_ . System-specific includes */
27 /*_ , Otherwise assume SUN */
29 typedef void *HMODULE
;
30 typedef void (*FARPROC
) ();
43 display_w32_error_msg (const char *additional_message
)
46 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
|
47 FORMAT_MESSAGE_ALLOCATE_BUFFER
, NULL
, GetLastError (), 0,
48 (LPTSTR
) & msg_buf
, 0, NULL
);
49 fprintf (stderr
, "klink load-extension: %s: %s", additional_message
,
54 dl_attach (const char *module
)
56 HMODULE dll
= LoadLibrary (module
);
58 display_w32_error_msg (module
);
63 dl_proc (HMODULE mo
, const char *proc
)
65 FARPROC procedure
= GetProcAddress (mo
, proc
);
67 display_w32_error_msg (proc
);
72 dl_detach (HMODULE mo
)
74 (void) FreeLibrary (mo
);
85 dl_attach (const char *module
)
87 HMODULE so
= dlopen (module
, RTLD_LAZY
);
90 fprintf (stderr
, "Error loading klink extension \"%s\": %s\n", module
,
97 dl_proc (HMODULE mo
, const char *proc
)
100 FARPROC fp
= (FARPROC
) dlsym (mo
, proc
);
101 if ((errmsg
= dlerror ()) == 0)
105 fprintf (stderr
, "Error initializing kernel module \"%s\": %s\n", proc
,
111 dl_detach (HMODULE mo
)
117 klink_load_ext (klink
* sc
, pko args
)
121 char filename
[MAXPATHLEN
], init_fn
[MAXPATHLEN
+ 6];
124 void (*module_init
) (klink
* sc
);
125 if ((args
!= K_NIL
) && is_string ((first_arg
= pair_car (sc
,args
))))
127 name
= string_value (first_arg
);
128 make_filename (name
, filename
);
129 make_init_fn (name
, init_fn
);
130 dll_handle
= dl_attach (filename
);
138 module_init
= (void (*)(klink
*)) dl_proc (dll_handle
, init_fn
);
139 if (module_init
!= 0)
160 make_filename (const char *name
, char *filename
)
162 strcpy (filename
, name
);
163 strcat (filename
, SUFFIX
);
166 make_init_fn (const char *name
, char *init_fn
)
168 const char *p
= strrchr (name
, '/');
177 strcpy (init_fn
, "init_");