Skip to content

Commit c2dea0b

Browse files
AlexGhitipalmer-dabbelt
authored andcommitted
riscv: Check relocations at compile time
Relocating kernel at runtime is done very early in the boot process, so it is not convenient to check for relocations there and react in case a relocation was not expected. There exists a script in scripts/ that extracts the relocations from vmlinux that is then used at postlink to check the relocations. Signed-off-by: Alexandre Ghiti <alex@ghiti.fr> Reviewed-by: Anup Patel <anup@brainfault.org> Link: https://lore.kernel.org/r/20230329045329.64565-6-alexghiti@rivosinc.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 47981b5 commit c2dea0b

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

arch/riscv/Makefile.postlink

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
# ===========================================================================
3+
# Post-link riscv pass
4+
# ===========================================================================
5+
#
6+
# Check that vmlinux relocations look sane
7+
8+
PHONY := __archpost
9+
__archpost:
10+
11+
-include include/config/auto.conf
12+
include $(srctree)/scripts/Kbuild.include
13+
14+
quiet_cmd_relocs_check = CHKREL $@
15+
cmd_relocs_check = \
16+
$(CONFIG_SHELL) $(srctree)/arch/riscv/tools/relocs_check.sh "$(OBJDUMP)" "$(NM)" "$@"
17+
18+
# `@true` prevents complaint when there is nothing to be done
19+
20+
vmlinux: FORCE
21+
@true
22+
ifdef CONFIG_RELOCATABLE
23+
$(call if_changed,relocs_check)
24+
endif
25+
26+
%.ko: FORCE
27+
@true
28+
29+
clean:
30+
@true
31+
32+
PHONY += FORCE clean
33+
34+
FORCE:
35+
36+
.PHONY: $(PHONY)

arch/riscv/tools/relocs_check.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0-or-later
3+
# Based on powerpc relocs_check.sh
4+
5+
# This script checks the relocations of a vmlinux for "suspicious"
6+
# relocations.
7+
8+
if [ $# -lt 3 ]; then
9+
echo "$0 [path to objdump] [path to nm] [path to vmlinux]" 1>&2
10+
exit 1
11+
fi
12+
13+
bad_relocs=$(
14+
${srctree}/scripts/relocs_check.sh "$@" |
15+
# These relocations are okay
16+
# R_RISCV_RELATIVE
17+
grep -F -w -v 'R_RISCV_RELATIVE'
18+
)
19+
20+
if [ -z "$bad_relocs" ]; then
21+
exit 0
22+
fi
23+
24+
num_bad=$(echo "$bad_relocs" | wc -l)
25+
echo "WARNING: $num_bad bad relocations"
26+
echo "$bad_relocs"

0 commit comments

Comments
 (0)