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