]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gdb/symfile.c
* gdbarch.sh (target_gdbarch): Remove macro.
[thirdparty/binutils-gdb.git] / gdb / symfile.c
index 416d35d13c34efdda5e02e5dd90cd78bb82bd370..55af541e93c077af4b7432ea032b4614a06b7dfd 100644 (file)
@@ -55,6 +55,7 @@
 #include "solib.h"
 #include "remote.h"
 #include "stack.h"
+#include "gdb_bfd.h"
 
 #include <sys/types.h>
 #include <fcntl.h>
@@ -1036,7 +1037,7 @@ new_symfile_objfile (struct objfile *objfile, int add_flags)
    loaded file.
 
    ABFD is a BFD already open on the file, as from symfile_bfd_open.
-   This BFD will be closed on error, and is always consumed by this function.
+   A new reference is acquired by this function.
 
    ADD_FLAGS encodes verbosity, whether this is main symbol file or
    extra, such as dynamically loaded code, and what to do with breakpoins.
@@ -1060,7 +1061,6 @@ symbol_file_add_with_addrs_or_offsets (bfd *abfd,
                                        int flags, struct objfile *parent)
 {
   struct objfile *objfile;
-  struct cleanup *my_cleanups;
   const char *name = bfd_get_filename (abfd);
   const int from_tty = add_flags & SYMFILE_VERBOSE;
   const int mainline = add_flags & SYMFILE_MAINLINE;
@@ -1074,8 +1074,6 @@ symbol_file_add_with_addrs_or_offsets (bfd *abfd,
       add_flags &= ~SYMFILE_NO_READ;
     }
 
-  my_cleanups = make_cleanup_bfd_close (abfd);
-
   /* Give user a chance to burp if we'd be
      interactively wiping out any existing symbols.  */
 
@@ -1086,7 +1084,6 @@ symbol_file_add_with_addrs_or_offsets (bfd *abfd,
     error (_("Not confirmed."));
 
   objfile = allocate_objfile (abfd, flags | (mainline ? OBJF_MAINLINE : 0));
-  discard_cleanups (my_cleanups);
 
   if (parent)
     add_separate_debug_objfile (objfile, parent);
@@ -1207,8 +1204,13 @@ struct objfile *
 symbol_file_add (char *name, int add_flags, struct section_addr_info *addrs,
                 int flags)
 {
-  return symbol_file_add_from_bfd (symfile_bfd_open (name), add_flags, addrs,
-                                   flags, NULL);
+  bfd *bfd = symfile_bfd_open (name);
+  struct cleanup *cleanup = make_cleanup_bfd_unref (bfd);
+  struct objfile *objf;
+
+  objf = symbol_file_add_from_bfd (bfd, add_flags, addrs, flags, NULL);
+  do_cleanups (cleanup);
+  return objf;
 }
 
 
@@ -1350,7 +1352,7 @@ separate_debug_file_exists (const char *name, unsigned long crc,
   if (filename_cmp (name, parent_objfile->name) == 0)
     return 0;
 
-  abfd = bfd_open_maybe_remote (name);
+  abfd = gdb_bfd_open_maybe_remote (name);
 
   if (!abfd)
     return 0;
@@ -1372,7 +1374,7 @@ separate_debug_file_exists (const char *name, unsigned long crc,
       if (abfd_stat.st_dev == parent_stat.st_dev
          && abfd_stat.st_ino == parent_stat.st_ino)
        {
-         bfd_close (abfd);
+         gdb_bfd_unref (abfd);
          return 0;
        }
       verified_as_different = 1;
@@ -1382,7 +1384,7 @@ separate_debug_file_exists (const char *name, unsigned long crc,
 
   file_crc_p = get_file_crc (abfd, &file_crc);
 
-  bfd_close (abfd);
+  gdb_bfd_unref (abfd);
 
   if (!file_crc_p)
     return 0;
@@ -1553,8 +1555,9 @@ find_separate_debug_file_by_debuglink (struct objfile *objfile)
       return NULL;
     }
 
+  cleanups = make_cleanup (xfree, debuglink);
   dir = xstrdup (objfile->name);
-  cleanups = make_cleanup (xfree, dir);
+  make_cleanup (xfree, dir);
   terminate_after_last_dir_separator (dir);
   canon_dir = lrealpath (dir);
 
@@ -1689,15 +1692,20 @@ set_initial_language (void)
 }
 
 /* If NAME is a remote name open the file using remote protocol, otherwise
-   open it normally.  */
+   open it normally.  Returns a new reference to the BFD.  On error,
+   returns NULL with the BFD error set.  */
 
 bfd *
-bfd_open_maybe_remote (const char *name)
+gdb_bfd_open_maybe_remote (const char *name)
 {
+  bfd *result;
+
   if (remote_filename_p (name))
-    return remote_bfd_open (name, gnutarget);
+    result = remote_bfd_open (name, gnutarget);
   else
-    return bfd_openr (name, gnutarget);
+    result = gdb_bfd_open (name, gnutarget, -1);
+
+  return result;
 }
 
 
@@ -1715,19 +1723,14 @@ symfile_bfd_open (char *name)
 
   if (remote_filename_p (name))
     {
-      name = xstrdup (name);
       sym_bfd = remote_bfd_open (name, gnutarget);
       if (!sym_bfd)
-       {
-         make_cleanup (xfree, name);
-         error (_("`%s': can't open to read symbols: %s."), name,
-                bfd_errmsg (bfd_get_error ()));
-       }
+       error (_("`%s': can't open to read symbols: %s."), name,
+              bfd_errmsg (bfd_get_error ()));
 
       if (!bfd_check_format (sym_bfd, bfd_object))
        {
-         bfd_close (sym_bfd);
-         make_cleanup (xfree, name);
+         make_cleanup_bfd_unref (sym_bfd);
          error (_("`%s': can't read symbols: %s."), name,
                 bfd_errmsg (bfd_get_error ()));
        }
@@ -1756,15 +1759,13 @@ symfile_bfd_open (char *name)
       perror_with_name (name);
     }
 
-  /* Free 1st new malloc'd copy, but keep the 2nd malloc'd copy in
-     bfd.  It'll be freed in free_objfile().  */
   xfree (name);
   name = absolute_name;
+  make_cleanup (xfree, name);
 
-  sym_bfd = bfd_fopen (name, gnutarget, FOPEN_RB, desc);
+  sym_bfd = gdb_bfd_open (name, gnutarget, desc);
   if (!sym_bfd)
     {
-      close (desc);
       make_cleanup (xfree, name);
       error (_("`%s': can't open to read symbols: %s."), name,
             bfd_errmsg (bfd_get_error ()));
@@ -1773,18 +1774,11 @@ symfile_bfd_open (char *name)
 
   if (!bfd_check_format (sym_bfd, bfd_object))
     {
-      /* FIXME: should be checking for errors from bfd_close (for one
-         thing, on error it does not free all the storage associated
-         with the bfd).  */
-      bfd_close (sym_bfd);     /* This also closes desc.  */
-      make_cleanup (xfree, name);
+      make_cleanup_bfd_unref (sym_bfd);
       error (_("`%s': can't read symbols: %s."), name,
             bfd_errmsg (bfd_get_error ()));
     }
 
-  /* bfd_usrdata exists for applications and libbfd must not touch it.  */
-  gdb_assert (bfd_usrdata (sym_bfd) == NULL);
-
   return sym_bfd;
 }
 
@@ -1963,7 +1957,7 @@ load_progress (ULONGEST bytes, void *untyped_arg)
         this section.  */
       ui_out_message (current_uiout, 0, "Loading section %s, size %s lma %s\n",
                      args->section_name, hex_string (args->section_size),
-                     paddress (target_gdbarch, args->lma));
+                     paddress (target_gdbarch (), args->lma));
       return;
     }
 
@@ -1981,10 +1975,10 @@ load_progress (ULONGEST bytes, void *untyped_arg)
 
       if (target_read_memory (args->lma, check, bytes) != 0)
        error (_("Download verify read failed at %s"),
-              paddress (target_gdbarch, args->lma));
+              paddress (target_gdbarch (), args->lma));
       if (memcmp (args->buffer, check, bytes) != 0)
        error (_("Download verify compare failed at %s"),
-              paddress (target_gdbarch, args->lma));
+              paddress (target_gdbarch (), args->lma));
       do_cleanups (verify_cleanups);
     }
   totals->data_count += bytes;
