From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by sourceware.org (Postfix) with ESMTPS id 4429E3858D1E for ; Mon, 27 Feb 2023 00:59:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4429E3858D1E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=harmstone.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wm1-x332.google.com with SMTP id bg16-20020a05600c3c9000b003eb34e21bdfso2603145wmb.0 for ; Sun, 26 Feb 2023 16:59:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:from:to:cc:subject:date:message-id:reply-to; bh=aCe0vSxN/1095c/q6kEJ4HEqrlXoKBiQ4UQHMFE7QUQ=; b=RNwn3Zg0pb7nHU/w3wqZiZJ+QUGbvbWNJ1+DTDohTDZPyhlDrdwEqOJ5cwTZoYc355 1smyRgFTBC+TqU/D88h1wvK11cl2J683GQ5yIso1f0Kp28VHE5/3gFCnco30bS1Kc0yn UscQlvyMOxSPn/fH2TMS1ykGFX0dgFuVKlCXzdzEaiUAWZe8RgyZYTLapRmO0VtBe9iG Qy9wXQCnN9k6EyWDPBSKij1viN3d1D5cC9GCh8Kg64+ny+XnglNynHgBWC1JUnv+9mul 7VMrZCEGMPWjuOLhDd7ITkPQOBNIYiBGQbN8troH72bhdivo+GGZOPQtaFFstkDyn1g5 q08w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:sender:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=aCe0vSxN/1095c/q6kEJ4HEqrlXoKBiQ4UQHMFE7QUQ=; b=5W7lqd/if0T/RfTDtaIeKp+lHpcw+YGx1ps75IsNf+s7yR2Xy7SYWenoOTkntdQnJ7 1/nAxAez4hoz6YG7Zy9osedMlPnNRQghmoWbD2gxr6Y8cclvmYOmwF/h1w7UdxMmgoc7 o+0APgIOhMWQbdGkCNFNFVnFWYOXf7PVPhrbkPIvn6AH+HJwE47PvF3gKY27d9JZLYgX 8oTPFfzSo1bSt3DLhYaoqdoM8z63Zdlq+n153yjgjBUydV3L4wQCENqT+Lwiul8dl1gB uRoc9m5wYOyUaGqYZ/IZX/adq10TQgPeju3N+mnssYkzLN23xQL+hsY/EEIU82k9ByY8 rezQ== X-Gm-Message-State: AO0yUKXZLx3YChl7w1+2M8TrYDlq8Tug86UUHLlhfwB8cHDw6B5matA8 cHKSXUQgt/Xwq4kIMzGlTz5UBbYevoE= X-Google-Smtp-Source: AK7set+9MUXJU9IJboPNhoeukTWI8N9hkOMd3BZE+ii454mtQGDq3p4EjcuP6gOU76i9WkTXxlmc3Q== X-Received: by 2002:a05:600c:1d92:b0:3ea:8ed9:8f03 with SMTP id p18-20020a05600c1d9200b003ea8ed98f03mr9960368wms.24.1677459577459; Sun, 26 Feb 2023 16:59:37 -0800 (PST) Received: from beren.harmstone.com ([2a02:8010:64ea:0:8eb8:7eff:fe53:9d5f]) by smtp.gmail.com with ESMTPSA id q8-20020a1ce908000000b003e70a7c1b73sm7138998wmc.16.2023.02.26.16.59.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Feb 2023 16:59:36 -0800 (PST) Sender: Mark Harmstone From: Mark Harmstone To: binutils@sourceware.org Cc: Mark Harmstone Subject: [PATCH v2] ld: Sort section contributions in PDB files Date: Mon, 27 Feb 2023 00:59:35 +0000 Message-Id: <20230227005935.12270-1-mark@harmstone.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,GIT_PATCH_0,HEADER_FROM_DIFFERENT_DOMAINS,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: Microsoft's DIA library, and thus also MSVC and WinDbg, expects section contributions to be ordered by section number and offset, otherwise it's unable to resolve line numbers. --- ld/pdb.c | 82 +++++++++++++++++------ ld/testsuite/ld-pe/pdb2-section-contrib.d | 6 +- 2 files changed, 66 insertions(+), 22 deletions(-) diff --git a/ld/pdb.c b/ld/pdb.c index 12c4ac4b112..07622ed4987 100644 --- a/ld/pdb.c +++ b/ld/pdb.c @@ -107,6 +107,13 @@ struct globals htab_t hashmap; }; +struct in_sc +{ + asection *s; + uint16_t sect_num; + uint16_t mod_index; +}; + static const uint32_t crc_table[] = { 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, @@ -4118,6 +4125,27 @@ find_section_number (bfd *abfd, asection *sect) return 0; } +/* Used as parameter to qsort, to sort section contributions by section and + offset. */ +static int +section_contribs_compare (const void *p1, const void *p2) +{ + const struct in_sc *sc1 = p1; + const struct in_sc *sc2 = p2; + + if (sc1->sect_num < sc2->sect_num) + return -1; + if (sc1->sect_num > sc2->sect_num) + return 1; + + if (sc1->s->output_offset < sc2->s->output_offset) + return -1; + if (sc1->s->output_offset > sc2->s->output_offset) + return 1; + + return 0; +} + /* Create the substream which maps addresses in the image file to locations in the original object files. */ static bool @@ -4128,6 +4156,8 @@ create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size) uint16_t mod_index; char *sect_flags; file_ptr offset; + struct in_sc *sc_in, *sc2; + uint32_t *ptr; for (bfd *in = coff_data (abfd)->link_info->input_bfds; in; in = in->link.next) @@ -4168,8 +4198,11 @@ create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size) offset += sizeof (struct external_scnhdr); } - sc = - (struct section_contribution *) ((uint8_t *) *data + sizeof (uint32_t)); + /* Microsoft's DIA expects section contributions to be sorted by section + number and offset, otherwise it will be unable to resolve line numbers. */ + + sc_in = xmalloc (num_sc * sizeof (* sc_in)); + sc2 = sc_in; mod_index = 0; for (bfd *in = coff_data (abfd)->link_info->input_bfds; in; @@ -4177,32 +4210,43 @@ create_section_contrib_substream (bfd *abfd, void **data, uint32_t *size) { for (asection *s = in->sections; s; s = s->next) { - uint16_t sect_num; - if (s->size == 0 || discarded_section (s)) continue; - sect_num = find_section_number (abfd, s->output_section); - - memcpy (&sc->characteristics, - sect_flags + ((sect_num - 1) * sizeof (uint32_t)), - sizeof (uint32_t)); - - bfd_putl16 (sect_num, &sc->section); - bfd_putl16 (0, &sc->padding1); - bfd_putl32 (s->output_offset, &sc->offset); - bfd_putl32 (s->size, &sc->size); - bfd_putl16 (mod_index, &sc->module_index); - bfd_putl16 (0, &sc->padding2); - bfd_putl32 (0, &sc->data_crc); - bfd_putl32 (0, &sc->reloc_crc); + sc2->s = s; + sc2->sect_num = find_section_number (abfd, s->output_section); + sc2->mod_index = mod_index; - sc++; + sc2++; } mod_index++; } + qsort (sc_in, num_sc, sizeof (struct in_sc), section_contribs_compare); + + ptr = *data; + sc = (struct section_contribution *) (ptr + 1); /* Skip the version word. */ + + for (unsigned int i = 0; i < num_sc; i++) + { + memcpy (&sc->characteristics, + sect_flags + ((sc_in[i].sect_num - 1) * sizeof (uint32_t)), + sizeof (uint32_t)); + + bfd_putl16 (sc_in[i].sect_num, &sc->section); + bfd_putl16 (0, &sc->padding1); + bfd_putl32 (sc_in[i].s->output_offset, &sc->offset); + bfd_putl32 (sc_in[i].s->size, &sc->size); + bfd_putl16 (sc_in[i].mod_index, &sc->module_index); + bfd_putl16 (0, &sc->padding2); + bfd_putl32 (0, &sc->data_crc); + bfd_putl32 (0, &sc->reloc_crc); + + sc++; + } + + free (sc_in); free (sect_flags); return true; diff --git a/ld/testsuite/ld-pe/pdb2-section-contrib.d b/ld/testsuite/ld-pe/pdb2-section-contrib.d index dd9437214bb..65ed76dafdc 100644 --- a/ld/testsuite/ld-pe/pdb2-section-contrib.d +++ b/ld/testsuite/ld-pe/pdb2-section-contrib.d @@ -4,9 +4,9 @@ tmpdir/pdb2-sc: file format binary Contents of section .data: 0000 2dba2ef1 01000000 00000000 10000000 -............... 0010 20000060 00000000 00000000 00000000 ..`............ - 0020 02000000 00000000 3d000000 40000040 ........=...@..@ - 0030 00000000 00000000 00000000 01000000 ................ - 0040 10000000 10000000 20000060 01000000 ........ ..`.... + 0020 01000000 10000000 10000000 20000060 ............ ..` + 0030 01000000 00000000 00000000 02000000 ................ + 0040 00000000 3d000000 40000040 00000000 ....=...@..@.... 0050 00000000 00000000 04000000 00000000 ................ 0060 0c000000 40000042 02000000 00000000 ....@..B........ 0070 00000000 .... \ No newline at end of file -- 2.39.1