mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Fangrui Song <maskray@google.com>
To: Arvind Sankar <nivedita@alum.mit.edu>
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	"H. Peter Anvin" <hpa@zytor.com>,
	x86@kernel.org, Nick Desaulniers <ndesaulniers@google.com>,
	Dmitry Golovin <dima@golovin.in>,
	clang-built-linux@googlegroups.com,
	Ard Biesheuvel <ardb@kernel.org>,
	Masahiro Yamada <masahiroy@kernel.org>,
	Daniel Kiper <daniel.kiper@oracle.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 3/4] x86/boot: Remove runtime relocations from head_{32,64}.S
Date: Sun, 24 May 2020 16:22:14 -0700	[thread overview]
Message-ID: <20200524232214.i7rfpdzoam6tpt6n@google.com> (raw)
In-Reply-To: <20200524212816.243139-4-nivedita@alum.mit.edu>


On 2020-05-24, Arvind Sankar wrote:
>The BFD linker generates runtime relocations for z_input_len and
>z_output_len, even though they are absolute symbols.
>
>Work around this by defining two variables input_len and output_len in
>addition to the symbols, and use them via position-independent
>references.
>
>This eliminates the last two runtime relocations in the head code and
>allows us to drop the -z noreloc-overflow flag to the linker.
>
>Signed-off-by: Arvind Sankar <nivedita@alum.mit.edu>
>---
> arch/x86/boot/compressed/Makefile  |  8 --------
> arch/x86/boot/compressed/head_32.S | 17 ++++++++---------
> arch/x86/boot/compressed/head_64.S |  4 ++--
> arch/x86/boot/compressed/mkpiggy.c |  6 ++++++
> 4 files changed, 16 insertions(+), 19 deletions(-)
>
>diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile
>index aa9ed814e5fa..d3e882e855ee 100644
>--- a/arch/x86/boot/compressed/Makefile
>+++ b/arch/x86/boot/compressed/Makefile
>@@ -49,15 +49,7 @@ UBSAN_SANITIZE :=n
> KBUILD_LDFLAGS := -m elf_$(UTS_MACHINE)
> # Compressed kernel should be built as PIE since it may be loaded at any
> # address by the bootloader.
>-ifeq ($(CONFIG_X86_32),y)
> KBUILD_LDFLAGS += $(call ld-option, -pie) $(call ld-option, --no-dynamic-linker)
>-else
>-# To build 64-bit compressed kernel as PIE, we disable relocation
>-# overflow check to avoid relocation overflow error with a new linker
>-# command-line option, -z noreloc-overflow.
>-KBUILD_LDFLAGS += $(shell $(LD) --help 2>&1 | grep -q "\-z noreloc-overflow" \
>-	&& echo "-z noreloc-overflow -pie --no-dynamic-linker")
>-endif
> LDFLAGS_vmlinux := -T
>
> hostprogs	:= mkpiggy
>diff --git a/arch/x86/boot/compressed/head_32.S b/arch/x86/boot/compressed/head_32.S
>index 66657bb99aae..064e895bad92 100644
>--- a/arch/x86/boot/compressed/head_32.S
>+++ b/arch/x86/boot/compressed/head_32.S
>@@ -193,18 +193,17 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
> /*
>  * Do the extraction, and jump to the new kernel..
>  */
>-				/* push arguments for extract_kernel: */
>-	pushl	$z_output_len	/* decompressed length, end of relocs */
>+	/* push arguments for extract_kernel: */
>
>-	pushl	%ebp		/* output address */
>-
>-	pushl	$z_input_len	/* input_len */
>+	pushl	output_len@GOTOFF(%ebx)	/* decompressed length, end of relocs */
>+	pushl	%ebp			/* output address */
>+	pushl	input_len@GOTOFF(%ebx)	/* input_len */
> 	leal	input_data@GOTOFF(%ebx), %eax
>-	pushl	%eax		/* input_data */
>+	pushl	%eax			/* input_data */
> 	leal	boot_heap@GOTOFF(%ebx), %eax
>-	pushl	%eax		/* heap area */
>-	pushl	%esi		/* real mode pointer */
>-	call	extract_kernel	/* returns kernel location in %eax */
>+	pushl	%eax			/* heap area */
>+	pushl	%esi			/* real mode pointer */
>+	call	extract_kernel		/* returns kernel location in %eax */
> 	addl	$24, %esp
>
> /*
>diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S
>index f6ba32cd5702..6e4704b6a94e 100644
>--- a/arch/x86/boot/compressed/head_64.S
>+++ b/arch/x86/boot/compressed/head_64.S
>@@ -508,9 +508,9 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated)
> 	movq	%rsi, %rdi		/* real mode address */
> 	leaq	boot_heap(%rip), %rsi	/* malloc area for uncompression */
> 	leaq	input_data(%rip), %rdx  /* input_data */
>-	movl	$z_input_len, %ecx	/* input_len */
>+	movl	input_len(%rip), %ecx	/* input_len */
> 	movq	%rbp, %r8		/* output target address */
>-	movl	$z_output_len, %r9d	/* decompressed length, end of relocs */
>+	movl	output_len(%rip), %r9d	/* decompressed length, end of relocs */
> 	call	extract_kernel		/* returns kernel location in %rax */
> 	popq	%rsi
>
>diff --git a/arch/x86/boot/compressed/mkpiggy.c b/arch/x86/boot/compressed/mkpiggy.c
>index 7e01248765b2..52aa56cdbacc 100644
>--- a/arch/x86/boot/compressed/mkpiggy.c
>+++ b/arch/x86/boot/compressed/mkpiggy.c
>@@ -60,6 +60,12 @@ int main(int argc, char *argv[])
> 	printf(".incbin \"%s\"\n", argv[1]);
> 	printf("input_data_end:\n");
>
>+	printf(".section \".rodata\",\"a\",@progbits\n");
>+	printf(".globl input_len\n");
>+	printf("input_len:\n\t.long %lu\n", ilen);
>+	printf(".globl output_len\n");
>+	printf("output_len:\n\t.long %lu\n", (unsigned long)olen);
>+
> 	retval = 0;
> bail:
> 	if (f)
>-- 
>2.26.2
>

