Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 39cf650

Browse files
bjohannesmeyerjpoimboe
authored andcommitted
scripts/faddr2line: Reduce number of readelf calls to three
Rather than calling readelf several times for each invocation of __faddr2line, call readelf only three times at the beginning, and save its result for future use. Signed-off-by: Brian Johannesmeyer <bjohannesmeyer@gmail.com> Link: https://lore.kernel.org/r/20240415145538.1938745-2-bjohannesmeyer@gmail.com Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
1 parent 22a40d1 commit 39cf650

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

scripts/faddr2line

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ command -v ${ADDR2LINE} >/dev/null 2>&1 || die "${ADDR2LINE} isn't installed"
8787
find_dir_prefix() {
8888
local objfile=$1
8989

90-
local start_kernel_addr=$(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' |
90+
local start_kernel_addr=$(echo "${ELF_SYMS}" | sed 's/\[.*\]//' |
9191
${AWK} '$8 == "start_kernel" {printf "0x%s", $2}')
9292
[[ -z $start_kernel_addr ]] && return
9393

@@ -103,6 +103,14 @@ find_dir_prefix() {
103103
return 0
104104
}
105105

106+
run_readelf() {
107+
local objfile=$1
108+
109+
ELF_FILEHEADER=$(${READELF} --file-header $objfile)
110+
ELF_SECHEADERS=$(${READELF} --section-headers --wide $objfile)
111+
ELF_SYMS=$(${READELF} --symbols --wide $objfile)
112+
}
113+
106114
__faddr2line() {
107115
local objfile=$1
108116
local func_addr=$2
@@ -125,7 +133,7 @@ __faddr2line() {
125133

126134
# vmlinux uses absolute addresses in the section table rather than
127135
# section offsets.
128-
local file_type=$(${READELF} --file-header $objfile |
136+
local file_type=$(echo "${ELF_FILEHEADER}" |
129137
${AWK} '$1 == "Type:" { print $2; exit }')
130138
if [[ $file_type = "EXEC" ]] || [[ $file_type == "DYN" ]]; then
131139
is_vmlinux=1
@@ -143,8 +151,7 @@ __faddr2line() {
143151
local sec_name
144152

145153
# Get the section size:
146-
sec_size=$(${READELF} --section-headers --wide $objfile |
147-
sed 's/\[ /\[/' |
154+
sec_size=$(echo "${ELF_SECHEADERS}" | sed 's/\[ /\[/' |
148155
${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print "0x" $6; exit }')
149156

150157
if [[ -z $sec_size ]]; then
@@ -154,8 +161,7 @@ __faddr2line() {
154161
fi
155162

156163
# Get the section name:
157-
sec_name=$(${READELF} --section-headers --wide $objfile |
158-
sed 's/\[ /\[/' |
164+
sec_name=$(echo "${ELF_SECHEADERS}" | sed 's/\[ /\[/' |
159165
${AWK} -v sec=$sym_sec '$1 == "[" sec "]" { print $2; exit }')
160166

161167
if [[ -z $sec_name ]]; then
@@ -197,7 +203,7 @@ __faddr2line() {
197203
found=2
198204
break
199205
fi
200-
done < <(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | ${AWK} -v sec=$sym_sec '$7 == sec' | sort --key=2)
206+
done < <(echo "${ELF_SYMS}" | sed 's/\[.*\]//' | ${AWK} -v sec=$sym_sec '$7 == sec' | sort --key=2)
201207

202208
if [[ $found = 0 ]]; then
203209
warn "can't find symbol: sym_name: $sym_name sym_sec: $sym_sec sym_addr: $sym_addr sym_elf_size: $sym_elf_size"
@@ -278,7 +284,7 @@ __faddr2line() {
278284

279285
DONE=1
280286

281-
done < <(${READELF} --symbols --wide $objfile | sed 's/\[.*\]//' | ${AWK} -v fn=$sym_name '$8 == fn')
287+
done < <(echo "${ELF_SYMS}" | sed 's/\[.*\]//' | ${AWK} -v fn=$sym_name '$8 == fn')
282288
}
283289

284290
[[ $# -lt 2 ]] && usage
@@ -291,7 +297,9 @@ LIST=0
291297
[[ ! -f $objfile ]] && die "can't find objfile $objfile"
292298
shift
293299

294-
${READELF} --section-headers --wide $objfile | ${GREP} -q '\.debug_info' || die "CONFIG_DEBUG_INFO not enabled"
300+
run_readelf $objfile
301+
302+
echo "${ELF_SECHEADERS}" | ${GREP} -q '\.debug_info' || die "CONFIG_DEBUG_INFO not enabled"
295303

296304
DIR_PREFIX=supercalifragilisticexpialidocious
297305
find_dir_prefix $objfile

0 commit comments

Comments
 (0)