* configure.in: Added gnuserv everywhere sn appears.
[deliverable/binutils-gdb.git] / gdb / exec.c
index b0565bc5b5c6ae8538993115417b75ce2bba5c17..fa64163ed781c65429a8d4f07a11165ae456d8af 100644 (file)
@@ -15,7 +15,7 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
-Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include "defs.h"
 #include "frame.h"
@@ -32,12 +32,12 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 #include <sys/param.h>
 #include <fcntl.h>
-#include <string.h>
+#include "gdb_string.h"
 
 #include "gdbcore.h"
 
 #include <ctype.h>
-#include <sys/stat.h>
+#include "gdb_stat.h"
 #ifndef O_BINARY
 #define O_BINARY 0
 #endif
@@ -58,6 +58,10 @@ static void set_section_command PARAMS ((char *, int));
 
 static void exec_files_info PARAMS ((struct target_ops *));
 
+static void bfdsec_to_vmap PARAMS ((bfd *, sec_ptr, PTR));
+
+static int ignore PARAMS ((CORE_ADDR, char *));
+
 extern int info_verbose;
 
 /* The Binary File Descriptor handle for the executable file.  */
@@ -103,22 +107,29 @@ exec_close (quitting)
          need_symtab_cleanup = 1;
        }
       else if (vp->bfd != exec_bfd)
-       bfd_close (vp->bfd);
+       /* FIXME-leak: We should be freeing vp->name too, I think.  */
+       if (!bfd_close (vp->bfd))
+         warning ("cannot close \"%s\": %s",
+                  vp->name, bfd_errmsg (bfd_get_error ()));
 
       /* FIXME: This routine is #if 0'd in symfile.c.  What should we
         be doing here?  Should we just free everything in
-        vp->objfile->symtabs?  Should free_objfile do that?  */
+        vp->objfile->symtabs?  Should free_objfile do that?
+        FIXME-as-well: free_objfile already free'd vp->name, so it isn't
+        valid here.  */
       free_named_symtabs (vp->name);
       free (vp);
     }
-  
+
   vmap = NULL;
 
   if (exec_bfd)
     {
       char *name = bfd_get_filename (exec_bfd);
 
-      bfd_close (exec_bfd);
+      if (!bfd_close (exec_bfd))
+       warning ("cannot close \"%s\": %s",
+                name, bfd_errmsg (bfd_get_error ()));
       free (name);
       exec_bfd = NULL;
     }
@@ -176,13 +187,30 @@ exec_file_command (args, from_tty)
       scratch_chan = openp (getenv ("PATH"), 1, filename, 
                            write_files? O_RDWR|O_BINARY: O_RDONLY|O_BINARY, 0,
                            &scratch_pathname);
+#if defined(__GO32__) || defined(__WIN32__)
+      if (scratch_chan < 0)
+      {
+       char *exename = alloca (strlen (filename) + 5);
+       strcat (strcpy (exename, filename), ".exe");
+       scratch_chan = openp (getenv ("PATH"), 1, exename, write_files ?
+               O_RDWR|O_BINARY : O_RDONLY|O_BINARY, 0, &scratch_pathname);
+      }
+#endif
       if (scratch_chan < 0)
        perror_with_name (filename);
-
       exec_bfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan);
+
       if (!exec_bfd)
        error ("\"%s\": could not open as an executable file: %s",
               scratch_pathname, bfd_errmsg (bfd_get_error ()));
