1 /* Work with executable files, for GDB.
3 Copyright (C) 1988-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "filenames.h"
29 #include "completer.h"
33 #include "arch-utils.h"
34 #include "gdbthread.h"
35 #include "progspace.h"
39 #include "readline/readline.h"
47 void (*deprecated_file_changed_hook) (char *);
49 /* Prototypes for local functions */
51 static void file_command (char *, int);
53 static void set_section_command (char *, int);
55 static void exec_files_info (struct target_ops *);
57 static void init_exec_ops (void);
59 void _initialize_exec (void);
61 /* The target vector for executable files. */
63 struct target_ops exec_ops;
65 /* Function used to implement to_find_memory_regions. */
67 static int (*exec_do_find_memory_regions) (find_memory_region_ftype, void *);
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"),
85 exec_open (char *args, int from_tty)
87 target_preopen (from_tty);
88 exec_file_attach (args, from_tty);
91 /* Close and clear exec_bfd. If we end up with no target sections to
92 read memory from, this unpushes the exec_ops target. */
101 gdb_bfd_unref (abfd);
103 /* Removing target sections may close the exec_ops target.
104 Clear exec_bfd before doing so to prevent recursion. */
108 remove_target_sections (&exec_bfd);
110 xfree (exec_filename);
111 exec_filename = NULL;
115 /* This is the target_close implementation. Clears all target
116 sections and closes all executable bfds from all program spaces. */
119 exec_close_1 (struct target_ops *self)
124 struct program_space *ss;
125 struct cleanup *old_chain;
127 old_chain = save_current_program_space ();
130 set_current_program_space (ss);
132 /* Delete all target sections. */
134 (current_target_sections,
135 -resize_section_table (current_target_sections, 0));
140 do_cleanups (old_chain);
145 exec_file_clear (int from_tty)
147 /* Remove exec file. */
151 printf_unfiltered (_("No executable file now.\n"));
154 /* Set FILENAME as the new exec file.
156 This function is intended to be behave essentially the same
157 as exec_file_command, except that the latter will detect when
158 a target is being debugged, and will ask the user whether it
159 should be shut down first. (If the answer is "no", then the
160 new file is ignored.)
162 This file is used by exec_file_command, to do the work of opening
163 and processing the exec file after any prompting has happened.
165 And, it is used by child_attach, when the attach command was
166 given a pid but not a exec pathname, and the attach command could
167 figure out the pathname from the pid. (In this case, we shouldn't
168 ask the user whether the current target should be shut down --
169 we're supplying the exec pathname late for good reason.) */
172 exec_file_attach (char *filename, int from_tty)
174 /* Remove any previous exec file. */
177 /* Now open and digest the file the user requested, if any. */
182 printf_unfiltered (_("No executable file now.\n"));
184 set_gdbarch_from_file (NULL);
188 struct cleanup *cleanups;
189 char *scratch_pathname, *canonical_pathname;
191 struct target_section *sections = NULL, *sections_end = NULL;
194 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, filename,
195 write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
197 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
198 if (scratch_chan < 0)
200 char *exename = alloca (strlen (filename) + 5);
202 strcat (strcpy (exename, filename), ".exe");
203 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename,
204 write_files ? O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
208 if (scratch_chan < 0)
209 perror_with_name (filename);
211 cleanups = make_cleanup (xfree, scratch_pathname);
213 /* gdb_bfd_open (and its variants) prefers canonicalized pathname for
214 better BFD caching. */
215 canonical_pathname = gdb_realpath (scratch_pathname);
216 make_cleanup (xfree, canonical_pathname);
219 exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget,
220 FOPEN_RUB, scratch_chan);
222 exec_bfd = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
226 error (_("\"%s\": could not open as an executable file: %s"),
227 scratch_pathname, bfd_errmsg (bfd_get_error ()));
230 gdb_assert (exec_filename == NULL);
231 exec_filename = gdb_realpath_keepfile (scratch_pathname);
233 if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
235 /* Make sure to close exec_bfd, or else "run" might try to use
238 error (_("\"%s\": not in executable format: %s"),
240 gdb_bfd_errmsg (bfd_get_error (), matching));
243 if (build_section_table (exec_bfd, §ions, §ions_end))
245 /* Make sure to close exec_bfd, or else "run" might try to use
248 error (_("\"%s\": can't find the file sections: %s"),
249 scratch_pathname, bfd_errmsg (bfd_get_error ()));
252 exec_bfd_mtime = bfd_get_mtime (exec_bfd);
256 set_gdbarch_from_file (exec_bfd);
258 /* Add the executable's sections to the current address spaces'
259 list of sections. This possibly pushes the exec_ops
261 add_target_sections (&exec_bfd, sections, sections_end);
264 /* Tell display code (if any) about the changed file name. */
265 if (deprecated_exec_file_display_hook)
266 (*deprecated_exec_file_display_hook) (filename);
268 do_cleanups (cleanups);
270 bfd_cache_close_all ();
271 observer_notify_executable_changed ();
274 /* Process the first arg in ARGS as the new exec file.
276 Note that we have to explicitly ignore additional args, since we can
277 be called from file_command(), which also calls symbol_file_command()
278 which can take multiple args.
280 If ARGS is NULL, we just want to close the exec file. */
283 exec_file_command (char *args, int from_tty)
288 if (from_tty && target_has_execution
289 && !query (_("A program is being debugged already.\n"
290 "Are you sure you want to change the file? ")))
291 error (_("File not changed."));
295 struct cleanup *cleanups;
297 /* Scan through the args and pick up the first non option arg
300 argv = gdb_buildargv (args);
301 cleanups = make_cleanup_freeargv (argv);
303 for (; (*argv != NULL) && (**argv == '-'); argv++)
307 error (_("No executable file name was specified"));
309 filename = tilde_expand (*argv);
310 make_cleanup (xfree, filename);
311 exec_file_attach (filename, from_tty);
313 do_cleanups (cleanups);
316 exec_file_attach (NULL, from_tty);
319 /* Set both the exec file and the symbol file, in one command.
320 What a novelty. Why did GDB go through four major releases before this
321 command was added? */
324 file_command (char *arg, int from_tty)
326 /* FIXME, if we lose on reading the symbol file, we should revert
327 the exec file, but that's rough. */
328 exec_file_command (arg, from_tty);
329 symbol_file_command (arg, from_tty);
330 if (deprecated_file_changed_hook)
331 deprecated_file_changed_hook (arg);
335 /* Locate all mappable sections of a BFD file.
336 table_pp_char is a char * to get it through bfd_map_over_sections;
337 we cast it back to its proper type. */
340 add_to_section_table (bfd *abfd, struct bfd_section *asect,
343 struct target_section **table_pp = (struct target_section **) table_pp_char;
346 gdb_assert (abfd == asect->owner);
348 /* Check the section flags, but do not discard zero-length sections, since
349 some symbols may still be attached to this section. For instance, we
350 encountered on sparc-solaris 2.10 a shared library with an empty .bss
351 section to which a symbol named "_end" was attached. The address
352 of this symbol still needs to be relocated. */
353 aflag = bfd_get_section_flags (abfd, asect);
354 if (!(aflag & SEC_ALLOC))
357 (*table_pp)->owner = NULL;
358 (*table_pp)->the_bfd_section = asect;
359 (*table_pp)->addr = bfd_section_vma (abfd, asect);
360 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
365 resize_section_table (struct target_section_table *table, int num_added)
370 old_count = table->sections_end - table->sections;
372 new_count = num_added + old_count;
376 table->sections = xrealloc (table->sections,
377 sizeof (struct target_section) * new_count);
378 table->sections_end = table->sections + new_count;
382 xfree (table->sections);
383 table->sections = table->sections_end = NULL;
389 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
390 Returns 0 if OK, 1 on error. */
393 build_section_table (struct bfd *some_bfd, struct target_section **start,
394 struct target_section **end)
398 count = bfd_count_sections (some_bfd);
401 *start = (struct target_section *) xmalloc (count * sizeof (**start));
403 bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
404 if (*end > *start + count)
405 internal_error (__FILE__, __LINE__,
406 _("failed internal consistency check"));
407 /* We could realloc the table, but it probably loses for most files. */
411 /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
412 current set of target sections. */
415 add_target_sections (void *owner,
416 struct target_section *sections,
417 struct target_section *sections_end)
420 struct target_section_table *table = current_target_sections;
422 count = sections_end - sections;
426 int space = resize_section_table (table, count);
429 for (i = 0; i < count; ++i)
431 table->sections[space + i] = sections[i];
432 table->sections[space + i].owner = owner;
435 /* If these are the first file sections we can provide memory
436 from, push the file_stratum target. */
440 push_target (&exec_ops);
445 /* Add the sections of OBJFILE to the current set of target sections. */
448 add_target_sections_of_objfile (struct objfile *objfile)
450 struct target_section_table *table = current_target_sections;
451 struct obj_section *osect;
454 struct target_section *ts;
459 /* Compute the number of sections to add. */
460 ALL_OBJFILE_OSECTIONS (objfile, osect)
462 if (bfd_get_section_size (osect->the_bfd_section) == 0)
470 space = resize_section_table (table, count);
472 ts = table->sections + space;
474 ALL_OBJFILE_OSECTIONS (objfile, osect)
476 if (bfd_get_section_size (osect->the_bfd_section) == 0)
479 gdb_assert (ts < table->sections + space + count);
481 ts->addr = obj_section_addr (osect);
482 ts->endaddr = obj_section_endaddr (osect);
483 ts->the_bfd_section = osect->the_bfd_section;
484 ts->owner = (void *) objfile;
490 /* Remove all target sections owned by OWNER.
491 OWNER must be the same value passed to add_target_sections. */
494 remove_target_sections (void *owner)
496 struct target_section *src, *dest;
497 struct target_section_table *table = current_target_sections;
499 gdb_assert (owner != NULL);
501 dest = table->sections;
502 for (src = table->sections; src < table->sections_end; src++)
503 if (src->owner != owner)
505 /* Keep this section. */
511 /* If we've dropped any sections, resize the section table. */
516 old_count = resize_section_table (table, dest - src);
518 /* If we don't have any more sections to read memory from,
519 remove the file_stratum target from the stack. */
520 if (old_count + (dest - src) == 0)
522 struct program_space *pspace;
525 if (pspace->target_sections.sections
526 != pspace->target_sections.sections_end)
529 unpush_target (&exec_ops);
537 section_table_available_memory (VEC(mem_range_s) *memory,
538 CORE_ADDR memaddr, ULONGEST len,
539 struct target_section *sections,
540 struct target_section *sections_end)
542 struct target_section *p;
544 for (p = sections; p < sections_end; p++)
546 if ((bfd_get_section_flags (p->the_bfd_section->owner,
548 & SEC_READONLY) == 0)
551 /* Copy the meta-data, adjusted. */
552 if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len))
554 ULONGEST lo1, hi1, lo2, hi2;
563 r = VEC_safe_push (mem_range_s, memory, NULL);
565 r->start = max (lo1, lo2);
566 r->length = min (hi1, hi2) - r->start;
573 enum target_xfer_status
574 section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
575 ULONGEST offset, ULONGEST len,
576 ULONGEST *xfered_len,
577 struct target_section *sections,
578 struct target_section *sections_end,
579 const char *section_name)
582 struct target_section *p;
583 ULONGEST memaddr = offset;
584 ULONGEST memend = memaddr + len;
587 internal_error (__FILE__, __LINE__,
588 _("failed internal consistency check"));
590 for (p = sections; p < sections_end; p++)
592 struct bfd_section *asect = p->the_bfd_section;
593 bfd *abfd = asect->owner;
595 if (section_name && strcmp (section_name, asect->name) != 0)
596 continue; /* not the section we need. */
597 if (memaddr >= p->addr)
599 if (memend <= p->endaddr)
601 /* Entire transfer is within this section. */
603 res = bfd_set_section_contents (abfd, asect,
604 writebuf, memaddr - p->addr,
607 res = bfd_get_section_contents (abfd, asect,
608 readbuf, memaddr - p->addr,
614 return TARGET_XFER_OK;
617 return TARGET_XFER_EOF;
619 else if (memaddr >= p->endaddr)
621 /* This section ends before the transfer starts. */
626 /* This section overlaps the transfer. Just do half. */
627 len = p->endaddr - memaddr;
629 res = bfd_set_section_contents (abfd, asect,
630 writebuf, memaddr - p->addr,
633 res = bfd_get_section_contents (abfd, asect,
634 readbuf, memaddr - p->addr,
639 return TARGET_XFER_OK;
642 return TARGET_XFER_EOF;
647 return TARGET_XFER_EOF; /* We can't help. */
650 static struct target_section_table *
651 exec_get_section_table (struct target_ops *ops)
653 return current_target_sections;
656 static enum target_xfer_status
657 exec_xfer_partial (struct target_ops *ops, enum target_object object,
658 const char *annex, gdb_byte *readbuf,
659 const gdb_byte *writebuf,
660 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
662 struct target_section_table *table = target_get_section_table (ops);
664 if (object == TARGET_OBJECT_MEMORY)
665 return section_table_xfer_memory_partial (readbuf, writebuf,
666 offset, len, xfered_len,
671 return TARGET_XFER_E_IO;
676 print_section_info (struct target_section_table *t, bfd *abfd)
678 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
679 struct target_section *p;
680 /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */
681 int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
683 printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
685 printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
686 if (abfd == exec_bfd)
688 /* gcc-3.4 does not like the initialization in
689 <p == t->sections_end>. */
690 bfd_vma displacement = 0;
693 for (p = t->sections; p < t->sections_end; p++)
695 struct bfd_section *psect = p->the_bfd_section;
696 bfd *pbfd = psect->owner;
698 if ((bfd_get_section_flags (pbfd, psect) & (SEC_ALLOC | SEC_LOAD))
699 != (SEC_ALLOC | SEC_LOAD))
702 if (bfd_get_section_vma (pbfd, psect) <= abfd->start_address
703 && abfd->start_address < (bfd_get_section_vma (pbfd, psect)
704 + bfd_get_section_size (psect)))
706 displacement = p->addr - bfd_get_section_vma (pbfd, psect);
710 if (p == t->sections_end)
711 warning (_("Cannot find section for the entry point of %s."),
712 bfd_get_filename (abfd));
714 entry_point = gdbarch_addr_bits_remove (gdbarch,
715 bfd_get_start_address (abfd)
717 printf_filtered (_("\tEntry point: %s\n"),
718 paddress (gdbarch, entry_point));
720 for (p = t->sections; p < t->sections_end; p++)
722 struct bfd_section *psect = p->the_bfd_section;
723 bfd *pbfd = psect->owner;
725 printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
726 printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
728 /* FIXME: A format of "08l" is not wide enough for file offsets
729 larger than 4GB. OTOH, making it "016l" isn't desirable either
730 since most output will then be much wider than necessary. It
731 may make sense to test the size of the file and choose the
732 format string accordingly. */
733 /* FIXME: i18n: Need to rewrite this sentence. */
735 printf_filtered (" @ %s",
736 hex_string_custom (psect->filepos, 8));
737 printf_filtered (" is %s", bfd_section_name (pbfd, psect));
739 printf_filtered (" in %s", bfd_get_filename (pbfd));
740 printf_filtered ("\n");
745 exec_files_info (struct target_ops *t)
748 print_section_info (current_target_sections, exec_bfd);
750 puts_filtered (_("\t<no file loaded>\n"));
754 set_section_command (char *args, int from_tty)
756 struct target_section *p;
759 unsigned long secaddr;
762 struct target_section_table *table;
765 error (_("Must specify section name and its virtual address"));
767 /* Parse out section name. */
768 for (secname = args; !isspace (*args); args++);
769 seclen = args - secname;
771 /* Parse out new virtual address. */
772 secaddr = parse_and_eval_address (args);
774 table = current_target_sections;
775 for (p = table->sections; p < table->sections_end; p++)
777 if (!strncmp (secname, bfd_section_name (p->bfd,
778 p->the_bfd_section), seclen)
779 && bfd_section_name (p->bfd, p->the_bfd_section)[seclen] == '\0')
781 offset = secaddr - p->addr;
783 p->endaddr += offset;
785 exec_files_info (&exec_ops);
789 if (seclen >= sizeof (secprint))
790 seclen = sizeof (secprint) - 1;
791 strncpy (secprint, secname, seclen);
792 secprint[seclen] = '\0';
793 error (_("Section %s not found"), secprint);
796 /* If we can find a section in FILENAME with BFD index INDEX, adjust
800 exec_set_section_address (const char *filename, int index, CORE_ADDR address)
802 struct target_section *p;
803 struct target_section_table *table;
805 table = current_target_sections;
806 for (p = table->sections; p < table->sections_end; p++)
808 if (filename_cmp (filename, p->the_bfd_section->owner->filename) == 0
809 && index == p->the_bfd_section->index)
811 p->endaddr += address - p->addr;
817 /* If mourn is being called in all the right places, this could be say
818 `gdb internal error' (since generic_mourn calls
819 breakpoint_init_inferior). */
822 ignore (struct target_ops *ops, struct gdbarch *gdbarch,
823 struct bp_target_info *bp_tgt)
829 exec_has_memory (struct target_ops *ops)
831 /* We can provide memory if we have any file/target sections to read
833 return (current_target_sections->sections
834 != current_target_sections->sections_end);
837 /* Find mapped memory. */
840 exec_set_find_memory_regions (int (*func) (find_memory_region_ftype, void *))
842 exec_do_find_memory_regions = func;
846 exec_find_memory_regions (struct target_ops *self,
847 find_memory_region_ftype func, void *data)
849 return exec_do_find_memory_regions (func, data);
852 static char *exec_make_note_section (struct target_ops *self, bfd *, int *);
854 /* Fill in the exec file target vector. Very few entries need to be
860 exec_ops.to_shortname = "exec";
861 exec_ops.to_longname = "Local exec file";
862 exec_ops.to_doc = "Use an executable file as a target.\n\
863 Specify the filename of the executable file.";
864 exec_ops.to_open = exec_open;
865 exec_ops.to_close = exec_close_1;
866 exec_ops.to_attach = find_default_attach;
867 exec_ops.to_xfer_partial = exec_xfer_partial;
868 exec_ops.to_get_section_table = exec_get_section_table;
869 exec_ops.to_files_info = exec_files_info;
870 exec_ops.to_insert_breakpoint = ignore;
871 exec_ops.to_remove_breakpoint = ignore;
872 exec_ops.to_create_inferior = find_default_create_inferior;
873 exec_ops.to_stratum = file_stratum;
874 exec_ops.to_has_memory = exec_has_memory;
875 exec_ops.to_make_corefile_notes = exec_make_note_section;
876 exec_ops.to_find_memory_regions = exec_find_memory_regions;
877 exec_ops.to_magic = OPS_MAGIC;
881 _initialize_exec (void)
883 struct cmd_list_element *c;
889 c = add_cmd ("file", class_files, file_command, _("\
890 Use FILE as program to be debugged.\n\
891 It is read for its symbols, for getting the contents of pure memory,\n\
892 and it is the program executed when you use the `run' command.\n\
893 If FILE cannot be found as specified, your execution directory path\n\
894 ($PATH) is searched for a command of that name.\n\
895 No arg means to have no executable file and no symbols."), &cmdlist);
896 set_cmd_completer (c, filename_completer);
899 c = add_cmd ("exec-file", class_files, exec_file_command, _("\
900 Use FILE as program for getting contents of pure memory.\n\
901 If FILE cannot be found as specified, your execution directory path\n\
902 is searched for a command of that name.\n\
903 No arg means have no executable file."), &cmdlist);
904 set_cmd_completer (c, filename_completer);
906 add_com ("section", class_files, set_section_command, _("\
907 Change the base address of section SECTION of the exec file to ADDR.\n\
908 This can be used if the exec file does not contain section addresses,\n\
909 (such as in the a.out format), or when the addresses specified in the\n\
910 file itself are wrong. Each section must be changed separately. The\n\
911 ``info files'' command lists all the sections and their addresses."));
913 add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
914 Set writing into executable and core files."), _("\
915 Show writing into executable and core files."), NULL,
918 &setlist, &showlist);
920 add_target_with_completer (&exec_ops, filename_completer);
924 exec_make_note_section (struct target_ops *self, bfd *obfd, int *note_size)
926 error (_("Can't create a corefile"));