summaryrefslogtreecommitdiff
path: root/bin/check-elf-dynamic-objects
blob: 415dea8f46ee5170411b5ab1f90344a74297c04c (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
#!/usr/bin/env bash
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

# verify that ELF NEEDED entries are known-good so hopefully builds run on
# lots of different GNU/Linux distributions

set -euo pipefail

PARA=1
check_path="${INSTDIR:-.}"

help()
{
    cat << "EOF"
    -d <dir>  directory to check
    -p        run unbound parallel checks
    -h        help
EOF
    [ -z "${1:-}" ] && exit 0
}

die()
{
    echo "$1"
    echo
    help 1
    exit 1
}

while [ "${1:-}" != "" ]; do
    parm=${1%%=*}
    arg=${1#*=}
    has_arg=
    if [ "${1}" != "${parm?}" ] ; then
        has_arg=1
    else
        arg=""
    fi

    case "${parm}" in
        --dir|-d)
	    if [ "$has_arg" ] ; then
		check_path="$arg"
	    else
		shift
		check_path="$1"
	    fi
	    if [ ! -d "$check_path" ]; then
		die "Invalid directory '$check_path'"
	    fi
	    ;;
        -h)
	    help
	    ;;
        -p)
	    # this sounds counter intuitive but the idea
            # is to possibly support -p <n>
            # in the meantime: 0 = nolimit and -p 1 would mean
            # the current default: serialize
            PARA=0
            ;;
        -*)
            die "Invalid option $1"
            ;;
        *)
            if [ "$DO_NEW" = 1 ] ; then
                REPO="$1"
            else
                die "Invalid argument $1"
            fi
            ;;
    esac
    shift
done


files=$(find "${check_path}/program" "${check_path}/sdk/bin" -type f)
# all RPATHs should point to ${INSTDIR}/program so that's the files they find
programfiles=$(echo ${files} | grep -o '/program/[^/]* ' | xargs -n 1 basename)

# allowlists should contain only system libraries that have a good reputation
# of maintaining ABI stability
# allow extending the allowlist using the environment variable to be able to work
# on the installer stuff without the need for a baseline setup
globalallowlist="ld-linux-x86-64.so.2 ld-linux.so.2 libc.so.6 libm.so.6 libdl.so.2 libpthread.so.0 librt.so.1 libutil.so.1 libnsl.so.1 libcrypt.so.1 libgcc_s.so.1 libstdc++.so.6 libz.so.1 libfontconfig.so.1 libfreetype.so.6 libxml2.so.2 libxslt.so.1 libexslt.so.0 libnspr4.so libnss3.so libnssutil3.so libplc4.so libplds4.so libsmime3.so libssl3.so ${LO_ELFCHECK_ALLOWLIST-}"
x11allowlist="libX11.so.6 libX11-xcb.so.1 libXext.so.6 libSM.so.6 libICE.so.6 libXinerama.so.1 libXrender.so.1 libXrandr.so.2 libcairo.so.2"
openglallowlist="libGL.so.1"
gobjectallowlist="libgobject-2.0.so.0 libglib-2.0.so.0"
gdbusallowlist="libdbus-glib-1.so.2 libdbus-1.so.3 libgmodule-2.0.so.0 libgthread-2.0.so.0 ${gobjectallowlist}"
gioallowlist="libgio-2.0.so.0 ${gdbusallowlist}"
gstreamerallowlist="libgsttag-1.0.so.0 libgstaudio-1.0.so.0 libgstpbutils-1.0.so.0 libgstvideo-1.0.so.0 libgstbase-1.0.so.0 libgstreamer-1.0.so.0 ${gobjectallowlist}"
gtk3allowlist="libgtk-3.so.0 libgdk-3.so.0 libcairo-gobject.so.2 libpangocairo-1.0.so.0 libfribidi.so.0 libatk-1.0.so.0 libcairo.so.2 libpangoft2-1.0.so.0 libpango-1.0.so.0 libfontconfig.so.1 libfreetype.so.6 libgdk_pixbuf-2.0.so.0 libharfbuzz.so.0 ${gioallowlist}"
gtk4allowlist="libgtk-4.so.1 libcairo-gobject.so.2 libpangocairo-1.0.so.0 libatk-1.0.so.0 libcairo.so.2 libpango-1.0.so.0 libgdk_pixbuf-2.0.so.0 libharfbuzz.so.0 libgraphene-1.0.so.0 ${gioallowlist}"
qt5allowlist="libQt5Core.so.5 libQt5Gui.so.5 libQt5Network.so.5 libQt5Widgets.so.5 libQt5X11Extras.so.5 libcairo.so.2 libxcb.so.1 libxcb-icccm.so.4 ${gobjectallowlist}"
qt6allowlist="libQt6Core.so.6 libQt6Gui.so.6 libQt6Network.so.6 libQt6Widgets.so.6 libxcb.so.1"
kf5allowlist="libKF5ConfigCore.so.5 libKF5CoreAddons.so.5 libKF5I18n.so.5 libKF5KIOCore.so.5 libKF5KIOFileWidgets.so.5 libKF5KIOWidgets.so.5 libKF5WindowSystem.so.5"
avahiallowlist="libavahi-common.so.3 libavahi-client.so.3 ${gdbusallowlist}"
kerberosallowlist="libgssapi_krb5.so.2 libcom_err.so.2 libkrb5.so.3"
dconfallowlist="libdconf.so.1 libgio-2.0.so.0 ${gobjectallowlist}"

