Skip to content

Commit 19b642d

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 f602864 commit 19b642d

File tree

3 files changed

+107
-32
lines changed

3 files changed

+107
-32
lines changed

examples/elf/main/Makefile

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

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

3763

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)$(MODLUE_NAME) g_mod >$@.tmp
4544
$(Q) $(call TESTANDREPLACEFILE, $@.tmp, $@)
4645

4746

tools/mksymtab.sh

Lines changed: 78 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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
5267
while getopts a: opt; do
5368
case $opt in
5469
a)
@@ -63,28 +78,63 @@ if [ $OPTIND != $(($# + 1)) ]; then
6378
usage "Arguments remaining: \"${@:$OPTIND}\""
6479
fi
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`
70-
if [ -z "$varlist" ]; then
71-
execlist=`find $dir -type f 2>/dev/null`
72-
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`
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
79104

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"`
105+
if [ -z "$varlist" ]; 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
117+
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`
121+
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`
124+
125+
# Remove the intersection between them, and the remaining symbols are found in the main image
126+
if [ ! -z "$varlist" ] && [ ! -z "$deflist" ]; then
127+
common=`echo "$varlist" | tr ' ' '\n' | sort | uniq | grep -Fxf <(echo "$deflist" | tr ' ' '\n' | sort | uniq) | tr '\n' ' '`
128+
if [ "x$common" != "x" ]; then
129+
varlist=`echo $varlist | sed "s/$common//g"`
130+
fi
84131
fi
85132
fi
86133
fi
87134

135+
# Sort and unique the varlist
136+
varlist=`echo "$varlist" | tr ' ' '\n' | sort | uniq | tr '\n' ' '`
137+
88138
for addsym in ${addlist[@]}; do
89139
if [ -f $addsym ]; then
90140
varlist="${varlist}\n$(cat $addsym | grep -v "^,.*")"

0 commit comments

Comments
 (0)