From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id 7BE143858C62 for ; Mon, 20 Feb 2023 14:13:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7BE143858C62 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-wr1-x434.google.com with SMTP id c5so2051238wrr.5 for ; Mon, 20 Feb 2023 06:13:35 -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=JblwmfjX6MX/qYm92Oys1qKZJHcES84orfOp21rYfuk=; b=fAdivR295NM1gTChEx3lVui6pr8TnvW0UiQkpYLv0u9CBZnh47TaliboAbz/XA4yhQ 1cZvBioMYxSS0q2aZ4pg60+r4wwO4FIt52i6xdYwWJU4kktE+J7ffOvGwJBxswsTsli1 XW5QS/rME7gAnE7ST0JQF4U7zar6JqEg1OLS48FWJ+UZKyY8texDG+Oi8+6q4UKxbgxf n4+JOP6Yf+B8I4f+DfLFoabyntTzXxbP3WaOcEWQp4N8DZNA2C15e5sUi7RjYCSgMbiY GaFq829gEcyEiP53+eCa4ZJCJjDi1mfTSXdPk/+yQgRne1t/rEEai7aLj7vDtGUG2CWN nAJQ== 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=JblwmfjX6MX/qYm92Oys1qKZJHcES84orfOp21rYfuk=; b=poVT+q/tTvD5nHpsSxw2jZ5t1342L+b2CctH/XfBDZeHqADbunTxS+kE6zr1pnDuUA 9JgfKpKWVF5zuR0zXJdrb5GB5gqE8M26lKMGuQjNy3Y2pRZe6ZSF4I7oe7gfLIa2rxOt 9GDBI2VS1gHG3dsbah4wLHcaRKcyd2kroeoVkQjPArWkT5dYgWYxGU2A/hgMRX/VRGCb AZqRdN8rjnOTtRrjuwOCDDnNAMeuiWExDiNX5KstFc6aw959p1I21Lxjddpscyv3dQKd 6jscA2j1fXpETwbR0xO/VwNbb8/CnlWt65GtRlnVBSHAPtOcrvwuNNeSosOZmoE8Qrd+ uY/g== X-Gm-Message-State: AO0yUKU8zC7NNWZ3ornXHJ2XlkWBWjr3YvqBMNwBKqpecZkfVUHoRJ0S 2zBQkkKfTe/1FA0F6vRNp7IU+GBp6qs= X-Google-Smtp-Source: AK7set+TRvjUxsRAoyRcJi4s1ZVRVa0rUa/pPxGB4qHF1K/xncEXKWwF84YMLBAQm2sC44BNXtMl7g== X-Received: by 2002:a5d:5902:0:b0:2c5:5dbf:1cc2 with SMTP id v2-20020a5d5902000000b002c55dbf1cc2mr1740242wrd.32.1676902413834; Mon, 20 Feb 2023 06:13:33 -0800 (PST) Received: from beren.harmstone.com ([2a02:8010:64ea:0:8eb8:7eff:fe53:9d5f]) by smtp.gmail.com with ESMTPSA id j27-20020a5d453b000000b002c577e2fc87sm3513769wra.15.2023.02.20.06.13.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Feb 2023 06:13:33 -0800 (PST) Sender: Mark Harmstone From: Mark Harmstone To: binutils@sourceware.org Cc: Mark Harmstone Subject: [PATCH] ld: Sort section contributions in PDB files Date: Mon, 20 Feb 2023 14:13:28 +0000 Message-Id: <20230220141328.20441-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.1 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 | 81 +++++++++++++++++------ ld/testsuite/ld-pe/pdb2-section-contrib.d | 6 +- 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/ld/pdb.c b/ld/pdb.c index 12c4ac4b112..6ac45dea4f2 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 = (const struct in_sc *) p1; + const struct in_sc *sc2 = (const struct in_sc *) 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,7 @@ 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; for (bfd *in = coff_data (abfd)->link_info->input_bfds; in; in = in->link.next) @@ -4168,8 +4197,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 (struct in_sc)); + sc2 = sc_in; mod_index = 0; for (bfd *in = coff_data (abfd)->link_info->input_bfds; in; @@ -4177,32 +4209,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); + + sc = + (struct section_contribution *) ((uint8_t *) *data + sizeof (uint32_t)); + + 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