Received: by 2002:a25:2c96:0:0:0:0:0 with SMTP id s144csp640983ybs; Sun, 24 May 2020 16:42:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPZiiyJwYtXDHZzFieUb1sIhvt8NjDioNgVPUX+3xyiGzjUIV8tdo+ZXWf5U3+dLhDNRpD X-Received: by 2002:a17:906:edbd:: with SMTP id sa29mr17249472ejb.128.1590363768402; Sun, 24 May 2020 16:42:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590363768; cv=none; d=google.com; s=arc-20160816; b=dthfzSXwaS9gm5/oxYRp5AvasU8zXwdW0XQLtK7/KYWEn2VFmqL7m7j/Lw0fUJ2AjF aW95udUZ/L1NQqgRzohuPoCXXXCYMWrGJXdhwUMQBYkY15xCO1MadgVcveP6g1eVSj1i 8JpcLqXIlsXP6qdRXJxj4oHL1dJtItjem7C9VD9glW3w7HqDmTqqkHWvGKBK1y6kJreq wLQdOnPuqWk5tWB9Ih0e6a4WarQl/ayhmKzuvr7WUXW51Itlgs8bAdmvxAIhElMl3uAd pON9lPEvyYprbAi1hgYCZGwYMwQHvU3Cu6B0ujYi64OY3HZpq6YTaaPmyQsxvr8LOD9y BGGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=1v9i69sL4QICbaidphEj2SUMbixrvb6wzpMA1fDBRz4=; b=N4+lYdlPXaYu0qZM0sZrl4Uy78TuohH9vryawzPCFjdp9SG6uIoCnP3gZx1q7o8A9i dremnZcD9eesco34AxHZ/YPrB0IDQqhvCrppVbmnk/hDfyMotat5EkJ8ddPwJS+MNJ2X z14enDhxMbrgsnMH3a44dYmXSag+uhX2p1Qb2jawMcz08llgyTWyATmcYtxXugZ6cIIy ML6e3eZ9LfhjsTBUVwsATBb/GeXhxluLNcM4l2p276zB6umokPeCSjnQ/M2k0uLDfPN0 aHC8N8reDxmQTYBTy3Twl8X+y2f6yF4szAEyY6Qk5tJ3qoI+DufUsHUMXCodHWJvpCuY wDYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=qdA3XEiN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lz8si9029360ejb.721.2020.05.24.16.42.26; Sun, 24 May 2020 16:42:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=qdA3XEiN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388574AbgEXXWU (ORCPT + 99 others); Sun, 24 May 2020 19:22:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388508AbgEXXWT (ORCPT ); Sun, 24 May 2020 19:22:19 -0400 Received: from mail-pj1-x1041.google.com (mail-pj1-x1041.google.com [IPv6:2607:f8b0:4864:20::1041]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0AEAC061A0E for ; Sun, 24 May 2020 16:22:19 -0700 (PDT) Received: by mail-pj1-x1041.google.com with SMTP id n15so7770545pjt.4 for ; Sun, 24 May 2020 16:22:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=1v9i69sL4QICbaidphEj2SUMbixrvb6wzpMA1fDBRz4=; b=qdA3XEiNpc4s3LOJdybF95r2coHrr4EYw1Kd9/E8t+hWRVSr1qrADWhihQgPdIeROt 9x4FWqxmQeMd2z5nOEAlFZHgcKPmzGTTxSHkEcDFIPf6klNTuMMYHARstzZR1R0TUq4T HjkbBSLyHsQB6EZx+IyPpA52mpXfOvEKAJ1VRbl2Z5TktoTgLmAXbbwrCxvCNm6KoTqr fRzoOG8WxyaE7OAnXGVNLUrnScDTpsCQ/xVXH6sGVG6t4spU3YnsAmr1vybMZU4rqUGe n7V2F24ZX/HQsgGiaWYXqXfbahkC/fW3GHul63KZ4hnqwjaM1U3SKxTx7ZksGI4SpTWm SrTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=1v9i69sL4QICbaidphEj2SUMbixrvb6wzpMA1fDBRz4=; b=JXjSVYnp/kPTR6l5nspdVQCVSBsrRpjPg6muzzLtaw4x9zBxuM8VHtpvi4856+vaeT XaWTNdfEKdw7xbPY5brj4o1szzTcinjUjvxR5fONaQtGQWME8rb9hCmq8r9ljLxfXKf0 QtVdWRHO7V9CLN3+UbmZwfarEv5ln0+ozBY6fLM524YukWouEWVNKR9tYCaOfWXFwHAa mFy4SFCkwKJkaMw/OEt0YW9zN2iBmg/dD7X4m6LujkVACh6038zY5SZDty1zacUEysWr RjjhzYEII0rQEzFgvzU/YSCvQXeKstKRi7iPQi2EihFMlVK1M5Lesg1perqLKjBWSr6s Owjw== X-Gm-Message-State: AOAM530cbcj9ea8iHyNKMLZA/Vvro4Vf6OR5LKEP/jbFTcc9gLzRhXfC X29y/4NoQcjSywAmR8enMfO9QA== X-Received: by 2002:a17:902:b18b:: with SMTP id s11mr24655796plr.160.1590362538802; Sun, 24 May 2020 16:22:18 -0700 (PDT) Received: from google.com ([2620:15c:2ce:0:9efe:9f1:9267:2b27]) by smtp.gmail.com with ESMTPSA id t20sm11471717pjo.13.2020.05.24.16.22.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 May 2020 16:22:17 -0700 (PDT) Date: Sun, 24 May 2020 16:22:14 -0700 From: Fangrui Song To: Arvind Sankar Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, Nick Desaulniers , Dmitry Golovin , clang-built-linux@googlegroups.com, Ard Biesheuvel , Masahiro Yamada , Daniel Kiper , linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/4] x86/boot: Remove runtime relocations from head_{32,64}.S Message-ID: <20200524232214.i7rfpdzoam6tpt6n@google.com> References: <20200524212816.243139-4-nivedita@alum.mit.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20200524212816.243139-4-nivedita@alum.mit.edu> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 >--- > 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