From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-x62f.google.com with SMTP id d3so3457825plr.10 for ; Thu, 22 Dec 2022 16:04:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=TsLgGC4iJstd2ME9WXnA/V/Zlq0K+ySQ7uElu6muNz4=; b=mZA+5MQFi2qj45rDRJGHUjX/05S7DQM8VzCVo8C1wFy+Ci1gANZIHNF7BTRHJLmprR FMzg5mRuiW84NQtIKHAgspOTf9dP45IN25OXwsNQ4AURzz+bWaI98qfe3RQqpTAdkLPp NtcEZbVU8gqs/VH4AojQXcJeSv4brO1UmxXPQCZ6VU6Up6+Roj+CzkqEO9V4FuDSjT8u uJiHjQn/rBRS0skNx1C6Ay0mTcFHvXQvJkSy91/xDyT8QgT0NfMXrqb2IhfyILsyCmus /PEaJsVImkoOO+WD5BJ0zS6SuhTLYzvPartS1qkz4pusmMJKD3dsHRy4jywC3HBQvbvA 966w== 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-Google-Smtp-Source: AMrXdXsrPpkIP769SpbNVL6BsKA7oGLy1V1hhqSrHyeTZ/NYZoghqCwKS+qY/muVlaEPxsU5D0LjZQ== 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 From: Alan Modra To: binutils@sourceware.org Subject: COFF build-id writes uninitialised data to file Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii 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 List-Id: 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 ................ -- Alan Modra Australia Development Lab, IBM