public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 2/6] Use a std::vector in target_section_table
Date: Sat,  3 Oct 2020 13:37:31 -0600	[thread overview]
Message-ID: <20201003193735.2532-3-tom@tromey.com> (raw)
In-Reply-To: <20201003193735.2532-1-tom@tromey.com>

This changes target_section_table to wrap a std::vector.  This
simplifies some code, and also enables the simplifications coming in
the subsequent patches.

Note that for solib, I chose to have it use a pointer to a
target_section_table.  This is more convoluted than would be ideal,
but I didn't want to convert solib to new/delete as a prerequisite for
this series.

gdb/ChangeLog
2020-10-03  Tom Tromey  <tom@tromey.com>

	* target.c (target_section_by_addr, memory_xfer_partial_1):
	Update.
	* target-section.h (struct target_section_table): Use
	std::vector.
	* symfile.h (build_section_addr_info_from_section_table): Take a
	target_section_table.
	* symfile.c (build_section_addr_info_from_section_table): Take a
	target_section_table.
	* solist.h (struct so_list) <sections>: Change type.
	<sections_end>: Remove.
	* solib.c (solib_map_sections, clear_so, solib_read_symbols)
	(solib_contains_address_p): Update.
	* solib-svr4.c (scan_dyntag): Update.
	* solib-dsbt.c (scan_dyntag): Update.
	* remote.c (remote_target::remote_xfer_live_readonly_partial):
	Update.
	* record-full.c (record_full_core_start, record_full_core_end):
	Remove.
	(record_full_core_sections): New global.
	(record_full_core_open_1, record_full_core_target::xfer_partial):
	Update.
	* exec.h (build_section_table, section_table_xfer_memory_partial)
	(add_target_sections): Take a target_section_table.
	* exec.c (exec_file_attach, clear_section_table): Update.
	(resize_section_table): Remove.
	(build_section_table, add_target_sections): Take a
	target_section_table.
	(add_target_sections_of_objfile, remove_target_sections)
	(exec_on_vfork): Update.
	(section_table_available_memory): Take a target_section_table.
	(section_table_read_available_memory): Update.
	(section_table_xfer_memory_partial): Take a target_section_table.
	(print_section_info, set_section_command)
	(exec_set_section_address, exec_target::has_memory): Update.
	* corelow.c (class core_target) <m_core_section_table,
	m_core_file_mappings>: Remove braces.
	<~core_target>: Remove.
	(core_target::core_target): Update.
	(core_target::~core_target): Remove.
	(core_target::build_file_mappings)
	(core_target::xfer_memory_via_mappings)
	(core_target::xfer_partial, core_target::info_proc_mappings):
	Update.
	* bfd-target.c (target_bfd::xfer_partial): Update.
	(target_bfd::target_bfd): Update.
	(target_bfd::~target_bfd): Remove.
---
 gdb/ChangeLog        |  49 +++++++++
 gdb/bfd-target.c     |  13 +--
 gdb/corelow.c        |  58 ++++------
 gdb/exec.c           | 247 +++++++++++++++----------------------------
 gdb/exec.h           |   9 +-
 gdb/record-full.c    |  33 +++---
 gdb/remote.c         |  11 +-
 gdb/solib-dsbt.c     |  18 ++--
 gdb/solib-svr4.c     |  18 ++--
 gdb/solib.c          |  33 +++---
 gdb/solist.h         |   4 +-
 gdb/symfile.c        |  13 +--
 gdb/symfile.h        |   6 +-
 gdb/target-section.h |   5 +-
 gdb/target.c         |  14 +--
 15 files changed, 230 insertions(+), 301 deletions(-)

diff --git a/gdb/bfd-target.c b/gdb/bfd-target.c
index 3d266951c5a..8a58e92eb1c 100644
--- a/gdb/bfd-target.c
+++ b/gdb/bfd-target.c
@@ -35,7 +35,6 @@ class target_bfd : public target_ops
 {
 public:
   explicit target_bfd (struct bfd *bfd);
-  ~target_bfd () override;
 
   const target_info &info () const override
   { return target_bfd_target_info; }
@@ -76,8 +75,7 @@ target_bfd::xfer_partial (target_object object,
       {
 	return section_table_xfer_memory_partial (readbuf, writebuf,
 						  offset, len, xfered_len,
-						  m_table.sections,
-						  m_table.sections_end);
+						  m_table);
       }
     default:
       return TARGET_XFER_E_IO;
@@ -93,14 +91,7 @@ target_bfd::get_section_table ()
 target_bfd::target_bfd (struct bfd *abfd)
   : m_bfd (gdb_bfd_ref_ptr::new_reference (abfd))
 {
-  m_table.sections = NULL;
-  m_table.sections_end = NULL;
-  build_section_table (abfd, &m_table.sections, &m_table.sections_end);
-}
-
-target_bfd::~target_bfd ()
-{
-  xfree (m_table.sections);
+  build_section_table (abfd, &m_table);
 }
 
 target_ops *
diff --git a/gdb/corelow.c b/gdb/corelow.c
index e82c183eae5..554561dbb36 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -67,7 +67,6 @@ class core_target final : public process_stratum_target
 {
 public:
   core_target ();
-  ~core_target () override;
 
   const target_info &info () const override
   { return core_target_info; }
@@ -126,11 +125,11 @@ class core_target final : public process_stratum_target
      shared library bfds.  The core bfd sections are an implementation
      detail of the core target, just like ptrace is for unix child
      targets.  */
-  target_section_table m_core_section_table {};
+  target_section_table m_core_section_table;
 
   /* File-backed address space mappings: some core files include
      information about memory mapped files.  */
-  target_section_table m_core_file_mappings {};
+  target_section_table m_core_file_mappings;
 
   /* Unavailable mappings.  These correspond to pathnames which either
      weren't found or could not be opened.  Knowing these addresses can
@@ -162,21 +161,13 @@ core_target::core_target ()
 	   bfd_get_filename (core_bfd));
 
   /* Find the data section */
-  if (build_section_table (core_bfd,
-			   &m_core_section_table.sections,
-			   &m_core_section_table.sections_end))
+  if (build_section_table (core_bfd, &m_core_section_table))
     error (_("\"%s\": Can't find sections: %s"),
 	   bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
 
   build_file_mappings ();
 }
 
-core_target::~core_target ()
-{
-  xfree (m_core_section_table.sections);
-  xfree (m_core_file_mappings.sections);
-}
-
 /* Construct the target_section_table for file-backed mappings if
    they exist.
 
@@ -202,12 +193,9 @@ core_target::build_file_mappings ()
   gdbarch_read_core_file_mappings (m_core_gdbarch, core_bfd,
 
     /* After determining the number of mappings, read_core_file_mappings
-       will invoke this lambda which allocates target_section storage for
-       the mappings.  */
-    [&] (ULONGEST count)
+       will invoke this lambda.  */
+    [&] (ULONGEST)
       {
-	m_core_file_mappings.sections = XNEWVEC (struct target_section, count);
-	m_core_file_mappings.sections_end = m_core_file_mappings.sections;
       },
 
     /* read_core_file_mappings will invoke this lambda for each mapping
@@ -280,11 +268,12 @@ core_target::build_file_mappings ()
 	bfd_set_section_alignment (sec, 2);
 
 	/* Set target_section fields.  */
-	struct target_section *ts = m_core_file_mappings.sections_end++;
-	ts->addr = start;
-	ts->endaddr = end;
-	ts->owner = nullptr;
-	ts->the_bfd_section = sec;
+	m_core_file_mappings.sections.emplace_back ();
+	target_section &ts = m_core_file_mappings.sections.back ();
+	ts.addr = start;
+	ts.endaddr = end;
+	ts.owner = nullptr;
+	ts.the_bfd_section = sec;
       });
 
   normalize_mem_ranges (&m_core_unavailable_mappings);
