Skip to content

RFC x86: Rely on toolchain for relocatable code

Ard Biesheuvel kvm linux-arch linux-doc linux-efi linux-kbuild linux-perf-users linux-pm linux-sparse lkml llvm rust-for-linux xen-devel 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest28 patches 50 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/28x86/pvh: Call C code via the kernel virtual mappingarch/x86/platform/pvh/head.S+5 -11 reply
2/28Documentation: Bump minimum GCC version to 8.1Documentation/admin-guide/README.rst Documentation/process/changes.rst+2 -25 replies
3/28x86/tools: Use mmap() to simplify relocs host toolarch/x86/tools/relocs.c arch/x86/tools/relocs.h+62 -850 replies
4/28x86/boot: Permit GOTPCREL relocations for x86_64 buildsarch/x86/Makefile arch/x86/kernel/vmlinux.lds.S arch/x86/tools/relocs.c +1 more+47 -22 replies
5/28x86: Define the stack protector guard symbol explicitlyarch/x86/Makefile arch/x86/include/asm/init.h arch/x86/include/asm/processor.h +2 more+10 -158 replies
6/28x86/percpu: Get rid of absolute per-CPU variable placementarch/x86/include/asm/desc.h arch/x86/include/asm/percpu.h arch/x86/include/asm/processor.h +9 more+13 -941 reply
7/28scripts/kallsyms: Avoid 0x0 as the relative basescripts/kallsyms.c+1 -10 replies
8/28scripts/kallsyms: Remove support for absolute per-CPU variablesinit/Kconfig kernel/kallsyms.c scripts/kallsyms.c +1 more+9 -620 replies
9/28x86/tools: Remove special relocation handling for per-CPU variablesarch/x86/boot/compressed/misc.c arch/x86/tools/relocs.c+2 -1420 replies
10/28x86/xen: Avoid relocatable quantities in Xen ELF notesarch/x86/kernel/vmlinux.lds.S arch/x86/platform/pvh/head.S arch/x86/tools/relocs.c +1 more+21 -50 replies
11/28x86/pvh: Avoid absolute symbol references in .head.textarch/x86/platform/pvh/head.S+27 -123 replies
12/28x86/pm-trace: Use RIP-relative accesses for .tracedataarch/x86/include/asm/pm-trace.h drivers/base/power/trace.c+5 -50 replies
13/28x86/kvm: Use RIP-relative addressingarch/x86/kernel/kvm.c+5 -30 replies
14/28x86/rethook: Use RIP-relative reference for return addressarch/x86/kernel/rethook.c+2 -14 replies
15/28x86/sync_core: Use RIP-relative addressingarch/x86/include/asm/sync_core.h+2 -10 replies
16/28x86/entry_64: Use RIP-relative addressingarch/x86/entry/calling.h arch/x86/entry/entry_64.S+12 -90 replies
17/28x86/hibernate: Prefer RIP-relative accessesarch/x86/power/hibernate_asm_64.S+2 -20 replies
18/28x86/boot/64: Determine VA/PA offset before entering C codearch/x86/include/asm/setup.h arch/x86/kernel/head64.c arch/x86/kernel/head_64.S+14 -50 replies
19/28x86/boot/64: Avoid intentional absolute symbol references in .head.textarch/x86/kernel/head64.c+18 -120 replies
20/28x64/acpi: Use PIC-compatible references in wakeup_64.Sarch/x86/kernel/acpi/wakeup_64.S+4 -70 replies
21/28x86/head: Use PIC-compatible symbol references in startup codearch/x86/kernel/head_64.S arch/x86/kernel/relocate_kernel_64.S+13 -70 replies
22/28asm-generic: Treat PIC .data.rel.ro sections as .rodatainclude/asm-generic/vmlinux.lds.h include/linux/compiler.h scripts/kallsyms.c +2 more+10 -90 replies
23/28tools/objtool: Mark generated sections as writabletools/objtool/elf.c+1 -10 replies
24/28tools/objtool: Treat indirect ftrace calls as direct callstools/objtool/check.c+30 -22 replies
25/28x86: Use PIE codegen for the core kernelarch/x86/Makefile arch/x86/boot/Makefile arch/x86/boot/compressed/Makefile +3 more+15 -217 replies
26/28x86/boot: Implement support for ELF RELA/RELR relocationsDocumentation/arch/x86/zero-page.rst arch/x86/Kconfig arch/x86/include/asm/setup.h +4 more+64 -80 replies
27/28x86/kernel: Switch to PIE linking for the core kernelarch/x86/Kconfig arch/x86/Makefile arch/x86/boot/compressed/misc.c +2 more+17 -57 replies
28/28x86/tools: Drop x86_64 support from 'relocs' toolarch/x86/tools/Makefile arch/x86/tools/relocs.c arch/x86/tools/relocs.h +2 more+9 -2090 replies