@@ -1992,7 +1986,7 @@ load_progress (ULONGEST bytes, void *untyped_arg)
   args->buffer += bytes;
   totals->write_count += 1;
   args->section_sent += bytes;
-  if (quit_flag
+  if (check_quit_flag ()
       || (deprecated_ui_load_progress_hook != NULL
          && deprecated_ui_load_progress_hook (args->section_name,
                                               args->section_sent)))
@@ -2109,17 +2103,14 @@ generic_load (char *args, int from_tty)
     }
 
   /* Open the file for loading.  */
-  loadfile_bfd = bfd_openr (filename, gnutarget);
+  loadfile_bfd = gdb_bfd_open (filename, gnutarget, -1);
   if (loadfile_bfd == NULL)
     {
       perror_with_name (filename);
       return;
     }
 
-  /* FIXME: should be checking for errors from bfd_close (for one thing,
-     on error it does not free all the storage associated with the
-     bfd).  */
-  make_cleanup_bfd_close (loadfile_bfd);
+  make_cleanup_bfd_unref (loadfile_bfd);
 
   if (!bfd_check_format (loadfile_bfd, bfd_object))
     {
@@ -2142,7 +2133,7 @@ generic_load (char *args, int from_tty)
 
   entry = bfd_get_start_address (loadfile_bfd);
   ui_out_text (uiout, "Start address ");
-  ui_out_field_fmt (uiout, "address", "%s", paddress (target_gdbarch, entry));
+  ui_out_field_fmt (uiout, "address", "%s", paddress (target_gdbarch (), entry));
   ui_out_text (uiout, ", load size ");
   ui_out_field_fmt (uiout, "load-size", "%lu", total_progress.data_count);
   ui_out_text (uiout, "\n");
@@ -2515,18 +2506,26 @@ reread_symbols (void)
 
          clear_objfile_data (objfile);
 
-         /* Clean up any state BFD has sitting around.  We don't need
-            to close the descriptor but BFD lacks a way of closing the
-            BFD without closing the descriptor.  */
-         obfd_filename = bfd_get_filename (objfile->obfd);
-         if (!bfd_close (objfile->obfd))
-           error (_("Can't close BFD for %s: %s"), objfile->name,
-                  bfd_errmsg (bfd_get_error ()));
-         objfile->obfd = bfd_open_maybe_remote (obfd_filename);
-         if (objfile->obfd == NULL)
-           error (_("Can't open %s to read symbols."), objfile->name);
-         else
-           objfile->obfd = gdb_bfd_ref (objfile->obfd);
+         /* Clean up any state BFD has sitting around.  */
+         {
+           struct bfd *obfd = objfile->obfd;
+
+           obfd_filename = bfd_get_filename (objfile->obfd);
+           /* 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)
+             {
+               /* We have to make a cleanup and error here, rather
+                  than erroring later, because once we unref OBFD,
+                  OBFD_FILENAME will be freed.  */
+               make_cleanup_bfd_unref (obfd);
+               error (_("Can't open %s to read symbols."), obfd_filename);
+             }
+           gdb_bfd_unref (obfd);
+         }
+
+         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,
@@ -2554,10 +2553,6 @@ reread_symbols (void)
          /* Free the obstacks for non-reusable objfiles.  */
          psymbol_bcache_free (objfile->psymbol_cache);
          objfile->psymbol_cache = psymbol_bcache_init ();
