From patchwork Mon Sep 28 09:01:45 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Deiminger X-Patchwork-Id: 144774 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7239D4BA23FA for ; Mon, 28 Sep 2026 09:05:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7239D4BA23FA Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=linutronix.de header.i=@linutronix.de header.a=rsa-sha256 header.s=2020 header.b=LegqPlS3; dkim=pass header.d=linutronix.de header.i=@linutronix.de header.a=ed25519-sha256 header.s=2020e header.b=mEltc0zX X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by sourceware.org (Postfix) with ESMTPS id 125E44BA23FA for ; Mon, 28 Sep 2026 09:02:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 125E44BA23FA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linutronix.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 125E44BA23FA Authentication-Results: sourceware.org; arc=none smtp.remote-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790586139; cv=none; b=PCghj7swa8SAZvXFsVKRmOvP/qZyoPUtY28Y32eLq7P7gwTaKShbi3aLmkB2G9uo1gqCng4VS1bPHx1+HlAHHxAhiLzXu0N28qfFD/aSKchsrfxnlwLeHEDHPHpQtIb6DErTZ2esdu5l0FhgQFiy7cfY/T4btVzVh0P6FgJtH3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790586139; c=relaxed/simple; bh=2Oeokj3RcPvySfCICpnu0EW/H53RSLyDxMzdCzL4JJY=; h=From:DKIM-Signature:DKIM-Signature:To:Subject:Date:Message-ID: MIME-Version; b=baIgL59XHsAsnDnBYUkvk2oU5SC+ydjDKZRRx7JnmI4H5eGeHf6KfkzcRvVnJv4IpSqc4HkJ0/r2HkRGUeBTo/Joh6L2+w4vCIBbpou7p4fD6tGmssUe+egCoYOXleVC1VY/PDt2H/3DJ1e7LtJrYiAsq2IrXauYUE30bdufnBc= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, secure) header.d=linutronix.de header.i=@linutronix.de header.a=rsa-sha256 header.s=2020 header.b=LegqPlS3; dkim=pass header.d=linutronix.de header.i=@linutronix.de header.a=ed25519-sha256 header.s=2020e header.b=mEltc0zX DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 125E44BA23FA From: Tobias Deiminger DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1790586133; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R08cHTnwgc4vM758hkgHOtkjmBeoWpMdPjaNW4B8t2g=; b=LegqPlS3/I+ZrFF5qcoyNiy6mzdpZVl5a3JjeaydMI0JWp+ZNduVBqcQ8ykYHJakrsmopz LxZrSyFaPgNt9j5UztMmCua5Ua7M5mUGRqkVA4vanKwYdB0ZPHQPtYArCP0ncJJdR76sHP GKn8UNXuF9mOS0unvnLrKB9tOSdd0w52IXSR7kQBgJSAN0VcgjJG5tGEEUlVx3RQAO7KU8 ar9gkZOMUH3qDkV7COUD4JqBNfP4+qrtCQ/QhNCZD4RRmjF4smjzSJUIcE+hFEix1JcTqZ e232kilKv+h+KFVQHzHblx1g6xR/EgcW18b1+ZNM49y7tUbg/c5FCYyztarkqA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1790586133; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=R08cHTnwgc4vM758hkgHOtkjmBeoWpMdPjaNW4B8t2g=; b=mEltc0zXN7oebaA7N2KP3nz3m/P2qve1/bj6JsN5VRo4/Hac+fms3ZC//EfUoyZo1qdPU5 Jbogy6CZtttYE3Ag== To: binutils@sourceware.org Cc: Tobias Deiminger Subject: [PATCH 2/3] bfd/PE: fix bigobj file corruption on objcopy Date: Mon, 28 Sep 2026 11:01:45 +0200 Message-ID: <20260928090146.4016188-3-tobias.deiminger@linutronix.de> In-Reply-To: <20260928090146.4016188-1-tobias.deiminger@linutronix.de> References: <20260928090146.4016188-1-tobias.deiminger@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces~patchwork=sourceware.org@sourceware.org Converting any linked executable to pe-bigobj-* silently produced corrupted section headers. For example: $ gcc -g -o testprog testprog.c $ objcopy -I elf64-x86-64 -O pe-bigobj-x86-64 testprog testprog.bigobj $ objdump -b pe-bigobj-x86-64 -h testprog.bigobj | head [...] Idx Name Size VMA LMA File off Algn 0 ^K^B^B/ 00001050 0000000000000000 0000000000000000 00001050 2**2 bigobj files can't have an OptionalHeader. coff_bigobj_swap_filehdr_in therefore correctly hardcodes f_opthdr = 0 when reading bigobj into memory. However, bigobj flags wrongly allowed for EXEC_P which requests an OptionalHeader to store a start address in multiple code paths. Further, bigobj's _bfd_aoutsz was initialized > 0 and therefore miscalculated the section header offset on write as if there was an OptionalHeader, causing an invalid gap. If a correct reader reads the produced image, it tries to read section headers but actually accesses undefined content from the gap. Fix it by ensuring bigobj formats have _bfd_aoutsz = 0 and EXEC_P unset, notably also for special cases like auto-promotion. This causes minor information loss (flag, start address) on some conversions, but it's inevitable if the format just can't represent it and still better than producing invalid output. Signed-off-by: Tobias Deiminger --- Here are two more ways to trigger the bug. File corruption during ld $ printf '.globl _entry\n.text\n_entry: ret\n' > entry.s $ x86_64-w64-mingw32-as -o entry.o entry.s $ ld -m i386pep --oformat pe-bigobj-x86-64 -e _entry -o linked.o entry.o $ objdump -h linked.o | head [...] Idx Name Size VMA LMA File off Algn 0 ^K^B^B/ 00001000 0000000000000000 0000000000000000 00001000 2**2 File corruption during auto-promotion (more than 65279 sections) $ printf '.section .t%d,"dr"\n.byte 1\n' $(seq 0 65999) > many.s $ x86_64-w64-mingw32-as -mbig-obj -o many.o many.s $ objcopy -I pe-bigobj-x86-64 -O pe-x86-64 --set-start 0x1000 many.o bigmany.o $ objdump -h bigmany.o | head [...] Idx Name Size VMA LMA File off Algn 0 ^K^B^B/ 00001000 0000000000000000 0000000000000000 00000000 2**2 bfd/coff-aarch64.c | 6 +++--- bfd/coff-i386.c | 6 +++--- bfd/coff-x86_64.c | 6 +++--- bfd/coffcode.h | 23 ++++++++++++++++++----- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/bfd/coff-aarch64.c b/bfd/coff-aarch64.c index d889359a6fc..50ba610ad46 100644 --- a/bfd/coff-aarch64.c +++ b/bfd/coff-aarch64.c @@ -1028,9 +1028,9 @@ const bfd_target BFD_ENDIAN_LITTLE, /* Data byte order is little. */ BFD_ENDIAN_LITTLE, /* Header byte order is little. */ - (HAS_RELOC | EXEC_P /* Object flags. */ - | HAS_LINENO | HAS_DEBUG - | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS), + (HAS_RELOC | HAS_LINENO /* Object flags. */ + | HAS_DEBUG | HAS_SYMS + | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS), (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* Section flags. */ #ifdef COFF_WITH_PE diff --git a/bfd/coff-i386.c b/bfd/coff-i386.c index 0e296a3400b..3c4e24a5ca1 100644 --- a/bfd/coff-i386.c +++ b/bfd/coff-i386.c @@ -801,9 +801,9 @@ const bfd_target BFD_ENDIAN_LITTLE, /* data byte order is little */ BFD_ENDIAN_LITTLE, /* header byte order is little */ - (HAS_RELOC | EXEC_P | /* object flags */ - HAS_LINENO | HAS_DEBUG | - HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS ), + (HAS_RELOC | HAS_LINENO | /* object flags */ + HAS_DEBUG | HAS_SYMS | + HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS ), (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */ #ifdef COFF_WITH_PE diff --git a/bfd/coff-x86_64.c b/bfd/coff-x86_64.c index 2c434624052..d60ba6af04f 100644 --- a/bfd/coff-x86_64.c +++ b/bfd/coff-x86_64.c @@ -984,9 +984,9 @@ const bfd_target BFD_ENDIAN_LITTLE, /* Data byte order is little. */ BFD_ENDIAN_LITTLE, /* Header byte order is little. */ - (HAS_RELOC | EXEC_P /* Object flags. */ - | HAS_LINENO | HAS_DEBUG - | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS), + (HAS_RELOC | HAS_LINENO /* Object flags. */ + | HAS_DEBUG | HAS_SYMS + | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS), (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* Section flags. */ #if defined(COFF_WITH_PE) diff --git a/bfd/coffcode.h b/bfd/coffcode.h index 784ed19f494..eb84f971833 100644 --- a/bfd/coffcode.h +++ b/bfd/coffcode.h @@ -3055,10 +3055,23 @@ coff_compute_section_file_positions (bfd * abfd) } #endif - if (bfd_get_start_address (abfd)) - /* A start address may have been added to the original file. In this - case it will need an optional header to record it. */ - abfd->flags |= EXEC_P; + if (bfd_applicable_file_flags (abfd) & EXEC_P) + { + /* A start address may have been added to the original file. In this + case it will need an optional header to record it. */ + if (bfd_get_start_address (abfd)) + abfd->flags |= EXEC_P; + } + else if (abfd->flags & EXEC_P) + { + /* ld or auto-promotion may have requested the executable flag even if + not supported by the target format. */ + _bfd_error_handler + /* xgettext:c-format */ + (_("%pB: warning: %s cannot be executable or have a start address"), + abfd, bfd_get_target (abfd)); + abfd->flags &= ~EXEC_P; + } if (abfd->flags & EXEC_P) sofar += bfd_coff_aoutsz (abfd); @@ -5940,7 +5953,7 @@ static const bfd_coff_backend_data bigobj_swap_table = coff_SWAP_lineno_out, coff_SWAP_reloc_out, coff_bigobj_swap_filehdr_out, coff_SWAP_aouthdr_out, coff_SWAP_scnhdr_out, - FILHSZ_BIGOBJ, AOUTSZ, SCNHSZ, SYMESZ_BIGOBJ, AUXESZ_BIGOBJ, + FILHSZ_BIGOBJ, 0, SCNHSZ, SYMESZ_BIGOBJ, AUXESZ_BIGOBJ, RELSZ, LINESZ, FILNMLEN_BIGOBJ, true, COFF_DEFAULT_LONG_SECTION_NAMES,