xref: /dflybsd-src/contrib/gdb-7/gdb/exec.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Work with executable files, for GDB.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4*5796c8dcSSimon Schubert    1998, 1999, 2000, 2001, 2002, 2003, 2007, 2008, 2009
5*5796c8dcSSimon Schubert    Free Software Foundation, Inc.
6*5796c8dcSSimon Schubert 
7*5796c8dcSSimon Schubert    This file is part of GDB.
8*5796c8dcSSimon Schubert 
9*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
10*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
11*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
12*5796c8dcSSimon Schubert    (at your option) any later version.
13*5796c8dcSSimon Schubert 
14*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
15*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
16*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*5796c8dcSSimon Schubert    GNU General Public License for more details.
18*5796c8dcSSimon Schubert 
19*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
20*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21*5796c8dcSSimon Schubert 
22*5796c8dcSSimon Schubert #include "defs.h"
23*5796c8dcSSimon Schubert #include "frame.h"
24*5796c8dcSSimon Schubert #include "inferior.h"
25*5796c8dcSSimon Schubert #include "target.h"
26*5796c8dcSSimon Schubert #include "gdbcmd.h"
27*5796c8dcSSimon Schubert #include "language.h"
28*5796c8dcSSimon Schubert #include "symfile.h"
29*5796c8dcSSimon Schubert #include "objfiles.h"
30*5796c8dcSSimon Schubert #include "completer.h"
31*5796c8dcSSimon Schubert #include "value.h"
32*5796c8dcSSimon Schubert #include "exec.h"
33*5796c8dcSSimon Schubert #include "observer.h"
34*5796c8dcSSimon Schubert #include "arch-utils.h"
35*5796c8dcSSimon Schubert 
36*5796c8dcSSimon Schubert #include <fcntl.h>
37*5796c8dcSSimon Schubert #include "readline/readline.h"
38*5796c8dcSSimon Schubert #include "gdb_string.h"
39*5796c8dcSSimon Schubert 
40*5796c8dcSSimon Schubert #include "gdbcore.h"
41*5796c8dcSSimon Schubert 
42*5796c8dcSSimon Schubert #include <ctype.h>
43*5796c8dcSSimon Schubert #include "gdb_stat.h"
44*5796c8dcSSimon Schubert 
45*5796c8dcSSimon Schubert #include "xcoffsolib.h"
46*5796c8dcSSimon Schubert 
47*5796c8dcSSimon Schubert struct vmap *map_vmap (bfd *, bfd *);
48*5796c8dcSSimon Schubert 
49*5796c8dcSSimon Schubert void (*deprecated_file_changed_hook) (char *);
50*5796c8dcSSimon Schubert 
51*5796c8dcSSimon Schubert /* Prototypes for local functions */
52*5796c8dcSSimon Schubert 
53*5796c8dcSSimon Schubert static void exec_close (int);
54*5796c8dcSSimon Schubert 
55*5796c8dcSSimon Schubert static void file_command (char *, int);
56*5796c8dcSSimon Schubert 
57*5796c8dcSSimon Schubert static void set_section_command (char *, int);
58*5796c8dcSSimon Schubert 
59*5796c8dcSSimon Schubert static void exec_files_info (struct target_ops *);
60*5796c8dcSSimon Schubert 
61*5796c8dcSSimon Schubert static void init_exec_ops (void);
62*5796c8dcSSimon Schubert 
63*5796c8dcSSimon Schubert void _initialize_exec (void);
64*5796c8dcSSimon Schubert 
65*5796c8dcSSimon Schubert /* The target vector for executable files.  */
66*5796c8dcSSimon Schubert 
67*5796c8dcSSimon Schubert struct target_ops exec_ops;
68*5796c8dcSSimon Schubert 
69*5796c8dcSSimon Schubert /* The Binary File Descriptor handle for the executable file.  */
70*5796c8dcSSimon Schubert 
71*5796c8dcSSimon Schubert bfd *exec_bfd = NULL;
72*5796c8dcSSimon Schubert long exec_bfd_mtime = 0;
73*5796c8dcSSimon Schubert 
74*5796c8dcSSimon Schubert /* GDB currently only supports a single symbol/address space for the
75*5796c8dcSSimon Schubert    whole debug session.  When that limitation is lifted, this global
76*5796c8dcSSimon Schubert    goes away.  */
77*5796c8dcSSimon Schubert static struct target_section_table current_target_sections_1;
78*5796c8dcSSimon Schubert 
79*5796c8dcSSimon Schubert /* The set of target sections matching the sections mapped into the
80*5796c8dcSSimon Schubert    current inferior's address space.  */
81*5796c8dcSSimon Schubert static struct target_section_table *current_target_sections
82*5796c8dcSSimon Schubert   = &current_target_sections_1;
83*5796c8dcSSimon Schubert 
84*5796c8dcSSimon Schubert /* Whether to open exec and core files read-only or read-write.  */
85*5796c8dcSSimon Schubert 
86*5796c8dcSSimon Schubert int write_files = 0;
87*5796c8dcSSimon Schubert static void
88*5796c8dcSSimon Schubert show_write_files (struct ui_file *file, int from_tty,
89*5796c8dcSSimon Schubert 		  struct cmd_list_element *c, const char *value)
90*5796c8dcSSimon Schubert {
91*5796c8dcSSimon Schubert   fprintf_filtered (file, _("Writing into executable and core files is %s.\n"),
92*5796c8dcSSimon Schubert 		    value);
93*5796c8dcSSimon Schubert }
94*5796c8dcSSimon Schubert 
95*5796c8dcSSimon Schubert 
96*5796c8dcSSimon Schubert struct vmap *vmap;
97*5796c8dcSSimon Schubert 
98*5796c8dcSSimon Schubert static void
99*5796c8dcSSimon Schubert exec_open (char *args, int from_tty)
100*5796c8dcSSimon Schubert {
101*5796c8dcSSimon Schubert   target_preopen (from_tty);
102*5796c8dcSSimon Schubert   exec_file_attach (args, from_tty);
103*5796c8dcSSimon Schubert }
104*5796c8dcSSimon Schubert 
105*5796c8dcSSimon Schubert /* Close and clear exec_bfd.  If we end up with no target sections to
106*5796c8dcSSimon Schubert    read memory from, this unpushes the exec_ops target.  */
107*5796c8dcSSimon Schubert 
108*5796c8dcSSimon Schubert static void
109*5796c8dcSSimon Schubert exec_close_1 (void)
110*5796c8dcSSimon Schubert {
111*5796c8dcSSimon Schubert   if (exec_bfd)
112*5796c8dcSSimon Schubert     {
113*5796c8dcSSimon Schubert       bfd *abfd = exec_bfd;
114*5796c8dcSSimon Schubert       char *name = bfd_get_filename (abfd);
115*5796c8dcSSimon Schubert 
116*5796c8dcSSimon Schubert       if (!bfd_close (abfd))
117*5796c8dcSSimon Schubert 	warning (_("cannot close \"%s\": %s"),
118*5796c8dcSSimon Schubert 		 name, bfd_errmsg (bfd_get_error ()));
119*5796c8dcSSimon Schubert       xfree (name);
120*5796c8dcSSimon Schubert 
121*5796c8dcSSimon Schubert       /* Removing target sections may close the exec_ops target.
122*5796c8dcSSimon Schubert 	 Clear exec_bfd before doing so to prevent recursion.  */
123*5796c8dcSSimon Schubert       exec_bfd = NULL;
124*5796c8dcSSimon Schubert       exec_bfd_mtime = 0;
125*5796c8dcSSimon Schubert 
126*5796c8dcSSimon Schubert       remove_target_sections (abfd);
127*5796c8dcSSimon Schubert     }
128*5796c8dcSSimon Schubert }
129*5796c8dcSSimon Schubert 
130*5796c8dcSSimon Schubert static void
131*5796c8dcSSimon Schubert exec_close (int quitting)
132*5796c8dcSSimon Schubert {
133*5796c8dcSSimon Schubert   int need_symtab_cleanup = 0;
134*5796c8dcSSimon Schubert   struct vmap *vp, *nxt;
135*5796c8dcSSimon Schubert 
136*5796c8dcSSimon Schubert   for (nxt = vmap; nxt != NULL;)
137*5796c8dcSSimon Schubert     {
138*5796c8dcSSimon Schubert       vp = nxt;
139*5796c8dcSSimon Schubert       nxt = vp->nxt;
140*5796c8dcSSimon Schubert 
141*5796c8dcSSimon Schubert       /* if there is an objfile associated with this bfd,
142*5796c8dcSSimon Schubert          free_objfile() will do proper cleanup of objfile *and* bfd. */
143*5796c8dcSSimon Schubert 
144*5796c8dcSSimon Schubert       if (vp->objfile)
145*5796c8dcSSimon Schubert 	{
146*5796c8dcSSimon Schubert 	  free_objfile (vp->objfile);
147*5796c8dcSSimon Schubert 	  need_symtab_cleanup = 1;
148*5796c8dcSSimon Schubert 	}
149*5796c8dcSSimon Schubert       else if (vp->bfd != exec_bfd)
150*5796c8dcSSimon Schubert 	/* FIXME-leak: We should be freeing vp->name too, I think.  */
151*5796c8dcSSimon Schubert 	if (!bfd_close (vp->bfd))
152*5796c8dcSSimon Schubert 	  warning (_("cannot close \"%s\": %s"),
153*5796c8dcSSimon Schubert 		   vp->name, bfd_errmsg (bfd_get_error ()));
154*5796c8dcSSimon Schubert 
155*5796c8dcSSimon Schubert       /* FIXME: This routine is #if 0'd in symfile.c.  What should we
156*5796c8dcSSimon Schubert          be doing here?  Should we just free everything in
157*5796c8dcSSimon Schubert          vp->objfile->symtabs?  Should free_objfile do that?
158*5796c8dcSSimon Schubert          FIXME-as-well: free_objfile already free'd vp->name, so it isn't
159*5796c8dcSSimon Schubert          valid here.  */
160*5796c8dcSSimon Schubert       free_named_symtabs (vp->name);
161*5796c8dcSSimon Schubert       xfree (vp);
162*5796c8dcSSimon Schubert     }
163*5796c8dcSSimon Schubert 
164*5796c8dcSSimon Schubert   vmap = NULL;
165*5796c8dcSSimon Schubert 
166*5796c8dcSSimon Schubert   /* Delete all target sections.  */
167*5796c8dcSSimon Schubert   resize_section_table
168*5796c8dcSSimon Schubert     (current_target_sections,
169*5796c8dcSSimon Schubert      -resize_section_table (current_target_sections, 0));
170*5796c8dcSSimon Schubert 
171*5796c8dcSSimon Schubert   /* Remove exec file.  */
172*5796c8dcSSimon Schubert   exec_close_1 ();
173*5796c8dcSSimon Schubert }
174*5796c8dcSSimon Schubert 
175*5796c8dcSSimon Schubert void
176*5796c8dcSSimon Schubert exec_file_clear (int from_tty)
177*5796c8dcSSimon Schubert {
178*5796c8dcSSimon Schubert   /* Remove exec file.  */
179*5796c8dcSSimon Schubert   exec_close_1 ();
180*5796c8dcSSimon Schubert 
181*5796c8dcSSimon Schubert   if (from_tty)
182*5796c8dcSSimon Schubert     printf_unfiltered (_("No executable file now.\n"));
183*5796c8dcSSimon Schubert }
184*5796c8dcSSimon Schubert 
185*5796c8dcSSimon Schubert /* Set FILENAME as the new exec file.
186*5796c8dcSSimon Schubert 
187*5796c8dcSSimon Schubert    This function is intended to be behave essentially the same
188*5796c8dcSSimon Schubert    as exec_file_command, except that the latter will detect when
189*5796c8dcSSimon Schubert    a target is being debugged, and will ask the user whether it
190*5796c8dcSSimon Schubert    should be shut down first.  (If the answer is "no", then the
191*5796c8dcSSimon Schubert    new file is ignored.)
192*5796c8dcSSimon Schubert 
193*5796c8dcSSimon Schubert    This file is used by exec_file_command, to do the work of opening
194*5796c8dcSSimon Schubert    and processing the exec file after any prompting has happened.
195*5796c8dcSSimon Schubert 
196*5796c8dcSSimon Schubert    And, it is used by child_attach, when the attach command was
197*5796c8dcSSimon Schubert    given a pid but not a exec pathname, and the attach command could
198*5796c8dcSSimon Schubert    figure out the pathname from the pid.  (In this case, we shouldn't
199*5796c8dcSSimon Schubert    ask the user whether the current target should be shut down --
200*5796c8dcSSimon Schubert    we're supplying the exec pathname late for good reason.)  */
201*5796c8dcSSimon Schubert 
202*5796c8dcSSimon Schubert void
203*5796c8dcSSimon Schubert exec_file_attach (char *filename, int from_tty)
204*5796c8dcSSimon Schubert {
205*5796c8dcSSimon Schubert   /* Remove any previous exec file.  */
206*5796c8dcSSimon Schubert   exec_close_1 ();
207*5796c8dcSSimon Schubert 
208*5796c8dcSSimon Schubert   /* Now open and digest the file the user requested, if any.  */
209*5796c8dcSSimon Schubert 
210*5796c8dcSSimon Schubert   if (!filename)
211*5796c8dcSSimon Schubert     {
212*5796c8dcSSimon Schubert       if (from_tty)
213*5796c8dcSSimon Schubert         printf_unfiltered (_("No executable file now.\n"));
214*5796c8dcSSimon Schubert 
215*5796c8dcSSimon Schubert       set_gdbarch_from_file (NULL);
216*5796c8dcSSimon Schubert     }
217*5796c8dcSSimon Schubert   else
218*5796c8dcSSimon Schubert     {
219*5796c8dcSSimon Schubert       struct cleanup *cleanups;
220*5796c8dcSSimon Schubert       char *scratch_pathname;
221*5796c8dcSSimon Schubert       int scratch_chan;
222*5796c8dcSSimon Schubert       struct target_section *sections = NULL, *sections_end = NULL;
223*5796c8dcSSimon Schubert 
224*5796c8dcSSimon Schubert       scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename,
225*5796c8dcSSimon Schubert 		   write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
226*5796c8dcSSimon Schubert 			    &scratch_pathname);
227*5796c8dcSSimon Schubert #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
228*5796c8dcSSimon Schubert       if (scratch_chan < 0)
229*5796c8dcSSimon Schubert 	{
230*5796c8dcSSimon Schubert 	  char *exename = alloca (strlen (filename) + 5);
231*5796c8dcSSimon Schubert 	  strcat (strcpy (exename, filename), ".exe");
232*5796c8dcSSimon Schubert 	  scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename,
233*5796c8dcSSimon Schubert 	     write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
234*5796c8dcSSimon Schubert 	     &scratch_pathname);
235*5796c8dcSSimon Schubert 	}
236*5796c8dcSSimon Schubert #endif
237*5796c8dcSSimon Schubert       if (scratch_chan < 0)
238*5796c8dcSSimon Schubert 	perror_with_name (filename);
239*5796c8dcSSimon Schubert       exec_bfd = bfd_fopen (scratch_pathname, gnutarget,
240*5796c8dcSSimon Schubert 			    write_files ? FOPEN_RUB : FOPEN_RB,
241*5796c8dcSSimon Schubert 			    scratch_chan);
242*5796c8dcSSimon Schubert 
243*5796c8dcSSimon Schubert       if (!exec_bfd)
244*5796c8dcSSimon Schubert 	{
245*5796c8dcSSimon Schubert 	  close (scratch_chan);
246*5796c8dcSSimon Schubert 	  error (_("\"%s\": could not open as an executable file: %s"),
247*5796c8dcSSimon Schubert 		 scratch_pathname, bfd_errmsg (bfd_get_error ()));
248*5796c8dcSSimon Schubert 	}
249*5796c8dcSSimon Schubert 
250*5796c8dcSSimon Schubert       /* At this point, scratch_pathname and exec_bfd->name both point to the
251*5796c8dcSSimon Schubert          same malloc'd string.  However exec_close() will attempt to free it
252*5796c8dcSSimon Schubert          via the exec_bfd->name pointer, so we need to make another copy and
253*5796c8dcSSimon Schubert          leave exec_bfd as the new owner of the original copy. */
254*5796c8dcSSimon Schubert       scratch_pathname = xstrdup (scratch_pathname);
255*5796c8dcSSimon Schubert       cleanups = make_cleanup (xfree, scratch_pathname);
256*5796c8dcSSimon Schubert 
257*5796c8dcSSimon Schubert       if (!bfd_check_format (exec_bfd, bfd_object))
258*5796c8dcSSimon Schubert 	{
259*5796c8dcSSimon Schubert 	  /* Make sure to close exec_bfd, or else "run" might try to use
260*5796c8dcSSimon Schubert 	     it.  */
261*5796c8dcSSimon Schubert 	  exec_close_1 ();
262*5796c8dcSSimon Schubert 	  error (_("\"%s\": not in executable format: %s"),
263*5796c8dcSSimon Schubert 		 scratch_pathname, bfd_errmsg (bfd_get_error ()));
264*5796c8dcSSimon Schubert 	}
265*5796c8dcSSimon Schubert 
266*5796c8dcSSimon Schubert       /* FIXME - This should only be run for RS6000, but the ifdef is a poor
267*5796c8dcSSimon Schubert          way to accomplish.  */
268*5796c8dcSSimon Schubert #ifdef DEPRECATED_IBM6000_TARGET
269*5796c8dcSSimon Schubert       /* Setup initial vmap. */
270*5796c8dcSSimon Schubert 
271*5796c8dcSSimon Schubert       map_vmap (exec_bfd, 0);
272*5796c8dcSSimon Schubert       if (vmap == NULL)
273*5796c8dcSSimon Schubert 	{
274*5796c8dcSSimon Schubert 	  /* Make sure to close exec_bfd, or else "run" might try to use
275*5796c8dcSSimon Schubert 	     it.  */
276*5796c8dcSSimon Schubert 	  exec_close_1 ();
277*5796c8dcSSimon Schubert 	  error (_("\"%s\": can't find the file sections: %s"),
278*5796c8dcSSimon Schubert 		 scratch_pathname, bfd_errmsg (bfd_get_error ()));
279*5796c8dcSSimon Schubert 	}
280*5796c8dcSSimon Schubert #endif /* DEPRECATED_IBM6000_TARGET */
281*5796c8dcSSimon Schubert 
282*5796c8dcSSimon Schubert       if (build_section_table (exec_bfd, &sections, &sections_end))
283*5796c8dcSSimon Schubert 	{
284*5796c8dcSSimon Schubert 	  /* Make sure to close exec_bfd, or else "run" might try to use
285*5796c8dcSSimon Schubert 	     it.  */
286*5796c8dcSSimon Schubert 	  exec_close_1 ();
287*5796c8dcSSimon Schubert 	  error (_("\"%s\": can't find the file sections: %s"),
288*5796c8dcSSimon Schubert 		 scratch_pathname, bfd_errmsg (bfd_get_error ()));
289*5796c8dcSSimon Schubert 	}
290*5796c8dcSSimon Schubert 
291*5796c8dcSSimon Schubert       exec_bfd_mtime = bfd_get_mtime (exec_bfd);
292*5796c8dcSSimon Schubert 
293*5796c8dcSSimon Schubert       validate_files ();
294*5796c8dcSSimon Schubert 
295*5796c8dcSSimon Schubert       set_gdbarch_from_file (exec_bfd);
296*5796c8dcSSimon Schubert 
297*5796c8dcSSimon Schubert       /* Add the executable's sections to the current address spaces'
298*5796c8dcSSimon Schubert 	 list of sections.  */
299*5796c8dcSSimon Schubert       add_target_sections (sections, sections_end);
300*5796c8dcSSimon Schubert       xfree (sections);
301*5796c8dcSSimon Schubert 
302*5796c8dcSSimon Schubert       /* Tell display code (if any) about the changed file name.  */
303*5796c8dcSSimon Schubert       if (deprecated_exec_file_display_hook)
304*5796c8dcSSimon Schubert 	(*deprecated_exec_file_display_hook) (filename);
305*5796c8dcSSimon Schubert 
306*5796c8dcSSimon Schubert       do_cleanups (cleanups);
307*5796c8dcSSimon Schubert     }
308*5796c8dcSSimon Schubert   bfd_cache_close_all ();
309*5796c8dcSSimon Schubert   observer_notify_executable_changed ();
310*5796c8dcSSimon Schubert }
311*5796c8dcSSimon Schubert 
312*5796c8dcSSimon Schubert /*  Process the first arg in ARGS as the new exec file.
313*5796c8dcSSimon Schubert 
314*5796c8dcSSimon Schubert    Note that we have to explicitly ignore additional args, since we can
315*5796c8dcSSimon Schubert    be called from file_command(), which also calls symbol_file_command()
316*5796c8dcSSimon Schubert    which can take multiple args.
317*5796c8dcSSimon Schubert 
318*5796c8dcSSimon Schubert    If ARGS is NULL, we just want to close the exec file. */
319*5796c8dcSSimon Schubert 
320*5796c8dcSSimon Schubert static void
321*5796c8dcSSimon Schubert exec_file_command (char *args, int from_tty)
322*5796c8dcSSimon Schubert {
323*5796c8dcSSimon Schubert   char **argv;
324*5796c8dcSSimon Schubert   char *filename;
325*5796c8dcSSimon Schubert 
326*5796c8dcSSimon Schubert   if (from_tty && target_has_execution
327*5796c8dcSSimon Schubert       && !query (_("A program is being debugged already.\n"
328*5796c8dcSSimon Schubert 		   "Are you sure you want to change the file? ")))
329*5796c8dcSSimon Schubert     error (_("File not changed."));
330*5796c8dcSSimon Schubert 
331*5796c8dcSSimon Schubert   if (args)
332*5796c8dcSSimon Schubert     {
333*5796c8dcSSimon Schubert       struct cleanup *cleanups;
334*5796c8dcSSimon Schubert 
335*5796c8dcSSimon Schubert       /* Scan through the args and pick up the first non option arg
336*5796c8dcSSimon Schubert          as the filename.  */
337*5796c8dcSSimon Schubert 
338*5796c8dcSSimon Schubert       argv = gdb_buildargv (args);
339*5796c8dcSSimon Schubert       cleanups = make_cleanup_freeargv (argv);
340*5796c8dcSSimon Schubert 
341*5796c8dcSSimon Schubert       for (; (*argv != NULL) && (**argv == '-'); argv++)
342*5796c8dcSSimon Schubert         {;
343*5796c8dcSSimon Schubert         }
344*5796c8dcSSimon Schubert       if (*argv == NULL)
345*5796c8dcSSimon Schubert         error (_("No executable file name was specified"));
346*5796c8dcSSimon Schubert 
347*5796c8dcSSimon Schubert       filename = tilde_expand (*argv);
348*5796c8dcSSimon Schubert       make_cleanup (xfree, filename);
349*5796c8dcSSimon Schubert       exec_file_attach (filename, from_tty);
350*5796c8dcSSimon Schubert 
351*5796c8dcSSimon Schubert       do_cleanups (cleanups);
352*5796c8dcSSimon Schubert     }
353*5796c8dcSSimon Schubert   else
354*5796c8dcSSimon Schubert     exec_file_attach (NULL, from_tty);
355*5796c8dcSSimon Schubert }
356*5796c8dcSSimon Schubert 
357*5796c8dcSSimon Schubert /* Set both the exec file and the symbol file, in one command.
358*5796c8dcSSimon Schubert    What a novelty.  Why did GDB go through four major releases before this
359*5796c8dcSSimon Schubert    command was added?  */
360*5796c8dcSSimon Schubert 
361*5796c8dcSSimon Schubert static void
362*5796c8dcSSimon Schubert file_command (char *arg, int from_tty)
363*5796c8dcSSimon Schubert {
364*5796c8dcSSimon Schubert   /* FIXME, if we lose on reading the symbol file, we should revert
365*5796c8dcSSimon Schubert      the exec file, but that's rough.  */
366*5796c8dcSSimon Schubert   exec_file_command (arg, from_tty);
367*5796c8dcSSimon Schubert   symbol_file_command (arg, from_tty);
368*5796c8dcSSimon Schubert   if (deprecated_file_changed_hook)
369*5796c8dcSSimon Schubert     deprecated_file_changed_hook (arg);
370*5796c8dcSSimon Schubert }
371*5796c8dcSSimon Schubert 
372*5796c8dcSSimon Schubert 
373*5796c8dcSSimon Schubert /* Locate all mappable sections of a BFD file.
374*5796c8dcSSimon Schubert    table_pp_char is a char * to get it through bfd_map_over_sections;
375*5796c8dcSSimon Schubert    we cast it back to its proper type.  */
376*5796c8dcSSimon Schubert 
377*5796c8dcSSimon Schubert static void
378*5796c8dcSSimon Schubert add_to_section_table (bfd *abfd, struct bfd_section *asect,
379*5796c8dcSSimon Schubert 		      void *table_pp_char)
380*5796c8dcSSimon Schubert {
381*5796c8dcSSimon Schubert   struct target_section **table_pp = (struct target_section **) table_pp_char;
382*5796c8dcSSimon Schubert   flagword aflag;
383*5796c8dcSSimon Schubert 
384*5796c8dcSSimon Schubert   /* Check the section flags, but do not discard zero-length sections, since
385*5796c8dcSSimon Schubert      some symbols may still be attached to this section.  For instance, we
386*5796c8dcSSimon Schubert      encountered on sparc-solaris 2.10 a shared library with an empty .bss
387*5796c8dcSSimon Schubert      section to which a symbol named "_end" was attached.  The address
388*5796c8dcSSimon Schubert      of this symbol still needs to be relocated.  */
389*5796c8dcSSimon Schubert   aflag = bfd_get_section_flags (abfd, asect);
390*5796c8dcSSimon Schubert   if (!(aflag & SEC_ALLOC))
391*5796c8dcSSimon Schubert     return;
392*5796c8dcSSimon Schubert 
393*5796c8dcSSimon Schubert   (*table_pp)->bfd = abfd;
394*5796c8dcSSimon Schubert   (*table_pp)->the_bfd_section = asect;
395*5796c8dcSSimon Schubert   (*table_pp)->addr = bfd_section_vma (abfd, asect);
396*5796c8dcSSimon Schubert   (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
397*5796c8dcSSimon Schubert   (*table_pp)++;
398*5796c8dcSSimon Schubert }
399*5796c8dcSSimon Schubert 
400*5796c8dcSSimon Schubert int
401*5796c8dcSSimon Schubert resize_section_table (struct target_section_table *table, int num_added)
402*5796c8dcSSimon Schubert {
403*5796c8dcSSimon Schubert   struct target_section *old_value;
404*5796c8dcSSimon Schubert   int old_count;
405*5796c8dcSSimon Schubert   int new_count;
406*5796c8dcSSimon Schubert 
407*5796c8dcSSimon Schubert   old_value = table->sections;
408*5796c8dcSSimon Schubert   old_count = table->sections_end - table->sections;
409*5796c8dcSSimon Schubert 
410*5796c8dcSSimon Schubert   new_count = num_added + old_count;
411*5796c8dcSSimon Schubert 
412*5796c8dcSSimon Schubert   if (new_count)
413*5796c8dcSSimon Schubert     {
414*5796c8dcSSimon Schubert       table->sections = xrealloc (table->sections,
415*5796c8dcSSimon Schubert 				  sizeof (struct target_section) * new_count);
416*5796c8dcSSimon Schubert       table->sections_end = table->sections + new_count;
417*5796c8dcSSimon Schubert     }
418*5796c8dcSSimon Schubert   else
419*5796c8dcSSimon Schubert     {
420*5796c8dcSSimon Schubert       xfree (table->sections);
421*5796c8dcSSimon Schubert       table->sections = table->sections_end = NULL;
422*5796c8dcSSimon Schubert     }
423*5796c8dcSSimon Schubert 
424*5796c8dcSSimon Schubert   return old_count;
425*5796c8dcSSimon Schubert }
426*5796c8dcSSimon Schubert 
427*5796c8dcSSimon Schubert /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
428*5796c8dcSSimon Schubert    Returns 0 if OK, 1 on error.  */
429*5796c8dcSSimon Schubert 
430*5796c8dcSSimon Schubert int
431*5796c8dcSSimon Schubert build_section_table (struct bfd *some_bfd, struct target_section **start,
432*5796c8dcSSimon Schubert 		     struct target_section **end)
433*5796c8dcSSimon Schubert {
434*5796c8dcSSimon Schubert   unsigned count;
435*5796c8dcSSimon Schubert 
436*5796c8dcSSimon Schubert   count = bfd_count_sections (some_bfd);
437*5796c8dcSSimon Schubert   if (*start)
438*5796c8dcSSimon Schubert     xfree (* start);
439*5796c8dcSSimon Schubert   *start = (struct target_section *) xmalloc (count * sizeof (**start));
440*5796c8dcSSimon Schubert   *end = *start;
441*5796c8dcSSimon Schubert   bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
442*5796c8dcSSimon Schubert   if (*end > *start + count)
443*5796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
444*5796c8dcSSimon Schubert   /* We could realloc the table, but it probably loses for most files.  */
445*5796c8dcSSimon Schubert   return 0;
446*5796c8dcSSimon Schubert }
447*5796c8dcSSimon Schubert 
448*5796c8dcSSimon Schubert /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
449*5796c8dcSSimon Schubert    current set of target sections.  */
450*5796c8dcSSimon Schubert 
451*5796c8dcSSimon Schubert void
452*5796c8dcSSimon Schubert add_target_sections (struct target_section *sections,
453*5796c8dcSSimon Schubert 		     struct target_section *sections_end)
454*5796c8dcSSimon Schubert {
455*5796c8dcSSimon Schubert   int count;
456*5796c8dcSSimon Schubert   struct target_section_table *table = current_target_sections;
457*5796c8dcSSimon Schubert 
458*5796c8dcSSimon Schubert   count = sections_end - sections;
459*5796c8dcSSimon Schubert 
460*5796c8dcSSimon Schubert   if (count > 0)
461*5796c8dcSSimon Schubert     {
462*5796c8dcSSimon Schubert       int space = resize_section_table (table, count);
463*5796c8dcSSimon Schubert       memcpy (table->sections + space,
464*5796c8dcSSimon Schubert 	      sections, count * sizeof (sections[0]));
465*5796c8dcSSimon Schubert 
466*5796c8dcSSimon Schubert       /* If these are the first file sections we can provide memory
467*5796c8dcSSimon Schubert 	 from, push the file_stratum target.  */
468*5796c8dcSSimon Schubert       if (space == 0)
469*5796c8dcSSimon Schubert 	push_target (&exec_ops);
470*5796c8dcSSimon Schubert     }
471*5796c8dcSSimon Schubert }
472*5796c8dcSSimon Schubert 
473*5796c8dcSSimon Schubert /* Remove all target sections taken from ABFD.  */
474*5796c8dcSSimon Schubert 
475*5796c8dcSSimon Schubert void
476*5796c8dcSSimon Schubert remove_target_sections (bfd *abfd)
477*5796c8dcSSimon Schubert {
478*5796c8dcSSimon Schubert   struct target_section *src, *dest;
479*5796c8dcSSimon Schubert 
480*5796c8dcSSimon Schubert   struct target_section_table *table = current_target_sections;
481*5796c8dcSSimon Schubert 
482*5796c8dcSSimon Schubert   dest = table->sections;
483*5796c8dcSSimon Schubert   for (src = table->sections; src < table->sections_end; src++)
484*5796c8dcSSimon Schubert     if (src->bfd != abfd)
485*5796c8dcSSimon Schubert       {
486*5796c8dcSSimon Schubert 	/* Keep this section.  */
487*5796c8dcSSimon Schubert 	if (dest < src)
488*5796c8dcSSimon Schubert 	  *dest = *src;
489*5796c8dcSSimon Schubert 	dest++;
490*5796c8dcSSimon Schubert       }
491*5796c8dcSSimon Schubert 
492*5796c8dcSSimon Schubert   /* If we've dropped any sections, resize the section table.  */
493*5796c8dcSSimon Schubert   if (dest < src)
494*5796c8dcSSimon Schubert     {
495*5796c8dcSSimon Schubert       int old_count;
496*5796c8dcSSimon Schubert 
497*5796c8dcSSimon Schubert       old_count = resize_section_table (table, dest - src);
498*5796c8dcSSimon Schubert 
499*5796c8dcSSimon Schubert       /* If we don't have any more sections to read memory from,
500*5796c8dcSSimon Schubert 	 remove the file_stratum target from the stack.  */
501*5796c8dcSSimon Schubert       if (old_count + (dest - src) == 0)
502*5796c8dcSSimon Schubert 	unpush_target (&exec_ops);
503*5796c8dcSSimon Schubert     }
504*5796c8dcSSimon Schubert }
505*5796c8dcSSimon Schubert 
506*5796c8dcSSimon Schubert 
507*5796c8dcSSimon Schubert static void
508*5796c8dcSSimon Schubert bfdsec_to_vmap (struct bfd *abfd, struct bfd_section *sect, void *arg3)
509*5796c8dcSSimon Schubert {
510*5796c8dcSSimon Schubert   struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *) arg3;
511*5796c8dcSSimon Schubert   struct vmap *vp;
512*5796c8dcSSimon Schubert 
513*5796c8dcSSimon Schubert   vp = vmap_bfd->pvmap;
514*5796c8dcSSimon Schubert 
515*5796c8dcSSimon Schubert   if ((bfd_get_section_flags (abfd, sect) & SEC_LOAD) == 0)
516*5796c8dcSSimon Schubert     return;
517*5796c8dcSSimon Schubert 
518*5796c8dcSSimon Schubert   if (strcmp (bfd_section_name (abfd, sect), ".text") == 0)
519*5796c8dcSSimon Schubert     {
520*5796c8dcSSimon Schubert       vp->tstart = bfd_section_vma (abfd, sect);
521*5796c8dcSSimon Schubert       vp->tend = vp->tstart + bfd_section_size (abfd, sect);
522*5796c8dcSSimon Schubert       vp->tvma = bfd_section_vma (abfd, sect);
523*5796c8dcSSimon Schubert       vp->toffs = sect->filepos;
524*5796c8dcSSimon Schubert     }
525*5796c8dcSSimon Schubert   else if (strcmp (bfd_section_name (abfd, sect), ".data") == 0)
526*5796c8dcSSimon Schubert     {
527*5796c8dcSSimon Schubert       vp->dstart = bfd_section_vma (abfd, sect);
528*5796c8dcSSimon Schubert       vp->dend = vp->dstart + bfd_section_size (abfd, sect);
529*5796c8dcSSimon Schubert       vp->dvma = bfd_section_vma (abfd, sect);
530*5796c8dcSSimon Schubert     }
531*5796c8dcSSimon Schubert   /* Silently ignore other types of sections. (FIXME?)  */
532*5796c8dcSSimon Schubert }
533*5796c8dcSSimon Schubert 
534*5796c8dcSSimon Schubert /* Make a vmap for ABFD which might be a member of the archive ARCH.
535*5796c8dcSSimon Schubert    Return the new vmap.  */
536*5796c8dcSSimon Schubert 
537*5796c8dcSSimon Schubert struct vmap *
538*5796c8dcSSimon Schubert map_vmap (bfd *abfd, bfd *arch)
539*5796c8dcSSimon Schubert {
540*5796c8dcSSimon Schubert   struct vmap_and_bfd vmap_bfd;
541*5796c8dcSSimon Schubert   struct vmap *vp, **vpp;
542*5796c8dcSSimon Schubert 
543*5796c8dcSSimon Schubert   vp = (struct vmap *) xmalloc (sizeof (*vp));
544*5796c8dcSSimon Schubert   memset ((char *) vp, '\0', sizeof (*vp));
545*5796c8dcSSimon Schubert   vp->nxt = 0;
546*5796c8dcSSimon Schubert   vp->bfd = abfd;
547*5796c8dcSSimon Schubert   vp->name = bfd_get_filename (arch ? arch : abfd);
548*5796c8dcSSimon Schubert   vp->member = arch ? bfd_get_filename (abfd) : "";
549*5796c8dcSSimon Schubert 
550*5796c8dcSSimon Schubert   vmap_bfd.pbfd = arch;
551*5796c8dcSSimon Schubert   vmap_bfd.pvmap = vp;
552*5796c8dcSSimon Schubert   bfd_map_over_sections (abfd, bfdsec_to_vmap, &vmap_bfd);
553*5796c8dcSSimon Schubert 
554*5796c8dcSSimon Schubert   /* Find the end of the list and append. */
555*5796c8dcSSimon Schubert   for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt)
556*5796c8dcSSimon Schubert     ;
557*5796c8dcSSimon Schubert   *vpp = vp;
558*5796c8dcSSimon Schubert 
559*5796c8dcSSimon Schubert   return vp;
560*5796c8dcSSimon Schubert }
561*5796c8dcSSimon Schubert 
562*5796c8dcSSimon Schubert 
563*5796c8dcSSimon Schubert int
564*5796c8dcSSimon Schubert section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
565*5796c8dcSSimon Schubert 				   ULONGEST offset, LONGEST len,
566*5796c8dcSSimon Schubert 				   struct target_section *sections,
567*5796c8dcSSimon Schubert 				   struct target_section *sections_end,
568*5796c8dcSSimon Schubert 				   const char *section_name)
569*5796c8dcSSimon Schubert {
570*5796c8dcSSimon Schubert   int res;
571*5796c8dcSSimon Schubert   struct target_section *p;
572*5796c8dcSSimon Schubert   ULONGEST memaddr = offset;
573*5796c8dcSSimon Schubert   ULONGEST memend = memaddr + len;
574*5796c8dcSSimon Schubert 
575*5796c8dcSSimon Schubert   if (len <= 0)
576*5796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
577*5796c8dcSSimon Schubert 
578*5796c8dcSSimon Schubert   for (p = sections; p < sections_end; p++)
579*5796c8dcSSimon Schubert     {
580*5796c8dcSSimon Schubert       if (section_name && strcmp (section_name, p->the_bfd_section->name) != 0)
581*5796c8dcSSimon Schubert 	continue;		/* not the section we need */
582*5796c8dcSSimon Schubert       if (memaddr >= p->addr)
583*5796c8dcSSimon Schubert         {
584*5796c8dcSSimon Schubert 	  if (memend <= p->endaddr)
585*5796c8dcSSimon Schubert 	    {
586*5796c8dcSSimon Schubert 	      /* Entire transfer is within this section.  */
587*5796c8dcSSimon Schubert 	      if (writebuf)
588*5796c8dcSSimon Schubert 		res = bfd_set_section_contents (p->bfd, p->the_bfd_section,
589*5796c8dcSSimon Schubert 						writebuf, memaddr - p->addr,
590*5796c8dcSSimon Schubert 						len);
591*5796c8dcSSimon Schubert 	      else
592*5796c8dcSSimon Schubert 		res = bfd_get_section_contents (p->bfd, p->the_bfd_section,
593*5796c8dcSSimon Schubert 						readbuf, memaddr - p->addr,
594*5796c8dcSSimon Schubert 						len);
595*5796c8dcSSimon Schubert 	      return (res != 0) ? len : 0;
596*5796c8dcSSimon Schubert 	    }
597*5796c8dcSSimon Schubert 	  else if (memaddr >= p->endaddr)
598*5796c8dcSSimon Schubert 	    {
599*5796c8dcSSimon Schubert 	      /* This section ends before the transfer starts.  */
600*5796c8dcSSimon Schubert 	      continue;
601*5796c8dcSSimon Schubert 	    }
602*5796c8dcSSimon Schubert 	  else
603*5796c8dcSSimon Schubert 	    {
604*5796c8dcSSimon Schubert 	      /* This section overlaps the transfer.  Just do half.  */
605*5796c8dcSSimon Schubert 	      len = p->endaddr - memaddr;
606*5796c8dcSSimon Schubert 	      if (writebuf)
607*5796c8dcSSimon Schubert 		res = bfd_set_section_contents (p->bfd, p->the_bfd_section,
608*5796c8dcSSimon Schubert 						writebuf, memaddr - p->addr,
609*5796c8dcSSimon Schubert 						len);
610*5796c8dcSSimon Schubert 	      else
611*5796c8dcSSimon Schubert 		res = bfd_get_section_contents (p->bfd, p->the_bfd_section,
612*5796c8dcSSimon Schubert 						readbuf, memaddr - p->addr,
613*5796c8dcSSimon Schubert 						len);
614*5796c8dcSSimon Schubert 	      return (res != 0) ? len : 0;
615*5796c8dcSSimon Schubert 	    }
616*5796c8dcSSimon Schubert         }
617*5796c8dcSSimon Schubert     }
618*5796c8dcSSimon Schubert 
619*5796c8dcSSimon Schubert   return 0;			/* We can't help */
620*5796c8dcSSimon Schubert }
621*5796c8dcSSimon Schubert 
622*5796c8dcSSimon Schubert struct target_section_table *
623*5796c8dcSSimon Schubert exec_get_section_table (struct target_ops *ops)
624*5796c8dcSSimon Schubert {
625*5796c8dcSSimon Schubert   return current_target_sections;
626*5796c8dcSSimon Schubert }
627*5796c8dcSSimon Schubert 
628*5796c8dcSSimon Schubert static LONGEST
629*5796c8dcSSimon Schubert exec_xfer_partial (struct target_ops *ops, enum target_object object,
630*5796c8dcSSimon Schubert 		   const char *annex, gdb_byte *readbuf,
631*5796c8dcSSimon Schubert 		   const gdb_byte *writebuf,
632*5796c8dcSSimon Schubert 		   ULONGEST offset, LONGEST len)
633*5796c8dcSSimon Schubert {
634*5796c8dcSSimon Schubert   struct target_section_table *table = target_get_section_table (ops);
635*5796c8dcSSimon Schubert 
636*5796c8dcSSimon Schubert   if (object == TARGET_OBJECT_MEMORY)
637*5796c8dcSSimon Schubert     return section_table_xfer_memory_partial (readbuf, writebuf,
638*5796c8dcSSimon Schubert 					      offset, len,
639*5796c8dcSSimon Schubert 					      table->sections,
640*5796c8dcSSimon Schubert 					      table->sections_end,
641*5796c8dcSSimon Schubert 					      NULL);
642*5796c8dcSSimon Schubert   else
643*5796c8dcSSimon Schubert     return -1;
644*5796c8dcSSimon Schubert }
645*5796c8dcSSimon Schubert 
646*5796c8dcSSimon Schubert 
647*5796c8dcSSimon Schubert void
648*5796c8dcSSimon Schubert print_section_info (struct target_section_table *t, bfd *abfd)
649*5796c8dcSSimon Schubert {
650*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
651*5796c8dcSSimon Schubert   struct target_section *p;
652*5796c8dcSSimon Schubert   /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64.  */
653*5796c8dcSSimon Schubert   int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
654*5796c8dcSSimon Schubert 
655*5796c8dcSSimon Schubert   printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
656*5796c8dcSSimon Schubert   wrap_here ("        ");
657*5796c8dcSSimon Schubert   printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
658*5796c8dcSSimon Schubert   if (abfd == exec_bfd)
659*5796c8dcSSimon Schubert     printf_filtered (_("\tEntry point: %s\n"),
660*5796c8dcSSimon Schubert                      paddress (gdbarch, bfd_get_start_address (abfd)));
661*5796c8dcSSimon Schubert   for (p = t->sections; p < t->sections_end; p++)
662*5796c8dcSSimon Schubert     {
663*5796c8dcSSimon Schubert       printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
664*5796c8dcSSimon Schubert       printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
665*5796c8dcSSimon Schubert 
666*5796c8dcSSimon Schubert       /* FIXME: A format of "08l" is not wide enough for file offsets
667*5796c8dcSSimon Schubert 	 larger than 4GB.  OTOH, making it "016l" isn't desirable either
668*5796c8dcSSimon Schubert 	 since most output will then be much wider than necessary.  It
669*5796c8dcSSimon Schubert 	 may make sense to test the size of the file and choose the
670*5796c8dcSSimon Schubert 	 format string accordingly.  */
671*5796c8dcSSimon Schubert       /* FIXME: i18n: Need to rewrite this sentence.  */
672*5796c8dcSSimon Schubert       if (info_verbose)
673*5796c8dcSSimon Schubert 	printf_filtered (" @ %s",
674*5796c8dcSSimon Schubert 			 hex_string_custom (p->the_bfd_section->filepos, 8));
675*5796c8dcSSimon Schubert       printf_filtered (" is %s", bfd_section_name (p->bfd, p->the_bfd_section));
676*5796c8dcSSimon Schubert       if (p->bfd != abfd)
677*5796c8dcSSimon Schubert 	printf_filtered (" in %s", bfd_get_filename (p->bfd));
678*5796c8dcSSimon Schubert       printf_filtered ("\n");
679*5796c8dcSSimon Schubert     }
680*5796c8dcSSimon Schubert }
681*5796c8dcSSimon Schubert 
682*5796c8dcSSimon Schubert static void
683*5796c8dcSSimon Schubert exec_files_info (struct target_ops *t)
684*5796c8dcSSimon Schubert {
685*5796c8dcSSimon Schubert   print_section_info (current_target_sections, exec_bfd);
686*5796c8dcSSimon Schubert 
687*5796c8dcSSimon Schubert   if (vmap)
688*5796c8dcSSimon Schubert     {
689*5796c8dcSSimon Schubert       int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
690*5796c8dcSSimon Schubert       struct vmap *vp;
691*5796c8dcSSimon Schubert 
692*5796c8dcSSimon Schubert       printf_unfiltered (_("\tMapping info for file `%s'.\n"), vmap->name);
693*5796c8dcSSimon Schubert       printf_unfiltered ("\t  %*s   %*s   %*s   %*s %8.8s %s\n",
694*5796c8dcSSimon Schubert 			 addr_size * 2, "tstart",
695*5796c8dcSSimon Schubert 			 addr_size * 2, "tend",
696*5796c8dcSSimon Schubert 			 addr_size * 2, "dstart",
697*5796c8dcSSimon Schubert 			 addr_size * 2, "dend",
698*5796c8dcSSimon Schubert 			 "section",
699*5796c8dcSSimon Schubert 			 "file(member)");
700*5796c8dcSSimon Schubert 
701*5796c8dcSSimon Schubert       for (vp = vmap; vp; vp = vp->nxt)
702*5796c8dcSSimon Schubert 	printf_unfiltered ("\t0x%s 0x%s 0x%s 0x%s %s%s%s%s\n",
703*5796c8dcSSimon Schubert 			   phex (vp->tstart, addr_size),
704*5796c8dcSSimon Schubert 			   phex (vp->tend, addr_size),
705*5796c8dcSSimon Schubert 			   phex (vp->dstart, addr_size),
706*5796c8dcSSimon Schubert 			   phex (vp->dend, addr_size),
707*5796c8dcSSimon Schubert 			   vp->name,
708*5796c8dcSSimon Schubert 			   *vp->member ? "(" : "", vp->member,
709*5796c8dcSSimon Schubert 			   *vp->member ? ")" : "");
710*5796c8dcSSimon Schubert     }
711*5796c8dcSSimon Schubert }
712*5796c8dcSSimon Schubert 
713*5796c8dcSSimon Schubert static void
714*5796c8dcSSimon Schubert set_section_command (char *args, int from_tty)
715*5796c8dcSSimon Schubert {
716*5796c8dcSSimon Schubert   struct target_section *p;
717*5796c8dcSSimon Schubert   char *secname;
718*5796c8dcSSimon Schubert   unsigned seclen;
719*5796c8dcSSimon Schubert   unsigned long secaddr;
720*5796c8dcSSimon Schubert   char secprint[100];
721*5796c8dcSSimon Schubert   long offset;
722*5796c8dcSSimon Schubert   struct target_section_table *table;
723*5796c8dcSSimon Schubert 
724*5796c8dcSSimon Schubert   if (args == 0)
725*5796c8dcSSimon Schubert     error (_("Must specify section name and its virtual address"));
726*5796c8dcSSimon Schubert 
727*5796c8dcSSimon Schubert   /* Parse out section name */
728*5796c8dcSSimon Schubert   for (secname = args; !isspace (*args); args++);
729*5796c8dcSSimon Schubert   seclen = args - secname;
730*5796c8dcSSimon Schubert 
731*5796c8dcSSimon Schubert   /* Parse out new virtual address */
732*5796c8dcSSimon Schubert   secaddr = parse_and_eval_address (args);
733*5796c8dcSSimon Schubert 
734*5796c8dcSSimon Schubert   table = current_target_sections;
735*5796c8dcSSimon Schubert   for (p = table->sections; p < table->sections_end; p++)
736*5796c8dcSSimon Schubert     {
737*5796c8dcSSimon Schubert       if (!strncmp (secname, bfd_section_name (exec_bfd, p->the_bfd_section), seclen)
738*5796c8dcSSimon Schubert 	  && bfd_section_name (exec_bfd, p->the_bfd_section)[seclen] == '\0')
739*5796c8dcSSimon Schubert 	{
740*5796c8dcSSimon Schubert 	  offset = secaddr - p->addr;
741*5796c8dcSSimon Schubert 	  p->addr += offset;
742*5796c8dcSSimon Schubert 	  p->endaddr += offset;
743*5796c8dcSSimon Schubert 	  if (from_tty)
744*5796c8dcSSimon Schubert 	    exec_files_info (&exec_ops);
745*5796c8dcSSimon Schubert 	  return;
746*5796c8dcSSimon Schubert 	}
747*5796c8dcSSimon Schubert     }
748*5796c8dcSSimon Schubert   if (seclen >= sizeof (secprint))
749*5796c8dcSSimon Schubert     seclen = sizeof (secprint) - 1;
750*5796c8dcSSimon Schubert   strncpy (secprint, secname, seclen);
751*5796c8dcSSimon Schubert   secprint[seclen] = '\0';
752*5796c8dcSSimon Schubert   error (_("Section %s not found"), secprint);
753*5796c8dcSSimon Schubert }
754*5796c8dcSSimon Schubert 
755*5796c8dcSSimon Schubert /* If we can find a section in FILENAME with BFD index INDEX, adjust
756*5796c8dcSSimon Schubert    it to ADDRESS.  */
757*5796c8dcSSimon Schubert 
758*5796c8dcSSimon Schubert void
759*5796c8dcSSimon Schubert exec_set_section_address (const char *filename, int index, CORE_ADDR address)
760*5796c8dcSSimon Schubert {
761*5796c8dcSSimon Schubert   struct target_section *p;
762*5796c8dcSSimon Schubert   struct target_section_table *table;
763*5796c8dcSSimon Schubert 
764*5796c8dcSSimon Schubert   table = current_target_sections;
765*5796c8dcSSimon Schubert   for (p = table->sections; p < table->sections_end; p++)
766*5796c8dcSSimon Schubert     {
767*5796c8dcSSimon Schubert       if (strcmp (filename, p->bfd->filename) == 0
768*5796c8dcSSimon Schubert 	  && index == p->the_bfd_section->index)
769*5796c8dcSSimon Schubert 	{
770*5796c8dcSSimon Schubert 	  p->endaddr += address - p->addr;
771*5796c8dcSSimon Schubert 	  p->addr = address;
772*5796c8dcSSimon Schubert 	}
773*5796c8dcSSimon Schubert     }
774*5796c8dcSSimon Schubert }
775*5796c8dcSSimon Schubert 
776*5796c8dcSSimon Schubert /* If mourn is being called in all the right places, this could be say
777*5796c8dcSSimon Schubert    `gdb internal error' (since generic_mourn calls
778*5796c8dcSSimon Schubert    breakpoint_init_inferior).  */
779*5796c8dcSSimon Schubert 
780*5796c8dcSSimon Schubert static int
781*5796c8dcSSimon Schubert ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
782*5796c8dcSSimon Schubert {
783*5796c8dcSSimon Schubert   return 0;
784*5796c8dcSSimon Schubert }
785*5796c8dcSSimon Schubert 
786*5796c8dcSSimon Schubert static int
787*5796c8dcSSimon Schubert exec_has_memory (struct target_ops *ops)
788*5796c8dcSSimon Schubert {
789*5796c8dcSSimon Schubert   /* We can provide memory if we have any file/target sections to read
790*5796c8dcSSimon Schubert      from.  */
791*5796c8dcSSimon Schubert   return (current_target_sections->sections
792*5796c8dcSSimon Schubert 	  != current_target_sections->sections_end);
793*5796c8dcSSimon Schubert }
794*5796c8dcSSimon Schubert 
795*5796c8dcSSimon Schubert /* Find mapped memory. */
796*5796c8dcSSimon Schubert 
797*5796c8dcSSimon Schubert extern void
798*5796c8dcSSimon Schubert exec_set_find_memory_regions (int (*func) (int (*) (CORE_ADDR,
799*5796c8dcSSimon Schubert 						    unsigned long,
800*5796c8dcSSimon Schubert 						    int, int, int,
801*5796c8dcSSimon Schubert 						    void *),
802*5796c8dcSSimon Schubert 					   void *))
803*5796c8dcSSimon Schubert {
804*5796c8dcSSimon Schubert   exec_ops.to_find_memory_regions = func;
805*5796c8dcSSimon Schubert }
806*5796c8dcSSimon Schubert 
807*5796c8dcSSimon Schubert static char *exec_make_note_section (bfd *, int *);
808*5796c8dcSSimon Schubert 
809*5796c8dcSSimon Schubert /* Fill in the exec file target vector.  Very few entries need to be
810*5796c8dcSSimon Schubert    defined.  */
811*5796c8dcSSimon Schubert 
812*5796c8dcSSimon Schubert static void
813*5796c8dcSSimon Schubert init_exec_ops (void)
814*5796c8dcSSimon Schubert {
815*5796c8dcSSimon Schubert   exec_ops.to_shortname = "exec";
816*5796c8dcSSimon Schubert   exec_ops.to_longname = "Local exec file";
817*5796c8dcSSimon Schubert   exec_ops.to_doc = "Use an executable file as a target.\n\
818*5796c8dcSSimon Schubert Specify the filename of the executable file.";
819*5796c8dcSSimon Schubert   exec_ops.to_open = exec_open;
820*5796c8dcSSimon Schubert   exec_ops.to_close = exec_close;
821*5796c8dcSSimon Schubert   exec_ops.to_attach = find_default_attach;
822*5796c8dcSSimon Schubert   exec_ops.to_xfer_partial = exec_xfer_partial;
823*5796c8dcSSimon Schubert   exec_ops.to_get_section_table = exec_get_section_table;
824*5796c8dcSSimon Schubert   exec_ops.to_files_info = exec_files_info;
825*5796c8dcSSimon Schubert   exec_ops.to_insert_breakpoint = ignore;
826*5796c8dcSSimon Schubert   exec_ops.to_remove_breakpoint = ignore;
827*5796c8dcSSimon Schubert   exec_ops.to_create_inferior = find_default_create_inferior;
828*5796c8dcSSimon Schubert   exec_ops.to_stratum = file_stratum;
829*5796c8dcSSimon Schubert   exec_ops.to_has_memory = exec_has_memory;
830*5796c8dcSSimon Schubert   exec_ops.to_make_corefile_notes = exec_make_note_section;
831*5796c8dcSSimon Schubert   exec_ops.to_magic = OPS_MAGIC;
832*5796c8dcSSimon Schubert }
833*5796c8dcSSimon Schubert 
834*5796c8dcSSimon Schubert void
835*5796c8dcSSimon Schubert _initialize_exec (void)
836*5796c8dcSSimon Schubert {
837*5796c8dcSSimon Schubert   struct cmd_list_element *c;
838*5796c8dcSSimon Schubert 
839*5796c8dcSSimon Schubert   init_exec_ops ();
840*5796c8dcSSimon Schubert 
841*5796c8dcSSimon Schubert   if (!dbx_commands)
842*5796c8dcSSimon Schubert     {
843*5796c8dcSSimon Schubert       c = add_cmd ("file", class_files, file_command, _("\
844*5796c8dcSSimon Schubert Use FILE as program to be debugged.\n\
845*5796c8dcSSimon Schubert It is read for its symbols, for getting the contents of pure memory,\n\
846*5796c8dcSSimon Schubert and it is the program executed when you use the `run' command.\n\
847*5796c8dcSSimon Schubert If FILE cannot be found as specified, your execution directory path\n\
848*5796c8dcSSimon Schubert ($PATH) is searched for a command of that name.\n\
849*5796c8dcSSimon Schubert No arg means to have no executable file and no symbols."), &cmdlist);
850*5796c8dcSSimon Schubert       set_cmd_completer (c, filename_completer);
851*5796c8dcSSimon Schubert     }
852*5796c8dcSSimon Schubert 
853*5796c8dcSSimon Schubert   c = add_cmd ("exec-file", class_files, exec_file_command, _("\
854*5796c8dcSSimon Schubert Use FILE as program for getting contents of pure memory.\n\
855*5796c8dcSSimon Schubert If FILE cannot be found as specified, your execution directory path\n\
856*5796c8dcSSimon Schubert is searched for a command of that name.\n\
857*5796c8dcSSimon Schubert No arg means have no executable file."), &cmdlist);
858*5796c8dcSSimon Schubert   set_cmd_completer (c, filename_completer);
859*5796c8dcSSimon Schubert 
860*5796c8dcSSimon Schubert   add_com ("section", class_files, set_section_command, _("\
861*5796c8dcSSimon Schubert Change the base address of section SECTION of the exec file to ADDR.\n\
862*5796c8dcSSimon Schubert This can be used if the exec file does not contain section addresses,\n\
863*5796c8dcSSimon Schubert (such as in the a.out format), or when the addresses specified in the\n\
864*5796c8dcSSimon Schubert file itself are wrong.  Each section must be changed separately.  The\n\
865*5796c8dcSSimon Schubert ``info files'' command lists all the sections and their addresses."));
866*5796c8dcSSimon Schubert 
867*5796c8dcSSimon Schubert   add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
868*5796c8dcSSimon Schubert Set writing into executable and core files."), _("\
869*5796c8dcSSimon Schubert Show writing into executable and core files."), NULL,
870*5796c8dcSSimon Schubert 			   NULL,
871*5796c8dcSSimon Schubert 			   show_write_files,
872*5796c8dcSSimon Schubert 			   &setlist, &showlist);
873*5796c8dcSSimon Schubert 
874*5796c8dcSSimon Schubert   add_target (&exec_ops);
875*5796c8dcSSimon Schubert }
876*5796c8dcSSimon Schubert 
877*5796c8dcSSimon Schubert static char *
878*5796c8dcSSimon Schubert exec_make_note_section (bfd *obfd, int *note_size)
879*5796c8dcSSimon Schubert {
880*5796c8dcSSimon Schubert   error (_("Can't create a corefile"));
881*5796c8dcSSimon Schubert }
882