scripts/xldd.in
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Tue Nov 23 21:35:15 2010 +0100 (2010-11-23)
changeset 2187 9cdb153ff9df
parent 2186 78d2f99d403f
child 2188 aa2305c5b2a5
permissions -rwxr-xr-x
scripts/xldd: report appropriate load address for 32- or 64-bit

For 32-bit target systems, report 4-byte (8-xdigit) wide adresses,
and for 64-bit, report 8-byte (16-xdigit) wide adresses.

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
[Note: this is missing for now...]
yann@2034
    59
yann@2034
    60
${my_name} will search the directory specified with --root for libraries
yann@2034
    61
to resolve the NEEDED tags. If --root is not set, then ${my_name} will
yann@2034
    62
use the value in the environment variable \${CT_XLDD_ROOT}. If neither
yann@2034
    63
is set, then this is an error.
yann@2034
    64
yann@2034
    65
If NEEDED libraries can't be found in the specified root directory, then
yann@2034
    66
${my_name} will also look in the sysroot of the toolchain to see if it
yann@2034
    67
can find them.
yann@2034
    68
yann@2034
    69
For NEEDED libraries that were found, the output will look like:
yann@2034
    70
        libneeded.so => /path/to/libneeded.so (0xloadaddr)
yann@2034
    71
yann@2034
    72
and for those that were not found, the output will look like:
yann@2034
    73
        libneeded.so not found
yann@2034
    74
yann@2034
    75
The paths are relative to the specified root directory, or to the sysroot
yann@2034
    76
(eg. /lib/libneeded.so, /usr/lib/libneeded.so, and so on...).
yann@2034
    77
yann@2034
    78
The expected load address 'loadaddr' is a faked address to match the output
yann@2034
    79
of the real ldd, but has no actual meaning (set to some constants for now,
yann@2183
    80
0x8badf00d for libraries from the sysroot, 0xdeadbeef for others).
yann@2034
    81
_EOF_
yann@2034
    82
yann@2034
    83
# Unimplemeted yet:
yann@2034
    84
#  -d, --data-relocs       process data relocations
yann@2034
    85
#  -r, --function-relocs   process data and function relocations
yann@2034
    86
#  -u, --unused            print unused direct dependencies
yann@2034
    87
#  -v, --verbose           print all information
yann@2034
    88
yann@2034
    89
# See also this thread:
yann@2034
    90
#  http://sourceware.org/ml/crossgcc/2008-09/msg00057.html
yann@2034
    91
}
yann@2034
    92
yann@2034
    93
# Parse command line options
yann@2034
    94
root="${CT_XLDD_ROOT}"
yann@2034
    95
show_system=
yann@2034
    96
while true; do
yann@2034
    97
    case "${1}" in
yann@2034
    98
        --help)
yann@2034
    99
            show_help
yann@2034
   100
            exit 0
yann@2034
   101
            ;;
yann@2034
   102
        --version)
yann@2034
   103
            show_version
yann@2034
   104
            exit 0
yann@2034
   105
            ;;
yann@2034
   106
        --root)
yann@2034
   107
            root="$2"
yann@2034
   108
            shift
yann@2034
   109
            ;;
yann@2034
   110
        --root=*)
yann@2034
   111
            root="${1#--root=}"
yann@2034
   112
            ;;
yann@2034
   113
        --show-system|-s)
yann@2034
   114
            show_system=1
yann@2034
   115
            ;;
yann@2034
   116
        -*)
yann@2034
   117
            do_opt_error "unrecognized option \`${1}'"
yann@2034
   118
            exit 1
yann@2034
   119
            ;;
yann@2034
   120
        *)
yann@2034
   121
            break
yann@2034
   122
            ;;
yann@2034
   123
    esac
yann@2034
   124
    shift
yann@2034
   125
done
yann@2034
   126
yann@2034
   127
# Sanity checks
yann@2034
   128
if [ -z "${root}" ]; then
yann@2034
   129
    do_opt_error "no root given"
yann@2034
   130
    exit 1
yann@2034
   131
fi
yann@2034
   132
if [ ! -d "${root}" ]; then
yann@2034
   133
    do_error "\`${root}': no such file or directory"
yann@2034
   134
    exit 1
yann@2034
   135