+
+      /* At this point, scratch_pathname and exec_bfd->name both point to the
+        same malloc'd string.  However exec_close() will attempt to free it
+        via the exec_bfd->name pointer, so we need to make another copy and
+        leave exec_bfd as the new owner of the original copy. */
+      scratch_pathname = strdup (scratch_pathname);
+      make_cleanup (free, scratch_pathname);
+      
       if (!bfd_check_format (exec_bfd, bfd_object))
        {
          /* Make sure to close exec_bfd, or else "run" might try to use
@@ -229,6 +257,8 @@ exec_file_command (args, from_tty)
        /* Set text_start to the lowest address of the start of any
           readonly code section and set text_end to the highest
           address of the end of any readonly code section.  */
+       /* FIXME: The comment above does not match the code.  The code
+          checks for sections with are either code *or* readonly.  */
 
        text_start = ~(CORE_ADDR)0;
        text_end = (CORE_ADDR)0;
@@ -366,7 +396,7 @@ map_vmap (abfd, arch)
   struct vmap_and_bfd vmap_bfd;
   struct vmap *vp, **vpp;
 
-  vp = (PTR) xmalloc (sizeof (*vp));
+  vp = (struct vmap *) xmalloc (sizeof (*vp));
   memset ((char *) vp, '\0', sizeof (*vp));
   vp->nxt = 0;
   vp->bfd = abfd;
@@ -579,26 +609,47 @@ ignore (addr, contents)
 }
 
 struct target_ops exec_ops = {
-       "exec", "Local exec file",
-       "Use an executable file as a target.\n\
-Specify the filename of the executable file.",
-       exec_file_command, exec_close, /* open, close */
-       find_default_attach, 0, 0, 0, /* attach, detach, resume, wait, */
-       0, 0, /* fetch_registers, store_registers, */
-       0, /* prepare_to_store, */
-       xfer_memory, exec_files_info,
-       ignore, ignore, /* insert_breakpoint, remove_breakpoint, */
-       0, 0, 0, 0, 0, /* terminal stuff */
-       0, 0, /* kill, load */
-       0, /* lookup sym */
-       find_default_create_inferior,
-       0, /* mourn_inferior */
-       0, /* can_run */
-       0, /* notice_signals */
-       file_stratum, 0, /* next */
-       0, 1, 0, 0, 0,  /* all mem, mem, stack, regs, exec */
-       0, 0,                   /* section pointers */
-       OPS_MAGIC,              /* Always the last thing */
+  "exec",                      /* to_shortname */
+  "Local exec file",           /* to_longname */
+  "Use an executable file as a target.\n\
+Specify the filename of the executable file.", /* to_doc */
+  exec_file_command,           /* to_open */
+  exec_close,                  /* to_close */
+  find_default_attach,         /* to_attach */
+  0,                           /* to_detach */
+  0,                           /* to_resume */
+  0,                           /* to_wait */
+  0,                           /* to_fetch_registers */
+  0,                           /* to_store_registers */
+  0,                           /* to_prepare_to_store */
+  xfer_memory,                 /* to_xfer_memory */
+  exec_files_info,             /* to_files_info */
+  ignore,                      /* to_insert_breakpoint */
+  ignore,                      /* to_remove_breakpoint */
+  0,                           /* to_terminal_init */
+  0,                           /* to_terminal_inferior */
+  0,                           /* to_terminal_ours_for_output */
+  0,                           /* to_terminal_ours */
+  0,                           /* to_terminal_info */
+  0,                           /* to_kill */
+  0,                           /* to_load */
+  0,                           /* to_lookup_symbol */
+  find_default_create_inferior,        /* to_create_inferior */
+  0,                           /* to_mourn_inferior */
+  0,                           /* to_can_run */
+  0,                           /* to_notice_signals */
+  0,                           /* to_thread_alive */
+  0,                           /* to_stop */
+  file_stratum,                        /* to_stratum */
+  0,                           /* to_next */
+  0,                           /* to_has_all_memory */
+  1,                           /* to_has_memory */
+  0,                           /* to_has_stack */
+  0,                           /* to_has_registers */
+  0,                           /* to_has_execution */
+  0,                           /* to_sections */
+  0,                           /* to_sections_end */
+  OPS_MAGIC,                   /* to_magic */
 };
 
 void
This page took 0.02915 seconds and 4 git commands to generate.