Shared object question - Linux

This is a discussion on Shared object question - Linux ; Hi all, i have a question about using shared object loaded with dlopen, dlsym .... in Linux. I have a simple test program with a global static variable : static char name[MAX_NAME]; First operation test program make is initialize "name" ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Shared object question

  1. Shared object question

    Hi all,
    i have a question about using shared object loaded with dlopen, dlsym
    .... in Linux.

    I have a simple test program with a global static variable :

    static char name[MAX_NAME];

    First operation test program make is initialize "name" whit a string.
    There is also a function "char *get_name()" returning a reference to name.

    I made a simple shared object with only function "print", this
    fuction use "get_name()" of main program to print the string contained
    in "name".

    The "print" function is dinamycally invoked loading with dlopen the
    shared object and resolving function address with dlsym.
    The problem is "print" function print always (null).

    I was convinced that a "so" could have access to the memory of the
    parent process but now I think I have some misundertand about shared
    objects. I've also tried to put name in heap and make it not static but
    without success.

    Can someone help me (also addressing some good online tutorial)? Need to
    use shared memory?

    many thanks
    Fabrizio

  2. Re: Shared object question

    fg wrote:
    > i have a question about using shared object loaded with dlopen, dlsym
    > ... in Linux.


    > I have a simple test program with a global static variable :


    > static char name[MAX_NAME];


    > First operation test program make is initialize "name" whit a string.
    > There is also a function "char *get_name()" returning a reference to name.


    I guess you mean a pointer to the the array 'name'.

    > I made a simple shared object


    I guess you mean a shared library.

    > with only function "print", this
    > fuction use "get_name()" of main program to print the string contained
    > in "name".


    > The "print" function is dinamycally invoked loading with dlopen the
    > shared object and resolving function address with dlsym.
    > The problem is "print" function print always (null).


    > I was convinced that a "so" could have access to the memory of the
    > parent process but now I think I have some misundertand about shared
    > objects. I've also tried to put name in heap and make it not static but
    > without success.


    Sorry, but you also will have to show the actual code you're using
    (and probably also tell how exactly you create the program and the
    shared library). If your get_name() function really returns a poin-
    ter to the string than the function from the shared library should
    have no problem printing the string.

    Here's a short program that demonstrates how you can get it to
    work (please replace the path to the shared library from the
    '/home/jens/TESTS' to the correct path for your system!):

    --- name.c ------------------------------------------

    #include
    #include

    static char name[ ] = "foobar";

    int main( void ) {
    void *handle;
    void ( *print )( void );
    const char *err;

    handle = dlopen( "/home/jens/TESTS/print.so", RTLD_NOW );
    if ( handle ) {
    dlerror( );
    print = dlsym( handle, "print" );
    if ( ( err = dlerror( ) ) == NULL )
    print();
    else
    fprintf( stderr, "Failed to get function print(): %s\n", err );
    dlclose( handle );
    }
    else
    fprintf( stderr, "Failed to dlopen() print.so: %s\n", dlerror( ) );
    return 0;
    }

    char *get_name( void ) {
    return name;
    }

    --- print.c ------------------------------------------
    #include

    extern char *get_name( void );

    void print( void ) {
    printf( "%s\n", get_name( ) );
    }

    Now compile it with

    gcc -W -Wall -o name name.c -ldl -Wl,-E
    gcc -W -Wall -shared -fPIC -o print.so print.c

    and if you invoke the resulting program 'name' it should print
    out the string (at least it does for me;-)

    You can replace the '-Wl,-E' option to gcc by '-rdynamic' or
    '-Wl,-export-dynamic'. And on some systems you can leave out
    the '-fPIC' (or '-fpic') option.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  3. Re: Shared object question

    Jens Thoms Toerring ha scritto:
    > fg wrote:
    >> i have a question about using shared object loaded with dlopen, dlsym
    >> ... in Linux.

    >
    >> I have a simple test program with a global static variable :

    >
    >> static char name[MAX_NAME];

    >
    >> First operation test program make is initialize "name" whit a string.
    >> There is also a function "char *get_name()" returning a reference to name.

    >
    > I guess you mean a pointer to the the array 'name'.
    >
    >> I made a simple shared object

    >
    > I guess you mean a shared library.
    >
    >> with only function "print", this
    >> fuction use "get_name()" of main program to print the string contained
    >> in "name".

    >
    >> The "print" function is dinamycally invoked loading with dlopen the
    >> shared object and resolving function address with dlsym.
    >> The problem is "print" function print always (null).

    >
    >> I was convinced that a "so" could have access to the memory of the
    >> parent process but now I think I have some misundertand about shared
    >> objects. I've also tried to put name in heap and make it not static but
    >> without success.

    >
    > Sorry, but you also will have to show the actual code you're using
    > (and probably also tell how exactly you create the program and the
    > shared library). If your get_name() function really returns a poin-
    > ter to the string than the function from the shared library should
    > have no problem printing the string.
    >
    > Here's a short program that demonstrates how you can get it to
    > work (please replace the path to the shared library from the
    > '/home/jens/TESTS' to the correct path for your system!):
    >
    > --- name.c ------------------------------------------
    >
    > #include
    > #include
    >
    > static char name[ ] = "foobar";
    >
    > int main( void ) {
    > void *handle;
    > void ( *print )( void );
    > const char *err;
    >
    > handle = dlopen( "/home/jens/TESTS/print.so", RTLD_NOW );
    > if ( handle ) {
    > dlerror( );
    > print = dlsym( handle, "print" );
    > if ( ( err = dlerror( ) ) == NULL )
    > print();
    > else
    > fprintf( stderr, "Failed to get function print(): %s\n", err );
    > dlclose( handle );
    > }
    > else
    > fprintf( stderr, "Failed to dlopen() print.so: %s\n", dlerror( ) );
    > return 0;
    > }
    >
    > char *get_name( void ) {
    > return name;
    > }
    >
    > --- print.c ------------------------------------------
    > #include
    >
    > extern char *get_name( void );
    >
    > void print( void ) {
    > printf( "%s\n", get_name( ) );
    > }
    >
    > Now compile it with
    >
    > gcc -W -Wall -o name name.c -ldl -Wl,-E
    > gcc -W -Wall -shared -fPIC -o print.so print.c
    >
    > and if you invoke the resulting program 'name' it should print
    > out the string (at least it does for me;-)
    >
    > You can replace the '-Wl,-E' option to gcc by '-rdynamic' or
    > '-Wl,-export-dynamic'. And on some systems you can leave out
    > the '-fPIC' (or '-fpic') option.
    >
    > Regards, Jens



    I've discovered my problem was don't put the -rdynamic flag in
    compilation of main program.
    Now all works ...

    many thanks
    Fabrizio

  4. Re: Shared object question

    fg wrote:
    >> The "print" function is dinamycally invoked loading with dlopen the
    >> shared object and resolving function address with dlsym.
    >> The problem is "print" function print always (null).

    >
    > I've discovered my problem was don't put the -rdynamic flag in
    > compilation of main program.


    That sounds strange - without the -rdynamic flag you shouldn't
    have been able to dlopen() the dynamic library (at least if you
    use RTLD_NOW) or should have gotten a run-time error from the
    dynamic linker (if you used RTLD_LAZY in dlopen()), but not have
    the program output "(null)". I am curious how you managed to get
    that...
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  5. Re: Shared object question

    Jens Thoms Toerring ha scritto:
    > fg wrote:
    >>> The "print" function is dinamycally invoked loading with dlopen the
    >>> shared object and resolving function address with dlsym.
    >>> The problem is "print" function print always (null).

    >> I've discovered my problem was don't put the -rdynamic flag in
    >> compilation of main program.

    >
    > That sounds strange - without the -rdynamic flag you shouldn't
    > have been able to dlopen() the dynamic library (at least if you
    > use RTLD_NOW) or should have gotten a run-time error from the
    > dynamic linker (if you used RTLD_LAZY in dlopen()), but not have
    > the program output "(null)". I am curious how you managed to get
    > that...
    > Regards, Jens


    I was using RTLD_LAZY, I could open and also invoke the function (with
    the (null) result).
    Code is very near te one You posted, the only difference is get_name()
    function isn't declared as extern in .so source but declared as
    prototype in an header file used by .so and main program.

    Fabrizio

  6. Re: Shared object question

    fg wrote:
    > I was using RTLD_LAZY, I could open and also invoke the function (with
    > the (null) result).


    Mmmmm, when I do that I get a run-time error

    ../name: symbol lookup error: /home/jens/TESTS/print.so: undefined symbol: get_name

    which looks much more reasonable to me than having "(null)" printed
    out (which is what you normally get when you pass a NULL pointer to
    printf() where printf() expects a pointer to a string)...

    > Code is very near te one You posted, the only difference is get_name()
    > function isn't declared as extern in .so source but declared as
    > prototype in an header file used by .so and main program.


    That's probably better (I simply didn't want to post an extra
    header file) but it shouldn't make a difference...

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

+ Reply to Thread