2019-08-10 19:16:33 +02:00
|
|
|
#!/usr/bin/env bash
|
2018-07-15 19:21:53 +02:00
|
|
|
############################################################################
|
|
|
|
# apps/tools/mksymtab.sh
|
|
|
|
#
|
2021-06-10 07:37:15 +02:00
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership. The
|
|
|
|
# ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
# "License"); you may not use this file except in compliance with the
|
|
|
|
# License. You may obtain a copy of the License at
|
2018-07-15 19:21:53 +02:00
|
|
|
#
|
2021-06-10 07:37:15 +02:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2018-07-15 19:21:53 +02:00
|
|
|
#
|
2021-06-10 07:37:15 +02:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
2018-07-15 19:21:53 +02:00
|
|
|
#
|
|
|
|
############################################################################
|
|
|
|
|
2019-01-27 14:42:49 +01:00
|
|
|
export LC_ALL=C
|
|
|
|
|
tools/mksymtab.sh: Support adding additional symbols that are not in undefined list
e.g. For CHRE dynamic nanoapps, pre-saving symbols that may be used later, no need recompiling.
Test: (unique & sorted)
$ nm ./hello_world.o | grep " U "
U __aeabi_unwind_cpp_pr1
U chreGetTime
U chreGetVersion
U chreLog
U __stack_chk_fail
U __stack_chk_guard
$ cat additional.txt -n
1 test_symbol
2 test_symbol
$ /PATH/TO/APPS/tools/mksymtab.sh ./hello_world.o MY_PREFIX -a additional.txt
#include <nuttx/compiler.h>
#include <nuttx/symtab.h>
extern void *__aeabi_unwind_cpp_pr1;
extern void *__stack_chk_fail;
extern void *__stack_chk_guard;
extern void *chreGetTime;
extern void *chreGetVersion;
extern void *chreLog;
extern void *test_symbol;
const struct symtab_s MY_PREFIX_exports[] =
{
{"__aeabi_unwind_cpp_pr1", &__aeabi_unwind_cpp_pr1},
{"__stack_chk_fail", &__stack_chk_fail},
{"__stack_chk_guard", &__stack_chk_guard},
{"chreGetTime", &chreGetTime},
{"chreGetVersion", &chreGetVersion},
{"chreLog", &chreLog},
{"test_symbol", &test_symbol},
};
const int MY_PREFIX_nexports = sizeof(MY_PREFIX_exports) / sizeof(struct symtab_s);
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-03-27 04:04:45 +01:00
|
|
|
usage="Usage: $0 <imagedirpath> [symtabprefix [additionalsymbolspath]]"
|
2018-07-15 19:21:53 +02:00
|
|
|
|
|
|
|
# Check for the required directory path
|
|
|
|
|
|
|
|
dir=$1
|
|
|
|
if [ -z "$dir" ]; then
|
|
|
|
echo "ERROR: Missing <imagedirpath>"
|
|
|
|
echo ""
|
|
|
|
echo $usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-05-23 14:46:57 +02:00
|
|
|
# Get the symbol table prefix
|
2018-07-15 19:21:53 +02:00
|
|
|
|
2020-05-23 14:46:57 +02:00
|
|
|
prefix=$2
|
tools/mksymtab.sh: Support adding additional symbols that are not in undefined list
e.g. For CHRE dynamic nanoapps, pre-saving symbols that may be used later, no need recompiling.
Test: (unique & sorted)
$ nm ./hello_world.o | grep " U "
U __aeabi_unwind_cpp_pr1
U chreGetTime
U chreGetVersion
U chreLog
U __stack_chk_fail
U __stack_chk_guard
$ cat additional.txt -n
1 test_symbol
2 test_symbol
$ /PATH/TO/APPS/tools/mksymtab.sh ./hello_world.o MY_PREFIX -a additional.txt
#include <nuttx/compiler.h>
#include <nuttx/symtab.h>
extern void *__aeabi_unwind_cpp_pr1;
extern void *__stack_chk_fail;
extern void *__stack_chk_guard;
extern void *chreGetTime;
extern void *chreGetVersion;
extern void *chreLog;
extern void *test_symbol;
const struct symtab_s MY_PREFIX_exports[] =
{
{"__aeabi_unwind_cpp_pr1", &__aeabi_unwind_cpp_pr1},
{"__stack_chk_fail", &__stack_chk_fail},
{"__stack_chk_guard", &__stack_chk_guard},
{"chreGetTime", &chreGetTime},
{"chreGetVersion", &chreGetVersion},
{"chreLog", &chreLog},
{"test_symbol", &test_symbol},
};
const int MY_PREFIX_nexports = sizeof(MY_PREFIX_exports) / sizeof(struct symtab_s);
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-03-27 04:04:45 +01:00
|
|
|
add_sym=$3
|
2018-07-15 19:21:53 +02:00
|
|
|
|
|
|
|
# Extract all of the undefined symbols from the ELF files and create a
|
|
|
|
# list of sorted, unique undefined variable names.
|
|
|
|
|
2020-05-23 14:46:57 +02:00
|
|
|
varlist=`find $dir -name *-thunk.S 2>/dev/null | xargs grep -h asciz | cut -f3 | sort | uniq`
|
|
|
|
if [ -z "$varlist" ]; then
|
2021-09-29 05:17:47 +02:00
|
|
|
execlist=`find $dir -type f 2>/dev/null`
|
2020-05-23 14:46:57 +02:00
|
|
|
if [ ! -z "$execlist" ]; then
|
2024-03-22 09:03:35 +01:00
|
|
|
|
|
|
|
# Get all undefined symbol names
|
2021-09-29 05:17:47 +02:00
|
|
|
varlist=`nm $execlist 2>/dev/null | fgrep ' U ' | sed -e "s/^[ ]*//g" | cut -d' ' -f2 | sort | uniq`
|
2024-03-22 09:03:35 +01:00
|
|
|
|
|
|
|
# Get all defined symbol names
|
|
|
|
deflist=`nm $execlist 2>/dev/null | fgrep -v -e ' U ' -e ':' | sed -e "s/^[0-9a-z]* //g" | cut -d' ' -f2 | sort | uniq`
|
|
|
|
|
|
|
|
# Remove the intersection between them, and the remaining symbols are found in the main image
|
|
|
|
common=`echo "$varlist" | tr ' ' '\n' | grep -Fxf <(echo "$deflist" | tr ' ' '\n') | tr '\n' ' '`
|
2024-03-29 13:30:43 +01:00
|
|
|
if [ "x$common" != "x" ]; then
|
|
|
|
varlist=`echo $varlist | sed "s/$common//g"`
|
|
|
|
fi
|
2020-05-23 14:46:57 +02:00
|
|
|
fi
|
2018-09-04 19:20:19 +02:00
|
|
|
fi
|
2018-07-15 19:21:53 +02:00
|
|
|
|
tools/mksymtab.sh: Support adding additional symbols that are not in undefined list
e.g. For CHRE dynamic nanoapps, pre-saving symbols that may be used later, no need recompiling.
Test: (unique & sorted)
$ nm ./hello_world.o | grep " U "
U __aeabi_unwind_cpp_pr1
U chreGetTime
U chreGetVersion
U chreLog
U __stack_chk_fail
U __stack_chk_guard
$ cat additional.txt -n
1 test_symbol
2 test_symbol
$ /PATH/TO/APPS/tools/mksymtab.sh ./hello_world.o MY_PREFIX -a additional.txt
#include <nuttx/compiler.h>
#include <nuttx/symtab.h>
extern void *__aeabi_unwind_cpp_pr1;
extern void *__stack_chk_fail;
extern void *__stack_chk_guard;
extern void *chreGetTime;
extern void *chreGetVersion;
extern void *chreLog;
extern void *test_symbol;
const struct symtab_s MY_PREFIX_exports[] =
{
{"__aeabi_unwind_cpp_pr1", &__aeabi_unwind_cpp_pr1},
{"__stack_chk_fail", &__stack_chk_fail},
{"__stack_chk_guard", &__stack_chk_guard},
{"chreGetTime", &chreGetTime},
{"chreGetVersion", &chreGetVersion},
{"chreLog", &chreLog},
{"test_symbol", &test_symbol},
};
const int MY_PREFIX_nexports = sizeof(MY_PREFIX_exports) / sizeof(struct symtab_s);
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-03-27 04:04:45 +01:00
|
|
|
if [ "x$add_sym" != "x" ]; then
|
|
|
|
if [ -f $add_sym ]; then
|
2024-04-25 13:55:32 +02:00
|
|
|
varlist="${varlist}\n$(cat $add_sym | grep -v "^,.*")"
|
tools/mksymtab.sh: Support adding additional symbols that are not in undefined list
e.g. For CHRE dynamic nanoapps, pre-saving symbols that may be used later, no need recompiling.
Test: (unique & sorted)
$ nm ./hello_world.o | grep " U "
U __aeabi_unwind_cpp_pr1
U chreGetTime
U chreGetVersion
U chreLog
U __stack_chk_fail
U __stack_chk_guard
$ cat additional.txt -n
1 test_symbol
2 test_symbol
$ /PATH/TO/APPS/tools/mksymtab.sh ./hello_world.o MY_PREFIX -a additional.txt
#include <nuttx/compiler.h>
#include <nuttx/symtab.h>
extern void *__aeabi_unwind_cpp_pr1;
extern void *__stack_chk_fail;
extern void *__stack_chk_guard;
extern void *chreGetTime;
extern void *chreGetVersion;
extern void *chreLog;
extern void *test_symbol;
const struct symtab_s MY_PREFIX_exports[] =
{
{"__aeabi_unwind_cpp_pr1", &__aeabi_unwind_cpp_pr1},
{"__stack_chk_fail", &__stack_chk_fail},
{"__stack_chk_guard", &__stack_chk_guard},
{"chreGetTime", &chreGetTime},
{"chreGetVersion", &chreGetVersion},
{"chreLog", &chreLog},
{"test_symbol", &test_symbol},
};
const int MY_PREFIX_nexports = sizeof(MY_PREFIX_exports) / sizeof(struct symtab_s);
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-03-27 04:04:45 +01:00
|
|
|
elif [ -d $add_sym ]; then
|
2024-04-25 13:55:32 +02:00
|
|
|
varlist="${varlist}\n$(find $add_sym -type f | xargs cat | grep -v "^,.*")"
|
tools/mksymtab.sh: Support adding additional symbols that are not in undefined list
e.g. For CHRE dynamic nanoapps, pre-saving symbols that may be used later, no need recompiling.
Test: (unique & sorted)
$ nm ./hello_world.o | grep " U "
U __aeabi_unwind_cpp_pr1
U chreGetTime
U chreGetVersion
U chreLog
U __stack_chk_fail
U __stack_chk_guard
$ cat additional.txt -n
1 test_symbol
2 test_symbol
$ /PATH/TO/APPS/tools/mksymtab.sh ./hello_world.o MY_PREFIX -a additional.txt
#include <nuttx/compiler.h>
#include <nuttx/symtab.h>
extern void *__aeabi_unwind_cpp_pr1;
extern void *__stack_chk_fail;
extern void *__stack_chk_guard;
extern void *chreGetTime;
extern void *chreGetVersion;
extern void *chreLog;
extern void *test_symbol;
const struct symtab_s MY_PREFIX_exports[] =
{
{"__aeabi_unwind_cpp_pr1", &__aeabi_unwind_cpp_pr1},
{"__stack_chk_fail", &__stack_chk_fail},
{"__stack_chk_guard", &__stack_chk_guard},
{"chreGetTime", &chreGetTime},
{"chreGetVersion", &chreGetVersion},
{"chreLog", &chreLog},
{"test_symbol", &test_symbol},
};
const int MY_PREFIX_nexports = sizeof(MY_PREFIX_exports) / sizeof(struct symtab_s);
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
2024-03-27 04:04:45 +01:00
|
|
|
else
|
|
|
|
echo $usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
varlist=$(echo -e "${varlist}" | sort -u)
|
|
|
|
fi
|
|
|
|
|
2018-07-15 19:21:53 +02:00
|
|
|
# Now output the symbol table as a structure in a C source file. All
|
|
|
|
# undefined symbols are declared as void* types. If the toolchain does
|
|
|
|
# any kind of checking for function vs. data objects, then this could
|
2020-02-23 05:51:44 +01:00
|
|
|
# failed
|
2018-07-15 19:21:53 +02:00
|
|
|
|
2020-05-23 14:46:57 +02:00
|
|
|
echo "#include <nuttx/compiler.h>"
|
|
|
|
echo "#include <nuttx/symtab.h>"
|
|
|
|
echo ""
|
2018-07-15 19:21:53 +02:00
|
|
|
|
2020-06-10 13:36:59 +02:00
|
|
|
for string in $varlist; do
|
|
|
|
var=`echo $string | sed -e "s/\"//g"`
|
2024-04-25 13:55:32 +02:00
|
|
|
echo "extern void *${var/,*/};"
|
2018-07-15 19:21:53 +02:00
|
|
|
done
|
|
|
|
|
2020-05-23 14:46:57 +02:00
|
|
|
echo ""
|
|
|
|
if [ -z "$prefix" ]; then
|
|
|
|
echo "#if defined(CONFIG_EXECFUNCS_HAVE_SYMTAB)"
|
|
|
|
echo "const struct symtab_s CONFIG_EXECFUNCS_SYMTAB_ARRAY[] = "
|
2022-10-15 12:19:09 +02:00
|
|
|
echo "#elif defined(CONFIG_NSH_SYMTAB)"
|
|
|
|
echo "const struct symtab_s CONFIG_NSH_SYMTAB_ARRAYNAME[] = "
|
2020-05-23 14:46:57 +02:00
|
|
|
echo "#else"
|
|
|
|
echo "const struct symtab_s dummy_symtab[] = "
|
|
|
|
echo "#endif"
|
|
|
|
else
|
|
|
|
echo "const struct symtab_s ${prefix}_exports[] = "
|
|
|
|
fi
|
|
|
|
echo "{"
|
2018-07-15 19:21:53 +02:00
|
|
|
|
2020-06-10 13:36:59 +02:00
|
|
|
for string in $varlist; do
|
|
|
|
var=`echo $string | sed -e "s/\"//g"`
|
2024-04-25 13:55:32 +02:00
|
|
|
echo " {\"${var/*,/}\", &${var/,*/}},"
|
2018-07-15 19:21:53 +02:00
|
|
|
done
|
|
|
|
|
2020-05-23 14:46:57 +02:00
|
|
|
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);"
|
2022-10-15 12:19:09 +02:00
|
|
|
echo "#elif defined(CONFIG_NSH_SYMTAB)"
|
|
|
|
echo "const int CONFIG_NSH_SYMTAB_COUNTNAME = sizeof(CONFIG_NSH_SYMTAB_ARRAYNAME) / sizeof(struct symtab_s);"
|
2020-05-23 14:46:57 +02:00
|
|
|
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
|