1 /* Work with executable files, for GDB.
3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4 1998, 1999, 2000, 2001, 2002, 2003, 2007, 2008, 2009
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "completer.h"
34 #include "arch-utils.h"
35 #include "gdbthread.h"
36 #include "progspace.h"
39 #include "readline/readline.h"
40 #include "gdb_string.h"
47 #include "xcoffsolib.h"
49 struct vmap *map_vmap (bfd *, bfd *);
51 void (*deprecated_file_changed_hook) (char *);
53 /* Prototypes for local functions */
55 static void file_command (char *, int);
57 static void set_section_command (char *, int);
59 static void exec_files_info (struct target_ops *);
61 static void init_exec_ops (void);
63 void _initialize_exec (void);
65 /* The target vector for executable files. */
67 struct target_ops exec_ops;
69 /* True if the exec target is pushed on the stack. */
70 static int using_exec_ops;
72 /* Whether to open exec and core files read-only or read-write. */
76 show_write_files (struct ui_file *file, int from_tty,
77 struct cmd_list_element *c, const char *value)
79 fprintf_filtered (file, _("Writing into executable and core files is %s.\n"),
87 exec_open (char *args, int from_tty)
89 target_preopen (from_tty);
90 exec_file_attach (args, from_tty);
93 /* Close and clear exec_bfd. If we end up with no target sections to
94 read memory from, this unpushes the exec_ops target. */
101 bfd *abfd = exec_bfd;
102 char *name = bfd_get_filename (abfd);
104 if (!bfd_close (abfd))
105 warning (_("cannot close \"%s\": %s"),
106 name, bfd_errmsg (bfd_get_error ()));
109 /* Removing target sections may close the exec_ops target.
110 Clear exec_bfd before doing so to prevent recursion. */
114 remove_target_sections (abfd);
118 /* This is the target_close implementation. Clears all target
119 sections and closes all executable bfds from all program spaces. */
122 exec_close_1 (int quitting)
124 int need_symtab_cleanup = 0;
125 struct vmap *vp, *nxt;
129 for (nxt = vmap; nxt != NULL;)
134 /* if there is an objfile associated with this bfd,
135 free_objfile() will do proper cleanup of objfile *and* bfd. */
139 free_objfile (vp->objfile);
140 need_symtab_cleanup = 1;
142 else if (vp->bfd != exec_bfd)
143 /* FIXME-leak: We should be freeing vp->name too, I think. */
144 if (!bfd_close (vp->bfd))
145 warning (_("cannot close \"%s\": %s"),
146 vp->name, bfd_errmsg (bfd_get_error ()));
148 /* FIXME: This routine is #if 0'd in symfile.c. What should we
149 be doing here? Should we just free everything in
150 vp->objfile->symtabs? Should free_objfile do that?
151 FIXME-as-well: free_objfile already free'd vp->name, so it isn't
153 free_named_symtabs (vp->name);
160 struct program_space *ss;
161 struct cleanup *old_chain;
163 old_chain = save_current_program_space ();
166 set_current_program_space (ss);
168 /* Delete all target sections. */
170 (current_target_sections,
171 -resize_section_table (current_target_sections, 0));
176 do_cleanups (old_chain);
181 exec_file_clear (int from_tty)
183 /* Remove exec file. */
187 printf_unfiltered (_("No executable file now.\n"));
190 /* Set FILENAME as the new exec file.
192 This function is intended to be behave essentially the same
193 as exec_file_command, except that the latter will detect when
194 a target is being debugged, and will ask the user whether it
195 should be shut down first. (If the answer is "no", then the
196 new file is ignored.)
198 This file is used by exec_file_command, to do the work of opening
199 and processing the exec file after any prompting has happened.
201 And, it is used by child_attach, when the attach command was
202 given a pid but not a exec pathname, and the attach command could
203 figure out the pathname from the pid. (In this case, we shouldn't
204 ask the user whether the current target should be shut down --
205 we're supplying the exec pathname late for good reason.) */
208 exec_file_attach (char *filename, int from_tty)
210 /* Remove any previous exec file. */
213 /* Now open and digest the file the user requested, if any. */
218 printf_unfiltered (_("No executable file now.\n"));
220 set_gdbarch_from_file (NULL);
224 struct cleanup *cleanups;
225 char *scratch_pathname;
227 struct target_section *sections = NULL, *sections_end = NULL;
229 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename,
230 write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
232 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
233 if (scratch_chan < 0)
235 char *exename = alloca (strlen (filename) + 5);
236 strcat (strcpy (exename, filename), ".exe");
237 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename,
238 write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
242 if (scratch_chan < 0)
243 perror_with_name (filename);
244 exec_bfd = bfd_fopen (scratch_pathname, gnutarget,
245 write_files ? FOPEN_RUB : FOPEN_RB,
250 close (scratch_chan);
251 error (_("\"%s\": could not open as an executable file: %s"),
252 scratch_pathname, bfd_errmsg (bfd_get_error ()));
255 /* At this point, scratch_pathname and exec_bfd->name both point to the
256 same malloc'd string. However exec_close() will attempt to free it
257 via the exec_bfd->name pointer, so we need to make another copy and
258 leave exec_bfd as the new owner of the original copy. */
259 scratch_pathname = xstrdup (scratch_pathname);
260 cleanups = make_cleanup (xfree, scratch_pathname);
262 if (!bfd_check_format (exec_bfd, bfd_object))
264 /* Make sure to close exec_bfd, or else "run" might try to use
267 error (_("\"%s\": not in executable format: %s"),
268 scratch_pathname, bfd_errmsg (bfd_get_error ()));
271 /* FIXME - This should only be run for RS6000, but the ifdef is a poor
272 way to accomplish. */
273 #ifdef DEPRECATED_IBM6000_TARGET
274 /* Setup initial vmap. */
276 map_vmap (exec_bfd, 0);
279 /* Make sure to close exec_bfd, or else "run" might try to use
282 error (_("\"%s\": can't find the file sections: %s"),
283 scratch_pathname, bfd_errmsg (bfd_get_error ()));
285 #endif /* DEPRECATED_IBM6000_TARGET */
287 if (build_section_table (exec_bfd, §ions, §ions_end))
289 /* Make sure to close exec_bfd, or else "run" might try to use
292 error (_("\"%s\": can't find the file sections: %s"),
293 scratch_pathname, bfd_errmsg (bfd_get_error ()));
296 exec_bfd_mtime = bfd_get_mtime (exec_bfd);
300 set_gdbarch_from_file (exec_bfd);
302 /* Add the executable's sections to the current address spaces'
303 list of sections. This possibly pushes the exec_ops
305 add_target_sections (sections, sections_end);
308 /* Tell display code (if any) about the changed file name. */
309 if (deprecated_exec_file_display_hook)
310 (*deprecated_exec_file_display_hook) (filename);
312 do_cleanups (cleanups);
314 bfd_cache_close_all ();
315 observer_notify_executable_changed ();
318 /* Process the first arg in ARGS as the new exec file.
320 Note that we have to explicitly ignore additional args, since we can
321 be called from file_command(), which also calls symbol_file_command()
322 which can take multiple args.
324 If ARGS is NULL, we just want to close the exec file. */
327 exec_file_command (char *args, int from_tty)
332 if (from_tty && target_has_execution
333 && !query (_("A program is being debugged already.\n"
334 "Are you sure you want to change the file? ")))
335 error (_("File not changed."));
339 struct cleanup *cleanups;
341 /* Scan through the args and pick up the first non option arg
344 argv = gdb_buildargv (args);
345 cleanups = make_cleanup_freeargv (argv);
347 for (; (*argv != NULL) && (**argv == '-'); argv++)
351 error (_("No executable file name was specified"));
353 filename = tilde_expand (*argv);
354 make_cleanup (xfree, filename);
355 exec_file_attach (filename, from_tty);
357 do_cleanups (cleanups);
360 exec_file_attach (NULL, from_tty);
363 /* Set both the exec file and the symbol file, in one command.
364 What a novelty. Why did GDB go through four major releases before this
365 command was added? */
368 file_command (char *arg, int from_tty)
370 /* FIXME, if we lose on reading the symbol file, we should revert
371 the exec file, but that's rough. */
372 exec_file_command (arg, from_tty);
373 symbol_file_command (arg, from_tty);
374 if (deprecated_file_changed_hook)
375 deprecated_file_changed_hook (arg);
379 /* Locate all mappable sections of a BFD file.
380 table_pp_char is a char * to get it through bfd_map_over_sections;
381 we cast it back to its proper type. */
384 add_to_section_table (bfd *abfd, struct bfd_section *asect,
387 struct target_section **table_pp = (struct target_section **) table_pp_char;
390 /* Check the section flags, but do not discard zero-length sections, since
391 some symbols may still be attached to this section. For instance, we
392 encountered on sparc-solaris 2.10 a shared library with an empty .bss
393 section to which a symbol named "_end" was attached. The address
394 of this symbol still needs to be relocated. */
395 aflag = bfd_get_section_flags (abfd, asect);
396 if (!(aflag & SEC_ALLOC))
399 (*table_pp)->bfd = abfd;
400 (*table_pp)->the_bfd_section = asect;
401 (*table_pp)->addr = bfd_section_vma (abfd, asect);
402 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
407 resize_section_table (struct target_section_table *table, int num_added)
409 struct target_section *old_value;
413 old_value = table->sections;
414 old_count = table->sections_end - table->sections;
416 new_count = num_added + old_count;
420 table->sections = xrealloc (table->sections,
421 sizeof (struct target_section) * new_count);
422 table->sections_end = table->sections + new_count;
426 xfree (table->sections);
427 table->sections = table->sections_end = NULL;
433 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
434 Returns 0 if OK, 1 on error. */
437 build_section_table (struct bfd *some_bfd, struct target_section **start,
438 struct target_section **end)
442 count = bfd_count_sections (some_bfd);
445 *start = (struct target_section *) xmalloc (count * sizeof (**start));
447 bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
448 if (*end > *start + count)
449 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
450 /* We could realloc the table, but it probably loses for most files. */
454 /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
455 current set of target sections. */
458 add_target_sections (struct target_section *sections,
459 struct target_section *sections_end)
462 struct target_section_table *table = current_target_sections;
464 count = sections_end - sections;
468 int space = resize_section_table (table, count);
469 memcpy (table->sections + space,
470 sections, count * sizeof (sections[0]));
472 /* If these are the first file sections we can provide memory
473 from, push the file_stratum target. */
477 push_target (&exec_ops);
482 /* Remove all target sections taken from ABFD. */
485 remove_target_sections (bfd *abfd)
487 struct target_section *src, *dest;
489 struct target_section_table *table = current_target_sections;
491 dest = table->sections;
492 for (src = table->sections; src < table->sections_end; src++)
493 if (src->bfd != abfd)
495 /* Keep this section. */
501 /* If we've dropped any sections, resize the section table. */
506 old_count = resize_section_table (table, dest - src);
508 /* If we don't have any more sections to read memory from,
509 remove the file_stratum target from the stack. */
510 if (old_count + (dest - src) == 0)
512 struct program_space *pspace;
515 if (pspace->target_sections.sections
516 != pspace->target_sections.sections_end)
519 unpush_target (&exec_ops);
526 bfdsec_to_vmap (struct bfd *abfd, struct bfd_section *sect, void *arg3)
528 struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *) arg3;
531 vp = vmap_bfd->pvmap;
533 if ((bfd_get_section_flags (abfd, sect) & SEC_LOAD) == 0)
536 if (strcmp (bfd_section_name (abfd, sect), ".text") == 0)
538 vp->tstart = bfd_section_vma (abfd, sect);
539 vp->tend = vp->tstart + bfd_section_size (abfd, sect);
540 vp->tvma = bfd_section_vma (abfd, sect);
541 vp->toffs = sect->filepos;
543 else if (strcmp (bfd_section_name (abfd, sect), ".data") == 0)
545 vp->dstart = bfd_section_vma (abfd, sect);
546 vp->dend = vp->dstart + bfd_section_size (abfd, sect);
547 vp->dvma = bfd_section_vma (abfd, sect);
549 /* Silently ignore other types of sections. (FIXME?) */
552 /* Make a vmap for ABFD which might be a member of the archive ARCH.
553 Return the new vmap. */
556 map_vmap (bfd *abfd, bfd *arch)
558 struct vmap_and_bfd vmap_bfd;
559 struct vmap *vp, **vpp;
561 vp = (struct vmap *) xmalloc (sizeof (*vp));
562 memset ((char *) vp, '\0', sizeof (*vp));
565 vp->name = bfd_get_filename (arch ? arch : abfd);
566 vp->member = arch ? bfd_get_filename (abfd) : "";
568 vmap_bfd.pbfd = arch;
570 bfd_map_over_sections (abfd, bfdsec_to_vmap, &vmap_bfd);
572 /* Find the end of the list and append. */
573 for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt)
582 section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
583 ULONGEST offset, LONGEST len,
584 struct target_section *sections,
585 struct target_section *sections_end,
586 const char *section_name)
589 struct target_section *p;
590 ULONGEST memaddr = offset;
591 ULONGEST memend = memaddr + len;
594 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
596 for (p = sections; p < sections_end; p++)
598 if (section_name && strcmp (section_name, p->the_bfd_section->name) != 0)
599 continue; /* not the section we need */
600 if (memaddr >= p->addr)
602 if (memend <= p->endaddr)
604 /* Entire transfer is within this section. */
606 res = bfd_set_section_contents (p->bfd, p->the_bfd_section,
607 writebuf, memaddr - p->addr,
610 res = bfd_get_section_contents (p->bfd, p->the_bfd_section,
611 readbuf, memaddr - p->addr,
613 return (res != 0) ? len : 0;
615 else if (memaddr >= p->endaddr)
617 /* This section ends before the transfer starts. */
622 /* This section overlaps the transfer. Just do half. */
623 len = p->endaddr - memaddr;
625 res = bfd_set_section_contents (p->bfd, p->the_bfd_section,
626 writebuf, memaddr - p->addr,
629 res = bfd_get_section_contents (p->bfd, p->the_bfd_section,
630 readbuf, memaddr - p->addr,
632 return (res != 0) ? len : 0;
637 return 0; /* We can't help */
640 struct target_section_table *
641 exec_get_section_table (struct target_ops *ops)
643 return current_target_sections;
647 exec_xfer_partial (struct target_ops *ops, enum target_object object,
648 const char *annex, gdb_byte *readbuf,
649 const gdb_byte *writebuf,
650 ULONGEST offset, LONGEST len)
652 struct target_section_table *table = target_get_section_table (ops);
654 if (object == TARGET_OBJECT_MEMORY)
655 return section_table_xfer_memory_partial (readbuf, writebuf,
666 print_section_info (struct target_section_table *t, bfd *abfd)
668 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
669 struct target_section *p;
670 /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */
671 int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
673 printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
675 printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
676 if (abfd == exec_bfd)
677 printf_filtered (_("\tEntry point: %s\n"),
678 paddress (gdbarch, bfd_get_start_address (abfd)));
679 for (p = t->sections; p < t->sections_end; p++)
681 printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
682 printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
684 /* FIXME: A format of "08l" is not wide enough for file offsets
685 larger than 4GB. OTOH, making it "016l" isn't desirable either
686 since most output will then be much wider than necessary. It
687 may make sense to test the size of the file and choose the
688 format string accordingly. */
689 /* FIXME: i18n: Need to rewrite this sentence. */
691 printf_filtered (" @ %s",
692 hex_string_custom (p->the_bfd_section->filepos, 8));
693 printf_filtered (" is %s", bfd_section_name (p->bfd, p->the_bfd_section));
695 printf_filtered (" in %s", bfd_get_filename (p->bfd));
696 printf_filtered ("\n");
701 exec_files_info (struct target_ops *t)
703 print_section_info (current_target_sections, exec_bfd);
707 int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
710 printf_unfiltered (_("\tMapping info for file `%s'.\n"), vmap->name);
711 printf_unfiltered ("\t %*s %*s %*s %*s %8.8s %s\n",
712 addr_size * 2, "tstart",
713 addr_size * 2, "tend",
714 addr_size * 2, "dstart",
715 addr_size * 2, "dend",
719 for (vp = vmap; vp; vp = vp->nxt)
720 printf_unfiltered ("\t0x%s 0x%s 0x%s 0x%s %s%s%s%s\n",
721 phex (vp->tstart, addr_size),
722 phex (vp->tend, addr_size),
723 phex (vp->dstart, addr_size),
724 phex (vp->dend, addr_size),
726 *vp->member ? "(" : "", vp->member,
727 *vp->member ? ")" : "");
732 set_section_command (char *args, int from_tty)
734 struct target_section *p;
737 unsigned long secaddr;
740 struct target_section_table *table;
743 error (_("Must specify section name and its virtual address"));
745 /* Parse out section name */
746 for (secname = args; !isspace (*args); args++);
747 seclen = args - secname;
749 /* Parse out new virtual address */
750 secaddr = parse_and_eval_address (args);
752 table = current_target_sections;
753 for (p = table->sections; p < table->sections_end; p++)
755 if (!strncmp (secname, bfd_section_name (exec_bfd, p->the_bfd_section), seclen)
756 && bfd_section_name (exec_bfd, p->the_bfd_section)[seclen] == '\0')
758 offset = secaddr - p->addr;
760 p->endaddr += offset;
762 exec_files_info (&exec_ops);
766 if (seclen >= sizeof (secprint))
767 seclen = sizeof (secprint) - 1;
768 strncpy (secprint, secname, seclen);
769 secprint[seclen] = '\0';
770 error (_("Section %s not found"), secprint);
773 /* If we can find a section in FILENAME with BFD index INDEX, adjust
777 exec_set_section_address (const char *filename, int index, CORE_ADDR address)
779 struct target_section *p;
780 struct target_section_table *table;
782 table = current_target_sections;
783 for (p = table->sections; p < table->sections_end; p++)
785 if (strcmp (filename, p->bfd->filename) == 0
786 && index == p->the_bfd_section->index)
788 p->endaddr += address - p->addr;
794 /* If mourn is being called in all the right places, this could be say
795 `gdb internal error' (since generic_mourn calls
796 breakpoint_init_inferior). */
799 ignore (struct gdbarch *gdbarch, struct bp_target_info *bp_tgt)
805 exec_has_memory (struct target_ops *ops)
807 /* We can provide memory if we have any file/target sections to read
809 return (current_target_sections->sections
810 != current_target_sections->sections_end);
813 /* Find mapped memory. */
816 exec_set_find_memory_regions (int (*func) (int (*) (CORE_ADDR,
822 exec_ops.to_find_memory_regions = func;
825 static char *exec_make_note_section (bfd *, int *);
827 /* Fill in the exec file target vector. Very few entries need to be
833 exec_ops.to_shortname = "exec";
834 exec_ops.to_longname = "Local exec file";
835 exec_ops.to_doc = "Use an executable file as a target.\n\
836 Specify the filename of the executable file.";
837 exec_ops.to_open = exec_open;
838 exec_ops.to_close = exec_close_1;
839 exec_ops.to_attach = find_default_attach;
840 exec_ops.to_xfer_partial = exec_xfer_partial;
841 exec_ops.to_get_section_table = exec_get_section_table;
842 exec_ops.to_files_info = exec_files_info;
843 exec_ops.to_insert_breakpoint = ignore;
844 exec_ops.to_remove_breakpoint = ignore;
845 exec_ops.to_create_inferior = find_default_create_inferior;
846 exec_ops.to_stratum = file_stratum;
847 exec_ops.to_has_memory = exec_has_memory;
848 exec_ops.to_make_corefile_notes = exec_make_note_section;
849 exec_ops.to_magic = OPS_MAGIC;
853 _initialize_exec (void)
855 struct cmd_list_element *c;
861 c = add_cmd ("file", class_files, file_command, _("\
862 Use FILE as program to be debugged.\n\
863 It is read for its symbols, for getting the contents of pure memory,\n\
864 and it is the program executed when you use the `run' command.\n\
865 If FILE cannot be found as specified, your execution directory path\n\
866 ($PATH) is searched for a command of that name.\n\
867 No arg means to have no executable file and no symbols."), &cmdlist);
868 set_cmd_completer (c, filename_completer);
871 c = add_cmd ("exec-file", class_files, exec_file_command, _("\
872 Use FILE as program for getting contents of pure memory.\n\
873 If FILE cannot be found as specified, your execution directory path\n\
874 is searched for a command of that name.\n\
875 No arg means have no executable file."), &cmdlist);
876 set_cmd_completer (c, filename_completer);
878 add_com ("section", class_files, set_section_command, _("\
879 Change the base address of section SECTION of the exec file to ADDR.\n\
880 This can be used if the exec file does not contain section addresses,\n\
881 (such as in the a.out format), or when the addresses specified in the\n\
882 file itself are wrong. Each section must be changed separately. The\n\
883 ``info files'' command lists all the sections and their addresses."));
885 add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
886 Set writing into executable and core files."), _("\
887 Show writing into executable and core files."), NULL,
890 &setlist, &showlist);
892 add_target (&exec_ops);
896 exec_make_note_section (bfd *obfd, int *note_size)
898 error (_("Can't create a corefile"));