#!/usr/bin/env bash
export LC_ALL=C
usage() {
if [ $# -ne 0 ]; then
echo "ERROR: $@"
fi
echo -e "\nUsage: $0 <imagedirpath> [symtabprefix] [-a additionalsymbolspath]"
exit 1
}
dir=$1
if [ -z "$dir" ]; then
usage "Missing <imagedirpath>"
fi
if [ "x${2:0:1}" != "x-" ]; then
prefix=$2
OPTIND=3
else
OPTIND=2
fi
while getopts a: opt; do
case $opt in
a)
addlist="${addlist[@]} $OPTARG"
;;
\?)
usage
esac
done
if [ $OPTIND != $(($# + 1)) ]; then
usage "Arguments remaining: \"${@:$OPTIND}\""
fi
varlist=`find $dir -name *-thunk.S 2>/dev/null | xargs grep -h asciz | cut -f3 | sort | uniq`
if [ -z "$varlist" ]; then
execlist=`find $dir -type f 2>/dev/null`
if [ ! -z "$execlist" ]; then
varlist=`nm $execlist 2>/dev/null | fgrep ' U ' | sed -e "s/^[ ]*//g" | cut -d' ' -f2 | sort | uniq`
deflist=`nm $execlist 2>/dev/null | fgrep -v -e ' U ' -e ':' | sed -e "s/^[0-9a-z]* //g" | cut -d' ' -f2 | sort | uniq`
common=`echo "$varlist" | tr ' ' '\n' | grep -Fxf <(echo "$deflist" | tr ' ' '\n') | tr '\n' ' '`
if [ "x$common" != "x" ]; then
varlist=`echo $varlist | sed "s/$common//g"`
fi
fi
fi
for addsym in ${addlist[@]}; do
if [ -f $addsym ]; then
varlist="${varlist}\n$(cat $addsym | grep -v "^,.*")"
elif [ -d $addsym ]; then
varlist="${varlist}\n$(find $addsym -type f | xargs cat | grep -v "^,.*")"
else
usage
fi
varlist=$(echo -e "${varlist}" | sort -u)
done
echo "#include <nuttx/compiler.h>"
echo "#include <nuttx/symtab.h>"
echo ""
for string in $varlist; do
var=`echo $string | sed -e "s/\"//g"`
echo "extern void *${var/,*/};"
done
echo ""
if [ -z "$prefix" ]; then
echo "#if defined(CONFIG_EXECFUNCS_HAVE_SYMTAB)"
echo "const struct symtab_s CONFIG_EXECFUNCS_SYMTAB_ARRAY[] = "
echo "#elif defined(CONFIG_NSH_SYMTAB)"
echo "const struct symtab_s CONFIG_NSH_SYMTAB_ARRAYNAME[] = "
echo "#elif defined(CONFIG_LIBC_ELF_HAVE_SYMTAB)"
echo "const struct symtab_s CONFIG_LIBC_ELF_SYMTAB_ARRAY[] = "
echo "#else"
echo "const struct symtab_s dummy_symtab[] = "
echo "#endif"
else
echo "const struct symtab_s ${prefix}_exports[] = "
fi
echo "{"
for string in $varlist; do
var=`echo $string | sed -e "s/\"//g"`
echo " {\"${var/*,/}\", &${var/,*/}},"
done
echo "};"
echo ""
if [ -z "$prefix" ]; then
echo "#if defined(CONFIG_EXECFUNCS_HAVE_SYMTAB)"
echo "const int CONFIG_EXECFUNCS_NSYMBOLS_VAR = sizeof(CONFIG_EXECFUNCS_SYMTAB_ARRAY) / sizeof(struct symtab_s);"
echo "#elif defined(CONFIG_NSH_SYMTAB)"
echo "const int CONFIG_NSH_SYMTAB_COUNTNAME = sizeof(CONFIG_NSH_SYMTAB_ARRAYNAME) / sizeof(struct symtab_s);"
echo "#elif defined(CONFIG_LIBC_ELF_HAVE_SYMTAB)"
echo "const int CONFIG_LIBC_ELF_NSYMBOLS_VAR = sizeof(CONFIG_LIBC_ELF_SYMTAB_ARRAY) / sizeof(struct symtab_s);"
echo "#else"
echo "const int dummy_nsymtabs = sizeof(dummy_symtab) / sizeof(struct symtab_s);"
echo "#endif"
else
echo "const int ${prefix}_nexports = sizeof(${prefix}_exports) / sizeof(struct symtab_s);"
fi