Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading multiple independent instances the same library? #146

Closed
alshdavid opened this issue Apr 7, 2024 · 1 comment
Closed

Loading multiple independent instances the same library? #146

alshdavid opened this issue Apr 7, 2024 · 1 comment
Labels

Comments

@alshdavid
Copy link

alshdavid commented Apr 7, 2024

I am trying to load the same library multiple times for the purposes of a plugin.

unsafe { libloading::Library::new(PathBuf.from("/path/to/lib.so")) }
unsafe { libloading::Library::new(PathBuf.from("/path/to/lib.so")) }

This seems to share state between the loaded libraries which is undesirable (the plugin uses global statics which causes conflicts when multiple instances are loaded).

I have tried to use:

let lib = unsafe {
  let lib = libloading::os::unix::Library::open(
    Some(PathBuf.from("/path/to/lib.so")),
    libloading::os::unix::RTLD_LOCAL
  ).unwrap();
  libloading::Library::from(lib)
};

However this give me the following error:

called `Result::unwrap()` on an `Err` value: 
  DlOpen { desc: "/path/to/lib.so: invalid mode for dlopen(): Invalid argument" }

Testing it out, if I copy the library multiple times (lib_copy1.so, lob_copy2.so, etc) - I can load that using Library::new() - I am just unsure how to get libloading to treat each instantiation as unique (I also need support for Windows).

@nagisa
Copy link
Owner

nagisa commented Apr 13, 2024

The only portable way to do this to the best of my knowledge is to have copies of the file with multiple names. If you're limited to GNU Linux, you may be able to achieve this with something like dlmopen that's been mentioned in this issue tracker in the past: #36. I wouldn't be surprised if Windows also had a system API to do something similar.

But either way you'll need to plumb all this manually. libloading is going to be fine with it so long as your underlying system-provided loader is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants