This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[5/10] RFC: introduce gdb_bfd_section_index


One of the goals of this series is to unify SYMBOL_SECTION and the BFD
section index.  This makes the code simpler to reason about, and lets us
remove the obj_section field.

However, when I looked into this some more, I found out that BFD has
some special sections which are globals in BFD, and which have a section
index of 0.

This patch works around this BFD problem by introducing
gdb_bfd_section_index to compute the proper index.  The special BFD
sections are all placed after a given BFD's ordinary sections.

I added a static assertion to gdb_bfd.c to notice the case where BFD
adds or removes sections of this type.

This patch updates most users of the BFD index to use this function
instead.  I left some spots untouched if they were provably ok; but I
could update every spot if that seems preferable.

Tom

	* coffread.c (cs_to_section): Use gdb_bfd_section_index.
	* elfread.c (record_minimal_symbol, elf_symtab_read): Use
	gdb_bfd_section_index.
	* gdb_bfd.c (gdb_bfd_section_index, gdb_bfd_count_sections):
	New functions.
	* gdb_bfd.h (gdb_bfd_section_index, gdb_bfd_count_sections):
	Declare.
	* machoread.c (macho_symtab_add_minsym, macho_symfile_offsets):
	Update.
	* objfiles.c (add_to_objfile_sections_full): New function.
	(add_to_objfile_sections): Use it.
	(build_section_table): Rewrite.
	(objfile_relocate1): Use gdb_bfd_section_index.  Update.
	* objfiles.h (obj_section_offset): Use gdb_bfd_section_index.
	(struct objfile) <sections>: Update comment.
	(ALL_OBJFILE_OSECTIONS): Skip sections where the_bfd_section
	is NULL.
	(ALL_OBJSECTIONS): Use it.
	* solib-dsbt.c (dsbt_relocate_main_executable): Update.
	* solib-frv.c (frv_relocate_main_executable): Update.
	* solib-target.c (solib_target_relocate_section_addresses):
	Use gdb_bfd_section_index.
	* symfile.c (build_section_addr_info_from_section_table):
	Use gdb_bfd_section_index.
	(build_section_addr_info_from_bfd, place_section): Likewise.
	* symtab.c (fixup_section): Update.
	* xcoffread.c (find_targ_sec): Use gdb_bfd_section_index.
---
 gdb/coffread.c     |    2 +-
 gdb/elfread.c      |   10 +++++--
 gdb/gdb_bfd.c      |   30 ++++++++++++++++++++
 gdb/gdb_bfd.h      |   14 +++++++++
 gdb/machoread.c    |   10 ++++---
 gdb/objfiles.c     |   75 +++++++++++++++++++++++++++++-----------------------
 gdb/objfiles.h     |   21 +++++++++-----
 gdb/solib-dsbt.c   |    2 +-
 gdb/solib-frv.c    |    2 +-
 gdb/solib-target.c |    3 +-
 gdb/symfile.c      |    9 +++---
 gdb/symtab.c       |    2 +-
 gdb/xcoffread.c    |    2 +-
 13 files changed, 124 insertions(+), 58 deletions(-)

diff --git a/gdb/coffread.c b/gdb/coffread.c
index e5629a6..f4bafb2 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -297,7 +297,7 @@ cs_to_section (struct coff_symbol *cs, struct objfile *objfile)
 
   if (sect == NULL)
     return SECT_OFF_TEXT (objfile);
-  return sect->index;
+  return gdb_bfd_section_index (objfile->obfd, sect);
 }
 
 /* Return the address of the section of a COFF symbol.  */
diff --git a/gdb/elfread.c b/gdb/elfread.c
index f9c5aa3..dde21ef 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -208,7 +208,9 @@ record_minimal_symbol (const char *name, int name_len, int copy_name,
     address = gdbarch_addr_bits_remove (gdbarch, address);
 
   return prim_record_minimal_symbol_full (name, name_len, copy_name, address,
-					  ms_type, bfd_section->index,
+					  ms_type,
+					  gdb_bfd_section_index (objfile->obfd,
+								 bfd_section),
 					  bfd_section, objfile);
 }
 
