From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id yK86CenalGm9Sz8AWB0awg (envelope-from ) for ; Tue, 17 Feb 2026 16:17:29 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=oct2025 header.b=ViMGHGGB; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 063B31E0BA; Tue, 17 Feb 2026 16:17:29 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id A84C01E08D for ; Tue, 17 Feb 2026 16:17:26 -0500 (EST) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 0B5834BA23D0 for ; Tue, 17 Feb 2026 21:17:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0B5834BA23D0 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=oct2025 header.b=ViMGHGGB Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id 8F8244BA2E1C for ; Tue, 17 Feb 2026 21:16:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8F8244BA2E1C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=polymtl.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=polymtl.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8F8244BA2E1C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=132.207.4.11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771363018; cv=none; b=kBMe0u/A0M2GfzwuQ5YrbG6bIE1iIsYYW4e4aJms40pj96vr1Pm9eYGP1MEHcpyrBaoX3LCSIV0Cxms/mSE16RW89LWuQsjvmVhpliiu3PuImoEoqx4D+cDgAxbkdjz9OXP7VS6ONDBuW5mqXxNUlg9C5jRZNmG6HcJR9fH0yLY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1771363018; c=relaxed/simple; bh=MhhIc5RKVTdcMKhrgRfejiBYLeTXvm6+WI74/kE+uY4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Qf7zicYPiUOlh+L2SljKVKdGBrmeIKnxfrz5Fi1CTO6Iyh2V7zrxXy8KUGDxhZMt7t+bXWS50ACFUt1O9OADU+Mpz2zfNLZOXk4nunb6XqU37VaEOQhyxpFPBQy6J1B1Sxwwp9LmLsfUiL5Zl0itwFMDOgru738Ug/yaylZTJJU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8F8244BA2E1C Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 61HLGqqQ200581 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 17 Feb 2026 16:16:56 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 61HLGqqQ200581 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=polymtl.ca; s=oct2025; t=1771363017; bh=se0tpMLpqgFaDKoXLpwhN/Ub6RyuWNTKd8SnpClzCU0=; h=From:To:Cc:Subject:Date:From; b=ViMGHGGB3T44DCtcJXfkJifgEPWEbPFm9HwyDUvFLiLS9ArQ5P6mUi0H/gSqSQhoI AISPbGDpvga/e/rGmQwFFHApo7UBlAHrePDGetoR+q/I6363r4FxGx0n29s1Qt9KKa MZ0V1slp/kwN6pzyI8Lgsw95CKee4dHVi/tjMVdgsOs4rsyco7zGMSHaGN4qHZ83vh nzn+XHdkOnk4xTafVU1SI7o0z84hqPHjCUCx1EsJnGcjl7T/MYxTftWKmaHQu8GvxC dK0tlw1G0f/2B13NMR2dDrUNAPftQcy7D5Oqaq7i0xNNjhE6ESnWa25Ud2dEPw1alU PxUHu50JMkZVQ== Received: by simark.ca (Postfix) id EED461E08D; Tue, 17 Feb 2026 16:16:51 -0500 (EST) From: simon.marchi@polymtl.ca To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH] gdb/solib-frv: remove manual memory management Date: Tue, 17 Feb 2026 16:16:44 -0500 Message-ID: <20260217211650.3999842-1-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Tue, 17 Feb 2026 21:16:52 +0000 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org From: Simon Marchi - Make fetch_loadmap return a unique pointer, adjust callers - Make fetch_loadmap use a unique pointer for ext_ldmbuf - Replace some fields of lm_info_frv to be unique pointers - Make main_executable_lm_info a unique pointer I can only build-test this. Change-Id: I5782ea31b5e30fef2c7468f0bc24d10ffae92669 --- gdb/solib-frv.c | 105 +++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 55 deletions(-) diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c index 95f97dc11333..dedf26ae64d8 100644 --- a/gdb/solib-frv.c +++ b/gdb/solib-frv.c @@ -80,6 +80,9 @@ struct ext_elf32_fdpic_loadmap { struct ext_elf32_fdpic_loadseg segs[1 /* nsegs, actually */]; }; +using ext_elf32_fdpic_loadmap_up + = gdb::unique_xmalloc_ptr; + /* Internal versions; the types are GDB types and the data in each of the fields is (or will be) decoded from the external struct for ease of consumption. */ @@ -102,19 +105,20 @@ struct int_elf32_fdpic_loadmap { struct int_elf32_fdpic_loadseg segs[1 /* nsegs, actually */]; }; +using int_elf32_fdpic_loadmap_up + = gdb::unique_xmalloc_ptr; + /* Given address LDMADDR, fetch and decode the loadmap at that address. Return NULL if there is a problem reading the target memory or if there doesn't appear to be a loadmap at the given address. The allocated space (representing the loadmap) returned by this function may be freed via a single call to xfree(). */ -static struct int_elf32_fdpic_loadmap * +static int_elf32_fdpic_loadmap_up fetch_loadmap (CORE_ADDR ldmaddr) { bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ()); struct ext_elf32_fdpic_loadmap ext_ldmbuf_partial; - struct ext_elf32_fdpic_loadmap *ext_ldmbuf; - struct int_elf32_fdpic_loadmap *int_ldmbuf; int ext_ldmbuf_size, int_ldmbuf_size; int version, seg, nsegs; @@ -147,18 +151,19 @@ fetch_loadmap (CORE_ADDR ldmaddr) /* Allocate space for the complete (external) loadmap. */ ext_ldmbuf_size = sizeof (struct ext_elf32_fdpic_loadmap) + (nsegs - 1) * sizeof (struct ext_elf32_fdpic_loadseg); - ext_ldmbuf = (struct ext_elf32_fdpic_loadmap *) xmalloc (ext_ldmbuf_size); + ext_elf32_fdpic_loadmap_up ext_ldmbuf + (XNEWVAR (ext_elf32_fdpic_loadmap, ext_ldmbuf_size)); /* Copy over the portion of the loadmap that's already been read. */ - memcpy (ext_ldmbuf, &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial); + memcpy (ext_ldmbuf.get (), &ext_ldmbuf_partial, sizeof ext_ldmbuf_partial); /* Read the rest of the loadmap from the target. */ if (target_read_memory (ldmaddr + sizeof ext_ldmbuf_partial, - (gdb_byte *) ext_ldmbuf + sizeof ext_ldmbuf_partial, + ((gdb_byte *) ext_ldmbuf.get () + + sizeof ext_ldmbuf_partial), ext_ldmbuf_size - sizeof ext_ldmbuf_partial)) { /* Couldn't read rest of the loadmap. */ - xfree (ext_ldmbuf); return NULL; } @@ -166,7 +171,8 @@ fetch_loadmap (CORE_ADDR ldmaddr) external loadsegs. I.e, allocate the internal loadsegs. */ int_ldmbuf_size = sizeof (struct int_elf32_fdpic_loadmap) + (nsegs - 1) * sizeof (struct int_elf32_fdpic_loadseg); - int_ldmbuf = (struct int_elf32_fdpic_loadmap *) xmalloc (int_ldmbuf_size); + int_elf32_fdpic_loadmap_up int_ldmbuf + (XNEWVAR (int_elf32_fdpic_loadmap, int_ldmbuf_size)); /* Place extracted information in internal structs. */ int_ldmbuf->version = version; @@ -187,7 +193,6 @@ fetch_loadmap (CORE_ADDR ldmaddr) byte_order); } - xfree (ext_ldmbuf); return int_ldmbuf; } @@ -219,22 +224,15 @@ struct ext_link_map struct lm_info_frv final : public lm_info { - lm_info_frv (int_elf32_fdpic_loadmap *map, CORE_ADDR got_value, + lm_info_frv (int_elf32_fdpic_loadmap_up map, CORE_ADDR got_value, CORE_ADDR lm_addr) - : map (map), got_value (got_value), lm_addr (lm_addr) + : map (std::move (map)), + got_value (got_value), + lm_addr (lm_addr) {} - DISABLE_COPY_AND_ASSIGN (lm_info_frv); - - ~lm_info_frv () - { - xfree (this->map); - xfree (this->dyn_syms); - xfree (this->dyn_relocs); - } - /* The loadmap, digested into an easier to use form. */ - int_elf32_fdpic_loadmap *map; + int_elf32_fdpic_loadmap_up map; /* The GOT address for this link map entry. */ CORE_ADDR got_value; /* The link map address, needed for frv_fetch_objfile_link_map(). */ @@ -250,16 +248,18 @@ struct lm_info_frv final : public lm_info supplied to the first call. Thus the caching of the dynamic symbols (dyn_syms) is critical for correct operation. The caching of the dynamic relocations could be dispensed with. */ - asymbol **dyn_syms = NULL; - arelent **dyn_relocs = NULL; + gdb::unique_xmalloc_ptr dyn_syms; + gdb::unique_xmalloc_ptr dyn_relocs; int dyn_reloc_count = 0; /* Number of dynamic relocs. */ }; +using lm_info_frv_up = std::unique_ptr; + /* The load map, got value, etc. are not available from the chain of loaded shared objects. ``main_executable_lm_info'' provides a way to get at this information so that it doesn't need to be frequently recomputed. Initialized by frv_relocate_main_executable(). */ -static lm_info_frv *main_executable_lm_info; +static lm_info_frv_up main_executable_lm_info; static void frv_relocate_main_executable (void); static CORE_ADDR main_got (void); @@ -378,14 +378,13 @@ frv_solib_ops::current_sos () const this in the list of shared objects. */ if (got_addr != mgot) { - struct int_elf32_fdpic_loadmap *loadmap; CORE_ADDR addr; /* Fetch the load map address. */ addr = extract_unsigned_integer (lm_buf.l_addr.map, sizeof lm_buf.l_addr.map, byte_order); - loadmap = fetch_loadmap (addr); + int_elf32_fdpic_loadmap_up loadmap = fetch_loadmap (addr); if (loadmap == NULL) { warning (_("frv_current_sos: Unable to fetch load map. " @@ -405,11 +404,10 @@ frv_solib_ops::current_sos () const if (name_buf == nullptr) warning (_("Can't read pathname for link map entry.")); - sos.emplace_back (std::make_unique (loadmap, got_addr, - lm_addr), - name_buf != nullptr ? name_buf.get () : "", + sos.emplace_back (std::make_unique (std::move (loadmap), + got_addr, lm_addr), name_buf != nullptr ? name_buf.get () : "", - *this); + name_buf != nullptr ? name_buf.get () : "", *this); } else { @@ -521,7 +519,6 @@ enable_break2 (void) int status; CORE_ADDR addr, interp_loadmap_addr; gdb_byte addr_buf[FRV_PTR_SIZE]; - struct int_elf32_fdpic_loadmap *ldm; /* Read the contents of the .interp section into a local buffer; the contents specify the dynamic linker this program uses. */ @@ -566,7 +563,7 @@ enable_break2 (void) solib_debug_printf ("interp_loadmap_addr = %s", hex_string_custom (interp_loadmap_addr, 8)); - ldm = fetch_loadmap (interp_loadmap_addr); + int_elf32_fdpic_loadmap_up ldm = fetch_loadmap (interp_loadmap_addr); if (ldm == NULL) { warning (_("Unable to load dynamic linker loadmap at address %s."), @@ -582,7 +579,7 @@ enable_break2 (void) { interp_text_sect_low = bfd_section_vma (interp_sect); interp_text_sect_low - += displacement_from_map (ldm, interp_text_sect_low); + += displacement_from_map (ldm.get (), interp_text_sect_low); interp_text_sect_high = interp_text_sect_low + bfd_section_size (interp_sect); } @@ -591,7 +588,7 @@ enable_break2 (void) { interp_plt_sect_low = bfd_section_vma (interp_sect); interp_plt_sect_low - += displacement_from_map (ldm, interp_plt_sect_low); + += displacement_from_map (ldm.get (), interp_plt_sect_low); interp_plt_sect_high = interp_plt_sect_low + bfd_section_size (interp_sect); } @@ -614,7 +611,7 @@ enable_break2 (void) solib_debug_printf ("_dl_debug_addr (prior to relocation) = %s", hex_string_custom (addr, 8)); - addr += displacement_from_map (ldm, addr); + addr += displacement_from_map (ldm.get (), addr); solib_debug_printf ("_dl_debug_addr (after relocation) = %s", hex_string_custom (addr, 8)); @@ -663,9 +660,6 @@ enable_break2 (void) } addr = extract_unsigned_integer (addr_buf, sizeof addr_buf, byte_order); - /* We're done with the loadmap. */ - xfree (ldm); - /* Remove all the solib event breakpoints. Their addresses may have changed since the last time we ran the program. */ remove_solib_event_breakpoints (); @@ -728,7 +722,6 @@ frv_relocate_main_executable (void) { int status; CORE_ADDR exec_addr, interp_addr; - struct int_elf32_fdpic_loadmap *ldm; int changed; status = frv_fdpic_loadmap_addresses (current_inferior ()->arch (), @@ -741,13 +734,14 @@ frv_relocate_main_executable (void) } /* Fetch the loadmap located at ``exec_addr''. */ - ldm = fetch_loadmap (exec_addr); - if (ldm == NULL) + int_elf32_fdpic_loadmap_up ldm_up = fetch_loadmap (exec_addr); + if (ldm_up == nullptr) error (_("Unable to load the executable's loadmap.")); - delete main_executable_lm_info; - main_executable_lm_info = new lm_info_frv (ldm, 0, 0); + main_executable_lm_info + = std::make_unique (std::move (ldm_up), 0, 0); + int_elf32_fdpic_loadmap *ldm = main_executable_lm_info->map.get (); objfile *objf = current_program_space->symfile_object_file; std::vector new_offsets (objf->section_offsets.size ()); changed = 0; @@ -816,9 +810,7 @@ frv_solib_ops::clear_solib (program_space *pspace) const lm_base_cache = 0; enable_break2_done = 0; main_lm_addr = 0; - - delete main_executable_lm_info; - main_executable_lm_info = NULL; + main_executable_lm_info.reset (); } void @@ -827,7 +819,7 @@ frv_solib_ops::relocate_section_addresses (solib &so, { int seg; auto *li = gdb::checked_static_cast (so.lm_info.get ()); - int_elf32_fdpic_loadmap *map = li->map; + int_elf32_fdpic_loadmap *map = li->map.get (); for (seg = 0; seg < map->nsegs; seg++) { @@ -868,7 +860,7 @@ frv_fdpic_find_global_pointer (CORE_ADDR addr) { int seg; auto *li = gdb::checked_static_cast (so.lm_info.get ()); - int_elf32_fdpic_loadmap *map = li->map; + int_elf32_fdpic_loadmap *map = li->map.get (); for (seg = 0; seg < map->nsegs; seg++) { @@ -915,7 +907,7 @@ frv_fdpic_find_canonical_descriptor (CORE_ADDR entry_point) objfile *objf = current_program_space->symfile_object_file; addr = find_canonical_descriptor_in_load_object (entry_point, got_value, name, objf->obfd.get (), - main_executable_lm_info); + main_executable_lm_info.get ()); /* If descriptor not found via main executable, check each load object in list of shared objects. */ @@ -973,10 +965,11 @@ find_canonical_descriptor_in_load_object return 0; /* Allocate space for the dynamic symbol table. */ - lm->dyn_syms = (asymbol **) xmalloc (storage_needed); + lm->dyn_syms.reset (XNEWVAR (asymbol *, storage_needed)); /* Fetch the dynamic symbol table. */ - number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, lm->dyn_syms); + number_of_symbols + = bfd_canonicalize_dynamic_symtab (abfd, lm->dyn_syms.get ()); if (number_of_symbols == 0) return 0; @@ -995,17 +988,18 @@ find_canonical_descriptor_in_load_object return 0; /* Allocate space for the relocs. */ - lm->dyn_relocs = (arelent **) xmalloc (storage_needed); + lm->dyn_relocs.reset (XNEWVAR (arelent *, storage_needed)); /* Fetch the dynamic relocs. */ lm->dyn_reloc_count - = bfd_canonicalize_dynamic_reloc (abfd, lm->dyn_relocs, lm->dyn_syms); + = bfd_canonicalize_dynamic_reloc (abfd, lm->dyn_relocs.get (), + lm->dyn_syms.get ()); } /* Search the dynamic relocs. */ for (i = 0; i < lm->dyn_reloc_count; i++) { - rel = lm->dyn_relocs[i]; + rel = lm->dyn_relocs.get ()[i]; /* Relocs of interest are those which meet the following criteria: @@ -1029,7 +1023,8 @@ find_canonical_descriptor_in_load_object gdb_byte buf [FRV_PTR_SIZE]; /* Compute address of address of candidate descriptor. */ - addr = rel->address + displacement_from_map (lm->map, rel->address); + addr = (rel->address + + displacement_from_map (lm->map.get (), rel->address)); /* Fetch address of candidate descriptor. */ if (target_read_memory (addr, buf, sizeof buf) != 0) base-commit: 625b835f63153677bf614de266c7bc8963163ffc -- 2.53.0