rust_libloading icon indicating copy to clipboard operation
rust_libloading copied to clipboard

Behaviour when importing function (with the same name) from two different shared libraries

Open mert-kurttutan opened this issue 11 months ago • 4 comments
trafficstars

What is the expected behaviour when we important a function with the same name from two different shared libraries.

In coding, this translates to:

    unsafe {
        let lib1 = libloading::Library::new("/path/to/liblibrary_one.so")?;
        let func1: libloading::Symbol<unsafe extern fn()> = lib.get(b"my_func").unwrap();

        let lib2 = libloading::Library::new("/path/to/liblibrary_two.so")?;
        let func2: libloading::Symbol<unsafe extern fn()> = lib.get(b"my_func").unwrap();

       func1();
       func2();

    }

In my debugging, when func1 is called before func2, function from library_two is called twice.

But, when func2 is called before func1, function from library_two is only called once, then function from library_one is called, so no double calling of function from 1st imported library. Is this behaviour expected ?

I aware this is info is a limited, not enough for repro but I can elaborate later on, giving actual info about the content if shared libraries.

mert-kurttutan avatar Nov 28 '24 04:11 mert-kurttutan

The behaviour doesn't sound expected, but at the same time this likely doesn't have anything to do with the implementation of libloading and more so with the specifics of your dynamic libraries and/or their interaction with the interpreter/loader that is used on your system.

A minimal reproducer would help here :)

nagisa avatar Nov 28 '24 22:11 nagisa

You are probably right. Tried have a reproducible example and it did not have the problem.

mert-kurttutan avatar Nov 29 '24 00:11 mert-kurttutan

Is this behaviour possible when the share library (that libloading loads) also loads other shared libraries using dlopen?

mert-kurttutan avatar Nov 29 '24 02:11 mert-kurttutan

It turns out they both had an inner functions (which is the main body) to which they make a call. This inner function has the same name on both shared libraries and exported.

mert-kurttutan avatar Nov 29 '24 05:11 mert-kurttutan

Yeah, this is unfortunately a platform behaviour that you will need to figure out how to work-around yourself.

nagisa avatar Mar 26 '25 17:03 nagisa