@@ -271,7 +273,8 @@ elf_symtab_read (struct objfile *objfile, int type,
 	  continue;
 	}
 
-      offset = ANOFFSET (objfile->section_offsets, sym->section->index);
+      offset = ANOFFSET (objfile->section_offsets,
+			 gdb_bfd_section_index (objfile->obfd, sym->section));
       if (type == ST_DYNAMIC
 	  && sym->section == bfd_und_section_ptr
 	  && (sym->flags & BSF_FUNCTION))
@@ -326,7 +329,8 @@ elf_symtab_read (struct objfile *objfile, int type,
 	      && bfd_get_section_by_name (abfd, ".plt") != NULL)
 	    continue;
 
-	  symaddr += ANOFFSET (objfile->section_offsets, sect->index);
+	  symaddr += ANOFFSET (objfile->section_offsets,
+			       gdb_bfd_section_index (objfile->obfd, sect));
 
 	  msym = record_minimal_symbol
 	    (sym->name, strlen (sym->name), copy_names,
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index d4540b9..9d0331f 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -547,6 +547,36 @@ gdb_bfd_fdopenr (const char *filename, const char *target, int fd)
 
 
 
+gdb_static_assert (ARRAY_SIZE (_bfd_std_section) == 4);
+
+/* See gdb_bfd.h.  */
+
+int
+gdb_bfd_section_index (bfd *abfd, asection *section)
+{
+  if (section == NULL)
+    return -1;
+  else if (section == bfd_com_section_ptr)
+    return bfd_count_sections (abfd) + 1;
+  else if (section == bfd_und_section_ptr)
+    return bfd_count_sections (abfd) + 2;
+  else if (section == bfd_abs_section_ptr)
+    return bfd_count_sections (abfd) + 3;
+  else if (section == bfd_ind_section_ptr)
+    return bfd_count_sections (abfd) + 4;
+  return section->index;
+}
+
+/* See gdb_bfd.h.  */
+
+int
+gdb_bfd_count_sections (bfd *abfd)
+{
+  return bfd_count_sections (abfd) + 4;
+}
+
+
+
 /* A callback for htab_traverse that prints a single BFD.  */
 
 static int
diff --git a/gdb/gdb_bfd.h b/gdb/gdb_bfd.h
index f3ee9ff..f60fb73 100644
--- a/gdb/gdb_bfd.h
+++ b/gdb/gdb_bfd.h
@@ -115,4 +115,18 @@ bfd *gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous);
 
 bfd *gdb_bfd_fdopenr (const char *filename, const char *target, int fd);
 
+
+
+/* Return the index of the BFD section SECTION.  Ordinarily this is
+   just the section's index, but for some special sections, like
+   bfd_com_section_ptr, it will be a synthesized value.  */
+
+int gdb_bfd_section_index (bfd *abfd, asection *section);
+
+
+/* Like bfd_count_sections, but include any possible global sections,
+   like bfd_com_section_ptr.  */
+
+int gdb_bfd_count_sections (bfd *abfd);
+
 #endif /* GDB_BFD_H */
diff --git a/gdb/machoread.c b/gdb/machoread.c
index fe77a2d..8385484 100644
--- a/gdb/machoread.c
+++ b/gdb/machoread.c
@@ -116,7 +116,8 @@ macho_symtab_add_minsym (struct objfile *objfile, const asymbol *sym)
       CORE_ADDR offset;
       enum minimal_symbol_type ms_type;
 
-      offset = ANOFFSET (objfile->section_offsets, sym->section->index);
+      offset = ANOFFSET (objfile->section_offsets,
+			 gdb_bfd_section_index (objfile->obfd, sym->section));
 
       /* Bfd symbols are section relative.  */
       symaddr = sym->value + sym->section->vma;
@@ -164,8 +165,9 @@ macho_symtab_add_minsym (struct objfile *objfile, const asymbol *sym)
         return;	/* Skip this symbol.  */
 
       prim_record_minimal_symbol_and_info
