1 /* Work with executable files, for GDB.
3 Copyright (C) 1988-2025 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/>. */
23 #include "cli/cli-cmds.h"
25 #include "filenames.h"
28 #include "completer.h"
31 #include "observable.h"
32 #include "arch-utils.h"
33 #include "gdbthread.h"
34 #include "progspace.h"
35 #include "progspace-and-thread.h"
42 #include "readline/tilde.h"
49 #include "gdbsupport/pathstuff.h"
50 #include "cli/cli-style.h"
51 #include "gdbsupport/buildargv.h"
53 void (*deprecated_file_changed_hook
) (const char *);
55 static const target_info exec_target_info
= {
57 N_("Local exec file"),
58 N_("Use an executable file as a target.\n\
59 Specify the filename of the executable file.")
62 /* The target vector for executable files. */
64 struct exec_target final
: public target_ops
66 const target_info
&info () const override
67 { return exec_target_info
; }
69 strata
stratum () const override
{ return file_stratum
; }
71 void close () override
;
72 enum target_xfer_status
xfer_partial (enum target_object object
,
75 const gdb_byte
*writebuf
,
76 ULONGEST offset
, ULONGEST len
,
77 ULONGEST
*xfered_len
) override
;
78 void files_info () override
;
80 bool has_memory () override
;
81 gdb::unique_xmalloc_ptr
<char> make_corefile_notes (bfd
*, int *) override
;
82 int find_memory_regions (find_memory_region_ftype func
, void *data
) override
;
85 static exec_target exec_ops
;
87 /* How to handle a mismatch between the current exec file and the exec
88 file determined from target. */
90 static const char *const exec_file_mismatch_names
[]
91 = {"ask", "warn", "off", NULL
};
92 enum exec_file_mismatch_mode
94 exec_file_mismatch_ask
, exec_file_mismatch_warn
, exec_file_mismatch_off
96 static const char *exec_file_mismatch
= exec_file_mismatch_names
[0];
97 static enum exec_file_mismatch_mode exec_file_mismatch_mode
98 = exec_file_mismatch_ask
;
102 show_exec_file_mismatch_command (struct ui_file
*file
, int from_tty
,
103 struct cmd_list_element
*c
, const char *value
)
106 _("exec-file-mismatch handling is currently \"%s\".\n"),
107 exec_file_mismatch_names
[exec_file_mismatch_mode
]);
110 /* Set command. Change the setting for range checking. */
112 set_exec_file_mismatch_command (const char *ignore
,
113 int from_tty
, struct cmd_list_element
*c
)
115 for (enum exec_file_mismatch_mode mode
= exec_file_mismatch_ask
;
117 mode
= static_cast<enum exec_file_mismatch_mode
>(1 + (int) mode
))
119 if (strcmp (exec_file_mismatch
, exec_file_mismatch_names
[mode
]) == 0)
121 exec_file_mismatch_mode
= mode
;
124 if (mode
== exec_file_mismatch_off
)
125 internal_error (_("Unrecognized exec-file-mismatch setting: \"%s\""),
130 /* Whether to open exec and core files read-only or read-write. */
132 bool write_files
= false;
134 show_write_files (struct ui_file
*file
, int from_tty
,
135 struct cmd_list_element
*c
, const char *value
)
137 gdb_printf (file
, _("Writing into executable and core files is %s.\n"),
143 exec_target_open (const char *args
, int from_tty
)
145 target_preopen (from_tty
);
147 std::string filename
= extract_single_filename_arg (args
);
148 exec_file_attach (filename
.empty () ? nullptr : filename
.c_str (),
152 /* This is the target_close implementation. Clears all target
153 sections and closes all executable bfds from all program spaces. */
156 exec_target::close ()
158 for (struct program_space
*ss
: program_spaces
)
160 ss
->clear_target_sections ();
168 try_open_exec_file (const char *exec_file_host
, struct inferior
*inf
,
169 symfile_add_flags add_flags
)
171 struct gdb_exception prev_err
;
173 /* exec_file_attach and symbol_file_add_main may throw an error if the file
174 cannot be opened either locally or remotely.
176 This happens for example, when the file is first found in the local
177 sysroot (above), and then disappears (a TOCTOU race), or when it doesn't
178 exist in the target filesystem, or when the file does exist, but
181 Even without a symbol file, the remote-based debugging session should
182 continue normally instead of ending abruptly. Hence we catch thrown
183 errors/exceptions in the following code. */
186 /* We must do this step even if exec_file_host is NULL, so that
187 exec_file_attach will clear state. */
188 exec_file_attach (exec_file_host
, add_flags
& SYMFILE_VERBOSE
);
190 catch (gdb_exception_error
&err
)
192 if (err
.message
!= NULL
)
193 warning ("%s", err
.what ());
195 prev_err
= std::move (err
);
198 if (exec_file_host
!= NULL
)
202 symbol_file_add_main (exec_file_host
, add_flags
);
204 catch (const gdb_exception_error
&err
)
207 warning ("%s", err
.what ());
215 validate_exec_file (int from_tty
)
217 /* If user asked to ignore the mismatch, do nothing. */
218 if (exec_file_mismatch_mode
== exec_file_mismatch_off
)
221 /* If there's no current executable, then there's nothing to
222 validate against, so we're done. */
223 const char *current_exec_file
= current_program_space
->exec_filename ();
224 if (current_exec_file
== nullptr)
227 /* Try to determine a filename from the process itself. If we
228 cannot get an executable from the process, then no validation is
230 const char *pid_exec_file
231 = target_pid_to_exec_file (current_inferior ()->pid
);
232 if (pid_exec_file
== nullptr)
235 /* In case current_exec_file was changed, reopen_exec_file ensures an up
236 to date build_id (will do nothing if the file timestamp did not
237 change). If exec file changed, reopen_exec_file has allocated another
238 file name, so get_exec_file again. */
240 current_exec_file
= current_program_space
->exec_filename ();
242 /* Try validating via build-id, if available. This is the most reliable
244 const bfd_build_id
*exec_file_build_id
245 = build_id_bfd_get (current_program_space
->exec_bfd ());
246 bool build_id_mismatch
= false;
247 if (exec_file_build_id
!= nullptr)
249 /* Prepend the target prefix, to force gdb_bfd_open to open the
250 file on the remote file system (if indeed remote). */
251 std::string target_pid_exec_file
252 = std::string (TARGET_SYSROOT_PREFIX
) + pid_exec_file
;
254 gdb_bfd_ref_ptr
abfd (gdb_bfd_open (target_pid_exec_file
.c_str (),
255 gnutarget
, -1, false));
258 const bfd_build_id
*target_exec_file_build_id
259 = build_id_bfd_get (abfd
.get ());
261 if (target_exec_file_build_id
!= nullptr)
263 if (build_id_equal (exec_file_build_id
,
264 target_exec_file_build_id
))
270 build_id_mismatch
= true;
275 if (build_id_mismatch
)
277 std::string
exec_file_target (pid_exec_file
);
279 /* In case the exec file is not local, exec_file_target has to point at
280 the target file system. */
281 if (is_target_filename (current_exec_file
) && !target_filesystem_is_local ())
282 exec_file_target
= TARGET_SYSROOT_PREFIX
+ exec_file_target
;
285 (_("Build ID mismatch between current exec-file %ps\n"
286 "and automatically determined exec-file %ps\n"
287 "exec-file-mismatch handling is currently \"%s\""),
288 styled_string (file_name_style
.style (), current_exec_file
),
289 styled_string (file_name_style
.style (), exec_file_target
.c_str ()),
290 exec_file_mismatch_names
[exec_file_mismatch_mode
]);
291 if (exec_file_mismatch_mode
== exec_file_mismatch_ask
)
293 symfile_add_flags add_flags
= SYMFILE_MAINLINE
;
296 add_flags
|= SYMFILE_VERBOSE
;
297 add_flags
|= SYMFILE_ALWAYS_CONFIRM
;
301 symbol_file_add_main (exec_file_target
.c_str (), add_flags
);
302 exec_file_attach (exec_file_target
.c_str (), from_tty
);
304 catch (const gdb_exception_error
&err
)
306 warning (_("loading %ps %s"),
307 styled_string (file_name_style
.style (),
308 exec_file_target
.c_str ()),
309 err
.message
!= NULL
? err
.what () : "error");
318 exec_file_locate_attach (int pid
, int defer_bp_reset
, int from_tty
)
320 const char *exec_file_target
;
321 symfile_add_flags add_flags
= 0;
323 /* Do nothing if we already have an executable filename. */
324 if (current_program_space
->exec_filename () != nullptr)
327 /* Try to determine a filename from the process itself. */
328 exec_file_target
= target_pid_to_exec_file (pid
);
329 if (exec_file_target
== NULL
)
331 warning (_("No executable has been specified and target does not "
333 "determining executable automatically. "
334 "Try using the \"%ps\" command."),
335 styled_string (command_style
.style (), "file"));
339 gdb::unique_xmalloc_ptr
<char> exec_file_host
340 = exec_file_find (exec_file_target
, NULL
);
341 if (exec_file_host
== nullptr)
343 warning (_("No executable has been specified, and target executable "
344 "%ps could not be found. Try using the \"%ps\" command."),
345 styled_string (file_name_style
.style (), exec_file_target
),
346 styled_string (command_style
.style (), "file"));
351 add_flags
|= SYMFILE_DEFER_BP_RESET
;
354 add_flags
|= SYMFILE_VERBOSE
;
356 /* Attempt to open the exec file. */
357 try_open_exec_file (exec_file_host
.get (), current_inferior (), add_flags
);
360 /* Set FILENAME as the new exec file.
362 This function is intended to be behave essentially the same
363 as exec_file_command, except that the latter will detect when
364 a target is being debugged, and will ask the user whether it
365 should be shut down first. (If the answer is "no", then the
366 new file is ignored.)
368 This file is used by exec_file_command, to do the work of opening
369 and processing the exec file after any prompting has happened.
371 And, it is used by child_attach, when the attach command was
372 given a pid but not a exec pathname, and the attach command could
373 figure out the pathname from the pid. (In this case, we shouldn't
374 ask the user whether the current target should be shut down --
375 we're supplying the exec pathname late for good reason.) */
378 exec_file_attach (const char *filename
, int from_tty
)
380 /* First, acquire a reference to the exec_bfd. We release
381 this at the end of the function; but acquiring it now lets the
382 BFD cache return it if this call refers to the same file. */
383 gdb_bfd_ref_ptr exec_bfd_holder
384 = gdb_bfd_ref_ptr::new_reference (current_program_space
->exec_bfd ());
386 /* Remove any previous exec file. */
387 current_program_space
->exec_close ();
389 /* Now open and digest the file the user requested, if any. */
394 gdb_printf (_("No executable file now.\n"));
396 set_gdbarch_from_file (NULL
);
400 int load_via_target
= 0;
401 const char *scratch_pathname
, *canonical_pathname
;
405 if (is_target_filename (filename
))
407 if (target_filesystem_is_local ())
408 filename
+= strlen (TARGET_SYSROOT_PREFIX
);
413 gdb::unique_xmalloc_ptr
<char> canonical_storage
, scratch_storage
;
416 /* gdb_bfd_fopen does not support "target:" filenames. */
418 warning (_("writing into executable files is "
419 "not supported for %s sysroots"),
420 TARGET_SYSROOT_PREFIX
);
422 scratch_pathname
= filename
;
424 canonical_pathname
= scratch_pathname
;
428 scratch_chan
= openp (getenv ("PATH"), OPF_TRY_CWD_FIRST
,
429 filename
, write_files
?
430 O_RDWR
| O_BINARY
: O_RDONLY
| O_BINARY
,
432 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
433 if (scratch_chan
< 0)
435 int first_errno
= errno
;
436 char *exename
= (char *) alloca (strlen (filename
) + 5);
438 strcat (strcpy (exename
, filename
), ".exe");
439 scratch_chan
= openp (getenv ("PATH"), OPF_TRY_CWD_FIRST
,
440 exename
, write_files
?
442 : O_RDONLY
| O_BINARY
,
444 if (scratch_chan
< 0)
448 if (scratch_chan
< 0)
449 perror_with_name (filename
);
451 scratch_pathname
= scratch_storage
.get ();
453 /* gdb_bfd_open (and its variants) prefers canonicalized
454 pathname for better BFD caching. */
455 canonical_storage
= gdb_realpath (scratch_pathname
);
456 canonical_pathname
= canonical_storage
.get ();
459 gdb_bfd_ref_ptr temp
;
460 if (write_files
&& !load_via_target
)
461 temp
= gdb_bfd_fopen (canonical_pathname
, gnutarget
,
462 FOPEN_RUB
, scratch_chan
);
464 temp
= gdb_bfd_open (canonical_pathname
, gnutarget
, scratch_chan
);
465 current_program_space
->set_exec_bfd (std::move (temp
));
467 if (!current_program_space
->exec_bfd ())
469 error (_("\"%s\": could not open as an executable file: %s."),
470 scratch_pathname
, bfd_errmsg (bfd_get_error ()));
473 /* gdb_realpath_keepfile resolves symlinks on the local
474 filesystem and so cannot be used for "target:" files. */
475 gdb_assert (current_program_space
->exec_filename () == nullptr);
477 current_program_space
->set_exec_filename
479 (bfd_get_filename (current_program_space
->exec_bfd ())));
481 current_program_space
->set_exec_filename
482 (make_unique_xstrdup (gdb_realpath_keepfile
483 (scratch_pathname
).c_str ()));
485 if (!bfd_check_format_matches (current_program_space
->exec_bfd (),
486 bfd_object
, &matching
))
488 /* Make sure to close exec_bfd, or else "run" might try to use
490 current_program_space
->exec_close ();
491 error (_("\"%s\": not in executable format: %s"), scratch_pathname
,
492 gdb_bfd_errmsg (bfd_get_error (), matching
).c_str ());
495 std::vector
<target_section
> sections
496 = build_section_table (current_program_space
->exec_bfd ());
498 current_program_space
->ebfd_mtime
499 = gdb_bfd_get_mtime (current_program_space
->exec_bfd ());
503 set_gdbarch_from_file (current_program_space
->exec_bfd ());
505 /* Add the executable's sections to the current address spaces'
506 list of sections. This possibly pushes the exec_ops
508 current_program_space
->add_target_sections
509 (current_program_space
->ebfd
.get (), sections
);
512 /* Are are loading the same executable? */
513 bfd
*prev_bfd
= exec_bfd_holder
.get ();
514 bfd
*curr_bfd
= current_program_space
->exec_bfd ();
515 bool reload_p
= (((prev_bfd
!= nullptr) == (curr_bfd
!= nullptr))
516 && (prev_bfd
== nullptr
517 || (strcmp (bfd_get_filename (prev_bfd
),
518 bfd_get_filename (curr_bfd
)) == 0)));
520 gdb::observers::executable_changed
.notify (current_program_space
, reload_p
);
526 no_executable_specified_error ()
528 error (_("No executable file specified.\n\
529 Use the \"file\" or \"exec-file\" command."));
532 /* Process the first arg in ARGS as the new exec file.
534 Note that we have to explicitly ignore additional args, since we can
535 be called from file_command(), which also calls symbol_file_command()
536 which can take multiple args.
538 If ARGS is NULL, we just want to close the exec file. */
541 exec_file_command (const char *args
, int from_tty
)
543 if (from_tty
&& target_has_execution ()
544 && !query (_("A program is being debugged already.\n"
545 "Are you sure you want to change the file? ")))
546 error (_("File not changed."));
550 /* Scan through the args and pick up the first non option arg
553 gdb_argv
built_argv (args
);
554 char **argv
= built_argv
.get ();
556 for (; (*argv
!= NULL
) && (**argv
== '-'); argv
++)
560 error (_("No executable file name was specified"));
562 gdb::unique_xmalloc_ptr
<char> filename (tilde_expand (*argv
));
563 exec_file_attach (filename
.get (), from_tty
);
566 exec_file_attach (NULL
, from_tty
);
569 /* Set both the exec file and the symbol file, in one command.
570 What a novelty. Why did GDB go through four major releases before this
571 command was added? */
574 file_command (const char *arg
, int from_tty
)
576 /* FIXME, if we lose on reading the symbol file, we should revert
577 the exec file, but that's rough. */
578 exec_file_command (arg
, from_tty
);
579 symbol_file_command (arg
, from_tty
);
580 if (deprecated_file_changed_hook
)
581 deprecated_file_changed_hook (arg
);
585 /* Builds a section table, given args BFD, TABLE. */
587 std::vector
<target_section
>
588 build_section_table (struct bfd
*some_bfd
)
590 std::vector
<target_section
> table
;
592 for (asection
*asect
: gdb_bfd_sections (some_bfd
))
596 /* Check the section flags, but do not discard zero-length
597 sections, since some symbols may still be attached to this
598 section. For instance, we encountered on sparc-solaris 2.10
599 a shared library with an empty .bss section to which a symbol
600 named "_end" was attached. The address of this symbol still
601 needs to be relocated. */
602 aflag
= bfd_section_flags (asect
);
603 if (!(aflag
& SEC_ALLOC
))
606 table
.emplace_back (bfd_section_vma (asect
),
607 bfd_section_vma (asect
) + bfd_section_size (asect
),
614 /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
615 current set of target sections. */
618 program_space::add_target_sections
619 (target_section_owner owner
, const std::vector
<target_section
> §ions
)
621 if (!sections
.empty ())
623 for (const target_section
&s
: sections
)
625 m_target_sections
.push_back (s
);
626 m_target_sections
.back ().owner
= owner
;
629 scoped_restore_current_pspace_and_thread restore_pspace_thread
;
631 /* If these are the first file sections we can provide memory
632 from, push the file_stratum target. Must do this in all
633 inferiors sharing the program space. */
634 for (inferior
*inf
: all_inferiors ())
636 if (inf
->pspace
!= this)
639 if (inf
->target_is_pushed (&exec_ops
))
642 switch_to_inferior_no_thread (inf
);
643 inf
->push_target (&exec_ops
);
648 /* Add the sections of OBJFILE to the current set of target sections. */
651 program_space::add_target_sections (struct objfile
*objfile
)
653 gdb_assert (objfile
!= nullptr);
655 /* Compute the number of sections to add. */
656 for (obj_section
*osect
: objfile
->sections ())
658 if (bfd_section_size (osect
->the_bfd_section
) == 0)
661 m_target_sections
.emplace_back (osect
->addr (), osect
->endaddr (),
662 osect
->the_bfd_section
, objfile
);
666 /* Remove all target sections owned by OWNER.
667 OWNER must be the same value passed to add_target_sections. */
670 program_space::remove_target_sections (target_section_owner owner
)
672 gdb_assert (owner
.v () != nullptr);
674 auto it
= std::remove_if (m_target_sections
.begin (),
675 m_target_sections
.end (),
676 [&] (target_section
§
)
678 return sect
.owner
.v () == owner
.v ();
680 m_target_sections
.erase (it
, m_target_sections
.end ());
682 /* If we don't have any more sections to read memory from,
683 remove the file_stratum target from the stack of each
684 inferior sharing the program space. */
685 if (m_target_sections
.empty ())
687 scoped_restore_current_pspace_and_thread restore_pspace_thread
;
689 for (inferior
*inf
: all_inferiors ())
691 if (inf
->pspace
!= this)
694 switch_to_inferior_no_thread (inf
);
695 inf
->unpush_target (&exec_ops
);
703 exec_on_vfork (inferior
*vfork_child
)
705 if (!vfork_child
->pspace
->target_sections ().empty ())
706 vfork_child
->push_target (&exec_ops
);
711 enum target_xfer_status
712 exec_read_partial_read_only (gdb_byte
*readbuf
, ULONGEST offset
,
713 ULONGEST len
, ULONGEST
*xfered_len
)
715 /* It's unduly pedantic to refuse to look at the executable for
716 read-only pieces; so do the equivalent of readonly regions aka
718 if (current_program_space
->exec_bfd () != NULL
)
724 for (s
= current_program_space
->exec_bfd ()->sections
; s
; s
= s
->next
)
726 if ((s
->flags
& SEC_LOAD
) == 0
727 || (s
->flags
& SEC_READONLY
) == 0)
731 size
= bfd_section_size (s
);
732 if (vma
<= offset
&& offset
< (vma
+ size
))
736 amt
= (vma
+ size
) - offset
;
740 amt
= bfd_get_section_contents (current_program_space
->exec_bfd (), s
,
741 readbuf
, offset
- vma
, amt
);
744 return TARGET_XFER_EOF
;
748 return TARGET_XFER_OK
;
754 /* Indicate failure to find the requested memory block. */
755 return TARGET_XFER_E_IO
;
758 /* Return all read-only memory ranges found in the target section
759 table defined by SECTIONS and SECTIONS_END, starting at (and
760 intersected with) MEMADDR for LEN bytes. */
762 static std::vector
<mem_range
>
763 section_table_available_memory (CORE_ADDR memaddr
, ULONGEST len
,
764 const std::vector
<target_section
> §ions
)
766 std::vector
<mem_range
> memory
;
768 for (const target_section
&p
: sections
)
770 if ((bfd_section_flags (p
.the_bfd_section
) & SEC_READONLY
) == 0)
773 /* Copy the meta-data, adjusted. */
774 if (mem_ranges_overlap (p
.addr
, p
.endaddr
- p
.addr
, memaddr
, len
))
776 ULONGEST lo1
, hi1
, lo2
, hi2
;
784 CORE_ADDR start
= std::max (lo1
, lo2
);
785 int length
= std::min (hi1
, hi2
) - start
;
787 memory
.emplace_back (start
, length
);
794 enum target_xfer_status
795 section_table_read_available_memory (gdb_byte
*readbuf
, ULONGEST offset
,
796 ULONGEST len
, ULONGEST
*xfered_len
)
798 const std::vector
<target_section
> *table
799 = target_get_section_table (current_inferior ()->top_target ());
800 std::vector
<mem_range
> available_memory
801 = section_table_available_memory (offset
, len
, *table
);
803 normalize_mem_ranges (&available_memory
);
805 for (const mem_range
&r
: available_memory
)
807 if (mem_ranges_overlap (r
.start
, r
.length
, offset
, len
))
810 enum target_xfer_status status
;
812 /* Get the intersection window. */
813 end
= std::min
<CORE_ADDR
> (offset
+ len
, r
.start
+ r
.length
);
815 gdb_assert (end
- offset
<= len
);
817 if (offset
>= r
.start
)
818 status
= exec_read_partial_read_only (readbuf
, offset
,
823 *xfered_len
= r
.start
- offset
;
824 status
= TARGET_XFER_UNAVAILABLE
;
831 return TARGET_XFER_UNAVAILABLE
;
834 enum target_xfer_status
835 section_table_xfer_memory_partial (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
836 ULONGEST offset
, ULONGEST len
,
837 ULONGEST
*xfered_len
,
838 const std::vector
<target_section
> §ions
,
839 gdb::function_view
<bool
840 (const struct target_section
*)> match_cb
)
843 ULONGEST memaddr
= offset
;
844 ULONGEST memend
= memaddr
+ len
;
846 gdb_assert (len
!= 0);
848 for (const target_section
&p
: sections
)
850 struct bfd_section
*asect
= p
.the_bfd_section
;
851 bfd
*abfd
= asect
->owner
;
853 if (match_cb
!= nullptr && !match_cb (&p
))
854 continue; /* not the section we need. */
855 if (memaddr
>= p
.addr
)
857 if (memend
<= p
.endaddr
)
859 /* Entire transfer is within this section. */
861 res
= bfd_set_section_contents (abfd
, asect
,
862 writebuf
, memaddr
- p
.addr
,
865 res
= bfd_get_section_contents (abfd
, asect
,
866 readbuf
, memaddr
- p
.addr
,
872 return TARGET_XFER_OK
;
875 return TARGET_XFER_EOF
;
877 else if (memaddr
>= p
.endaddr
)
879 /* This section ends before the transfer starts. */
884 /* This section overlaps the transfer. Just do half. */
885 len
= p
.endaddr
- memaddr
;
887 res
= bfd_set_section_contents (abfd
, asect
,
888 writebuf
, memaddr
- p
.addr
,
891 res
= bfd_get_section_contents (abfd
, asect
,
892 readbuf
, memaddr
- p
.addr
,
897 return TARGET_XFER_OK
;
900 return TARGET_XFER_EOF
;
905 return TARGET_XFER_EOF
; /* We can't help. */
908 enum target_xfer_status
909 exec_target::xfer_partial (enum target_object object
,
910 const char *annex
, gdb_byte
*readbuf
,
911 const gdb_byte
*writebuf
,
912 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
914 const std::vector
<target_section
> *table
= target_get_section_table (this);
916 if (object
== TARGET_OBJECT_MEMORY
)
917 return section_table_xfer_memory_partial (readbuf
, writebuf
,
918 offset
, len
, xfered_len
,
921 return TARGET_XFER_E_IO
;
926 print_section_info (const std::vector
<target_section
> *t
, bfd
*abfd
)
928 struct gdbarch
*gdbarch
= gdbarch_from_bfd (abfd
);
929 /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */
930 int wid
= gdbarch_addr_bit (gdbarch
) <= 32 ? 8 : 16;
932 gdb_printf ("\t`%ps', ",
933 styled_string (file_name_style
.style (),
934 bfd_get_filename (abfd
)));
935 gdb_stdout
->wrap_here (8);
936 gdb_printf (_("file type %s.\n"), bfd_get_target (abfd
));
937 if (abfd
== current_program_space
->exec_bfd ())
939 /* gcc-3.4 does not like the initialization in
940 <p == t->sections_end>. */
941 bfd_vma displacement
= 0;
945 for (const target_section
&p
: *t
)
947 struct bfd_section
*psect
= p
.the_bfd_section
;
949 if ((bfd_section_flags (psect
) & (SEC_ALLOC
| SEC_LOAD
))
950 != (SEC_ALLOC
| SEC_LOAD
))
953 if (bfd_section_vma (psect
) <= abfd
->start_address
954 && abfd
->start_address
< (bfd_section_vma (psect
)
955 + bfd_section_size (psect
)))
957 displacement
= p
.addr
- bfd_section_vma (psect
);
963 warning (_("Cannot find section for the entry point of %ps."),
964 styled_string (file_name_style
.style (),
965 bfd_get_filename (abfd
)));
967 entry_point
= gdbarch_addr_bits_remove (gdbarch
,
968 bfd_get_start_address (abfd
)
970 gdb_printf (_("\tEntry point: %s\n"),
971 paddress (gdbarch
, entry_point
));
973 for (const target_section
&p
: *t
)
975 struct bfd_section
*psect
= p
.the_bfd_section
;
976 bfd
*pbfd
= psect
->owner
;
978 gdb_printf ("\t%s", hex_string_custom (p
.addr
, wid
));
979 gdb_printf (" - %s", hex_string_custom (p
.endaddr
, wid
));
981 /* FIXME: A format of "08l" is not wide enough for file offsets
982 larger than 4GB. OTOH, making it "016l" isn't desirable either
983 since most output will then be much wider than necessary. It
984 may make sense to test the size of the file and choose the
985 format string accordingly. */
986 /* FIXME: i18n: Need to rewrite this sentence. */
989 hex_string_custom (psect
->filepos
, 8));
990 gdb_printf (" is %s", bfd_section_name (psect
));
992 gdb_printf (" in %ps",
993 styled_string (file_name_style
.style (),
994 bfd_get_filename (pbfd
)));
1000 exec_target::files_info ()
1002 if (current_program_space
->exec_bfd ())
1003 print_section_info (¤t_program_space
->target_sections (),
1004 current_program_space
->exec_bfd ());
1006 gdb_puts (_("\t<no file loaded>\n"));
1010 set_section_command (const char *args
, int from_tty
)
1012 const char *secname
;
1015 error (_("Must specify section name and its virtual address"));
1017 /* Parse out section name. */
1018 for (secname
= args
; !isspace (*args
); args
++);
1019 unsigned seclen
= args
- secname
;
1021 /* Parse out new virtual address. */
1022 CORE_ADDR secaddr
= parse_and_eval_address (args
);
1024 for (target_section
&p
: current_program_space
->target_sections ())
1026 if (!strncmp (secname
, bfd_section_name (p
.the_bfd_section
), seclen
)
1027 && bfd_section_name (p
.the_bfd_section
)[seclen
] == '\0')
1029 long offset
= secaddr
- p
.addr
;
1031 p
.endaddr
+= offset
;
1033 exec_ops
.files_info ();
1038 std::string
secprint (secname
, seclen
);
1039 error (_("Section %s not found"), secprint
.c_str ());
1042 /* If we can find a section in FILENAME with BFD index INDEX, adjust
1046 exec_set_section_address (const char *filename
, int index
, CORE_ADDR address
)
1048 for (target_section
&p
: current_program_space
->target_sections ())
1050 if (filename_cmp (filename
,
1051 bfd_get_filename (p
.the_bfd_section
->owner
)) == 0
1052 && index
== p
.the_bfd_section
->index
)
1054 p
.endaddr
+= address
- p
.addr
;
1061 exec_target::has_memory ()
1063 /* We can provide memory if we have any file/target sections to read
1065 return !current_program_space
->target_sections ().empty ();
1068 gdb::unique_xmalloc_ptr
<char>
1069 exec_target::make_corefile_notes (bfd
*obfd
, int *note_size
)
1071 error (_("Can't create a corefile"));
1075 exec_target::find_memory_regions (find_memory_region_ftype func
, void *data
)
1077 return objfile_find_memory_regions (this, func
, data
);
1080 INIT_GDB_FILE (exec
)
1082 struct cmd_list_element
*c
;
1084 c
= add_cmd ("file", class_files
, file_command
, _("\
1085 Use FILE as program to be debugged.\n\
1086 It is read for its symbols, for getting the contents of pure memory,\n\
1087 and it is the program executed when you use the `run' command.\n\
1088 If FILE cannot be found as specified, your execution directory path\n\
1089 ($PATH) is searched for a command of that name.\n\
1090 No arg means to have no executable file and no symbols."), &cmdlist
);
1091 set_cmd_completer (c
, filename_maybe_quoted_completer
);
1093 c
= add_cmd ("exec-file", class_files
, exec_file_command
, _("\
1094 Use FILE as program for getting contents of pure memory.\n\
1095 If FILE cannot be found as specified, your execution directory path\n\
1096 is searched for a command of that name.\n\
1097 No arg means have no executable file."), &cmdlist
);
1098 set_cmd_completer (c
, filename_maybe_quoted_completer
);
1100 add_com ("section", class_files
, set_section_command
, _("\
1101 Change the base address of section SECTION of the exec file to ADDR.\n\
1102 This can be used if the exec file does not contain section addresses,\n\
1103 (such as in the a.out format), or when the addresses specified in the\n\
1104 file itself are wrong. Each section must be changed separately. The\n\
1105 ``info files'' command lists all the sections and their addresses."));
1107 add_setshow_boolean_cmd ("write", class_support
, &write_files
, _("\
1108 Set writing into executable and core files."), _("\
1109 Show writing into executable and core files."), NULL
,
1112 &setlist
, &showlist
);
1114 add_setshow_enum_cmd ("exec-file-mismatch", class_support
,
1115 exec_file_mismatch_names
,
1116 &exec_file_mismatch
,
1118 Set exec-file-mismatch handling (ask|warn|off)."),
1120 Show exec-file-mismatch handling (ask|warn|off)."),
1122 Specifies how to handle a mismatch between the current exec-file\n\
1123 loaded by GDB and the exec-file automatically determined when attaching\n\
1125 ask - warn the user and ask whether to load the determined exec-file.\n\
1126 warn - warn the user, but do not change the exec-file.\n\
1127 off - do not check for mismatch.\n\
1129 GDB detects a mismatch by comparing the build IDs of the files.\n\
1130 If the user confirms loading the determined exec-file, then its symbols\n\
1131 will be loaded as well."),
1132 set_exec_file_mismatch_command
,
1133 show_exec_file_mismatch_command
,
1134 &setlist
, &showlist
);
1136 add_target (exec_target_info
, exec_target_open
,
1137 filename_maybe_quoted_completer
);