[20211013]閱讀ldd原始碼跟蹤.txt

lfree發表於2021-10-13

[20211013]閱讀ldd原始碼跟蹤.txt

--//前幾天才發現ldd是bash shell指令碼,我看了一下原始碼,感覺最後的執行有點怪,做一個記錄.

$ file $(which ldd)
/usr/bin/ldd: Bourne shell script text executable

$ cp /u01/app/oracle/product/11.2.0.4/dbhome_1/bin/lsnrctl .

$ bash -x ldd lsnrctl
Program=bash LineNo=8 :|bash -x ldd lsnrctl
Program=ldd LineNo=27 /usr/bin/ldd:|TEXTDOMAIN=libc
Program=ldd LineNo=28 /usr/bin/ldd:|TEXTDOMAINDIR=/usr/share/locale
Program=ldd LineNo=30 /usr/bin/ldd:|RTLDLIST='/lib/ld-linux.so.2 /lib64/ld-linux-x86-64.so.2'
Program=ldd LineNo=31 /usr/bin/ldd:|warn=
Program=ldd LineNo=32 /usr/bin/ldd:|bind_now=
Program=ldd LineNo=33 /usr/bin/ldd:|verbose=
Program=ldd LineNo=35 /usr/bin/ldd:|test 1 -gt 0
Program=ldd LineNo=36 /usr/bin/ldd:|case "$1" in
Program=ldd LineNo=92 /usr/bin/ldd:|break
Program=ldd LineNo=103 /usr/bin/ldd:|add_env='LD_TRACE_LOADED_OBJECTS=1 LD_WARN= LD_BIND_NOW='
Program=ldd LineNo=104 /usr/bin/ldd:|add_env='LD_TRACE_LOADED_OBJECTS=1 LD_WARN= LD_BIND_NOW= LD_LIBRARY_VERSION=$verify_out'
Program=ldd LineNo=105 /usr/bin/ldd:|add_env='LD_TRACE_LOADED_OBJECTS=1 LD_WARN= LD_BIND_NOW= LD_LIBRARY_VERSION=$verify_out LD_VERBOSE='
Program=ldd LineNo=106 /usr/bin/ldd:|test '' = yes
Program=ldd LineNo=114 /usr/bin/ldd:|set -o pipefail
Program=ldd LineNo=124 /usr/bin/ldd:|case $# in
Program=ldd LineNo=131 /usr/bin/ldd:|single_file=t
Program=ldd LineNo=138 /usr/bin/ldd:|result=0
Program=ldd LineNo=139 /usr/bin/ldd:|for file in '"$@"'
Program=ldd LineNo=141 /usr/bin/ldd:|test t = t
Program=ldd LineNo=142 /usr/bin/ldd:|case $file in
Program=ldd LineNo=145 /usr/bin/ldd:|file=./lsnrctl
Program=ldd LineNo=148 /usr/bin/ldd:|test '!' -e ./lsnrctl
Program=ldd LineNo=151 /usr/bin/ldd:|test '!' -f ./lsnrctl
Program=ldd LineNo=154 /usr/bin/ldd:|test -r ./lsnrctl
Program=ldd LineNo=155 /usr/bin/ldd:|test -x ./lsnrctl
Program=ldd LineNo=157 /usr/bin/ldd:|RTLD=
Program=ldd LineNo=158 /usr/bin/ldd:|ret=1
Program=ldd LineNo=159 /usr/bin/ldd:|for rtld in '${RTLDLIST}'
Program=ldd LineNo=160 /usr/bin/ldd:|test -x /lib/ld-linux.so.2
PProgram=ldd LineNo=161 /usr/bin/ldd:|/lib/ld-linux.so.2 --verify ./lsnrctl
Program=ldd LineNo=161 /usr/bin/ldd:|verify_out=
Program=ldd LineNo=162 /usr/bin/ldd:|ret=1
Program=ldd LineNo=163 /usr/bin/ldd:|case $ret in
Program=ldd LineNo=159 /usr/bin/ldd:|for rtld in '${RTLDLIST}'
Program=ldd LineNo=160 /usr/bin/ldd:|test -x /lib64/ld-linux-x86-64.so.2
PProgram=ldd LineNo=161 /usr/bin/ldd:|/lib64/ld-linux-x86-64.so.2 --verify ./lsnrctl
Program=ldd LineNo=161 /usr/bin/ldd:|verify_out=
Program=ldd LineNo=162 /usr/bin/ldd:|ret=0
Program=ldd LineNo=163 /usr/bin/ldd:|case $ret in
Program=ldd LineNo=164 /usr/bin/ldd:|RTLD=/lib64/ld-linux-x86-64.so.2
Program=ldd LineNo=164 /usr/bin/ldd:|break
Program=ldd LineNo=168 /usr/bin/ldd:|case $ret in
Program=ldd LineNo=177 /usr/bin/ldd:|try_trace /lib64/ld-linux-x86-64.so.2 ./lsnrctl
Program=ldd LineNo=116 /usr/bin/ldd:try_trace|eval LD_TRACE_LOADED_OBJECTS=1 LD_WARN= LD_BIND_NOW= 'LD_LIBRARY_VERSION=$verify_out' LD_VERBOSE= '"$@"'
Program=ldd LineNo=116 /usr/bin/ldd:try_trace|cat
PProgram=ldd LineNo=116 /usr/bin/ldd:try_trace|LD_TRACE_LOADED_OBJECTS=1
PProgram=ldd LineNo=116 /usr/bin/ldd:try_trace|LD_WARN=
PProgram=ldd LineNo=116 /usr/bin/ldd:try_trace|LD_BIND_NOW=
PProgram=ldd LineNo=116 /usr/bin/ldd:try_trace|LD_LIBRARY_VERSION=
PProgram=ldd LineNo=116 /usr/bin/ldd:try_trace|LD_VERBOSE=
PProgram=ldd LineNo=116 /usr/bin/ldd:try_trace|/lib64/ld-linux-x86-64.so.2 ./lsnrctl
        linux-vdso.so.1 =>  (0x00007ffffd5e0000)
        libclntsh.so.11.1 => /u01/app/oracle/product/11.2.0.4/dbhome_1/lib/libclntsh.so.11.1 (0x00007fe81c758000)
        libnnz11.so => /u01/app/oracle/product/11.2.0.4/dbhome_1/lib/libnnz11.so (0x00007fe81c38a000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003799800000)
        libm.so.6 => /lib64/libm.so.6 (0x00000032cac00000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x000000379a000000)
        libnsl.so.1 => /lib64/libnsl.so.1 (0x000000379c400000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003799000000)
        libaio.so.1 => /usr/lib64/libaio.so.1 (0x00007fe81c187000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003798c00000)
