Skip to content

Commit c97bb67

Browse files
anjiahao1extinguish
authored andcommitted
elf:avoid interference between different ELFs generated by symtab
if defined CONFIG_EXAMPLES_ELF and CONFIG_EXAMPLES_MODLUE, elf will generated BINDIR, so generated symtab will interference. It supports inputting multiple specified files in mksymtab.sh to avoid interference. Signed-off-by: anjiahao <[email protected]>
1 parent 69ee29e commit c97bb67

File tree

3 files changed

+97
-32
lines changed

3 files changed

+97
-32
lines changed

examples/elf/main/Makefile

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,32 @@ MAINSRC = elf_main.c
3030
SYMTABSRC = test_symtab.c
3131
SYMTABOBJ = $(SYMTABSRC:.c=$(OBJEXT))
3232

33+
ELFNAME = $(BINDIR)/errno $(BINDIR)/hello
34+
ifneq ($(CONFIG_DISABLE_SIGNALS),y)
35+
ELFNAME += $(BINDIR)/signal
36+
endif
37+
38+
ifeq ($(CONFIG_HAVE_CXX),y)
39+
ELFNAME += $(BINDIR)/hello++1 $(BINDIR)/hello++2
40+
ifeq ($(CONFIG_HAVE_CXXINITIALIZE),y)
41+
ELFNAME += $(BINDIR)/hello++3
42+
endif
43+
ifeq ($(CONFIG_EXAMPLES_ELF_CXX),y)
44+
ELFNAME += $(BINDIR)/hello++4 $(BINDIR)/hello++5
45+
endif
46+
endif
47+
ifeq ($(CONFIG_EXAMPLES_ELF_LONGJMP),y)
48+
ELFNAME += $(BINDIR)/longjmp
49+
endif
50+
ifneq ($(CONFIG_DISABLE_PTHREAD),y)
51+
ELFNAME += $(BINDIR)/pthread $(BINDIR)/mutex
52+
endif
53+
ifneq ($(CONFIG_ARCH_ADDRENV),y)
54+
ELFNAME += $(BINDIR)/task
55+
endif
56+
3357
$(SYMTABSRC):
34-
$(Q) $(APPDIR)$(DELIM)tools$(DELIM)mksymtab.sh $(BINDIR) g_elf >$@.tmp
58+
$(Q) $(APPDIR)$(DELIM)tools$(DELIM)mksymtab.sh $(ELFNAME) g_elf >$@.tmp
3559
$(Q) $(call TESTANDREPLACEFILE, $@.tmp, $@)
3660

3761

examples/module/main/Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@ MAINSRC = module_main.c
3434
SYMTABSRC = mod_symtab.c
3535
SYMTABOBJ = $(SYMTABSRC:.c=$(OBJEXT))
3636

37-
37+
MODLUE_NAME = chardev
3838
ifneq ($(CONFIG_BUILD_FLAT),y)
3939
PASS1_SYMTAB = $(TOPDIR)/pass1/mod_symtab.c
40-
MODLUE_NAME = chardev
4140
endif
4241

4342
$(SYMTABSRC):
44-
$(Q) $(APPDIR)$(DELIM)tools$(DELIM)mksymtab.sh $(BINDIR) g_mod >$@.tmp
43+
$(Q) $(APPDIR)$(DELIM)tools$(DELIM)mksymtab.sh $(BINDIR)$(DELIM)chardev g_mod >$@.tmp
4544
$(Q) $(call TESTANDREPLACEFILE, $@.tmp, $@)
4645

4746

tools/mksymtab.sh

Lines changed: 70 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,38 @@ 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=""
3537

