public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Fengkai Sun via Libc-help <libc-help@sourceware.org>
Cc: Fengkai Sun <qcloud1014@gmail.com>
Subject: Re: Why does dynamic linker search global scope before local scope?
Date: Sun, 27 Jun 2021 16:44:26 +0200	[thread overview]
Message-ID: <87lf6v4bat.fsf@oldenburg.str.redhat.com> (raw)
In-Reply-To: <CAF6YOcNG2FyGpLrCeGt6cE2jpTeoekKw_ns5mgtSP-N0bAYZ_A@mail.gmail.com> (Fengkai Sun via Libc-help's message of "Sun, 27 Jun 2021 22:29:40 +0800")

* Fengkai Sun via Libc-help:

> After reading some LD_DEBUG messages, I've found that the dynamic linker
> searches the global scope before the local scope, and the flag
> RTLD_DEEPBIND can change this behavior.
>
> I'm really curious about the reason why the dynamic linker won't search the
> local scope first by default. To me, the static linker finds out the
> dependencies of a DSO, and there is a very high probability that they are
> defined in one of the DT_NEEDED entries. One can use the symbols in the
> global scope to do interposition, but that is quite rare, so always
> searching in the global scope seems tedious.

Interposing symbols from the main program is fairly common.  malloc is
the most obvious example, but this also happens with certain plugin
frameworks where the framework is available as both an executable (with
the framework statically linked) and a shared object.  In the latter
case, some shared objects may link against the framework dynamically,
but if running under the executable, the definitions in the executable
must be used.  Such an approach used to be quite common before the
effects of cheap PC-relative addressing, LTO and lack of semantic
interposition of locally-defined symbols were more widely understood.

> Currently, LD_PRELOAD is in the global scope, after the main executable.
> Though the use of RTLD_DEEPBIND may invalidate the preload semantics, it's
> just a design choice. The dynamic linker can search local scope first, and
> add LD_PRELOAD somewhere in the local scope.

Interposition does not necessarily involve LD_PRELOAD.

Thanks,
Florian


      reply	other threads:[~2021-06-27 14:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-27 14:29 Fengkai Sun
2021-06-27 14:44 ` Florian Weimer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87lf6v4bat.fsf@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=libc-help@sourceware.org \
    --cc=qcloud1014@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).