public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Peng Yu <pengyu.ut@gmail.com>
To: libc-help <libc-help@sourceware.org>
Subject: About of implementation of ldd
Date: Tue, 20 Apr 2021 09:12:47 -0500	[thread overview]
Message-ID: <CABrM6w=-3a4354FD0Y-3Np9XnpxeSM7aSTgPC5viUzp9f+xuxA@mail.gmail.com> (raw)

Hi,

I added `set -x` to ldd to see what commands it calls. The result
shows many commands.

$ ./ldd /bin/echo
+ TEXTDOMAIN=libc
+ TEXTDOMAINDIR=/usr/share/locale
+ RTLDLIST='/lib/ld-linux.so.2 /lib64/ld-linux-x86-64.so.2
/libx32/ld-linux-x32.so.2'
+ warn=
+ bind_now=
+ verbose=
+ test 1 -gt 0
+ case "$1" in
+ break
+ add_env='LD_TRACE_LOADED_OBJECTS=1 LD_WARN= LD_BIND_NOW='
+ add_env='LD_TRACE_LOADED_OBJECTS=1 LD_WARN= LD_BIND_NOW=
LD_LIBRARY_VERSION=$verify_out'
+ add_env='LD_TRACE_LOADED_OBJECTS=1 LD_WARN= LD_BIND_NOW=
LD_LIBRARY_VERSION=$verify_out LD_VERBOSE='
+ test '' = yes
+ case $# in
+ single_file=t
+ result=0
+ for file in "$@"
+ test t = t
+ case $file in
+ :
+ test '!' -e /bin/echo
+ test '!' -f /bin/echo
+ test -r /bin/echo
+ RTLD=
+ ret=1
+ for rtld in ${RTLDLIST}
+ test -x /lib/ld-linux.so.2
++ /lib/ld-linux.so.2
+ dummy='Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]
You have invoked `ld.so'\'', the helper program for shared library executables.
This program usually lives in the file `/lib/ld.so'\'', and special directives
in executable files using ELF shared libraries tell the system'\''s program
loader to load the helper program from this file.  This helper program loads
the shared libraries needed by the program executable, prepares the program
to run, and runs it.  You may invoke this helper program directly from the
command line to load and run an ELF executable file; this is like executing
that file itself, but always uses this helper program from the file you
specified, instead of the helper program file specified in the executable
file you run.  This is mostly of use for maintainers to test new versions
of this helper program; chances are you did not intend to run this program.

  --list                list all dependencies and how they are resolved
  --verify              verify that given object really is a dynamically linked
            object we can handle
  --inhibit-cache       Do not use /etc/ld.so.cache
  --library-path PATH   use given PATH instead of content of the environment
            variable LD_LIBRARY_PATH
  --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names
            in LIST
  --audit LIST          use objects named in LIST as auditors
  --preload LIST        preload objects named in LIST'
+ test 127 = 127
++ /lib/ld-linux.so.2 --verify /bin/echo
+ verify_out=
+ ret=1
+ case $ret in
+ for rtld in ${RTLDLIST}
+ test -x /lib64/ld-linux-x86-64.so.2
++ /lib64/ld-linux-x86-64.so.2
+ dummy='Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]
You have invoked `ld.so'\'', the helper program for shared library executables.
This program usually lives in the file `/lib/ld.so'\'', and special directives
in executable files using ELF shared libraries tell the system'\''s program
loader to load the helper program from this file.  This helper program loads
the shared libraries needed by the program executable, prepares the program
to run, and runs it.  You may invoke this helper program directly from the
command line to load and run an ELF executable file; this is like executing
that file itself, but always uses this helper program from the file you
specified, instead of the helper program file specified in the executable
file you run.  This is mostly of use for maintainers to test new versions
of this helper program; chances are you did not intend to run this program.

  --list                list all dependencies and how they are resolved
  --verify              verify that given object really is a dynamically linked
            object we can handle
  --inhibit-cache       Do not use /etc/ld.so.cache
  --library-path PATH   use given PATH instead of content of the environment
            variable LD_LIBRARY_PATH
  --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names
            in LIST
  --audit LIST          use objects named in LIST as auditors
  --preload LIST        preload objects named in LIST'
+ test 127 = 127
++ /lib64/ld-linux-x86-64.so.2 --verify /bin/echo
+ verify_out=
+ ret=0
+ case $ret in
+ RTLD=/lib64/ld-linux-x86-64.so.2
+ break
+ case $ret in
+ try_trace /lib64/ld-linux-x86-64.so.2 /bin/echo
++ eval LD_TRACE_LOADED_OBJECTS=1 LD_WARN= LD_BIND_NOW=
'LD_LIBRARY_VERSION=$verify_out' LD_VERBOSE= '"$@"'
++ rc=0
++ printf x
++ exit 0
+ output='+++ LD_TRACE_LOADED_OBJECTS=1
+++ LD_WARN=
+++ LD_BIND_NOW=
+++ LD_LIBRARY_VERSION=
+++ LD_VERBOSE=
+++ /lib64/ld-linux-x86-64.so.2 /bin/echo
    linux-vdso.so.1 (0x00007ffd6f1b1000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9562705000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f95628f4000)
x'
+ rc=0
+ printf %s '+++ LD_TRACE_LOADED_OBJECTS=1
+++ LD_WARN=
+++ LD_BIND_NOW=
+++ LD_LIBRARY_VERSION=
+++ LD_VERBOSE=
+++ /lib64/ld-linux-x86-64.so.2 /bin/echo
    linux-vdso.so.1 (0x00007ffd6f1b1000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9562705000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f95628f4000)
'
+++ LD_TRACE_LOADED_OBJECTS=1
+++ LD_WARN=
+++ LD_BIND_NOW=
+++ LD_LIBRARY_VERSION=
+++ LD_VERBOSE=
+++ /lib64/ld-linux-x86-64.so.2 /bin/echo
    linux-vdso.so.1 (0x00007ffd6f1b1000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9562705000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f95628f4000)
+ return 0
+ exit 0

But the bottom line is just these two commands. It seems most of the
code is for resolving which interpreter should be used. But that info
should already be in an ELF file. Why not just read from the ELF file
directly to simplify the code?

$ readelf -p .interp /bin/echo

String dump of section '.interp':
  [     0]  /lib64/ld-linux-x86-64.so.2
$ LD_TRACE_LOADED_OBJECTS=1 /lib64/ld-linux-x86-64.so.2 /bin/echo
    linux-vdso.so.1 (0x00007ffe7d5fd000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3bfb981000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f3bfbb70000)

-- 
Regards,
Peng

                 reply	other threads:[~2021-04-20 14:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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='CABrM6w=-3a4354FD0Y-3Np9XnpxeSM7aSTgPC5viUzp9f+xuxA@mail.gmail.com' \
    --to=pengyu.ut@gmail.com \
    --cc=libc-help@sourceware.org \
    /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).