[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 03/11] x86/boot: Check at build time that the trampoline needs no relocations



The trampoline must not need relocations in the final binary.  For PE,
the loader applies base relocations to the whole image before any Xen
code runs, so relocations in the trampoline would have to be reverted
by hand when it is placed in low memory.  That means patching code,
which is unusual and unwanted on x86-64, where code is normally
%rip-relative.  The only relocations allowed are against the abs_*
symbols, which are absolute and so fully resolved at link time.

Add two checks:
- after assembling head.o, the trampoline section must have no
  relocations other than non PC-relative ones against abs_* symbols;
- after linking xen-syms and xen.efi, every abs_* symbol referenced by
  head.o must be present with absolute type.  Checking presence is
  needed for xen.efi, as ld silently drops non-absolute symbols with a
  value below the image base, while still emitting base relocations
  for their references.

Signed-off-by: Frediano Ziglio <freddy77@xxxxxxxxx>
Co-Authored-By: Claude Opus 5.5 <noreply@xxxxxxxxxxxxx>
---
 xen/arch/x86/Makefile                |  5 ++-
 xen/arch/x86/boot/.gitignore         |  1 +
 xen/arch/x86/boot/Makefile           | 13 ++++++++
 xen/tools/check-abs-symbols.sh       | 50 ++++++++++++++++++++++++++++
 xen/tools/check-trampoline-relocs.sh | 47 ++++++++++++++++++++++++++
 5 files changed, 115 insertions(+), 1 deletion(-)
 create mode 100755 xen/tools/check-abs-symbols.sh
 create mode 100755 xen/tools/check-trampoline-relocs.sh

diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile
index 320593a1e8..a90d15748d 100644
--- a/xen/arch/x86/Makefile
+++ b/xen/arch/x86/Makefile
@@ -115,7 +115,10 @@ CFLAGS-$(XEN_BUILD_EFI) += -DXEN_BUILD_EFI
 
 LAST_LINKING_PASS := 2
 
-final-image-check-$(CONFIG_XEN_IBT) = $(SHELL) $(srctree)/tools/check-endbr.sh 
$(1)
+# The trampoline relies on the abs_* symbols being absolute.
+final-image-check-y = \
+    $(SHELL) $(srctree)/tools/check-abs-symbols.sh $(1) $(obj)/boot/head.o
+final-image-check-$(CONFIG_XEN_IBT) += && $(SHELL) 
$(srctree)/tools/check-endbr.sh $(1)
 
 include scripts/Makefile.link
 
diff --git a/xen/arch/x86/boot/.gitignore b/xen/arch/x86/boot/.gitignore
index 595cef6a2c..fb669b2eae 100644
--- a/xen/arch/x86/boot/.gitignore
+++ b/xen/arch/x86/boot/.gitignore
@@ -3,3 +3,4 @@
 /built-in-32.*.bin
 /built-in-32.*.map
 /built-in-32.S
+/head.trampoline-checked
diff --git a/xen/arch/x86/boot/Makefile b/xen/arch/x86/boot/Makefile
index ff0d61d7ac..8411535397 100644
--- a/xen/arch/x86/boot/Makefile
+++ b/xen/arch/x86/boot/Makefile
@@ -14,6 +14,19 @@ targets   += $(obj32)
 
 obj32 := $(addprefix $(obj)/,$(obj32))
 
+# The trampoline must not contain relocations, apart from the ones against the
+# abs_* symbols, which are resolved at link time.
+quiet_cmd_check_trampoline = CHECK   $<
+cmd_check_trampoline = \
+    OBJDUMP="$(OBJDUMP)" $(SHELL) $(srctree)/tools/check-trampoline-relocs.sh 
$< && \
+    touch $@
+
+$(obj)/head.trampoline-checked: $(obj)/head.o \
+                                $(srctree)/tools/check-trampoline-relocs.sh 
FORCE
+       $(call if_changed,check_trampoline)
+
+extra-y += head.trampoline-checked
+
 CFLAGS_x86_32 := $(subst -m64,-m32 -march=i686,$(XEN_TREEWIDE_CFLAGS))
 $(call cc-options-add,CFLAGS_x86_32,CC,$(EMBEDDED_EXTRA_CFLAGS))
 CFLAGS_x86_32 += -Werror -fno-builtin -g0 -msoft-float -mregparm=3
