Remove fake operand handling for extended mnemonics.
[binutils-gdb.git] / gdb / exec.c
blob3023ff7e5aa9570a4f8200137c70c4dd263ef4ed
1 /* Work with executable files, for GDB.
3 Copyright (C) 1988-2018 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/>. */
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "target.h"
24 #include "gdbcmd.h"
25 #include "language.h"
26 #include "filenames.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "completer.h"
30 #include "value.h"
31 #include "exec.h"
32 #include "observable.h"
33 #include "arch-utils.h"
34 #include "gdbthread.h"
35 #include "progspace.h"
36 #include "gdb_bfd.h"
37 #include "gcore.h"
38 #include "source.h"
40 #include <fcntl.h>
41 #include "readline/readline.h"
42 #include "gdbcore.h"
44 #include <ctype.h>
45 #include <sys/stat.h>
46 #include "solist.h"
47 #include <algorithm>
48 #include "common/pathstuff.h"
50 void (*deprecated_file_changed_hook) (const char *);
52 static const target_info exec_target_info = {
53 "exec",
54 N_("Local exec file"),
55 N_("Use an executable file as a target.\n\
56 Specify the filename of the executable file.")
59 /* The target vector for executable files. */
61 struct exec_target final : public target_ops
63 exec_target ()
64 { to_stratum = file_stratum; }
66 const target_info &info () const override
67 { return exec_target_info; }
69 void close () override;
70 enum target_xfer_status xfer_partial (enum target_object object,
71 const char *annex,
72 gdb_byte *readbuf,
73 const gdb_byte *writebuf,
74 ULONGEST offset, ULONGEST len,
75 ULONGEST *xfered_len) override;
76 struct target_section_table *get_section_table () override;
77 void files_info () override;
79 bool has_memory () override;
80 char *make_corefile_notes (bfd *, int *) override;
81 int find_memory_regions (find_memory_region_ftype func, void *data) override;
84 static exec_target exec_ops;
86 /* Whether to open exec and core files read-only or read-write. */
88 int write_files = 0;
89 static void
90 show_write_files (struct ui_file *file, int from_tty,
91 struct cmd_list_element *c, const char *value)
93 fprintf_filtered (file, _("Writing into executable and core files is %s.\n"),
94 value);
98 static void
99 exec_target_open (const char *args, int from_tty)
101 target_preopen (from_tty);
102 exec_file_attach (args, from_tty);
105 /* Close and clear exec_bfd. If we end up with no target sections to
106 read memory from, this unpushes the exec_ops target. */
108 void
109 exec_close (void)
111 if (exec_bfd)
113 bfd *abfd = exec_bfd;
115 gdb_bfd_unref (abfd);
117 /* Removing target sections may close the exec_ops target.
118 Clear exec_bfd before doing so to prevent recursion. */
119 exec_bfd = NULL;
120 exec_bfd_mtime = 0;
122 remove_target_sections (&exec_bfd);
124 xfree (exec_filename);
125 exec_filename = NULL;
129 /* This is the target_close implementation. Clears all target
130 sections and closes all executable bfds from all program spaces. */
132 void
133 exec_target::close ()
135 struct program_space *ss;
136 scoped_restore_current_program_space restore_pspace;
138 ALL_PSPACES (ss)
140 set_current_program_space (ss);
141 clear_section_table (current_target_sections);
142 exec_close ();
146 /* See gdbcore.h. */
148 void
149 try_open_exec_file (const char *exec_file_host, struct inferior *inf,
150 symfile_add_flags add_flags)
152 struct cleanup *old_chain;
153 struct gdb_exception prev_err = exception_none;
155 old_chain = make_cleanup (free_current_contents, &prev_err.message);
157 /* exec_file_attach and symbol_file_add_main may throw an error if the file
158 cannot be opened either locally or remotely.
160 This happens for example, when the file is first found in the local
161 sysroot (above), and then disappears (a TOCTOU race), or when it doesn't
162 exist in the target filesystem, or when the file does exist, but
163 is not readable.
165 Even without a symbol file, the remote-based debugging session should
166 continue normally instead of ending abruptly. Hence we catch thrown
167 errors/exceptions in the following code. */
170 /* We must do this step even if exec_file_host is NULL, so that
171 exec_file_attach will clear state. */
172 exec_file_attach (exec_file_host, add_flags & SYMFILE_VERBOSE);
174 CATCH (err, RETURN_MASK_ERROR)
176 if (err.message != NULL)
177 warning ("%s", err.message);
179 prev_err = err;
181 /* Save message so it doesn't get trashed by the catch below. */
182 if (err.message != NULL)
183 prev_err.message = xstrdup (err.message);
185 END_CATCH
187 if (exec_file_host != NULL)
191 symbol_file_add_main (exec_file_host, add_flags);
193 CATCH (err, RETURN_MASK_ERROR)
195 if (!exception_print_same (prev_err, err))
196 warning ("%s", err.message);
198 END_CATCH
201 do_cleanups (old_chain);
204 /* See gdbcore.h. */
206 void
207 exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty)
209 char *exec_file_target;
210 symfile_add_flags add_flags = 0;
212 /* Do nothing if we already have an executable filename. */
213 if (get_exec_file (0) != NULL)
214 return;
216 /* Try to determine a filename from the process itself. */
217 exec_file_target = target_pid_to_exec_file (pid);
218 if (exec_file_target == NULL)
220 warning (_("No executable has been specified and target does not "
221 "support\n"
222 "determining executable automatically. "
223 "Try using the \"file\" command."));
224 return;
227 gdb::unique_xmalloc_ptr<char> exec_file_host
228 = exec_file_find (exec_file_target, NULL);
230 if (defer_bp_reset)
231 add_flags |= SYMFILE_DEFER_BP_RESET;
233 if (from_tty)
234 add_flags |= SYMFILE_VERBOSE;
236 /* Attempt to open the exec file. */
237 try_open_exec_file (exec_file_host.get (), current_inferior (), add_flags);
240 /* Set FILENAME as the new exec file.
242 This function is intended to be behave essentially the same
243 as exec_file_command, except that the latter will detect when
244 a target is being debugged, and will ask the user whether it
245 should be shut down first. (If the answer is "no", then the
246 new file is ignored.)
248 This file is used by exec_file_command, to do the work of opening
249 and processing the exec file after any prompting has happened.
251 And, it is used by child_attach, when the attach command was
252 given a pid but not a exec pathname, and the attach command could
253 figure out the pathname from the pid. (In this case, we shouldn't
254 ask the user whether the current target should be shut down --
255 we're supplying the exec pathname late for good reason.) */
257 void
258 exec_file_attach (const char *filename, int from_tty)
260 /* First, acquire a reference to the current exec_bfd. We release
261 this at the end of the function; but acquiring it now lets the
262 BFD cache return it if this call refers to the same file. */
263 gdb_bfd_ref_ptr exec_bfd_holder = gdb_bfd_ref_ptr::new_reference (exec_bfd);
265 /* Remove any previous exec file. */
266 exec_close ();
268 /* Now open and digest the file the user requested, if any. */
270 if (!filename)
272 if (from_tty)
273 printf_unfiltered (_("No executable file now.\n"));
275 set_gdbarch_from_file (NULL);
277 else
279 int load_via_target = 0;
280 const char *scratch_pathname, *canonical_pathname;
281 int scratch_chan;
282 struct target_section *sections = NULL, *sections_end = NULL;
283 char **matching;
285 if (is_target_filename (filename))
287 if (target_filesystem_is_local ())
288 filename += strlen (TARGET_SYSROOT_PREFIX);
289 else
290 load_via_target = 1;
293 gdb::unique_xmalloc_ptr<char> canonical_storage, scratch_storage;
294 if (load_via_target)
296 /* gdb_bfd_fopen does not support "target:" filenames. */
297 if (write_files)
298 warning (_("writing into executable files is "
299 "not supported for %s sysroots"),
300 TARGET_SYSROOT_PREFIX);
302 scratch_pathname = filename;
303 scratch_chan = -1;
304 canonical_pathname = scratch_pathname;
306 else
308 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
309 filename, write_files ?
310 O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
311 &scratch_storage);
312 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
313 if (scratch_chan < 0)
315 char *exename = (char *) alloca (strlen (filename) + 5);
317 strcat (strcpy (exename, filename), ".exe");
318 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
319 exename, write_files ?
320 O_RDWR | O_BINARY
321 : O_RDONLY | O_BINARY,
322 &scratch_storage);
324 #endif
325 if (scratch_chan < 0)
326 perror_with_name (filename);
328 scratch_pathname = scratch_storage.get ();
330 /* gdb_bfd_open (and its variants) prefers canonicalized
331 pathname for better BFD caching. */
332 canonical_storage = gdb_realpath (scratch_pathname);
333 canonical_pathname = canonical_storage.get ();
336 gdb_bfd_ref_ptr temp;
337 if (write_files && !load_via_target)
338 temp = gdb_bfd_fopen (canonical_pathname, gnutarget,
339 FOPEN_RUB, scratch_chan);
340 else
341 temp = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
342 exec_bfd = temp.release ();
344 if (!exec_bfd)
346 error (_("\"%s\": could not open as an executable file: %s."),
347 scratch_pathname, bfd_errmsg (bfd_get_error ()));
350 /* gdb_realpath_keepfile resolves symlinks on the local
351 filesystem and so cannot be used for "target:" files. */
352 gdb_assert (exec_filename == NULL);
353 if (load_via_target)
354 exec_filename = xstrdup (bfd_get_filename (exec_bfd));
355 else
356 exec_filename = gdb_realpath_keepfile (scratch_pathname).release ();
358 if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
360 /* Make sure to close exec_bfd, or else "run" might try to use
361 it. */
362 exec_close ();
363 error (_("\"%s\": not in executable format: %s"),
364 scratch_pathname,
365 gdb_bfd_errmsg (bfd_get_error (), matching));
368 if (build_section_table (exec_bfd, &sections, &sections_end))
370 /* Make sure to close exec_bfd, or else "run" might try to use
371 it. */
372 exec_close ();
373 error (_("\"%s\": can't find the file sections: %s"),
374 scratch_pathname, bfd_errmsg (bfd_get_error ()));
377 exec_bfd_mtime = bfd_get_mtime (exec_bfd);
379 validate_files ();
381 set_gdbarch_from_file (exec_bfd);
383 /* Add the executable's sections to the current address spaces'
384 list of sections. This possibly pushes the exec_ops
385 target. */
386 add_target_sections (&exec_bfd, sections, sections_end);
387 xfree (sections);
389 /* Tell display code (if any) about the changed file name. */
390 if (deprecated_exec_file_display_hook)
391 (*deprecated_exec_file_display_hook) (filename);
394 bfd_cache_close_all ();
395 gdb::observers::executable_changed.notify ();
398 /* Process the first arg in ARGS as the new exec file.
400 Note that we have to explicitly ignore additional args, since we can
401 be called from file_command(), which also calls symbol_file_command()
402 which can take multiple args.
404 If ARGS is NULL, we just want to close the exec file. */
406 static void
407 exec_file_command (const char *args, int from_tty)
409 if (from_tty && target_has_execution
410 && !query (_("A program is being debugged already.\n"
411 "Are you sure you want to change the file? ")))
412 error (_("File not changed."));
414 if (args)
416 /* Scan through the args and pick up the first non option arg
417 as the filename. */
419 gdb_argv built_argv (args);
420 char **argv = built_argv.get ();
422 for (; (*argv != NULL) && (**argv == '-'); argv++)
425 if (*argv == NULL)
426 error (_("No executable file name was specified"));
428 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (*argv));
429 exec_file_attach (filename.get (), from_tty);
431 else
432 exec_file_attach (NULL, from_tty);
435 /* Set both the exec file and the symbol file, in one command.
436 What a novelty. Why did GDB go through four major releases before this
437 command was added? */
439 static void
440 file_command (const char *arg, int from_tty)
442 /* FIXME, if we lose on reading the symbol file, we should revert
443 the exec file, but that's rough. */
444 exec_file_command (arg, from_tty);
445 symbol_file_command (arg, from_tty);
446 if (deprecated_file_changed_hook)
447 deprecated_file_changed_hook (arg);
451 /* Locate all mappable sections of a BFD file.
452 table_pp_char is a char * to get it through bfd_map_over_sections;
453 we cast it back to its proper type. */
455 static void
456 add_to_section_table (bfd *abfd, struct bfd_section *asect,
457 void *table_pp_char)
459 struct target_section **table_pp = (struct target_section **) table_pp_char;
460 flagword aflag;
462 gdb_assert (abfd == asect->owner);
464 /* Check the section flags, but do not discard zero-length sections, since
465 some symbols may still be attached to this section. For instance, we
466 encountered on sparc-solaris 2.10 a shared library with an empty .bss
467 section to which a symbol named "_end" was attached. The address
468 of this symbol still needs to be relocated. */
469 aflag = bfd_get_section_flags (abfd, asect);
470 if (!(aflag & SEC_ALLOC))
471 return;
473 (*table_pp)->owner = NULL;
474 (*table_pp)->the_bfd_section = asect;
475 (*table_pp)->addr = bfd_section_vma (abfd, asect);
476 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
477 (*table_pp)++;
480 /* See exec.h. */
482 void
483 clear_section_table (struct target_section_table *table)
485 xfree (table->sections);
486 table->sections = table->sections_end = NULL;
489 /* Resize section table TABLE by ADJUSTMENT.
490 ADJUSTMENT may be negative, in which case the caller must have already
491 removed the sections being deleted.
492 Returns the old size. */
494 static int
495 resize_section_table (struct target_section_table *table, int adjustment)
497 int old_count;
498 int new_count;
500 old_count = table->sections_end - table->sections;
502 new_count = adjustment + old_count;
504 if (new_count)
506 table->sections = XRESIZEVEC (struct target_section, table->sections,
507 new_count);
508 table->sections_end = table->sections + new_count;
510 else
511 clear_section_table (table);
513 return old_count;
516 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
517 Returns 0 if OK, 1 on error. */
520 build_section_table (struct bfd *some_bfd, struct target_section **start,
521 struct target_section **end)
523 unsigned count;
525 count = bfd_count_sections (some_bfd);
526 if (*start)
527 xfree (* start);
528 *start = XNEWVEC (struct target_section, count);
529 *end = *start;
530 bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
531 if (*end > *start + count)
532 internal_error (__FILE__, __LINE__,
533 _("failed internal consistency check"));
534 /* We could realloc the table, but it probably loses for most files. */
535 return 0;
538 /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
539 current set of target sections. */
541 void
542 add_target_sections (void *owner,
543 struct target_section *sections,
544 struct target_section *sections_end)
546 int count;
547 struct target_section_table *table = current_target_sections;
549 count = sections_end - sections;
551 if (count > 0)
553 int space = resize_section_table (table, count);
554 int i;
556 for (i = 0; i < count; ++i)
558 table->sections[space + i] = sections[i];
559 table->sections[space + i].owner = owner;
562 /* If these are the first file sections we can provide memory
563 from, push the file_stratum target. */
564 if (!target_is_pushed (&exec_ops))
565 push_target (&exec_ops);
569 /* Add the sections of OBJFILE to the current set of target sections. */
571 void
572 add_target_sections_of_objfile (struct objfile *objfile)
574 struct target_section_table *table = current_target_sections;
575 struct obj_section *osect;
576 int space;
577 unsigned count = 0;
578 struct target_section *ts;
580 if (objfile == NULL)
581 return;
583 /* Compute the number of sections to add. */
584 ALL_OBJFILE_OSECTIONS (objfile, osect)
586 if (bfd_get_section_size (osect->the_bfd_section) == 0)
587 continue;
588 count++;
591 if (count == 0)
592 return;
594 space = resize_section_table (table, count);
596 ts = table->sections + space;
598 ALL_OBJFILE_OSECTIONS (objfile, osect)
600 if (bfd_get_section_size (osect->the_bfd_section) == 0)
601 continue;
603 gdb_assert (ts < table->sections + space + count);
605 ts->addr = obj_section_addr (osect);
606 ts->endaddr = obj_section_endaddr (osect);
607 ts->the_bfd_section = osect->the_bfd_section;
608 ts->owner = (void *) objfile;
610 ts++;
614 /* Remove all target sections owned by OWNER.
615 OWNER must be the same value passed to add_target_sections. */
617 void
618 remove_target_sections (void *owner)
620 struct target_section *src, *dest;
621 struct target_section_table *table = current_target_sections;
623 gdb_assert (owner != NULL);
625 dest = table->sections;
626 for (src = table->sections; src < table->sections_end; src++)
627 if (src->owner != owner)
629 /* Keep this section. */
630 if (dest < src)
631 *dest = *src;
632 dest++;
635 /* If we've dropped any sections, resize the section table. */
636 if (dest < src)
638 int old_count;
640 old_count = resize_section_table (table, dest - src);
642 /* If we don't have any more sections to read memory from,
643 remove the file_stratum target from the stack. */
644 if (old_count + (dest - src) == 0)
646 struct program_space *pspace;
648 ALL_PSPACES (pspace)
649 if (pspace->target_sections.sections
650 != pspace->target_sections.sections_end)
651 return;
653 unpush_target (&exec_ops);
660 enum target_xfer_status
661 exec_read_partial_read_only (gdb_byte *readbuf, ULONGEST offset,
662 ULONGEST len, ULONGEST *xfered_len)
664 /* It's unduly pedantic to refuse to look at the executable for
665 read-only pieces; so do the equivalent of readonly regions aka
666 QTro packet. */
667 if (exec_bfd != NULL)
669 asection *s;
670 bfd_size_type size;
671 bfd_vma vma;
673 for (s = exec_bfd->sections; s; s = s->next)
675 if ((s->flags & SEC_LOAD) == 0
676 || (s->flags & SEC_READONLY) == 0)
677 continue;
679 vma = s->vma;
680 size = bfd_get_section_size (s);
681 if (vma <= offset && offset < (vma + size))
683 ULONGEST amt;
685 amt = (vma + size) - offset;
686 if (amt > len)
687 amt = len;
689 amt = bfd_get_section_contents (exec_bfd, s,
690 readbuf, offset - vma, amt);
692 if (amt == 0)
693 return TARGET_XFER_EOF;
694 else
696 *xfered_len = amt;
697 return TARGET_XFER_OK;
703 /* Indicate failure to find the requested memory block. */
704 return TARGET_XFER_E_IO;
707 /* Return all read-only memory ranges found in the target section
708 table defined by SECTIONS and SECTIONS_END, starting at (and
709 intersected with) MEMADDR for LEN bytes. */
711 static std::vector<mem_range>
712 section_table_available_memory (CORE_ADDR memaddr, ULONGEST len,
713 struct target_section *sections,
714 struct target_section *sections_end)
716 std::vector<mem_range> memory;
718 for (target_section *p = sections; p < sections_end; p++)
720 if ((bfd_get_section_flags (p->the_bfd_section->owner,
721 p->the_bfd_section)
722 & SEC_READONLY) == 0)
723 continue;
725 /* Copy the meta-data, adjusted. */
726 if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len))
728 ULONGEST lo1, hi1, lo2, hi2;
730 lo1 = memaddr;
731 hi1 = memaddr + len;
733 lo2 = p->addr;
734 hi2 = p->endaddr;
736 CORE_ADDR start = std::max (lo1, lo2);
737 int length = std::min (hi1, hi2) - start;
739 memory.emplace_back (start, length);
743 return memory;
746 enum target_xfer_status
747 section_table_read_available_memory (gdb_byte *readbuf, ULONGEST offset,
748 ULONGEST len, ULONGEST *xfered_len)
750 target_section_table *table = target_get_section_table (&exec_ops);
751 std::vector<mem_range> available_memory
752 = section_table_available_memory (offset, len,
753 table->sections, table->sections_end);
755 normalize_mem_ranges (&available_memory);
757 for (const mem_range &r : available_memory)
759 if (mem_ranges_overlap (r.start, r.length, offset, len))
761 CORE_ADDR end;
762 enum target_xfer_status status;
764 /* Get the intersection window. */
765 end = std::min<CORE_ADDR> (offset + len, r.start + r.length);
767 gdb_assert (end - offset <= len);
769 if (offset >= r.start)
770 status = exec_read_partial_read_only (readbuf, offset,
771 end - offset,
772 xfered_len);
773 else
775 *xfered_len = r.start - offset;
776 status = TARGET_XFER_UNAVAILABLE;
778 return status;
782 *xfered_len = len;
783 return TARGET_XFER_UNAVAILABLE;
786 enum target_xfer_status
787 section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
788 ULONGEST offset, ULONGEST len,
789 ULONGEST *xfered_len,
790 struct target_section *sections,
791 struct target_section *sections_end,
792 const char *section_name)
794 int res;
795 struct target_section *p;
796 ULONGEST memaddr = offset;
797 ULONGEST memend = memaddr + len;
799 if (len == 0)
800 internal_error (__FILE__, __LINE__,
801 _("failed internal consistency check"));
803 for (p = sections; p < sections_end; p++)
805 struct bfd_section *asect = p->the_bfd_section;
806 bfd *abfd = asect->owner;
808 if (section_name && strcmp (section_name, asect->name) != 0)
809 continue; /* not the section we need. */
810 if (memaddr >= p->addr)
812 if (memend <= p->endaddr)
814 /* Entire transfer is within this section. */
815 if (writebuf)
816 res = bfd_set_section_contents (abfd, asect,
817 writebuf, memaddr - p->addr,
818 len);
819 else
820 res = bfd_get_section_contents (abfd, asect,
821 readbuf, memaddr - p->addr,
822 len);
824 if (res != 0)
826 *xfered_len = len;
827 return TARGET_XFER_OK;
829 else
830 return TARGET_XFER_EOF;
832 else if (memaddr >= p->endaddr)
834 /* This section ends before the transfer starts. */
835 continue;
837 else
839 /* This section overlaps the transfer. Just do half. */
840 len = p->endaddr - memaddr;
841 if (writebuf)
842 res = bfd_set_section_contents (abfd, asect,
843 writebuf, memaddr - p->addr,
844 len);
845 else
846 res = bfd_get_section_contents (abfd, asect,
847 readbuf, memaddr - p->addr,
848 len);
849 if (res != 0)
851 *xfered_len = len;
852 return TARGET_XFER_OK;
854 else
855 return TARGET_XFER_EOF;
860 return TARGET_XFER_EOF; /* We can't help. */
863 struct target_section_table *
864 exec_target::get_section_table ()
866 return current_target_sections;
869 enum target_xfer_status
870 exec_target::xfer_partial (enum target_object object,
871 const char *annex, gdb_byte *readbuf,
872 const gdb_byte *writebuf,
873 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
875 struct target_section_table *table = get_section_table ();
877 if (object == TARGET_OBJECT_MEMORY)
878 return section_table_xfer_memory_partial (readbuf, writebuf,
879 offset, len, xfered_len,
880 table->sections,
881 table->sections_end,
882 NULL);
883 else
884 return TARGET_XFER_E_IO;
888 void
889 print_section_info (struct target_section_table *t, bfd *abfd)
891 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
892 struct target_section *p;
893 /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */
894 int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
896 printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
897 wrap_here (" ");
898 printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
899 if (abfd == exec_bfd)
901 /* gcc-3.4 does not like the initialization in
902 <p == t->sections_end>. */
903 bfd_vma displacement = 0;
904 bfd_vma entry_point;
906 for (p = t->sections; p < t->sections_end; p++)
908 struct bfd_section *psect = p->the_bfd_section;
909 bfd *pbfd = psect->owner;
911 if ((bfd_get_section_flags (pbfd, psect) & (SEC_ALLOC | SEC_LOAD))
912 != (SEC_ALLOC | SEC_LOAD))
913 continue;
915 if (bfd_get_section_vma (pbfd, psect) <= abfd->start_address
916 && abfd->start_address < (bfd_get_section_vma (pbfd, psect)
917 + bfd_get_section_size (psect)))
919 displacement = p->addr - bfd_get_section_vma (pbfd, psect);
920 break;
923 if (p == t->sections_end)
924 warning (_("Cannot find section for the entry point of %s."),
925 bfd_get_filename (abfd));
927 entry_point = gdbarch_addr_bits_remove (gdbarch,
928 bfd_get_start_address (abfd)
929 + displacement);
930 printf_filtered (_("\tEntry point: %s\n"),
931 paddress (gdbarch, entry_point));
933 for (p = t->sections; p < t->sections_end; p++)
935 struct bfd_section *psect = p->the_bfd_section;
936 bfd *pbfd = psect->owner;
938 printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
939 printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
941 /* FIXME: A format of "08l" is not wide enough for file offsets
942 larger than 4GB. OTOH, making it "016l" isn't desirable either
943 since most output will then be much wider than necessary. It
944 may make sense to test the size of the file and choose the
945 format string accordingly. */
946 /* FIXME: i18n: Need to rewrite this sentence. */
947 if (info_verbose)
948 printf_filtered (" @ %s",
949 hex_string_custom (psect->filepos, 8));
950 printf_filtered (" is %s", bfd_section_name (pbfd, psect));
951 if (pbfd != abfd)
952 printf_filtered (" in %s", bfd_get_filename (pbfd));
953 printf_filtered ("\n");
957 void
958 exec_target::files_info ()
960 if (exec_bfd)
961 print_section_info (current_target_sections, exec_bfd);
962 else
963 puts_filtered (_("\t<no file loaded>\n"));
966 static void
967 set_section_command (const char *args, int from_tty)
969 struct target_section *p;
970 const char *secname;
971 unsigned seclen;
972 unsigned long secaddr;
973 char secprint[100];
974 long offset;
975 struct target_section_table *table;
977 if (args == 0)
978 error (_("Must specify section name and its virtual address"));
980 /* Parse out section name. */
981 for (secname = args; !isspace (*args); args++);
982 seclen = args - secname;
984 /* Parse out new virtual address. */
985 secaddr = parse_and_eval_address (args);
987 table = current_target_sections;
988 for (p = table->sections; p < table->sections_end; p++)
990 if (!strncmp (secname, bfd_section_name (p->bfd,
991 p->the_bfd_section), seclen)
992 && bfd_section_name (p->bfd, p->the_bfd_section)[seclen] == '\0')
994 offset = secaddr - p->addr;
995 p->addr += offset;
996 p->endaddr += offset;
997 if (from_tty)
998 exec_ops.files_info ();
999 return;
1002 if (seclen >= sizeof (secprint))
1003 seclen = sizeof (secprint) - 1;
1004 strncpy (secprint, secname, seclen);
1005 secprint[seclen] = '\0';
1006 error (_("Section %s not found"), secprint);
1009 /* If we can find a section in FILENAME with BFD index INDEX, adjust
1010 it to ADDRESS. */
1012 void
1013 exec_set_section_address (const char *filename, int index, CORE_ADDR address)
1015 struct target_section *p;
1016 struct target_section_table *table;
1018 table = current_target_sections;
1019 for (p = table->sections; p < table->sections_end; p++)
1021 if (filename_cmp (filename, p->the_bfd_section->owner->filename) == 0
1022 && index == p->the_bfd_section->index)
1024 p->endaddr += address - p->addr;
1025 p->addr = address;
1030 bool
1031 exec_target::has_memory ()
1033 /* We can provide memory if we have any file/target sections to read
1034 from. */
1035 return (current_target_sections->sections
1036 != current_target_sections->sections_end);
1039 char *
1040 exec_target::make_corefile_notes (bfd *obfd, int *note_size)
1042 error (_("Can't create a corefile"));
1046 exec_target::find_memory_regions (find_memory_region_ftype func, void *data)
1048 return objfile_find_memory_regions (this, func, data);
1051 void
1052 _initialize_exec (void)
1054 struct cmd_list_element *c;
1056 if (!dbx_commands)
1058 c = add_cmd ("file", class_files, file_command, _("\
1059 Use FILE as program to be debugged.\n\
1060 It is read for its symbols, for getting the contents of pure memory,\n\
1061 and it is the program executed when you use the `run' command.\n\
1062 If FILE cannot be found as specified, your execution directory path\n\
1063 ($PATH) is searched for a command of that name.\n\
1064 No arg means to have no executable file and no symbols."), &cmdlist);
1065 set_cmd_completer (c, filename_completer);
1068 c = add_cmd ("exec-file", class_files, exec_file_command, _("\
1069 Use FILE as program for getting contents of pure memory.\n\
1070 If FILE cannot be found as specified, your execution directory path\n\
1071 is searched for a command of that name.\n\
1072 No arg means have no executable file."), &cmdlist);
1073 set_cmd_completer (c, filename_completer);
1075 add_com ("section", class_files, set_section_command, _("\
1076 Change the base address of section SECTION of the exec file to ADDR.\n\
1077 This can be used if the exec file does not contain section addresses,\n\
1078 (such as in the a.out format), or when the addresses specified in the\n\
1079 file itself are wrong. Each section must be changed separately. The\n\
1080 ``info files'' command lists all the sections and their addresses."));
1082 add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
1083 Set writing into executable and core files."), _("\
1084 Show writing into executable and core files."), NULL,
1085 NULL,
1086 show_write_files,
1087 &setlist, &showlist);
1089 add_target (exec_target_info, exec_target_open, filename_completer);