fi
yann@2034
   136
yann@2034
   137
sysroot="$( "${gcc}" -print-sysroot )"
yann@2034
   138
yann@2034
   139
do_report_needed_found() {
yann@2034
   140
    local needed="${1}"
yann@2034
   141
    local path="${2}"
yann@2034
   142
    local system="${3}"
yann@2034
   143
    local loadaddr
yann@2034
   144
    local sys
yann@2034
   145
yann@2034
   146
    if [ -z "${system}" ]; then
yann@2034
   147
        loadaddr="${fake_load_addr}"
yann@2034
   148
    else
yann@2034
   149
        loadaddr="${fake_load_addr_sys}"
yann@2034
   150
        if [ -n "${show_system}" ]; then
yann@2034
   151
            sys=" [*]"
yann@2034
   152
        fi
yann@2034
   153
    fi
yann@2034
   154
yann@2034
   155
    printf "%8s%s => %s (0x%0*x)%s\n"   \
yann@2034
   156
           ""                           \
yann@2034
   157
           "${needed}"                  \
yann@2034
   158
           "${path}"                    \
yann@2187
   159
           "$((bits/4))"                \
yann@2034
   160
           "${loadaddr}"                \
yann@2034
   161
           "${sys}"
yann@2034
   162
}
yann@2034
   163
yann@2034
   164
# Search a needed file, scanning ${lib_dir} in the root directory
yann@2034
   165
do_find_needed() {
yann@2034
   166
    local needed="${1}"
yann@2034
   167
    local found
yann@2034
   168
    local found_sysroot
yann@2034
   169
    local d
yann@2034
   170
yann@2034
   171
    for d in "${needed_search_path[@]}"; do
yann@2034
   172
        if [ -f "${root}${d}/${needed}" ]; then
yann@2034
   173
            found="${d}/${needed}"
yann@2186
   174
            break
yann@2034
   175
        fi
yann@2034
   176
    done
yann@2034
   177
    if [ -z "${found}" ]; then
yann@2034
   178
    for d in "${needed_search_path[@]}"; do
yann@2034
   179
        if [ -f "${sysroot}${d}/${needed}" ]; then
yann@2034
   180
            found_sysroot="${d}/${needed}"
yann@2186
   181
            break
yann@2034
   182
        fi
yann@2034
   183
    done
yann@2034
   184
    fi
yann@2034
   185
yann@2034
   186
    if [ -n "${found}" ]; then
yann@2034
   187
        do_report_needed_found "${needed}" "${found}"
yann@2034
   188
        do_process_file "${root}${found}"
yann@2034
   189
    elif [ -n "${found_sysroot}" ]; then
yann@2034
   190
        do_report_needed_found "${needed}" "${found_sysroot}" "sys"
yann@2034
   191
        do_process_file "${sysroot}${found_sysroot}"
yann@2034
   192
    else
yann@2034
   193
        printf "%8c%s not found\n" "" "${needed}"
yann@2034
   194
    fi
yann@2034
   195
}
yann@2034
   196
yann@2034
   197
# Scan a file for all NEEDED tags
yann@2034
   198
do_process_file() {
yann@2034
   199
    local file="${1}"
yann@2034
   200
yann@2034
   201
    "${readelf}" -d "${file}"                                           \
yann@2034
   202
    |"${grep}" -E '\(NEEDED\)'                                          \
yann@2034
   203
    |"${sed}" -r -e 's/^.*Shared library:[[:space:]]+\[(.*)\]$/\1/;'    \
yann@2034
   204
    |while read needed; do
yann@2034
   205
        do_find_needed "${needed}"
yann@2034
   206
     done
yann@2034
   207
}
yann@2034
   208
yann@2034
   209
# Build up the full list of search directories
yann@2034
   210
declare -a needed_search_path
yann@2034
   211
ld_library_path="${ld_library_path}:"
yann@2034
   212
while [ -n "${ld_library_path}" ]; do
yann@2034
   213
    d="${ld_library_path%%:*}"
yann@2034
   214
    [ -n "${d}" ] && needed_search_path+=( "${d}" )
yann@2034
   215
    ld_library_path="${ld_library_path#*:}"
yann@2034
   216
done
yann@2034
   217
yann@2034
   218
do_process_file "${1}"