@@ -27,28 +27,43 @@ usage() {
2727 if [ $# -ne 0 ]; then
2828 echo " ERROR: $@ "
2929 fi
30- echo -e " \nUsage: $0 <imagedirpath> [symtabprefix] [-a additionalsymbolspath]"
30+ echo -e " \nUsage: $0 <imagedirpath1> [imagedirpath2 ... ] [symtabprefix] [-a additionalsymbolspath]"
3131 exit 1
3232}
3333
34- # Check for the required directory path
34+ # Collect all image directory/file paths until we hit a non-path argument
35+ dirs=()
36+ prefix=" "
37+ arg_count=0
3538
36- dir=$1
37- if [ -z " $dir " ]; then
38- usage " Missing <imagedirpath>"
39- fi
39+ while [ $# -gt 0 ]; do
40+ # Check if this is an option flag
41+ if [ " x${1: 0: 1} " = " x-" ]; then
42+ break
43+ fi
4044
41- # Get the symbol table prefix
45+ arg_count= $(( arg_count + 1 ))
4246
43- if [ " x${2: 0: 1} " != " x-" ]; then
44- prefix=$2
45- OPTIND=3
46- else
47- OPTIND=2
48- fi
47+ # Always collect the argument (whether path exists or not)
48+ # Only treat as prefix if it doesn't look like a path
49+ if [[ " $1 " =~ ^/ ]] || [ -e " $1 " ]; then
50+ # Looks like a path (starts with /) or exists
51+ dirs+=(" $1 " )
52+ shift
53+ else
54+ # Doesn't look like a path, treat as prefix
55+ prefix=$1
56+ shift
57+ break
58+ fi
59+ done
4960
50- # Parse remaining arguments
61+ # Check we have at least one argument
62+ if [ $arg_count -eq 0 ]; then
63+ usage " Missing <imagedirpath>"
64+ fi
5165
66+ # Parse remaining arguments for options
5267while getopts a: opt; do
5368 case $opt in
5469 a)
@@ -63,28 +78,60 @@ if [ $OPTIND != $(($# + 1)) ]; then
6378 usage " Arguments remaining: \" ${@: $OPTIND } \" "
6479fi
6580
81+ # Function to get exec list from a path (file or directory)
82+ get_exec_list () {
83+ local path=$1
84+ if [ -f " $path " ]; then
85+ echo " $path "
86+ elif [ -d " $path " ]; then
87+ find " $path " -type f 2> /dev/null
88+ fi
89+ }
90+
6691# Extract all of the undefined symbols from the ELF files and create a
6792# list of sorted, unique undefined variable names.
6893
69- varlist=` find $dir -name * -thunk.S 2> /dev/null | xargs grep -h asciz | cut -f3 | sort | uniq`
94+ # First try to find thunk files from all directories
95+ varlist=" "
96+ for dir in " ${dirs[@]} " ; do
97+ if [ -d " $dir " ]; then
98+ thunklist=` find $dir -name * -thunk.S 2> /dev/null | xargs grep -h asciz 2> /dev/null | cut -f3`
99+ if [ ! -z " $thunklist " ]; then
100+ varlist=" ${varlist} ${thunklist} "
101+ fi
102+ fi
103+ done
104+
70105if [ -z " $varlist " ]; then
71- execlist=` find $dir -type f 2> /dev/null`
72- if [ ! -z " $execlist " ]; then
106+ # Collect all executable files from all paths
107+ execlist=" "
108+ for dir in " ${dirs[@]} " ; do
109+ # Only process if path exists
110+ if [ -e " $dir " ]; then
111+ pathlist=` get_exec_list " $dir " `
112+ if [ ! -z " $pathlist " ]; then
113+ execlist=" ${execlist} ${pathlist} "
114+ fi
115+ fi
116+ done
73117
74- # Get all undefined symbol names
75- varlist=` nm $execlist 2> /dev/null | grep -F ' U ' | sed -e " s/^[ ]*//g" | cut -d' ' -f2 | sort | uniq`
118+ if [ ! -z " $execlist " ]; then
119+ # Get all undefined symbol names
120+ varlist=$( nm $execlist 2> /dev/null | grep -F ' U ' | sed -e " s/^[ ]*//g" | cut -d' ' -f2)
76121
77- # Get all defined symbol names
78- deflist=` nm $execlist 2> /dev/null | grep -F -v -e ' U ' -e ' :' | sed -e " s/^[0-9a-z]* //g" | cut -d' ' -f2 | sort | uniq `
122+ # Get all defined symbol names
123+ deflist=$( nm $execlist 2> /dev/null | grep -F -v -e ' U ' -e ' :' | sed -e " s/^[0-9a-z]* //g" | cut -d' ' -f2)
79124
80- # Remove the intersection between them, and the remaining symbols are found in the main image
81- common=` echo " $varlist " | tr ' ' ' \n' | grep -Fxf <( echo " $deflist " | tr ' ' ' \n' ) | tr ' \n' ' ' `
82- if [ " x$common " != " x" ]; then
83- varlist=` echo $varlist | sed " s/$common //g" `
125+ # Remove the intersection between them, and the remaining symbols are found in the main image
126+ if [ ! -z " $varlist " ] && [ ! -z " $deflist " ]; then
127+ varlist=$( echo " $varlist " | tr ' ' ' \n' | sort -u | grep -vFxf <( echo " $deflist " | tr ' ' ' \n' | sort -u) | tr ' \n' ' ' )
84128 fi
85129 fi
86130fi
87131
132+ # Sort and unique the varlist
133+ varlist=` echo " $varlist " | tr ' ' ' \n' | sort | uniq | tr ' \n' ' ' `
134+
88135for addsym in ${addlist[@]} ; do
89136 if [ -f $addsym ]; then
90137 varlist=" ${varlist} \n$( cat $addsym | grep -v " ^,.*" ) "
0 commit comments