summaryrefslogtreecommitdiff
path: root/scripts/xldd.in
blob: 7e8beaaec987f6b8e95b4d6215b8cb96a685e7f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!@@CT_bash@@

# NON-CONFIGURABLE STUFF!
export LC_ALL=C
version="@@CT_VERSION@@"
bits="@@CT_BITS@@"
sed="@@CT_sed@@"
grep="@@CT_grep@@"

my_name="$( basename "${0}" )"
prefix="${0%-ldd}"
gcc="${prefix}-gcc"
readelf="${prefix}-readelf"
fake_load_addr="$((0xdeadbeef))"
fake_load_addr_sys="$((0x8badf00d))"
ld_library_path="/lib:/usr/lib"

do_error() {
    printf "%s: %s\n" "${my_name}" "$*" >&2
}

do_opt_error() {
    do_error "$@"
    printf "Try \`%s --help' for more information\n" >&2
}

do_trace() {
    local depth=0

    [ -z "${CT_XLDD_VERBOSE}" ] && return 0

    for((depth=0; "${#FUNCNAME[$((depth+1))]}" != 0; depth++)); do :; done
    printf "%*s" $((4*(depth-1))) "" >&2
    printf -- "$@" >&2
}

show_version() {
    # Fake a real ldd, just in case some dumb script would check
    cat <<_EOF_
ldd (crosstool-NG) ${version}
Copyright (C) 2010 "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Licensed under the GPLv2, see the file LICENSES in the top-directory of the
sources for this package.
_EOF_
}

show_help() {
    cat <<_EOF_
Usage: ${my_name} [OPTION]... --root DIR FILE...
      --help              print this help and exit
      --version           print version information and exit
      --root dir          treat dir as being the root of the target
  -s, --show-system       mark libs from the sysroot with a trailing '[*]'

_EOF_
    cat <<_EOF_ |fmt
${my_name} tries to mimick the behavior of a real native ldd, but can be
used in a cross-development environment. Here is how it differs from a
real native ldd:

If the CT_XLDD_DEBUG variable is set and non-empty, then ${myname} will
print a lot of debug messages, explaining how it builds the library
search path, and how each library was found and why.

The LD_LIBRARY_PATH variable is not used, as it can not reliably be
guessed except at runtime, and we can't run.

${my_name} does not scan /etc/ld.so.cache, but instead uses /etc/ld.so.conf
(it understands the include directives therein for libces that have that).

${my_name} will search the directory specified with --root for libraries
to resolve the NEEDED tags. If --root is not set, then ${my_name} will
use the value in the environment variable \${CT_XLDD_ROOT}. If neither
is set, then this is an error.

If NEEDED libraries can't be found in the specified root directory, then
${my_name} will also look in the sysroot of the toolchain to see if it
can find them.

For NEEDED libraries that were found, the output will look like:
        libneeded.so => /path/to/libneeded.so (0xloadaddr)

and for those that were not found, the output will look like:
        libneeded.so not found

The paths are relative to the specified root directory, or to the sysroot
(eg. /lib/libneeded.so, /usr/lib/libneeded.so, and so on...).

The expected load address 'loadaddr' is a faked address to match the output
of the real ldd, but has no actual meaning (set to some constants for now,
0x8badf00d for libraries from the sysroot, 0xdeadbeef for others).
_EOF_

# Unimplemeted yet:
#  -d, --data-relocs       process data relocations
#  -r, --function-relocs   process data and function relocations
#  -u, --unused            print unused direct dependencies
#  -v, --verbose           print all information

# See also this thread:
#  http://sourceware.org/ml/crossgcc/2008-09/msg00057.html
}

# Parse command line options
root="${CT_XLDD_ROOT}"
show_system=
while true; do
    case "${1}" in
        --help)
            show_help
            exit 0
            ;;
        --version)
            show_version
            exit 0
            ;;
        --root)
            root="$2"
            shift
            ;;
        --root=*)
            root="${1#--root=}"
            ;;
        --show-system|-s)
            show_system=1
            ;;
        -*)
            do_opt_error "unrecognized option \`${1}'"
            exit 1
            ;;
        *)
            break
            ;;
    esac
    shift
done

# Sanity checks
if [ -z "${root}" ]; then
    do_opt_error "no root given"
    exit 1
