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/>. */
26 #include "filenames.h"
29 #include "completer.h"
32 #include "observable.h"
33 #include "arch-utils.h"
34 #include "gdbthread.h"
35 #include "progspace.h"
41 #include "readline/readline.h"
48 #include "common/pathstuff.h"
50 void (*deprecated_file_changed_hook
) (const char *);
52 /* The target vector for executable files. */
54 struct exec_target final
: public target_ops
57 { to_stratum
= file_stratum
; }
59 const char *shortname () override
62 const char *longname () override
63 { return _("Local exec file"); }
65 const char *doc () override
68 Use an executable file as a target.\n\
69 Specify the filename of the executable file.");
72 void open (const char *, int) override
;
73 void close () override
;
74 enum target_xfer_status
xfer_partial (enum target_object object
,
77 const gdb_byte
*writebuf
,
78 ULONGEST offset
, ULONGEST len
,
79 ULONGEST
*xfered_len
) override
;
80 struct target_section_table
*get_section_table () override
;
81 void files_info () override
;
83 int has_memory () override
;
84 char *make_corefile_notes (bfd
*, int *) override
;
85 int find_memory_regions (find_memory_region_ftype func
, void *data
) override
;
88 static exec_target exec_ops
;
90 /* Whether to open exec and core files read-only or read-write. */
94 show_write_files (struct ui_file
*file
, int from_tty
,
95 struct cmd_list_element
*c
, const char *value
)
97 fprintf_filtered (file
, _("Writing into executable and core files is %s.\n"),
103 exec_target::open (const char *args
, int from_tty
)
105 target_preopen (from_tty
);
106 exec_file_attach (args
, from_tty
);
109 /* Close and clear exec_bfd. If we end up with no target sections to
110 read memory from, this unpushes the exec_ops target. */
117 bfd
*abfd
= exec_bfd
;
119 gdb_bfd_unref (abfd
);
121 /* Removing target sections may close the exec_ops target.
122 Clear exec_bfd before doing so to prevent recursion. */
126 remove_target_sections (&exec_bfd
);
128 xfree (exec_filename
);
129 exec_filename
= NULL
;
133 /* This is the target_close implementation. Clears all target
134 sections and closes all executable bfds from all program spaces. */
137 exec_target::close ()
139 struct program_space
*ss
;
140 scoped_restore_current_program_space restore_pspace
;
144 set_current_program_space (ss
);
145 clear_section_table (current_target_sections
);
153 try_open_exec_file (const char *exec_file_host
, struct inferior
*inf
,
154 symfile_add_flags add_flags
)
156 struct cleanup
*old_chain
;
157 struct gdb_exception prev_err
= exception_none
;
159 old_chain
= make_cleanup (free_current_contents
, &prev_err
.message
);
161 /* exec_file_attach and symbol_file_add_main may throw an error if the file
162 cannot be opened either locally or remotely.
164 This happens for example, when the file is first found in the local
165 sysroot (above), and then disappears (a TOCTOU race), or when it doesn't
166 exist in the target filesystem, or when the file does exist, but
169 Even without a symbol file, the remote-based debugging session should
170 continue normally instead of ending abruptly. Hence we catch thrown
171 errors/exceptions in the following code. */
174 /* We must do this step even if exec_file_host is NULL, so that
175 exec_file_attach will clear state. */
176 exec_file_attach (exec_file_host
, add_flags
& SYMFILE_VERBOSE
);
178 CATCH (err
, RETURN_MASK_ERROR
)
180 if (err
.message
!= NULL
)
181 warning ("%s", err
.message
);
185 /* Save message so it doesn't get trashed by the catch below. */
186 if (err
.message
!= NULL
)
187 prev_err
.message
= xstrdup (err
.message
);
191 if (exec_file_host
!= NULL
)
195 symbol_file_add_main (exec_file_host
, add_flags
);
197 CATCH (err
, RETURN_MASK_ERROR
)
199 if (!exception_print_same (prev_err
, err
))
200 warning ("%s", err
.message
);
205 do_cleanups (old_chain
);
211 exec_file_locate_attach (int pid
, int defer_bp_reset
, int from_tty
)
213 char *exec_file_target
;
214 symfile_add_flags add_flags
= 0;
216 /* Do nothing if we already have an executable filename. */
217 if (get_exec_file (0) != NULL
)
220 /* Try to determine a filename from the process itself. */
221 exec_file_target
= target_pid_to_exec_file (pid
);
222 if (exec_file_target
== NULL
)
224 warning (_("No executable has been specified and target does not "
226 "determining executable automatically. "
227 "Try using the \"file\" command."));
231 gdb::unique_xmalloc_ptr
<char> exec_file_host
232 = exec_file_find (exec_file_target
, NULL
);
235 add_flags
|= SYMFILE_DEFER_BP_RESET
;
238 add_flags
|= SYMFILE_VERBOSE
;
240 /* Attempt to open the exec file. */
241 try_open_exec_file (exec_file_host
.get (), current_inferior (), add_flags
);
244 /* Set FILENAME as the new exec file.
246 This function is intended to be behave essentially the same
247 as exec_file_command, except that the latter will detect when
248 a target is being debugged, and will ask the user whether it
249 should be shut down first. (If the answer is "no", then the
250 new file is ignored.)
252 This file is used by exec_file_command, to do the work of opening
253 and processing the exec file after any prompting has happened.
255 And, it is used by child_attach, when the attach command was
256 given a pid but not a exec pathname, and the attach command could
257 figure out the pathname from the pid. (In this case, we shouldn't
258 ask the user whether the current target should be shut down --
259 we're supplying the exec pathname late for good reason.) */
262 exec_file_attach (const char *filename
, int from_tty
)
264 /* First, acquire a reference to the current exec_bfd. We release
265 this at the end of the function; but acquiring it now lets the
266 BFD cache return it if this call refers to the same file. */
267 gdb_bfd_ref_ptr exec_bfd_holder
= gdb_bfd_ref_ptr::new_reference (exec_bfd
);
269 /* Remove any previous exec file. */
272 /* Now open and digest the file the user requested, if any. */
277 printf_unfiltered (_("No executable file now.\n"));
279 set_gdbarch_from_file (NULL
);
283 int load_via_target
= 0;
284 const char *scratch_pathname
, *canonical_pathname
;
286 struct target_section
*sections
= NULL
, *sections_end
= NULL
;
289 if (is_target_filename (filename
))
291 if (target_filesystem_is_local ())
292 filename
+= strlen (TARGET_SYSROOT_PREFIX
);
297 gdb::unique_xmalloc_ptr
<char> canonical_storage
, scratch_storage
;
300 /* gdb_bfd_fopen does not support "target:" filenames. */
302 warning (_("writing into executable files is "
303 "not supported for %s sysroots"),
304 TARGET_SYSROOT_PREFIX
);
306 scratch_pathname
= filename
;
308 canonical_pathname
= scratch_pathname
;
312 scratch_chan
= openp (getenv ("PATH"), OPF_TRY_CWD_FIRST
,
313 filename
, write_files
?
314 O_RDWR
| O_BINARY
: O_RDONLY
| O_BINARY
,
316 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
317 if (scratch_chan
< 0)
319 char *exename
= (char *) alloca (strlen (filename
) + 5);
321 strcat (strcpy (exename
, filename
), ".exe");
322 scratch_chan
= openp (getenv ("PATH"), OPF_TRY_CWD_FIRST
,
323 exename
, write_files
?
325 : O_RDONLY
| O_BINARY
,
329 if (scratch_chan
< 0)
330 perror_with_name (filename
);
332 scratch_pathname
= scratch_storage
.get ();
334 /* gdb_bfd_open (and its variants) prefers canonicalized
335 pathname for better BFD caching. */
336 canonical_storage
= gdb_realpath (scratch_pathname
);
337 canonical_pathname
= canonical_storage
.get ();
340 gdb_bfd_ref_ptr temp
;
341 if (write_files
&& !load_via_target
)
342 temp
= gdb_bfd_fopen (canonical_pathname
, gnutarget
,
343 FOPEN_RUB
, scratch_chan
);
345 temp
= gdb_bfd_open (canonical_pathname
, gnutarget
, scratch_chan
);
346 exec_bfd
= temp
.release ();
350 error (_("\"%s\": could not open as an executable file: %s."),
351 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
354 /* gdb_realpath_keepfile resolves symlinks on the local
355 filesystem and so cannot be used for "target:" files. */
356 gdb_assert (exec_filename
== NULL
);
358 exec_filename
= xstrdup (bfd_get_filename (exec_bfd
));
360 exec_filename
= gdb_realpath_keepfile (scratch_pathname
).release ();
362 if (!bfd_check_format_matches (exec_bfd
, bfd_object
, &matching
))
364 /* Make sure to close exec_bfd, or else "run" might try to use
367 error (_("\"%s\": not in executable format: %s"),
369 gdb_bfd_errmsg (bfd_get_error (), matching
));
372 if (build_section_table (exec_bfd
, §ions
, §ions_end
))
374 /* Make sure to close exec_bfd, or else "run" might try to use
377 error (_("\"%s\": can't find the file sections: %s"),
378 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
381 exec_bfd_mtime
= bfd_get_mtime (exec_bfd
);
385 set_gdbarch_from_file (exec_bfd
);
387 /* Add the executable's sections to the current address spaces'
388 list of sections. This possibly pushes the exec_ops
390 add_target_sections (&exec_bfd
, sections
, sections_end
);
393 /* Tell display code (if any) about the changed file name. */
394 if (deprecated_exec_file_display_hook
)
395 (*deprecated_exec_file_display_hook
) (filename
);
398 bfd_cache_close_all ();
399 gdb::observers::executable_changed
.notify ();
402 /* Process the first arg in ARGS as the new exec file.
404 Note that we have to explicitly ignore additional args, since we can
405 be called from file_command(), which also calls symbol_file_command()
406 which can take multiple args.
408 If ARGS is NULL, we just want to close the exec file. */
411 exec_file_command (const char *args
, int from_tty
)
413 if (from_tty
&& target_has_execution
414 && !query (_("A program is being debugged already.\n"
415 "Are you sure you want to change the file? ")))
416 error (_("File not changed."));
420 /* Scan through the args and pick up the first non option arg
423 gdb_argv
built_argv (args
);
424 char **argv
= built_argv
.get ();
426 for (; (*argv
!= NULL
) && (**argv
== '-'); argv
++)
430 error (_("No executable file name was specified"));
432 gdb::unique_xmalloc_ptr
<char> filename (tilde_expand (*argv
));
433 exec_file_attach (filename
.get (), from_tty
);
436 exec_file_attach (NULL
, from_tty
);
439 /* Set both the exec file and the symbol file, in one command.
440 What a novelty. Why did GDB go through four major releases before this
441 command was added? */
444 file_command (const char *arg
, int from_tty
)
446 /* FIXME, if we lose on reading the symbol file, we should revert
447 the exec file, but that's rough. */
448 exec_file_command (arg
, from_tty
);
449 symbol_file_command (arg
, from_tty
);
450 if (deprecated_file_changed_hook
)
451 deprecated_file_changed_hook (arg
);
455 /* Locate all mappable sections of a BFD file.
456 table_pp_char is a char * to get it through bfd_map_over_sections;
457 we cast it back to its proper type. */
460 add_to_section_table (bfd
*abfd
, struct bfd_section
*asect
,
463 struct target_section
**table_pp
= (struct target_section
**) table_pp_char
;
466 gdb_assert (abfd
== asect
->owner
);
468 /* Check the section flags, but do not discard zero-length sections, since
469 some symbols may still be attached to this section. For instance, we
470 encountered on sparc-solaris 2.10 a shared library with an empty .bss
471 section to which a symbol named "_end" was attached. The address
472 of this symbol still needs to be relocated. */
473 aflag
= bfd_get_section_flags (abfd
, asect
);
474 if (!(aflag
& SEC_ALLOC
))
477 (*table_pp
)->owner
= NULL
;
478 (*table_pp
)->the_bfd_section
= asect
;
479 (*table_pp
)->addr
= bfd_section_vma (abfd
, asect
);
480 (*table_pp
)->endaddr
= (*table_pp
)->addr
+ bfd_section_size (abfd
, asect
);
487 clear_section_table (struct target_section_table
*table
)
489 xfree (table
->sections
);
490 table
->sections
= table
->sections_end
= NULL
;
493 /* Resize section table TABLE by ADJUSTMENT.
494 ADJUSTMENT may be negative, in which case the caller must have already
495 removed the sections being deleted.
496 Returns the old size. */
499 resize_section_table (struct target_section_table
*table
, int adjustment
)
504 old_count
= table
->sections_end
- table
->sections
;
506 new_count
= adjustment
+ old_count
;
510 table
->sections
= XRESIZEVEC (struct target_section
, table
->sections
,
512 table
->sections_end
= table
->sections
+ new_count
;
515 clear_section_table (table
);
520 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
521 Returns 0 if OK, 1 on error. */
524 build_section_table (struct bfd
*some_bfd
, struct target_section
**start
,
525 struct target_section
**end
)
529 count
= bfd_count_sections (some_bfd
);
532 *start
= XNEWVEC (struct target_section
, count
);
534 bfd_map_over_sections (some_bfd
, add_to_section_table
, (char *) end
);
535 if (*end
> *start
+ count
)
536 internal_error (__FILE__
, __LINE__
,
537 _("failed internal consistency check"));
538 /* We could realloc the table, but it probably loses for most files. */
542 /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
543 current set of target sections. */
546 add_target_sections (void *owner
,
547 struct target_section
*sections
,
548 struct target_section
*sections_end
)
551 struct target_section_table
*table
= current_target_sections
;
553 count
= sections_end
- sections
;
557 int space
= resize_section_table (table
, count
);
560 for (i
= 0; i
< count
; ++i
)
562 table
->sections
[space
+ i
] = sections
[i
];
563 table
->sections
[space
+ i
].owner
= owner
;
566 /* If these are the first file sections we can provide memory
567 from, push the file_stratum target. */
568 if (!target_is_pushed (&exec_ops
))
569 push_target (&exec_ops
);
573 /* Add the sections of OBJFILE to the current set of target sections. */
576 add_target_sections_of_objfile (struct objfile
*objfile
)
578 struct target_section_table
*table
= current_target_sections
;
579 struct obj_section
*osect
;
582 struct target_section
*ts
;
587 /* Compute the number of sections to add. */
588 ALL_OBJFILE_OSECTIONS (objfile
, osect
)
590 if (bfd_get_section_size (osect
->the_bfd_section
) == 0)
598 space
= resize_section_table (table
, count
);
600 ts
= table
->sections
+ space
;
602 ALL_OBJFILE_OSECTIONS (objfile
, osect
)
604 if (bfd_get_section_size (osect
->the_bfd_section
) == 0)
607 gdb_assert (ts
< table
->sections
+ space
+ count
);
609 ts
->addr
= obj_section_addr (osect
);
610 ts
->endaddr
= obj_section_endaddr (osect
);
611 ts
->the_bfd_section
= osect
->the_bfd_section
;
612 ts
->owner
= (void *) objfile
;
618 /* Remove all target sections owned by OWNER.
619 OWNER must be the same value passed to add_target_sections. */
622 remove_target_sections (void *owner
)
624 struct target_section
*src
, *dest
;
625 struct target_section_table
*table
= current_target_sections
;
627 gdb_assert (owner
!= NULL
);
629 dest
= table
->sections
;
630 for (src
= table
->sections
; src
< table
->sections_end
; src
++)
631 if (src
->owner
!= owner
)
633 /* Keep this section. */
639 /* If we've dropped any sections, resize the section table. */
644 old_count
= resize_section_table (table
, dest
- src
);
646 /* If we don't have any more sections to read memory from,
647 remove the file_stratum target from the stack. */
648 if (old_count
+ (dest
- src
) == 0)
650 struct program_space
*pspace
;
653 if (pspace
->target_sections
.sections
654 != pspace
->target_sections
.sections_end
)
657 unpush_target (&exec_ops
);
664 enum target_xfer_status
665 exec_read_partial_read_only (gdb_byte
*readbuf
, ULONGEST offset
,
666 ULONGEST len
, ULONGEST
*xfered_len
)
668 /* It's unduly pedantic to refuse to look at the executable for
669 read-only pieces; so do the equivalent of readonly regions aka
671 if (exec_bfd
!= NULL
)
677 for (s
= exec_bfd
->sections
; s
; s
= s
->next
)
679 if ((s
->flags
& SEC_LOAD
) == 0
680 || (s
->flags
& SEC_READONLY
) == 0)
684 size
= bfd_get_section_size (s
);
685 if (vma
<= offset
&& offset
< (vma
+ size
))
689 amt
= (vma
+ size
) - offset
;
693 amt
= bfd_get_section_contents (exec_bfd
, s
,
694 readbuf
, offset
- vma
, amt
);
697 return TARGET_XFER_EOF
;
701 return TARGET_XFER_OK
;
707 /* Indicate failure to find the requested memory block. */
708 return TARGET_XFER_E_IO
;
711 /* Return all read-only memory ranges found in the target section
712 table defined by SECTIONS and SECTIONS_END, starting at (and
713 intersected with) MEMADDR for LEN bytes. */
715 static std::vector
<mem_range
>
716 section_table_available_memory (CORE_ADDR memaddr
, ULONGEST len
,
717 struct target_section
*sections
,
718 struct target_section
*sections_end
)
720 std::vector
<mem_range
> memory
;
722 for (target_section
*p
= sections
; p
< sections_end
; p
++)
724 if ((bfd_get_section_flags (p
->the_bfd_section
->owner
,
726 & SEC_READONLY
) == 0)
729 /* Copy the meta-data, adjusted. */
730 if (mem_ranges_overlap (p
->addr
, p
->endaddr
- p
->addr
, memaddr
, len
))
732 ULONGEST lo1
, hi1
, lo2
, hi2
;
740 CORE_ADDR start
= std::max (lo1
, lo2
);
741 int length
= std::min (hi1
, hi2
) - start
;
743 memory
.emplace_back (start
, length
);
750 enum target_xfer_status
751 section_table_read_available_memory (gdb_byte
*readbuf
, ULONGEST offset
,
752 ULONGEST len
, ULONGEST
*xfered_len
)
754 target_section_table
*table
= target_get_section_table (&exec_ops
);
755 std::vector
<mem_range
> available_memory
756 = section_table_available_memory (offset
, len
,
757 table
->sections
, table
->sections_end
);
759 normalize_mem_ranges (&available_memory
);
761 for (const mem_range
&r
: available_memory
)
763 if (mem_ranges_overlap (r
.start
, r
.length
, offset
, len
))
766 enum target_xfer_status status
;
768 /* Get the intersection window. */
769 end
= std::min
<CORE_ADDR
> (offset
+ len
, r
.start
+ r
.length
);
771 gdb_assert (end
- offset
<= len
);
773 if (offset
>= r
.start
)
774 status
= exec_read_partial_read_only (readbuf
, offset
,
779 *xfered_len
= r
.start
- offset
;
780 status
= TARGET_XFER_UNAVAILABLE
;
787 return TARGET_XFER_UNAVAILABLE
;
790 enum target_xfer_status
791 section_table_xfer_memory_partial (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
792 ULONGEST offset
, ULONGEST len
,
793 ULONGEST
*xfered_len
,
794 struct target_section
*sections
,
795 struct target_section
*sections_end
,
796 const char *section_name
)
799 struct target_section
*p
;
800 ULONGEST memaddr
= offset
;
801 ULONGEST memend
= memaddr
+ len
;
804 internal_error (__FILE__
, __LINE__
,
805 _("failed internal consistency check"));
807 for (p
= sections
; p
< sections_end
; p
++)
809 struct bfd_section
*asect
= p
->the_bfd_section
;
810 bfd
*abfd
= asect
->owner
;
812 if (section_name
&& strcmp (section_name
, asect
->name
) != 0)
813 continue; /* not the section we need. */
814 if (memaddr
>= p
->addr
)
816 if (memend
<= p
->endaddr
)
818 /* Entire transfer is within this section. */
820 res
= bfd_set_section_contents (abfd
, asect
,
821 writebuf
, memaddr
- p
->addr
,
824 res
= bfd_get_section_contents (abfd
, asect
,
825 readbuf
, memaddr
- p
->addr
,
831 return TARGET_XFER_OK
;
834 return TARGET_XFER_EOF
;
836 else if (memaddr
>= p
->endaddr
)
838 /* This section ends before the transfer starts. */
843 /* This section overlaps the transfer. Just do half. */
844 len
= p
->endaddr
- memaddr
;
846 res
= bfd_set_section_contents (abfd
, asect
,
847 writebuf
, memaddr
- p
->addr
,
850 res
= bfd_get_section_contents (abfd
, asect
,
851 readbuf
, memaddr
- p
->addr
,
856 return TARGET_XFER_OK
;
859 return TARGET_XFER_EOF
;
864 return TARGET_XFER_EOF
; /* We can't help. */
867 struct target_section_table
*
868 exec_target::get_section_table ()
870 return current_target_sections
;
873 enum target_xfer_status
874 exec_target::xfer_partial (enum target_object object
,
875 const char *annex
, gdb_byte
*readbuf
,
876 const gdb_byte
*writebuf
,
877 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
879 struct target_section_table
*table
= get_section_table ();
881 if (object
== TARGET_OBJECT_MEMORY
)
882 return section_table_xfer_memory_partial (readbuf
, writebuf
,
883 offset
, len
, xfered_len
,
888 return TARGET_XFER_E_IO
;
893 print_section_info (struct target_section_table
*t
, bfd
*abfd
)
895 struct gdbarch
*gdbarch
= gdbarch_from_bfd (abfd
);
896 struct target_section
*p
;
897 /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */
898 int wid
= gdbarch_addr_bit (gdbarch
) <= 32 ? 8 : 16;
900 printf_filtered ("\t`%s', ", bfd_get_filename (abfd
));
902 printf_filtered (_("file type %s.\n"), bfd_get_target (abfd
));
903 if (abfd
== exec_bfd
)
905 /* gcc-3.4 does not like the initialization in
906 <p == t->sections_end>. */
907 bfd_vma displacement
= 0;
910 for (p
= t
->sections
; p
< t
->sections_end
; p
++)
912 struct bfd_section
*psect
= p
->the_bfd_section
;
913 bfd
*pbfd
= psect
->owner
;
915 if ((bfd_get_section_flags (pbfd
, psect
) & (SEC_ALLOC
| SEC_LOAD
))
916 != (SEC_ALLOC
| SEC_LOAD
))
919 if (bfd_get_section_vma (pbfd
, psect
) <= abfd
->start_address
920 && abfd
->start_address
< (bfd_get_section_vma (pbfd
, psect
)
921 + bfd_get_section_size (psect
)))
923 displacement
= p
->addr
- bfd_get_section_vma (pbfd
, psect
);
927 if (p
== t
->sections_end
)
928 warning (_("Cannot find section for the entry point of %s."),
929 bfd_get_filename (abfd
));
931 entry_point
= gdbarch_addr_bits_remove (gdbarch
,
932 bfd_get_start_address (abfd
)
934 printf_filtered (_("\tEntry point: %s\n"),
935 paddress (gdbarch
, entry_point
));
937 for (p
= t
->sections
; p
< t
->sections_end
; p
++)
939 struct bfd_section
*psect
= p
->the_bfd_section
;
940 bfd
*pbfd
= psect
->owner
;
942 printf_filtered ("\t%s", hex_string_custom (p
->addr
, wid
));
943 printf_filtered (" - %s", hex_string_custom (p
->endaddr
, wid
));
945 /* FIXME: A format of "08l" is not wide enough for file offsets
946 larger than 4GB. OTOH, making it "016l" isn't desirable either
947 since most output will then be much wider than necessary. It
948 may make sense to test the size of the file and choose the
949 format string accordingly. */
950 /* FIXME: i18n: Need to rewrite this sentence. */
952 printf_filtered (" @ %s",
953 hex_string_custom (psect
->filepos
, 8));
954 printf_filtered (" is %s", bfd_section_name (pbfd
, psect
));
956 printf_filtered (" in %s", bfd_get_filename (pbfd
));
957 printf_filtered ("\n");
962 exec_target::files_info ()
965 print_section_info (current_target_sections
, exec_bfd
);
967 puts_filtered (_("\t<no file loaded>\n"));
971 set_section_command (const char *args
, int from_tty
)
973 struct target_section
*p
;
976 unsigned long secaddr
;
979 struct target_section_table
*table
;
982 error (_("Must specify section name and its virtual address"));
984 /* Parse out section name. */
985 for (secname
= args
; !isspace (*args
); args
++);
986 seclen
= args
- secname
;
988 /* Parse out new virtual address. */
989 secaddr
= parse_and_eval_address (args
);
991 table
= current_target_sections
;
992 for (p
= table
->sections
; p
< table
->sections_end
; p
++)
994 if (!strncmp (secname
, bfd_section_name (p
->bfd
,
995 p
->the_bfd_section
), seclen
)
996 && bfd_section_name (p
->bfd
, p
->the_bfd_section
)[seclen
] == '\0')
998 offset
= secaddr
- p
->addr
;
1000 p
->endaddr
+= offset
;
1002 exec_ops
.files_info ();
1006 if (seclen
>= sizeof (secprint
))
1007 seclen
= sizeof (secprint
) - 1;
1008 strncpy (secprint
, secname
, seclen
);
1009 secprint
[seclen
] = '\0';
1010 error (_("Section %s not found"), secprint
);
1013 /* If we can find a section in FILENAME with BFD index INDEX, adjust
1017 exec_set_section_address (const char *filename
, int index
, CORE_ADDR address
)
1019 struct target_section
*p
;
1020 struct target_section_table
*table
;
1022 table
= current_target_sections
;
1023 for (p
= table
->sections
; p
< table
->sections_end
; p
++)
1025 if (filename_cmp (filename
, p
->the_bfd_section
->owner
->filename
) == 0
1026 && index
== p
->the_bfd_section
->index
)
1028 p
->endaddr
+= address
- p
->addr
;
1035 exec_target::has_memory ()
1037 /* We can provide memory if we have any file/target sections to read
1039 return (current_target_sections
->sections
1040 != current_target_sections
->sections_end
);
1044 exec_target::make_corefile_notes (bfd
*obfd
, int *note_size
)
1046 error (_("Can't create a corefile"));
1050 exec_target::find_memory_regions (find_memory_region_ftype func
, void *data
)
1052 return objfile_find_memory_regions (this, func
, data
);
1056 _initialize_exec (void)
1058 struct cmd_list_element
*c
;
1062 c
= add_cmd ("file", class_files
, file_command
, _("\
1063 Use FILE as program to be debugged.\n\
1064 It is read for its symbols, for getting the contents of pure memory,\n\
1065 and it is the program executed when you use the `run' command.\n\
1066 If FILE cannot be found as specified, your execution directory path\n\
1067 ($PATH) is searched for a command of that name.\n\
1068 No arg means to have no executable file and no symbols."), &cmdlist
);
1069 set_cmd_completer (c
, filename_completer
);
1072 c
= add_cmd ("exec-file", class_files
, exec_file_command
, _("\
1073 Use FILE as program for getting contents of pure memory.\n\
1074 If FILE cannot be found as specified, your execution directory path\n\
1075 is searched for a command of that name.\n\
1076 No arg means have no executable file."), &cmdlist
);
1077 set_cmd_completer (c
, filename_completer
);
1079 add_com ("section", class_files
, set_section_command
, _("\
1080 Change the base address of section SECTION of the exec file to ADDR.\n\
1081 This can be used if the exec file does not contain section addresses,\n\
1082 (such as in the a.out format), or when the addresses specified in the\n\
1083 file itself are wrong. Each section must be changed separately. The\n\
1084 ``info files'' command lists all the sections and their addresses."));
1086 add_setshow_boolean_cmd ("write", class_support
, &write_files
, _("\
1087 Set writing into executable and core files."), _("\
1088 Show writing into executable and core files."), NULL
,
1091 &setlist
, &showlist
);
1093 add_target_with_completer (&exec_ops
, filename_completer
);