]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/exec.c
Convert struct target_ops to C++
[thirdparty/binutils-gdb.git] / gdb / exec.c
CommitLineData
c906108c 1/* Work with executable files, for GDB.
4646aa9d 2
e2882c85 3 Copyright (C) 1988-2018 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"
22#include "inferior.h"
23#include "target.h"
24#include "gdbcmd.h"
25#include "language.h"
0ba1096a 26#include "filenames.h"
c906108c
SS
27#include "symfile.h"
28#include "objfiles.h"
c5f0f3d0 29#include "completer.h"
fd0407d6 30#include "value.h"
4646aa9d 31#include "exec.h"
76727919 32#include "observable.h"
dacec2a8 33#include "arch-utils.h"
6c95b8df
PA
34#include "gdbthread.h"
35#include "progspace.h"
cbb099e8 36#include "gdb_bfd.h"
b427c1bc 37#include "gcore.h"
b46a8d7c 38#include "source.h"
c906108c 39
c906108c 40#include <fcntl.h>
dbda9972 41#include "readline/readline.h"
c906108c
SS
42#include "gdbcore.h"
43
44#include <ctype.h>
53ce3c39 45#include <sys/stat.h>
a9a5a3d1 46#include "solist.h"
325fac50 47#include <algorithm>
b4987c95 48#include "common/pathstuff.h"
c906108c 49
1d8b34a7 50void (*deprecated_file_changed_hook) (const char *);
c906108c 51
c906108c
SS
52/* The target vector for executable files. */
53
f6ac5f3d
PA
54struct exec_target final : public target_ops
55{
56 exec_target ()
57 { to_stratum = file_stratum; }
58
59 const char *shortname () override
60 { return "exec"; }
61
62 const char *longname () override
63 { return _("Local exec file"); }
64
65 const char *doc () override
66 {
67 return _("\
68Use an executable file as a target.\n\
69Specify the filename of the executable file.");
70 }
71
72 void open (const char *, int) override;
73 void close () override;
74 enum target_xfer_status xfer_partial (enum target_object object,
75 const char *annex,
76 gdb_byte *readbuf,
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;
82
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;
86};
87
88static exec_target exec_ops;
c906108c 89
c906108c
SS
90/* Whether to open exec and core files read-only or read-write. */
91
92int write_files = 0;
920d2a44
AC
93static void
94show_write_files (struct ui_file *file, int from_tty,
95 struct cmd_list_element *c, const char *value)
96{
97 fprintf_filtered (file, _("Writing into executable and core files is %s.\n"),
98 value);
99}
100
c906108c 101
f6ac5f3d
PA
102void
103exec_target::open (const char *args, int from_tty)
1adeb98a
FN
104{
105 target_preopen (from_tty);
106 exec_file_attach (args, from_tty);
107}
108
07b82ea5
PA
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. */
111
6c95b8df
PA
112void
113exec_close (void)
07b82ea5
PA
114{
115 if (exec_bfd)
116 {
117 bfd *abfd = exec_bfd;
07b82ea5 118
cbb099e8 119 gdb_bfd_unref (abfd);
07b82ea5
PA
120
121 /* Removing target sections may close the exec_ops target.
122 Clear exec_bfd before doing so to prevent recursion. */
123 exec_bfd = NULL;
124 exec_bfd_mtime = 0;
125
046ac79f 126 remove_target_sections (&exec_bfd);
1f0c4988
JK
127
128 xfree (exec_filename);
129 exec_filename = NULL;
07b82ea5
PA
130 }
131}
132
6c95b8df
PA
133/* This is the target_close implementation. Clears all target
134 sections and closes all executable bfds from all program spaces. */
135
f6ac5f3d
PA
136void
137exec_target::close ()
c906108c 138{
ab16fce8 139 struct program_space *ss;
5ed8105e 140 scoped_restore_current_program_space restore_pspace;
6c95b8df 141
ab16fce8 142 ALL_PSPACES (ss)
5ed8105e
PA
143 {
144 set_current_program_space (ss);
145 clear_section_table (current_target_sections);
146 exec_close ();
147 }
c906108c
SS
148}
149
f6ac5f3d 150/* See gdbcore.h. */
a10de604
GB
151
152void
ecf45d2c
SL
153try_open_exec_file (const char *exec_file_host, struct inferior *inf,
154 symfile_add_flags add_flags)
a10de604 155{
88178e82 156 struct cleanup *old_chain;
57d1de9c 157 struct gdb_exception prev_err = exception_none;
a10de604 158
ecf45d2c 159 old_chain = make_cleanup (free_current_contents, &prev_err.message);
57d1de9c
LM
160
161 /* exec_file_attach and symbol_file_add_main may throw an error if the file
162 cannot be opened either locally or remotely.
163
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
167 is not readable.
88178e82 168
57d1de9c
LM
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. */
172 TRY
173 {
ecf45d2c
SL
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);
57d1de9c
LM
177 }
178 CATCH (err, RETURN_MASK_ERROR)
179 {
180 if (err.message != NULL)
181 warning ("%s", err.message);
182
183 prev_err = err;
184
185 /* Save message so it doesn't get trashed by the catch below. */
b5e1db87
LM
186 if (err.message != NULL)
187 prev_err.message = xstrdup (err.message);
57d1de9c
LM
188 }
189 END_CATCH
190
ecf45d2c 191 if (exec_file_host != NULL)
57d1de9c 192 {
ecf45d2c
SL
193 TRY
194 {
195 symbol_file_add_main (exec_file_host, add_flags);
196 }
197 CATCH (err, RETURN_MASK_ERROR)
198 {
199 if (!exception_print_same (prev_err, err))
200 warning ("%s", err.message);
201 }
202 END_CATCH
57d1de9c 203 }
ecf45d2c
SL
204
205 do_cleanups (old_chain);
206}
207
208/* See gdbcore.h. */
209
210void
211exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty)
212{
797bc1cb 213 char *exec_file_target;
ecf45d2c
SL
214 symfile_add_flags add_flags = 0;
215
216 /* Do nothing if we already have an executable filename. */
217 if (get_exec_file (0) != NULL)
218 return;
219
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)
57d1de9c 223 {
ecf45d2c
SL
224 warning (_("No executable has been specified and target does not "
225 "support\n"
226 "determining executable automatically. "
227 "Try using the \"file\" command."));
228 return;
57d1de9c 229 }
88178e82 230
797bc1cb
TT
231 gdb::unique_xmalloc_ptr<char> exec_file_host
232 = exec_file_find (exec_file_target, NULL);
ecf45d2c
SL
233
234 if (defer_bp_reset)
235 add_flags |= SYMFILE_DEFER_BP_RESET;
236
237 if (from_tty)
238 add_flags |= SYMFILE_VERBOSE;
239
240 /* Attempt to open the exec file. */
797bc1cb 241 try_open_exec_file (exec_file_host.get (), current_inferior (), add_flags);
a10de604
GB
242}
243
907083d1 244/* Set FILENAME as the new exec file.
c906108c 245
c5aa993b
JM
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.)
c906108c 251
c5aa993b
JM
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.
c906108c 254
c5aa993b
JM
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 --
907083d1 259 we're supplying the exec pathname late for good reason.) */
c906108c
SS
260
261void
5f08566b 262exec_file_attach (const char *filename, int from_tty)
c906108c 263{
9b333ba3
TT
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. */
1831a9f9 267 gdb_bfd_ref_ptr exec_bfd_holder = gdb_bfd_ref_ptr::new_reference (exec_bfd);
192b62ce 268
c906108c 269 /* Remove any previous exec file. */
6c95b8df 270 exec_close ();
c906108c
SS
271
272 /* Now open and digest the file the user requested, if any. */
273
1adeb98a
FN
274 if (!filename)
275 {
276 if (from_tty)
a3f17187 277 printf_unfiltered (_("No executable file now.\n"));
7a107747
DJ
278
279 set_gdbarch_from_file (NULL);
1adeb98a
FN
280 }
281 else
c906108c 282 {
64c0b5de 283 int load_via_target = 0;
14278e1f 284 const char *scratch_pathname, *canonical_pathname;
c906108c 285 int scratch_chan;
07b82ea5 286 struct target_section *sections = NULL, *sections_end = NULL;
d18b8b7a 287 char **matching;
c5aa993b 288
64c0b5de
GB
289 if (is_target_filename (filename))
290 {
291 if (target_filesystem_is_local ())
292 filename += strlen (TARGET_SYSROOT_PREFIX);
293 else
294 load_via_target = 1;
295 }
296
14278e1f 297 gdb::unique_xmalloc_ptr<char> canonical_storage, scratch_storage;
64c0b5de 298 if (load_via_target)
c5aa993b 299 {
64c0b5de
GB
300 /* gdb_bfd_fopen does not support "target:" filenames. */
301 if (write_files)
302 warning (_("writing into executable files is "
303 "not supported for %s sysroots"),
304 TARGET_SYSROOT_PREFIX);
305
14278e1f 306 scratch_pathname = filename;
64c0b5de 307 scratch_chan = -1;
64c0b5de 308 canonical_pathname = scratch_pathname;
c5aa993b 309 }
64c0b5de
GB
310 else
311 {
312 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
313 filename, write_files ?
314 O_RDWR | O_BINARY : O_RDONLY | O_BINARY,
e0cc99a6 315 &scratch_storage);
64c0b5de
GB
316#if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
317 if (scratch_chan < 0)
318 {
0ae1c716 319 char *exename = (char *) alloca (strlen (filename) + 5);
64c0b5de
GB
320
321 strcat (strcpy (exename, filename), ".exe");
322 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST,
323 exename, write_files ?
324 O_RDWR | O_BINARY
325 : O_RDONLY | O_BINARY,
e0cc99a6 326 &scratch_storage);
64c0b5de 327 }
c906108c 328#endif
64c0b5de
GB
329 if (scratch_chan < 0)
330 perror_with_name (filename);
a4453b7e 331
e0cc99a6 332 scratch_pathname = scratch_storage.get ();
a4453b7e 333
64c0b5de
GB
334 /* gdb_bfd_open (and its variants) prefers canonicalized
335 pathname for better BFD caching. */
14278e1f
TT
336 canonical_storage = gdb_realpath (scratch_pathname);
337 canonical_pathname = canonical_storage.get ();
64c0b5de 338 }
1f0c4988 339
192b62ce 340 gdb_bfd_ref_ptr temp;
64c0b5de 341 if (write_files && !load_via_target)
192b62ce
TT
342 temp = gdb_bfd_fopen (canonical_pathname, gnutarget,
343 FOPEN_RUB, scratch_chan);
1c00ec6b 344 else
192b62ce
TT
345 temp = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
346 exec_bfd = temp.release ();
c906108c
SS
347
348 if (!exec_bfd)
9fe4a216 349 {
d5131498 350 error (_("\"%s\": could not open as an executable file: %s."),
9fe4a216
TT
351 scratch_pathname, bfd_errmsg (bfd_get_error ()));
352 }
c906108c 353
64c0b5de
GB
354 /* gdb_realpath_keepfile resolves symlinks on the local
355 filesystem and so cannot be used for "target:" files. */
1f0c4988 356 gdb_assert (exec_filename == NULL);
64c0b5de
GB
357 if (load_via_target)
358 exec_filename = xstrdup (bfd_get_filename (exec_bfd));
359 else
4971c9a7 360 exec_filename = gdb_realpath_keepfile (scratch_pathname).release ();
1f0c4988 361
d18b8b7a 362 if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
c906108c
SS
363 {
364 /* Make sure to close exec_bfd, or else "run" might try to use
365 it. */
6c95b8df 366 exec_close ();
8a3fe4f8 367 error (_("\"%s\": not in executable format: %s"),
d18b8b7a
HZ
368 scratch_pathname,
369 gdb_bfd_errmsg (bfd_get_error (), matching));
c906108c
SS
370 }
371
07b82ea5 372 if (build_section_table (exec_bfd, &sections, &sections_end))
c906108c
SS
373 {
374 /* Make sure to close exec_bfd, or else "run" might try to use
375 it. */
6c95b8df 376 exec_close ();
8a3fe4f8 377 error (_("\"%s\": can't find the file sections: %s"),
c906108c
SS
378 scratch_pathname, bfd_errmsg (bfd_get_error ()));
379 }
380
c04ea773
DJ
381 exec_bfd_mtime = bfd_get_mtime (exec_bfd);
382
c906108c
SS
383 validate_files ();
384
385 set_gdbarch_from_file (exec_bfd);
386
07b82ea5 387 /* Add the executable's sections to the current address spaces'
6c95b8df
PA
388 list of sections. This possibly pushes the exec_ops
389 target. */
ed9eebaf 390 add_target_sections (&exec_bfd, sections, sections_end);
07b82ea5 391 xfree (sections);
c906108c
SS
392
393 /* Tell display code (if any) about the changed file name. */
9a4105ab
AC
394 if (deprecated_exec_file_display_hook)
395 (*deprecated_exec_file_display_hook) (filename);
c906108c 396 }
9b333ba3 397
ce7d4522 398 bfd_cache_close_all ();
76727919 399 gdb::observers::executable_changed.notify ();
c906108c
SS
400}
401
402/* Process the first arg in ARGS as the new exec file.
403
c5aa993b
JM
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()
1adeb98a
FN
406 which can take multiple args.
407
0963b4bd 408 If ARGS is NULL, we just want to close the exec file. */
c906108c 409
1adeb98a 410static void
1d8b34a7 411exec_file_command (const char *args, int from_tty)
c906108c 412{
4c42eaff
DJ
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."));
1adeb98a
FN
417
418 if (args)
419 {
420 /* Scan through the args and pick up the first non option arg
421 as the filename. */
422
773a1edc
TT
423 gdb_argv built_argv (args);
424 char **argv = built_argv.get ();
1adeb98a
FN
425
426 for (; (*argv != NULL) && (**argv == '-'); argv++)
427 {;
428 }
429 if (*argv == NULL)
8a3fe4f8 430 error (_("No executable file name was specified"));
1adeb98a 431
773a1edc
TT
432 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (*argv));
433 exec_file_attach (filename.get (), from_tty);
1adeb98a
FN
434 }
435 else
436 exec_file_attach (NULL, from_tty);
c906108c
SS
437}
438
0963b4bd 439/* Set both the exec file and the symbol file, in one command.
c906108c
SS
440 What a novelty. Why did GDB go through four major releases before this
441 command was added? */
442
443static void
1d8b34a7 444file_command (const char *arg, int from_tty)
c906108c
SS
445{
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);
9a4105ab
AC
450 if (deprecated_file_changed_hook)
451 deprecated_file_changed_hook (arg);
c906108c 452}
c906108c 453\f
c5aa993b 454
0963b4bd 455/* Locate all mappable sections of a BFD file.
c906108c
SS
456 table_pp_char is a char * to get it through bfd_map_over_sections;
457 we cast it back to its proper type. */
458
459static void
7be0c536
AC
460add_to_section_table (bfd *abfd, struct bfd_section *asect,
461 void *table_pp_char)
c906108c 462{
0542c86d 463 struct target_section **table_pp = (struct target_section **) table_pp_char;
c906108c
SS
464 flagword aflag;
465
2b2848e2
DE
466 gdb_assert (abfd == asect->owner);
467
0f5d55d8
JB
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. */
c906108c
SS
473 aflag = bfd_get_section_flags (abfd, asect);
474 if (!(aflag & SEC_ALLOC))
475 return;
0f5d55d8 476
046ac79f 477 (*table_pp)->owner = NULL;
c906108c
SS
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);
481 (*table_pp)++;
482}
483
a5b1fd27
DE
484/* See exec.h. */
485
486void
487clear_section_table (struct target_section_table *table)
488{
489 xfree (table->sections);
490 table->sections = table->sections_end = NULL;
491}
492
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. */
497
498static int
499resize_section_table (struct target_section_table *table, int adjustment)
07b82ea5 500{
07b82ea5
PA
501 int old_count;
502 int new_count;
503
07b82ea5
PA
504 old_count = table->sections_end - table->sections;
505
a5b1fd27 506 new_count = adjustment + old_count;
07b82ea5
PA
507
508 if (new_count)
509 {
224c3ddb
SM
510 table->sections = XRESIZEVEC (struct target_section, table->sections,
511 new_count);
07b82ea5
PA
512 table->sections_end = table->sections + new_count;
513 }
514 else
a5b1fd27 515 clear_section_table (table);
07b82ea5
PA
516
517 return old_count;
518}
519
c906108c
SS
520/* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
521 Returns 0 if OK, 1 on error. */
522
523int
0542c86d
PA
524build_section_table (struct bfd *some_bfd, struct target_section **start,
525 struct target_section **end)
c906108c
SS
526{
527 unsigned count;
528
529 count = bfd_count_sections (some_bfd);
530 if (*start)
b8c9b27d 531 xfree (* start);
8d749320 532 *start = XNEWVEC (struct target_section, count);
c906108c 533 *end = *start;
c5aa993b 534 bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
c906108c 535 if (*end > *start + count)
3e43a32a
MS
536 internal_error (__FILE__, __LINE__,
537 _("failed internal consistency check"));
c906108c
SS
538 /* We could realloc the table, but it probably loses for most files. */
539 return 0;
540}
07b82ea5
PA
541
542/* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
543 current set of target sections. */
544
545void
046ac79f 546add_target_sections (void *owner,
ed9eebaf 547 struct target_section *sections,
07b82ea5
PA
548 struct target_section *sections_end)
549{
550 int count;
551 struct target_section_table *table = current_target_sections;
552
553 count = sections_end - sections;
554
555 if (count > 0)
556 {
557 int space = resize_section_table (table, count);
ed9eebaf 558 int i;
d7f9d729 559
ed9eebaf
TT
560 for (i = 0; i < count; ++i)
561 {
562 table->sections[space + i] = sections[i];
046ac79f 563 table->sections[space + i].owner = owner;
ed9eebaf 564 }
07b82ea5
PA
565
566 /* If these are the first file sections we can provide memory
567 from, push the file_stratum target. */
ab16fce8
TT
568 if (!target_is_pushed (&exec_ops))
569 push_target (&exec_ops);
07b82ea5
PA
570 }
571}
572
76ad5e1e
NB
573/* Add the sections of OBJFILE to the current set of target sections. */
574
575void
576add_target_sections_of_objfile (struct objfile *objfile)
577{
578 struct target_section_table *table = current_target_sections;
579 struct obj_section *osect;
580 int space;
581 unsigned count = 0;
582 struct target_section *ts;
583
584 if (objfile == NULL)
585 return;
586
587 /* Compute the number of sections to add. */
588 ALL_OBJFILE_OSECTIONS (objfile, osect)
589 {
590 if (bfd_get_section_size (osect->the_bfd_section) == 0)
591 continue;
592 count++;
593 }
594
595 if (count == 0)
596 return;
597
598 space = resize_section_table (table, count);
599
600 ts = table->sections + space;
601
602 ALL_OBJFILE_OSECTIONS (objfile, osect)
603 {
604 if (bfd_get_section_size (osect->the_bfd_section) == 0)
605 continue;
606
607 gdb_assert (ts < table->sections + space + count);
608
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;
613
614 ts++;
615 }
616}
617
046ac79f
JK
618/* Remove all target sections owned by OWNER.
619 OWNER must be the same value passed to add_target_sections. */
07b82ea5
PA
620
621void
046ac79f 622remove_target_sections (void *owner)
07b82ea5
PA
623{
624 struct target_section *src, *dest;
07b82ea5
PA
625 struct target_section_table *table = current_target_sections;
626
046ac79f
JK
627 gdb_assert (owner != NULL);
628
07b82ea5
PA
629 dest = table->sections;
630 for (src = table->sections; src < table->sections_end; src++)
046ac79f 631 if (src->owner != owner)
07b82ea5
PA
632 {
633 /* Keep this section. */
634 if (dest < src)
635 *dest = *src;
636 dest++;
637 }
638
639 /* If we've dropped any sections, resize the section table. */
640 if (dest < src)
641 {
642 int old_count;
643
644 old_count = resize_section_table (table, dest - src);
645
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)
6c95b8df
PA
649 {
650 struct program_space *pspace;
651
652 ALL_PSPACES (pspace)
653 if (pspace->target_sections.sections
654 != pspace->target_sections.sections_end)
655 return;
656
657 unpush_target (&exec_ops);
658 }
07b82ea5
PA
659 }
660}
661
c906108c 662\f
348f8c02 663
1ca49d37
YQ
664enum target_xfer_status
665exec_read_partial_read_only (gdb_byte *readbuf, ULONGEST offset,
666 ULONGEST len, ULONGEST *xfered_len)
667{
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
670 QTro packet. */
671 if (exec_bfd != NULL)
672 {
673 asection *s;
674 bfd_size_type size;
675 bfd_vma vma;
676
677 for (s = exec_bfd->sections; s; s = s->next)
678 {
679 if ((s->flags & SEC_LOAD) == 0
680 || (s->flags & SEC_READONLY) == 0)
681 continue;
682
683 vma = s->vma;
684 size = bfd_get_section_size (s);
685 if (vma <= offset && offset < (vma + size))
686 {
687 ULONGEST amt;
688
689 amt = (vma + size) - offset;
690 if (amt > len)
691 amt = len;
692
693 amt = bfd_get_section_contents (exec_bfd, s,
694 readbuf, offset - vma, amt);
695
696 if (amt == 0)
697 return TARGET_XFER_EOF;
698 else
699 {
700 *xfered_len = amt;
701 return TARGET_XFER_OK;
702 }
703 }
704 }
705 }
706
707 /* Indicate failure to find the requested memory block. */
708 return TARGET_XFER_E_IO;
709}
710
a79b1bc6 711/* Return all read-only memory ranges found in the target section
5a2eb0ef 712 table defined by SECTIONS and SECTIONS_END, starting at (and
a79b1bc6 713 intersected with) MEMADDR for LEN bytes. */
5a2eb0ef 714
a79b1bc6
SM
715static std::vector<mem_range>
716section_table_available_memory (CORE_ADDR memaddr, ULONGEST len,
e6ca34fc
PA
717 struct target_section *sections,
718 struct target_section *sections_end)
719{
a79b1bc6 720 std::vector<mem_range> memory;
e6ca34fc 721
a79b1bc6 722 for (target_section *p = sections; p < sections_end; p++)
e6ca34fc 723 {
2b2848e2
DE
724 if ((bfd_get_section_flags (p->the_bfd_section->owner,
725 p->the_bfd_section)
e6ca34fc
PA
726 & SEC_READONLY) == 0)
727 continue;
728
729 /* Copy the meta-data, adjusted. */
730 if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len))
731 {
732 ULONGEST lo1, hi1, lo2, hi2;
e6ca34fc
PA
733
734 lo1 = memaddr;
735 hi1 = memaddr + len;
736
737 lo2 = p->addr;
738 hi2 = p->endaddr;
739
a79b1bc6
SM
740 CORE_ADDR start = std::max (lo1, lo2);
741 int length = std::min (hi1, hi2) - start;
e6ca34fc 742
a79b1bc6 743 memory.emplace_back (start, length);
e6ca34fc
PA
744 }
745 }
746
747 return memory;
748}
749
1ee79381
YQ
750enum target_xfer_status
751section_table_read_available_memory (gdb_byte *readbuf, ULONGEST offset,
752 ULONGEST len, ULONGEST *xfered_len)
753{
a79b1bc6
SM
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);
1ee79381 758
a79b1bc6 759 normalize_mem_ranges (&available_memory);
1ee79381 760
a79b1bc6 761 for (const mem_range &r : available_memory)
1ee79381 762 {
a79b1bc6 763 if (mem_ranges_overlap (r.start, r.length, offset, len))
1ee79381
YQ
764 {
765 CORE_ADDR end;
766 enum target_xfer_status status;
767
768 /* Get the intersection window. */
a79b1bc6 769 end = std::min<CORE_ADDR> (offset + len, r.start + r.length);
1ee79381
YQ
770
771 gdb_assert (end - offset <= len);
772
a79b1bc6 773 if (offset >= r.start)
1ee79381
YQ
774 status = exec_read_partial_read_only (readbuf, offset,
775 end - offset,
776 xfered_len);
777 else
778 {
a79b1bc6 779 *xfered_len = r.start - offset;
bc113b4e 780 status = TARGET_XFER_UNAVAILABLE;
1ee79381 781 }
1ee79381
YQ
782 return status;
783 }
784 }
1ee79381
YQ
785
786 *xfered_len = len;
bc113b4e 787 return TARGET_XFER_UNAVAILABLE;
1ee79381
YQ
788}
789
9b409511 790enum target_xfer_status
07b82ea5 791section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
b55e14c7 792 ULONGEST offset, ULONGEST len,
9b409511 793 ULONGEST *xfered_len,
07b82ea5
PA
794 struct target_section *sections,
795 struct target_section *sections_end,
796 const char *section_name)
c906108c 797{
020cc13c 798 int res;
0542c86d 799 struct target_section *p;
07b82ea5
PA
800 ULONGEST memaddr = offset;
801 ULONGEST memend = memaddr + len;
c906108c 802
b55e14c7 803 if (len == 0)
3e43a32a
MS
804 internal_error (__FILE__, __LINE__,
805 _("failed internal consistency check"));
c906108c 806
348f8c02 807 for (p = sections; p < sections_end; p++)
c906108c 808 {
2b2848e2
DE
809 struct bfd_section *asect = p->the_bfd_section;
810 bfd *abfd = asect->owner;
811
812 if (section_name && strcmp (section_name, asect->name) != 0)
0963b4bd 813 continue; /* not the section we need. */
c906108c 814 if (memaddr >= p->addr)
3db26b01
JB
815 {
816 if (memend <= p->endaddr)
817 {
818 /* Entire transfer is within this section. */
07b82ea5 819 if (writebuf)
2b2848e2 820 res = bfd_set_section_contents (abfd, asect,
07b82ea5 821 writebuf, memaddr - p->addr,
85302095
AC
822 len);
823 else
2b2848e2 824 res = bfd_get_section_contents (abfd, asect,
07b82ea5 825 readbuf, memaddr - p->addr,
85302095 826 len);
9b409511
YQ
827
828 if (res != 0)
829 {
830 *xfered_len = len;
831 return TARGET_XFER_OK;
832 }
833 else
834 return TARGET_XFER_EOF;
3db26b01
JB
835 }
836 else if (memaddr >= p->endaddr)
837 {
838 /* This section ends before the transfer starts. */
839 continue;
840 }
841 else
842 {
843 /* This section overlaps the transfer. Just do half. */
844 len = p->endaddr - memaddr;
07b82ea5 845 if (writebuf)
2b2848e2 846 res = bfd_set_section_contents (abfd, asect,
07b82ea5 847 writebuf, memaddr - p->addr,
85302095
AC
848 len);
849 else
2b2848e2 850 res = bfd_get_section_contents (abfd, asect,
07b82ea5 851 readbuf, memaddr - p->addr,
85302095 852 len);
9b409511
YQ
853 if (res != 0)
854 {
855 *xfered_len = len;
856 return TARGET_XFER_OK;
857 }
858 else
859 return TARGET_XFER_EOF;
3db26b01
JB
860 }
861 }
c906108c
SS
862 }
863
9b409511 864 return TARGET_XFER_EOF; /* We can't help. */
c906108c 865}
348f8c02 866
f6ac5f3d
PA
867struct target_section_table *
868exec_target::get_section_table ()
348f8c02 869{
07b82ea5 870 return current_target_sections;
348f8c02
PA
871}
872
f6ac5f3d
PA
873enum target_xfer_status
874exec_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)
348f8c02 878{
f6ac5f3d 879 struct target_section_table *table = get_section_table ();
07b82ea5
PA
880
881 if (object == TARGET_OBJECT_MEMORY)
882 return section_table_xfer_memory_partial (readbuf, writebuf,
9b409511 883 offset, len, xfered_len,
07b82ea5
PA
884 table->sections,
885 table->sections_end,
886 NULL);
887 else
2ed4b548 888 return TARGET_XFER_E_IO;
348f8c02 889}
c906108c 890\f
c5aa993b 891
c906108c 892void
07b82ea5 893print_section_info (struct target_section_table *t, bfd *abfd)
c906108c 894{
5af949e3 895 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
0542c86d 896 struct target_section *p;
17a912b6 897 /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */
5af949e3 898 int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
c906108c 899
c5aa993b 900 printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
c906108c 901 wrap_here (" ");
a3f17187 902 printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
c906108c 903 if (abfd == exec_bfd)
51bee8e9 904 {
3e43a32a
MS
905 /* gcc-3.4 does not like the initialization in
906 <p == t->sections_end>. */
d904de5b 907 bfd_vma displacement = 0;
2f1bdd26 908 bfd_vma entry_point;
51bee8e9
JK
909
910 for (p = t->sections; p < t->sections_end; p++)
911 {
2b2848e2
DE
912 struct bfd_section *psect = p->the_bfd_section;
913 bfd *pbfd = psect->owner;
51bee8e9 914
2b2848e2 915 if ((bfd_get_section_flags (pbfd, psect) & (SEC_ALLOC | SEC_LOAD))
51bee8e9
JK
916 != (SEC_ALLOC | SEC_LOAD))
917 continue;
918
2b2848e2
DE
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)))
51bee8e9 922 {
2b2848e2 923 displacement = p->addr - bfd_get_section_vma (pbfd, psect);
51bee8e9
JK
924 break;
925 }
926 }
927 if (p == t->sections_end)
b37520b6 928 warning (_("Cannot find section for the entry point of %s."),
d904de5b 929 bfd_get_filename (abfd));
51bee8e9 930
2f1bdd26
MGD
931 entry_point = gdbarch_addr_bits_remove (gdbarch,
932 bfd_get_start_address (abfd)
933 + displacement);
51bee8e9 934 printf_filtered (_("\tEntry point: %s\n"),
2f1bdd26 935 paddress (gdbarch, entry_point));
51bee8e9 936 }
07b82ea5 937 for (p = t->sections; p < t->sections_end; p++)
c906108c 938 {
2b2848e2
DE
939 struct bfd_section *psect = p->the_bfd_section;
940 bfd *pbfd = psect->owner;
941
bb599908
PH
942 printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
943 printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
bcf16802
KB
944
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. */
a3f17187 950 /* FIXME: i18n: Need to rewrite this sentence. */
c906108c
SS
951 if (info_verbose)
952 printf_filtered (" @ %s",
2b2848e2
DE
953 hex_string_custom (psect->filepos, 8));
954 printf_filtered (" is %s", bfd_section_name (pbfd, psect));
955 if (pbfd != abfd)
956 printf_filtered (" in %s", bfd_get_filename (pbfd));
c906108c
SS
957 printf_filtered ("\n");
958 }
959}
960
f6ac5f3d
PA
961void
962exec_target::files_info ()
c906108c 963{
57008375
JK
964 if (exec_bfd)
965 print_section_info (current_target_sections, exec_bfd);
966 else
967 puts_filtered (_("\t<no file loaded>\n"));
c906108c
SS
968}
969
970static void
0b39b52e 971set_section_command (const char *args, int from_tty)
c906108c 972{
0542c86d 973 struct target_section *p;
0b39b52e 974 const char *secname;
c906108c
SS
975 unsigned seclen;
976 unsigned long secaddr;
977 char secprint[100];
978 long offset;
07b82ea5 979 struct target_section_table *table;
c906108c
SS
980
981 if (args == 0)
8a3fe4f8 982 error (_("Must specify section name and its virtual address"));
c906108c 983
0963b4bd 984 /* Parse out section name. */
c5aa993b 985 for (secname = args; !isspace (*args); args++);
c906108c
SS
986 seclen = args - secname;
987
0963b4bd 988 /* Parse out new virtual address. */
c906108c
SS
989 secaddr = parse_and_eval_address (args);
990
07b82ea5
PA
991 table = current_target_sections;
992 for (p = table->sections; p < table->sections_end; p++)
c5aa993b 993 {
57008375 994 if (!strncmp (secname, bfd_section_name (p->bfd,
3e43a32a 995 p->the_bfd_section), seclen)
57008375 996 && bfd_section_name (p->bfd, p->the_bfd_section)[seclen] == '\0')
c5aa993b
JM
997 {
998 offset = secaddr - p->addr;
999 p->addr += offset;
1000 p->endaddr += offset;
1001 if (from_tty)
f6ac5f3d 1002 exec_ops.files_info ();
c5aa993b
JM
1003 return;
1004 }
c906108c 1005 }
c906108c
SS
1006 if (seclen >= sizeof (secprint))
1007 seclen = sizeof (secprint) - 1;
1008 strncpy (secprint, secname, seclen);
1009 secprint[seclen] = '\0';
8a3fe4f8 1010 error (_("Section %s not found"), secprint);
c906108c
SS
1011}
1012
30510692
DJ
1013/* If we can find a section in FILENAME with BFD index INDEX, adjust
1014 it to ADDRESS. */
c1bd25fd
DJ
1015
1016void
1017exec_set_section_address (const char *filename, int index, CORE_ADDR address)
1018{
0542c86d 1019 struct target_section *p;
07b82ea5 1020 struct target_section_table *table;
c1bd25fd 1021
07b82ea5
PA
1022 table = current_target_sections;
1023 for (p = table->sections; p < table->sections_end; p++)
c1bd25fd 1024 {
2b2848e2 1025 if (filename_cmp (filename, p->the_bfd_section->owner->filename) == 0
30510692 1026 && index == p->the_bfd_section->index)
c1bd25fd 1027 {
30510692 1028 p->endaddr += address - p->addr;
c1bd25fd 1029 p->addr = address;
c1bd25fd
DJ
1030 }
1031 }
1032}
1033
f6ac5f3d
PA
1034int
1035exec_target::has_memory ()
c35b1492
PA
1036{
1037 /* We can provide memory if we have any file/target sections to read
1038 from. */
1039 return (current_target_sections->sections
1040 != current_target_sections->sections_end);
1041}
1042
f6ac5f3d
PA
1043char *
1044exec_target::make_corefile_notes (bfd *obfd, int *note_size)
83814951
TT
1045{
1046 error (_("Can't create a corefile"));
1047}
be4d1333 1048
f6ac5f3d
PA
1049int
1050exec_target::find_memory_regions (find_memory_region_ftype func, void *data)
c906108c 1051{
f6ac5f3d 1052 return objfile_find_memory_regions (this, func, data);
c906108c
SS
1053}
1054
1055void
fba45db2 1056_initialize_exec (void)
c906108c
SS
1057{
1058 struct cmd_list_element *c;
1059
c906108c
SS
1060 if (!dbx_commands)
1061 {
1a966eab
AC
1062 c = add_cmd ("file", class_files, file_command, _("\
1063Use FILE as program to be debugged.\n\
c906108c
SS
1064It is read for its symbols, for getting the contents of pure memory,\n\
1065and it is the program executed when you use the `run' command.\n\
1066If FILE cannot be found as specified, your execution directory path\n\
1067($PATH) is searched for a command of that name.\n\
1a966eab 1068No arg means to have no executable file and no symbols."), &cmdlist);
5ba2abeb 1069 set_cmd_completer (c, filename_completer);
c906108c
SS
1070 }
1071
1a966eab
AC
1072 c = add_cmd ("exec-file", class_files, exec_file_command, _("\
1073Use FILE as program for getting contents of pure memory.\n\
c906108c
SS
1074If FILE cannot be found as specified, your execution directory path\n\
1075is searched for a command of that name.\n\
1a966eab 1076No arg means have no executable file."), &cmdlist);
5ba2abeb 1077 set_cmd_completer (c, filename_completer);
c906108c 1078
1bedd215
AC
1079 add_com ("section", class_files, set_section_command, _("\
1080Change the base address of section SECTION of the exec file to ADDR.\n\
c906108c
SS
1081This 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\
1083file itself are wrong. Each section must be changed separately. The\n\
1bedd215 1084``info files'' command lists all the sections and their addresses."));
c906108c 1085
5bf193a2
AC
1086 add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
1087Set writing into executable and core files."), _("\
1088Show writing into executable and core files."), NULL,
1089 NULL,
920d2a44 1090 show_write_files,
5bf193a2 1091 &setlist, &showlist);
c5aa993b 1092
9852c492 1093 add_target_with_completer (&exec_ops, filename_completer);
c906108c 1094}