Reviews trailers per patch

3 of 28 patches have a Reviewed-by or Acked-by.

PatchArnd BergmannChristoph LameterJason AndryukMiguel OjedaMark Rutland
1/28T-b R-b
2/28A-b A-b A-b
3/28
4/28
5/28
6/28R-b
7/28
8/28
9/28
10/28
11/28
12/28
13/28
14/28
15/28
16/28
17/28
18/28
19/28
20/28
21/28
22/28
23/28
24/28
25/28
26/28
27/28
28/28

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 5 reviewers.

Read the v1 thread

Cover letter [RFC PATCH 00/28] x86: Rely on toolchain for relocatable code

From: Ard Biesheuvel <[email protected]>

The x86_64 port has a number of historical quirks that result in a reliance on toolchain features that are either poorly specified or basically implementation details of the toolchain:

- the 'kernel' C model implemented by the compiler is intended for position dependent code residing in the 'negative' 2 GiB of the virtual address space, but is used to create a position independent executable (for virtual KASLR);

- the 'kernel' C model has other properties that are not written down anywhere, and may therefore deviate between compilers and versions, which now includes the Rust compilers too (e.g., use %gs not %fs for per-CPU references);

- the relocation format used to perform the PIE relocation at boot is complicated and non-standard, as it deals with 3 types of displacements, including 32-bit negative displacements for RIP-relative per-CPU references that are not subject to relocation fixups (as they are places in a separate, disjoint address space);

- the relocation table is generated from static relocation metadata taken from the ELF input objects into the linker, and these describe the input not the output - relaxations or other linker tweaks may result in a mismatch between the two, and GNU ld and LLD display different behavior here;

- this disjoint per-CPU address space requires elaborate hacks in the linker script and the startup code;

- some of the startup code executes from a 1:1 mapping of memory, where RIP-relative references are mandatory, whereas RIP-relative per-CPU variable references can only work correctly from the kernel virtual mapping (as they need to wrap around from the negative 2 GiB space into the 0x0 based per-CPU region);

The reason for this odd situation wrt per-CPU variable addressing is the fact that we rely on the user-space TLS arrangement for per-task stack cookies, and this was implemented using a fixed offset of 40 bytes from %GS. If we bump the minimum GCC version to 8.1, we can switch to symbol based stack cookie references, allowing the same arrangement to be adopted as on other architectures, i.e., where the CPU register carries the per-CPU offset, and UP or boot-time per-CPU references point into the per-CPU load area directly (using an offset of 0x0).

With that out of the way, we can untangle this whole thing, and replace the bespoke tooling and relocation formats with ordinary, linker generated relocation tables, using the RELR format that reduces the memory footprint of the relocation table by 20x. The compilers can efficiently generate position independent code these days, without unnecessary indirections via the Global Object Table (GOT) except for a handful of special cases (see the KVM patch for an example where a GOT-based indirection is the best choice for pushing the absolute address of a symbol onto the stack in a position independent manner when there are no free GPRs)

It also brings us much closer to the ordinary PIE relocation model used for most of user space, which is therefore much better supported and less likely to create problems as we increase the range of compilers and linkers that need to be supported.

Tested on GCC 8 - 14 and Clang 15 - 17, using EFI and bare metal boot using a variety of entry points (decompressor, EFI stub, XenPV, PVH)

Cc "H. Peter Anvin" <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Uros Bizjak <[email protected]>
Cc Dennis Zhou <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Vitaly Kuznetsov <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Boris Ostrovsky <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Kees Cook <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Keith Packard <[email protected]>
Cc Justin Stitt <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Adrian Hunter <[email protected]>
Cc Kan Liang <[email protected]>