check_one_file()
{
local file="$1"

    skip=0
    allowlist="${globalallowlist}"
    case "${file}" in
        */sdk/docs/*)
            # skip the majority of files, no ELF binaries here
            skip=1
        ;;
        */_uuid.cpython-*.so)
            allowlist="${allowlist} libuuid.so.1"
        ;;
        */libcairo-lo.so.2)
            allowlist="${allowlist} ${x11allowlist} libxcb-shm.so.0 libxcb.so.1 libxcb-render.so.0"
        ;;
        */libcairocanvaslo.so)
            allowlist="${allowlist} libcairo.so.2"
        ;;
        */libucpgio1lo.so|*/liblosessioninstalllo.so|*/libevoablo.so)
            allowlist="${allowlist} ${gioallowlist}"
        ;;
        */libavmediagst.so)
            allowlist="${allowlist} ${gstreamerallowlist}"
        ;;
        */libavmediagtk.so)
            allowlist="${allowlist} ${gtk4allowlist}"
        ;;
        */libvclplug_kf5lo.so|*/libkf5be1lo.so)
            if [ "$ENABLE_KF5" = TRUE ]; then
                allowlist="${allowlist} ${qt5allowlist} ${kf5allowlist}"
            fi
        ;;
        */libvclplug_gtk3lo.so|*/updater)
            allowlist="${allowlist} ${x11allowlist} ${gtk3allowlist}"
        ;;
        */libvclplug_gtk4lo.so)
            allowlist="${allowlist} ${x11allowlist} ${gtk4allowlist}"
        ;;
        */libvclplug_qt5lo.so)
            if [ "$ENABLE_QT5" = TRUE ]; then
                allowlist="${allowlist} ${qt5allowlist}"
            fi
        ;;
        */libvclplug_qt6lo.so)
            if [ "$ENABLE_QT6" = TRUE ]; then
                allowlist="${allowlist} ${qt6allowlist}"
            fi
        ;;
        */libvclplug_gtk3_kde5lo.so)
            if [ "$ENABLE_GTK3_KDE5" = TRUE ]; then
                allowlist="${allowlist} ${x11allowlist} ${gtk3allowlist} ${qt5allowlist} ${kf5allowlist}"
            fi
        ;;
        */lo_kde5filepicker)
            if [ "$ENABLE_GTK3_KDE5" = TRUE ]; then
                allowlist="${allowlist} ${x11allowlist} ${gtk3allowlist} ${qt5allowlist} \
                    ${kf5allowlist}"
            fi
        ;;
        */libdesktop_detectorlo.so|*/oosplash|*/gengal.bin)
            allowlist="${allowlist} ${x11allowlist}"
        ;;
        */libvclplug_genlo.so|*/libchartcorelo.so|*/libavmediaogl.so|*/libOGLTranslo.so|*/liboglcanvaslo.so)
            allowlist="${allowlist} ${x11allowlist} ${openglallowlist}"
        ;;
        */libvcllo.so)
            allowlist="${allowlist} ${x11allowlist} ${openglallowlist} ${gioallowlist} libcups.so.2"
        ;;
        */libsofficeapp.so)
            allowlist="${allowlist} ${x11allowlist} ${openglallowlist} ${gioallowlist} libcups.so.2"
        ;;
        */liblibreofficekitgtk.so)
            allowlist="${allowlist} ${gtk3allowlist}"
        ;;
        */libsdlo.so)
            allowlist="${allowlist} ${avahiallowlist}"
        ;;
        */libskialo.so)
            allowlist="${allowlist} ${openglallowlist} ${x11allowlist}"
        ;;
        */libofficebean.so)
            allowlist="${allowlist} libjawt.so"
        ;;
        */libpostgresql-sdbc-impllo.so)
            allowlist="${allowlist} ${kerberosallowlist}"
        ;;
        */libconfigmgrlo.so)
            if [ "$ENABLE_DCONF" = TRUE ]; then
                allowlist="${allowlist} ${dconfallowlist}"
            fi
        ;;
        */libmergedlo.so)
            allowlist="${allowlist} ${x11allowlist} ${openglallowlist} ${gioallowlist} ${dconfallowlist} libcups.so.2 libcairo.so.2"
        ;;
    esac
    if test "${skip}" = 0 && readelf -d "${file}" &> /dev/null ; then
        rpath=$(readelf -d "${file}" | grep '(\(RPATH\|RUNPATH\))' || true)
        neededs=$(readelf -d "${file}" | grep '(NEEDED)' | sed -e 's/.*\[\(.*\)\]$/\1/')
        neededsinternal=
        for needed in ${neededs}
        do
            if ! echo ${allowlist} | grep -q -w "${needed}" ; then
                neededsinternal="${neededsinternal} ${needed}"
                if ! echo ${programfiles} | grep -q -w "${needed}" ; then
                    echo "${file}" has suspicious NEEDED: "${needed}"
                    status=1
                fi
            fi
        done
        if test -z "${rpath}" ; then
            case "${file}" in
                */python-core-*/lib/lib-dynload/*)
                    # python modules don't have RPATH
                ;;
                */share/extensions/*)
                    # extension libraries don't have RPATH
                ;;
                *)
                    # no NEEDED from instdir, no RPATH needed
                    if test -n "${neededsinternal}" ; then
                        echo "${file}" has no RPATH
                        status=1
                    fi
                ;;
            esac
        else
            case "$file" in
                */sdk/bin/*)
                    if echo "${rpath}" | grep -q -v '\[\$ORIGIN/../../program\]$' ; then
                        echo "${file}" has unexpected RPATH "${rpath}"
                        status=1
                    fi
                ;;
                *)
                    if echo "${rpath}" | grep -q -v '\[\$ORIGIN\]$' ; then
                        echo "${file}" has unexpected RPATH "${rpath}"
                        status=1
                    fi
                ;;
            esac
        fi
    fi
}
status=0

if [ "$PARA" = "1" ] ; then
    for file in ${files}
    do
	check_one_file $file
    done
else
    rm -f check_elf.out
    for file in ${files}
    do
	(
	check_one_file $file
	)>> check_elf.out &
    done

    wait

    if [ -s check_elf.out ] ; then
        cat check_elf.out
        status=1
    fi
    rm check_elf.out
fi
exit ${status}