From patchwork Sun Apr 2 22:23:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 78308 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1941930vqo; Sun, 2 Apr 2023 15:23:18 -0700 (PDT) X-Google-Smtp-Source: AKy350bhSDVPB93bEAeZbWyj9X7dav4iF2yWnl9sXoe+JQjRFEjv6wZQ1tawLrPBLPX5NHcE670+ X-Received: by 2002:a05:6402:517c:b0:4fb:9372:f837 with SMTP id d28-20020a056402517c00b004fb9372f837mr17638073ede.4.1680474198250; Sun, 02 Apr 2023 15:23:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680474198; cv=none; d=google.com; s=arc-20160816; b=X6Y4RE4ScgZIbV3fv8ladPI/TOHYuSPTQwpBsyA8XLjK3JxJUoEYvnYY3IC/JfohHy PtUthISsnL9Y49DNHdO52O7v7Lb5XPkfwrCupiQkkwyjQAdKNpzY3gJi4jcIOW6PxAnn kbb3R57UjPGjQimElOkb/y+LZN7WBmi/cleCX4K6oXuriPswynH4DgdEl9xaaGtcYus5 HOUiA33sUIPDpjP2jhEhdQ4oxG21e967081tqaLO2Rv6+hloKyUbGPjNd7Ib8J8T7Ifs 9m2LsfxfCPu9Tmjc+yIWsuh64Sm0lSn+2fwKcwGA1V3Yctv1UBgsmqV11+xG8f63XHfl Kxeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Zg9J0z0nyjHahIM0/zx9OoXO7J2PiPDfU3TbOutMRJ4=; b=uqTK7SJ7BAf7tB77qywcTTv//pHOd1Mw8JAjIyp+z8u6T21TU8TSAqwEqcSnfb5opK HagRhVqsLY9a7GsJAp5AEM9+1gjI/6O9FMo/wLSHE9RsiNG80av+7XIpwwkucTc/OwpP sGaH5f67hImvj4xxr8lJvpcdDpgJ6Fp90eiwv33iQUJEHn7HTEZW9unhYXQvg664hHWv REonHjgUna6x5eW0+OvIQbheLKOG8xVheT+mxMjyxahiuOe846U4XvS810f+5H98MXgn TZcmlNEUHNWoqZlc/UVej6O5o5ZSj0TKTKgmBOHraBDC8LhxjAFvHI8Zpx/x3JUaNvXz zlZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=GMFCnZN1; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id us9-20020a170906bfc900b0092bb8b36552si6123869ejb.615.2023.04.02.15.23.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Apr 2023 15:23:18 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=GMFCnZN1; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 10D753858414 for ; Sun, 2 Apr 2023 22:23:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 10D753858414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680474197; bh=Zg9J0z0nyjHahIM0/zx9OoXO7J2PiPDfU3TbOutMRJ4=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=GMFCnZN1FEVAHH5feouD+ZTA1ic7l2NsGccWAjceEDf9nVt4C1QIZ+0jS6aTxaEqC uxHAic5TTCssnNiCPK0EOBoAQIxgGeJ28SwGwOLDvgrig4KGmeXikdsQoXuvrIkkvR c/tnmS79RYU27DC2lBJ15VFD3g394ogg3IRZX/+0= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id 0E5AD3858C83 for ; Sun, 2 Apr 2023 22:23:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0E5AD3858C83 Received: by mail-pl1-x62b.google.com with SMTP id n14so10411838plc.8 for ; Sun, 02 Apr 2023 15:23:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680474186; 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=Zg9J0z0nyjHahIM0/zx9OoXO7J2PiPDfU3TbOutMRJ4=; b=d9feGfE4JEz7F5wnYEhVstORZO7OwXcAOvWE/fqXvl2LsDP5pegmHttjsrtqYxqAyG I2XJNdSuD1eaREnx0ui5G5GxmBYe6LWUv+YLMD4dZV3bst18l5R7n30ZTD1IQDLLjf02 gHmbvXxV68ch4VgtMpKmrJTUrchSkdUTp4S61lgqFtccMzm3SmU4q+ydOkxhq/DIABFX r3ncorWXSIAe3sXzWsBFEhMo83FrdzP97F0q62rIZPIsgEOsH2Ukbof3Yx8zRlRPQKMs 8DrDgem3L4NyI8ON7m0I/LiWYi6J90kOtGfQLy4iprSQo3Mncs+l8eW/ErRPQl9YZhGz mmAg== X-Gm-Message-State: AAQBX9eOuP434TMbrG8/WdA1uIFbiMdFcVem/+BwkQLad/Y1Ht+oA/xj hdugwKic8vhO1cio0/S6oZ4ysD3v99o= X-Received: by 2002:a17:90a:1954:b0:240:f8a6:55c7 with SMTP id 20-20020a17090a195400b00240f8a655c7mr4982896pjh.20.1680474186333; Sun, 02 Apr 2023 15:23:06 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:4c75:41d5:4aa9:f18e]) by smtp.gmail.com with ESMTPSA id fa20-20020a17090af0d400b00233afe09177sm8516599pjb.8.2023.04.02.15.23.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Apr 2023 15:23:05 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id A1B301142C55; Mon, 3 Apr 2023 07:53:03 +0930 (ACST) Date: Mon, 3 Apr 2023 07:53:03 +0930 To: binutils@sourceware.org Subject: rddbg.c stabs FIXMEs Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3034.5 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 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: , X-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra 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?1762104912887495089?= X-GMAIL-MSGID: =?utf-8?q?1762104912887495089?= This should sort out some very old FIXMEs in code handling stabs debug info. Necessary if we are to fuss over freeing up memory before objdump and objcopy exit. It is of course better from a user viewpoint to *not* free memory, which takes some time, and leave that to process exit. The only reason to do so is that having many memory leaks in binutils/ code tends to hide leaks in bfd/ or opcodes/, which we should care about. * budbg.h (parse_stab): Update prototype. * debug.h (debug_start_source): Update prototype. * debug.c (debug_start_source): Add name_used. Set if stashed. * rddbg.c (read_symbol_stabs_debugging_info): Always malloc stab string passed to parse_stab. Free stab string when unreferenced. (read_section_stabs_debugging_info): Likewise, and strings section contents. * stabs.c (parse_stab): Add string_used param. Set if string stashed. Pass to debug_start_source. Realloc file_types array rather that using malloc. Clarify comment about debug_make_indirect_type. diff --git a/binutils/budbg.h b/binutils/budbg.h index a507ff9acf4..4a93b4e29b4 100644 --- a/binutils/budbg.h +++ b/binutils/budbg.h @@ -39,7 +39,7 @@ extern void *start_stab (void *, bfd *, bool, asymbol **, long); extern bool finish_stab (void *, void *); extern bool parse_stab - (void *, void *, int, int, bfd_vma, const char *); + (void *, void *, int, int, bfd_vma, const char *, bool *); extern bool write_stabs_in_sections_debugging_info (bfd *, void *, bfd_byte **, bfd_size_type *, bfd_byte **, bfd_size_type *); diff --git a/binutils/debug.c b/binutils/debug.c index ccd708f26d0..fd9a98ad01b 100644 --- a/binutils/debug.c +++ b/binutils/debug.c @@ -713,7 +713,7 @@ debug_set_filename (void *handle, const char *name) include files in a single compilation unit. */ bool -debug_start_source (void *handle, const char *name) +debug_start_source (void *handle, const char *name, bool *name_used) { struct debug_handle *info = (struct debug_handle *) handle; struct debug_file *f, **pf; @@ -740,6 +740,7 @@ debug_start_source (void *handle, const char *name) memset (f, 0, sizeof *f); f->filename = name; + *name_used = true; for (pf = &info->current_file->next; *pf != NULL; diff --git a/binutils/debug.h b/binutils/debug.h index 7dcb0878e66..fd19c8bc8d4 100644 --- a/binutils/debug.h +++ b/binutils/debug.h @@ -435,7 +435,7 @@ extern bool debug_set_filename (void *, const char *); /* Change source files to the given file name. This is used for include files in a single compilation unit. */ -extern bool debug_start_source (void *, const char *); +extern bool debug_start_source (void *, const char *, bool *); /* Record a function definition. This implicitly starts a function block. The debug_type argument is the type of the return value. diff --git a/binutils/rddbg.c b/binutils/rddbg.c index 15081c3f7c7..fb59f526979 100644 --- a/binutils/rddbg.c +++ b/binutils/rddbg.c @@ -194,6 +194,7 @@ read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, { size_t len; char *f, *s; + bool f_used; if (stroff + strx >= strsize) { @@ -238,10 +239,13 @@ read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, free (f); f = s; } + if (!f) + f = xstrdup (s); save_stab (type, desc, value, s); - if (! parse_stab (dhandle, shandle, type, desc, value, s)) + if (! parse_stab (dhandle, shandle, type, desc, value, + f, &f_used)) { stab_context (); free_saved_stabs (); @@ -252,18 +256,14 @@ read_section_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, return false; } - /* Don't free f, since I think the stabs code - expects strings to hang around. This should be - straightened out. FIXME. */ + if (!f_used) + free (f); } } free_saved_stabs (); free (stabs); - - /* Don't free strings, since I think the stabs code expects - the strings to hang around. This should be straightened - out. FIXME. */ + free (strings); } } @@ -297,6 +297,7 @@ read_symbol_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, { const char *s; char *f; + bool f_used; if (shandle == NULL) { @@ -327,20 +328,22 @@ read_symbol_stabs_debugging_info (bfd *abfd, asymbol **syms, long symcount, f = n; s = n; } + if (!f) + f = xstrdup (s); save_stab (i.stab_type, i.stab_desc, i.value, s); if (! parse_stab (dhandle, shandle, i.stab_type, i.stab_desc, - i.value, s)) + i.value, f, &f_used)) { stab_context (); + free (f); free_saved_stabs (); return false; } - /* Don't free f, since I think the stabs code expects - strings to hang around. This should be straightened out. - FIXME. */ + if (!f_used) + free (f); } } diff --git a/binutils/stabs.c b/binutils/stabs.c index 85eebebafdd..f5a76dda5cc 100644 --- a/binutils/stabs.c +++ b/binutils/stabs.c @@ -422,11 +422,12 @@ finish_stab (void *dhandle, void *handle) bool parse_stab (void *dhandle, void *handle, int type, int desc, bfd_vma value, - const char *string) + const char *string, bool *string_used) { const char * string_end; struct stab_handle *info = (struct stab_handle *) handle; + *string_used = false; /* gcc will emit two N_SO strings per compilation unit, one for the directory name and one for the file name. We just collect N_SO strings as we see them, and start the new compilation unit when @@ -447,11 +448,11 @@ parse_stab (void *dhandle, void *handle, int type, int desc, bfd_vma value, info->file_start_offset = info->so_value; /* We need to reset the mapping from type numbers to types. We - can't free the old mapping, because of the use of - debug_make_indirect_type. */ + can only free the file_types array, not the stab_types + list entries due to the use of debug_make_indirect_type. */ info->files = 1; info->file_types = ((struct stab_types **) - xmalloc (sizeof *info->file_types)); + xrealloc (info->file_types, sizeof *info->file_types)); info->file_types[0] = NULL; info->so_string = NULL; @@ -566,21 +567,29 @@ parse_stab (void *dhandle, void *handle, int type, int desc, bfd_vma value, case N_SOL: /* Start an include file. */ - if (! debug_start_source (dhandle, string)) + if (! debug_start_source (dhandle, string, string_used)) return false; break; case N_BINCL: /* Start an include file which may be replaced. */ + *string_used = true; push_bincl (info, string, value); - if (! debug_start_source (dhandle, string)) + if (! debug_start_source (dhandle, string, string_used)) return false; break; case N_EINCL: /* End an N_BINCL include. */ - if (! debug_start_source (dhandle, pop_bincl (info))) - return false; + string = pop_bincl (info); + if (! debug_start_source (dhandle, string, string_used)) + { + free ((char *) string); + return false; + } + if (!*string_used) + free ((char *) string); + *string_used = false; break; case N_EXCL: