From patchwork Fri Dec 23 00:04:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 36015 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp51874wrn; Thu, 22 Dec 2022 16:05:07 -0800 (PST) X-Google-Smtp-Source: AMrXdXufaywr8HWcNIqWppnydeEM56p/lPs+QLK+FDyAg8BA6rVDH5yH78+QIOIRrGNggDGa7Maw X-Received: by 2002:a17:907:8b09:b0:7c1:6e82:35fc with SMTP id sz9-20020a1709078b0900b007c16e8235fcmr7894537ejc.40.1671753907696; Thu, 22 Dec 2022 16:05:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671753907; cv=none; d=google.com; s=arc-20160816; b=uBcWgzLZ5cs/NtTWKRgEubKRHhNfY0V7pRyGii/TfMQUSSk1OaZulSUbzLzH81gup4 eRLcsYRS4tCLzWomXzANAUP42VY/iPOrms82t4DBRvZxmYtn+aYQggGGfe+HyOPQYbkS /HVGear/D0Bt7wA6exB5OnJXFdBbiqjC5ShpIos2H1hhmLBbAvd2t7g2vBASwd/gCu9m BDSDtKC7vf3+8gQoS4GM7Jdt5T3VXCyLFywAzVK4cL9n807lxJ6dF6AK9ut0b5z8Y1uH m6MYmZCQbwnUGHxL5YUP8Et9ys9uzTf6wX9WAbNT2JclfBrBf/qT8snkiGsrak9gvRL6 kq4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=TsLgGC4iJstd2ME9WXnA/V/Zlq0K+ySQ7uElu6muNz4=; b=S7LDbSCME/Ocb2mMlxpD7Pemttwvvy4WmzaaqhP7XTwo+H1nfenp6G/Kbspv6YoJ2+ zhSzi+mQuSHwA1rWBnLYjuN6zAJpPMG6qoSSXtzeghD2sYUE08swGBr6txHPyX3KmZoq 8YeQzXGoED81u1fylkHkf1R4UwOICRgsKdvMC4LjvkW1jrkOXVp5087hAxPSRL+ohIp5 m1nsPwd8Q4K8HWdzt1KNiesD5QXdLsDfO0dhvWlE3oxv95Rmlwk1voNKDtm1YUzvNWm9 BrfcHgNx03e2/g4oux82uFWxLyIiPIRIsJZz1S3lIdh1TxiV65bwEURN+XjuYtUJj9v+ alow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=MZzY21Gm; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id dn16-20020a17090794d000b007d6648874a2si1385444ejc.711.2022.12.22.16.05.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Dec 2022 16:05:07 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=MZzY21Gm; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BA1AF385802F for ; Fri, 23 Dec 2022 00:05:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BA1AF385802F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671753906; bh=TsLgGC4iJstd2ME9WXnA/V/Zlq0K+ySQ7uElu6muNz4=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=MZzY21GmtrMpmETbU0cl2EGlWgugkMca+GFkHPWgxpAOIM6XVrhF0EXi7h8XsNaEA KQvOrnp8B/3KStXqJu0mO9IufyDlwq4cQIOzLdcO4Xzmq6qSJe5iTOdYpS0gmkwVjJ HBAXzw7ridFzU55DTfPh9ghoZHDNWmTg3gv8iVwE= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id 20FC83858D1E for ; Fri, 23 Dec 2022 00:04:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 20FC83858D1E Received: by mail-pl1-x62f.google.com with SMTP id d3so3457825plr.10 for ; Thu, 22 Dec 2022 16:04:57 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TsLgGC4iJstd2ME9WXnA/V/Zlq0K+ySQ7uElu6muNz4=; b=3jTIMX0Z21zNxdO5rlS/VogItnD0KAhGVQhCnuKD4vWVoKjDF9vdY6MAHXWZtEyIhp 3ZVsjAhdwL6aaTNvz8lZrT8BVZGqElbwxkx074FiorqOn9PFzEVoBja/YH1g2TS37vP5 6eNCvAmiM1j24+IxAuPC6lTeU/TgbDuDn9KpYR9xTcSdyZ3bwc7XOlmliNG1F88RsrJW Pqzjc1v8AbkoDBDB9/irBfG61ClA4PxlHTSyh6wUod3f9kYmWBz8EqcT0ea4ovCj0ryw asu3CNiXn9WOz2d7SiG8guRyzK3Fdfrqi7OJzbQmR6ldzt2HR7AdHcgQqv1u05fIFp26 7/vQ== X-Gm-Message-State: AFqh2koGFMM8V6NHBBB/iabPaYkwNK48gaDZwYn9rIHNhY0oVhyrkZq2 YBses4OhBsvdDXFvT+Tyyrko9koQNBg= X-Received: by 2002:a17:902:d3c9:b0:190:eec0:9fe3 with SMTP id w9-20020a170902d3c900b00190eec09fe3mr8542679plb.48.1671753895876; Thu, 22 Dec 2022 16:04:55 -0800 (PST) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:ff9a:bf9c:868:302c]) by smtp.gmail.com with ESMTPSA id p8-20020a170902780800b0017f59ebafe7sm1042373pll.212.2022.12.22.16.04.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Dec 2022 16:04:55 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 18D7F11400D5; Fri, 23 Dec 2022 10:34:53 +1030 (ACDT) Date: Fri, 23 Dec 2022 10:34:53 +1030 To: binutils@sourceware.org Subject: COFF build-id writes uninitialised data to file Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3034.8 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, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1752961025394967085?= X-GMAIL-MSGID: =?utf-8?q?1752961025394967085?= 1) The first write in write_build_id wrote rubbish past the struct external_IMAGE_DEBUG_DIRECTORY, which was later overwritten with correct data. No user visible problem there, except that tools like valgrind complain. 2) The size for the pdb name was incorrectly calculated. * emultempl/pe.em (write_build_id): Write the debug directory, not the entire section contents. (setup_build_id): Add size for the base name of pdb_name, not the full path. * emultempl/pep.em: Likewise. * testsuite/ld-pe/pdb2-section-contrib.d: Update. diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index 907259535b7..1e0e23df043 100644 --- a/ld/emultempl/pe.em +++ b/ld/emultempl/pe.em @@ -1285,7 +1285,6 @@ write_build_id (bfd *abfd) asection *asec; struct bfd_link_order *link_order = NULL; unsigned char *contents; - bfd_size_type size; bfd_size_type build_id_size; unsigned char *build_id; const char *pdb_base_name = NULL; @@ -1320,7 +1319,6 @@ write_build_id (bfd *abfd) if (t->build_id.sec->contents == NULL) t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size); contents = t->build_id.sec->contents; - size = t->build_id.sec->size; build_id_size = compute_build_id_size (t->build_id.style); build_id = xmalloc (build_id_size); @@ -1351,7 +1349,7 @@ write_build_id (bfd *abfd) if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0) return 0; - if (bfd_bwrite (contents, size, abfd) != size) + if (bfd_bwrite (contents, sizeof (*ext), abfd) != sizeof (*ext)) return 0; if (pdb) @@ -1417,7 +1415,7 @@ setup_build_id (bfd *ibfd) + sizeof (CV_INFO_PDB70) + 1; if (pdb_name) - s->size += strlen (pdb_name); + s->size += strlen (lbasename (pdb_name)); return true; } diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em index e2c538e6d99..e61414893ad 100644 --- a/ld/emultempl/pep.em +++ b/ld/emultempl/pep.em @@ -1277,7 +1277,6 @@ write_build_id (bfd *abfd) asection *asec; struct bfd_link_order *link_order = NULL; unsigned char *contents; - bfd_size_type size; bfd_size_type build_id_size; unsigned char *build_id; const char *pdb_base_name = NULL; @@ -1312,7 +1311,6 @@ write_build_id (bfd *abfd) if (t->build_id.sec->contents == NULL) t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size); contents = t->build_id.sec->contents; - size = t->build_id.sec->size; build_id_size = compute_build_id_size (t->build_id.style); build_id = xmalloc (build_id_size); @@ -1343,7 +1341,7 @@ write_build_id (bfd *abfd) if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0) return 0; - if (bfd_bwrite (contents, size, abfd) != size) + if (bfd_bwrite (contents, sizeof (*ext), abfd) != sizeof (*ext)) return 0; if (pdb) @@ -1409,7 +1407,7 @@ setup_build_id (bfd *ibfd) + sizeof (CV_INFO_PDB70) + 1; if (pdb_name) - s->size += strlen (pdb_name); + s->size += strlen (lbasename (pdb_name)); return true; } diff --git a/ld/testsuite/ld-pe/pdb2-section-contrib.d b/ld/testsuite/ld-pe/pdb2-section-contrib.d index 3afeb149e4b..dd9437214bb 100644 --- a/ld/testsuite/ld-pe/pdb2-section-contrib.d +++ b/ld/testsuite/ld-pe/pdb2-section-contrib.d @@ -4,7 +4,7 @@ tmpdir/pdb2-sc: file format binary Contents of section .data: 0000 2dba2ef1 01000000 00000000 10000000 -............... 0010 20000060 00000000 00000000 00000000 ..`............ - 0020 02000000 00000000 44000000 40000040 ........D...@..@ + 0020 02000000 00000000 3d000000 40000040 ........=...@..@ 0030 00000000 00000000 00000000 01000000 ................ 0040 10000000 10000000 20000060 01000000 ........ ..`.... 0050 00000000 00000000 04000000 00000000 ................