Re: dlsym(RTLD_NEXT) and weak symbols - FreeBSD

This is a discussion on Re: dlsym(RTLD_NEXT) and weak symbols - FreeBSD ; Maxim Sobolev wrote: > Hi, > > I am not sure if it has even worked correctly, but calling > dlsym("dlopen", RTLD_NEXT) returns reference to the dlopen() function in > the libc, not reference to dlopen() function in the ld-elf.so. ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Re: dlsym(RTLD_NEXT) and weak symbols

  1. Re: dlsym(RTLD_NEXT) and weak symbols

    Maxim Sobolev wrote:
    > Hi,
    >
    > I am not sure if it has even worked correctly, but calling
    > dlsym("dlopen", RTLD_NEXT) returns reference to the dlopen() function in
    > the libc, not reference to dlopen() function in the ld-elf.so. The
    > attempt to call this function then fails, since dlopen() in libc is just
    > a stub to make static linking happy.
    >
    > #pragma weak dlopen
    > void *
    > dlopen(const char *name, int mode)
    > {
    > _rtld_error(sorry);
    > return NULL;
    > }
    >
    > IMHO this is incorrect and is probably part of the bigger problem. The
    > dlsym(3) should return first non-weak symbol instead.


    The following patch fixes the issue for me:

    --- rtld.c 2008-08-18 13:24:19.000000000 -0700
    +++ rtld.c 2008-10-08 15:28:47.000000000 -0700
    @@ -1871,10 +1871,25 @@
    if (handle == RTLD_NEXT)
    obj = obj->next;
    for (; obj != NULL; obj = obj->next) {
    - if ((def = symlook_obj(name, hash, obj, true)) != NULL) {
    + if ((def = symlook_obj(name, hash, obj, true)) != NULL &&
    + ELF_ST_BIND(def->st_info) != STB_WEAK) {
    defobj = obj;
    break;
    }
    + def = NULL;
    + }
    + /*
    + * Search the dynamic linker itself, and possibly resolve the
    + * symbol from there. Only the values listed in the "exports"
    + * array can be resolved from the dynamic linker.
    + */
    + if (def == NULL) {
    + def = symlook_obj(name, hash, &obj_rtld, true);
    + if (def != NULL && is_exported(def)) {
    + defobj = &obj_rtld;
    + } else {
    + def = NULL;
    + }
    }
    } else {
    assert(handle == RTLD_DEFAULT);

    -Maxim
    _______________________________________________
    freebsd-current@freebsd.org mailing list
    http://lists.freebsd.org/mailman/lis...reebsd-current
    To unsubscribe, send any mail to "freebsd-current-unsubscribe@freebsd.org"


  2. Re: dlsym(RTLD_NEXT) and weak symbols

    On Wed, 08 Oct 2008 17:59:01 -0700
    Maxim Sobolev wrote:

    > Alexander Kabaev wrote:
    > > On Wed, 08 Oct 2008 15:33:41 -0700
    > > Maxim Sobolev wrote:
    > >> The following patch fixes the issue for me:
    > >>

    > >
    > >
    > > I do not think your patch is completely correct. How about this one
    > > instead:

    >
    > I see where you go, but I disagree. IMHO symbols in the ld-elf.so.1
    > should be searched even if there is no matching weak symbol found in
    > other libraries. Technically, nothing says that the libc should have
    > those weak symbols for dlopen and friends and also application can
    > only be linked to ld-elf.so.1, not libc at all, and still
    > dlsym("dlopen") should work.
    >
    > -Maxim


    Read the patch again please. You do not see where I am going

    We need to grab first non-weak symbol that matches or first weak symbol
    if no non-weak alternatives are found.


    --
    Alexander Kabaev

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (FreeBSD)

    iD8DBQFI7VntQ6z1jMm+XZYRArTeAJ91/SxJzyowkKNxU6xLE9a8b7BEqQCfQdg+
    TSMqGWj/9RkRIfnh9qJWruw=
    =NcP4
    -----END PGP SIGNATURE-----


+ Reply to Thread