[2/3] gdb: use std::vector to store segments in symfile_segment_data

Message ID 20200519013533.1428830-3-simon.marchi@efficios.com
State New
Headers show
Series
  • C++ification of symfile_segment_data
Related show

Commit Message

Simon Marchi via Gdb-patches May 19, 2020, 1:35 a.m.
Instead of maintaining two vectors, I added a small `segment` class
which holds both the base address and size of one segment and replaced
the two `segment_bases` and `segment_sizes` arrays with a single vector.

The rest of the changes are straightforward, no behavior changes are
expected.

gdb/ChangeLog:

	* symfile.h (struct symfile_segment_data) <struct segment>: New.
	<segments>: New.
	<segment_bases, segment_sizes>: Remove.
	* symfile.c (default_symfile_segments): Update.
	* elfread.c (elf_symfile_segments): Update.
	* remote.c (remote_target::get_offsets): Update.
	* solib-target.c (solib_target_relocate_section_addresses):
	Update.
---
 gdb/elfread.c      |  8 +-------
 gdb/remote.c       | 10 +++++-----
 gdb/solib-target.c | 10 +++++-----
 gdb/symfile.c      | 14 +++++---------
 gdb/symfile.h      | 32 +++++++++++++++++---------------
 5 files changed, 33 insertions(+), 41 deletions(-)

-- 
2.26.2

Comments

Simon Marchi via Gdb-patches May 19, 2020, 2:52 p.m. | #1
On 2020-05-19 10:55 a.m., Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:

> 

> Simon>    symfile_segment_data_up data (new symfile_segment_data);

> Simon> -  data->num_segments = num_segments;

> Simon> -  data->segment_bases = XCNEWVEC (CORE_ADDR, num_segments);

> Simon> -  data->segment_sizes = XCNEWVEC (CORE_ADDR, num_segments);

> 

> Simon>    for (i = 0; i < num_segments; i++)

> Simon> -    {

> Simon> -      data->segment_bases[i] = segments[i]->p_vaddr;

> Simon> -      data->segment_sizes[i] = segments[i]->p_memsz;

> Simon> -    }

> Simon> +    data->segments.emplace_back (segments[i]->p_vaddr, segments[i]->p_memsz);

> 

> It's probably better to just resize() to the desired size, instead of

> using emplace_back.  That may save some memory, because it won't

> over-allocate.

> 

> Probably not super important, but at the same time easy to do.


Good point, will do.  I probably can't resize if the elements are not
default constructible, but if I call reserve, it should work as well?

Simon
Tom Tromey May 19, 2020, 2:55 p.m. | #2
>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:


Simon>    symfile_segment_data_up data (new symfile_segment_data);
Simon> -  data->num_segments = num_segments;
Simon> -  data->segment_bases = XCNEWVEC (CORE_ADDR, num_segments);
Simon> -  data->segment_sizes = XCNEWVEC (CORE_ADDR, num_segments);

Simon>    for (i = 0; i < num_segments; i++)
Simon> -    {
Simon> -      data->segment_bases[i] = segments[i]->p_vaddr;
Simon> -      data->segment_sizes[i] = segments[i]->p_memsz;
Simon> -    }
Simon> +    data->segments.emplace_back (segments[i]->p_vaddr, segments[i]->p_memsz);

It's probably better to just resize() to the desired size, instead of
using emplace_back.  That may save some memory, because it won't
over-allocate.

Probably not super important, but at the same time easy to do.

Tom
Tom Tromey May 19, 2020, 3:44 p.m. | #3
>>>>> "Simon" == Simon Marchi <simon.marchi@efficios.com> writes:


Simon> Good point, will do.  I probably can't resize if the elements are not
Simon> default constructible, but if I call reserve, it should work as well?

Yeah, that seems reasonable.

Tom

Patch

diff --git a/gdb/elfread.c b/gdb/elfread.c
index 4804d62074c4..822f443fa8d3 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -112,15 +112,9 @@  elf_symfile_segments (bfd *abfd)
     return NULL;
 
   symfile_segment_data_up data (new symfile_segment_data);
-  data->num_segments = num_segments;
-  data->segment_bases = XCNEWVEC (CORE_ADDR, num_segments);
-  data->segment_sizes = XCNEWVEC (CORE_ADDR, num_segments);
 
   for (i = 0; i < num_segments; i++)
-    {
-      data->segment_bases[i] = segments[i]->p_vaddr;
-      data->segment_sizes[i] = segments[i]->p_memsz;
-    }
+    data->segments.emplace_back (segments[i]->p_vaddr, segments[i]->p_memsz);
 
   num_sections = bfd_count_sections (abfd);
   data->segment_info = XCNEWVEC (int, num_sections);
diff --git a/gdb/remote.c b/gdb/remote.c
index a28f34d157a9..312a03c8fb42 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -4198,10 +4198,10 @@  remote_target::get_offsets ()
      by assuming that the .text and .data offsets apply to the whole
      text and data segments.  Convert the offsets given in the packet
      to base addresses for symfile_map_offsets_to_segments.  */
-  else if (data && data->num_segments == 2)
+  else if (data != nullptr && data->segments.size () == 2)
     {
-      segments[0] = data->segment_bases[0] + text_addr;
-      segments[1] = data->segment_bases[1] + data_addr;
+      segments[0] = data->segments[0].base + text_addr;
+      segments[1] = data->segments[1].base + data_addr;
       num_segments = 2;
     }
   /* If the object file has only one segment, assume that it is text
@@ -4209,9 +4209,9 @@  remote_target::get_offsets ()
      but programs with no code are useless.  Of course the code might
      have ended up in the data segment... to detect that we would need
      the permissions here.  */
