1 /* Work with executable files, for GDB. 2 3 Copyright (C) 1988-2016 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 "observer.h" 33 #include "arch-utils.h" 34 #include "gdbthread.h" 35 #include "progspace.h" 36 #include "gdb_bfd.h" 37 #include "gcore.h" 38 39 #include <fcntl.h> 40 #include "readline/readline.h" 41 #include "gdbcore.h" 42 43 #include <ctype.h> 44 #include <sys/stat.h> 45 #include "solist.h" 46 47 void (*deprecated_file_changed_hook) (char *); 48 49 /* Prototypes for local functions */ 50 51 static void file_command (char *, int); 52 53 static void set_section_command (char *, int); 54 55 static void exec_files_info (struct target_ops *); 56 57 static void init_exec_ops (void); 58 59 void _initialize_exec (void); 60 61 /* The target vector for executable files. */ 62 63 static struct target_ops exec_ops; 64 65 /* Whether to open exec and core files read-only or read-write. */ 66 67 int write_files = 0; 68 static void 69 show_write_files (struct ui_file *file, int from_tty, 70 struct cmd_list_element *c, const char *value) 71 { 72 fprintf_filtered (file, _("Writing into executable and core files is %s.\n"), 73 value); 74 } 75 76 77 static void 78 exec_open (const char *args, int from_tty) 79 { 80 target_preopen (from_tty); 81 exec_file_attach (args, from_tty); 82 } 83 84 /* Close and clear exec_bfd. If we end up with no target sections to 85 read memory from, this unpushes the exec_ops target. */ 86 87 void 88 exec_close (void) 89 { 90 if (exec_bfd) 91 { 92 bfd *abfd = exec_bfd; 93 94 gdb_bfd_unref (abfd); 95 96 /* Removing target sections may close the exec_ops target. 97 Clear exec_bfd before doing so to prevent recursion. */ 98 exec_bfd = NULL; 99 exec_bfd_mtime = 0; 100 101 remove_target_sections (&exec_bfd); 102 103 xfree (exec_filename); 104 exec_filename = NULL; 105 } 106 } 107 108 /* This is the target_close implementation. Clears all target 109 sections and closes all executable bfds from all program spaces. */ 110 111 static void 112 exec_close_1 (struct target_ops *self) 113 { 114 struct program_space *ss; 115 struct cleanup *old_chain; 116 117 old_chain = save_current_program_space (); 118 ALL_PSPACES (ss) 119 { 120 set_current_program_space (ss); 121 clear_section_table (current_target_sections); 122 exec_close (); 123 } 124 125 do_cleanups (old_chain); 126 } 127 128 void 129 exec_file_clear (int from_tty) 130 { 131 /* Remove exec file. */ 132 exec_close (); 133 134 if (from_tty) 135 printf_unfiltered (_("No executable file now.\n")); 136 } 137 138 /* Returns non-zero if exceptions E1 and E2 are equal. Returns zero 139 otherwise. */ 140 141 static int 142 exception_print_same (struct gdb_exception e1, struct gdb_exception e2) 143 { 144 const char *msg1 = e1.message; 145 const char *msg2 = e2.message; 146 147 if (msg1 == NULL) 148 msg1 = ""; 149 if (msg2 == NULL) 150 msg2 = ""; 151 152 return (e1.reason == e2.reason 153 && e1.error == e2.error 154 && strcmp (e1.message, e2.message) == 0); 155 } 156 157 /* See gdbcore.h. */ 158 159 void 160 exec_file_locate_attach (int pid, int defer_bp_reset, int from_tty) 161 { 162 char *exec_file, *full_exec_path = NULL; 163 struct cleanup *old_chain; 164 struct gdb_exception prev_err = exception_none; 165 166 /* Do nothing if we already have an executable filename. */ 167 exec_file = (char *) get_exec_file (0); 168 if (exec_file != NULL) 169 return; 170 171 /* Try to determine a filename from the process itself. */ 172 exec_file = target_pid_to_exec_file (pid); 173 if (exec_file == NULL) 174 { 175 warning (_("No executable has been specified and target does not " 176 "support\n" 177 "determining executable automatically. " 178 "Try using the \"file\" command.")); 179 return; 180 } 181 182 /* If gdb_sysroot is not empty and the discovered filename 183 is absolute then prefix the filename with gdb_sysroot. */ 184 if (*gdb_sysroot != '\0' && IS_ABSOLUTE_PATH (exec_file)) 185 { 186 full_exec_path = exec_file_find (exec_file, NULL); 187 if (full_exec_path == NULL) 188 return; 189 } 190 else 191 { 192 /* It's possible we don't have a full path, but rather just a 193 filename. Some targets, such as HP-UX, don't provide the 194 full path, sigh. 195 196 Attempt to qualify the filename against the source path. 197 (If that fails, we'll just fall back on the original 198 filename. Not much more we can do...) */ 199 if (!source_full_path_of (exec_file, &full_exec_path)) 200 full_exec_path = xstrdup (exec_file); 201 } 202 203 old_chain = make_cleanup (xfree, full_exec_path); 204 make_cleanup (free_current_contents, &prev_err.message); 205 206 /* exec_file_attach and symbol_file_add_main may throw an error if the file 207 cannot be opened either locally or remotely. 208 209 This happens for example, when the file is first found in the local 210 sysroot (above), and then disappears (a TOCTOU race), or when it doesn't 211 exist in the target filesystem, or when the file does exist, but 212 is not readable. 213 214 Even without a symbol file, the remote-based debugging session should 215 continue normally instead of ending abruptly. Hence we catch thrown 216 errors/exceptions in the following code. */ 217 TRY 218 { 219 exec_file_attach (full_exec_path, from_tty); 220 } 221 CATCH (err, RETURN_MASK_ERROR) 222 { 223 if (err.message != NULL) 224 warning ("%s", err.message); 225 226 prev_err = err; 227 228 /* Save message so it doesn't get trashed by the catch below. */ 229 prev_err.message = xstrdup (err.message); 230 } 231 END_CATCH 232 233 TRY 234 { 235 if (defer_bp_reset) 236 current_inferior ()->symfile_flags |= SYMFILE_DEFER_BP_RESET; 237 symbol_file_add_main (full_exec_path, from_tty); 238 } 239 CATCH (err, RETURN_MASK_ERROR) 240 { 241 if (!exception_print_same (prev_err, err)) 242 warning ("%s", err.message); 243 } 244 END_CATCH 245 current_inferior ()->symfile_flags &= ~SYMFILE_DEFER_BP_RESET; 246 247 do_cleanups (old_chain); 248 } 249 250 /* Set FILENAME as the new exec file. 251 252 This function is intended to be behave essentially the same 253 as exec_file_command, except that the latter will detect when 254 a target is being debugged, and will ask the user whether it 255 should be shut down first. (If the answer is "no", then the 256 new file is ignored.) 257 258 This file is used by exec_file_command, to do the work of opening 259 and processing the exec file after any prompting has happened. 260 261 And, it is used by child_attach, when the attach command was 262 given a pid but not a exec pathname, and the attach command could 263 figure out the pathname from the pid. (In this case, we shouldn't 264 ask the user whether the current target should be shut down -- 265 we're supplying the exec pathname late for good reason.) */ 266 267 void 268 exec_file_attach (const char *filename, int from_tty) 269 { 270 struct cleanup *cleanups; 271 272 /* First, acquire a reference to the current exec_bfd. We release 273 this at the end of the function; but acquiring it now lets the 274 BFD cache return it if this call refers to the same file. */ 275 gdb_bfd_ref (exec_bfd); 276 cleanups = make_cleanup_bfd_unref (exec_bfd); 277 278 /* Remove any previous exec file. */ 279 exec_close (); 280 281 /* Now open and digest the file the user requested, if any. */ 282 283 if (!filename) 284 { 285 if (from_tty) 286 printf_unfiltered (_("No executable file now.\n")); 287 288 set_gdbarch_from_file (NULL); 289 } 290 else 291 { 292 int load_via_target = 0; 293 char *scratch_pathname, *canonical_pathname; 294 int scratch_chan; 295 struct target_section *sections = NULL, *sections_end = NULL; 296 char **matching; 297 298 if (is_target_filename (filename)) 299 { 300 if (target_filesystem_is_local ()) 301 filename += strlen (TARGET_SYSROOT_PREFIX); 302 else 303 load_via_target = 1; 304 } 305 306 if (load_via_target) 307 { 308 /* gdb_bfd_fopen does not support "target:" filenames. */ 309 if (write_files) 310 warning (_("writing into executable files is " 311 "not supported for %s sysroots"), 312 TARGET_SYSROOT_PREFIX); 313 314 scratch_pathname = xstrdup (filename); 315 make_cleanup (xfree, scratch_pathname); 316 317 scratch_chan = -1; 318 319 canonical_pathname = scratch_pathname; 320 } 321 else 322 { 323 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, 324 filename, write_files ? 325 O_RDWR | O_BINARY : O_RDONLY | O_BINARY, 326 &scratch_pathname); 327 #if defined(__GO32__) || defined(_WIN32) || defined(__CYGWIN__) 328 if (scratch_chan < 0) 329 { 330 char *exename = (char *) alloca (strlen (filename) + 5); 331 332 strcat (strcpy (exename, filename), ".exe"); 333 scratch_chan = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, 334 exename, write_files ? 335 O_RDWR | O_BINARY 336 : O_RDONLY | O_BINARY, 337 &scratch_pathname); 338 } 339 #endif 340 if (scratch_chan < 0) 341 perror_with_name (filename); 342 343 make_cleanup (xfree, scratch_pathname); 344 345 /* gdb_bfd_open (and its variants) prefers canonicalized 346 pathname for better BFD caching. */ 347 canonical_pathname = gdb_realpath (scratch_pathname); 348 make_cleanup (xfree, canonical_pathname); 349 } 350 351 if (write_files && !load_via_target) 352 exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget, 353 FOPEN_RUB, scratch_chan); 354 else 355 exec_bfd = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan); 356 357 if (!exec_bfd) 358 { 359 error (_("\"%s\": could not open as an executable file: %s."), 360 scratch_pathname, bfd_errmsg (bfd_get_error ())); 361 } 362 363 /* gdb_realpath_keepfile resolves symlinks on the local 364 filesystem and so cannot be used for "target:" files. */ 365 gdb_assert (exec_filename == NULL); 366 if (load_via_target) 367 exec_filename = xstrdup (bfd_get_filename (exec_bfd)); 368 else 369 exec_filename = gdb_realpath_keepfile (scratch_pathname); 370 371 if (!bfd_check_format_matches (exec_bfd, bfd_object, &matching)) 372 { 373 /* Make sure to close exec_bfd, or else "run" might try to use 374 it. */ 375 exec_close (); 376 error (_("\"%s\": not in executable format: %s"), 377 scratch_pathname, 378 gdb_bfd_errmsg (bfd_get_error (), matching)); 379 } 380 381 if (build_section_table (exec_bfd, §ions, §ions_end)) 382 { 383 /* Make sure to close exec_bfd, or else "run" might try to use 384 it. */ 385 exec_close (); 386 error (_("\"%s\": can't find the file sections: %s"), 387 scratch_pathname, bfd_errmsg (bfd_get_error ())); 388 } 389 390 exec_bfd_mtime = bfd_get_mtime (exec_bfd); 391 392 validate_files (); 393 394 set_gdbarch_from_file (exec_bfd); 395 396 /* Add the executable's sections to the current address spaces' 397 list of sections. This possibly pushes the exec_ops 398 target. */ 399 add_target_sections (&exec_bfd, sections, sections_end); 400 xfree (sections); 401 402 /* Tell display code (if any) about the changed file name. */ 403 if (deprecated_exec_file_display_hook) 404 (*deprecated_exec_file_display_hook) (filename); 405 } 406 407 do_cleanups (cleanups); 408 409 bfd_cache_close_all (); 410 observer_notify_executable_changed (); 411 } 412 413 /* Process the first arg in ARGS as the new exec file. 414 415 Note that we have to explicitly ignore additional args, since we can 416 be called from file_command(), which also calls symbol_file_command() 417 which can take multiple args. 418 419 If ARGS is NULL, we just want to close the exec file. */ 420 421 static void 422 exec_file_command (char *args, int from_tty) 423 { 424 char **argv; 425 char *filename; 426 427 if (from_tty && target_has_execution 428 && !query (_("A program is being debugged already.\n" 429 "Are you sure you want to change the file? "))) 430 error (_("File not changed.")); 431 432 if (args) 433 { 434 struct cleanup *cleanups; 435 436 /* Scan through the args and pick up the first non option arg 437 as the filename. */ 438 439 argv = gdb_buildargv (args); 440 cleanups = make_cleanup_freeargv (argv); 441 442 for (; (*argv != NULL) && (**argv == '-'); argv++) 443 {; 444 } 445 if (*argv == NULL) 446 error (_("No executable file name was specified")); 447 448 filename = tilde_expand (*argv); 449 make_cleanup (xfree, filename); 450 exec_file_attach (filename, from_tty); 451 452 do_cleanups (cleanups); 453 } 454 else 455 exec_file_attach (NULL, from_tty); 456 } 457 458 /* Set both the exec file and the symbol file, in one command. 459 What a novelty. Why did GDB go through four major releases before this 460 command was added? */ 461 462 static void 463 file_command (char *arg, int from_tty) 464 { 465 /* FIXME, if we lose on reading the symbol file, we should revert 466 the exec file, but that's rough. */ 467 exec_file_command (arg, from_tty); 468 symbol_file_command (arg, from_tty); 469 if (deprecated_file_changed_hook) 470 deprecated_file_changed_hook (arg); 471 } 472 473 474 /* Locate all mappable sections of a BFD file. 475 table_pp_char is a char * to get it through bfd_map_over_sections; 476 we cast it back to its proper type. */ 477 478 static void 479 add_to_section_table (bfd *abfd, struct bfd_section *asect, 480 void *table_pp_char) 481 { 482 struct target_section **table_pp = (struct target_section **) table_pp_char; 483 flagword aflag; 484 485 gdb_assert (abfd == asect->owner); 486 487 /* Check the section flags, but do not discard zero-length sections, since 488 some symbols may still be attached to this section. For instance, we 489 encountered on sparc-solaris 2.10 a shared library with an empty .bss 490 section to which a symbol named "_end" was attached. The address 491 of this symbol still needs to be relocated. */ 492 aflag = bfd_get_section_flags (abfd, asect); 493 if (!(aflag & SEC_ALLOC)) 494 return; 495 496 (*table_pp)->owner = NULL; 497 (*table_pp)->the_bfd_section = asect; 498 (*table_pp)->addr = bfd_section_vma (abfd, asect); 499 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect); 500 (*table_pp)++; 501 } 502 503 /* See exec.h. */ 504 505 void 506 clear_section_table (struct target_section_table *table) 507 { 508 xfree (table->sections); 509 table->sections = table->sections_end = NULL; 510 } 511 512 /* Resize section table TABLE by ADJUSTMENT. 513 ADJUSTMENT may be negative, in which case the caller must have already 514 removed the sections being deleted. 515 Returns the old size. */ 516 517 static int 518 resize_section_table (struct target_section_table *table, int adjustment) 519 { 520 int old_count; 521 int new_count; 522 523 old_count = table->sections_end - table->sections; 524 525 new_count = adjustment + old_count; 526 527 if (new_count) 528 { 529 table->sections = XRESIZEVEC (struct target_section, table->sections, 530 new_count); 531 table->sections_end = table->sections + new_count; 532 } 533 else 534 clear_section_table (table); 535 536 return old_count; 537 } 538 539 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR. 540 Returns 0 if OK, 1 on error. */ 541 542 int 543 build_section_table (struct bfd *some_bfd, struct target_section **start, 544 struct target_section **end) 545 { 546 unsigned count; 547 548 count = bfd_count_sections (some_bfd); 549 if (*start) 550 xfree (* start); 551 *start = XNEWVEC (struct target_section, count); 552 *end = *start; 553 bfd_map_over_sections (some_bfd, add_to_section_table, (char *) end); 554 if (*end > *start + count) 555 internal_error (__FILE__, __LINE__, 556 _("failed internal consistency check")); 557 /* We could realloc the table, but it probably loses for most files. */ 558 return 0; 559 } 560 561 /* Add the sections array defined by [SECTIONS..SECTIONS_END[ to the 562 current set of target sections. */ 563 564 void 565 add_target_sections (void *owner, 566 struct target_section *sections, 567 struct target_section *sections_end) 568 { 569 int count; 570 struct target_section_table *table = current_target_sections; 571 572 count = sections_end - sections; 573 574 if (count > 0) 575 { 576 int space = resize_section_table (table, count); 577 int i; 578 579 for (i = 0; i < count; ++i) 580 { 581 table->sections[space + i] = sections[i]; 582 table->sections[space + i].owner = owner; 583 } 584 585 /* If these are the first file sections we can provide memory 586 from, push the file_stratum target. */ 587 if (!target_is_pushed (&exec_ops)) 588 push_target (&exec_ops); 589 } 590 } 591 592 /* Add the sections of OBJFILE to the current set of target sections. */ 593 594 void 595 add_target_sections_of_objfile (struct objfile *objfile) 596 { 597 struct target_section_table *table = current_target_sections; 598 struct obj_section *osect; 599 int space; 600 unsigned count = 0; 601 struct target_section *ts; 602 603 if (objfile == NULL) 604 return; 605 606 /* Compute the number of sections to add. */ 607 ALL_OBJFILE_OSECTIONS (objfile, osect) 608 { 609 if (bfd_get_section_size (osect->the_bfd_section) == 0) 610 continue; 611 count++; 612 } 613 614 if (count == 0) 615 return; 616 617 space = resize_section_table (table, count); 618 619 ts = table->sections + space; 620 621 ALL_OBJFILE_OSECTIONS (objfile, osect) 622 { 623 if (bfd_get_section_size (osect->the_bfd_section) == 0) 624 continue; 625 626 gdb_assert (ts < table->sections + space + count); 627 628 ts->addr = obj_section_addr (osect); 629 ts->endaddr = obj_section_endaddr (osect); 630 ts->the_bfd_section = osect->the_bfd_section; 631 ts->owner = (void *) objfile; 632 633 ts++; 634 } 635 } 636 637 /* Remove all target sections owned by OWNER. 638 OWNER must be the same value passed to add_target_sections. */ 639 640 void 641 remove_target_sections (void *owner) 642 { 643 struct target_section *src, *dest; 644 struct target_section_table *table = current_target_sections; 645 646 gdb_assert (owner != NULL); 647 648 dest = table->sections; 649 for (src = table->sections; src < table->sections_end; src++) 650 if (src->owner != owner) 651 { 652 /* Keep this section. */ 653 if (dest < src) 654 *dest = *src; 655 dest++; 656 } 657 658 /* If we've dropped any sections, resize the section table. */ 659 if (dest < src) 660 { 661 int old_count; 662 663 old_count = resize_section_table (table, dest - src); 664 665 /* If we don't have any more sections to read memory from, 666 remove the file_stratum target from the stack. */ 667 if (old_count + (dest - src) == 0) 668 { 669 struct program_space *pspace; 670 671 ALL_PSPACES (pspace) 672 if (pspace->target_sections.sections 673 != pspace->target_sections.sections_end) 674 return; 675 676 unpush_target (&exec_ops); 677 } 678 } 679 } 680 681 682 683 enum target_xfer_status 684 exec_read_partial_read_only (gdb_byte *readbuf, ULONGEST offset, 685 ULONGEST len, ULONGEST *xfered_len) 686 { 687 /* It's unduly pedantic to refuse to look at the executable for 688 read-only pieces; so do the equivalent of readonly regions aka 689 QTro packet. */ 690 if (exec_bfd != NULL) 691 { 692 asection *s; 693 bfd_size_type size; 694 bfd_vma vma; 695 696 for (s = exec_bfd->sections; s; s = s->next) 697 { 698 if ((s->flags & SEC_LOAD) == 0 699 || (s->flags & SEC_READONLY) == 0) 700 continue; 701 702 vma = s->vma; 703 size = bfd_get_section_size (s); 704 if (vma <= offset && offset < (vma + size)) 705 { 706 ULONGEST amt; 707 708 amt = (vma + size) - offset; 709 if (amt > len) 710 amt = len; 711 712 amt = bfd_get_section_contents (exec_bfd, s, 713 readbuf, offset - vma, amt); 714 715 if (amt == 0) 716 return TARGET_XFER_EOF; 717 else 718 { 719 *xfered_len = amt; 720 return TARGET_XFER_OK; 721 } 722 } 723 } 724 } 725 726 /* Indicate failure to find the requested memory block. */ 727 return TARGET_XFER_E_IO; 728 } 729 730 /* Appends all read-only memory ranges found in the target section 731 table defined by SECTIONS and SECTIONS_END, starting at (and 732 intersected with) MEMADDR for LEN bytes. Returns the augmented 733 VEC. */ 734 735 static VEC(mem_range_s) * 736 section_table_available_memory (VEC(mem_range_s) *memory, 737 CORE_ADDR memaddr, ULONGEST len, 738 struct target_section *sections, 739 struct target_section *sections_end) 740 { 741 struct target_section *p; 742 743 for (p = sections; p < sections_end; p++) 744 { 745 if ((bfd_get_section_flags (p->the_bfd_section->owner, 746 p->the_bfd_section) 747 & SEC_READONLY) == 0) 748 continue; 749 750 /* Copy the meta-data, adjusted. */ 751 if (mem_ranges_overlap (p->addr, p->endaddr - p->addr, memaddr, len)) 752 { 753 ULONGEST lo1, hi1, lo2, hi2; 754 struct mem_range *r; 755 756 lo1 = memaddr; 757 hi1 = memaddr + len; 758 759 lo2 = p->addr; 760 hi2 = p->endaddr; 761 762 r = VEC_safe_push (mem_range_s, memory, NULL); 763 764 r->start = max (lo1, lo2); 765 r->length = min (hi1, hi2) - r->start; 766 } 767 } 768 769 return memory; 770 } 771 772 enum target_xfer_status 773 section_table_read_available_memory (gdb_byte *readbuf, ULONGEST offset, 774 ULONGEST len, ULONGEST *xfered_len) 775 { 776 VEC(mem_range_s) *available_memory = NULL; 777 struct target_section_table *table; 778 struct cleanup *old_chain; 779 mem_range_s *r; 780 int i; 781 782 table = target_get_section_table (&exec_ops); 783 available_memory = section_table_available_memory (available_memory, 784 offset, len, 785 table->sections, 786 table->sections_end); 787 788 old_chain = make_cleanup (VEC_cleanup(mem_range_s), 789 &available_memory); 790 791 normalize_mem_ranges (available_memory); 792 793 for (i = 0; 794 VEC_iterate (mem_range_s, available_memory, i, r); 795 i++) 796 { 797 if (mem_ranges_overlap (r->start, r->length, offset, len)) 798 { 799 CORE_ADDR end; 800 enum target_xfer_status status; 801 802 /* Get the intersection window. */ 803 end = min (offset + len, r->start + r->length); 804 805 gdb_assert (end - offset <= len); 806 807 if (offset >= r->start) 808 status = exec_read_partial_read_only (readbuf, offset, 809 end - offset, 810 xfered_len); 811 else 812 { 813 *xfered_len = r->start - offset; 814 status = TARGET_XFER_UNAVAILABLE; 815 } 816 do_cleanups (old_chain); 817 return status; 818 } 819 } 820 do_cleanups (old_chain); 821 822 *xfered_len = len; 823 return TARGET_XFER_UNAVAILABLE; 824 } 825 826 enum target_xfer_status 827 section_table_xfer_memory_partial (gdb_byte *readbuf, const gdb_byte *writebuf, 828 ULONGEST offset, ULONGEST len, 829 ULONGEST *xfered_len, 830 struct target_section *sections, 831 struct target_section *sections_end, 832 const char *section_name) 833 { 834 int res; 835 struct target_section *p; 836 ULONGEST memaddr = offset; 837 ULONGEST memend = memaddr + len; 838 839 if (len == 0) 840 internal_error (__FILE__, __LINE__, 841 _("failed internal consistency check")); 842 843 for (p = sections; p < sections_end; p++) 844 { 845 struct bfd_section *asect = p->the_bfd_section; 846 bfd *abfd = asect->owner; 847 848 if (section_name && strcmp (section_name, asect->name) != 0) 849 continue; /* not the section we need. */ 850 if (memaddr >= p->addr) 851 { 852 if (memend <= p->endaddr) 853 { 854 /* Entire transfer is within this section. */ 855 if (writebuf) 856 res = bfd_set_section_contents (abfd, asect, 857 writebuf, memaddr - p->addr, 858 len); 859 else 860 res = bfd_get_section_contents (abfd, asect, 861 readbuf, memaddr - p->addr, 862 len); 863 864 if (res != 0) 865 { 866 *xfered_len = len; 867 return TARGET_XFER_OK; 868 } 869 else 870 return TARGET_XFER_EOF; 871 } 872 else if (memaddr >= p->endaddr) 873 { 874 /* This section ends before the transfer starts. */ 875 continue; 876 } 877 else 878 { 879 /* This section overlaps the transfer. Just do half. */ 880 len = p->endaddr - memaddr; 881 if (writebuf) 882 res = bfd_set_section_contents (abfd, asect, 883 writebuf, memaddr - p->addr, 884 len); 885 else 886 res = bfd_get_section_contents (abfd, asect, 887 readbuf, memaddr - p->addr, 888 len); 889 if (res != 0) 890 { 891 *xfered_len = len; 892 return TARGET_XFER_OK; 893 } 894 else 895 return TARGET_XFER_EOF; 896 } 897 } 898 } 899 900 return TARGET_XFER_EOF; /* We can't help. */ 901 } 902 903 static struct target_section_table * 904 exec_get_section_table (struct target_ops *ops) 905 { 906 return current_target_sections; 907 } 908 909 static enum target_xfer_status 910 exec_xfer_partial (struct target_ops *ops, enum target_object object, 911 const char *annex, gdb_byte *readbuf, 912 const gdb_byte *writebuf, 913 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) 914 { 915 struct target_section_table *table = target_get_section_table (ops); 916 917 if (object == TARGET_OBJECT_MEMORY) 918 return section_table_xfer_memory_partial (readbuf, writebuf, 919 offset, len, xfered_len, 920 table->sections, 921 table->sections_end, 922 NULL); 923 else 924 return TARGET_XFER_E_IO; 925 } 926 927 928 void 929 print_section_info (struct target_section_table *t, bfd *abfd) 930 { 931 struct gdbarch *gdbarch = gdbarch_from_bfd (abfd); 932 struct target_section *p; 933 /* FIXME: 16 is not wide enough when gdbarch_addr_bit > 64. */ 934 int wid = gdbarch_addr_bit (gdbarch) <= 32 ? 8 : 16; 935 936 printf_filtered ("\t`%s', ", bfd_get_filename (abfd)); 937 wrap_here (" "); 938 printf_filtered (_("file type %s.\n"), bfd_get_target (abfd)); 939 if (abfd == exec_bfd) 940 { 941 /* gcc-3.4 does not like the initialization in 942 <p == t->sections_end>. */ 943 bfd_vma displacement = 0; 944 bfd_vma entry_point; 945 946 for (p = t->sections; p < t->sections_end; p++) 947 { 948 struct bfd_section *psect = p->the_bfd_section; 949 bfd *pbfd = psect->owner; 950 951 if ((bfd_get_section_flags (pbfd, psect) & (SEC_ALLOC | SEC_LOAD)) 952 != (SEC_ALLOC | SEC_LOAD)) 953 continue; 954 955 if (bfd_get_section_vma (pbfd, psect) <= abfd->start_address 956 && abfd->start_address < (bfd_get_section_vma (pbfd, psect) 957 + bfd_get_section_size (psect))) 958 { 959 displacement = p->addr - bfd_get_section_vma (pbfd, psect); 960 break; 961 } 962 } 963 if (p == t->sections_end) 964 warning (_("Cannot find section for the entry point of %s."), 965 bfd_get_filename (abfd)); 966 967 entry_point = gdbarch_addr_bits_remove (gdbarch, 968 bfd_get_start_address (abfd) 969 + displacement); 970 printf_filtered (_("\tEntry point: %s\n"), 971 paddress (gdbarch, entry_point)); 972 } 973 for (p = t->sections; p < t->sections_end; p++) 974 { 975 struct bfd_section *psect = p->the_bfd_section; 976 bfd *pbfd = psect->owner; 977 978 printf_filtered ("\t%s", hex_string_custom (p->addr, wid)); 979 printf_filtered (" - %s", hex_string_custom (p->endaddr, wid)); 980 981 /* FIXME: A format of "08l" is not wide enough for file offsets 982 larger than 4GB. OTOH, making it "016l" isn't desirable either 983 since most output will then be much wider than necessary. It 984 may make sense to test the size of the file and choose the 985 format string accordingly. */ 986 /* FIXME: i18n: Need to rewrite this sentence. */ 987 if (info_verbose) 988 printf_filtered (" @ %s", 989 hex_string_custom (psect->filepos, 8)); 990 printf_filtered (" is %s", bfd_section_name (pbfd, psect)); 991 if (pbfd != abfd) 992 printf_filtered (" in %s", bfd_get_filename (pbfd)); 993 printf_filtered ("\n"); 994 } 995 } 996 997 static void 998 exec_files_info (struct target_ops *t) 999 { 1000 if (exec_bfd) 1001 print_section_info (current_target_sections, exec_bfd); 1002 else 1003 puts_filtered (_("\t<no file loaded>\n")); 1004 } 1005 1006 static void 1007 set_section_command (char *args, int from_tty) 1008 { 1009 struct target_section *p; 1010 char *secname; 1011 unsigned seclen; 1012 unsigned long secaddr; 1013 char secprint[100]; 1014 long offset; 1015 struct target_section_table *table; 1016 1017 if (args == 0) 1018 error (_("Must specify section name and its virtual address")); 1019 1020 /* Parse out section name. */ 1021 for (secname = args; !isspace (*args); args++); 1022 seclen = args - secname; 1023 1024 /* Parse out new virtual address. */ 1025 secaddr = parse_and_eval_address (args); 1026 1027 table = current_target_sections; 1028 for (p = table->sections; p < table->sections_end; p++) 1029 { 1030 if (!strncmp (secname, bfd_section_name (p->bfd, 1031 p->the_bfd_section), seclen) 1032 && bfd_section_name (p->bfd, p->the_bfd_section)[seclen] == '\0') 1033 { 1034 offset = secaddr - p->addr; 1035 p->addr += offset; 1036 p->endaddr += offset; 1037 if (from_tty) 1038 exec_files_info (&exec_ops); 1039 return; 1040 } 1041 } 1042 if (seclen >= sizeof (secprint)) 1043 seclen = sizeof (secprint) - 1; 1044 strncpy (secprint, secname, seclen); 1045 secprint[seclen] = '\0'; 1046 error (_("Section %s not found"), secprint); 1047 } 1048 1049 /* If we can find a section in FILENAME with BFD index INDEX, adjust 1050 it to ADDRESS. */ 1051 1052 void 1053 exec_set_section_address (const char *filename, int index, CORE_ADDR address) 1054 { 1055 struct target_section *p; 1056 struct target_section_table *table; 1057 1058 table = current_target_sections; 1059 for (p = table->sections; p < table->sections_end; p++) 1060 { 1061 if (filename_cmp (filename, p->the_bfd_section->owner->filename) == 0 1062 && index == p->the_bfd_section->index) 1063 { 1064 p->endaddr += address - p->addr; 1065 p->addr = address; 1066 } 1067 } 1068 } 1069 1070 /* If mourn is being called in all the right places, this could be say 1071 `gdb internal error' (since generic_mourn calls 1072 breakpoint_init_inferior). */ 1073 1074 static int 1075 ignore (struct target_ops *ops, struct gdbarch *gdbarch, 1076 struct bp_target_info *bp_tgt) 1077 { 1078 return 0; 1079 } 1080 1081 /* Implement the to_remove_breakpoint method. */ 1082 1083 static int 1084 exec_remove_breakpoint (struct target_ops *ops, struct gdbarch *gdbarch, 1085 struct bp_target_info *bp_tgt, 1086 enum remove_bp_reason reason) 1087 { 1088 return 0; 1089 } 1090 1091 static int 1092 exec_has_memory (struct target_ops *ops) 1093 { 1094 /* We can provide memory if we have any file/target sections to read 1095 from. */ 1096 return (current_target_sections->sections 1097 != current_target_sections->sections_end); 1098 } 1099 1100 static char * 1101 exec_make_note_section (struct target_ops *self, bfd *obfd, int *note_size) 1102 { 1103 error (_("Can't create a corefile")); 1104 } 1105 1106 /* Fill in the exec file target vector. Very few entries need to be 1107 defined. */ 1108 1109 static void 1110 init_exec_ops (void) 1111 { 1112 exec_ops.to_shortname = "exec"; 1113 exec_ops.to_longname = "Local exec file"; 1114 exec_ops.to_doc = "Use an executable file as a target.\n\ 1115 Specify the filename of the executable file."; 1116 exec_ops.to_open = exec_open; 1117 exec_ops.to_close = exec_close_1; 1118 exec_ops.to_xfer_partial = exec_xfer_partial; 1119 exec_ops.to_get_section_table = exec_get_section_table; 1120 exec_ops.to_files_info = exec_files_info; 1121 exec_ops.to_insert_breakpoint = ignore; 1122 exec_ops.to_remove_breakpoint = exec_remove_breakpoint; 1123 exec_ops.to_stratum = file_stratum; 1124 exec_ops.to_has_memory = exec_has_memory; 1125 exec_ops.to_make_corefile_notes = exec_make_note_section; 1126 exec_ops.to_find_memory_regions = objfile_find_memory_regions; 1127 exec_ops.to_magic = OPS_MAGIC; 1128 } 1129 1130 void 1131 _initialize_exec (void) 1132 { 1133 struct cmd_list_element *c; 1134 1135 init_exec_ops (); 1136 1137 if (!dbx_commands) 1138 { 1139 c = add_cmd ("file", class_files, file_command, _("\ 1140 Use FILE as program to be debugged.\n\ 1141 It is read for its symbols, for getting the contents of pure memory,\n\ 1142 and it is the program executed when you use the `run' command.\n\ 1143 If FILE cannot be found as specified, your execution directory path\n\ 1144 ($PATH) is searched for a command of that name.\n\ 1145 No arg means to have no executable file and no symbols."), &cmdlist); 1146 set_cmd_completer (c, filename_completer); 1147 } 1148 1149 c = add_cmd ("exec-file", class_files, exec_file_command, _("\ 1150 Use FILE as program for getting contents of pure memory.\n\ 1151 If FILE cannot be found as specified, your execution directory path\n\ 1152 is searched for a command of that name.\n\ 1153 No arg means have no executable file."), &cmdlist); 1154 set_cmd_completer (c, filename_completer); 1155 1156 add_com ("section", class_files, set_section_command, _("\ 1157 Change the base address of section SECTION of the exec file to ADDR.\n\ 1158 This can be used if the exec file does not contain section addresses,\n\ 1159 (such as in the a.out format), or when the addresses specified in the\n\ 1160 file itself are wrong. Each section must be changed separately. The\n\ 1161 ``info files'' command lists all the sections and their addresses.")); 1162 1163 add_setshow_boolean_cmd ("write", class_support, &write_files, _("\ 1164 Set writing into executable and core files."), _("\ 1165 Show writing into executable and core files."), NULL, 1166 NULL, 1167 show_write_files, 1168 &setlist, &showlist); 1169 1170 add_target_with_completer (&exec_ops, filename_completer); 1171 } 1172