diff --git a/xen/tools/check-abs-symbols.sh b/xen/tools/check-abs-symbols.sh
new file mode 100755
index 0000000000..3db5a8e01d
--- /dev/null
+++ b/xen/tools/check-abs-symbols.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+#
+# Usage ./$0 image head.o
+#
+# The trampoline references Xen symbols only through the abs_* symbols defined
+# in the linker script.  These must be ABSOLUTE symbols, so that references to
+# them are fully resolved at link time and don't produce base relocations in
+# xen.efi.
+#
+# Check that every abs_* symbol referenced by head.o is present in the final
+# image with absolute type.  Checking presence matters for PE images, where a
+# non-absolute symbol with a value below the image base isn't representable and
+# is silently dropped from the symbol table.
+#
+set -e
+
+# Pretty-print parameters a little for message
+MSG_PFX="${0##*/} ${1##*/}"
+
+NM="${NM:-nm}"
+AWK="${AWK:-awk}"
+
+error() {
+    printf '%s\n' "$MSG_PFX Error: $*" >&2
+    exit 1
+}
+
+# Undefined symbols are listed as "U name".
+NEEDED=$($NM -u "$2" | $AWK '$1 == "U" && $2 ~ /^abs_/ { print $2 }')
+[ -n "$NEEDED" ] || error "no abs_* symbols referenced by ${2##*/}"
+
+SYMS=$($NM -p "$1")
+
+# Defined symbols are listed as "value type name".
+BAD=$(printf '%s\n%s\n' "$NEEDED" "--" "$SYMS" | $AWK '
+    !syms && $0 == "--" { syms = 1; next }
+    !syms { needed[$1] = 1; next }
+    NF == 3 && $3 ~ /^abs_/ {
+        seen[$3] = 1
+        if ($2 != "A")
+            printf "  %s: type %s, not absolute\n", $3, $2
+    }
+    END {
+        for (s in needed)
+            if (!(s in seen))
+                printf "  %s: missing (not absolute?)\n", s
+    }')
+
+[ -z "$BAD" ] || error "bad abs_* symbols:
+$BAD"
diff --git a/xen/tools/check-trampoline-relocs.sh 
b/xen/tools/check-trampoline-relocs.sh
new file mode 100755
index 0000000000..5b79f88698
--- /dev/null
+++ b/xen/tools/check-trampoline-relocs.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+#
+# Usage ./$0 head.o
+#
+# The trampoline must not need relocations in the final binary.  For PE, the
+# loader applies base relocations to the whole image before any Xen code runs,
+# so relocations in the trampoline would have to be reverted by hand when it is
+# placed in low memory, which means patching code.  The only relocations
+# allowed are against the abs_* symbols, which are ABSOLUTE linker script
+# symbols and so are fully resolved at link time.
+#
+# Check that the trampoline section of the object file containing the
+# trampoline has no other relocations.
+#
+set -e
+
+# Pretty-print parameters a little for message
+MSG_PFX="${0##*/} ${1##*/}"
+
+OBJDUMP="${OBJDUMP:-objdump}"
+AWK="${AWK:-awk}"
+
+error() {
+    printf '%s\n' "$MSG_PFX Error: $*" >&2
+    exit 1
+}
+
+SECTION=.init.trampoline
+
+# Make sure the section exists, so a rename doesn't silently skip the check.
+$OBJDUMP -h "$1" | grep -qF " $SECTION " || error "section $SECTION not found"
+
+# Don't use -j, llvm-objdump doesn't apply it to relocations.
+RELOCS=$($OBJDUMP -r "$1")
+
+# Relocations are listed as "OFFSET TYPE SYMBOL[+-ADDEND]" after a
+# "RELOCATION RECORDS FOR [section]:" header.  PC-relative references are
+# wrong even against abs_* symbols, as the code is moved.
+BAD=$(echo "$RELOCS" | $AWK -v sect=$SECTION '
+    /^RELOCATION RECORDS FOR / { in_sect = ($4 == "[" sect "]:"); next }
+    in_sect && NF >= 3 && $1 ~ /^[0-9a-fA-F]+$/ {
+        if ($3 !~ /^abs_/ || $2 ~ /PC|PLT|GOT/)
+            printf "  %s+0x%s: %s %s\n", sect, $1, $2, $3
+    }')
+
+[ -z "$BAD" ] || error "unexpected relocations in the trampoline:
+$BAD"
-- 
2.43.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.