Ard Biesheuvel (28):
x86/pvh: Call C code via the kernel virtual mapping
Documentation: Bump minimum GCC version to 8.1 x86/tools: Use mmap() to simplify relocs host tool x86/boot: Permit GOTPCREL relocations for x86_64 builds
x86: Define the stack protector guard symbol explicitly x86/percpu: Get rid of absolute per-CPU variable placement scripts/kallsyms: Avoid 0x0 as the relative base scripts/kallsyms: Remove support for absolute per-CPU variables x86/tools: Remove special relocation handling for per-CPU variables x86/xen: Avoid relocatable quantities in Xen ELF notes x86/pvh: Avoid absolute symbol references in .head.text x86/pm-trace: Use RIP-relative accesses for .tracedata x86/kvm: Use RIP-relative addressing
x86/rethook: Use RIP-relative reference for return address x86/sync_core: Use RIP-relative addressing
x86/entry_64: Use RIP-relative addressing
x86/hibernate: Prefer RIP-relative accesses
x86/boot/64: Determine VA/PA offset before entering C code x86/boot/64: Avoid intentional absolute symbol references in
.head.text
x64/acpi: Use PIC-compatible references in wakeup_64.S x86/head: Use PIC-compatible symbol references in startup code
asm-generic: Treat PIC .data.rel.ro sections as .rodata tools/objtool: Mark generated sections as writable tools/objtool: Treat indirect ftrace calls as direct calls
x86: Use PIE codegen for the core kernel
x86/boot: Implement support for ELF RELA/RELR relocations x86/kernel: Switch to PIE linking for the core kernel x86/tools: Drop x86_64 support from 'relocs' tool

 Documentation/admin-guide/README.rst    |   2 +-
 Documentation/arch/x86/zero-page.rst    |   3 +-
 Documentation/process/changes.rst       |   2 +-
 arch/x86/Kconfig                        |   3 +-
 arch/x86/Makefile                       |  22 +-
 arch/x86/boot/Makefile                  |   1 +
 arch/x86/boot/compressed/Makefile       |   2 +-
 arch/x86/boot/compressed/misc.c         |  16 +-
 arch/x86/entry/calling.h                |   9 +-
 arch/x86/entry/entry_64.S               |  12 +-
 arch/x86/entry/vdso/Makefile            |   1 +
 arch/x86/include/asm/desc.h             |   1 -
 arch/x86/include/asm/init.h             |   2 +-
 arch/x86/include/asm/percpu.h           |  22 -
 arch/x86/include/asm/pm-trace.h         |   4 +-
 arch/x86/include/asm/processor.h        |  14 +-
 arch/x86/include/asm/setup.h            |   3 +-
 arch/x86/include/asm/stackprotector.h   |   4 -
 arch/x86/include/asm/sync_core.h        |   3 +-
 arch/x86/include/uapi/asm/bootparam.h   |   2 +-
 arch/x86/kernel/acpi/wakeup_64.S        |  11 +-
 arch/x86/kernel/head64.c                |  76 +++-
 arch/x86/kernel/head_64.S               |  40 +-
 arch/x86/kernel/irq_64.c                |   1 -
 arch/x86/kernel/kvm.c                   |   8 +-
 arch/x86/kernel/relocate_kernel_64.S    |   6 +-
 arch/x86/kernel/rethook.c               |   3 +-
 arch/x86/kernel/setup_percpu.c          |   9 +-
 arch/x86/kernel/vmlinux.lds.S           |  75 ++--
 arch/x86/platform/pvh/head.S            |  57 ++-
 arch/x86/power/hibernate_asm_64.S       |   4 +-
 arch/x86/realmode/rm/Makefile           |   1 +
 arch/x86/tools/Makefile                 |   2 +-
 arch/x86/tools/relocs.c                 | 425 +++-----------------
 arch/x86/tools/relocs.h                 |  11 +-
 arch/x86/tools/relocs_64.c              |  18 -
 arch/x86/tools/relocs_common.c          |  11 +-
 arch/x86/xen/xen-head.S                 |  16 +-
 drivers/base/power/trace.c              |   6 +-
 drivers/firmware/efi/libstub/x86-stub.c |   2 +
 include/asm-generic/vmlinux.lds.h       |  10 +-
 include/linux/compiler.h                |   2 +-
 init/Kconfig                            |   5 -
 kernel/kallsyms.c                       |  12 +-
 scripts/kallsyms.c                      |  53 +--
 scripts/link-vmlinux.sh                 |   4 -
 tools/objtool/check.c                   |  43 +-
 tools/objtool/elf.c                     |   2 +-
 tools/objtool/include/objtool/special.h |   2 +-
 tools/perf/util/annotate.c              |   4 +-
 50 files changed, 380 insertions(+), 667 deletions(-)
 delete mode 100644 arch/x86/tools/relocs_64.c
Signature · 1 line
2.46.0.792.g87dc391469-goog