Program=ldd LineNo=190 /usr/bin/ldd:|exit 0


--//注意看最後幾行
Program=ldd LineNo=116 /usr/bin/ldd:try_trace|eval LD_TRACE_LOADED_OBJECTS=1 LD_WARN= LD_BIND_NOW= 'LD_LIBRARY_VERSION=$verify_out' LD_VERBOSE= '"$@"'
Program=ldd LineNo=116 /usr/bin/ldd:try_trace|cat
PProgram=ldd LineNo=116 /usr/bin/ldd:try_trace|LD_TRACE_LOADED_OBJECTS=1
PProgram=ldd LineNo=116 /usr/bin/ldd:try_trace|LD_WARN=
PProgram=ldd LineNo=116 /usr/bin/ldd:try_trace|LD_BIND_NOW=
PProgram=ldd LineNo=116 /usr/bin/ldd:try_trace|LD_LIBRARY_VERSION=
PProgram=ldd LineNo=116 /usr/bin/ldd:try_trace|LD_VERBOSE=
PProgram=ldd LineNo=116 /usr/bin/ldd:try_trace|/lib64/ld-linux-x86-64.so.2 ./lsnrctl

--//感覺執行的是內容是:
eval LD_TRACE_LOADED_OBJECTS=1 LD_WARN= LD_BIND_NOW= LD_LIBRARY_VERSION= LD_VERBOSE= /lib64/ld-linux-x86-64.so.2 ./lsnrctl

--//如果仔細看可以發現開始檢查引數,給一下引數LD開頭的賦值,然後執行如下:
eval LD_TRACE_LOADED_OBJECTS=1 LD_WARN= LD_BIND_NOW= LD_LIBRARY_VERSION= LD_VERBOSE= /lib64/ld-linux-x86-64.so.2 ./lsnrctl

