dlsym() does not return local symbol address

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

dlsym() does not return local symbol address

Martin Gadbois
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all,

According to my interpretation, the program:

#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <dlfcn.h>

int local_sym(void)
{
        printf("Hello!\n");
        return 0;
}

int main()
{
        int (*sym)(void);
        void *h;

        h = dlopen(NULL,RTLD_NOW|RTLD_GLOBAL);
        if(h != NULL)
        {
                sym = dlsym(h,"local_sym");
                if(sym == NULL)
                        printf("DL error: %s\n",dlerror());
                else
                {
                        printf("Got it!\n");
                        sym();
                }
                dlclose(h);
        }
        return 0;
}

should print:
Got it!
Hello!

but it prints:
DL error: ./t: undefined symbol: local_sym

I use glibc-2.3.3, Gentoo ebuild sys-libs/glibc-2.3.3.20040420, Linux
version 2.6.13-gentoo-r5.

The same program works well if I lookup "printf", for example.

Is this a bug? Or is it my interpretation of "man dlsym"?

Thanks!

- --
==============         +----------------------------------------------+
Martin Gadbois         | "Windows might take you from 0 to 60 faster, |
S/W Developer          |  but to go to 100 you need Unix."            |
Colubris Networks Inc. +----------------------------------------------+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFZhtO9Y3/iTTCEDkRAhqOAJ4+39qSyHhjx/kct1ZgU+ldGmzz+gCgybaz
n9VxA6j1pVecMZVHqbDaZnI=
=yJaQ
-----END PGP SIGNATURE-----
Reply | Threaded
Open this post in threaded view
|

Re: dlsym() does not return local symbol address

Daniel Jacobowitz-2
On Thu, Nov 23, 2006 at 05:06:06PM -0500, Martin Gadbois wrote:
> Is this a bug? Or is it my interpretation of "man dlsym"?

It's not a bug.  It only can identify dynamic symbols, and by default
application symbols are not dynamic.  Look at the linker -rdynamic
documentation for more.

--
Daniel Jacobowitz
CodeSourcery
Reply | Threaded
Open this post in threaded view
|

Re: dlsym() does not return local symbol address

Martin Gadbois
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Daniel Jacobowitz wrote:
> On Thu, Nov 23, 2006 at 05:06:06PM -0500, Martin Gadbois wrote:
>> Is this a bug? Or is it my interpretation of "man dlsym"?
>
> It's not a bug.  It only can identify dynamic symbols, and by default
> application symbols are not dynamic.  Look at the linker -rdynamic
> documentation for more.
>

Compiling/linking with -rdynamic make it work! Many thanks!

- --
==============         +----------------------------------------------+
Martin Gadbois         | "Windows might take you from 0 to 60 faster, |
S/W Developer          |  but to go to 100 you need Unix."            |
Colubris Networks Inc. +----------------------------------------------+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFZzS79Y3/iTTCEDkRAo7zAJ4jGwl4i3ypMjKJ9lyAI5TAXl2EYACfX8Nn
kGn1dOIICdT3SYI4V51MPzE=
=Ahkp
-----END PGP SIGNATURE-----