From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gateway31.websitewelcome.com (gateway31.websitewelcome.com [192.185.144.28]) by sourceware.org (Postfix) with ESMTPS id 1DD403857C69 for ; Sun, 9 Aug 2020 13:53:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1DD403857C69 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=tom@tromey.com Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway31.websitewelcome.com (Postfix) with ESMTP id 2DFA213A06 for ; Sun, 9 Aug 2020 08:53:03 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 4ll0kymQ0BD8b4ll0kYqaT; Sun, 09 Aug 2020 08:53:03 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=/ZhoXAbl5RrGxw5u+yhTLQo/DfG6L7e4/FRUFaO6Y7A=; b=DRBG6ckXvXuPKLZvfMg4K3vLry 70zsxRXtk8sX+bROnu0EXmOkCe7vYRyxQhYfati2BwhCz9vgV0if+Xixox9qlw9p4CvP7YJlaINsh 4ZMTSw9RfmfHwM82mGnldB8uh; Received: from 75-166-101-103.hlrn.qwest.net ([75.166.101.103]:56058 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1k4lkz-0022sH-VX; Sun, 09 Aug 2020 07:53:02 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 5/6] Simplify compile_module cleanup Date: Sun, 9 Aug 2020 07:52:57 -0600 Message-Id: <20200809135258.8207-6-tom@tromey.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20200809135258.8207-1-tom@tromey.com> References: <20200809135258.8207-1-tom@tromey.com> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.101.103 X-Source-L: No X-Exim-ID: 1k4lkz-0022sH-VX X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-101-103.hlrn.qwest.net (bapiya.Home) [75.166.101.103]:56058 X-Source-Auth: tom+tromey.com X-Email-Count: 6 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3035.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 09 Aug 2020 13:53:07 -0000 This simplifies compile_module cleanup by removing the need to explicitly free anything. struct setup_sections_data is also cleaned up a bit. gdb/ChangeLog 2020-08-08 Tom Tromey * compile/compile-object-run.c (do_module_cleanup) <~do_module_cleanup> :Remove. (do_module_cleanup): Update. * compile/compile-object-load.h (struct munmap_list): Add move assignment operator. : Now a std::string. : Rename. No longer a pointer. * compile/compile-object-load.c (struct setup_sections_data): Add constructor. : Declare. : Move earlier. : New member. : Rename, add initializers where needed. (setup_sections_data::setup_one_section): Rename from setup_sections. Update. (compile_object_load): Update. Don't use bfd_map_over_sections. --- gdb/ChangeLog | 20 ++++++ gdb/compile/compile-object-load.c | 103 +++++++++++++++++------------- gdb/compile/compile-object-load.h | 8 ++- gdb/compile/compile-object-run.c | 8 +-- 4 files changed, 83 insertions(+), 56 deletions(-) diff --git a/gdb/compile/compile-object-load.c b/gdb/compile/compile-object-load.c index dff10fe94f4..bde3b742db4 100644 --- a/gdb/compile/compile-object-load.c +++ b/gdb/compile/compile-object-load.c @@ -62,34 +62,50 @@ munmap_list::~munmap_list () } } -/* Helper data for setup_sections. */ +/* A data structure that is used to lay out sections of our objfile in + inferior memory. */ struct setup_sections_data { + explicit setup_sections_data (bfd *abfd) + : m_bfd (abfd), + m_last_section_first (abfd->sections) + { + } + + /* Place all ABFD sections next to each other obeying all + constraints. */ + void setup_one_section (asection *sect); + + /* List of inferior mmap ranges where setup_sections should add its + next range. */ + struct munmap_list munmap_list; + +private: + + /* The BFD. */ + bfd *m_bfd; + /* Size of all recent sections with matching LAST_PROT. */ - CORE_ADDR last_size; + CORE_ADDR m_last_size = 0; /* First section matching LAST_PROT. */ - asection *last_section_first; + asection *m_last_section_first; /* Memory protection like the prot parameter of gdbarch_infcall_mmap. */ - unsigned last_prot; + unsigned m_last_prot = -1; /* Maximum of alignments of all sections matching LAST_PROT. This value is always at least 1. This value is always a power of 2. */ - CORE_ADDR last_max_alignment; + CORE_ADDR m_last_max_alignment = -1; - /* List of inferior mmap ranges where setup_sections should add its - next range. */ - std::unique_ptr munmap_list; }; -/* Place all ABFD sections next to each other obeying all constraints. */ +/* See setup_sections_data. */ -static void -setup_sections (bfd *abfd, asection *sect, void *data_voidp) +void +setup_sections_data::setup_one_section (asection *sect) { - struct setup_sections_data *data = (struct setup_sections_data *) data_voidp; CORE_ADDR alignment; unsigned prot; @@ -112,7 +128,7 @@ setup_sections (bfd *abfd, asection *sect, void *data_voidp) if (compile_debug) fprintf_unfiltered (gdb_stdlog, "module \"%s\" section \"%s\" size %s prot %u\n", - bfd_get_filename (abfd), + bfd_get_filename (m_bfd), bfd_section_name (sect), paddress (target_gdbarch (), bfd_section_size (sect)), @@ -122,55 +138,55 @@ setup_sections (bfd *abfd, asection *sect, void *data_voidp) prot = -1; if (sect == NULL - || (data->last_prot != prot && bfd_section_size (sect) != 0)) + || (m_last_prot != prot && bfd_section_size (sect) != 0)) { CORE_ADDR addr; asection *sect_iter; - if (data->last_size != 0) + if (m_last_size != 0) { - addr = gdbarch_infcall_mmap (target_gdbarch (), data->last_size, - data->last_prot); - data->munmap_list->add (addr, data->last_size); + addr = gdbarch_infcall_mmap (target_gdbarch (), m_last_size, + m_last_prot); + munmap_list.add (addr, m_last_size); if (compile_debug) fprintf_unfiltered (gdb_stdlog, "allocated %s bytes at %s prot %u\n", - paddress (target_gdbarch (), data->last_size), + paddress (target_gdbarch (), m_last_size), paddress (target_gdbarch (), addr), - data->last_prot); + m_last_prot); } else addr = 0; - if ((addr & (data->last_max_alignment - 1)) != 0) + if ((addr & (m_last_max_alignment - 1)) != 0) error (_("Inferior compiled module address %s " "is not aligned to BFD required %s."), paddress (target_gdbarch (), addr), - paddress (target_gdbarch (), data->last_max_alignment)); + paddress (target_gdbarch (), m_last_max_alignment)); - for (sect_iter = data->last_section_first; sect_iter != sect; + for (sect_iter = m_last_section_first; sect_iter != sect; sect_iter = sect_iter->next) if ((bfd_section_flags (sect_iter) & SEC_ALLOC) != 0) bfd_set_section_vma (sect_iter, addr + bfd_section_vma (sect_iter)); - data->last_size = 0; - data->last_section_first = sect; - data->last_prot = prot; - data->last_max_alignment = 1; + m_last_size = 0; + m_last_section_first = sect; + m_last_prot = prot; + m_last_max_alignment = 1; } if (sect == NULL) return; alignment = ((CORE_ADDR) 1) << bfd_section_alignment (sect); - data->last_max_alignment = std::max (data->last_max_alignment, alignment); + m_last_max_alignment = std::max (m_last_max_alignment, alignment); - data->last_size = (data->last_size + alignment - 1) & -alignment; + m_last_size = (m_last_size + alignment - 1) & -alignment; - bfd_set_section_vma (sect, data->last_size); + bfd_set_section_vma (sect, m_last_size); - data->last_size += bfd_section_size (sect); - data->last_size = (data->last_size + alignment - 1) & -alignment; + m_last_size += bfd_section_size (sect); + m_last_size = (m_last_size + alignment - 1) & -alignment; } /* Helper for link_callbacks callbacks vector. */ @@ -586,7 +602,6 @@ compile_module_up compile_object_load (const compile_file_names &file_names, enum compile_i_scope_types scope, void *scope_data) { - struct setup_sections_data setup_sections_data; CORE_ADDR regs_addr, out_value_addr = 0; struct symbol *func_sym; struct type *func_type; @@ -616,14 +631,10 @@ compile_object_load (const compile_file_names &file_names, if ((bfd_get_file_flags (abfd.get ()) & (EXEC_P | DYNAMIC)) != 0) error (_("\"%s\": not in object format."), filename.get ()); - setup_sections_data.last_size = 0; - setup_sections_data.last_section_first = abfd->sections; - setup_sections_data.last_prot = -1; - setup_sections_data.last_max_alignment = 1; - setup_sections_data.munmap_list.reset (new struct munmap_list); - - bfd_map_over_sections (abfd.get (), setup_sections, &setup_sections_data); - setup_sections (abfd.get (), NULL, &setup_sections_data); + struct setup_sections_data setup_sections_data (abfd.get ()); + for (asection *sect = abfd->sections; sect != nullptr; sect = sect->next) + setup_sections_data.setup_one_section (sect); + setup_sections_data.setup_one_section (nullptr); storage_needed = bfd_get_symtab_upper_bound (abfd.get ()); if (storage_needed < 0) @@ -757,7 +768,7 @@ compile_object_load (const compile_file_names &file_names, TYPE_LENGTH (regs_type), GDB_MMAP_PROT_READ); gdb_assert (regs_addr != 0); - setup_sections_data.munmap_list->add (regs_addr, TYPE_LENGTH (regs_type)); + setup_sections_data.munmap_list.add (regs_addr, TYPE_LENGTH (regs_type)); if (compile_debug) fprintf_unfiltered (gdb_stdlog, "allocated %s bytes at %s for registers\n", @@ -779,8 +790,8 @@ compile_object_load (const compile_file_names &file_names, (GDB_MMAP_PROT_READ | GDB_MMAP_PROT_WRITE)); gdb_assert (out_value_addr != 0); - setup_sections_data.munmap_list->add (out_value_addr, - TYPE_LENGTH (out_value_type)); + setup_sections_data.munmap_list.add (out_value_addr, + TYPE_LENGTH (out_value_type)); if (compile_debug) fprintf_unfiltered (gdb_stdlog, "allocated %s bytes at %s for printed value\n", @@ -791,14 +802,14 @@ compile_object_load (const compile_file_names &file_names, compile_module_up retval (new struct compile_module); retval->objfile = objfile_holder.release (); - retval->source_file = xstrdup (file_names.source_file ()); + retval->source_file = file_names.source_file (); retval->func_sym = func_sym; retval->regs_addr = regs_addr; retval->scope = scope; retval->scope_data = scope_data; retval->out_value_type = out_value_type; retval->out_value_addr = out_value_addr; - retval->munmap_list_head = setup_sections_data.munmap_list.release (); + retval->munmap_list = std::move (setup_sections_data.munmap_list); return retval; } diff --git a/gdb/compile/compile-object-load.h b/gdb/compile/compile-object-load.h index 9def29ebc9b..0254390109e 100644 --- a/gdb/compile/compile-object-load.h +++ b/gdb/compile/compile-object-load.h @@ -29,6 +29,8 @@ struct munmap_list DISABLE_COPY_AND_ASSIGN (munmap_list); + munmap_list &operator= (munmap_list &&) = default; + /* Add a region to the list. */ void add (CORE_ADDR addr, CORE_ADDR size); @@ -56,8 +58,8 @@ struct compile_module /* objfile for the compiled module. */ struct objfile *objfile; - /* .c file OBJFILE was built from. It needs to be xfree-d. */ - char *source_file; + /* .c file OBJFILE was built from. */ + std::string source_file; /* Inferior function GCC_FE_WRAPPER_FUNCTION. */ struct symbol *func_sym; @@ -81,7 +83,7 @@ struct compile_module CORE_ADDR out_value_addr; /* Track inferior memory reserved by inferior mmap. */ - struct munmap_list *munmap_list_head; + struct munmap_list munmap_list; }; /* A unique pointer for a compile_module. */ diff --git a/gdb/compile/compile-object-run.c b/gdb/compile/compile-object-run.c index ac0a995fee9..10867eee30c 100644 --- a/gdb/compile/compile-object-run.c +++ b/gdb/compile/compile-object-run.c @@ -38,12 +38,6 @@ struct do_module_cleanup { } - ~do_module_cleanup () - { - delete module->munmap_list_head; - xfree (module->source_file); - } - DISABLE_COPY_AND_ASSIGN (do_module_cleanup); /* Boolean to set true upon a call of do_module_cleanup. @@ -99,7 +93,7 @@ do_module_cleanup (void *arg, int registers_valid) } /* Delete the .c file. */ - unlink (data->module->source_file); + unlink (data->module->source_file.c_str ()); /* Delete the .o file. */ unlink (objfile_name_s); -- 2.17.2