#!/bin/bash

#
# Copyright (C) 2016 The Android Open Source Project
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

# This shell-script checks the symbols in libavb.a and fails
# if a reference not starting with avb_ is referenced. It's intended
# to catch mistakes where the standard C library is inadvertently
# used.

set -e

SYMBOLS_FILE=$(mktemp /tmp/libavb_symbols.XXXXXXXXXX)

trap "rm -f '${SYMBOLS_FILE}'" EXIT

readelf --symbols --wide "${ANDROID_HOST_OUT}/obj/STATIC_LIBRARIES/libavb_intermediates/libavb.a" | \
  awk '$7 == "UND" && $8 != "" {print $8}' | \
  grep -v ^avb_ | \
  sort -u > "${SYMBOLS_FILE}"

# If this file is non-empty, it means that the library is using
# symbols not starting with "avb_".
if [ -s "${SYMBOLS_FILE}" ] ; then
  echo "ERROR: $0: Unexpected symbols in libavb:" >&2
  cat "${SYMBOLS_FILE}" >&2
  exit 1
fi
