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
show_write_files(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)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
exec_open(char * args,int from_tty)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
exec_close(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
exec_close_1(int quitting)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
exec_file_clear(int from_tty)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
exec_file_attach(char * filename,int from_tty)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, §ions, §ions_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
exec_file_command(char * args,int from_tty)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
file_command(char * arg,int from_tty)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
add_to_section_table(bfd * abfd,struct bfd_section * asect,void * table_pp_char)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
resize_section_table(struct target_section_table * table,int num_added)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
4097f357fefSJohn Marino if (!num_added)
4107f357fefSJohn Marino return old_count;
4117f357fefSJohn Marino
4125796c8dcSSimon Schubert new_count = num_added + old_count;
4135796c8dcSSimon Schubert
4145796c8dcSSimon Schubert if (new_count)
4155796c8dcSSimon Schubert {
4165796c8dcSSimon Schubert table->sections = xrealloc (table->sections,
4175796c8dcSSimon Schubert sizeof (struct target_section) * new_count);
4185796c8dcSSimon Schubert table->sections_end = table->sections + new_count;
4195796c8dcSSimon Schubert }
4205796c8dcSSimon Schubert else
4215796c8dcSSimon Schubert {
4225796c8dcSSimon Schubert xfree (table->sections);
4235796c8dcSSimon Schubert table->sections = table->sections_end = NULL;
4245796c8dcSSimon Schubert }
4255796c8dcSSimon Schubert
4265796c8dcSSimon Schubert return old_count;
4275796c8dcSSimon Schubert }
4285796c8dcSSimon Schubert
4295796c8dcSSimon Schubert /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
4305796c8dcSSimon Schubert Returns 0 if OK, 1 on error. */
4315796c8dcSSimon Schubert
4325796c8dcSSimon Schubert int
build_section_table(struct bfd * some_bfd,struct target_section ** start,struct target_section ** end)4335796c8dcSSimon Schubert build_section_table (struct bfd *some_bfd, struct target_section **start,
4345796c8dcSSimon Schubert struct target_section **end)
4355796c8dcSSimon Schubert {
4365796c8dcSSimon Schubert unsigned count;
4375796c8dcSSimon Schubert
4385796c8dcSSimon Schubert count = bfd_count_sections (some_bfd);
4395796c8dcSSimon Schubert if (*start)
4405796c8dcSSimon Schubert xfree (* start);
4415796c8dcSSimon Schubert *start = (struct target_section *) xmalloc (count * sizeof (**start));
4425796c8dcSSimon Schubert *end = *start;
4435796c8dcSSimon Schubert bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
4445796c8dcSSimon Schubert if (*end > *start + count)
445c50c785cSJohn Marino internal_error (__FILE__, __LINE__,
446c50c785cSJohn Marino _("failed internal consistency check"));
4475796c8dcSSimon Schubert /* We could realloc the table, but it probably loses for most files. */
4485796c8dcSSimon Schubert return 0;
4495796c8dcSSimon Schubert }
4505796c8dcSSimon Schubert
4515796c8dcSSimon Schubert /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
4525796c8dcSSimon Schubert current set of target sections. */
4535796c8dcSSimon Schubert
4545796c8dcSSimon Schubert void
add_target_sections(void * key,struct target_section * sections,struct target_section * sections_end)455*ef5ccd6cSJohn Marino add_target_sections (void *key,
456*ef5ccd6cSJohn Marino struct target_section *sections,
4575796c8dcSSimon Schubert struct target_section *sections_end)
4585796c8dcSSimon Schubert {
4595796c8dcSSimon Schubert int count;
4605796c8dcSSimon Schubert struct target_section_table *table = current_target_sections;
4615796c8dcSSimon Schubert
4625796c8dcSSimon Schubert count = sections_end - sections;
4635796c8dcSSimon Schubert
4645796c8dcSSimon Schubert if (count > 0)
4655796c8dcSSimon Schubert {
4665796c8dcSSimon Schubert int space = resize_section_table (table, count);
467*ef5ccd6cSJohn Marino int i;
468cf7f2e2dSJohn Marino
469*ef5ccd6cSJohn Marino for (i = 0; i < count; ++i)
470*ef5ccd6cSJohn Marino {
471*ef5ccd6cSJohn Marino table->sections[space + i] = sections[i];
472*ef5ccd6cSJohn Marino table->sections[space + i].key = key;
473*ef5ccd6cSJohn Marino }
4745796c8dcSSimon Schubert
4755796c8dcSSimon Schubert /* If these are the first file sections we can provide memory
4765796c8dcSSimon Schubert from, push the file_stratum target. */
477cf7f2e2dSJohn Marino if (!using_exec_ops)
478cf7f2e2dSJohn Marino {
479cf7f2e2dSJohn Marino using_exec_ops = 1;
4805796c8dcSSimon Schubert push_target (&exec_ops);
4815796c8dcSSimon Schubert }
4825796c8dcSSimon Schubert }
483cf7f2e2dSJohn Marino }
4845796c8dcSSimon Schubert
4855796c8dcSSimon Schubert /* Remove all target sections taken from ABFD. */
4865796c8dcSSimon Schubert
4875796c8dcSSimon Schubert void
remove_target_sections(void * key,bfd * abfd)488*ef5ccd6cSJohn Marino remove_target_sections (void *key, bfd *abfd)
4895796c8dcSSimon Schubert {
4905796c8dcSSimon Schubert struct target_section *src, *dest;
4915796c8dcSSimon Schubert struct target_section_table *table = current_target_sections;
4925796c8dcSSimon Schubert
4935796c8dcSSimon Schubert dest = table->sections;
4945796c8dcSSimon Schubert for (src = table->sections; src < table->sections_end; src++)
495*ef5ccd6cSJohn Marino if (src->key != key || src->bfd != abfd)
4965796c8dcSSimon Schubert {
4975796c8dcSSimon Schubert /* Keep this section. */
4985796c8dcSSimon Schubert if (dest < src)
4995796c8dcSSimon Schubert *dest = *src;
5005796c8dcSSimon Schubert dest++;
5015796c8dcSSimon Schubert }
5025796c8dcSSimon Schubert
5035796c8dcSSimon Schubert /* If we've dropped any sections, resize the section table. */
5045796c8dcSSimon Schubert if (dest < src)
5055796c8dcSSimon Schubert {
5065796c8dcSSimon Schubert int old_count;
5075796c8dcSSimon Schubert
5085796c8dcSSimon Schubert old_count = resize_section_table (table, dest - src);
5095796c8dcSSimon Schubert
5105796c8dcSSimon Schubert /* If we don't have any more sections to read memory from,
5115796c8dcSSimon Schubert remove the file_stratum target from the stack. */
5125796c8dcSSimon Schubert if (old_count + (dest - src) == 0)
513cf7f2e2dSJohn Marino {
514cf7f2e2dSJohn Marino struct program_space *pspace;
515cf7f2e2dSJohn Marino
516cf7f2e2dSJohn Marino ALL_PSPACES (pspace)
517cf7f2e2dSJohn Marino if (pspace->target_sections.sections
518cf7f2e2dSJohn Marino != pspace->target_sections.sections_end)
519cf7f2e2dSJohn Marino return;
520cf7f2e2dSJohn Marino
5215796c8dcSSimon Schubert unpush_target (&exec_ops);
5225796c8dcSSimon Schubert }
5235796c8dcSSimon Schubert }
524cf7f2e2dSJohn Marino }
5255796c8dcSSimon Schubert
5265796c8dcSSimon Schubert
5275796c8dcSSimon Schubert static void
bfdsec_to_vmap(struct bfd * abfd,struct bfd_section * sect,void * arg3)5285796c8dcSSimon Schubert bfdsec_to_vmap (struct bfd *abfd, struct bfd_section *sect, void *arg3)
5295796c8dcSSimon Schubert {
5305796c8dcSSimon Schubert struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *) arg3;
5315796c8dcSSimon Schubert struct vmap *vp;
5325796c8dcSSimon Schubert
5335796c8dcSSimon Schubert vp = vmap_bfd->pvmap;
5345796c8dcSSimon Schubert
5355796c8dcSSimon Schubert if ((bfd_get_section_flags (abfd, sect) & SEC_LOAD) == 0)
5365796c8dcSSimon Schubert return;
5375796c8dcSSimon Schubert
5385796c8dcSSimon Schubert if (strcmp (bfd_section_name (abfd, sect), ".text") == 0)
5395796c8dcSSimon Schubert {
5405796c8dcSSimon Schubert vp->tstart = bfd_section_vma (abfd, sect);
5415796c8dcSSimon Schubert vp->tend = vp->tstart + bfd_section_size (abfd, sect);
5425796c8dcSSimon Schubert vp->tvma = bfd_section_vma (abfd, sect);
5435796c8dcSSimon Schubert vp->toffs = sect->filepos;
5445796c8dcSSimon Schubert }
5455796c8dcSSimon Schubert else if (strcmp (bfd_section_name (abfd, sect), ".data") == 0)
5465796c8dcSSimon Schubert {
5475796c8dcSSimon Schubert vp->dstart = bfd_section_vma (abfd, sect);
5485796c8dcSSimon Schubert vp->dend = vp->dstart + bfd_section_size (abfd, sect);
5495796c8dcSSimon Schubert vp->dvma = bfd_section_vma (abfd, sect);
5505796c8dcSSimon Schubert }
5515796c8dcSSimon Schubert /* Silently ignore other types of sections. (FIXME?) */
5525796c8dcSSimon Schubert }
5535796c8dcSSimon Schubert
5545796c8dcSSimon Schubert /* Make a vmap for ABFD which might be a member of the archive ARCH.
5555796c8dcSSimon Schubert Return the new vmap. */
5565796c8dcSSimon Schubert
5575796c8dcSSimon Schubert struct vmap *
map_vmap(bfd * abfd,bfd * arch)5585796c8dcSSimon Schubert map_vmap (bfd *abfd, bfd *arch)
5595796c8dcSSimon Schubert {
5605796c8dcSSimon Schubert struct vmap_and_bfd vmap_bfd;
5615796c8dcSSimon Schubert struct vmap *vp, **vpp;
5625796c8dcSSimon Schubert
5635796c8dcSSimon Schubert vp = (struct vmap *) xmalloc (sizeof (*vp));
5645796c8dcSSimon Schubert memset ((char *) vp, '\0', sizeof (*vp));
5655796c8dcSSimon Schubert vp->nxt = 0;
5665796c8dcSSimon Schubert vp->bfd = abfd;
567*ef5ccd6cSJohn Marino gdb_bfd_ref (abfd);
5685796c8dcSSimon Schubert vp->name = bfd_get_filename (arch ? arch : abfd);
5695796c8dcSSimon Schubert vp->member = arch ? bfd_get_filename (abfd) : "";
5705796c8dcSSimon Schubert
5715796c8dcSSimon Schubert vmap_bfd.pbfd = arch;
5725796c8dcSSimon Schubert vmap_bfd.pvmap = vp;
5735796c8dcSSimon Schubert bfd_map_over_sections (abfd, bfdsec_to_vmap, &vmap_bfd);
5745796c8dcSSimon Schubert
5755796c8dcSSimon Schubert /* Find the end of the list and append. */
5765796c8dcSSimon Schubert for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt)
5775796c8dcSSimon Schubert ;
5785796c8dcSSimon Schubert *vpp = vp;
5795796c8dcSSimon Schubert
5805796c8dcSSimon Schubert return vp;
5815796c8dcSSimon Schubert }
5825796c8dcSSimon Schubert
5835796c8dcSSimon Schubert
VEC(mem_range_s)584c50c785cSJohn Marino VEC(mem_range_s) *
585c50c785cSJohn Marino section_table_available_memory (VEC(mem_range_s) *memory,
586c50c785cSJohn Marino CORE_ADDR memaddr, ULONGEST len,
587c50c785cSJohn Marino struct target_section *sections,
588c50c785cSJohn Marino struct target_section *sections_end)
589c50c785cSJohn Marino {
590c50c785cSJohn Marino struct target_section *p;
591c50c785cSJohn Marino
592c50c785cSJohn Marino for (p = sections; p < sections_end; p++)
593c50c785cSJohn Marino {
594c50c785cSJohn Marino if ((bfd_get_section_flags (p->bfd, p->the_bfd_section)
595c50c785cSJohn Marino & SEC_READONLY) == 0)
596c50c785cSJohn Marino continue;
597c50c785cSJohn Marino
598c50c785cSJohn Marino /* Copy the meta-data, adjusted. */
599c50c785cSJohn Marino if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len))
600c50c785cSJohn Marino {
601c50c785cSJohn Marino ULONGEST lo1, hi1, lo2, hi2;
602c50c785cSJohn Marino struct mem_range *r;
603c50c785cSJohn Marino
604c50c785cSJohn Marino lo1 = memaddr;
605c50c785cSJohn Marino hi1 = memaddr + len;
606c50c785cSJohn Marino
607c50c785cSJohn Marino lo2 = p->addr;
608c50c785cSJohn Marino hi2 = p->endaddr;
609c50c785cSJohn Marino
610c50c785cSJohn Marino r = VEC_safe_push (mem_range_s, memory, NULL);
611c50c785cSJohn Marino
612c50c785cSJohn Marino r->start = max (lo1, lo2);
613c50c785cSJohn Marino r->length = min (hi1, hi2) - r->start;
614c50c785cSJohn Marino }
615c50c785cSJohn Marino }
616c50c785cSJohn Marino
617c50c785cSJohn Marino return memory;
618c50c785cSJohn Marino }
619c50c785cSJohn Marino
6205796c8dcSSimon Schubert int
section_table_xfer_memory_partial(gdb_byte * readbuf,const gdb_byte * writebuf,ULONGEST offset,LONGEST len,struct target_section * sections,struct target_section * sections_end,const char * section_name)6215796c8dcSSimon Schubert section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
6225796c8dcSSimon Schubert ULONGEST offset, LONGEST len,
6235796c8dcSSimon Schubert struct target_section *sections,
6245796c8dcSSimon Schubert struct target_section *sections_end,
6255796c8dcSSimon Schubert const char *section_name)
6265796c8dcSSimon Schubert {
6275796c8dcSSimon Schubert int res;
6285796c8dcSSimon Schubert struct target_section *p;
6295796c8dcSSimon Schubert ULONGEST memaddr = offset;
6305796c8dcSSimon Schubert ULONGEST memend = memaddr + len;
6315796c8dcSSimon Schubert
6325796c8dcSSimon Schubert if (len <= 0)
633c50c785cSJohn Marino internal_error (__FILE__, __LINE__,
634c50c785cSJohn Marino _("failed internal consistency check"));
6355796c8dcSSimon Schubert
6365796c8dcSSimon Schubert for (p = sections; p < sections_end; p++)
6375796c8dcSSimon Schubert {
6385796c8dcSSimon Schubert if (section_name && strcmp (section_name, p->the_bfd_section->name) != 0)
639c50c785cSJohn Marino continue; /* not the section we need. */
6405796c8dcSSimon Schubert if (memaddr >= p->addr)
6415796c8dcSSimon Schubert {
6425796c8dcSSimon Schubert if (memend <= p->endaddr)
6435796c8dcSSimon Schubert {
6445796c8dcSSimon Schubert /* Entire transfer is within this section. */
6455796c8dcSSimon Schubert if (writebuf)
6465796c8dcSSimon Schubert res = bfd_set_section_contents (p->bfd, p->the_bfd_section,
6475796c8dcSSimon Schubert writebuf, memaddr - p->addr,
6485796c8dcSSimon Schubert len);
6495796c8dcSSimon Schubert else
6505796c8dcSSimon Schubert res = bfd_get_section_contents (p->bfd, p->the_bfd_section,
6515796c8dcSSimon Schubert readbuf, memaddr - p->addr,
6525796c8dcSSimon Schubert len);
6535796c8dcSSimon Schubert return (res != 0) ? len : 0;
6545796c8dcSSimon Schubert }
6555796c8dcSSimon Schubert else if (memaddr >= p->endaddr)
6565796c8dcSSimon Schubert {
6575796c8dcSSimon Schubert /* This section ends before the transfer starts. */
6585796c8dcSSimon Schubert continue;
6595796c8dcSSimon Schubert }
6605796c8dcSSimon Schubert else
6615796c8dcSSimon Schubert {
6625796c8dcSSimon Schubert /* This section overlaps the transfer. Just do half. */
6635796c8dcSSimon Schubert len = p->endaddr - memaddr;
6645796c8dcSSimon Schubert if (writebuf)
6655796c8dcSSimon Schubert res = bfd_set_section_contents (p->bfd, p->the_bfd_section,
6665796c8dcSSimon Schubert writebuf, memaddr - p->addr,
6675796c8dcSSimon Schubert len);
6685796c8dcSSimon Schubert else
6695796c8dcSSimon Schubert res = bfd_get_section_contents (p->bfd, p->the_bfd_section,
6705796c8dcSSimon Schubert readbuf, memaddr - p->addr,
6715796c8dcSSimon Schubert len);
6725796c8dcSSimon Schubert return (res != 0) ? len : 0;
6735796c8dcSSimon Schubert }
6745796c8dcSSimon Schubert }
6755796c8dcSSimon Schubert }
6765796c8dcSSimon Schubert
677c50c785cSJohn Marino return 0; /* We can't help. */
6785796c8dcSSimon Schubert }
6795796c8dcSSimon Schubert
680*ef5ccd6cSJohn Marino static struct target_section_table *
exec_get_section_table(struct target_ops * ops)6815796c8dcSSimon Schubert exec_get_section_table (struct target_ops *ops)
6825796c8dcSSimon Schubert {
6835796c8dcSSimon Schubert return current_target_sections;
6845796c8dcSSimon Schubert }
6855796c8dcSSimon Schubert
6865796c8dcSSimon Schubert static LONGEST
exec_xfer_partial(struct target_ops * ops,enum target_object object,const char * annex,gdb_byte * readbuf,const gdb_byte * writebuf,ULONGEST offset,LONGEST len)6875796c8dcSSimon Schubert exec_xfer_partial (struct target_ops *ops, enum target_object object,
6885796c8dcSSimon Schubert const char *annex, gdb_byte *readbuf,
6895796c8dcSSimon Schubert const gdb_byte *writebuf,
6905796c8dcSSimon Schubert ULONGEST offset, LONGEST len)
6915796c8dcSSimon Schubert {
6925796c8dcSSimon Schubert struct target_section_table *table = target_get_section_table (ops);
6935796c8dcSSimon Schubert
6945796c8dcSSimon Schubert if (object == TARGET_OBJECT_MEMORY)
6955796c8dcSSimon Schubert return section_table_xfer_memory_partial (readbuf, writebuf,
6965796c8dcSSimon Schubert offset, len,
6975796c8dcSSimon Schubert table->sections,
6985796c8dcSSimon Schubert table->sections_end,
6995796c8dcSSimon Schubert NULL);
7005796c8dcSSimon Schubert else
7015796c8dcSSimon Schubert return -1;
7025796c8dcSSimon Schubert }
7035796c8dcSSimon Schubert
7045796c8dcSSimon Schubert
7055796c8dcSSimon Schubert void
print_section_info(struct target_section_table * t,bfd * abfd)7065796c8dcSSimon Schubert print_section_info (struct target_section_table *t, bfd *abfd)
7075796c8dcSSimon Schubert {
7085796c8dcSSimon Schubert struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
7095796c8dcSSimon Schubert struct target_section *p;
7105796c8dcSSimon Schubert /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */
7115796c8dcSSimon Schubert int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
7125796c8dcSSimon Schubert
7135796c8dcSSimon Schubert printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
7145796c8dcSSimon Schubert wrap_here (" ");
7155796c8dcSSimon Schubert printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
7165796c8dcSSimon Schubert if (abfd == exec_bfd)
717cf7f2e2dSJohn Marino {
718c50c785cSJohn Marino /* gcc-3.4 does not like the initialization in
719c50c785cSJohn Marino <p == t->sections_end>. */
720cf7f2e2dSJohn Marino bfd_vma displacement = 0;
721cf7f2e2dSJohn Marino bfd_vma entry_point;
722cf7f2e2dSJohn Marino
723cf7f2e2dSJohn Marino for (p = t->sections; p < t->sections_end; p++)
724cf7f2e2dSJohn Marino {
725cf7f2e2dSJohn Marino asection *asect = p->the_bfd_section;
726cf7f2e2dSJohn Marino
727cf7f2e2dSJohn Marino if ((bfd_get_section_flags (abfd, asect) & (SEC_ALLOC | SEC_LOAD))
728cf7f2e2dSJohn Marino != (SEC_ALLOC | SEC_LOAD))
729cf7f2e2dSJohn Marino continue;
730cf7f2e2dSJohn Marino
731cf7f2e2dSJohn Marino if (bfd_get_section_vma (abfd, asect) <= abfd->start_address
732cf7f2e2dSJohn Marino && abfd->start_address < (bfd_get_section_vma (abfd, asect)
733cf7f2e2dSJohn Marino + bfd_get_section_size (asect)))
734cf7f2e2dSJohn Marino {
735cf7f2e2dSJohn Marino displacement = p->addr - bfd_get_section_vma (abfd, asect);
736cf7f2e2dSJohn Marino break;
737cf7f2e2dSJohn Marino }
738cf7f2e2dSJohn Marino }
739cf7f2e2dSJohn Marino if (p == t->sections_end)
740c50c785cSJohn Marino warning (_("Cannot find section for the entry point of %s."),
741cf7f2e2dSJohn Marino bfd_get_filename (abfd));
742cf7f2e2dSJohn Marino
743cf7f2e2dSJohn Marino entry_point = gdbarch_addr_bits_remove (gdbarch,
744cf7f2e2dSJohn Marino bfd_get_start_address (abfd)
745cf7f2e2dSJohn Marino + displacement);
7465796c8dcSSimon Schubert printf_filtered (_("\tEntry point: %s\n"),
747cf7f2e2dSJohn Marino paddress (gdbarch, entry_point));
748cf7f2e2dSJohn Marino }
7495796c8dcSSimon Schubert for (p = t->sections; p < t->sections_end; p++)
7505796c8dcSSimon Schubert {
7515796c8dcSSimon Schubert printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
7525796c8dcSSimon Schubert printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
7535796c8dcSSimon Schubert
7545796c8dcSSimon Schubert /* FIXME: A format of "08l" is not wide enough for file offsets
7555796c8dcSSimon Schubert larger than 4GB. OTOH, making it "016l" isn't desirable either
7565796c8dcSSimon Schubert since most output will then be much wider than necessary. It
7575796c8dcSSimon Schubert may make sense to test the size of the file and choose the
7585796c8dcSSimon Schubert format string accordingly. */
7595796c8dcSSimon Schubert /* FIXME: i18n: Need to rewrite this sentence. */
7605796c8dcSSimon Schubert if (info_verbose)
7615796c8dcSSimon Schubert printf_filtered (" @ %s",
7625796c8dcSSimon Schubert hex_string_custom (p->the_bfd_section->filepos, 8));
763c50c785cSJohn Marino printf_filtered (" is %s", bfd_section_name (p->bfd,
764c50c785cSJohn Marino p->the_bfd_section));
7655796c8dcSSimon Schubert if (p->bfd != abfd)
7665796c8dcSSimon Schubert printf_filtered (" in %s", bfd_get_filename (p->bfd));
7675796c8dcSSimon Schubert printf_filtered ("\n");
7685796c8dcSSimon Schubert }
7695796c8dcSSimon Schubert }
7705796c8dcSSimon Schubert
7715796c8dcSSimon Schubert static void
exec_files_info(struct target_ops * t)7725796c8dcSSimon Schubert exec_files_info (struct target_ops *t)
7735796c8dcSSimon Schubert {
774*ef5ccd6cSJohn Marino if (exec_bfd)
7755796c8dcSSimon Schubert print_section_info (current_target_sections, exec_bfd);
776*ef5ccd6cSJohn Marino else
777*ef5ccd6cSJohn Marino puts_filtered (_("\t<no file loaded>\n"));
7785796c8dcSSimon Schubert
7795796c8dcSSimon Schubert if (vmap)
7805796c8dcSSimon Schubert {
781*ef5ccd6cSJohn Marino int addr_size = gdbarch_addr_bit (target_gdbarch ()) / 8;
7825796c8dcSSimon Schubert struct vmap *vp;
7835796c8dcSSimon Schubert
7845796c8dcSSimon Schubert printf_unfiltered (_("\tMapping info for file `%s'.\n"), vmap->name);
7855796c8dcSSimon Schubert printf_unfiltered ("\t %*s %*s %*s %*s %8.8s %s\n",
7865796c8dcSSimon Schubert addr_size * 2, "tstart",
7875796c8dcSSimon Schubert addr_size * 2, "tend",
7885796c8dcSSimon Schubert addr_size * 2, "dstart",
7895796c8dcSSimon Schubert addr_size * 2, "dend",
7905796c8dcSSimon Schubert "section",
7915796c8dcSSimon Schubert "file(member)");
7925796c8dcSSimon Schubert
7935796c8dcSSimon Schubert for (vp = vmap; vp; vp = vp->nxt)
7945796c8dcSSimon Schubert printf_unfiltered ("\t0x%s 0x%s 0x%s 0x%s %s%s%s%s\n",
7955796c8dcSSimon Schubert phex (vp->tstart, addr_size),
7965796c8dcSSimon Schubert phex (vp->tend, addr_size),
7975796c8dcSSimon Schubert phex (vp->dstart, addr_size),
7985796c8dcSSimon Schubert phex (vp->dend, addr_size),
7995796c8dcSSimon Schubert vp->name,
8005796c8dcSSimon Schubert *vp->member ? "(" : "", vp->member,
8015796c8dcSSimon Schubert *vp->member ? ")" : "");
8025796c8dcSSimon Schubert }
8035796c8dcSSimon Schubert }
8045796c8dcSSimon Schubert
8055796c8dcSSimon Schubert static void
set_section_command(char * args,int from_tty)8065796c8dcSSimon Schubert set_section_command (char *args, int from_tty)
8075796c8dcSSimon Schubert {
8085796c8dcSSimon Schubert struct target_section *p;
8095796c8dcSSimon Schubert char *secname;
8105796c8dcSSimon Schubert unsigned seclen;
8115796c8dcSSimon Schubert unsigned long secaddr;
8125796c8dcSSimon Schubert char secprint[100];
8135796c8dcSSimon Schubert long offset;
8145796c8dcSSimon Schubert struct target_section_table *table;
8155796c8dcSSimon Schubert
8165796c8dcSSimon Schubert if (args == 0)
8175796c8dcSSimon Schubert error (_("Must specify section name and its virtual address"));
8185796c8dcSSimon Schubert
819c50c785cSJohn Marino /* Parse out section name. */
8205796c8dcSSimon Schubert for (secname = args; !isspace (*args); args++);
8215796c8dcSSimon Schubert seclen = args - secname;
8225796c8dcSSimon Schubert
823c50c785cSJohn Marino /* Parse out new virtual address. */
8245796c8dcSSimon Schubert secaddr = parse_and_eval_address (args);
8255796c8dcSSimon Schubert
8265796c8dcSSimon Schubert table = current_target_sections;
8275796c8dcSSimon Schubert for (p = table->sections; p < table->sections_end; p++)
8285796c8dcSSimon Schubert {
829*ef5ccd6cSJohn Marino if (!strncmp (secname, bfd_section_name (p->bfd,
830c50c785cSJohn Marino p->the_bfd_section), seclen)
831*ef5ccd6cSJohn Marino && bfd_section_name (p->bfd, p->the_bfd_section)[seclen] == '\0')
8325796c8dcSSimon Schubert {
8335796c8dcSSimon Schubert offset = secaddr - p->addr;
8345796c8dcSSimon Schubert p->addr += offset;
8355796c8dcSSimon Schubert p->endaddr += offset;
8365796c8dcSSimon Schubert if (from_tty)
8375796c8dcSSimon Schubert exec_files_info (&exec_ops);
8385796c8dcSSimon Schubert return;
8395796c8dcSSimon Schubert }
8405796c8dcSSimon Schubert }
8415796c8dcSSimon Schubert if (seclen >= sizeof (secprint))
8425796c8dcSSimon Schubert seclen = sizeof (secprint) - 1;
8435796c8dcSSimon Schubert strncpy (secprint, secname, seclen);
8445796c8dcSSimon Schubert secprint[seclen] = '\0';
8455796c8dcSSimon Schubert error (_("Section %s not found"), secprint);
8465796c8dcSSimon Schubert }
8475796c8dcSSimon Schubert
8485796c8dcSSimon Schubert /* If we can find a section in FILENAME with BFD index INDEX, adjust
8495796c8dcSSimon Schubert it to ADDRESS. */
8505796c8dcSSimon Schubert
8515796c8dcSSimon Schubert void
exec_set_section_address(const char * filename,int index,CORE_ADDR address)8525796c8dcSSimon Schubert exec_set_section_address (const char *filename, int index, CORE_ADDR address)
8535796c8dcSSimon Schubert {
8545796c8dcSSimon Schubert struct target_section *p;
8555796c8dcSSimon Schubert struct target_section_table *table;
8565796c8dcSSimon Schubert
8575796c8dcSSimon Schubert table = current_target_sections;
8585796c8dcSSimon Schubert for (p = table->sections; p < table->sections_end; p++)
8595796c8dcSSimon Schubert {
860c50c785cSJohn Marino if (filename_cmp (filename, p->bfd->filename) == 0
8615796c8dcSSimon Schubert && index == p->the_bfd_section->index)
8625796c8dcSSimon Schubert {
8635796c8dcSSimon Schubert p->endaddr += address - p->addr;
8645796c8dcSSimon Schubert p->addr = address;
8655796c8dcSSimon Schubert }
8665796c8dcSSimon Schubert }
8675796c8dcSSimon Schubert }
8685796c8dcSSimon Schubert
8695796c8dcSSimon Schubert /* If mourn is being called in all the right places, this could be say
8705796c8dcSSimon Schubert `gdb internal error' (since generic_mourn calls
8715796c8dcSSimon Schubert breakpoint_init_inferior). */
8725796c8dcSSimon Schubert
8735796c8dcSSimon Schubert static int
ignore(struct gdbarch * gdbarch,struct bp_target_info * bp_tgt)8745796c8dcSSimon Schubert ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
8755796c8dcSSimon Schubert {
8765796c8dcSSimon Schubert return 0;
8775796c8dcSSimon Schubert }
8785796c8dcSSimon Schubert
8795796c8dcSSimon Schubert static int
exec_has_memory(struct target_ops * ops)8805796c8dcSSimon Schubert exec_has_memory (struct target_ops *ops)
8815796c8dcSSimon Schubert {
8825796c8dcSSimon Schubert /* We can provide memory if we have any file/target sections to read
8835796c8dcSSimon Schubert from. */
8845796c8dcSSimon Schubert return (current_target_sections->sections
8855796c8dcSSimon Schubert != current_target_sections->sections_end);
8865796c8dcSSimon Schubert }
8875796c8dcSSimon Schubert
8885796c8dcSSimon Schubert /* Find mapped memory. */
8895796c8dcSSimon Schubert
8905796c8dcSSimon Schubert extern void
exec_set_find_memory_regions(int (* func)(find_memory_region_ftype,void *))891c50c785cSJohn Marino exec_set_find_memory_regions (int (*func) (find_memory_region_ftype, void *))
8925796c8dcSSimon Schubert {
8935796c8dcSSimon Schubert exec_ops.to_find_memory_regions = func;
8945796c8dcSSimon Schubert }
8955796c8dcSSimon Schubert
8965796c8dcSSimon Schubert static char *exec_make_note_section (bfd *, int *);
8975796c8dcSSimon Schubert
8985796c8dcSSimon Schubert /* Fill in the exec file target vector. Very few entries need to be
8995796c8dcSSimon Schubert defined. */
9005796c8dcSSimon Schubert
9015796c8dcSSimon Schubert static void
init_exec_ops(void)9025796c8dcSSimon Schubert init_exec_ops (void)
9035796c8dcSSimon Schubert {
9045796c8dcSSimon Schubert exec_ops.to_shortname = "exec";
9055796c8dcSSimon Schubert exec_ops.to_longname = "Local exec file";
9065796c8dcSSimon Schubert exec_ops.to_doc = "Use an executable file as a target.\n\
9075796c8dcSSimon Schubert Specify the filename of the executable file.";
9085796c8dcSSimon Schubert exec_ops.to_open = exec_open;
909cf7f2e2dSJohn Marino exec_ops.to_close = exec_close_1;
9105796c8dcSSimon Schubert exec_ops.to_attach = find_default_attach;
9115796c8dcSSimon Schubert exec_ops.to_xfer_partial = exec_xfer_partial;
9125796c8dcSSimon Schubert exec_ops.to_get_section_table = exec_get_section_table;
9135796c8dcSSimon Schubert exec_ops.to_files_info = exec_files_info;
9145796c8dcSSimon Schubert exec_ops.to_insert_breakpoint = ignore;
9155796c8dcSSimon Schubert exec_ops.to_remove_breakpoint = ignore;
9165796c8dcSSimon Schubert exec_ops.to_create_inferior = find_default_create_inferior;
9175796c8dcSSimon Schubert exec_ops.to_stratum = file_stratum;
9185796c8dcSSimon Schubert exec_ops.to_has_memory = exec_has_memory;
9195796c8dcSSimon Schubert exec_ops.to_make_corefile_notes = exec_make_note_section;
9205796c8dcSSimon Schubert exec_ops.to_magic = OPS_MAGIC;
9215796c8dcSSimon Schubert }
9225796c8dcSSimon Schubert
9235796c8dcSSimon Schubert void
_initialize_exec(void)9245796c8dcSSimon Schubert _initialize_exec (void)
9255796c8dcSSimon Schubert {
9265796c8dcSSimon Schubert struct cmd_list_element *c;
9275796c8dcSSimon Schubert
9285796c8dcSSimon Schubert init_exec_ops ();
9295796c8dcSSimon Schubert
9305796c8dcSSimon Schubert if (!dbx_commands)
9315796c8dcSSimon Schubert {
9325796c8dcSSimon Schubert c = add_cmd ("file", class_files, file_command, _("\
9335796c8dcSSimon Schubert Use FILE as program to be debugged.\n\
9345796c8dcSSimon Schubert It is read for its symbols, for getting the contents of pure memory,\n\
9355796c8dcSSimon Schubert and it is the program executed when you use the `run' command.\n\
9365796c8dcSSimon Schubert If FILE cannot be found as specified, your execution directory path\n\
9375796c8dcSSimon Schubert ($PATH) is searched for a command of that name.\n\
9385796c8dcSSimon Schubert No arg means to have no executable file and no symbols."), &cmdlist);
9395796c8dcSSimon Schubert set_cmd_completer (c, filename_completer);
9405796c8dcSSimon Schubert }
9415796c8dcSSimon Schubert
9425796c8dcSSimon Schubert c = add_cmd ("exec-file", class_files, exec_file_command, _("\
9435796c8dcSSimon Schubert Use FILE as program for getting contents of pure memory.\n\
9445796c8dcSSimon Schubert If FILE cannot be found as specified, your execution directory path\n\
9455796c8dcSSimon Schubert is searched for a command of that name.\n\
9465796c8dcSSimon Schubert No arg means have no executable file."), &cmdlist);
9475796c8dcSSimon Schubert set_cmd_completer (c, filename_completer);
9485796c8dcSSimon Schubert
9495796c8dcSSimon Schubert add_com ("section", class_files, set_section_command, _("\
9505796c8dcSSimon Schubert Change the base address of section SECTION of the exec file to ADDR.\n\
9515796c8dcSSimon Schubert This can be used if the exec file does not contain section addresses,\n\
9525796c8dcSSimon Schubert (such as in the a.out format), or when the addresses specified in the\n\
9535796c8dcSSimon Schubert file itself are wrong. Each section must be changed separately. The\n\
9545796c8dcSSimon Schubert ``info files'' command lists all the sections and their addresses."));
9555796c8dcSSimon Schubert
9565796c8dcSSimon Schubert add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
9575796c8dcSSimon Schubert Set writing into executable and core files."), _("\
9585796c8dcSSimon Schubert Show writing into executable and core files."), NULL,
9595796c8dcSSimon Schubert NULL,
9605796c8dcSSimon Schubert show_write_files,
9615796c8dcSSimon Schubert &setlist, &showlist);
9625796c8dcSSimon Schubert
9635796c8dcSSimon Schubert add_target (&exec_ops);
9645796c8dcSSimon Schubert }
9655796c8dcSSimon Schubert
9665796c8dcSSimon Schubert static char *
exec_make_note_section(bfd * obfd,int * note_size)9675796c8dcSSimon Schubert exec_make_note_section (bfd *obfd, int *note_size)
9685796c8dcSSimon Schubert {
9695796c8dcSSimon Schubert error (_("Can't create a corefile"));
9705796c8dcSSimon Schubert }
971