36-
dir=$1
37-
if [ -z "$dir" ]; then
38-
usage "Missing <imagedirpath>"
39-
fi
40-
41-
# Get the symbol table prefix
38+
while [ $# -gt 0 ]; do
39+
# Check if this is an option flag
40+
if [ "x${1:0:1}" = "x-" ]; then
41+
break
42+
fi
43+
44+
# Check if this is a valid path (file or directory)
45+
if [ -e "$1" ]; then
46+
dirs+=("$1")
47+
shift
48+
else
49+
# Not a valid path, treat as prefix
50+
prefix=$1
51+
shift
52+
break
53+
fi
54+
done
4255

43-
if [ "x${2:0:1}" != "x-" ]; then
44-
prefix=$2
45-
OPTIND=3
46-
else
47-
OPTIND=2
56+
# Check we have at least one directory
57+
if [ ${#dirs[@]} -eq 0 ]; then
58+
usage "Missing <imagedirpath>"
4859
fi
4960

50-
# Parse remaining arguments
51-
61+
# Parse remaining arguments for options
5262
while getopts a: opt; do
5363
case $opt in
5464
a)
@@ -63,28 +73,60 @@ if [ $OPTIND != $(($# + 1)) ]; then
6373
usage "Arguments remaining: \"${@:$OPTIND}\""
6474
fi
6575

76+
# Function to get exec list from a path (file or directory)
77+
get_exec_list() {
78+
local path=$1
79+
if [ -f "$path" ]; then
80+
echo "$path"
81+
elif [ -d "$path" ]; then
82+
find "$path" -type f 2>/dev/null
83+
fi
84+
}
85+
6686
# Extract all of the undefined symbols from the ELF files and create a
6787
# list of sorted, unique undefined variable names.
6888

69-
varlist=`find $dir -name *-thunk.S 2>/dev/null | xargs grep -h asciz | cut -f3 | sort | uniq`
89+
# First try to find thunk files from all directories
90+
varlist=""
91+
for dir in "${dirs[@]}"; do
92+
if [ -d "$dir" ]; then
93+
thunklist=`find $dir -name *-thunk.S 2>/dev/null | xargs grep -h asciz 2>/dev/null | cut -f3`
94+
if [ ! -z "$thunklist" ]; then
95+
varlist="${varlist} ${thunklist}"
96+
fi
97+
fi
98+
done
99+
70100
if [ -z "$varlist" ]; then
71-
execlist=`find $dir -type f 2>/dev/null`
101+
# Collect all executable files from all paths
102+
execlist=""
103+
for dir in "${dirs[@]}"; do
104+
pathlist=`get_exec_list "$dir"`
105+
if [ ! -z "$pathlist" ]; then
106+
execlist="${execlist} ${pathlist}"
107+
fi
108+
done
109+
72110
if [ ! -z "$execlist" ]; then
73-
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`
76-
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`
79-
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"`
111+
# Get all undefined symbol names
112+
varlist=`nm $execlist 2>/dev/null | grep -F ' U ' | sed -e "s/^[ ]*//g" | cut -d' ' -f2`
113+
114+
# Get all defined symbol names
115+
deflist=`nm $execlist 2>/dev/null | grep -F -v -e ' U ' -e ':' | sed -e "s/^[0-9a-z]* //g" | cut -d' ' -f2`
116+
117+
# Remove the intersection between them, and the remaining symbols are found in the main image
118+
if [ ! -z "$varlist" ] && [ ! -z "$deflist" ]; then
119+
common=`echo "$varlist" | tr ' ' '\n' | sort | uniq | grep -Fxf <(echo "$deflist" | tr ' ' '\n' | sort | uniq) | tr '\n' ' '`
120+
if [ "x$common" != "x" ]; then
121+
varlist=`echo $varlist | sed "s/$common//g"`
122+
fi
84123
fi
85124
fi
86125
fi
87126

127+
# Sort and unique the varlist
128+
varlist=`echo "$varlist" | tr ' ' '\n' | sort | uniq | tr '\n' ' '`
129+
88130
for addsym in ${addlist[@]}; do
89131
if [ -f $addsym ]; then
90132
varlist="${varlist}\n$(cat $addsym | grep -v "^,.*")"

0 commit comments

Comments
 (0)