scripts/xldd.in
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Tue Nov 23 21:35:41 2010 +0100 (2010-11-23)
changeset 2189 52e1698ac243
parent 2188 aa2305c5b2a5
child 2190 9b9a0bb51cfb
permissions -rwxr-xr-x
scripts/xldd: parse /etc/ld.so.conf

Scan /etc/ld.so.conf for paths to search for libraries.
Also follow include directives in there.

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