1 /* Work with executable files, for GDB. 2 Copyright 1988, 1989, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. 3 4 This file is part of GDB. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 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 "symfile.h" 27 #include "objfiles.h" 28 29 #ifdef USG 30 #include <sys/types.h> 31 #endif 32 33 #include <fcntl.h> 34 #include "gdb_string.h" 35 36 #include "gdbcore.h" 37 38 #include <ctype.h> 39 #include "gdb_stat.h" 40 #ifndef O_BINARY 41 #define O_BINARY 0 42 #endif 43 44 #include "xcoffsolib.h" 45 46 struct vmap *map_vmap PARAMS ((bfd *, bfd *)); 47 48 /* Prototypes for local functions */ 49 50 static void add_to_section_table PARAMS ((bfd *, sec_ptr, PTR)); 51 52 static void exec_close PARAMS ((int)); 53 54 static void file_command PARAMS ((char *, int)); 55 56 static void set_section_command PARAMS ((char *, int)); 57 58 static void exec_files_info PARAMS ((struct target_ops *)); 59 60 static void bfdsec_to_vmap PARAMS ((bfd *, sec_ptr, PTR)); 61 62 static int ignore PARAMS ((CORE_ADDR, char *)); 63 64 extern int info_verbose; 65 66 /* The Binary File Descriptor handle for the executable file. */ 67 68 bfd *exec_bfd = NULL; 69 70 /* Whether to open exec and core files read-only or read-write. */ 71 72 int write_files = 0; 73 74 /* Text start and end addresses (KLUDGE) if needed */ 75 76 #ifdef NEED_TEXT_START_END 77 CORE_ADDR text_start = 0; 78 CORE_ADDR text_end = 0; 79 #endif 80 81 struct vmap *vmap; 82 83 /* Forward decl */ 84 85 extern struct target_ops exec_ops; 86 87 /* ARGSUSED */ 88 static void 89 exec_close (quitting) 90 int quitting; 91 { 92 int need_symtab_cleanup = 0; 93 struct vmap *vp, *nxt; 94 95 for (nxt = vmap; nxt != NULL; ) 96 { 97 vp = nxt; 98 nxt = vp->nxt; 99 100 /* if there is an objfile associated with this bfd, 101 free_objfile() will do proper cleanup of objfile *and* bfd. */ 102 103 if (vp->objfile) 104 { 105 free_objfile (vp->objfile); 106 need_symtab_cleanup = 1; 107 } 108 else if (vp->bfd != exec_bfd) 109 /* FIXME-leak: We should be freeing vp->name too, I think. */ 110 if (!bfd_close (vp->bfd)) 111 warning ("cannot close \"%s\": %s", 112 vp->name, bfd_errmsg (bfd_get_error ())); 113 114 /* FIXME: This routine is #if 0'd in symfile.c. What should we 115 be doing here? Should we just free everything in 116 vp->objfile->symtabs? Should free_objfile do that? 117 FIXME-as-well: free_objfile already free'd vp->name, so it isn't 118 valid here. */ 119 free_named_symtabs (vp->name); 120 free (vp); 121 } 122 123 vmap = NULL; 124 125 if (exec_bfd) 126 { 127 char *name = bfd_get_filename (exec_bfd); 128 129 if (!bfd_close (exec_bfd)) 130 warning ("cannot close \"%s\": %s", 131 name, bfd_errmsg (bfd_get_error ())); 132 free (name); 133 exec_bfd = NULL; 134 } 135 136 if (exec_ops.to_sections) 137 { 138 free ((PTR)exec_ops.to_sections); 139 exec_ops.to_sections = NULL; 140 exec_ops.to_sections_end = NULL; 141 } 142 } 143 144 /* Process the first arg in ARGS as the new exec file. 145 146 Note that we have to explicitly ignore additional args, since we can 147 be called from file_command(), which also calls symbol_file_command() 148 which can take multiple args. */ 149 150 void 151 exec_file_command (args, from_tty) 152 char *args; 153 int from_tty; 154 { 155 char **argv; 156 char *filename; 157 158 target_preopen (from_tty); 159 160 /* Remove any previous exec file. */ 161 unpush_target (&exec_ops); 162 163 /* Now open and digest the file the user requested, if any. */ 164 165 if (args) 166 { 167 char *scratch_pathname; 168 int scratch_chan; 169 170 /* Scan through the args and pick up the first non option arg 171 as the filename. */ 172 173 argv = buildargv (args); 174 if (argv == NULL) 175 nomem (0); 176 177 make_cleanup (freeargv, (char *) argv); 178 179 for (; (*argv != NULL) && (**argv == '-'); argv++) {;} 180 if (*argv == NULL) 181 error ("no exec file name was specified"); 182 183 filename = tilde_expand (*argv); 184 make_cleanup (free, filename); 185 186 scratch_chan = openp (getenv ("PATH"), 1, filename, 187 write_files? O_RDWR|O_BINARY: O_RDONLY|O_BINARY, 0, 188 &scratch_pathname); 189 #if defined(__GO32__) || defined(_WIN32) 190 if (scratch_chan < 0) 191 { 192 char *exename = alloca (strlen (filename) + 5); 193 strcat (strcpy (exename, filename), ".exe"); 194 scratch_chan = openp (getenv ("PATH"), 1, exename, write_files ? 195 O_RDWR|O_BINARY : O_RDONLY|O_BINARY, 0, &scratch_pathname); 196 } 197 #endif 198 if (scratch_chan < 0) 199 perror_with_name (filename); 200 exec_bfd = bfd_fdopenr (scratch_pathname, gnutarget, scratch_chan); 201 202 if (!exec_bfd) 203 error ("\"%s\": could not open as an executable file: %s", 204 scratch_pathname, bfd_errmsg (bfd_get_error ())); 205 206 /* At this point, scratch_pathname and exec_bfd->name both point to the 207 same malloc'd string. However exec_close() will attempt to free it 208 via the exec_bfd->name pointer, so we need to make another copy and 209 leave exec_bfd as the new owner of the original copy. */ 210 scratch_pathname = strdup (scratch_pathname); 211 make_cleanup (free, scratch_pathname); 212 213 if (!bfd_check_format (exec_bfd, bfd_object)) 214 { 215 /* Make sure to close exec_bfd, or else "run" might try to use 216 it. */ 217 exec_close (0); 218 error ("\"%s\": not in executable format: %s", 219 scratch_pathname, bfd_errmsg (bfd_get_error ())); 220 } 221 222 /* FIXME - This should only be run for RS6000, but the ifdef is a poor 223 way to accomplish. */ 224 #ifdef IBM6000_TARGET 225 /* Setup initial vmap. */ 226 227 map_vmap (exec_bfd, 0); 228 if (vmap == NULL) 229 { 230 /* Make sure to close exec_bfd, or else "run" might try to use 231 it. */ 232 exec_close (0); 233 error ("\"%s\": can't find the file sections: %s", 234 scratch_pathname, bfd_errmsg (bfd_get_error ())); 235 } 236 #endif /* IBM6000_TARGET */ 237 238 if (build_section_table (exec_bfd, &exec_ops.to_sections, 239 &exec_ops.to_sections_end)) 240 { 241 /* Make sure to close exec_bfd, or else "run" might try to use 242 it. */ 243 exec_close (0); 244 error ("\"%s\": can't find the file sections: %s", 245 scratch_pathname, bfd_errmsg (bfd_get_error ())); 246 } 247 248 #ifdef NEED_TEXT_START_END 249 250 /* text_end is sometimes used for where to put call dummies. A 251 few ports use these for other purposes too. */ 252 253 { 254 struct section_table *p; 255 256 /* Set text_start to the lowest address of the start of any 257 readonly code section and set text_end to the highest 258 address of the end of any readonly code section. */ 259 /* FIXME: The comment above does not match the code. The code 260 checks for sections with are either code *or* readonly. */ 261 262 text_start = ~(CORE_ADDR)0; 263 text_end = (CORE_ADDR)0; 264 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) 265 if (bfd_get_section_flags (p->bfd, p->the_bfd_section) 266 & (SEC_CODE | SEC_READONLY)) 267 { 268 if (text_start > p->addr) 269 text_start = p->addr; 270 if (text_end < p->endaddr) 271 text_end = p->endaddr; 272 } 273 } 274 #endif 275 276 validate_files (); 277 278 set_endian_from_file (exec_bfd); 279 280 push_target (&exec_ops); 281 282 /* Tell display code (if any) about the changed file name. */ 283 if (exec_file_display_hook) 284 (*exec_file_display_hook) (filename); 285 } 286 else if (from_tty) 287 printf_unfiltered ("No exec file now.\n"); 288 } 289 290 /* Set both the exec file and the symbol file, in one command. 291 What a novelty. Why did GDB go through four major releases before this 292 command was added? */ 293 294 static void 295 file_command (arg, from_tty) 296 char *arg; 297 int from_tty; 298 { 299 /* FIXME, if we lose on reading the symbol file, we should revert 300 the exec file, but that's rough. */ 301 exec_file_command (arg, from_tty); 302 symbol_file_command (arg, from_tty); 303 } 304 305 306 /* Locate all mappable sections of a BFD file. 307 table_pp_char is a char * to get it through bfd_map_over_sections; 308 we cast it back to its proper type. */ 309 310 static void 311 add_to_section_table (abfd, asect, table_pp_char) 312 bfd *abfd; 313 sec_ptr asect; 314 PTR table_pp_char; 315 { 316 struct section_table **table_pp = (struct section_table **)table_pp_char; 317 flagword aflag; 318 319 aflag = bfd_get_section_flags (abfd, asect); 320 if (!(aflag & SEC_ALLOC)) 321 return; 322 if (0 == bfd_section_size (abfd, asect)) 323 return; 324 (*table_pp)->bfd = abfd; 325 (*table_pp)->the_bfd_section = asect; 326 (*table_pp)->addr = bfd_section_vma (abfd, asect); 327 (*table_pp)->endaddr = (*table_pp)->addr + bfd_section_size (abfd, asect); 328 (*table_pp)++; 329 } 330 331 /* Builds a section table, given args BFD, SECTABLE_PTR, SECEND_PTR. 332 Returns 0 if OK, 1 on error. */ 333 334 int 335 build_section_table (some_bfd, start, end) 336 bfd *some_bfd; 337 struct section_table **start, **end; 338 { 339 unsigned count; 340 341 count = bfd_count_sections (some_bfd); 342 if (*start) 343 free ((PTR)*start); 344 *start = (struct section_table *) xmalloc (count * sizeof (**start)); 345 *end = *start; 346 bfd_map_over_sections (some_bfd, add_to_section_table, (char *)end); 347 if (*end > *start + count) 348 abort(); 349 /* We could realloc the table, but it probably loses for most files. */ 350 return 0; 351 } 352 353 static void 354 bfdsec_to_vmap(abfd, sect, arg3) 355 bfd *abfd; 356 sec_ptr sect; 357 PTR arg3; 358 { 359 struct vmap_and_bfd *vmap_bfd = (struct vmap_and_bfd *) arg3; 360 struct vmap *vp; 361 362 vp = vmap_bfd->pvmap; 363 364 if ((bfd_get_section_flags (abfd, sect) & SEC_LOAD) == 0) 365 return; 366 367 if (STREQ (bfd_section_name (abfd, sect), ".text")) 368 { 369 vp->tstart = 0; 370 vp->tend = vp->tstart + bfd_section_size (abfd, sect); 371 372 /* When it comes to this adjustment value, in contrast to our previous 373 belief shared objects should behave the same as the main load segment. 374 This is the offset from the beginning of text section to the first 375 real instruction. */ 376 377 vp->tadj = sect->filepos - bfd_section_vma (abfd, sect); 378 } 379 else if (STREQ (bfd_section_name (abfd, sect), ".data")) 380 { 381 vp->dstart = 0; 382 vp->dend = vp->dstart + bfd_section_size (abfd, sect); 383 } 384 /* Silently ignore other types of sections. (FIXME?) */ 385 } 386 387 /* Make a vmap for ABFD which might be a member of the archive ARCH. 388 Return the new vmap. */ 389 390 struct vmap * 391 map_vmap (abfd, arch) 392 bfd *abfd; 393 bfd *arch; 394 { 395 struct vmap_and_bfd vmap_bfd; 396 struct vmap *vp, **vpp; 397 398 vp = (struct vmap *) xmalloc (sizeof (*vp)); 399 memset ((char *) vp, '\0', sizeof (*vp)); 400 vp->nxt = 0; 401 vp->bfd = abfd; 402 vp->name = bfd_get_filename (arch ? arch : abfd); 403 vp->member = arch ? bfd_get_filename (abfd) : ""; 404 405 vmap_bfd.pbfd = arch; 406 vmap_bfd.pvmap = vp; 407 bfd_map_over_sections (abfd, bfdsec_to_vmap, &vmap_bfd); 408 409 /* Find the end of the list and append. */ 410 for (vpp = &vmap; *vpp; vpp = &(*vpp)->nxt) 411 ; 412 *vpp = vp; 413 414 return vp; 415 } 416 417 /* Read or write the exec file. 418 419 Args are address within a BFD file, address within gdb address-space, 420 length, and a flag indicating whether to read or write. 421 422 Result is a length: 423 424 0: We cannot handle this address and length. 425 > 0: We have handled N bytes starting at this address. 426 (If N == length, we did it all.) We might be able 427 to handle more bytes beyond this length, but no 428 promises. 429 < 0: We cannot handle this address, but if somebody 430 else handles (-N) bytes, we can start from there. 431 432 The same routine is used to handle both core and exec files; 433 we just tail-call it with more arguments to select between them. */ 434 435 int 436 xfer_memory (memaddr, myaddr, len, write, target) 437 CORE_ADDR memaddr; 438 char *myaddr; 439 int len; 440 int write; 441 struct target_ops *target; 442 { 443 boolean res; 444 struct section_table *p; 445 CORE_ADDR nextsectaddr, memend; 446 boolean (*xfer_fn) PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type)); 447 448 if (len <= 0) 449 abort(); 450 451 memend = memaddr + len; 452 xfer_fn = write ? bfd_set_section_contents : bfd_get_section_contents; 453 nextsectaddr = memend; 454 455 for (p = target->to_sections; p < target->to_sections_end; p++) 456 { 457 if (p->addr <= memaddr) 458 if (p->endaddr >= memend) 459 { 460 /* Entire transfer is within this section. */ 461 res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, 462 memaddr - p->addr, len); 463 return (res != 0) ? len : 0; 464 } 465 else if (p->endaddr <= memaddr) 466 { 467 /* This section ends before the transfer starts. */ 468 continue; 469 } 470 else 471 { 472 /* This section overlaps the transfer. Just do half. */ 473 len = p->endaddr - memaddr; 474 res = xfer_fn (p->bfd, p->the_bfd_section, myaddr, 475 memaddr - p->addr, len); 476 return (res != 0) ? len : 0; 477 } 478 else if (p->addr < nextsectaddr) 479 nextsectaddr = p->addr; 480 } 481 482 if (nextsectaddr >= memend) 483 return 0; /* We can't help */ 484 else 485 return - (nextsectaddr - memaddr); /* Next boundary where we can help */ 486 } 487 488 #ifdef FIXME 489 #ifdef REG_STACK_SEGMENT 490 /* MOVE TO BFD... */ 491 /* Pyramids and AM29000s have an extra segment in the virtual address space 492 for the (control) stack of register-window frames. The AM29000 folk 493 call it the "register stack" rather than the "memory stack". */ 494 else if (memaddr >= reg_stack_start && memaddr < reg_stack_end) 495 { 496 i = min (len, reg_stack_end - memaddr); 497 fileptr = memaddr - reg_stack_start + reg_stack_offset; 498 wanna_xfer = coredata; 499 } 500 #endif /* REG_STACK_SEGMENT */ 501 #endif /* FIXME */ 502 503 void 504 print_section_info (t, abfd) 505 struct target_ops *t; 506 bfd *abfd; 507 { 508 struct section_table *p; 509 510 printf_filtered ("\t`%s', ", bfd_get_filename(abfd)); 511 wrap_here (" "); 512 printf_filtered ("file type %s.\n", bfd_get_target(abfd)); 513 if (abfd == exec_bfd) 514 { 515 printf_filtered ("\tEntry point: "); 516 print_address_numeric (bfd_get_start_address (abfd), 1, gdb_stdout); 517 printf_filtered ("\n"); 518 } 519 for (p = t->to_sections; p < t->to_sections_end; p++) 520 { 521 /* FIXME-32x64 need a print_address_numeric with field width */ 522 printf_filtered ("\t%s", local_hex_string_custom ((unsigned long) p->addr, "08l")); 523 printf_filtered (" - %s", local_hex_string_custom ((unsigned long) p->endaddr, "08l")); 524 if (info_verbose) 525 printf_filtered (" @ %s", 526 local_hex_string_custom ((unsigned long) p->the_bfd_section->filepos, "08l")); 527 printf_filtered (" is %s", bfd_section_name (p->bfd, p->the_bfd_section)); 528 if (p->bfd != abfd) 529 { 530 printf_filtered (" in %s", bfd_get_filename (p->bfd)); 531 } 532 printf_filtered ("\n"); 533 } 534 } 535 536 static void 537 exec_files_info (t) 538 struct target_ops *t; 539 { 540 print_section_info (t, exec_bfd); 541 542 if (vmap) 543 { 544 struct vmap *vp; 545 546 printf_unfiltered ("\tMapping info for file `%s'.\n", vmap->name); 547 printf_unfiltered ("\t %8.8s %8.8s %8.8s %8.8s %8.8s %s\n", 548 "tstart", "tend", "dstart", "dend", "section", 549 "file(member)"); 550 551 for (vp = vmap; vp; vp = vp->nxt) 552 printf_unfiltered ("\t0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x %s%s%s%s\n", 553 vp->tstart, vp->tend, vp->dstart, vp->dend, vp->name, 554 *vp->member ? "(" : "", vp->member, 555 *vp->member ? ")" : ""); 556 } 557 } 558 559 static void 560 set_section_command (args, from_tty) 561 char *args; 562 int from_tty; 563 { 564 struct section_table *p; 565 char *secname; 566 unsigned seclen; 567 unsigned long secaddr; 568 char secprint[100]; 569 long offset; 570 571 if (args == 0) 572 error ("Must specify section name and its virtual address"); 573 574 /* Parse out section name */ 575 for (secname = args; !isspace(*args); args++) ; 576 seclen = args - secname; 577 578 /* Parse out new virtual address */ 579 secaddr = parse_and_eval_address (args); 580 581 for (p = exec_ops.to_sections; p < exec_ops.to_sections_end; p++) { 582 if (!strncmp (secname, bfd_section_name (exec_bfd, p->the_bfd_section), seclen) 583 && bfd_section_name (exec_bfd, p->the_bfd_section)[seclen] == '\0') { 584 offset = secaddr - p->addr; 585 p->addr += offset; 586 p->endaddr += offset; 587 if (from_tty) 588 exec_files_info(&exec_ops); 589 return; 590 } 591 } 592 if (seclen >= sizeof (secprint)) 593 seclen = sizeof (secprint) - 1; 594 strncpy (secprint, secname, seclen); 595 secprint[seclen] = '\0'; 596 error ("Section %s not found", secprint); 597 } 598 599 /* If mourn is being called in all the right places, this could be say 600 `gdb internal error' (since generic_mourn calls breakpoint_init_inferior). */ 601 602 static int 603 ignore (addr, contents) 604 CORE_ADDR addr; 605 char *contents; 606 { 607 return 0; 608 } 609 610 struct target_ops exec_ops = { 611 "exec", /* to_shortname */ 612 "Local exec file", /* to_longname */ 613 "Use an executable file as a target.\n\ 614 Specify the filename of the executable file.", /* to_doc */ 615 exec_file_command, /* to_open */ 616 exec_close, /* to_close */ 617 find_default_attach, /* to_attach */ 618 0, /* to_detach */ 619 0, /* to_resume */ 620 0, /* to_wait */ 621 0, /* to_fetch_registers */ 622 0, /* to_store_registers */ 623 0, /* to_prepare_to_store */ 624 xfer_memory, /* to_xfer_memory */ 625 exec_files_info, /* to_files_info */ 626 ignore, /* to_insert_breakpoint */ 627 ignore, /* to_remove_breakpoint */ 628 0, /* to_terminal_init */ 629 0, /* to_terminal_inferior */ 630 0, /* to_terminal_ours_for_output */ 631 0, /* to_terminal_ours */ 632 0, /* to_terminal_info */ 633 0, /* to_kill */ 634 0, /* to_load */ 635 0, /* to_lookup_symbol */ 636 find_default_create_inferior, /* to_create_inferior */ 637 0, /* to_mourn_inferior */ 638 0, /* to_can_run */ 639 0, /* to_notice_signals */ 640 0, /* to_thread_alive */ 641 0, /* to_stop */ 642 file_stratum, /* to_stratum */ 643 0, /* to_next */ 644 0, /* to_has_all_memory */ 645 1, /* to_has_memory */ 646 0, /* to_has_stack */ 647 0, /* to_has_registers */ 648 0, /* to_has_execution */ 649 0, /* to_sections */ 650 0, /* to_sections_end */ 651 OPS_MAGIC, /* to_magic */ 652 }; 653 654 void 655 _initialize_exec() 656 { 657 struct cmd_list_element *c; 658 659 c = add_cmd ("file", class_files, file_command, 660 "Use FILE as program to be debugged.\n\ 661 It is read for its symbols, for getting the contents of pure memory,\n\ 662 and it is the program executed when you use the `run' command.\n\ 663 If FILE cannot be found as specified, your execution directory path\n\ 664 ($PATH) is searched for a command of that name.\n\ 665 No arg means to have no executable file and no symbols.", &cmdlist); 666 c->completer = filename_completer; 667 668 c = add_cmd ("exec-file", class_files, exec_file_command, 669 "Use FILE as program for getting contents of pure memory.\n\ 670 If FILE cannot be found as specified, your execution directory path\n\ 671 is searched for a command of that name.\n\ 672 No arg means have no executable file.", &cmdlist); 673 c->completer = filename_completer; 674 675 add_com ("section", class_files, set_section_command, 676 "Change the base address of section SECTION of the exec file to ADDR.\n\ 677 This can be used if the exec file does not contain section addresses,\n\ 678 (such as in the a.out format), or when the addresses specified in the\n\ 679 file itself are wrong. Each section must be changed separately. The\n\ 680 ``info files'' command lists all the sections and their addresses."); 681 682 add_show_from_set 683 (add_set_cmd ("write", class_support, var_boolean, (char *)&write_files, 684 "Set writing into executable and core files.", 685 &setlist), 686 &showlist); 687 688 add_target (&exec_ops); 689 } 690