Manuel Pégourié-Gonnard | 0d0a104 | 2021-09-22 12:15:27 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Manuel Pégourié-Gonnard | 0d0a104 | 2021-09-22 12:15:27 +0200 | [diff] [blame] | 5 | # |
| 6 | # Purpose |
| 7 | # |
valerio | 0b04864 | 2023-04-20 13:28:39 +0200 | [diff] [blame] | 8 | # Show external links in built libraries (X509 or TLS) or modules. This is |
| 9 | # usually done to list Crypto dependencies or to check modules' |
| 10 | # interdependencies. |
Manuel Pégourié-Gonnard | 0d0a104 | 2021-09-22 12:15:27 +0200 | [diff] [blame] | 11 | # |
| 12 | # Usage: |
| 13 | # - build the library with debug symbols and the config you're interested in |
Janos Follath | bd445af | 2024-12-02 14:44:28 +0000 | [diff] [blame] | 14 | # (default, full, etc.) |
valerio | 0b04864 | 2023-04-20 13:28:39 +0200 | [diff] [blame] | 15 | # - launch this script with 1 or more arguments depending on the analysis' goal: |
| 16 | # - if only 1 argument is used (which is the name of the used config, |
| 17 | # ex: full), then the analysis is done on libmbedx509 and libmbedtls |
| 18 | # libraries by default |
| 19 | # - if multiple arguments are provided, then modules' names (ex: pk, |
| 20 | # pkparse, pkwrite, etc) are expected after the 1st one and the analysis |
| 21 | # will be done on those modules instead of the libraries. |
Manuel Pégourié-Gonnard | 0d0a104 | 2021-09-22 12:15:27 +0200 | [diff] [blame] | 22 | |
| 23 | set -eu |
| 24 | |
| 25 | # list mbedtls_ symbols of a given type in a static library |
| 26 | syms() { |
| 27 | TYPE="$1" |
| 28 | FILE="$2" |
| 29 | |
| 30 | nm "$FILE" | sed -n "s/[0-9a-f ]*${TYPE} \(mbedtls_.*\)/\1/p" | sort -u |
| 31 | } |
| 32 | |
valerio | 0b04864 | 2023-04-20 13:28:39 +0200 | [diff] [blame] | 33 | # Check if the provided name refers to a module or library and return the |
| 34 | # same path with proper extension |
| 35 | get_file_with_extension() { |
| 36 | BASE=$1 |
| 37 | if [ -f $BASE.o ]; then |
| 38 | echo $BASE.o |
| 39 | elif [ -f $BASE.a ]; then |
| 40 | echo $BASE.a |
| 41 | fi |
| 42 | } |
| 43 | |
Manuel Pégourié-Gonnard | 0d0a104 | 2021-09-22 12:15:27 +0200 | [diff] [blame] | 44 | # create listings for the given library |
| 45 | list() { |
| 46 | NAME="$1" |
valerio | 0b04864 | 2023-04-20 13:28:39 +0200 | [diff] [blame] | 47 | FILE=$(get_file_with_extension "library/${NAME}") |
Manuel Pégourié-Gonnard | 0d0a104 | 2021-09-22 12:15:27 +0200 | [diff] [blame] | 48 | PREF="${CONFIG}-$NAME" |
| 49 | |
| 50 | syms '[TRrD]' $FILE > ${PREF}-defined |
| 51 | syms U $FILE > ${PREF}-unresolved |
| 52 | |
| 53 | diff ${PREF}-defined ${PREF}-unresolved \ |
| 54 | | sed -n 's/^> //p' > ${PREF}-external |
| 55 | sed 's/mbedtls_\([^_]*\).*/\1/' ${PREF}-external \ |
| 56 | | uniq -c | sort -rn > ${PREF}-modules |
| 57 | |
| 58 | rm ${PREF}-defined ${PREF}-unresolved |
| 59 | } |
| 60 | |
| 61 | CONFIG="${1:-unknown}" |
| 62 | |
valerio | 0b04864 | 2023-04-20 13:28:39 +0200 | [diff] [blame] | 63 | # List of modules to check is provided as parameters |
| 64 | if [ $# -gt 1 ]; then |
| 65 | shift 1 |
| 66 | ITEMS_TO_CHECK="$@" |
| 67 | else |
| 68 | ITEMS_TO_CHECK="libmbedx509 libmbedtls" |
| 69 | fi |
| 70 | |
| 71 | for ITEM in $ITEMS_TO_CHECK; do |
| 72 | list $ITEM |
| 73 | done |