-        (sym->name, symaddr, ms_type, sym->section->index,
-         sym->section, objfile);
+        (sym->name, symaddr, ms_type,
+	 gdb_bfd_section_index (objfile->obfd, sym->section),
+	 sym->section, objfile);
     }
 }
 
@@ -1011,7 +1013,7 @@ macho_symfile_offsets (struct objfile *objfile,
   ALL_OBJFILE_OSECTIONS (objfile, osect)
     {
       const char *bfd_sect_name = osect->the_bfd_section->name;
-      int sect_index = osect->the_bfd_section->index;
+      int sect_index = osect - objfile->sections;;
 
       if (strncmp (bfd_sect_name, "LC_SEGMENT.", 11) == 0)
 	bfd_sect_name += 11;
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index 8c17c14..26e2b33 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -181,50 +181,55 @@ set_objfile_per_bfd (struct objfile *objfile)
    the end of the table (objfile->sections_end).  */
 
 static void
+add_to_objfile_sections_full (struct bfd *abfd, struct bfd_section *asect,
+			      struct objfile *objfile, int force)
+{
+  struct obj_section *section;
+
+  if (!force)
+    {
+      flagword aflag;
+
+      aflag = bfd_get_section_flags (abfd, asect);
+      if (!(aflag & SEC_ALLOC))
+	return;
+    }
+
+  section = &objfile->sections[gdb_bfd_section_index (abfd, asect)];
+  section->objfile = objfile;
+  section->the_bfd_section = asect;
+  section->ovly_mapped = 0;
+}
+
+static void
 add_to_objfile_sections (struct bfd *abfd, struct bfd_section *asect,
 			 void *objfilep)
 {
-  struct objfile *objfile = (struct objfile *) objfilep;
-  struct obj_section section;
-  flagword aflag;
-
-  aflag = bfd_get_section_flags (abfd, asect);
-  if (!(aflag & SEC_ALLOC))
-    return;
-  if (bfd_section_size (abfd, asect) == 0)
-    return;
-
-  section.objfile = objfile;
-  section.the_bfd_section = asect;
-  section.ovly_mapped = 0;
-  obstack_grow (&objfile->objfile_obstack,
-		(char *) &section, sizeof (section));
-  objfile->sections_end
-    = (struct obj_section *) (((size_t) objfile->sections_end) + 1);
+  add_to_objfile_sections_full (abfd, asect, objfilep, 0);
 }
 
 /* Builds a section table for OBJFILE.
 
-   Note that while we are building the table, which goes into the
-   objfile obstack, we hijack the sections_end pointer to instead hold
-   a count of the number of sections.  When bfd_map_over_sections
-   returns, this count is used to compute the pointer to the end of
-   the sections table, which then overwrites the count.
-
-   Also note that the OFFSET and OVLY_MAPPED in each table entry
-   are initialized to zero.
-
-   Also note that if anything else writes to the objfile obstack while
-   we are building the table, we're pretty much hosed.  */
+   Note that the OFFSET and OVLY_MAPPED in each table entry are
+   initialized to zero.  */
 
 void
 build_objfile_section_table (struct objfile *objfile)
 {
-  objfile->sections_end = 0;
+  int count = gdb_bfd_count_sections (objfile->obfd);
+
+  objfile->sections = OBSTACK_CALLOC (&objfile->objfile_obstack,
+				      count,
+				      struct obj_section);
+  objfile->sections_end = (objfile->sections + count);
   bfd_map_over_sections (objfile->obfd,
 			 add_to_objfile_sections, (void *) objfile);
-  objfile->sections = obstack_finish (&objfile->objfile_obstack);
-  objfile->sections_end = objfile->sections + (size_t) objfile->sections_end;
+
+  /* See gdb_bfd_section_index.  */
+  add_to_objfile_sections_full (objfile->obfd, bfd_com_section_ptr, objfile, 1);
+  add_to_objfile_sections_full (objfile->obfd, bfd_und_section_ptr, objfile, 1);
+  add_to_objfile_sections_full (objfile->obfd, bfd_abs_section_ptr, objfile, 1);
+  add_to_objfile_sections_full (objfile->obfd, bfd_ind_section_ptr, objfile, 1);
 }
 
 /* Given a pointer to an initialized bfd (ABFD) and some flag bits
@@ -802,7 +807,11 @@ objfile_relocate1 (struct objfile *objfile,
       struct obj_section *s;
       s = find_pc_section (objfile->ei.entry_point);
       if (s)
-        objfile->ei.entry_point += ANOFFSET (delta, s->the_bfd_section->index);
+	{
+	  int idx = gdb_bfd_section_index (objfile->obfd, s->the_bfd_section);
+
+	  objfile->ei.entry_point += ANOFFSET (delta, idx);
+	}
       else
         objfile->ei.entry_point += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
     }
@@ -820,7 +829,7 @@ objfile_relocate1 (struct objfile *objfile,
   /* Update the table in exec_ops, used to read memory.  */
   ALL_OBJFILE_OSECTIONS (objfile, s)
     {
-      int idx = s->the_bfd_section->index;
+      int idx = s - objfile->sections;
 
       exec_set_section_address (bfd_get_filename (objfile->obfd), idx,
 				obj_section_addr (s));
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index 6d29084..205efcd 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -24,6 +24,7 @@
 #include "symfile.h"		/* For struct psymbol_allocation_list.  */
 #include "progspace.h"
 #include "registry.h"
+#include "gdb_bfd.h"
 
 struct bcache;
 struct htab;
@@ -123,7 +124,7 @@ struct obj_section
 
 /* Relocation offset applied to S.  */
 #define obj_section_offset(s)						\
-  (((s)->objfile->section_offsets)->offsets[(s)->the_bfd_section->index])
+  (((s)->objfile->section_offsets)->offsets[gdb_bfd_section_index ((s)->objfile->obfd, (s)->the_bfd_section)])
 
 /* The memory address of section S (vma + offset).  */
 #define obj_section_addr(s)				      		\
@@ -354,9 +355,10 @@ struct objfile
        among other things, is used to map pc addresses into sections.
        SECTIONS points to the first entry in the table, and
        SECTIONS_END points to the first location past the last entry
-       in the table.  The table is stored on the objfile_obstack.
-       There is no particular order to the sections in this table, and it
-       only contains sections we care about (e.g. non-empty, SEC_ALLOC).  */
+       in the table.  The table is stored on the objfile_obstack.  The
+       sections are indexed by the BFD section index; but the
+       structure data is only valid for certain sections
+       (e.g. non-empty, SEC_ALLOC).  */
 
     struct obj_section *sections, *sections_end;
 
@@ -581,7 +583,12 @@ extern void default_iterate_over_objfiles_in_search_order
     ALL_OBJFILE_MSYMBOLS (objfile, m)
 
 #define ALL_OBJFILE_OSECTIONS(objfile, osect)	\
-  for (osect = objfile->sections; osect < objfile->sections_end; osect++)
+  for (osect = objfile->sections; osect < objfile->sections_end; osect++) \
+    if (osect->the_bfd_section == NULL)					\
+      {									\
+	/* Nothing.  */							\
+      }									\
+    else
 
 /* Traverse all obj_sections in all objfiles in the current program
    space.
@@ -617,9 +624,7 @@ extern void default_iterate_over_objfiles_in_search_order
 	? ((objfile) = (objfile)->next,					\
 	   (objfile) != NULL ? (osect) = (objfile)->sections_end : 0)	\
 	: 0))								\
-    for ((osect) = (objfile)->sections;					\
-	 (osect) < (objfile)->sections_end;				\
-	 (osect)++)
+    ALL_OBJFILE_OSECTIONS (objfile, osect)
 
 #define SECT_OFF_DATA(objfile) \
      ((objfile->sect_index_data == -1) \
diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c
index c41326b..586ab8e 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -1054,7 +1054,7 @@ dsbt_relocate_main_executable (void)
       int osect_idx;
       int seg;
 
-      osect_idx = osect->the_bfd_section->index;
+      osect_idx = osect - symfile_objfile->sections;
 
       /* Current address of section.  */
       addr = obj_section_addr (osect);
diff --git a/gdb/solib-frv.c b/gdb/solib-frv.c
index 57e418f..52588bc 100644
--- a/gdb/solib-frv.c
+++ b/gdb/solib-frv.c
@@ -812,7 +812,7 @@ frv_relocate_main_executable (void)
       int osect_idx;
       int seg;
       
-      osect_idx = osect->the_bfd_section->index;
+      osect_idx = osect - symfile_objfile->sections;
 
       /* Current address of section.  */
       addr = obj_section_addr (osect);
diff --git a/gdb/solib-target.c b/gdb/solib-target.c
index d897bc0..0ad29ba 100644
--- a/gdb/solib-target.c
+++ b/gdb/solib-target.c
@@ -456,7 +456,8 @@ Could not relocate shared library \"%s\": bad offsets"), so->so_name);
 	}
     }
 
