1#!/bin/bash 2 3# 4# Copyright 2024, The Android Open Source Project 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may 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, 14# WITHOUT 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# 19 20# Usage 21# ./get_padded_memsize.sh <TINYSYS/SLPI/QSH> <obj_name> 22 23# Quit if any command produces an error. 24set -e 25 26# Parse variables 27PLATFORM=$1 28PLATFORM_NOT_CORRECT_STR="You must specify the platform being analyzed. Should be TINYSYS, QSH, or SLPI" 29: ${PLATFORM:?$PLATFORM_NOT_CORRECT_STR} 30if [ "$PLATFORM" != "TINYSYS" ] && [ "$PLATFORM" != "SLPI" ] && [ "$PLATFORM" != "QSH" ]; then 31 echo $PLATFORM_NOT_CORRECT_STR 32 exit 1 33fi 34 35OBJ=$2 36: ${OBJ:?"You must specify the .so to size."} 37 38# Setup required paths and obtain segments 39if [ "$PLATFORM" == "TINYSYS" ]; then 40 : ${RISCV_TOOLCHAIN_PATH}:?"Set RISCV_TOOLCHAIN_PATH, e.g. prebuilts/clang/md32rv/linux-x86" 41 READELF_PATH="$RISCV_TOOLCHAIN_PATH/bin/llvm-readelf" 42elif [ "$PLATFORM" == "SLPI" ] || [ "$PLATFORM" == "QSH" ]; then 43 : ${HEXAGON_TOOLS_PREFIX:?"Set HEXAGON_TOOLS_PREFIX, e.g. export HEXAGON_TOOLS_PREFIX=\$HOME/Qualcomm/HEXAGON_Tools/8.1.04"} 44 READELF_PATH="$HEXAGON_TOOLS_PREFIX/Tools/bin/hexagon-readelf" 45else 46 READELF_PATH="readelf" 47fi 48 49SEGMENTS="$($READELF_PATH -l $OBJ | grep LOAD)" 50 51# Save current IFS to restore later. 52CURR_IFS=$IFS 53 54printf "\n$OBJ\n" 55TOTAL=0 56IFS=$'\n' 57for LINE in $SEGMENTS; do 58 # Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align 59 IFS=" " HEADERS=(${LINE}) 60 LEN=${#HEADERS[@]} 61 62 MEMSIZE=$(( HEADERS[5] )) 63 # Flg can have a space in it, 'R E', for example. 64 ALIGN=$(( HEADERS[LEN - 1] )) 65 # Rounded up to the next integral multiple of Align. 66 QUOTIENT=$(( (MEMSIZE + ALIGN - 1) / ALIGN )) 67 PADDED=$(( ALIGN * QUOTIENT )) 68 PADDING=$(( PADDED - MEMSIZE )) 69 70 printf ' MemSize:0x%x Align:0x%x Padded:0x%x Padding:%d\n' $MEMSIZE $ALIGN $PADDED $PADDING 71 TOTAL=$(( TOTAL + PADDED )) 72done 73 74IFS=$CURR_IFS 75printf 'Total Padded MemSize: 0x%x (%d)\n' $TOTAL $TOTAL 76 77