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]

[PATCH 9/9] do not malloc objfile->name


There's no particularly good reason to require the use of malloc for
objfile->name, particularly because we have many other storage options
that don't require extra manual management.

This patch changes gdb to either allocate the objfile name statically
(in, e.g., the jit.c case) or to simply point to the BFD's file name.

	* jit.c (jit_object_close_impl): Don't malloc the objfile
	name.
	* objfiles.c (allocate_objfile): Don't malloc the objfile
	name.
	(free_objfile): Don't free the objfile name.
	* objfiles.h (struct objfile) <name>: Update comment.
	* symfile.c (reread_symbols): Fix reference counting.  Don't
	malloc objfile name.
---
 gdb/jit.c      |    3 +--
 gdb/objfiles.c |    5 ++---
 gdb/objfiles.h |    5 +++--
 gdb/symfile.c  |    8 ++++++--
 4 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/gdb/jit.c b/gdb/jit.c
index aa9d5e5..3a15d37 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -761,8 +761,7 @@ jit_object_close_impl (struct gdb_symbol_callbacks *cb,
 
   terminate_minimal_symbol_table (objfile);
 
-  xfree (objfile->name);
-  objfile->name = xstrdup ("<< JIT compiled code >>");
+  objfile->name = "<< JIT compiled code >>";
 
   j = NULL;
   for (i = obj->symtabs; i; i = j)
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index d279af0..dcaba15 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -258,7 +258,7 @@ allocate_objfile (bfd *abfd, int flags)
       /* Look up the gdbarch associated with the BFD.  */
       objfile->gdbarch = gdbarch_from_bfd (abfd);
 
-      objfile->name = xstrdup (bfd_get_filename (abfd));
+      objfile->name = bfd_get_filename (abfd);
       objfile->mtime = bfd_get_mtime (abfd);
 
       /* Build section table.  */
@@ -266,7 +266,7 @@ allocate_objfile (bfd *abfd, int flags)
     }
   else
     {
-      objfile->name = xstrdup ("<<anonymous objfile>>");
+      objfile->name = "<<anonymous objfile>>";
     }
 
   objfile->per_bfd = get_objfile_bfd_data (objfile, abfd);
@@ -657,7 +657,6 @@ free_objfile (struct objfile *objfile)
 
   /* The last thing we do is free the objfile struct itself.  */
 
-  xfree (objfile->name);
   if (objfile->global_psymbols.list)
     xfree (objfile->global_psymbols.list);
   if (objfile->static_psymbols.list)
diff --git a/gdb/objfiles.h b/gdb/objfiles.h
index f4397d0..8e2f5c0 100644
--- a/gdb/objfiles.h
+++ b/gdb/objfiles.h
@@ -202,8 +202,9 @@ struct objfile
 
     struct objfile *next;
 
-    /* The object file's name, tilde-expanded and absolute.  Malloc'd; free it
-       if you free this struct.  This pointer is never NULL.  */
+    /* The object file's name, tilde-expanded and absolute.  This
+       pointer is never NULL.  This does not have to be freed; it is
+       guaranteed to have a lifetime at least as long as the objfile.  */
 
     char *name;
 
diff --git a/gdb/symfile.c b/gdb/symfile.c
index fdf604d..aa94917 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -2517,11 +2517,15 @@ reread_symbols (void)
 	    /* Open the new BFD before freeing the old one, so that
 	       the filename remains live.  */
 	    objfile->obfd = gdb_bfd_open_maybe_remote (obfd_filename);
+	    if (objfile->obfd == NULL)
+	      {
+		make_cleanup_bfd_unref (obfd);
+		error (_("Can't open %s to read symbols."), objfile->name);
+	      }
 	    gdb_bfd_unref (obfd);
 	  }
 
-	  if (objfile->obfd == NULL)
-	    error (_("Can't open %s to read symbols."), objfile->name);
+	  objfile->name = bfd_get_filename (objfile->obfd);
 	  /* bfd_openr sets cacheable to true, which is what we want.  */
 	  if (!bfd_check_format (objfile->obfd, bfd_object))
 	    error (_("Can't read symbols from %s: %s."), objfile->name,
-- 
1.7.7.6


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