Removed superflous code.
[deliverable/binutils-gdb.git] / gdb / exec.c
index af5ef8354ed423eb663a1f6526d9e68fbedcdd24..c39e118d302d8789504f025dc2dd6a13c64abad4 100644 (file)
@@ -1,5 +1,6 @@
 /* Work with executable files, for GDB. 
-   Copyright 1988, 1989, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
+   Copyright 1988, 1989, 1991, 1992, 1993, 1994, 1997
+             Free Software Foundation, Inc.
 
 This file is part of GDB.
 
@@ -15,7 +16,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"
@@ -30,14 +31,13 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include <sys/types.h>
 #endif
 
-#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
@@ -46,6 +46,8 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 struct vmap *map_vmap PARAMS ((bfd *, bfd *));
 
+void (*file_changed_hook) PARAMS ((char *));
+
 /* Prototypes for local functions */
 
 static void add_to_section_table PARAMS ((bfd *, sec_ptr, PTR));
@@ -58,6 +60,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.  */
@@ -183,13 +189,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
@@ -256,6 +279,7 @@ exec_file_command (args, from_tty)
       validate_files ();
 
       set_endian_from_file (exec_bfd);
+      set_architecture_from_file (exec_bfd);
 
       push_target (&exec_ops);
 
@@ -264,7 +288,7 @@ exec_file_command (args, from_tty)
        (*exec_file_display_hook) (filename);
     }
   else if (from_tty)
-    printf_unfiltered ("No exec file now.\n");
+    printf_unfiltered ("No executable file now.\n");
 }
 
 /* Set both the exec file and the symbol file, in one command.  
@@ -280,6 +304,8 @@ file_command (arg, from_tty)
      the exec file, but that's rough.  */
   exec_file_command (arg, from_tty);
   symbol_file_command (arg, from_tty);
+  if (file_changed_hook)
+    file_changed_hook (arg);
 }
 
 \f
@@ -346,20 +372,16 @@ bfdsec_to_vmap(abfd, sect, arg3)
 
   if (STREQ (bfd_section_name (abfd, sect), ".text"))
     {
-      vp->tstart = 0;
+      vp->tstart = bfd_section_vma (abfd, sect);
       vp->tend = vp->tstart + bfd_section_size (abfd, sect);
-
-      /* When it comes to this adjustment value, in contrast to our previous
-        belief shared objects should behave the same as the main load segment.
-        This is the offset from the beginning of text section to the first
-        real instruction. */
-
-      vp->tadj = sect->filepos - bfd_section_vma (abfd, sect);
+      vp->tvma = bfd_section_vma (abfd, sect);
+      vp->toffs = sect->filepos;
     }
   else if (STREQ (bfd_section_name (abfd, sect), ".data"))
     {
-      vp->dstart = 0;
+      vp->dstart = bfd_section_vma (abfd, sect);
       vp->dend = vp->dstart + bfd_section_size (abfd, sect);
+      vp->dvma = bfd_section_vma (abfd, sect);
     }
   /* Silently ignore other types of sections. (FIXME?)  */
 }
@@ -424,25 +446,79 @@ xfer_memory (memaddr, myaddr, len, write, target)
   struct section_table *p;
   CORE_ADDR nextsectaddr, memend;
   boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
+  asection *section;
 
   if (len <= 0)
     abort();
 
+  if (overlay_debugging)
+    {
+      section = find_pc_overlay (memaddr);
+      if (pc_in_unmapped_range (memaddr, section))
+       memaddr = overlay_mapped_address (memaddr, section);
+    }
+
   memend = memaddr + len;
   xfer_fn = write ? bfd_set_section_contents : bfd_get_section_contents;
   nextsectaddr = memend;
 
+#if 0 /* Stu's implementation */
+/* If a section has been specified, try to use it.  Note that we cannot use the
+   specified section directly.  This is because it usually comes from the
+   symbol file, which may be different from the exec or core file.  Instead, we
+   have to lookup the specified section by name in the bfd associated with
+   to_sections.  */
+
+  if (target_memory_bfd_section)
+    {
+      asection *s;
+      bfd *abfd;
+      asection *target_section;
+      bfd *target_bfd;
+
+      s = target_memory_bfd_section;
+      abfd = s->owner;
+
+      target_bfd = target->to_sections->bfd;
+      target_section = bfd_get_section_by_name (target_bfd, bfd_section_name (abfd, s));
+
+      if (target_section)
+       {
+         bfd_vma sec_addr;
+         bfd_size_type sec_size;
+
+         sec_addr = bfd_section_vma (target_bfd, target_section);
+         sec_size = target_section->_raw_size;
+
+         /* Make sure the requested memory starts inside the section.  */
+
+         if (memaddr >= sec_addr
+             && memaddr < sec_addr + sec_size)
+           {
+             /* Cut back length in case request overflows the end of the section. */
+             len = min (len, sec_addr + sec_size - memaddr);
+
+             res = xfer_fn (target_bfd, target_section, myaddr, memaddr - sec_addr, len);
+
+             return res ? len : 0;
+           }
+       }
+    }
+#endif /* 0, Stu's implementation */
   for (p = target->to_sections; p < target->to_sections_end; p++)
     {
-      if (p->addr <= memaddr)
-       if (p->endaddr >= memend)
+      if (overlay_debugging && section && p->the_bfd_section &&
+         strcmp (section->name, p->the_bfd_section->name) != 0)
+       continue;       /* not the section we need */
+      if (memaddr >= p->addr)
+       if (memend <= p->endaddr)
          {
            /* Entire transfer is within this section.  */
            res = xfer_fn (p->bfd, p->the_bfd_section, myaddr,
                           memaddr - p->addr, len);
            return (res != 0) ? len : 0;
          }
-       else if (p->endaddr <= memaddr)
+       else if (memaddr >= p->endaddr)
          {
            /* This section ends before the transfer starts.  */
            continue;
@@ -455,8 +531,8 @@ xfer_memory (memaddr, myaddr, len, write, target)
                           memaddr - p->addr, len);
            return (res != 0) ? len : 0;
          }
-      else if (p->addr < nextsectaddr)
-       nextsectaddr = p->addr;
+      else
+       nextsectaddr = min (nextsectaddr, p->addr);
     }
 
   if (nextsectaddr >= memend)
@@ -588,26 +664,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.026898 seconds and 4 git commands to generate.