-  else if (data && data->num_segments == 1)
+  else if (data && data->segments.size () == 1)
     {
-      segments[0] = data->segment_bases[0] + text_addr;
+      segments[0] = data->segments[0].base + text_addr;
       num_segments = 1;
     }
   /* There's no way to relocate by segment.  */
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index 35e50a3e00b4..ba056478c061 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -386,9 +386,9 @@  Could not relocate shared library \"%s\": bad offsets"), so->so_name);
 		 "info sharedlibrary".  Report any consecutive segments
 		 which were relocated as a single unit.  */
 	      gdb_assert (li->segment_bases.size () > 0);
-	      orig_delta = li->segment_bases[0] - data->segment_bases[0];
+	      orig_delta = li->segment_bases[0] - data->segments[0].base;
 
-	      for (i = 1; i < data->num_segments; i++)
+	      for (i = 1; i < data->segments.size (); i++)
 		{
 		  /* If we have run out of offsets, assume all
 		     remaining segments have the same offset.  */
@@ -397,14 +397,14 @@  Could not relocate shared library \"%s\": bad offsets"), so->so_name);
 
 		  /* If this segment does not have the same offset, do
 		     not include it in the library's range.  */
-		  if (li->segment_bases[i] - data->segment_bases[i]
+		  if (li->segment_bases[i] - data->segments[i].base
 		      != orig_delta)
 		    break;
 		}
 
 	      so->addr_low = li->segment_bases[0];
-	      so->addr_high = (data->segment_bases[i - 1]
-			       + data->segment_sizes[i - 1]
+	      so->addr_high = (data->segments[i - 1].base
+			       + data->segments[i - 1].size
 			       + orig_delta);
 	      gdb_assert (so->addr_low <= so->addr_high);
 	    }
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 9d5e2824b2a7..22793e736398 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -745,9 +745,6 @@  default_symfile_segments (bfd *abfd)
   high = low + bfd_section_size (sect);
 
   symfile_segment_data_up data (new symfile_segment_data);
-  data->num_segments = 1;
-  data->segment_bases = XCNEW (CORE_ADDR);
-  data->segment_sizes = XCNEW (CORE_ADDR);
 
   num_sections = bfd_count_sections (abfd);
   data->segment_info = XCNEWVEC (int, num_sections);
@@ -768,8 +765,7 @@  default_symfile_segments (bfd *abfd)
       data->segment_info[i] = 1;
     }
 
-  data->segment_bases[0] = low;
-  data->segment_sizes[0] = high - low;
+  data->segments.emplace_back (low, high - low);
 
   return data;
 }
@@ -3663,13 +3659,13 @@  symfile_map_offsets_to_segments (bfd *abfd,
   /* If we do not have segment mappings for the object file, we
      can not relocate it by segments.  */
   gdb_assert (data != NULL);
-  gdb_assert (data->num_segments > 0);
+  gdb_assert (data->segments.size () > 0);
 
   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
     {
       int which = data->segment_info[i];
 
-      gdb_assert (0 <= which && which <= data->num_segments);
+      gdb_assert (0 <= which && which <= data->segments.size ());
 
       /* Don't bother computing offsets for sections that aren't
          loaded as part of any segment.  */
@@ -3681,7 +3677,7 @@  symfile_map_offsets_to_segments (bfd *abfd,
       if (which > num_segment_bases)
         which = num_segment_bases;
 
-      offsets[i] = segment_bases[which - 1] - data->segment_bases[which - 1];
+      offsets[i] = segment_bases[which - 1] - data->segments[which - 1].base;
     }
 
   return 1;
@@ -3699,7 +3695,7 @@  symfile_find_segment_sections (struct objfile *objfile)
   if (data == NULL)
     return;
 
-  if (data->num_segments != 1 && data->num_segments != 2)
+  if (data->segments.size () != 1 && data->segments.size () != 2)
     return;
 
   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
diff --git a/gdb/symfile.h b/gdb/symfile.h
index 2dfa6556d479..1f2395169af9 100644
--- a/gdb/symfile.h
+++ b/gdb/symfile.h
@@ -80,29 +80,31 @@  typedef std::vector<other_sections> section_addr_info;
    each BFD section belongs to.  */
 struct symfile_segment_data
 {
+  struct segment
+  {
+    segment (CORE_ADDR base, CORE_ADDR size)
+      : base (base), size (size)
+    {}
+
+    /* The original base address the segment.  */
+    CORE_ADDR base;
+
+    /* The memory size of the segment.  */
+    CORE_ADDR size;
+  };
+
   ~symfile_segment_data ()
   {
-    xfree (this->segment_bases);
-    xfree (this->segment_sizes);
     xfree (this->segment_info);
   }
 
-  /* How many segments are present in this file.  If there are
+  /* The segments present in this file.  If there are
      two, the text segment is the first one and the data segment
      is the second one.  */
-  int num_segments = 0;
-
-  /* If NUM_SEGMENTS is greater than zero, the original base address
-     of each segment.  */
-  CORE_ADDR *segment_bases = nullptr;
-
-  /* If NUM_SEGMENTS is greater than zero, the memory size of each
-     segment.  */
-  CORE_ADDR *segment_sizes = nullptr;
+  std::vector<segment> segments;
 
-  /* If NUM_SEGMENTS is greater than zero, this is an array of entries
-     recording which segment contains each BFD section.
-     SEGMENT_INFO[I] is S+1 if the I'th BFD section belongs to segment
+  /* This is an array of entries recording which segment contains each BFD
+     section.  SEGMENT_INFO[I] is S+1 if the I'th BFD section belongs to segment
      S, or zero if it is not in any segment.  */
   int *segment_info = nullptr;
 };