15796c8dcSSimon Schubert /* Work with executable files, for GDB. 25796c8dcSSimon Schubert 35796c8dcSSimon Schubert Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 4*cf7f2e2dSJohn Marino 1998, 1999, 2000, 2001, 2002, 2003, 2007, 2008, 2009, 2010 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" 285796c8dcSSimon Schubert #include "symfile.h" 295796c8dcSSimon Schubert #include "objfiles.h" 305796c8dcSSimon Schubert #include "completer.h" 315796c8dcSSimon Schubert #include "value.h" 325796c8dcSSimon Schubert #include "exec.h" 335796c8dcSSimon Schubert #include "observer.h" 345796c8dcSSimon Schubert #include "arch-utils.h" 35*cf7f2e2dSJohn Marino #include "gdbthread.h" 36*cf7f2e2dSJohn Marino #include "progspace.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 69*cf7f2e2dSJohn Marino /* True if the exec target is pushed on the stack. */ 70*cf7f2e2dSJohn Marino static int using_exec_ops; 715796c8dcSSimon Schubert 725796c8dcSSimon Schubert /* Whether to open exec and core files read-only or read-write. */ 735796c8dcSSimon Schubert 745796c8dcSSimon Schubert int write_files = 0; 755796c8dcSSimon Schubert static void 765796c8dcSSimon Schubert show_write_files (struct ui_file *file, int from_tty, 775796c8dcSSimon Schubert struct cmd_list_element *c, const char *value) 785796c8dcSSimon Schubert { 795796c8dcSSimon Schubert fprintf_filtered (file, _("Writing into executable and core files is %s.\n"), 805796c8dcSSimon Schubert value); 815796c8dcSSimon Schubert } 825796c8dcSSimon Schubert 835796c8dcSSimon Schubert 845796c8dcSSimon Schubert struct vmap *vmap; 855796c8dcSSimon Schubert 865796c8dcSSimon Schubert static void 875796c8dcSSimon Schubert exec_open (char *args, int from_tty) 885796c8dcSSimon Schubert { 895796c8dcSSimon Schubert target_preopen (from_tty); 905796c8dcSSimon Schubert exec_file_attach (args, from_tty); 915796c8dcSSimon Schubert } 925796c8dcSSimon Schubert 935796c8dcSSimon Schubert /* Close and clear exec_bfd. If we end up with no target sections to 945796c8dcSSimon Schubert read memory from, this unpushes the exec_ops target. */ 955796c8dcSSimon Schubert 96*cf7f2e2dSJohn Marino void 97*cf7f2e2dSJohn Marino exec_close (void) 985796c8dcSSimon Schubert { 995796c8dcSSimon Schubert if (exec_bfd) 1005796c8dcSSimon Schubert { 1015796c8dcSSimon Schubert bfd *abfd = exec_bfd; 1025796c8dcSSimon Schubert char *name = bfd_get_filename (abfd); 1035796c8dcSSimon Schubert 104*cf7f2e2dSJohn Marino gdb_bfd_close_or_warn (abfd); 1055796c8dcSSimon Schubert xfree (name); 1065796c8dcSSimon Schubert 1075796c8dcSSimon Schubert /* Removing target sections may close the exec_ops target. 1085796c8dcSSimon Schubert Clear exec_bfd before doing so to prevent recursion. */ 1095796c8dcSSimon Schubert exec_bfd = NULL; 1105796c8dcSSimon Schubert exec_bfd_mtime = 0; 1115796c8dcSSimon Schubert 1125796c8dcSSimon Schubert remove_target_sections (abfd); 1135796c8dcSSimon Schubert } 1145796c8dcSSimon Schubert } 1155796c8dcSSimon Schubert 116*cf7f2e2dSJohn Marino /* This is the target_close implementation. Clears all target 117*cf7f2e2dSJohn Marino sections and closes all executable bfds from all program spaces. */ 118*cf7f2e2dSJohn Marino 1195796c8dcSSimon Schubert static void 120*cf7f2e2dSJohn Marino exec_close_1 (int quitting) 1215796c8dcSSimon Schubert { 1225796c8dcSSimon Schubert int need_symtab_cleanup = 0; 1235796c8dcSSimon Schubert struct vmap *vp, *nxt; 1245796c8dcSSimon Schubert 125*cf7f2e2dSJohn Marino using_exec_ops = 0; 126*cf7f2e2dSJohn Marino 1275796c8dcSSimon Schubert for (nxt = vmap; nxt != NULL;) 1285796c8dcSSimon Schubert { 1295796c8dcSSimon Schubert vp = nxt; 1305796c8dcSSimon Schubert nxt = vp->nxt; 1315796c8dcSSimon Schubert 1325796c8dcSSimon Schubert /* if there is an objfile associated with this bfd, 1335796c8dcSSimon Schubert free_objfile() will do proper cleanup of objfile *and* bfd. */ 1345796c8dcSSimon Schubert 1355796c8dcSSimon Schubert if (vp->objfile) 1365796c8dcSSimon Schubert { 1375796c8dcSSimon Schubert free_objfile (vp->objfile); 1385796c8dcSSimon Schubert need_symtab_cleanup = 1; 1395796c8dcSSimon Schubert } 1405796c8dcSSimon Schubert else if (vp->bfd != exec_bfd) 1415796c8dcSSimon Schubert /* FIXME-leak: We should be freeing vp->name too, I think. */ 142*cf7f2e2dSJohn Marino gdb_bfd_close_or_warn (vp->bfd); 1435796c8dcSSimon Schubert 1445796c8dcSSimon Schubert xfree (vp); 1455796c8dcSSimon Schubert } 1465796c8dcSSimon Schubert 1475796c8dcSSimon Schubert vmap = NULL; 1485796c8dcSSimon Schubert 149*cf7f2e2dSJohn Marino { 150*cf7f2e2dSJohn Marino struct program_space *ss; 151*cf7f2e2dSJohn Marino struct cleanup *old_chain; 152*cf7f2e2dSJohn Marino 153*cf7f2e2dSJohn Marino old_chain = save_current_program_space (); 154*cf7f2e2dSJohn Marino ALL_PSPACES (ss) 155*cf7f2e2dSJohn Marino { 156*cf7f2e2dSJohn Marino set_current_program_space (ss); 157*cf7f2e2dSJohn Marino 1585796c8dcSSimon Schubert /* Delete all target sections. */ 1595796c8dcSSimon Schubert resize_section_table 1605796c8dcSSimon Schubert (current_target_sections, 1615796c8dcSSimon Schubert -resize_section_table (current_target_sections, 0)); 1625796c8dcSSimon Schubert 163*cf7f2e2dSJohn Marino exec_close (); 164*cf7f2e2dSJohn Marino } 165*cf7f2e2dSJohn Marino 166*cf7f2e2dSJohn Marino do_cleanups (old_chain); 167*cf7f2e2dSJohn Marino } 1685796c8dcSSimon Schubert } 1695796c8dcSSimon Schubert 1705796c8dcSSimon Schubert void 1715796c8dcSSimon Schubert exec_file_clear (int from_tty) 1725796c8dcSSimon Schubert { 1735796c8dcSSimon Schubert /* Remove exec file. */ 174*cf7f2e2dSJohn Marino exec_close (); 1755796c8dcSSimon Schubert 1765796c8dcSSimon Schubert if (from_tty) 1775796c8dcSSimon Schubert printf_unfiltered (_("No executable file now.\n")); 1785796c8dcSSimon Schubert } 1795796c8dcSSimon Schubert 1805796c8dcSSimon Schubert /* Set FILENAME as the new exec file. 1815796c8dcSSimon Schubert 1825796c8dcSSimon Schubert This function is intended to be behave essentially the same 1835796c8dcSSimon Schubert as exec_file_command, except that the latter will detect when 1845796c8dcSSimon Schubert a target is being debugged, and will ask the user whether it 1855796c8dcSSimon Schubert should be shut down first. (If the answer is "no", then the 1865796c8dcSSimon Schubert new file is ignored.) 1875796c8dcSSimon Schubert 1885796c8dcSSimon Schubert This file is used by exec_file_command, to do the work of opening 1895796c8dcSSimon Schubert and processing the exec file after any prompting has happened. 1905796c8dcSSimon Schubert 1915796c8dcSSimon Schubert And, it is used by child_attach, when the attach command was 1925796c8dcSSimon Schubert given a pid but not a exec pathname, and the attach command could 1935796c8dcSSimon Schubert figure out the pathname from the pid. (In this case, we shouldn't 1945796c8dcSSimon Schubert ask the user whether the current target should be shut down -- 1955796c8dcSSimon Schubert we're supplying the exec pathname late for good reason.) */ 1965796c8dcSSimon Schubert 1975796c8dcSSimon Schubert void 1985796c8dcSSimon Schubert exec_file_attach (char *filename, int from_tty) 1995796c8dcSSimon Schubert { 2005796c8dcSSimon Schubert /* Remove any previous exec file. */ 201*cf7f2e2dSJohn Marino exec_close (); 2025796c8dcSSimon Schubert 2035796c8dcSSimon Schubert /* Now open and digest the file the user requested, if any. */ 2045796c8dcSSimon Schubert 2055796c8dcSSimon Schubert if (!filename) 2065796c8dcSSimon Schubert { 2075796c8dcSSimon Schubert if (from_tty) 2085796c8dcSSimon Schubert printf_unfiltered (_("No executable file now.\n")); 2095796c8dcSSimon Schubert 2105796c8dcSSimon Schubert set_gdbarch_from_file (NULL); 2115796c8dcSSimon Schubert } 2125796c8dcSSimon Schubert else 2135796c8dcSSimon Schubert { 2145796c8dcSSimon Schubert struct cleanup *cleanups; 2155796c8dcSSimon Schubert char *scratch_pathname; 2165796c8dcSSimon Schubert int scratch_chan; 2175796c8dcSSimon Schubert struct target_section *sections = NULL, *sections_end = NULL; 218*cf7f2e2dSJohn Marino char **matching; 2195796c8dcSSimon Schubert 2205796c8dcSSimon Schubert scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename, 2215796c8dcSSimon Schubert write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY, 2225796c8dcSSimon Schubert &scratch_pathname); 2235796c8dcSSimon Schubert #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__) 2245796c8dcSSimon Schubert if (scratch_chan < 0) 2255796c8dcSSimon Schubert { 2265796c8dcSSimon Schubert char *exename = alloca (strlen (filename) + 5); 227*cf7f2e2dSJohn Marino 2285796c8dcSSimon Schubert strcat (strcpy (exename, filename), ".exe"); 2295796c8dcSSimon Schubert scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename, 2305796c8dcSSimon Schubert write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY, 2315796c8dcSSimon Schubert &scratch_pathname); 2325796c8dcSSimon Schubert } 2335796c8dcSSimon Schubert #endif 2345796c8dcSSimon Schubert if (scratch_chan < 0) 2355796c8dcSSimon Schubert perror_with_name (filename); 2365796c8dcSSimon Schubert exec_bfd = bfd_fopen (scratch_pathname, gnutarget, 2375796c8dcSSimon Schubert write_files ? FOPEN_RUB : FOPEN_RB, 2385796c8dcSSimon Schubert scratch_chan); 2395796c8dcSSimon Schubert 2405796c8dcSSimon Schubert if (!exec_bfd) 2415796c8dcSSimon Schubert { 2425796c8dcSSimon Schubert close (scratch_chan); 2435796c8dcSSimon Schubert error (_("\"%s\": could not open as an executable file: %s"), 2445796c8dcSSimon Schubert scratch_pathname, bfd_errmsg (bfd_get_error ())); 2455796c8dcSSimon Schubert } 2465796c8dcSSimon Schubert 2475796c8dcSSimon Schubert /* At this point, scratch_pathname and exec_bfd->name both point to the 2485796c8dcSSimon Schubert same malloc'd string. However exec_close() will attempt to free it 2495796c8dcSSimon Schubert via the exec_bfd->name pointer, so we need to make another copy and 2505796c8dcSSimon Schubert leave exec_bfd as the new owner of the original copy. */ 2515796c8dcSSimon Schubert scratch_pathname = xstrdup (scratch_pathname); 2525796c8dcSSimon Schubert cleanups = make_cleanup (xfree, scratch_pathname); 2535796c8dcSSimon Schubert 254*cf7f2e2dSJohn Marino if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching)) 2555796c8dcSSimon Schubert { 2565796c8dcSSimon Schubert /* Make sure to close exec_bfd, or else "run" might try to use 2575796c8dcSSimon Schubert it. */ 258*cf7f2e2dSJohn Marino exec_close (); 2595796c8dcSSimon Schubert error (_("\"%s\": not in executable format: %s"), 260*cf7f2e2dSJohn Marino scratch_pathname, 261*cf7f2e2dSJohn Marino gdb_bfd_errmsg (bfd_get_error (), matching)); 2625796c8dcSSimon Schubert } 2635796c8dcSSimon Schubert 2645796c8dcSSimon Schubert /* FIXME - This should only be run for RS6000, but the ifdef is a poor 2655796c8dcSSimon Schubert way to accomplish. */ 2665796c8dcSSimon Schubert #ifdef DEPRECATED_IBM6000_TARGET 2675796c8dcSSimon Schubert /* Setup initial vmap. */ 2685796c8dcSSimon Schubert 2695796c8dcSSimon Schubert map_vmap (exec_bfd, 0); 2705796c8dcSSimon Schubert if (vmap == NULL) 2715796c8dcSSimon Schubert { 2725796c8dcSSimon Schubert /* Make sure to close exec_bfd, or else "run" might try to use 2735796c8dcSSimon Schubert it. */ 274*cf7f2e2dSJohn Marino exec_close (); 2755796c8dcSSimon Schubert error (_("\"%s\": can't find the file sections: %s"), 2765796c8dcSSimon Schubert scratch_pathname, bfd_errmsg (bfd_get_error ())); 2775796c8dcSSimon Schubert } 2785796c8dcSSimon Schubert #endif /* DEPRECATED_IBM6000_TARGET */ 2795796c8dcSSimon Schubert 2805796c8dcSSimon Schubert if (build_section_table (exec_bfd, §ions, §ions_end)) 2815796c8dcSSimon Schubert { 2825796c8dcSSimon Schubert /* Make sure to close exec_bfd, or else "run" might try to use 2835796c8dcSSimon Schubert it. */ 284*cf7f2e2dSJohn Marino exec_close (); 2855796c8dcSSimon Schubert error (_("\"%s\": can't find the file sections: %s"), 2865796c8dcSSimon Schubert scratch_pathname, bfd_errmsg (bfd_get_error ())); 2875796c8dcSSimon Schubert } 2885796c8dcSSimon Schubert 2895796c8dcSSimon Schubert exec_bfd_mtime = bfd_get_mtime (exec_bfd); 2905796c8dcSSimon Schubert 2915796c8dcSSimon Schubert validate_files (); 2925796c8dcSSimon Schubert 2935796c8dcSSimon Schubert set_gdbarch_from_file (exec_bfd); 2945796c8dcSSimon Schubert 2955796c8dcSSimon Schubert /* Add the executable's sections to the current address spaces' 296*cf7f2e2dSJohn Marino list of sections. This possibly pushes the exec_ops 297*cf7f2e2dSJohn Marino target. */ 2985796c8dcSSimon Schubert add_target_sections (sections, sections_end); 2995796c8dcSSimon Schubert xfree (sections); 3005796c8dcSSimon Schubert 3015796c8dcSSimon Schubert /* Tell display code (if any) about the changed file name. */ 3025796c8dcSSimon Schubert if (deprecated_exec_file_display_hook) 3035796c8dcSSimon Schubert (*deprecated_exec_file_display_hook) (filename); 3045796c8dcSSimon Schubert 3055796c8dcSSimon Schubert do_cleanups (cleanups); 3065796c8dcSSimon Schubert } 3075796c8dcSSimon Schubert bfd_cache_close_all (); 3085796c8dcSSimon Schubert observer_notify_executable_changed (); 3095796c8dcSSimon Schubert } 3105796c8dcSSimon Schubert 3115796c8dcSSimon Schubert /* Process the first arg in ARGS as the new exec file. 3125796c8dcSSimon Schubert 3135796c8dcSSimon Schubert Note that we have to explicitly ignore additional args, since we can 3145796c8dcSSimon Schubert be called from file_command(), which also calls symbol_file_command() 3155796c8dcSSimon Schubert which can take multiple args. 3165796c8dcSSimon Schubert 3175796c8dcSSimon Schubert If ARGS is NULL, we just want to close the exec file. */ 3185796c8dcSSimon Schubert 3195796c8dcSSimon Schubert static void 3205796c8dcSSimon Schubert exec_file_command (char *args, int from_tty) 3215796c8dcSSimon Schubert { 3225796c8dcSSimon Schubert char **argv; 3235796c8dcSSimon Schubert char *filename; 3245796c8dcSSimon Schubert 3255796c8dcSSimon Schubert if (from_tty && target_has_execution 3265796c8dcSSimon Schubert && !query (_("A program is being debugged already.\n" 3275796c8dcSSimon Schubert "Are you sure you want to change the file? "))) 3285796c8dcSSimon Schubert error (_("File not changed.")); 3295796c8dcSSimon Schubert 3305796c8dcSSimon Schubert if (args) 3315796c8dcSSimon Schubert { 3325796c8dcSSimon Schubert struct cleanup *cleanups; 3335796c8dcSSimon Schubert 3345796c8dcSSimon Schubert /* Scan through the args and pick up the first non option arg 3355796c8dcSSimon Schubert as the filename. */ 3365796c8dcSSimon Schubert 3375796c8dcSSimon Schubert argv = gdb_buildargv (args); 3385796c8dcSSimon Schubert cleanups = make_cleanup_freeargv (argv); 3395796c8dcSSimon Schubert 3405796c8dcSSimon Schubert for (; (*argv != NULL) && (**argv == '-'); argv++) 3415796c8dcSSimon Schubert {; 3425796c8dcSSimon Schubert } 3435796c8dcSSimon Schubert if (*argv == NULL) 3445796c8dcSSimon Schubert error (_("No executable file name was specified")); 3455796c8dcSSimon Schubert 3465796c8dcSSimon Schubert filename = tilde_expand (*argv); 3475796c8dcSSimon Schubert make_cleanup (xfree, filename); 3485796c8dcSSimon Schubert exec_file_attach (filename, from_tty); 3495796c8dcSSimon Schubert 3505796c8dcSSimon Schubert do_cleanups (cleanups); 3515796c8dcSSimon Schubert } 3525796c8dcSSimon Schubert else 3535796c8dcSSimon Schubert exec_file_attach (NULL, from_tty); 3545796c8dcSSimon Schubert } 3555796c8dcSSimon Schubert 3565796c8dcSSimon Schubert /* Set both the exec file and the symbol file, in one command. 3575796c8dcSSimon Schubert What a novelty. Why did GDB go through four major releases before this 3585796c8dcSSimon Schubert command was added? */ 3595796c8dcSSimon Schubert 3605796c8dcSSimon Schubert static void 3615796c8dcSSimon Schubert file_command (char *arg, int from_tty) 3625796c8dcSSimon Schubert { 3635796c8dcSSimon Schubert /* FIXME, if we lose on reading the symbol file, we should revert 3645796c8dcSSimon Schubert the exec file, but that's rough. */ 3655796c8dcSSimon Schubert exec_file_command (arg, from_tty); 3665796c8dcSSimon Schubert symbol_file_command (arg, from_tty); 3675796c8dcSSimon Schubert if (deprecated_file_changed_hook) 3685796c8dcSSimon Schubert deprecated_file_changed_hook (arg); 3695796c8dcSSimon Schubert } 3705796c8dcSSimon Schubert 3715796c8dcSSimon Schubert 3725796c8dcSSimon Schubert /* Locate all mappable sections of a BFD file. 3735796c8dcSSimon Schubert table_pp_char is a char * to get it through bfd_map_over_sections; 3745796c8dcSSimon Schubert we cast it back to its proper type. */ 3755796c8dcSSimon Schubert 3765796c8dcSSimon Schubert static void 3775796c8dcSSimon Schubert add_to_section_table (bfd *abfd, struct bfd_section *asect, 3785796c8dcSSimon Schubert void *table_pp_char) 3795796c8dcSSimon Schubert { 3805796c8dcSSimon Schubert struct target_section **table_pp = (struct target_section **) table_pp_char; 3815796c8dcSSimon Schubert flagword aflag; 3825796c8dcSSimon Schubert 3835796c8dcSSimon Schubert /* Check the section flags, but do not discard zero-length sections, since 3845796c8dcSSimon Schubert some symbols may still be attached to this section. For instance, we 3855796c8dcSSimon Schubert encountered on sparc-solaris 2.10 a shared library with an empty .bss 3865796c8dcSSimon Schubert section to which a symbol named "_end" was attached. The address 3875796c8dcSSimon Schubert of this symbol still needs to be relocated. */ 3885796c8dcSSimon Schubert aflag = bfd_get_section_flags (abfd, asect); 3895796c8dcSSimon Schubert if (!(aflag & SEC_ALLOC)) 3905796c8dcSSimon Schubert return; 3915796c8dcSSimon Schubert 3925796c8dcSSimon Schubert (*table_pp)->bfd = abfd; 3935796c8dcSSimon Schubert (*table_pp)->the_bfd_section = asect; 3945796c8dcSSimon Schubert (*table_pp)->addr = bfd_section_vma (abfd, asect); 3955796c8dcSSimon Schubert (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect); 3965796c8dcSSimon Schubert (*table_pp)++; 3975796c8dcSSimon Schubert } 3985796c8dcSSimon Schubert 3995796c8dcSSimon Schubert int 4005796c8dcSSimon Schubert resize_section_table (struct target_section_table *table, int num_added) 4015796c8dcSSimon Schubert { 4025796c8dcSSimon Schubert struct target_section *old_value; 4035796c8dcSSimon Schubert int old_count; 4045796c8dcSSimon Schubert int new_count; 4055796c8dcSSimon Schubert 4065796c8dcSSimon Schubert old_value = table->sections; 4075796c8dcSSimon Schubert old_count = table->sections_end - table->sections; 4085796c8dcSSimon Schubert 4095796c8dcSSimon Schubert new_count = num_added + old_count; 4105796c8dcSSimon Schubert 4115796c8dcSSimon Schubert if (new_count) 4125796c8dcSSimon Schubert { 4135796c8dcSSimon Schubert table->sections = xrealloc (table->sections, 4145796c8dcSSimon Schubert sizeof (struct target_section) * new_count); 4155796c8dcSSimon Schubert table->sections_end = table->sections + new_count; 4165796c8dcSSimon Schubert } 4175796c8dcSSimon Schubert else 4185796c8dcSSimon Schubert { 4195796c8dcSSimon Schubert xfree (table->sections); 4205796c8dcSSimon Schubert table->sections = table->sections_end = NULL; 4215796c8dcSSimon Schubert } 4225796c8dcSSimon Schubert 4235796c8dcSSimon Schubert return old_count; 4245796c8dcSSimon Schubert } 4255796c8dcSSimon Schubert 4265796c8dcSSimon Schubert /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR. 4275796c8dcSSimon Schubert Returns 0 if OK, 1 on error. */ 4285796c8dcSSimon Schubert 4295796c8dcSSimon Schubert int 4305796c8dcSSimon Schubert build_section_table (struct bfd *some_bfd, struct target_section **start, 4315796c8dcSSimon Schubert struct target_section **end) 4325796c8dcSSimon Schubert { 4335796c8dcSSimon Schubert unsigned count; 4345796c8dcSSimon Schubert 4355796c8dcSSimon Schubert count = bfd_count_sections (some_bfd); 4365796c8dcSSimon Schubert if (*start) 4375796c8dcSSimon Schubert xfree (* start); 4385796c8dcSSimon Schubert *start = (struct target_section *) xmalloc (count * sizeof (**start)); 4395796c8dcSSimon Schubert *end = *start; 4405796c8dcSSimon Schubert bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end); 4415796c8dcSSimon Schubert if (*end > *start + count) 4425796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, _("failed internal consistency check")); 4435796c8dcSSimon Schubert /* We could realloc the table, but it probably loses for most files. */ 4445796c8dcSSimon Schubert return 0; 4455796c8dcSSimon Schubert } 4465796c8dcSSimon Schubert 4475796c8dcSSimon Schubert /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the 4485796c8dcSSimon Schubert current set of target sections. */ 4495796c8dcSSimon Schubert 4505796c8dcSSimon Schubert void 4515796c8dcSSimon Schubert add_target_sections (struct target_section *sections, 4525796c8dcSSimon Schubert struct target_section *sections_end) 4535796c8dcSSimon Schubert { 4545796c8dcSSimon Schubert int count; 4555796c8dcSSimon Schubert struct target_section_table *table = current_target_sections; 4565796c8dcSSimon Schubert 4575796c8dcSSimon Schubert count = sections_end - sections; 4585796c8dcSSimon Schubert 4595796c8dcSSimon Schubert if (count > 0) 4605796c8dcSSimon Schubert { 4615796c8dcSSimon Schubert int space = resize_section_table (table, count); 462*cf7f2e2dSJohn Marino 4635796c8dcSSimon Schubert memcpy (table->sections + space, 4645796c8dcSSimon Schubert sections, count * sizeof (sections[0])); 4655796c8dcSSimon Schubert 4665796c8dcSSimon Schubert /* If these are the first file sections we can provide memory 4675796c8dcSSimon Schubert from, push the file_stratum target. */ 468*cf7f2e2dSJohn Marino if (!using_exec_ops) 469*cf7f2e2dSJohn Marino { 470*cf7f2e2dSJohn Marino using_exec_ops = 1; 4715796c8dcSSimon Schubert push_target (&exec_ops); 4725796c8dcSSimon Schubert } 4735796c8dcSSimon Schubert } 474*cf7f2e2dSJohn Marino } 4755796c8dcSSimon Schubert 4765796c8dcSSimon Schubert /* Remove all target sections taken from ABFD. */ 4775796c8dcSSimon Schubert 4785796c8dcSSimon Schubert void 4795796c8dcSSimon Schubert remove_target_sections (bfd *abfd) 4805796c8dcSSimon Schubert { 4815796c8dcSSimon Schubert struct target_section *src, *dest; 4825796c8dcSSimon Schubert struct target_section_table *table = current_target_sections; 4835796c8dcSSimon Schubert 4845796c8dcSSimon Schubert dest = table->sections; 4855796c8dcSSimon Schubert for (src = table->sections; src < table->sections_end; src++) 4865796c8dcSSimon Schubert if (src->bfd != abfd) 4875796c8dcSSimon Schubert { 4885796c8dcSSimon Schubert /* Keep this section. */ 4895796c8dcSSimon Schubert if (dest < src) 4905796c8dcSSimon Schubert *dest = *src; 4915796c8dcSSimon Schubert dest++; 4925796c8dcSSimon Schubert } 4935796c8dcSSimon Schubert 4945796c8dcSSimon Schubert /* If we've dropped any sections, resize the section table. */ 4955796c8dcSSimon Schubert if (dest < src) 4965796c8dcSSimon Schubert { 4975796c8dcSSimon Schubert int old_count; 4985796c8dcSSimon Schubert 4995796c8dcSSimon Schubert old_count = resize_section_table (table, dest - src); 5005796c8dcSSimon Schubert 5015796c8dcSSimon Schubert /* If we don't have any more sections to read memory from, 5025796c8dcSSimon Schubert remove the file_stratum target from the stack. */ 5035796c8dcSSimon Schubert if (old_count + (dest - src) == 0) 504*cf7f2e2dSJohn Marino { 505*cf7f2e2dSJohn Marino struct program_space *pspace; 506*cf7f2e2dSJohn Marino 507*cf7f2e2dSJohn Marino ALL_PSPACES (pspace) 508*cf7f2e2dSJohn Marino if (pspace->target_sections.sections 509*cf7f2e2dSJohn Marino != pspace->target_sections.sections_end) 510*cf7f2e2dSJohn Marino return; 511*cf7f2e2dSJohn Marino 5125796c8dcSSimon Schubert unpush_target (&exec_ops); 5135796c8dcSSimon Schubert } 5145796c8dcSSimon Schubert } 515*cf7f2e2dSJohn Marino } 5165796c8dcSSimon Schubert 5175796c8dcSSimon Schubert 5185796c8dcSSimon Schubert static void 5195796c8dcSSimon Schubert bfdsec_to_vmap (struct bfd *abfd, struct bfd_section *sect, void *arg3) 5205796c8dcSSimon Schubert { 5215796c8dcSSimon Schubert struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *) arg3; 5225796c8dcSSimon Schubert struct vmap *vp; 5235796c8dcSSimon Schubert 5245796c8dcSSimon Schubert vp = vmap_bfd->pvmap; 5255796c8dcSSimon Schubert 5265796c8dcSSimon Schubert if ((bfd_get_section_flags (abfd, sect) & SEC_LOAD) == 0) 5275796c8dcSSimon Schubert return; 5285796c8dcSSimon Schubert 5295796c8dcSSimon Schubert if (strcmp (bfd_section_name (abfd, sect), ".text") == 0) 5305796c8dcSSimon Schubert { 5315796c8dcSSimon Schubert vp->tstart = bfd_section_vma (abfd, sect); 5325796c8dcSSimon Schubert vp->tend = vp->tstart + bfd_section_size (abfd, sect); 5335796c8dcSSimon Schubert vp->tvma = bfd_section_vma (abfd, sect); 5345796c8dcSSimon Schubert vp->toffs = sect->filepos; 5355796c8dcSSimon Schubert } 5365796c8dcSSimon Schubert else if (strcmp (bfd_section_name (abfd, sect), ".data") == 0) 5375796c8dcSSimon Schubert { 5385796c8dcSSimon Schubert vp->dstart = bfd_section_vma (abfd, sect); 5395796c8dcSSimon Schubert vp->dend = vp->dstart + bfd_section_size (abfd, sect); 5405796c8dcSSimon Schubert vp->dvma = bfd_section_vma (abfd, sect); 5415796c8dcSSimon Schubert } 5425796c8dcSSimon Schubert /* Silently ignore other types of sections. (FIXME?) */ 5435796c8dcSSimon Schubert } 5445796c8dcSSimon Schubert 5455796c8dcSSimon Schubert /* Make a vmap for ABFD which might be a member of the archive ARCH. 5465796c8dcSSimon Schubert Return the new vmap. */ 5475796c8dcSSimon Schubert 5485796c8dcSSimon Schubert struct vmap * 5495796c8dcSSimon Schubert map_vmap (bfd *abfd, bfd *arch) 5505796c8dcSSimon Schubert { 5515796c8dcSSimon Schubert struct vmap_and_bfd vmap_bfd; 5525796c8dcSSimon Schubert struct vmap *vp, **vpp; 5535796c8dcSSimon Schubert 5545796c8dcSSimon Schubert vp = (struct vmap *) xmalloc (sizeof (*vp)); 5555796c8dcSSimon Schubert memset ((char *) vp, '\0', sizeof (*vp)); 5565796c8dcSSimon Schubert vp->nxt = 0; 5575796c8dcSSimon Schubert vp->bfd = abfd; 5585796c8dcSSimon Schubert vp->name = bfd_get_filename (arch ? arch : abfd); 5595796c8dcSSimon Schubert vp->member = arch ? bfd_get_filename (abfd) : ""; 5605796c8dcSSimon Schubert 5615796c8dcSSimon Schubert vmap_bfd.pbfd = arch; 5625796c8dcSSimon Schubert vmap_bfd.pvmap = vp; 5635796c8dcSSimon Schubert bfd_map_over_sections (abfd, bfdsec_to_vmap, &vmap_bfd); 5645796c8dcSSimon Schubert 5655796c8dcSSimon Schubert /* Find the end of the list and append. */ 5665796c8dcSSimon Schubert for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt) 5675796c8dcSSimon Schubert ; 5685796c8dcSSimon Schubert *vpp = vp; 5695796c8dcSSimon Schubert 5705796c8dcSSimon Schubert return vp; 5715796c8dcSSimon Schubert } 5725796c8dcSSimon Schubert 5735796c8dcSSimon Schubert 5745796c8dcSSimon Schubert int 5755796c8dcSSimon Schubert section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf, 5765796c8dcSSimon Schubert ULONGEST offset, LONGEST len, 5775796c8dcSSimon Schubert struct target_section *sections, 5785796c8dcSSimon Schubert struct target_section *sections_end, 5795796c8dcSSimon Schubert const char *section_name) 5805796c8dcSSimon Schubert { 5815796c8dcSSimon Schubert int res; 5825796c8dcSSimon Schubert struct target_section *p; 5835796c8dcSSimon Schubert ULONGEST memaddr = offset; 5845796c8dcSSimon Schubert ULONGEST memend = memaddr + len; 5855796c8dcSSimon Schubert 5865796c8dcSSimon Schubert if (len <= 0) 5875796c8dcSSimon Schubert internal_error (__FILE__, __LINE__, _("failed internal consistency check")); 5885796c8dcSSimon Schubert 5895796c8dcSSimon Schubert for (p = sections; p < sections_end; p++) 5905796c8dcSSimon Schubert { 5915796c8dcSSimon Schubert if (section_name && strcmp (section_name, p->the_bfd_section->name) != 0) 5925796c8dcSSimon Schubert continue; /* not the section we need */ 5935796c8dcSSimon Schubert if (memaddr >= p->addr) 5945796c8dcSSimon Schubert { 5955796c8dcSSimon Schubert if (memend <= p->endaddr) 5965796c8dcSSimon Schubert { 5975796c8dcSSimon Schubert /* Entire transfer is within this section. */ 5985796c8dcSSimon Schubert if (writebuf) 5995796c8dcSSimon Schubert res = bfd_set_section_contents (p->bfd, p->the_bfd_section, 6005796c8dcSSimon Schubert writebuf, memaddr - p->addr, 6015796c8dcSSimon Schubert len); 6025796c8dcSSimon Schubert else 6035796c8dcSSimon Schubert res = bfd_get_section_contents (p->bfd, p->the_bfd_section, 6045796c8dcSSimon Schubert readbuf, memaddr - p->addr, 6055796c8dcSSimon Schubert len); 6065796c8dcSSimon Schubert return (res != 0) ? len : 0; 6075796c8dcSSimon Schubert } 6085796c8dcSSimon Schubert else if (memaddr >= p->endaddr) 6095796c8dcSSimon Schubert { 6105796c8dcSSimon Schubert /* This section ends before the transfer starts. */ 6115796c8dcSSimon Schubert continue; 6125796c8dcSSimon Schubert } 6135796c8dcSSimon Schubert else 6145796c8dcSSimon Schubert { 6155796c8dcSSimon Schubert /* This section overlaps the transfer. Just do half. */ 6165796c8dcSSimon Schubert len = p->endaddr - memaddr; 6175796c8dcSSimon Schubert if (writebuf) 6185796c8dcSSimon Schubert res = bfd_set_section_contents (p->bfd, p->the_bfd_section, 6195796c8dcSSimon Schubert writebuf, memaddr - p->addr, 6205796c8dcSSimon Schubert len); 6215796c8dcSSimon Schubert else 6225796c8dcSSimon Schubert res = bfd_get_section_contents (p->bfd, p->the_bfd_section, 6235796c8dcSSimon Schubert readbuf, memaddr - p->addr, 6245796c8dcSSimon Schubert len); 6255796c8dcSSimon Schubert return (res != 0) ? len : 0; 6265796c8dcSSimon Schubert } 6275796c8dcSSimon Schubert } 6285796c8dcSSimon Schubert } 6295796c8dcSSimon Schubert 6305796c8dcSSimon Schubert return 0; /* We can't help */ 6315796c8dcSSimon Schubert } 6325796c8dcSSimon Schubert 6335796c8dcSSimon Schubert struct target_section_table * 6345796c8dcSSimon Schubert exec_get_section_table (struct target_ops *ops) 6355796c8dcSSimon Schubert { 6365796c8dcSSimon Schubert return current_target_sections; 6375796c8dcSSimon Schubert } 6385796c8dcSSimon Schubert 6395796c8dcSSimon Schubert static LONGEST 6405796c8dcSSimon Schubert exec_xfer_partial (struct target_ops *ops, enum target_object object, 6415796c8dcSSimon Schubert const char *annex, gdb_byte *readbuf, 6425796c8dcSSimon Schubert const gdb_byte *writebuf, 6435796c8dcSSimon Schubert ULONGEST offset, LONGEST len) 6445796c8dcSSimon Schubert { 6455796c8dcSSimon Schubert struct target_section_table *table = target_get_section_table (ops); 6465796c8dcSSimon Schubert 6475796c8dcSSimon Schubert if (object == TARGET_OBJECT_MEMORY) 6485796c8dcSSimon Schubert return section_table_xfer_memory_partial (readbuf, writebuf, 6495796c8dcSSimon Schubert offset, len, 6505796c8dcSSimon Schubert table->sections, 6515796c8dcSSimon Schubert table->sections_end, 6525796c8dcSSimon Schubert NULL); 6535796c8dcSSimon Schubert else 6545796c8dcSSimon Schubert return -1; 6555796c8dcSSimon Schubert } 6565796c8dcSSimon Schubert 6575796c8dcSSimon Schubert 6585796c8dcSSimon Schubert void 6595796c8dcSSimon Schubert print_section_info (struct target_section_table *t, bfd *abfd) 6605796c8dcSSimon Schubert { 6615796c8dcSSimon Schubert struct gdbarch *gdbarch = gdbarch_from_bfd (abfd); 6625796c8dcSSimon Schubert struct target_section *p; 6635796c8dcSSimon Schubert /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */ 6645796c8dcSSimon Schubert int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16; 6655796c8dcSSimon Schubert 6665796c8dcSSimon Schubert printf_filtered ("\t`%s', ", bfd_get_filename (abfd)); 6675796c8dcSSimon Schubert wrap_here (" "); 6685796c8dcSSimon Schubert printf_filtered (_("file type %s.\n"), bfd_get_target (abfd)); 6695796c8dcSSimon Schubert if (abfd == exec_bfd) 670*cf7f2e2dSJohn Marino { 671*cf7f2e2dSJohn Marino /* gcc-3.4 does not like the initialization in <p == t->sections_end>. */ 672*cf7f2e2dSJohn Marino bfd_vma displacement = 0; 673*cf7f2e2dSJohn Marino bfd_vma entry_point; 674*cf7f2e2dSJohn Marino 675*cf7f2e2dSJohn Marino for (p = t->sections; p < t->sections_end; p++) 676*cf7f2e2dSJohn Marino { 677*cf7f2e2dSJohn Marino asection *asect = p->the_bfd_section; 678*cf7f2e2dSJohn Marino 679*cf7f2e2dSJohn Marino if ((bfd_get_section_flags (abfd, asect) & (SEC_ALLOC | SEC_LOAD)) 680*cf7f2e2dSJohn Marino != (SEC_ALLOC | SEC_LOAD)) 681*cf7f2e2dSJohn Marino continue; 682*cf7f2e2dSJohn Marino 683*cf7f2e2dSJohn Marino if (bfd_get_section_vma (abfd, asect) <= abfd->start_address 684*cf7f2e2dSJohn Marino && abfd->start_address < (bfd_get_section_vma (abfd, asect) 685*cf7f2e2dSJohn Marino + bfd_get_section_size (asect))) 686*cf7f2e2dSJohn Marino { 687*cf7f2e2dSJohn Marino displacement = p->addr - bfd_get_section_vma (abfd, asect); 688*cf7f2e2dSJohn Marino break; 689*cf7f2e2dSJohn Marino } 690*cf7f2e2dSJohn Marino } 691*cf7f2e2dSJohn Marino if (p == t->sections_end) 692*cf7f2e2dSJohn Marino warning (_("Cannot find section for the entry point of %s.\n"), 693*cf7f2e2dSJohn Marino bfd_get_filename (abfd)); 694*cf7f2e2dSJohn Marino 695*cf7f2e2dSJohn Marino entry_point = gdbarch_addr_bits_remove (gdbarch, 696*cf7f2e2dSJohn Marino bfd_get_start_address (abfd) 697*cf7f2e2dSJohn Marino + displacement); 6985796c8dcSSimon Schubert printf_filtered (_("\tEntry point: %s\n"), 699*cf7f2e2dSJohn Marino paddress (gdbarch, entry_point)); 700*cf7f2e2dSJohn Marino } 7015796c8dcSSimon Schubert for (p = t->sections; p < t->sections_end; p++) 7025796c8dcSSimon Schubert { 7035796c8dcSSimon Schubert printf_filtered ("\t%s", hex_string_custom (p->addr, wid)); 7045796c8dcSSimon Schubert printf_filtered (" - %s", hex_string_custom (p->endaddr, wid)); 7055796c8dcSSimon Schubert 7065796c8dcSSimon Schubert /* FIXME: A format of "08l" is not wide enough for file offsets 7075796c8dcSSimon Schubert larger than 4GB. OTOH, making it "016l" isn't desirable either 7085796c8dcSSimon Schubert since most output will then be much wider than necessary. It 7095796c8dcSSimon Schubert may make sense to test the size of the file and choose the 7105796c8dcSSimon Schubert format string accordingly. */ 7115796c8dcSSimon Schubert /* FIXME: i18n: Need to rewrite this sentence. */ 7125796c8dcSSimon Schubert if (info_verbose) 7135796c8dcSSimon Schubert printf_filtered (" @ %s", 7145796c8dcSSimon Schubert hex_string_custom (p->the_bfd_section->filepos, 8)); 7155796c8dcSSimon Schubert printf_filtered (" is %s", bfd_section_name (p->bfd, p->the_bfd_section)); 7165796c8dcSSimon Schubert if (p->bfd != abfd) 7175796c8dcSSimon Schubert printf_filtered (" in %s", bfd_get_filename (p->bfd)); 7185796c8dcSSimon Schubert printf_filtered ("\n"); 7195796c8dcSSimon Schubert } 7205796c8dcSSimon Schubert } 7215796c8dcSSimon Schubert 7225796c8dcSSimon Schubert static void 7235796c8dcSSimon Schubert exec_files_info (struct target_ops *t) 7245796c8dcSSimon Schubert { 7255796c8dcSSimon Schubert print_section_info (current_target_sections, exec_bfd); 7265796c8dcSSimon Schubert 7275796c8dcSSimon Schubert if (vmap) 7285796c8dcSSimon Schubert { 7295796c8dcSSimon Schubert int addr_size = gdbarch_addr_bit (target_gdbarch) / 8; 7305796c8dcSSimon Schubert struct vmap *vp; 7315796c8dcSSimon Schubert 7325796c8dcSSimon Schubert printf_unfiltered (_("\tMapping info for file `%s'.\n"), vmap->name); 7335796c8dcSSimon Schubert printf_unfiltered ("\t %*s %*s %*s %*s %8.8s %s\n", 7345796c8dcSSimon Schubert addr_size * 2, "tstart", 7355796c8dcSSimon Schubert addr_size * 2, "tend", 7365796c8dcSSimon Schubert addr_size * 2, "dstart", 7375796c8dcSSimon Schubert addr_size * 2, "dend", 7385796c8dcSSimon Schubert "section", 7395796c8dcSSimon Schubert "file(member)"); 7405796c8dcSSimon Schubert 7415796c8dcSSimon Schubert for (vp = vmap; vp; vp = vp->nxt) 7425796c8dcSSimon Schubert printf_unfiltered ("\t0x%s 0x%s 0x%s 0x%s %s%s%s%s\n", 7435796c8dcSSimon Schubert phex (vp->tstart, addr_size), 7445796c8dcSSimon Schubert phex (vp->tend, addr_size), 7455796c8dcSSimon Schubert phex (vp->dstart, addr_size), 7465796c8dcSSimon Schubert phex (vp->dend, addr_size), 7475796c8dcSSimon Schubert vp->name, 7485796c8dcSSimon Schubert *vp->member ? "(" : "", vp->member, 7495796c8dcSSimon Schubert *vp->member ? ")" : ""); 7505796c8dcSSimon Schubert } 7515796c8dcSSimon Schubert } 7525796c8dcSSimon Schubert 7535796c8dcSSimon Schubert static void 7545796c8dcSSimon Schubert set_section_command (char *args, int from_tty) 7555796c8dcSSimon Schubert { 7565796c8dcSSimon Schubert struct target_section *p; 7575796c8dcSSimon Schubert char *secname; 7585796c8dcSSimon Schubert unsigned seclen; 7595796c8dcSSimon Schubert unsigned long secaddr; 7605796c8dcSSimon Schubert char secprint[100]; 7615796c8dcSSimon Schubert long offset; 7625796c8dcSSimon Schubert struct target_section_table *table; 7635796c8dcSSimon Schubert 7645796c8dcSSimon Schubert if (args == 0) 7655796c8dcSSimon Schubert error (_("Must specify section name and its virtual address")); 7665796c8dcSSimon Schubert 7675796c8dcSSimon Schubert /* Parse out section name */ 7685796c8dcSSimon Schubert for (secname = args; !isspace (*args); args++); 7695796c8dcSSimon Schubert seclen = args - secname; 7705796c8dcSSimon Schubert 7715796c8dcSSimon Schubert /* Parse out new virtual address */ 7725796c8dcSSimon Schubert secaddr = parse_and_eval_address (args); 7735796c8dcSSimon Schubert 7745796c8dcSSimon Schubert table = current_target_sections; 7755796c8dcSSimon Schubert for (p = table->sections; p < table->sections_end; p++) 7765796c8dcSSimon Schubert { 7775796c8dcSSimon Schubert if (!strncmp (secname, bfd_section_name (exec_bfd, p->the_bfd_section), seclen) 7785796c8dcSSimon Schubert && bfd_section_name (exec_bfd, p->the_bfd_section)[seclen] == '\0') 7795796c8dcSSimon Schubert { 7805796c8dcSSimon Schubert offset = secaddr - p->addr; 7815796c8dcSSimon Schubert p->addr += offset; 7825796c8dcSSimon Schubert p->endaddr += offset; 7835796c8dcSSimon Schubert if (from_tty) 7845796c8dcSSimon Schubert exec_files_info (&exec_ops); 7855796c8dcSSimon Schubert return; 7865796c8dcSSimon Schubert } 7875796c8dcSSimon Schubert } 7885796c8dcSSimon Schubert if (seclen >= sizeof (secprint)) 7895796c8dcSSimon Schubert seclen = sizeof (secprint) - 1; 7905796c8dcSSimon Schubert strncpy (secprint, secname, seclen); 7915796c8dcSSimon Schubert secprint[seclen] = '\0'; 7925796c8dcSSimon Schubert error (_("Section %s not found"), secprint); 7935796c8dcSSimon Schubert } 7945796c8dcSSimon Schubert 7955796c8dcSSimon Schubert /* If we can find a section in FILENAME with BFD index INDEX, adjust 7965796c8dcSSimon Schubert it to ADDRESS. */ 7975796c8dcSSimon Schubert 7985796c8dcSSimon Schubert void 7995796c8dcSSimon Schubert exec_set_section_address (const char *filename, int index, CORE_ADDR address) 8005796c8dcSSimon Schubert { 8015796c8dcSSimon Schubert struct target_section *p; 8025796c8dcSSimon Schubert struct target_section_table *table; 8035796c8dcSSimon Schubert 8045796c8dcSSimon Schubert table = current_target_sections; 8055796c8dcSSimon Schubert for (p = table->sections; p < table->sections_end; p++) 8065796c8dcSSimon Schubert { 8075796c8dcSSimon Schubert if (strcmp (filename, p->bfd->filename) == 0 8085796c8dcSSimon Schubert && index == p->the_bfd_section->index) 8095796c8dcSSimon Schubert { 8105796c8dcSSimon Schubert p->endaddr += address - p->addr; 8115796c8dcSSimon Schubert p->addr = address; 8125796c8dcSSimon Schubert } 8135796c8dcSSimon Schubert } 8145796c8dcSSimon Schubert } 8155796c8dcSSimon Schubert 8165796c8dcSSimon Schubert /* If mourn is being called in all the right places, this could be say 8175796c8dcSSimon Schubert `gdb internal error' (since generic_mourn calls 8185796c8dcSSimon Schubert breakpoint_init_inferior). */ 8195796c8dcSSimon Schubert 8205796c8dcSSimon Schubert static int 8215796c8dcSSimon Schubert ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt) 8225796c8dcSSimon Schubert { 8235796c8dcSSimon Schubert return 0; 8245796c8dcSSimon Schubert } 8255796c8dcSSimon Schubert 8265796c8dcSSimon Schubert static int 8275796c8dcSSimon Schubert exec_has_memory (struct target_ops *ops) 8285796c8dcSSimon Schubert { 8295796c8dcSSimon Schubert /* We can provide memory if we have any file/target sections to read 8305796c8dcSSimon Schubert from. */ 8315796c8dcSSimon Schubert return (current_target_sections->sections 8325796c8dcSSimon Schubert != current_target_sections->sections_end); 8335796c8dcSSimon Schubert } 8345796c8dcSSimon Schubert 8355796c8dcSSimon Schubert /* Find mapped memory. */ 8365796c8dcSSimon Schubert 8375796c8dcSSimon Schubert extern void 8385796c8dcSSimon Schubert exec_set_find_memory_regions (int (*func) (int (*) (CORE_ADDR, 8395796c8dcSSimon Schubert unsigned long, 8405796c8dcSSimon Schubert int, int, int, 8415796c8dcSSimon Schubert void *), 8425796c8dcSSimon Schubert void *)) 8435796c8dcSSimon Schubert { 8445796c8dcSSimon Schubert exec_ops.to_find_memory_regions = func; 8455796c8dcSSimon Schubert } 8465796c8dcSSimon Schubert 8475796c8dcSSimon Schubert static char *exec_make_note_section (bfd *, int *); 8485796c8dcSSimon Schubert 8495796c8dcSSimon Schubert /* Fill in the exec file target vector. Very few entries need to be 8505796c8dcSSimon Schubert defined. */ 8515796c8dcSSimon Schubert 8525796c8dcSSimon Schubert static void 8535796c8dcSSimon Schubert init_exec_ops (void) 8545796c8dcSSimon Schubert { 8555796c8dcSSimon Schubert exec_ops.to_shortname = "exec"; 8565796c8dcSSimon Schubert exec_ops.to_longname = "Local exec file"; 8575796c8dcSSimon Schubert exec_ops.to_doc = "Use an executable file as a target.\n\ 8585796c8dcSSimon Schubert Specify the filename of the executable file."; 8595796c8dcSSimon Schubert exec_ops.to_open = exec_open; 860*cf7f2e2dSJohn Marino exec_ops.to_close = exec_close_1; 8615796c8dcSSimon Schubert exec_ops.to_attach = find_default_attach; 8625796c8dcSSimon Schubert exec_ops.to_xfer_partial = exec_xfer_partial; 8635796c8dcSSimon Schubert exec_ops.to_get_section_table = exec_get_section_table; 8645796c8dcSSimon Schubert exec_ops.to_files_info = exec_files_info; 8655796c8dcSSimon Schubert exec_ops.to_insert_breakpoint = ignore; 8665796c8dcSSimon Schubert exec_ops.to_remove_breakpoint = ignore; 8675796c8dcSSimon Schubert exec_ops.to_create_inferior = find_default_create_inferior; 8685796c8dcSSimon Schubert exec_ops.to_stratum = file_stratum; 8695796c8dcSSimon Schubert exec_ops.to_has_memory = exec_has_memory; 8705796c8dcSSimon Schubert exec_ops.to_make_corefile_notes = exec_make_note_section; 8715796c8dcSSimon Schubert exec_ops.to_magic = OPS_MAGIC; 8725796c8dcSSimon Schubert } 8735796c8dcSSimon Schubert 8745796c8dcSSimon Schubert void 8755796c8dcSSimon Schubert _initialize_exec (void) 8765796c8dcSSimon Schubert { 8775796c8dcSSimon Schubert struct cmd_list_element *c; 8785796c8dcSSimon Schubert 8795796c8dcSSimon Schubert init_exec_ops (); 8805796c8dcSSimon Schubert 8815796c8dcSSimon Schubert if (!dbx_commands) 8825796c8dcSSimon Schubert { 8835796c8dcSSimon Schubert c = add_cmd ("file", class_files, file_command, _("\ 8845796c8dcSSimon Schubert Use FILE as program to be debugged.\n\ 8855796c8dcSSimon Schubert It is read for its symbols, for getting the contents of pure memory,\n\ 8865796c8dcSSimon Schubert and it is the program executed when you use the `run' command.\n\ 8875796c8dcSSimon Schubert If FILE cannot be found as specified, your execution directory path\n\ 8885796c8dcSSimon Schubert ($PATH) is searched for a command of that name.\n\ 8895796c8dcSSimon Schubert No arg means to have no executable file and no symbols."), &cmdlist); 8905796c8dcSSimon Schubert set_cmd_completer (c, filename_completer); 8915796c8dcSSimon Schubert } 8925796c8dcSSimon Schubert 8935796c8dcSSimon Schubert c = add_cmd ("exec-file", class_files, exec_file_command, _("\ 8945796c8dcSSimon Schubert Use FILE as program for getting contents of pure memory.\n\ 8955796c8dcSSimon Schubert If FILE cannot be found as specified, your execution directory path\n\ 8965796c8dcSSimon Schubert is searched for a command of that name.\n\ 8975796c8dcSSimon Schubert No arg means have no executable file."), &cmdlist); 8985796c8dcSSimon Schubert set_cmd_completer (c, filename_completer); 8995796c8dcSSimon Schubert 9005796c8dcSSimon Schubert add_com ("section", class_files, set_section_command, _("\ 9015796c8dcSSimon Schubert Change the base address of section SECTION of the exec file to ADDR.\n\ 9025796c8dcSSimon Schubert This can be used if the exec file does not contain section addresses,\n\ 9035796c8dcSSimon Schubert (such as in the a.out format), or when the addresses specified in the\n\ 9045796c8dcSSimon Schubert file itself are wrong. Each section must be changed separately. The\n\ 9055796c8dcSSimon Schubert ``info files'' command lists all the sections and their addresses.")); 9065796c8dcSSimon Schubert 9075796c8dcSSimon Schubert add_setshow_boolean_cmd ("write", class_support, &write_files, _("\ 9085796c8dcSSimon Schubert Set writing into executable and core files."), _("\ 9095796c8dcSSimon Schubert Show writing into executable and core files."), NULL, 9105796c8dcSSimon Schubert NULL, 9115796c8dcSSimon Schubert show_write_files, 9125796c8dcSSimon Schubert &setlist, &showlist); 9135796c8dcSSimon Schubert 9145796c8dcSSimon Schubert add_target (&exec_ops); 9155796c8dcSSimon Schubert } 9165796c8dcSSimon Schubert 9175796c8dcSSimon Schubert static char * 9185796c8dcSSimon Schubert exec_make_note_section (bfd *obfd, int *note_size) 9195796c8dcSSimon Schubert { 9205796c8dcSSimon Schubert error (_("Can't create a corefile")); 9215796c8dcSSimon Schubert } 922