From patchwork Sun Sep 17 18:12:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 141223 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2367659vqi; Sun, 17 Sep 2023 17:52:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFm5Z7gGW46Vk/WSmFDZmbwLm1k+xsvyNydIKn5WPQf6ky5QrDOM3U78PC/58D6Tjcwgr2n X-Received: by 2002:a05:6a20:3d83:b0:154:fdc0:48d5 with SMTP id s3-20020a056a203d8300b00154fdc048d5mr12865314pzi.30.1694998336474; Sun, 17 Sep 2023 17:52:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694998336; cv=none; d=google.com; s=arc-20160816; b=IuDtENcHJ5BLznZHuPHxHaOHPYxu5WbEPn5mLzS9MNsqyoIwhAoiT+1DQcMn9Mk8qr PAfS3j9FlSKscDVWwplkkOVDXOWNIDI4w6FIW/1uuAIj7oZpgZilUpEHJ7+aOsEUh/o+ SRqjZmf8jqblAL/lAFTn+hcatpH9+X0A6XOCxEyyAvOWc6xRYiAgPU522ElTKeY0QkQN HLo4X+84ff1EB2vaKSY5WzrePHl7FJEZw+CNtknsPGIVeK0/IPUHReZCi1vqLwYGL/Qq lQ94K/Fjf5aZhB0CWYA0oZrGRNQF1/bWrZuuPvJpSYrpjsUvg6WaUZxmKSlmZwdI6Uzf fnPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=M4R1VVypAmNtRAtSZGqDkFuQH4C8pYAgt5jxU9SCqsY=; fh=mh/EFXDyWz6SYA4qEbcbMgOZ07AT4DlRJonTEXgTb50=; b=FxixKki1rEp0dBcGNRkHizx9KK77wDxsDFuwrHwNNSqPXMakQi3GwHeF6eMqEm9gOW Yu69cSR2o/Aozer9KWnRH2PINQXc5Yvbsgvp3NqWVjNcbGcQ2RnssShmXmI7h5DTYIfl OoYxw1mUbLm9agdmUYOqHC5mTk8B8uoB3hJHJdfF2Fo3sSocPRFmOIcoVN4pXBEOUJUw vcdA1dD+z3k7ohJbqFO5R7crGfAYZO9LrNCb22KZ2jodTni3+PU2CTbKEWpHA2ssP46s RDBpJt56lWEfbTL8WXgLBW38Y6tMnAataPZKd/S7oLJugsQgJ8wQApDiKqpGnYQZIPNL Jjlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=YQMsAm3Q; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=+VUL5SqH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id w7-20020a636207000000b0056c2f5088ccsi7156540pgb.86.2023.09.17.17.52.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Sep 2023 17:52:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=YQMsAm3Q; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=+VUL5SqH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 6CB1381F3562; Sun, 17 Sep 2023 11:16:07 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238549AbjIQSNP (ORCPT + 28 others); Sun, 17 Sep 2023 14:13:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238229AbjIQSNB (ORCPT ); Sun, 17 Sep 2023 14:13:01 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 961F4193; Sun, 17 Sep 2023 11:12:52 -0700 (PDT) Date: Sun, 17 Sep 2023 18:12:50 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1694974371; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M4R1VVypAmNtRAtSZGqDkFuQH4C8pYAgt5jxU9SCqsY=; b=YQMsAm3QiN1rYIgHMmlCsQHKPTCmiRr1QYBLdvyeinaE/tFmvuygQiQJsEndZi/eJLWInG f82+jNHSd0pifUXKLLO7XpAh6Ra24Rlm335MBJaiU/mzl7v9O69aErHQ8ZbeU70BWsdG4E q3gVNc9wbwW/WYw6k3GS0IWUD8dvS5zFhQjd79mAk31zs9ukfxIP3KBPgMfAtPfRxs7Ehp OWu7v22iseFLOwYd4TfQMPkHcBTU+KrbV5u70lmpCSquSEup84rMVNmeyiSsf7c0SY2CKk CFh/WqoAhX+t7pM+dq2LX+HaJQ4Ra+B7wo15+wWJhuE8p1/7dve3/+02LCJM2A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1694974371; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=M4R1VVypAmNtRAtSZGqDkFuQH4C8pYAgt5jxU9SCqsY=; b=+VUL5SqHMxgA8Va56pUaDvmXRzNCuDHIMe/frCJfuIe6MgiiZ82LyihV9PdEW3209ggl0v QP93KGyh9n6cj6DA== From: "tip-bot2 for Ard Biesheuvel" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/boot] x86/boot: Construct PE/COFF .text section from assembler Cc: Ard Biesheuvel , Ingo Molnar , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230915171623.655440-15-ardb@google.com> References: <20230915171623.655440-15-ardb@google.com> MIME-Version: 1.0 Message-ID: <169497437050.27769.9197452501392752841.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Sun, 17 Sep 2023 11:16:07 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777145690684312346 X-GMAIL-MSGID: 1777334575673676943 The following commit has been merged into the x86/boot branch of tip: Commit-ID: efa089e63b56bdc5eca754b995cb039dd7a5457e Gitweb: https://git.kernel.org/tip/efa089e63b56bdc5eca754b995cb039dd7a5457e Author: Ard Biesheuvel AuthorDate: Fri, 15 Sep 2023 17:16:29 Committer: Ingo Molnar CommitterDate: Sun, 17 Sep 2023 19:48:43 +02:00 x86/boot: Construct PE/COFF .text section from assembler Now that the size of the setup block is visible to the assembler, it is possible to populate the PE/COFF header fields from the asm code directly, instead of poking the values into the binary using the build tool. This will make it easier to reorganize the section layout without having to tweak the build tool in lockstep. This change has no impact on the resulting bzImage binary. Signed-off-by: Ard Biesheuvel Signed-off-by: Ingo Molnar Link: https://lore.kernel.org/r/20230915171623.655440-15-ardb@google.com --- arch/x86/boot/header.S | 22 +++++------------ arch/x86/boot/tools/build.c | 47 +------------------------------------ 2 files changed, 7 insertions(+), 62 deletions(-) diff --git a/arch/x86/boot/header.S b/arch/x86/boot/header.S index 34e9b35..2b07bc5 100644 --- a/arch/x86/boot/header.S +++ b/arch/x86/boot/header.S @@ -75,14 +75,12 @@ optional_header: .byte 0x02 # MajorLinkerVersion .byte 0x14 # MinorLinkerVersion - # Filled in by build.c - .long 0 # SizeOfCode + .long setup_size + ZO__end - 0x200 # SizeOfCode .long 0 # SizeOfInitializedData .long 0 # SizeOfUninitializedData - # Filled in by build.c - .long 0x0000 # AddressOfEntryPoint + .long setup_size + ZO_efi_pe_entry # AddressOfEntryPoint .long 0x0200 # BaseOfCode #ifdef CONFIG_X86_32 @@ -105,10 +103,7 @@ extra_header_fields: .word 0 # MinorSubsystemVersion .long 0 # Win32VersionValue - # - # The size of the bzImage is written in tools/build.c - # - .long 0 # SizeOfImage + .long setup_size + ZO__end # SizeOfImage .long 0x200 # SizeOfHeaders .long 0 # CheckSum @@ -199,18 +194,15 @@ section_table: IMAGE_SCN_MEM_DISCARDABLE # Characteristics #endif - # - # The offset & size fields are filled in by build.c. - # .ascii ".text" .byte 0 .byte 0 .byte 0 - .long 0 - .long 0x0 # startup_{32,64} - .long 0 # Size of initialized data + .long ZO__end + .long setup_size + .long ZO__edata # Size of initialized data # on disk - .long 0x0 # startup_{32,64} + .long setup_size .long 0 # PointerToRelocations .long 0 # PointerToLineNumbers .word 0 # NumberOfRelocations diff --git a/arch/x86/boot/tools/build.c b/arch/x86/boot/tools/build.c index e792c6c..9712f27 100644 --- a/arch/x86/boot/tools/build.c +++ b/arch/x86/boot/tools/build.c @@ -50,10 +50,8 @@ u8 buf[SETUP_SECT_MAX*512]; #define PECOFF_RELOC_RESERVE 0x20 #define PECOFF_COMPAT_RESERVE 0x20 -static unsigned long efi_pe_entry; static unsigned long efi32_pe_entry; static unsigned long _edata; -static unsigned long _end; /*----------------------------------------------------------------------*/ @@ -216,32 +214,6 @@ static void update_pecoff_setup_and_reloc(unsigned int size) #endif } -static void update_pecoff_text(unsigned int text_start, unsigned int file_sz) -{ - unsigned int pe_header; - unsigned int text_sz = file_sz - text_start; - unsigned int bss_sz = _end - text_sz; - - pe_header = get_unaligned_le32(&buf[0x3c]); - - /* - * Size of code: Subtract the size of the first sector (512 bytes) - * which includes the header. - */ - put_unaligned_le32(file_sz - 512 + bss_sz, &buf[pe_header + 0x1c]); - - /* Size of image */ - put_unaligned_le32(file_sz + bss_sz, &buf[pe_header + 0x50]); - - /* - * Address of entry point for PE/COFF executable - */ - put_unaligned_le32(text_start + efi_pe_entry, &buf[pe_header + 0x28]); - - update_pecoff_section_header_fields(".text", text_start, text_sz + bss_sz, - text_sz, text_start); -} - static int reserve_pecoff_reloc_section(int c) { /* Reserve 0x20 bytes for .reloc section */ @@ -249,22 +221,9 @@ static int reserve_pecoff_reloc_section(int c) return PECOFF_RELOC_RESERVE; } -static void efi_stub_defaults(void) -{ - /* Defaults for old kernel */ -#ifdef CONFIG_X86_32 - efi_pe_entry = 0x10; -#else - efi_pe_entry = 0x210; -#endif -} - #else static inline void update_pecoff_setup_and_reloc(unsigned int size) {} -static inline void update_pecoff_text(unsigned int text_start, - unsigned int file_sz) {} -static inline void efi_stub_defaults(void) {} static inline int reserve_pecoff_reloc_section(int c) { @@ -307,10 +266,8 @@ static void parse_zoffset(char *fname) p = (char *)buf; while (p && *p) { - PARSE_ZOFS(p, efi_pe_entry); PARSE_ZOFS(p, efi32_pe_entry); PARSE_ZOFS(p, _edata); - PARSE_ZOFS(p, _end); p = strchr(p, '\n'); while (p && (*p == '\r' || *p == '\n')) @@ -328,8 +285,6 @@ int main(int argc, char ** argv) void *kernel; u32 crc = 0xffffffffUL; - efi_stub_defaults(); - if (argc != 5) usage(); parse_zoffset(argv[3]); @@ -376,8 +331,6 @@ int main(int argc, char ** argv) kernel = mmap(NULL, sz, PROT_READ, MAP_SHARED, fd, 0); if (kernel == MAP_FAILED) die("Unable to mmap '%s': %m", argv[2]); - update_pecoff_text(setup_sectors * 512, i + _edata); - crc = partial_crc32(buf, i, crc); if (fwrite(buf, 1, i, dest) != i)