scripts/xldd.in
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Tue Nov 23 21:35:45 2010 +0100 (2010-11-23)
changeset 2190 9b9a0bb51cfb
parent 2189 52e1698ac243
child 2191 226d9684bf3c
permissions -rwxr-xr-x
script/xldd: add debug traces

Add debug traces to help understand how xldd finds the
libraries, what directories it scans, in which order...

Signed-off-by: "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
yann@2034
     1
#!@@CT_bash@@
yann@2034
     2
yann@2034
     3
# NON-CONFIGURABLE STUFF!
yann@2034
     4
export LC_ALL=C
yann@2184
     5
version="@@CT_VERSION@@"
yann@2187
     6
bits="@@CT_BITS@@"
yann@2034
     7
sed="@@CT_sed@@"
yann@2034
     8
grep="@@CT_grep@@"
yann@2187
     9
yann@2034
    10
my_name="$( basename "${0}" )"
yann@2034
    11
prefix="${0%-ldd}"
yann@2034
    12
gcc="${prefix}-gcc"
yann@2034
    13
readelf="${prefix}-readelf"
yann@2034
    14
fake_load_addr="$((0xdeadbeef))"
yann@2034
    15
fake_load_addr_sys="$((0x8badf00d))"
yann@2034
    16
ld_library_path="/lib:/usr/lib"
yann@2034
    17
yann@2034
    18
do_error() {
yann@2034
    19
    printf "%s: %s\n" "${my_name}" "$*" >&2
yann@2034
    20
}
yann@2034
    21
yann@2034
    22
do_opt_error() {
yann@2034
    23
    do_error "$@"
yann@2183
    24
    printf "Try \`%s --help' for more information\n" >&2
yann@2034
    25
}
yann@2034
    26
yann@2190
    27
do_trace() {
yann@2190
    28
    local depth=0
yann@2190
    29
yann@2190
    30
    [ -z "${CT_XLDD_VERBOSE}" ] && return 0
yann@2190
    31
yann@2190
    32
    for((depth=0; "${#FUNCNAME[$((depth+1))]}" != 0; depth++)); do :; done
yann@2190
    33
    printf "%*s" $((4*(depth-1))) "" >&2
yann@2190
    34
    printf -- "$@" >&2
yann@2190
    35
}
yann@2190
    36
yann@2034
    37
show_version() {
yann@2034
    38
    # Fake a real ldd, just in case some dumb script would check
yann@2034
    39
    cat <<_EOF_
yann@2184
    40
ldd (crosstool-NG) ${version}
yann@2034
    41
Copyright (C) 2010 "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
yann@2034
    42
This is free software; see the source for copying conditions.  There is NO
yann@2034
    43
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
yann@2034
    44
Licensed under the GPLv2, see the file LICENSES in the top-directory of the
yann@2034
    45
sources for this package.
yann@2034
    46
_EOF_
yann@2034
    47
}
yann@2034
    48
yann@2034
    49
show_help() {
yann@2034
    50
    cat <<_EOF_
yann@2034
    51
Usage: ${my_name} [OPTION]... --root DIR FILE...
yann@2034
    52
      --help              print this help and exit
yann@2034
    53
      --version           print version information and exit
yann@2034
    54
      --root dir          treat dir as being the root of the target
yann@2034
    55
  -s, --show-system       mark libs from the sysroot with a trailing '[*]'
yann@2034
    56
yann@2034
    57
_EOF_
yann@2034
    58
    cat <<_EOF_ |fmt
yann@2034
    59
${my_name} tries to mimick the behavior of a real native ldd, but can be
yann@2034
    60
used in a cross-development environment. Here is how it differs from a
yann@2034
    61
real native ldd:
yann@2034
    62
yann@2190
    63
If the CT_XLDD_DEBUG variable is set and non-empty, then ${myname} will
yann@2190
    64
print a lot of debug messages, explaining how it builds the library
yann@2190
    65
search path, and how each library was found and why.
yann@2190
    66
yann@2034
    67
The LD_LIBRARY_PATH variable is not used, as it can not reliably be
yann@2034
    68
guessed except at runtime, and we can't run.
yann@2034
    69
yann@2034
    70
${my_name} does not scan /etc/ld.so.cache, but instead uses /etc/ld.so.conf
yann@2034
    71
(it understands the include directives therein for libces that have that).
yann@2034
    72
yann@2034
    73
${my_name} will search the directory specified with --root for libraries
yann@2034
    74
to resolve the NEEDED tags. If --root is not set, then ${my_name} will
yann@2034
    75
use the value in the environment variable \${CT_XLDD_ROOT}. If neither
yann@2034
    76
is set, then this is an error.
yann@2034
    77
yann@2034
    78
If NEEDED libraries can't be found in the specified root directory, then
yann@2034
    79
${my_name} will also look in the sysroot of the toolchain to see if it
yann@2034
    80
can find them.
yann@2034
    81
yann@2034
    82
For NEEDED libraries that were found, the output will look like:
yann@2034
    83
        libneeded.so => /path/to/libneeded.so (0xloadaddr)
yann@2034
    84
yann@2034
    85
and for those that were not found, the output will look like:
yann@2034
    86
        libneeded.so not found
yann@2034
    87
yann@2034
    88
The paths are relative to the specified root directory, or to the sysroot
yann@2034
    89
(eg. /lib/libneeded.so, /usr/lib/libneeded.so, and so on...).
yann@2034
    90
yann@2034
    91
The expected load address 'loadaddr' is a faked address to match the output
yann@2034
    92
of the real ldd, but has no actual meaning (set to some constants for now,
yann@2183
    93
0x8badf00d for libraries from the sysroot, 0xdeadbeef for others).
yann@2034
    94
_EOF_
yann@2034
    95
yann@2034
    96
# Unimplemeted yet:
yann@2034
    97
#  -d, --data-relocs       process data relocations
yann@2034
    98
#  -r, --function-relocs   process data and function relocations
yann@2034
    99
#  -u, --unused            print unused direct dependencies
yann@2034
   100
#  -v, --verbose           print all information
yann@2034
   101
yann@2034
   102
# See also this thread:
yann@2034
   103
#  http://sourceware.org/ml/crossgcc/2008-09/msg00057.html
yann@2034
   104
}
yann@2034
   105
