xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | # Script to validate NaCl binaries after linking. |
| 3 | |
| 4 | # Copyright (C) 2015-2016 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. |
| 22 | READELF="$1" |
| 23 | binary="$2" |
| 24 | |
| 25 | if [ -z "$NACL_SDK_ROOT" ]; then |
| 26 | echo >&2 "$0: NACL_SDK_ROOT must be set in the environment" |
| 27 | exit 77 |
| 28 | fi |
| 29 | |
| 30 | ncval="${NACL_SDK_ROOT}/tools/ncval" |
| 31 | |
| 32 | if [ ! -x "$ncval" ]; then |
| 33 | echo >&2 "$0: No ncval binary in $ncval" |
| 34 | exit 77 |
| 35 | fi |
| 36 | |
| 37 | "${READELF}" -Wl "$binary" | awk ' |
| 38 | BEGIN { saw_load = saw_text = 0 } |
| 39 | $1 == "LOAD" { |
| 40 | saw_load = 1; |
| 41 | if (/ R.E /) saw_code = 1; |
| 42 | } |
| 43 | END { |
| 44 | exit (saw_code ? 11 : saw_load ? 22 : 1); |
| 45 | } |
| 46 | ' |
| 47 | case $? in |
| 48 | 11) |
| 49 | # We saw a code segment, so we can try ncval. |
| 50 | ;; |
| 51 | 22) |
| 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 | ;; |
| 61 | esac |
| 62 | |
| 63 | if "$ncval" "$binary"; then |
| 64 | echo >&2 "+++ Validated: $binary" |
| 65 | exit 0 |
| 66 | else |
| 67 | echo >&2 "*** Validation failed: $binary" |
| 68 | exit 2 |
| 69 | fi |