Hi,
[Ian: this identical to the patch I sent to you.]
The attached patch modifies Jolt so that its only exported primitive
is _libid instead of dlsym and the RTLD_* constants. It is intended
for _libid to provide a stable ABI so that all Jolt code can work
across platforms without modification.
For this reason, I've introduced some cross-platform ID_DLOPEN* and
ID_DLSYM* constants to id.h (with appropriate glue code in libid.c),
and numbered all the elements of struct __libid, so that Jolt code can
call them by ((long@ _libid OFFSET) ...). This will only be needed
until Jolt understands C and can use the symbolic struct member names
instead.
Have fun,
--
Michael FIG <[EMAIL PROTECTED]> //\
http://michael.fig.org/ \//
Export only _libid as a Jolt primitive.
diff -r 582a6512823c function/jolt-burg/Compiler.st
--- a/function/jolt-burg/Compiler.st Mon Dec 03 19:00:36 2007 -0600
+++ b/function/jolt-burg/Compiler.st Mon Dec 03 19:57:08 2007 -0600
@@ -160,11 +160,7 @@ Symbol _dlsym_: _string { _return (oop)_
[
| _value |
- { v__value= (oop)RTLD_DEFAULT; }. TheGlobalEnvironment defineVariable: #_RTLD_DEFAULT value_: _value.
- { v__value= (oop)RTLD_LAZY; }. TheGlobalEnvironment defineVariable: #_RTLD_LAZY value_: _value.
- { v__value= (oop)RTLD_NOW; }. TheGlobalEnvironment defineVariable: #_RTLD_NOW value_: _value.
- { v__value= (oop)RTLD_GLOBAL; }. TheGlobalEnvironment defineVariable: #_RTLD_GLOBAL value_: _value.
- TheGlobalEnvironment defineVariable: #_dlsym value_: #dlsym _dlsym.
+ { v__value= (oop)_libid; }. TheGlobalEnvironment defineVariable: #_libid value_: _value.
]
"----------------------------------------------------------------"
diff -r 582a6512823c function/jolt-burg/boot.k
--- a/function/jolt-burg/boot.k Mon Dec 03 19:00:36 2007 -0600
+++ b/function/jolt-burg/boot.k Mon Dec 03 19:57:08 2007 -0600
@@ -17,14 +17,14 @@
;;; Last edited: 2007-07-03 13:11:04 by piumarta on emilia.lax04.mtt
-(define __dlsym _dlsym)
-
-(define printf (__dlsym _RTLD_DEFAULT "printf"))
-(define exit (__dlsym _RTLD_DEFAULT "exit"))
+;; _libid->dlsym(ID_DLSYM_DEFAULT, sym)
+(define printf ((long@ _libid 1) 0 "printf"))
+(define exit ((long@ _libid 1) 0 "exit"))
(define _dlsym
(lambda (lib name)
- (let ((addr (__dlsym lib name)))
+ ;; _libid->dlsym(lib, name)
+ (let ((addr ((long@ _libid 1) lib name)))
(or addr
(let ()
(printf "%s: symbol lookup failed\n" name)
@@ -33,7 +33,7 @@
(define dlsym
(lambda (name)
- (_dlsym _RTLD_DEFAULT name)))
+ (_dlsym 0 name)))
(define sprintf (dlsym "sprintf"))
(define malloc (dlsym "malloc"))
@@ -42,14 +42,17 @@
(define strcmp (dlsym "strcmp"))
(define usleep (dlsym "usleep"))
-(define _dlopen (dlsym "dlopen"))
-(define dlclose (dlsym "dlclose"))
+(define dlclose
+ (lambda (lib)
+ ;; _libid->dlclose(lib)
+ ((long@ _libid 2) lib)))
(define %%dlopen
(lambda (dir lib ext)
(let ((buf (malloc 1024)))
(sprintf buf "%s%s%s" dir lib ext)
- (let ((handle (_dlopen buf (| _RTLD_NOW _RTLD_GLOBAL))))
+ ;; _libid->dlopen(buf, ID_DLOPEN_NOW|ID_DLOPEN_GLOBAL)
+ (let ((handle ((long@ _libid 0) buf 258)))
(free buf)
handle))))
@@ -76,8 +79,14 @@
;; import/export to/from the object namespace
-(define import (dlsym "_libid_import"))
-(define export (dlsym "_libid_export"))
+(define import
+ (lambda (key)
+ ;; _libid->import(key);
+ ((long@ _libid 24) key)))
+(define export
+ (lambda (key value)
+ ;; _libid->export(key, value)
+ ((long@ _libid 25) key value)))
(define Object (import "Object"))
(define OS (import "OS"))
@@ -97,7 +106,10 @@
;; message send syntax
-(define _bind (dlsym "_libid_bind"))
+(define _bind
+ (lambda (selector receiver)
+ ;; _libid->bind(selector, receiver)
+ ((long@ _libid 32) selector receiver)))
(define Array (import "Array"))
(define Array__new_ (long@ (_bind 'new: Array)))
diff -r 582a6512823c function/jolt-burg/debug.k
--- a/function/jolt-burg/debug.k Mon Dec 03 19:00:36 2007 -0600
+++ b/function/jolt-burg/debug.k Mon Dec 03 19:57:08 2007 -0600
@@ -20,7 +20,10 @@
(define error (lambda (fmt a b c d e f g h) (printf fmt a b c d e f g h) (printf "\n") (exit 1)))
(define syntax-error (lambda (form message) (printf "syntax error: expected %s\n" message) (exit 1)))
-(define _param (dlsym "_libid_param"))
+(define _param
+ (lambda (index)
+ ;; _libid->param(index)
+ ((long@ _libid 26) index)))
(define %main (long@ (_param 1)))
(define %sysarch (_param 5))
@@ -95,7 +98,10 @@
(while (get-caller-fp (get-caller-fp %top-level-fp))
(set %top-level-fp (get-caller-fp %top-level-fp)))
-(define _libid_backtrace (dlsym "_libid_backtrace"))
+(define _libid_backtrace
+ (lambda ()
+ ;; _libid->backtrace()
+ ((long@ _libid 45))))
(define backtrace
(lambda (result)
@@ -113,7 +119,7 @@
(printf "; ---- end of backtrace\n")
result))
-(let ((main (long@ ((dlsym "_libid_param") 1)))
+(let ((main (long@ (_param 1)))
(symbols (malloc 8192)))
(sprintf symbols "%s.sym" main)
(load-if-present symbols)
diff -r 582a6512823c object/id/id.h
--- a/object/id/id.h Mon Dec 03 19:00:36 2007 -0600
+++ b/object/id/id.h Mon Dec 03 19:57:08 2007 -0600
@@ -28,13 +28,25 @@ struct __methodinfo
void *meta;
};
+enum __libid_dlopen_mode
+{
+ ID_DLOPEN_LAZY= (1 << 0),
+ ID_DLOPEN_NOW= (1 << 1),
+ ID_DLOPEN_GLOBAL= (1 << 8)
+};
+#define ID_DLOPEN_SELF 0
+#define ID_DLSYM_DEFAULT ((id_dl_t) 0)
+
+/* This is an opaque type, casted to a platform-dependent handle. */
+typedef struct _id_dl_t *id_dl_t;
+
struct __libid
{
/* bootstrap */
- dlhandle_t (*dlopen )(const char *path, int mode);
- void *(*dlsym )(dlhandle_t handle, const char *symbol);
- int (*dlclose)(dlhandle_t handle);
+ id_dl_t (*dlopen )(const char *path, int mode); /* 0 */
+ void *(*dlsym )(id_dl_t handle, const char *symbol); /* 1 */
+ int (*dlclose)(id_dl_t handle); /* 2 */
void *unused03;
void *unused04;
@@ -44,9 +56,9 @@ struct __libid
/* globals */
- oop _object;
- oop tag_vtable;
- oop nil_vtable;
+ oop _object; /* 8 */
+ oop tag_vtable; /* 9 */
+ oop nil_vtable; /* 10 */
void *unused11;
void *unused12;
@@ -56,21 +68,21 @@ struct __libid
/* allocation */
- oop (*intern)(const char *string);
- oop (*proto)(oop base);
- void (*method)(oop type, oop selector, _imp_t method);
- oop (*alloc)(oop type, size_t size);
- oop *(*palloc)(size_t size);
- void *(*balloc)(size_t size);
+ oop (*intern)(const char *string); /* 16 */
+ oop (*proto)(oop base); /* 17 */
+ void (*method)(oop type, oop selector, _imp_t method); /* 18 */
+ oop (*alloc)(oop type, size_t size); /* 19 */
+ oop *(*palloc)(size_t size); /* 20 */
+ void *(*balloc)(size_t size); /* 21 */
void *unused22;
void *unused23;
/* environment */
- oop (*import)(const char *key);
- oop (*export)(const char *key, oop value);
- void *(*param)(int index);
+ oop (*import)(const char *key); /* 24 */
+ oop (*export)(const char *key, oop value); /* 25 */
+ void *(*param)(int index); /* 26 */
void *unused27;
void *unused28;
@@ -80,14 +92,14 @@ struct __libid
/* messaging */
- struct __closure *(*bind )(oop selector, oop receiver);
- struct __lookup (*bind2)(oop selector, oop receiver);
+ struct __closure *(*bind )(oop selector, oop receiver); /* 32 */
+ struct __lookup (*bind2)(oop selector, oop receiver); /* 33 */
void *unused34;
void *unused35;
- oop (*nlreturn)(oop nlr, oop result);
- oop (*nlresult)(void);
+ oop (*nlreturn)(oop nlr, oop result); /* 36 */
+ oop (*nlresult)(void); /* 37 */
void *unused38;
void *unused39;
@@ -96,11 +108,11 @@ struct __libid
/* debugging */
- void *(*enter)(struct __methodinfo *info);
- void (*line)(int line);
- void (*leave)(void *cookie);
- char *(*backtrace)(void);
- void *(*methodAt)(int offset);
+ void *(*enter)(struct __methodinfo *info); /* 42 */
+ void (*line)(int line); /* 43 */
+ void (*leave)(void *cookie); /* 44 */
+ char *(*backtrace)(void); /* 45 */
+ void *(*methodAt)(int offset); /* 46 */
void *unused47;
void *unused48;
@@ -110,10 +122,10 @@ struct __libid
/* gc */
- void (*gc_addRoots)(char *low, char *high);
- void (*gc_gcollect)(void);
- int (*gc_generalRegisterDisappearingLink)(void **link, void *obj);
- int (*gc_unregisterDisappearingLink)(void **link);
+ void (*gc_addRoots)(char *low, char *high); /* 52 */
+ void (*gc_gcollect)(void); /* 53 */
+ int (*gc_generalRegisterDisappearingLink)(void **link, void *obj); /* 54 */
+ int (*gc_unregisterDisappearingLink)(void **link); /* 55 */
void *unused56;
void *unused57;
diff -r 582a6512823c object/id/libid.c
--- a/object/id/libid.c Mon Dec 03 19:00:36 2007 -0600
+++ b/object/id/libid.c Mon Dec 03 19:57:08 2007 -0600
@@ -12,20 +12,6 @@
#include <sys/stat.h>
#include <sys/param.h>
#include <assert.h>
-#if defined(WIN32)
-# include <windows.h>
- typedef HINSTANCE dlhandle_t;
-# define RTLD_DEFAULT 0
-# define RTLD_LAZY 0
-# define RTLD_GLOBAL 0
-# define RTLD_NOW 0
- dlhandle_t dlopen(const char *path, int mode);
- void *dlsym(dlhandle_t handle, const char *symbol);
- int dlclose(dlhandle_t handle);
-#else
-# include <dlfcn.h>
- typedef void *dlhandle_t;
-#endif
#include "gc/gc.h"
@@ -69,6 +55,9 @@ void _libid_line(int line);
void _libid_line(int line);
void _libid_leave(void *cookie);
char *_libid_backtrace(void);
+id_dl_t _libid_dlopen(const char *path, int mode);
+void *_libid_dlsym(id_dl_t handle, const char *symbol);
+int _libid_dlclose(id_dl_t handle);
#define _send(MSG, RCV, ARG...) ({ \
oop _r= (RCV); \
@@ -436,21 +425,21 @@ static oop _object___beNilType(oop _thun
return _libid_nil_vtable= _object___vtable(0, self, self);
}
-#define ID_RTLD_FLAGS RTLD_LAZY | RTLD_GLOBAL
+#define ID_DLOPEN_FLAGS ID_DLOPEN_LAZY | ID_DLOPEN_GLOBAL
static oop _object___import_(oop _thunk, oop state, oop self, char *fileName, char *initName)
{
- dlhandle_t lib= 0;
+ id_dl_t lib= 0;
void *init= 0;
- init= dlsym(RTLD_DEFAULT, initName);
- dprintf("dlsym RTLD_DEFAULT %s -> %p\n", initName, init);
+ init= _libid_dlsym(ID_DLSYM_DEFAULT, initName);
+ dprintf("_libid_dlsym ID_DLSYM_DEFAULT %s -> %p\n", initName, init);
if (!init)
{
- lib= dlopen(0, ID_RTLD_FLAGS);
- dprintf("1 dlopen 0 -> %p\n", lib);
- init= dlsym(lib, initName);
- dprintf("2 dlsym %p %s -> %p\n", lib, initName, init);
- dlclose(lib);
+ lib= _libid_dlopen(ID_DLOPEN_SELF, ID_DLOPEN_FLAGS);
+ dprintf("1 _libid_dlopen self -> %p\n", lib);
+ init= _libid_dlsym(lib, initName);
+ dprintf("2 _libid_dlsym %p %s -> %p\n", lib, initName, init);
+ _libid_dlclose(lib);
}
if (init)
{
@@ -460,21 +449,21 @@ static oop _object___import_(oop _thunk,
{
char path[MAXPATHLEN];
snprintf(path, MAXPATHLEN, "%s.so", fileName);
- lib= dlopen(path, ID_RTLD_FLAGS);
- dprintf("3 dlopen %s -> %p\n", path, lib);
+ lib= _libid_dlopen(path, ID_DLOPEN_FLAGS);
+ dprintf("3 _libid_dlopen %s -> %p\n", path, lib);
if (!lib)
{
snprintf(path, MAXPATHLEN, "./%s.so", fileName);
- lib= dlopen(path, ID_RTLD_FLAGS);
- dprintf("4 dlopen %s -> %p\n", path, lib);
+ lib= _libid_dlopen(path, ID_DLOPEN_FLAGS);
+ dprintf("4 _libid_dlopen %s -> %p\n", path, lib);
}
if (!lib)
{
char *prefix;
if (!(prefix= getenv("IDC_LIBDIR"))) prefix= PREFIX;
snprintf(path, MAXPATHLEN, "%s%s.so", prefix, fileName);
- lib= dlopen(path, ID_RTLD_FLAGS);
- dprintf("5 dlopen %s -> %p\n", path, lib);
+ lib= _libid_dlopen(path, ID_DLOPEN_FLAGS);
+ dprintf("5 _libid_dlopen %s -> %p\n", path, lib);
}
# if defined(WIN32)
if (!lib)
@@ -482,19 +471,19 @@ static oop _object___import_(oop _thunk,
char *p;
for (p= path; *p; ++p)
if ('/' == *p) *p= '\\';
- lib= dlopen(path, ID_RTLD_FLAGS);
- dprintf("7 dlopen %s -> %p\n", path, lib);
+ lib= _libid_dlopen(path, ID_DLOPEN_FLAGS);
+ dprintf("7 _libid_dlopen %s -> %p\n", path, lib);
}
if (!lib)
{
snprintf(path, MAXPATHLEN, ".\\%s.so", fileName);
- lib= dlopen(path, ID_RTLD_FLAGS);
- dprintf("6 dlopen %s -> %p\n", path, lib);
+ lib= _libid_dlopen(path, ID_DLOPEN_FLAGS);
+ dprintf("6 _libid_dlopen %s -> %p\n", path, lib);
}
# endif
if (!lib) fatal("import: %s.so: No such file or directory\n", fileName);
- init= dlsym(lib, "__id__init__");
- dprintf("7 dlsym %p __id__init__ -> %p\n", lib, init);
+ init= _libid_dlsym(lib, "__id__init__");
+ dprintf("7 _libid_dlsym %p __id__init__ -> %p\n", lib, init);
if (!init) fatal("%s: __id__init__: Undefined symbol\n", path);
}
dprintf("INIT %s %p %p\n", fileName, lib, init);
@@ -696,7 +685,7 @@ struct __lookup _libid_bind2(oop selecto
# if GLOBAL_MCACHE
probe= (((unsigned)vtable << 2) ^ ((unsigned)selector >> 3)) & (GLOBAL_MCACHE - 1);
entry= _libid_mcache[probe];
- if (entry->selector == selector && entry->vtable == vtable)
+ if (entry && entry->selector == selector && entry->vtable == vtable)
return (struct __lookup){ (struct __closure *)entry->closure, fragment };
# endif
assoc= recursionGuard++ ? _vtable__lookup_(0, vtable, vtable, selector) : _send(s_lookup_, vtable, selector);
@@ -1031,9 +1020,9 @@ struct __libid *_libid_init(int *argcp,
signal(SIGHUP, sighup);
# endif
- _libid.dlopen = dlopen;
- _libid.dlsym = dlsym;
- _libid.dlclose = dlclose;
+ _libid.dlopen = _libid_dlopen;
+ _libid.dlsym = _libid_dlsym;
+ _libid.dlclose = _libid_dlclose;
_libid._object = _object;
_libid.nil_vtable = _libid_nil_vtable;
@@ -1062,10 +1051,10 @@ struct __libid *_libid_init(int *argcp,
_libid.backtrace = _libid_backtrace;
_libid.methodAt = _libid_methodAt;
- _libid.gc_addRoots = _libid.dlsym(RTLD_DEFAULT, "GC_add_roots");
- _libid.gc_gcollect = _libid.dlsym(RTLD_DEFAULT, "GC_gcollect");
- _libid.gc_unregisterDisappearingLink = _libid.dlsym(RTLD_DEFAULT, "GC_unregister_disappearing_link");
- _libid.gc_generalRegisterDisappearingLink = _libid.dlsym(RTLD_DEFAULT, "GC_general_register_disappearing_link");
+ _libid.gc_addRoots = _libid.dlsym(ID_DLSYM_DEFAULT, "GC_add_roots");
+ _libid.gc_gcollect = _libid.dlsym(ID_DLSYM_DEFAULT, "GC_gcollect");
+ _libid.gc_unregisterDisappearingLink = _libid.dlsym(ID_DLSYM_DEFAULT, "GC_unregister_disappearing_link");
+ _libid.gc_generalRegisterDisappearingLink = _libid.dlsym(ID_DLSYM_DEFAULT, "GC_general_register_disappearing_link");
return &_libid;
}
@@ -1074,11 +1063,12 @@ struct __libid *_libid_init(int *argcp,
#if defined(WIN32)
# include <tlhelp32.h>
+# include <windows.h>
struct dll
{
char *name;
- dlhandle_t handle;
+ HINSTANCE handle;
struct dll *next;
};
@@ -1088,9 +1078,9 @@ static int dlinitialised= 0;
static void dlinit(void);
-dlhandle_t dlopen(const char *path, int mode)
-{
- dlhandle_t handle= 0;
+id_dl_t _libid_dlopen(const char *path, int mode)
+{
+ HINSTANCE handle= 0;
if (!dlinitialised) dlinit();
{
unsigned int errormode= SetErrorMode(SEM_FAILCRITICALERRORS);
@@ -1106,7 +1096,7 @@ dlhandle_t dlopen(const char *path, int
dll->next= dlls;
dlls= dll;
}
- return handle;
+ return (id_dl_t)handle;
}
static void dlinit(void)
@@ -1119,50 +1109,78 @@ static void dlinit(void)
me32.dwSize= sizeof(MODULEENTRY32);
if (Module32First(snapshot, &me32))
do
- dlopen(me32.szModule, RTLD_NOW | RTLD_GLOBAL);
+ _libid_dlopen(me32.szModule, ID_DLOPEN_NOW | ID_DLOPEN_GLOBAL);
while (Module32Next(snapshot, &me32));
CloseHandle(snapshot);
}
}
-void *dlsym(dlhandle_t handle, const char *symbol)
+void *_libid_dlsym(id_dl_t handle, const char *symbol)
{
void *addr= 0;
struct dll *dll;
if (!dlinitialised) dlinit();
if (handle)
{
- addr= GetProcAddress(handle, symbol);
- dprintf("dlsym %p \"%s\" -> %p\n", handle, symbol, addr);
+ addr= GetProcAddress((HINSTANCE)handle, symbol);
+ dprintf("_libid_dlsym %p \"%s\" -> %p\n", handle, symbol, addr);
return addr;
}
if ((addr= GetProcAddress(GetModuleHandle(0), symbol)))
{
- dprintf("dlsym <main> \"%s\" -> %p\n", symbol, addr);
+ dprintf("_libid_dlsym <main> \"%s\" -> %p\n", symbol, addr);
return addr;
}
for (dll= dlls; dll; dll= dll->next)
if ((addr= GetProcAddress(dll->handle, symbol)))
{
- dprintf("dlsym dll %p \"%s\" -> %p\n", dll->handle, symbol, addr);
+ dprintf("_libid_dlsym dll %p \"%s\" -> %p\n", dll->handle, symbol, addr);
return addr;
}
- dprintf("dlsym 0 \"%s\" -> FAIL\n", symbol);
+ dprintf("_libid_dlsym 0 \"%s\" -> FAIL\n", symbol);
return 0;
}
-int dlclose(dlhandle_t handle)
+int _libid_dlclose(id_dl_t handle)
{
struct dll *dll, **dllp;
for (dllp= &dlls; (dll= *dllp); dllp= &dll->next)
- if (dll->handle == handle)
+ if (dll->handle == (HINSTANCE)handle)
{
*dllp= dll->next;
free(dll);
break;
}
- FreeLibrary(handle);
+ FreeLibrary((HINSTANCE)handle);
return 0;
}
-#endif
+#else /* !defined(WIN32) */
+# include <dlfcn.h>
+
+id_dl_t _libid_dlopen(const char *path, int mode)
+{
+ int md = 0;
+ if ((mode & ID_DLOPEN_LAZY)) {
+ md |= RTLD_LAZY;
+ }
+ if ((mode & ID_DLOPEN_NOW)) {
+ md |= RTLD_NOW;
+ }
+ if ((mode & ID_DLOPEN_GLOBAL)) {
+ md |= RTLD_GLOBAL;
+ }
+ return (id_dl_t)dlopen(path, md);
+}
+
+void *_libid_dlsym(id_dl_t handle, const char *symbol)
+{
+ void *hnd = (handle == ID_DLSYM_DEFAULT) ? RTLD_DEFAULT : (void *)handle;
+ return dlsym(hnd, symbol);
+}
+
+int _libid_dlclose(id_dl_t handle)
+{
+ return dlclose((void *)handle);
+}
+#endif /* WIN32 */
diff -r 582a6512823c object/idc/CCodeGenerator.st
--- a/object/idc/CCodeGenerator.st Mon Dec 03 19:00:36 2007 -0600
+++ b/object/idc/CCodeGenerator.st Mon Dec 03 19:57:08 2007 -0600
@@ -78,10 +78,8 @@ CCodeGenerator headerTextType: outputTyp
#if defined(WIN32)
# include <malloc.h>
# include <windows.h>
- typedef HINSTANCE dlhandle_t;
#else
# include <dlfcn.h>
- typedef void *dlhandle_t;
#endif
#ifndef O_BINARY
# define O_BINARY 0
_______________________________________________
fonc mailing list
[email protected]
http://vpri.org/mailman/listinfo/fonc