xref: /dflybsd-src/contrib/gdb-7/gdb/exec.c (revision ef5ccd6c41237a870dd7242b72b006d6bd42cd07)
15796c8dcSSimon Schubert /* Work with executable files, for GDB.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1988-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "frame.h"
225796c8dcSSimon Schubert #include "inferior.h"
235796c8dcSSimon Schubert #include "target.h"
245796c8dcSSimon Schubert #include "gdbcmd.h"
255796c8dcSSimon Schubert #include "language.h"
26c50c785cSJohn Marino #include "filenames.h"
275796c8dcSSimon Schubert #include "symfile.h"
285796c8dcSSimon Schubert #include "objfiles.h"
295796c8dcSSimon Schubert #include "completer.h"
305796c8dcSSimon Schubert #include "value.h"
315796c8dcSSimon Schubert #include "exec.h"
325796c8dcSSimon Schubert #include "observer.h"
335796c8dcSSimon Schubert #include "arch-utils.h"
34cf7f2e2dSJohn Marino #include "gdbthread.h"
35cf7f2e2dSJohn Marino #include "progspace.h"
36*ef5ccd6cSJohn Marino #include "gdb_bfd.h"
375796c8dcSSimon Schubert 
385796c8dcSSimon Schubert #include <fcntl.h>
395796c8dcSSimon Schubert #include "readline/readline.h"
405796c8dcSSimon Schubert #include "gdb_string.h"
415796c8dcSSimon Schubert 
425796c8dcSSimon Schubert #include "gdbcore.h"
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert #include <ctype.h>
455796c8dcSSimon Schubert #include "gdb_stat.h"
465796c8dcSSimon Schubert 
475796c8dcSSimon Schubert #include "xcoffsolib.h"
485796c8dcSSimon Schubert 
495796c8dcSSimon Schubert struct vmap *map_vmap (bfd *, bfd *);
505796c8dcSSimon Schubert 
515796c8dcSSimon Schubert void (*deprecated_file_changed_hook) (char *);
525796c8dcSSimon Schubert 
535796c8dcSSimon Schubert /* Prototypes for local functions */
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert static void file_command (char *, int);
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert static void set_section_command (char *, int);
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert static void exec_files_info (struct target_ops *);
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert static void init_exec_ops (void);
625796c8dcSSimon Schubert 
635796c8dcSSimon Schubert void _initialize_exec (void);
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert /* The target vector for executable files.  */
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert struct target_ops exec_ops;
685796c8dcSSimon Schubert 
69cf7f2e2dSJohn Marino /* True if the exec target is pushed on the stack.  */
70cf7f2e2dSJohn Marino static int using_exec_ops;
715796c8dcSSimon Schubert 
725796c8dcSSimon Schubert /* Whether to open exec and core files read-only or read-write.  */
735796c8dcSSimon Schubert 
745796c8dcSSimon Schubert int write_files = 0;
755796c8dcSSimon Schubert static void
765796c8dcSSimon Schubert show_write_files (struct ui_file *file, int from_tty,
775796c8dcSSimon Schubert 		  struct cmd_list_element *c, const char *value)
785796c8dcSSimon Schubert {
795796c8dcSSimon Schubert   fprintf_filtered (file, _("Writing into executable and core files is %s.\n"),
805796c8dcSSimon Schubert 		    value);
815796c8dcSSimon Schubert }
825796c8dcSSimon Schubert 
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert struct vmap *vmap;
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert static void
875796c8dcSSimon Schubert exec_open (char *args, int from_tty)
885796c8dcSSimon Schubert {
895796c8dcSSimon Schubert   target_preopen (from_tty);
905796c8dcSSimon Schubert   exec_file_attach (args, from_tty);
915796c8dcSSimon Schubert }
925796c8dcSSimon Schubert 
935796c8dcSSimon Schubert /* Close and clear exec_bfd.  If we end up with no target sections to
945796c8dcSSimon Schubert    read memory from, this unpushes the exec_ops target.  */
955796c8dcSSimon Schubert 
96cf7f2e2dSJohn Marino void
97cf7f2e2dSJohn Marino exec_close (void)
985796c8dcSSimon Schubert {
995796c8dcSSimon Schubert   if (exec_bfd)
1005796c8dcSSimon Schubert     {
1015796c8dcSSimon Schubert       bfd *abfd = exec_bfd;
1025796c8dcSSimon Schubert 
103*ef5ccd6cSJohn Marino       gdb_bfd_unref (abfd);
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert       /* Removing target sections may close the exec_ops target.
1065796c8dcSSimon Schubert 	 Clear exec_bfd before doing so to prevent recursion.  */
1075796c8dcSSimon Schubert       exec_bfd = NULL;
1085796c8dcSSimon Schubert       exec_bfd_mtime = 0;
1095796c8dcSSimon Schubert 
110*ef5ccd6cSJohn Marino       remove_target_sections (&exec_bfd, abfd);
111*ef5ccd6cSJohn Marino 
112*ef5ccd6cSJohn Marino       xfree (exec_filename);
113*ef5ccd6cSJohn Marino       exec_filename = NULL;
1145796c8dcSSimon Schubert     }
1155796c8dcSSimon Schubert }
1165796c8dcSSimon Schubert 
117cf7f2e2dSJohn Marino /* This is the target_close implementation.  Clears all target
118cf7f2e2dSJohn Marino    sections and closes all executable bfds from all program spaces.  */
119cf7f2e2dSJohn Marino 
1205796c8dcSSimon Schubert static void
121cf7f2e2dSJohn Marino exec_close_1 (int quitting)
1225796c8dcSSimon Schubert {
1235796c8dcSSimon Schubert   struct vmap *vp, *nxt;
1245796c8dcSSimon Schubert 
125cf7f2e2dSJohn Marino   using_exec_ops = 0;
126cf7f2e2dSJohn Marino 
1275796c8dcSSimon Schubert   for (nxt = vmap; nxt != NULL;)
1285796c8dcSSimon Schubert     {
1295796c8dcSSimon Schubert       vp = nxt;
1305796c8dcSSimon Schubert       nxt = vp->nxt;
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert       if (vp->objfile)
1335796c8dcSSimon Schubert 	free_objfile (vp->objfile);
134*ef5ccd6cSJohn Marino 
135*ef5ccd6cSJohn Marino       gdb_bfd_unref (vp->bfd);
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert       xfree (vp);
1385796c8dcSSimon Schubert     }
1395796c8dcSSimon Schubert 
1405796c8dcSSimon Schubert   vmap = NULL;
1415796c8dcSSimon Schubert 
142cf7f2e2dSJohn Marino   {
143cf7f2e2dSJohn Marino     struct program_space *ss;
144cf7f2e2dSJohn Marino     struct cleanup *old_chain;
145cf7f2e2dSJohn Marino 
146cf7f2e2dSJohn Marino     old_chain = save_current_program_space ();
147cf7f2e2dSJohn Marino     ALL_PSPACES (ss)
148cf7f2e2dSJohn Marino     {
149cf7f2e2dSJohn Marino       set_current_program_space (ss);
150cf7f2e2dSJohn Marino 
1515796c8dcSSimon Schubert       /* Delete all target sections.  */
1525796c8dcSSimon Schubert       resize_section_table
1535796c8dcSSimon Schubert 	(current_target_sections,
1545796c8dcSSimon Schubert 	 -resize_section_table (current_target_sections, 0));
1555796c8dcSSimon Schubert 
156cf7f2e2dSJohn Marino       exec_close ();
157cf7f2e2dSJohn Marino     }
158cf7f2e2dSJohn Marino 
159cf7f2e2dSJohn Marino     do_cleanups (old_chain);
160cf7f2e2dSJohn Marino   }
1615796c8dcSSimon Schubert }
1625796c8dcSSimon Schubert 
1635796c8dcSSimon Schubert void
1645796c8dcSSimon Schubert exec_file_clear (int from_tty)
1655796c8dcSSimon Schubert {
1665796c8dcSSimon Schubert   /* Remove exec file.  */
167cf7f2e2dSJohn Marino   exec_close ();
1685796c8dcSSimon Schubert 
1695796c8dcSSimon Schubert   if (from_tty)
1705796c8dcSSimon Schubert     printf_unfiltered (_("No executable file now.\n"));
1715796c8dcSSimon Schubert }
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert /* Set FILENAME as the new exec file.
1745796c8dcSSimon Schubert 
1755796c8dcSSimon Schubert    This function is intended to be behave essentially the same
1765796c8dcSSimon Schubert    as exec_file_command, except that the latter will detect when
1775796c8dcSSimon Schubert    a target is being debugged, and will ask the user whether it
1785796c8dcSSimon Schubert    should be shut down first.  (If the answer is "no", then the
1795796c8dcSSimon Schubert    new file is ignored.)
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert    This file is used by exec_file_command, to do the work of opening
1825796c8dcSSimon Schubert    and processing the exec file after any prompting has happened.
1835796c8dcSSimon Schubert 
1845796c8dcSSimon Schubert    And, it is used by child_attach, when the attach command was
1855796c8dcSSimon Schubert    given a pid but not a exec pathname, and the attach command could
1865796c8dcSSimon Schubert    figure out the pathname from the pid.  (In this case, we shouldn't
1875796c8dcSSimon Schubert    ask the user whether the current target should be shut down --
1885796c8dcSSimon Schubert    we're supplying the exec pathname late for good reason.)  */
1895796c8dcSSimon Schubert 
1905796c8dcSSimon Schubert void
1915796c8dcSSimon Schubert exec_file_attach (char *filename, int from_tty)
1925796c8dcSSimon Schubert {
1935796c8dcSSimon Schubert   /* Remove any previous exec file.  */
194cf7f2e2dSJohn Marino   exec_close ();
1955796c8dcSSimon Schubert 
1965796c8dcSSimon Schubert   /* Now open and digest the file the user requested, if any.  */
1975796c8dcSSimon Schubert 
1985796c8dcSSimon Schubert   if (!filename)
1995796c8dcSSimon Schubert     {
2005796c8dcSSimon Schubert       if (from_tty)
2015796c8dcSSimon Schubert         printf_unfiltered (_("No executable file now.\n"));
2025796c8dcSSimon Schubert 
2035796c8dcSSimon Schubert       set_gdbarch_from_file (NULL);
2045796c8dcSSimon Schubert     }
2055796c8dcSSimon Schubert   else
2065796c8dcSSimon Schubert     {
2075796c8dcSSimon Schubert       struct cleanup *cleanups;
208*ef5ccd6cSJohn Marino       char *scratch_pathname, *canonical_pathname;
2095796c8dcSSimon Schubert       int scratch_chan;
2105796c8dcSSimon Schubert       struct target_section *sections = NULL, *sections_end = NULL;
211cf7f2e2dSJohn Marino       char **matching;
2125796c8dcSSimon Schubert 
213*ef5ccd6cSJohn Marino       scratch_chan = openp (getenv ("PATH"),
214*ef5ccd6cSJohn Marino 			    OPF_TRY_CWD_FIRST | OPF_DISABLE_REALPATH, filename,
2155796c8dcSSimon Schubert 		   write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
2165796c8dcSSimon Schubert 			    &scratch_pathname);
2175796c8dcSSimon Schubert #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
2185796c8dcSSimon Schubert       if (scratch_chan < 0)
2195796c8dcSSimon Schubert 	{
2205796c8dcSSimon Schubert 	  char *exename = alloca (strlen (filename) + 5);
221cf7f2e2dSJohn Marino 
2225796c8dcSSimon Schubert 	  strcat (strcpy (exename, filename), ".exe");
223*ef5ccd6cSJohn Marino 	  scratch_chan = openp (getenv ("PATH"),
224*ef5ccd6cSJohn Marino 				OPF_TRY_CWD_FIRST | OPF_DISABLE_REALPATH,
225*ef5ccd6cSJohn Marino 				exename,
2265796c8dcSSimon Schubert 	     write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
2275796c8dcSSimon Schubert 	     &scratch_pathname);
2285796c8dcSSimon Schubert 	}
2295796c8dcSSimon Schubert #endif
2305796c8dcSSimon Schubert       if (scratch_chan < 0)
2315796c8dcSSimon Schubert 	perror_with_name (filename);
232*ef5ccd6cSJohn Marino 
233*ef5ccd6cSJohn Marino       cleanups = make_cleanup (xfree, scratch_pathname);
234*ef5ccd6cSJohn Marino 
235*ef5ccd6cSJohn Marino       /* gdb_bfd_open (and its variants) prefers canonicalized pathname for
236*ef5ccd6cSJohn Marino 	 better BFD caching.  */
237*ef5ccd6cSJohn Marino       canonical_pathname = gdb_realpath (scratch_pathname);
238*ef5ccd6cSJohn Marino       make_cleanup (xfree, canonical_pathname);
239*ef5ccd6cSJohn Marino 
240*ef5ccd6cSJohn Marino       if (write_files)
241*ef5ccd6cSJohn Marino 	exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget,
242*ef5ccd6cSJohn Marino 				  FOPEN_RUB, scratch_chan);
243*ef5ccd6cSJohn Marino       else
244*ef5ccd6cSJohn Marino 	exec_bfd = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
2455796c8dcSSimon Schubert 
2465796c8dcSSimon Schubert       if (!exec_bfd)
2475796c8dcSSimon Schubert 	{
2485796c8dcSSimon Schubert 	  error (_("\"%s\": could not open as an executable file: %s"),
2495796c8dcSSimon Schubert 		 scratch_pathname, bfd_errmsg (bfd_get_error ()));
2505796c8dcSSimon Schubert 	}
2515796c8dcSSimon Schubert 
252*ef5ccd6cSJohn Marino       gdb_assert (exec_filename == NULL);
253*ef5ccd6cSJohn Marino       exec_filename = xstrdup (scratch_pathname);
2545796c8dcSSimon Schubert 
255cf7f2e2dSJohn Marino       if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
2565796c8dcSSimon Schubert 	{
2575796c8dcSSimon Schubert 	  /* Make sure to close exec_bfd, or else "run" might try to use
2585796c8dcSSimon Schubert 	     it.  */
259cf7f2e2dSJohn Marino 	  exec_close ();
2605796c8dcSSimon Schubert 	  error (_("\"%s\": not in executable format: %s"),
261cf7f2e2dSJohn Marino 		 scratch_pathname,
262cf7f2e2dSJohn Marino 		 gdb_bfd_errmsg (bfd_get_error (), matching));
2635796c8dcSSimon Schubert 	}
2645796c8dcSSimon Schubert 
2655796c8dcSSimon Schubert       /* FIXME - This should only be run for RS6000, but the ifdef is a poor
2665796c8dcSSimon Schubert          way to accomplish.  */
2675796c8dcSSimon Schubert #ifdef DEPRECATED_IBM6000_TARGET
2685796c8dcSSimon Schubert       /* Setup initial vmap.  */
2695796c8dcSSimon Schubert 
2705796c8dcSSimon Schubert       map_vmap (exec_bfd, 0);
2715796c8dcSSimon Schubert       if (vmap == NULL)
2725796c8dcSSimon Schubert 	{
2735796c8dcSSimon Schubert 	  /* Make sure to close exec_bfd, or else "run" might try to use
2745796c8dcSSimon Schubert 	     it.  */
275cf7f2e2dSJohn Marino 	  exec_close ();
2765796c8dcSSimon Schubert 	  error (_("\"%s\": can't find the file sections: %s"),
2775796c8dcSSimon Schubert 		 scratch_pathname, bfd_errmsg (bfd_get_error ()));
2785796c8dcSSimon Schubert 	}
2795796c8dcSSimon Schubert #endif /* DEPRECATED_IBM6000_TARGET */
2805796c8dcSSimon Schubert 
2815796c8dcSSimon Schubert       if (build_section_table (exec_bfd, &sections, &sections_end))
2825796c8dcSSimon Schubert 	{
2835796c8dcSSimon Schubert 	  /* Make sure to close exec_bfd, or else "run" might try to use
2845796c8dcSSimon Schubert 	     it.  */
285cf7f2e2dSJohn Marino 	  exec_close ();
2865796c8dcSSimon Schubert 	  error (_("\"%s\": can't find the file sections: %s"),
2875796c8dcSSimon Schubert 		 scratch_pathname, bfd_errmsg (bfd_get_error ()));
2885796c8dcSSimon Schubert 	}
2895796c8dcSSimon Schubert 
2905796c8dcSSimon Schubert       exec_bfd_mtime = bfd_get_mtime (exec_bfd);
2915796c8dcSSimon Schubert 
2925796c8dcSSimon Schubert       validate_files ();
2935796c8dcSSimon Schubert 
2945796c8dcSSimon Schubert       set_gdbarch_from_file (exec_bfd);
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert       /* Add the executable's sections to the current address spaces'
297cf7f2e2dSJohn Marino 	 list of sections.  This possibly pushes the exec_ops
298cf7f2e2dSJohn Marino 	 target.  */
299*ef5ccd6cSJohn Marino       add_target_sections (&exec_bfd, sections, sections_end);
3005796c8dcSSimon Schubert       xfree (sections);
3015796c8dcSSimon Schubert 
3025796c8dcSSimon Schubert       /* Tell display code (if any) about the changed file name.  */
3035796c8dcSSimon Schubert       if (deprecated_exec_file_display_hook)
3045796c8dcSSimon Schubert 	(*deprecated_exec_file_display_hook) (filename);
3055796c8dcSSimon Schubert 
3065796c8dcSSimon Schubert       do_cleanups (cleanups);
3075796c8dcSSimon Schubert     }
3085796c8dcSSimon Schubert   bfd_cache_close_all ();
3095796c8dcSSimon Schubert   observer_notify_executable_changed ();
3105796c8dcSSimon Schubert }
3115796c8dcSSimon Schubert 
3125796c8dcSSimon Schubert /*  Process the first arg in ARGS as the new exec file.
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert    Note that we have to explicitly ignore additional args, since we can
3155796c8dcSSimon Schubert    be called from file_command(), which also calls symbol_file_command()
3165796c8dcSSimon Schubert    which can take multiple args.
3175796c8dcSSimon Schubert 
3185796c8dcSSimon Schubert    If ARGS is NULL, we just want to close the exec file.  */
3195796c8dcSSimon Schubert 
3205796c8dcSSimon Schubert static void
3215796c8dcSSimon Schubert exec_file_command (char *args, int from_tty)
3225796c8dcSSimon Schubert {
3235796c8dcSSimon Schubert   char **argv;
3245796c8dcSSimon Schubert   char *filename;
3255796c8dcSSimon Schubert 
3265796c8dcSSimon Schubert   if (from_tty && target_has_execution
3275796c8dcSSimon Schubert       && !query (_("A program is being debugged already.\n"
3285796c8dcSSimon Schubert 		   "Are you sure you want to change the file? ")))
3295796c8dcSSimon Schubert     error (_("File not changed."));
3305796c8dcSSimon Schubert 
3315796c8dcSSimon Schubert   if (args)
3325796c8dcSSimon Schubert     {
3335796c8dcSSimon Schubert       struct cleanup *cleanups;
3345796c8dcSSimon Schubert 
3355796c8dcSSimon Schubert       /* Scan through the args and pick up the first non option arg
3365796c8dcSSimon Schubert          as the filename.  */
3375796c8dcSSimon Schubert 
3385796c8dcSSimon Schubert       argv = gdb_buildargv (args);
3395796c8dcSSimon Schubert       cleanups = make_cleanup_freeargv (argv);
3405796c8dcSSimon Schubert 
3415796c8dcSSimon Schubert       for (; (*argv != NULL) && (**argv == '-'); argv++)
3425796c8dcSSimon Schubert         {;
3435796c8dcSSimon Schubert         }
3445796c8dcSSimon Schubert       if (*argv == NULL)
3455796c8dcSSimon Schubert         error (_("No executable file name was specified"));
3465796c8dcSSimon Schubert 
3475796c8dcSSimon Schubert       filename = tilde_expand (*argv);
3485796c8dcSSimon Schubert       make_cleanup (xfree, filename);
3495796c8dcSSimon Schubert       exec_file_attach (filename, from_tty);
3505796c8dcSSimon Schubert 
3515796c8dcSSimon Schubert       do_cleanups (cleanups);
3525796c8dcSSimon Schubert     }
3535796c8dcSSimon Schubert   else
3545796c8dcSSimon Schubert     exec_file_attach (NULL, from_tty);
3555796c8dcSSimon Schubert }
3565796c8dcSSimon Schubert 
3575796c8dcSSimon Schubert /* Set both the exec file and the symbol file, in one command.
3585796c8dcSSimon Schubert    What a novelty.  Why did GDB go through four major releases before this
3595796c8dcSSimon Schubert    command was added?  */
3605796c8dcSSimon Schubert 
3615796c8dcSSimon Schubert static void
3625796c8dcSSimon Schubert file_command (char *arg, int from_tty)
3635796c8dcSSimon Schubert {
3645796c8dcSSimon Schubert   /* FIXME, if we lose on reading the symbol file, we should revert
3655796c8dcSSimon Schubert      the exec file, but that's rough.  */
3665796c8dcSSimon Schubert   exec_file_command (arg, from_tty);
3675796c8dcSSimon Schubert   symbol_file_command (arg, from_tty);
3685796c8dcSSimon Schubert   if (deprecated_file_changed_hook)
3695796c8dcSSimon Schubert     deprecated_file_changed_hook (arg);
3705796c8dcSSimon Schubert }
3715796c8dcSSimon Schubert 
3725796c8dcSSimon Schubert 
3735796c8dcSSimon Schubert /* Locate all mappable sections of a BFD file.
3745796c8dcSSimon Schubert    table_pp_char is a char * to get it through bfd_map_over_sections;
3755796c8dcSSimon Schubert    we cast it back to its proper type.  */
3765796c8dcSSimon Schubert 
3775796c8dcSSimon Schubert static void
3785796c8dcSSimon Schubert add_to_section_table (bfd *abfd, struct bfd_section *asect,
3795796c8dcSSimon Schubert 		      void *table_pp_char)
3805796c8dcSSimon Schubert {
3815796c8dcSSimon Schubert   struct target_section **table_pp = (struct target_section **) table_pp_char;
3825796c8dcSSimon Schubert   flagword aflag;
3835796c8dcSSimon Schubert 
3845796c8dcSSimon Schubert   /* Check the section flags, but do not discard zero-length sections, since
3855796c8dcSSimon Schubert      some symbols may still be attached to this section.  For instance, we
3865796c8dcSSimon Schubert      encountered on sparc-solaris 2.10 a shared library with an empty .bss
3875796c8dcSSimon Schubert      section to which a symbol named "_end" was attached.  The address
3885796c8dcSSimon Schubert      of this symbol still needs to be relocated.  */
3895796c8dcSSimon Schubert   aflag = bfd_get_section_flags (abfd, asect);
3905796c8dcSSimon Schubert   if (!(aflag & SEC_ALLOC))
3915796c8dcSSimon Schubert     return;
3925796c8dcSSimon Schubert 
393*ef5ccd6cSJohn Marino   (*table_pp)->key = NULL;
3945796c8dcSSimon Schubert   (*table_pp)->bfd = abfd;
3955796c8dcSSimon Schubert   (*table_pp)->the_bfd_section = asect;
3965796c8dcSSimon Schubert   (*table_pp)->addr = bfd_section_vma (abfd, asect);
3975796c8dcSSimon Schubert   (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
3985796c8dcSSimon Schubert   (*table_pp)++;
3995796c8dcSSimon Schubert }
4005796c8dcSSimon Schubert 
4015796c8dcSSimon Schubert int
4025796c8dcSSimon Schubert resize_section_table (struct target_section_table *table, int num_added)
4035796c8dcSSimon Schubert {
4045796c8dcSSimon Schubert   int old_count;
4055796c8dcSSimon Schubert   int new_count;
4065796c8dcSSimon Schubert 
4075796c8dcSSimon Schubert   old_count = table->sections_end - table->sections;
4085796c8dcSSimon Schubert 
4095796c8dcSSimon Schubert   new_count = num_added + old_count;
4105796c8dcSSimon Schubert 
4115796c8dcSSimon Schubert   if (new_count)
4125796c8dcSSimon Schubert     {
4135796c8dcSSimon Schubert       table->sections = xrealloc (table->sections,
4145796c8dcSSimon Schubert 				  sizeof (struct target_section) * new_count);
4155796c8dcSSimon Schubert       table->sections_end = table->sections + new_count;
4165796c8dcSSimon Schubert     }
4175796c8dcSSimon Schubert   else
4185796c8dcSSimon Schubert     {
4195796c8dcSSimon Schubert       xfree (table->sections);
4205796c8dcSSimon Schubert       table->sections = table->sections_end = NULL;
4215796c8dcSSimon Schubert     }
4225796c8dcSSimon Schubert 
4235796c8dcSSimon Schubert   return old_count;
4245796c8dcSSimon Schubert }
4255796c8dcSSimon Schubert 
4265796c8dcSSimon Schubert /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
4275796c8dcSSimon Schubert    Returns 0 if OK, 1 on error.  */
4285796c8dcSSimon Schubert 
4295796c8dcSSimon Schubert int
4305796c8dcSSimon Schubert build_section_table (struct bfd *some_bfd, struct target_section **start,
4315796c8dcSSimon Schubert 		     struct target_section **end)
4325796c8dcSSimon Schubert {
4335796c8dcSSimon Schubert   unsigned count;
4345796c8dcSSimon Schubert 
4355796c8dcSSimon Schubert   count = bfd_count_sections (some_bfd);
4365796c8dcSSimon Schubert   if (*start)
4375796c8dcSSimon Schubert     xfree (* start);
4385796c8dcSSimon Schubert   *start = (struct target_section *) xmalloc (count * sizeof (**start));
4395796c8dcSSimon Schubert   *end = *start;
4405796c8dcSSimon Schubert   bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
4415796c8dcSSimon Schubert   if (*end > *start + count)
442c50c785cSJohn Marino     internal_error (__FILE__, __LINE__,
443c50c785cSJohn Marino 		    _("failed internal consistency check"));
4445796c8dcSSimon Schubert   /* We could realloc the table, but it probably loses for most files.  */
4455796c8dcSSimon Schubert   return 0;
4465796c8dcSSimon Schubert }
4475796c8dcSSimon Schubert 
4485796c8dcSSimon Schubert /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
4495796c8dcSSimon Schubert    current set of target sections.  */
4505796c8dcSSimon Schubert 
4515796c8dcSSimon Schubert void
452*ef5ccd6cSJohn Marino add_target_sections (void *key,
453*ef5ccd6cSJohn Marino 		     struct target_section *sections,
4545796c8dcSSimon Schubert 		     struct target_section *sections_end)
4555796c8dcSSimon Schubert {
4565796c8dcSSimon Schubert   int count;
4575796c8dcSSimon Schubert   struct target_section_table *table = current_target_sections;
4585796c8dcSSimon Schubert 
4595796c8dcSSimon Schubert   count = sections_end - sections;
4605796c8dcSSimon Schubert 
4615796c8dcSSimon Schubert   if (count > 0)
4625796c8dcSSimon Schubert     {
4635796c8dcSSimon Schubert       int space = resize_section_table (table, count);
464*ef5ccd6cSJohn Marino       int i;
465cf7f2e2dSJohn Marino 
466*ef5ccd6cSJohn Marino       for (i = 0; i < count; ++i)
467*ef5ccd6cSJohn Marino 	{
468*ef5ccd6cSJohn Marino 	  table->sections[space + i] = sections[i];
469*ef5ccd6cSJohn Marino 	  table->sections[space + i].key = key;
470*ef5ccd6cSJohn Marino 	}
4715796c8dcSSimon Schubert 
4725796c8dcSSimon Schubert       /* If these are the first file sections we can provide memory
4735796c8dcSSimon Schubert 	 from, push the file_stratum target.  */
474cf7f2e2dSJohn Marino       if (!using_exec_ops)
475cf7f2e2dSJohn Marino 	{
476cf7f2e2dSJohn Marino 	  using_exec_ops = 1;
4775796c8dcSSimon Schubert 	  push_target (&exec_ops);
4785796c8dcSSimon Schubert 	}
4795796c8dcSSimon Schubert     }
480cf7f2e2dSJohn Marino }
4815796c8dcSSimon Schubert 
4825796c8dcSSimon Schubert /* Remove all target sections taken from ABFD.  */
4835796c8dcSSimon Schubert 
4845796c8dcSSimon Schubert void
485*ef5ccd6cSJohn Marino remove_target_sections (void *key, bfd *abfd)
4865796c8dcSSimon Schubert {
4875796c8dcSSimon Schubert   struct target_section *src, *dest;
4885796c8dcSSimon Schubert   struct target_section_table *table = current_target_sections;
4895796c8dcSSimon Schubert 
4905796c8dcSSimon Schubert   dest = table->sections;
4915796c8dcSSimon Schubert   for (src = table->sections; src < table->sections_end; src++)
492*ef5ccd6cSJohn Marino     if (src->key != key || src->bfd != abfd)
4935796c8dcSSimon Schubert       {
4945796c8dcSSimon Schubert 	/* Keep this section.  */
4955796c8dcSSimon Schubert 	if (dest < src)
4965796c8dcSSimon Schubert 	  *dest = *src;
4975796c8dcSSimon Schubert 	dest++;
4985796c8dcSSimon Schubert       }
4995796c8dcSSimon Schubert 
5005796c8dcSSimon Schubert   /* If we've dropped any sections, resize the section table.  */
5015796c8dcSSimon Schubert   if (dest < src)
5025796c8dcSSimon Schubert     {
5035796c8dcSSimon Schubert       int old_count;
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert       old_count = resize_section_table (table, dest - src);
5065796c8dcSSimon Schubert 
5075796c8dcSSimon Schubert       /* If we don't have any more sections to read memory from,
5085796c8dcSSimon Schubert 	 remove the file_stratum target from the stack.  */
5095796c8dcSSimon Schubert       if (old_count + (dest - src) == 0)
510cf7f2e2dSJohn Marino 	{
511cf7f2e2dSJohn Marino 	  struct program_space *pspace;
512cf7f2e2dSJohn Marino 
513cf7f2e2dSJohn Marino 	  ALL_PSPACES (pspace)
514cf7f2e2dSJohn Marino 	    if (pspace->target_sections.sections
515cf7f2e2dSJohn Marino 		!= pspace->target_sections.sections_end)
516cf7f2e2dSJohn Marino 	      return;
517cf7f2e2dSJohn Marino 
5185796c8dcSSimon Schubert 	  unpush_target (&exec_ops);
5195796c8dcSSimon Schubert 	}
5205796c8dcSSimon Schubert     }
521cf7f2e2dSJohn Marino }
5225796c8dcSSimon Schubert 
5235796c8dcSSimon Schubert 
5245796c8dcSSimon Schubert static void
5255796c8dcSSimon Schubert bfdsec_to_vmap (struct bfd *abfd, struct bfd_section *sect, void *arg3)
5265796c8dcSSimon Schubert {
5275796c8dcSSimon Schubert   struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *) arg3;
5285796c8dcSSimon Schubert   struct vmap *vp;
5295796c8dcSSimon Schubert 
5305796c8dcSSimon Schubert   vp = vmap_bfd->pvmap;
5315796c8dcSSimon Schubert 
5325796c8dcSSimon Schubert   if ((bfd_get_section_flags (abfd, sect) & SEC_LOAD) == 0)
5335796c8dcSSimon Schubert     return;
5345796c8dcSSimon Schubert 
5355796c8dcSSimon Schubert   if (strcmp (bfd_section_name (abfd, sect), ".text") == 0)
5365796c8dcSSimon Schubert     {
5375796c8dcSSimon Schubert       vp->tstart = bfd_section_vma (abfd, sect);
5385796c8dcSSimon Schubert       vp->tend = vp->tstart + bfd_section_size (abfd, sect);
5395796c8dcSSimon Schubert       vp->tvma = bfd_section_vma (abfd, sect);
5405796c8dcSSimon Schubert       vp->toffs = sect->filepos;
5415796c8dcSSimon Schubert     }
5425796c8dcSSimon Schubert   else if (strcmp (bfd_section_name (abfd, sect), ".data") == 0)
5435796c8dcSSimon Schubert     {
5445796c8dcSSimon Schubert       vp->dstart = bfd_section_vma (abfd, sect);
5455796c8dcSSimon Schubert       vp->dend = vp->dstart + bfd_section_size (abfd, sect);
5465796c8dcSSimon Schubert       vp->dvma = bfd_section_vma (abfd, sect);
5475796c8dcSSimon Schubert     }
5485796c8dcSSimon Schubert   /* Silently ignore other types of sections.  (FIXME?)  */
5495796c8dcSSimon Schubert }
5505796c8dcSSimon Schubert 
5515796c8dcSSimon Schubert /* Make a vmap for ABFD which might be a member of the archive ARCH.
5525796c8dcSSimon Schubert    Return the new vmap.  */
5535796c8dcSSimon Schubert 
5545796c8dcSSimon Schubert struct vmap *
5555796c8dcSSimon Schubert map_vmap (bfd *abfd, bfd *arch)
5565796c8dcSSimon Schubert {
5575796c8dcSSimon Schubert   struct vmap_and_bfd vmap_bfd;
5585796c8dcSSimon Schubert   struct vmap *vp, **vpp;
5595796c8dcSSimon Schubert 
5605796c8dcSSimon Schubert   vp = (struct vmap *) xmalloc (sizeof (*vp));
5615796c8dcSSimon Schubert   memset ((char *) vp, '\0', sizeof (*vp));
5625796c8dcSSimon Schubert   vp->nxt = 0;
5635796c8dcSSimon Schubert   vp->bfd = abfd;
564*ef5ccd6cSJohn Marino   gdb_bfd_ref (abfd);
5655796c8dcSSimon Schubert   vp->name = bfd_get_filename (arch ? arch : abfd);
5665796c8dcSSimon Schubert   vp->member = arch ? bfd_get_filename (abfd) : "";
5675796c8dcSSimon Schubert 
5685796c8dcSSimon Schubert   vmap_bfd.pbfd = arch;
5695796c8dcSSimon Schubert   vmap_bfd.pvmap = vp;
5705796c8dcSSimon Schubert   bfd_map_over_sections (abfd, bfdsec_to_vmap, &vmap_bfd);
5715796c8dcSSimon Schubert 
5725796c8dcSSimon Schubert   /* Find the end of the list and append.  */
5735796c8dcSSimon Schubert   for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt)
5745796c8dcSSimon Schubert     ;
5755796c8dcSSimon Schubert   *vpp = vp;
5765796c8dcSSimon Schubert 
5775796c8dcSSimon Schubert   return vp;
5785796c8dcSSimon Schubert }
5795796c8dcSSimon Schubert 
5805796c8dcSSimon Schubert 
581c50c785cSJohn Marino VEC(mem_range_s) *
582c50c785cSJohn Marino section_table_available_memory (VEC(mem_range_s) *memory,
583c50c785cSJohn Marino 				CORE_ADDR memaddr, ULONGEST len,
584c50c785cSJohn Marino 				struct target_section *sections,
585c50c785cSJohn Marino 				struct target_section *sections_end)
586c50c785cSJohn Marino {
587c50c785cSJohn Marino   struct target_section *p;
588c50c785cSJohn Marino 
589c50c785cSJohn Marino   for (p = sections; p < sections_end; p++)
590c50c785cSJohn Marino     {
591c50c785cSJohn Marino       if ((bfd_get_section_flags (p->bfd, p->the_bfd_section)
592c50c785cSJohn Marino 	   & SEC_READONLY) == 0)
593c50c785cSJohn Marino 	continue;
594c50c785cSJohn Marino 
595c50c785cSJohn Marino       /* Copy the meta-data, adjusted.  */
596c50c785cSJohn Marino       if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len))
597c50c785cSJohn Marino 	{
598c50c785cSJohn Marino 	  ULONGEST lo1, hi1, lo2, hi2;
599c50c785cSJohn Marino 	  struct mem_range *r;
600c50c785cSJohn Marino 
601c50c785cSJohn Marino 	  lo1 = memaddr;
602c50c785cSJohn Marino 	  hi1 = memaddr + len;
603c50c785cSJohn Marino 
604c50c785cSJohn Marino 	  lo2 = p->addr;
605c50c785cSJohn Marino 	  hi2 = p->endaddr;
606c50c785cSJohn Marino 
607c50c785cSJohn Marino 	  r = VEC_safe_push (mem_range_s, memory, NULL);
608c50c785cSJohn Marino 
609c50c785cSJohn Marino 	  r->start = max (lo1, lo2);
610c50c785cSJohn Marino 	  r->length = min (hi1, hi2) - r->start;
611c50c785cSJohn Marino 	}
612c50c785cSJohn Marino     }
613c50c785cSJohn Marino 
614c50c785cSJohn Marino   return memory;
615c50c785cSJohn Marino }
616c50c785cSJohn Marino 
6175796c8dcSSimon Schubert int
6185796c8dcSSimon Schubert section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
6195796c8dcSSimon Schubert 				   ULONGEST offset, LONGEST len,
6205796c8dcSSimon Schubert 				   struct target_section *sections,
6215796c8dcSSimon Schubert 				   struct target_section *sections_end,
6225796c8dcSSimon Schubert 				   const char *section_name)
6235796c8dcSSimon Schubert {
6245796c8dcSSimon Schubert   int res;
6255796c8dcSSimon Schubert   struct target_section *p;
6265796c8dcSSimon Schubert   ULONGEST memaddr = offset;
6275796c8dcSSimon Schubert   ULONGEST memend = memaddr + len;
6285796c8dcSSimon Schubert 
6295796c8dcSSimon Schubert   if (len <= 0)
630c50c785cSJohn Marino     internal_error (__FILE__, __LINE__,
631c50c785cSJohn Marino 		    _("failed internal consistency check"));
6325796c8dcSSimon Schubert 
6335796c8dcSSimon Schubert   for (p = sections; p < sections_end; p++)
6345796c8dcSSimon Schubert     {
6355796c8dcSSimon Schubert       if (section_name && strcmp (section_name, p->the_bfd_section->name) != 0)
636c50c785cSJohn Marino 	continue;		/* not the section we need.  */
6375796c8dcSSimon Schubert       if (memaddr >= p->addr)
6385796c8dcSSimon Schubert         {
6395796c8dcSSimon Schubert 	  if (memend <= p->endaddr)
6405796c8dcSSimon Schubert 	    {
6415796c8dcSSimon Schubert 	      /* Entire transfer is within this section.  */
6425796c8dcSSimon Schubert 	      if (writebuf)
6435796c8dcSSimon Schubert 		res = bfd_set_section_contents (p->bfd, p->the_bfd_section,
6445796c8dcSSimon Schubert 						writebuf, memaddr - p->addr,
6455796c8dcSSimon Schubert 						len);
6465796c8dcSSimon Schubert 	      else
6475796c8dcSSimon Schubert 		res = bfd_get_section_contents (p->bfd, p->the_bfd_section,
6485796c8dcSSimon Schubert 						readbuf, memaddr - p->addr,
6495796c8dcSSimon Schubert 						len);
6505796c8dcSSimon Schubert 	      return (res != 0) ? len : 0;
6515796c8dcSSimon Schubert 	    }
6525796c8dcSSimon Schubert 	  else if (memaddr >= p->endaddr)
6535796c8dcSSimon Schubert 	    {
6545796c8dcSSimon Schubert 	      /* This section ends before the transfer starts.  */
6555796c8dcSSimon Schubert 	      continue;
6565796c8dcSSimon Schubert 	    }
6575796c8dcSSimon Schubert 	  else
6585796c8dcSSimon Schubert 	    {
6595796c8dcSSimon Schubert 	      /* This section overlaps the transfer.  Just do half.  */
6605796c8dcSSimon Schubert 	      len = p->endaddr - memaddr;
6615796c8dcSSimon Schubert 	      if (writebuf)
6625796c8dcSSimon Schubert 		res = bfd_set_section_contents (p->bfd, p->the_bfd_section,
6635796c8dcSSimon Schubert 						writebuf, memaddr - p->addr,
6645796c8dcSSimon Schubert 						len);
6655796c8dcSSimon Schubert 	      else
6665796c8dcSSimon Schubert 		res = bfd_get_section_contents (p->bfd, p->the_bfd_section,
6675796c8dcSSimon Schubert 						readbuf, memaddr - p->addr,
6685796c8dcSSimon Schubert 						len);
6695796c8dcSSimon Schubert 	      return (res != 0) ? len : 0;
6705796c8dcSSimon Schubert 	    }
6715796c8dcSSimon Schubert         }
6725796c8dcSSimon Schubert     }
6735796c8dcSSimon Schubert 
674c50c785cSJohn Marino   return 0;			/* We can't help.  */
6755796c8dcSSimon Schubert }
6765796c8dcSSimon Schubert 
677*ef5ccd6cSJohn Marino static struct target_section_table *
6785796c8dcSSimon Schubert exec_get_section_table (struct target_ops *ops)
6795796c8dcSSimon Schubert {
6805796c8dcSSimon Schubert   return current_target_sections;
6815796c8dcSSimon Schubert }
6825796c8dcSSimon Schubert 
6835796c8dcSSimon Schubert static LONGEST
6845796c8dcSSimon Schubert exec_xfer_partial (struct target_ops *ops, enum target_object object,
6855796c8dcSSimon Schubert 		   const char *annex, gdb_byte *readbuf,
6865796c8dcSSimon Schubert 		   const gdb_byte *writebuf,
6875796c8dcSSimon Schubert 		   ULONGEST offset, LONGEST len)
6885796c8dcSSimon Schubert {
6895796c8dcSSimon Schubert   struct target_section_table *table = target_get_section_table (ops);
6905796c8dcSSimon Schubert 
6915796c8dcSSimon Schubert   if (object == TARGET_OBJECT_MEMORY)
6925796c8dcSSimon Schubert     return section_table_xfer_memory_partial (readbuf, writebuf,
6935796c8dcSSimon Schubert 					      offset, len,
6945796c8dcSSimon Schubert 					      table->sections,
6955796c8dcSSimon Schubert 					      table->sections_end,
6965796c8dcSSimon Schubert 					      NULL);
6975796c8dcSSimon Schubert   else
6985796c8dcSSimon Schubert     return -1;
6995796c8dcSSimon Schubert }
7005796c8dcSSimon Schubert 
7015796c8dcSSimon Schubert 
7025796c8dcSSimon Schubert void
7035796c8dcSSimon Schubert print_section_info (struct target_section_table *t, bfd *abfd)
7045796c8dcSSimon Schubert {
7055796c8dcSSimon Schubert   struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
7065796c8dcSSimon Schubert   struct target_section *p;
7075796c8dcSSimon Schubert   /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64.  */
7085796c8dcSSimon Schubert   int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
7095796c8dcSSimon Schubert 
7105796c8dcSSimon Schubert   printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
7115796c8dcSSimon Schubert   wrap_here ("        ");
7125796c8dcSSimon Schubert   printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
7135796c8dcSSimon Schubert   if (abfd == exec_bfd)
714cf7f2e2dSJohn Marino     {
715c50c785cSJohn Marino       /* gcc-3.4 does not like the initialization in
716c50c785cSJohn Marino 	 <p == t->sections_end>.  */
717cf7f2e2dSJohn Marino       bfd_vma displacement = 0;
718cf7f2e2dSJohn Marino       bfd_vma entry_point;
719cf7f2e2dSJohn Marino 
720cf7f2e2dSJohn Marino       for (p = t->sections; p < t->sections_end; p++)
721cf7f2e2dSJohn Marino 	{
722cf7f2e2dSJohn Marino 	  asection *asect = p->the_bfd_section;
723cf7f2e2dSJohn Marino 
724cf7f2e2dSJohn Marino 	  if ((bfd_get_section_flags (abfd, asect) & (SEC_ALLOC | SEC_LOAD))
725cf7f2e2dSJohn Marino 	      != (SEC_ALLOC | SEC_LOAD))
726cf7f2e2dSJohn Marino 	    continue;
727cf7f2e2dSJohn Marino 
728cf7f2e2dSJohn Marino 	  if (bfd_get_section_vma (abfd, asect) <= abfd->start_address
729cf7f2e2dSJohn Marino 	      && abfd->start_address < (bfd_get_section_vma (abfd, asect)
730cf7f2e2dSJohn Marino 					+ bfd_get_section_size (asect)))
731cf7f2e2dSJohn Marino 	    {
732cf7f2e2dSJohn Marino 	      displacement = p->addr - bfd_get_section_vma (abfd, asect);
733cf7f2e2dSJohn Marino 	      break;
734cf7f2e2dSJohn Marino 	    }
735cf7f2e2dSJohn Marino 	}
736cf7f2e2dSJohn Marino       if (p == t->sections_end)
737c50c785cSJohn Marino 	warning (_("Cannot find section for the entry point of %s."),
738cf7f2e2dSJohn Marino 		 bfd_get_filename (abfd));
739cf7f2e2dSJohn Marino 
740cf7f2e2dSJohn Marino       entry_point = gdbarch_addr_bits_remove (gdbarch,
741cf7f2e2dSJohn Marino 					      bfd_get_start_address (abfd)
742cf7f2e2dSJohn Marino 						+ displacement);
7435796c8dcSSimon Schubert       printf_filtered (_("\tEntry point: %s\n"),
744cf7f2e2dSJohn Marino 		       paddress (gdbarch, entry_point));
745cf7f2e2dSJohn Marino     }
7465796c8dcSSimon Schubert   for (p = t->sections; p < t->sections_end; p++)
7475796c8dcSSimon Schubert     {
7485796c8dcSSimon Schubert       printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
7495796c8dcSSimon Schubert       printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
7505796c8dcSSimon Schubert 
7515796c8dcSSimon Schubert       /* FIXME: A format of "08l" is not wide enough for file offsets
7525796c8dcSSimon Schubert 	 larger than 4GB.  OTOH, making it "016l" isn't desirable either
7535796c8dcSSimon Schubert 	 since most output will then be much wider than necessary.  It
7545796c8dcSSimon Schubert 	 may make sense to test the size of the file and choose the
7555796c8dcSSimon Schubert 	 format string accordingly.  */
7565796c8dcSSimon Schubert       /* FIXME: i18n: Need to rewrite this sentence.  */
7575796c8dcSSimon Schubert       if (info_verbose)
7585796c8dcSSimon Schubert 	printf_filtered (" @ %s",
7595796c8dcSSimon Schubert 			 hex_string_custom (p->the_bfd_section->filepos, 8));
760c50c785cSJohn Marino       printf_filtered (" is %s", bfd_section_name (p->bfd,
761c50c785cSJohn Marino 						   p->the_bfd_section));
7625796c8dcSSimon Schubert       if (p->bfd != abfd)
7635796c8dcSSimon Schubert 	printf_filtered (" in %s", bfd_get_filename (p->bfd));
7645796c8dcSSimon Schubert       printf_filtered ("\n");
7655796c8dcSSimon Schubert     }
7665796c8dcSSimon Schubert }
7675796c8dcSSimon Schubert 
7685796c8dcSSimon Schubert static void
7695796c8dcSSimon Schubert exec_files_info (struct target_ops *t)
7705796c8dcSSimon Schubert {
771*ef5ccd6cSJohn Marino   if (exec_bfd)
7725796c8dcSSimon Schubert     print_section_info (current_target_sections, exec_bfd);
773*ef5ccd6cSJohn Marino   else
774*ef5ccd6cSJohn Marino     puts_filtered (_("\t<no file loaded>\n"));
7755796c8dcSSimon Schubert 
7765796c8dcSSimon Schubert   if (vmap)
7775796c8dcSSimon Schubert     {
778*ef5ccd6cSJohn Marino       int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
7795796c8dcSSimon Schubert       struct vmap *vp;
7805796c8dcSSimon Schubert 
7815796c8dcSSimon Schubert       printf_unfiltered (_("\tMapping info for file `%s'.\n"), vmap->name);
7825796c8dcSSimon Schubert       printf_unfiltered ("\t  %*s   %*s   %*s   %*s %8.8s %s\n",
7835796c8dcSSimon Schubert 			 addr_size * 2, "tstart",
7845796c8dcSSimon Schubert 			 addr_size * 2, "tend",
7855796c8dcSSimon Schubert 			 addr_size * 2, "dstart",
7865796c8dcSSimon Schubert 			 addr_size * 2, "dend",
7875796c8dcSSimon Schubert 			 "section",
7885796c8dcSSimon Schubert 			 "file(member)");
7895796c8dcSSimon Schubert 
7905796c8dcSSimon Schubert       for (vp = vmap; vp; vp = vp->nxt)
7915796c8dcSSimon Schubert 	printf_unfiltered ("\t0x%s 0x%s 0x%s 0x%s %s%s%s%s\n",
7925796c8dcSSimon Schubert 			   phex (vp->tstart, addr_size),
7935796c8dcSSimon Schubert 			   phex (vp->tend, addr_size),
7945796c8dcSSimon Schubert 			   phex (vp->dstart, addr_size),
7955796c8dcSSimon Schubert 			   phex (vp->dend, addr_size),
7965796c8dcSSimon Schubert 			   vp->name,
7975796c8dcSSimon Schubert 			   *vp->member ? "(" : "", vp->member,
7985796c8dcSSimon Schubert 			   *vp->member ? ")" : "");
7995796c8dcSSimon Schubert     }
8005796c8dcSSimon Schubert }
8015796c8dcSSimon Schubert 
8025796c8dcSSimon Schubert static void
8035796c8dcSSimon Schubert set_section_command (char *args, int from_tty)
8045796c8dcSSimon Schubert {
8055796c8dcSSimon Schubert   struct target_section *p;
8065796c8dcSSimon Schubert   char *secname;
8075796c8dcSSimon Schubert   unsigned seclen;
8085796c8dcSSimon Schubert   unsigned long secaddr;
8095796c8dcSSimon Schubert   char secprint[100];
8105796c8dcSSimon Schubert   long offset;
8115796c8dcSSimon Schubert   struct target_section_table *table;
8125796c8dcSSimon Schubert 
8135796c8dcSSimon Schubert   if (args == 0)
8145796c8dcSSimon Schubert     error (_("Must specify section name and its virtual address"));
8155796c8dcSSimon Schubert 
816c50c785cSJohn Marino   /* Parse out section name.  */
8175796c8dcSSimon Schubert   for (secname = args; !isspace (*args); args++);
8185796c8dcSSimon Schubert   seclen = args - secname;
8195796c8dcSSimon Schubert 
820c50c785cSJohn Marino   /* Parse out new virtual address.  */
8215796c8dcSSimon Schubert   secaddr = parse_and_eval_address (args);
8225796c8dcSSimon Schubert 
8235796c8dcSSimon Schubert   table = current_target_sections;
8245796c8dcSSimon Schubert   for (p = table->sections; p < table->sections_end; p++)
8255796c8dcSSimon Schubert     {
826*ef5ccd6cSJohn Marino       if (!strncmp (secname, bfd_section_name (p->bfd,
827c50c785cSJohn Marino 					       p->the_bfd_section), seclen)
828*ef5ccd6cSJohn Marino 	  && bfd_section_name (p->bfd, p->the_bfd_section)[seclen] == '\0')
8295796c8dcSSimon Schubert 	{
8305796c8dcSSimon Schubert 	  offset = secaddr - p->addr;
8315796c8dcSSimon Schubert 	  p->addr += offset;
8325796c8dcSSimon Schubert 	  p->endaddr += offset;
8335796c8dcSSimon Schubert 	  if (from_tty)
8345796c8dcSSimon Schubert 	    exec_files_info (&exec_ops);
8355796c8dcSSimon Schubert 	  return;
8365796c8dcSSimon Schubert 	}
8375796c8dcSSimon Schubert     }
8385796c8dcSSimon Schubert   if (seclen >= sizeof (secprint))
8395796c8dcSSimon Schubert     seclen = sizeof (secprint) - 1;
8405796c8dcSSimon Schubert   strncpy (secprint, secname, seclen);
8415796c8dcSSimon Schubert   secprint[seclen] = '\0';
8425796c8dcSSimon Schubert   error (_("Section %s not found"), secprint);
8435796c8dcSSimon Schubert }
8445796c8dcSSimon Schubert 
8455796c8dcSSimon Schubert /* If we can find a section in FILENAME with BFD index INDEX, adjust
8465796c8dcSSimon Schubert    it to ADDRESS.  */
8475796c8dcSSimon Schubert 
8485796c8dcSSimon Schubert void
8495796c8dcSSimon Schubert exec_set_section_address (const char *filename, int index, CORE_ADDR address)
8505796c8dcSSimon Schubert {
8515796c8dcSSimon Schubert   struct target_section *p;
8525796c8dcSSimon Schubert   struct target_section_table *table;
8535796c8dcSSimon Schubert 
8545796c8dcSSimon Schubert   table = current_target_sections;
8555796c8dcSSimon Schubert   for (p = table->sections; p < table->sections_end; p++)
8565796c8dcSSimon Schubert     {
857c50c785cSJohn Marino       if (filename_cmp (filename, p->bfd->filename) == 0
8585796c8dcSSimon Schubert 	  && index == p->the_bfd_section->index)
8595796c8dcSSimon Schubert 	{
8605796c8dcSSimon Schubert 	  p->endaddr += address - p->addr;
8615796c8dcSSimon Schubert 	  p->addr = address;
8625796c8dcSSimon Schubert 	}
8635796c8dcSSimon Schubert     }
8645796c8dcSSimon Schubert }
8655796c8dcSSimon Schubert 
8665796c8dcSSimon Schubert /* If mourn is being called in all the right places, this could be say
8675796c8dcSSimon Schubert    `gdb internal error' (since generic_mourn calls
8685796c8dcSSimon Schubert    breakpoint_init_inferior).  */
8695796c8dcSSimon Schubert 
8705796c8dcSSimon Schubert static int
8715796c8dcSSimon Schubert ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
8725796c8dcSSimon Schubert {
8735796c8dcSSimon Schubert   return 0;
8745796c8dcSSimon Schubert }
8755796c8dcSSimon Schubert 
8765796c8dcSSimon Schubert static int
8775796c8dcSSimon Schubert exec_has_memory (struct target_ops *ops)
8785796c8dcSSimon Schubert {
8795796c8dcSSimon Schubert   /* We can provide memory if we have any file/target sections to read
8805796c8dcSSimon Schubert      from.  */
8815796c8dcSSimon Schubert   return (current_target_sections->sections
8825796c8dcSSimon Schubert 	  != current_target_sections->sections_end);
8835796c8dcSSimon Schubert }
8845796c8dcSSimon Schubert 
8855796c8dcSSimon Schubert /* Find mapped memory.  */
8865796c8dcSSimon Schubert 
8875796c8dcSSimon Schubert extern void
888c50c785cSJohn Marino exec_set_find_memory_regions (int (*func) (find_memory_region_ftype, void *))
8895796c8dcSSimon Schubert {
8905796c8dcSSimon Schubert   exec_ops.to_find_memory_regions = func;
8915796c8dcSSimon Schubert }
8925796c8dcSSimon Schubert 
8935796c8dcSSimon Schubert static char *exec_make_note_section (bfd *, int *);
8945796c8dcSSimon Schubert 
8955796c8dcSSimon Schubert /* Fill in the exec file target vector.  Very few entries need to be
8965796c8dcSSimon Schubert    defined.  */
8975796c8dcSSimon Schubert 
8985796c8dcSSimon Schubert static void
8995796c8dcSSimon Schubert init_exec_ops (void)
9005796c8dcSSimon Schubert {
9015796c8dcSSimon Schubert   exec_ops.to_shortname = "exec";
9025796c8dcSSimon Schubert   exec_ops.to_longname = "Local exec file";
9035796c8dcSSimon Schubert   exec_ops.to_doc = "Use an executable file as a target.\n\
9045796c8dcSSimon Schubert Specify the filename of the executable file.";
9055796c8dcSSimon Schubert   exec_ops.to_open = exec_open;
906cf7f2e2dSJohn Marino   exec_ops.to_close = exec_close_1;
9075796c8dcSSimon Schubert   exec_ops.to_attach = find_default_attach;
9085796c8dcSSimon Schubert   exec_ops.to_xfer_partial = exec_xfer_partial;
9095796c8dcSSimon Schubert   exec_ops.to_get_section_table = exec_get_section_table;
9105796c8dcSSimon Schubert   exec_ops.to_files_info = exec_files_info;
9115796c8dcSSimon Schubert   exec_ops.to_insert_breakpoint = ignore;
9125796c8dcSSimon Schubert   exec_ops.to_remove_breakpoint = ignore;
9135796c8dcSSimon Schubert   exec_ops.to_create_inferior = find_default_create_inferior;
9145796c8dcSSimon Schubert   exec_ops.to_stratum = file_stratum;
9155796c8dcSSimon Schubert   exec_ops.to_has_memory = exec_has_memory;
9165796c8dcSSimon Schubert   exec_ops.to_make_corefile_notes = exec_make_note_section;
9175796c8dcSSimon Schubert   exec_ops.to_magic = OPS_MAGIC;
9185796c8dcSSimon Schubert }
9195796c8dcSSimon Schubert 
9205796c8dcSSimon Schubert void
9215796c8dcSSimon Schubert _initialize_exec (void)
9225796c8dcSSimon Schubert {
9235796c8dcSSimon Schubert   struct cmd_list_element *c;
9245796c8dcSSimon Schubert 
9255796c8dcSSimon Schubert   init_exec_ops ();
9265796c8dcSSimon Schubert 
9275796c8dcSSimon Schubert   if (!dbx_commands)
9285796c8dcSSimon Schubert     {
9295796c8dcSSimon Schubert       c = add_cmd ("file", class_files, file_command, _("\
9305796c8dcSSimon Schubert Use FILE as program to be debugged.\n\
9315796c8dcSSimon Schubert It is read for its symbols, for getting the contents of pure memory,\n\
9325796c8dcSSimon Schubert and it is the program executed when you use the `run' command.\n\
9335796c8dcSSimon Schubert If FILE cannot be found as specified, your execution directory path\n\
9345796c8dcSSimon Schubert ($PATH) is searched for a command of that name.\n\
9355796c8dcSSimon Schubert No arg means to have no executable file and no symbols."), &cmdlist);
9365796c8dcSSimon Schubert       set_cmd_completer (c, filename_completer);
9375796c8dcSSimon Schubert     }
9385796c8dcSSimon Schubert 
9395796c8dcSSimon Schubert   c = add_cmd ("exec-file", class_files, exec_file_command, _("\
9405796c8dcSSimon Schubert Use FILE as program for getting contents of pure memory.\n\
9415796c8dcSSimon Schubert If FILE cannot be found as specified, your execution directory path\n\
9425796c8dcSSimon Schubert is searched for a command of that name.\n\
9435796c8dcSSimon Schubert No arg means have no executable file."), &cmdlist);
9445796c8dcSSimon Schubert   set_cmd_completer (c, filename_completer);
9455796c8dcSSimon Schubert 
9465796c8dcSSimon Schubert   add_com ("section", class_files, set_section_command, _("\
9475796c8dcSSimon Schubert Change the base address of section SECTION of the exec file to ADDR.\n\
9485796c8dcSSimon Schubert This can be used if the exec file does not contain section addresses,\n\
9495796c8dcSSimon Schubert (such as in the a.out format), or when the addresses specified in the\n\
9505796c8dcSSimon Schubert file itself are wrong.  Each section must be changed separately.  The\n\
9515796c8dcSSimon Schubert ``info files'' command lists all the sections and their addresses."));
9525796c8dcSSimon Schubert 
9535796c8dcSSimon Schubert   add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
9545796c8dcSSimon Schubert Set writing into executable and core files."), _("\
9555796c8dcSSimon Schubert Show writing into executable and core files."), NULL,
9565796c8dcSSimon Schubert 			   NULL,
9575796c8dcSSimon Schubert 			   show_write_files,
9585796c8dcSSimon Schubert 			   &setlist, &showlist);
9595796c8dcSSimon Schubert 
9605796c8dcSSimon Schubert   add_target (&exec_ops);
9615796c8dcSSimon Schubert }
9625796c8dcSSimon Schubert 
9635796c8dcSSimon Schubert static char *
9645796c8dcSSimon Schubert exec_make_note_section (bfd *obfd, int *note_size)
9655796c8dcSSimon Schubert {
9665796c8dcSSimon Schubert   error (_("Can't create a corefile"));
9675796c8dcSSimon Schubert }
968