blob: 1e1ec8c292bbbe59242ddc2f79d97e0c4fdc87b6 [file] [log] [blame]
Manuel Pégourié-Gonnard0d0a1042021-09-22 12:15:27 +02001#!/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#
valerio0b048642023-04-20 13:28:39 +020020# 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é-Gonnard0d0a1042021-09-22 12:15:27 +020023#
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.)
valerio0b048642023-04-20 13:28:39 +020027# - 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é-Gonnard0d0a1042021-09-22 12:15:27 +020034
35set -eu
36
37# list mbedtls_ symbols of a given type in a static library
38syms() {
39 TYPE="$1"
40 FILE="$2"
41
42 nm "$FILE" | sed -n "s/[0-9a-f ]*${TYPE} \(mbedtls_.*\)/\1/p" | sort -u
43}
44
valerio0b048642023-04-20 13:28:39 +020045# Check if the provided name refers to a module or library and return the
46# same path with proper extension
47get_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é-Gonnard0d0a1042021-09-22 12:15:27 +020056# create listings for the given library
57list() {
58 NAME="$1"
valerio0b048642023-04-20 13:28:39 +020059 FILE=$(get_file_with_extension "library/${NAME}")
Manuel Pégourié-Gonnard0d0a1042021-09-22 12:15:27 +020060 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
73CONFIG="${1:-unknown}"
74
valerio0b048642023-04-20 13:28:39 +020075# List of modules to check is provided as parameters
76if [ $# -gt 1 ]; then
77 shift 1
78 ITEMS_TO_CHECK="$@"
79else
80 ITEMS_TO_CHECK="libmbedx509 libmbedtls"
81fi
82
83for ITEM in $ITEMS_TO_CHECK; do
84 list $ITEM
85done