Skip to content

Commit bdbc0c7

Browse files
David BrazdilMarc Zyngier
authored andcommitted
KVM: arm64: Ensure that all nVHE hyp code is in .hyp.text
Some compilers may put a subset of generated functions into '.text.*' ELF sections and the linker may leverage this division to optimize ELF layout. Unfortunately, the recently introduced HYPCOPY command assumes that all executable code (with the exception of specialized sections such as '.hyp.idmap.text') is in the '.text' section. If this assumption is broken, code in '.text.*' will be merged into kernel proper '.text' instead of the '.hyp.text' that is mapped in EL2. To ensure that this cannot happen, insert an OBJDUMP assertion into HYPCOPY. The command dumps a list of ELF sections in the input object file and greps for '.text.'. If found, compilation fails. Tested with both binutils' and LLVM's objdump (the output format is different). GCC offers '-fno-reorder-functions' to disable this behaviour. Select the flag if it is available. From inspection of GCC source (latest Git in July 2020), this flag does force all code into '.text'. By default, GCC uses profile data, heuristics and attributes to select a subsection. LLVM/Clang currently does not have a similar optimization pass. It can place static constructors into '.text.startup' and it's optimizer can be provided with profile data to reorder hot/cold functions. Neither of these is applicable to nVHE hyp code. If this changes in the future, the OBJDUMP assertion should alert users to the problem. Signed-off-by: David Brazdil <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 6de7dd3 commit bdbc0c7

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

arch/arm64/kvm/hyp/nvhe/Makefile

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,30 @@ $(obj)/%.hyp.tmp.o: $(src)/%.S FORCE
2020
$(obj)/%.hyp.o: $(obj)/%.hyp.tmp.o FORCE
2121
$(call if_changed,hypcopy)
2222

23+
# Disable reordering functions by GCC (enabled at -O2).
24+
# This pass puts functions into '.text.*' sections to aid the linker
25+
# in optimizing ELF layout. See HYPCOPY comment below for more info.
26+
ccflags-y += $(call cc-option,-fno-reorder-functions)
27+
28+
# The HYPCOPY command uses `objcopy` to prefix all ELF symbol names
29+
# and relevant ELF section names to avoid clashes with VHE code/data.
30+
#
31+
# Hyp code is assumed to be in the '.text' section of the input object
32+
# files (with the exception of specialized sections such as
33+
# '.hyp.idmap.text'). This assumption may be broken by a compiler that
34+
# divides code into sections like '.text.unlikely' so as to optimize
35+
# ELF layout. HYPCOPY checks that no such sections exist in the input
36+
# using `objdump`, otherwise they would be linked together with other
37+
# kernel code and not memory-mapped correctly at runtime.
2338
quiet_cmd_hypcopy = HYPCOPY $@
24-
cmd_hypcopy = $(OBJCOPY) --prefix-symbols=__kvm_nvhe_ \
25-
--rename-section=.text=.hyp.text \
26-
$< $@
39+
cmd_hypcopy = \
40+
if $(OBJDUMP) -h $< | grep -F '.text.'; then \
41+
echo "$@: function reordering not supported in nVHE hyp code" >&2; \
42+
/bin/false; \
43+
fi; \
44+
$(OBJCOPY) --prefix-symbols=__kvm_nvhe_ \
45+
--rename-section=.text=.hyp.text \
46+
$< $@
2747

2848
# Remove ftrace and Shadow Call Stack CFLAGS.
2949
# This is equivalent to the 'notrace' and '__noscs' annotations.

0 commit comments

Comments
 (0)