15796c8dcSSimon Schubert /* Work with executable files, for GDB. 25796c8dcSSimon Schubert 35796c8dcSSimon Schubert Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 4c50c785cSJohn Marino 1998, 1999, 2000, 2001, 2002, 2003, 2007, 2008, 2009, 2010, 2011 55796c8dcSSimon Schubert Free Software Foundation, Inc. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert This file is part of GDB. 85796c8dcSSimon Schubert 95796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 105796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 115796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 125796c8dcSSimon Schubert (at your option) any later version. 135796c8dcSSimon Schubert 145796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 155796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 165796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 175796c8dcSSimon Schubert GNU General Public License for more details. 185796c8dcSSimon Schubert 195796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 205796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 215796c8dcSSimon Schubert 225796c8dcSSimon Schubert #include "defs.h" 235796c8dcSSimon Schubert #include "frame.h" 245796c8dcSSimon Schubert #include "inferior.h" 255796c8dcSSimon Schubert #include "target.h" 265796c8dcSSimon Schubert #include "gdbcmd.h" 275796c8dcSSimon Schubert #include "language.h" 28c50c785cSJohn Marino #include "filenames.h" 295796c8dcSSimon Schubert #include "symfile.h" 305796c8dcSSimon Schubert #include "objfiles.h" 315796c8dcSSimon Schubert #include "completer.h" 325796c8dcSSimon Schubert #include "value.h" 335796c8dcSSimon Schubert #include "exec.h" 345796c8dcSSimon Schubert #include "observer.h" 355796c8dcSSimon Schubert #include "arch-utils.h" 36cf7f2e2dSJohn Marino #include "gdbthread.h" 37cf7f2e2dSJohn Marino #include "progspace.h" 385796c8dcSSimon Schubert 395796c8dcSSimon Schubert #include <fcntl.h> 405796c8dcSSimon Schubert #include "readline/readline.h" 415796c8dcSSimon Schubert #include "gdb_string.h" 425796c8dcSSimon Schubert 435796c8dcSSimon Schubert #include "gdbcore.h" 445796c8dcSSimon Schubert 455796c8dcSSimon Schubert #include <ctype.h> 465796c8dcSSimon Schubert #include "gdb_stat.h" 475796c8dcSSimon Schubert 485796c8dcSSimon Schubert #include "xcoffsolib.h" 495796c8dcSSimon Schubert 505796c8dcSSimon Schubert struct vmap *map_vmap (bfd *, bfd *); 515796c8dcSSimon Schubert 525796c8dcSSimon Schubert void (*deprecated_file_changed_hook) (char *); 535796c8dcSSimon Schubert 545796c8dcSSimon Schubert /* Prototypes for local functions */ 555796c8dcSSimon Schubert 565796c8dcSSimon Schubert static void file_command (char *, int); 575796c8dcSSimon Schubert 585796c8dcSSimon Schubert static void set_section_command (char *, int); 595796c8dcSSimon Schubert 605796c8dcSSimon Schubert static void exec_files_info (struct target_ops *); 615796c8dcSSimon Schubert 625796c8dcSSimon Schubert static void init_exec_ops (void); 635796c8dcSSimon Schubert 645796c8dcSSimon Schubert void _initialize_exec (void); 655796c8dcSSimon Schubert 665796c8dcSSimon Schubert /* The target vector for executable files. */ 675796c8dcSSimon Schubert 685796c8dcSSimon Schubert struct target_ops exec_ops; 695796c8dcSSimon Schubert 70cf7f2e2dSJohn Marino /* True if the exec target is pushed on the stack. */ 71cf7f2e2dSJohn Marino static int using_exec_ops; 725796c8dcSSimon Schubert 735796c8dcSSimon Schubert /* Whether to open exec and core files read-only or read-write. */ 745796c8dcSSimon Schubert 755796c8dcSSimon Schubert int write_files = 0; 765796c8dcSSimon Schubert static void 775796c8dcSSimon Schubert show_write_files (struct ui_file *file, int from_tty, 785796c8dcSSimon Schubert struct cmd_list_element *c, const char *value) 795796c8dcSSimon Schubert { 805796c8dcSSimon Schubert fprintf_filtered (file, _("Writing into executable and core files is %s.\n"), 815796c8dcSSimon Schubert value); 825796c8dcSSimon Schubert } 835796c8dcSSimon Schubert 845796c8dcSSimon Schubert 855796c8dcSSimon Schubert struct vmap *vmap; 865796c8dcSSimon Schubert 875796c8dcSSimon Schubert static void 885796c8dcSSimon Schubert exec_open (char *args, int from_tty) 895796c8dcSSimon Schubert { 905796c8dcSSimon Schubert target_preopen (from_tty); 915796c8dcSSimon Schubert exec_file_attach (args, from_tty); 925796c8dcSSimon Schubert } 935796c8dcSSimon Schubert 945796c8dcSSimon Schubert /* Close and clear exec_bfd. If we end up with no target sections to 955796c8dcSSimon Schubert read memory from, this unpushes the exec_ops target. */ 965796c8dcSSimon Schubert 97cf7f2e2dSJohn Marino void 98cf7f2e2dSJohn Marino exec_close (void) 995796c8dcSSimon Schubert { 1005796c8dcSSimon Schubert if (exec_bfd) 1015796c8dcSSimon Schubert { 1025796c8dcSSimon Schubert bfd *abfd = exec_bfd; 1035796c8dcSSimon Schubert char *name = bfd_get_filename (abfd); 1045796c8dcSSimon Schubert 105cf7f2e2dSJohn Marino gdb_bfd_close_or_warn (abfd); 1065796c8dcSSimon Schubert xfree (name); 1075796c8dcSSimon Schubert 1085796c8dcSSimon Schubert /* Removing target sections may close the exec_ops target. 1095796c8dcSSimon Schubert Clear exec_bfd before doing so to prevent recursion. */ 1105796c8dcSSimon Schubert exec_bfd = NULL; 1115796c8dcSSimon Schubert exec_bfd_mtime = 0; 1125796c8dcSSimon Schubert 1135796c8dcSSimon Schubert remove_target_sections (abfd); 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 int need_symtab_cleanup = 0; 1245796c8dcSSimon Schubert struct vmap *vp, *nxt; 1255796c8dcSSimon Schubert 126cf7f2e2dSJohn Marino using_exec_ops = 0; 127cf7f2e2dSJohn Marino 1285796c8dcSSimon Schubert for (nxt = vmap; nxt != NULL;) 1295796c8dcSSimon Schubert { 1305796c8dcSSimon Schubert vp = nxt; 1315796c8dcSSimon Schubert nxt = vp->nxt; 1325796c8dcSSimon Schubert 1335796c8dcSSimon Schubert /* if there is an objfile associated with this bfd, 1345796c8dcSSimon Schubert free_objfile() will do proper cleanup of objfile *and* bfd. */ 1355796c8dcSSimon Schubert 1365796c8dcSSimon Schubert if (vp->objfile) 1375796c8dcSSimon Schubert { 1385796c8dcSSimon Schubert free_objfile (vp->objfile); 1395796c8dcSSimon Schubert need_symtab_cleanup = 1; 1405796c8dcSSimon Schubert } 1415796c8dcSSimon Schubert else if (vp->bfd != exec_bfd) 1425796c8dcSSimon Schubert /* FIXME-leak: We should be freeing vp->name too, I think. */ 143cf7f2e2dSJohn Marino gdb_bfd_close_or_warn (vp->bfd); 1445796c8dcSSimon Schubert 1455796c8dcSSimon Schubert xfree (vp); 1465796c8dcSSimon Schubert } 1475796c8dcSSimon Schubert 1485796c8dcSSimon Schubert vmap = NULL; 1495796c8dcSSimon Schubert 150cf7f2e2dSJohn Marino { 151cf7f2e2dSJohn Marino struct program_space *ss; 152cf7f2e2dSJohn Marino struct cleanup *old_chain; 153cf7f2e2dSJohn Marino 154cf7f2e2dSJohn Marino old_chain = save_current_program_space (); 155cf7f2e2dSJohn Marino ALL_PSPACES (ss) 156cf7f2e2dSJohn Marino { 157cf7f2e2dSJohn Marino set_current_program_space (ss); 158cf7f2e2dSJohn Marino 1595796c8dcSSimon Schubert /* Delete all target sections. */ 1605796c8dcSSimon Schubert resize_section_table 1615796c8dcSSimon Schubert (current_target_sections, 1625796c8dcSSimon Schubert -resize_section_table (current_target_sections, 0)); 1635796c8dcSSimon Schubert 164cf7f2e2dSJohn Marino exec_close (); 165cf7f2e2dSJohn Marino } 166cf7f2e2dSJohn Marino 167cf7f2e2dSJohn Marino do_cleanups (old_chain); 168cf7f2e2dSJohn Marino } 1695796c8dcSSimon Schubert } 1705796c8dcSSimon Schubert 1715796c8dcSSimon Schubert void 1725796c8dcSSimon Schubert exec_file_clear (int from_tty) 1735796c8dcSSimon Schubert { 1745796c8dcSSimon Schubert /* Remove exec file. */ 175cf7f2e2dSJohn Marino exec_close (); 1765796c8dcSSimon Schubert 1775796c8dcSSimon Schubert if (from_tty) 1785796c8dcSSimon Schubert printf_unfiltered (_("No executable file now.\n")); 1795796c8dcSSimon Schubert } 1805796c8dcSSimon Schubert 1815796c8dcSSimon Schubert /* Set FILENAME as the new exec file. 1825796c8dcSSimon Schubert 1835796c8dcSSimon Schubert This function is intended to be behave essentially the same 1845796c8dcSSimon Schubert as exec_file_command, except that the latter will detect when 1855796c8dcSSimon Schubert a target is being debugged, and will ask the user whether it 1865796c8dcSSimon Schubert should be shut down first. (If the answer is "no", then the 1875796c8dcSSimon Schubert new file is ignored.) 1885796c8dcSSimon Schubert 1895796c8dcSSimon Schubert This file is used by exec_file_command, to do the work of opening 1905796c8dcSSimon Schubert and processing the exec file after any prompting has happened. 1915796c8dcSSimon Schubert 1925796c8dcSSimon Schubert And, it is used by child_attach, when the attach command was 1935796c8dcSSimon Schubert given a pid but not a exec pathname, and the attach command could 1945796c8dcSSimon Schubert figure out the pathname from the pid. (In this case, we shouldn't 1955796c8dcSSimon Schubert ask the user whether the current target should be shut down -- 1965796c8dcSSimon Schubert we're supplying the exec pathname late for good reason.) */ 1975796c8dcSSimon Schubert 1985796c8dcSSimon Schubert void 1995796c8dcSSimon Schubert exec_file_attach (char *filename, int from_tty) 2005796c8dcSSimon Schubert { 2015796c8dcSSimon Schubert /* Remove any previous exec file. */ 202cf7f2e2dSJohn Marino exec_close (); 2035796c8dcSSimon Schubert 2045796c8dcSSimon Schubert /* Now open and digest the file the user requested, if any. */ 2055796c8dcSSimon Schubert 2065796c8dcSSimon Schubert if (!filename) 2075796c8dcSSimon Schubert { 2085796c8dcSSimon Schubert if (from_tty) 2095796c8dcSSimon Schubert printf_unfiltered (_("No executable file now.\n")); 2105796c8dcSSimon Schubert 2115796c8dcSSimon Schubert set_gdbarch_from_file (NULL); 2125796c8dcSSimon Schubert } 2135796c8dcSSimon Schubert else 2145796c8dcSSimon Schubert { 2155796c8dcSSimon Schubert struct cleanup *cleanups; 2165796c8dcSSimon Schubert char *scratch_pathname; 2175796c8dcSSimon Schubert int scratch_chan; 2185796c8dcSSimon Schubert struct target_section *sections = NULL, *sections_end = NULL; 219cf7f2e2dSJohn Marino char **matching; 2205796c8dcSSimon Schubert 2215796c8dcSSimon Schubert scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename, 2225796c8dcSSimon Schubert write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY, 2235796c8dcSSimon Schubert &scratch_pathname); 2245796c8dcSSimon Schubert #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__) 2255796c8dcSSimon Schubert if (scratch_chan < 0) 2265796c8dcSSimon Schubert { 2275796c8dcSSimon Schubert char *exename = alloca (strlen (filename) + 5); 228cf7f2e2dSJohn Marino 2295796c8dcSSimon Schubert strcat (strcpy (exename, filename), ".exe"); 2305796c8dcSSimon Schubert scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename, 2315796c8dcSSimon Schubert write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY, 2325796c8dcSSimon Schubert &scratch_pathname); 2335796c8dcSSimon Schubert } 2345796c8dcSSimon Schubert #endif 2355796c8dcSSimon Schubert if (scratch_chan < 0) 2365796c8dcSSimon Schubert perror_with_name (filename); 2375796c8dcSSimon Schubert exec_bfd = bfd_fopen (scratch_pathname, gnutarget, 2385796c8dcSSimon Schubert write_files ? FOPEN_RUB : FOPEN_RB, 2395796c8dcSSimon Schubert scratch_chan); 2405796c8dcSSimon Schubert 2415796c8dcSSimon Schubert if (!exec_bfd) 2425796c8dcSSimon Schubert { 2435796c8dcSSimon Schubert close (scratch_chan); 2445796c8dcSSimon Schubert error (_("\"%s\": could not open as an executable file: %s"), 2455796c8dcSSimon Schubert scratch_pathname, bfd_errmsg (bfd_get_error ())); 2465796c8dcSSimon Schubert } 2475796c8dcSSimon Schubert 2485796c8dcSSimon Schubert /* At this point, scratch_pathname and exec_bfd->name both point to the 2495796c8dcSSimon Schubert same malloc'd string. However exec_close() will attempt to free it 2505796c8dcSSimon Schubert via the exec_bfd->name pointer, so we need to make another copy and 2515796c8dcSSimon Schubert leave exec_bfd as the new owner of the original copy. */ 2525796c8dcSSimon Schubert scratch_pathname = xstrdup (scratch_pathname); 2535796c8dcSSimon Schubert cleanups = make_cleanup (xfree, 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. */ 2995796c8dcSSimon Schubert add_target_sections (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 3935796c8dcSSimon Schubert (*table_pp)->bfd = abfd; 3945796c8dcSSimon Schubert (*table_pp)->the_bfd_section = asect; 3955796c8dcSSimon Schubert (*table_pp)->addr = bfd_section_vma (abfd, asect); 3965796c8dcSSimon Schubert (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect); 3975796c8dcSSimon Schubert (*table_pp)++; 3985796c8dcSSimon Schubert } 3995796c8dcSSimon Schubert 4005796c8dcSSimon Schubert int 4015796c8dcSSimon Schubert resize_section_table (struct target_section_table *table, int num_added) 4025796c8dcSSimon Schubert { 4035796c8dcSSimon Schubert int old_count; 4045796c8dcSSimon Schubert int new_count; 4055796c8dcSSimon Schubert 4065796c8dcSSimon Schubert old_count = table->sections_end - table->sections; 4075796c8dcSSimon Schubert 408*7f357fefSJohn Marino if (!num_added) 409*7f357fefSJohn Marino return old_count; 410*7f357fefSJohn Marino 4115796c8dcSSimon Schubert new_count = num_added + old_count; 4125796c8dcSSimon Schubert 4135796c8dcSSimon Schubert if (new_count) 4145796c8dcSSimon Schubert { 4155796c8dcSSimon Schubert table->sections = xrealloc (table->sections, 4165796c8dcSSimon Schubert sizeof (struct target_section) * new_count); 4175796c8dcSSimon Schubert table->sections_end = table->sections + new_count; 4185796c8dcSSimon Schubert } 4195796c8dcSSimon Schubert else 4205796c8dcSSimon Schubert { 4215796c8dcSSimon Schubert xfree (table->sections); 4225796c8dcSSimon Schubert table->sections = table->sections_end = NULL; 4235796c8dcSSimon Schubert } 4245796c8dcSSimon Schubert 4255796c8dcSSimon Schubert return old_count; 4265796c8dcSSimon Schubert } 4275796c8dcSSimon Schubert 4285796c8dcSSimon Schubert /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR. 4295796c8dcSSimon Schubert Returns 0 if OK, 1 on error. */ 4305796c8dcSSimon Schubert 4315796c8dcSSimon Schubert int 4325796c8dcSSimon Schubert build_section_table (struct bfd *some_bfd, struct target_section **start, 4335796c8dcSSimon Schubert struct target_section **end) 4345796c8dcSSimon Schubert { 4355796c8dcSSimon Schubert unsigned count; 4365796c8dcSSimon Schubert 4375796c8dcSSimon Schubert count = bfd_count_sections (some_bfd); 4385796c8dcSSimon Schubert if (*start) 4395796c8dcSSimon Schubert xfree (* start); 4405796c8dcSSimon Schubert *start = (struct target_section *) xmalloc (count * sizeof (**start)); 4415796c8dcSSimon Schubert *end = *start; 4425796c8dcSSimon Schubert bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end); 4435796c8dcSSimon Schubert if (*end > *start + count) 444c50c785cSJohn Marino internal_error (__FILE__, __LINE__, 445c50c785cSJohn Marino _("failed internal consistency check")); 4465796c8dcSSimon Schubert /* We could realloc the table, but it probably loses for most files. */ 4475796c8dcSSimon Schubert return 0; 4485796c8dcSSimon Schubert } 4495796c8dcSSimon Schubert 4505796c8dcSSimon Schubert /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the 4515796c8dcSSimon Schubert current set of target sections. */ 4525796c8dcSSimon Schubert 4535796c8dcSSimon Schubert void 4545796c8dcSSimon Schubert add_target_sections (struct target_section *sections, 4555796c8dcSSimon Schubert struct target_section *sections_end) 4565796c8dcSSimon Schubert { 4575796c8dcSSimon Schubert int count; 4585796c8dcSSimon Schubert struct target_section_table *table = current_target_sections; 4595796c8dcSSimon Schubert 4605796c8dcSSimon Schubert count = sections_end - sections; 4615796c8dcSSimon Schubert 4625796c8dcSSimon Schubert if (count > 0) 4635796c8dcSSimon Schubert { 4645796c8dcSSimon Schubert int space = resize_section_table (table, count); 465cf7f2e2dSJohn Marino 4665796c8dcSSimon Schubert memcpy (table->sections + space, 4675796c8dcSSimon Schubert sections, count * sizeof (sections[0])); 4685796c8dcSSimon Schubert 4695796c8dcSSimon Schubert /* If these are the first file sections we can provide memory 4705796c8dcSSimon Schubert from, push the file_stratum target. */ 471cf7f2e2dSJohn Marino if (!using_exec_ops) 472cf7f2e2dSJohn Marino { 473cf7f2e2dSJohn Marino using_exec_ops = 1; 4745796c8dcSSimon Schubert push_target (&exec_ops); 4755796c8dcSSimon Schubert } 4765796c8dcSSimon Schubert } 477cf7f2e2dSJohn Marino } 4785796c8dcSSimon Schubert 4795796c8dcSSimon Schubert /* Remove all target sections taken from ABFD. */ 4805796c8dcSSimon Schubert 4815796c8dcSSimon Schubert void 4825796c8dcSSimon Schubert remove_target_sections (bfd *abfd) 4835796c8dcSSimon Schubert { 4845796c8dcSSimon Schubert struct target_section *src, *dest; 4855796c8dcSSimon Schubert struct target_section_table *table = current_target_sections; 4865796c8dcSSimon Schubert 4875796c8dcSSimon Schubert dest = table->sections; 4885796c8dcSSimon Schubert for (src = table->sections; src < table->sections_end; src++) 4895796c8dcSSimon Schubert if (src->bfd != abfd) 4905796c8dcSSimon Schubert { 4915796c8dcSSimon Schubert /* Keep this section. */ 4925796c8dcSSimon Schubert if (dest < src) 4935796c8dcSSimon Schubert *dest = *src; 4945796c8dcSSimon Schubert dest++; 4955796c8dcSSimon Schubert } 4965796c8dcSSimon Schubert 4975796c8dcSSimon Schubert /* If we've dropped any sections, resize the section table. */ 4985796c8dcSSimon Schubert if (dest < src) 4995796c8dcSSimon Schubert { 5005796c8dcSSimon Schubert int old_count; 5015796c8dcSSimon Schubert 5025796c8dcSSimon Schubert old_count = resize_section_table (table, dest - src); 5035796c8dcSSimon Schubert 5045796c8dcSSimon Schubert /* If we don't have any more sections to read memory from, 5055796c8dcSSimon Schubert remove the file_stratum target from the stack. */ 5065796c8dcSSimon Schubert if (old_count + (dest - src) == 0) 507cf7f2e2dSJohn Marino { 508cf7f2e2dSJohn Marino struct program_space *pspace; 509cf7f2e2dSJohn Marino 510cf7f2e2dSJohn Marino ALL_PSPACES (pspace) 511cf7f2e2dSJohn Marino if (pspace->target_sections.sections 512cf7f2e2dSJohn Marino != pspace->target_sections.sections_end) 513cf7f2e2dSJohn Marino return; 514cf7f2e2dSJohn Marino 5155796c8dcSSimon Schubert unpush_target (&exec_ops); 5165796c8dcSSimon Schubert } 5175796c8dcSSimon Schubert } 518cf7f2e2dSJohn Marino } 5195796c8dcSSimon Schubert 5205796c8dcSSimon Schubert 5215796c8dcSSimon Schubert static void 5225796c8dcSSimon Schubert bfdsec_to_vmap (struct bfd *abfd, struct bfd_section *sect, void *arg3) 5235796c8dcSSimon Schubert { 5245796c8dcSSimon Schubert struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *) arg3; 5255796c8dcSSimon Schubert struct vmap *vp; 5265796c8dcSSimon Schubert 5275796c8dcSSimon Schubert vp = vmap_bfd->pvmap; 5285796c8dcSSimon Schubert 5295796c8dcSSimon Schubert if ((bfd_get_section_flags (abfd, sect) & SEC_LOAD) == 0) 5305796c8dcSSimon Schubert return; 5315796c8dcSSimon Schubert 5325796c8dcSSimon Schubert if (strcmp (bfd_section_name (abfd, sect), ".text") == 0) 5335796c8dcSSimon Schubert { 5345796c8dcSSimon Schubert vp->tstart = bfd_section_vma (abfd, sect); 5355796c8dcSSimon Schubert vp->tend = vp->tstart + bfd_section_size (abfd, sect); 5365796c8dcSSimon Schubert vp->tvma = bfd_section_vma (abfd, sect); 5375796c8dcSSimon Schubert vp->toffs = sect->filepos; 5385796c8dcSSimon Schubert } 5395796c8dcSSimon Schubert else if (strcmp (bfd_section_name (abfd, sect), ".data") == 0) 5405796c8dcSSimon Schubert { 5415796c8dcSSimon Schubert vp->dstart = bfd_section_vma (abfd, sect); 5425796c8dcSSimon Schubert vp->dend = vp->dstart + bfd_section_size (abfd, sect); 5435796c8dcSSimon Schubert vp->dvma = bfd_section_vma (abfd, sect); 5445796c8dcSSimon Schubert } 5455796c8dcSSimon Schubert /* Silently ignore other types of sections. (FIXME?) */ 5465796c8dcSSimon Schubert } 5475796c8dcSSimon Schubert 5485796c8dcSSimon Schubert /* Make a vmap for ABFD which might be a member of the archive ARCH. 5495796c8dcSSimon Schubert Return the new vmap. */ 5505796c8dcSSimon Schubert 5515796c8dcSSimon Schubert struct vmap * 5525796c8dcSSimon Schubert map_vmap (bfd *abfd, bfd *arch) 5535796c8dcSSimon Schubert { 5545796c8dcSSimon Schubert struct vmap_and_bfd vmap_bfd; 5555796c8dcSSimon Schubert struct vmap *vp, **vpp; 5565796c8dcSSimon Schubert 5575796c8dcSSimon Schubert vp = (struct vmap *) xmalloc (sizeof (*vp)); 5585796c8dcSSimon Schubert memset ((char *) vp, '\0', sizeof (*vp)); 5595796c8dcSSimon Schubert vp->nxt = 0; 5605796c8dcSSimon Schubert vp->bfd = abfd; 5615796c8dcSSimon Schubert vp->name = bfd_get_filename (arch ? arch : abfd); 5625796c8dcSSimon Schubert vp->member = arch ? bfd_get_filename (abfd) : ""; 5635796c8dcSSimon Schubert 5645796c8dcSSimon Schubert vmap_bfd.pbfd = arch; 5655796c8dcSSimon Schubert vmap_bfd.pvmap = vp; 5665796c8dcSSimon Schubert bfd_map_over_sections (abfd, bfdsec_to_vmap, &vmap_bfd); 5675796c8dcSSimon Schubert 5685796c8dcSSimon Schubert /* Find the end of the list and append. */ 5695796c8dcSSimon Schubert for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt) 5705796c8dcSSimon Schubert ; 5715796c8dcSSimon Schubert *vpp = vp; 5725796c8dcSSimon Schubert 5735796c8dcSSimon Schubert return vp; 5745796c8dcSSimon Schubert } 5755796c8dcSSimon Schubert 5765796c8dcSSimon Schubert 577c50c785cSJohn Marino VEC(mem_range_s) * 578c50c785cSJohn Marino section_table_available_memory (VEC(mem_range_s) *memory, 579c50c785cSJohn Marino CORE_ADDR memaddr, ULONGEST len, 580c50c785cSJohn Marino struct target_section *sections, 581c50c785cSJohn Marino struct target_section *sections_end) 582c50c785cSJohn Marino { 583c50c785cSJohn Marino struct target_section *p; 584c50c785cSJohn Marino 585c50c785cSJohn Marino for (p = sections; p < sections_end; p++) 586c50c785cSJohn Marino { 587c50c785cSJohn Marino if ((bfd_get_section_flags (p->bfd, p->the_bfd_section) 588c50c785cSJohn Marino & SEC_READONLY) == 0) 589c50c785cSJohn Marino continue; 590c50c785cSJohn Marino 591c50c785cSJohn Marino /* Copy the meta-data, adjusted. */ 592c50c785cSJohn Marino if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len)) 593c50c785cSJohn Marino { 594c50c785cSJohn Marino ULONGEST lo1, hi1, lo2, hi2; 595c50c785cSJohn Marino struct mem_range *r; 596c50c785cSJohn Marino 597c50c785cSJohn Marino lo1 = memaddr; 598c50c785cSJohn Marino hi1 = memaddr + len; 599c50c785cSJohn Marino 600c50c785cSJohn Marino lo2 = p->addr; 601c50c785cSJohn Marino hi2 = p->endaddr; 602c50c785cSJohn Marino 603c50c785cSJohn Marino r = VEC_safe_push (mem_range_s, memory, NULL); 604c50c785cSJohn Marino 605c50c785cSJohn Marino r->start = max (lo1, lo2); 606c50c785cSJohn Marino r->length = min (hi1, hi2) - r->start; 607c50c785cSJohn Marino } 608c50c785cSJohn Marino } 609c50c785cSJohn Marino 610c50c785cSJohn Marino return memory; 611c50c785cSJohn Marino } 612c50c785cSJohn Marino 6135796c8dcSSimon Schubert int 6145796c8dcSSimon Schubert section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf, 6155796c8dcSSimon Schubert ULONGEST offset, LONGEST len, 6165796c8dcSSimon Schubert struct target_section *sections, 6175796c8dcSSimon Schubert struct target_section *sections_end, 6185796c8dcSSimon Schubert const char *section_name) 6195796c8dcSSimon Schubert { 6205796c8dcSSimon Schubert int res; 6215796c8dcSSimon Schubert struct target_section *p; 6225796c8dcSSimon Schubert ULONGEST memaddr = offset; 6235796c8dcSSimon Schubert ULONGEST memend = memaddr + len; 6245796c8dcSSimon Schubert 6255796c8dcSSimon Schubert if (len <= 0) 626c50c785cSJohn Marino internal_error (__FILE__, __LINE__, 627c50c785cSJohn Marino _("failed internal consistency check")); 6285796c8dcSSimon Schubert 6295796c8dcSSimon Schubert for (p = sections; p < sections_end; p++) 6305796c8dcSSimon Schubert { 6315796c8dcSSimon Schubert if (section_name && strcmp (section_name, p->the_bfd_section->name) != 0) 632c50c785cSJohn Marino continue; /* not the section we need. */ 6335796c8dcSSimon Schubert if (memaddr >= p->addr) 6345796c8dcSSimon Schubert { 6355796c8dcSSimon Schubert if (memend <= p->endaddr) 6365796c8dcSSimon Schubert { 6375796c8dcSSimon Schubert /* Entire transfer is within this section. */ 6385796c8dcSSimon Schubert if (writebuf) 6395796c8dcSSimon Schubert res = bfd_set_section_contents (p->bfd, p->the_bfd_section, 6405796c8dcSSimon Schubert writebuf, memaddr - p->addr, 6415796c8dcSSimon Schubert len); 6425796c8dcSSimon Schubert else 6435796c8dcSSimon Schubert res = bfd_get_section_contents (p->bfd, p->the_bfd_section, 6445796c8dcSSimon Schubert readbuf, memaddr - p->addr, 6455796c8dcSSimon Schubert len); 6465796c8dcSSimon Schubert return (res != 0) ? len : 0; 6475796c8dcSSimon Schubert } 6485796c8dcSSimon Schubert else if (memaddr >= p->endaddr) 6495796c8dcSSimon Schubert { 6505796c8dcSSimon Schubert /* This section ends before the transfer starts. */ 6515796c8dcSSimon Schubert continue; 6525796c8dcSSimon Schubert } 6535796c8dcSSimon Schubert else 6545796c8dcSSimon Schubert { 6555796c8dcSSimon Schubert /* This section overlaps the transfer. Just do half. */ 6565796c8dcSSimon Schubert len = p->endaddr - memaddr; 6575796c8dcSSimon Schubert if (writebuf) 6585796c8dcSSimon Schubert res = bfd_set_section_contents (p->bfd, p->the_bfd_section, 6595796c8dcSSimon Schubert writebuf, memaddr - p->addr, 6605796c8dcSSimon Schubert len); 6615796c8dcSSimon Schubert else 6625796c8dcSSimon Schubert res = bfd_get_section_contents (p->bfd, p->the_bfd_section, 6635796c8dcSSimon Schubert readbuf, memaddr - p->addr, 6645796c8dcSSimon Schubert len); 6655796c8dcSSimon Schubert return (res != 0) ? len : 0; 6665796c8dcSSimon Schubert } 6675796c8dcSSimon Schubert } 6685796c8dcSSimon Schubert } 6695796c8dcSSimon Schubert 670c50c785cSJohn Marino return 0; /* We can't help. */ 6715796c8dcSSimon Schubert } 6725796c8dcSSimon Schubert 6735796c8dcSSimon Schubert struct target_section_table * 6745796c8dcSSimon Schubert exec_get_section_table (struct target_ops *ops) 6755796c8dcSSimon Schubert { 6765796c8dcSSimon Schubert return current_target_sections; 6775796c8dcSSimon Schubert } 6785796c8dcSSimon Schubert 6795796c8dcSSimon Schubert static LONGEST 6805796c8dcSSimon Schubert exec_xfer_partial (struct target_ops *ops, enum target_object object, 6815796c8dcSSimon Schubert const char *annex, gdb_byte *readbuf, 6825796c8dcSSimon Schubert const gdb_byte *writebuf, 6835796c8dcSSimon Schubert ULONGEST offset, LONGEST len) 6845796c8dcSSimon Schubert { 6855796c8dcSSimon Schubert struct target_section_table *table = target_get_section_table (ops); 6865796c8dcSSimon Schubert 6875796c8dcSSimon Schubert if (object == TARGET_OBJECT_MEMORY) 6885796c8dcSSimon Schubert return section_table_xfer_memory_partial (readbuf, writebuf, 6895796c8dcSSimon Schubert offset, len, 6905796c8dcSSimon Schubert table->sections, 6915796c8dcSSimon Schubert table->sections_end, 6925796c8dcSSimon Schubert NULL); 6935796c8dcSSimon Schubert else 6945796c8dcSSimon Schubert return -1; 6955796c8dcSSimon Schubert } 6965796c8dcSSimon Schubert 6975796c8dcSSimon Schubert 6985796c8dcSSimon Schubert void 6995796c8dcSSimon Schubert print_section_info (struct target_section_table *t, bfd *abfd) 7005796c8dcSSimon Schubert { 7015796c8dcSSimon Schubert struct gdbarch *gdbarch = gdbarch_from_bfd (abfd); 7025796c8dcSSimon Schubert struct target_section *p; 7035796c8dcSSimon Schubert /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */ 7045796c8dcSSimon Schubert int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16; 7055796c8dcSSimon Schubert 7065796c8dcSSimon Schubert printf_filtered ("\t`%s', ", bfd_get_filename (abfd)); 7075796c8dcSSimon Schubert wrap_here (" "); 7085796c8dcSSimon Schubert printf_filtered (_("file type %s.\n"), bfd_get_target (abfd)); 7095796c8dcSSimon Schubert if (abfd == exec_bfd) 710cf7f2e2dSJohn Marino { 711c50c785cSJohn Marino /* gcc-3.4 does not like the initialization in 712c50c785cSJohn Marino <p == t->sections_end>. */ 713cf7f2e2dSJohn Marino bfd_vma displacement = 0; 714cf7f2e2dSJohn Marino bfd_vma entry_point; 715cf7f2e2dSJohn Marino 716cf7f2e2dSJohn Marino for (p = t->sections; p < t->sections_end; p++) 717cf7f2e2dSJohn Marino { 718cf7f2e2dSJohn Marino asection *asect = p->the_bfd_section; 719cf7f2e2dSJohn Marino 720cf7f2e2dSJohn Marino if ((bfd_get_section_flags (abfd, asect) & (SEC_ALLOC | SEC_LOAD)) 721cf7f2e2dSJohn Marino != (SEC_ALLOC | SEC_LOAD)) 722cf7f2e2dSJohn Marino continue; 723cf7f2e2dSJohn Marino 724cf7f2e2dSJohn Marino if (bfd_get_section_vma (abfd, asect) <= abfd->start_address 725cf7f2e2dSJohn Marino && abfd->start_address < (bfd_get_section_vma (abfd, asect) 726cf7f2e2dSJohn Marino + bfd_get_section_size (asect))) 727cf7f2e2dSJohn Marino { 728cf7f2e2dSJohn Marino displacement = p->addr - bfd_get_section_vma (abfd, asect); 729cf7f2e2dSJohn Marino break; 730cf7f2e2dSJohn Marino } 731cf7f2e2dSJohn Marino } 732cf7f2e2dSJohn Marino if (p == t->sections_end) 733c50c785cSJohn Marino warning (_("Cannot find section for the entry point of %s."), 734cf7f2e2dSJohn Marino bfd_get_filename (abfd)); 735cf7f2e2dSJohn Marino 736cf7f2e2dSJohn Marino entry_point = gdbarch_addr_bits_remove (gdbarch, 737cf7f2e2dSJohn Marino bfd_get_start_address (abfd) 738cf7f2e2dSJohn Marino + displacement); 7395796c8dcSSimon Schubert printf_filtered (_("\tEntry point: %s\n"), 740cf7f2e2dSJohn Marino paddress (gdbarch, entry_point)); 741cf7f2e2dSJohn Marino } 7425796c8dcSSimon Schubert for (p = t->sections; p < t->sections_end; p++) 7435796c8dcSSimon Schubert { 7445796c8dcSSimon Schubert printf_filtered ("\t%s", hex_string_custom (p->addr, wid)); 7455796c8dcSSimon Schubert printf_filtered (" - %s", hex_string_custom (p->endaddr, wid)); 7465796c8dcSSimon Schubert 7475796c8dcSSimon Schubert /* FIXME: A format of "08l" is not wide enough for file offsets 7485796c8dcSSimon Schubert larger than 4GB. OTOH, making it "016l" isn't desirable either 7495796c8dcSSimon Schubert since most output will then be much wider than necessary. It 7505796c8dcSSimon Schubert may make sense to test the size of the file and choose the 7515796c8dcSSimon Schubert format string accordingly. */ 7525796c8dcSSimon Schubert /* FIXME: i18n: Need to rewrite this sentence. */ 7535796c8dcSSimon Schubert if (info_verbose) 7545796c8dcSSimon Schubert printf_filtered (" @ %s", 7555796c8dcSSimon Schubert hex_string_custom (p->the_bfd_section->filepos, 8)); 756c50c785cSJohn Marino printf_filtered (" is %s", bfd_section_name (p->bfd, 757c50c785cSJohn Marino p->the_bfd_section)); 7585796c8dcSSimon Schubert if (p->bfd != abfd) 7595796c8dcSSimon Schubert printf_filtered (" in %s", bfd_get_filename (p->bfd)); 7605796c8dcSSimon Schubert printf_filtered ("\n"); 7615796c8dcSSimon Schubert } 7625796c8dcSSimon Schubert } 7635796c8dcSSimon Schubert 7645796c8dcSSimon Schubert static void 7655796c8dcSSimon Schubert exec_files_info (struct target_ops *t) 7665796c8dcSSimon Schubert { 7675796c8dcSSimon Schubert print_section_info (current_target_sections, exec_bfd); 7685796c8dcSSimon Schubert 7695796c8dcSSimon Schubert if (vmap) 7705796c8dcSSimon Schubert { 7715796c8dcSSimon Schubert int addr_size = gdbarch_addr_bit (target_gdbarch) / 8; 7725796c8dcSSimon Schubert struct vmap *vp; 7735796c8dcSSimon Schubert 7745796c8dcSSimon Schubert printf_unfiltered (_("\tMapping info for file `%s'.\n"), vmap->name); 7755796c8dcSSimon Schubert printf_unfiltered ("\t %*s %*s %*s %*s %8.8s %s\n", 7765796c8dcSSimon Schubert addr_size * 2, "tstart", 7775796c8dcSSimon Schubert addr_size * 2, "tend", 7785796c8dcSSimon Schubert addr_size * 2, "dstart", 7795796c8dcSSimon Schubert addr_size * 2, "dend", 7805796c8dcSSimon Schubert "section", 7815796c8dcSSimon Schubert "file(member)"); 7825796c8dcSSimon Schubert 7835796c8dcSSimon Schubert for (vp = vmap; vp; vp = vp->nxt) 7845796c8dcSSimon Schubert printf_unfiltered ("\t0x%s 0x%s 0x%s 0x%s %s%s%s%s\n", 7855796c8dcSSimon Schubert phex (vp->tstart, addr_size), 7865796c8dcSSimon Schubert phex (vp->tend, addr_size), 7875796c8dcSSimon Schubert phex (vp->dstart, addr_size), 7885796c8dcSSimon Schubert phex (vp->dend, addr_size), 7895796c8dcSSimon Schubert vp->name, 7905796c8dcSSimon Schubert *vp->member ? "(" : "", vp->member, 7915796c8dcSSimon Schubert *vp->member ? ")" : ""); 7925796c8dcSSimon Schubert } 7935796c8dcSSimon Schubert } 7945796c8dcSSimon Schubert 7955796c8dcSSimon Schubert static void 7965796c8dcSSimon Schubert set_section_command (char *args, int from_tty) 7975796c8dcSSimon Schubert { 7985796c8dcSSimon Schubert struct target_section *p; 7995796c8dcSSimon Schubert char *secname; 8005796c8dcSSimon Schubert unsigned seclen; 8015796c8dcSSimon Schubert unsigned long secaddr; 8025796c8dcSSimon Schubert char secprint[100]; 8035796c8dcSSimon Schubert long offset; 8045796c8dcSSimon Schubert struct target_section_table *table; 8055796c8dcSSimon Schubert 8065796c8dcSSimon Schubert if (args == 0) 8075796c8dcSSimon Schubert error (_("Must specify section name and its virtual address")); 8085796c8dcSSimon Schubert 809c50c785cSJohn Marino /* Parse out section name. */ 8105796c8dcSSimon Schubert for (secname = args; !isspace (*args); args++); 8115796c8dcSSimon Schubert seclen = args - secname; 8125796c8dcSSimon Schubert 813c50c785cSJohn Marino /* Parse out new virtual address. */ 8145796c8dcSSimon Schubert secaddr = parse_and_eval_address (args); 8155796c8dcSSimon Schubert 8165796c8dcSSimon Schubert table = current_target_sections; 8175796c8dcSSimon Schubert for (p = table->sections; p < table->sections_end; p++) 8185796c8dcSSimon Schubert { 819c50c785cSJohn Marino if (!strncmp (secname, bfd_section_name (exec_bfd, 820c50c785cSJohn Marino p->the_bfd_section), seclen) 8215796c8dcSSimon Schubert && bfd_section_name (exec_bfd, p->the_bfd_section)[seclen] == '\0') 8225796c8dcSSimon Schubert { 8235796c8dcSSimon Schubert offset = secaddr - p->addr; 8245796c8dcSSimon Schubert p->addr += offset; 8255796c8dcSSimon Schubert p->endaddr += offset; 8265796c8dcSSimon Schubert if (from_tty) 8275796c8dcSSimon Schubert exec_files_info (&exec_ops); 8285796c8dcSSimon Schubert return; 8295796c8dcSSimon Schubert } 8305796c8dcSSimon Schubert } 8315796c8dcSSimon Schubert if (seclen >= sizeof (secprint)) 8325796c8dcSSimon Schubert seclen = sizeof (secprint) - 1; 8335796c8dcSSimon Schubert strncpy (secprint, secname, seclen); 8345796c8dcSSimon Schubert secprint[seclen] = '\0'; 8355796c8dcSSimon Schubert error (_("Section %s not found"), secprint); 8365796c8dcSSimon Schubert } 8375796c8dcSSimon Schubert 8385796c8dcSSimon Schubert /* If we can find a section in FILENAME with BFD index INDEX, adjust 8395796c8dcSSimon Schubert it to ADDRESS. */ 8405796c8dcSSimon Schubert 8415796c8dcSSimon Schubert void 8425796c8dcSSimon Schubert exec_set_section_address (const char *filename, int index, CORE_ADDR address) 8435796c8dcSSimon Schubert { 8445796c8dcSSimon Schubert struct target_section *p; 8455796c8dcSSimon Schubert struct target_section_table *table; 8465796c8dcSSimon Schubert 8475796c8dcSSimon Schubert table = current_target_sections; 8485796c8dcSSimon Schubert for (p = table->sections; p < table->sections_end; p++) 8495796c8dcSSimon Schubert { 850c50c785cSJohn Marino if (filename_cmp (filename, p->bfd->filename) == 0 8515796c8dcSSimon Schubert && index == p->the_bfd_section->index) 8525796c8dcSSimon Schubert { 8535796c8dcSSimon Schubert p->endaddr += address - p->addr; 8545796c8dcSSimon Schubert p->addr = address; 8555796c8dcSSimon Schubert } 8565796c8dcSSimon Schubert } 8575796c8dcSSimon Schubert } 8585796c8dcSSimon Schubert 8595796c8dcSSimon Schubert /* If mourn is being called in all the right places, this could be say 8605796c8dcSSimon Schubert `gdb internal error' (since generic_mourn calls 8615796c8dcSSimon Schubert breakpoint_init_inferior). */ 8625796c8dcSSimon Schubert 8635796c8dcSSimon Schubert static int 8645796c8dcSSimon Schubert ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt) 8655796c8dcSSimon Schubert { 8665796c8dcSSimon Schubert return 0; 8675796c8dcSSimon Schubert } 8685796c8dcSSimon Schubert 8695796c8dcSSimon Schubert static int 8705796c8dcSSimon Schubert exec_has_memory (struct target_ops *ops) 8715796c8dcSSimon Schubert { 8725796c8dcSSimon Schubert /* We can provide memory if we have any file/target sections to read 8735796c8dcSSimon Schubert from. */ 8745796c8dcSSimon Schubert return (current_target_sections->sections 8755796c8dcSSimon Schubert != current_target_sections->sections_end); 8765796c8dcSSimon Schubert } 8775796c8dcSSimon Schubert 8785796c8dcSSimon Schubert /* Find mapped memory. */ 8795796c8dcSSimon Schubert 8805796c8dcSSimon Schubert extern void 881c50c785cSJohn Marino exec_set_find_memory_regions (int (*func) (find_memory_region_ftype, void *)) 8825796c8dcSSimon Schubert { 8835796c8dcSSimon Schubert exec_ops.to_find_memory_regions = func; 8845796c8dcSSimon Schubert } 8855796c8dcSSimon Schubert 8865796c8dcSSimon Schubert static char *exec_make_note_section (bfd *, int *); 8875796c8dcSSimon Schubert 8885796c8dcSSimon Schubert /* Fill in the exec file target vector. Very few entries need to be 8895796c8dcSSimon Schubert defined. */ 8905796c8dcSSimon Schubert 8915796c8dcSSimon Schubert static void 8925796c8dcSSimon Schubert init_exec_ops (void) 8935796c8dcSSimon Schubert { 8945796c8dcSSimon Schubert exec_ops.to_shortname = "exec"; 8955796c8dcSSimon Schubert exec_ops.to_longname = "Local exec file"; 8965796c8dcSSimon Schubert exec_ops.to_doc = "Use an executable file as a target.\n\ 8975796c8dcSSimon Schubert Specify the filename of the executable file."; 8985796c8dcSSimon Schubert exec_ops.to_open = exec_open; 899cf7f2e2dSJohn Marino exec_ops.to_close = exec_close_1; 9005796c8dcSSimon Schubert exec_ops.to_attach = find_default_attach; 9015796c8dcSSimon Schubert exec_ops.to_xfer_partial = exec_xfer_partial; 9025796c8dcSSimon Schubert exec_ops.to_get_section_table = exec_get_section_table; 9035796c8dcSSimon Schubert exec_ops.to_files_info = exec_files_info; 9045796c8dcSSimon Schubert exec_ops.to_insert_breakpoint = ignore; 9055796c8dcSSimon Schubert exec_ops.to_remove_breakpoint = ignore; 9065796c8dcSSimon Schubert exec_ops.to_create_inferior = find_default_create_inferior; 9075796c8dcSSimon Schubert exec_ops.to_stratum = file_stratum; 9085796c8dcSSimon Schubert exec_ops.to_has_memory = exec_has_memory; 9095796c8dcSSimon Schubert exec_ops.to_make_corefile_notes = exec_make_note_section; 9105796c8dcSSimon Schubert exec_ops.to_magic = OPS_MAGIC; 9115796c8dcSSimon Schubert } 9125796c8dcSSimon Schubert 9135796c8dcSSimon Schubert void 9145796c8dcSSimon Schubert _initialize_exec (void) 9155796c8dcSSimon Schubert { 9165796c8dcSSimon Schubert struct cmd_list_element *c; 9175796c8dcSSimon Schubert 9185796c8dcSSimon Schubert init_exec_ops (); 9195796c8dcSSimon Schubert 9205796c8dcSSimon Schubert if (!dbx_commands) 9215796c8dcSSimon Schubert { 9225796c8dcSSimon Schubert c = add_cmd ("file", class_files, file_command, _("\ 9235796c8dcSSimon Schubert Use FILE as program to be debugged.\n\ 9245796c8dcSSimon Schubert It is read for its symbols, for getting the contents of pure memory,\n\ 9255796c8dcSSimon Schubert and it is the program executed when you use the `run' command.\n\ 9265796c8dcSSimon Schubert If FILE cannot be found as specified, your execution directory path\n\ 9275796c8dcSSimon Schubert ($PATH) is searched for a command of that name.\n\ 9285796c8dcSSimon Schubert No arg means to have no executable file and no symbols."), &cmdlist); 9295796c8dcSSimon Schubert set_cmd_completer (c, filename_completer); 9305796c8dcSSimon Schubert } 9315796c8dcSSimon Schubert 9325796c8dcSSimon Schubert c = add_cmd ("exec-file", class_files, exec_file_command, _("\ 9335796c8dcSSimon Schubert Use FILE as program for getting contents of pure memory.\n\ 9345796c8dcSSimon Schubert If FILE cannot be found as specified, your execution directory path\n\ 9355796c8dcSSimon Schubert is searched for a command of that name.\n\ 9365796c8dcSSimon Schubert No arg means have no executable file."), &cmdlist); 9375796c8dcSSimon Schubert set_cmd_completer (c, filename_completer); 9385796c8dcSSimon Schubert 9395796c8dcSSimon Schubert add_com ("section", class_files, set_section_command, _("\ 9405796c8dcSSimon Schubert Change the base address of section SECTION of the exec file to ADDR.\n\ 9415796c8dcSSimon Schubert This can be used if the exec file does not contain section addresses,\n\ 9425796c8dcSSimon Schubert (such as in the a.out format), or when the addresses specified in the\n\ 9435796c8dcSSimon Schubert file itself are wrong. Each section must be changed separately. The\n\ 9445796c8dcSSimon Schubert ``info files'' command lists all the sections and their addresses.")); 9455796c8dcSSimon Schubert 9465796c8dcSSimon Schubert add_setshow_boolean_cmd ("write", class_support, &write_files, _("\ 9475796c8dcSSimon Schubert Set writing into executable and core files."), _("\ 9485796c8dcSSimon Schubert Show writing into executable and core files."), NULL, 9495796c8dcSSimon Schubert NULL, 9505796c8dcSSimon Schubert show_write_files, 9515796c8dcSSimon Schubert &setlist, &showlist); 9525796c8dcSSimon Schubert 9535796c8dcSSimon Schubert add_target (&exec_ops); 9545796c8dcSSimon Schubert } 9555796c8dcSSimon Schubert 9565796c8dcSSimon Schubert static char * 9575796c8dcSSimon Schubert exec_make_note_section (bfd *obfd, int *note_size) 9585796c8dcSSimon Schubert { 9595796c8dcSSimon Schubert error (_("Can't create a corefile")); 9605796c8dcSSimon Schubert } 961