-         bcache_xfree (objfile->macro_cache);
-         objfile->macro_cache = bcache_xmalloc (NULL, NULL);
-         bcache_xfree (objfile->filename_cache);
-         objfile->filename_cache = bcache_xmalloc (NULL,NULL);
          if (objfile->demangled_names_hash != NULL)
            {
              htab_delete (objfile->demangled_names_hash);
@@ -2578,6 +2573,8 @@ reread_symbols (void)
          memset (&objfile->msymbol_demangled_hash, 0,
                  sizeof (objfile->msymbol_demangled_hash));
 
+         set_objfile_per_bfd (objfile);
+
          /* obstack_init also initializes the obstack so it is
             empty.  We could use obstack_specify_allocation but
             gdb_obstack.h specifies the alloc/dealloc functions.  */
@@ -2862,7 +2859,7 @@ allocate_symtab (const char *filename, struct objfile *objfile)
     obstack_alloc (&objfile->objfile_obstack, sizeof (struct symtab));
   memset (symtab, 0, sizeof (*symtab));
   symtab->filename = (char *) bcache (filename, strlen (filename) + 1,
-                                     objfile->filename_cache);
+                                     objfile->per_bfd->filename_cache);
   symtab->fullname = NULL;
   symtab->language = deduce_language_from_filename (filename);
   symtab->debugformat = "unknown";
@@ -2873,6 +2870,26 @@ allocate_symtab (const char *filename, struct objfile *objfile)
   symtab->next = objfile->symtabs;
   objfile->symtabs = symtab;
 
+  if (symtab_create_debug)
+    {
+      /* Be a bit clever with debugging messages, and don't print objfile
+        every time, only when it changes.  */
+      static char *last_objfile_name = NULL;
+
+      if (last_objfile_name == NULL
+         || strcmp (last_objfile_name, objfile->name) != 0)
+       {
+         xfree (last_objfile_name);
+         last_objfile_name = xstrdup (objfile->name);
+         fprintf_unfiltered (gdb_stdlog,
+                             "Creating one or more symtabs for objfile %s ...\n",
+                             last_objfile_name);
+       }
+      fprintf_unfiltered (gdb_stdlog,
+                         "Created symtab %s for module %s.\n",
+                         host_address_to_string (symtab), filename);
+    }
+
   return (symtab);
 }
 \f
@@ -3379,7 +3396,7 @@ overlay_load_command (char *args, int from_tty)
    A place-holder for a mis-typed command.  */
 
 /* Command list chain containing all defined "overlay" subcommands.  */
-struct cmd_list_element *overlaylist;
+static struct cmd_list_element *overlaylist;
 
 static void
 overlay_command (char *args, int from_tty)