Probably worth mentioning that this works around GNU ld<2.35


This commit fixing https://sourceware.org/bugzilla/show_bug.cgi?id=25754
also fixed the bug. (Just verified that both 2.24 and 2.34 have the bug. binutils-gdb HEAD (future 2.35) is good.)

% cat a.s
pushl $z_input_len
% cat b.s
.globl z_input_len
z_input_len = 0xb612
% gcc -m32 -c a.s b.s
% ld.bfd -m elf_i386 -pie a.o b.o  # has an incorrect R_386_RELATIVE before binutils 2.35


Reviewed-by: Fangrui Song <maskray@google.com>

  reply	other threads:[~2020-05-24 23:22 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-01  8:42 [PATCH] x86/boot: allow a relocatable kernel to be linked with lld Dmitry Golovin
2020-05-02  3:43 ` Nathan Chancellor
2020-05-15 18:50 ` Borislav Petkov
     [not found]   ` <602331589572661@mail.yandex.ru>
2020-05-17 19:44     ` Fangrui Song
2020-05-17 20:25       ` Arvind Sankar
2020-05-18 19:10         ` Nick Desaulniers
2020-05-24 21:28           ` [PATCH 0/4] x86/boot: Remove runtime relocations from compressed kernel Arvind Sankar
2020-05-25  7:10             ` Ard Biesheuvel
2020-05-25 22:59             ` [PATCH v2 " Arvind Sankar
2020-05-26 12:29               ` Sedat Dilek
2020-05-26 12:30                 ` Ard Biesheuvel
2020-05-26 12:33                   ` Sedat Dilek
2020-05-26 12:44                     ` Sedat Dilek
2020-05-26 14:47                       ` Arvind Sankar
2020-05-26 14:50                         ` Sedat Dilek
2020-05-26 15:36                           ` Arvind Sankar
2020-05-26 15:38                             ` Sedat Dilek
2020-05-27  6:26                             ` Sedat Dilek
2020-05-26 14:48                       ` Sedat Dilek
2020-05-26 14:55                         ` Sedat Dilek
2020-05-26 15:07                           ` Sedat Dilek
2020-05-26 15:31                             ` Arvind Sankar
2020-05-27  6:24                               ` Sedat Dilek
2020-05-26 16:18                             ` Sedat Dilek
2020-05-25 22:59             ` [PATCH v2 1/4] x86/boot: Add .text.* to setup.ld Arvind Sankar
2020-05-25 22:59             ` [PATCH v2 2/4] x86/boot: Remove run-time relocations from .head.text code Arvind Sankar
2020-05-25 22:59             ` [PATCH v2 3/4] x86/boot: Remove runtime relocations from head_{32,64}.S Arvind Sankar
2020-05-25 22:59             ` [PATCH v2 4/4] x86/boot: Check that there are no runtime relocations Arvind Sankar
2020-05-26  6:11               ` Ard Biesheuvel
2020-05-26 15:16                 ` Arvind Sankar
2020-05-26 17:13                   ` Fangrui Song
2020-05-26 19:14                     ` Arvind Sankar
2020-08-06 11:19                       ` Andy Shevchenko
2020-08-06 16:12                         ` Arvind Sankar
2020-05-26  0:37             ` [PATCH 0/4] x86/boot: Remove runtime relocations from compressed kernel Fangrui Song
2020-05-24 21:28           ` [PATCH 1/4] x86/boot: Add .text.startup to setup.ld Arvind Sankar
2020-05-24 22:13             ` Fangrui Song
2020-05-24 23:00               ` Arvind Sankar
2020-05-24 23:49                 ` Fangrui Song
2020-05-24 22:48             ` Brian Gerst
2020-05-24 21:28           ` [PATCH 2/4] x86/boot: Remove runtime relocations from .head.text code Arvind Sankar
2020-05-24 22:53             ` Fangrui Song
2020-05-24 23:44               ` Arvind Sankar
2020-05-25  0:55                 ` Fangrui Song
2020-05-24 21:28           ` [PATCH 3/4] x86/boot: Remove runtime relocations from head_{32,64}.S Arvind Sankar
2020-05-24 23:22             ` Fangrui Song [this message]
2020-05-24 23:58               ` Arvind Sankar
2020-05-24 21:28           ` [PATCH 4/4] x86/boot: Check that there are no runtime relocations Arvind Sankar
2020-05-24 23:36             ` Fangrui Song
2020-05-24 23:57               ` Arvind Sankar
2020-05-25  6:10             ` Ard Biesheuvel
2020-05-25 16:26               ` Fangrui Song
2020-05-25 19:22                 ` Arvind Sankar

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=20200524232214.i7rfpdzoam6tpt6n@google.com \
    --to=maskray@google.com \
    --cc=ardb@kernel.org \
    --cc=bp@alien8.de \
    --cc=clang-built-linux@googlegroups.com \
    --cc=daniel.kiper@oracle.com \
    --cc=dima@golovin.in \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=mingo@redhat.com \
    --cc=ndesaulniers@google.com \
    --cc=nivedita@alum.mit.edu \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®