fi
if [ ! -d "${root}" ]; then
    do_error "\`${root}': no such file or directory"
    exit 1
fi

sysroot="$( "${gcc}" -print-sysroot 2>/dev/null )"
if [ -z "${sysroot}" ]; then
    sysroot="$( "${gcc}" -print-file-name=libc.so 2>/dev/null   \
                |sed -r -e 's:/usr/lib/libc.so$::;'             \
              )"
fi
if [ -z "${sysroot}" ]; then
    do_error "unable to find sysroot for \`${gcc}'"
fi

do_report_needed_found() {
    local needed="${1}"
    local path="${2}"
    local system="${3}"
    local loadaddr
    local sys

    if [ -z "${system}" ]; then
        loadaddr="${fake_load_addr}"
    else
        loadaddr="${fake_load_addr_sys}"
        if [ -n "${show_system}" ]; then
            sys=" [*]"
        fi
    fi

    printf "%8s%s => %s (0x%0*x)%s\n"   \
           ""                           \
           "${needed}"                  \
           "${path}"                    \
           "$((bits/4))"                \
           "${loadaddr}"                \
           "${sys}"
}

# Search a needed file, scanning ${lib_dir} in the root directory
do_find_needed() {
    local needed="${1}"
    local found
    local found_sysroot
    local d

    do_trace "Searching for '${needed}'\n"

    for d in "${needed_search_path[@]}"; do
        do_trace "-> looking in '${d}'\n"
        if [ -f "${root}${d}/${needed}" ]; then
            found="${d}/${needed}"
            do_trace "---> found\n"
            break
        fi
    done
    if [ -z "${found}" ]; then
    for d in "${needed_search_path[@]}"; do
        do_trace "-> looking in '${d}' (sysroot)\n"
        if [ -f "${sysroot}${d}/${needed}" ]; then
            found_sysroot="${d}/${needed}"
            do_trace "---> found\n"
            break
        fi
    done
    fi

    if [ -n "${found}" ]; then
        do_report_needed_found "${needed}" "${found}"
        do_process_file "${root}${found}"
    elif [ -n "${found_sysroot}" ]; then
        do_report_needed_found "${needed}" "${found_sysroot}" "sys"
        do_process_file "${sysroot}${found_sysroot}"
    else
        printf "%8c%s not found\n" "" "${needed}"
    fi
}

# Scan a file for all NEEDED tags
do_process_file() {
    local file="${1}"

    do_trace "Parsing file '${file}'\n"

    "${readelf}" -d "${file}"                                           \
    |"${grep}" -E '\(NEEDED\)'                                          \
    |"${sed}" -r -e 's/^.*Shared library:[[:space:]]+\[(.*)\]$/\1/;'    \
    |while read needed; do
        do_find_needed "${needed}"
     done
}

# Recursively scan a /etc/ld.so.conf file
do_scan_etc_ldsoconf() {
    local ldsoconf="${1}"
    local g
    local f

    [ -f "${ldsoconf}" ] || return 0
    do_trace "Parsing ld.so.conf: '${ldsoconf}'\n"

    while read line; do
        case "${line}" in
            include\ *)
                g="${root}${line#include }"
                do_trace "-> handling include directive '${g}'\n"
                for f in ${g}; do
                    do_scan_etc_ldsoconf "${f}"
                done
                ;;
            \#*|"")
                ;;
            *)
                do_trace "-> adding search dir '${line}'\n"
                needed_search_path+=( "${line}" )
                ;;
        esac
    done <"${ldsoconf}"
}

# Build up the full list of search directories
declare -a needed_search_path
do_trace "Adding basic lib dirs\n"
ld_library_path="${ld_library_path}:"
while [ -n "${ld_library_path}" ]; do
    d="${ld_library_path%%:*}"
    if [ -n "${d}" ]; then
        do_trace "-> adding search dir '${d}'\n"
        needed_search_path+=( "${d}" )
    fi
    ld_library_path="${ld_library_path#*:}"
done
do_trace "Scanning '/etc/ld.so.conf'\n"
do_scan_etc_ldsoconf "${root}/etc/ld.so.conf"
do_trace "Search path:\n"
for p in "${needed_search_path[@]}"; do
    do_trace "-> '${p}'\n"
done

do_trace "Scanning file '${1}'\n"
do_process_file "${1}"