Patchwork [gmane.comp.gnu.binutils,2/2] Introduce bfd_set_filename

login
register
mail settings
Submitter Tom Tromey
Date Sept. 11, 2019, 5:12 p.m.
Message ID <87v9ty4v0h.fsf@tromey.com>
Download mbox | patch
Permalink /patch/34499/
State New
Headers show

Comments

Tom Tromey - Sept. 11, 2019, 5:12 p.m.
I sent this to binutils@ yesterday, and just FYI, I am going to check it in.

Tom
This introduces a new bfd_set_filename function, which is then used in
various spots in gdb.  This allows for the removal of some casts.

2019-09-10  Tom Tromey  <tom@tromey.com>

	* opncls.c (bfd_set_filename): New function.
	* bfd-in2.h: Regenerate.

gdb/ChangeLog
2019-09-10  Tom Tromey  <tom@tromey.com>

	* symfile-mem.c (symbol_file_add_from_memory): Use
	bfd_set_filename.
	* solib-darwin.c (darwin_bfd_open): Use bfd_set_filename.
	* solib-aix.c (solib_aix_bfd_open): Use bfd_set_filename.
---
 bfd/ChangeLog      |  5 +++++
 bfd/bfd-in2.h      |  2 ++
 bfd/opncls.c       | 20 ++++++++++++++++++++
 gdb/ChangeLog      |  7 +++++++
 gdb/solib-aix.c    |  8 ++++----
 gdb/solib-darwin.c |  3 +--
 gdb/symfile-mem.c  |  6 ++----
 7 files changed, 41 insertions(+), 10 deletions(-)

Patch

diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index 7b1cfbcf1a2..f98f7b0e73b 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -1193,6 +1193,8 @@  bfd_boolean bfd_fill_in_gnu_debuglink_section
 
 char *bfd_follow_build_id_debuglink (bfd *abfd, const char *dir);
 
+void bfd_set_filename (bfd *abfd, char *filename);
+
 /* Extracted from libbfd.c.  */
 
 /* Byte swapping macros for user section data.  */
diff --git a/bfd/opncls.c b/bfd/opncls.c
index 07f89b9a4c1..d14dfe93206 100644
--- a/bfd/opncls.c
+++ b/bfd/opncls.c
@@ -2089,3 +2089,23 @@  bfd_follow_build_id_debuglink (bfd *abfd, const char *dir)
 				   get_build_id_name,
 				   check_build_id_file, &build_id);
 }
+
+/*
+FUNCTION
+	bfd_set_filename
+
+SYNOPSIS
+	void bfd_set_filename (bfd *abfd, char *filename);
+
+DESCRIPTION
+	Set the filename of @var{abfd}.  The old filename, if any, is freed.
+	@var{filename} must be allocated using @code{xmalloc}.  After
+	this call, it is owned @var{abfd}.
+*/
+
+void
+bfd_set_filename (bfd *abfd, char *filename)
+{
+  free ((char *) abfd->filename);
+  abfd->filename = filename;
+}
diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index 2b5444293e2..0c8a18267c9 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -643,10 +643,10 @@  solib_aix_bfd_open (const char *pathname)
      along with appended parenthesized member name in order to allow commands
      listing all shared libraries to display.  Otherwise, we would only be
      displaying the name of the archive member object.  */
-  xfree ((char *) bfd_get_filename (object_bfd.get ()));
-  object_bfd->filename = xstrprintf ("%s%s",
-				     bfd_get_filename (archive_bfd.get ()),
-				     sep);
+  bfd_set_filename (object_bfd.get (),
+		    xstrprintf ("%s%s",
+				bfd_get_filename (archive_bfd.get ()),
+				sep));
 
   return object_bfd;
 }
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index 3dd30d2aa3c..778c6d9f617 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -670,8 +670,7 @@  darwin_bfd_open (const char *pathname)
   /* The current filename for fat-binary BFDs is a name generated
      by BFD, usually a string containing the name of the architecture.
      Reset its value to the actual filename.  */
-  xfree ((char *) bfd_get_filename (res.get ()));
-  res->filename = xstrdup (pathname);
+  bfd_set_filename (res.get (), xstrdup (pathname));
 
   return res;
 }
diff --git a/gdb/symfile-mem.c b/gdb/symfile-mem.c
index 6d1deae1e0e..05542c13915 100644
--- a/gdb/symfile-mem.c
+++ b/gdb/symfile-mem.c
@@ -101,11 +101,9 @@  symbol_file_add_from_memory (struct bfd *templ, CORE_ADDR addr,
   /* Manage the new reference for the duration of this function.  */
   gdb_bfd_ref_ptr nbfd_holder = gdb_bfd_ref_ptr::new_reference (nbfd);
 
-  xfree ((char *) bfd_get_filename (nbfd));
   if (name == NULL)
-    nbfd->filename = xstrdup ("shared object read from target memory");
-  else
-    nbfd->filename = name;
+    name = xstrdup ("shared object read from target memory");
+  bfd_set_filename (nbfd, name);
 
   if (!bfd_check_format (nbfd, bfd_object))
     error (_("Got object file from memory but can't read symbols: %s."),