From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) by sourceware.org (Postfix) with ESMTPS id 73A44385042D for ; Tue, 20 Apr 2021 14:12:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 73A44385042D Received: by mail-il1-x135.google.com with SMTP id i22so27448405ila.11 for ; Tue, 20 Apr 2021 07:12:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=jPJvQsn8Yu5zy0oEUD5ldtbmX0xxxP6gd4NnytqYg0g=; b=LtFEyh9rXKKwkE81Xbv1HC+kCS0W7u5g+4XqpCs9j4O8btk7hRjb+Izob8gnb8Hmrq bSLab+apu75G9BmzHBeivib8EHNveFSupgB/gaukD/TUdD+qVBil/l5+FsLnM0TJK6Di Zr/d0+GJz6+ypEZnBbegwd5Ykk4sfwyzyFYG00idnPq2OdmHxx01ZOkvioOOE6RxHtK7 Q71h8bYf2Bd5+qvCA990AncmIDEmRwBTr1D+Xo+kJf2aGCjloQahUNs5RIumOlmDYMQy e1xjMd/qt2NgjMTh4fcmcOc5zaKW3WwxXlJLQpm8yLcQJvsCguIiC2N8yKKQjrsCMYgE Cisw== X-Gm-Message-State: AOAM53002FKlPLqGAMChiWGpCacWxRe9BoMI1t+eJFdBc8QvJvCYGIx0 uh51TnqTN9XoQRYnelBMTooIRArlbLgXQFZdJRdkjz4H1Vc= X-Google-Smtp-Source: ABdhPJyOs5q5jG0r8tvvHY0drGKCnRqc1bv6+cktFOIgiYzJsMBG7bn/OoTHL/thuW6P/LjorkBXE+RrmZkj8mj6sPs= X-Received: by 2002:a05:6e02:1c49:: with SMTP id d9mr20876820ilg.95.1618927978465; Tue, 20 Apr 2021 07:12:58 -0700 (PDT) MIME-Version: 1.0 From: Peng Yu Date: Tue, 20 Apr 2021 09:12:47 -0500 Message-ID: Subject: About of implementation of ldd To: libc-help Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-help@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-help mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Apr 2021 14:13:10 -0000 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