@@ -759,8 +748,7 @@ core_target::xfer_memory_via_mappings (gdb_byte *readbuf,
   xfer_status = (section_table_xfer_memory_partial
 		   (readbuf, writebuf,
 		    offset, len, xfered_len,
-		    m_core_file_mappings.sections,
-		    m_core_file_mappings.sections_end));
+		    m_core_file_mappings));
 
   if (xfer_status == TARGET_XFER_OK || m_core_unavailable_mappings.empty ())
     return xfer_status;
@@ -818,8 +806,7 @@ core_target::xfer_partial (enum target_object object, const char *annex,
 	xfer_status = section_table_xfer_memory_partial
 			(readbuf, writebuf,
 			 offset, len, xfered_len,
-			 m_core_section_table.sections,
-			 m_core_section_table.sections_end,
+			 m_core_section_table,
 			 has_contents_cb);
 	if (xfer_status == TARGET_XFER_OK)
 	  return TARGET_XFER_OK;
@@ -829,7 +816,7 @@ core_target::xfer_partial (enum target_object object, const char *annex,
 	   or the like) as this should provide a more accurate
 	   result.  If not, check the stratum beneath us, which should
 	   be the file stratum.  */
-	if (m_core_file_mappings.sections != nullptr)
+	if (!m_core_file_mappings.sections.empty ())
 	  xfer_status = xfer_memory_via_mappings (readbuf, writebuf, offset,
 						  len, xfered_len);
 	else
@@ -848,8 +835,7 @@ core_target::xfer_partial (enum target_object object, const char *annex,
 	xfer_status = section_table_xfer_memory_partial
 			(readbuf, writebuf,
 			 offset, len, xfered_len,
-			 m_core_section_table.sections,
-			 m_core_section_table.sections_end,
+			 m_core_section_table,
 			 no_contents_cb);
 
 	return xfer_status;
@@ -1114,7 +1100,7 @@ get_current_core_target ()
 void
 core_target::info_proc_mappings (struct gdbarch *gdbarch)
 {
-  if (m_core_file_mappings.sections != m_core_file_mappings.sections_end)
+  if (!m_core_file_mappings.sections.empty ())
     {
       printf_filtered (_("Mapped address spaces:\n\n"));
       if (gdbarch_addr_bit (gdbarch) == 32)
@@ -1133,14 +1119,12 @@ core_target::info_proc_mappings (struct gdbarch *gdbarch)
 	}
     }
 
-  for (const struct target_section *tsp = m_core_file_mappings.sections;
-       tsp < m_core_file_mappings.sections_end;
-       tsp++)
+  for (const target_section &tsp : m_core_file_mappings.sections)
     {
-      ULONGEST start = tsp->addr;
-      ULONGEST end = tsp->endaddr;
-      ULONGEST file_ofs = tsp->the_bfd_section->filepos;
-      const char *filename = bfd_get_filename (tsp->the_bfd_section->owner);
+      ULONGEST start = tsp.addr;
+      ULONGEST end = tsp.endaddr;
+      ULONGEST file_ofs = tsp.the_bfd_section->filepos;
+      const char *filename = bfd_get_filename (tsp.the_bfd_section->owner);
 
       if (gdbarch_addr_bit (gdbarch) == 32)
 	printf_filtered ("\t%10s %10s %10s %10s %s\n",
diff --git a/gdb/exec.c b/gdb/exec.c
index 251e24dd26b..e3e515fedec 100644
--- a/gdb/exec.c
+++ b/gdb/exec.c
@@ -413,7 +413,7 @@ exec_file_attach (const char *filename, int from_tty)
       int load_via_target = 0;
       const char *scratch_pathname, *canonical_pathname;
       int scratch_chan;
-      struct target_section *sections = NULL, *sections_end = NULL;
+      target_section_table sections;
       char **matching;
 
       if (is_target_filename (filename))
@@ -503,7 +503,7 @@ exec_file_attach (const char *filename, int from_tty)
 		 gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
 	}
 
-      if (build_section_table (exec_bfd, &sections, &sections_end))
+      if (build_section_table (exec_bfd, &sections))
 	{
 	  /* Make sure to close exec_bfd, or else "run" might try to use
 	     it.  */
@@ -522,8 +522,7 @@ exec_file_attach (const char *filename, int from_tty)
       /* Add the executable's sections to the current address spaces'
 	 list of sections.  This possibly pushes the exec_ops
 	 target.  */
-      add_target_sections (&exec_bfd, sections, sections_end);
-      xfree (sections);
+      add_target_sections (&exec_bfd, sections);
 
       /* Tell display code (if any) about the changed file name.  */
       if (deprecated_exec_file_display_hook)
@@ -592,50 +591,16 @@ file_command (const char *arg, int from_tty)
 void
 clear_section_table (struct target_section_table *table)
 {
-  xfree (table->sections);
-  table->sections = table->sections_end = NULL;
+  table->sections.clear ();
 }
 
-/* Resize section table TABLE by ADJUSTMENT.
-   ADJUSTMENT may be negative, in which case the caller must have already
-   removed the sections being deleted.
-   Returns the old size.  */
-
-static int
-resize_section_table (struct target_section_table *table, int adjustment)
-{
-  int old_count;
-  int new_count;
-
-  old_count = table->sections_end - table->sections;
-
-  new_count = adjustment + old_count;
-
-  if (new_count)
-    {
-      table->sections = XRESIZEVEC (struct target_section, table->sections,
-				    new_count);
-      table->sections_end = table->sections + new_count;
-    }
-  else
-    clear_section_table (table);
-
-  return old_count;
-}
-
-/* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
+/* Builds a section table, given args BFD, TABLE.
    Returns 0 if OK, 1 on error.  */
 
 int
-build_section_table (struct bfd *some_bfd, struct target_section **start,
-		     struct target_section **end)
+build_section_table (struct bfd *some_bfd, target_section_table *table)
 {
-  unsigned count;
-
-  count = bfd_count_sections (some_bfd);
-  xfree (*start);
-  *start = XNEWVEC (struct target_section, count);
-  *end = *start;
+  table->sections.clear ();
   for (asection *asect : gdb_bfd_sections (some_bfd))
     {
       flagword aflag;
@@ -650,15 +615,14 @@ build_section_table (struct bfd *some_bfd, struct target_section **start,
       if (!(aflag & SEC_ALLOC))
 	continue;
 
-      (*end)->owner = NULL;
-      (*end)->the_bfd_section = asect;
-      (*end)->addr = bfd_section_vma (asect);
-      (*end)->endaddr = (*end)->addr + bfd_section_size (asect);
-      (*end)++;
+      table->sections.emplace_back ();
+      target_section &sect = table->sections.back ();
+      sect.owner = NULL;
+      sect.the_bfd_section = asect;
+      sect.addr = bfd_section_vma (asect);
+      sect.endaddr = sect.addr + bfd_section_size (asect);
     }
 
-  gdb_assert (*end <= *start + count);
-
   /* We could realloc the table, but it probably loses for most files.  */
   return 0;
 }
@@ -668,23 +632,16 @@ build_section_table (struct bfd *some_bfd, struct target_section **start,
 
 void
 add_target_sections (void *owner,
-		     struct target_section *sections,
-		     struct target_section *sections_end)
+		     const target_section_table &sections)
 {
-  int count;
   struct target_section_table *table = current_target_sections;
 
-  count = sections_end - sections;
-
-  if (count > 0)
+  if (!sections.sections.empty ())
     {
-      int space = resize_section_table (table, count);
-      int i;
-
-      for (i = 0; i < count; ++i)
+      for (const target_section &s : sections.sections)
 	{
-	  table->sections[space + i] = sections[i];
-	  table->sections[space + i].owner = owner;
+	  table->sections.push_back (s);
+	  table->sections.back ().owner = owner;
 	}
 
       scoped_restore_current_pspace_and_thread restore_pspace_thread;
@@ -714,9 +671,7 @@ add_target_sections_of_objfile (struct objfile *objfile)
 {
   struct target_section_table *table = current_target_sections;
   struct obj_section *osect;
-  int space;
   unsigned count = 0;
-  struct target_section *ts;
 
   if (objfile == NULL)
     return;
@@ -732,23 +687,17 @@ add_target_sections_of_objfile (struct objfile *objfile)
   if (count == 0)
     return;
 
-  space = resize_section_table (table, count);
-
-  ts = table->sections + space;
-
   ALL_OBJFILE_OSECTIONS (objfile, osect)
     {
       if (bfd_section_size (osect->the_bfd_section) == 0)
 	continue;
 
-      gdb_assert (ts < table->sections + space + count);
-
-      ts->addr = obj_section_addr (osect);
-      ts->endaddr = obj_section_endaddr (osect);
-      ts->the_bfd_section = osect->the_bfd_section;
-      ts->owner = (void *) objfile;
-
-      ts++;
+      table->sections.emplace_back ();
+      target_section &ts = table->sections.back ();
+      ts.addr = obj_section_addr (osect);
+      ts.endaddr = obj_section_endaddr (osect);
+      ts.the_bfd_section = osect->the_bfd_section;
+      ts.owner = (void *) objfile;
     }
 }
 
@@ -758,48 +707,36 @@ add_target_sections_of_objfile (struct objfile *objfile)
 void
 remove_target_sections (void *owner)
 {
-  struct target_section *src, *dest;
   struct target_section_table *table = current_target_sections;
 
   gdb_assert (owner != NULL);
 
-  dest = table->sections;
-  for (src = table->sections; src < table->sections_end; src++)
-    if (src->owner != owner)
-      {
-	/* Keep this section.  */
-	if (dest < src)
-	  *dest = *src;
-	dest++;
-      }
-
-  /* If we've dropped any sections, resize the section table.  */
-  if (dest < src)
+  auto it = std::remove_if (table->sections.begin (),
+			    table->sections.end (),
+			    [&] (target_section &sect)
+			    {
+			      return sect.owner == owner;
+			    });
+  table->sections.erase (it, table->sections.end ());
+
+  /* If we don't have any more sections to read memory from,
+     remove the file_stratum target from the stack of each
+     inferior sharing the program space.  */
+  if (table->sections.empty ())
     {
-      int old_count;
-
-      old_count = resize_section_table (table, dest - src);
+      scoped_restore_current_pspace_and_thread restore_pspace_thread;
+      program_space *curr_pspace = current_program_space;
 
-      /* If we don't have any more sections to read memory from,
-	 remove the file_stratum target from the stack of each
-	 inferior sharing the program space.  */
-      if (old_count + (dest - src) == 0)
+      for (inferior *inf : all_inferiors ())
 	{
-	  scoped_restore_current_pspace_and_thread restore_pspace_thread;
-	  program_space *curr_pspace = current_program_space;
-
-	  for (inferior *inf : all_inferiors ())
-	    {
-	      if (inf->pspace != curr_pspace)
-		continue;
+	  if (inf->pspace != curr_pspace)
+	    continue;
 
-	      if (inf->pspace->target_sections.sections
-		  != inf->pspace->target_sections.sections_end)
-		continue;
+	  if (!inf->pspace->target_sections.sections.empty ())
+	    continue;
 
-	      switch_to_inferior_no_thread (inf);
-	      unpush_target (&exec_ops);
-	    }
+	  switch_to_inferior_no_thread (inf);
+	  unpush_target (&exec_ops);
 	}
     }
 }
@@ -809,8 +746,7 @@ remove_target_sections (void *owner)
 void
 exec_on_vfork ()
 {
-  if (current_program_space->target_sections.sections
-      != current_program_space->target_sections.sections_end)
+  if (!current_program_space->target_sections.sections.empty ())
     push_target (&exec_ops);
 }
 
@@ -869,26 +805,25 @@ exec_read_partial_read_only (gdb_byte *readbuf, ULONGEST offset,
 
 static std::vector<mem_range>
 section_table_available_memory (CORE_ADDR memaddr, ULONGEST len,
-				struct target_section *sections,
-				struct target_section *sections_end)
+				const target_section_table &sections)
 {
   std::vector<mem_range> memory;
 
-  for (target_section *p = sections; p < sections_end; p++)
+  for (const target_section &p : sections.sections)
     {
-      if ((bfd_section_flags (p->the_bfd_section) & SEC_READONLY) == 0)
+      if ((bfd_section_flags (p.the_bfd_section) & SEC_READONLY) == 0)
 	continue;
 
       /* Copy the meta-data, adjusted.  */
-      if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len))
+      if (mem_ranges_overlap (p.addr, p.endaddr - p.addr, memaddr, len))
 	{
 	  ULONGEST lo1, hi1, lo2, hi2;
 
 	  lo1 = memaddr;
 	  hi1 = memaddr + len;
 
-	  lo2 = p->addr;
-	  hi2 = p->endaddr;
+	  lo2 = p.addr;
+	  hi2 = p.endaddr;
 
 	  CORE_ADDR start = std::max (lo1, lo2);
 	  int length = std::min (hi1, hi2) - start;
@@ -906,8 +841,7 @@ section_table_read_available_memory (gdb_byte *readbuf, ULONGEST offset,
 {
   target_section_table *table = target_get_section_table (&exec_ops);
   std::vector<mem_range> available_memory
-    = section_table_available_memory (offset, len,
-				      table->sections, table->sections_end);
+    = section_table_available_memory (offset, len, *table);
 
   normalize_mem_ranges (&available_memory);
 
@@ -944,37 +878,35 @@ enum target_xfer_status
 section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
 				   ULONGEST offset, ULONGEST len,
 				   ULONGEST *xfered_len,
-				   struct target_section *sections,
-				   struct target_section *sections_end,
+				   const target_section_table &sections,
 				   gdb::function_view<bool
 				     (const struct target_section *)> match_cb)
 {
   int res;
-  struct target_section *p;
   ULONGEST memaddr = offset;
   ULONGEST memend = memaddr + len;
 
   gdb_assert (len != 0);
 
-  for (p = sections; p < sections_end; p++)
+  for (const target_section &p : sections.sections)
     {
-      struct bfd_section *asect = p->the_bfd_section;
+      struct bfd_section *asect = p.the_bfd_section;
       bfd *abfd = asect->owner;
 
-      if (match_cb != nullptr && !match_cb (p))
+      if (match_cb != nullptr && !match_cb (&p))
 	continue;		/* not the section we need.  */
-      if (memaddr >= p->addr)
+      if (memaddr >= p.addr)
         {
-	  if (memend <= p->endaddr)
+	  if (memend <= p.endaddr)
 	    {
 	      /* Entire transfer is within this section.  */
 	      if (writebuf)
 		res = bfd_set_section_contents (abfd, asect,
-						writebuf, memaddr - p->addr,
+						writebuf, memaddr - p.addr,
 						len);
 	      else
 		res = bfd_get_section_contents (abfd, asect,
-						readbuf, memaddr - p->addr,
+						readbuf, memaddr - p.addr,
 						len);
 
 	      if (res != 0)
@@ -985,7 +917,7 @@ section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
 	      else
 		return TARGET_XFER_EOF;
 	    }
-	  else if (memaddr >= p->endaddr)
+	  else if (memaddr >= p.endaddr)
 	    {
 	      /* This section ends before the transfer starts.  */
 	      continue;
@@ -993,14 +925,14 @@ section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
 	  else
 	    {
 	      /* This section overlaps the transfer.  Just do half.  */
-	      len = p->endaddr - memaddr;
+	      len = p.endaddr - memaddr;
 	      if (writebuf)
 		res = bfd_set_section_contents (abfd, asect,
-						writebuf, memaddr - p->addr,
+						writebuf, memaddr - p.addr,
 						len);
 	      else
 		res = bfd_get_section_contents (abfd, asect,
-						readbuf, memaddr - p->addr,
+						readbuf, memaddr - p.addr,
 						len);
 	      if (res != 0)
 		{
@@ -1033,8 +965,7 @@ exec_target::xfer_partial (enum target_object object,
   if (object == TARGET_OBJECT_MEMORY)
     return section_table_xfer_memory_partial (readbuf, writebuf,
 					      offset, len, xfered_len,
-					      table->sections,
-					      table->sections_end);
+					      *table);
   else
     return TARGET_XFER_E_IO;
 }
@@ -1044,7 +975,6 @@ void
 print_section_info (struct target_section_table *t, bfd *abfd)
 {
   struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
-  struct target_section *p;
   /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64.  */
   int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
 
@@ -1059,10 +989,11 @@ print_section_info (struct target_section_table *t, bfd *abfd)
 	 <p == t->sections_end>.  */
       bfd_vma displacement = 0;
       bfd_vma entry_point;
+      bool found = false;
 
-      for (p = t->sections; p < t->sections_end; p++)
+      for (const target_section &p : t->sections)
 	{
-	  struct bfd_section *psect = p->the_bfd_section;
+	  struct bfd_section *psect = p.the_bfd_section;
 
 	  if ((bfd_section_flags (psect) & (SEC_ALLOC | SEC_LOAD))
 	      != (SEC_ALLOC | SEC_LOAD))
@@ -1072,11 +1003,12 @@ print_section_info (struct target_section_table *t, bfd *abfd)
 	      && abfd->start_address < (bfd_section_vma (psect)
 					+ bfd_section_size (psect)))
 	    {
-	      displacement = p->addr - bfd_section_vma (psect);
+	      displacement = p.addr - bfd_section_vma (psect);
+	      found = true;
 	      break;
 	    }
 	}
-      if (p == t->sections_end)
+      if (!found)
 	warning (_("Cannot find section for the entry point of %ps."),
 		 styled_string (file_name_style.style (),
 				bfd_get_filename (abfd)));
@@ -1087,13 +1019,13 @@ print_section_info (struct target_section_table *t, bfd *abfd)
       printf_filtered (_("\tEntry point: %s\n"),
 		       paddress (gdbarch, entry_point));
     }
-  for (p = t->sections; p < t->sections_end; p++)
+  for (const target_section &p : t->sections)
     {
-      struct bfd_section *psect = p->the_bfd_section;
+      struct bfd_section *psect = p.the_bfd_section;
       bfd *pbfd = psect->owner;
 
-      printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
-      printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
+      printf_filtered ("\t%s", hex_string_custom (p.addr, wid));
+      printf_filtered (" - %s", hex_string_custom (p.endaddr, wid));
 
       /* FIXME: A format of "08l" is not wide enough for file offsets
 	 larger than 4GB.  OTOH, making it "016l" isn't desirable either
@@ -1125,7 +1057,6 @@ exec_target::files_info ()
 static void
 set_section_command (const char *args, int from_tty)
 {
-  struct target_section *p;
   const char *secname;
   unsigned seclen;
   unsigned long secaddr;
@@ -1144,14 +1075,14 @@ set_section_command (const char *args, int from_tty)
   secaddr = parse_and_eval_address (args);
 
   table = current_target_sections;
-  for (p = table->sections; p < table->sections_end; p++)
+  for (target_section &p : table->sections)
     {
-      if (!strncmp (secname, bfd_section_name (p->the_bfd_section), seclen)
-	  && bfd_section_name (p->the_bfd_section)[seclen] == '\0')
+      if (!strncmp (secname, bfd_section_name (p.the_bfd_section), seclen)
+	  && bfd_section_name (p.the_bfd_section)[seclen] == '\0')
 	{
-	  offset = secaddr - p->addr;
-	  p->addr += offset;
-	  p->endaddr += offset;
+	  offset = secaddr - p.addr;
+	  p.addr += offset;
+	  p.endaddr += offset;
 	  if (from_tty)
 	    exec_ops.files_info ();
 	  return;
@@ -1170,18 +1101,17 @@ set_section_command (const char *args, int from_tty)
 void
 exec_set_section_address (const char *filename, int index, CORE_ADDR address)
 {
-  struct target_section *p;
   struct target_section_table *table;
 
   table = current_target_sections;
-  for (p = table->sections; p < table->sections_end; p++)
+  for (target_section &p : table->sections)
     {
       if (filename_cmp (filename,
-			bfd_get_filename (p->the_bfd_section->owner)) == 0
-	  && index == p->the_bfd_section->index)
+			bfd_get_filename (p.the_bfd_section->owner)) == 0
+	  && index == p.the_bfd_section->index)
 	{
-	  p->endaddr += address - p->addr;
-	  p->addr = address;
+	  p.endaddr += address - p.addr;
+	  p.addr = address;
 	}
     }
 }
@@ -1191,8 +1121,7 @@ exec_target::has_memory ()
 {
   /* We can provide memory if we have any file/target sections to read
      from.  */
-  return (current_target_sections->sections
-	  != current_target_sections->sections_end);
+  return !current_target_sections->sections.empty ();
 }
 
 char *
diff --git a/gdb/exec.h b/gdb/exec.h
index 82eb39c55d8..d26eba49236 100644
--- a/gdb/exec.h
+++ b/gdb/exec.h
@@ -37,8 +37,7 @@ struct objfile;
 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
    Returns 0 if OK, 1 on error.  */
 
-extern int build_section_table (struct bfd *, struct target_section **,
-				struct target_section **);
+extern int build_section_table (struct bfd *, struct target_section_table *);
 
 /* Remove all entries from TABLE.  */
 
@@ -86,8 +85,7 @@ extern enum target_xfer_status
   section_table_xfer_memory_partial (gdb_byte *,
 				     const gdb_byte *,
 				     ULONGEST, ULONGEST, ULONGEST *,
-				     struct target_section *,
-				     struct target_section *,
+				     const target_section_table &,
 				     gdb::function_view<bool
 				       (const struct target_section *)> match_cb
 				         = nullptr);
@@ -111,8 +109,7 @@ extern void remove_target_sections (void *owner);
    current set of target sections.  */
 
 extern void add_target_sections (void *owner,
-				 struct target_section *sections,
-				 struct target_section *sections_end);
+				 const target_section_table &sections);
 
 /* Add the sections of OBJFILE to the current set of target sections.
  * OBJFILE owns the new target sections.  */
diff --git a/gdb/record-full.c b/gdb/record-full.c
index 9a6187e8104..5dcb42d2f4e 100644
--- a/gdb/record-full.c
+++ b/gdb/record-full.c
@@ -171,8 +171,7 @@ struct record_full_core_buf_entry
 
 /* Record buf with core target.  */
 static detached_regcache *record_full_core_regbuf = NULL;
-static struct target_section *record_full_core_start;
-static struct target_section *record_full_core_end;
+static target_section_table record_full_core_sections;
 static struct record_full_core_buf_entry *record_full_core_buf_list = NULL;
 
 /* The following variables are used for managing the linked list that
@@ -924,9 +923,7 @@ record_full_core_open_1 (const char *name, int from_tty)
   for (i = 0; i < regnum; i ++)
     record_full_core_regbuf->raw_supply (i, *regcache);
 
-  /* Get record_full_core_start and record_full_core_end.  */
-  if (build_section_table (core_bfd, &record_full_core_start,
-			   &record_full_core_end))
+  if (build_section_table (core_bfd, &record_full_core_sections))
     {
       delete record_full_core_regbuf;
       record_full_core_regbuf = NULL;
@@ -2147,27 +2144,25 @@ record_full_core_target::xfer_partial (enum target_object object,
     {
       if (record_full_gdb_operation_disable || !writebuf)
 	{
-	  struct target_section *p;
-
-	  for (p = record_full_core_start; p < record_full_core_end; p++)
+	  for (target_section &p : record_full_core_sections.sections)
 	    {
-	      if (offset >= p->addr)
+	      if (offset >= p.addr)
 		{
 		  struct record_full_core_buf_entry *entry;
 		  ULONGEST sec_offset;
 
-		  if (offset >= p->endaddr)
+		  if (offset >= p.endaddr)
 		    continue;
 
-		  if (offset + len > p->endaddr)
-		    len = p->endaddr - offset;
+		  if (offset + len > p.endaddr)
+		    len = p.endaddr - offset;
 
-		  sec_offset = offset - p->addr;
+		  sec_offset = offset - p.addr;
 
 		  /* Read readbuf or write writebuf p, offset, len.  */
 		  /* Check flags.  */
-		  if (p->the_bfd_section->flags & SEC_CONSTRUCTOR
-		      || (p->the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
+		  if (p.the_bfd_section->flags & SEC_CONSTRUCTOR
+		      || (p.the_bfd_section->flags & SEC_HAS_CONTENTS) == 0)
 		    {
 		      if (readbuf)
 			memset (readbuf, 0, len);
@@ -2178,7 +2173,7 @@ record_full_core_target::xfer_partial (enum target_object object,
 		  /* Get record_full_core_buf_entry.  */
 		  for (entry = record_full_core_buf_list; entry;
 		       entry = entry->prev)
-		    if (entry->p == p)
+		    if (entry->p == &p)
 		      break;
 		  if (writebuf)
 		    {
@@ -2186,10 +2181,10 @@ record_full_core_target::xfer_partial (enum target_object object,
 			{
 			  /* Add a new entry.  */
 			  entry = XNEW (struct record_full_core_buf_entry);
-			  entry->p = p;
+			  entry->p = &p;
 			  if (!bfd_malloc_and_get_section
-			        (p->the_bfd_section->owner,
-				 p->the_bfd_section,
+			        (p.the_bfd_section->owner,
+				 p.the_bfd_section,
 				 &entry->buf))
 			    {
 			      xfree (entry);
diff --git a/gdb/remote.c b/gdb/remote.c
index f95148643f8..c9c5ed03ea2 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -8895,22 +8895,21 @@ remote_target::remote_xfer_live_readonly_partial (gdb_byte *readbuf,
   if (secp != NULL
       && (bfd_section_flags (secp->the_bfd_section) & SEC_READONLY))
     {
-      struct target_section *p;
       ULONGEST memend = memaddr + len;
 
       table = target_get_section_table (this);
 
-      for (p = table->sections; p < table->sections_end; p++)
+      for (target_section &p : table->sections)
 	{
-	  if (memaddr >= p->addr)
+	  if (memaddr >= p.addr)
 	    {
-	      if (memend <= p->endaddr)
+	      if (memend <= p.endaddr)
 		{
 		  /* Entire transfer is within this section.  */
 		  return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
 					      xfered_len);
 		}
-	      else if (memaddr >= p->endaddr)
+	      else if (memaddr >= p.endaddr)
 		{
 		  /* This section ends before the transfer starts.  */
 		  continue;
@@ -8918,7 +8917,7 @@ remote_target::remote_xfer_live_readonly_partial (gdb_byte *readbuf,
 	      else
 		{
 		  /* This section overlaps the transfer.  Just do half.  */
-		  len = p->endaddr - memaddr;
+		  len = p.endaddr - memaddr;
 		  return remote_read_bytes_1 (memaddr, readbuf, len, unit_size,
 					      xfered_len);
 		}
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index 0f146725dba..59089216314 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -407,7 +407,6 @@ scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
   Elf32_External_Dyn *x_dynp_32;
   Elf64_External_Dyn *x_dynp_64;
   struct bfd_section *sect;
-  struct target_section *target_section;
 
   if (abfd == NULL)
     return 0;
@@ -424,14 +423,15 @@ scan_dyntag (int dyntag, bfd *abfd, CORE_ADDR *ptr)
   if (sect == NULL)
     return 0;
 
-  for (target_section = current_target_sections->sections;
-       target_section < current_target_sections->sections_end;
-       target_section++)
-    if (sect == target_section->the_bfd_section)
-      break;
-  if (target_section < current_target_sections->sections_end)
-    dyn_addr = target_section->addr;
-  else
+  bool found = false;
+  for (target_section &target_section : current_target_sections->sections)
+    if (sect == target_section.the_bfd_section)
+      {
+	dyn_addr = target_section.addr;
+	found = true;
+	break;
+      }
+  if (!found)
     {
       /* ABFD may come from OBJFILE acting only as a symbol file without being
 	 loaded into the target (see add_symbol_file_command).  This case is
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 4ae21d190bf..9bb728ae9b9 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -591,7 +591,6 @@ scan_dyntag (const int desired_dyntag, bfd *abfd, CORE_ADDR *ptr,
   Elf32_External_Dyn *x_dynp_32;
   Elf64_External_Dyn *x_dynp_64;
   struct bfd_section *sect;
-  struct target_section *target_section;
 
   if (abfd == NULL)
     return 0;
@@ -608,14 +607,15 @@ scan_dyntag (const int desired_dyntag, bfd *abfd, CORE_ADDR *ptr,
   if (sect == NULL)
     return 0;
 
-  for (target_section = current_target_sections->sections;
-       target_section < current_target_sections->sections_end;
-       target_section++)
-    if (sect == target_section->the_bfd_section)
-      break;
-  if (target_section < current_target_sections->sections_end)
-    dyn_addr = target_section->addr;
-  else
+  bool found = false;
+  for (target_section &target_section : current_target_sections->sections)
+    if (sect == target_section.the_bfd_section)
+      {
+	dyn_addr = target_section.addr;
+	found = true;
+	break;
+      }
+  if (!found)
     {
       /* ABFD may come from OBJFILE acting only as a symbol file without being
 	 loaded into the target (see add_symbol_file_command).  This case is
diff --git a/gdb/solib.c b/gdb/solib.c
index cf5d05e83b2..906e1788c49 100644
--- a/gdb/solib.c
+++ b/gdb/solib.c
@@ -532,7 +532,6 @@ static int
 solib_map_sections (struct so_list *so)
 {
   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
-  struct target_section *p;
 
   gdb::unique_xmalloc_ptr<char> filename (tilde_expand (so->so_name));
   gdb_bfd_ref_ptr abfd (ops->bfd_open (filename.get ()));
@@ -553,27 +552,29 @@ solib_map_sections (struct so_list *so)
     error (_("Shared library file name is too long."));
   strcpy (so->so_name, bfd_get_filename (so->abfd));
 
-  if (build_section_table (so->abfd, &so->sections, &so->sections_end))
+  if (so->sections == nullptr)
+    so->sections = new target_section_table;
+  if (build_section_table (so->abfd, so->sections))
     {
       error (_("Can't find the file sections in `%s': %s"),
 	     bfd_get_filename (so->abfd), bfd_errmsg (bfd_get_error ()));
     }
 
-  for (p = so->sections; p < so->sections_end; p++)
+  for (target_section &p : so->sections->sections)
     {
       /* Relocate the section binding addresses as recorded in the shared
          object's file by the base address to which the object was actually
          mapped.  */
-      ops->relocate_section_addresses (so, p);
+      ops->relocate_section_addresses (so, &p);
 
       /* If the target didn't provide information about the address
 	 range of the shared object, assume we want the location of
 	 the .text section.  */
       if (so->addr_low == 0 && so->addr_high == 0
-	  && strcmp (p->the_bfd_section->name, ".text") == 0)
+	  && strcmp (p.the_bfd_section->name, ".text") == 0)
 	{
-	  so->addr_low = p->addr;
-	  so->addr_high = p->endaddr;
+	  so->addr_low = p.addr;
+	  so->addr_high = p.endaddr;
 	}
     }
 
@@ -581,7 +582,7 @@ solib_map_sections (struct so_list *so)
      section tables.  Do this immediately after mapping the object so
      that later nodes in the list can query this object, as is needed
      in solib-osf.c.  */
-  add_target_sections (so, so->sections, so->sections_end);
+  add_target_sections (so, *so->sections);
 
   return 1;
 }
@@ -600,11 +601,8 @@ clear_so (struct so_list *so)
 {
   const struct target_so_ops *ops = solib_ops (target_gdbarch ());
 
-  if (so->sections)
-    {
-      xfree (so->sections);
-      so->sections = so->sections_end = NULL;
-    }
+  delete so->sections;
+  so->sections = NULL;
 
   gdb_bfd_unref (so->abfd);
   so->abfd = NULL;
@@ -683,8 +681,7 @@ solib_read_symbols (struct so_list *so, symfile_add_flags flags)
 	  if (so->objfile == NULL)
 	    {
 	      section_addr_info sap
-		= build_section_addr_info_from_section_table (so->sections,
-							      so->sections_end);
+		= build_section_addr_info_from_section_table (*so->sections);
 	      so->objfile = symbol_file_add_from_bfd (so->abfd, so->so_name,
 						      flags, &sap,
 						      OBJF_SHARED, NULL);
@@ -1120,10 +1117,8 @@ bool
 solib_contains_address_p (const struct so_list *const solib,
 			  CORE_ADDR address)
 {
-  struct target_section *p;
-
-  for (p = solib->sections; p < solib->sections_end; p++)
-    if (p->addr <= address && address < p->endaddr)
+  for (target_section &p : solib->sections->sections)
+    if (p.addr <= address && address < p.endaddr)
       return true;
 
   return false;
diff --git a/gdb/solist.h b/gdb/solist.h
index 0360d342ae8..1de797cc14b 100644
--- a/gdb/solist.h
+++ b/gdb/solist.h
@@ -23,6 +23,7 @@
 /* For domain_enum domain.  */
 #include "symtab.h"
 #include "gdb_bfd.h"
+#include "target-section.h"
 
 /* Base class for target-specific link map information.  */
 
@@ -71,8 +72,7 @@ struct so_list
      the file cannot be found or after the command "nosharedlibrary".  */
   struct objfile *objfile;
 
-  struct target_section *sections;
-  struct target_section *sections_end;
+  target_section_table *sections;
 
   /* Record the range of addresses belonging to this shared library.
      There may not be just one (e.g. if two segments are relocated
diff --git a/gdb/symfile.c b/gdb/symfile.c
index baed72e936e..f4acec50cca 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -212,21 +212,18 @@ find_lowest_section (asection *sect, asection **lowest)
    an existing section table.  */
 
 section_addr_info
-build_section_addr_info_from_section_table (const struct target_section *start,
-                                            const struct target_section *end)
+build_section_addr_info_from_section_table (const target_section_table &table)
 {
-  const struct target_section *stp;
-
   section_addr_info sap;
 
-  for (stp = start; stp != end; stp++)
+  for (const target_section &stp : table.sections)
     {
-      struct bfd_section *asect = stp->the_bfd_section;
+      struct bfd_section *asect = stp.the_bfd_section;
       bfd *abfd = asect->owner;
 
       if (bfd_section_flags (asect) & (SEC_ALLOC | SEC_LOAD)
-	  && sap.size () < end - start)
-	sap.emplace_back (stp->addr,
+	  && sap.size () < table.sections.size ())
+	sap.emplace_back (stp.addr,
 			  bfd_section_name (asect),
 			  gdb_bfd_section_index (abfd, asect));
     }
diff --git a/gdb/symfile.h b/gdb/symfile.h
index ae29451fc6a..df646f0736f 100644
--- a/gdb/symfile.h
+++ b/gdb/symfile.h
@@ -27,6 +27,7 @@
 #include "objfile-flags.h"
 #include "gdb_bfd.h"
 #include "gdbsupport/function-view.h"
+#include "target-section.h"
 
 /* Opaque declarations.  */
 struct target_section;
@@ -451,10 +452,7 @@ extern std::string find_separate_debug_file_by_debuglink (struct objfile *);
    existing section table.  */
 
 extern section_addr_info
-   build_section_addr_info_from_section_table (const struct target_section
-					       *start,
-					       const struct target_section
-					       *end);
+    build_section_addr_info_from_section_table (const target_section_table &table);
 
 			/*   Variables   */
 
diff --git a/gdb/target-section.h b/gdb/target-section.h
index 581bc1dbe9f..8c81f3d79ba 100644
--- a/gdb/target-section.h
+++ b/gdb/target-section.h
@@ -39,12 +39,11 @@ struct target_section
     void *owner;
   };
 
-/* Holds an array of target sections.  Defined by [SECTIONS..SECTIONS_END[.  */
+/* Holds an array of target sections.  */
 
 struct target_section_table
 {
-  struct target_section *sections;
-  struct target_section *sections_end;
+  std::vector<struct target_section> sections;
 };
 
 #endif /* GDB_TARGET_SECTION_H */
diff --git a/gdb/target.c b/gdb/target.c
index dd78a848cae..f828d3d6cc3 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -823,15 +823,14 @@ struct target_section *
 target_section_by_addr (struct target_ops *target, CORE_ADDR addr)
 {
   struct target_section_table *table = target_get_section_table (target);
-  struct target_section *secp;
 
   if (table == NULL)
     return NULL;
 
-  for (secp = table->sections; secp < table->sections_end; secp++)
+  for (target_section &secp : table->sections)
     {
-      if (addr >= secp->addr && addr < secp->endaddr)
-	return secp;
+      if (addr >= secp.addr && addr < secp.endaddr)
+	return &secp;
     }
   return NULL;
 }
@@ -978,9 +977,7 @@ memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
 
 	  return section_table_xfer_memory_partial (readbuf, writebuf,
 						    memaddr, len, xfered_len,
-						    table->sections,
-						    table->sections_end,
-						    match_cb);
+						    *table, match_cb);
 	}
     }
 
@@ -997,8 +994,7 @@ memory_xfer_partial_1 (struct target_ops *ops, enum target_object object,
 	  table = target_get_section_table (ops);
 	  return section_table_xfer_memory_partial (readbuf, writebuf,
 						    memaddr, len, xfered_len,
-						    table->sections,
-						    table->sections_end);
+						    *table);
 	}
     }
 
-- 
2.17.2


  parent reply	other threads:[~2020-10-03 19:37 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-03 19:37 [PATCH 0/6] Change target section table management Tom Tromey
2020-10-03 19:37 ` [PATCH 1/6] Introduce target-section.h Tom Tromey
2020-10-04  8:29   ` Andrew Burgess
2020-10-09  1:17     ` Tom Tromey
2020-10-03 19:37 ` Tom Tromey [this message]
2020-10-13 16:41   ` [PATCH 2/6] Use a std::vector in target_section_table Luis Machado
2020-10-13 20:35     ` Tom Tromey
2020-10-13 22:48       ` Luis Machado
2020-10-14 12:58         ` Tom Tromey
2020-10-14 13:01           ` Luis Machado
2020-10-14 13:18             ` Tom Tromey
2020-10-14 13:23               ` Luis Machado
2020-10-14 14:06                 ` Simon Marchi
2020-10-14  9:09   ` Tom de Vries
2020-11-10 17:38   ` Simon Marchi
2020-10-03 19:37 ` [PATCH 3/6] build_section_table cannot fail Tom Tromey
2020-10-04  8:38   ` Andrew Burgess
2020-10-03 19:37 ` [PATCH 4/6] Simplify add_target_sections_of_objfile Tom Tromey
2020-10-04  8:40   ` Andrew Burgess
2020-10-03 19:37 ` [PATCH 5/6] Remove clear_section_table Tom Tromey
2020-10-04  8:44   ` Andrew Burgess
2020-10-03 19:37 ` [PATCH 6/6] Change target_section_table to std::vector alias Tom Tromey
2020-10-04  8:46   ` Andrew Burgess
2020-10-13  2:19 ` [PATCH 0/6] Change target section table management Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201003193735.2532-3-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).