About of implementation of ldd

Peng Yu pengyu.ut@gmail.com
Tue Apr 20 14:12:47 GMT 2021


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


More information about the Libc-help mailing list