Closed Bug 518130 Opened 11 years ago Closed 10 years ago

[jsctypes] provide platform-independent way to open a library

Categories

(Core :: js-ctypes, defect)

x86
Linux
defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: dwitte, Unassigned)

References

Details

(Keywords: dev-doc-complete, Whiteboard: fixed by bug 577589)

Currently, you need to specify the full library name to ctypes.open(), which means you need to pass libfoo.so on linux, libfoo.dylib on mac, and foo.dll on windows. We should abstract this logic into open() itself, perhaps with a flag to enable this behavior (ctypes.SEARCH), to make things easier in the common case.

pyctypes has something like this, which also hunts around your search path for the library in an appropriate way on each platform:
http://docs.python.org/library/ctypes.html#finding-shared-libraries
Product: Other Applications → Core
https://developer.mozilla.org/en/JavaScript_code_modules/ctypes.jsm#Example.3a_Calling_Windows_routines

That blurb seems to indicate you can already do open("user32") and get the correct library.

dolske: Did you find that open("libnss3.dylib") didn't seem to work either, so you had to pass in the full path? Might be a separate bug from this one though.
Blocks: 513798
Keywords: dev-doc-needed
Hmm. Maybe just define the system-specific extension in ctypes (eg ctypes.LIBEXT == ".dll" on Win32, == ".so" on Unix)? Thus, ctypes.open("mylib" + ctypes.LIBEXT);

I'm not sure hiding the extension entirely makes sense... You wouldn't want open("user32") to try opening "user32.so" on linux, and there are cases where you might want to open a specific library version, eg "mylib.so.2". Doesn't seem like there's much gain from hiding the extension.

I would like to see the ability to use some of the non-filename flags from dlopen/dlsym, though... eg RTLD_NOLOAD with dlopen, RTLD_DEFAULT with dlsym. My usecase with WeaveCrypto is that I want to load NSS, but only the image already loaded into the process. Determining which that is can be tricky, specifically on Linux.

Thus, I'd either want something like:

  for each(var file in ["path1/libnss.so", "path2/libnss.so", ...]) {
    var lib = ctypes.open(file, RTLD_NOLOAD);
    if (lib) break;
  }

or

  var lib = ctypes.open("RTLD_DEFAULT");
  var myfunc = lib.declare("myfunc", ...);


There might be some things to consider with wanting to load a symbol Foo from library Bar, where the actual library/symbol is being influenced by LD_PRELOAD, LD_LIBARY_PATH, or the way the app was linked with -R... And maybe that's the one you want, and maybe it isn't. :-o
Why not use PR_GetLibraryName ?
I fixed bug 577589 (because I didn't look hard enough for this bug), which lets you do:
var name = ctypes.libraryName("foo");
and get back foo.dll / libfoo.so / libfoo.dylib depending on platform.

Is that enough for this bug?
Status: NEW → RESOLVED
Closed: 10 years ago
Depends on: 577589
Resolution: --- → FIXED
Whiteboard: fixed by bug 577589
Yup! :)
You need to log in before you can comment on or make changes to this bug.