yann@2034
   106
# Parse command line options
yann@2034
   107
root="${CT_XLDD_ROOT}"
yann@2034
   108
show_system=
yann@2034
   109
while true; do
yann@2034
   110
    case "${1}" in
yann@2034
   111
        --help)
yann@2034
   112
            show_help
yann@2034
   113
            exit 0
yann@2034
   114
            ;;
yann@2034
   115
        --version)
yann@2034
   116
            show_version
yann@2034
   117
            exit 0
yann@2034
   118
            ;;
yann@2034
   119
        --root)
yann@2034
   120
            root="$2"
yann@2034
   121
            shift
yann@2034
   122
            ;;
yann@2034
   123
        --root=*)
yann@2034
   124
            root="${1#--root=}"
yann@2034
   125
            ;;
yann@2034
   126
        --show-system|-s)
yann@2034
   127
            show_system=1
yann@2034
   128
            ;;
yann@2034
   129
        -*)
yann@2034
   130
            do_opt_error "unrecognized option \`${1}'"
yann@2034
   131
            exit 1
yann@2034
   132
            ;;
yann@2034
   133
        *)
yann@2034
   134
            break
yann@2034
   135
            ;;
yann@2034
   136
    esac
yann@2034
   137
    shift
yann@2034
   138
done
yann@2034
   139
yann@2034
   140
# Sanity checks
yann@2034
   141
if [ -z "${root}" ]; then
yann@2034
   142
    do_opt_error "no root given"
yann@2034
   143
    exit 1
yann@2034
   144
fi
yann@2034
   145
if [ ! -d "${root}" ]; then
yann@2034
   146
    do_error "\`${root}': no such file or directory"
yann@2034
   147
    exit 1
yann@2034
   148
fi
yann@2034
   149
yann@2188
   150
sysroot="$( "${gcc}" -print-sysroot 2>/dev/null )"
yann@2188
   151
if [ -z "${sysroot}" ]; then
yann@2188
   152
    sysroot="$( "${gcc}" -print-file-name=libc.so 2>/dev/null   \
yann@2188
   153
                |sed -r -e 's:/usr/lib/libc.so$::;'             \
yann@2188
   154
              )"
yann@2188
   155
fi
yann@2188
   156
if [ -z "${sysroot}" ]; then
yann@2188
   157
    do_error "unable to find sysroot for \`${gcc}'"
yann@2188
   158
fi
yann@2034
   159
yann@2034
   160
do_report_needed_found() {
yann@2034
   161
    local needed="${1}"
yann@2034
   162
    local path="${2}"
yann@2034
   163
    local system="${3}"
yann@2034
   164
    local loadaddr
yann@2034
   165
    local sys
yann@2034
   166
yann@2034
   167
    if [ -z "${system}" ]; then
yann@2034
   168
        loadaddr="${fake_load_addr}"
yann@2034
   169
    else
yann@2034
   170
        loadaddr="${fake_load_addr_sys}"
yann@2034
   171
        if [ -n "${show_system}" ]; then
yann@2034
   172
            sys=" [*]"
yann@2034
   173
        fi
yann@2034
   174
    fi
yann@2034
   175
yann@2034
   176
    printf "%8s%s => %s (0x%0*x)%s\n"   \
yann@2034
   177
           ""                           \
yann@2034
   178
           "${needed}"                  \
yann@2034
   179
           "${path}"                    \
yann@2187
   180
           "$((bits/4))"                \
yann@2034
   181
           "${loadaddr}"                \
yann@2034
   182
           "${sys}"
yann@2034
   183
}
yann@2034
   184
yann@2034
   185
# Search a needed file, scanning ${lib_dir} in the root directory
yann@2034
   186
