From patchwork Sun Nov 27 02:38:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Harmstone X-Patchwork-Id: 26308 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp4920507wrr; Sat, 26 Nov 2022 18:39:05 -0800 (PST) X-Google-Smtp-Source: AA0mqf7ZME9fH5pb520Nq+c4wsP1L+8GvMxDKeZhmPhLob9iUwEb3V5N+vDQypwjvGrYBnHmlauQ X-Received: by 2002:a05:6402:7d1:b0:460:858e:a8e1 with SMTP id u17-20020a05640207d100b00460858ea8e1mr24234224edy.401.1669516745912; Sat, 26 Nov 2022 18:39:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669516745; cv=none; d=google.com; s=arc-20160816; b=yL49aIx9TF4DdXKe6RZQmHx8Sn56GBJ0uPNGFA2hiR3W+77HRHH2bFiw0Ox/7H2C+O vSJ1doPReATP+F8pC0P4Pq4KleyErefbuOy0FlPuRw82drht/oYEUGn1jdnpTs2gzyS2 rncRVNqYBCGqDv9cSDA4NECGhQcUMLwwF8IH05mqk/9p2keX7r/hy4B2ef2EviamYKTO AxOWwWJAROjcmMx4wDVIPOq3FMlPQSyww2pgvmU6IWry5pmZ+tuO+/SBYkoKnjQVkdG/ l+1tXPIXrHT1nZ/MnYMD9JuzQht6uOO3ezrXojTHIxF/lvjkqMpPsGTyQxobPurLw9SG SU7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:dmarc-filter:delivered-to; bh=XTwfzy17AGTM61b/37zMwEWdX+3NVHz9Mvaeamrt/60=; b=zZDhl/SHB4rrIQq2Zq8wK/Cba1PM0q76XBcEvWOBYsq0UwxAR3JSF9Mt48Jk0cU5pe cwzIDtGB5L2OObXzQVl38oifVN6CkqzIY2F7Xo2fHGwQTOfDU+EdQcI3iwi3/n+/FQip +mCi0eV4rAMbRWQ3n5mgVQfCRwffmwJmDv+rzRGHIG173gR9+Sd0f7z9qKvJLqNiESMr ybOl4sCbme+x3756qUz7lG2V2PHmRq3G2nk9k4KQc/LesUqjR4cHiJlK0o23tGAMOjjf m3PLCXgLbUat3NZv9T40p5/R/2PMQHZc0pUvl/gUQNgr+9eYnJqqWhoeLSweRgtW1dV1 BuhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20210112 header.b=dbFR6vOP; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id dm20-20020a170907949400b0078db89b2566si8052871ejc.699.2022.11.26.18.39.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Nov 2022 18:39:05 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20210112 header.b=dbFR6vOP; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0089C3852C42 for ; Sun, 27 Nov 2022 02:39:00 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id 48782385B19A for ; Sun, 27 Nov 2022 02:38:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 48782385B19A 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-x32f.google.com with SMTP id j5-20020a05600c410500b003cfa9c0ea76so6064548wmi.3 for ; Sat, 26 Nov 2022 18:38:53 -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=XTwfzy17AGTM61b/37zMwEWdX+3NVHz9Mvaeamrt/60=; b=dbFR6vOPrdI4MitkK7I15NC9lh2fOkmGRNOaqzBc6YfM8tiJyQ3SbePPSuBkBFbrCg 9cGOpe5QPAUNlEzfHHj8P5Rk4PIyJbgV1Hu8tjfilJnR4OkVcRQfOd9t+ynpr+hv+gZz 0tNOzxk0/94Zyo+9C+ogZLnkCtw9oYqgte9gFONmMKqaJ7nJGJEy30onjpV7iIbScsyk otTrdSNNN9WOSsAypNPEjefN2yk5p3DHSqIxsVKcy+OrMxJCdqrvPQZ0Iius5ADXXutw RwPhWYr3nYB5+jIzNAKtUPI+ivB0Yr8Bw5UmI2a/82ztfxhLZlJzfpGKDTMwO0M/Fuey qpdw== 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=XTwfzy17AGTM61b/37zMwEWdX+3NVHz9Mvaeamrt/60=; b=u8zF97md7Dww+cvBajkVz8XKVX+0E6ithtIpkgO9JxrS0kG4LA9JvfM+L5KK3aS0bK sxWzVwJgoxQ0EbYklOO4H1SjCZCzuDADmI59WJOXMKrgdJVFgSfe22k2Js0CWHJORnKl DEVby81Aylkjsf2TPxB2/xtWz3NEMHe7re66fNPEbG9cBwdYEQD5HExjiKfsVstbBxq+ 3pV9KL1CuBMp8EQO5fnjEnc7qv4sRQ9DT5y6A1eS04dU5GJyVD5jOWuBbhvcay0/3uqN 1Gp5DaNFgBkyosw6PPN55yC6x5GKEFtgAEJ4foDVhlJudRyEzPmTPPSFP6UP94vF/plq 8pRQ== X-Gm-Message-State: ANoB5pn+eDwrL+xReKrB0kts+oUTJpPbRyxNPkvHETmC5bw100TnLCBY /GqfqEYvq5+tnBTo9g3TlWdrr7xX59k= X-Received: by 2002:a05:600c:3110:b0:3cf:b07a:cd56 with SMTP id g16-20020a05600c311000b003cfb07acd56mr35439257wmo.143.1669516731764; Sat, 26 Nov 2022 18:38:51 -0800 (PST) Received: from beren.harmstone.com ([2a02:8010:64ea:0:8eb8:7eff:fe53:9d5f]) by smtp.gmail.com with ESMTPSA id l35-20020a05600c1d2300b003cf878c4468sm15890384wms.5.2022.11.26.18.38.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 26 Nov 2022 18:38:51 -0800 (PST) From: Mark Harmstone To: binutils@sourceware.org Cc: Mark Harmstone Subject: [PATCH] ld: Write DEBUG_S_LINES entries in PDB file Date: Sun, 27 Nov 2022 02:38:40 +0000 Message-Id: <20221127023840.32080-2-mark@harmstone.com> X-Mailer: git-send-email 2.37.4 In-Reply-To: <20221127023840.32080-1-mark@harmstone.com> References: <20221125025433.26818-1-mark@harmstone.com> <20221127023840.32080-1-mark@harmstone.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.2 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 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: , 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?1750615191273876473?= X-GMAIL-MSGID: =?utf-8?q?1750615191273876473?= This extracts the DEBUG_S_LINES subsections of the .debug$S section of the input files, resolves the relocations, and copies them into the module symbol stream of the PDB. --- 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 42bb1b3a91b..cfc76004227 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; @@ -734,6 +814,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; @@ -770,7 +872,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; @@ -785,7 +888,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); @@ -918,7 +1021,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: