Convert struct target_ops to C++
[deliverable/binutils-gdb.git] / gdb / exec.c
1 /* Work with executable files, for GDB.
2
3 Copyright (C) 1988-2018 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
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"
26 #include "filenames.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "completer.h"
30 #include "value.h"
31 #include "exec.h"
32 #include "observable.h"
33 #include "arch-utils.h"
34 #include "gdbthread.h"
35 #include "progspace.h"
36 #include "gdb_bfd.h"
37 #include "gcore.h"
38 #include "source.h"
39
40 #include <fcntl.h>
41 #include "readline/readline.h"
42 #include "gdbcore.h"
43
44 #include <ctype.h>
45 #include <sys/stat.h>
46 #include "solist.h"
47 #include <algorithm>
48 #include "common/pathstuff.h"
49
50 void (*deprecated_file_changed_hook) (const char *);
51
52 /* The target vector for executable files. */
53
54 struct 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 _("\
68 Use an executable file as a target.\n\
69 Specify 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
88 static exec_target exec_ops;
89
90 /* Whether to open exec and core files read-only or read-write. */
91
92 int write_files = 0;
93 static void
94 show_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
101
102 void
103 exec_target::open (const char *args, int from_tty)
104 {
105 target_preopen (from_tty);
106 exec_file_attach (args, from_tty);
107 }
108
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
112 void
113 exec_close (void)
114 {
115 if (exec_bfd)
116 {
117 bfd *abfd = exec_bfd;
118
119 gdb_bfd_unref (abfd);
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
126 remove_target_sections (&exec_bfd);
127
128 xfree (exec_filename);
129 exec_filename = NULL;
130 }
131 }
132
133 /* This is the target_close implementation. Clears all target
134 sections and closes all executable bfds from all program spaces. */
135
136 void
137 exec_target::close ()
138 {
139 struct program_space *ss;
140 scoped_restore_current_program_space restore_pspace;
141
142 ALL_PSPACES (ss)
143 {
144 set_current_program_space (ss);
145 clear_section_table (current_target_sections);
146 exec_close ();
147 }
148 }
149
150 /* See gdbcore.h. */
151
152 void
153 try_open_exec_file (const char *exec_file_host, struct inferior *inf,
154 symfile_add_flags add_flags)
155 {
156 struct cleanup *old_chain;
157 struct gdb_exception prev_err = exception_none;
158
159 old_chain = make_cleanup (free_current_contents, &prev_err.message);
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.
168
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 {
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);
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. */
186 if (err.message != NULL)
187 prev_err.message = xstrdup (err.message);
188 }
189 END_CATCH
190
191 if (exec_file_host != NULL)
192 {
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
203 }
204
205 do_cleanups (old_chain);
206 }
207
208 /* See gdbcore.h. */
209
210 void
211 exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty)
212 {
213 char *exec_file_target;
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)
223 {
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;
229 }
230
231 gdb::unique_xmalloc_ptr<char> exec_file_host
232 = exec_file_find (exec_file_target, NULL);
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. */
241 try_open_exec_file (exec_file_host.get (), current_inferior (), add_flags);
242 }
243
244 /* Set FILENAME as the new exec file.
245
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.)
251
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.
254
255 And, it is used by child_attach, when the attach command was
256 given a pid but not a exec pathname, and the attach command could
257 figure out the pathname from the pid. (In this case, we shouldn't
258 ask the user whether the current target should be shut down --
259 we're supplying the exec pathname late for good reason.) */
260
261 void
262 exec_file_attach (const char *filename, int from_tty)
263 {
264 /* First, acquire a reference to the current exec_bfd. We release
265 this at the end of the function; but acquiring it now lets the
266 BFD cache return it if this call refers to the same file. */
267 gdb_bfd_ref_ptr exec_bfd_holder = gdb_bfd_ref_ptr::new_reference (exec_bfd);
268
269 /* Remove any previous exec file. */
270 exec_close ();
271
272 /* Now open and digest the file the user requested, if any. */
273
274 if (!filename)
275 {
276 if (from_tty)
277 printf_unfiltered (_("No executable file now.\n"));
278
279 set_gdbarch_from_file (NULL);
280 }
281 else
282 {
283 int load_via_target = 0;
284 const char *scratch_pathname, *canonical_pathname;
285 int scratch_chan;
286 struct target_section *sections = NULL, *sections_end = NULL;
287 char **matching;
288
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
297 gdb::unique_xmalloc_ptr<char> canonical_storage, scratch_storage;
298 if (load_via_target)
299 {
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
306 scratch_pathname = filename;
307 scratch_chan = -1;
308 canonical_pathname = scratch_pathname;
309 }
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,
315 &scratch_storage);
316 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__)
317 if (scratch_chan < 0)
318 {
319 char *exename = (char *) alloca (strlen (filename) + 5);
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,
326 &scratch_storage);
327 }
328 #endif
329 if (scratch_chan < 0)
330 perror_with_name (filename);
331
332 scratch_pathname = scratch_storage.get ();
333
334 /* gdb_bfd_open (and its variants) prefers canonicalized
335 pathname for better BFD caching. */
336 canonical_storage = gdb_realpath (scratch_pathname);
337 canonical_pathname = canonical_storage.get ();
338 }
339
340 gdb_bfd_ref_ptr temp;
341 if (write_files && !load_via_target)
342 temp = gdb_bfd_fopen (canonical_pathname, gnutarget,
343 FOPEN_RUB, scratch_chan);
344 else
345 temp = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
346 exec_bfd = temp.release ();
347
348 if (!exec_bfd)
349 {
350 error (_("\"%s\": could not open as an executable file: %s."),
351 scratch_pathname, bfd_errmsg (bfd_get_error ()));
352 }
353
354 /* gdb_realpath_keepfile resolves symlinks on the local
355 filesystem and so cannot be used for "target:" files. */
356 gdb_assert (exec_filename == NULL);
357 if (load_via_target)
358 exec_filename = xstrdup (bfd_get_filename (exec_bfd));
359 else
360 exec_filename = gdb_realpath_keepfile (scratch_pathname).release ();
361
362 if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching))
363 {
364 /* Make sure to close exec_bfd, or else "run" might try to use
365 it. */
366 exec_close ();
367 error (_("\"%s\": not in executable format: %s"),
368 scratch_pathname,
369 gdb_bfd_errmsg (bfd_get_error (), matching));
370 }
371
372 if (build_section_table (exec_bfd, &sections, &sections_end))
373 {
374 /* Make sure to close exec_bfd, or else "run" might try to use
375 it. */
376 exec_close ();
377 error (_("\"%s\": can't find the file sections: %s"),
378 scratch_pathname, bfd_errmsg (bfd_get_error ()));
379 }
380
381 exec_bfd_mtime = bfd_get_mtime (exec_bfd);
382
383 validate_files ();
384
385 set_gdbarch_from_file (exec_bfd);
386
387 /* Add the executable's sections to the current address spaces'
388 list of sections. This possibly pushes the exec_ops
389 target. */
390 add_target_sections (&exec_bfd, sections, sections_end);
391 xfree (sections);
392
393 /* Tell display code (if any) about the changed file name. */
394 if (deprecated_exec_file_display_hook)
395 (*deprecated_exec_file_display_hook) (filename);
396 }
397
398 bfd_cache_close_all ();
399 gdb::observers::executable_changed.notify ();
400 }
401
402 /* Process the first arg in ARGS as the new exec file.
403
404 Note that we have to explicitly ignore additional args, since we can
405 be called from file_command(), which also calls symbol_file_command()
406 which can take multiple args.
407
408 If ARGS is NULL, we just want to close the exec file. */
409
410 static void
411 exec_file_command (const char *args, int from_tty)
412 {
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."));
417
418 if (args)
419 {
420 /* Scan through the args and pick up the first non option arg
421 as the filename. */
422
423 gdb_argv built_argv (args);
424 char **argv = built_argv.get ();
425
426 for (; (*argv != NULL) && (**argv == '-'); argv++)
427 {;
428 }
429 if (*argv == NULL)
430 error (_("No executable file name was specified"));
431
432 gdb::unique_xmalloc_ptr<char> filename (tilde_expand (*argv));
433 exec_file_attach (filename.get (), from_tty);
434 }
435 else
436 exec_file_attach (NULL, from_tty);
437 }
438
439 /* Set both the exec file and the symbol file, in one command.
440 What a novelty. Why did GDB go through four major releases before this
441 command was added? */
442
443 static void
444 file_command (const char *arg, int from_tty)
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);
450 if (deprecated_file_changed_hook)
451 deprecated_file_changed_hook (arg);
452 }
453 \f
454
455 /* Locate all mappable sections of a BFD file.
456 table_pp_char is a char * to get it through bfd_map_over_sections;
457 we cast it back to its proper type. */
458
459 static void
460 add_to_section_table (bfd *abfd, struct bfd_section *asect,
461 void *table_pp_char)
462 {
463 struct target_section **table_pp = (struct target_section **) table_pp_char;
464 flagword aflag;
465
466 gdb_assert (abfd == asect->owner);
467
468 /* Check the section flags, but do not discard zero-length sections, since
469 some symbols may still be attached to this section. For instance, we
470 encountered on sparc-solaris 2.10 a shared library with an empty .bss
471 section to which a symbol named "_end" was attached. The address
472 of this symbol still needs to be relocated. */
473 aflag = bfd_get_section_flags (abfd, asect);
474 if (!(aflag & SEC_ALLOC))
475 return;
476
477 (*table_pp)->owner = NULL;
478 (*table_pp)->the_bfd_section = asect;
479 (*table_pp)->addr = bfd_section_vma (abfd, asect);
480 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect);
481 (*table_pp)++;
482 }
483
484 /* See exec.h. */
485
486 void
487 clear_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
498 static int
499 resize_section_table (struct target_section_table *table, int adjustment)
500 {
501 int old_count;
502 int new_count;
503
504 old_count = table->sections_end - table->sections;
505
506 new_count = adjustment + old_count;
507
508 if (new_count)
509 {
510 table->sections = XRESIZEVEC (struct target_section, table->sections,
511 new_count);
512 table->sections_end = table->sections + new_count;
513 }
514 else
515 clear_section_table (table);
516
517 return old_count;
518 }
519
520 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR.
521 Returns 0 if OK, 1 on error. */
522
523 int
524 build_section_table (struct bfd *some_bfd, struct target_section **start,
525 struct target_section **end)
526 {
527 unsigned count;
528
529 count = bfd_count_sections (some_bfd);
530 if (*start)
531 xfree (* start);
532 *start = XNEWVEC (struct target_section, count);
533 *end = *start;
534 bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end);
535 if (*end > *start + count)
536 internal_error (__FILE__, __LINE__,
537 _("failed internal consistency check"));
538 /* We could realloc the table, but it probably loses for most files. */
539 return 0;
540 }
541
542 /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the
543 current set of target sections. */
544
545 void
546 add_target_sections (void *owner,
547 struct target_section *sections,
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);
558 int i;
559
560 for (i = 0; i < count; ++i)
561 {
562 table->sections[space + i] = sections[i];
563 table->sections[space + i].owner = owner;
564 }
565
566 /* If these are the first file sections we can provide memory
567 from, push the file_stratum target. */
568 if (!target_is_pushed (&exec_ops))
569 push_target (&exec_ops);
570 }
571 }
572
573 /* Add the sections of OBJFILE to the current set of target sections. */
574
575 void
576 add_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
618 /* Remove all target sections owned by OWNER.
619 OWNER must be the same value passed to add_target_sections. */
620
621 void
622 remove_target_sections (void *owner)
623 {
624 struct target_section *src, *dest;
625 struct target_section_table *table = current_target_sections;
626
627 gdb_assert (owner != NULL);
628
629 dest = table->sections;
630 for (src = table->sections; src < table->sections_end; src++)
631 if (src->owner != owner)
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)
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 }
659 }
660 }
661
662 \f
663
664 enum target_xfer_status
665 exec_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
711 /* Return all read-only memory ranges found in the target section
712 table defined by SECTIONS and SECTIONS_END, starting at (and
713 intersected with) MEMADDR for LEN bytes. */
714
715 static std::vector<mem_range>
716 section_table_available_memory (CORE_ADDR memaddr, ULONGEST len,
717 struct target_section *sections,
718 struct target_section *sections_end)
719 {
720 std::vector<mem_range> memory;
721
722 for (target_section *p = sections; p < sections_end; p++)
723 {
724 if ((bfd_get_section_flags (p->the_bfd_section->owner,
725 p->the_bfd_section)
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;
733
734 lo1 = memaddr;
735 hi1 = memaddr + len;
736
737 lo2 = p->addr;
738 hi2 = p->endaddr;
739
740 CORE_ADDR start = std::max (lo1, lo2);
741 int length = std::min (hi1, hi2) - start;
742
743 memory.emplace_back (start, length);
744 }
745 }
746
747 return memory;
748 }
749
750 enum target_xfer_status
751 section_table_read_available_memory (gdb_byte *readbuf, ULONGEST offset,
752 ULONGEST len, ULONGEST *xfered_len)
753 {
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);
758
759 normalize_mem_ranges (&available_memory);
760
761 for (const mem_range &r : available_memory)
762 {
763 if (mem_ranges_overlap (r.start, r.length, offset, len))
764 {
765 CORE_ADDR end;
766 enum target_xfer_status status;
767
768 /* Get the intersection window. */
769 end = std::min<CORE_ADDR> (offset + len, r.start + r.length);
770
771 gdb_assert (end - offset <= len);
772
773 if (offset >= r.start)
774 status = exec_read_partial_read_only (readbuf, offset,
775 end - offset,
776 xfered_len);
777 else
778 {
779 *xfered_len = r.start - offset;
780 status = TARGET_XFER_UNAVAILABLE;
781 }
782 return status;
783 }
784 }
785
786 *xfered_len = len;
787 return TARGET_XFER_UNAVAILABLE;
788 }
789
790 enum target_xfer_status
791 section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf,
792 ULONGEST offset, ULONGEST len,
793 ULONGEST *xfered_len,
794 struct target_section *sections,
795 struct target_section *sections_end,
796 const char *section_name)
797 {
798 int res;
799 struct target_section *p;
800 ULONGEST memaddr = offset;
801 ULONGEST memend = memaddr + len;
802
803 if (len == 0)
804 internal_error (__FILE__, __LINE__,
805 _("failed internal consistency check"));
806
807 for (p = sections; p < sections_end; p++)
808 {
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)
813 continue; /* not the section we need. */
814 if (memaddr >= p->addr)
815 {
816 if (memend <= p->endaddr)
817 {
818 /* Entire transfer is within this section. */
819 if (writebuf)
820 res = bfd_set_section_contents (abfd, asect,
821 writebuf, memaddr - p->addr,
822 len);
823 else
824 res = bfd_get_section_contents (abfd, asect,
825 readbuf, memaddr - p->addr,
826 len);
827
828 if (res != 0)
829 {
830 *xfered_len = len;
831 return TARGET_XFER_OK;
832 }
833 else
834 return TARGET_XFER_EOF;
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;
845 if (writebuf)
846 res = bfd_set_section_contents (abfd, asect,
847 writebuf, memaddr - p->addr,
848 len);
849 else
850 res = bfd_get_section_contents (abfd, asect,
851 readbuf, memaddr - p->addr,
852 len);
853 if (res != 0)
854 {
855 *xfered_len = len;
856 return TARGET_XFER_OK;
857 }
858 else
859 return TARGET_XFER_EOF;
860 }
861 }
862 }
863
864 return TARGET_XFER_EOF; /* We can't help. */
865 }
866
867 struct target_section_table *
868 exec_target::get_section_table ()
869 {
870 return current_target_sections;
871 }
872
873 enum target_xfer_status
874 exec_target::xfer_partial (enum target_object object,
875 const char *annex, gdb_byte *readbuf,
876 const gdb_byte *writebuf,
877 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
878 {
879 struct target_section_table *table = get_section_table ();
880
881 if (object == TARGET_OBJECT_MEMORY)
882 return section_table_xfer_memory_partial (readbuf, writebuf,
883 offset, len, xfered_len,
884 table->sections,
885 table->sections_end,
886 NULL);
887 else
888 return TARGET_XFER_E_IO;
889 }
890 \f
891
892 void
893 print_section_info (struct target_section_table *t, bfd *abfd)
894 {
895 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
896 struct target_section *p;
897 /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */
898 int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16;
899
900 printf_filtered ("\t`%s', ", bfd_get_filename (abfd));
901 wrap_here (" ");
902 printf_filtered (_("file type %s.\n"), bfd_get_target (abfd));
903 if (abfd == exec_bfd)
904 {
905 /* gcc-3.4 does not like the initialization in
906 <p == t->sections_end>. */
907 bfd_vma displacement = 0;
908 bfd_vma entry_point;
909
910 for (p = t->sections; p < t->sections_end; p++)
911 {
912 struct bfd_section *psect = p->the_bfd_section;
913 bfd *pbfd = psect->owner;
914
915 if ((bfd_get_section_flags (pbfd, psect) & (SEC_ALLOC | SEC_LOAD))
916 != (SEC_ALLOC | SEC_LOAD))
917 continue;
918
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)))
922 {
923 displacement = p->addr - bfd_get_section_vma (pbfd, psect);
924 break;
925 }
926 }
927 if (p == t->sections_end)
928 warning (_("Cannot find section for the entry point of %s."),
929 bfd_get_filename (abfd));
930
931 entry_point = gdbarch_addr_bits_remove (gdbarch,
932 bfd_get_start_address (abfd)
933 + displacement);
934 printf_filtered (_("\tEntry point: %s\n"),
935 paddress (gdbarch, entry_point));
936 }
937 for (p = t->sections; p < t->sections_end; p++)
938 {
939 struct bfd_section *psect = p->the_bfd_section;
940 bfd *pbfd = psect->owner;
941
942 printf_filtered ("\t%s", hex_string_custom (p->addr, wid));
943 printf_filtered (" - %s", hex_string_custom (p->endaddr, wid));
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. */
950 /* FIXME: i18n: Need to rewrite this sentence. */
951 if (info_verbose)
952 printf_filtered (" @ %s",
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));
957 printf_filtered ("\n");
958 }
959 }
960
961 void
962 exec_target::files_info ()
963 {
964 if (exec_bfd)
965 print_section_info (current_target_sections, exec_bfd);
966 else
967 puts_filtered (_("\t<no file loaded>\n"));
968 }
969
970 static void
971 set_section_command (const char *args, int from_tty)
972 {
973 struct target_section *p;
974 const char *secname;
975 unsigned seclen;
976 unsigned long secaddr;
977 char secprint[100];
978 long offset;
979 struct target_section_table *table;
980
981 if (args == 0)
982 error (_("Must specify section name and its virtual address"));
983
984 /* Parse out section name. */
985 for (secname = args; !isspace (*args); args++);
986 seclen = args - secname;
987
988 /* Parse out new virtual address. */
989 secaddr = parse_and_eval_address (args);
990
991 table = current_target_sections;
992 for (p = table->sections; p < table->sections_end; p++)
993 {
994 if (!strncmp (secname, bfd_section_name (p->bfd,
995 p->the_bfd_section), seclen)
996 && bfd_section_name (p->bfd, p->the_bfd_section)[seclen] == '\0')
997 {
998 offset = secaddr - p->addr;
999 p->addr += offset;
1000 p->endaddr += offset;
1001 if (from_tty)
1002 exec_ops.files_info ();
1003 return;
1004 }
1005 }
1006 if (seclen >= sizeof (secprint))
1007 seclen = sizeof (secprint) - 1;
1008 strncpy (secprint, secname, seclen);
1009 secprint[seclen] = '\0';
1010 error (_("Section %s not found"), secprint);
1011 }
1012
1013 /* If we can find a section in FILENAME with BFD index INDEX, adjust
1014 it to ADDRESS. */
1015
1016 void
1017 exec_set_section_address (const char *filename, int index, CORE_ADDR address)
1018 {
1019 struct target_section *p;
1020 struct target_section_table *table;
1021
1022 table = current_target_sections;
1023 for (p = table->sections; p < table->sections_end; p++)
1024 {
1025 if (filename_cmp (filename, p->the_bfd_section->owner->filename) == 0
1026 && index == p->the_bfd_section->index)
1027 {
1028 p->endaddr += address - p->addr;
1029 p->addr = address;
1030 }
1031 }
1032 }
1033
1034 int
1035 exec_target::has_memory ()
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
1043 char *
1044 exec_target::make_corefile_notes (bfd *obfd, int *note_size)
1045 {
1046 error (_("Can't create a corefile"));
1047 }
1048
1049 int
1050 exec_target::find_memory_regions (find_memory_region_ftype func, void *data)
1051 {
1052 return objfile_find_memory_regions (this, func, data);
1053 }
1054
1055 void
1056 _initialize_exec (void)
1057 {
1058 struct cmd_list_element *c;
1059
1060 if (!dbx_commands)
1061 {
1062 c = add_cmd ("file", class_files, file_command, _("\
1063 Use FILE as program to be debugged.\n\
1064 It is read for its symbols, for getting the contents of pure memory,\n\
1065 and it is the program executed when you use the `run' command.\n\
1066 If FILE cannot be found as specified, your execution directory path\n\
1067 ($PATH) is searched for a command of that name.\n\
1068 No arg means to have no executable file and no symbols."), &cmdlist);
1069 set_cmd_completer (c, filename_completer);
1070 }
1071
1072 c = add_cmd ("exec-file", class_files, exec_file_command, _("\
1073 Use FILE as program for getting contents of pure memory.\n\
1074 If FILE cannot be found as specified, your execution directory path\n\
1075 is searched for a command of that name.\n\
1076 No arg means have no executable file."), &cmdlist);
1077 set_cmd_completer (c, filename_completer);
1078
1079 add_com ("section", class_files, set_section_command, _("\
1080 Change the base address of section SECTION of the exec file to ADDR.\n\
1081 This can be used if the exec file does not contain section addresses,\n\
1082 (such as in the a.out format), or when the addresses specified in the\n\
1083 file itself are wrong. Each section must be changed separately. The\n\
1084 ``info files'' command lists all the sections and their addresses."));
1085
1086 add_setshow_boolean_cmd ("write", class_support, &write_files, _("\
1087 Set writing into executable and core files."), _("\
1088 Show writing into executable and core files."), NULL,
1089 NULL,
1090 show_write_files,
1091 &setlist, &showlist);
1092
1093 add_target_with_completer (&exec_ops, filename_completer);
1094 }
This page took 0.105128 seconds and 5 git commands to generate.