-  offset = so->lm_info->offsets->offsets[sec->the_bfd_section->index];
+  offset = so->lm_info->offsets->offsets[gdb_bfd_section_index (sec->bfd,
+								sec->the_bfd_section)];
   sec->addr += offset;
   sec->endaddr += offset;
 }
diff --git a/gdb/symfile.c b/gdb/symfile.c
index 6f968b7..2f2cfb3 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -235,7 +235,8 @@ build_section_addr_info_from_section_table (const struct target_section *start,
 	  sap->other[oidx].addr = stp->addr;
 	  sap->other[oidx].name
 	    = xstrdup (bfd_section_name (stp->bfd, stp->the_bfd_section));
-	  sap->other[oidx].sectindex = stp->the_bfd_section->index;
+	  sap->other[oidx].sectindex
+	    = gdb_bfd_section_index (stp->bfd, stp->the_bfd_section);
 	  oidx++;
 	}
     }
@@ -258,7 +259,7 @@ build_section_addr_info_from_bfd (bfd *abfd)
       {
 	sap->other[i].addr = bfd_get_section_vma (abfd, sec);
 	sap->other[i].name = xstrdup (bfd_get_section_name (abfd, sec));
-	sap->other[i].sectindex = sec->index;
+	sap->other[i].sectindex = gdb_bfd_section_index (abfd, sec);
 	i++;
       }
   return sap;
