blob: 199f1b89ac55aeddcb78658a8f48895ee1b94ad2 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#!/bin/sh
2# Script to validate NaCl binaries after linking.
3
4# Copyright (C) 2015 Free Software Foundation, Inc.
5# This file is part of the GNU C Library.
6
7# The GNU C Library is free software; you can redistribute it and/or
8# modify it under the terms of the GNU Lesser General Public
9# License as published by the Free Software Foundation; either
10# version 2.1 of the License, or (at your option) any later version.
11
12# The GNU C Library is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15# Lesser General Public License for more details.
16
17# You should have received a copy of the GNU Lesser General Public
18# License along with the GNU C Library; if not, see
19# <http://www.gnu.org/licenses/>.
20
21# See sysdeps/nacl/Makefile for how this script is invoked.
22READELF="$1"
23binary="$2"
24
25if [ -z "$NACL_SDK_ROOT" ]; then
26 echo >&2 "$0: NACL_SDK_ROOT must be set in the environment"
27 exit 77
28fi
29
30ncval="${NACL_SDK_ROOT}/tools/ncval"
31
32if [ ! -x "$ncval" ]; then
33 echo >&2 "$0: No ncval binary in $ncval"
34 exit 77
35fi
36
37"${READELF}" -Wl "$binary" | awk '
38BEGIN { saw_load = saw_text = 0 }
39$1 == "LOAD" {
40 saw_load = 1;
41 if (/ R.E /) saw_code = 1;
42}
43END {
44 exit (saw_code ? 11 : saw_load ? 22 : 1);
45}
46'
47case $? in
4811)
49 # We saw a code segment, so we can try ncval.
50 ;;
5122)
52 # We saw LOAD segments but none of them were code.
53 echo >&2 "+++ No code: $binary"
54 exit 0
55 ;;
56*)
57 # Something funny going on.
58 echo >&2 "*** Failed to analyze: $binary"
59 exit 2
60 ;;
61esac
62
63if "$ncval" "$binary"; then
64 echo >&2 "+++ Validated: $binary"
65 exit 0
66else
67 echo >&2 "*** Validation failed: $binary"
68 exit 2
69fi