~aleteoryx/muditaos

muditaos/tools/find_global_data.sh -rwxr-xr-x 1.6 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash 
# Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

# A script to find duplicated initialization code of global data

rootdir="$(realpath $(dirname $(realpath $0))/..)"
objfile="${1:-$(realpath ${rootdir}/build-rt1051-Debug/PurePhone.elf)}"

if [ ! -f ${objfile} ]; then
    echo "No such file: ${objfile}"
    exit 1
fi

type=$( file ${objfile} | grep "ARM" )

if [[ -z "${type}" ]]; then
    echo "x86"
    OBJDUMP="objdump"
    NM="nm"
else
    echo "ARM"
    OBJDUMP="arm-none-eabi-objdump"
    NM="arm-none-eabi-nm"
fi

STATIC_SYMBOL="_Z41__static_initialization_and_destruction_0ii"

staticList=($(${NM} --print-size ${objfile} | grep "${STATIC_SYMBOL}"|cut -f1,2 -d" "|tr " " ":" ))

echo "${#staticList[@]}"

I=0
statcListSize=${#staticList[@]}

searchRegexp='(.*\.hpp:[0-9]+)'
while [[ $I -lt $statcListSize ]]
do
    
    symbol=${staticList[${I}]}
    symbolStart="0x${staticList[${I}]%:*}"
    symbolSize="0x${staticList[${I}]#*:}"
    printf -v symbolEnd "0x%x" $(( ${symbolStart} + ${symbolSize} ))
    echo "${I}/${statcListSize}: ${symbolStart} - ${symbolEnd}"

    items=( $(${OBJDUMP} --start-address=${symbolStart} --stop-address=${symbolEnd} -d -l ${objfile} ) )
    J=0
    while [[ ${J} -lt ${#items[@]} ]]
    do
        item=$( echo ${items[$J]} | cut -f1 -d" ")
        if [[ ${item} =~ ${searchRegexp} ]]; then
            paths="$paths\n${BASH_REMATCH[1]}"
        fi
        J=$(( $J + 1 ))
    done
    I=$(( $I + 1 ))
done

if [[ -n ${paths} ]]; then
    echo -e ${paths} | sort | uniq -c | sort -n
fi