@@ -383,7 +384,7 @@ place_section (bfd *abfd, asection *sect, void *obj)
     return;
 
   /* If the user specified an offset, honor it.  */
-  if (offsets[sect->index] != 0)
+  if (offsets[gdb_bfd_section_index (abfd, sect)] != 0)
     return;
 
   /* Otherwise, let's try to find a place for the section.  */
@@ -427,7 +428,7 @@ place_section (bfd *abfd, asection *sect, void *obj)
     }
   while (!done);
 
-  offsets[sect->index] = start_addr;
+  offsets[gdb_bfd_section_index (abfd, sect)] = start_addr;
   arg->lowest = start_addr + bfd_get_section_size (sect);
 }
 
diff --git a/gdb/symtab.c b/gdb/symtab.c
index df9caef..0f1cd68 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -1060,7 +1060,7 @@ fixup_section (struct general_symbol_info *ginfo,
 
       ALL_OBJFILE_OSECTIONS (objfile, s)
 	{
-	  int idx = s->the_bfd_section->index;
+	  int idx = s - objfile->sections;
 	  CORE_ADDR offset = ANOFFSET (objfile->section_offsets, idx);
 
 	  if (obj_section_addr (s) - offset <= addr
diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c
index cb0cb72..45ed799 100644
--- a/gdb/xcoffread.c
+++ b/gdb/xcoffread.c
@@ -278,7 +278,7 @@ find_targ_sec (bfd *abfd, asection *sect, void *obj)
       else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
 	*args->resultp = SECT_OFF_DATA (objfile);
       else
-	*args->resultp = sect->index;
+	*args->resultp = gdb_bfd_section_index (abfd, sect);
       *args->bfd_sect = sect;
     }
 }
-- 
1.7.7.6


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]