Manuel Pégourié-Gonnard | 0d0a104 | 2021-09-22 12:15:27 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright The Mbed TLS Contributors |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | # Purpose |
| 19 | # |
valerio | 0b04864 | 2023-04-20 13:28:39 +0200 | [diff] [blame] | 20 | # Show external links in built libraries (X509 or TLS) or modules. This is |
| 21 | # usually done to list Crypto dependencies or to check modules' |
| 22 | # interdependencies. |
Manuel Pégourié-Gonnard | 0d0a104 | 2021-09-22 12:15:27 +0200 | [diff] [blame] | 23 | # |
| 24 | # Usage: |
| 25 | # - build the library with debug symbols and the config you're interested in |
| 26 | # (default, full minus MBEDTLS_USE_PSA_CRYPTO, full, etc.) |
valerio | 0b04864 | 2023-04-20 13:28:39 +0200 | [diff] [blame] | 27 | # - launch this script with 1 or more arguments depending on the analysis' goal: |
| 28 | # - if only 1 argument is used (which is the name of the used config, |
| 29 | # ex: full), then the analysis is done on libmbedx509 and libmbedtls |
| 30 | # libraries by default |
| 31 | # - if multiple arguments are provided, then modules' names (ex: pk, |
| 32 | # pkparse, pkwrite, etc) are expected after the 1st one and the analysis |
| 33 | # will be done on those modules instead of the libraries. |
Manuel Pégourié-Gonnard | 0d0a104 | 2021-09-22 12:15:27 +0200 | [diff] [blame] | 34 | |
| 35 | set -eu |
| 36 | |
| 37 | # list mbedtls_ symbols of a given type in a static library |
| 38 | syms() { |
| 39 | TYPE="$1" |
| 40 | FILE="$2" |
| 41 | |
| 42 | nm "$FILE" | sed -n "s/[0-9a-f ]*${TYPE} \(mbedtls_.*\)/\1/p" | sort -u |
| 43 | } |
| 44 | |
valerio | 0b04864 | 2023-04-20 13:28:39 +0200 | [diff] [blame] | 45 | # Check if the provided name refers to a module or library and return the |
| 46 | # same path with proper extension |
| 47 | get_file_with_extension() { |
| 48 | BASE=$1 |
| 49 | if [ -f $BASE.o ]; then |
| 50 | echo $BASE.o |
| 51 | elif [ -f $BASE.a ]; then |
| 52 | echo $BASE.a |
| 53 | fi |
| 54 | } |
| 55 | |
Manuel Pégourié-Gonnard | 0d0a104 | 2021-09-22 12:15:27 +0200 | [diff] [blame] | 56 | # create listings for the given library |
| 57 | list() { |
| 58 | NAME="$1" |
valerio | 0b04864 | 2023-04-20 13:28:39 +0200 | [diff] [blame] | 59 | FILE=$(get_file_with_extension "library/${NAME}") |
Manuel Pégourié-Gonnard | 0d0a104 | 2021-09-22 12:15:27 +0200 | [diff] [blame] | 60 | PREF="${CONFIG}-$NAME" |
| 61 | |
| 62 | syms '[TRrD]' $FILE > ${PREF}-defined |
| 63 | syms U $FILE > ${PREF}-unresolved |
| 64 | |
| 65 | diff ${PREF}-defined ${PREF}-unresolved \ |
| 66 | | sed -n 's/^> //p' > ${PREF}-external |
| 67 | sed 's/mbedtls_\([^_]*\).*/\1/' ${PREF}-external \ |
| 68 | | uniq -c | sort -rn > ${PREF}-modules |
| 69 | |
| 70 | rm ${PREF}-defined ${PREF}-unresolved |
| 71 | } |
| 72 | |
| 73 | CONFIG="${1:-unknown}" |
| 74 | |
valerio | 0b04864 | 2023-04-20 13:28:39 +0200 | [diff] [blame] | 75 | # List of modules to check is provided as parameters |
| 76 | if [ $# -gt 1 ]; then |
| 77 | shift 1 |
| 78 | ITEMS_TO_CHECK="$@" |
| 79 | else |
| 80 | ITEMS_TO_CHECK="libmbedx509 libmbedtls" |
| 81 | fi |
| 82 | |
| 83 | for ITEM in $ITEMS_TO_CHECK; do |
| 84 | list $ITEM |
| 85 | done |