From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id E47A73835579 for ; Fri, 9 Dec 2022 01:52:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E47A73835579 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-x331.google.com with SMTP id v124-20020a1cac82000000b003cf7a4ea2caso4775277wme.5 for ; Thu, 08 Dec 2022 17:52:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=eK3s9gyPJGoCIB3cn8RWhonluPbzDyvsVWN1Hj/aPyo=; b=SxRQEf/dyw6apSL9Bom0vSVC5n6rGNMMpTatacoCjLR2e8qdMB5RwsEipxp2uYL8NY nDzcEkbHzvcUkDhaia71TkdLGk3tE0jJTkCbrr7KQS7mfB6cS9x+trekTTImdvWvdWG/ SNpED3Icqaojfi68xnjKw1QbQL7rAj4kN9GtqS31LTm15EIZCjuJLMs+w515bSH1KI9W HQLewsHbx9c8rCHRlAt8GMnez+TZGvUq2luJalhtQjtm7uxsL4+1HPJpw8zWyMUHMIaD LgXdN+l20hrT0xkA93vvl8v+LLF/dfvrifpiNN+TDA7k33SgYe8lqyFInK41pAs4i/0D CqmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=eK3s9gyPJGoCIB3cn8RWhonluPbzDyvsVWN1Hj/aPyo=; b=NFnaEmC1SR/2WgJm1rVegbQ4JVD2eSnx4N5b0E+fZTAAeVGtWt23hrT5zHlCLrOl96 ZaI8+e3A/7qxtI7aBEHoJ5qpzFIbkFZGM80m8ZZLg/qe6+L/mfvsa5BK5Brk9brgEOZE MkXM//d+/yTcBc+9tYSeMi8NefdpsQFG0oMoXOC+Y6m5O9t5VCZhfRsR7aZeE0mbNJD0 hFLFdDgbiUBB61ZTGRDevrqLmrSYMommWPg9sUM7QGsoTMljROZTXIDID2CTkZLPEcmI gp5TI4fODE2KIR5rTexpMt616JXgG7RdrsS0G1lklurqq3swgEzgRTHJmS8SKTo6XW14 enpg== X-Gm-Message-State: ANoB5plZPHS1ZizyUZgxPWt+q7ijE4a2Qx7pAKDDKnpXuzYwDYVRZvr1 h+FmaiIZV5rJt+hZZWzRFACjaVQWrdE= X-Google-Smtp-Source: AA0mqf549Vdg9u8Ki9wVMdYiNyZ9/1schm3bAo2niIonJJI+ev6GLO9LWGWOgyh5ioKnIspBD1SCFA== X-Received: by 2002:a05:600c:4909:b0:3d1:f3eb:c718 with SMTP id f9-20020a05600c490900b003d1f3ebc718mr3694996wmp.19.1670550767414; Thu, 08 Dec 2022 17:52:47 -0800 (PST) Received: from beren.harmstone.com ([2a02:8010:64ea:0:8eb8:7eff:fe53:9d5f]) by smtp.gmail.com with ESMTPSA id fc18-20020a05600c525200b003d04e4ed873sm7473139wmb.22.2022.12.08.17.52.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Dec 2022 17:52:46 -0800 (PST) Sender: Mark Harmstone From: Mark Harmstone To: binutils@sourceware.org, nickc@redhat.com Cc: Mark Harmstone Subject: [PATCH 04/10] ld: Write DEBUG_S_LINES entries in PDB file Date: Fri, 9 Dec 2022 01:52:34 +0000 Message-Id: <20221209015240.6348-4-mark@harmstone.com> X-Mailer: git-send-email 2.37.4 In-Reply-To: <20221209015240.6348-1-mark@harmstone.com> References: <20221209015240.6348-1-mark@harmstone.com> 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: --- ld/pdb.c | 111 +++++++++++++++++++++++++++- ld/pdb.h | 1 + ld/testsuite/ld-pe/pdb.exp | 2 +- ld/testsuite/ld-pe/pdb3-c13-info1.d | 8 +- ld/testsuite/ld-pe/pdb3a.s | 88 ++++++++++++++++++++++ 5 files changed, 204 insertions(+), 6 deletions(-) diff --git a/ld/pdb.c b/ld/pdb.c index 3edeab25332..a2f78c4c348 100644 --- a/ld/pdb.c +++ b/ld/pdb.c @@ -623,7 +623,8 @@ parse_string_table (bfd_byte *data, size_t size, static bool handle_debugs_section (asection *s, bfd *mod, struct string_table *strings, uint8_t **dataptr, uint32_t *sizeptr, - struct mod_source_files *mod_source) + struct mod_source_files *mod_source, + bfd *abfd) { bfd_byte *data = NULL; size_t off; @@ -637,6 +638,59 @@ handle_debugs_section (asection *s, bfd *mod, struct string_table *strings, if (!data) return false; + /* Resolve relocations. Addresses are stored within the .debug$S section as + a .secidx, .secrel32 pair. */ + + if (s->flags & SEC_RELOC) + { + struct internal_reloc *relocs; + struct internal_syment *symbols; + asection **sectlist; + unsigned int syment_count; + int sect_num; + struct external_syment *ext; + + syment_count = obj_raw_syment_count (mod); + + relocs = + _bfd_coff_read_internal_relocs (mod, s, false, NULL, true, NULL); + + symbols = xmalloc (sizeof (struct internal_syment) * syment_count); + sectlist = xmalloc (sizeof (asection *) * syment_count); + + ext = (struct external_syment *) (coff_data (mod)->external_syms); + + for (unsigned int i = 0; i < syment_count; i++) + { + bfd_coff_swap_sym_in (mod, &ext[i], &symbols[i]); + } + + sect_num = 1; + + for (asection *sect = mod->sections; sect; sect = sect->next) + { + for (unsigned int i = 0; i < syment_count; i++) + { + if (symbols[i].n_scnum == sect_num) + sectlist[i] = sect; + } + + sect_num++; + } + + if (!bfd_coff_relocate_section (abfd, coff_data (abfd)->link_info, mod, + s, data, relocs, symbols, sectlist)) + { + free (sectlist); + free (symbols); + free (data); + return false; + } + + free (sectlist); + free (symbols); + } + if (bfd_getl32 (data) != CV_SIGNATURE_C13) { free (data); @@ -689,6 +743,32 @@ handle_debugs_section (asection *s, bfd *mod, struct string_table *strings, string_table = (char *) data + off; break; + + case DEBUG_S_LINES: + { + uint16_t sect; + + if (size < sizeof (uint32_t) + sizeof (uint16_t)) + { + free (data); + bfd_set_error (bfd_error_bad_value); + return false; + } + + sect = bfd_getl16 (data + off + sizeof (uint32_t)); + + /* Skip GC'd symbols. */ + if (sect != 0) + { + c13_size += sizeof (uint32_t) + sizeof (uint32_t) + size; + + if (c13_size % sizeof (uint32_t)) + c13_size += + sizeof (uint32_t) - (c13_size % sizeof (uint32_t)); + } + + break; + } } off += size; @@ -733,6 +813,28 @@ handle_debugs_section (asection *s, bfd *mod, struct string_table *strings, bufptr += sizeof (uint32_t) + sizeof (uint32_t) + size; break; + + case DEBUG_S_LINES: + { + uint16_t sect; + + sect = bfd_getl16 (data + off + sizeof (uint32_t)); + + /* Skip if GC'd. */ + if (sect != 0) + { + bfd_putl32 (type, bufptr); + bufptr += sizeof (uint32_t); + + bfd_putl32 (size, bufptr); + bufptr += sizeof (uint32_t); + + memcpy (bufptr, data + off, size); + bufptr += size; + } + + break; + } } off += size; @@ -769,7 +871,8 @@ static bool populate_module_stream (bfd *stream, bfd *mod, uint32_t *sym_byte_size, struct string_table *strings, uint32_t *c13_info_size, - struct mod_source_files *mod_source) + struct mod_source_files *mod_source, + bfd *abfd) { uint8_t int_buf[sizeof (uint32_t)]; uint8_t *c13_info = NULL; @@ -784,7 +887,7 @@ populate_module_stream (bfd *stream, bfd *mod, uint32_t *sym_byte_size, if (!strcmp (s->name, ".debug$S") && s->size >= sizeof (uint32_t)) { if (!handle_debugs_section (s, mod, strings, &c13_info, - c13_info_size, mod_source)) + c13_info_size, mod_source, abfd)) { free (c13_info); free (mod_source->files); @@ -916,7 +1019,7 @@ create_module_info_substream (bfd *abfd, bfd *pdb, void **data, if (!populate_module_stream (stream, in, &sym_byte_size, strings, &c13_info_size, - &source->mods[mod_num])) + &source->mods[mod_num], abfd)) { for (unsigned int i = 0; i < source->mod_count; i++) { diff --git a/ld/pdb.h b/ld/pdb.h index e8f673c24a0..bbb106043c4 100644 --- a/ld/pdb.h +++ b/ld/pdb.h @@ -155,6 +155,7 @@ struct optional_dbg_header #define CV_SIGNATURE_C13 4 +#define DEBUG_S_LINES 0xf2 #define DEBUG_S_STRINGTABLE 0xf3 #define DEBUG_S_FILECHKSMS 0xf4 diff --git a/ld/testsuite/ld-pe/pdb.exp b/ld/testsuite/ld-pe/pdb.exp index 9dab41110ac..2e5f83477aa 100644 --- a/ld/testsuite/ld-pe/pdb.exp +++ b/ld/testsuite/ld-pe/pdb.exp @@ -870,7 +870,7 @@ proc test4 { } { return } - if ![ld_link $ld "tmpdir/pdb3.exe" "--pdb=tmpdir/pdb3.pdb tmpdir/pdb3a.o tmpdir/pdb3b.o"] { + if ![ld_link $ld "tmpdir/pdb3.exe" "--pdb=tmpdir/pdb3.pdb --gc-sections -e main tmpdir/pdb3a.o tmpdir/pdb3b.o"] { unsupported "Create PE image with PDB file" return } diff --git a/ld/testsuite/ld-pe/pdb3-c13-info1.d b/ld/testsuite/ld-pe/pdb3-c13-info1.d index f92062ba4e5..5a4f94861c7 100644 --- a/ld/testsuite/ld-pe/pdb3-c13-info1.d +++ b/ld/testsuite/ld-pe/pdb3-c13-info1.d @@ -5,4 +5,10 @@ Contents of section .data: 0000 f4000000 30000000 02000000 10016745 ....0.........gE 0010 2301efcd ab8998ba dcfe1023 45670000 #..........#Eg.. 0020 06000000 100198ba dcfe1023 45676745 ...........#EggE - 0030 2301efcd ab890000 #....... \ No newline at end of file + 0030 2301efcd ab890000 f2000000 58000000 #...........X... + 0040 00000000 01000000 14000000 00000000 ................ + 0050 02000000 1c000000 00000000 01000080 ................ + 0060 04000000 02000080 18000000 02000000 ................ + 0070 1c000000 08000000 03000080 0c000000 ................ + 0080 04000080 00000000 01000000 14000000 ................ + 0090 10000000 05000080 ........ \ No newline at end of file diff --git a/ld/testsuite/ld-pe/pdb3a.s b/ld/testsuite/ld-pe/pdb3a.s index 71795b53a66..1df84a344f6 100644 --- a/ld/testsuite/ld-pe/pdb3a.s +++ b/ld/testsuite/ld-pe/pdb3a.s @@ -1,4 +1,5 @@ .equ CV_SIGNATURE_C13, 4 +.equ DEBUG_S_LINES, 0xf2 .equ DEBUG_S_STRINGTABLE, 0xf3 .equ DEBUG_S_FILECHKSMS, 0xf4 .equ CHKSUM_TYPE_MD5, 1 @@ -50,3 +51,90 @@ .chksms_end: .balign 4 + +.long DEBUG_S_LINES +.long .lines_end - .lines_start + +.lines_start: + +.secrel32 main +.secidx main +.short 0 # flags +.long .main_end - main # length of region + +.lines_block1: + +.long 0 # file ID 0 (foo) +.long 2 # no. lines +.long .lines_block2 - .lines_block1 # length + +.long .line1 - main +.long 0x80000001 # line 1 +.long .line2 - main +.long 0x80000002 # line 2 + +.lines_block2: + +.long 0x18 # file ID 18 (bar) +.long 2 # no. lines +.long .lines_block3 - .lines_block2 # length + +.long .line3 - main +.long 0x80000003 # line 3 +.long .line4 - main +.long 0x80000004 # line 4 + +.lines_block3: + +.long 0 # file ID 0 (foo) +.long 1 # no. lines +.long .lines_end - .lines_block3 # length + +.long .line5 - main +.long 0x80000005 # line 5 + +.lines_end: + +.long DEBUG_S_LINES +.long .lines_end2 - .lines_start2 + +.lines_start2: + +.secrel32 gcfunc +.secidx gcfunc +.short 0 # flags +.long .gcfunc_end - gcfunc # length of region + +.lines_block4: + +.long 0 # file ID 0 (foo) +.long 1 # no. lines +.long .lines_end2 - .lines_block4 # length + +.long .line6 - gcfunc +.long 0x80000006 # line 6 + +.lines_end2: + +.text + +.global main +main: +.line1: + .long 0x12345678 +.line2: + .long 0x12345678 +.line3: + .long 0x12345678 +.line4: + .long 0x12345678 +.line5: + .long 0x12345678 +.main_end: + +.section "gcsect" + +gcfunc: +.line6: + .long 0x12345678 +.gcfunc_end: -- 2.37.4