Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / exec.c
CommitLineData
c906108c 1/* Work with executable files, for GDB.
4646aa9d 2
88b9d363 3 Copyright (C) 1988-2022 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "frame.h"
d55e5aa6 22#include "inferior.h"
4de283e4
TT
23#include "target.h"
24#include "gdbcmd.h"
c906108c 25#include "language.h"
4de283e4
TT
26#include "filenames.h"
27#include "symfile.h"
c906108c 28#include "objfiles.h"
4de283e4
TT
29#include "completer.h"
30#include "value.h"
31#include "exec.h"
76727919 32#include "observable.h"
4de283e4
TT
33#include "arch-utils.h"
34#include "gdbthread.h"
6c95b8df 35#include "progspace.h"
53af73bf 36#include "progspace-and-thread.h"
4de283e4
TT
37#include "gdb_bfd.h"
38#include "gcore.h"
39#include "source.h"
98c59b52 40#include "build-id.h"
4de283e4
TT
41
42#include <fcntl.h>
e0eac551 43#include "readline/tilde.h"
4de283e4
TT
44#include "gdbcore.h"
45
46#include <ctype.h>
47#include <sys/stat.h>
a9a5a3d1 48#include "solist.h"
4de283e4 49#include <algorithm>
268a13a5 50#include "gdbsupport/pathstuff.h"
a2fedca9 51#include "cli/cli-style.h"
c906108c 52
1d8b34a7 53void (*deprecated_file_changed_hook) (const char *);
c906108c 54
d9f719f1
PA
55static const target_info exec_target_info = {
56 "exec",
57 N_("Local exec file"),
58 N_("Use an executable file as a target.\n\
59Specify the filename of the executable file.")
60};
61
c906108c
SS
62/* The target vector for executable files. */
63
f6ac5f3d
PA
64struct exec_target final : public target_ops
65{
d9f719f1
PA
66 const target_info &info () const override
67 { return exec_target_info; }
f6ac5f3d 68
66b4deae
PA
69 strata stratum () const override { return file_stratum; }
70
f6ac5f3d
PA
71 void close () override;
72 enum target_xfer_status xfer_partial (enum target_object object,
73 const char *annex,
74 gdb_byte *readbuf,
75 const gdb_byte *writebuf,
76 ULONGEST offset, ULONGEST len,
77 ULONGEST *xfered_len) override;
f6ac5f3d
PA
78 void files_info () override;
79
57810aa7 80 bool has_memory () override;
24f5300a 81 gdb::unique_xmalloc_ptr<char> make_corefile_notes (bfd *, int *) override;
f6ac5f3d
PA
82 int find_memory_regions (find_memory_region_ftype func, void *data) override;
83};
84
85static exec_target exec_ops;
c906108c 86
a2fedca9
PW
87/* How to handle a mismatch between the current exec file and the exec
88 file determined from target. */
89
90static const char *const exec_file_mismatch_names[]
91 = {"ask", "warn", "off", NULL };
92enum exec_file_mismatch_mode
93 {
94 exec_file_mismatch_ask, exec_file_mismatch_warn, exec_file_mismatch_off
95 };
96static const char *exec_file_mismatch = exec_file_mismatch_names[0];
97static enum exec_file_mismatch_mode exec_file_mismatch_mode
98 = exec_file_mismatch_ask;
99
100/* Show command. */
101static void
102show_exec_file_mismatch_command (struct ui_file *file, int from_tty,
103 struct cmd_list_element *c, const char *value)
104{
105 fprintf_filtered (gdb_stdout,
106 _("exec-file-mismatch handling is currently \"%s\".\n"),
107 exec_file_mismatch_names[exec_file_mismatch_mode]);
108}
109
110/* Set command. Change the setting for range checking. */
111static void
112set_exec_file_mismatch_command (const char *ignore,
113 int from_tty, struct cmd_list_element *c)
114{
115 for (enum exec_file_mismatch_mode mode = exec_file_mismatch_ask;
116 ;
117 mode = static_cast<enum exec_file_mismatch_mode>(1 + (int) mode))
118 {
119 if (strcmp (exec_file_mismatch, exec_file_mismatch_names[mode]) == 0)
120 {
121 exec_file_mismatch_mode = mode;
122 return;
123 }
124 if (mode == exec_file_mismatch_off)
125 internal_error (__FILE__, __LINE__,
126 _("Unrecognized exec-file-mismatch setting: \"%s\""),
127 exec_file_mismatch);
128 }
129}
130
c906108c
SS
131/* Whether to open exec and core files read-only or read-write. */
132
491144b5 133bool write_files = false;
920d2a44
AC
134static void
135show_write_files (struct ui_file *file, int from_tty,
136 struct cmd_list_element *c, const char *value)
137{
138 fprintf_filtered (file, _("Writing into executable and core files is %s.\n"),
139 value);
140}
141
c906108c 142
d9f719f1
PA
143static void
144exec_target_open (const char *args, int from_tty)
1adeb98a
FN
145{
146 target_preopen (from_tty);
147 exec_file_attach (args, from_tty);
148}
149
6c95b8df
PA
150/* This is the target_close implementation. Clears all target
151 sections and closes all executable bfds from all program spaces. */
152
f6ac5f3d
PA
153void
154exec_target::close ()
c906108c 155{
94c93c35 156 for (struct program_space *ss : program_spaces)
5ed8105e 157 {
02f7d26b 158 ss->clear_target_sections ();
8a4f1402 159 ss->exec_close ();
5ed8105e 160 }
c906108c
SS
161}
162
f6ac5f3d 163/* See gdbcore.h. */
a10de604
GB
164
165void
ecf45d2c
SL
166try_open_exec_file (const char *exec_file_host, struct inferior *inf,
167 symfile_add_flags add_flags)
a10de604 168{
cc06b668 169 struct gdb_exception prev_err;
a10de604 170
57d1de9c
LM
171 /* exec_file_attach and symbol_file_add_main may throw an error if the file
172 cannot be opened either locally or remotely.
173
174 This happens for example, when the file is first found in the local
175 sysroot (above), and then disappears (a TOCTOU race), or when it doesn't
176 exist in the target filesystem, or when the file does exist, but
177 is not readable.
88178e82 178
57d1de9c
LM
179 Even without a symbol file, the remote-based debugging session should
180 continue normally instead of ending abruptly. Hence we catch thrown
181 errors/exceptions in the following code. */
a70b8144 182 try
57d1de9c 183 {
ecf45d2c
SL
184 /* We must do this step even if exec_file_host is NULL, so that
185 exec_file_attach will clear state. */
186 exec_file_attach (exec_file_host, add_flags & SYMFILE_VERBOSE);
57d1de9c 187 }
94aeb44b 188 catch (gdb_exception_error &err)
57d1de9c
LM
189 {
190 if (err.message != NULL)
3d6e9d23 191 warning ("%s", err.what ());
57d1de9c 192
94aeb44b 193 prev_err = std::move (err);
57d1de9c 194 }
57d1de9c 195
ecf45d2c 196 if (exec_file_host != NULL)
57d1de9c 197 {
a70b8144 198 try
ecf45d2c
SL
199 {
200 symbol_file_add_main (exec_file_host, add_flags);
201 }
230d2906 202 catch (const gdb_exception_error &err)
ecf45d2c
SL
203 {
204 if (!exception_print_same (prev_err, err))
3d6e9d23 205 warning ("%s", err.what ());
ecf45d2c 206 }
57d1de9c 207 }
ecf45d2c
SL
208}
209
210/* See gdbcore.h. */
211
a2fedca9
PW
212void
213validate_exec_file (int from_tty)
214{
215 /* If user asked to ignore the mismatch, do nothing. */
216 if (exec_file_mismatch_mode == exec_file_mismatch_off)
217 return;
218
219 const char *current_exec_file = get_exec_file (0);
220 struct inferior *inf = current_inferior ();
221 /* Try to determine a filename from the process itself. */
222 const char *pid_exec_file = target_pid_to_exec_file (inf->pid);
98c59b52 223 bool build_id_mismatch = false;
a2fedca9 224
98c59b52 225 /* If we cannot validate the exec file, return. */
a2fedca9
PW
226 if (current_exec_file == NULL || pid_exec_file == NULL)
227 return;
228
98c59b52
PA
229 /* Try validating via build-id, if available. This is the most
230 reliable check. */
48e9cc84
PW
231
232 /* In case current_exec_file was changed, reopen_exec_file ensures
233 an up to date build_id (will do nothing if the file timestamp
234 did not change). If exec file changed, reopen_exec_file has
235 allocated another file name, so get_exec_file again. */
236 reopen_exec_file ();
237 current_exec_file = get_exec_file (0);
238
7e10abd1
TT
239 const bfd_build_id *exec_file_build_id
240 = build_id_bfd_get (current_program_space->exec_bfd ());
98c59b52
PA
241 if (exec_file_build_id != nullptr)
242 {
243 /* Prepend the target prefix, to force gdb_bfd_open to open the
244 file on the remote file system (if indeed remote). */
245 std::string target_pid_exec_file
246 = std::string (TARGET_SYSROOT_PREFIX) + pid_exec_file;
247
248 gdb_bfd_ref_ptr abfd (gdb_bfd_open (target_pid_exec_file.c_str (),
249 gnutarget, -1, false));
250 if (abfd != nullptr)
251 {
252 const bfd_build_id *target_exec_file_build_id
253 = build_id_bfd_get (abfd.get ());
a2fedca9 254
98c59b52
PA
255 if (target_exec_file_build_id != nullptr)
256 {
257 if (exec_file_build_id->size == target_exec_file_build_id->size
258 && memcmp (exec_file_build_id->data,
259 target_exec_file_build_id->data,
260 exec_file_build_id->size) == 0)
261 {
262 /* Match. */
263 return;
264 }
265 else
266 build_id_mismatch = true;
267 }
268 }
269 }
a2fedca9 270
98c59b52 271 if (build_id_mismatch)
a2fedca9 272 {
98c59b52
PA
273 std::string exec_file_target (pid_exec_file);
274
275 /* In case the exec file is not local, exec_file_target has to point at
276 the target file system. */
277 if (is_target_filename (current_exec_file) && !target_filesystem_is_local ())
278 exec_file_target = TARGET_SYSROOT_PREFIX + exec_file_target;
279
a2fedca9 280 warning
0a278aa7 281 (_("Build ID mismatch between current exec-file %ps\n"
a2fedca9
PW
282 "and automatically determined exec-file %ps\n"
283 "exec-file-mismatch handling is currently \"%s\""),
284 styled_string (file_name_style.style (), current_exec_file),
285 styled_string (file_name_style.style (), exec_file_target.c_str ()),
286 exec_file_mismatch_names[exec_file_mismatch_mode]);
287 if (exec_file_mismatch_mode == exec_file_mismatch_ask)
288 {
289 symfile_add_flags add_flags = SYMFILE_MAINLINE;
290 if (from_tty)
a8654e7d
PW
291 {
292 add_flags |= SYMFILE_VERBOSE;
293 add_flags |= SYMFILE_ALWAYS_CONFIRM;
294 }
a2fedca9
PW
295 try
296 {
297 symbol_file_add_main (exec_file_target.c_str (), add_flags);
298 exec_file_attach (exec_file_target.c_str (), from_tty);
299 }
300 catch (gdb_exception_error &err)
301 {
302 warning (_("loading %ps %s"),
303 styled_string (file_name_style.style (),
304 exec_file_target.c_str ()),
305 err.message != NULL ? err.what () : "error");
306 }
307 }
308 }
309}
310
311/* See gdbcore.h. */
312
ecf45d2c
SL
313void
314exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty)
315{
797bc1cb 316 char *exec_file_target;
ecf45d2c
SL
317 symfile_add_flags add_flags = 0;
318
319 /* Do nothing if we already have an executable filename. */
320 if (get_exec_file (0) != NULL)
321 return;
322
323 /* Try to determine a filename from the process itself. */
324 exec_file_target = target_pid_to_exec_file (pid);
325 if (exec_file_target == NULL)
57d1de9c 326 {
ecf45d2c
SL
327 warning (_("No executable has been specified and target does not "
328 "support\n"
329 "determining executable automatically. "
330 "Try using the \"file\" command."));
331 return;
57d1de9c 332 }
88178e82 333
797bc1cb
TT
334 gdb::unique_xmalloc_ptr<char> exec_file_host
335 = exec_file_find (exec_file_target, NULL);
ecf45d2c
SL
336
337 if (defer_bp_reset)
338 add_flags |= SYMFILE_DEFER_BP_RESET;
339
340 if (from_tty)
341 add_flags |= SYMFILE_VERBOSE;
342
343 /* Attempt to open the exec file. */
797bc1cb 344 try_open_exec_file (exec_file_host.get (), current_inferior (), add_flags);
a10de604
GB
345}
346
907083d1 347/* Set FILENAME as the new exec file.
c906108c 348
c5aa993b
JM
349 This function is intended to be behave essentially the same
350 as exec_file_command, except that the latter will detect when
351 a target is being debugged, and will ask the user whether it
352 should be shut down first. (If the answer is "no", then the
353 new file is ignored.)
c906108c 354
c5aa993b
JM
355 This file is used by exec_file_command, to do the work of opening
356 and processing the exec file after any prompting has happened.
c906108c 357
c5aa993b
JM
358 And, it is used by child_attach, when the attach command was
359 given a pid but not a exec pathname, and the attach command could
360 figure out the pathname from the pid. (In this case, we shouldn't
361 ask the user whether the current target should be shut down --
907083d1 362 we're supplying the exec pathname late for good reason.) */
c906108c
SS
363
364void
5f08566b 365exec_file_attach (const char *filename, int from_tty)
c906108c 366{
7e10abd1 367 /* First, acquire a reference to the exec_bfd. We release
9b333ba3
TT
368 this at the end of the function; but acquiring it now lets the
369 BFD cache return it if this call refers to the same file. */
7e10abd1
TT
370 gdb_bfd_ref_ptr exec_bfd_holder
371 = gdb_bfd_ref_ptr::new_reference (current_program_space->exec_bfd ());
192b62ce 372
c906108c 373 /* Remove any previous exec file. */
8a4f1402 374 current_program_space->exec_close ();
c906108c
SS
375
376 /* Now open and digest the file the user requested, if any. */
377
1adeb98a
FN
378 if (!filename)
379 {
380 if (from_tty)
dda83cd7 381 printf_unfiltered (_("No executable file now.\n"));
7a107747
DJ
382
383 set_gdbarch_from_file (NULL);
1adeb98a
FN
384 }
385 else
c906108c 386 {
64c0b5de 387 int load_via_target = 0;
14278e1f 388 const char *scratch_pathname, *canonical_pathname;
c906108c 389 int scratch_chan;
d18b8b7a 390 char **matching;
c5aa993b 391
64c0b5de
GB
392 if (is_target_filename (filename))
393 {
394 if (target_filesystem_is_local ())
395 filename += strlen (TARGET_SYSROOT_PREFIX);
396 else
397 load_via_target = 1;
398 }
399
14278e1f 400 gdb::unique_xmalloc_ptr<char> canonical_storage, scratch_storage;
64c0b5de 401 if (load_via_target)
c5aa993b 402 {
64c0b5de
GB
403 /* gdb_bfd_fopen does not support "target:" filenames. */
404 if (write_files)
405 warning (_("writing into executable files is "
406 "not supported for %s sysroots"),
407 TARGET_SYSROOT_PREFIX);
408
14278e1f 409 scratch_pathname = filename;
64c0b5de 410 scratch_chan = -1;
64c0b5de 411 canonical_pathname = scratch_pathname;
c5aa993b 412 }
64c0b5de
GB
413 else
414 {
415 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
416 filename, write_files ?
417 O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
e0cc99a6 418 &scratch_storage);
64c0b5de
GB
419#if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
420 if (scratch_chan < 0)
421 {
96445f0b 422 int first_errno = errno;
0ae1c716 423 char *exename = (char *) alloca (strlen (filename) + 5);
64c0b5de
GB
424
425 strcat (strcpy (exename, filename), ".exe");
426 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
427 exename, write_files ?
428 O_RDWR | O_BINARY
429 : O_RDONLY | O_BINARY,
e0cc99a6 430 &scratch_storage);
96445f0b
HD
431 if (scratch_chan < 0)
432 errno = first_errno;
64c0b5de 433 }
c906108c 434#endif
64c0b5de
GB
435 if (scratch_chan < 0)
436 perror_with_name (filename);
a4453b7e 437
e0cc99a6 438 scratch_pathname = scratch_storage.get ();
a4453b7e 439
64c0b5de
GB
440 /* gdb_bfd_open (and its variants) prefers canonicalized
441 pathname for better BFD caching. */
14278e1f
TT
442 canonical_storage = gdb_realpath (scratch_pathname);
443 canonical_pathname = canonical_storage.get ();
64c0b5de 444 }
1f0c4988 445
192b62ce 446 gdb_bfd_ref_ptr temp;
64c0b5de 447 if (write_files && !load_via_target)
192b62ce
TT
448 temp = gdb_bfd_fopen (canonical_pathname, gnutarget,
449 FOPEN_RUB, scratch_chan);
1c00ec6b 450 else
192b62ce 451 temp = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
19f6550e 452 current_program_space->set_exec_bfd (std::move (temp));
c906108c 453
7e10abd1 454 if (!current_program_space->exec_bfd ())
9fe4a216 455 {
2840a186
TV
456 error (_("\"%s\": could not open as an executable file: %s."),
457 scratch_pathname, bfd_errmsg (bfd_get_error ()));
9fe4a216 458 }
c906108c 459
64c0b5de
GB
460 /* gdb_realpath_keepfile resolves symlinks on the local
461 filesystem and so cannot be used for "target:" files. */
c20cb686 462 gdb_assert (current_program_space->exec_filename == nullptr);
64c0b5de 463 if (load_via_target)
c20cb686 464 current_program_space->exec_filename
7e10abd1
TT
465 = (make_unique_xstrdup
466 (bfd_get_filename (current_program_space->exec_bfd ())));
64c0b5de 467 else
c20cb686
TT
468 current_program_space->exec_filename
469 = gdb_realpath_keepfile (scratch_pathname);
1f0c4988 470
7e10abd1
TT
471 if (!bfd_check_format_matches (current_program_space->exec_bfd (),
472 bfd_object, &matching))
c906108c
SS
473 {
474 /* Make sure to close exec_bfd, or else "run" might try to use
475 it. */
8a4f1402 476 current_program_space->exec_close ();
2840a186 477 error (_("\"%s\": not in executable format: %s"), scratch_pathname,
803c08d0 478 gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
c906108c
SS
479 }
480
7e10abd1
TT
481 target_section_table sections
482 = build_section_table (current_program_space->exec_bfd ());
c906108c 483
7e10abd1
TT
484 current_program_space->ebfd_mtime
485 = bfd_get_mtime (current_program_space->exec_bfd ());
c04ea773 486
c906108c
SS
487 validate_files ();
488
7e10abd1 489 set_gdbarch_from_file (current_program_space->exec_bfd ());
c906108c 490
07b82ea5 491 /* Add the executable's sections to the current address spaces'
6c95b8df
PA
492 list of sections. This possibly pushes the exec_ops
493 target. */
3769e227
TT
494 current_program_space->add_target_sections (&current_program_space->ebfd,
495 sections);
c906108c
SS
496
497 /* Tell display code (if any) about the changed file name. */
9a4105ab
AC
498 if (deprecated_exec_file_display_hook)
499 (*deprecated_exec_file_display_hook) (filename);
c906108c 500 }
9b333ba3 501
ce7d4522 502 bfd_cache_close_all ();
76727919 503 gdb::observers::executable_changed.notify ();
c906108c
SS
504}
505
506/* Process the first arg in ARGS as the new exec file.
507
c5aa993b
JM
508 Note that we have to explicitly ignore additional args, since we can
509 be called from file_command(), which also calls symbol_file_command()
1adeb98a
FN
510 which can take multiple args.
511
0963b4bd 512 If ARGS is NULL, we just want to close the exec file. */
c906108c 513
1adeb98a 514static void
1d8b34a7 515exec_file_command (const char *args, int from_tty)
c906108c 516{
55f6301a 517 if (from_tty && target_has_execution ()
4c42eaff
DJ
518 && !query (_("A program is being debugged already.\n"
519 "Are you sure you want to change the file? ")))
520 error (_("File not changed."));
1adeb98a
FN
521
522 if (args)
523 {
524 /* Scan through the args and pick up the first non option arg
dda83cd7 525 as the filename. */
1adeb98a 526
773a1edc
TT
527 gdb_argv built_argv (args);
528 char **argv = built_argv.get ();
1adeb98a
FN
529
530 for (; (*argv != NULL) && (**argv == '-'); argv++)
dda83cd7
SM
531 {;
532 }
1adeb98a 533 if (*argv == NULL)
dda83cd7 534 error (_("No executable file name was specified"));
1adeb98a 535
773a1edc
TT
536 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (*argv));
537 exec_file_attach (filename.get (), from_tty);
1adeb98a
FN
538 }
539 else
540 exec_file_attach (NULL, from_tty);
c906108c
SS
541}
542
0963b4bd 543/* Set both the exec file and the symbol file, in one command.
c906108c
SS
544 What a novelty. Why did GDB go through four major releases before this
545 command was added? */
546
547static void
1d8b34a7 548file_command (const char *arg, int from_tty)
c906108c
SS
549{
550 /* FIXME, if we lose on reading the symbol file, we should revert
551 the exec file, but that's rough. */
552 exec_file_command (arg, from_tty);
553 symbol_file_command (arg, from_tty);
9a4105ab
AC
554 if (deprecated_file_changed_hook)
555 deprecated_file_changed_hook (arg);
c906108c 556}
c906108c 557\f
c5aa993b 558
2d128614 559/* Builds a section table, given args BFD, TABLE. */
c906108c 560
2d128614
TT
561target_section_table
562build_section_table (struct bfd *some_bfd)
c906108c 563{
2d128614
TT
564 target_section_table table;
565
8a6bb1d1
TT
566 for (asection *asect : gdb_bfd_sections (some_bfd))
567 {
568 flagword aflag;
569
570 /* Check the section flags, but do not discard zero-length
571 sections, since some symbols may still be attached to this
572 section. For instance, we encountered on sparc-solaris 2.10
573 a shared library with an empty .bss section to which a symbol
574 named "_end" was attached. The address of this symbol still
575 needs to be relocated. */
576 aflag = bfd_section_flags (asect);
577 if (!(aflag & SEC_ALLOC))
578 continue;
579
6be2a9ab
TT
580 table.emplace_back (bfd_section_vma (asect),
581 bfd_section_vma (asect) + bfd_section_size (asect),
582 asect);
8a6bb1d1 583 }
e2ff18a0 584
2d128614 585 return table;
c906108c 586}
07b82ea5
PA
587
588/* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
589 current set of target sections. */
590
591void
3769e227
TT
592program_space::add_target_sections (void *owner,
593 const target_section_table &sections)
07b82ea5 594{
d7a78e5c 595 if (!sections.empty ())
07b82ea5 596 {
d7a78e5c 597 for (const target_section &s : sections)
ed9eebaf 598 {
02f7d26b
AB
599 m_target_sections.push_back (s);
600 m_target_sections.back ().owner = owner;
ed9eebaf 601 }
07b82ea5 602
53af73bf 603 scoped_restore_current_pspace_and_thread restore_pspace_thread;
5b6d1e4f 604
07b82ea5 605 /* If these are the first file sections we can provide memory
5b6d1e4f
PA
606 from, push the file_stratum target. Must do this in all
607 inferiors sharing the program space. */
608 for (inferior *inf : all_inferiors ())
609 {
3769e227 610 if (inf->pspace != this)
5b6d1e4f
PA
611 continue;
612
613 if (inf->target_is_pushed (&exec_ops))
614 continue;
615
616 switch_to_inferior_no_thread (inf);
02980c56 617 inf->push_target (&exec_ops);
5b6d1e4f 618 }
07b82ea5
PA
619 }
620}
621
76ad5e1e
NB
622/* Add the sections of OBJFILE to the current set of target sections. */
623
624void
d9eebde0 625program_space::add_target_sections (struct objfile *objfile)
76ad5e1e 626{
76ad5e1e 627 struct obj_section *osect;
76ad5e1e 628
91840ee3 629 gdb_assert (objfile != nullptr);
76ad5e1e
NB
630
631 /* Compute the number of sections to add. */
76ad5e1e
NB
632 ALL_OBJFILE_OSECTIONS (objfile, osect)
633 {
fd361982 634 if (bfd_section_size (osect->the_bfd_section) == 0)
76ad5e1e
NB
635 continue;
636
0c1bcd23 637 m_target_sections.emplace_back (osect->addr (), osect->endaddr (),
02f7d26b 638 osect->the_bfd_section, (void *) objfile);
76ad5e1e
NB
639 }
640}
641
046ac79f
JK
642/* Remove all target sections owned by OWNER.
643 OWNER must be the same value passed to add_target_sections. */
07b82ea5
PA
644
645void
2a3f84af 646program_space::remove_target_sections (void *owner)
07b82ea5 647{
046ac79f
JK
648 gdb_assert (owner != NULL);
649
02f7d26b
AB
650 auto it = std::remove_if (m_target_sections.begin (),
651 m_target_sections.end (),
bb2a6777
TT
652 [&] (target_section &sect)
653 {
654 return sect.owner == owner;
655 });
02f7d26b 656 m_target_sections.erase (it, m_target_sections.end ());
bb2a6777
TT
657
658 /* If we don't have any more sections to read memory from,
659 remove the file_stratum target from the stack of each
660 inferior sharing the program space. */
02f7d26b 661 if (m_target_sections.empty ())
07b82ea5 662 {
bb2a6777 663 scoped_restore_current_pspace_and_thread restore_pspace_thread;
07b82ea5 664
bb2a6777 665 for (inferior *inf : all_inferiors ())
6c95b8df 666 {
2a3f84af 667 if (inf->pspace != this)
bb2a6777 668 continue;
6c95b8df 669
bb2a6777 670 switch_to_inferior_no_thread (inf);
fadf6add 671 inf->unpush_target (&exec_ops);
6c95b8df 672 }
07b82ea5
PA
673 }
674}
675
5b6d1e4f
PA
676/* See exec.h. */
677
678void
679exec_on_vfork ()
680{
02f7d26b 681 if (!current_program_space->target_sections ().empty ())
02980c56 682 current_inferior ()->push_target (&exec_ops);
5b6d1e4f
PA
683}
684
c906108c 685\f
348f8c02 686
1ca49d37
YQ
687enum target_xfer_status
688exec_read_partial_read_only (gdb_byte *readbuf, ULONGEST offset,
689 ULONGEST len, ULONGEST *xfered_len)
690{
691 /* It's unduly pedantic to refuse to look at the executable for
692 read-only pieces; so do the equivalent of readonly regions aka
693 QTro packet. */
7e10abd1 694 if (current_program_space->exec_bfd () != NULL)
1ca49d37
YQ
695 {
696 asection *s;
697 bfd_size_type size;
698 bfd_vma vma;
699
7e10abd1 700 for (s = current_program_space->exec_bfd ()->sections; s; s = s->next)
1ca49d37
YQ
701 {
702 if ((s->flags & SEC_LOAD) == 0
703 || (s->flags & SEC_READONLY) == 0)
704 continue;
705
706 vma = s->vma;
fd361982 707 size = bfd_section_size (s);
1ca49d37
YQ
708 if (vma <= offset && offset < (vma + size))
709 {
710 ULONGEST amt;
711
712 amt = (vma + size) - offset;
713 if (amt > len)
714 amt = len;
715
7e10abd1 716 amt = bfd_get_section_contents (current_program_space->exec_bfd (), s,
1ca49d37
YQ
717 readbuf, offset - vma, amt);
718
719 if (amt == 0)
720 return TARGET_XFER_EOF;
721 else
722 {
723 *xfered_len = amt;
724 return TARGET_XFER_OK;
725 }
726 }
727 }
728 }
729
730 /* Indicate failure to find the requested memory block. */
731 return TARGET_XFER_E_IO;
732}
733
a79b1bc6 734/* Return all read-only memory ranges found in the target section
5a2eb0ef 735 table defined by SECTIONS and SECTIONS_END, starting at (and
a79b1bc6 736 intersected with) MEMADDR for LEN bytes. */
5a2eb0ef 737
a79b1bc6
SM
738static std::vector<mem_range>
739section_table_available_memory (CORE_ADDR memaddr, ULONGEST len,
bb2a6777 740 const target_section_table &sections)
e6ca34fc 741{
a79b1bc6 742 std::vector<mem_range> memory;
e6ca34fc 743
d7a78e5c 744 for (const target_section &p : sections)
e6ca34fc 745 {
bb2a6777 746 if ((bfd_section_flags (p.the_bfd_section) & SEC_READONLY) == 0)
e6ca34fc
PA
747 continue;
748
749 /* Copy the meta-data, adjusted. */
bb2a6777 750 if (mem_ranges_overlap (p.addr, p.endaddr - p.addr, memaddr, len))
e6ca34fc
PA
751 {
752 ULONGEST lo1, hi1, lo2, hi2;
e6ca34fc
PA
753
754 lo1 = memaddr;
755 hi1 = memaddr + len;
756
bb2a6777
TT
757 lo2 = p.addr;
758 hi2 = p.endaddr;
e6ca34fc 759
a79b1bc6
SM
760 CORE_ADDR start = std::max (lo1, lo2);
761 int length = std::min (hi1, hi2) - start;
e6ca34fc 762
a79b1bc6 763 memory.emplace_back (start, length);
e6ca34fc
PA
764 }
765 }
766
767 return memory;
768}
769
1ee79381
YQ
770enum target_xfer_status
771section_table_read_available_memory (gdb_byte *readbuf, ULONGEST offset,
772 ULONGEST len, ULONGEST *xfered_len)
773{
336aa7b7 774 const target_section_table *table
328d42d8 775 = target_get_section_table (current_inferior ()->top_target ());
a79b1bc6 776 std::vector<mem_range> available_memory
bb2a6777 777 = section_table_available_memory (offset, len, *table);
1ee79381 778
a79b1bc6 779 normalize_mem_ranges (&available_memory);
1ee79381 780
a79b1bc6 781 for (const mem_range &r : available_memory)
1ee79381 782 {
a79b1bc6 783 if (mem_ranges_overlap (r.start, r.length, offset, len))
1ee79381
YQ
784 {
785 CORE_ADDR end;
786 enum target_xfer_status status;
787
788 /* Get the intersection window. */
a79b1bc6 789 end = std::min<CORE_ADDR> (offset + len, r.start + r.length);
1ee79381
YQ
790
791 gdb_assert (end - offset <= len);
792
a79b1bc6 793 if (offset >= r.start)
1ee79381
YQ
794 status = exec_read_partial_read_only (readbuf, offset,
795 end - offset,
796 xfered_len);
797 else
798 {
a79b1bc6 799 *xfered_len = r.start - offset;
bc113b4e 800 status = TARGET_XFER_UNAVAILABLE;
1ee79381 801 }
1ee79381
YQ
802 return status;
803 }
804 }
1ee79381
YQ
805
806 *xfered_len = len;
bc113b4e 807 return TARGET_XFER_UNAVAILABLE;
1ee79381
YQ
808}
809
9b409511 810enum target_xfer_status
07b82ea5 811section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
b55e14c7 812 ULONGEST offset, ULONGEST len,
9b409511 813 ULONGEST *xfered_len,
bb2a6777 814 const target_section_table &sections,
e56cb451
KB
815 gdb::function_view<bool
816 (const struct target_section *)> match_cb)
c906108c 817{
020cc13c 818 int res;
07b82ea5
PA
819 ULONGEST memaddr = offset;
820 ULONGEST memend = memaddr + len;
c906108c 821
e2ff18a0 822 gdb_assert (len != 0);
c906108c 823
d7a78e5c 824 for (const target_section &p : sections)
c906108c 825 {
bb2a6777 826 struct bfd_section *asect = p.the_bfd_section;
2b2848e2
DE
827 bfd *abfd = asect->owner;
828
bb2a6777 829 if (match_cb != nullptr && !match_cb (&p))
0963b4bd 830 continue; /* not the section we need. */
bb2a6777 831 if (memaddr >= p.addr)
dda83cd7 832 {
bb2a6777 833 if (memend <= p.endaddr)
3db26b01
JB
834 {
835 /* Entire transfer is within this section. */
07b82ea5 836 if (writebuf)
2b2848e2 837 res = bfd_set_section_contents (abfd, asect,
bb2a6777 838 writebuf, memaddr - p.addr,
85302095
AC
839 len);
840 else
2b2848e2 841 res = bfd_get_section_contents (abfd, asect,
bb2a6777 842 readbuf, memaddr - p.addr,
85302095 843 len);
9b409511
YQ
844
845 if (res != 0)
846 {
847 *xfered_len = len;
848 return TARGET_XFER_OK;
849 }
850 else
851 return TARGET_XFER_EOF;
3db26b01 852 }
bb2a6777 853 else if (memaddr >= p.endaddr)
3db26b01
JB
854 {
855 /* This section ends before the transfer starts. */
856 continue;
857 }
858 else
859 {
860 /* This section overlaps the transfer. Just do half. */
bb2a6777 861 len = p.endaddr - memaddr;
07b82ea5 862 if (writebuf)
2b2848e2 863 res = bfd_set_section_contents (abfd, asect,
bb2a6777 864 writebuf, memaddr - p.addr,
85302095
AC
865 len);
866 else
2b2848e2 867 res = bfd_get_section_contents (abfd, asect,
bb2a6777 868 readbuf, memaddr - p.addr,
85302095 869 len);
9b409511
YQ
870 if (res != 0)
871 {
872 *xfered_len = len;
873 return TARGET_XFER_OK;
874 }
875 else
876 return TARGET_XFER_EOF;
3db26b01 877 }
dda83cd7 878 }
c906108c
SS
879 }
880
9b409511 881 return TARGET_XFER_EOF; /* We can't help. */
c906108c 882}
348f8c02 883
f6ac5f3d
PA
884enum target_xfer_status
885exec_target::xfer_partial (enum target_object object,
886 const char *annex, gdb_byte *readbuf,
887 const gdb_byte *writebuf,
888 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
348f8c02 889{
19cf757a 890 const target_section_table *table = target_get_section_table (this);
07b82ea5
PA
891
892 if (object == TARGET_OBJECT_MEMORY)
893 return section_table_xfer_memory_partial (readbuf, writebuf,
9b409511 894 offset, len, xfered_len,
bb2a6777 895 *table);
07b82ea5 896 else
2ed4b548 897 return TARGET_XFER_E_IO;
348f8c02 898}
c906108c 899\f
c5aa993b 900
c906108c 901void
19cf757a 902print_section_info (const target_section_table *t, bfd *abfd)
c906108c 903{
5af949e3 904 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
17a912b6 905 /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */
5af949e3 906 int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
c906108c 907
a2fedca9
PW
908 printf_filtered ("\t`%ps', ",
909 styled_string (file_name_style.style (),
910 bfd_get_filename (abfd)));
c906108c 911 wrap_here (" ");
a3f17187 912 printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
7e10abd1 913 if (abfd == current_program_space->exec_bfd ())
51bee8e9 914 {
3e43a32a
MS
915 /* gcc-3.4 does not like the initialization in
916 <p == t->sections_end>. */
d904de5b 917 bfd_vma displacement = 0;
2f1bdd26 918 bfd_vma entry_point;
bb2a6777 919 bool found = false;
51bee8e9 920
d7a78e5c 921 for (const target_section &p : *t)
51bee8e9 922 {
bb2a6777 923 struct bfd_section *psect = p.the_bfd_section;
51bee8e9 924
fd361982 925 if ((bfd_section_flags (psect) & (SEC_ALLOC | SEC_LOAD))
51bee8e9
JK
926 != (SEC_ALLOC | SEC_LOAD))
927 continue;
928
fd361982
AM
929 if (bfd_section_vma (psect) <= abfd->start_address
930 && abfd->start_address < (bfd_section_vma (psect)
931 + bfd_section_size (psect)))
51bee8e9 932 {
bb2a6777
TT
933 displacement = p.addr - bfd_section_vma (psect);
934 found = true;
51bee8e9
JK
935 break;
936 }
937 }
bb2a6777 938 if (!found)
a2fedca9
PW
939 warning (_("Cannot find section for the entry point of %ps."),
940 styled_string (file_name_style.style (),
941 bfd_get_filename (abfd)));
51bee8e9 942
2f1bdd26
MGD
943 entry_point = gdbarch_addr_bits_remove (gdbarch,
944 bfd_get_start_address (abfd)
945 + displacement);
51bee8e9 946 printf_filtered (_("\tEntry point: %s\n"),
2f1bdd26 947 paddress (gdbarch, entry_point));
51bee8e9 948 }
d7a78e5c 949 for (const target_section &p : *t)
c906108c 950 {
bb2a6777 951 struct bfd_section *psect = p.the_bfd_section;
2b2848e2
DE
952 bfd *pbfd = psect->owner;
953
bb2a6777
TT
954 printf_filtered ("\t%s", hex_string_custom (p.addr, wid));
955 printf_filtered (" - %s", hex_string_custom (p.endaddr, wid));
bcf16802
KB
956
957 /* FIXME: A format of "08l" is not wide enough for file offsets
958 larger than 4GB. OTOH, making it "016l" isn't desirable either
959 since most output will then be much wider than necessary. It
960 may make sense to test the size of the file and choose the
961 format string accordingly. */
a3f17187 962 /* FIXME: i18n: Need to rewrite this sentence. */
c906108c
SS
963 if (info_verbose)
964 printf_filtered (" @ %s",
2b2848e2 965 hex_string_custom (psect->filepos, 8));
fd361982 966 printf_filtered (" is %s", bfd_section_name (psect));
2b2848e2 967 if (pbfd != abfd)
a2fedca9
PW
968 printf_filtered (" in %ps",
969 styled_string (file_name_style.style (),
970 bfd_get_filename (pbfd)));
c906108c
SS
971 printf_filtered ("\n");
972 }
973}
974
f6ac5f3d
PA
975void
976exec_target::files_info ()
c906108c 977{
7e10abd1 978 if (current_program_space->exec_bfd ())
02f7d26b 979 print_section_info (&current_program_space->target_sections (),
7e10abd1 980 current_program_space->exec_bfd ());
57008375
JK
981 else
982 puts_filtered (_("\t<no file loaded>\n"));
c906108c
SS
983}
984
985static void
0b39b52e 986set_section_command (const char *args, int from_tty)
c906108c 987{
0b39b52e 988 const char *secname;
c906108c
SS
989
990 if (args == 0)
8a3fe4f8 991 error (_("Must specify section name and its virtual address"));
c906108c 992
0963b4bd 993 /* Parse out section name. */
c5aa993b 994 for (secname = args; !isspace (*args); args++);
dd80d750 995 unsigned seclen = args - secname;
c906108c 996
0963b4bd 997 /* Parse out new virtual address. */
dd80d750 998 CORE_ADDR secaddr = parse_and_eval_address (args);
c906108c 999
02f7d26b 1000 for (target_section &p : current_program_space->target_sections ())
c5aa993b 1001 {
bb2a6777
TT
1002 if (!strncmp (secname, bfd_section_name (p.the_bfd_section), seclen)
1003 && bfd_section_name (p.the_bfd_section)[seclen] == '\0')
c5aa993b 1004 {
dd80d750 1005 long offset = secaddr - p.addr;
bb2a6777
TT
1006 p.addr += offset;
1007 p.endaddr += offset;
c5aa993b 1008 if (from_tty)
f6ac5f3d 1009 exec_ops.files_info ();
c5aa993b
JM
1010 return;
1011 }
c906108c 1012 }
dd80d750
AB
1013
1014 std::string secprint (secname, seclen);
1015 error (_("Section %s not found"), secprint.c_str ());
c906108c
SS
1016}
1017
30510692
DJ
1018/* If we can find a section in FILENAME with BFD index INDEX, adjust
1019 it to ADDRESS. */
c1bd25fd
DJ
1020
1021void
1022exec_set_section_address (const char *filename, int index, CORE_ADDR address)
1023{
02f7d26b 1024 for (target_section &p : current_program_space->target_sections ())
c1bd25fd 1025 {
c7e97679 1026 if (filename_cmp (filename,
bb2a6777
TT
1027 bfd_get_filename (p.the_bfd_section->owner)) == 0
1028 && index == p.the_bfd_section->index)
c1bd25fd 1029 {
bb2a6777
TT
1030 p.endaddr += address - p.addr;
1031 p.addr = address;
c1bd25fd
DJ
1032 }
1033 }
1034}
1035
57810aa7 1036bool
f6ac5f3d 1037exec_target::has_memory ()
c35b1492
PA
1038{
1039 /* We can provide memory if we have any file/target sections to read
1040 from. */
02f7d26b 1041 return !current_program_space->target_sections ().empty ();
c35b1492
PA
1042}
1043
24f5300a 1044gdb::unique_xmalloc_ptr<char>
f6ac5f3d 1045exec_target::make_corefile_notes (bfd *obfd, int *note_size)
83814951
TT
1046{
1047 error (_("Can't create a corefile"));
1048}
be4d1333 1049
f6ac5f3d
PA
1050int
1051exec_target::find_memory_regions (find_memory_region_ftype func, void *data)
c906108c 1052{
f6ac5f3d 1053 return objfile_find_memory_regions (this, func, data);
c906108c
SS
1054}
1055
6c265988 1056void _initialize_exec ();
c906108c 1057void
6c265988 1058_initialize_exec ()
c906108c
SS
1059{
1060 struct cmd_list_element *c;
1061
c906108c
SS
1062 if (!dbx_commands)
1063 {
1a966eab
AC
1064 c = add_cmd ("file", class_files, file_command, _("\
1065Use FILE as program to be debugged.\n\
c906108c
SS
1066It is read for its symbols, for getting the contents of pure memory,\n\
1067and it is the program executed when you use the `run' command.\n\
1068If FILE cannot be found as specified, your execution directory path\n\
1069($PATH) is searched for a command of that name.\n\
1a966eab 1070No arg means to have no executable file and no symbols."), &cmdlist);
5ba2abeb 1071 set_cmd_completer (c, filename_completer);
c906108c
SS
1072 }
1073
1a966eab
AC
1074 c = add_cmd ("exec-file", class_files, exec_file_command, _("\
1075Use FILE as program for getting contents of pure memory.\n\
c906108c
SS
1076If FILE cannot be found as specified, your execution directory path\n\
1077is searched for a command of that name.\n\
1a966eab 1078No arg means have no executable file."), &cmdlist);
5ba2abeb 1079 set_cmd_completer (c, filename_completer);
c906108c 1080
1bedd215
AC
1081 add_com ("section", class_files, set_section_command, _("\
1082Change the base address of section SECTION of the exec file to ADDR.\n\
c906108c
SS
1083This can be used if the exec file does not contain section addresses,\n\
1084(such as in the a.out format), or when the addresses specified in the\n\
1085file itself are wrong. Each section must be changed separately. The\n\
1bedd215 1086``info files'' command lists all the sections and their addresses."));
c906108c 1087
5bf193a2
AC
1088 add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
1089Set writing into executable and core files."), _("\
1090Show writing into executable and core files."), NULL,
1091 NULL,
920d2a44 1092 show_write_files,
5bf193a2 1093 &setlist, &showlist);
c5aa993b 1094
a2fedca9
PW
1095 add_setshow_enum_cmd ("exec-file-mismatch", class_support,
1096 exec_file_mismatch_names,
1097 &exec_file_mismatch,
1098 _("\
1099Set exec-file-mismatch handling (ask|warn|off)."),
1100 _("\
1101Show exec-file-mismatch handling (ask|warn|off)."),
1102 _("\
98c59b52
PA
1103Specifies how to handle a mismatch between the current exec-file\n\
1104loaded by GDB and the exec-file automatically determined when attaching\n\
a2fedca9
PW
1105to a process:\n\n\
1106 ask - warn the user and ask whether to load the determined exec-file.\n\
1107 warn - warn the user, but do not change the exec-file.\n\
0a278aa7
PW
1108 off - do not check for mismatch.\n\
1109\n\
1110GDB detects a mismatch by comparing the build IDs of the files.\n\
1111If the user confirms loading the determined exec-file, then its symbols\n\
1112will be loaded as well."),
a2fedca9
PW
1113 set_exec_file_mismatch_command,
1114 show_exec_file_mismatch_command,
1115 &setlist, &showlist);
1116
d9f719f1 1117 add_target (exec_target_info, exec_target_open, filename_completer);
c906108c 1118}
This page took 1.61854 seconds and 4 git commands to generate.