do_find_needed() {
yann@2034
   187
    local needed="${1}"
yann@2034
   188
    local found
yann@2034
   189
    local found_sysroot
yann@2034
   190
    local d
yann@2034
   191
yann@2190
   192
    do_trace "Searching for '${needed}'\n"
yann@2190
   193
yann@2034
   194
    for d in "${needed_search_path[@]}"; do
yann@2190
   195
        do_trace "-> looking in '${d}'\n"
yann@2034
   196
        if [ -f "${root}${d}/${needed}" ]; then
yann@2034
   197
            found="${d}/${needed}"
yann@2190
   198
            do_trace "---> found\n"
yann@2186
   199
            break
yann@2034
   200
        fi
yann@2034
   201
    done
yann@2034
   202
    if [ -z "${found}" ]; then
yann@2034
   203
    for d in "${needed_search_path[@]}"; do
yann@2190
   204
        do_trace "-> looking in '${d}' (sysroot)\n"
yann@2034
   205
        if [ -f "${sysroot}${d}/${needed}" ]; then
yann@2034
   206
            found_sysroot="${d}/${needed}"
yann@2190
   207
            do_trace "---> found\n"
yann@2186
   208
            break
yann@2034
   209
        fi
yann@2034
   210
    done
yann@2034
   211
    fi
yann@2034
   212
yann@2034
   213
    if [ -n "${found}" ]; then
yann@2034
   214
        do_report_needed_found "${needed}" "${found}"
yann@2034
   215
        do_process_file "${root}${found}"
yann@2034
   216
    elif [ -n "${found_sysroot}" ]; then
yann@2034
   217
        do_report_needed_found "${needed}" "${found_sysroot}" "sys"
yann@2034
   218
        do_process_file "${sysroot}${found_sysroot}"
yann@2034
   219
    else
yann@2034
   220
        printf "%8c%s not found\n" "" "${needed}"
yann@2034
   221
    fi
yann@2034
   222
}
yann@2034
   223
yann@2034
   224
# Scan a file for all NEEDED tags
yann@2034
   225
do_process_file() {
yann@2034
   226
    local file="${1}"
yann@2034
   227
yann@2190
   228
    do_trace "Parsing file '${file}'\n"
yann@2190
   229
yann@2034
   230
    "${readelf}" -d "${file}"                                           \
yann@2034
   231
    |"${grep}" -E '\(NEEDED\)'                                          \
yann@2034
   232
    |"${sed}" -r -e 's/^.*Shared library:[[:space:]]+\[(.*)\]$/\1/;'    \
yann@2034
   233
    |while read needed; do
yann@2034
   234
        do_find_needed "${needed}"
yann@2034
   235
     done
yann@2034
   236
}
yann@2034
   237
yann@2189
   238
# Recursively scan a /etc/ld.so.conf file
yann@2189
   239
do_scan_etc_ldsoconf() {
yann@2189
   240
    local ldsoconf="${1}"
yann@2189
   241
    local g
yann@2189
   242
    local f
yann@2189
   243
yann@2189
   244
    [ -f "${ldsoconf}" ] || return 0
yann@2190
   245
    do_trace "Parsing ld.so.conf: '${ldsoconf}'\n"
yann@2189
   246
yann@2189
   247
    while read line; do
yann@2189
   248
        case "${line}" in
yann@2189
   249
            include\ *)
yann@2189
   250
                g="${root}${line#include }"
yann@2190
   251
                do_trace "-> handling include directive '${g}'\n"
yann@2189
   252
                for f in ${g}; do
yann@2189
   253
                    do_scan_etc_ldsoconf "${f}"
yann@2189
   254
                done
yann@2189
   255
                ;;
yann@2189
   256
            \#*|"")
yann@2189
   257
                ;;
yann@2189
   258
            *)
yann@2190
   259
                do_trace "-> adding search dir '${line}'\n"
yann@2189
   260
                needed_search_path+=( "${line}" )
yann@2189
   261
                ;;
yann@2189
   262
        esac
yann@2189
   263
    done <"${ldsoconf}"
yann@2189
   264
}
yann@2189
   265
yann@2034
   266
# Build up the full list of search directories
yann@2034
   267
declare -a needed_search_path
yann@2190
   268
do_trace "Adding basic lib dirs\n"
yann@2034
   269
ld_library_path="${ld_library_path}:"
yann@2034
   270
while [ -n "${ld_library_path}" ]; do
yann@2034
   271
    d="${ld_library_path%%:*}"
yann@2190
   272
    if [ -n "${d}" ]; then
yann@2190
   273
        do_trace "-> adding search dir '${d}'\n"
yann@2190
   274
        needed_search_path+=( "${d}" )
yann@2190
   275
    fi
yann@2034
   276
    ld_library_path="${ld_library_path#*:}"
yann@2034
   277
done
yann@2190
   278
do_trace "Scanning '/etc/ld.so.conf'\n"
yann@2189
   279
do_scan_etc_ldsoconf "${root}/etc/ld.so.conf"
yann@2190
   280
do_trace "Search path:\n"
yann@2190
   281
for p in "${needed_search_path[@]}"; do
yann@2190
   282
    do_trace "-> '${p}'\n"
yann@2190
   283
done
yann@2034
   284
yann@2190
   285
do_trace "Scanning file '${1}'\n"
yann@2034
   286
do_process_file "${1}"