--//手工執行如下:
$ eval LD_TRACE_LOADED_OBJECTS=1 LD_WARN= LD_BIND_NOW= LD_LIBRARY_VERSION= LD_VERBOSE= /lib64/ld-linux-x86-64.so.2 ./lsnrctl
        linux-vdso.so.1 =>  (0x00007fff2097b000)
        libclntsh.so.11.1 => /u01/app/oracle/product/11.2.0.4/dbhome_1/lib/libclntsh.so.11.1 (0x00007f88c2549000)
        libnnz11.so => /u01/app/oracle/product/11.2.0.4/dbhome_1/lib/libnnz11.so (0x00007f88c217b000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003799800000)
        libm.so.6 => /lib64/libm.so.6 (0x00000032cac00000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x000000379a000000)
        libnsl.so.1 => /lib64/libnsl.so.1 (0x000000379c400000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003799000000)
        libaio.so.1 => /usr/lib64/libaio.so.1 (0x00007f88c1f78000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003798c00000)

--//如果單獨執行如下,直接執行程式:
$ /lib64/ld-linux-x86-64.so.2 ./lsnrctl
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 13-OCT-2021 15:23:45
Copyright (c) 1991, 2013, Oracle.  All rights reserved.
Welcome to LSNRCTL, type "help" for information.
LSNRCTL>

$ eval  /lib64/ld-linux-x86-64.so.2 ./lsnrctl
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 13-OCT-2021 15:24:48
Copyright (c) 1991, 2013, Oracle.  All rights reserved.
Welcome to LSNRCTL, type "help" for information.
LSNRCTL>

--//只要設定LD_TRACE_LOADED_OBJECTS的環境變數就可以執行,不需要賦值.
$ eval LD_TRACE_LOADED_OBJECTS=  /lib64/ld-linux-x86-64.so.2 ./lsnrctl
        linux-vdso.so.1 =>  (0x00007fffb93c1000)
        libclntsh.so.11.1 => /u01/app/oracle/product/11.2.0.4/dbhome_1/lib/libclntsh.so.11.1 (0x00007f427e229000)
        libnnz11.so => /u01/app/oracle/product/11.2.0.4/dbhome_1/lib/libnnz11.so (0x00007f427de5b000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003799800000)
        libm.so.6 => /lib64/libm.so.6 (0x00000032cac00000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x000000379a000000)
        libnsl.so.1 => /lib64/libnsl.so.1 (0x000000379c400000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003799000000)
        libaio.so.1 => /usr/lib64/libaio.so.1 (0x00007f427dc58000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003798c00000)

$ export LD_TRACE_LOADED_OBJECTS=
$ /lib64/ld-linux-x86-64.so.2 ./lsnrctl
        linux-vdso.so.1 =>  (0x00007fff2829c000)
        libclntsh.so.11.1 => /u01/app/oracle/product/11.2.0.4/dbhome_1/lib/libclntsh.so.11.1 (0x00007f360edb5000)
        libnnz11.so => /u01/app/oracle/product/11.2.0.4/dbhome_1/lib/libnnz11.so (0x00007f360e9e7000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003799800000)
        libm.so.6 => /lib64/libm.so.6 (0x00000032cac00000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x000000379a000000)
        libnsl.so.1 => /lib64/libnsl.so.1 (0x000000379c400000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003799000000)
        libaio.so.1 => /usr/lib64/libaio.so.1 (0x00007f360e7e4000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003798c00000)

$ unset LD_TRACE_LOADED_OBJECTS
$ /lib64/ld-linux-x86-64.so.2 ./lsnrctl
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 13-OCT-2021 15:29:31
Copyright (c) 1991, 2013, Oracle.  All rights reserved.
Welcome to LSNRCTL, type "help" for information.
LSNRCTL>

$ LD_TRACE_LOADED_OBJECTS=1
$ /lib64/ld-linux-x86-64.so.2 ./lsnrctl
LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 13-OCT-2021 15:29:55
Copyright (c) 1991, 2013, Oracle.  All rights reserved.
Welcome to LSNRCTL, type "help" for information.
LSNRCTL>


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2795768/,如需轉載,請註明出處,否則將追究法律責任。

相關文章