RFC x86: Rely on toolchain for relocatable code
Get series with b4
b4 am [email protected]Versions newest first
- v1 RFC Latest28 patches 50 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/28 | x86/pvh: Call C code via the kernel virtual mappingarch/ | +5 | 1 reply | |
| 2/28 | Documentation: Bump minimum GCC version to 8.1Documentation/ Documentation/ | +2 | 5 replies | |
| 3/28 | x86/tools: Use mmap() to simplify relocs host toolarch/ arch/ | +62 | 0 replies | |
| 4/28 | x86/boot: Permit GOTPCREL relocations for x86_64 buildsarch/ arch/ arch/ +1 more | +47 | 2 replies | |
| 5/28 | x86: Define the stack protector guard symbol explicitlyarch/ arch/ arch/ +2 more | +10 | 8 replies | |
| 6/28 | x86/percpu: Get rid of absolute per-CPU variable placementarch/ arch/ arch/ +9 more | +13 | 1 reply | |
| 7/28 | scripts/kallsyms: Avoid 0x0 as the relative basescripts/ | +1 | 0 replies | |
| 8/28 | scripts/kallsyms: Remove support for absolute per-CPU variablesinit/ kernel/ scripts/ +1 more | +9 | 0 replies | |
| 9/28 | x86/tools: Remove special relocation handling for per-CPU variablesarch/ arch/ | +2 | 0 replies | |
| 10/28 | x86/xen: Avoid relocatable quantities in Xen ELF notesarch/ arch/ arch/ +1 more | +21 | 0 replies | |
| 11/28 | x86/pvh: Avoid absolute symbol references in .head.textarch/ | +27 | 3 replies | |
| 12/28 | x86/pm-trace: Use RIP-relative accesses for .tracedataarch/ drivers/ | +5 | 0 replies | |
| 13/28 | x86/kvm: Use RIP-relative addressingarch/ | +5 | 0 replies | |
| 14/28 | x86/rethook: Use RIP-relative reference for return addressarch/ | +2 | 4 replies | |
| 15/28 | x86/sync_core: Use RIP-relative addressingarch/ | +2 | 0 replies | |
| 16/28 | x86/entry_64: Use RIP-relative addressingarch/ arch/ | +12 | 0 replies | |
| 17/28 | x86/hibernate: Prefer RIP-relative accessesarch/ | +2 | 0 replies | |
| 18/28 | x86/boot/64: Determine VA/PA offset before entering C codearch/ arch/ arch/ | +14 | 0 replies | |
| 19/28 | x86/boot/64: Avoid intentional absolute symbol references in .head.textarch/ | +18 | 0 replies | |
| 20/28 | x64/acpi: Use PIC-compatible references in wakeup_64.Sarch/ | +4 | 0 replies | |
| 21/28 | x86/head: Use PIC-compatible symbol references in startup codearch/ arch/ | +13 | 0 replies | |
| 22/28 | asm-generic: Treat PIC .data.rel.ro sections as .rodatainclude/ include/ scripts/ +2 more | +10 | 0 replies | |
| 23/28 | tools/objtool: Mark generated sections as writabletools/ | +1 | 0 replies | |
| 24/28 | tools/objtool: Treat indirect ftrace calls as direct callstools/ | +30 | 2 replies | |
| 25/28 | x86: Use PIE codegen for the core kernelarch/ arch/ arch/ +3 more | +15 | 17 replies | |
| 26/28 | x86/boot: Implement support for ELF RELA/RELR relocationsDocumentation/ arch/ arch/ +4 more | +64 | 0 replies | |
| 27/28 | x86/kernel: Switch to PIE linking for the core kernelarch/ arch/ arch/ +2 more | +17 | 7 replies | |
| 28/28 | x86/tools: Drop x86_64 support from 'relocs' toolarch/ arch/ arch/ +2 more | +9 | 0 replies |
Reviews trailers per patch
3 of 28 patches have a Reviewed-by or Acked-by.
| Patch | Arnd Bergmann | Christoph Lameter | Jason Andryuk | Miguel Ojeda | Mark Rutland |
|---|---|---|---|---|---|
| 1/28 | T-b R-b | ||||
| 2/28 | A-b | A-b | A-b | ||
| 3/28 | |||||
| 4/28 | |||||
| 5/28 | |||||
| 6/28 | R-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.
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)
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