From patchwork Fri Oct 2 17:45:09 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleg Tolmatcev X-Patchwork-Id: 145164 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 B90B74BAE7EF for ; Fri, 2 Oct 2026 17:47:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B90B74BAE7EF Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=aS4oKAe0 X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-wm2-x11.google.com (mail-wm2-x11.google.com [IPv6:2a00:1450:4864:31::11]) by sourceware.org (Postfix) with ESMTPS id E54354BA9003 for ; Fri, 2 Oct 2026 17:45:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E54354BA9003 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E54354BA9003 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:31::11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790963149; cv=none; b=dZhp6SDbAf0uO0sKlkz/ouOynPyl5+dVc4GD4z+M3f3szFlPUjKyegqVAgXjk1sT/m+ihWFcHRPIamwK8HNxUfJ05GQSvhHnpxa1iJisDTSzc9hEmt/Vy3EY4mJwn0RuzjHHzYMWhvI9kKfI3JmDQlmEkcIe65BI0xJUZchUPD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790963149; c=relaxed/simple; bh=f7vNavwvvgFQ9J5I8BZCeXzDlDunO10AQbpvCBq2SPw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=fYDQfx88ojghag5XLeuNGxpOZUV41TYa58c6FdeKRtMgnF7BdIn5zGpV0hlYyamNZzRhbpY1yB9UbqkI+qHqzomPXGDAYM3P6LDuRqEIAAzQ4zPhvIltWi3AxT0JTyrDlq4Oa2ObOUj6RZeH0QjIdV/ZbNysjxiLGtDn45A8JxM= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=aS4oKAe0 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E54354BA9003 Received: by mail-wm2-x11.google.com with SMTP id 5b1f17b1804b1-49b912d8239so1278755e9.0 for ; Fri, 02 Oct 2026 10:45:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790963148; x=1791567948; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=4sf/at6g/VLPLELXgNJZfcdvd6uGso455OUW+zpFdjg=; b=aS4oKAe0wTakVJL0S2vEOW++pJyuJj/8T3t84xIHmgBmmRepxGs1QTPHJSbpqNDuZM PKkWbgQz8doyODx/tN5vgO5NCWohTVPp0II4nZ3REiJaeCwvmZ7G+0FnrvCYh1l64Xbz 5QYlhitGsOG40VAHs3lePIUmqJEpE6dcMKZowgBQB2Pq36h368nQyp19n23RuPYt9qFK toPisb64sWhU4Vv9rv2ZGOiiDL9VYLVtJNVI+10Ntt/VnAqyUggY8UFrkhJwbhCNnWec vGf9lnOUaj+idLoAyGhfKigWOnZducDwzD2YqiLKiP8qMFkiTSB2w/nJACFB0Kojn/DQ jbcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790963148; x=1791567948; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-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eGDntn/XyUr83Rl1D1ljoXybHsefGJj6e8 geBAqzrNqZCzsMSbJDgPvxx2gYa6cwWao38YPMUBLPLmwNeP/wTVlsbnmMX/u2CemfKot3MZnWn aVA= X-Received: by 2002:a05:600c:628d:b0:4a1:1601:6919 with SMTP id 5b1f17b1804b1-4a11601696dmr43856515e9.30.1790963147554; Fri, 02 Oct 2026 10:45:47 -0700 (PDT) Received: from Mein-PC (p200300ed8f43f8003c9e41d91f6282c4.dip0.t-ipconnect.de. [2003:ed:8f43:f800:3c9e:41d9:1f62:82c4]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4a03fef5950sm60380325e9.2.2026.10.02.10.45.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Oct 2026 10:45:47 -0700 (PDT) From: Oleg Tolmatcev To: binutils@sourceware.org Cc: Oleg Tolmatcev Subject: [PATCH v2] ld/pdb: Fix PDB source-info substream overflow Date: Fri, 2 Oct 2026 19:45:09 +0200 Message-ID: <20261002174508.1414-4-oleg.tolmatcev@gmail.com> X-Mailer: git-send-email 2.56.0.windows.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_PBL, 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 source_files_count counts every source file reference across all modules, not the deduplicated count that goes into the substream header, so a uint16_t is too narrow. A link with more than 65535 references wraps it, allocates too small a buffer for the source-info substream, and then writes past the end of it. ld/ * pdb.c (create_source_info_substream): Make source_files_count a uint32_t. * testsuite/ld-pe/pdb4a.s: New test source. * testsuite/ld-pe/pdb4b.s: New test source. * testsuite/ld-pe/pdb.exp (test11): Run the new test. --- ld/pdb.c | 2 +- ld/testsuite/ld-pe/pdb.exp | 23 +++++++++++++++++++ ld/testsuite/ld-pe/pdb4a.s | 45 ++++++++++++++++++++++++++++++++++++++ ld/testsuite/ld-pe/pdb4b.s | 38 ++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 ld/testsuite/ld-pe/pdb4a.s create mode 100644 ld/testsuite/ld-pe/pdb4b.s diff --git a/ld/pdb.c b/ld/pdb.c index f906e5f9142..46236fcbd4f 100644 --- a/ld/pdb.c +++ b/ld/pdb.c @@ -4356,7 +4356,7 @@ create_source_info_substream (void **data, uint32_t *size, struct source_files_info *source) { uint16_t dedupe_source_files_count = 0; - uint16_t source_files_count = 0; + unsigned int source_files_count = 0; uint32_t strings_len = 0; uint8_t *ptr; diff --git a/ld/testsuite/ld-pe/pdb.exp b/ld/testsuite/ld-pe/pdb.exp index 1439baa4bf8..48b0f517ec7 100644 --- a/ld/testsuite/ld-pe/pdb.exp +++ b/ld/testsuite/ld-pe/pdb.exp @@ -1837,6 +1837,28 @@ proc test10 { } { } } +# Check that a PDB file with more than 65535 source file references can be +# created. + +proc test11 { } { + global as + global ld + global srcdir + global subdir + + if { ![ld_assemble $as $srcdir/$subdir/pdb4a.s tmpdir/pdb4a.o] + || ![ld_assemble $as $srcdir/$subdir/pdb4b.s tmpdir/pdb4b.o] } { + unsupported "Build pdb4a.o and pdb4b.o" + return + } + + if ![ld_link $ld "tmpdir/pdb4.exe" "--pdb=tmpdir/pdb4.pdb -e main tmpdir/pdb4a.o tmpdir/pdb4b.o"] { + fail "Create PDB file with more than 65535 source file references" + } else { + pass "Create PDB file with more than 65535 source file references" + } +} + test1 test2 test3 @@ -1847,3 +1869,4 @@ test7 test8 test9 test10 +test11 diff --git a/ld/testsuite/ld-pe/pdb4a.s b/ld/testsuite/ld-pe/pdb4a.s new file mode 100644 index 00000000000..d4693b69a23 --- /dev/null +++ b/ld/testsuite/ld-pe/pdb4a.s @@ -0,0 +1,45 @@ +/* Two modules with 32768 source file references each, so that the total + number of references in the DBI source-info substream exceeds 65535. */ + + .equ CV_SIGNATURE_C13, 4 + .equ DEBUG_S_STRINGTABLE, 0xf3 + .equ DEBUG_S_FILECHKSMS, 0xf4 + .equ CHKSUM_TYPE_NONE, 0 + + .equ NUM_CHKSMS, 32768 + + .section ".debug$S", "rn" + .long CV_SIGNATURE_C13 + .long DEBUG_S_STRINGTABLE + .long .strings_end - .strings_start + +.strings_start: + + .asciz "" + +.src1: + .asciz "foo" + +.strings_end: + + .balign 4 + + .long DEBUG_S_FILECHKSMS + .long .chksms_end - .chksms_start + +.chksms_start: + + .rept NUM_CHKSMS + .long .src1 - .strings_start + .byte 0 /* checksum length */ + .byte CHKSUM_TYPE_NONE + .short 0 /* padding */ + .endr + +.chksms_end: + + .text + + .global main +main: + .long 0x12345678 diff --git a/ld/testsuite/ld-pe/pdb4b.s b/ld/testsuite/ld-pe/pdb4b.s new file mode 100644 index 00000000000..029d7703897 --- /dev/null +++ b/ld/testsuite/ld-pe/pdb4b.s @@ -0,0 +1,38 @@ +/* See pdb4a.s. */ + + .equ CV_SIGNATURE_C13, 4 + .equ DEBUG_S_STRINGTABLE, 0xf3 + .equ DEBUG_S_FILECHKSMS, 0xf4 + .equ CHKSUM_TYPE_NONE, 0 + + .equ NUM_CHKSMS, 32768 + + .section ".debug$S", "rn" + .long CV_SIGNATURE_C13 + .long DEBUG_S_STRINGTABLE + .long .strings_end - .strings_start + +.strings_start: + + .asciz "" + +.src1: + .asciz "bar" + +.strings_end: + + .balign 4 + + .long DEBUG_S_FILECHKSMS + .long .chksms_end - .chksms_start + +.chksms_start: + + .rept NUM_CHKSMS + .long .src1 - .strings_start + .byte 0 /* checksum length */ + .byte CHKSUM_TYPE_NONE + .short 0 /* padding */ + .endr + +.chksms_end: