From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gateway30.websitewelcome.com (gateway30.websitewelcome.com [192.185.152.11]) by sourceware.org (Postfix) with ESMTPS id ACF7A394C007 for ; Fri, 28 Aug 2020 16:23:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org ACF7A394C007 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 cm10.websitewelcome.com (cm10.websitewelcome.com [100.42.49.4]) by gateway30.websitewelcome.com (Postfix) with ESMTP id A774E6DCC for ; Fri, 28 Aug 2020 11:23:55 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id BhARkC4wULFNkBhARkeS60; Fri, 28 Aug 2020 11:23:55 -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=/YmE4hbYaT1EeKG8GX8p3FJ8RN6Ivqvsjg8r8Fzxc/g=; b=jRr1/pbBjMA39vaR2Qs7Izmpt5 T6iRlc8uISVF1L6ESEaCGkEEHGlJFVVPBHaZ54Apj5KlUbDto9gWSkBW4JdKOnJEbBwZxU/87yJBC IJlt10s2QmoUd6iD62DaFIEXi; Received: from 71-218-12-75.hlrn.qwest.net ([71.218.12.75]:33904 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 1kBhAR-002WJo-2x; Fri, 28 Aug 2020 10:23:55 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 10/13] Use gdb_bfd_sections in restore_command Date: Fri, 28 Aug 2020 10:23:46 -0600 Message-Id: <20200828162349.987-11-tom@tromey.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20200828162349.987-1-tom@tromey.com> References: <20200828162349.987-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: 71.218.12.75 X-Source-L: No X-Exim-ID: 1kBhAR-002WJo-2x X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 71-218-12-75.hlrn.qwest.net (bapiya.Home) [71.218.12.75]:33904 X-Source-Auth: tom+tromey.com X-Email-Count: 11 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3035.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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: Fri, 28 Aug 2020 16:23:59 -0000 This changes restore_command to avoid bfd_map_over_sections, in favor of iteration. A helper data structure can also be removed by this patch. gdb/ChangeLog 2020-08-28 Tom Tromey * cli/cli-dump.c (struct callback_data): Remove. (restore_one_section): Rename from restore_section_callback. Change parameters. (restore_binary_file): Change parameters. (restore_command): Use foreach. --- gdb/ChangeLog | 8 ++++ gdb/cli/cli-dump.c | 94 ++++++++++++++++++++++------------------------ 2 files changed, 53 insertions(+), 49 deletions(-) diff --git a/gdb/cli/cli-dump.c b/gdb/cli/cli-dump.c index 567ef2eeded..849964ece6b 100644 --- a/gdb/cli/cli-dump.c +++ b/gdb/cli/cli-dump.c @@ -371,22 +371,14 @@ add_dump_command (const char *name, c->doc = concat ("Append ", c->doc + 6, (char *)NULL); } -/* Opaque data for restore_section_callback. */ -struct callback_data { - CORE_ADDR load_offset; - CORE_ADDR load_start; - CORE_ADDR load_end; -}; - -/* Function: restore_section_callback. - - Callback function for bfd_map_over_sections. - Selectively loads the sections into memory. */ +/* Selectively loads the sections into memory. */ static void -restore_section_callback (bfd *ibfd, asection *isec, void *args) +restore_one_section (bfd *ibfd, asection *isec, + CORE_ADDR load_offset, + CORE_ADDR load_start, + CORE_ADDR load_end) { - struct callback_data *data = (struct callback_data *) args; bfd_vma sec_start = bfd_section_vma (isec); bfd_size_type size = bfd_section_size (isec); bfd_vma sec_end = sec_start + size; @@ -399,8 +391,8 @@ restore_section_callback (bfd *ibfd, asection *isec, void *args) return; /* Does the section overlap with the desired restore range? */ - if (sec_end <= data->load_start - || (data->load_end > 0 && sec_start >= data->load_end)) + if (sec_end <= load_start + || (load_end > 0 && sec_start >= load_end)) { /* No, no useable data in this section. */ printf_filtered (_("skipping section %s...\n"), @@ -411,12 +403,12 @@ restore_section_callback (bfd *ibfd, asection *isec, void *args) /* Compare section address range with user-requested address range (if any). Compute where the actual transfer should start and end. */ - if (sec_start < data->load_start) - sec_offset = data->load_start - sec_start; + if (sec_start < load_start) + sec_offset = load_start - sec_start; /* Size of a partial transfer. */ sec_load_count -= sec_offset; - if (data->load_end > 0 && sec_end > data->load_end) - sec_load_count -= sec_end - data->load_end; + if (load_end > 0 && sec_end > load_end) + sec_load_count -= sec_end - load_end; /* Get the data. */ gdb::byte_vector buf (size); @@ -429,26 +421,28 @@ restore_section_callback (bfd *ibfd, asection *isec, void *args) (unsigned long) sec_start, (unsigned long) sec_end); - if (data->load_offset != 0 || data->load_start != 0 || data->load_end != 0) + if (load_offset != 0 || load_start != 0 || load_end != 0) printf_filtered (" into memory (%s to %s)\n", paddress (target_gdbarch (), (unsigned long) sec_start - + sec_offset + data->load_offset), + + sec_offset + load_offset), paddress (target_gdbarch (), (unsigned long) sec_start + sec_offset - + data->load_offset + sec_load_count)); + + load_offset + sec_load_count)); else puts_filtered ("\n"); /* Write the data. */ - ret = target_write_memory (sec_start + sec_offset + data->load_offset, + ret = target_write_memory (sec_start + sec_offset + load_offset, &buf[sec_offset], sec_load_count); if (ret != 0) warning (_("restore: memory write failed (%s)."), safe_strerror (ret)); } static void -restore_binary_file (const char *filename, struct callback_data *data) +restore_binary_file (const char *filename, CORE_ADDR load_offset, + CORE_ADDR load_start, CORE_ADDR load_end) + { gdb_file_up file = gdb_fopen_cloexec (filename, FOPEN_RB); long len; @@ -466,25 +460,25 @@ restore_binary_file (const char *filename, struct callback_data *data) else perror_with_name (filename); - if (len <= data->load_start) + if (len <= load_start) error (_("Start address is greater than length of binary file %s."), filename); /* Chop off "len" if it exceeds the requested load_end addr. */ - if (data->load_end != 0 && data->load_end < len) - len = data->load_end; + if (load_end != 0 && load_end < len) + len = load_end; /* Chop off "len" if the requested load_start addr skips some bytes. */ - if (data->load_start > 0) - len -= data->load_start; + if (load_start > 0) + len -= load_start; printf_filtered ("Restoring binary file %s into memory (0x%lx to 0x%lx)\n", filename, - (unsigned long) (data->load_start + data->load_offset), - (unsigned long) (data->load_start + data->load_offset + len)); + (unsigned long) (load_start + load_offset), + (unsigned long) (load_start + load_offset + len)); /* Now set the file pos to the requested load start pos. */ - if (fseek (file.get (), data->load_start, SEEK_SET) != 0) + if (fseek (file.get (), load_start, SEEK_SET) != 0) perror_with_name (filename); /* Now allocate a buffer and read the file contents. */ @@ -493,8 +487,7 @@ restore_binary_file (const char *filename, struct callback_data *data) perror_with_name (filename); /* Now write the buffer into target memory. */ - len = target_write_memory (data->load_start + data->load_offset, - buf.data (), len); + len = target_write_memory (load_start + load_offset, buf.data (), len); if (len != 0) warning (_("restore: memory write failed (%s)."), safe_strerror (len)); } @@ -502,15 +495,14 @@ restore_binary_file (const char *filename, struct callback_data *data) static void restore_command (const char *args, int from_tty) { - struct callback_data data; int binary_flag = 0; if (!target_has_execution) noprocess (); - data.load_offset = 0; - data.load_start = 0; - data.load_end = 0; + CORE_ADDR load_offset = 0; + CORE_ADDR load_start = 0; + CORE_ADDR load_end = 0; /* Parse the input arguments. First is filename (required). */ gdb::unique_xmalloc_ptr filename = scan_filename (&args, NULL); @@ -527,19 +519,20 @@ restore_command (const char *args, int from_tty) } /* Parse offset (optional). */ if (args != NULL && *args != '\0') - data.load_offset = binary_flag ? - parse_and_eval_address (scan_expression (&args, NULL).get ()) : - parse_and_eval_long (scan_expression (&args, NULL).get ()); + load_offset + = (binary_flag + ? parse_and_eval_address (scan_expression (&args, NULL).get ()) + : parse_and_eval_long (scan_expression (&args, NULL).get ())); if (args != NULL && *args != '\0') { /* Parse start address (optional). */ - data.load_start = + load_start = parse_and_eval_long (scan_expression (&args, NULL).get ()); if (args != NULL && *args != '\0') { /* Parse end address (optional). */ - data.load_end = parse_and_eval_long (args); - if (data.load_end <= data.load_start) + load_end = parse_and_eval_long (args); + if (load_end <= load_start) error (_("Start must be less than end.")); } } @@ -547,13 +540,14 @@ restore_command (const char *args, int from_tty) if (info_verbose) printf_filtered ("Restore file %s offset 0x%lx start 0x%lx end 0x%lx\n", - filename.get (), (unsigned long) data.load_offset, - (unsigned long) data.load_start, - (unsigned long) data.load_end); + filename.get (), (unsigned long) load_offset, + (unsigned long) load_start, + (unsigned long) load_end); if (binary_flag) { - restore_binary_file (filename.get (), &data); + restore_binary_file (filename.get (), load_offset, load_start, + load_end); } else { @@ -561,7 +555,9 @@ restore_command (const char *args, int from_tty) gdb_bfd_ref_ptr ibfd (bfd_openr_or_error (filename.get (), NULL)); /* Process the sections. */ - bfd_map_over_sections (ibfd.get (), restore_section_callback, &data); + for (asection *sect : gdb_bfd_sections (ibfd)) + restore_one_section (ibfd.get (), sect, load_offset, load_start, + load_end); } } -- 2.17.2