All of lore.kernel.org
 help / color / mirror / Atom feed
From: Frediano Ziglio <freddy77@gmail.com>
To: xen-devel@lists.xenproject.org
Cc: "Frediano Ziglio" <freddy77@gmail.com>,
	"Jan Beulich" <jbeulich@suse.com>,
	"Andrew Cooper" <andrew.cooper3@citrix.com>,
	"Roger Pau Monné" <roger@xenproject.org>,
	"Teddy Astie" <teddy.astie@vates.tech>,
	"Anthony PERARD" <anthony.perard@vates.tech>,
	"Michal Orzel" <michal.orzel@amd.com>,
	"Julien Grall" <julien@xen.org>,
	"Stefano Stabellini" <sstabellini@kernel.org>,
	"Claude Opus 5 . 5" <noreply@anthropic.com>
Subject: [PATCH 03/11] x86/boot: Check at build time that the trampoline needs no relocations
Date: Fri,  4 Dec 2026 20:50:36 +0000	[thread overview]
Message-ID: <20261204205045.596093-4-freddy77@gmail.com> (raw)
In-Reply-To: <20261204205045.596093-1-freddy77@gmail.com>

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@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
 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



  parent reply	other threads:[~2026-12-04 20:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 20:50 [PATCH 00/11] x86/boot: Improve trampoline code Frediano Ziglio
2026-12-04 20:50 ` [PATCH 01/11] x86/boot: Prevent some possible firmware misbehaviour Frediano Ziglio
2026-12-04 20:50 ` [PATCH 02/11] x86/boot: Avoid PE relocations in the trampoline Frediano Ziglio
2026-12-04 20:50 ` Frediano Ziglio [this message]
2026-12-04 20:50 ` [PATCH 04/11] x86/boot: Reduce " Frediano Ziglio
2026-12-04 20:50 ` [PATCH 05/11] x86/boot: Share the switch to 64-bit mode between SIPI and S3 paths Frediano Ziglio
2026-12-04 20:50 ` [PATCH 06/11] x86/boot: Use stack to remove a relocation from trampoline code Frediano Ziglio
2026-12-04 20:50 ` [PATCH 07/11] x86/boot: Apply relocations in the trampoline after the copy Frediano Ziglio
2026-12-04 20:50 ` [PATCH 08/11] x86/boot: Group trampoline code and data using subsections Frediano Ziglio
2026-12-04 20:50 ` [PATCH 09/11] x86/boot: Use a writable copy of the trampoline data Frediano Ziglio
2026-12-04 20:50 ` [PATCH 10/11] x86/boot: Assemble the trampoline separately from head.S Frediano Ziglio
2026-12-04 20:50 ` [PATCH 11/11] x86/boot: Check the kind of symbols passed to the bootsym helpers Frediano Ziglio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204205045.596093-4-freddy77@gmail.com \
    --to=freddy77@gmail.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=anthony.perard@vates.tech \
    --cc=jbeulich@suse.com \
    --cc=julien@xen.org \
    --cc=michal.orzel@amd.com \
    --cc=noreply@anthropic.com \
    --cc=roger@xenproject.org \
    --cc=sstabellini@kernel.org \
    --cc=teddy.astie@vates.tech \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.