Uber-Dynamic Linkery

  • My Firefox extension contains a dynamic library that I build in Xcode.  It links to a function, NS_GetServiceManager(), which, until Firefox version 22, is defined in libxpcom.dylib, which is one of the dozen or so libraries that Mozilla ships in Firefox.  But starting in Firefox 22, the definition of NS_GetServiceManager() got moved to a different library, XUL.

    So, if I build *my* dylib linked to libxpcom.dylib, it works OK in Firefox 21 but fails to load in Firefox 22.  If I build it linked to XUL, it works OK in Firefox 22, but in Firefox 21, Firefox crashes when NS_GetServiceManager() is invoked.

    Is there any kind of linking trickery I could use to build a dylib which will work in both new and old Firefox versions?

    Thanks!

    Jerry Krinock
  • On 31 May 2013, at 06:25, Jerry Krinock wrote:

    > My Firefox extension contains a dynamic library that I build in Xcode.  It links to a function, NS_GetServiceManager(), which, until Firefox version 22, is defined in libxpcom.dylib, which is one of the dozen or so libraries that Mozilla ships in Firefox.  But starting in Firefox 22, the definition of NS_GetServiceManager() got moved to a different library, XUL.
    >
    > So, if I build *my* dylib linked to libxpcom.dylib, it works OK in Firefox 21 but fails to load in Firefox 22.  If I build it linked to XUL, it works OK in Firefox 22, but in Firefox 21, Firefox crashes when NS_GetServiceManager() is invoked.
    >
    > Is there any kind of linking trickery I could use to build a dylib which will work in both new and old Firefox versions?

    Look up the address of the function at run time (e.g. using dlopen/dlsym).

    Jonas
  • On 2013 May 31, at 06:10, Jonas Maebe <jonas.maebe...> wrote:

    > Look up the address of the function at run time (e.g. using dlopen/dlsym).

    Thank you, Jonas!

    I started using dlopen() and dlsym() per the Apple documentation [1], which discusses how to load plain C functions at runtime.  Not too bad.  But then I ran into a member function from a C++ class.  Oh, no!  I found this very good article [2] on how to dynamically load C++, but it looks as though the Mozilla declarations are not extern "C", so it's not going to work.  And to make matters worse, a template seems to be involved.  I hate C++.

    If anyone has any other ideas, let us know.  Should I try harder with the C++?  I'm even considering whether I should download the Mozilla source code (Oh no!!!) and try to build my own little static library which defines the handful of functions and classes that I need.

    Jerry

    [1] https://developer.apple.com/library/mac/#documentation/DeveloperTools/Conce
    ptual/DynamicLibraries/100-Articles/UsingDynamicLibraries.html#//apple_ref/
    doc/uid/TP40002182-SW10


    [2] http://www.isotton.com/devel/docs/C++-dlopen-mini-HOWTO/C++-dlopen-mini-HOW
    TO.html
  • On Fri, May 31, 2013 at 10:42 PM, Jerry Krinock <jerry...> wrote:

    >
    > On 2013 May 31, at 06:10, Jonas Maebe <jonas.maebe...> wrote:
    >
    >> Look up the address of the function at run time (e.g. using
    > dlopen/dlsym).
    >
    > Thank you, Jonas!
    >
    > I started using dlopen() and dlsym() per the Apple documentation [1],
    > which discusses how to load plain C functions at runtime.  Not too bad.
    > But then I ran into a member function from a C++ class.  Oh, no!  I found
    > this very good article [2] on how to dynamically load C++, but it looks as
    > though the Mozilla declarations are not extern "C", so it's not going to
    > work.  And to make matters worse, a template seems to be involved.  I hate
    > C++.
    >
    > If anyone has any other ideas, let us know.  Should I try harder with the
    > C++?  I'm even considering whether I should download the Mozilla source
    > code (Oh no!!!) and try to build my own little static library which defines
    > the handful of functions and classes that I need.
    >

    You can use dlopen()/dlsym() to load C++ symbols, but it's harder.  You'll
    need to figure out the mangled names for the functions to pass to dlsym(),
    and you may need to play around with calling conventions to get things
    working.

    I'd just create a pair of "shim" libraries that simply wrap 'extern "C"'
    around the functions you need, one for old Firefox and one for new, and use
    dlopen()/dlsym() to load the shim you want at runtime.

    --
    Mark Wagner
previous month may 2013 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Go to today